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 nfc_put_device(dev); 1095 rc = -ENODEV; 1096 goto exit; 1097 } 1098 1099 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) { 1100 if (dev->dep_link_up) { 1101 rc = -EINPROGRESS; 1102 goto exit; 1103 } 1104 1105 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]); 1106 } 1107 1108 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) 1109 local->rw = rw; 1110 1111 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) 1112 local->miux = cpu_to_be16(miux); 1113 1114 exit: 1115 device_unlock(&dev->dev); 1116 1117 nfc_put_device(dev); 1118 1119 return rc; 1120 } 1121 1122 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info) 1123 { 1124 struct nfc_dev *dev; 1125 struct nfc_llcp_local *local; 1126 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1]; 1127 u32 idx; 1128 u8 tid; 1129 char *uri; 1130 int rc = 0, rem; 1131 size_t uri_len, tlvs_len; 1132 struct hlist_head sdreq_list; 1133 struct nfc_llcp_sdp_tlv *sdreq; 1134 1135 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1136 !info->attrs[NFC_ATTR_LLC_SDP]) 1137 return -EINVAL; 1138 1139 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1140 1141 dev = nfc_get_device(idx); 1142 if (!dev) 1143 return -ENODEV; 1144 1145 device_lock(&dev->dev); 1146 1147 if (dev->dep_link_up == false) { 1148 rc = -ENOLINK; 1149 goto exit; 1150 } 1151 1152 local = nfc_llcp_find_local(dev); 1153 if (!local) { 1154 nfc_put_device(dev); 1155 rc = -ENODEV; 1156 goto exit; 1157 } 1158 1159 INIT_HLIST_HEAD(&sdreq_list); 1160 1161 tlvs_len = 0; 1162 1163 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) { 1164 rc = nla_parse_nested_deprecated(sdp_attrs, NFC_SDP_ATTR_MAX, 1165 attr, nfc_sdp_genl_policy, 1166 info->extack); 1167 1168 if (rc != 0) { 1169 rc = -EINVAL; 1170 goto exit; 1171 } 1172 1173 if (!sdp_attrs[NFC_SDP_ATTR_URI]) 1174 continue; 1175 1176 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]); 1177 if (uri_len == 0) 1178 continue; 1179 1180 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]); 1181 if (uri == NULL || *uri == 0) 1182 continue; 1183 1184 tid = local->sdreq_next_tid++; 1185 1186 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len); 1187 if (sdreq == NULL) { 1188 rc = -ENOMEM; 1189 goto exit; 1190 } 1191 1192 tlvs_len += sdreq->tlv_len; 1193 1194 hlist_add_head(&sdreq->node, &sdreq_list); 1195 } 1196 1197 if (hlist_empty(&sdreq_list)) { 1198 rc = -EINVAL; 1199 goto exit; 1200 } 1201 1202 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len); 1203 exit: 1204 device_unlock(&dev->dev); 1205 1206 nfc_put_device(dev); 1207 1208 return rc; 1209 } 1210 1211 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info) 1212 { 1213 struct nfc_dev *dev; 1214 int rc; 1215 u32 idx; 1216 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1]; 1217 1218 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 1219 return -EINVAL; 1220 1221 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1222 1223 dev = nfc_get_device(idx); 1224 if (!dev) 1225 return -ENODEV; 1226 1227 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME], 1228 sizeof(firmware_name)); 1229 1230 rc = nfc_fw_download(dev, firmware_name); 1231 1232 nfc_put_device(dev); 1233 return rc; 1234 } 1235 1236 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name, 1237 u32 result) 1238 { 1239 struct sk_buff *msg; 1240 void *hdr; 1241 1242 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1243 if (!msg) 1244 return -ENOMEM; 1245 1246 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 1247 NFC_CMD_FW_DOWNLOAD); 1248 if (!hdr) 1249 goto free_msg; 1250 1251 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) || 1252 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) || 1253 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 1254 goto nla_put_failure; 1255 1256 genlmsg_end(msg, hdr); 1257 1258 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 1259 1260 return 0; 1261 1262 nla_put_failure: 1263 free_msg: 1264 nlmsg_free(msg); 1265 return -EMSGSIZE; 1266 } 1267 1268 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info) 1269 { 1270 struct nfc_dev *dev; 1271 int rc; 1272 u32 idx, se_idx; 1273 1274 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1275 !info->attrs[NFC_ATTR_SE_INDEX]) 1276 return -EINVAL; 1277 1278 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1279 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); 1280 1281 dev = nfc_get_device(idx); 1282 if (!dev) 1283 return -ENODEV; 1284 1285 rc = nfc_enable_se(dev, se_idx); 1286 1287 nfc_put_device(dev); 1288 return rc; 1289 } 1290 1291 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info) 1292 { 1293 struct nfc_dev *dev; 1294 int rc; 1295 u32 idx, se_idx; 1296 1297 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1298 !info->attrs[NFC_ATTR_SE_INDEX]) 1299 return -EINVAL; 1300 1301 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1302 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); 1303 1304 dev = nfc_get_device(idx); 1305 if (!dev) 1306 return -ENODEV; 1307 1308 rc = nfc_disable_se(dev, se_idx); 1309 1310 nfc_put_device(dev); 1311 return rc; 1312 } 1313 1314 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev, 1315 u32 portid, u32 seq, 1316 struct netlink_callback *cb, 1317 int flags) 1318 { 1319 void *hdr; 1320 struct nfc_se *se, *n; 1321 1322 list_for_each_entry_safe(se, n, &dev->secure_elements, list) { 1323 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, 1324 NFC_CMD_GET_SE); 1325 if (!hdr) 1326 goto nla_put_failure; 1327 1328 if (cb) 1329 genl_dump_check_consistent(cb, hdr); 1330 1331 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || 1332 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) || 1333 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type)) 1334 goto nla_put_failure; 1335 1336 genlmsg_end(msg, hdr); 1337 } 1338 1339 return 0; 1340 1341 nla_put_failure: 1342 genlmsg_cancel(msg, hdr); 1343 return -EMSGSIZE; 1344 } 1345 1346 static int nfc_genl_dump_ses(struct sk_buff *skb, 1347 struct netlink_callback *cb) 1348 { 1349 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; 1350 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; 1351 bool first_call = false; 1352 1353 if (!iter) { 1354 first_call = true; 1355 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); 1356 if (!iter) 1357 return -ENOMEM; 1358 cb->args[0] = (long) iter; 1359 } 1360 1361 mutex_lock(&nfc_devlist_mutex); 1362 1363 cb->seq = nfc_devlist_generation; 1364 1365 if (first_call) { 1366 nfc_device_iter_init(iter); 1367 dev = nfc_device_iter_next(iter); 1368 } 1369 1370 while (dev) { 1371 int rc; 1372 1373 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid, 1374 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); 1375 if (rc < 0) 1376 break; 1377 1378 dev = nfc_device_iter_next(iter); 1379 } 1380 1381 mutex_unlock(&nfc_devlist_mutex); 1382 1383 cb->args[1] = (long) dev; 1384 1385 return skb->len; 1386 } 1387 1388 static int nfc_genl_dump_ses_done(struct netlink_callback *cb) 1389 { 1390 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; 1391 1392 nfc_device_iter_exit(iter); 1393 kfree(iter); 1394 1395 return 0; 1396 } 1397 1398 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx, 1399 u8 *apdu, size_t apdu_length, 1400 se_io_cb_t cb, void *cb_context) 1401 { 1402 struct nfc_se *se; 1403 int rc; 1404 1405 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); 1406 1407 device_lock(&dev->dev); 1408 1409 if (!device_is_registered(&dev->dev)) { 1410 rc = -ENODEV; 1411 goto error; 1412 } 1413 1414 if (!dev->dev_up) { 1415 rc = -ENODEV; 1416 goto error; 1417 } 1418 1419 if (!dev->ops->se_io) { 1420 rc = -EOPNOTSUPP; 1421 goto error; 1422 } 1423 1424 se = nfc_find_se(dev, se_idx); 1425 if (!se) { 1426 rc = -EINVAL; 1427 goto error; 1428 } 1429 1430 if (se->state != NFC_SE_ENABLED) { 1431 rc = -ENODEV; 1432 goto error; 1433 } 1434 1435 rc = dev->ops->se_io(dev, se_idx, apdu, 1436 apdu_length, cb, cb_context); 1437 1438 error: 1439 device_unlock(&dev->dev); 1440 return rc; 1441 } 1442 1443 struct se_io_ctx { 1444 u32 dev_idx; 1445 u32 se_idx; 1446 }; 1447 1448 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err) 1449 { 1450 struct se_io_ctx *ctx = context; 1451 struct sk_buff *msg; 1452 void *hdr; 1453 1454 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1455 if (!msg) { 1456 kfree(ctx); 1457 return; 1458 } 1459 1460 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 1461 NFC_CMD_SE_IO); 1462 if (!hdr) 1463 goto free_msg; 1464 1465 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) || 1466 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) || 1467 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu)) 1468 goto nla_put_failure; 1469 1470 genlmsg_end(msg, hdr); 1471 1472 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); 1473 1474 kfree(ctx); 1475 1476 return; 1477 1478 nla_put_failure: 1479 free_msg: 1480 nlmsg_free(msg); 1481 kfree(ctx); 1482 1483 return; 1484 } 1485 1486 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info) 1487 { 1488 struct nfc_dev *dev; 1489 struct se_io_ctx *ctx; 1490 u32 dev_idx, se_idx; 1491 u8 *apdu; 1492 size_t apdu_len; 1493 1494 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1495 !info->attrs[NFC_ATTR_SE_INDEX] || 1496 !info->attrs[NFC_ATTR_SE_APDU]) 1497 return -EINVAL; 1498 1499 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1500 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); 1501 1502 dev = nfc_get_device(dev_idx); 1503 if (!dev) 1504 return -ENODEV; 1505 1506 if (!dev->ops || !dev->ops->se_io) 1507 return -ENOTSUPP; 1508 1509 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]); 1510 if (apdu_len == 0) 1511 return -EINVAL; 1512 1513 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]); 1514 if (!apdu) 1515 return -EINVAL; 1516 1517 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL); 1518 if (!ctx) 1519 return -ENOMEM; 1520 1521 ctx->dev_idx = dev_idx; 1522 ctx->se_idx = se_idx; 1523 1524 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx); 1525 } 1526 1527 static int nfc_genl_vendor_cmd(struct sk_buff *skb, 1528 struct genl_info *info) 1529 { 1530 struct nfc_dev *dev; 1531 struct nfc_vendor_cmd *cmd; 1532 u32 dev_idx, vid, subcmd; 1533 u8 *data; 1534 size_t data_len; 1535 int i, err; 1536 1537 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 1538 !info->attrs[NFC_ATTR_VENDOR_ID] || 1539 !info->attrs[NFC_ATTR_VENDOR_SUBCMD]) 1540 return -EINVAL; 1541 1542 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 1543 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]); 1544 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]); 1545 1546 dev = nfc_get_device(dev_idx); 1547 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds) 1548 return -ENODEV; 1549 1550 if (info->attrs[NFC_ATTR_VENDOR_DATA]) { 1551 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]); 1552 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]); 1553 if (data_len == 0) 1554 return -EINVAL; 1555 } else { 1556 data = NULL; 1557 data_len = 0; 1558 } 1559 1560 for (i = 0; i < dev->n_vendor_cmds; i++) { 1561 cmd = &dev->vendor_cmds[i]; 1562 1563 if (cmd->vendor_id != vid || cmd->subcmd != subcmd) 1564 continue; 1565 1566 dev->cur_cmd_info = info; 1567 err = cmd->doit(dev, data, data_len); 1568 dev->cur_cmd_info = NULL; 1569 return err; 1570 } 1571 1572 return -EOPNOTSUPP; 1573 } 1574 1575 /* message building helper */ 1576 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq, 1577 int flags, u8 cmd) 1578 { 1579 /* since there is no private header just add the generic one */ 1580 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd); 1581 } 1582 1583 static struct sk_buff * 1584 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen, 1585 u32 portid, u32 seq, 1586 enum nfc_attrs attr, 1587 u32 oui, u32 subcmd, gfp_t gfp) 1588 { 1589 struct sk_buff *skb; 1590 void *hdr; 1591 1592 skb = nlmsg_new(approxlen + 100, gfp); 1593 if (!skb) 1594 return NULL; 1595 1596 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR); 1597 if (!hdr) { 1598 kfree_skb(skb); 1599 return NULL; 1600 } 1601 1602 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx)) 1603 goto nla_put_failure; 1604 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui)) 1605 goto nla_put_failure; 1606 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd)) 1607 goto nla_put_failure; 1608 1609 ((void **)skb->cb)[0] = dev; 1610 ((void **)skb->cb)[1] = hdr; 1611 1612 return skb; 1613 1614 nla_put_failure: 1615 kfree_skb(skb); 1616 return NULL; 1617 } 1618 1619 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev, 1620 enum nfc_attrs attr, 1621 u32 oui, u32 subcmd, 1622 int approxlen) 1623 { 1624 if (WARN_ON(!dev->cur_cmd_info)) 1625 return NULL; 1626 1627 return __nfc_alloc_vendor_cmd_skb(dev, approxlen, 1628 dev->cur_cmd_info->snd_portid, 1629 dev->cur_cmd_info->snd_seq, attr, 1630 oui, subcmd, GFP_KERNEL); 1631 } 1632 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb); 1633 1634 int nfc_vendor_cmd_reply(struct sk_buff *skb) 1635 { 1636 struct nfc_dev *dev = ((void **)skb->cb)[0]; 1637 void *hdr = ((void **)skb->cb)[1]; 1638 1639 /* clear CB data for netlink core to own from now on */ 1640 memset(skb->cb, 0, sizeof(skb->cb)); 1641 1642 if (WARN_ON(!dev->cur_cmd_info)) { 1643 kfree_skb(skb); 1644 return -EINVAL; 1645 } 1646 1647 genlmsg_end(skb, hdr); 1648 return genlmsg_reply(skb, dev->cur_cmd_info); 1649 } 1650 EXPORT_SYMBOL(nfc_vendor_cmd_reply); 1651 1652 static const struct genl_ops nfc_genl_ops[] = { 1653 { 1654 .cmd = NFC_CMD_GET_DEVICE, 1655 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1656 .doit = nfc_genl_get_device, 1657 .dumpit = nfc_genl_dump_devices, 1658 .done = nfc_genl_dump_devices_done, 1659 }, 1660 { 1661 .cmd = NFC_CMD_DEV_UP, 1662 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1663 .doit = nfc_genl_dev_up, 1664 }, 1665 { 1666 .cmd = NFC_CMD_DEV_DOWN, 1667 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1668 .doit = nfc_genl_dev_down, 1669 }, 1670 { 1671 .cmd = NFC_CMD_START_POLL, 1672 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1673 .doit = nfc_genl_start_poll, 1674 }, 1675 { 1676 .cmd = NFC_CMD_STOP_POLL, 1677 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1678 .doit = nfc_genl_stop_poll, 1679 }, 1680 { 1681 .cmd = NFC_CMD_DEP_LINK_UP, 1682 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1683 .doit = nfc_genl_dep_link_up, 1684 }, 1685 { 1686 .cmd = NFC_CMD_DEP_LINK_DOWN, 1687 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1688 .doit = nfc_genl_dep_link_down, 1689 }, 1690 { 1691 .cmd = NFC_CMD_GET_TARGET, 1692 .validate = GENL_DONT_VALIDATE_STRICT | 1693 GENL_DONT_VALIDATE_DUMP_STRICT, 1694 .dumpit = nfc_genl_dump_targets, 1695 .done = nfc_genl_dump_targets_done, 1696 }, 1697 { 1698 .cmd = NFC_CMD_LLC_GET_PARAMS, 1699 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1700 .doit = nfc_genl_llc_get_params, 1701 }, 1702 { 1703 .cmd = NFC_CMD_LLC_SET_PARAMS, 1704 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1705 .doit = nfc_genl_llc_set_params, 1706 }, 1707 { 1708 .cmd = NFC_CMD_LLC_SDREQ, 1709 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1710 .doit = nfc_genl_llc_sdreq, 1711 }, 1712 { 1713 .cmd = NFC_CMD_FW_DOWNLOAD, 1714 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1715 .doit = nfc_genl_fw_download, 1716 }, 1717 { 1718 .cmd = NFC_CMD_ENABLE_SE, 1719 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1720 .doit = nfc_genl_enable_se, 1721 }, 1722 { 1723 .cmd = NFC_CMD_DISABLE_SE, 1724 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1725 .doit = nfc_genl_disable_se, 1726 }, 1727 { 1728 .cmd = NFC_CMD_GET_SE, 1729 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1730 .dumpit = nfc_genl_dump_ses, 1731 .done = nfc_genl_dump_ses_done, 1732 }, 1733 { 1734 .cmd = NFC_CMD_SE_IO, 1735 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1736 .doit = nfc_genl_se_io, 1737 }, 1738 { 1739 .cmd = NFC_CMD_ACTIVATE_TARGET, 1740 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1741 .doit = nfc_genl_activate_target, 1742 }, 1743 { 1744 .cmd = NFC_CMD_VENDOR, 1745 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1746 .doit = nfc_genl_vendor_cmd, 1747 }, 1748 { 1749 .cmd = NFC_CMD_DEACTIVATE_TARGET, 1750 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1751 .doit = nfc_genl_deactivate_target, 1752 }, 1753 }; 1754 1755 static struct genl_family nfc_genl_family __ro_after_init = { 1756 .hdrsize = 0, 1757 .name = NFC_GENL_NAME, 1758 .version = NFC_GENL_VERSION, 1759 .maxattr = NFC_ATTR_MAX, 1760 .policy = nfc_genl_policy, 1761 .module = THIS_MODULE, 1762 .ops = nfc_genl_ops, 1763 .n_ops = ARRAY_SIZE(nfc_genl_ops), 1764 .mcgrps = nfc_genl_mcgrps, 1765 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps), 1766 }; 1767 1768 1769 struct urelease_work { 1770 struct work_struct w; 1771 u32 portid; 1772 }; 1773 1774 static void nfc_urelease_event_work(struct work_struct *work) 1775 { 1776 struct urelease_work *w = container_of(work, struct urelease_work, w); 1777 struct class_dev_iter iter; 1778 struct nfc_dev *dev; 1779 1780 pr_debug("portid %d\n", w->portid); 1781 1782 mutex_lock(&nfc_devlist_mutex); 1783 1784 nfc_device_iter_init(&iter); 1785 dev = nfc_device_iter_next(&iter); 1786 1787 while (dev) { 1788 mutex_lock(&dev->genl_data.genl_data_mutex); 1789 1790 if (dev->genl_data.poll_req_portid == w->portid) { 1791 nfc_stop_poll(dev); 1792 dev->genl_data.poll_req_portid = 0; 1793 } 1794 1795 mutex_unlock(&dev->genl_data.genl_data_mutex); 1796 1797 dev = nfc_device_iter_next(&iter); 1798 } 1799 1800 nfc_device_iter_exit(&iter); 1801 1802 mutex_unlock(&nfc_devlist_mutex); 1803 1804 kfree(w); 1805 } 1806 1807 static int nfc_genl_rcv_nl_event(struct notifier_block *this, 1808 unsigned long event, void *ptr) 1809 { 1810 struct netlink_notify *n = ptr; 1811 struct urelease_work *w; 1812 1813 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) 1814 goto out; 1815 1816 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid); 1817 1818 w = kmalloc(sizeof(*w), GFP_ATOMIC); 1819 if (w) { 1820 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work); 1821 w->portid = n->portid; 1822 schedule_work((struct work_struct *) w); 1823 } 1824 1825 out: 1826 return NOTIFY_DONE; 1827 } 1828 1829 void nfc_genl_data_init(struct nfc_genl_data *genl_data) 1830 { 1831 genl_data->poll_req_portid = 0; 1832 mutex_init(&genl_data->genl_data_mutex); 1833 } 1834 1835 void nfc_genl_data_exit(struct nfc_genl_data *genl_data) 1836 { 1837 mutex_destroy(&genl_data->genl_data_mutex); 1838 } 1839 1840 static struct notifier_block nl_notifier = { 1841 .notifier_call = nfc_genl_rcv_nl_event, 1842 }; 1843 1844 /** 1845 * nfc_genl_init() - Initialize netlink interface 1846 * 1847 * This initialization function registers the nfc netlink family. 1848 */ 1849 int __init nfc_genl_init(void) 1850 { 1851 int rc; 1852 1853 rc = genl_register_family(&nfc_genl_family); 1854 if (rc) 1855 return rc; 1856 1857 netlink_register_notifier(&nl_notifier); 1858 1859 return 0; 1860 } 1861 1862 /** 1863 * nfc_genl_exit() - Deinitialize netlink interface 1864 * 1865 * This exit function unregisters the nfc netlink family. 1866 */ 1867 void nfc_genl_exit(void) 1868 { 1869 netlink_unregister_notifier(&nl_notifier); 1870 genl_unregister_family(&nfc_genl_family); 1871 } 1872