1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2011 Instituto Nokia de Tecnologia 4 * 5 * Authors: 6 * Lauro Ramos Venancio <lauro.venancio@openbossa.org> 7 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org> 8 * 9 * Vendor commands implementation based on net/wireless/nl80211.c 10 * which is: 11 * 12 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net> 13 * Copyright 2013-2014 Intel Mobile Communications GmbH 14 */ 15 16 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ 17 18 #include <net/genetlink.h> 19 #include <linux/nfc.h> 20 #include <linux/slab.h> 21 22 #include "nfc.h" 23 #include "llcp.h" 24 25 static const struct genl_multicast_group nfc_genl_mcgrps[] = { 26 { .name = NFC_GENL_MCAST_EVENT_NAME, }, 27 }; 28 29 static struct genl_family nfc_genl_family; 30 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = { 31 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 }, 32 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING, 33 .len = NFC_DEVICE_NAME_MAXSIZE }, 34 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 }, 35 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 }, 36 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 }, 37 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 }, 38 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 }, 39 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 }, 40 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 }, 41 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 }, 42 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 }, 43 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED }, 44 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING, 45 .len = NFC_FIRMWARE_NAME_MAXSIZE }, 46 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY }, 47 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY }, 48 49 }; 50 51 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = { 52 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING, 53 .len = U8_MAX - 4 }, 54 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 }, 55 }; 56 57 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target, 58 struct netlink_callback *cb, int flags) 59 { 60 void *hdr; 61 62 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 63 &nfc_genl_family, flags, NFC_CMD_GET_TARGET); 64 if (!hdr) 65 return -EMSGSIZE; 66 67 genl_dump_check_consistent(cb, hdr); 68 69 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) || 70 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) || 71 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) || 72 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res)) 73 goto nla_put_failure; 74 if (target->nfcid1_len > 0 && 75 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len, 76 target->nfcid1)) 77 goto nla_put_failure; 78 if (target->sensb_res_len > 0 && 79 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len, 80 target->sensb_res)) 81 goto nla_put_failure; 82 if (target->sensf_res_len > 0 && 83 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len, 84 target->sensf_res)) 85 goto nla_put_failure; 86 87 if (target->is_iso15693) { 88 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID, 89 target->iso15693_dsfid) || 90 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID, 91 sizeof(target->iso15693_uid), target->iso15693_uid)) 92 goto nla_put_failure; 93 } 94 95 genlmsg_end(msg, hdr); 96 return 0; 97 98 nla_put_failure: 99 genlmsg_cancel(msg, hdr); 100 return -EMSGSIZE; 101 } 102 103 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb) 104 { 105 const struct genl_dumpit_info *info = genl_dumpit_info(cb); 106 struct nfc_dev *dev; 107 u32 idx; 108 109 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 110 return ERR_PTR(-EINVAL); 111 112 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 113 114 dev = nfc_get_device(idx); 115 if (!dev) 116 return ERR_PTR(-ENODEV); 117 118 return dev; 119 } 120 121 static int nfc_genl_dump_targets(struct sk_buff *skb, 122 struct netlink_callback *cb) 123 { 124 int i = cb->args[0]; 125 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; 126 int rc; 127 128 if (!dev) { 129 dev = __get_device_from_cb(cb); 130 if (IS_ERR(dev)) 131 return PTR_ERR(dev); 132 133 cb->args[1] = (long) dev; 134 } 135 136 device_lock(&dev->dev); 137 138 cb->seq = dev->targets_generation; 139 140 while (i < dev->n_targets) { 141 rc = nfc_genl_send_target(skb, &dev->targets[i], cb, 142 NLM_F_MULTI); 143 if (rc < 0) 144 break; 145 146 i++; 147 } 148 149 device_unlock(&dev->dev); 150 151 cb->args[0] = i; 152 153 return skb->len; 154 } 155 156 static int nfc_genl_dump_targets_done(struct netlink_callback *cb) 157 { 158 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; 159 160 if (dev) 161 nfc_put_device(dev); 162 163 return 0; 164 } 165 166 int nfc_genl_targets_found(struct nfc_dev *dev) 167 { 168 struct sk_buff *msg; 169 void *hdr; 170 171 dev->genl_data.poll_req_portid = 0; 172 173 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 174 if (!msg) 175 return -ENOMEM; 176 177 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 178 NFC_EVENT_TARGETS_FOUND); 179 if (!hdr) 180 goto free_msg; 181 182 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 183 goto nla_put_failure; 184 185 genlmsg_end(msg, hdr); 186 187 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); 188 189 nla_put_failure: 190 free_msg: 191 nlmsg_free(msg); 192 return -EMSGSIZE; 193 } 194 195 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx) 196 { 197 struct sk_buff *msg; 198 void *hdr; 199 200 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 201 if (!msg) 202 return -ENOMEM; 203 204 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 205 NFC_EVENT_TARGET_LOST); 206 if (!hdr) 207 goto free_msg; 208 209 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || 210 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) 211 goto nla_put_failure; 212 213 genlmsg_end(msg, hdr); 214 215 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 216 217 return 0; 218 219 nla_put_failure: 220 free_msg: 221 nlmsg_free(msg); 222 return -EMSGSIZE; 223 } 224 225 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol) 226 { 227 struct sk_buff *msg; 228 void *hdr; 229 230 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 231 if (!msg) 232 return -ENOMEM; 233 234 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 235 NFC_EVENT_TM_ACTIVATED); 236 if (!hdr) 237 goto free_msg; 238 239 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 240 goto nla_put_failure; 241 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol)) 242 goto nla_put_failure; 243 244 genlmsg_end(msg, hdr); 245 246 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 247 248 return 0; 249 250 nla_put_failure: 251 free_msg: 252 nlmsg_free(msg); 253 return -EMSGSIZE; 254 } 255 256 int nfc_genl_tm_deactivated(struct nfc_dev *dev) 257 { 258 struct sk_buff *msg; 259 void *hdr; 260 261 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 262 if (!msg) 263 return -ENOMEM; 264 265 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 266 NFC_EVENT_TM_DEACTIVATED); 267 if (!hdr) 268 goto free_msg; 269 270 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 271 goto nla_put_failure; 272 273 genlmsg_end(msg, hdr); 274 275 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 276 277 return 0; 278 279 nla_put_failure: 280 free_msg: 281 nlmsg_free(msg); 282 return -EMSGSIZE; 283 } 284 285 static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg) 286 { 287 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || 288 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || 289 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || 290 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) || 291 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode)) 292 return -1; 293 return 0; 294 } 295 296 int nfc_genl_device_added(struct nfc_dev *dev) 297 { 298 struct sk_buff *msg; 299 void *hdr; 300 301 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 302 if (!msg) 303 return -ENOMEM; 304 305 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 306 NFC_EVENT_DEVICE_ADDED); 307 if (!hdr) 308 goto free_msg; 309 310 if (nfc_genl_setup_device_added(dev, msg)) 311 goto nla_put_failure; 312 313 genlmsg_end(msg, hdr); 314 315 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 316 317 return 0; 318 319 nla_put_failure: 320 free_msg: 321 nlmsg_free(msg); 322 return -EMSGSIZE; 323 } 324 325 int nfc_genl_device_removed(struct nfc_dev *dev) 326 { 327 struct sk_buff *msg; 328 void *hdr; 329 330 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 331 if (!msg) 332 return -ENOMEM; 333 334 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 335 NFC_EVENT_DEVICE_REMOVED); 336 if (!hdr) 337 goto free_msg; 338 339 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 340 goto nla_put_failure; 341 342 genlmsg_end(msg, hdr); 343 344 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 345 346 return 0; 347 348 nla_put_failure: 349 free_msg: 350 nlmsg_free(msg); 351 return -EMSGSIZE; 352 } 353 354 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list) 355 { 356 struct sk_buff *msg; 357 struct nlattr *sdp_attr, *uri_attr; 358 struct nfc_llcp_sdp_tlv *sdres; 359 struct hlist_node *n; 360 void *hdr; 361 int rc = -EMSGSIZE; 362 int i; 363 364 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 365 if (!msg) 366 return -ENOMEM; 367 368 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 369 NFC_EVENT_LLC_SDRES); 370 if (!hdr) 371 goto free_msg; 372 373 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 374 goto nla_put_failure; 375 376 sdp_attr = nla_nest_start_noflag(msg, NFC_ATTR_LLC_SDP); 377 if (sdp_attr == NULL) { 378 rc = -ENOMEM; 379 goto nla_put_failure; 380 } 381 382 i = 1; 383 hlist_for_each_entry_safe(sdres, n, sdres_list, node) { 384 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap); 385 386 uri_attr = nla_nest_start_noflag(msg, i++); 387 if (uri_attr == NULL) { 388 rc = -ENOMEM; 389 goto nla_put_failure; 390 } 391 392 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap)) 393 goto nla_put_failure; 394 395 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri)) 396 goto nla_put_failure; 397 398 nla_nest_end(msg, uri_attr); 399 400 hlist_del(&sdres->node); 401 402 nfc_llcp_free_sdp_tlv(sdres); 403 } 404 405 nla_nest_end(msg, sdp_attr); 406 407 genlmsg_end(msg, hdr); 408 409 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); 410 411 nla_put_failure: 412 free_msg: 413 nlmsg_free(msg); 414 415 nfc_llcp_free_sdp_tlv_list(sdres_list); 416 417 return rc; 418 } 419 420 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type) 421 { 422 struct sk_buff *msg; 423 void *hdr; 424 425 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 426 if (!msg) 427 return -ENOMEM; 428 429 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 430 NFC_EVENT_SE_ADDED); 431 if (!hdr) 432 goto free_msg; 433 434 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || 435 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) || 436 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type)) 437 goto nla_put_failure; 438 439 genlmsg_end(msg, hdr); 440 441 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 442 443 return 0; 444 445 nla_put_failure: 446 free_msg: 447 nlmsg_free(msg); 448 return -EMSGSIZE; 449 } 450 451 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx) 452 { 453 struct sk_buff *msg; 454 void *hdr; 455 456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 457 if (!msg) 458 return -ENOMEM; 459 460 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 461 NFC_EVENT_SE_REMOVED); 462 if (!hdr) 463 goto free_msg; 464 465 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || 466 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx)) 467 goto nla_put_failure; 468 469 genlmsg_end(msg, hdr); 470 471 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 472 473 return 0; 474 475 nla_put_failure: 476 free_msg: 477 nlmsg_free(msg); 478 return -EMSGSIZE; 479 } 480 481 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx, 482 struct nfc_evt_transaction *evt_transaction) 483 { 484 struct nfc_se *se; 485 struct sk_buff *msg; 486 void *hdr; 487 488 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 489 if (!msg) 490 return -ENOMEM; 491 492 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 493 NFC_EVENT_SE_TRANSACTION); 494 if (!hdr) 495 goto free_msg; 496 497 se = nfc_find_se(dev, se_idx); 498 if (!se) 499 goto free_msg; 500 501 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || 502 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) || 503 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) || 504 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len, 505 evt_transaction->aid) || 506 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len, 507 evt_transaction->params)) 508 goto nla_put_failure; 509 510 /* evt_transaction is no more used */ 511 devm_kfree(&dev->dev, evt_transaction); 512 513 genlmsg_end(msg, hdr); 514 515 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 516 517 return 0; 518 519 nla_put_failure: 520 free_msg: 521 /* evt_transaction is no more used */ 522 devm_kfree(&dev->dev, evt_transaction); 523 nlmsg_free(msg); 524 return -EMSGSIZE; 525 } 526 527 int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx) 528 { 529 struct nfc_se *se; 530 struct sk_buff *msg; 531 void *hdr; 532 533 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 534 if (!msg) 535 return -ENOMEM; 536 537 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 538 NFC_EVENT_SE_CONNECTIVITY); 539 if (!hdr) 540 goto free_msg; 541 542 se = nfc_find_se(dev, se_idx); 543 if (!se) 544 goto free_msg; 545 546 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || 547 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) || 548 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type)) 549 goto nla_put_failure; 550 551 genlmsg_end(msg, hdr); 552 553 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 554 555 return 0; 556 557 nla_put_failure: 558 free_msg: 559 nlmsg_free(msg); 560 return -EMSGSIZE; 561 } 562 563 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev, 564 u32 portid, u32 seq, 565 struct netlink_callback *cb, 566 int flags) 567 { 568 void *hdr; 569 570 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, 571 NFC_CMD_GET_DEVICE); 572 if (!hdr) 573 return -EMSGSIZE; 574 575 if (cb) 576 genl_dump_check_consistent(cb, hdr); 577 578 if (nfc_genl_setup_device_added(dev, msg)) 579 goto nla_put_failure; 580 581 genlmsg_end(msg, hdr); 582 return 0; 583 584 nla_put_failure: 585 genlmsg_cancel(msg, hdr); 586 return -EMSGSIZE; 587 } 588 589 static int nfc_genl_dump_devices(struct sk_buff *skb, 590 struct netlink_callback *cb) 591 { 592 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; 593 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; 594 bool first_call = false; 595 596 if (!iter) { 597 first_call = true; 598 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); 599 if (!iter) 600 return -ENOMEM; 601 cb->args[0] = (long) iter; 602 } 603 604 mutex_lock(&nfc_devlist_mutex); 605 606 cb->seq = nfc_devlist_generation; 607 608 if (first_call) { 609 nfc_device_iter_init(iter); 610 dev = nfc_device_iter_next(iter); 611 } 612 613 while (dev) { 614 int rc; 615 616 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid, 617 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); 618 if (rc < 0) 619 break; 620 621 dev = nfc_device_iter_next(iter); 622 } 623 624 mutex_unlock(&nfc_devlist_mutex); 625 626 cb->args[1] = (long) dev; 627 628 return skb->len; 629 } 630 631 static int nfc_genl_dump_devices_done(struct netlink_callback *cb) 632 { 633 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; 634 635 nfc_device_iter_exit(iter); 636 kfree(iter); 637 638 return 0; 639 } 640 641 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx, 642 u8 comm_mode, u8 rf_mode) 643 { 644 struct sk_buff *msg; 645 void *hdr; 646 647 pr_debug("DEP link is up\n"); 648 649 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 650 if (!msg) 651 return -ENOMEM; 652 653 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP); 654 if (!hdr) 655 goto free_msg; 656 657 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 658 goto nla_put_failure; 659 if (rf_mode == NFC_RF_INITIATOR && 660 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) 661 goto nla_put_failure; 662 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) || 663 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode)) 664 goto nla_put_failure; 665 666 genlmsg_end(msg, hdr); 667 668 dev->dep_link_up = true; 669 670 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); 671 672 return 0; 673 674 nla_put_failure: 675 free_msg: 676 nlmsg_free(msg); 677 return -EMSGSIZE; 678 } 679 680 int nfc_genl_dep_link_down_event(struct nfc_dev *dev) 681 { 682 struct sk_buff *msg; 683 void *hdr; 684 685 pr_debug("DEP link is down\n"); 686 687 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 688 if (!msg) 689 return -ENOMEM; 690 691 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 692 NFC_CMD_DEP_LINK_DOWN); 693 if (!hdr) 694 goto free_msg; 695 696 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 697 goto nla_put_failure; 698 699 genlmsg_end(msg, hdr); 700 701 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); 702 703 return 0; 704 705 nla_put_failure: 706 free_msg: 707 nlmsg_free(msg); 708 return -EMSGSIZE; 709 } 710 711 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) 712 { 713 struct sk_buff *msg; 714 struct nfc_dev *dev; 715 u32 idx; 716 int rc = -ENOBUFS; 717 718 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 719 return -EINVAL; 720 721 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 722 723 dev = nfc_get_device(idx); 724 if (!dev) 725 return -ENODEV; 726 727 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 728 if (!msg) { 729 rc = -ENOMEM; 730 goto out_putdev; 731 } 732 733 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq, 734 NULL, 0); 735 if (rc < 0) 736 goto out_free; 737 738 nfc_put_device(dev); 739 740 return genlmsg_reply(msg, info); 741 742 out_free: 743 nlmsg_free(msg); 744 out_putdev: 745 nfc_put_device(dev); 746 return rc; 747 } 748 749 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) 750 { 751 struct nfc_dev *dev; 752 int rc; 753 u32 idx; 754 755 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 756 return -EINVAL; 757 758 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 759 760 dev = nfc_get_device(idx); 761 if (!dev) 762 return -ENODEV; 763 764 rc = nfc_dev_up(dev); 765 766 nfc_put_device(dev); 767 return rc; 768 } 769 770 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) 771 { 772 struct nfc_dev *dev; 773 int rc; 774 u32 idx; 775 776 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 777 return -EINVAL; 778 779 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 780 781 dev = nfc_get_device(idx); 782 if (!dev) 783 return -ENODEV; 784 785 rc = nfc_dev_down(dev); 786 787 nfc_put_device(dev); 788 return rc; 789 } 790 791 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) 792 { 793 struct nfc_dev *dev; 794 int rc; 795 u32 idx; 796 u32 im_protocols = 0, tm_protocols = 0; 797 798 pr_debug("Poll start\n"); 799 800 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 801 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] && 802 !info->attrs[NFC_ATTR_PROTOCOLS]) && 803 !info->attrs[NFC_ATTR_TM_PROTOCOLS])) 804 return -EINVAL; 805 806 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 807 808 if (info->attrs[NFC_ATTR_TM_PROTOCOLS]) 809 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]); 810 811 if (info->attrs[NFC_ATTR_IM_PROTOCOLS]) 812 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]); 813 else if (info->attrs[NFC_ATTR_PROTOCOLS]) 814 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); 815 816 dev = nfc_get_device(idx); 817 if (!dev) 818 return -ENODEV; 819 820 mutex_lock(&dev->genl_data.genl_data_mutex); 821 822 rc = nfc_start_poll(dev, im_protocols, tm_protocols); 823 if (!rc) 824 dev->genl_data.poll_req_portid = info->snd_portid; 825 826 mutex_unlock(&dev->genl_data.genl_data_mutex); 827 828 nfc_put_device(dev); 829 return rc; 830 } 831 832 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) 833 { 834 struct nfc_dev *dev; 835 int rc; 836 u32 idx; 837 838 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 839 return -EINVAL; 840 841 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 842 843 dev = nfc_get_device(idx); 844 if (!dev) 845 return -ENODEV; 846 847 device_lock(&dev->dev); 848 849 if (!dev->polling) { 850 device_unlock(&dev->dev); 851 return -EINVAL; 852 } 853 854 device_unlock(&dev->dev); 855 856 mutex_lock(&dev->genl_data.genl_data_mutex); 857 858 if (dev->genl_data.poll_req_portid != info->snd_portid) { 859 rc = -EBUSY; 860 goto out; 861 } 862 863 rc = nfc_stop_poll(dev); 864 dev->genl_data.poll_req_portid = 0; 865 866 out: 867 mutex_unlock(&dev->genl_data.genl_data_mutex); 868 nfc_put_device(dev); 869 return rc; 870 } 871 872 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info) 873 { 874 struct nfc_dev *dev; 875 u32 device_idx, target_idx, protocol; 876 int rc; 877 878 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 879 !info->attrs[NFC_ATTR_TARGET_INDEX] || 880 !info->attrs[NFC_ATTR_PROTOCOLS]) 881 return -EINVAL; 882 883 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 884 885 dev = nfc_get_device(device_idx); 886 if (!dev) 887 return -ENODEV; 888 889 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); 890 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); 891 892 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP); 893 rc = nfc_activate_target(dev, target_idx, protocol); 894 895 nfc_put_device(dev); 896 return rc; 897 } 898 899 static int nfc_genl_deactivate_target(struct sk_buff *skb, 900 struct genl_info *info) 901 { 902 struct nfc_dev *dev; 903 u32 device_idx, target_idx; 904 int rc; 905 906 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 907 !info->attrs[NFC_ATTR_TARGET_INDEX]) 908 return -EINVAL; 909 910 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 911 912 dev = nfc_get_device(device_idx); 913 if (!dev) 914 return -ENODEV; 915 916 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); 917 918 rc = nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP); 919 920 nfc_put_device(dev); 921 return rc; 922 } 923 924 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info) 925 { 926 struct nfc_dev *dev; 927 int rc, tgt_idx; 928 u32 idx; 929 u8 comm; 930 931 pr_debug("DEP link up\n"); 932 933 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 934 !info->attrs[NFC_ATTR_COMM_MODE]) 935 return -EINVAL; 936 937 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 938 if (!info->attrs[NFC_ATTR_TARGET_INDEX]) 939 tgt_idx = NFC_TARGET_IDX_ANY; 940 else 941 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); 942 943 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]); 944 945 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE) 946 return -EINVAL; 947 948 dev = nfc_get_device(idx); 949 if (!dev) 950 return -ENODEV; 951 952 rc = nfc_dep_link_up(dev, tgt_idx, comm); 953 954 nfc_put_device(dev); 955 956 return rc; 957 } 958 959 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info) 960 { 961 struct nfc_dev *dev; 962 int rc; 963 u32 idx; 964 965 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 966 !info->attrs[NFC_ATTR_TARGET_INDEX]) 967 return -EINVAL; 968 969 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 970 971 dev = nfc_get_device(idx); 972 if (!dev) 973 return -ENODEV; 974 975 rc = nfc_dep_link_down(dev); 976 977 nfc_put_device(dev); 978 return rc; 979 } 980 981 static int nfc_genl_send_params(struct sk_buff *msg, 982 struct nfc_llcp_local *local, 983 u32 portid, u32 seq) 984 { 985 void *hdr; 986 987 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0, 988 NFC_CMD_LLC_GET_PARAMS); 989 if (!hdr) 990 return -EMSGSIZE; 991 992 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) || 993 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) || 994 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) || 995 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux))) 996 goto nla_put_failure; 997 998 genlmsg_end(msg, hdr); 999 return 0; 1000 1001 nla_put_failure: 1002 genlmsg_cancel(msg, hdr); 1003 return -EMSGSIZE; 1004 } 1005 1006 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info) 1007 { 1008 struct nfc_dev *dev; 1009 struct nfc_llcp_local *local; 1010 int rc = 0; 1011 struct sk_buff *msg = NULL; 1012 u32 idx; 1013 1014 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1015 !info->attrs[NFC_ATTR_FIRMWARE_NAME]) 1016 return -EINVAL; 1017 1018 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1019 1020 dev = nfc_get_device(idx); 1021 if (!dev) 1022 return -ENODEV; 1023 1024 device_lock(&dev->dev); 1025 1026 local = nfc_llcp_find_local(dev); 1027 if (!local) { 1028 rc = -ENODEV; 1029 goto exit; 1030 } 1031 1032 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1033 if (!msg) { 1034 rc = -ENOMEM; 1035 goto exit; 1036 } 1037 1038 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq); 1039 1040 exit: 1041 device_unlock(&dev->dev); 1042 1043 nfc_put_device(dev); 1044 1045 if (rc < 0) { 1046 if (msg) 1047 nlmsg_free(msg); 1048 1049 return rc; 1050 } 1051 1052 return genlmsg_reply(msg, info); 1053 } 1054 1055 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info) 1056 { 1057 struct nfc_dev *dev; 1058 struct nfc_llcp_local *local; 1059 u8 rw = 0; 1060 u16 miux = 0; 1061 u32 idx; 1062 int rc = 0; 1063 1064 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1065 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] && 1066 !info->attrs[NFC_ATTR_LLC_PARAM_RW] && 1067 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX])) 1068 return -EINVAL; 1069 1070 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) { 1071 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]); 1072 1073 if (rw > LLCP_MAX_RW) 1074 return -EINVAL; 1075 } 1076 1077 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) { 1078 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]); 1079 1080 if (miux > LLCP_MAX_MIUX) 1081 return -EINVAL; 1082 } 1083 1084 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1085 1086 dev = nfc_get_device(idx); 1087 if (!dev) 1088 return -ENODEV; 1089 1090 device_lock(&dev->dev); 1091 1092 local = nfc_llcp_find_local(dev); 1093 if (!local) { 1094 rc = -ENODEV; 1095 goto exit; 1096 } 1097 1098 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) { 1099 if (dev->dep_link_up) { 1100 rc = -EINPROGRESS; 1101 goto exit; 1102 } 1103 1104 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]); 1105 } 1106 1107 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) 1108 local->rw = rw; 1109 1110 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) 1111 local->miux = cpu_to_be16(miux); 1112 1113 exit: 1114 device_unlock(&dev->dev); 1115 1116 nfc_put_device(dev); 1117 1118 return rc; 1119 } 1120 1121 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info) 1122 { 1123 struct nfc_dev *dev; 1124 struct nfc_llcp_local *local; 1125 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1]; 1126 u32 idx; 1127 u8 tid; 1128 char *uri; 1129 int rc = 0, rem; 1130 size_t uri_len, tlvs_len; 1131 struct hlist_head sdreq_list; 1132 struct nfc_llcp_sdp_tlv *sdreq; 1133 1134 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1135 !info->attrs[NFC_ATTR_LLC_SDP]) 1136 return -EINVAL; 1137 1138 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1139 1140 dev = nfc_get_device(idx); 1141 if (!dev) 1142 return -ENODEV; 1143 1144 device_lock(&dev->dev); 1145 1146 if (dev->dep_link_up == false) { 1147 rc = -ENOLINK; 1148 goto exit; 1149 } 1150 1151 local = nfc_llcp_find_local(dev); 1152 if (!local) { 1153 rc = -ENODEV; 1154 goto exit; 1155 } 1156 1157 INIT_HLIST_HEAD(&sdreq_list); 1158 1159 tlvs_len = 0; 1160 1161 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) { 1162 rc = nla_parse_nested_deprecated(sdp_attrs, NFC_SDP_ATTR_MAX, 1163 attr, nfc_sdp_genl_policy, 1164 info->extack); 1165 1166 if (rc != 0) { 1167 rc = -EINVAL; 1168 goto exit; 1169 } 1170 1171 if (!sdp_attrs[NFC_SDP_ATTR_URI]) 1172 continue; 1173 1174 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]); 1175 if (uri_len == 0) 1176 continue; 1177 1178 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]); 1179 if (uri == NULL || *uri == 0) 1180 continue; 1181 1182 tid = local->sdreq_next_tid++; 1183 1184 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len); 1185 if (sdreq == NULL) { 1186 rc = -ENOMEM; 1187 goto exit; 1188 } 1189 1190 tlvs_len += sdreq->tlv_len; 1191 1192 hlist_add_head(&sdreq->node, &sdreq_list); 1193 } 1194 1195 if (hlist_empty(&sdreq_list)) { 1196 rc = -EINVAL; 1197 goto exit; 1198 } 1199 1200 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len); 1201 exit: 1202 device_unlock(&dev->dev); 1203 1204 nfc_put_device(dev); 1205 1206 return rc; 1207 } 1208 1209 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info) 1210 { 1211 struct nfc_dev *dev; 1212 int rc; 1213 u32 idx; 1214 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1]; 1215 1216 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 1217 return -EINVAL; 1218 1219 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1220 1221 dev = nfc_get_device(idx); 1222 if (!dev) 1223 return -ENODEV; 1224 1225 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME], 1226 sizeof(firmware_name)); 1227 1228 rc = nfc_fw_download(dev, firmware_name); 1229 1230 nfc_put_device(dev); 1231 return rc; 1232 } 1233 1234 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name, 1235 u32 result) 1236 { 1237 struct sk_buff *msg; 1238 void *hdr; 1239 1240 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1241 if (!msg) 1242 return -ENOMEM; 1243 1244 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 1245 NFC_CMD_FW_DOWNLOAD); 1246 if (!hdr) 1247 goto free_msg; 1248 1249 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) || 1250 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) || 1251 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 1252 goto nla_put_failure; 1253 1254 genlmsg_end(msg, hdr); 1255 1256 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 1257 1258 return 0; 1259 1260 nla_put_failure: 1261 free_msg: 1262 nlmsg_free(msg); 1263 return -EMSGSIZE; 1264 } 1265 1266 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info) 1267 { 1268 struct nfc_dev *dev; 1269 int rc; 1270 u32 idx, se_idx; 1271 1272 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1273 !info->attrs[NFC_ATTR_SE_INDEX]) 1274 return -EINVAL; 1275 1276 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1277 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); 1278 1279 dev = nfc_get_device(idx); 1280 if (!dev) 1281 return -ENODEV; 1282 1283 rc = nfc_enable_se(dev, se_idx); 1284 1285 nfc_put_device(dev); 1286 return rc; 1287 } 1288 1289 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info) 1290 { 1291 struct nfc_dev *dev; 1292 int rc; 1293 u32 idx, se_idx; 1294 1295 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1296 !info->attrs[NFC_ATTR_SE_INDEX]) 1297 return -EINVAL; 1298 1299 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1300 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); 1301 1302 dev = nfc_get_device(idx); 1303 if (!dev) 1304 return -ENODEV; 1305 1306 rc = nfc_disable_se(dev, se_idx); 1307 1308 nfc_put_device(dev); 1309 return rc; 1310 } 1311 1312 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev, 1313 u32 portid, u32 seq, 1314 struct netlink_callback *cb, 1315 int flags) 1316 { 1317 void *hdr; 1318 struct nfc_se *se, *n; 1319 1320 list_for_each_entry_safe(se, n, &dev->secure_elements, list) { 1321 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, 1322 NFC_CMD_GET_SE); 1323 if (!hdr) 1324 goto nla_put_failure; 1325 1326 if (cb) 1327 genl_dump_check_consistent(cb, hdr); 1328 1329 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || 1330 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) || 1331 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type)) 1332 goto nla_put_failure; 1333 1334 genlmsg_end(msg, hdr); 1335 } 1336 1337 return 0; 1338 1339 nla_put_failure: 1340 genlmsg_cancel(msg, hdr); 1341 return -EMSGSIZE; 1342 } 1343 1344 static int nfc_genl_dump_ses(struct sk_buff *skb, 1345 struct netlink_callback *cb) 1346 { 1347 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; 1348 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; 1349 bool first_call = false; 1350 1351 if (!iter) { 1352 first_call = true; 1353 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); 1354 if (!iter) 1355 return -ENOMEM; 1356 cb->args[0] = (long) iter; 1357 } 1358 1359 mutex_lock(&nfc_devlist_mutex); 1360 1361 cb->seq = nfc_devlist_generation; 1362 1363 if (first_call) { 1364 nfc_device_iter_init(iter); 1365 dev = nfc_device_iter_next(iter); 1366 } 1367 1368 while (dev) { 1369 int rc; 1370 1371 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid, 1372 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); 1373 if (rc < 0) 1374 break; 1375 1376 dev = nfc_device_iter_next(iter); 1377 } 1378 1379 mutex_unlock(&nfc_devlist_mutex); 1380 1381 cb->args[1] = (long) dev; 1382 1383 return skb->len; 1384 } 1385 1386 static int nfc_genl_dump_ses_done(struct netlink_callback *cb) 1387 { 1388 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; 1389 1390 nfc_device_iter_exit(iter); 1391 kfree(iter); 1392 1393 return 0; 1394 } 1395 1396 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx, 1397 u8 *apdu, size_t apdu_length, 1398 se_io_cb_t cb, void *cb_context) 1399 { 1400 struct nfc_se *se; 1401 int rc; 1402 1403 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); 1404 1405 device_lock(&dev->dev); 1406 1407 if (!device_is_registered(&dev->dev)) { 1408 rc = -ENODEV; 1409 goto error; 1410 } 1411 1412 if (!dev->dev_up) { 1413 rc = -ENODEV; 1414 goto error; 1415 } 1416 1417 if (!dev->ops->se_io) { 1418 rc = -EOPNOTSUPP; 1419 goto error; 1420 } 1421 1422 se = nfc_find_se(dev, se_idx); 1423 if (!se) { 1424 rc = -EINVAL; 1425 goto error; 1426 } 1427 1428 if (se->state != NFC_SE_ENABLED) { 1429 rc = -ENODEV; 1430 goto error; 1431 } 1432 1433 rc = dev->ops->se_io(dev, se_idx, apdu, 1434 apdu_length, cb, cb_context); 1435 1436 error: 1437 device_unlock(&dev->dev); 1438 return rc; 1439 } 1440 1441 struct se_io_ctx { 1442 u32 dev_idx; 1443 u32 se_idx; 1444 }; 1445 1446 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err) 1447 { 1448 struct se_io_ctx *ctx = context; 1449 struct sk_buff *msg; 1450 void *hdr; 1451 1452 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1453 if (!msg) { 1454 kfree(ctx); 1455 return; 1456 } 1457 1458 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 1459 NFC_CMD_SE_IO); 1460 if (!hdr) 1461 goto free_msg; 1462 1463 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) || 1464 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) || 1465 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu)) 1466 goto nla_put_failure; 1467 1468 genlmsg_end(msg, hdr); 1469 1470 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 1471 1472 kfree(ctx); 1473 1474 return; 1475 1476 nla_put_failure: 1477 free_msg: 1478 nlmsg_free(msg); 1479 kfree(ctx); 1480 1481 return; 1482 } 1483 1484 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info) 1485 { 1486 struct nfc_dev *dev; 1487 struct se_io_ctx *ctx; 1488 u32 dev_idx, se_idx; 1489 u8 *apdu; 1490 size_t apdu_len; 1491 1492 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1493 !info->attrs[NFC_ATTR_SE_INDEX] || 1494 !info->attrs[NFC_ATTR_SE_APDU]) 1495 return -EINVAL; 1496 1497 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1498 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); 1499 1500 dev = nfc_get_device(dev_idx); 1501 if (!dev) 1502 return -ENODEV; 1503 1504 if (!dev->ops || !dev->ops->se_io) 1505 return -ENOTSUPP; 1506 1507 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]); 1508 if (apdu_len == 0) 1509 return -EINVAL; 1510 1511 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]); 1512 if (!apdu) 1513 return -EINVAL; 1514 1515 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL); 1516 if (!ctx) 1517 return -ENOMEM; 1518 1519 ctx->dev_idx = dev_idx; 1520 ctx->se_idx = se_idx; 1521 1522 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx); 1523 } 1524 1525 static int nfc_genl_vendor_cmd(struct sk_buff *skb, 1526 struct genl_info *info) 1527 { 1528 struct nfc_dev *dev; 1529 struct nfc_vendor_cmd *cmd; 1530 u32 dev_idx, vid, subcmd; 1531 u8 *data; 1532 size_t data_len; 1533 int i, err; 1534 1535 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1536 !info->attrs[NFC_ATTR_VENDOR_ID] || 1537 !info->attrs[NFC_ATTR_VENDOR_SUBCMD]) 1538 return -EINVAL; 1539 1540 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1541 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]); 1542 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]); 1543 1544 dev = nfc_get_device(dev_idx); 1545 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds) 1546 return -ENODEV; 1547 1548 if (info->attrs[NFC_ATTR_VENDOR_DATA]) { 1549 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]); 1550 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]); 1551 if (data_len == 0) 1552 return -EINVAL; 1553 } else { 1554 data = NULL; 1555 data_len = 0; 1556 } 1557 1558 for (i = 0; i < dev->n_vendor_cmds; i++) { 1559 cmd = &dev->vendor_cmds[i]; 1560 1561 if (cmd->vendor_id != vid || cmd->subcmd != subcmd) 1562 continue; 1563 1564 dev->cur_cmd_info = info; 1565 err = cmd->doit(dev, data, data_len); 1566 dev->cur_cmd_info = NULL; 1567 return err; 1568 } 1569 1570 return -EOPNOTSUPP; 1571 } 1572 1573 /* message building helper */ 1574 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq, 1575 int flags, u8 cmd) 1576 { 1577 /* since there is no private header just add the generic one */ 1578 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd); 1579 } 1580 1581 static struct sk_buff * 1582 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen, 1583 u32 portid, u32 seq, 1584 enum nfc_attrs attr, 1585 u32 oui, u32 subcmd, gfp_t gfp) 1586 { 1587 struct sk_buff *skb; 1588 void *hdr; 1589 1590 skb = nlmsg_new(approxlen + 100, gfp); 1591 if (!skb) 1592 return NULL; 1593 1594 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR); 1595 if (!hdr) { 1596 kfree_skb(skb); 1597 return NULL; 1598 } 1599 1600 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx)) 1601 goto nla_put_failure; 1602 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui)) 1603 goto nla_put_failure; 1604 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd)) 1605 goto nla_put_failure; 1606 1607 ((void **)skb->cb)[0] = dev; 1608 ((void **)skb->cb)[1] = hdr; 1609 1610 return skb; 1611 1612 nla_put_failure: 1613 kfree_skb(skb); 1614 return NULL; 1615 } 1616 1617 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev, 1618 enum nfc_attrs attr, 1619 u32 oui, u32 subcmd, 1620 int approxlen) 1621 { 1622 if (WARN_ON(!dev->cur_cmd_info)) 1623 return NULL; 1624 1625 return __nfc_alloc_vendor_cmd_skb(dev, approxlen, 1626 dev->cur_cmd_info->snd_portid, 1627 dev->cur_cmd_info->snd_seq, attr, 1628 oui, subcmd, GFP_KERNEL); 1629 } 1630 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb); 1631 1632 int nfc_vendor_cmd_reply(struct sk_buff *skb) 1633 { 1634 struct nfc_dev *dev = ((void **)skb->cb)[0]; 1635 void *hdr = ((void **)skb->cb)[1]; 1636 1637 /* clear CB data for netlink core to own from now on */ 1638 memset(skb->cb, 0, sizeof(skb->cb)); 1639 1640 if (WARN_ON(!dev->cur_cmd_info)) { 1641 kfree_skb(skb); 1642 return -EINVAL; 1643 } 1644 1645 genlmsg_end(skb, hdr); 1646 return genlmsg_reply(skb, dev->cur_cmd_info); 1647 } 1648 EXPORT_SYMBOL(nfc_vendor_cmd_reply); 1649 1650 static const struct genl_ops nfc_genl_ops[] = { 1651 { 1652 .cmd = NFC_CMD_GET_DEVICE, 1653 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1654 .doit = nfc_genl_get_device, 1655 .dumpit = nfc_genl_dump_devices, 1656 .done = nfc_genl_dump_devices_done, 1657 }, 1658 { 1659 .cmd = NFC_CMD_DEV_UP, 1660 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1661 .doit = nfc_genl_dev_up, 1662 }, 1663 { 1664 .cmd = NFC_CMD_DEV_DOWN, 1665 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1666 .doit = nfc_genl_dev_down, 1667 }, 1668 { 1669 .cmd = NFC_CMD_START_POLL, 1670 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1671 .doit = nfc_genl_start_poll, 1672 }, 1673 { 1674 .cmd = NFC_CMD_STOP_POLL, 1675 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1676 .doit = nfc_genl_stop_poll, 1677 }, 1678 { 1679 .cmd = NFC_CMD_DEP_LINK_UP, 1680 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1681 .doit = nfc_genl_dep_link_up, 1682 }, 1683 { 1684 .cmd = NFC_CMD_DEP_LINK_DOWN, 1685 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1686 .doit = nfc_genl_dep_link_down, 1687 }, 1688 { 1689 .cmd = NFC_CMD_GET_TARGET, 1690 .validate = GENL_DONT_VALIDATE_STRICT | 1691 GENL_DONT_VALIDATE_DUMP_STRICT, 1692 .dumpit = nfc_genl_dump_targets, 1693 .done = nfc_genl_dump_targets_done, 1694 }, 1695 { 1696 .cmd = NFC_CMD_LLC_GET_PARAMS, 1697 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1698 .doit = nfc_genl_llc_get_params, 1699 }, 1700 { 1701 .cmd = NFC_CMD_LLC_SET_PARAMS, 1702 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1703 .doit = nfc_genl_llc_set_params, 1704 }, 1705 { 1706 .cmd = NFC_CMD_LLC_SDREQ, 1707 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1708 .doit = nfc_genl_llc_sdreq, 1709 }, 1710 { 1711 .cmd = NFC_CMD_FW_DOWNLOAD, 1712 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1713 .doit = nfc_genl_fw_download, 1714 }, 1715 { 1716 .cmd = NFC_CMD_ENABLE_SE, 1717 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1718 .doit = nfc_genl_enable_se, 1719 }, 1720 { 1721 .cmd = NFC_CMD_DISABLE_SE, 1722 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1723 .doit = nfc_genl_disable_se, 1724 }, 1725 { 1726 .cmd = NFC_CMD_GET_SE, 1727 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1728 .dumpit = nfc_genl_dump_ses, 1729 .done = nfc_genl_dump_ses_done, 1730 }, 1731 { 1732 .cmd = NFC_CMD_SE_IO, 1733 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1734 .doit = nfc_genl_se_io, 1735 }, 1736 { 1737 .cmd = NFC_CMD_ACTIVATE_TARGET, 1738 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1739 .doit = nfc_genl_activate_target, 1740 }, 1741 { 1742 .cmd = NFC_CMD_VENDOR, 1743 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1744 .doit = nfc_genl_vendor_cmd, 1745 }, 1746 { 1747 .cmd = NFC_CMD_DEACTIVATE_TARGET, 1748 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1749 .doit = nfc_genl_deactivate_target, 1750 }, 1751 }; 1752 1753 static struct genl_family nfc_genl_family __ro_after_init = { 1754 .hdrsize = 0, 1755 .name = NFC_GENL_NAME, 1756 .version = NFC_GENL_VERSION, 1757 .maxattr = NFC_ATTR_MAX, 1758 .policy = nfc_genl_policy, 1759 .module = THIS_MODULE, 1760 .ops = nfc_genl_ops, 1761 .n_ops = ARRAY_SIZE(nfc_genl_ops), 1762 .mcgrps = nfc_genl_mcgrps, 1763 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps), 1764 }; 1765 1766 1767 struct urelease_work { 1768 struct work_struct w; 1769 u32 portid; 1770 }; 1771 1772 static void nfc_urelease_event_work(struct work_struct *work) 1773 { 1774 struct urelease_work *w = container_of(work, struct urelease_work, w); 1775 struct class_dev_iter iter; 1776 struct nfc_dev *dev; 1777 1778 pr_debug("portid %d\n", w->portid); 1779 1780 mutex_lock(&nfc_devlist_mutex); 1781 1782 nfc_device_iter_init(&iter); 1783 dev = nfc_device_iter_next(&iter); 1784 1785 while (dev) { 1786 mutex_lock(&dev->genl_data.genl_data_mutex); 1787 1788 if (dev->genl_data.poll_req_portid == w->portid) { 1789 nfc_stop_poll(dev); 1790 dev->genl_data.poll_req_portid = 0; 1791 } 1792 1793 mutex_unlock(&dev->genl_data.genl_data_mutex); 1794 1795 dev = nfc_device_iter_next(&iter); 1796 } 1797 1798 nfc_device_iter_exit(&iter); 1799 1800 mutex_unlock(&nfc_devlist_mutex); 1801 1802 kfree(w); 1803 } 1804 1805 static int nfc_genl_rcv_nl_event(struct notifier_block *this, 1806 unsigned long event, void *ptr) 1807 { 1808 struct netlink_notify *n = ptr; 1809 struct urelease_work *w; 1810 1811 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) 1812 goto out; 1813 1814 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid); 1815 1816 w = kmalloc(sizeof(*w), GFP_ATOMIC); 1817 if (w) { 1818 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work); 1819 w->portid = n->portid; 1820 schedule_work((struct work_struct *) w); 1821 } 1822 1823 out: 1824 return NOTIFY_DONE; 1825 } 1826 1827 void nfc_genl_data_init(struct nfc_genl_data *genl_data) 1828 { 1829 genl_data->poll_req_portid = 0; 1830 mutex_init(&genl_data->genl_data_mutex); 1831 } 1832 1833 void nfc_genl_data_exit(struct nfc_genl_data *genl_data) 1834 { 1835 mutex_destroy(&genl_data->genl_data_mutex); 1836 } 1837 1838 static struct notifier_block nl_notifier = { 1839 .notifier_call = nfc_genl_rcv_nl_event, 1840 }; 1841 1842 /** 1843 * nfc_genl_init() - Initialize netlink interface 1844 * 1845 * This initialization function registers the nfc netlink family. 1846 */ 1847 int __init nfc_genl_init(void) 1848 { 1849 int rc; 1850 1851 rc = genl_register_family(&nfc_genl_family); 1852 if (rc) 1853 return rc; 1854 1855 netlink_register_notifier(&nl_notifier); 1856 1857 return 0; 1858 } 1859 1860 /** 1861 * nfc_genl_exit() - Deinitialize netlink interface 1862 * 1863 * This exit function unregisters the nfc netlink family. 1864 */ 1865 void nfc_genl_exit(void) 1866 { 1867 netlink_unregister_notifier(&nl_notifier); 1868 genl_unregister_family(&nfc_genl_family); 1869 } 1870