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