blob: ebbf6fb88b3596708597331ae630be2a7dcade6d [file] [log] [blame]
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001/*
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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
Jeff Kirsher98b32de2013-12-06 08:56:16 -080019 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030020 */
21
Samuel Ortiz52858b52011-12-14 16:43:05 +010022#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perches20c239c2011-11-29 11:37:33 -080023
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030024#include <net/genetlink.h>
25#include <linux/nfc.h>
26#include <linux/slab.h>
27
28#include "nfc.h"
Samuel Ortiz30cc4582013-04-26 11:49:40 +020029#include "llcp.h"
Thierry Escande52feb442012-10-17 14:43:39 +020030
Johannes Berg2a94fe42013-11-19 15:19:39 +010031static const struct genl_multicast_group nfc_genl_mcgrps[] = {
32 { .name = NFC_GENL_MCAST_EVENT_NAME, },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030033};
34
H Hartley Sweetene5fe4cf2012-05-07 12:31:28 +020035static struct genl_family nfc_genl_family = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030036 .id = GENL_ID_GENERATE,
37 .hdrsize = 0,
38 .name = NFC_GENL_NAME,
39 .version = NFC_GENL_VERSION,
40 .maxattr = NFC_ATTR_MAX,
41};
42
43static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
44 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
45 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
46 .len = NFC_DEVICE_NAME_MAXSIZE },
47 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010048 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
49 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010050 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020051 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
52 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Thierry Escande8af362d2013-02-15 10:42:52 +010053 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
54 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
55 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010056 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
Eric Lapuyade9674da82013-04-29 17:13:27 +020057 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
58 .len = NFC_FIRMWARE_NAME_MAXSIZE },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +020059 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010060};
61
62static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
63 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
64 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030065};
66
67static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010068 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030069{
70 void *hdr;
71
Eric W. Biederman15e47302012-09-07 20:12:54 +000072 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010073 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030074 if (!hdr)
75 return -EMSGSIZE;
76
77 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
78
David S. Miller1e6428d2012-03-29 23:23:57 -040079 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
80 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
81 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
82 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
83 goto nla_put_failure;
84 if (target->nfcid1_len > 0 &&
85 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
86 target->nfcid1))
87 goto nla_put_failure;
88 if (target->sensb_res_len > 0 &&
89 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
90 target->sensb_res))
91 goto nla_put_failure;
92 if (target->sensf_res_len > 0 &&
93 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
94 target->sensf_res))
95 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030096
97 return genlmsg_end(msg, hdr);
98
99nla_put_failure:
100 genlmsg_cancel(msg, hdr);
101 return -EMSGSIZE;
102}
103
104static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
105{
106 struct nfc_dev *dev;
107 int rc;
108 u32 idx;
109
110 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100111 nfc_genl_family.attrbuf,
112 nfc_genl_family.maxattr,
113 nfc_genl_policy);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300114 if (rc < 0)
115 return ERR_PTR(rc);
116
117 if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
118 return ERR_PTR(-EINVAL);
119
120 idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
121
122 dev = nfc_get_device(idx);
123 if (!dev)
124 return ERR_PTR(-ENODEV);
125
126 return dev;
127}
128
129static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100130 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300131{
132 int i = cb->args[0];
133 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
134 int rc;
135
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300136 if (!dev) {
137 dev = __get_device_from_cb(cb);
138 if (IS_ERR(dev))
139 return PTR_ERR(dev);
140
141 cb->args[1] = (long) dev;
142 }
143
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200144 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300145
146 cb->seq = dev->targets_generation;
147
148 while (i < dev->n_targets) {
149 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100150 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300151 if (rc < 0)
152 break;
153
154 i++;
155 }
156
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200157 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300158
159 cb->args[0] = i;
160
161 return skb->len;
162}
163
164static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
165{
166 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
167
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300168 if (dev)
169 nfc_put_device(dev);
170
171 return 0;
172}
173
174int nfc_genl_targets_found(struct nfc_dev *dev)
175{
176 struct sk_buff *msg;
177 void *hdr;
178
Eric W. Biederman15e47302012-09-07 20:12:54 +0000179 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300180
Thomas Graf58050fc2012-06-28 03:57:45 +0000181 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300182 if (!msg)
183 return -ENOMEM;
184
185 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100186 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300187 if (!hdr)
188 goto free_msg;
189
David S. Miller1e6428d2012-03-29 23:23:57 -0400190 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
191 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300192
193 genlmsg_end(msg, hdr);
194
Johannes Berg2a94fe42013-11-19 15:19:39 +0100195 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300196
197nla_put_failure:
198 genlmsg_cancel(msg, hdr);
199free_msg:
200 nlmsg_free(msg);
201 return -EMSGSIZE;
202}
203
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200204int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
205{
206 struct sk_buff *msg;
207 void *hdr;
208
Thomas Graf58050fc2012-06-28 03:57:45 +0000209 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200210 if (!msg)
211 return -ENOMEM;
212
213 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
214 NFC_EVENT_TARGET_LOST);
215 if (!hdr)
216 goto free_msg;
217
John W. Linville59ef43e2012-04-18 14:17:13 -0400218 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
219 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
220 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200221
222 genlmsg_end(msg, hdr);
223
Johannes Berg2a94fe42013-11-19 15:19:39 +0100224 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200225
226 return 0;
227
228nla_put_failure:
229 genlmsg_cancel(msg, hdr);
230free_msg:
231 nlmsg_free(msg);
232 return -EMSGSIZE;
233}
234
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200235int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
236{
237 struct sk_buff *msg;
238 void *hdr;
239
Thomas Graf58050fc2012-06-28 03:57:45 +0000240 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200241 if (!msg)
242 return -ENOMEM;
243
244 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
245 NFC_EVENT_TM_ACTIVATED);
246 if (!hdr)
247 goto free_msg;
248
249 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
250 goto nla_put_failure;
251 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
252 goto nla_put_failure;
253
254 genlmsg_end(msg, hdr);
255
Johannes Berg2a94fe42013-11-19 15:19:39 +0100256 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200257
258 return 0;
259
260nla_put_failure:
261 genlmsg_cancel(msg, hdr);
262free_msg:
263 nlmsg_free(msg);
264 return -EMSGSIZE;
265}
266
267int nfc_genl_tm_deactivated(struct nfc_dev *dev)
268{
269 struct sk_buff *msg;
270 void *hdr;
271
Thomas Graf58050fc2012-06-28 03:57:45 +0000272 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200273 if (!msg)
274 return -ENOMEM;
275
276 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
277 NFC_EVENT_TM_DEACTIVATED);
278 if (!hdr)
279 goto free_msg;
280
281 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
282 goto nla_put_failure;
283
284 genlmsg_end(msg, hdr);
285
Johannes Berg2a94fe42013-11-19 15:19:39 +0100286 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200287
288 return 0;
289
290nla_put_failure:
291 genlmsg_cancel(msg, hdr);
292free_msg:
293 nlmsg_free(msg);
294 return -EMSGSIZE;
295}
296
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300297int nfc_genl_device_added(struct nfc_dev *dev)
298{
299 struct sk_buff *msg;
300 void *hdr;
301
Thomas Graf58050fc2012-06-28 03:57:45 +0000302 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300303 if (!msg)
304 return -ENOMEM;
305
306 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100307 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300308 if (!hdr)
309 goto free_msg;
310
David S. Miller1e6428d2012-03-29 23:23:57 -0400311 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
312 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
313 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
314 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
315 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300316
317 genlmsg_end(msg, hdr);
318
Johannes Berg2a94fe42013-11-19 15:19:39 +0100319 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300320
321 return 0;
322
323nla_put_failure:
324 genlmsg_cancel(msg, hdr);
325free_msg:
326 nlmsg_free(msg);
327 return -EMSGSIZE;
328}
329
330int nfc_genl_device_removed(struct nfc_dev *dev)
331{
332 struct sk_buff *msg;
333 void *hdr;
334
Thomas Graf58050fc2012-06-28 03:57:45 +0000335 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300336 if (!msg)
337 return -ENOMEM;
338
339 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100340 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300341 if (!hdr)
342 goto free_msg;
343
David S. Miller1e6428d2012-03-29 23:23:57 -0400344 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
345 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300346
347 genlmsg_end(msg, hdr);
348
Johannes Berg2a94fe42013-11-19 15:19:39 +0100349 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300350
351 return 0;
352
353nla_put_failure:
354 genlmsg_cancel(msg, hdr);
355free_msg:
356 nlmsg_free(msg);
357 return -EMSGSIZE;
358}
359
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100360int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
361{
362 struct sk_buff *msg;
363 struct nlattr *sdp_attr, *uri_attr;
364 struct nfc_llcp_sdp_tlv *sdres;
365 struct hlist_node *n;
366 void *hdr;
367 int rc = -EMSGSIZE;
368 int i;
369
370 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
371 if (!msg)
372 return -ENOMEM;
373
374 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
375 NFC_EVENT_LLC_SDRES);
376 if (!hdr)
377 goto free_msg;
378
379 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
380 goto nla_put_failure;
381
382 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
383 if (sdp_attr == NULL) {
384 rc = -ENOMEM;
385 goto nla_put_failure;
386 }
387
388 i = 1;
389 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
390 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
391
392 uri_attr = nla_nest_start(msg, i++);
393 if (uri_attr == NULL) {
394 rc = -ENOMEM;
395 goto nla_put_failure;
396 }
397
398 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
399 goto nla_put_failure;
400
401 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
402 goto nla_put_failure;
403
404 nla_nest_end(msg, uri_attr);
405
406 hlist_del(&sdres->node);
407
408 nfc_llcp_free_sdp_tlv(sdres);
409 }
410
411 nla_nest_end(msg, sdp_attr);
412
413 genlmsg_end(msg, hdr);
414
Johannes Berg2a94fe42013-11-19 15:19:39 +0100415 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100416
417nla_put_failure:
418 genlmsg_cancel(msg, hdr);
419
420free_msg:
421 nlmsg_free(msg);
422
423 nfc_llcp_free_sdp_tlv_list(sdres_list);
424
425 return rc;
426}
427
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200428int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
429{
430 struct sk_buff *msg;
431 void *hdr;
432
433 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
434 if (!msg)
435 return -ENOMEM;
436
437 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
438 NFC_EVENT_SE_ADDED);
439 if (!hdr)
440 goto free_msg;
441
442 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
443 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
444 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
445 goto nla_put_failure;
446
447 genlmsg_end(msg, hdr);
448
Johannes Berg2a94fe42013-11-19 15:19:39 +0100449 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200450
451 return 0;
452
453nla_put_failure:
454 genlmsg_cancel(msg, hdr);
455free_msg:
456 nlmsg_free(msg);
457 return -EMSGSIZE;
458}
459
460int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
461{
462 struct sk_buff *msg;
463 void *hdr;
464
465 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
466 if (!msg)
467 return -ENOMEM;
468
469 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
470 NFC_EVENT_SE_REMOVED);
471 if (!hdr)
472 goto free_msg;
473
474 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
475 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
476 goto nla_put_failure;
477
478 genlmsg_end(msg, hdr);
479
Johannes Berg2a94fe42013-11-19 15:19:39 +0100480 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200481
482 return 0;
483
484nla_put_failure:
485 genlmsg_cancel(msg, hdr);
486free_msg:
487 nlmsg_free(msg);
488 return -EMSGSIZE;
489}
490
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300491static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000492 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100493 struct netlink_callback *cb,
494 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300495{
496 void *hdr;
497
Eric W. Biederman15e47302012-09-07 20:12:54 +0000498 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100499 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300500 if (!hdr)
501 return -EMSGSIZE;
502
503 if (cb)
504 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
505
David S. Miller1e6428d2012-03-29 23:23:57 -0400506 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
507 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
508 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200509 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
510 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400511 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300512
513 return genlmsg_end(msg, hdr);
514
515nla_put_failure:
516 genlmsg_cancel(msg, hdr);
517 return -EMSGSIZE;
518}
519
520static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100521 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300522{
523 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
524 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
525 bool first_call = false;
526
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300527 if (!iter) {
528 first_call = true;
529 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
530 if (!iter)
531 return -ENOMEM;
532 cb->args[0] = (long) iter;
533 }
534
535 mutex_lock(&nfc_devlist_mutex);
536
537 cb->seq = nfc_devlist_generation;
538
539 if (first_call) {
540 nfc_device_iter_init(iter);
541 dev = nfc_device_iter_next(iter);
542 }
543
544 while (dev) {
545 int rc;
546
Eric W. Biederman15e47302012-09-07 20:12:54 +0000547 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100548 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300549 if (rc < 0)
550 break;
551
552 dev = nfc_device_iter_next(iter);
553 }
554
555 mutex_unlock(&nfc_devlist_mutex);
556
557 cb->args[1] = (long) dev;
558
559 return skb->len;
560}
561
562static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
563{
564 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
565
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300566 nfc_device_iter_exit(iter);
567 kfree(iter);
568
569 return 0;
570}
571
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100572int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100573 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100574{
575 struct sk_buff *msg;
576 void *hdr;
577
578 pr_debug("DEP link is up\n");
579
Thomas Graf58050fc2012-06-28 03:57:45 +0000580 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100581 if (!msg)
582 return -ENOMEM;
583
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100584 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100585 if (!hdr)
586 goto free_msg;
587
David S. Miller1e6428d2012-03-29 23:23:57 -0400588 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
589 goto nla_put_failure;
590 if (rf_mode == NFC_RF_INITIATOR &&
591 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
592 goto nla_put_failure;
593 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
594 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
595 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100596
597 genlmsg_end(msg, hdr);
598
599 dev->dep_link_up = true;
600
Johannes Berg2a94fe42013-11-19 15:19:39 +0100601 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100602
603 return 0;
604
605nla_put_failure:
606 genlmsg_cancel(msg, hdr);
607free_msg:
608 nlmsg_free(msg);
609 return -EMSGSIZE;
610}
611
612int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
613{
614 struct sk_buff *msg;
615 void *hdr;
616
617 pr_debug("DEP link is down\n");
618
Thomas Graf58050fc2012-06-28 03:57:45 +0000619 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100620 if (!msg)
621 return -ENOMEM;
622
623 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100624 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100625 if (!hdr)
626 goto free_msg;
627
David S. Miller1e6428d2012-03-29 23:23:57 -0400628 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
629 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100630
631 genlmsg_end(msg, hdr);
632
Johannes Berg2a94fe42013-11-19 15:19:39 +0100633 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100634
635 return 0;
636
637nla_put_failure:
638 genlmsg_cancel(msg, hdr);
639free_msg:
640 nlmsg_free(msg);
641 return -EMSGSIZE;
642}
643
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300644static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
645{
646 struct sk_buff *msg;
647 struct nfc_dev *dev;
648 u32 idx;
649 int rc = -ENOBUFS;
650
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300651 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
652 return -EINVAL;
653
654 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
655
656 dev = nfc_get_device(idx);
657 if (!dev)
658 return -ENODEV;
659
Thomas Graf58050fc2012-06-28 03:57:45 +0000660 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300661 if (!msg) {
662 rc = -ENOMEM;
663 goto out_putdev;
664 }
665
Eric W. Biederman15e47302012-09-07 20:12:54 +0000666 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100667 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300668 if (rc < 0)
669 goto out_free;
670
671 nfc_put_device(dev);
672
673 return genlmsg_reply(msg, info);
674
675out_free:
676 nlmsg_free(msg);
677out_putdev:
678 nfc_put_device(dev);
679 return rc;
680}
681
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300682static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
683{
684 struct nfc_dev *dev;
685 int rc;
686 u32 idx;
687
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300688 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
689 return -EINVAL;
690
691 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
692
693 dev = nfc_get_device(idx);
694 if (!dev)
695 return -ENODEV;
696
697 rc = nfc_dev_up(dev);
698
699 nfc_put_device(dev);
700 return rc;
701}
702
703static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
704{
705 struct nfc_dev *dev;
706 int rc;
707 u32 idx;
708
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300709 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
710 return -EINVAL;
711
712 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
713
714 dev = nfc_get_device(idx);
715 if (!dev)
716 return -ENODEV;
717
718 rc = nfc_dev_down(dev);
719
720 nfc_put_device(dev);
721 return rc;
722}
723
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300724static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
725{
726 struct nfc_dev *dev;
727 int rc;
728 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200729 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300730
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100731 pr_debug("Poll start\n");
732
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300733 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200734 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
735 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200736 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300737 return -EINVAL;
738
739 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200740
741 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
742 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200743
744 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
745 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200746 else if (info->attrs[NFC_ATTR_PROTOCOLS])
747 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300748
749 dev = nfc_get_device(idx);
750 if (!dev)
751 return -ENODEV;
752
753 mutex_lock(&dev->genl_data.genl_data_mutex);
754
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200755 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300756 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000757 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300758
759 mutex_unlock(&dev->genl_data.genl_data_mutex);
760
761 nfc_put_device(dev);
762 return rc;
763}
764
765static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
766{
767 struct nfc_dev *dev;
768 int rc;
769 u32 idx;
770
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300771 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
772 return -EINVAL;
773
774 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
775
776 dev = nfc_get_device(idx);
777 if (!dev)
778 return -ENODEV;
779
Samuel Ortiza831b912012-06-28 16:41:57 +0200780 device_lock(&dev->dev);
781
782 if (!dev->polling) {
783 device_unlock(&dev->dev);
784 return -EINVAL;
785 }
786
787 device_unlock(&dev->dev);
788
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300789 mutex_lock(&dev->genl_data.genl_data_mutex);
790
Eric W. Biederman15e47302012-09-07 20:12:54 +0000791 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300792 rc = -EBUSY;
793 goto out;
794 }
795
796 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000797 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300798
799out:
800 mutex_unlock(&dev->genl_data.genl_data_mutex);
801 nfc_put_device(dev);
802 return rc;
803}
804
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100805static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
806{
807 struct nfc_dev *dev;
808 int rc, tgt_idx;
809 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100810 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100811
812 pr_debug("DEP link up\n");
813
814 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100815 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100816 return -EINVAL;
817
818 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
819 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
820 tgt_idx = NFC_TARGET_IDX_ANY;
821 else
822 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
823
824 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100825
826 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
827 return -EINVAL;
828
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100829 dev = nfc_get_device(idx);
830 if (!dev)
831 return -ENODEV;
832
Samuel Ortiz47807d32012-03-05 01:03:50 +0100833 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100834
835 nfc_put_device(dev);
836
837 return rc;
838}
839
840static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
841{
842 struct nfc_dev *dev;
843 int rc;
844 u32 idx;
845
846 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
847 return -EINVAL;
848
849 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
850
851 dev = nfc_get_device(idx);
852 if (!dev)
853 return -ENODEV;
854
855 rc = nfc_dep_link_down(dev);
856
857 nfc_put_device(dev);
858 return rc;
859}
860
Thierry Escande52feb442012-10-17 14:43:39 +0200861static int nfc_genl_send_params(struct sk_buff *msg,
862 struct nfc_llcp_local *local,
863 u32 portid, u32 seq)
864{
865 void *hdr;
866
867 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
868 NFC_CMD_LLC_GET_PARAMS);
869 if (!hdr)
870 return -EMSGSIZE;
871
872 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
873 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
874 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
875 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
876 goto nla_put_failure;
877
878 return genlmsg_end(msg, hdr);
879
880nla_put_failure:
881
882 genlmsg_cancel(msg, hdr);
883 return -EMSGSIZE;
884}
885
886static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
887{
888 struct nfc_dev *dev;
889 struct nfc_llcp_local *local;
890 int rc = 0;
891 struct sk_buff *msg = NULL;
892 u32 idx;
893
894 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
895 return -EINVAL;
896
897 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
898
899 dev = nfc_get_device(idx);
900 if (!dev)
901 return -ENODEV;
902
903 device_lock(&dev->dev);
904
905 local = nfc_llcp_find_local(dev);
906 if (!local) {
907 rc = -ENODEV;
908 goto exit;
909 }
910
911 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
912 if (!msg) {
913 rc = -ENOMEM;
914 goto exit;
915 }
916
917 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
918
919exit:
920 device_unlock(&dev->dev);
921
922 nfc_put_device(dev);
923
924 if (rc < 0) {
925 if (msg)
926 nlmsg_free(msg);
927
928 return rc;
929 }
930
931 return genlmsg_reply(msg, info);
932}
933
934static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
935{
936 struct nfc_dev *dev;
937 struct nfc_llcp_local *local;
938 u8 rw = 0;
939 u16 miux = 0;
940 u32 idx;
941 int rc = 0;
942
943 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
944 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
945 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
946 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
947 return -EINVAL;
948
949 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
950 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
951
952 if (rw > LLCP_MAX_RW)
953 return -EINVAL;
954 }
955
956 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
957 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
958
959 if (miux > LLCP_MAX_MIUX)
960 return -EINVAL;
961 }
962
963 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
964
965 dev = nfc_get_device(idx);
966 if (!dev)
967 return -ENODEV;
968
969 device_lock(&dev->dev);
970
971 local = nfc_llcp_find_local(dev);
972 if (!local) {
973 nfc_put_device(dev);
974 rc = -ENODEV;
975 goto exit;
976 }
977
978 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
979 if (dev->dep_link_up) {
980 rc = -EINPROGRESS;
981 goto exit;
982 }
983
984 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
985 }
986
987 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
988 local->rw = rw;
989
990 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
991 local->miux = cpu_to_be16(miux);
992
993exit:
994 device_unlock(&dev->dev);
995
996 nfc_put_device(dev);
997
998 return rc;
999}
1000
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001001static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1002{
1003 struct nfc_dev *dev;
1004 struct nfc_llcp_local *local;
1005 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1006 u32 idx;
1007 u8 tid;
1008 char *uri;
1009 int rc = 0, rem;
1010 size_t uri_len, tlvs_len;
1011 struct hlist_head sdreq_list;
1012 struct nfc_llcp_sdp_tlv *sdreq;
1013
1014 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1015 !info->attrs[NFC_ATTR_LLC_SDP])
1016 return -EINVAL;
1017
1018 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1019
1020 dev = nfc_get_device(idx);
1021 if (!dev) {
1022 rc = -ENODEV;
1023 goto exit;
1024 }
1025
1026 device_lock(&dev->dev);
1027
1028 if (dev->dep_link_up == false) {
1029 rc = -ENOLINK;
1030 goto exit;
1031 }
1032
1033 local = nfc_llcp_find_local(dev);
1034 if (!local) {
1035 nfc_put_device(dev);
1036 rc = -ENODEV;
1037 goto exit;
1038 }
1039
1040 INIT_HLIST_HEAD(&sdreq_list);
1041
1042 tlvs_len = 0;
1043
1044 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1045 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1046 nfc_sdp_genl_policy);
1047
1048 if (rc != 0) {
1049 rc = -EINVAL;
1050 goto exit;
1051 }
1052
1053 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1054 continue;
1055
1056 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1057 if (uri_len == 0)
1058 continue;
1059
1060 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1061 if (uri == NULL || *uri == 0)
1062 continue;
1063
1064 tid = local->sdreq_next_tid++;
1065
1066 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1067 if (sdreq == NULL) {
1068 rc = -ENOMEM;
1069 goto exit;
1070 }
1071
1072 tlvs_len += sdreq->tlv_len;
1073
1074 hlist_add_head(&sdreq->node, &sdreq_list);
1075 }
1076
1077 if (hlist_empty(&sdreq_list)) {
1078 rc = -EINVAL;
1079 goto exit;
1080 }
1081
1082 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1083exit:
1084 device_unlock(&dev->dev);
1085
1086 nfc_put_device(dev);
1087
1088 return rc;
1089}
1090
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001091static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001092{
1093 struct nfc_dev *dev;
1094 int rc;
1095 u32 idx;
1096 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1097
1098 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1099 return -EINVAL;
1100
1101 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1102
1103 dev = nfc_get_device(idx);
1104 if (!dev)
1105 return -ENODEV;
1106
1107 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1108 sizeof(firmware_name));
1109
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001110 rc = nfc_fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001111
1112 nfc_put_device(dev);
1113 return rc;
1114}
1115
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001116int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1117 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001118{
1119 struct sk_buff *msg;
1120 void *hdr;
1121
1122 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1123 if (!msg)
1124 return -ENOMEM;
1125
1126 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001127 NFC_CMD_FW_DOWNLOAD);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001128 if (!hdr)
1129 goto free_msg;
1130
1131 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001132 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
Eric Lapuyade9674da82013-04-29 17:13:27 +02001133 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1134 goto nla_put_failure;
1135
1136 genlmsg_end(msg, hdr);
1137
Johannes Berg2a94fe42013-11-19 15:19:39 +01001138 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001139
1140 return 0;
1141
1142nla_put_failure:
1143 genlmsg_cancel(msg, hdr);
1144free_msg:
1145 nlmsg_free(msg);
1146 return -EMSGSIZE;
1147}
1148
Samuel Ortizbe085652013-05-10 17:07:32 +02001149static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1150{
1151 struct nfc_dev *dev;
1152 int rc;
1153 u32 idx, se_idx;
1154
1155 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1156 !info->attrs[NFC_ATTR_SE_INDEX])
1157 return -EINVAL;
1158
1159 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1160 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1161
1162 dev = nfc_get_device(idx);
1163 if (!dev)
1164 return -ENODEV;
1165
1166 rc = nfc_enable_se(dev, se_idx);
1167
1168 nfc_put_device(dev);
1169 return rc;
1170}
1171
1172static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1173{
1174 struct nfc_dev *dev;
1175 int rc;
1176 u32 idx, se_idx;
1177
1178 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1179 !info->attrs[NFC_ATTR_SE_INDEX])
1180 return -EINVAL;
1181
1182 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1183 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1184
1185 dev = nfc_get_device(idx);
1186 if (!dev)
1187 return -ENODEV;
1188
1189 rc = nfc_disable_se(dev, se_idx);
1190
1191 nfc_put_device(dev);
1192 return rc;
1193}
1194
Samuel Ortizac22ac42013-07-24 18:10:50 +02001195static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1196 u32 portid, u32 seq,
1197 struct netlink_callback *cb,
1198 int flags)
1199{
1200 void *hdr;
1201 struct nfc_se *se, *n;
1202
1203 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1204 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1205 NFC_CMD_GET_SE);
1206 if (!hdr)
1207 goto nla_put_failure;
1208
1209 if (cb)
1210 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1211
1212 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1213 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1214 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1215 goto nla_put_failure;
1216
1217 if (genlmsg_end(msg, hdr) < 0)
1218 goto nla_put_failure;
1219 }
1220
1221 return 0;
1222
1223nla_put_failure:
1224 genlmsg_cancel(msg, hdr);
1225 return -EMSGSIZE;
1226}
1227
1228static int nfc_genl_dump_ses(struct sk_buff *skb,
1229 struct netlink_callback *cb)
1230{
1231 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1232 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1233 bool first_call = false;
1234
1235 if (!iter) {
1236 first_call = true;
1237 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1238 if (!iter)
1239 return -ENOMEM;
1240 cb->args[0] = (long) iter;
1241 }
1242
1243 mutex_lock(&nfc_devlist_mutex);
1244
1245 cb->seq = nfc_devlist_generation;
1246
1247 if (first_call) {
1248 nfc_device_iter_init(iter);
1249 dev = nfc_device_iter_next(iter);
1250 }
1251
1252 while (dev) {
1253 int rc;
1254
1255 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1256 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1257 if (rc < 0)
1258 break;
1259
1260 dev = nfc_device_iter_next(iter);
1261 }
1262
1263 mutex_unlock(&nfc_devlist_mutex);
1264
1265 cb->args[1] = (long) dev;
1266
1267 return skb->len;
1268}
1269
1270static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1271{
1272 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1273
1274 nfc_device_iter_exit(iter);
1275 kfree(iter);
1276
1277 return 0;
1278}
1279
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001280struct se_io_ctx {
1281 u32 dev_idx;
1282 u32 se_idx;
1283};
1284
Samuel Ortizddc1a702013-10-07 14:18:44 +02001285static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001286{
1287 struct se_io_ctx *ctx = context;
1288 struct sk_buff *msg;
1289 void *hdr;
1290
1291 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1292 if (!msg) {
1293 kfree(ctx);
1294 return;
1295 }
1296
1297 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1298 NFC_CMD_SE_IO);
1299 if (!hdr)
1300 goto free_msg;
1301
1302 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1303 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1304 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1305 goto nla_put_failure;
1306
1307 genlmsg_end(msg, hdr);
1308
Johannes Berg2a94fe42013-11-19 15:19:39 +01001309 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001310
1311 kfree(ctx);
1312
1313 return;
1314
1315nla_put_failure:
1316 genlmsg_cancel(msg, hdr);
1317free_msg:
1318 nlmsg_free(msg);
1319 kfree(ctx);
1320
1321 return;
1322}
1323
1324static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1325{
1326 struct nfc_dev *dev;
1327 struct se_io_ctx *ctx;
1328 u32 dev_idx, se_idx;
1329 u8 *apdu;
1330 size_t apdu_len;
1331
1332 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1333 !info->attrs[NFC_ATTR_SE_INDEX] ||
1334 !info->attrs[NFC_ATTR_SE_APDU])
1335 return -EINVAL;
1336
1337 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1338 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1339
1340 dev = nfc_get_device(dev_idx);
1341 if (!dev)
1342 return -ENODEV;
1343
1344 if (!dev->ops || !dev->ops->se_io)
1345 return -ENOTSUPP;
1346
1347 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1348 if (apdu_len == 0)
1349 return -EINVAL;
1350
1351 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1352 if (!apdu)
1353 return -EINVAL;
1354
1355 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1356 if (!ctx)
1357 return -ENOMEM;
1358
1359 ctx->dev_idx = dev_idx;
1360 ctx->se_idx = se_idx;
1361
1362 return dev->ops->se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1363}
1364
Johannes Berg4534de82013-11-14 17:14:46 +01001365static const struct genl_ops nfc_genl_ops[] = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001366 {
1367 .cmd = NFC_CMD_GET_DEVICE,
1368 .doit = nfc_genl_get_device,
1369 .dumpit = nfc_genl_dump_devices,
1370 .done = nfc_genl_dump_devices_done,
1371 .policy = nfc_genl_policy,
1372 },
1373 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001374 .cmd = NFC_CMD_DEV_UP,
1375 .doit = nfc_genl_dev_up,
1376 .policy = nfc_genl_policy,
1377 },
1378 {
1379 .cmd = NFC_CMD_DEV_DOWN,
1380 .doit = nfc_genl_dev_down,
1381 .policy = nfc_genl_policy,
1382 },
1383 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001384 .cmd = NFC_CMD_START_POLL,
1385 .doit = nfc_genl_start_poll,
1386 .policy = nfc_genl_policy,
1387 },
1388 {
1389 .cmd = NFC_CMD_STOP_POLL,
1390 .doit = nfc_genl_stop_poll,
1391 .policy = nfc_genl_policy,
1392 },
1393 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001394 .cmd = NFC_CMD_DEP_LINK_UP,
1395 .doit = nfc_genl_dep_link_up,
1396 .policy = nfc_genl_policy,
1397 },
1398 {
1399 .cmd = NFC_CMD_DEP_LINK_DOWN,
1400 .doit = nfc_genl_dep_link_down,
1401 .policy = nfc_genl_policy,
1402 },
1403 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001404 .cmd = NFC_CMD_GET_TARGET,
1405 .dumpit = nfc_genl_dump_targets,
1406 .done = nfc_genl_dump_targets_done,
1407 .policy = nfc_genl_policy,
1408 },
Thierry Escande52feb442012-10-17 14:43:39 +02001409 {
1410 .cmd = NFC_CMD_LLC_GET_PARAMS,
1411 .doit = nfc_genl_llc_get_params,
1412 .policy = nfc_genl_policy,
1413 },
1414 {
1415 .cmd = NFC_CMD_LLC_SET_PARAMS,
1416 .doit = nfc_genl_llc_set_params,
1417 .policy = nfc_genl_policy,
1418 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001419 {
1420 .cmd = NFC_CMD_LLC_SDREQ,
1421 .doit = nfc_genl_llc_sdreq,
1422 .policy = nfc_genl_policy,
1423 },
Eric Lapuyade9674da82013-04-29 17:13:27 +02001424 {
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001425 .cmd = NFC_CMD_FW_DOWNLOAD,
1426 .doit = nfc_genl_fw_download,
Eric Lapuyade9674da82013-04-29 17:13:27 +02001427 .policy = nfc_genl_policy,
1428 },
Samuel Ortizbe085652013-05-10 17:07:32 +02001429 {
1430 .cmd = NFC_CMD_ENABLE_SE,
1431 .doit = nfc_genl_enable_se,
1432 .policy = nfc_genl_policy,
1433 },
1434 {
1435 .cmd = NFC_CMD_DISABLE_SE,
1436 .doit = nfc_genl_disable_se,
1437 .policy = nfc_genl_policy,
1438 },
Samuel Ortizac22ac42013-07-24 18:10:50 +02001439 {
1440 .cmd = NFC_CMD_GET_SE,
1441 .dumpit = nfc_genl_dump_ses,
1442 .done = nfc_genl_dump_ses_done,
1443 .policy = nfc_genl_policy,
1444 },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001445 {
1446 .cmd = NFC_CMD_SE_IO,
1447 .doit = nfc_genl_se_io,
1448 .policy = nfc_genl_policy,
1449 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001450};
1451
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001452
1453struct urelease_work {
1454 struct work_struct w;
John W. Linvillec4876062012-09-28 11:11:16 -04001455 int portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001456};
1457
1458static void nfc_urelease_event_work(struct work_struct *work)
1459{
1460 struct urelease_work *w = container_of(work, struct urelease_work, w);
1461 struct class_dev_iter iter;
1462 struct nfc_dev *dev;
1463
John W. Linvillec4876062012-09-28 11:11:16 -04001464 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001465
1466 mutex_lock(&nfc_devlist_mutex);
1467
1468 nfc_device_iter_init(&iter);
1469 dev = nfc_device_iter_next(&iter);
1470
1471 while (dev) {
1472 mutex_lock(&dev->genl_data.genl_data_mutex);
1473
John W. Linvillec4876062012-09-28 11:11:16 -04001474 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001475 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001476 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001477 }
1478
1479 mutex_unlock(&dev->genl_data.genl_data_mutex);
1480
1481 dev = nfc_device_iter_next(&iter);
1482 }
1483
1484 nfc_device_iter_exit(&iter);
1485
1486 mutex_unlock(&nfc_devlist_mutex);
1487
1488 kfree(w);
1489}
1490
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001491static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001492 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001493{
1494 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001495 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001496
1497 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1498 goto out;
1499
Eric W. Biederman15e47302012-09-07 20:12:54 +00001500 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001501
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001502 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1503 if (w) {
1504 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001505 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001506 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001507 }
1508
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001509out:
1510 return NOTIFY_DONE;
1511}
1512
1513void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1514{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001515 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001516 mutex_init(&genl_data->genl_data_mutex);
1517}
1518
1519void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1520{
1521 mutex_destroy(&genl_data->genl_data_mutex);
1522}
1523
1524static struct notifier_block nl_notifier = {
1525 .notifier_call = nfc_genl_rcv_nl_event,
1526};
1527
1528/**
1529 * nfc_genl_init() - Initialize netlink interface
1530 *
1531 * This initialization function registers the nfc netlink family.
1532 */
1533int __init nfc_genl_init(void)
1534{
1535 int rc;
1536
Johannes Berg2a94fe42013-11-19 15:19:39 +01001537 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1538 nfc_genl_ops,
1539 nfc_genl_mcgrps);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001540 if (rc)
1541 return rc;
1542
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001543 netlink_register_notifier(&nl_notifier);
1544
Johannes Berg2a94fe42013-11-19 15:19:39 +01001545 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001546}
1547
1548/**
1549 * nfc_genl_exit() - Deinitialize netlink interface
1550 *
1551 * This exit function unregisters the nfc netlink family.
1552 */
1553void nfc_genl_exit(void)
1554{
1555 netlink_unregister_notifier(&nl_notifier);
1556 genl_unregister_family(&nfc_genl_family);
1557}