Linux 4.19.133
[linux/fpc-iii.git] / drivers / staging / greybus / authentication.c
bloba5d7c53df987b4a3bba1c8c6c47c49f57c9b30c8
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Greybus Component Authentication Protocol (CAP) Driver.
5 * Copyright 2016 Google Inc.
6 * Copyright 2016 Linaro Ltd.
7 */
9 #include "greybus.h"
11 #include <linux/cdev.h>
12 #include <linux/fs.h>
13 #include <linux/ioctl.h>
14 #include <linux/uaccess.h>
16 #include "greybus_authentication.h"
17 #include "firmware.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
27 struct gb_cap {
28 struct device *parent;
29 struct gb_connection *connection;
30 struct kref kref;
31 struct list_head node;
32 bool disabled; /* connection getting disabled */
34 struct mutex mutex;
35 struct cdev cdev;
36 struct device *class_device;
37 dev_t dev_num;
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);
50 kfree(cap);
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)
66 struct gb_cap *cap;
68 mutex_lock(&list_mutex);
70 list_for_each_entry(cap, &cap_list, node) {
71 if (&cap->cdev == cdev) {
72 kref_get(&cap->kref);
73 goto unlock;
77 cap = NULL;
79 unlock:
80 mutex_unlock(&list_mutex);
82 return cap;
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;
89 int ret;
91 ret = gb_operation_sync(connection, GB_CAP_TYPE_GET_ENDPOINT_UID, NULL,
92 0, &response, sizeof(response));
93 if (ret) {
94 dev_err(cap->parent, "failed to get endpoint uid (%d)\n", ret);
95 return ret;
98 memcpy(euid, response.uid, sizeof(response.uid));
100 return 0;
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;
111 int ret;
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,
117 GFP_KERNEL);
118 if (!op)
119 return -ENOMEM;
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);
126 if (ret) {
127 dev_err(cap->parent, "failed to get certificate (%d)\n", ret);
128 goto done;
131 response = op->response->payload;
132 *result = response->result_code;
133 *size = op->response->payload_size - sizeof(*response);
134 memcpy(certificate, response->certificate, *size);
136 done:
137 gb_operation_put(op);
138 return ret;
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;
150 int ret;
152 op = gb_operation_create_flags(connection, GB_CAP_TYPE_AUTHENTICATE,
153 sizeof(*request), max_size,
154 GB_OPERATION_FLAG_SHORT_RESPONSE,
155 GFP_KERNEL);
156 if (!op)
157 return -ENOMEM;
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);
165 if (ret) {
166 dev_err(cap->parent, "failed to authenticate (%d)\n", ret);
167 goto done;
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);
176 done:
177 gb_operation_put(op);
178 return ret;
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 */
188 if (cap) {
189 file->private_data = cap;
190 return 0;
193 return -ENODEV;
196 static int cap_release(struct inode *inode, struct file *file)
198 struct gb_cap *cap = file->private_data;
200 put_cap(cap);
201 return 0;
204 static int cap_ioctl(struct gb_cap *cap, unsigned int cmd,
205 void __user *buf)
207 struct cap_ioc_get_endpoint_uid endpoint_uid;
208 struct cap_ioc_get_ims_certificate *ims_cert;
209 struct cap_ioc_authenticate *authenticate;
210 size_t size;
211 int ret;
213 switch (cmd) {
214 case CAP_IOC_GET_ENDPOINT_UID:
215 ret = cap_get_endpoint_uid(cap, endpoint_uid.uid);
216 if (ret)
217 return ret;
219 if (copy_to_user(buf, &endpoint_uid, sizeof(endpoint_uid)))
220 return -EFAULT;
222 return 0;
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))
235 ret = -EFAULT;
236 kfree(ims_cert);
238 return ret;
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,
246 authenticate->uid,
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))
253 ret = -EFAULT;
254 kfree(authenticate);
256 return ret;
257 default:
258 return -ENOTTY;
262 static long cap_ioctl_unlocked(struct file *file, unsigned int cmd,
263 unsigned long arg)
265 struct gb_cap *cap = file->private_data;
266 struct gb_bundle *bundle = cap->connection->bundle;
267 int ret = -ENODEV;
270 * Serialize ioctls.
272 * We don't want the user to do multiple authentication operations in
273 * parallel.
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
277 * new operations.
279 mutex_lock(&cap->mutex);
280 if (!cap->disabled) {
281 ret = gb_pm_runtime_get_sync(bundle);
282 if (!ret) {
283 ret = cap_ioctl(cap, cmd, (void __user *)arg);
284 gb_pm_runtime_put_autosuspend(bundle);
287 mutex_unlock(&cap->mutex);
289 return ret;
292 static const struct file_operations cap_fops = {
293 .owner = THIS_MODULE,
294 .open = cap_open,
295 .release = cap_release,
296 .unlocked_ioctl = cap_ioctl_unlocked,
299 int gb_cap_connection_init(struct gb_connection *connection)
301 struct gb_cap *cap;
302 int ret, minor;
304 if (!connection)
305 return 0;
307 cap = kzalloc(sizeof(*cap), GFP_KERNEL);
308 if (!cap)
309 return -ENOMEM;
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);
322 if (ret)
323 goto err_list_del;
325 minor = ida_simple_get(&cap_minors_map, 0, NUM_MINORS, GFP_KERNEL);
326 if (minor < 0) {
327 ret = minor;
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);
336 if (ret)
337 goto err_remove_ida;
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);
344 goto err_del_cdev;
347 return 0;
349 err_del_cdev:
350 cdev_del(&cap->cdev);
351 err_remove_ida:
352 ida_simple_remove(&cap_minors_map, minor);
353 err_connection_disable:
354 gb_connection_disable(connection);
355 err_list_del:
356 mutex_lock(&list_mutex);
357 list_del(&cap->node);
358 mutex_unlock(&list_mutex);
360 put_cap(cap);
362 return ret;
365 void gb_cap_connection_exit(struct gb_connection *connection)
367 struct gb_cap *cap;
369 if (!connection)
370 return;
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
397 * cap freed.
399 put_cap(cap);
402 int cap_init(void)
404 int ret;
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,
411 "gb_authenticate");
412 if (ret)
413 goto err_remove_class;
415 return 0;
417 err_remove_class:
418 class_destroy(cap_class);
419 return ret;
422 void cap_exit(void)
424 unregister_chrdev_region(cap_dev_num, NUM_MINORS);
425 class_destroy(cap_class);
426 ida_destroy(&cap_minors_map);