|
13 | 13 | #include <linux/idr.h>
|
14 | 14 | #include <linux/module.h>
|
15 | 15 | #include <linux/slab.h>
|
| 16 | +#include <linux/random.h> |
| 17 | +#include <crypto/hash.h> |
16 | 18 |
|
17 | 19 | #include "tb.h"
|
18 | 20 |
|
19 | 21 | static DEFINE_IDA(tb_domain_ida);
|
20 | 22 |
|
| 23 | +static const char * const tb_security_names[] = { |
| 24 | + [TB_SECURITY_NONE] = "none", |
| 25 | + [TB_SECURITY_USER] = "user", |
| 26 | + [TB_SECURITY_SECURE] = "secure", |
| 27 | + [TB_SECURITY_DPONLY] = "dponly", |
| 28 | +}; |
| 29 | + |
| 30 | +static ssize_t security_show(struct device *dev, struct device_attribute *attr, |
| 31 | + char *buf) |
| 32 | +{ |
| 33 | + struct tb *tb = container_of(dev, struct tb, dev); |
| 34 | + |
| 35 | + return sprintf(buf, "%s\n", tb_security_names[tb->security_level]); |
| 36 | +} |
| 37 | +static DEVICE_ATTR_RO(security); |
| 38 | + |
| 39 | +static struct attribute *domain_attrs[] = { |
| 40 | + &dev_attr_security.attr, |
| 41 | + NULL, |
| 42 | +}; |
| 43 | + |
| 44 | +static struct attribute_group domain_attr_group = { |
| 45 | + .attrs = domain_attrs, |
| 46 | +}; |
| 47 | + |
| 48 | +static const struct attribute_group *domain_attr_groups[] = { |
| 49 | + &domain_attr_group, |
| 50 | + NULL, |
| 51 | +}; |
| 52 | + |
21 | 53 | struct bus_type tb_bus_type = {
|
22 | 54 | .name = "thunderbolt",
|
23 | 55 | };
|
@@ -82,6 +114,7 @@ struct tb *tb_domain_alloc(struct tb_nhi *nhi, size_t privsize)
|
82 | 114 | tb->dev.parent = &nhi->pdev->dev;
|
83 | 115 | tb->dev.bus = &tb_bus_type;
|
84 | 116 | tb->dev.type = &tb_domain_type;
|
| 117 | + tb->dev.groups = domain_attr_groups; |
85 | 118 | dev_set_name(&tb->dev, "domain%d", tb->index);
|
86 | 119 | device_initialize(&tb->dev);
|
87 | 120 |
|
@@ -140,6 +173,12 @@ int tb_domain_add(struct tb *tb)
|
140 | 173 | */
|
141 | 174 | tb_ctl_start(tb->ctl);
|
142 | 175 |
|
| 176 | + if (tb->cm_ops->driver_ready) { |
| 177 | + ret = tb->cm_ops->driver_ready(tb); |
| 178 | + if (ret) |
| 179 | + goto err_ctl_stop; |
| 180 | + } |
| 181 | + |
143 | 182 | ret = device_add(&tb->dev);
|
144 | 183 | if (ret)
|
145 | 184 | goto err_ctl_stop;
|
@@ -231,6 +270,162 @@ int tb_domain_resume_noirq(struct tb *tb)
|
231 | 270 | return ret;
|
232 | 271 | }
|
233 | 272 |
|
| 273 | +int tb_domain_suspend(struct tb *tb) |
| 274 | +{ |
| 275 | + int ret; |
| 276 | + |
| 277 | + mutex_lock(&tb->lock); |
| 278 | + if (tb->cm_ops->suspend) { |
| 279 | + ret = tb->cm_ops->suspend(tb); |
| 280 | + if (ret) { |
| 281 | + mutex_unlock(&tb->lock); |
| 282 | + return ret; |
| 283 | + } |
| 284 | + } |
| 285 | + mutex_unlock(&tb->lock); |
| 286 | + return 0; |
| 287 | +} |
| 288 | + |
| 289 | +void tb_domain_complete(struct tb *tb) |
| 290 | +{ |
| 291 | + mutex_lock(&tb->lock); |
| 292 | + if (tb->cm_ops->complete) |
| 293 | + tb->cm_ops->complete(tb); |
| 294 | + mutex_unlock(&tb->lock); |
| 295 | +} |
| 296 | + |
| 297 | +/** |
| 298 | + * tb_domain_approve_switch() - Approve switch |
| 299 | + * @tb: Domain the switch belongs to |
| 300 | + * @sw: Switch to approve |
| 301 | + * |
| 302 | + * This will approve switch by connection manager specific means. In |
| 303 | + * case of success the connection manager will create tunnels for all |
| 304 | + * supported protocols. |
| 305 | + */ |
| 306 | +int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw) |
| 307 | +{ |
| 308 | + struct tb_switch *parent_sw; |
| 309 | + |
| 310 | + if (!tb->cm_ops->approve_switch) |
| 311 | + return -EPERM; |
| 312 | + |
| 313 | + /* The parent switch must be authorized before this one */ |
| 314 | + parent_sw = tb_to_switch(sw->dev.parent); |
| 315 | + if (!parent_sw || !parent_sw->authorized) |
| 316 | + return -EINVAL; |
| 317 | + |
| 318 | + return tb->cm_ops->approve_switch(tb, sw); |
| 319 | +} |
| 320 | + |
| 321 | +/** |
| 322 | + * tb_domain_approve_switch_key() - Approve switch and add key |
| 323 | + * @tb: Domain the switch belongs to |
| 324 | + * @sw: Switch to approve |
| 325 | + * |
| 326 | + * For switches that support secure connect, this function first adds |
| 327 | + * key to the switch NVM using connection manager specific means. If |
| 328 | + * adding the key is successful, the switch is approved and connected. |
| 329 | + * |
| 330 | + * Return: %0 on success and negative errno in case of failure. |
| 331 | + */ |
| 332 | +int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw) |
| 333 | +{ |
| 334 | + struct tb_switch *parent_sw; |
| 335 | + int ret; |
| 336 | + |
| 337 | + if (!tb->cm_ops->approve_switch || !tb->cm_ops->add_switch_key) |
| 338 | + return -EPERM; |
| 339 | + |
| 340 | + /* The parent switch must be authorized before this one */ |
| 341 | + parent_sw = tb_to_switch(sw->dev.parent); |
| 342 | + if (!parent_sw || !parent_sw->authorized) |
| 343 | + return -EINVAL; |
| 344 | + |
| 345 | + ret = tb->cm_ops->add_switch_key(tb, sw); |
| 346 | + if (ret) |
| 347 | + return ret; |
| 348 | + |
| 349 | + return tb->cm_ops->approve_switch(tb, sw); |
| 350 | +} |
| 351 | + |
| 352 | +/** |
| 353 | + * tb_domain_challenge_switch_key() - Challenge and approve switch |
| 354 | + * @tb: Domain the switch belongs to |
| 355 | + * @sw: Switch to approve |
| 356 | + * |
| 357 | + * For switches that support secure connect, this function generates |
| 358 | + * random challenge and sends it to the switch. The switch responds to |
| 359 | + * this and if the response matches our random challenge, the switch is |
| 360 | + * approved and connected. |
| 361 | + * |
| 362 | + * Return: %0 on success and negative errno in case of failure. |
| 363 | + */ |
| 364 | +int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw) |
| 365 | +{ |
| 366 | + u8 challenge[TB_SWITCH_KEY_SIZE]; |
| 367 | + u8 response[TB_SWITCH_KEY_SIZE]; |
| 368 | + u8 hmac[TB_SWITCH_KEY_SIZE]; |
| 369 | + struct tb_switch *parent_sw; |
| 370 | + struct crypto_shash *tfm; |
| 371 | + struct shash_desc *shash; |
| 372 | + int ret; |
| 373 | + |
| 374 | + if (!tb->cm_ops->approve_switch || !tb->cm_ops->challenge_switch_key) |
| 375 | + return -EPERM; |
| 376 | + |
| 377 | + /* The parent switch must be authorized before this one */ |
| 378 | + parent_sw = tb_to_switch(sw->dev.parent); |
| 379 | + if (!parent_sw || !parent_sw->authorized) |
| 380 | + return -EINVAL; |
| 381 | + |
| 382 | + get_random_bytes(challenge, sizeof(challenge)); |
| 383 | + ret = tb->cm_ops->challenge_switch_key(tb, sw, challenge, response); |
| 384 | + if (ret) |
| 385 | + return ret; |
| 386 | + |
| 387 | + tfm = crypto_alloc_shash("hmac(sha256)", 0, 0); |
| 388 | + if (IS_ERR(tfm)) |
| 389 | + return PTR_ERR(tfm); |
| 390 | + |
| 391 | + ret = crypto_shash_setkey(tfm, sw->key, TB_SWITCH_KEY_SIZE); |
| 392 | + if (ret) |
| 393 | + goto err_free_tfm; |
| 394 | + |
| 395 | + shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm), |
| 396 | + GFP_KERNEL); |
| 397 | + if (!shash) { |
| 398 | + ret = -ENOMEM; |
| 399 | + goto err_free_tfm; |
| 400 | + } |
| 401 | + |
| 402 | + shash->tfm = tfm; |
| 403 | + shash->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 404 | + |
| 405 | + memset(hmac, 0, sizeof(hmac)); |
| 406 | + ret = crypto_shash_digest(shash, challenge, sizeof(hmac), hmac); |
| 407 | + if (ret) |
| 408 | + goto err_free_shash; |
| 409 | + |
| 410 | + /* The returned HMAC must match the one we calculated */ |
| 411 | + if (memcmp(response, hmac, sizeof(hmac))) { |
| 412 | + ret = -EKEYREJECTED; |
| 413 | + goto err_free_shash; |
| 414 | + } |
| 415 | + |
| 416 | + crypto_free_shash(tfm); |
| 417 | + kfree(shash); |
| 418 | + |
| 419 | + return tb->cm_ops->approve_switch(tb, sw); |
| 420 | + |
| 421 | +err_free_shash: |
| 422 | + kfree(shash); |
| 423 | +err_free_tfm: |
| 424 | + crypto_free_shash(tfm); |
| 425 | + |
| 426 | + return ret; |
| 427 | +} |
| 428 | + |
234 | 429 | int tb_domain_init(void)
|
235 | 430 | {
|
236 | 431 | return bus_register(&tb_bus_type);
|
|
0 commit comments