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