1 // SPDX-License-Identifier: GPL-2.0
3 * Greybus Component Authentication Protocol (CAP) Driver.
5 * Copyright 2016 Google Inc.
6 * Copyright 2016 Linaro Ltd.
11 #include <linux/cdev.h>
13 #include <linux/ioctl.h>
14 #include <linux/uaccess.h>
16 #include "greybus_authentication.h"
19 #define CAP_TIMEOUT_MS 1000
22 * Number of minor devices this driver supports.
23 * There will be exactly one required per Interface.
25 #define NUM_MINORS U8_MAX
28 struct device
*parent
;
29 struct gb_connection
*connection
;
31 struct list_head node
;
32 bool disabled
; /* connection getting disabled */
36 struct device
*class_device
;
40 static struct class *cap_class
;
41 static dev_t cap_dev_num
;
42 static DEFINE_IDA(cap_minors_map
);
43 static LIST_HEAD(cap_list
);
44 static DEFINE_MUTEX(list_mutex
);
46 static void cap_kref_release(struct kref
*kref
)
48 struct gb_cap
*cap
= container_of(kref
, struct gb_cap
, kref
);
54 * All users of cap take a reference (from within list_mutex lock), before
55 * they get a pointer to play with. And the structure will be freed only after
56 * the last user has put the reference to it.
58 static void put_cap(struct gb_cap
*cap
)
60 kref_put(&cap
->kref
, cap_kref_release
);
63 /* Caller must call put_cap() after using struct gb_cap */
64 static struct gb_cap
*get_cap(struct cdev
*cdev
)
68 mutex_lock(&list_mutex
);
70 list_for_each_entry(cap
, &cap_list
, node
) {
71 if (&cap
->cdev
== cdev
) {
80 mutex_unlock(&list_mutex
);
85 static int cap_get_endpoint_uid(struct gb_cap
*cap
, u8
*euid
)
87 struct gb_connection
*connection
= cap
->connection
;
88 struct gb_cap_get_endpoint_uid_response response
;
91 ret
= gb_operation_sync(connection
, GB_CAP_TYPE_GET_ENDPOINT_UID
, NULL
,
92 0, &response
, sizeof(response
));
94 dev_err(cap
->parent
, "failed to get endpoint uid (%d)\n", ret
);
98 memcpy(euid
, response
.uid
, sizeof(response
.uid
));
103 static int cap_get_ims_certificate(struct gb_cap
*cap
, u32
class, u32 id
,
104 u8
*certificate
, u32
*size
, u8
*result
)
106 struct gb_connection
*connection
= cap
->connection
;
107 struct gb_cap_get_ims_certificate_request
*request
;
108 struct gb_cap_get_ims_certificate_response
*response
;
109 size_t max_size
= gb_operation_get_payload_size_max(connection
);
110 struct gb_operation
*op
;
113 op
= gb_operation_create_flags(connection
,
114 GB_CAP_TYPE_GET_IMS_CERTIFICATE
,
115 sizeof(*request
), max_size
,
116 GB_OPERATION_FLAG_SHORT_RESPONSE
,
121 request
= op
->request
->payload
;
122 request
->certificate_class
= cpu_to_le32(class);
123 request
->certificate_id
= cpu_to_le32(id
);
125 ret
= gb_operation_request_send_sync(op
);
127 dev_err(cap
->parent
, "failed to get certificate (%d)\n", ret
);
131 response
= op
->response
->payload
;
132 *result
= response
->result_code
;
133 *size
= op
->response
->payload_size
- sizeof(*response
);
134 memcpy(certificate
, response
->certificate
, *size
);
137 gb_operation_put(op
);
141 static int cap_authenticate(struct gb_cap
*cap
, u32 auth_type
, u8
*uid
,
142 u8
*challenge
, u8
*result
, u8
*auth_response
,
143 u32
*signature_size
, u8
*signature
)
145 struct gb_connection
*connection
= cap
->connection
;
146 struct gb_cap_authenticate_request
*request
;
147 struct gb_cap_authenticate_response
*response
;
148 size_t max_size
= gb_operation_get_payload_size_max(connection
);
149 struct gb_operation
*op
;
152 op
= gb_operation_create_flags(connection
, GB_CAP_TYPE_AUTHENTICATE
,
153 sizeof(*request
), max_size
,
154 GB_OPERATION_FLAG_SHORT_RESPONSE
,
159 request
= op
->request
->payload
;
160 request
->auth_type
= cpu_to_le32(auth_type
);
161 memcpy(request
->uid
, uid
, sizeof(request
->uid
));
162 memcpy(request
->challenge
, challenge
, sizeof(request
->challenge
));
164 ret
= gb_operation_request_send_sync(op
);
166 dev_err(cap
->parent
, "failed to authenticate (%d)\n", ret
);
170 response
= op
->response
->payload
;
171 *result
= response
->result_code
;
172 *signature_size
= op
->response
->payload_size
- sizeof(*response
);
173 memcpy(auth_response
, response
->response
, sizeof(response
->response
));
174 memcpy(signature
, response
->signature
, *signature_size
);
177 gb_operation_put(op
);
181 /* Char device fops */
183 static int cap_open(struct inode
*inode
, struct file
*file
)
185 struct gb_cap
*cap
= get_cap(inode
->i_cdev
);
187 /* cap structure can't get freed until file descriptor is closed */
189 file
->private_data
= cap
;
196 static int cap_release(struct inode
*inode
, struct file
*file
)
198 struct gb_cap
*cap
= file
->private_data
;
204 static int cap_ioctl(struct gb_cap
*cap
, unsigned int cmd
,
207 struct cap_ioc_get_endpoint_uid endpoint_uid
;
208 struct cap_ioc_get_ims_certificate
*ims_cert
;
209 struct cap_ioc_authenticate
*authenticate
;
214 case CAP_IOC_GET_ENDPOINT_UID
:
215 ret
= cap_get_endpoint_uid(cap
, endpoint_uid
.uid
);
219 if (copy_to_user(buf
, &endpoint_uid
, sizeof(endpoint_uid
)))
223 case CAP_IOC_GET_IMS_CERTIFICATE
:
224 size
= sizeof(*ims_cert
);
225 ims_cert
= memdup_user(buf
, size
);
226 if (IS_ERR(ims_cert
))
227 return PTR_ERR(ims_cert
);
229 ret
= cap_get_ims_certificate(cap
, ims_cert
->certificate_class
,
230 ims_cert
->certificate_id
,
231 ims_cert
->certificate
,
232 &ims_cert
->cert_size
,
233 &ims_cert
->result_code
);
234 if (!ret
&& copy_to_user(buf
, ims_cert
, size
))
239 case CAP_IOC_AUTHENTICATE
:
240 size
= sizeof(*authenticate
);
241 authenticate
= memdup_user(buf
, size
);
242 if (IS_ERR(authenticate
))
243 return PTR_ERR(authenticate
);
245 ret
= cap_authenticate(cap
, authenticate
->auth_type
,
247 authenticate
->challenge
,
248 &authenticate
->result_code
,
249 authenticate
->response
,
250 &authenticate
->signature_size
,
251 authenticate
->signature
);
252 if (!ret
&& copy_to_user(buf
, authenticate
, size
))
262 static long cap_ioctl_unlocked(struct file
*file
, unsigned int cmd
,
265 struct gb_cap
*cap
= file
->private_data
;
266 struct gb_bundle
*bundle
= cap
->connection
->bundle
;
272 * We don't want the user to do multiple authentication operations in
275 * This is also used to protect ->disabled, which is used to check if
276 * the connection is getting disconnected, so that we don't start any
279 mutex_lock(&cap
->mutex
);
280 if (!cap
->disabled
) {
281 ret
= gb_pm_runtime_get_sync(bundle
);
283 ret
= cap_ioctl(cap
, cmd
, (void __user
*)arg
);
284 gb_pm_runtime_put_autosuspend(bundle
);
287 mutex_unlock(&cap
->mutex
);
292 static const struct file_operations cap_fops
= {
293 .owner
= THIS_MODULE
,
295 .release
= cap_release
,
296 .unlocked_ioctl
= cap_ioctl_unlocked
,
299 int gb_cap_connection_init(struct gb_connection
*connection
)
307 cap
= kzalloc(sizeof(*cap
), GFP_KERNEL
);
311 cap
->parent
= &connection
->bundle
->dev
;
312 cap
->connection
= connection
;
313 mutex_init(&cap
->mutex
);
314 gb_connection_set_data(connection
, cap
);
315 kref_init(&cap
->kref
);
317 mutex_lock(&list_mutex
);
318 list_add(&cap
->node
, &cap_list
);
319 mutex_unlock(&list_mutex
);
321 ret
= gb_connection_enable(connection
);
325 minor
= ida_simple_get(&cap_minors_map
, 0, NUM_MINORS
, GFP_KERNEL
);
328 goto err_connection_disable
;
331 /* Add a char device to allow userspace to interact with cap */
332 cap
->dev_num
= MKDEV(MAJOR(cap_dev_num
), minor
);
333 cdev_init(&cap
->cdev
, &cap_fops
);
335 ret
= cdev_add(&cap
->cdev
, cap
->dev_num
, 1);
339 /* Add a soft link to the previously added char-dev within the bundle */
340 cap
->class_device
= device_create(cap_class
, cap
->parent
, cap
->dev_num
,
341 NULL
, "gb-authenticate-%d", minor
);
342 if (IS_ERR(cap
->class_device
)) {
343 ret
= PTR_ERR(cap
->class_device
);
350 cdev_del(&cap
->cdev
);
352 ida_simple_remove(&cap_minors_map
, minor
);
353 err_connection_disable
:
354 gb_connection_disable(connection
);
356 mutex_lock(&list_mutex
);
357 list_del(&cap
->node
);
358 mutex_unlock(&list_mutex
);
365 void gb_cap_connection_exit(struct gb_connection
*connection
)
372 cap
= gb_connection_get_data(connection
);
374 device_destroy(cap_class
, cap
->dev_num
);
375 cdev_del(&cap
->cdev
);
376 ida_simple_remove(&cap_minors_map
, MINOR(cap
->dev_num
));
379 * Disallow any new ioctl operations on the char device and wait for
380 * existing ones to finish.
382 mutex_lock(&cap
->mutex
);
383 cap
->disabled
= true;
384 mutex_unlock(&cap
->mutex
);
386 /* All pending greybus operations should have finished by now */
387 gb_connection_disable(cap
->connection
);
389 /* Disallow new users to get access to the cap structure */
390 mutex_lock(&list_mutex
);
391 list_del(&cap
->node
);
392 mutex_unlock(&list_mutex
);
395 * All current users of cap would have taken a reference to it by
396 * now, we can drop our reference and wait the last user will get
406 cap_class
= class_create(THIS_MODULE
, "gb_authenticate");
407 if (IS_ERR(cap_class
))
408 return PTR_ERR(cap_class
);
410 ret
= alloc_chrdev_region(&cap_dev_num
, 0, NUM_MINORS
,
413 goto err_remove_class
;
418 class_destroy(cap_class
);
424 unregister_chrdev_region(cap_dev_num
, NUM_MINORS
);
425 class_destroy(cap_class
);
426 ida_destroy(&cap_minors_map
);