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