2 * Greybus Component Authentication Protocol (CAP) Driver.
4 * Copyright 2016 Google Inc.
5 * Copyright 2016 Linaro Ltd.
7 * Released under the GPLv2 only.
12 #include <linux/cdev.h>
14 #include <linux/ioctl.h>
15 #include <linux/uaccess.h>
17 #include "greybus_authentication.h"
20 #define CAP_TIMEOUT_MS 1000
23 * Number of minor devices this driver supports.
24 * There will be exactly one required per Interface.
26 #define NUM_MINORS U8_MAX
29 struct device *parent;
30 struct gb_connection *connection;
32 struct list_head node;
33 bool disabled; /* connection getting disabled */
37 struct device *class_device;
41 static struct class *cap_class;
42 static dev_t cap_dev_num;
43 static DEFINE_IDA(cap_minors_map);
44 static LIST_HEAD(cap_list);
45 static DEFINE_MUTEX(list_mutex);
47 static void cap_kref_release(struct kref *kref)
49 struct gb_cap *cap = container_of(kref, struct gb_cap, kref);
55 * All users of cap take a reference (from within list_mutex lock), before
56 * they get a pointer to play with. And the structure will be freed only after
57 * the last user has put the reference to it.
59 static void put_cap(struct gb_cap *cap)
61 kref_put(&cap->kref, cap_kref_release);
64 /* Caller must call put_cap() after using struct gb_cap */
65 static struct gb_cap *get_cap(struct cdev *cdev)
69 mutex_lock(&list_mutex);
71 list_for_each_entry(cap, &cap_list, node) {
72 if (&cap->cdev == cdev) {
81 mutex_unlock(&list_mutex);
86 static int cap_get_endpoint_uid(struct gb_cap *cap, u8 *euid)
88 struct gb_connection *connection = cap->connection;
89 struct gb_cap_get_endpoint_uid_response response;
92 ret = gb_operation_sync(connection, GB_CAP_TYPE_GET_ENDPOINT_UID, NULL,
93 0, &response, sizeof(response));
95 dev_err(cap->parent, "failed to get endpoint uid (%d)\n", ret);
99 memcpy(euid, response.uid, sizeof(response.uid));
104 static int cap_get_ims_certificate(struct gb_cap *cap, u32 class, u32 id,
105 u8 *certificate, u32 *size, u8 *result)
107 struct gb_connection *connection = cap->connection;
108 struct gb_cap_get_ims_certificate_request *request;
109 struct gb_cap_get_ims_certificate_response *response;
110 size_t max_size = gb_operation_get_payload_size_max(connection);
111 struct gb_operation *op;
114 op = gb_operation_create_flags(connection,
115 GB_CAP_TYPE_GET_IMS_CERTIFICATE,
116 sizeof(*request), max_size,
117 GB_OPERATION_FLAG_SHORT_RESPONSE,
122 request = op->request->payload;
123 request->certificate_class = cpu_to_le32(class);
124 request->certificate_id = cpu_to_le32(id);
126 ret = gb_operation_request_send_sync(op);
128 dev_err(cap->parent, "failed to get certificate (%d)\n", ret);
132 response = op->response->payload;
133 *result = response->result_code;
134 *size = op->response->payload_size - sizeof(*response);
135 memcpy(certificate, response->certificate, *size);
138 gb_operation_put(op);
142 static int cap_authenticate(struct gb_cap *cap, u32 auth_type, u8 *uid,
143 u8 *challenge, u8 *result, u8 *auth_response,
144 u32 *signature_size, u8 *signature)
146 struct gb_connection *connection = cap->connection;
147 struct gb_cap_authenticate_request *request;
148 struct gb_cap_authenticate_response *response;
149 size_t max_size = gb_operation_get_payload_size_max(connection);
150 struct gb_operation *op;
153 op = gb_operation_create_flags(connection, GB_CAP_TYPE_AUTHENTICATE,
154 sizeof(*request), max_size,
155 GB_OPERATION_FLAG_SHORT_RESPONSE,
160 request = op->request->payload;
161 request->auth_type = cpu_to_le32(auth_type);
162 memcpy(request->uid, uid, sizeof(request->uid));
163 memcpy(request->challenge, challenge, sizeof(request->challenge));
165 ret = gb_operation_request_send_sync(op);
167 dev_err(cap->parent, "failed to authenticate (%d)\n", ret);
171 response = op->response->payload;
172 *result = response->result_code;
173 *signature_size = op->response->payload_size - sizeof(*response);
174 memcpy(auth_response, response->response, sizeof(response->response));
175 memcpy(signature, response->signature, *signature_size);
178 gb_operation_put(op);
182 /* Char device fops */
184 static int cap_open(struct inode *inode, struct file *file)
186 struct gb_cap *cap = get_cap(inode->i_cdev);
188 /* cap structure can't get freed until file descriptor is closed */
190 file->private_data = cap;
197 static int cap_release(struct inode *inode, struct file *file)
199 struct gb_cap *cap = file->private_data;
205 static int cap_ioctl(struct gb_cap *cap, unsigned int cmd,
208 struct cap_ioc_get_endpoint_uid endpoint_uid;
209 struct cap_ioc_get_ims_certificate *ims_cert;
210 struct cap_ioc_authenticate *authenticate;
215 case CAP_IOC_GET_ENDPOINT_UID:
216 ret = cap_get_endpoint_uid(cap, endpoint_uid.uid);
220 if (copy_to_user(buf, &endpoint_uid, sizeof(endpoint_uid)))
224 case CAP_IOC_GET_IMS_CERTIFICATE:
225 size = sizeof(*ims_cert);
226 ims_cert = memdup_user(buf, size);
227 if (IS_ERR(ims_cert))
228 return PTR_ERR(ims_cert);
230 ret = cap_get_ims_certificate(cap, ims_cert->certificate_class,
231 ims_cert->certificate_id,
232 ims_cert->certificate,
233 &ims_cert->cert_size,
234 &ims_cert->result_code);
235 if (!ret && copy_to_user(buf, ims_cert, size))
240 case CAP_IOC_AUTHENTICATE:
241 size = sizeof(*authenticate);
242 authenticate = memdup_user(buf, size);
243 if (IS_ERR(authenticate))
244 return PTR_ERR(authenticate);
246 ret = cap_authenticate(cap, authenticate->auth_type,
248 authenticate->challenge,
249 &authenticate->result_code,
250 authenticate->response,
251 &authenticate->signature_size,
252 authenticate->signature);
253 if (!ret && copy_to_user(buf, authenticate, size))
263 static long cap_ioctl_unlocked(struct file *file, unsigned int cmd,
266 struct gb_cap *cap = file->private_data;
267 struct gb_bundle *bundle = cap->connection->bundle;
273 * We don't want the user to do multiple authentication operations in
276 * This is also used to protect ->disabled, which is used to check if
277 * the connection is getting disconnected, so that we don't start any
280 mutex_lock(&cap->mutex);
281 if (!cap->disabled) {
282 ret = gb_pm_runtime_get_sync(bundle);
284 ret = cap_ioctl(cap, cmd, (void __user *)arg);
285 gb_pm_runtime_put_autosuspend(bundle);
288 mutex_unlock(&cap->mutex);
293 static const struct file_operations cap_fops = {
294 .owner = THIS_MODULE,
296 .release = cap_release,
297 .unlocked_ioctl = cap_ioctl_unlocked,
300 int gb_cap_connection_init(struct gb_connection *connection)
308 cap = kzalloc(sizeof(*cap), GFP_KERNEL);
312 cap->parent = &connection->bundle->dev;
313 cap->connection = connection;
314 mutex_init(&cap->mutex);
315 gb_connection_set_data(connection, cap);
316 kref_init(&cap->kref);
318 mutex_lock(&list_mutex);
319 list_add(&cap->node, &cap_list);
320 mutex_unlock(&list_mutex);
322 ret = gb_connection_enable(connection);
326 minor = ida_simple_get(&cap_minors_map, 0, NUM_MINORS, GFP_KERNEL);
329 goto err_connection_disable;
332 /* Add a char device to allow userspace to interact with cap */
333 cap->dev_num = MKDEV(MAJOR(cap_dev_num), minor);
334 cdev_init(&cap->cdev, &cap_fops);
336 ret = cdev_add(&cap->cdev, cap->dev_num, 1);
340 /* Add a soft link to the previously added char-dev within the bundle */
341 cap->class_device = device_create(cap_class, cap->parent, cap->dev_num,
342 NULL, "gb-authenticate-%d", minor);
343 if (IS_ERR(cap->class_device)) {
344 ret = PTR_ERR(cap->class_device);
351 cdev_del(&cap->cdev);
353 ida_simple_remove(&cap_minors_map, minor);
354 err_connection_disable:
355 gb_connection_disable(connection);
357 mutex_lock(&list_mutex);
358 list_del(&cap->node);
359 mutex_unlock(&list_mutex);
366 void gb_cap_connection_exit(struct gb_connection *connection)
373 cap = gb_connection_get_data(connection);
375 device_destroy(cap_class, cap->dev_num);
376 cdev_del(&cap->cdev);
377 ida_simple_remove(&cap_minors_map, MINOR(cap->dev_num));
380 * Disallow any new ioctl operations on the char device and wait for
381 * existing ones to finish.
383 mutex_lock(&cap->mutex);
384 cap->disabled = true;
385 mutex_unlock(&cap->mutex);
387 /* All pending greybus operations should have finished by now */
388 gb_connection_disable(cap->connection);
390 /* Disallow new users to get access to the cap structure */
391 mutex_lock(&list_mutex);
392 list_del(&cap->node);
393 mutex_unlock(&list_mutex);
396 * All current users of cap would have taken a reference to it by
397 * now, we can drop our reference and wait the last user will get
407 cap_class = class_create(THIS_MODULE, "gb_authenticate");
408 if (IS_ERR(cap_class))
409 return PTR_ERR(cap_class);
411 ret = alloc_chrdev_region(&cap_dev_num, 0, NUM_MINORS,
414 goto err_remove_class;
419 class_destroy(cap_class);
425 unregister_chrdev_region(cap_dev_num, NUM_MINORS);
426 class_destroy(cap_class);
427 ida_destroy(&cap_minors_map);