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