Phoebe Buckheister | 5d637d5 | 2014-05-16 17:46:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Fraunhofer ITWM |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * Written by: |
| 14 | * Phoebe Buckheister <phoebe.buckheister@itwm.fraunhofer.de> |
| 15 | */ |
| 16 | |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/bug.h> |
| 19 | #include <linux/completion.h> |
| 20 | #include <net/ieee802154.h> |
Phoebe Buckheister | 4c14a2f | 2014-05-16 17:46:39 +0200 | [diff] [blame] | 21 | #include <crypto/algapi.h> |
Phoebe Buckheister | 5d637d5 | 2014-05-16 17:46:37 +0200 | [diff] [blame] | 22 | |
| 23 | #include "mac802154.h" |
| 24 | #include "llsec.h" |
| 25 | |
| 26 | static void llsec_key_put(struct mac802154_llsec_key *key); |
| 27 | static bool llsec_key_id_equal(const struct ieee802154_llsec_key_id *a, |
| 28 | const struct ieee802154_llsec_key_id *b); |
| 29 | |
| 30 | static void llsec_dev_free(struct mac802154_llsec_device *dev); |
| 31 | |
| 32 | void mac802154_llsec_init(struct mac802154_llsec *sec) |
| 33 | { |
| 34 | memset(sec, 0, sizeof(*sec)); |
| 35 | |
| 36 | memset(&sec->params.default_key_source, 0xFF, IEEE802154_ADDR_LEN); |
| 37 | |
| 38 | INIT_LIST_HEAD(&sec->table.security_levels); |
| 39 | INIT_LIST_HEAD(&sec->table.devices); |
| 40 | INIT_LIST_HEAD(&sec->table.keys); |
| 41 | hash_init(sec->devices_short); |
| 42 | hash_init(sec->devices_hw); |
| 43 | rwlock_init(&sec->lock); |
| 44 | } |
| 45 | |
| 46 | void mac802154_llsec_destroy(struct mac802154_llsec *sec) |
| 47 | { |
| 48 | struct ieee802154_llsec_seclevel *sl, *sn; |
| 49 | struct ieee802154_llsec_device *dev, *dn; |
| 50 | struct ieee802154_llsec_key_entry *key, *kn; |
| 51 | |
| 52 | list_for_each_entry_safe(sl, sn, &sec->table.security_levels, list) { |
| 53 | struct mac802154_llsec_seclevel *msl; |
| 54 | |
| 55 | msl = container_of(sl, struct mac802154_llsec_seclevel, level); |
| 56 | list_del(&sl->list); |
| 57 | kfree(msl); |
| 58 | } |
| 59 | |
| 60 | list_for_each_entry_safe(dev, dn, &sec->table.devices, list) { |
| 61 | struct mac802154_llsec_device *mdev; |
| 62 | |
| 63 | mdev = container_of(dev, struct mac802154_llsec_device, dev); |
| 64 | list_del(&dev->list); |
| 65 | llsec_dev_free(mdev); |
| 66 | } |
| 67 | |
| 68 | list_for_each_entry_safe(key, kn, &sec->table.keys, list) { |
| 69 | struct mac802154_llsec_key *mkey; |
| 70 | |
| 71 | mkey = container_of(key->key, struct mac802154_llsec_key, key); |
| 72 | list_del(&key->list); |
| 73 | llsec_key_put(mkey); |
| 74 | kfree(key); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | int mac802154_llsec_get_params(struct mac802154_llsec *sec, |
| 81 | struct ieee802154_llsec_params *params) |
| 82 | { |
| 83 | read_lock_bh(&sec->lock); |
| 84 | *params = sec->params; |
| 85 | read_unlock_bh(&sec->lock); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int mac802154_llsec_set_params(struct mac802154_llsec *sec, |
| 91 | const struct ieee802154_llsec_params *params, |
| 92 | int changed) |
| 93 | { |
| 94 | write_lock_bh(&sec->lock); |
| 95 | |
| 96 | if (changed & IEEE802154_LLSEC_PARAM_ENABLED) |
| 97 | sec->params.enabled = params->enabled; |
| 98 | if (changed & IEEE802154_LLSEC_PARAM_FRAME_COUNTER) |
| 99 | sec->params.frame_counter = params->frame_counter; |
| 100 | if (changed & IEEE802154_LLSEC_PARAM_OUT_LEVEL) |
| 101 | sec->params.out_level = params->out_level; |
| 102 | if (changed & IEEE802154_LLSEC_PARAM_OUT_KEY) |
| 103 | sec->params.out_key = params->out_key; |
| 104 | if (changed & IEEE802154_LLSEC_PARAM_KEY_SOURCE) |
| 105 | sec->params.default_key_source = params->default_key_source; |
| 106 | if (changed & IEEE802154_LLSEC_PARAM_PAN_ID) |
| 107 | sec->params.pan_id = params->pan_id; |
| 108 | if (changed & IEEE802154_LLSEC_PARAM_HWADDR) |
| 109 | sec->params.hwaddr = params->hwaddr; |
| 110 | if (changed & IEEE802154_LLSEC_PARAM_COORD_HWADDR) |
| 111 | sec->params.coord_hwaddr = params->coord_hwaddr; |
| 112 | if (changed & IEEE802154_LLSEC_PARAM_COORD_SHORTADDR) |
| 113 | sec->params.coord_shortaddr = params->coord_shortaddr; |
| 114 | |
| 115 | write_unlock_bh(&sec->lock); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | static struct mac802154_llsec_key* |
| 123 | llsec_key_alloc(const struct ieee802154_llsec_key *template) |
| 124 | { |
| 125 | const int authsizes[3] = { 4, 8, 16 }; |
| 126 | struct mac802154_llsec_key *key; |
| 127 | int i; |
| 128 | |
| 129 | key = kzalloc(sizeof(*key), GFP_KERNEL); |
| 130 | if (!key) |
| 131 | return NULL; |
| 132 | |
| 133 | kref_init(&key->ref); |
| 134 | key->key = *template; |
| 135 | |
| 136 | BUILD_BUG_ON(ARRAY_SIZE(authsizes) != ARRAY_SIZE(key->tfm)); |
| 137 | |
| 138 | for (i = 0; i < ARRAY_SIZE(key->tfm); i++) { |
| 139 | key->tfm[i] = crypto_alloc_aead("ccm(aes)", 0, |
| 140 | CRYPTO_ALG_ASYNC); |
| 141 | if (!key->tfm[i]) |
| 142 | goto err_tfm; |
| 143 | if (crypto_aead_setkey(key->tfm[i], template->key, |
| 144 | IEEE802154_LLSEC_KEY_SIZE)) |
| 145 | goto err_tfm; |
| 146 | if (crypto_aead_setauthsize(key->tfm[i], authsizes[i])) |
| 147 | goto err_tfm; |
| 148 | } |
| 149 | |
| 150 | key->tfm0 = crypto_alloc_blkcipher("ctr(aes)", 0, CRYPTO_ALG_ASYNC); |
| 151 | if (!key->tfm0) |
| 152 | goto err_tfm; |
| 153 | |
| 154 | if (crypto_blkcipher_setkey(key->tfm0, template->key, |
| 155 | IEEE802154_LLSEC_KEY_SIZE)) |
| 156 | goto err_tfm0; |
| 157 | |
| 158 | return key; |
| 159 | |
| 160 | err_tfm0: |
| 161 | crypto_free_blkcipher(key->tfm0); |
| 162 | err_tfm: |
| 163 | for (i = 0; i < ARRAY_SIZE(key->tfm); i++) |
| 164 | if (key->tfm[i]) |
| 165 | crypto_free_aead(key->tfm[i]); |
| 166 | |
| 167 | kfree(key); |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | static void llsec_key_release(struct kref *ref) |
| 172 | { |
| 173 | struct mac802154_llsec_key *key; |
| 174 | int i; |
| 175 | |
| 176 | key = container_of(ref, struct mac802154_llsec_key, ref); |
| 177 | |
| 178 | for (i = 0; i < ARRAY_SIZE(key->tfm); i++) |
| 179 | crypto_free_aead(key->tfm[i]); |
| 180 | |
| 181 | crypto_free_blkcipher(key->tfm0); |
| 182 | kfree(key); |
| 183 | } |
| 184 | |
| 185 | static struct mac802154_llsec_key* |
| 186 | llsec_key_get(struct mac802154_llsec_key *key) |
| 187 | { |
| 188 | kref_get(&key->ref); |
| 189 | return key; |
| 190 | } |
| 191 | |
| 192 | static void llsec_key_put(struct mac802154_llsec_key *key) |
| 193 | { |
| 194 | kref_put(&key->ref, llsec_key_release); |
| 195 | } |
| 196 | |
| 197 | static bool llsec_key_id_equal(const struct ieee802154_llsec_key_id *a, |
| 198 | const struct ieee802154_llsec_key_id *b) |
| 199 | { |
| 200 | if (a->mode != b->mode) |
| 201 | return false; |
| 202 | |
| 203 | if (a->mode == IEEE802154_SCF_KEY_IMPLICIT) |
| 204 | return ieee802154_addr_equal(&a->device_addr, &b->device_addr); |
| 205 | |
| 206 | if (a->id != b->id) |
| 207 | return false; |
| 208 | |
| 209 | switch (a->mode) { |
| 210 | case IEEE802154_SCF_KEY_SHORT_INDEX: |
| 211 | return a->short_source == b->short_source; |
| 212 | case IEEE802154_SCF_KEY_HW_INDEX: |
| 213 | return a->extended_source == b->extended_source; |
| 214 | } |
| 215 | |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | int mac802154_llsec_key_add(struct mac802154_llsec *sec, |
| 220 | const struct ieee802154_llsec_key_id *id, |
| 221 | const struct ieee802154_llsec_key *key) |
| 222 | { |
| 223 | struct mac802154_llsec_key *mkey = NULL; |
| 224 | struct ieee802154_llsec_key_entry *pos, *new; |
| 225 | |
| 226 | if (!(key->frame_types & (1 << IEEE802154_FC_TYPE_MAC_CMD)) && |
| 227 | key->cmd_frame_ids) |
| 228 | return -EINVAL; |
| 229 | |
| 230 | list_for_each_entry(pos, &sec->table.keys, list) { |
| 231 | if (llsec_key_id_equal(&pos->id, id)) |
| 232 | return -EEXIST; |
| 233 | |
| 234 | if (memcmp(pos->key->key, key->key, |
| 235 | IEEE802154_LLSEC_KEY_SIZE)) |
| 236 | continue; |
| 237 | |
| 238 | mkey = container_of(pos->key, struct mac802154_llsec_key, key); |
| 239 | |
| 240 | /* Don't allow multiple instances of the same AES key to have |
| 241 | * different allowed frame types/command frame ids, as this is |
| 242 | * not possible in the 802.15.4 PIB. |
| 243 | */ |
| 244 | if (pos->key->frame_types != key->frame_types || |
| 245 | pos->key->cmd_frame_ids != key->cmd_frame_ids) |
| 246 | return -EEXIST; |
| 247 | |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
| 252 | if (!new) |
| 253 | return -ENOMEM; |
| 254 | |
| 255 | if (!mkey) |
| 256 | mkey = llsec_key_alloc(key); |
| 257 | else |
| 258 | mkey = llsec_key_get(mkey); |
| 259 | |
| 260 | if (!mkey) |
| 261 | goto fail; |
| 262 | |
| 263 | new->id = *id; |
| 264 | new->key = &mkey->key; |
| 265 | |
| 266 | list_add_rcu(&new->list, &sec->table.keys); |
| 267 | |
| 268 | return 0; |
| 269 | |
| 270 | fail: |
| 271 | kfree(new); |
| 272 | return -ENOMEM; |
| 273 | } |
| 274 | |
| 275 | int mac802154_llsec_key_del(struct mac802154_llsec *sec, |
| 276 | const struct ieee802154_llsec_key_id *key) |
| 277 | { |
| 278 | struct ieee802154_llsec_key_entry *pos; |
| 279 | |
| 280 | list_for_each_entry(pos, &sec->table.keys, list) { |
| 281 | struct mac802154_llsec_key *mkey; |
| 282 | |
| 283 | mkey = container_of(pos->key, struct mac802154_llsec_key, key); |
| 284 | |
| 285 | if (llsec_key_id_equal(&pos->id, key)) { |
| 286 | llsec_key_put(mkey); |
| 287 | return 0; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return -ENOENT; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | |
| 296 | static bool llsec_dev_use_shortaddr(__le16 short_addr) |
| 297 | { |
| 298 | return short_addr != cpu_to_le16(IEEE802154_ADDR_UNDEF) && |
| 299 | short_addr != cpu_to_le16(0xffff); |
| 300 | } |
| 301 | |
| 302 | static u32 llsec_dev_hash_short(__le16 short_addr, __le16 pan_id) |
| 303 | { |
| 304 | return ((__force u16) short_addr) << 16 | (__force u16) pan_id; |
| 305 | } |
| 306 | |
| 307 | static u64 llsec_dev_hash_long(__le64 hwaddr) |
| 308 | { |
| 309 | return (__force u64) hwaddr; |
| 310 | } |
| 311 | |
| 312 | static struct mac802154_llsec_device* |
| 313 | llsec_dev_find_short(struct mac802154_llsec *sec, __le16 short_addr, |
| 314 | __le16 pan_id) |
| 315 | { |
| 316 | struct mac802154_llsec_device *dev; |
| 317 | u32 key = llsec_dev_hash_short(short_addr, pan_id); |
| 318 | |
| 319 | hash_for_each_possible_rcu(sec->devices_short, dev, bucket_s, key) { |
| 320 | if (dev->dev.short_addr == short_addr && |
| 321 | dev->dev.pan_id == pan_id) |
| 322 | return dev; |
| 323 | } |
| 324 | |
| 325 | return NULL; |
| 326 | } |
| 327 | |
| 328 | static struct mac802154_llsec_device* |
| 329 | llsec_dev_find_long(struct mac802154_llsec *sec, __le64 hwaddr) |
| 330 | { |
| 331 | struct mac802154_llsec_device *dev; |
| 332 | u64 key = llsec_dev_hash_long(hwaddr); |
| 333 | |
| 334 | hash_for_each_possible_rcu(sec->devices_hw, dev, bucket_hw, key) { |
| 335 | if (dev->dev.hwaddr == hwaddr) |
| 336 | return dev; |
| 337 | } |
| 338 | |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | static void llsec_dev_free(struct mac802154_llsec_device *dev) |
| 343 | { |
| 344 | struct ieee802154_llsec_device_key *pos, *pn; |
| 345 | struct mac802154_llsec_device_key *devkey; |
| 346 | |
| 347 | list_for_each_entry_safe(pos, pn, &dev->dev.keys, list) { |
| 348 | devkey = container_of(pos, struct mac802154_llsec_device_key, |
| 349 | devkey); |
| 350 | |
| 351 | list_del(&pos->list); |
| 352 | kfree(devkey); |
| 353 | } |
| 354 | |
| 355 | kfree(dev); |
| 356 | } |
| 357 | |
| 358 | int mac802154_llsec_dev_add(struct mac802154_llsec *sec, |
| 359 | const struct ieee802154_llsec_device *dev) |
| 360 | { |
| 361 | struct mac802154_llsec_device *entry; |
| 362 | u32 skey = llsec_dev_hash_short(dev->short_addr, dev->pan_id); |
| 363 | u64 hwkey = llsec_dev_hash_long(dev->hwaddr); |
| 364 | |
| 365 | BUILD_BUG_ON(sizeof(hwkey) != IEEE802154_ADDR_LEN); |
| 366 | |
| 367 | if ((llsec_dev_use_shortaddr(dev->short_addr) && |
| 368 | llsec_dev_find_short(sec, dev->short_addr, dev->pan_id)) || |
| 369 | llsec_dev_find_long(sec, dev->hwaddr)) |
| 370 | return -EEXIST; |
| 371 | |
| 372 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 373 | if (!entry) |
| 374 | return -ENOMEM; |
| 375 | |
| 376 | entry->dev = *dev; |
| 377 | spin_lock_init(&entry->lock); |
| 378 | INIT_LIST_HEAD(&entry->dev.keys); |
| 379 | |
| 380 | if (llsec_dev_use_shortaddr(dev->short_addr)) |
| 381 | hash_add_rcu(sec->devices_short, &entry->bucket_s, skey); |
| 382 | else |
| 383 | INIT_HLIST_NODE(&entry->bucket_s); |
| 384 | |
| 385 | hash_add_rcu(sec->devices_hw, &entry->bucket_hw, hwkey); |
| 386 | list_add_tail_rcu(&entry->dev.list, &sec->table.devices); |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static void llsec_dev_free_rcu(struct rcu_head *rcu) |
| 392 | { |
| 393 | llsec_dev_free(container_of(rcu, struct mac802154_llsec_device, rcu)); |
| 394 | } |
| 395 | |
| 396 | int mac802154_llsec_dev_del(struct mac802154_llsec *sec, __le64 device_addr) |
| 397 | { |
| 398 | struct mac802154_llsec_device *pos; |
| 399 | |
| 400 | pos = llsec_dev_find_long(sec, device_addr); |
| 401 | if (!pos) |
| 402 | return -ENOENT; |
| 403 | |
| 404 | hash_del_rcu(&pos->bucket_s); |
| 405 | hash_del_rcu(&pos->bucket_hw); |
| 406 | call_rcu(&pos->rcu, llsec_dev_free_rcu); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | |
| 412 | |
| 413 | static struct mac802154_llsec_device_key* |
| 414 | llsec_devkey_find(struct mac802154_llsec_device *dev, |
| 415 | const struct ieee802154_llsec_key_id *key) |
| 416 | { |
| 417 | struct ieee802154_llsec_device_key *devkey; |
| 418 | |
| 419 | list_for_each_entry_rcu(devkey, &dev->dev.keys, list) { |
| 420 | if (!llsec_key_id_equal(key, &devkey->key_id)) |
| 421 | continue; |
| 422 | |
| 423 | return container_of(devkey, struct mac802154_llsec_device_key, |
| 424 | devkey); |
| 425 | } |
| 426 | |
| 427 | return NULL; |
| 428 | } |
| 429 | |
| 430 | int mac802154_llsec_devkey_add(struct mac802154_llsec *sec, |
| 431 | __le64 dev_addr, |
| 432 | const struct ieee802154_llsec_device_key *key) |
| 433 | { |
| 434 | struct mac802154_llsec_device *dev; |
| 435 | struct mac802154_llsec_device_key *devkey; |
| 436 | |
| 437 | dev = llsec_dev_find_long(sec, dev_addr); |
| 438 | |
| 439 | if (!dev) |
| 440 | return -ENOENT; |
| 441 | |
| 442 | if (llsec_devkey_find(dev, &key->key_id)) |
| 443 | return -EEXIST; |
| 444 | |
| 445 | devkey = kmalloc(sizeof(*devkey), GFP_KERNEL); |
| 446 | if (!devkey) |
| 447 | return -ENOMEM; |
| 448 | |
| 449 | devkey->devkey = *key; |
| 450 | list_add_tail_rcu(&devkey->devkey.list, &dev->dev.keys); |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | int mac802154_llsec_devkey_del(struct mac802154_llsec *sec, |
| 455 | __le64 dev_addr, |
| 456 | const struct ieee802154_llsec_device_key *key) |
| 457 | { |
| 458 | struct mac802154_llsec_device *dev; |
| 459 | struct mac802154_llsec_device_key *devkey; |
| 460 | |
| 461 | dev = llsec_dev_find_long(sec, dev_addr); |
| 462 | |
| 463 | if (!dev) |
| 464 | return -ENOENT; |
| 465 | |
| 466 | devkey = llsec_devkey_find(dev, &key->key_id); |
| 467 | if (!devkey) |
| 468 | return -ENOENT; |
| 469 | |
| 470 | list_del_rcu(&devkey->devkey.list); |
| 471 | kfree_rcu(devkey, rcu); |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | |
| 477 | static struct mac802154_llsec_seclevel* |
| 478 | llsec_find_seclevel(const struct mac802154_llsec *sec, |
| 479 | const struct ieee802154_llsec_seclevel *sl) |
| 480 | { |
| 481 | struct ieee802154_llsec_seclevel *pos; |
| 482 | |
| 483 | list_for_each_entry(pos, &sec->table.security_levels, list) { |
| 484 | if (pos->frame_type != sl->frame_type || |
| 485 | (pos->frame_type == IEEE802154_FC_TYPE_MAC_CMD && |
| 486 | pos->cmd_frame_id != sl->cmd_frame_id) || |
| 487 | pos->device_override != sl->device_override || |
| 488 | pos->sec_levels != sl->sec_levels) |
| 489 | continue; |
| 490 | |
| 491 | return container_of(pos, struct mac802154_llsec_seclevel, |
| 492 | level); |
| 493 | } |
| 494 | |
| 495 | return NULL; |
| 496 | } |
| 497 | |
| 498 | int mac802154_llsec_seclevel_add(struct mac802154_llsec *sec, |
| 499 | const struct ieee802154_llsec_seclevel *sl) |
| 500 | { |
| 501 | struct mac802154_llsec_seclevel *entry; |
| 502 | |
| 503 | if (llsec_find_seclevel(sec, sl)) |
| 504 | return -EEXIST; |
| 505 | |
| 506 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 507 | if (!entry) |
| 508 | return -ENOMEM; |
| 509 | |
| 510 | entry->level = *sl; |
| 511 | |
| 512 | list_add_tail_rcu(&entry->level.list, &sec->table.security_levels); |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | int mac802154_llsec_seclevel_del(struct mac802154_llsec *sec, |
| 518 | const struct ieee802154_llsec_seclevel *sl) |
| 519 | { |
| 520 | struct mac802154_llsec_seclevel *pos; |
| 521 | |
| 522 | pos = llsec_find_seclevel(sec, sl); |
| 523 | if (!pos) |
| 524 | return -ENOENT; |
| 525 | |
| 526 | list_del_rcu(&pos->level.list); |
| 527 | kfree_rcu(pos, rcu); |
| 528 | |
| 529 | return 0; |
| 530 | } |
Phoebe Buckheister | 03556e4 | 2014-05-16 17:46:38 +0200 | [diff] [blame] | 531 | |
| 532 | |
| 533 | |
| 534 | static int llsec_recover_addr(struct mac802154_llsec *sec, |
| 535 | struct ieee802154_addr *addr) |
| 536 | { |
| 537 | __le16 caddr = sec->params.coord_shortaddr; |
| 538 | addr->pan_id = sec->params.pan_id; |
| 539 | |
| 540 | if (caddr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) { |
| 541 | return -EINVAL; |
| 542 | } else if (caddr == cpu_to_le16(IEEE802154_ADDR_UNDEF)) { |
| 543 | addr->extended_addr = sec->params.coord_hwaddr; |
| 544 | addr->mode = IEEE802154_ADDR_LONG; |
| 545 | } else { |
| 546 | addr->short_addr = sec->params.coord_shortaddr; |
| 547 | addr->mode = IEEE802154_ADDR_SHORT; |
| 548 | } |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static struct mac802154_llsec_key* |
| 554 | llsec_lookup_key(struct mac802154_llsec *sec, |
| 555 | const struct ieee802154_hdr *hdr, |
| 556 | const struct ieee802154_addr *addr, |
| 557 | struct ieee802154_llsec_key_id *key_id) |
| 558 | { |
| 559 | struct ieee802154_addr devaddr = *addr; |
| 560 | u8 key_id_mode = hdr->sec.key_id_mode; |
| 561 | struct ieee802154_llsec_key_entry *key_entry; |
| 562 | struct mac802154_llsec_key *key; |
| 563 | |
| 564 | if (key_id_mode == IEEE802154_SCF_KEY_IMPLICIT && |
| 565 | devaddr.mode == IEEE802154_ADDR_NONE) { |
| 566 | if (hdr->fc.type == IEEE802154_FC_TYPE_BEACON) { |
| 567 | devaddr.extended_addr = sec->params.coord_hwaddr; |
| 568 | devaddr.mode = IEEE802154_ADDR_LONG; |
| 569 | } else if (llsec_recover_addr(sec, &devaddr) < 0) { |
| 570 | return NULL; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | list_for_each_entry_rcu(key_entry, &sec->table.keys, list) { |
| 575 | const struct ieee802154_llsec_key_id *id = &key_entry->id; |
| 576 | |
| 577 | if (!(key_entry->key->frame_types & BIT(hdr->fc.type))) |
| 578 | continue; |
| 579 | |
| 580 | if (id->mode != key_id_mode) |
| 581 | continue; |
| 582 | |
| 583 | if (key_id_mode == IEEE802154_SCF_KEY_IMPLICIT) { |
| 584 | if (ieee802154_addr_equal(&devaddr, &id->device_addr)) |
| 585 | goto found; |
| 586 | } else { |
| 587 | if (id->id != hdr->sec.key_id) |
| 588 | continue; |
| 589 | |
| 590 | if ((key_id_mode == IEEE802154_SCF_KEY_INDEX) || |
| 591 | (key_id_mode == IEEE802154_SCF_KEY_SHORT_INDEX && |
| 592 | id->short_source == hdr->sec.short_src) || |
| 593 | (key_id_mode == IEEE802154_SCF_KEY_HW_INDEX && |
| 594 | id->extended_source == hdr->sec.extended_src)) |
| 595 | goto found; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | return NULL; |
| 600 | |
| 601 | found: |
| 602 | key = container_of(key_entry->key, struct mac802154_llsec_key, key); |
| 603 | if (key_id) |
| 604 | *key_id = key_entry->id; |
| 605 | return llsec_key_get(key); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | static void llsec_geniv(u8 iv[16], __le64 addr, |
| 610 | const struct ieee802154_sechdr *sec) |
| 611 | { |
| 612 | __be64 addr_bytes = (__force __be64) swab64((__force u64) addr); |
| 613 | __be32 frame_counter = (__force __be32) swab32((__force u32) sec->frame_counter); |
| 614 | |
| 615 | iv[0] = 1; /* L' = L - 1 = 1 */ |
| 616 | memcpy(iv + 1, &addr_bytes, sizeof(addr_bytes)); |
| 617 | memcpy(iv + 9, &frame_counter, sizeof(frame_counter)); |
| 618 | iv[13] = sec->level; |
| 619 | iv[14] = 0; |
| 620 | iv[15] = 1; |
| 621 | } |
| 622 | |
| 623 | static int |
| 624 | llsec_do_encrypt_unauth(struct sk_buff *skb, const struct mac802154_llsec *sec, |
| 625 | const struct ieee802154_hdr *hdr, |
| 626 | struct mac802154_llsec_key *key) |
| 627 | { |
| 628 | u8 iv[16]; |
| 629 | struct scatterlist src; |
| 630 | struct blkcipher_desc req = { |
| 631 | .tfm = key->tfm0, |
| 632 | .info = iv, |
| 633 | .flags = 0, |
| 634 | }; |
| 635 | |
| 636 | llsec_geniv(iv, sec->params.hwaddr, &hdr->sec); |
| 637 | sg_init_one(&src, skb->data, skb->len); |
| 638 | return crypto_blkcipher_encrypt_iv(&req, &src, &src, skb->len); |
| 639 | } |
| 640 | |
| 641 | static struct crypto_aead* |
| 642 | llsec_tfm_by_len(struct mac802154_llsec_key *key, int authlen) |
| 643 | { |
| 644 | int i; |
| 645 | |
| 646 | for (i = 0; i < ARRAY_SIZE(key->tfm); i++) |
| 647 | if (crypto_aead_authsize(key->tfm[i]) == authlen) |
| 648 | return key->tfm[i]; |
| 649 | |
| 650 | BUG(); |
| 651 | } |
| 652 | |
| 653 | static int |
| 654 | llsec_do_encrypt_auth(struct sk_buff *skb, const struct mac802154_llsec *sec, |
| 655 | const struct ieee802154_hdr *hdr, |
| 656 | struct mac802154_llsec_key *key) |
| 657 | { |
| 658 | u8 iv[16]; |
| 659 | unsigned char *data; |
| 660 | int authlen, assoclen, datalen, rc; |
| 661 | struct scatterlist src, assoc[2], dst[2]; |
| 662 | struct aead_request *req; |
| 663 | |
| 664 | authlen = ieee802154_sechdr_authtag_len(&hdr->sec); |
| 665 | llsec_geniv(iv, sec->params.hwaddr, &hdr->sec); |
| 666 | |
| 667 | req = aead_request_alloc(llsec_tfm_by_len(key, authlen), GFP_ATOMIC); |
| 668 | if (!req) |
| 669 | return -ENOMEM; |
| 670 | |
| 671 | sg_init_table(assoc, 2); |
| 672 | sg_set_buf(&assoc[0], skb_mac_header(skb), skb->mac_len); |
| 673 | assoclen = skb->mac_len; |
| 674 | |
| 675 | data = skb_mac_header(skb) + skb->mac_len; |
| 676 | datalen = skb_tail_pointer(skb) - data; |
| 677 | |
| 678 | if (hdr->sec.level & IEEE802154_SCF_SECLEVEL_ENC) { |
| 679 | sg_set_buf(&assoc[1], data, 0); |
| 680 | } else { |
| 681 | sg_set_buf(&assoc[1], data, datalen); |
| 682 | assoclen += datalen; |
| 683 | datalen = 0; |
| 684 | } |
| 685 | |
| 686 | sg_init_one(&src, data, datalen); |
| 687 | |
| 688 | sg_init_table(dst, 2); |
| 689 | sg_set_buf(&dst[0], data, datalen); |
| 690 | sg_set_buf(&dst[1], skb_put(skb, authlen), authlen); |
| 691 | |
| 692 | aead_request_set_callback(req, 0, NULL, NULL); |
| 693 | aead_request_set_assoc(req, assoc, assoclen); |
| 694 | aead_request_set_crypt(req, &src, dst, datalen, iv); |
| 695 | |
| 696 | rc = crypto_aead_encrypt(req); |
| 697 | |
| 698 | kfree(req); |
| 699 | |
| 700 | return rc; |
| 701 | } |
| 702 | |
| 703 | static int llsec_do_encrypt(struct sk_buff *skb, |
| 704 | const struct mac802154_llsec *sec, |
| 705 | const struct ieee802154_hdr *hdr, |
| 706 | struct mac802154_llsec_key *key) |
| 707 | { |
| 708 | if (hdr->sec.level == IEEE802154_SCF_SECLEVEL_ENC) |
| 709 | return llsec_do_encrypt_unauth(skb, sec, hdr, key); |
| 710 | else |
| 711 | return llsec_do_encrypt_auth(skb, sec, hdr, key); |
| 712 | } |
| 713 | |
| 714 | int mac802154_llsec_encrypt(struct mac802154_llsec *sec, struct sk_buff *skb) |
| 715 | { |
| 716 | struct ieee802154_hdr hdr; |
| 717 | int rc, authlen, hlen; |
| 718 | struct mac802154_llsec_key *key; |
| 719 | u32 frame_ctr; |
| 720 | |
| 721 | hlen = ieee802154_hdr_pull(skb, &hdr); |
| 722 | |
| 723 | if (hlen < 0 || hdr.fc.type != IEEE802154_FC_TYPE_DATA) |
| 724 | return -EINVAL; |
| 725 | |
| 726 | if (!hdr.fc.security_enabled || hdr.sec.level == 0) { |
| 727 | skb_push(skb, hlen); |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | authlen = ieee802154_sechdr_authtag_len(&hdr.sec); |
| 732 | |
| 733 | if (skb->len + hlen + authlen + IEEE802154_MFR_SIZE > IEEE802154_MTU) |
| 734 | return -EMSGSIZE; |
| 735 | |
| 736 | rcu_read_lock(); |
| 737 | |
| 738 | read_lock_bh(&sec->lock); |
| 739 | |
| 740 | if (!sec->params.enabled) { |
| 741 | rc = -EINVAL; |
| 742 | goto fail_read; |
| 743 | } |
| 744 | |
| 745 | key = llsec_lookup_key(sec, &hdr, &hdr.dest, NULL); |
| 746 | if (!key) { |
| 747 | rc = -ENOKEY; |
| 748 | goto fail_read; |
| 749 | } |
| 750 | |
| 751 | read_unlock_bh(&sec->lock); |
| 752 | |
| 753 | write_lock_bh(&sec->lock); |
| 754 | |
| 755 | frame_ctr = be32_to_cpu(sec->params.frame_counter); |
| 756 | hdr.sec.frame_counter = cpu_to_le32(frame_ctr); |
| 757 | if (frame_ctr == 0xFFFFFFFF) { |
| 758 | write_unlock_bh(&sec->lock); |
| 759 | llsec_key_put(key); |
| 760 | rc = -EOVERFLOW; |
| 761 | goto fail; |
| 762 | } |
| 763 | |
| 764 | sec->params.frame_counter = cpu_to_be32(frame_ctr + 1); |
| 765 | |
| 766 | write_unlock_bh(&sec->lock); |
| 767 | |
| 768 | rcu_read_unlock(); |
| 769 | |
| 770 | skb->mac_len = ieee802154_hdr_push(skb, &hdr); |
| 771 | skb_reset_mac_header(skb); |
| 772 | |
| 773 | rc = llsec_do_encrypt(skb, sec, &hdr, key); |
| 774 | llsec_key_put(key); |
| 775 | |
| 776 | return rc < 0 ? rc : 0; |
| 777 | |
| 778 | fail_read: |
Phoebe Buckheister | 6f3eabc | 2014-05-20 13:14:22 +0200 | [diff] [blame^] | 779 | read_unlock_bh(&sec->lock); |
Phoebe Buckheister | 03556e4 | 2014-05-16 17:46:38 +0200 | [diff] [blame] | 780 | fail: |
| 781 | rcu_read_unlock(); |
| 782 | return rc; |
| 783 | } |
Phoebe Buckheister | 4c14a2f | 2014-05-16 17:46:39 +0200 | [diff] [blame] | 784 | |
| 785 | |
| 786 | |
| 787 | static struct mac802154_llsec_device* |
| 788 | llsec_lookup_dev(struct mac802154_llsec *sec, |
| 789 | const struct ieee802154_addr *addr) |
| 790 | { |
| 791 | struct ieee802154_addr devaddr = *addr; |
| 792 | struct mac802154_llsec_device *dev = NULL; |
| 793 | |
| 794 | if (devaddr.mode == IEEE802154_ADDR_NONE && |
| 795 | llsec_recover_addr(sec, &devaddr) < 0) |
| 796 | return NULL; |
| 797 | |
| 798 | if (devaddr.mode == IEEE802154_ADDR_SHORT) { |
| 799 | u32 key = llsec_dev_hash_short(devaddr.short_addr, |
| 800 | devaddr.pan_id); |
| 801 | |
| 802 | hash_for_each_possible_rcu(sec->devices_short, dev, |
| 803 | bucket_s, key) { |
| 804 | if (dev->dev.pan_id == devaddr.pan_id && |
| 805 | dev->dev.short_addr == devaddr.short_addr) |
| 806 | return dev; |
| 807 | } |
| 808 | } else { |
| 809 | u64 key = llsec_dev_hash_long(devaddr.extended_addr); |
| 810 | |
| 811 | hash_for_each_possible_rcu(sec->devices_hw, dev, |
| 812 | bucket_hw, key) { |
| 813 | if (dev->dev.hwaddr == devaddr.extended_addr) |
| 814 | return dev; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | return NULL; |
| 819 | } |
| 820 | |
| 821 | static int |
| 822 | llsec_lookup_seclevel(const struct mac802154_llsec *sec, |
| 823 | u8 frame_type, u8 cmd_frame_id, |
| 824 | struct ieee802154_llsec_seclevel *rlevel) |
| 825 | { |
| 826 | struct ieee802154_llsec_seclevel *level; |
| 827 | |
| 828 | list_for_each_entry_rcu(level, &sec->table.security_levels, list) { |
| 829 | if (level->frame_type == frame_type && |
| 830 | (frame_type != IEEE802154_FC_TYPE_MAC_CMD || |
| 831 | level->cmd_frame_id == cmd_frame_id)) { |
| 832 | *rlevel = *level; |
| 833 | return 0; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | return -EINVAL; |
| 838 | } |
| 839 | |
| 840 | static int |
| 841 | llsec_do_decrypt_unauth(struct sk_buff *skb, const struct mac802154_llsec *sec, |
| 842 | const struct ieee802154_hdr *hdr, |
| 843 | struct mac802154_llsec_key *key, __le64 dev_addr) |
| 844 | { |
| 845 | u8 iv[16]; |
| 846 | unsigned char *data; |
| 847 | int datalen; |
| 848 | struct scatterlist src; |
| 849 | struct blkcipher_desc req = { |
| 850 | .tfm = key->tfm0, |
| 851 | .info = iv, |
| 852 | .flags = 0, |
| 853 | }; |
| 854 | |
| 855 | llsec_geniv(iv, dev_addr, &hdr->sec); |
| 856 | data = skb_mac_header(skb) + skb->mac_len; |
| 857 | datalen = skb_tail_pointer(skb) - data; |
| 858 | |
| 859 | sg_init_one(&src, data, datalen); |
| 860 | |
| 861 | return crypto_blkcipher_decrypt_iv(&req, &src, &src, datalen); |
| 862 | } |
| 863 | |
| 864 | static int |
| 865 | llsec_do_decrypt_auth(struct sk_buff *skb, const struct mac802154_llsec *sec, |
| 866 | const struct ieee802154_hdr *hdr, |
| 867 | struct mac802154_llsec_key *key, __le64 dev_addr) |
| 868 | { |
| 869 | u8 iv[16]; |
| 870 | unsigned char *data; |
| 871 | int authlen, datalen, assoclen, rc; |
| 872 | struct scatterlist src, assoc[2]; |
| 873 | struct aead_request *req; |
| 874 | |
| 875 | authlen = ieee802154_sechdr_authtag_len(&hdr->sec); |
| 876 | llsec_geniv(iv, dev_addr, &hdr->sec); |
| 877 | |
| 878 | req = aead_request_alloc(llsec_tfm_by_len(key, authlen), GFP_ATOMIC); |
| 879 | if (!req) |
| 880 | return -ENOMEM; |
| 881 | |
| 882 | sg_init_table(assoc, 2); |
| 883 | sg_set_buf(&assoc[0], skb_mac_header(skb), skb->mac_len); |
| 884 | assoclen = skb->mac_len; |
| 885 | |
| 886 | data = skb_mac_header(skb) + skb->mac_len; |
| 887 | datalen = skb_tail_pointer(skb) - data; |
| 888 | |
| 889 | if (hdr->sec.level & IEEE802154_SCF_SECLEVEL_ENC) { |
| 890 | sg_set_buf(&assoc[1], data, 0); |
| 891 | } else { |
| 892 | sg_set_buf(&assoc[1], data, datalen - authlen); |
| 893 | assoclen += datalen - authlen; |
| 894 | data += datalen - authlen; |
| 895 | datalen = authlen; |
| 896 | } |
| 897 | |
| 898 | sg_init_one(&src, data, datalen); |
| 899 | |
| 900 | aead_request_set_callback(req, 0, NULL, NULL); |
| 901 | aead_request_set_assoc(req, assoc, assoclen); |
| 902 | aead_request_set_crypt(req, &src, &src, datalen, iv); |
| 903 | |
| 904 | rc = crypto_aead_decrypt(req); |
| 905 | |
| 906 | kfree(req); |
| 907 | skb_trim(skb, skb->len - authlen); |
| 908 | |
| 909 | return rc; |
| 910 | } |
| 911 | |
| 912 | static int |
| 913 | llsec_do_decrypt(struct sk_buff *skb, const struct mac802154_llsec *sec, |
| 914 | const struct ieee802154_hdr *hdr, |
| 915 | struct mac802154_llsec_key *key, __le64 dev_addr) |
| 916 | { |
| 917 | if (hdr->sec.level == IEEE802154_SCF_SECLEVEL_ENC) |
| 918 | return llsec_do_decrypt_unauth(skb, sec, hdr, key, dev_addr); |
| 919 | else |
| 920 | return llsec_do_decrypt_auth(skb, sec, hdr, key, dev_addr); |
| 921 | } |
| 922 | |
| 923 | static int |
Phoebe Buckheister | f0f77dc | 2014-05-16 17:46:45 +0200 | [diff] [blame] | 924 | llsec_update_devkey_record(struct mac802154_llsec_device *dev, |
| 925 | const struct ieee802154_llsec_key_id *in_key) |
| 926 | { |
| 927 | struct mac802154_llsec_device_key *devkey; |
| 928 | |
| 929 | devkey = llsec_devkey_find(dev, in_key); |
| 930 | |
| 931 | if (!devkey) { |
| 932 | struct mac802154_llsec_device_key *next; |
| 933 | |
| 934 | next = kzalloc(sizeof(*devkey), GFP_ATOMIC); |
| 935 | if (!next) |
| 936 | return -ENOMEM; |
| 937 | |
| 938 | next->devkey.key_id = *in_key; |
| 939 | |
| 940 | spin_lock_bh(&dev->lock); |
| 941 | |
| 942 | devkey = llsec_devkey_find(dev, in_key); |
| 943 | if (!devkey) |
| 944 | list_add_rcu(&next->devkey.list, &dev->dev.keys); |
| 945 | else |
| 946 | kfree(next); |
| 947 | |
| 948 | spin_unlock_bh(&dev->lock); |
| 949 | } |
| 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | static int |
Phoebe Buckheister | 4c14a2f | 2014-05-16 17:46:39 +0200 | [diff] [blame] | 955 | llsec_update_devkey_info(struct mac802154_llsec_device *dev, |
| 956 | const struct ieee802154_llsec_key_id *in_key, |
| 957 | u32 frame_counter) |
| 958 | { |
| 959 | struct mac802154_llsec_device_key *devkey = NULL; |
| 960 | |
| 961 | if (dev->dev.key_mode == IEEE802154_LLSEC_DEVKEY_RESTRICT) { |
| 962 | devkey = llsec_devkey_find(dev, in_key); |
| 963 | if (!devkey) |
| 964 | return -ENOENT; |
| 965 | } |
| 966 | |
Phoebe Buckheister | f0f77dc | 2014-05-16 17:46:45 +0200 | [diff] [blame] | 967 | if (dev->dev.key_mode == IEEE802154_LLSEC_DEVKEY_RECORD) { |
| 968 | int rc = llsec_update_devkey_record(dev, in_key); |
| 969 | |
| 970 | if (rc < 0) |
| 971 | return rc; |
| 972 | } |
| 973 | |
Phoebe Buckheister | 4c14a2f | 2014-05-16 17:46:39 +0200 | [diff] [blame] | 974 | spin_lock_bh(&dev->lock); |
| 975 | |
| 976 | if ((!devkey && frame_counter < dev->dev.frame_counter) || |
| 977 | (devkey && frame_counter < devkey->devkey.frame_counter)) { |
| 978 | spin_unlock_bh(&dev->lock); |
| 979 | return -EINVAL; |
| 980 | } |
| 981 | |
| 982 | if (devkey) |
| 983 | devkey->devkey.frame_counter = frame_counter + 1; |
| 984 | else |
| 985 | dev->dev.frame_counter = frame_counter + 1; |
| 986 | |
| 987 | spin_unlock_bh(&dev->lock); |
| 988 | |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | int mac802154_llsec_decrypt(struct mac802154_llsec *sec, struct sk_buff *skb) |
| 993 | { |
| 994 | struct ieee802154_hdr hdr; |
| 995 | struct mac802154_llsec_key *key; |
| 996 | struct ieee802154_llsec_key_id key_id; |
| 997 | struct mac802154_llsec_device *dev; |
| 998 | struct ieee802154_llsec_seclevel seclevel; |
| 999 | int err; |
| 1000 | __le64 dev_addr; |
| 1001 | u32 frame_ctr; |
| 1002 | |
| 1003 | if (ieee802154_hdr_peek(skb, &hdr) < 0) |
| 1004 | return -EINVAL; |
| 1005 | if (!hdr.fc.security_enabled) |
| 1006 | return 0; |
| 1007 | if (hdr.fc.version == 0) |
| 1008 | return -EINVAL; |
| 1009 | |
| 1010 | read_lock_bh(&sec->lock); |
| 1011 | if (!sec->params.enabled) { |
| 1012 | read_unlock_bh(&sec->lock); |
| 1013 | return -EINVAL; |
| 1014 | } |
| 1015 | read_unlock_bh(&sec->lock); |
| 1016 | |
| 1017 | rcu_read_lock(); |
| 1018 | |
| 1019 | key = llsec_lookup_key(sec, &hdr, &hdr.source, &key_id); |
| 1020 | if (!key) { |
| 1021 | err = -ENOKEY; |
| 1022 | goto fail; |
| 1023 | } |
| 1024 | |
| 1025 | dev = llsec_lookup_dev(sec, &hdr.source); |
| 1026 | if (!dev) { |
| 1027 | err = -EINVAL; |
| 1028 | goto fail_dev; |
| 1029 | } |
| 1030 | |
| 1031 | if (llsec_lookup_seclevel(sec, hdr.fc.type, 0, &seclevel) < 0) { |
| 1032 | err = -EINVAL; |
| 1033 | goto fail_dev; |
| 1034 | } |
| 1035 | |
| 1036 | if (!(seclevel.sec_levels & BIT(hdr.sec.level)) && |
| 1037 | (hdr.sec.level == 0 && seclevel.device_override && |
| 1038 | !dev->dev.seclevel_exempt)) { |
| 1039 | err = -EINVAL; |
| 1040 | goto fail_dev; |
| 1041 | } |
| 1042 | |
| 1043 | frame_ctr = le32_to_cpu(hdr.sec.frame_counter); |
| 1044 | |
| 1045 | if (frame_ctr == 0xffffffff) { |
| 1046 | err = -EOVERFLOW; |
| 1047 | goto fail_dev; |
| 1048 | } |
| 1049 | |
| 1050 | err = llsec_update_devkey_info(dev, &key_id, frame_ctr); |
| 1051 | if (err) |
| 1052 | goto fail_dev; |
| 1053 | |
| 1054 | dev_addr = dev->dev.hwaddr; |
| 1055 | |
| 1056 | rcu_read_unlock(); |
| 1057 | |
| 1058 | err = llsec_do_decrypt(skb, sec, &hdr, key, dev_addr); |
| 1059 | llsec_key_put(key); |
| 1060 | return err; |
| 1061 | |
| 1062 | fail_dev: |
| 1063 | llsec_key_put(key); |
| 1064 | fail: |
| 1065 | rcu_read_unlock(); |
| 1066 | return err; |
| 1067 | } |