GNU Linux-libre 4.14.254-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         nfc_device_iter_exit(iter);
670         kfree(iter);
671
672         return 0;
673 }
674
675 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
676                                u8 comm_mode, u8 rf_mode)
677 {
678         struct sk_buff *msg;
679         void *hdr;
680
681         pr_debug("DEP link is up\n");
682
683         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
684         if (!msg)
685                 return -ENOMEM;
686
687         hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
688         if (!hdr)
689                 goto free_msg;
690
691         if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
692                 goto nla_put_failure;
693         if (rf_mode == NFC_RF_INITIATOR &&
694             nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
695                 goto nla_put_failure;
696         if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
697             nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
698                 goto nla_put_failure;
699
700         genlmsg_end(msg, hdr);
701
702         dev->dep_link_up = true;
703
704         genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
705
706         return 0;
707
708 nla_put_failure:
709         genlmsg_cancel(msg, hdr);
710 free_msg:
711         nlmsg_free(msg);
712         return -EMSGSIZE;
713 }
714
715 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
716 {
717         struct sk_buff *msg;
718         void *hdr;
719
720         pr_debug("DEP link is down\n");
721
722         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
723         if (!msg)
724                 return -ENOMEM;
725
726         hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
727                           NFC_CMD_DEP_LINK_DOWN);
728         if (!hdr)
729                 goto free_msg;
730
731         if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
732                 goto nla_put_failure;
733
734         genlmsg_end(msg, hdr);
735
736         genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
737
738         return 0;
739
740 nla_put_failure:
741         genlmsg_cancel(msg, hdr);
742 free_msg:
743         nlmsg_free(msg);
744         return -EMSGSIZE;
745 }
746
747 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
748 {
749         struct sk_buff *msg;
750         struct nfc_dev *dev;
751         u32 idx;
752         int rc = -ENOBUFS;
753
754         if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
755                 return -EINVAL;
756
757         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
758
759         dev = nfc_get_device(idx);
760         if (!dev)
761                 return -ENODEV;
762
763         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
764         if (!msg) {
765                 rc = -ENOMEM;
766                 goto out_putdev;
767         }
768
769         rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
770                                   NULL, 0);
771         if (rc < 0)
772                 goto out_free;
773
774         nfc_put_device(dev);
775
776         return genlmsg_reply(msg, info);
777
778 out_free:
779         nlmsg_free(msg);
780 out_putdev:
781         nfc_put_device(dev);
782         return rc;
783 }
784
785 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
786 {
787         struct nfc_dev *dev;
788         int rc;
789         u32 idx;
790
791         if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
792                 return -EINVAL;
793
794         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
795
796         dev = nfc_get_device(idx);
797         if (!dev)
798                 return -ENODEV;
799
800         rc = nfc_dev_up(dev);
801
802         nfc_put_device(dev);
803         return rc;
804 }
805
806 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
807 {
808         struct nfc_dev *dev;
809         int rc;
810         u32 idx;
811
812         if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
813                 return -EINVAL;
814
815         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
816
817         dev = nfc_get_device(idx);
818         if (!dev)
819                 return -ENODEV;
820
821         rc = nfc_dev_down(dev);
822
823         nfc_put_device(dev);
824         return rc;
825 }
826
827 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
828 {
829         struct nfc_dev *dev;
830         int rc;
831         u32 idx;
832         u32 im_protocols = 0, tm_protocols = 0;
833
834         pr_debug("Poll start\n");
835
836         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
837             ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
838               !info->attrs[NFC_ATTR_PROTOCOLS]) &&
839               !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
840                 return -EINVAL;
841
842         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
843
844         if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
845                 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
846
847         if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
848                 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
849         else if (info->attrs[NFC_ATTR_PROTOCOLS])
850                 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
851
852         dev = nfc_get_device(idx);
853         if (!dev)
854                 return -ENODEV;
855
856         mutex_lock(&dev->genl_data.genl_data_mutex);
857
858         rc = nfc_start_poll(dev, im_protocols, tm_protocols);
859         if (!rc)
860                 dev->genl_data.poll_req_portid = info->snd_portid;
861
862         mutex_unlock(&dev->genl_data.genl_data_mutex);
863
864         nfc_put_device(dev);
865         return rc;
866 }
867
868 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
869 {
870         struct nfc_dev *dev;
871         int rc;
872         u32 idx;
873
874         if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
875                 return -EINVAL;
876
877         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
878
879         dev = nfc_get_device(idx);
880         if (!dev)
881                 return -ENODEV;
882
883         device_lock(&dev->dev);
884
885         if (!dev->polling) {
886                 device_unlock(&dev->dev);
887                 nfc_put_device(dev);
888                 return -EINVAL;
889         }
890
891         device_unlock(&dev->dev);
892
893         mutex_lock(&dev->genl_data.genl_data_mutex);
894
895         if (dev->genl_data.poll_req_portid != info->snd_portid) {
896                 rc = -EBUSY;
897                 goto out;
898         }
899
900         rc = nfc_stop_poll(dev);
901         dev->genl_data.poll_req_portid = 0;
902
903 out:
904         mutex_unlock(&dev->genl_data.genl_data_mutex);
905         nfc_put_device(dev);
906         return rc;
907 }
908
909 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
910 {
911         struct nfc_dev *dev;
912         u32 device_idx, target_idx, protocol;
913         int rc;
914
915         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
916             !info->attrs[NFC_ATTR_TARGET_INDEX] ||
917             !info->attrs[NFC_ATTR_PROTOCOLS])
918                 return -EINVAL;
919
920         device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
921
922         dev = nfc_get_device(device_idx);
923         if (!dev)
924                 return -ENODEV;
925
926         target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
927         protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
928
929         nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
930         rc = nfc_activate_target(dev, target_idx, protocol);
931
932         nfc_put_device(dev);
933         return rc;
934 }
935
936 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
937 {
938         struct nfc_dev *dev;
939         int rc, tgt_idx;
940         u32 idx;
941         u8 comm;
942
943         pr_debug("DEP link up\n");
944
945         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
946             !info->attrs[NFC_ATTR_COMM_MODE])
947                 return -EINVAL;
948
949         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
950         if (!info->attrs[NFC_ATTR_TARGET_INDEX])
951                 tgt_idx = NFC_TARGET_IDX_ANY;
952         else
953                 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
954
955         comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
956
957         if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
958                 return -EINVAL;
959
960         dev = nfc_get_device(idx);
961         if (!dev)
962                 return -ENODEV;
963
964         rc = nfc_dep_link_up(dev, tgt_idx, comm);
965
966         nfc_put_device(dev);
967
968         return rc;
969 }
970
971 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
972 {
973         struct nfc_dev *dev;
974         int rc;
975         u32 idx;
976
977         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
978             !info->attrs[NFC_ATTR_TARGET_INDEX])
979                 return -EINVAL;
980
981         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
982
983         dev = nfc_get_device(idx);
984         if (!dev)
985                 return -ENODEV;
986
987         rc = nfc_dep_link_down(dev);
988
989         nfc_put_device(dev);
990         return rc;
991 }
992
993 static int nfc_genl_send_params(struct sk_buff *msg,
994                                 struct nfc_llcp_local *local,
995                                 u32 portid, u32 seq)
996 {
997         void *hdr;
998
999         hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
1000                           NFC_CMD_LLC_GET_PARAMS);
1001         if (!hdr)
1002                 return -EMSGSIZE;
1003
1004         if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
1005             nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
1006             nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
1007             nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1008                 goto nla_put_failure;
1009
1010         genlmsg_end(msg, hdr);
1011         return 0;
1012
1013 nla_put_failure:
1014
1015         genlmsg_cancel(msg, hdr);
1016         return -EMSGSIZE;
1017 }
1018
1019 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1020 {
1021         struct nfc_dev *dev;
1022         struct nfc_llcp_local *local;
1023         int rc = 0;
1024         struct sk_buff *msg = NULL;
1025         u32 idx;
1026
1027         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1028             !info->attrs[NFC_ATTR_FIRMWARE_NAME])
1029                 return -EINVAL;
1030
1031         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1032
1033         dev = nfc_get_device(idx);
1034         if (!dev)
1035                 return -ENODEV;
1036
1037         device_lock(&dev->dev);
1038
1039         local = nfc_llcp_find_local(dev);
1040         if (!local) {
1041                 rc = -ENODEV;
1042                 goto exit;
1043         }
1044
1045         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1046         if (!msg) {
1047                 rc = -ENOMEM;
1048                 goto exit;
1049         }
1050
1051         rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1052
1053 exit:
1054         device_unlock(&dev->dev);
1055
1056         nfc_put_device(dev);
1057
1058         if (rc < 0) {
1059                 if (msg)
1060                         nlmsg_free(msg);
1061
1062                 return rc;
1063         }
1064
1065         return genlmsg_reply(msg, info);
1066 }
1067
1068 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1069 {
1070         struct nfc_dev *dev;
1071         struct nfc_llcp_local *local;
1072         u8 rw = 0;
1073         u16 miux = 0;
1074         u32 idx;
1075         int rc = 0;
1076
1077         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1078             (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1079              !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1080              !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1081                 return -EINVAL;
1082
1083         if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1084                 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1085
1086                 if (rw > LLCP_MAX_RW)
1087                         return -EINVAL;
1088         }
1089
1090         if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1091                 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1092
1093                 if (miux > LLCP_MAX_MIUX)
1094                         return -EINVAL;
1095         }
1096
1097         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1098
1099         dev = nfc_get_device(idx);
1100         if (!dev)
1101                 return -ENODEV;
1102
1103         device_lock(&dev->dev);
1104
1105         local = nfc_llcp_find_local(dev);
1106         if (!local) {
1107                 rc = -ENODEV;
1108                 goto exit;
1109         }
1110
1111         if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1112                 if (dev->dep_link_up) {
1113                         rc = -EINPROGRESS;
1114                         goto exit;
1115                 }
1116
1117                 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1118         }
1119
1120         if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1121                 local->rw = rw;
1122
1123         if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1124                 local->miux = cpu_to_be16(miux);
1125
1126 exit:
1127         device_unlock(&dev->dev);
1128
1129         nfc_put_device(dev);
1130
1131         return rc;
1132 }
1133
1134 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1135 {
1136         struct nfc_dev *dev;
1137         struct nfc_llcp_local *local;
1138         struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1139         u32 idx;
1140         u8 tid;
1141         char *uri;
1142         int rc = 0, rem;
1143         size_t uri_len, tlvs_len;
1144         struct hlist_head sdreq_list;
1145         struct nfc_llcp_sdp_tlv *sdreq;
1146
1147         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1148             !info->attrs[NFC_ATTR_LLC_SDP])
1149                 return -EINVAL;
1150
1151         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1152
1153         dev = nfc_get_device(idx);
1154         if (!dev)
1155                 return -ENODEV;
1156
1157         device_lock(&dev->dev);
1158
1159         if (dev->dep_link_up == false) {
1160                 rc = -ENOLINK;
1161                 goto exit;
1162         }
1163
1164         local = nfc_llcp_find_local(dev);
1165         if (!local) {
1166                 rc = -ENODEV;
1167                 goto exit;
1168         }
1169
1170         INIT_HLIST_HEAD(&sdreq_list);
1171
1172         tlvs_len = 0;
1173
1174         nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1175                 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1176                                       nfc_sdp_genl_policy, info->extack);
1177
1178                 if (rc != 0) {
1179                         rc = -EINVAL;
1180                         goto exit;
1181                 }
1182
1183                 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1184                         continue;
1185
1186                 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1187                 if (uri_len == 0)
1188                         continue;
1189
1190                 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1191                 if (uri == NULL || *uri == 0)
1192                         continue;
1193
1194                 tid = local->sdreq_next_tid++;
1195
1196                 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1197                 if (sdreq == NULL) {
1198                         rc = -ENOMEM;
1199                         goto exit;
1200                 }
1201
1202                 tlvs_len += sdreq->tlv_len;
1203
1204                 hlist_add_head(&sdreq->node, &sdreq_list);
1205         }
1206
1207         if (hlist_empty(&sdreq_list)) {
1208                 rc = -EINVAL;
1209                 goto exit;
1210         }
1211
1212         rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1213 exit:
1214         device_unlock(&dev->dev);
1215
1216         nfc_put_device(dev);
1217
1218         return rc;
1219 }
1220
1221 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1222 {
1223         struct nfc_dev *dev;
1224         int rc;
1225         u32 idx;
1226         char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1227
1228         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || !info->attrs[NFC_ATTR_FIRMWARE_NAME])
1229                 return -EINVAL;
1230
1231         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1232
1233         dev = nfc_get_device(idx);
1234         if (!dev)
1235                 return -ENODEV;
1236
1237         nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1238                     sizeof(firmware_name));
1239
1240         rc = nfc_fw_download(dev, firmware_name);
1241
1242         nfc_put_device(dev);
1243         return rc;
1244 }
1245
1246 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1247                               u32 result)
1248 {
1249         struct sk_buff *msg;
1250         void *hdr;
1251
1252         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1253         if (!msg)
1254                 return -ENOMEM;
1255
1256         hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1257                           NFC_CMD_FW_DOWNLOAD);
1258         if (!hdr)
1259                 goto free_msg;
1260
1261         if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1262             nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1263             nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1264                 goto nla_put_failure;
1265
1266         genlmsg_end(msg, hdr);
1267
1268         genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1269
1270         return 0;
1271
1272 nla_put_failure:
1273         genlmsg_cancel(msg, hdr);
1274 free_msg:
1275         nlmsg_free(msg);
1276         return -EMSGSIZE;
1277 }
1278
1279 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1280 {
1281         struct nfc_dev *dev;
1282         int rc;
1283         u32 idx, se_idx;
1284
1285         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1286             !info->attrs[NFC_ATTR_SE_INDEX])
1287                 return -EINVAL;
1288
1289         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1290         se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1291
1292         dev = nfc_get_device(idx);
1293         if (!dev)
1294                 return -ENODEV;
1295
1296         rc = nfc_enable_se(dev, se_idx);
1297
1298         nfc_put_device(dev);
1299         return rc;
1300 }
1301
1302 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1303 {
1304         struct nfc_dev *dev;
1305         int rc;
1306         u32 idx, se_idx;
1307
1308         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1309             !info->attrs[NFC_ATTR_SE_INDEX])
1310                 return -EINVAL;
1311
1312         idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1313         se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1314
1315         dev = nfc_get_device(idx);
1316         if (!dev)
1317                 return -ENODEV;
1318
1319         rc = nfc_disable_se(dev, se_idx);
1320
1321         nfc_put_device(dev);
1322         return rc;
1323 }
1324
1325 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1326                                 u32 portid, u32 seq,
1327                                 struct netlink_callback *cb,
1328                                 int flags)
1329 {
1330         void *hdr;
1331         struct nfc_se *se, *n;
1332
1333         list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1334                 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1335                                   NFC_CMD_GET_SE);
1336                 if (!hdr)
1337                         goto nla_put_failure;
1338
1339                 if (cb)
1340                         genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1341
1342                 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1343                     nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1344                     nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1345                         goto nla_put_failure;
1346
1347                 genlmsg_end(msg, hdr);
1348         }
1349
1350         return 0;
1351
1352 nla_put_failure:
1353         genlmsg_cancel(msg, hdr);
1354         return -EMSGSIZE;
1355 }
1356
1357 static int nfc_genl_dump_ses(struct sk_buff *skb,
1358                                  struct netlink_callback *cb)
1359 {
1360         struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1361         struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1362         bool first_call = false;
1363
1364         if (!iter) {
1365                 first_call = true;
1366                 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1367                 if (!iter)
1368                         return -ENOMEM;
1369                 cb->args[0] = (long) iter;
1370         }
1371
1372         mutex_lock(&nfc_devlist_mutex);
1373
1374         cb->seq = nfc_devlist_generation;
1375
1376         if (first_call) {
1377                 nfc_device_iter_init(iter);
1378                 dev = nfc_device_iter_next(iter);
1379         }
1380
1381         while (dev) {
1382                 int rc;
1383
1384                 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1385                                           cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1386                 if (rc < 0)
1387                         break;
1388
1389                 dev = nfc_device_iter_next(iter);
1390         }
1391
1392         mutex_unlock(&nfc_devlist_mutex);
1393
1394         cb->args[1] = (long) dev;
1395
1396         return skb->len;
1397 }
1398
1399 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1400 {
1401         struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1402
1403         nfc_device_iter_exit(iter);
1404         kfree(iter);
1405
1406         return 0;
1407 }
1408
1409 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1410                      u8 *apdu, size_t apdu_length,
1411                      se_io_cb_t cb, void *cb_context)
1412 {
1413         struct nfc_se *se;
1414         int rc;
1415
1416         pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1417
1418         device_lock(&dev->dev);
1419
1420         if (!device_is_registered(&dev->dev)) {
1421                 rc = -ENODEV;
1422                 goto error;
1423         }
1424
1425         if (!dev->dev_up) {
1426                 rc = -ENODEV;
1427                 goto error;
1428         }
1429
1430         if (!dev->ops->se_io) {
1431                 rc = -EOPNOTSUPP;
1432                 goto error;
1433         }
1434
1435         se = nfc_find_se(dev, se_idx);
1436         if (!se) {
1437                 rc = -EINVAL;
1438                 goto error;
1439         }
1440
1441         if (se->state != NFC_SE_ENABLED) {
1442                 rc = -ENODEV;
1443                 goto error;
1444         }
1445
1446         rc = dev->ops->se_io(dev, se_idx, apdu,
1447                         apdu_length, cb, cb_context);
1448
1449 error:
1450         device_unlock(&dev->dev);
1451         return rc;
1452 }
1453
1454 struct se_io_ctx {
1455         u32 dev_idx;
1456         u32 se_idx;
1457 };
1458
1459 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1460 {
1461         struct se_io_ctx *ctx = context;
1462         struct sk_buff *msg;
1463         void *hdr;
1464
1465         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1466         if (!msg) {
1467                 kfree(ctx);
1468                 return;
1469         }
1470
1471         hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1472                           NFC_CMD_SE_IO);
1473         if (!hdr)
1474                 goto free_msg;
1475
1476         if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1477             nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1478             nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1479                 goto nla_put_failure;
1480
1481         genlmsg_end(msg, hdr);
1482
1483         genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1484
1485         kfree(ctx);
1486
1487         return;
1488
1489 nla_put_failure:
1490         genlmsg_cancel(msg, hdr);
1491 free_msg:
1492         nlmsg_free(msg);
1493         kfree(ctx);
1494
1495         return;
1496 }
1497
1498 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1499 {
1500         struct nfc_dev *dev;
1501         struct se_io_ctx *ctx;
1502         u32 dev_idx, se_idx;
1503         u8 *apdu;
1504         size_t apdu_len;
1505
1506         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1507             !info->attrs[NFC_ATTR_SE_INDEX] ||
1508             !info->attrs[NFC_ATTR_SE_APDU])
1509                 return -EINVAL;
1510
1511         dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1512         se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1513
1514         dev = nfc_get_device(dev_idx);
1515         if (!dev)
1516                 return -ENODEV;
1517
1518         if (!dev->ops || !dev->ops->se_io)
1519                 return -ENOTSUPP;
1520
1521         apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1522         if (apdu_len == 0)
1523                 return -EINVAL;
1524
1525         apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1526         if (!apdu)
1527                 return -EINVAL;
1528
1529         ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1530         if (!ctx)
1531                 return -ENOMEM;
1532
1533         ctx->dev_idx = dev_idx;
1534         ctx->se_idx = se_idx;
1535
1536         return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1537 }
1538
1539 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1540                                struct genl_info *info)
1541 {
1542         struct nfc_dev *dev;
1543         struct nfc_vendor_cmd *cmd;
1544         u32 dev_idx, vid, subcmd;
1545         u8 *data;
1546         size_t data_len;
1547         int i, err;
1548
1549         if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1550             !info->attrs[NFC_ATTR_VENDOR_ID] ||
1551             !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1552                 return -EINVAL;
1553
1554         dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1555         vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1556         subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1557
1558         dev = nfc_get_device(dev_idx);
1559         if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1560                 return -ENODEV;
1561
1562         if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1563                 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1564                 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1565                 if (data_len == 0)
1566                         return -EINVAL;
1567         } else {
1568                 data = NULL;
1569                 data_len = 0;
1570         }
1571
1572         for (i = 0; i < dev->n_vendor_cmds; i++) {
1573                 cmd = &dev->vendor_cmds[i];
1574
1575                 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1576                         continue;
1577
1578                 dev->cur_cmd_info = info;
1579                 err = cmd->doit(dev, data, data_len);
1580                 dev->cur_cmd_info = NULL;
1581                 return err;
1582         }
1583
1584         return -EOPNOTSUPP;
1585 }
1586
1587 /* message building helper */
1588 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1589                                 int flags, u8 cmd)
1590 {
1591         /* since there is no private header just add the generic one */
1592         return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1593 }
1594
1595 static struct sk_buff *
1596 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1597                            u32 portid, u32 seq,
1598                            enum nfc_attrs attr,
1599                            u32 oui, u32 subcmd, gfp_t gfp)
1600 {
1601         struct sk_buff *skb;
1602         void *hdr;
1603
1604         skb = nlmsg_new(approxlen + 100, gfp);
1605         if (!skb)
1606                 return NULL;
1607
1608         hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1609         if (!hdr) {
1610                 kfree_skb(skb);
1611                 return NULL;
1612         }
1613
1614         if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1615                 goto nla_put_failure;
1616         if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1617                 goto nla_put_failure;
1618         if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1619                 goto nla_put_failure;
1620
1621         ((void **)skb->cb)[0] = dev;
1622         ((void **)skb->cb)[1] = hdr;
1623
1624         return skb;
1625
1626 nla_put_failure:
1627         kfree_skb(skb);
1628         return NULL;
1629 }
1630
1631 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1632                                                  enum nfc_attrs attr,
1633                                                  u32 oui, u32 subcmd,
1634                                                  int approxlen)
1635 {
1636         if (WARN_ON(!dev->cur_cmd_info))
1637                 return NULL;
1638
1639         return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1640                                           dev->cur_cmd_info->snd_portid,
1641                                           dev->cur_cmd_info->snd_seq, attr,
1642                                           oui, subcmd, GFP_KERNEL);
1643 }
1644 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1645
1646 int nfc_vendor_cmd_reply(struct sk_buff *skb)
1647 {
1648         struct nfc_dev *dev = ((void **)skb->cb)[0];
1649         void *hdr = ((void **)skb->cb)[1];
1650
1651         /* clear CB data for netlink core to own from now on */
1652         memset(skb->cb, 0, sizeof(skb->cb));
1653
1654         if (WARN_ON(!dev->cur_cmd_info)) {
1655                 kfree_skb(skb);
1656                 return -EINVAL;
1657         }
1658
1659         genlmsg_end(skb, hdr);
1660         return genlmsg_reply(skb, dev->cur_cmd_info);
1661 }
1662 EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1663
1664 static const struct genl_ops nfc_genl_ops[] = {
1665         {
1666                 .cmd = NFC_CMD_GET_DEVICE,
1667                 .doit = nfc_genl_get_device,
1668                 .dumpit = nfc_genl_dump_devices,
1669                 .done = nfc_genl_dump_devices_done,
1670                 .policy = nfc_genl_policy,
1671         },
1672         {
1673                 .cmd = NFC_CMD_DEV_UP,
1674                 .doit = nfc_genl_dev_up,
1675                 .policy = nfc_genl_policy,
1676         },
1677         {
1678                 .cmd = NFC_CMD_DEV_DOWN,
1679                 .doit = nfc_genl_dev_down,
1680                 .policy = nfc_genl_policy,
1681         },
1682         {
1683                 .cmd = NFC_CMD_START_POLL,
1684                 .doit = nfc_genl_start_poll,
1685                 .policy = nfc_genl_policy,
1686         },
1687         {
1688                 .cmd = NFC_CMD_STOP_POLL,
1689                 .doit = nfc_genl_stop_poll,
1690                 .policy = nfc_genl_policy,
1691         },
1692         {
1693                 .cmd = NFC_CMD_DEP_LINK_UP,
1694                 .doit = nfc_genl_dep_link_up,
1695                 .policy = nfc_genl_policy,
1696         },
1697         {
1698                 .cmd = NFC_CMD_DEP_LINK_DOWN,
1699                 .doit = nfc_genl_dep_link_down,
1700                 .policy = nfc_genl_policy,
1701         },
1702         {
1703                 .cmd = NFC_CMD_GET_TARGET,
1704                 .dumpit = nfc_genl_dump_targets,
1705                 .done = nfc_genl_dump_targets_done,
1706                 .policy = nfc_genl_policy,
1707         },
1708         {
1709                 .cmd = NFC_CMD_LLC_GET_PARAMS,
1710                 .doit = nfc_genl_llc_get_params,
1711                 .policy = nfc_genl_policy,
1712         },
1713         {
1714                 .cmd = NFC_CMD_LLC_SET_PARAMS,
1715                 .doit = nfc_genl_llc_set_params,
1716                 .policy = nfc_genl_policy,
1717         },
1718         {
1719                 .cmd = NFC_CMD_LLC_SDREQ,
1720                 .doit = nfc_genl_llc_sdreq,
1721                 .policy = nfc_genl_policy,
1722         },
1723         {
1724                 .cmd = NFC_CMD_FW_DOWNLOAD,
1725                 .doit = nfc_genl_fw_download,
1726                 .policy = nfc_genl_policy,
1727         },
1728         {
1729                 .cmd = NFC_CMD_ENABLE_SE,
1730                 .doit = nfc_genl_enable_se,
1731                 .policy = nfc_genl_policy,
1732         },
1733         {
1734                 .cmd = NFC_CMD_DISABLE_SE,
1735                 .doit = nfc_genl_disable_se,
1736                 .policy = nfc_genl_policy,
1737         },
1738         {
1739                 .cmd = NFC_CMD_GET_SE,
1740                 .dumpit = nfc_genl_dump_ses,
1741                 .done = nfc_genl_dump_ses_done,
1742                 .policy = nfc_genl_policy,
1743         },
1744         {
1745                 .cmd = NFC_CMD_SE_IO,
1746                 .doit = nfc_genl_se_io,
1747                 .policy = nfc_genl_policy,
1748         },
1749         {
1750                 .cmd = NFC_CMD_ACTIVATE_TARGET,
1751                 .doit = nfc_genl_activate_target,
1752                 .policy = nfc_genl_policy,
1753         },
1754         {
1755                 .cmd = NFC_CMD_VENDOR,
1756                 .doit = nfc_genl_vendor_cmd,
1757                 .policy = nfc_genl_policy,
1758         },
1759 };
1760
1761 static struct genl_family nfc_genl_family __ro_after_init = {
1762         .hdrsize = 0,
1763         .name = NFC_GENL_NAME,
1764         .version = NFC_GENL_VERSION,
1765         .maxattr = NFC_ATTR_MAX,
1766         .module = THIS_MODULE,
1767         .ops = nfc_genl_ops,
1768         .n_ops = ARRAY_SIZE(nfc_genl_ops),
1769         .mcgrps = nfc_genl_mcgrps,
1770         .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1771 };
1772
1773
1774 struct urelease_work {
1775         struct  work_struct w;
1776         u32     portid;
1777 };
1778
1779 static void nfc_urelease_event_work(struct work_struct *work)
1780 {
1781         struct urelease_work *w = container_of(work, struct urelease_work, w);
1782         struct class_dev_iter iter;
1783         struct nfc_dev *dev;
1784
1785         pr_debug("portid %d\n", w->portid);
1786
1787         mutex_lock(&nfc_devlist_mutex);
1788
1789         nfc_device_iter_init(&iter);
1790         dev = nfc_device_iter_next(&iter);
1791
1792         while (dev) {
1793                 mutex_lock(&dev->genl_data.genl_data_mutex);
1794
1795                 if (dev->genl_data.poll_req_portid == w->portid) {
1796                         nfc_stop_poll(dev);
1797                         dev->genl_data.poll_req_portid = 0;
1798                 }
1799
1800                 mutex_unlock(&dev->genl_data.genl_data_mutex);
1801
1802                 dev = nfc_device_iter_next(&iter);
1803         }
1804
1805         nfc_device_iter_exit(&iter);
1806
1807         mutex_unlock(&nfc_devlist_mutex);
1808
1809         kfree(w);
1810 }
1811
1812 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1813                                  unsigned long event, void *ptr)
1814 {
1815         struct netlink_notify *n = ptr;
1816         struct urelease_work *w;
1817
1818         if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1819                 goto out;
1820
1821         pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1822
1823         w = kmalloc(sizeof(*w), GFP_ATOMIC);
1824         if (w) {
1825                 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1826                 w->portid = n->portid;
1827                 schedule_work((struct work_struct *) w);
1828         }
1829
1830 out:
1831         return NOTIFY_DONE;
1832 }
1833
1834 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1835 {
1836         genl_data->poll_req_portid = 0;
1837         mutex_init(&genl_data->genl_data_mutex);
1838 }
1839
1840 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1841 {
1842         mutex_destroy(&genl_data->genl_data_mutex);
1843 }
1844
1845 static struct notifier_block nl_notifier = {
1846         .notifier_call  = nfc_genl_rcv_nl_event,
1847 };
1848
1849 /**
1850  * nfc_genl_init() - Initialize netlink interface
1851  *
1852  * This initialization function registers the nfc netlink family.
1853  */
1854 int __init nfc_genl_init(void)
1855 {
1856         int rc;
1857
1858         rc = genl_register_family(&nfc_genl_family);
1859         if (rc)
1860                 return rc;
1861
1862         netlink_register_notifier(&nl_notifier);
1863
1864         return 0;
1865 }
1866
1867 /**
1868  * nfc_genl_exit() - Deinitialize netlink interface
1869  *
1870  * This exit function unregisters the nfc netlink family.
1871  */
1872 void nfc_genl_exit(void)
1873 {
1874         netlink_unregister_notifier(&nl_notifier);
1875         genl_unregister_family(&nfc_genl_family);
1876 }