1 // SPDX-License-Identifier: GPL-2.0
3 * Greybus Component Authentication Protocol (CAP) Driver.
5 * Copyright 2016 Google Inc.
6 * Copyright 2016 Linaro Ltd.
9 #include <linux/greybus.h>
10 #include <linux/cdev.h>
12 #include <linux/ioctl.h>
13 #include <linux/uaccess.h>
15 #include "greybus_authentication.h"
18 #define CAP_TIMEOUT_MS 1000
21 * Number of minor devices this driver supports.
22 * There will be exactly one required per Interface.
24 #define NUM_MINORS U8_MAX
27 struct device
*parent
;
28 struct gb_connection
*connection
;
30 struct list_head node
;
31 bool disabled
; /* connection getting disabled */
35 struct device
*class_device
;
39 static struct class *cap_class
;
40 static dev_t cap_dev_num
;
41 static DEFINE_IDA(cap_minors_map
);
42 static LIST_HEAD(cap_list
);
43 static DEFINE_MUTEX(list_mutex
);
45 static void cap_kref_release(struct kref
*kref
)
47 struct gb_cap
*cap
= container_of(kref
, struct gb_cap
, kref
);
53 * All users of cap take a reference (from within list_mutex lock), before
54 * they get a pointer to play with. And the structure will be freed only after
55 * the last user has put the reference to it.
57 static void put_cap(struct gb_cap
*cap
)
59 kref_put(&cap
->kref
, cap_kref_release
);
62 /* Caller must call put_cap() after using struct gb_cap */
63 static struct gb_cap
*get_cap(struct cdev
*cdev
)
67 mutex_lock(&list_mutex
);
69 list_for_each_entry(cap
, &cap_list
, node
) {
70 if (&cap
->cdev
== cdev
) {
79 mutex_unlock(&list_mutex
);
84 static int cap_get_endpoint_uid(struct gb_cap
*cap
, u8
*euid
)
86 struct gb_connection
*connection
= cap
->connection
;
87 struct gb_cap_get_endpoint_uid_response response
;
90 ret
= gb_operation_sync(connection
, GB_CAP_TYPE_GET_ENDPOINT_UID
, NULL
,
91 0, &response
, sizeof(response
));
93 dev_err(cap
->parent
, "failed to get endpoint uid (%d)\n", ret
);
97 memcpy(euid
, response
.uid
, sizeof(response
.uid
));
102 static int cap_get_ims_certificate(struct gb_cap
*cap
, u32
class, u32 id
,
103 u8
*certificate
, u32
*size
, u8
*result
)
105 struct gb_connection
*connection
= cap
->connection
;
106 struct gb_cap_get_ims_certificate_request
*request
;
107 struct gb_cap_get_ims_certificate_response
*response
;
108 size_t max_size
= gb_operation_get_payload_size_max(connection
);
109 struct gb_operation
*op
;
112 op
= gb_operation_create_flags(connection
,
113 GB_CAP_TYPE_GET_IMS_CERTIFICATE
,
114 sizeof(*request
), max_size
,
115 GB_OPERATION_FLAG_SHORT_RESPONSE
,
120 request
= op
->request
->payload
;
121 request
->certificate_class
= cpu_to_le32(class);
122 request
->certificate_id
= cpu_to_le32(id
);
124 ret
= gb_operation_request_send_sync(op
);
126 dev_err(cap
->parent
, "failed to get certificate (%d)\n", ret
);
130 response
= op
->response
->payload
;
131 *result
= response
->result_code
;
132 *size
= op
->response
->payload_size
- sizeof(*response
);
133 memcpy(certificate
, response
->certificate
, *size
);
136 gb_operation_put(op
);
140 static int cap_authenticate(struct gb_cap
*cap
, u32 auth_type
, u8
*uid
,
141 u8
*challenge
, u8
*result
, u8
*auth_response
,
142 u32
*signature_size
, u8
*signature
)
144 struct gb_connection
*connection
= cap
->connection
;
145 struct gb_cap_authenticate_request
*request
;
146 struct gb_cap_authenticate_response
*response
;
147 size_t max_size
= gb_operation_get_payload_size_max(connection
);
148 struct gb_operation
*op
;
151 op
= gb_operation_create_flags(connection
, GB_CAP_TYPE_AUTHENTICATE
,
152 sizeof(*request
), max_size
,
153 GB_OPERATION_FLAG_SHORT_RESPONSE
,
158 request
= op
->request
->payload
;
159 request
->auth_type
= cpu_to_le32(auth_type
);
160 memcpy(request
->uid
, uid
, sizeof(request
->uid
));
161 memcpy(request
->challenge
, challenge
, sizeof(request
->challenge
));
163 ret
= gb_operation_request_send_sync(op
);
165 dev_err(cap
->parent
, "failed to authenticate (%d)\n", ret
);
169 response
= op
->response
->payload
;
170 *result
= response
->result_code
;
171 *signature_size
= op
->response
->payload_size
- sizeof(*response
);
172 memcpy(auth_response
, response
->response
, sizeof(response
->response
));
173 memcpy(signature
, response
->signature
, *signature_size
);
176 gb_operation_put(op
);
180 /* Char device fops */
182 static int cap_open(struct inode
*inode
, struct file
*file
)
184 struct gb_cap
*cap
= get_cap(inode
->i_cdev
);
186 /* cap structure can't get freed until file descriptor is closed */
188 file
->private_data
= cap
;
195 static int cap_release(struct inode
*inode
, struct file
*file
)
197 struct gb_cap
*cap
= file
->private_data
;
203 static int cap_ioctl(struct gb_cap
*cap
, unsigned int cmd
,
206 struct cap_ioc_get_endpoint_uid endpoint_uid
;
207 struct cap_ioc_get_ims_certificate
*ims_cert
;
208 struct cap_ioc_authenticate
*authenticate
;
213 case CAP_IOC_GET_ENDPOINT_UID
:
214 ret
= cap_get_endpoint_uid(cap
, endpoint_uid
.uid
);
218 if (copy_to_user(buf
, &endpoint_uid
, sizeof(endpoint_uid
)))
222 case CAP_IOC_GET_IMS_CERTIFICATE
:
223 size
= sizeof(*ims_cert
);
224 ims_cert
= memdup_user(buf
, size
);
225 if (IS_ERR(ims_cert
))
226 return PTR_ERR(ims_cert
);
228 ret
= cap_get_ims_certificate(cap
, ims_cert
->certificate_class
,
229 ims_cert
->certificate_id
,
230 ims_cert
->certificate
,
231 &ims_cert
->cert_size
,
232 &ims_cert
->result_code
);
233 if (!ret
&& copy_to_user(buf
, ims_cert
, size
))
238 case CAP_IOC_AUTHENTICATE
:
239 size
= sizeof(*authenticate
);
240 authenticate
= memdup_user(buf
, size
);
241 if (IS_ERR(authenticate
))
242 return PTR_ERR(authenticate
);
244 ret
= cap_authenticate(cap
, authenticate
->auth_type
,
246 authenticate
->challenge
,
247 &authenticate
->result_code
,
248 authenticate
->response
,
249 &authenticate
->signature_size
,
250 authenticate
->signature
);
251 if (!ret
&& copy_to_user(buf
, authenticate
, size
))
261 static long cap_ioctl_unlocked(struct file
*file
, unsigned int cmd
,
264 struct gb_cap
*cap
= file
->private_data
;
265 struct gb_bundle
*bundle
= cap
->connection
->bundle
;
271 * We don't want the user to do multiple authentication operations in
274 * This is also used to protect ->disabled, which is used to check if
275 * the connection is getting disconnected, so that we don't start any
278 mutex_lock(&cap
->mutex
);
279 if (!cap
->disabled
) {
280 ret
= gb_pm_runtime_get_sync(bundle
);
282 ret
= cap_ioctl(cap
, cmd
, (void __user
*)arg
);
283 gb_pm_runtime_put_autosuspend(bundle
);
286 mutex_unlock(&cap
->mutex
);
291 static const struct file_operations cap_fops
= {
292 .owner
= THIS_MODULE
,
294 .release
= cap_release
,
295 .unlocked_ioctl
= cap_ioctl_unlocked
,
298 int gb_cap_connection_init(struct gb_connection
*connection
)
306 cap
= kzalloc(sizeof(*cap
), GFP_KERNEL
);
310 cap
->parent
= &connection
->bundle
->dev
;
311 cap
->connection
= connection
;
312 mutex_init(&cap
->mutex
);
313 gb_connection_set_data(connection
, cap
);
314 kref_init(&cap
->kref
);
316 mutex_lock(&list_mutex
);
317 list_add(&cap
->node
, &cap_list
);
318 mutex_unlock(&list_mutex
);
320 ret
= gb_connection_enable(connection
);
324 minor
= ida_simple_get(&cap_minors_map
, 0, NUM_MINORS
, GFP_KERNEL
);
327 goto err_connection_disable
;
330 /* Add a char device to allow userspace to interact with cap */
331 cap
->dev_num
= MKDEV(MAJOR(cap_dev_num
), minor
);
332 cdev_init(&cap
->cdev
, &cap_fops
);
334 ret
= cdev_add(&cap
->cdev
, cap
->dev_num
, 1);
338 /* Add a soft link to the previously added char-dev within the bundle */
339 cap
->class_device
= device_create(cap_class
, cap
->parent
, cap
->dev_num
,
340 NULL
, "gb-authenticate-%d", minor
);
341 if (IS_ERR(cap
->class_device
)) {
342 ret
= PTR_ERR(cap
->class_device
);
349 cdev_del(&cap
->cdev
);
351 ida_simple_remove(&cap_minors_map
, minor
);
352 err_connection_disable
:
353 gb_connection_disable(connection
);
355 mutex_lock(&list_mutex
);
356 list_del(&cap
->node
);
357 mutex_unlock(&list_mutex
);
364 void gb_cap_connection_exit(struct gb_connection
*connection
)
371 cap
= gb_connection_get_data(connection
);
373 device_destroy(cap_class
, cap
->dev_num
);
374 cdev_del(&cap
->cdev
);
375 ida_simple_remove(&cap_minors_map
, MINOR(cap
->dev_num
));
378 * Disallow any new ioctl operations on the char device and wait for
379 * existing ones to finish.
381 mutex_lock(&cap
->mutex
);
382 cap
->disabled
= true;
383 mutex_unlock(&cap
->mutex
);
385 /* All pending greybus operations should have finished by now */
386 gb_connection_disable(cap
->connection
);
388 /* Disallow new users to get access to the cap structure */
389 mutex_lock(&list_mutex
);
390 list_del(&cap
->node
);
391 mutex_unlock(&list_mutex
);
394 * All current users of cap would have taken a reference to it by
395 * now, we can drop our reference and wait the last user will get
405 cap_class
= class_create(THIS_MODULE
, "gb_authenticate");
406 if (IS_ERR(cap_class
))
407 return PTR_ERR(cap_class
);
409 ret
= alloc_chrdev_region(&cap_dev_num
, 0, NUM_MINORS
,
412 goto err_remove_class
;
417 class_destroy(cap_class
);
423 unregister_chrdev_region(cap_dev_num
, NUM_MINORS
);
424 class_destroy(cap_class
);
425 ida_destroy(&cap_minors_map
);