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, write to the 20 * Free Software Foundation, Inc., 21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 22 */ 23 24 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ 25 26 #include <net/genetlink.h> 27 #include <linux/nfc.h> 28 #include <linux/slab.h> 29 30 #include "nfc.h" 31 #include "llcp.h" 32 33 static struct genl_multicast_group nfc_genl_event_mcgrp = { 34 .name = NFC_GENL_MCAST_EVENT_NAME, 35 }; 36 37 static struct genl_family nfc_genl_family = { 38 .id = GENL_ID_GENERATE, 39 .hdrsize = 0, 40 .name = NFC_GENL_NAME, 41 .version = NFC_GENL_VERSION, 42 .maxattr = NFC_ATTR_MAX, 43 }; 44 45 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = { 46 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 }, 47 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING, 48 .len = NFC_DEVICE_NAME_MAXSIZE }, 49 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 }, 50 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 }, 51 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 }, 52 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 }, 53 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 }, 54 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 }, 55 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 }, 56 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 }, 57 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 }, 58 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED }, 59 }; 60 61 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = { 62 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING }, 63 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 }, 64 }; 65 66 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target, 67 struct netlink_callback *cb, int flags) 68 { 69 void *hdr; 70 71 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 72 &nfc_genl_family, flags, NFC_CMD_GET_TARGET); 73 if (!hdr) 74 return -EMSGSIZE; 75 76 genl_dump_check_consistent(cb, hdr, &nfc_genl_family); 77 78 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) || 79 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) || 80 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) || 81 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res)) 82 goto nla_put_failure; 83 if (target->nfcid1_len > 0 && 84 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len, 85 target->nfcid1)) 86 goto nla_put_failure; 87 if (target->sensb_res_len > 0 && 88 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len, 89 target->sensb_res)) 90 goto nla_put_failure; 91 if (target->sensf_res_len > 0 && 92 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len, 93 target->sensf_res)) 94 goto nla_put_failure; 95 96 return genlmsg_end(msg, hdr); 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 struct nfc_dev *dev; 106 int rc; 107 u32 idx; 108 109 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize, 110 nfc_genl_family.attrbuf, 111 nfc_genl_family.maxattr, 112 nfc_genl_policy); 113 if (rc < 0) 114 return ERR_PTR(rc); 115 116 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]) 117 return ERR_PTR(-EINVAL); 118 119 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]); 120 121 dev = nfc_get_device(idx); 122 if (!dev) 123 return ERR_PTR(-ENODEV); 124 125 return dev; 126 } 127 128 static int nfc_genl_dump_targets(struct sk_buff *skb, 129 struct netlink_callback *cb) 130 { 131 int i = cb->args[0]; 132 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; 133 int rc; 134 135 if (!dev) { 136 dev = __get_device_from_cb(cb); 137 if (IS_ERR(dev)) 138 return PTR_ERR(dev); 139 140 cb->args[1] = (long) dev; 141 } 142 143 device_lock(&dev->dev); 144 145 cb->seq = dev->targets_generation; 146 147 while (i < dev->n_targets) { 148 rc = nfc_genl_send_target(skb, &dev->targets[i], cb, 149 NLM_F_MULTI); 150 if (rc < 0) 151 break; 152 153 i++; 154 } 155 156 device_unlock(&dev->dev); 157 158 cb->args[0] = i; 159 160 return skb->len; 161 } 162 163 static int nfc_genl_dump_targets_done(struct netlink_callback *cb) 164 { 165 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; 166 167 if (dev) 168 nfc_put_device(dev); 169 170 return 0; 171 } 172 173 int nfc_genl_targets_found(struct nfc_dev *dev) 174 { 175 struct sk_buff *msg; 176 void *hdr; 177 178 dev->genl_data.poll_req_portid = 0; 179 180 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 181 if (!msg) 182 return -ENOMEM; 183 184 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 185 NFC_EVENT_TARGETS_FOUND); 186 if (!hdr) 187 goto free_msg; 188 189 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 190 goto nla_put_failure; 191 192 genlmsg_end(msg, hdr); 193 194 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); 195 196 nla_put_failure: 197 genlmsg_cancel(msg, hdr); 198 free_msg: 199 nlmsg_free(msg); 200 return -EMSGSIZE; 201 } 202 203 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx) 204 { 205 struct sk_buff *msg; 206 void *hdr; 207 208 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 209 if (!msg) 210 return -ENOMEM; 211 212 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 213 NFC_EVENT_TARGET_LOST); 214 if (!hdr) 215 goto free_msg; 216 217 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || 218 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) 219 goto nla_put_failure; 220 221 genlmsg_end(msg, hdr); 222 223 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 224 225 return 0; 226 227 nla_put_failure: 228 genlmsg_cancel(msg, hdr); 229 free_msg: 230 nlmsg_free(msg); 231 return -EMSGSIZE; 232 } 233 234 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol) 235 { 236 struct sk_buff *msg; 237 void *hdr; 238 239 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 240 if (!msg) 241 return -ENOMEM; 242 243 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 244 NFC_EVENT_TM_ACTIVATED); 245 if (!hdr) 246 goto free_msg; 247 248 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 249 goto nla_put_failure; 250 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol)) 251 goto nla_put_failure; 252 253 genlmsg_end(msg, hdr); 254 255 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 256 257 return 0; 258 259 nla_put_failure: 260 genlmsg_cancel(msg, hdr); 261 free_msg: 262 nlmsg_free(msg); 263 return -EMSGSIZE; 264 } 265 266 int nfc_genl_tm_deactivated(struct nfc_dev *dev) 267 { 268 struct sk_buff *msg; 269 void *hdr; 270 271 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 272 if (!msg) 273 return -ENOMEM; 274 275 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 276 NFC_EVENT_TM_DEACTIVATED); 277 if (!hdr) 278 goto free_msg; 279 280 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 281 goto nla_put_failure; 282 283 genlmsg_end(msg, hdr); 284 285 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 286 287 return 0; 288 289 nla_put_failure: 290 genlmsg_cancel(msg, hdr); 291 free_msg: 292 nlmsg_free(msg); 293 return -EMSGSIZE; 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 (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || 311 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || 312 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || 313 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up)) 314 goto nla_put_failure; 315 316 genlmsg_end(msg, hdr); 317 318 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 319 320 return 0; 321 322 nla_put_failure: 323 genlmsg_cancel(msg, hdr); 324 free_msg: 325 nlmsg_free(msg); 326 return -EMSGSIZE; 327 } 328 329 int nfc_genl_device_removed(struct nfc_dev *dev) 330 { 331 struct sk_buff *msg; 332 void *hdr; 333 334 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 335 if (!msg) 336 return -ENOMEM; 337 338 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 339 NFC_EVENT_DEVICE_REMOVED); 340 if (!hdr) 341 goto free_msg; 342 343 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 344 goto nla_put_failure; 345 346 genlmsg_end(msg, hdr); 347 348 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 349 350 return 0; 351 352 nla_put_failure: 353 genlmsg_cancel(msg, hdr); 354 free_msg: 355 nlmsg_free(msg); 356 return -EMSGSIZE; 357 } 358 359 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list) 360 { 361 struct sk_buff *msg; 362 struct nlattr *sdp_attr, *uri_attr; 363 struct nfc_llcp_sdp_tlv *sdres; 364 struct hlist_node *n; 365 void *hdr; 366 int rc = -EMSGSIZE; 367 int i; 368 369 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 370 if (!msg) 371 return -ENOMEM; 372 373 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 374 NFC_EVENT_LLC_SDRES); 375 if (!hdr) 376 goto free_msg; 377 378 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 379 goto nla_put_failure; 380 381 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP); 382 if (sdp_attr == NULL) { 383 rc = -ENOMEM; 384 goto nla_put_failure; 385 } 386 387 i = 1; 388 hlist_for_each_entry_safe(sdres, n, sdres_list, node) { 389 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap); 390 391 uri_attr = nla_nest_start(msg, i++); 392 if (uri_attr == NULL) { 393 rc = -ENOMEM; 394 goto nla_put_failure; 395 } 396 397 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap)) 398 goto nla_put_failure; 399 400 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri)) 401 goto nla_put_failure; 402 403 nla_nest_end(msg, uri_attr); 404 405 hlist_del(&sdres->node); 406 407 nfc_llcp_free_sdp_tlv(sdres); 408 } 409 410 nla_nest_end(msg, sdp_attr); 411 412 genlmsg_end(msg, hdr); 413 414 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); 415 416 nla_put_failure: 417 genlmsg_cancel(msg, hdr); 418 419 free_msg: 420 nlmsg_free(msg); 421 422 nfc_llcp_free_sdp_tlv_list(sdres_list); 423 424 return rc; 425 } 426 427 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev, 428 u32 portid, u32 seq, 429 struct netlink_callback *cb, 430 int flags) 431 { 432 void *hdr; 433 434 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, 435 NFC_CMD_GET_DEVICE); 436 if (!hdr) 437 return -EMSGSIZE; 438 439 if (cb) 440 genl_dump_check_consistent(cb, hdr, &nfc_genl_family); 441 442 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || 443 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || 444 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || 445 nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) || 446 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) || 447 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode)) 448 goto nla_put_failure; 449 450 return genlmsg_end(msg, hdr); 451 452 nla_put_failure: 453 genlmsg_cancel(msg, hdr); 454 return -EMSGSIZE; 455 } 456 457 static int nfc_genl_dump_devices(struct sk_buff *skb, 458 struct netlink_callback *cb) 459 { 460 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; 461 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; 462 bool first_call = false; 463 464 if (!iter) { 465 first_call = true; 466 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); 467 if (!iter) 468 return -ENOMEM; 469 cb->args[0] = (long) iter; 470 } 471 472 mutex_lock(&nfc_devlist_mutex); 473 474 cb->seq = nfc_devlist_generation; 475 476 if (first_call) { 477 nfc_device_iter_init(iter); 478 dev = nfc_device_iter_next(iter); 479 } 480 481 while (dev) { 482 int rc; 483 484 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid, 485 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); 486 if (rc < 0) 487 break; 488 489 dev = nfc_device_iter_next(iter); 490 } 491 492 mutex_unlock(&nfc_devlist_mutex); 493 494 cb->args[1] = (long) dev; 495 496 return skb->len; 497 } 498 499 static int nfc_genl_dump_devices_done(struct netlink_callback *cb) 500 { 501 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; 502 503 nfc_device_iter_exit(iter); 504 kfree(iter); 505 506 return 0; 507 } 508 509 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx, 510 u8 comm_mode, u8 rf_mode) 511 { 512 struct sk_buff *msg; 513 void *hdr; 514 515 pr_debug("DEP link is up\n"); 516 517 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 518 if (!msg) 519 return -ENOMEM; 520 521 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP); 522 if (!hdr) 523 goto free_msg; 524 525 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 526 goto nla_put_failure; 527 if (rf_mode == NFC_RF_INITIATOR && 528 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) 529 goto nla_put_failure; 530 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) || 531 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode)) 532 goto nla_put_failure; 533 534 genlmsg_end(msg, hdr); 535 536 dev->dep_link_up = true; 537 538 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); 539 540 return 0; 541 542 nla_put_failure: 543 genlmsg_cancel(msg, hdr); 544 free_msg: 545 nlmsg_free(msg); 546 return -EMSGSIZE; 547 } 548 549 int nfc_genl_dep_link_down_event(struct nfc_dev *dev) 550 { 551 struct sk_buff *msg; 552 void *hdr; 553 554 pr_debug("DEP link is down\n"); 555 556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 557 if (!msg) 558 return -ENOMEM; 559 560 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, 561 NFC_CMD_DEP_LINK_DOWN); 562 if (!hdr) 563 goto free_msg; 564 565 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) 566 goto nla_put_failure; 567 568 genlmsg_end(msg, hdr); 569 570 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); 571 572 return 0; 573 574 nla_put_failure: 575 genlmsg_cancel(msg, hdr); 576 free_msg: 577 nlmsg_free(msg); 578 return -EMSGSIZE; 579 } 580 581 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) 582 { 583 struct sk_buff *msg; 584 struct nfc_dev *dev; 585 u32 idx; 586 int rc = -ENOBUFS; 587 588 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 589 return -EINVAL; 590 591 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 592 593 dev = nfc_get_device(idx); 594 if (!dev) 595 return -ENODEV; 596 597 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 598 if (!msg) { 599 rc = -ENOMEM; 600 goto out_putdev; 601 } 602 603 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq, 604 NULL, 0); 605 if (rc < 0) 606 goto out_free; 607 608 nfc_put_device(dev); 609 610 return genlmsg_reply(msg, info); 611 612 out_free: 613 nlmsg_free(msg); 614 out_putdev: 615 nfc_put_device(dev); 616 return rc; 617 } 618 619 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) 620 { 621 struct nfc_dev *dev; 622 int rc; 623 u32 idx; 624 625 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 626 return -EINVAL; 627 628 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 629 630 dev = nfc_get_device(idx); 631 if (!dev) 632 return -ENODEV; 633 634 rc = nfc_dev_up(dev); 635 636 nfc_put_device(dev); 637 return rc; 638 } 639 640 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) 641 { 642 struct nfc_dev *dev; 643 int rc; 644 u32 idx; 645 646 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 647 return -EINVAL; 648 649 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 650 651 dev = nfc_get_device(idx); 652 if (!dev) 653 return -ENODEV; 654 655 rc = nfc_dev_down(dev); 656 657 nfc_put_device(dev); 658 return rc; 659 } 660 661 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) 662 { 663 struct nfc_dev *dev; 664 int rc; 665 u32 idx; 666 u32 im_protocols = 0, tm_protocols = 0; 667 668 pr_debug("Poll start\n"); 669 670 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 671 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] && 672 !info->attrs[NFC_ATTR_PROTOCOLS]) && 673 !info->attrs[NFC_ATTR_TM_PROTOCOLS])) 674 return -EINVAL; 675 676 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 677 678 if (info->attrs[NFC_ATTR_TM_PROTOCOLS]) 679 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]); 680 681 if (info->attrs[NFC_ATTR_IM_PROTOCOLS]) 682 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]); 683 else if (info->attrs[NFC_ATTR_PROTOCOLS]) 684 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); 685 686 dev = nfc_get_device(idx); 687 if (!dev) 688 return -ENODEV; 689 690 mutex_lock(&dev->genl_data.genl_data_mutex); 691 692 rc = nfc_start_poll(dev, im_protocols, tm_protocols); 693 if (!rc) 694 dev->genl_data.poll_req_portid = info->snd_portid; 695 696 mutex_unlock(&dev->genl_data.genl_data_mutex); 697 698 nfc_put_device(dev); 699 return rc; 700 } 701 702 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) 703 { 704 struct nfc_dev *dev; 705 int rc; 706 u32 idx; 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 device_lock(&dev->dev); 718 719 if (!dev->polling) { 720 device_unlock(&dev->dev); 721 return -EINVAL; 722 } 723 724 device_unlock(&dev->dev); 725 726 mutex_lock(&dev->genl_data.genl_data_mutex); 727 728 if (dev->genl_data.poll_req_portid != info->snd_portid) { 729 rc = -EBUSY; 730 goto out; 731 } 732 733 rc = nfc_stop_poll(dev); 734 dev->genl_data.poll_req_portid = 0; 735 736 out: 737 mutex_unlock(&dev->genl_data.genl_data_mutex); 738 nfc_put_device(dev); 739 return rc; 740 } 741 742 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info) 743 { 744 struct nfc_dev *dev; 745 int rc, tgt_idx; 746 u32 idx; 747 u8 comm; 748 749 pr_debug("DEP link up\n"); 750 751 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 752 !info->attrs[NFC_ATTR_COMM_MODE]) 753 return -EINVAL; 754 755 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 756 if (!info->attrs[NFC_ATTR_TARGET_INDEX]) 757 tgt_idx = NFC_TARGET_IDX_ANY; 758 else 759 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); 760 761 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]); 762 763 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE) 764 return -EINVAL; 765 766 dev = nfc_get_device(idx); 767 if (!dev) 768 return -ENODEV; 769 770 rc = nfc_dep_link_up(dev, tgt_idx, comm); 771 772 nfc_put_device(dev); 773 774 return rc; 775 } 776 777 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info) 778 { 779 struct nfc_dev *dev; 780 int rc; 781 u32 idx; 782 783 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 784 return -EINVAL; 785 786 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 787 788 dev = nfc_get_device(idx); 789 if (!dev) 790 return -ENODEV; 791 792 rc = nfc_dep_link_down(dev); 793 794 nfc_put_device(dev); 795 return rc; 796 } 797 798 static int nfc_genl_send_params(struct sk_buff *msg, 799 struct nfc_llcp_local *local, 800 u32 portid, u32 seq) 801 { 802 void *hdr; 803 804 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0, 805 NFC_CMD_LLC_GET_PARAMS); 806 if (!hdr) 807 return -EMSGSIZE; 808 809 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) || 810 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) || 811 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) || 812 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux))) 813 goto nla_put_failure; 814 815 return genlmsg_end(msg, hdr); 816 817 nla_put_failure: 818 819 genlmsg_cancel(msg, hdr); 820 return -EMSGSIZE; 821 } 822 823 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info) 824 { 825 struct nfc_dev *dev; 826 struct nfc_llcp_local *local; 827 int rc = 0; 828 struct sk_buff *msg = NULL; 829 u32 idx; 830 831 if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) 832 return -EINVAL; 833 834 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 835 836 dev = nfc_get_device(idx); 837 if (!dev) 838 return -ENODEV; 839 840 device_lock(&dev->dev); 841 842 local = nfc_llcp_find_local(dev); 843 if (!local) { 844 rc = -ENODEV; 845 goto exit; 846 } 847 848 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 849 if (!msg) { 850 rc = -ENOMEM; 851 goto exit; 852 } 853 854 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq); 855 856 exit: 857 device_unlock(&dev->dev); 858 859 nfc_put_device(dev); 860 861 if (rc < 0) { 862 if (msg) 863 nlmsg_free(msg); 864 865 return rc; 866 } 867 868 return genlmsg_reply(msg, info); 869 } 870 871 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info) 872 { 873 struct nfc_dev *dev; 874 struct nfc_llcp_local *local; 875 u8 rw = 0; 876 u16 miux = 0; 877 u32 idx; 878 int rc = 0; 879 880 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 881 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] && 882 !info->attrs[NFC_ATTR_LLC_PARAM_RW] && 883 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX])) 884 return -EINVAL; 885 886 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) { 887 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]); 888 889 if (rw > LLCP_MAX_RW) 890 return -EINVAL; 891 } 892 893 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) { 894 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]); 895 896 if (miux > LLCP_MAX_MIUX) 897 return -EINVAL; 898 } 899 900 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 901 902 dev = nfc_get_device(idx); 903 if (!dev) 904 return -ENODEV; 905 906 device_lock(&dev->dev); 907 908 local = nfc_llcp_find_local(dev); 909 if (!local) { 910 nfc_put_device(dev); 911 rc = -ENODEV; 912 goto exit; 913 } 914 915 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) { 916 if (dev->dep_link_up) { 917 rc = -EINPROGRESS; 918 goto exit; 919 } 920 921 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]); 922 } 923 924 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) 925 local->rw = rw; 926 927 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) 928 local->miux = cpu_to_be16(miux); 929 930 exit: 931 device_unlock(&dev->dev); 932 933 nfc_put_device(dev); 934 935 return rc; 936 } 937 938 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info) 939 { 940 struct nfc_dev *dev; 941 struct nfc_llcp_local *local; 942 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1]; 943 u32 idx; 944 u8 tid; 945 char *uri; 946 int rc = 0, rem; 947 size_t uri_len, tlvs_len; 948 struct hlist_head sdreq_list; 949 struct nfc_llcp_sdp_tlv *sdreq; 950 951 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || 952 !info->attrs[NFC_ATTR_LLC_SDP]) 953 return -EINVAL; 954 955 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); 956 957 dev = nfc_get_device(idx); 958 if (!dev) { 959 rc = -ENODEV; 960 goto exit; 961 } 962 963 device_lock(&dev->dev); 964 965 if (dev->dep_link_up == false) { 966 rc = -ENOLINK; 967 goto exit; 968 } 969 970 local = nfc_llcp_find_local(dev); 971 if (!local) { 972 nfc_put_device(dev); 973 rc = -ENODEV; 974 goto exit; 975 } 976 977 INIT_HLIST_HEAD(&sdreq_list); 978 979 tlvs_len = 0; 980 981 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) { 982 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr, 983 nfc_sdp_genl_policy); 984 985 if (rc != 0) { 986 rc = -EINVAL; 987 goto exit; 988 } 989 990 if (!sdp_attrs[NFC_SDP_ATTR_URI]) 991 continue; 992 993 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]); 994 if (uri_len == 0) 995 continue; 996 997 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]); 998 if (uri == NULL || *uri == 0) 999 continue; 1000 1001 tid = local->sdreq_next_tid++; 1002 1003 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len); 1004 if (sdreq == NULL) { 1005 rc = -ENOMEM; 1006 goto exit; 1007 } 1008 1009 tlvs_len += sdreq->tlv_len; 1010 1011 hlist_add_head(&sdreq->node, &sdreq_list); 1012 } 1013 1014 if (hlist_empty(&sdreq_list)) { 1015 rc = -EINVAL; 1016 goto exit; 1017 } 1018 1019 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len); 1020 exit: 1021 device_unlock(&dev->dev); 1022 1023 nfc_put_device(dev); 1024 1025 return rc; 1026 } 1027 1028 static struct genl_ops nfc_genl_ops[] = { 1029 { 1030 .cmd = NFC_CMD_GET_DEVICE, 1031 .doit = nfc_genl_get_device, 1032 .dumpit = nfc_genl_dump_devices, 1033 .done = nfc_genl_dump_devices_done, 1034 .policy = nfc_genl_policy, 1035 }, 1036 { 1037 .cmd = NFC_CMD_DEV_UP, 1038 .doit = nfc_genl_dev_up, 1039 .policy = nfc_genl_policy, 1040 }, 1041 { 1042 .cmd = NFC_CMD_DEV_DOWN, 1043 .doit = nfc_genl_dev_down, 1044 .policy = nfc_genl_policy, 1045 }, 1046 { 1047 .cmd = NFC_CMD_START_POLL, 1048 .doit = nfc_genl_start_poll, 1049 .policy = nfc_genl_policy, 1050 }, 1051 { 1052 .cmd = NFC_CMD_STOP_POLL, 1053 .doit = nfc_genl_stop_poll, 1054 .policy = nfc_genl_policy, 1055 }, 1056 { 1057 .cmd = NFC_CMD_DEP_LINK_UP, 1058 .doit = nfc_genl_dep_link_up, 1059 .policy = nfc_genl_policy, 1060 }, 1061 { 1062 .cmd = NFC_CMD_DEP_LINK_DOWN, 1063 .doit = nfc_genl_dep_link_down, 1064 .policy = nfc_genl_policy, 1065 }, 1066 { 1067 .cmd = NFC_CMD_GET_TARGET, 1068 .dumpit = nfc_genl_dump_targets, 1069 .done = nfc_genl_dump_targets_done, 1070 .policy = nfc_genl_policy, 1071 }, 1072 { 1073 .cmd = NFC_CMD_LLC_GET_PARAMS, 1074 .doit = nfc_genl_llc_get_params, 1075 .policy = nfc_genl_policy, 1076 }, 1077 { 1078 .cmd = NFC_CMD_LLC_SET_PARAMS, 1079 .doit = nfc_genl_llc_set_params, 1080 .policy = nfc_genl_policy, 1081 }, 1082 { 1083 .cmd = NFC_CMD_LLC_SDREQ, 1084 .doit = nfc_genl_llc_sdreq, 1085 .policy = nfc_genl_policy, 1086 }, 1087 }; 1088 1089 1090 struct urelease_work { 1091 struct work_struct w; 1092 int portid; 1093 }; 1094 1095 static void nfc_urelease_event_work(struct work_struct *work) 1096 { 1097 struct urelease_work *w = container_of(work, struct urelease_work, w); 1098 struct class_dev_iter iter; 1099 struct nfc_dev *dev; 1100 1101 pr_debug("portid %d\n", w->portid); 1102 1103 mutex_lock(&nfc_devlist_mutex); 1104 1105 nfc_device_iter_init(&iter); 1106 dev = nfc_device_iter_next(&iter); 1107 1108 while (dev) { 1109 mutex_lock(&dev->genl_data.genl_data_mutex); 1110 1111 if (dev->genl_data.poll_req_portid == w->portid) { 1112 nfc_stop_poll(dev); 1113 dev->genl_data.poll_req_portid = 0; 1114 } 1115 1116 mutex_unlock(&dev->genl_data.genl_data_mutex); 1117 1118 dev = nfc_device_iter_next(&iter); 1119 } 1120 1121 nfc_device_iter_exit(&iter); 1122 1123 mutex_unlock(&nfc_devlist_mutex); 1124 1125 kfree(w); 1126 } 1127 1128 static int nfc_genl_rcv_nl_event(struct notifier_block *this, 1129 unsigned long event, void *ptr) 1130 { 1131 struct netlink_notify *n = ptr; 1132 struct urelease_work *w; 1133 1134 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) 1135 goto out; 1136 1137 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid); 1138 1139 w = kmalloc(sizeof(*w), GFP_ATOMIC); 1140 if (w) { 1141 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work); 1142 w->portid = n->portid; 1143 schedule_work((struct work_struct *) w); 1144 } 1145 1146 out: 1147 return NOTIFY_DONE; 1148 } 1149 1150 void nfc_genl_data_init(struct nfc_genl_data *genl_data) 1151 { 1152 genl_data->poll_req_portid = 0; 1153 mutex_init(&genl_data->genl_data_mutex); 1154 } 1155 1156 void nfc_genl_data_exit(struct nfc_genl_data *genl_data) 1157 { 1158 mutex_destroy(&genl_data->genl_data_mutex); 1159 } 1160 1161 static struct notifier_block nl_notifier = { 1162 .notifier_call = nfc_genl_rcv_nl_event, 1163 }; 1164 1165 /** 1166 * nfc_genl_init() - Initialize netlink interface 1167 * 1168 * This initialization function registers the nfc netlink family. 1169 */ 1170 int __init nfc_genl_init(void) 1171 { 1172 int rc; 1173 1174 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops, 1175 ARRAY_SIZE(nfc_genl_ops)); 1176 if (rc) 1177 return rc; 1178 1179 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp); 1180 1181 netlink_register_notifier(&nl_notifier); 1182 1183 return rc; 1184 } 1185 1186 /** 1187 * nfc_genl_exit() - Deinitialize netlink interface 1188 * 1189 * This exit function unregisters the nfc netlink family. 1190 */ 1191 void nfc_genl_exit(void) 1192 { 1193 netlink_unregister_notifier(&nl_notifier); 1194 genl_unregister_family(&nfc_genl_family); 1195 } 1196