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