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
32static struct genl_multicast_group nfc_genl_event_mcgrp = {
33 .name = NFC_GENL_MCAST_EVENT_NAME,
34};
35
36struct genl_family nfc_genl_family = {
37 .id = GENL_ID_GENERATE,
38 .hdrsize = 0,
39 .name = NFC_GENL_NAME,
40 .version = NFC_GENL_VERSION,
41 .maxattr = NFC_ATTR_MAX,
42};
43
44static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
45 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
46 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
47 .len = NFC_DEVICE_NAME_MAXSIZE },
48 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
49 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
50 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
51 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
52};
53
54static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
55 struct netlink_callback *cb, int flags)
56{
57 void *hdr;
58
59 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
60 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
61 if (!hdr)
62 return -EMSGSIZE;
63
64 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
65
66 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target->idx);
67 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols);
68 NLA_PUT_U16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res);
69 NLA_PUT_U8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res);
70 if (target->nfcid1_len > 0)
71 NLA_PUT(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
72 target->nfcid1);
73 if (target->sensb_res_len > 0)
74 NLA_PUT(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
75 target->sensb_res);
76 if (target->sensf_res_len > 0)
77 NLA_PUT(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
78 target->sensf_res);
79
80 return genlmsg_end(msg, hdr);
81
82nla_put_failure:
83 genlmsg_cancel(msg, hdr);
84 return -EMSGSIZE;
85}
86
87static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
88{
89 struct nfc_dev *dev;
90 int rc;
91 u32 idx;
92
93 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
94 nfc_genl_family.attrbuf,
95 nfc_genl_family.maxattr,
96 nfc_genl_policy);
97 if (rc < 0)
98 return ERR_PTR(rc);
99
100 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
101 return ERR_PTR(-EINVAL);
102
103 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
104
105 dev = nfc_get_device(idx);
106 if (!dev)
107 return ERR_PTR(-ENODEV);
108
109 return dev;
110}
111
112static int nfc_genl_dump_targets(struct sk_buff *skb,
113 struct netlink_callback *cb)
114{
115 int i = cb->args[0];
116 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
117 int rc;
118
119 if (!dev) {
120 dev = __get_device_from_cb(cb);
121 if (IS_ERR(dev))
122 return PTR_ERR(dev);
123
124 cb->args[1] = (long) dev;
125 }
126
127 spin_lock_bh(&dev->targets_lock);
128
129 cb->seq = dev->targets_generation;
130
131 while (i < dev->n_targets) {
132 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
133 NLM_F_MULTI);
134 if (rc < 0)
135 break;
136
137 i++;
138 }
139
140 spin_unlock_bh(&dev->targets_lock);
141
142 cb->args[0] = i;
143
144 return skb->len;
145}
146
147static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
148{
149 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
150
151 if (dev)
152 nfc_put_device(dev);
153
154 return 0;
155}
156
157int nfc_genl_targets_found(struct nfc_dev *dev)
158{
159 struct sk_buff *msg;
160 void *hdr;
161
162 dev->genl_data.poll_req_pid = 0;
163
164 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
165 if (!msg)
166 return -ENOMEM;
167
168 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
169 NFC_EVENT_TARGETS_FOUND);
170 if (!hdr)
171 goto free_msg;
172
173 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
174
175 genlmsg_end(msg, hdr);
176
177 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
178
179nla_put_failure:
180 genlmsg_cancel(msg, hdr);
181free_msg:
182 nlmsg_free(msg);
183 return -EMSGSIZE;
184}
185
186int nfc_genl_device_added(struct nfc_dev *dev)
187{
188 struct sk_buff *msg;
189 void *hdr;
190
191 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
192 if (!msg)
193 return -ENOMEM;
194
195 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
196 NFC_EVENT_DEVICE_ADDED);
197 if (!hdr)
198 goto free_msg;
199
200 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
201 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
202 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
203 NLA_PUT_U8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up);
204
205 genlmsg_end(msg, hdr);
206
207 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
208
209 return 0;
210
211nla_put_failure:
212 genlmsg_cancel(msg, hdr);
213free_msg:
214 nlmsg_free(msg);
215 return -EMSGSIZE;
216}
217
218int nfc_genl_device_removed(struct nfc_dev *dev)
219{
220 struct sk_buff *msg;
221 void *hdr;
222
223 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
224 if (!msg)
225 return -ENOMEM;
226
227 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
228 NFC_EVENT_DEVICE_REMOVED);
229 if (!hdr)
230 goto free_msg;
231
232 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
233
234 genlmsg_end(msg, hdr);
235
236 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
237
238 return 0;
239
240nla_put_failure:
241 genlmsg_cancel(msg, hdr);
242free_msg:
243 nlmsg_free(msg);
244 return -EMSGSIZE;
245}
246
247static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
248 u32 pid, u32 seq,
249 struct netlink_callback *cb,
250 int flags)
251{
252 void *hdr;
253
254 hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
255 NFC_CMD_GET_DEVICE);
256 if (!hdr)
257 return -EMSGSIZE;
258
259 if (cb)
260 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
261
262 NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
263 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
264 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
265 NLA_PUT_U8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up);
266
267 return genlmsg_end(msg, hdr);
268
269nla_put_failure:
270 genlmsg_cancel(msg, hdr);
271 return -EMSGSIZE;
272}
273
274static int nfc_genl_dump_devices(struct sk_buff *skb,
275 struct netlink_callback *cb)
276{
277 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
278 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
279 bool first_call = false;
280
281 if (!iter) {
282 first_call = true;
283 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
284 if (!iter)
285 return -ENOMEM;
286 cb->args[0] = (long) iter;
287 }
288
289 mutex_lock(&nfc_devlist_mutex);
290
291 cb->seq = nfc_devlist_generation;
292
293 if (first_call) {
294 nfc_device_iter_init(iter);
295 dev = nfc_device_iter_next(iter);
296 }
297
298 while (dev) {
299 int rc;
300
301 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
302 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
303 if (rc < 0)
304 break;
305
306 dev = nfc_device_iter_next(iter);
307 }
308
309 mutex_unlock(&nfc_devlist_mutex);
310
311 cb->args[1] = (long) dev;
312
313 return skb->len;
314}
315
316static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
317{
318 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
319
320 nfc_device_iter_exit(iter);
321 kfree(iter);
322
323 return 0;
324}
325
326int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
327 u8 comm_mode, u8 rf_mode)
328{
329 struct sk_buff *msg;
330 void *hdr;
331
332 pr_debug("DEP link is up\n");
333
334 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
335 if (!msg)
336 return -ENOMEM;
337
338 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
339 if (!hdr)
340 goto free_msg;
341
342 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
343 if (rf_mode == NFC_RF_INITIATOR)
344 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
345 NLA_PUT_U8(msg, NFC_ATTR_COMM_MODE, comm_mode);
346 NLA_PUT_U8(msg, NFC_ATTR_RF_MODE, rf_mode);
347
348 genlmsg_end(msg, hdr);
349
350 dev->dep_link_up = true;
351
352 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
353
354 return 0;
355
356nla_put_failure:
357 genlmsg_cancel(msg, hdr);
358free_msg:
359 nlmsg_free(msg);
360 return -EMSGSIZE;
361}
362
363int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
364{
365 struct sk_buff *msg;
366 void *hdr;
367
368 pr_debug("DEP link is down\n");
369
370 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
371 if (!msg)
372 return -ENOMEM;
373
374 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
375 NFC_CMD_DEP_LINK_DOWN);
376 if (!hdr)
377 goto free_msg;
378
379 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
380
381 genlmsg_end(msg, hdr);
382
383 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
384
385 return 0;
386
387nla_put_failure:
388 genlmsg_cancel(msg, hdr);
389free_msg:
390 nlmsg_free(msg);
391 return -EMSGSIZE;
392}
393
394static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
395{
396 struct sk_buff *msg;
397 struct nfc_dev *dev;
398 u32 idx;
399 int rc = -ENOBUFS;
400
401 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
402 return -EINVAL;
403
404 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
405
406 dev = nfc_get_device(idx);
407 if (!dev)
408 return -ENODEV;
409
410 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
411 if (!msg) {
412 rc = -ENOMEM;
413 goto out_putdev;
414 }
415
416 rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
417 NULL, 0);
418 if (rc < 0)
419 goto out_free;
420
421 nfc_put_device(dev);
422
423 return genlmsg_reply(msg, info);
424
425out_free:
426 nlmsg_free(msg);
427out_putdev:
428 nfc_put_device(dev);
429 return rc;
430}
431
432static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
433{
434 struct nfc_dev *dev;
435 int rc;
436 u32 idx;
437
438 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
439 return -EINVAL;
440
441 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
442
443 dev = nfc_get_device(idx);
444 if (!dev)
445 return -ENODEV;
446
447 rc = nfc_dev_up(dev);
448
449 nfc_put_device(dev);
450 return rc;
451}
452
453static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
454{
455 struct nfc_dev *dev;
456 int rc;
457 u32 idx;
458
459 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
460 return -EINVAL;
461
462 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
463
464 dev = nfc_get_device(idx);
465 if (!dev)
466 return -ENODEV;
467
468 rc = nfc_dev_down(dev);
469
470 nfc_put_device(dev);
471 return rc;
472}
473
474static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
475{
476 struct nfc_dev *dev;
477 int rc;
478 u32 idx;
479 u32 protocols;
480
481 pr_debug("Poll start\n");
482
483 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
484 !info->attrs[NFC_ATTR_PROTOCOLS])
485 return -EINVAL;
486
487 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
488 protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
489
490 dev = nfc_get_device(idx);
491 if (!dev)
492 return -ENODEV;
493
494 mutex_lock(&dev->genl_data.genl_data_mutex);
495
496 rc = nfc_start_poll(dev, protocols);
497 if (!rc)
498 dev->genl_data.poll_req_pid = info->snd_pid;
499
500 mutex_unlock(&dev->genl_data.genl_data_mutex);
501
502 nfc_put_device(dev);
503 return rc;
504}
505
506static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
507{
508 struct nfc_dev *dev;
509 int rc;
510 u32 idx;
511
512 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
513 return -EINVAL;
514
515 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
516
517 dev = nfc_get_device(idx);
518 if (!dev)
519 return -ENODEV;
520
521 mutex_lock(&dev->genl_data.genl_data_mutex);
522
523 if (dev->genl_data.poll_req_pid != info->snd_pid) {
524 rc = -EBUSY;
525 goto out;
526 }
527
528 rc = nfc_stop_poll(dev);
529 dev->genl_data.poll_req_pid = 0;
530
531out:
532 mutex_unlock(&dev->genl_data.genl_data_mutex);
533 nfc_put_device(dev);
534 return rc;
535}
536
537static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
538{
539 struct nfc_dev *dev;
540 int rc, tgt_idx;
541 u32 idx;
542 u8 comm;
543
544 pr_debug("DEP link up\n");
545
546 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
547 !info->attrs[NFC_ATTR_COMM_MODE])
548 return -EINVAL;
549
550 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
551 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
552 tgt_idx = NFC_TARGET_IDX_ANY;
553 else
554 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
555
556 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
557
558 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
559 return -EINVAL;
560
561 dev = nfc_get_device(idx);
562 if (!dev)
563 return -ENODEV;
564
565 rc = nfc_dep_link_up(dev, tgt_idx, comm);
566
567 nfc_put_device(dev);
568
569 return rc;
570}
571
572static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
573{
574 struct nfc_dev *dev;
575 int rc;
576 u32 idx;
577
578 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
579 return -EINVAL;
580
581 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
582
583 dev = nfc_get_device(idx);
584 if (!dev)
585 return -ENODEV;
586
587 rc = nfc_dep_link_down(dev);
588
589 nfc_put_device(dev);
590 return rc;
591}
592
593static struct genl_ops nfc_genl_ops[] = {
594 {
595 .cmd = NFC_CMD_GET_DEVICE,
596 .doit = nfc_genl_get_device,
597 .dumpit = nfc_genl_dump_devices,
598 .done = nfc_genl_dump_devices_done,
599 .policy = nfc_genl_policy,
600 },
601 {
602 .cmd = NFC_CMD_DEV_UP,
603 .doit = nfc_genl_dev_up,
604 .policy = nfc_genl_policy,
605 },
606 {
607 .cmd = NFC_CMD_DEV_DOWN,
608 .doit = nfc_genl_dev_down,
609 .policy = nfc_genl_policy,
610 },
611 {
612 .cmd = NFC_CMD_START_POLL,
613 .doit = nfc_genl_start_poll,
614 .policy = nfc_genl_policy,
615 },
616 {
617 .cmd = NFC_CMD_STOP_POLL,
618 .doit = nfc_genl_stop_poll,
619 .policy = nfc_genl_policy,
620 },
621 {
622 .cmd = NFC_CMD_DEP_LINK_UP,
623 .doit = nfc_genl_dep_link_up,
624 .policy = nfc_genl_policy,
625 },
626 {
627 .cmd = NFC_CMD_DEP_LINK_DOWN,
628 .doit = nfc_genl_dep_link_down,
629 .policy = nfc_genl_policy,
630 },
631 {
632 .cmd = NFC_CMD_GET_TARGET,
633 .dumpit = nfc_genl_dump_targets,
634 .done = nfc_genl_dump_targets_done,
635 .policy = nfc_genl_policy,
636 },
637};
638
639static int nfc_genl_rcv_nl_event(struct notifier_block *this,
640 unsigned long event, void *ptr)
641{
642 struct netlink_notify *n = ptr;
643 struct class_dev_iter iter;
644 struct nfc_dev *dev;
645
646 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
647 goto out;
648
649 pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
650
651 nfc_device_iter_init(&iter);
652 dev = nfc_device_iter_next(&iter);
653
654 while (dev) {
655 if (dev->genl_data.poll_req_pid == n->pid) {
656 nfc_stop_poll(dev);
657 dev->genl_data.poll_req_pid = 0;
658 }
659 dev = nfc_device_iter_next(&iter);
660 }
661
662 nfc_device_iter_exit(&iter);
663
664out:
665 return NOTIFY_DONE;
666}
667
668void nfc_genl_data_init(struct nfc_genl_data *genl_data)
669{
670 genl_data->poll_req_pid = 0;
671 mutex_init(&genl_data->genl_data_mutex);
672}
673
674void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
675{
676 mutex_destroy(&genl_data->genl_data_mutex);
677}
678
679static struct notifier_block nl_notifier = {
680 .notifier_call = nfc_genl_rcv_nl_event,
681};
682
683
684
685
686
687
688int __init nfc_genl_init(void)
689{
690 int rc;
691
692 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
693 ARRAY_SIZE(nfc_genl_ops));
694 if (rc)
695 return rc;
696
697 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
698
699 netlink_register_notifier(&nl_notifier);
700
701 return rc;
702}
703
704
705
706
707
708
709void nfc_genl_exit(void)
710{
711 netlink_unregister_notifier(&nl_notifier);
712 genl_unregister_family(&nfc_genl_family);
713}
714