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