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