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