0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
0017
0018 #include <net/genetlink.h>
0019 #include <linux/nfc.h>
0020 #include <linux/slab.h>
0021
0022 #include "nfc.h"
0023 #include "llcp.h"
0024
0025 static const struct genl_multicast_group nfc_genl_mcgrps[] = {
0026 { .name = NFC_GENL_MCAST_EVENT_NAME, },
0027 };
0028
0029 static struct genl_family nfc_genl_family;
0030 static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
0031 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
0032 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
0033 .len = NFC_DEVICE_NAME_MAXSIZE },
0034 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
0035 [NFC_ATTR_TARGET_INDEX] = { .type = NLA_U32 },
0036 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
0037 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
0038 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
0039 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
0040 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
0041 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
0042 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
0043 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
0044 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
0045 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
0046 .len = NFC_FIRMWARE_NAME_MAXSIZE },
0047 [NFC_ATTR_SE_INDEX] = { .type = NLA_U32 },
0048 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
0049 [NFC_ATTR_VENDOR_ID] = { .type = NLA_U32 },
0050 [NFC_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
0051 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
0052
0053 };
0054
0055 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
0056 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING,
0057 .len = U8_MAX - 4 },
0058 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
0059 };
0060
0061 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
0062 struct netlink_callback *cb, int flags)
0063 {
0064 void *hdr;
0065
0066 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
0067 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
0068 if (!hdr)
0069 return -EMSGSIZE;
0070
0071 genl_dump_check_consistent(cb, hdr);
0072
0073 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
0074 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
0075 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
0076 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
0077 goto nla_put_failure;
0078 if (target->nfcid1_len > 0 &&
0079 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
0080 target->nfcid1))
0081 goto nla_put_failure;
0082 if (target->sensb_res_len > 0 &&
0083 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
0084 target->sensb_res))
0085 goto nla_put_failure;
0086 if (target->sensf_res_len > 0 &&
0087 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
0088 target->sensf_res))
0089 goto nla_put_failure;
0090
0091 if (target->is_iso15693) {
0092 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
0093 target->iso15693_dsfid) ||
0094 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
0095 sizeof(target->iso15693_uid), target->iso15693_uid))
0096 goto nla_put_failure;
0097 }
0098
0099 genlmsg_end(msg, hdr);
0100 return 0;
0101
0102 nla_put_failure:
0103 genlmsg_cancel(msg, hdr);
0104 return -EMSGSIZE;
0105 }
0106
0107 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
0108 {
0109 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
0110 struct nfc_dev *dev;
0111 u32 idx;
0112
0113 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
0114 return ERR_PTR(-EINVAL);
0115
0116 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
0117
0118 dev = nfc_get_device(idx);
0119 if (!dev)
0120 return ERR_PTR(-ENODEV);
0121
0122 return dev;
0123 }
0124
0125 static int nfc_genl_dump_targets(struct sk_buff *skb,
0126 struct netlink_callback *cb)
0127 {
0128 int i = cb->args[0];
0129 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
0130 int rc;
0131
0132 if (!dev) {
0133 dev = __get_device_from_cb(cb);
0134 if (IS_ERR(dev))
0135 return PTR_ERR(dev);
0136
0137 cb->args[1] = (long) dev;
0138 }
0139
0140 device_lock(&dev->dev);
0141
0142 cb->seq = dev->targets_generation;
0143
0144 while (i < dev->n_targets) {
0145 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
0146 NLM_F_MULTI);
0147 if (rc < 0)
0148 break;
0149
0150 i++;
0151 }
0152
0153 device_unlock(&dev->dev);
0154
0155 cb->args[0] = i;
0156
0157 return skb->len;
0158 }
0159
0160 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
0161 {
0162 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
0163
0164 if (dev)
0165 nfc_put_device(dev);
0166
0167 return 0;
0168 }
0169
0170 int nfc_genl_targets_found(struct nfc_dev *dev)
0171 {
0172 struct sk_buff *msg;
0173 void *hdr;
0174
0175 dev->genl_data.poll_req_portid = 0;
0176
0177 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
0178 if (!msg)
0179 return -ENOMEM;
0180
0181 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0182 NFC_EVENT_TARGETS_FOUND);
0183 if (!hdr)
0184 goto free_msg;
0185
0186 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
0187 goto nla_put_failure;
0188
0189 genlmsg_end(msg, hdr);
0190
0191 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
0192
0193 nla_put_failure:
0194 free_msg:
0195 nlmsg_free(msg);
0196 return -EMSGSIZE;
0197 }
0198
0199 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
0200 {
0201 struct sk_buff *msg;
0202 void *hdr;
0203
0204 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0205 if (!msg)
0206 return -ENOMEM;
0207
0208 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0209 NFC_EVENT_TARGET_LOST);
0210 if (!hdr)
0211 goto free_msg;
0212
0213 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
0214 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
0215 goto nla_put_failure;
0216
0217 genlmsg_end(msg, hdr);
0218
0219 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
0220
0221 return 0;
0222
0223 nla_put_failure:
0224 free_msg:
0225 nlmsg_free(msg);
0226 return -EMSGSIZE;
0227 }
0228
0229 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
0230 {
0231 struct sk_buff *msg;
0232 void *hdr;
0233
0234 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0235 if (!msg)
0236 return -ENOMEM;
0237
0238 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0239 NFC_EVENT_TM_ACTIVATED);
0240 if (!hdr)
0241 goto free_msg;
0242
0243 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
0244 goto nla_put_failure;
0245 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
0246 goto nla_put_failure;
0247
0248 genlmsg_end(msg, hdr);
0249
0250 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
0251
0252 return 0;
0253
0254 nla_put_failure:
0255 free_msg:
0256 nlmsg_free(msg);
0257 return -EMSGSIZE;
0258 }
0259
0260 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
0261 {
0262 struct sk_buff *msg;
0263 void *hdr;
0264
0265 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0266 if (!msg)
0267 return -ENOMEM;
0268
0269 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0270 NFC_EVENT_TM_DEACTIVATED);
0271 if (!hdr)
0272 goto free_msg;
0273
0274 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
0275 goto nla_put_failure;
0276
0277 genlmsg_end(msg, hdr);
0278
0279 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
0280
0281 return 0;
0282
0283 nla_put_failure:
0284 free_msg:
0285 nlmsg_free(msg);
0286 return -EMSGSIZE;
0287 }
0288
0289 static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg)
0290 {
0291 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
0292 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
0293 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
0294 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
0295 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
0296 return -1;
0297 return 0;
0298 }
0299
0300 int nfc_genl_device_added(struct nfc_dev *dev)
0301 {
0302 struct sk_buff *msg;
0303 void *hdr;
0304
0305 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0306 if (!msg)
0307 return -ENOMEM;
0308
0309 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0310 NFC_EVENT_DEVICE_ADDED);
0311 if (!hdr)
0312 goto free_msg;
0313
0314 if (nfc_genl_setup_device_added(dev, msg))
0315 goto nla_put_failure;
0316
0317 genlmsg_end(msg, hdr);
0318
0319 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
0320
0321 return 0;
0322
0323 nla_put_failure:
0324 free_msg:
0325 nlmsg_free(msg);
0326 return -EMSGSIZE;
0327 }
0328
0329 int nfc_genl_device_removed(struct nfc_dev *dev)
0330 {
0331 struct sk_buff *msg;
0332 void *hdr;
0333
0334 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0335 if (!msg)
0336 return -ENOMEM;
0337
0338 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0339 NFC_EVENT_DEVICE_REMOVED);
0340 if (!hdr)
0341 goto free_msg;
0342
0343 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
0344 goto nla_put_failure;
0345
0346 genlmsg_end(msg, hdr);
0347
0348 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
0349
0350 return 0;
0351
0352 nla_put_failure:
0353 free_msg:
0354 nlmsg_free(msg);
0355 return -EMSGSIZE;
0356 }
0357
0358 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
0359 {
0360 struct sk_buff *msg;
0361 struct nlattr *sdp_attr, *uri_attr;
0362 struct nfc_llcp_sdp_tlv *sdres;
0363 struct hlist_node *n;
0364 void *hdr;
0365 int rc = -EMSGSIZE;
0366 int i;
0367
0368 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0369 if (!msg)
0370 return -ENOMEM;
0371
0372 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0373 NFC_EVENT_LLC_SDRES);
0374 if (!hdr)
0375 goto free_msg;
0376
0377 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
0378 goto nla_put_failure;
0379
0380 sdp_attr = nla_nest_start_noflag(msg, NFC_ATTR_LLC_SDP);
0381 if (sdp_attr == NULL) {
0382 rc = -ENOMEM;
0383 goto nla_put_failure;
0384 }
0385
0386 i = 1;
0387 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
0388 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
0389
0390 uri_attr = nla_nest_start_noflag(msg, i++);
0391 if (uri_attr == NULL) {
0392 rc = -ENOMEM;
0393 goto nla_put_failure;
0394 }
0395
0396 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
0397 goto nla_put_failure;
0398
0399 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
0400 goto nla_put_failure;
0401
0402 nla_nest_end(msg, uri_attr);
0403
0404 hlist_del(&sdres->node);
0405
0406 nfc_llcp_free_sdp_tlv(sdres);
0407 }
0408
0409 nla_nest_end(msg, sdp_attr);
0410
0411 genlmsg_end(msg, hdr);
0412
0413 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
0414
0415 nla_put_failure:
0416 free_msg:
0417 nlmsg_free(msg);
0418
0419 nfc_llcp_free_sdp_tlv_list(sdres_list);
0420
0421 return rc;
0422 }
0423
0424 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
0425 {
0426 struct sk_buff *msg;
0427 void *hdr;
0428
0429 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0430 if (!msg)
0431 return -ENOMEM;
0432
0433 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0434 NFC_EVENT_SE_ADDED);
0435 if (!hdr)
0436 goto free_msg;
0437
0438 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
0439 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
0440 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
0441 goto nla_put_failure;
0442
0443 genlmsg_end(msg, hdr);
0444
0445 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
0446
0447 return 0;
0448
0449 nla_put_failure:
0450 free_msg:
0451 nlmsg_free(msg);
0452 return -EMSGSIZE;
0453 }
0454
0455 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
0456 {
0457 struct sk_buff *msg;
0458 void *hdr;
0459
0460 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0461 if (!msg)
0462 return -ENOMEM;
0463
0464 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0465 NFC_EVENT_SE_REMOVED);
0466 if (!hdr)
0467 goto free_msg;
0468
0469 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
0470 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
0471 goto nla_put_failure;
0472
0473 genlmsg_end(msg, hdr);
0474
0475 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
0476
0477 return 0;
0478
0479 nla_put_failure:
0480 free_msg:
0481 nlmsg_free(msg);
0482 return -EMSGSIZE;
0483 }
0484
0485 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
0486 struct nfc_evt_transaction *evt_transaction)
0487 {
0488 struct nfc_se *se;
0489 struct sk_buff *msg;
0490 void *hdr;
0491
0492 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0493 if (!msg)
0494 return -ENOMEM;
0495
0496 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0497 NFC_EVENT_SE_TRANSACTION);
0498 if (!hdr)
0499 goto free_msg;
0500
0501 se = nfc_find_se(dev, se_idx);
0502 if (!se)
0503 goto free_msg;
0504
0505 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
0506 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
0507 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
0508 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
0509 evt_transaction->aid) ||
0510 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
0511 evt_transaction->params))
0512 goto nla_put_failure;
0513
0514
0515 devm_kfree(&dev->dev, evt_transaction);
0516
0517 genlmsg_end(msg, hdr);
0518
0519 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
0520
0521 return 0;
0522
0523 nla_put_failure:
0524 free_msg:
0525
0526 devm_kfree(&dev->dev, evt_transaction);
0527 nlmsg_free(msg);
0528 return -EMSGSIZE;
0529 }
0530
0531 int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
0532 {
0533 const struct nfc_se *se;
0534 struct sk_buff *msg;
0535 void *hdr;
0536
0537 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0538 if (!msg)
0539 return -ENOMEM;
0540
0541 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0542 NFC_EVENT_SE_CONNECTIVITY);
0543 if (!hdr)
0544 goto free_msg;
0545
0546 se = nfc_find_se(dev, se_idx);
0547 if (!se)
0548 goto free_msg;
0549
0550 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
0551 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
0552 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
0553 goto nla_put_failure;
0554
0555 genlmsg_end(msg, hdr);
0556
0557 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
0558
0559 return 0;
0560
0561 nla_put_failure:
0562 free_msg:
0563 nlmsg_free(msg);
0564 return -EMSGSIZE;
0565 }
0566
0567 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
0568 u32 portid, u32 seq,
0569 struct netlink_callback *cb,
0570 int flags)
0571 {
0572 void *hdr;
0573
0574 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
0575 NFC_CMD_GET_DEVICE);
0576 if (!hdr)
0577 return -EMSGSIZE;
0578
0579 if (cb)
0580 genl_dump_check_consistent(cb, hdr);
0581
0582 if (nfc_genl_setup_device_added(dev, msg))
0583 goto nla_put_failure;
0584
0585 genlmsg_end(msg, hdr);
0586 return 0;
0587
0588 nla_put_failure:
0589 genlmsg_cancel(msg, hdr);
0590 return -EMSGSIZE;
0591 }
0592
0593 static int nfc_genl_dump_devices(struct sk_buff *skb,
0594 struct netlink_callback *cb)
0595 {
0596 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
0597 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
0598 bool first_call = false;
0599
0600 if (!iter) {
0601 first_call = true;
0602 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
0603 if (!iter)
0604 return -ENOMEM;
0605 cb->args[0] = (long) iter;
0606 }
0607
0608 mutex_lock(&nfc_devlist_mutex);
0609
0610 cb->seq = nfc_devlist_generation;
0611
0612 if (first_call) {
0613 nfc_device_iter_init(iter);
0614 dev = nfc_device_iter_next(iter);
0615 }
0616
0617 while (dev) {
0618 int rc;
0619
0620 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
0621 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
0622 if (rc < 0)
0623 break;
0624
0625 dev = nfc_device_iter_next(iter);
0626 }
0627
0628 mutex_unlock(&nfc_devlist_mutex);
0629
0630 cb->args[1] = (long) dev;
0631
0632 return skb->len;
0633 }
0634
0635 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
0636 {
0637 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
0638
0639 if (iter) {
0640 nfc_device_iter_exit(iter);
0641 kfree(iter);
0642 }
0643
0644 return 0;
0645 }
0646
0647 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
0648 u8 comm_mode, u8 rf_mode)
0649 {
0650 struct sk_buff *msg;
0651 void *hdr;
0652
0653 pr_debug("DEP link is up\n");
0654
0655 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
0656 if (!msg)
0657 return -ENOMEM;
0658
0659 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
0660 if (!hdr)
0661 goto free_msg;
0662
0663 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
0664 goto nla_put_failure;
0665 if (rf_mode == NFC_RF_INITIATOR &&
0666 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
0667 goto nla_put_failure;
0668 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
0669 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
0670 goto nla_put_failure;
0671
0672 genlmsg_end(msg, hdr);
0673
0674 dev->dep_link_up = true;
0675
0676 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
0677
0678 return 0;
0679
0680 nla_put_failure:
0681 free_msg:
0682 nlmsg_free(msg);
0683 return -EMSGSIZE;
0684 }
0685
0686 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
0687 {
0688 struct sk_buff *msg;
0689 void *hdr;
0690
0691 pr_debug("DEP link is down\n");
0692
0693 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
0694 if (!msg)
0695 return -ENOMEM;
0696
0697 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
0698 NFC_CMD_DEP_LINK_DOWN);
0699 if (!hdr)
0700 goto free_msg;
0701
0702 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
0703 goto nla_put_failure;
0704
0705 genlmsg_end(msg, hdr);
0706
0707 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
0708
0709 return 0;
0710
0711 nla_put_failure:
0712 free_msg:
0713 nlmsg_free(msg);
0714 return -EMSGSIZE;
0715 }
0716
0717 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
0718 {
0719 struct sk_buff *msg;
0720 struct nfc_dev *dev;
0721 u32 idx;
0722 int rc = -ENOBUFS;
0723
0724 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
0725 return -EINVAL;
0726
0727 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
0728
0729 dev = nfc_get_device(idx);
0730 if (!dev)
0731 return -ENODEV;
0732
0733 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0734 if (!msg) {
0735 rc = -ENOMEM;
0736 goto out_putdev;
0737 }
0738
0739 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
0740 NULL, 0);
0741 if (rc < 0)
0742 goto out_free;
0743
0744 nfc_put_device(dev);
0745
0746 return genlmsg_reply(msg, info);
0747
0748 out_free:
0749 nlmsg_free(msg);
0750 out_putdev:
0751 nfc_put_device(dev);
0752 return rc;
0753 }
0754
0755 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
0756 {
0757 struct nfc_dev *dev;
0758 int rc;
0759 u32 idx;
0760
0761 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
0762 return -EINVAL;
0763
0764 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
0765
0766 dev = nfc_get_device(idx);
0767 if (!dev)
0768 return -ENODEV;
0769
0770 rc = nfc_dev_up(dev);
0771
0772 nfc_put_device(dev);
0773 return rc;
0774 }
0775
0776 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
0777 {
0778 struct nfc_dev *dev;
0779 int rc;
0780 u32 idx;
0781
0782 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
0783 return -EINVAL;
0784
0785 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
0786
0787 dev = nfc_get_device(idx);
0788 if (!dev)
0789 return -ENODEV;
0790
0791 rc = nfc_dev_down(dev);
0792
0793 nfc_put_device(dev);
0794 return rc;
0795 }
0796
0797 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
0798 {
0799 struct nfc_dev *dev;
0800 int rc;
0801 u32 idx;
0802 u32 im_protocols = 0, tm_protocols = 0;
0803
0804 pr_debug("Poll start\n");
0805
0806 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
0807 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
0808 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
0809 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
0810 return -EINVAL;
0811
0812 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
0813
0814 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
0815 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
0816
0817 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
0818 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
0819 else if (info->attrs[NFC_ATTR_PROTOCOLS])
0820 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
0821
0822 dev = nfc_get_device(idx);
0823 if (!dev)
0824 return -ENODEV;
0825
0826 mutex_lock(&dev->genl_data.genl_data_mutex);
0827
0828 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
0829 if (!rc)
0830 dev->genl_data.poll_req_portid = info->snd_portid;
0831
0832 mutex_unlock(&dev->genl_data.genl_data_mutex);
0833
0834 nfc_put_device(dev);
0835 return rc;
0836 }
0837
0838 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
0839 {
0840 struct nfc_dev *dev;
0841 int rc;
0842 u32 idx;
0843
0844 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
0845 return -EINVAL;
0846
0847 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
0848
0849 dev = nfc_get_device(idx);
0850 if (!dev)
0851 return -ENODEV;
0852
0853 device_lock(&dev->dev);
0854
0855 if (!dev->polling) {
0856 device_unlock(&dev->dev);
0857 nfc_put_device(dev);
0858 return -EINVAL;
0859 }
0860
0861 device_unlock(&dev->dev);
0862
0863 mutex_lock(&dev->genl_data.genl_data_mutex);
0864
0865 if (dev->genl_data.poll_req_portid != info->snd_portid) {
0866 rc = -EBUSY;
0867 goto out;
0868 }
0869
0870 rc = nfc_stop_poll(dev);
0871 dev->genl_data.poll_req_portid = 0;
0872
0873 out:
0874 mutex_unlock(&dev->genl_data.genl_data_mutex);
0875 nfc_put_device(dev);
0876 return rc;
0877 }
0878
0879 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
0880 {
0881 struct nfc_dev *dev;
0882 u32 device_idx, target_idx, protocol;
0883 int rc;
0884
0885 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
0886 !info->attrs[NFC_ATTR_TARGET_INDEX] ||
0887 !info->attrs[NFC_ATTR_PROTOCOLS])
0888 return -EINVAL;
0889
0890 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
0891
0892 dev = nfc_get_device(device_idx);
0893 if (!dev)
0894 return -ENODEV;
0895
0896 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
0897 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
0898
0899 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
0900 rc = nfc_activate_target(dev, target_idx, protocol);
0901
0902 nfc_put_device(dev);
0903 return rc;
0904 }
0905
0906 static int nfc_genl_deactivate_target(struct sk_buff *skb,
0907 struct genl_info *info)
0908 {
0909 struct nfc_dev *dev;
0910 u32 device_idx, target_idx;
0911 int rc;
0912
0913 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
0914 !info->attrs[NFC_ATTR_TARGET_INDEX])
0915 return -EINVAL;
0916
0917 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
0918
0919 dev = nfc_get_device(device_idx);
0920 if (!dev)
0921 return -ENODEV;
0922
0923 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
0924
0925 rc = nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
0926
0927 nfc_put_device(dev);
0928 return rc;
0929 }
0930
0931 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
0932 {
0933 struct nfc_dev *dev;
0934 int rc, tgt_idx;
0935 u32 idx;
0936 u8 comm;
0937
0938 pr_debug("DEP link up\n");
0939
0940 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
0941 !info->attrs[NFC_ATTR_COMM_MODE])
0942 return -EINVAL;
0943
0944 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
0945 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
0946 tgt_idx = NFC_TARGET_IDX_ANY;
0947 else
0948 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
0949
0950 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
0951
0952 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
0953 return -EINVAL;
0954
0955 dev = nfc_get_device(idx);
0956 if (!dev)
0957 return -ENODEV;
0958
0959 rc = nfc_dep_link_up(dev, tgt_idx, comm);
0960
0961 nfc_put_device(dev);
0962
0963 return rc;
0964 }
0965
0966 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
0967 {
0968 struct nfc_dev *dev;
0969 int rc;
0970 u32 idx;
0971
0972 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
0973 !info->attrs[NFC_ATTR_TARGET_INDEX])
0974 return -EINVAL;
0975
0976 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
0977
0978 dev = nfc_get_device(idx);
0979 if (!dev)
0980 return -ENODEV;
0981
0982 rc = nfc_dep_link_down(dev);
0983
0984 nfc_put_device(dev);
0985 return rc;
0986 }
0987
0988 static int nfc_genl_send_params(struct sk_buff *msg,
0989 struct nfc_llcp_local *local,
0990 u32 portid, u32 seq)
0991 {
0992 void *hdr;
0993
0994 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
0995 NFC_CMD_LLC_GET_PARAMS);
0996 if (!hdr)
0997 return -EMSGSIZE;
0998
0999 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
1000 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
1001 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
1002 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1003 goto nla_put_failure;
1004
1005 genlmsg_end(msg, hdr);
1006 return 0;
1007
1008 nla_put_failure:
1009 genlmsg_cancel(msg, hdr);
1010 return -EMSGSIZE;
1011 }
1012
1013 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1014 {
1015 struct nfc_dev *dev;
1016 struct nfc_llcp_local *local;
1017 int rc = 0;
1018 struct sk_buff *msg = NULL;
1019 u32 idx;
1020
1021 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1022 !info->attrs[NFC_ATTR_FIRMWARE_NAME])
1023 return -EINVAL;
1024
1025 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1026
1027 dev = nfc_get_device(idx);
1028 if (!dev)
1029 return -ENODEV;
1030
1031 device_lock(&dev->dev);
1032
1033 local = nfc_llcp_find_local(dev);
1034 if (!local) {
1035 rc = -ENODEV;
1036 goto exit;
1037 }
1038
1039 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1040 if (!msg) {
1041 rc = -ENOMEM;
1042 goto exit;
1043 }
1044
1045 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1046
1047 exit:
1048 device_unlock(&dev->dev);
1049
1050 nfc_put_device(dev);
1051
1052 if (rc < 0) {
1053 if (msg)
1054 nlmsg_free(msg);
1055
1056 return rc;
1057 }
1058
1059 return genlmsg_reply(msg, info);
1060 }
1061
1062 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1063 {
1064 struct nfc_dev *dev;
1065 struct nfc_llcp_local *local;
1066 u8 rw = 0;
1067 u16 miux = 0;
1068 u32 idx;
1069 int rc = 0;
1070
1071 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1072 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1073 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1074 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1075 return -EINVAL;
1076
1077 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1078 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1079
1080 if (rw > LLCP_MAX_RW)
1081 return -EINVAL;
1082 }
1083
1084 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1085 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1086
1087 if (miux > LLCP_MAX_MIUX)
1088 return -EINVAL;
1089 }
1090
1091 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1092
1093 dev = nfc_get_device(idx);
1094 if (!dev)
1095 return -ENODEV;
1096
1097 device_lock(&dev->dev);
1098
1099 local = nfc_llcp_find_local(dev);
1100 if (!local) {
1101 rc = -ENODEV;
1102 goto exit;
1103 }
1104
1105 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1106 if (dev->dep_link_up) {
1107 rc = -EINPROGRESS;
1108 goto exit;
1109 }
1110
1111 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1112 }
1113
1114 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1115 local->rw = rw;
1116
1117 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1118 local->miux = cpu_to_be16(miux);
1119
1120 exit:
1121 device_unlock(&dev->dev);
1122
1123 nfc_put_device(dev);
1124
1125 return rc;
1126 }
1127
1128 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1129 {
1130 struct nfc_dev *dev;
1131 struct nfc_llcp_local *local;
1132 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1133 u32 idx;
1134 u8 tid;
1135 char *uri;
1136 int rc = 0, rem;
1137 size_t uri_len, tlvs_len;
1138 struct hlist_head sdreq_list;
1139 struct nfc_llcp_sdp_tlv *sdreq;
1140
1141 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1142 !info->attrs[NFC_ATTR_LLC_SDP])
1143 return -EINVAL;
1144
1145 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1146
1147 dev = nfc_get_device(idx);
1148 if (!dev)
1149 return -ENODEV;
1150
1151 device_lock(&dev->dev);
1152
1153 if (dev->dep_link_up == false) {
1154 rc = -ENOLINK;
1155 goto exit;
1156 }
1157
1158 local = nfc_llcp_find_local(dev);
1159 if (!local) {
1160 rc = -ENODEV;
1161 goto exit;
1162 }
1163
1164 INIT_HLIST_HEAD(&sdreq_list);
1165
1166 tlvs_len = 0;
1167
1168 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1169 rc = nla_parse_nested_deprecated(sdp_attrs, NFC_SDP_ATTR_MAX,
1170 attr, nfc_sdp_genl_policy,
1171 info->extack);
1172
1173 if (rc != 0) {
1174 rc = -EINVAL;
1175 goto exit;
1176 }
1177
1178 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1179 continue;
1180
1181 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1182 if (uri_len == 0)
1183 continue;
1184
1185 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1186 if (uri == NULL || *uri == 0)
1187 continue;
1188
1189 tid = local->sdreq_next_tid++;
1190
1191 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1192 if (sdreq == NULL) {
1193 rc = -ENOMEM;
1194 goto exit;
1195 }
1196
1197 tlvs_len += sdreq->tlv_len;
1198
1199 hlist_add_head(&sdreq->node, &sdreq_list);
1200 }
1201
1202 if (hlist_empty(&sdreq_list)) {
1203 rc = -EINVAL;
1204 goto exit;
1205 }
1206
1207 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1208 exit:
1209 device_unlock(&dev->dev);
1210
1211 nfc_put_device(dev);
1212
1213 return rc;
1214 }
1215
1216 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1217 {
1218 struct nfc_dev *dev;
1219 int rc;
1220 u32 idx;
1221 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1222
1223 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || !info->attrs[NFC_ATTR_FIRMWARE_NAME])
1224 return -EINVAL;
1225
1226 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1227
1228 dev = nfc_get_device(idx);
1229 if (!dev)
1230 return -ENODEV;
1231
1232 nla_strscpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1233 sizeof(firmware_name));
1234
1235 rc = nfc_fw_download(dev, firmware_name);
1236
1237 nfc_put_device(dev);
1238 return rc;
1239 }
1240
1241 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1242 u32 result)
1243 {
1244 struct sk_buff *msg;
1245 void *hdr;
1246
1247 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1248 if (!msg)
1249 return -ENOMEM;
1250
1251 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1252 NFC_CMD_FW_DOWNLOAD);
1253 if (!hdr)
1254 goto free_msg;
1255
1256 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1257 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1258 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1259 goto nla_put_failure;
1260
1261 genlmsg_end(msg, hdr);
1262
1263 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
1264
1265 return 0;
1266
1267 nla_put_failure:
1268 free_msg:
1269 nlmsg_free(msg);
1270 return -EMSGSIZE;
1271 }
1272
1273 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1274 {
1275 struct nfc_dev *dev;
1276 int rc;
1277 u32 idx, se_idx;
1278
1279 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1280 !info->attrs[NFC_ATTR_SE_INDEX])
1281 return -EINVAL;
1282
1283 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1284 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1285
1286 dev = nfc_get_device(idx);
1287 if (!dev)
1288 return -ENODEV;
1289
1290 rc = nfc_enable_se(dev, se_idx);
1291
1292 nfc_put_device(dev);
1293 return rc;
1294 }
1295
1296 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1297 {
1298 struct nfc_dev *dev;
1299 int rc;
1300 u32 idx, se_idx;
1301
1302 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1303 !info->attrs[NFC_ATTR_SE_INDEX])
1304 return -EINVAL;
1305
1306 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1307 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1308
1309 dev = nfc_get_device(idx);
1310 if (!dev)
1311 return -ENODEV;
1312
1313 rc = nfc_disable_se(dev, se_idx);
1314
1315 nfc_put_device(dev);
1316 return rc;
1317 }
1318
1319 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1320 u32 portid, u32 seq,
1321 struct netlink_callback *cb,
1322 int flags)
1323 {
1324 void *hdr;
1325 struct nfc_se *se, *n;
1326
1327 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1328 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1329 NFC_CMD_GET_SE);
1330 if (!hdr)
1331 goto nla_put_failure;
1332
1333 if (cb)
1334 genl_dump_check_consistent(cb, hdr);
1335
1336 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1337 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1338 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1339 goto nla_put_failure;
1340
1341 genlmsg_end(msg, hdr);
1342 }
1343
1344 return 0;
1345
1346 nla_put_failure:
1347 genlmsg_cancel(msg, hdr);
1348 return -EMSGSIZE;
1349 }
1350
1351 static int nfc_genl_dump_ses(struct sk_buff *skb,
1352 struct netlink_callback *cb)
1353 {
1354 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1355 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1356 bool first_call = false;
1357
1358 if (!iter) {
1359 first_call = true;
1360 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1361 if (!iter)
1362 return -ENOMEM;
1363 cb->args[0] = (long) iter;
1364 }
1365
1366 mutex_lock(&nfc_devlist_mutex);
1367
1368 cb->seq = nfc_devlist_generation;
1369
1370 if (first_call) {
1371 nfc_device_iter_init(iter);
1372 dev = nfc_device_iter_next(iter);
1373 }
1374
1375 while (dev) {
1376 int rc;
1377
1378 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1379 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1380 if (rc < 0)
1381 break;
1382
1383 dev = nfc_device_iter_next(iter);
1384 }
1385
1386 mutex_unlock(&nfc_devlist_mutex);
1387
1388 cb->args[1] = (long) dev;
1389
1390 return skb->len;
1391 }
1392
1393 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1394 {
1395 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1396
1397 if (iter) {
1398 nfc_device_iter_exit(iter);
1399 kfree(iter);
1400 }
1401
1402 return 0;
1403 }
1404
1405 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1406 u8 *apdu, size_t apdu_length,
1407 se_io_cb_t cb, void *cb_context)
1408 {
1409 struct nfc_se *se;
1410 int rc;
1411
1412 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1413
1414 device_lock(&dev->dev);
1415
1416 if (!device_is_registered(&dev->dev)) {
1417 rc = -ENODEV;
1418 goto error;
1419 }
1420
1421 if (!dev->dev_up) {
1422 rc = -ENODEV;
1423 goto error;
1424 }
1425
1426 if (!dev->ops->se_io) {
1427 rc = -EOPNOTSUPP;
1428 goto error;
1429 }
1430
1431 se = nfc_find_se(dev, se_idx);
1432 if (!se) {
1433 rc = -EINVAL;
1434 goto error;
1435 }
1436
1437 if (se->state != NFC_SE_ENABLED) {
1438 rc = -ENODEV;
1439 goto error;
1440 }
1441
1442 rc = dev->ops->se_io(dev, se_idx, apdu,
1443 apdu_length, cb, cb_context);
1444
1445 error:
1446 device_unlock(&dev->dev);
1447 return rc;
1448 }
1449
1450 struct se_io_ctx {
1451 u32 dev_idx;
1452 u32 se_idx;
1453 };
1454
1455 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1456 {
1457 struct se_io_ctx *ctx = context;
1458 struct sk_buff *msg;
1459 void *hdr;
1460
1461 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1462 if (!msg) {
1463 kfree(ctx);
1464 return;
1465 }
1466
1467 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1468 NFC_CMD_SE_IO);
1469 if (!hdr)
1470 goto free_msg;
1471
1472 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1473 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1474 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1475 goto nla_put_failure;
1476
1477 genlmsg_end(msg, hdr);
1478
1479 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1480
1481 kfree(ctx);
1482
1483 return;
1484
1485 nla_put_failure:
1486 free_msg:
1487 nlmsg_free(msg);
1488 kfree(ctx);
1489
1490 return;
1491 }
1492
1493 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1494 {
1495 struct nfc_dev *dev;
1496 struct se_io_ctx *ctx;
1497 u32 dev_idx, se_idx;
1498 u8 *apdu;
1499 size_t apdu_len;
1500
1501 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1502 !info->attrs[NFC_ATTR_SE_INDEX] ||
1503 !info->attrs[NFC_ATTR_SE_APDU])
1504 return -EINVAL;
1505
1506 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1507 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1508
1509 dev = nfc_get_device(dev_idx);
1510 if (!dev)
1511 return -ENODEV;
1512
1513 if (!dev->ops || !dev->ops->se_io)
1514 return -ENOTSUPP;
1515
1516 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1517 if (apdu_len == 0)
1518 return -EINVAL;
1519
1520 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1521 if (!apdu)
1522 return -EINVAL;
1523
1524 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1525 if (!ctx)
1526 return -ENOMEM;
1527
1528 ctx->dev_idx = dev_idx;
1529 ctx->se_idx = se_idx;
1530
1531 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1532 }
1533
1534 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1535 struct genl_info *info)
1536 {
1537 struct nfc_dev *dev;
1538 const struct nfc_vendor_cmd *cmd;
1539 u32 dev_idx, vid, subcmd;
1540 u8 *data;
1541 size_t data_len;
1542 int i, err;
1543
1544 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1545 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1546 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1547 return -EINVAL;
1548
1549 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1550 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1551 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1552
1553 dev = nfc_get_device(dev_idx);
1554 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1555 return -ENODEV;
1556
1557 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1558 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1559 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1560 if (data_len == 0)
1561 return -EINVAL;
1562 } else {
1563 data = NULL;
1564 data_len = 0;
1565 }
1566
1567 for (i = 0; i < dev->n_vendor_cmds; i++) {
1568 cmd = &dev->vendor_cmds[i];
1569
1570 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1571 continue;
1572
1573 dev->cur_cmd_info = info;
1574 err = cmd->doit(dev, data, data_len);
1575 dev->cur_cmd_info = NULL;
1576 return err;
1577 }
1578
1579 return -EOPNOTSUPP;
1580 }
1581
1582
1583 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1584 int flags, u8 cmd)
1585 {
1586
1587 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1588 }
1589
1590 static struct sk_buff *
1591 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1592 u32 portid, u32 seq,
1593 enum nfc_attrs attr,
1594 u32 oui, u32 subcmd, gfp_t gfp)
1595 {
1596 struct sk_buff *skb;
1597 void *hdr;
1598
1599 skb = nlmsg_new(approxlen + 100, gfp);
1600 if (!skb)
1601 return NULL;
1602
1603 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1604 if (!hdr) {
1605 kfree_skb(skb);
1606 return NULL;
1607 }
1608
1609 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1610 goto nla_put_failure;
1611 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1612 goto nla_put_failure;
1613 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1614 goto nla_put_failure;
1615
1616 ((void **)skb->cb)[0] = dev;
1617 ((void **)skb->cb)[1] = hdr;
1618
1619 return skb;
1620
1621 nla_put_failure:
1622 kfree_skb(skb);
1623 return NULL;
1624 }
1625
1626 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1627 enum nfc_attrs attr,
1628 u32 oui, u32 subcmd,
1629 int approxlen)
1630 {
1631 if (WARN_ON(!dev->cur_cmd_info))
1632 return NULL;
1633
1634 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1635 dev->cur_cmd_info->snd_portid,
1636 dev->cur_cmd_info->snd_seq, attr,
1637 oui, subcmd, GFP_KERNEL);
1638 }
1639 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1640
1641 int nfc_vendor_cmd_reply(struct sk_buff *skb)
1642 {
1643 struct nfc_dev *dev = ((void **)skb->cb)[0];
1644 void *hdr = ((void **)skb->cb)[1];
1645
1646
1647 memset(skb->cb, 0, sizeof(skb->cb));
1648
1649 if (WARN_ON(!dev->cur_cmd_info)) {
1650 kfree_skb(skb);
1651 return -EINVAL;
1652 }
1653
1654 genlmsg_end(skb, hdr);
1655 return genlmsg_reply(skb, dev->cur_cmd_info);
1656 }
1657 EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1658
1659 static const struct genl_ops nfc_genl_ops[] = {
1660 {
1661 .cmd = NFC_CMD_GET_DEVICE,
1662 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1663 .doit = nfc_genl_get_device,
1664 .dumpit = nfc_genl_dump_devices,
1665 .done = nfc_genl_dump_devices_done,
1666 },
1667 {
1668 .cmd = NFC_CMD_DEV_UP,
1669 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1670 .doit = nfc_genl_dev_up,
1671 .flags = GENL_ADMIN_PERM,
1672 },
1673 {
1674 .cmd = NFC_CMD_DEV_DOWN,
1675 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1676 .doit = nfc_genl_dev_down,
1677 .flags = GENL_ADMIN_PERM,
1678 },
1679 {
1680 .cmd = NFC_CMD_START_POLL,
1681 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1682 .doit = nfc_genl_start_poll,
1683 .flags = GENL_ADMIN_PERM,
1684 },
1685 {
1686 .cmd = NFC_CMD_STOP_POLL,
1687 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1688 .doit = nfc_genl_stop_poll,
1689 .flags = GENL_ADMIN_PERM,
1690 },
1691 {
1692 .cmd = NFC_CMD_DEP_LINK_UP,
1693 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1694 .doit = nfc_genl_dep_link_up,
1695 .flags = GENL_ADMIN_PERM,
1696 },
1697 {
1698 .cmd = NFC_CMD_DEP_LINK_DOWN,
1699 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1700 .doit = nfc_genl_dep_link_down,
1701 .flags = GENL_ADMIN_PERM,
1702 },
1703 {
1704 .cmd = NFC_CMD_GET_TARGET,
1705 .validate = GENL_DONT_VALIDATE_STRICT |
1706 GENL_DONT_VALIDATE_DUMP_STRICT,
1707 .dumpit = nfc_genl_dump_targets,
1708 .done = nfc_genl_dump_targets_done,
1709 },
1710 {
1711 .cmd = NFC_CMD_LLC_GET_PARAMS,
1712 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1713 .doit = nfc_genl_llc_get_params,
1714 },
1715 {
1716 .cmd = NFC_CMD_LLC_SET_PARAMS,
1717 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1718 .doit = nfc_genl_llc_set_params,
1719 .flags = GENL_ADMIN_PERM,
1720 },
1721 {
1722 .cmd = NFC_CMD_LLC_SDREQ,
1723 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1724 .doit = nfc_genl_llc_sdreq,
1725 .flags = GENL_ADMIN_PERM,
1726 },
1727 {
1728 .cmd = NFC_CMD_FW_DOWNLOAD,
1729 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1730 .doit = nfc_genl_fw_download,
1731 .flags = GENL_ADMIN_PERM,
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 .flags = GENL_ADMIN_PERM,
1738 },
1739 {
1740 .cmd = NFC_CMD_DISABLE_SE,
1741 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1742 .doit = nfc_genl_disable_se,
1743 .flags = GENL_ADMIN_PERM,
1744 },
1745 {
1746 .cmd = NFC_CMD_GET_SE,
1747 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1748 .dumpit = nfc_genl_dump_ses,
1749 .done = nfc_genl_dump_ses_done,
1750 },
1751 {
1752 .cmd = NFC_CMD_SE_IO,
1753 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1754 .doit = nfc_genl_se_io,
1755 .flags = GENL_ADMIN_PERM,
1756 },
1757 {
1758 .cmd = NFC_CMD_ACTIVATE_TARGET,
1759 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1760 .doit = nfc_genl_activate_target,
1761 .flags = GENL_ADMIN_PERM,
1762 },
1763 {
1764 .cmd = NFC_CMD_VENDOR,
1765 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1766 .doit = nfc_genl_vendor_cmd,
1767 .flags = GENL_ADMIN_PERM,
1768 },
1769 {
1770 .cmd = NFC_CMD_DEACTIVATE_TARGET,
1771 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1772 .doit = nfc_genl_deactivate_target,
1773 .flags = GENL_ADMIN_PERM,
1774 },
1775 };
1776
1777 static struct genl_family nfc_genl_family __ro_after_init = {
1778 .hdrsize = 0,
1779 .name = NFC_GENL_NAME,
1780 .version = NFC_GENL_VERSION,
1781 .maxattr = NFC_ATTR_MAX,
1782 .policy = nfc_genl_policy,
1783 .module = THIS_MODULE,
1784 .ops = nfc_genl_ops,
1785 .n_ops = ARRAY_SIZE(nfc_genl_ops),
1786 .mcgrps = nfc_genl_mcgrps,
1787 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1788 };
1789
1790
1791 struct urelease_work {
1792 struct work_struct w;
1793 u32 portid;
1794 };
1795
1796 static void nfc_urelease_event_work(struct work_struct *work)
1797 {
1798 struct urelease_work *w = container_of(work, struct urelease_work, w);
1799 struct class_dev_iter iter;
1800 struct nfc_dev *dev;
1801
1802 pr_debug("portid %d\n", w->portid);
1803
1804 mutex_lock(&nfc_devlist_mutex);
1805
1806 nfc_device_iter_init(&iter);
1807 dev = nfc_device_iter_next(&iter);
1808
1809 while (dev) {
1810 mutex_lock(&dev->genl_data.genl_data_mutex);
1811
1812 if (dev->genl_data.poll_req_portid == w->portid) {
1813 nfc_stop_poll(dev);
1814 dev->genl_data.poll_req_portid = 0;
1815 }
1816
1817 mutex_unlock(&dev->genl_data.genl_data_mutex);
1818
1819 dev = nfc_device_iter_next(&iter);
1820 }
1821
1822 nfc_device_iter_exit(&iter);
1823
1824 mutex_unlock(&nfc_devlist_mutex);
1825
1826 kfree(w);
1827 }
1828
1829 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1830 unsigned long event, void *ptr)
1831 {
1832 struct netlink_notify *n = ptr;
1833 struct urelease_work *w;
1834
1835 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1836 goto out;
1837
1838 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1839
1840 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1841 if (w) {
1842 INIT_WORK(&w->w, nfc_urelease_event_work);
1843 w->portid = n->portid;
1844 schedule_work(&w->w);
1845 }
1846
1847 out:
1848 return NOTIFY_DONE;
1849 }
1850
1851 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1852 {
1853 genl_data->poll_req_portid = 0;
1854 mutex_init(&genl_data->genl_data_mutex);
1855 }
1856
1857 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1858 {
1859 mutex_destroy(&genl_data->genl_data_mutex);
1860 }
1861
1862 static struct notifier_block nl_notifier = {
1863 .notifier_call = nfc_genl_rcv_nl_event,
1864 };
1865
1866
1867
1868
1869
1870
1871 int __init nfc_genl_init(void)
1872 {
1873 int rc;
1874
1875 rc = genl_register_family(&nfc_genl_family);
1876 if (rc)
1877 return rc;
1878
1879 netlink_register_notifier(&nl_notifier);
1880
1881 return 0;
1882 }
1883
1884
1885
1886
1887
1888
1889 void nfc_genl_exit(void)
1890 {
1891 netlink_unregister_notifier(&nl_notifier);
1892 genl_unregister_family(&nfc_genl_family);
1893 }