blob: aca903c126713b36b51f33fed3ce5960727ba130 [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 *
Samuel Ortiz9e580952014-10-14 02:19:46 +02008 * 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 *
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030014 * 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
Jeff Kirsher98b32de2013-12-06 08:56:16 -080025 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030026 */
27
Samuel Ortiz52858b52011-12-14 16:43:05 +010028#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perches20c239c2011-11-29 11:37:33 -080029
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030030#include <net/genetlink.h>
31#include <linux/nfc.h>
32#include <linux/slab.h>
33
34#include "nfc.h"
Samuel Ortiz30cc4582013-04-26 11:49:40 +020035#include "llcp.h"
Thierry Escande52feb442012-10-17 14:43:39 +020036
Johannes Berg2a94fe42013-11-19 15:19:39 +010037static const struct genl_multicast_group nfc_genl_mcgrps[] = {
38 { .name = NFC_GENL_MCAST_EVENT_NAME, },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030039};
40
Johannes Berg489111e2016-10-24 14:40:03 +020041static struct genl_family nfc_genl_family;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030042static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
43 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
44 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
45 .len = NFC_DEVICE_NAME_MAXSIZE },
46 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010047 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
48 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010049 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020050 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
51 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Thierry Escande8af362d2013-02-15 10:42:52 +010052 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
53 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
54 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010055 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
Eric Lapuyade9674da82013-04-29 17:13:27 +020056 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
57 .len = NFC_FIRMWARE_NAME_MAXSIZE },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +020058 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
Christophe Ricard29e76922015-08-19 21:26:43 +020059 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
60
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010061};
62
63static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
64 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
65 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030066};
67
68static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010069 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030070{
71 void *hdr;
72
Eric W. Biederman15e47302012-09-07 20:12:54 +000073 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010074 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030075 if (!hdr)
76 return -EMSGSIZE;
77
78 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
79
David S. Miller1e6428d2012-03-29 23:23:57 -040080 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
81 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
82 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
83 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
84 goto nla_put_failure;
85 if (target->nfcid1_len > 0 &&
86 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
87 target->nfcid1))
88 goto nla_put_failure;
89 if (target->sensb_res_len > 0 &&
90 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
91 target->sensb_res))
92 goto nla_put_failure;
93 if (target->sensf_res_len > 0 &&
94 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
95 target->sensf_res))
96 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030097
Mark A. Greerf5f68722014-01-14 17:52:11 -070098 if (target->is_iso15693) {
99 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
100 target->iso15693_dsfid) ||
101 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
102 sizeof(target->iso15693_uid), target->iso15693_uid))
103 goto nla_put_failure;
104 }
105
Johannes Berg053c0952015-01-16 22:09:00 +0100106 genlmsg_end(msg, hdr);
107 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300108
109nla_put_failure:
110 genlmsg_cancel(msg, hdr);
111 return -EMSGSIZE;
112}
113
114static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
115{
Johannes Bergc90c39d2016-10-24 14:40:01 +0200116 struct nlattr **attrbuf = genl_family_attrbuf(&nfc_genl_family);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300117 struct nfc_dev *dev;
118 int rc;
119 u32 idx;
120
121 rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
Johannes Bergfceb6432017-04-12 14:34:07 +0200122 attrbuf, nfc_genl_family.maxattr, nfc_genl_policy,
123 NULL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300124 if (rc < 0)
125 return ERR_PTR(rc);
126
Johannes Bergc90c39d2016-10-24 14:40:01 +0200127 if (!attrbuf[NFC_ATTR_DEVICE_INDEX])
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300128 return ERR_PTR(-EINVAL);
129
Johannes Bergc90c39d2016-10-24 14:40:01 +0200130 idx = nla_get_u32(attrbuf[NFC_ATTR_DEVICE_INDEX]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300131
132 dev = nfc_get_device(idx);
133 if (!dev)
134 return ERR_PTR(-ENODEV);
135
136 return dev;
137}
138
139static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100140 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300141{
142 int i = cb->args[0];
143 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
144 int rc;
145
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300146 if (!dev) {
147 dev = __get_device_from_cb(cb);
148 if (IS_ERR(dev))
149 return PTR_ERR(dev);
150
151 cb->args[1] = (long) dev;
152 }
153
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200154 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300155
156 cb->seq = dev->targets_generation;
157
158 while (i < dev->n_targets) {
159 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100160 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300161 if (rc < 0)
162 break;
163
164 i++;
165 }
166
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200167 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300168
169 cb->args[0] = i;
170
171 return skb->len;
172}
173
174static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
175{
176 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
177
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300178 if (dev)
179 nfc_put_device(dev);
180
181 return 0;
182}
183
184int nfc_genl_targets_found(struct nfc_dev *dev)
185{
186 struct sk_buff *msg;
187 void *hdr;
188
Eric W. Biederman15e47302012-09-07 20:12:54 +0000189 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300190
Thomas Graf58050fc2012-06-28 03:57:45 +0000191 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300192 if (!msg)
193 return -ENOMEM;
194
195 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100196 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300197 if (!hdr)
198 goto free_msg;
199
David S. Miller1e6428d2012-03-29 23:23:57 -0400200 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
201 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300202
203 genlmsg_end(msg, hdr);
204
Johannes Berg2a94fe42013-11-19 15:19:39 +0100205 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300206
207nla_put_failure:
208 genlmsg_cancel(msg, hdr);
209free_msg:
210 nlmsg_free(msg);
211 return -EMSGSIZE;
212}
213
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200214int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
215{
216 struct sk_buff *msg;
217 void *hdr;
218
Thomas Graf58050fc2012-06-28 03:57:45 +0000219 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200220 if (!msg)
221 return -ENOMEM;
222
223 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
224 NFC_EVENT_TARGET_LOST);
225 if (!hdr)
226 goto free_msg;
227
John W. Linville59ef43e2012-04-18 14:17:13 -0400228 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
229 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
230 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200231
232 genlmsg_end(msg, hdr);
233
Johannes Berg2a94fe42013-11-19 15:19:39 +0100234 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200235
236 return 0;
237
238nla_put_failure:
239 genlmsg_cancel(msg, hdr);
240free_msg:
241 nlmsg_free(msg);
242 return -EMSGSIZE;
243}
244
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200245int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
246{
247 struct sk_buff *msg;
248 void *hdr;
249
Thomas Graf58050fc2012-06-28 03:57:45 +0000250 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200251 if (!msg)
252 return -ENOMEM;
253
254 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
255 NFC_EVENT_TM_ACTIVATED);
256 if (!hdr)
257 goto free_msg;
258
259 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
260 goto nla_put_failure;
261 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
262 goto nla_put_failure;
263
264 genlmsg_end(msg, hdr);
265
Johannes Berg2a94fe42013-11-19 15:19:39 +0100266 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200267
268 return 0;
269
270nla_put_failure:
271 genlmsg_cancel(msg, hdr);
272free_msg:
273 nlmsg_free(msg);
274 return -EMSGSIZE;
275}
276
277int nfc_genl_tm_deactivated(struct nfc_dev *dev)
278{
279 struct sk_buff *msg;
280 void *hdr;
281
Thomas Graf58050fc2012-06-28 03:57:45 +0000282 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200283 if (!msg)
284 return -ENOMEM;
285
286 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
287 NFC_EVENT_TM_DEACTIVATED);
288 if (!hdr)
289 goto free_msg;
290
291 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
292 goto nla_put_failure;
293
294 genlmsg_end(msg, hdr);
295
Johannes Berg2a94fe42013-11-19 15:19:39 +0100296 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200297
298 return 0;
299
300nla_put_failure:
301 genlmsg_cancel(msg, hdr);
302free_msg:
303 nlmsg_free(msg);
304 return -EMSGSIZE;
305}
306
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300307int nfc_genl_device_added(struct nfc_dev *dev)
308{
309 struct sk_buff *msg;
310 void *hdr;
311
Thomas Graf58050fc2012-06-28 03:57:45 +0000312 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300313 if (!msg)
314 return -ENOMEM;
315
316 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100317 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300318 if (!hdr)
319 goto free_msg;
320
David S. Miller1e6428d2012-03-29 23:23:57 -0400321 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
322 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
323 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
324 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
325 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300326
327 genlmsg_end(msg, hdr);
328
Johannes Berg2a94fe42013-11-19 15:19:39 +0100329 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300330
331 return 0;
332
333nla_put_failure:
334 genlmsg_cancel(msg, hdr);
335free_msg:
336 nlmsg_free(msg);
337 return -EMSGSIZE;
338}
339
340int nfc_genl_device_removed(struct nfc_dev *dev)
341{
342 struct sk_buff *msg;
343 void *hdr;
344
Thomas Graf58050fc2012-06-28 03:57:45 +0000345 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300346 if (!msg)
347 return -ENOMEM;
348
349 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100350 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300351 if (!hdr)
352 goto free_msg;
353
David S. Miller1e6428d2012-03-29 23:23:57 -0400354 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
355 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300356
357 genlmsg_end(msg, hdr);
358
Johannes Berg2a94fe42013-11-19 15:19:39 +0100359 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300360
361 return 0;
362
363nla_put_failure:
364 genlmsg_cancel(msg, hdr);
365free_msg:
366 nlmsg_free(msg);
367 return -EMSGSIZE;
368}
369
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100370int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
371{
372 struct sk_buff *msg;
373 struct nlattr *sdp_attr, *uri_attr;
374 struct nfc_llcp_sdp_tlv *sdres;
375 struct hlist_node *n;
376 void *hdr;
377 int rc = -EMSGSIZE;
378 int i;
379
380 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
381 if (!msg)
382 return -ENOMEM;
383
384 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
385 NFC_EVENT_LLC_SDRES);
386 if (!hdr)
387 goto free_msg;
388
389 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
390 goto nla_put_failure;
391
392 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
393 if (sdp_attr == NULL) {
394 rc = -ENOMEM;
395 goto nla_put_failure;
396 }
397
398 i = 1;
399 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
400 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
401
402 uri_attr = nla_nest_start(msg, i++);
403 if (uri_attr == NULL) {
404 rc = -ENOMEM;
405 goto nla_put_failure;
406 }
407
408 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
409 goto nla_put_failure;
410
411 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
412 goto nla_put_failure;
413
414 nla_nest_end(msg, uri_attr);
415
416 hlist_del(&sdres->node);
417
418 nfc_llcp_free_sdp_tlv(sdres);
419 }
420
421 nla_nest_end(msg, sdp_attr);
422
423 genlmsg_end(msg, hdr);
424
Johannes Berg2a94fe42013-11-19 15:19:39 +0100425 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100426
427nla_put_failure:
428 genlmsg_cancel(msg, hdr);
429
430free_msg:
431 nlmsg_free(msg);
432
433 nfc_llcp_free_sdp_tlv_list(sdres_list);
434
435 return rc;
436}
437
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200438int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
439{
440 struct sk_buff *msg;
441 void *hdr;
442
443 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
444 if (!msg)
445 return -ENOMEM;
446
447 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
448 NFC_EVENT_SE_ADDED);
449 if (!hdr)
450 goto free_msg;
451
452 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
453 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
454 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
455 goto nla_put_failure;
456
457 genlmsg_end(msg, hdr);
458
Johannes Berg2a94fe42013-11-19 15:19:39 +0100459 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200460
461 return 0;
462
463nla_put_failure:
464 genlmsg_cancel(msg, hdr);
465free_msg:
466 nlmsg_free(msg);
467 return -EMSGSIZE;
468}
469
470int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
471{
472 struct sk_buff *msg;
473 void *hdr;
474
475 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
476 if (!msg)
477 return -ENOMEM;
478
479 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
480 NFC_EVENT_SE_REMOVED);
481 if (!hdr)
482 goto free_msg;
483
484 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
485 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
486 goto nla_put_failure;
487
488 genlmsg_end(msg, hdr);
489
Johannes Berg2a94fe42013-11-19 15:19:39 +0100490 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200491
492 return 0;
493
494nla_put_failure:
495 genlmsg_cancel(msg, hdr);
496free_msg:
497 nlmsg_free(msg);
498 return -EMSGSIZE;
499}
500
Christophe Ricard447b27c2015-02-01 22:26:16 +0100501int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
502 struct nfc_evt_transaction *evt_transaction)
503{
504 struct nfc_se *se;
505 struct sk_buff *msg;
506 void *hdr;
507
508 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
509 if (!msg)
510 return -ENOMEM;
511
512 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
513 NFC_EVENT_SE_TRANSACTION);
514 if (!hdr)
515 goto free_msg;
516
517 se = nfc_find_se(dev, se_idx);
518 if (!se)
519 goto free_msg;
520
521 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
522 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
523 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
524 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
525 evt_transaction->aid) ||
526 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
527 evt_transaction->params))
528 goto nla_put_failure;
529
530 /* evt_transaction is no more used */
531 devm_kfree(&dev->dev, evt_transaction);
532
533 genlmsg_end(msg, hdr);
534
535 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
536
537 return 0;
538
539nla_put_failure:
540 genlmsg_cancel(msg, hdr);
541free_msg:
542 /* evt_transaction is no more used */
543 devm_kfree(&dev->dev, evt_transaction);
544 nlmsg_free(msg);
545 return -EMSGSIZE;
546}
547
Christophe Ricard9afec6d2015-12-23 23:45:18 +0100548int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
549{
550 struct nfc_se *se;
551 struct sk_buff *msg;
552 void *hdr;
553
554 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
555 if (!msg)
556 return -ENOMEM;
557
558 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
559 NFC_EVENT_SE_CONNECTIVITY);
560 if (!hdr)
561 goto free_msg;
562
563 se = nfc_find_se(dev, se_idx);
564 if (!se)
565 goto free_msg;
566
567 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
568 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
569 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
570 goto nla_put_failure;
571
572 genlmsg_end(msg, hdr);
573
574 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
575
576 return 0;
577
578nla_put_failure:
579 genlmsg_cancel(msg, hdr);
580free_msg:
581 nlmsg_free(msg);
582 return -EMSGSIZE;
583}
584
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300585static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000586 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100587 struct netlink_callback *cb,
588 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300589{
590 void *hdr;
591
Eric W. Biederman15e47302012-09-07 20:12:54 +0000592 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100593 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300594 if (!hdr)
595 return -EMSGSIZE;
596
597 if (cb)
598 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
599
David S. Miller1e6428d2012-03-29 23:23:57 -0400600 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
601 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
602 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
Thierry Escande7ad39392012-10-05 11:19:58 +0200603 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
604 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
David S. Miller1e6428d2012-03-29 23:23:57 -0400605 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300606
Johannes Berg053c0952015-01-16 22:09:00 +0100607 genlmsg_end(msg, hdr);
608 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300609
610nla_put_failure:
611 genlmsg_cancel(msg, hdr);
612 return -EMSGSIZE;
613}
614
615static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100616 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300617{
618 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
619 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
620 bool first_call = false;
621
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300622 if (!iter) {
623 first_call = true;
624 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
625 if (!iter)
626 return -ENOMEM;
627 cb->args[0] = (long) iter;
628 }
629
630 mutex_lock(&nfc_devlist_mutex);
631
632 cb->seq = nfc_devlist_generation;
633
634 if (first_call) {
635 nfc_device_iter_init(iter);
636 dev = nfc_device_iter_next(iter);
637 }
638
639 while (dev) {
640 int rc;
641
Eric W. Biederman15e47302012-09-07 20:12:54 +0000642 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100643 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300644 if (rc < 0)
645 break;
646
647 dev = nfc_device_iter_next(iter);
648 }
649
650 mutex_unlock(&nfc_devlist_mutex);
651
652 cb->args[1] = (long) dev;
653
654 return skb->len;
655}
656
657static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
658{
659 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
660
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300661 nfc_device_iter_exit(iter);
662 kfree(iter);
663
664 return 0;
665}
666
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100667int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100668 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100669{
670 struct sk_buff *msg;
671 void *hdr;
672
673 pr_debug("DEP link is up\n");
674
Thomas Graf58050fc2012-06-28 03:57:45 +0000675 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100676 if (!msg)
677 return -ENOMEM;
678
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100679 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100680 if (!hdr)
681 goto free_msg;
682
David S. Miller1e6428d2012-03-29 23:23:57 -0400683 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
684 goto nla_put_failure;
685 if (rf_mode == NFC_RF_INITIATOR &&
686 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
687 goto nla_put_failure;
688 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
689 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
690 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100691
692 genlmsg_end(msg, hdr);
693
694 dev->dep_link_up = true;
695
Johannes Berg2a94fe42013-11-19 15:19:39 +0100696 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100697
698 return 0;
699
700nla_put_failure:
701 genlmsg_cancel(msg, hdr);
702free_msg:
703 nlmsg_free(msg);
704 return -EMSGSIZE;
705}
706
707int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
708{
709 struct sk_buff *msg;
710 void *hdr;
711
712 pr_debug("DEP link is down\n");
713
Thomas Graf58050fc2012-06-28 03:57:45 +0000714 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100715 if (!msg)
716 return -ENOMEM;
717
718 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100719 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100720 if (!hdr)
721 goto free_msg;
722
David S. Miller1e6428d2012-03-29 23:23:57 -0400723 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
724 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100725
726 genlmsg_end(msg, hdr);
727
Johannes Berg2a94fe42013-11-19 15:19:39 +0100728 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100729
730 return 0;
731
732nla_put_failure:
733 genlmsg_cancel(msg, hdr);
734free_msg:
735 nlmsg_free(msg);
736 return -EMSGSIZE;
737}
738
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300739static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
740{
741 struct sk_buff *msg;
742 struct nfc_dev *dev;
743 u32 idx;
744 int rc = -ENOBUFS;
745
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300746 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
747 return -EINVAL;
748
749 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
750
751 dev = nfc_get_device(idx);
752 if (!dev)
753 return -ENODEV;
754
Thomas Graf58050fc2012-06-28 03:57:45 +0000755 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300756 if (!msg) {
757 rc = -ENOMEM;
758 goto out_putdev;
759 }
760
Eric W. Biederman15e47302012-09-07 20:12:54 +0000761 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100762 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300763 if (rc < 0)
764 goto out_free;
765
766 nfc_put_device(dev);
767
768 return genlmsg_reply(msg, info);
769
770out_free:
771 nlmsg_free(msg);
772out_putdev:
773 nfc_put_device(dev);
774 return rc;
775}
776
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300777static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
778{
779 struct nfc_dev *dev;
780 int rc;
781 u32 idx;
782
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300783 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
784 return -EINVAL;
785
786 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
787
788 dev = nfc_get_device(idx);
789 if (!dev)
790 return -ENODEV;
791
792 rc = nfc_dev_up(dev);
793
794 nfc_put_device(dev);
795 return rc;
796}
797
798static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
799{
800 struct nfc_dev *dev;
801 int rc;
802 u32 idx;
803
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300804 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
805 return -EINVAL;
806
807 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
808
809 dev = nfc_get_device(idx);
810 if (!dev)
811 return -ENODEV;
812
813 rc = nfc_dev_down(dev);
814
815 nfc_put_device(dev);
816 return rc;
817}
818
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300819static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
820{
821 struct nfc_dev *dev;
822 int rc;
823 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200824 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300825
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100826 pr_debug("Poll start\n");
827
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300828 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200829 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
830 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200831 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300832 return -EINVAL;
833
834 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200835
836 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
837 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200838
839 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
840 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200841 else if (info->attrs[NFC_ATTR_PROTOCOLS])
842 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300843
844 dev = nfc_get_device(idx);
845 if (!dev)
846 return -ENODEV;
847
848 mutex_lock(&dev->genl_data.genl_data_mutex);
849
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200850 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300851 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000852 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300853
854 mutex_unlock(&dev->genl_data.genl_data_mutex);
855
856 nfc_put_device(dev);
857 return rc;
858}
859
860static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
861{
862 struct nfc_dev *dev;
863 int rc;
864 u32 idx;
865
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300866 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
867 return -EINVAL;
868
869 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
870
871 dev = nfc_get_device(idx);
872 if (!dev)
873 return -ENODEV;
874
Samuel Ortiza831b912012-06-28 16:41:57 +0200875 device_lock(&dev->dev);
876
877 if (!dev->polling) {
878 device_unlock(&dev->dev);
879 return -EINVAL;
880 }
881
882 device_unlock(&dev->dev);
883
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300884 mutex_lock(&dev->genl_data.genl_data_mutex);
885
Eric W. Biederman15e47302012-09-07 20:12:54 +0000886 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300887 rc = -EBUSY;
888 goto out;
889 }
890
891 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000892 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300893
894out:
895 mutex_unlock(&dev->genl_data.genl_data_mutex);
896 nfc_put_device(dev);
897 return rc;
898}
899
Christophe Ricard3682f492014-12-02 21:27:50 +0100900static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
901{
902 struct nfc_dev *dev;
903 u32 device_idx, target_idx, protocol;
904 int rc;
905
906 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
907 return -EINVAL;
908
909 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
910
911 dev = nfc_get_device(device_idx);
912 if (!dev)
913 return -ENODEV;
914
915 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
916 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
917
Christophe Ricard96d45812015-10-25 22:54:43 +0100918 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
Christophe Ricard3682f492014-12-02 21:27:50 +0100919 rc = nfc_activate_target(dev, target_idx, protocol);
920
921 nfc_put_device(dev);
922 return 0;
923}
924
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100925static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
926{
927 struct nfc_dev *dev;
928 int rc, tgt_idx;
929 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100930 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100931
932 pr_debug("DEP link up\n");
933
934 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100935 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100936 return -EINVAL;
937
938 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
939 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
940 tgt_idx = NFC_TARGET_IDX_ANY;
941 else
942 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
943
944 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100945
946 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
947 return -EINVAL;
948
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100949 dev = nfc_get_device(idx);
950 if (!dev)
951 return -ENODEV;
952
Samuel Ortiz47807d32012-03-05 01:03:50 +0100953 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100954
955 nfc_put_device(dev);
956
957 return rc;
958}
959
960static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
961{
962 struct nfc_dev *dev;
963 int rc;
964 u32 idx;
965
966 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
967 return -EINVAL;
968
969 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
970
971 dev = nfc_get_device(idx);
972 if (!dev)
973 return -ENODEV;
974
975 rc = nfc_dep_link_down(dev);
976
977 nfc_put_device(dev);
978 return rc;
979}
980
Thierry Escande52feb442012-10-17 14:43:39 +0200981static int nfc_genl_send_params(struct sk_buff *msg,
982 struct nfc_llcp_local *local,
983 u32 portid, u32 seq)
984{
985 void *hdr;
986
987 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
988 NFC_CMD_LLC_GET_PARAMS);
989 if (!hdr)
990 return -EMSGSIZE;
991
992 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
993 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
994 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
995 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
996 goto nla_put_failure;
997
Johannes Berg053c0952015-01-16 22:09:00 +0100998 genlmsg_end(msg, hdr);
999 return 0;
Thierry Escande52feb442012-10-17 14:43:39 +02001000
1001nla_put_failure:
1002
1003 genlmsg_cancel(msg, hdr);
1004 return -EMSGSIZE;
1005}
1006
1007static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1008{
1009 struct nfc_dev *dev;
1010 struct nfc_llcp_local *local;
1011 int rc = 0;
1012 struct sk_buff *msg = NULL;
1013 u32 idx;
1014
1015 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
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 return -ENODEV;
1023
1024 device_lock(&dev->dev);
1025
1026 local = nfc_llcp_find_local(dev);
1027 if (!local) {
1028 rc = -ENODEV;
1029 goto exit;
1030 }
1031
1032 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1033 if (!msg) {
1034 rc = -ENOMEM;
1035 goto exit;
1036 }
1037
1038 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1039
1040exit:
1041 device_unlock(&dev->dev);
1042
1043 nfc_put_device(dev);
1044
1045 if (rc < 0) {
1046 if (msg)
1047 nlmsg_free(msg);
1048
1049 return rc;
1050 }
1051
1052 return genlmsg_reply(msg, info);
1053}
1054
1055static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1056{
1057 struct nfc_dev *dev;
1058 struct nfc_llcp_local *local;
1059 u8 rw = 0;
1060 u16 miux = 0;
1061 u32 idx;
1062 int rc = 0;
1063
1064 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1065 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1066 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1067 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1068 return -EINVAL;
1069
1070 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1071 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1072
1073 if (rw > LLCP_MAX_RW)
1074 return -EINVAL;
1075 }
1076
1077 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1078 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1079
1080 if (miux > LLCP_MAX_MIUX)
1081 return -EINVAL;
1082 }
1083
1084 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1085
1086 dev = nfc_get_device(idx);
1087 if (!dev)
1088 return -ENODEV;
1089
1090 device_lock(&dev->dev);
1091
1092 local = nfc_llcp_find_local(dev);
1093 if (!local) {
1094 nfc_put_device(dev);
1095 rc = -ENODEV;
1096 goto exit;
1097 }
1098
1099 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1100 if (dev->dep_link_up) {
1101 rc = -EINPROGRESS;
1102 goto exit;
1103 }
1104
1105 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1106 }
1107
1108 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1109 local->rw = rw;
1110
1111 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1112 local->miux = cpu_to_be16(miux);
1113
1114exit:
1115 device_unlock(&dev->dev);
1116
1117 nfc_put_device(dev);
1118
1119 return rc;
1120}
1121
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001122static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1123{
1124 struct nfc_dev *dev;
1125 struct nfc_llcp_local *local;
1126 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1127 u32 idx;
1128 u8 tid;
1129 char *uri;
1130 int rc = 0, rem;
1131 size_t uri_len, tlvs_len;
1132 struct hlist_head sdreq_list;
1133 struct nfc_llcp_sdp_tlv *sdreq;
1134
1135 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1136 !info->attrs[NFC_ATTR_LLC_SDP])
1137 return -EINVAL;
1138
1139 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1140
1141 dev = nfc_get_device(idx);
Julia Lawall9abebb82015-10-17 11:32:19 +02001142 if (!dev)
1143 return -ENODEV;
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001144
1145 device_lock(&dev->dev);
1146
1147 if (dev->dep_link_up == false) {
1148 rc = -ENOLINK;
1149 goto exit;
1150 }
1151
1152 local = nfc_llcp_find_local(dev);
1153 if (!local) {
1154 nfc_put_device(dev);
1155 rc = -ENODEV;
1156 goto exit;
1157 }
1158
1159 INIT_HLIST_HEAD(&sdreq_list);
1160
1161 tlvs_len = 0;
1162
1163 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1164 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02001165 nfc_sdp_genl_policy, NULL);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001166
1167 if (rc != 0) {
1168 rc = -EINVAL;
1169 goto exit;
1170 }
1171
1172 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1173 continue;
1174
1175 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1176 if (uri_len == 0)
1177 continue;
1178
1179 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1180 if (uri == NULL || *uri == 0)
1181 continue;
1182
1183 tid = local->sdreq_next_tid++;
1184
1185 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1186 if (sdreq == NULL) {
1187 rc = -ENOMEM;
1188 goto exit;
1189 }
1190
1191 tlvs_len += sdreq->tlv_len;
1192
1193 hlist_add_head(&sdreq->node, &sdreq_list);
1194 }
1195
1196 if (hlist_empty(&sdreq_list)) {
1197 rc = -EINVAL;
1198 goto exit;
1199 }
1200
1201 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1202exit:
1203 device_unlock(&dev->dev);
1204
1205 nfc_put_device(dev);
1206
1207 return rc;
1208}
1209
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001210static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001211{
1212 struct nfc_dev *dev;
1213 int rc;
1214 u32 idx;
1215 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1216
1217 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1218 return -EINVAL;
1219
1220 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1221
1222 dev = nfc_get_device(idx);
1223 if (!dev)
1224 return -ENODEV;
1225
1226 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1227 sizeof(firmware_name));
1228
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001229 rc = nfc_fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001230
1231 nfc_put_device(dev);
1232 return rc;
1233}
1234
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001235int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1236 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001237{
1238 struct sk_buff *msg;
1239 void *hdr;
1240
1241 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1242 if (!msg)
1243 return -ENOMEM;
1244
1245 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001246 NFC_CMD_FW_DOWNLOAD);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001247 if (!hdr)
1248 goto free_msg;
1249
1250 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001251 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
Eric Lapuyade9674da82013-04-29 17:13:27 +02001252 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1253 goto nla_put_failure;
1254
1255 genlmsg_end(msg, hdr);
1256
Johannes Berg2a94fe42013-11-19 15:19:39 +01001257 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001258
1259 return 0;
1260
1261nla_put_failure:
1262 genlmsg_cancel(msg, hdr);
1263free_msg:
1264 nlmsg_free(msg);
1265 return -EMSGSIZE;
1266}
1267
Samuel Ortizbe085652013-05-10 17:07:32 +02001268static int nfc_genl_enable_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_enable_se(dev, se_idx);
1286
1287 nfc_put_device(dev);
1288 return rc;
1289}
1290
1291static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1292{
1293 struct nfc_dev *dev;
1294 int rc;
1295 u32 idx, se_idx;
1296
1297 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1298 !info->attrs[NFC_ATTR_SE_INDEX])
1299 return -EINVAL;
1300
1301 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1302 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1303
1304 dev = nfc_get_device(idx);
1305 if (!dev)
1306 return -ENODEV;
1307
1308 rc = nfc_disable_se(dev, se_idx);
1309
1310 nfc_put_device(dev);
1311 return rc;
1312}
1313
Samuel Ortizac22ac42013-07-24 18:10:50 +02001314static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1315 u32 portid, u32 seq,
1316 struct netlink_callback *cb,
1317 int flags)
1318{
1319 void *hdr;
1320 struct nfc_se *se, *n;
1321
1322 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1323 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1324 NFC_CMD_GET_SE);
1325 if (!hdr)
1326 goto nla_put_failure;
1327
1328 if (cb)
1329 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1330
1331 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1332 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1333 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1334 goto nla_put_failure;
1335
Johannes Berg053c0952015-01-16 22:09:00 +01001336 genlmsg_end(msg, hdr);
Samuel Ortizac22ac42013-07-24 18:10:50 +02001337 }
1338
1339 return 0;
1340
1341nla_put_failure:
1342 genlmsg_cancel(msg, hdr);
1343 return -EMSGSIZE;
1344}
1345
1346static int nfc_genl_dump_ses(struct sk_buff *skb,
1347 struct netlink_callback *cb)
1348{
1349 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1350 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1351 bool first_call = false;
1352
1353 if (!iter) {
1354 first_call = true;
1355 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1356 if (!iter)
1357 return -ENOMEM;
1358 cb->args[0] = (long) iter;
1359 }
1360
1361 mutex_lock(&nfc_devlist_mutex);
1362
1363 cb->seq = nfc_devlist_generation;
1364
1365 if (first_call) {
1366 nfc_device_iter_init(iter);
1367 dev = nfc_device_iter_next(iter);
1368 }
1369
1370 while (dev) {
1371 int rc;
1372
1373 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1374 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1375 if (rc < 0)
1376 break;
1377
1378 dev = nfc_device_iter_next(iter);
1379 }
1380
1381 mutex_unlock(&nfc_devlist_mutex);
1382
1383 cb->args[1] = (long) dev;
1384
1385 return skb->len;
1386}
1387
1388static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1389{
1390 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1391
1392 nfc_device_iter_exit(iter);
1393 kfree(iter);
1394
1395 return 0;
1396}
1397
Christophe Ricardcd96db62014-12-02 21:27:51 +01001398static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1399 u8 *apdu, size_t apdu_length,
1400 se_io_cb_t cb, void *cb_context)
1401{
1402 struct nfc_se *se;
1403 int rc;
1404
1405 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1406
1407 device_lock(&dev->dev);
1408
1409 if (!device_is_registered(&dev->dev)) {
1410 rc = -ENODEV;
1411 goto error;
1412 }
1413
1414 if (!dev->dev_up) {
1415 rc = -ENODEV;
1416 goto error;
1417 }
1418
1419 if (!dev->ops->se_io) {
1420 rc = -EOPNOTSUPP;
1421 goto error;
1422 }
1423
1424 se = nfc_find_se(dev, se_idx);
1425 if (!se) {
1426 rc = -EINVAL;
1427 goto error;
1428 }
1429
1430 if (se->state != NFC_SE_ENABLED) {
1431 rc = -ENODEV;
1432 goto error;
1433 }
1434
1435 rc = dev->ops->se_io(dev, se_idx, apdu,
1436 apdu_length, cb, cb_context);
1437
1438error:
1439 device_unlock(&dev->dev);
1440 return rc;
1441}
1442
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001443struct se_io_ctx {
1444 u32 dev_idx;
1445 u32 se_idx;
1446};
1447
Samuel Ortizddc1a702013-10-07 14:18:44 +02001448static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001449{
1450 struct se_io_ctx *ctx = context;
1451 struct sk_buff *msg;
1452 void *hdr;
1453
1454 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1455 if (!msg) {
1456 kfree(ctx);
1457 return;
1458 }
1459
1460 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1461 NFC_CMD_SE_IO);
1462 if (!hdr)
1463 goto free_msg;
1464
1465 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1466 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1467 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1468 goto nla_put_failure;
1469
1470 genlmsg_end(msg, hdr);
1471
Johannes Berg2a94fe42013-11-19 15:19:39 +01001472 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001473
1474 kfree(ctx);
1475
1476 return;
1477
1478nla_put_failure:
1479 genlmsg_cancel(msg, hdr);
1480free_msg:
1481 nlmsg_free(msg);
1482 kfree(ctx);
1483
1484 return;
1485}
1486
1487static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1488{
1489 struct nfc_dev *dev;
1490 struct se_io_ctx *ctx;
1491 u32 dev_idx, se_idx;
1492 u8 *apdu;
1493 size_t apdu_len;
1494
1495 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1496 !info->attrs[NFC_ATTR_SE_INDEX] ||
1497 !info->attrs[NFC_ATTR_SE_APDU])
1498 return -EINVAL;
1499
1500 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1501 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1502
1503 dev = nfc_get_device(dev_idx);
1504 if (!dev)
1505 return -ENODEV;
1506
1507 if (!dev->ops || !dev->ops->se_io)
1508 return -ENOTSUPP;
1509
1510 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1511 if (apdu_len == 0)
1512 return -EINVAL;
1513
1514 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1515 if (!apdu)
1516 return -EINVAL;
1517
1518 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1519 if (!ctx)
1520 return -ENOMEM;
1521
1522 ctx->dev_idx = dev_idx;
1523 ctx->se_idx = se_idx;
1524
Christophe Ricardcd96db62014-12-02 21:27:51 +01001525 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001526}
1527
Samuel Ortiz9e580952014-10-14 02:19:46 +02001528static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1529 struct genl_info *info)
1530{
1531 struct nfc_dev *dev;
1532 struct nfc_vendor_cmd *cmd;
1533 u32 dev_idx, vid, subcmd;
1534 u8 *data;
1535 size_t data_len;
Christophe Ricard29e76922015-08-19 21:26:43 +02001536 int i, err;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001537
1538 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1539 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1540 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1541 return -EINVAL;
1542
1543 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1544 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1545 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1546
1547 dev = nfc_get_device(dev_idx);
1548 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1549 return -ENODEV;
1550
Christophe Ricardfe202fe2015-08-14 22:33:40 +02001551 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1552 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
Samuel Ortiz9e580952014-10-14 02:19:46 +02001553 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1554 if (data_len == 0)
1555 return -EINVAL;
1556 } else {
Christophe Ricardadca3c32015-08-17 08:33:43 +02001557 data = NULL;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001558 data_len = 0;
1559 }
1560
1561 for (i = 0; i < dev->n_vendor_cmds; i++) {
1562 cmd = &dev->vendor_cmds[i];
1563
1564 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1565 continue;
1566
Christophe Ricard29e76922015-08-19 21:26:43 +02001567 dev->cur_cmd_info = info;
1568 err = cmd->doit(dev, data, data_len);
1569 dev->cur_cmd_info = NULL;
1570 return err;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001571 }
1572
1573 return -EOPNOTSUPP;
1574}
1575
Christophe Ricard29e76922015-08-19 21:26:43 +02001576/* message building helper */
1577static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1578 int flags, u8 cmd)
1579{
1580 /* since there is no private header just add the generic one */
1581 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1582}
1583
1584static struct sk_buff *
1585__nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1586 u32 portid, u32 seq,
1587 enum nfc_attrs attr,
1588 u32 oui, u32 subcmd, gfp_t gfp)
1589{
1590 struct sk_buff *skb;
1591 void *hdr;
1592
1593 skb = nlmsg_new(approxlen + 100, gfp);
1594 if (!skb)
1595 return NULL;
1596
1597 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1598 if (!hdr) {
1599 kfree_skb(skb);
1600 return NULL;
1601 }
1602
1603 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1604 goto nla_put_failure;
1605 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1606 goto nla_put_failure;
1607 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1608 goto nla_put_failure;
1609
1610 ((void **)skb->cb)[0] = dev;
1611 ((void **)skb->cb)[1] = hdr;
1612
1613 return skb;
1614
1615nla_put_failure:
1616 kfree_skb(skb);
1617 return NULL;
1618}
1619
1620struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1621 enum nfc_attrs attr,
1622 u32 oui, u32 subcmd,
1623 int approxlen)
1624{
1625 if (WARN_ON(!dev->cur_cmd_info))
1626 return NULL;
1627
1628 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1629 dev->cur_cmd_info->snd_portid,
1630 dev->cur_cmd_info->snd_seq, attr,
1631 oui, subcmd, GFP_KERNEL);
1632}
1633EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1634
1635int nfc_vendor_cmd_reply(struct sk_buff *skb)
1636{
1637 struct nfc_dev *dev = ((void **)skb->cb)[0];
1638 void *hdr = ((void **)skb->cb)[1];
1639
1640 /* clear CB data for netlink core to own from now on */
1641 memset(skb->cb, 0, sizeof(skb->cb));
1642
1643 if (WARN_ON(!dev->cur_cmd_info)) {
1644 kfree_skb(skb);
1645 return -EINVAL;
1646 }
1647
1648 genlmsg_end(skb, hdr);
1649 return genlmsg_reply(skb, dev->cur_cmd_info);
1650}
1651EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1652
Johannes Berg4534de82013-11-14 17:14:46 +01001653static const struct genl_ops nfc_genl_ops[] = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001654 {
1655 .cmd = NFC_CMD_GET_DEVICE,
1656 .doit = nfc_genl_get_device,
1657 .dumpit = nfc_genl_dump_devices,
1658 .done = nfc_genl_dump_devices_done,
1659 .policy = nfc_genl_policy,
1660 },
1661 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001662 .cmd = NFC_CMD_DEV_UP,
1663 .doit = nfc_genl_dev_up,
1664 .policy = nfc_genl_policy,
1665 },
1666 {
1667 .cmd = NFC_CMD_DEV_DOWN,
1668 .doit = nfc_genl_dev_down,
1669 .policy = nfc_genl_policy,
1670 },
1671 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001672 .cmd = NFC_CMD_START_POLL,
1673 .doit = nfc_genl_start_poll,
1674 .policy = nfc_genl_policy,
1675 },
1676 {
1677 .cmd = NFC_CMD_STOP_POLL,
1678 .doit = nfc_genl_stop_poll,
1679 .policy = nfc_genl_policy,
1680 },
1681 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001682 .cmd = NFC_CMD_DEP_LINK_UP,
1683 .doit = nfc_genl_dep_link_up,
1684 .policy = nfc_genl_policy,
1685 },
1686 {
1687 .cmd = NFC_CMD_DEP_LINK_DOWN,
1688 .doit = nfc_genl_dep_link_down,
1689 .policy = nfc_genl_policy,
1690 },
1691 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001692 .cmd = NFC_CMD_GET_TARGET,
1693 .dumpit = nfc_genl_dump_targets,
1694 .done = nfc_genl_dump_targets_done,
1695 .policy = nfc_genl_policy,
1696 },
Thierry Escande52feb442012-10-17 14:43:39 +02001697 {
1698 .cmd = NFC_CMD_LLC_GET_PARAMS,
1699 .doit = nfc_genl_llc_get_params,
1700 .policy = nfc_genl_policy,
1701 },
1702 {
1703 .cmd = NFC_CMD_LLC_SET_PARAMS,
1704 .doit = nfc_genl_llc_set_params,
1705 .policy = nfc_genl_policy,
1706 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001707 {
1708 .cmd = NFC_CMD_LLC_SDREQ,
1709 .doit = nfc_genl_llc_sdreq,
1710 .policy = nfc_genl_policy,
1711 },
Eric Lapuyade9674da82013-04-29 17:13:27 +02001712 {
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001713 .cmd = NFC_CMD_FW_DOWNLOAD,
1714 .doit = nfc_genl_fw_download,
Eric Lapuyade9674da82013-04-29 17:13:27 +02001715 .policy = nfc_genl_policy,
1716 },
Samuel Ortizbe085652013-05-10 17:07:32 +02001717 {
1718 .cmd = NFC_CMD_ENABLE_SE,
1719 .doit = nfc_genl_enable_se,
1720 .policy = nfc_genl_policy,
1721 },
1722 {
1723 .cmd = NFC_CMD_DISABLE_SE,
1724 .doit = nfc_genl_disable_se,
1725 .policy = nfc_genl_policy,
1726 },
Samuel Ortizac22ac42013-07-24 18:10:50 +02001727 {
1728 .cmd = NFC_CMD_GET_SE,
1729 .dumpit = nfc_genl_dump_ses,
1730 .done = nfc_genl_dump_ses_done,
1731 .policy = nfc_genl_policy,
1732 },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001733 {
1734 .cmd = NFC_CMD_SE_IO,
1735 .doit = nfc_genl_se_io,
1736 .policy = nfc_genl_policy,
1737 },
Christophe Ricard3682f492014-12-02 21:27:50 +01001738 {
1739 .cmd = NFC_CMD_ACTIVATE_TARGET,
1740 .doit = nfc_genl_activate_target,
1741 .policy = nfc_genl_policy,
1742 },
Samuel Ortiz9e580952014-10-14 02:19:46 +02001743 {
1744 .cmd = NFC_CMD_VENDOR,
1745 .doit = nfc_genl_vendor_cmd,
1746 .policy = nfc_genl_policy,
1747 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001748};
1749
Johannes Berg56989f62016-10-24 14:40:05 +02001750static struct genl_family nfc_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001751 .hdrsize = 0,
1752 .name = NFC_GENL_NAME,
1753 .version = NFC_GENL_VERSION,
1754 .maxattr = NFC_ATTR_MAX,
1755 .module = THIS_MODULE,
1756 .ops = nfc_genl_ops,
1757 .n_ops = ARRAY_SIZE(nfc_genl_ops),
1758 .mcgrps = nfc_genl_mcgrps,
1759 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1760};
1761
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001762
1763struct urelease_work {
1764 struct work_struct w;
Richard Weinberger65bc4f92015-04-13 00:52:36 +02001765 u32 portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001766};
1767
1768static void nfc_urelease_event_work(struct work_struct *work)
1769{
1770 struct urelease_work *w = container_of(work, struct urelease_work, w);
1771 struct class_dev_iter iter;
1772 struct nfc_dev *dev;
1773
John W. Linvillec4876062012-09-28 11:11:16 -04001774 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001775
1776 mutex_lock(&nfc_devlist_mutex);
1777
1778 nfc_device_iter_init(&iter);
1779 dev = nfc_device_iter_next(&iter);
1780
1781 while (dev) {
1782 mutex_lock(&dev->genl_data.genl_data_mutex);
1783
John W. Linvillec4876062012-09-28 11:11:16 -04001784 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001785 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001786 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001787 }
1788
1789 mutex_unlock(&dev->genl_data.genl_data_mutex);
1790
1791 dev = nfc_device_iter_next(&iter);
1792 }
1793
1794 nfc_device_iter_exit(&iter);
1795
1796 mutex_unlock(&nfc_devlist_mutex);
1797
1798 kfree(w);
1799}
1800
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001801static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001802 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001803{
1804 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001805 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001806
1807 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1808 goto out;
1809
Eric W. Biederman15e47302012-09-07 20:12:54 +00001810 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001811
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001812 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1813 if (w) {
1814 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001815 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001816 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001817 }
1818
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001819out:
1820 return NOTIFY_DONE;
1821}
1822
1823void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1824{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001825 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001826 mutex_init(&genl_data->genl_data_mutex);
1827}
1828
1829void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1830{
1831 mutex_destroy(&genl_data->genl_data_mutex);
1832}
1833
1834static struct notifier_block nl_notifier = {
1835 .notifier_call = nfc_genl_rcv_nl_event,
1836};
1837
1838/**
1839 * nfc_genl_init() - Initialize netlink interface
1840 *
1841 * This initialization function registers the nfc netlink family.
1842 */
1843int __init nfc_genl_init(void)
1844{
1845 int rc;
1846
Johannes Berg489111e2016-10-24 14:40:03 +02001847 rc = genl_register_family(&nfc_genl_family);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001848 if (rc)
1849 return rc;
1850
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001851 netlink_register_notifier(&nl_notifier);
1852
Johannes Berg2a94fe42013-11-19 15:19:39 +01001853 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001854}
1855
1856/**
1857 * nfc_genl_exit() - Deinitialize netlink interface
1858 *
1859 * This exit function unregisters the nfc netlink family.
1860 */
1861void nfc_genl_exit(void)
1862{
1863 netlink_unregister_notifier(&nl_notifier);
1864 genl_unregister_family(&nfc_genl_family);
1865}