blob: 6b0850e63e09cf747a498f913f7eabe11e59c380 [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
OGAWA Hirofumi85a25662017-02-04 10:15:55 +0900307static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg)
308{
309 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
310 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
311 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
312 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
313 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
314 return -1;
315 return 0;
316}
317
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300318int nfc_genl_device_added(struct nfc_dev *dev)
319{
320 struct sk_buff *msg;
321 void *hdr;
322
Thomas Graf58050fc2012-06-28 03:57:45 +0000323 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300324 if (!msg)
325 return -ENOMEM;
326
327 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100328 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300329 if (!hdr)
330 goto free_msg;
331
OGAWA Hirofumi85a25662017-02-04 10:15:55 +0900332 if (nfc_genl_setup_device_added(dev, msg))
David S. Miller1e6428d2012-03-29 23:23:57 -0400333 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300334
335 genlmsg_end(msg, hdr);
336
Johannes Berg2a94fe42013-11-19 15:19:39 +0100337 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300338
339 return 0;
340
341nla_put_failure:
342 genlmsg_cancel(msg, hdr);
343free_msg:
344 nlmsg_free(msg);
345 return -EMSGSIZE;
346}
347
348int nfc_genl_device_removed(struct nfc_dev *dev)
349{
350 struct sk_buff *msg;
351 void *hdr;
352
Thomas Graf58050fc2012-06-28 03:57:45 +0000353 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300354 if (!msg)
355 return -ENOMEM;
356
357 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100358 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300359 if (!hdr)
360 goto free_msg;
361
David S. Miller1e6428d2012-03-29 23:23:57 -0400362 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
363 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300364
365 genlmsg_end(msg, hdr);
366
Johannes Berg2a94fe42013-11-19 15:19:39 +0100367 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300368
369 return 0;
370
371nla_put_failure:
372 genlmsg_cancel(msg, hdr);
373free_msg:
374 nlmsg_free(msg);
375 return -EMSGSIZE;
376}
377
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100378int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
379{
380 struct sk_buff *msg;
381 struct nlattr *sdp_attr, *uri_attr;
382 struct nfc_llcp_sdp_tlv *sdres;
383 struct hlist_node *n;
384 void *hdr;
385 int rc = -EMSGSIZE;
386 int i;
387
388 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
389 if (!msg)
390 return -ENOMEM;
391
392 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
393 NFC_EVENT_LLC_SDRES);
394 if (!hdr)
395 goto free_msg;
396
397 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
398 goto nla_put_failure;
399
400 sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
401 if (sdp_attr == NULL) {
402 rc = -ENOMEM;
403 goto nla_put_failure;
404 }
405
406 i = 1;
407 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
408 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
409
410 uri_attr = nla_nest_start(msg, i++);
411 if (uri_attr == NULL) {
412 rc = -ENOMEM;
413 goto nla_put_failure;
414 }
415
416 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
417 goto nla_put_failure;
418
419 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
420 goto nla_put_failure;
421
422 nla_nest_end(msg, uri_attr);
423
424 hlist_del(&sdres->node);
425
426 nfc_llcp_free_sdp_tlv(sdres);
427 }
428
429 nla_nest_end(msg, sdp_attr);
430
431 genlmsg_end(msg, hdr);
432
Johannes Berg2a94fe42013-11-19 15:19:39 +0100433 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100434
435nla_put_failure:
436 genlmsg_cancel(msg, hdr);
437
438free_msg:
439 nlmsg_free(msg);
440
441 nfc_llcp_free_sdp_tlv_list(sdres_list);
442
443 return rc;
444}
445
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200446int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
447{
448 struct sk_buff *msg;
449 void *hdr;
450
451 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
452 if (!msg)
453 return -ENOMEM;
454
455 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
456 NFC_EVENT_SE_ADDED);
457 if (!hdr)
458 goto free_msg;
459
460 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
461 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
462 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
463 goto nla_put_failure;
464
465 genlmsg_end(msg, hdr);
466
Johannes Berg2a94fe42013-11-19 15:19:39 +0100467 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200468
469 return 0;
470
471nla_put_failure:
472 genlmsg_cancel(msg, hdr);
473free_msg:
474 nlmsg_free(msg);
475 return -EMSGSIZE;
476}
477
478int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
479{
480 struct sk_buff *msg;
481 void *hdr;
482
483 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
484 if (!msg)
485 return -ENOMEM;
486
487 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
488 NFC_EVENT_SE_REMOVED);
489 if (!hdr)
490 goto free_msg;
491
492 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
493 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
494 goto nla_put_failure;
495
496 genlmsg_end(msg, hdr);
497
Johannes Berg2a94fe42013-11-19 15:19:39 +0100498 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200499
500 return 0;
501
502nla_put_failure:
503 genlmsg_cancel(msg, hdr);
504free_msg:
505 nlmsg_free(msg);
506 return -EMSGSIZE;
507}
508
Christophe Ricard447b27c2015-02-01 22:26:16 +0100509int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
510 struct nfc_evt_transaction *evt_transaction)
511{
512 struct nfc_se *se;
513 struct sk_buff *msg;
514 void *hdr;
515
516 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
517 if (!msg)
518 return -ENOMEM;
519
520 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
521 NFC_EVENT_SE_TRANSACTION);
522 if (!hdr)
523 goto free_msg;
524
525 se = nfc_find_se(dev, se_idx);
526 if (!se)
527 goto free_msg;
528
529 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
530 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
531 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
532 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
533 evt_transaction->aid) ||
534 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
535 evt_transaction->params))
536 goto nla_put_failure;
537
538 /* evt_transaction is no more used */
539 devm_kfree(&dev->dev, evt_transaction);
540
541 genlmsg_end(msg, hdr);
542
543 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
544
545 return 0;
546
547nla_put_failure:
548 genlmsg_cancel(msg, hdr);
549free_msg:
550 /* evt_transaction is no more used */
551 devm_kfree(&dev->dev, evt_transaction);
552 nlmsg_free(msg);
553 return -EMSGSIZE;
554}
555
Christophe Ricard9afec6d2015-12-23 23:45:18 +0100556int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
557{
558 struct nfc_se *se;
559 struct sk_buff *msg;
560 void *hdr;
561
562 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
563 if (!msg)
564 return -ENOMEM;
565
566 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
567 NFC_EVENT_SE_CONNECTIVITY);
568 if (!hdr)
569 goto free_msg;
570
571 se = nfc_find_se(dev, se_idx);
572 if (!se)
573 goto free_msg;
574
575 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
576 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
577 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
578 goto nla_put_failure;
579
580 genlmsg_end(msg, hdr);
581
582 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
583
584 return 0;
585
586nla_put_failure:
587 genlmsg_cancel(msg, hdr);
588free_msg:
589 nlmsg_free(msg);
590 return -EMSGSIZE;
591}
592
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300593static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000594 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100595 struct netlink_callback *cb,
596 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300597{
598 void *hdr;
599
Eric W. Biederman15e47302012-09-07 20:12:54 +0000600 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100601 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300602 if (!hdr)
603 return -EMSGSIZE;
604
605 if (cb)
606 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
607
OGAWA Hirofumi85a25662017-02-04 10:15:55 +0900608 if (nfc_genl_setup_device_added(dev, msg))
David S. Miller1e6428d2012-03-29 23:23:57 -0400609 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300610
Johannes Berg053c0952015-01-16 22:09:00 +0100611 genlmsg_end(msg, hdr);
612 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300613
614nla_put_failure:
615 genlmsg_cancel(msg, hdr);
616 return -EMSGSIZE;
617}
618
619static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100620 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300621{
622 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
623 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
624 bool first_call = false;
625
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300626 if (!iter) {
627 first_call = true;
628 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
629 if (!iter)
630 return -ENOMEM;
631 cb->args[0] = (long) iter;
632 }
633
634 mutex_lock(&nfc_devlist_mutex);
635
636 cb->seq = nfc_devlist_generation;
637
638 if (first_call) {
639 nfc_device_iter_init(iter);
640 dev = nfc_device_iter_next(iter);
641 }
642
643 while (dev) {
644 int rc;
645
Eric W. Biederman15e47302012-09-07 20:12:54 +0000646 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100647 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300648 if (rc < 0)
649 break;
650
651 dev = nfc_device_iter_next(iter);
652 }
653
654 mutex_unlock(&nfc_devlist_mutex);
655
656 cb->args[1] = (long) dev;
657
658 return skb->len;
659}
660
661static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
662{
663 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
664
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300665 nfc_device_iter_exit(iter);
666 kfree(iter);
667
668 return 0;
669}
670
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100671int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100672 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100673{
674 struct sk_buff *msg;
675 void *hdr;
676
677 pr_debug("DEP link is up\n");
678
Thomas Graf58050fc2012-06-28 03:57:45 +0000679 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100680 if (!msg)
681 return -ENOMEM;
682
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100683 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100684 if (!hdr)
685 goto free_msg;
686
David S. Miller1e6428d2012-03-29 23:23:57 -0400687 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
688 goto nla_put_failure;
689 if (rf_mode == NFC_RF_INITIATOR &&
690 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
691 goto nla_put_failure;
692 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
693 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
694 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100695
696 genlmsg_end(msg, hdr);
697
698 dev->dep_link_up = true;
699
Johannes Berg2a94fe42013-11-19 15:19:39 +0100700 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100701
702 return 0;
703
704nla_put_failure:
705 genlmsg_cancel(msg, hdr);
706free_msg:
707 nlmsg_free(msg);
708 return -EMSGSIZE;
709}
710
711int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
712{
713 struct sk_buff *msg;
714 void *hdr;
715
716 pr_debug("DEP link is down\n");
717
Thomas Graf58050fc2012-06-28 03:57:45 +0000718 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100719 if (!msg)
720 return -ENOMEM;
721
722 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100723 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100724 if (!hdr)
725 goto free_msg;
726
David S. Miller1e6428d2012-03-29 23:23:57 -0400727 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
728 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100729
730 genlmsg_end(msg, hdr);
731
Johannes Berg2a94fe42013-11-19 15:19:39 +0100732 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100733
734 return 0;
735
736nla_put_failure:
737 genlmsg_cancel(msg, hdr);
738free_msg:
739 nlmsg_free(msg);
740 return -EMSGSIZE;
741}
742
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300743static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
744{
745 struct sk_buff *msg;
746 struct nfc_dev *dev;
747 u32 idx;
748 int rc = -ENOBUFS;
749
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300750 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
751 return -EINVAL;
752
753 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
754
755 dev = nfc_get_device(idx);
756 if (!dev)
757 return -ENODEV;
758
Thomas Graf58050fc2012-06-28 03:57:45 +0000759 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300760 if (!msg) {
761 rc = -ENOMEM;
762 goto out_putdev;
763 }
764
Eric W. Biederman15e47302012-09-07 20:12:54 +0000765 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100766 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300767 if (rc < 0)
768 goto out_free;
769
770 nfc_put_device(dev);
771
772 return genlmsg_reply(msg, info);
773
774out_free:
775 nlmsg_free(msg);
776out_putdev:
777 nfc_put_device(dev);
778 return rc;
779}
780
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300781static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
782{
783 struct nfc_dev *dev;
784 int rc;
785 u32 idx;
786
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300787 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
788 return -EINVAL;
789
790 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
791
792 dev = nfc_get_device(idx);
793 if (!dev)
794 return -ENODEV;
795
796 rc = nfc_dev_up(dev);
797
798 nfc_put_device(dev);
799 return rc;
800}
801
802static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
803{
804 struct nfc_dev *dev;
805 int rc;
806 u32 idx;
807
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300808 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
809 return -EINVAL;
810
811 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
812
813 dev = nfc_get_device(idx);
814 if (!dev)
815 return -ENODEV;
816
817 rc = nfc_dev_down(dev);
818
819 nfc_put_device(dev);
820 return rc;
821}
822
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300823static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
824{
825 struct nfc_dev *dev;
826 int rc;
827 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200828 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300829
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100830 pr_debug("Poll start\n");
831
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300832 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200833 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
834 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200835 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300836 return -EINVAL;
837
838 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200839
840 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
841 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200842
843 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
844 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200845 else if (info->attrs[NFC_ATTR_PROTOCOLS])
846 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300847
848 dev = nfc_get_device(idx);
849 if (!dev)
850 return -ENODEV;
851
852 mutex_lock(&dev->genl_data.genl_data_mutex);
853
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200854 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300855 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000856 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300857
858 mutex_unlock(&dev->genl_data.genl_data_mutex);
859
860 nfc_put_device(dev);
861 return rc;
862}
863
864static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
865{
866 struct nfc_dev *dev;
867 int rc;
868 u32 idx;
869
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300870 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
871 return -EINVAL;
872
873 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
874
875 dev = nfc_get_device(idx);
876 if (!dev)
877 return -ENODEV;
878
Samuel Ortiza831b912012-06-28 16:41:57 +0200879 device_lock(&dev->dev);
880
881 if (!dev->polling) {
882 device_unlock(&dev->dev);
883 return -EINVAL;
884 }
885
886 device_unlock(&dev->dev);
887
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300888 mutex_lock(&dev->genl_data.genl_data_mutex);
889
Eric W. Biederman15e47302012-09-07 20:12:54 +0000890 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300891 rc = -EBUSY;
892 goto out;
893 }
894
895 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000896 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300897
898out:
899 mutex_unlock(&dev->genl_data.genl_data_mutex);
900 nfc_put_device(dev);
901 return rc;
902}
903
Christophe Ricard3682f492014-12-02 21:27:50 +0100904static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
905{
906 struct nfc_dev *dev;
907 u32 device_idx, target_idx, protocol;
908 int rc;
909
910 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
911 return -EINVAL;
912
913 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
914
915 dev = nfc_get_device(device_idx);
916 if (!dev)
917 return -ENODEV;
918
919 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
920 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
921
Christophe Ricard96d45812015-10-25 22:54:43 +0100922 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
Christophe Ricard3682f492014-12-02 21:27:50 +0100923 rc = nfc_activate_target(dev, target_idx, protocol);
924
925 nfc_put_device(dev);
Andy Shevchenko32671832017-03-22 21:20:58 +0200926 return rc;
Christophe Ricard3682f492014-12-02 21:27:50 +0100927}
928
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100929static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
930{
931 struct nfc_dev *dev;
932 int rc, tgt_idx;
933 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100934 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100935
936 pr_debug("DEP link up\n");
937
938 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100939 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100940 return -EINVAL;
941
942 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
943 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
944 tgt_idx = NFC_TARGET_IDX_ANY;
945 else
946 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
947
948 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100949
950 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
951 return -EINVAL;
952
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100953 dev = nfc_get_device(idx);
954 if (!dev)
955 return -ENODEV;
956
Samuel Ortiz47807d32012-03-05 01:03:50 +0100957 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100958
959 nfc_put_device(dev);
960
961 return rc;
962}
963
964static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
965{
966 struct nfc_dev *dev;
967 int rc;
968 u32 idx;
969
970 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
971 return -EINVAL;
972
973 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
974
975 dev = nfc_get_device(idx);
976 if (!dev)
977 return -ENODEV;
978
979 rc = nfc_dep_link_down(dev);
980
981 nfc_put_device(dev);
982 return rc;
983}
984
Thierry Escande52feb442012-10-17 14:43:39 +0200985static int nfc_genl_send_params(struct sk_buff *msg,
986 struct nfc_llcp_local *local,
987 u32 portid, u32 seq)
988{
989 void *hdr;
990
991 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
992 NFC_CMD_LLC_GET_PARAMS);
993 if (!hdr)
994 return -EMSGSIZE;
995
996 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
997 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
998 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
999 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1000 goto nla_put_failure;
1001
Johannes Berg053c0952015-01-16 22:09:00 +01001002 genlmsg_end(msg, hdr);
1003 return 0;
Thierry Escande52feb442012-10-17 14:43:39 +02001004
1005nla_put_failure:
1006
1007 genlmsg_cancel(msg, hdr);
1008 return -EMSGSIZE;
1009}
1010
1011static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1012{
1013 struct nfc_dev *dev;
1014 struct nfc_llcp_local *local;
1015 int rc = 0;
1016 struct sk_buff *msg = NULL;
1017 u32 idx;
1018
1019 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1020 return -EINVAL;
1021
1022 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1023
1024 dev = nfc_get_device(idx);
1025 if (!dev)
1026 return -ENODEV;
1027
1028 device_lock(&dev->dev);
1029
1030 local = nfc_llcp_find_local(dev);
1031 if (!local) {
1032 rc = -ENODEV;
1033 goto exit;
1034 }
1035
1036 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1037 if (!msg) {
1038 rc = -ENOMEM;
1039 goto exit;
1040 }
1041
1042 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1043
1044exit:
1045 device_unlock(&dev->dev);
1046
1047 nfc_put_device(dev);
1048
1049 if (rc < 0) {
1050 if (msg)
1051 nlmsg_free(msg);
1052
1053 return rc;
1054 }
1055
1056 return genlmsg_reply(msg, info);
1057}
1058
1059static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1060{
1061 struct nfc_dev *dev;
1062 struct nfc_llcp_local *local;
1063 u8 rw = 0;
1064 u16 miux = 0;
1065 u32 idx;
1066 int rc = 0;
1067
1068 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1069 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1070 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1071 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1072 return -EINVAL;
1073
1074 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1075 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1076
1077 if (rw > LLCP_MAX_RW)
1078 return -EINVAL;
1079 }
1080
1081 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1082 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1083
1084 if (miux > LLCP_MAX_MIUX)
1085 return -EINVAL;
1086 }
1087
1088 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1089
1090 dev = nfc_get_device(idx);
1091 if (!dev)
1092 return -ENODEV;
1093
1094 device_lock(&dev->dev);
1095
1096 local = nfc_llcp_find_local(dev);
1097 if (!local) {
1098 nfc_put_device(dev);
1099 rc = -ENODEV;
1100 goto exit;
1101 }
1102
1103 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1104 if (dev->dep_link_up) {
1105 rc = -EINPROGRESS;
1106 goto exit;
1107 }
1108
1109 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1110 }
1111
1112 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1113 local->rw = rw;
1114
1115 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1116 local->miux = cpu_to_be16(miux);
1117
1118exit:
1119 device_unlock(&dev->dev);
1120
1121 nfc_put_device(dev);
1122
1123 return rc;
1124}
1125
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001126static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1127{
1128 struct nfc_dev *dev;
1129 struct nfc_llcp_local *local;
1130 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1131 u32 idx;
1132 u8 tid;
1133 char *uri;
1134 int rc = 0, rem;
1135 size_t uri_len, tlvs_len;
1136 struct hlist_head sdreq_list;
1137 struct nfc_llcp_sdp_tlv *sdreq;
1138
1139 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1140 !info->attrs[NFC_ATTR_LLC_SDP])
1141 return -EINVAL;
1142
1143 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1144
1145 dev = nfc_get_device(idx);
Julia Lawall9abebb82015-10-17 11:32:19 +02001146 if (!dev)
1147 return -ENODEV;
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001148
1149 device_lock(&dev->dev);
1150
1151 if (dev->dep_link_up == false) {
1152 rc = -ENOLINK;
1153 goto exit;
1154 }
1155
1156 local = nfc_llcp_find_local(dev);
1157 if (!local) {
1158 nfc_put_device(dev);
1159 rc = -ENODEV;
1160 goto exit;
1161 }
1162
1163 INIT_HLIST_HEAD(&sdreq_list);
1164
1165 tlvs_len = 0;
1166
1167 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1168 rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
Johannes Bergfe521452017-04-12 14:34:08 +02001169 nfc_sdp_genl_policy, info->extack);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001170
1171 if (rc != 0) {
1172 rc = -EINVAL;
1173 goto exit;
1174 }
1175
1176 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1177 continue;
1178
1179 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1180 if (uri_len == 0)
1181 continue;
1182
1183 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1184 if (uri == NULL || *uri == 0)
1185 continue;
1186
1187 tid = local->sdreq_next_tid++;
1188
1189 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1190 if (sdreq == NULL) {
1191 rc = -ENOMEM;
1192 goto exit;
1193 }
1194
1195 tlvs_len += sdreq->tlv_len;
1196
1197 hlist_add_head(&sdreq->node, &sdreq_list);
1198 }
1199
1200 if (hlist_empty(&sdreq_list)) {
1201 rc = -EINVAL;
1202 goto exit;
1203 }
1204
1205 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1206exit:
1207 device_unlock(&dev->dev);
1208
1209 nfc_put_device(dev);
1210
1211 return rc;
1212}
1213
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001214static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001215{
1216 struct nfc_dev *dev;
1217 int rc;
1218 u32 idx;
1219 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1220
1221 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1222 return -EINVAL;
1223
1224 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1225
1226 dev = nfc_get_device(idx);
1227 if (!dev)
1228 return -ENODEV;
1229
1230 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1231 sizeof(firmware_name));
1232
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001233 rc = nfc_fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001234
1235 nfc_put_device(dev);
1236 return rc;
1237}
1238
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001239int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1240 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001241{
1242 struct sk_buff *msg;
1243 void *hdr;
1244
1245 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1246 if (!msg)
1247 return -ENOMEM;
1248
1249 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001250 NFC_CMD_FW_DOWNLOAD);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001251 if (!hdr)
1252 goto free_msg;
1253
1254 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001255 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
Eric Lapuyade9674da82013-04-29 17:13:27 +02001256 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1257 goto nla_put_failure;
1258
1259 genlmsg_end(msg, hdr);
1260
Johannes Berg2a94fe42013-11-19 15:19:39 +01001261 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001262
1263 return 0;
1264
1265nla_put_failure:
1266 genlmsg_cancel(msg, hdr);
1267free_msg:
1268 nlmsg_free(msg);
1269 return -EMSGSIZE;
1270}
1271
Samuel Ortizbe085652013-05-10 17:07:32 +02001272static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1273{
1274 struct nfc_dev *dev;
1275 int rc;
1276 u32 idx, se_idx;
1277
1278 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1279 !info->attrs[NFC_ATTR_SE_INDEX])
1280 return -EINVAL;
1281
1282 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1283 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1284
1285 dev = nfc_get_device(idx);
1286 if (!dev)
1287 return -ENODEV;
1288
1289 rc = nfc_enable_se(dev, se_idx);
1290
1291 nfc_put_device(dev);
1292 return rc;
1293}
1294
1295static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1296{
1297 struct nfc_dev *dev;
1298 int rc;
1299 u32 idx, se_idx;
1300
1301 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1302 !info->attrs[NFC_ATTR_SE_INDEX])
1303 return -EINVAL;
1304
1305 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1306 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1307
1308 dev = nfc_get_device(idx);
1309 if (!dev)
1310 return -ENODEV;
1311
1312 rc = nfc_disable_se(dev, se_idx);
1313
1314 nfc_put_device(dev);
1315 return rc;
1316}
1317
Samuel Ortizac22ac42013-07-24 18:10:50 +02001318static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1319 u32 portid, u32 seq,
1320 struct netlink_callback *cb,
1321 int flags)
1322{
1323 void *hdr;
1324 struct nfc_se *se, *n;
1325
1326 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1327 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1328 NFC_CMD_GET_SE);
1329 if (!hdr)
1330 goto nla_put_failure;
1331
1332 if (cb)
1333 genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1334
1335 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1336 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1337 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1338 goto nla_put_failure;
1339
Johannes Berg053c0952015-01-16 22:09:00 +01001340 genlmsg_end(msg, hdr);
Samuel Ortizac22ac42013-07-24 18:10:50 +02001341 }
1342
1343 return 0;
1344
1345nla_put_failure:
1346 genlmsg_cancel(msg, hdr);
1347 return -EMSGSIZE;
1348}
1349
1350static int nfc_genl_dump_ses(struct sk_buff *skb,
1351 struct netlink_callback *cb)
1352{
1353 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1354 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1355 bool first_call = false;
1356
1357 if (!iter) {
1358 first_call = true;
1359 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1360 if (!iter)
1361 return -ENOMEM;
1362 cb->args[0] = (long) iter;
1363 }
1364
1365 mutex_lock(&nfc_devlist_mutex);
1366
1367 cb->seq = nfc_devlist_generation;
1368
1369 if (first_call) {
1370 nfc_device_iter_init(iter);
1371 dev = nfc_device_iter_next(iter);
1372 }
1373
1374 while (dev) {
1375 int rc;
1376
1377 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1378 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1379 if (rc < 0)
1380 break;
1381
1382 dev = nfc_device_iter_next(iter);
1383 }
1384
1385 mutex_unlock(&nfc_devlist_mutex);
1386
1387 cb->args[1] = (long) dev;
1388
1389 return skb->len;
1390}
1391
1392static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1393{
1394 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1395
1396 nfc_device_iter_exit(iter);
1397 kfree(iter);
1398
1399 return 0;
1400}
1401
Christophe Ricardcd96db62014-12-02 21:27:51 +01001402static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1403 u8 *apdu, size_t apdu_length,
1404 se_io_cb_t cb, void *cb_context)
1405{
1406 struct nfc_se *se;
1407 int rc;
1408
1409 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1410
1411 device_lock(&dev->dev);
1412
1413 if (!device_is_registered(&dev->dev)) {
1414 rc = -ENODEV;
1415 goto error;
1416 }
1417
1418 if (!dev->dev_up) {
1419 rc = -ENODEV;
1420 goto error;
1421 }
1422
1423 if (!dev->ops->se_io) {
1424 rc = -EOPNOTSUPP;
1425 goto error;
1426 }
1427
1428 se = nfc_find_se(dev, se_idx);
1429 if (!se) {
1430 rc = -EINVAL;
1431 goto error;
1432 }
1433
1434 if (se->state != NFC_SE_ENABLED) {
1435 rc = -ENODEV;
1436 goto error;
1437 }
1438
1439 rc = dev->ops->se_io(dev, se_idx, apdu,
1440 apdu_length, cb, cb_context);
1441
1442error:
1443 device_unlock(&dev->dev);
1444 return rc;
1445}
1446
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001447struct se_io_ctx {
1448 u32 dev_idx;
1449 u32 se_idx;
1450};
1451
Samuel Ortizddc1a702013-10-07 14:18:44 +02001452static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001453{
1454 struct se_io_ctx *ctx = context;
1455 struct sk_buff *msg;
1456 void *hdr;
1457
1458 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1459 if (!msg) {
1460 kfree(ctx);
1461 return;
1462 }
1463
1464 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1465 NFC_CMD_SE_IO);
1466 if (!hdr)
1467 goto free_msg;
1468
1469 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1470 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1471 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1472 goto nla_put_failure;
1473
1474 genlmsg_end(msg, hdr);
1475
Johannes Berg2a94fe42013-11-19 15:19:39 +01001476 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001477
1478 kfree(ctx);
1479
1480 return;
1481
1482nla_put_failure:
1483 genlmsg_cancel(msg, hdr);
1484free_msg:
1485 nlmsg_free(msg);
1486 kfree(ctx);
1487
1488 return;
1489}
1490
1491static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1492{
1493 struct nfc_dev *dev;
1494 struct se_io_ctx *ctx;
1495 u32 dev_idx, se_idx;
1496 u8 *apdu;
1497 size_t apdu_len;
1498
1499 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1500 !info->attrs[NFC_ATTR_SE_INDEX] ||
1501 !info->attrs[NFC_ATTR_SE_APDU])
1502 return -EINVAL;
1503
1504 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1505 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1506
1507 dev = nfc_get_device(dev_idx);
1508 if (!dev)
1509 return -ENODEV;
1510
1511 if (!dev->ops || !dev->ops->se_io)
1512 return -ENOTSUPP;
1513
1514 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1515 if (apdu_len == 0)
1516 return -EINVAL;
1517
1518 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1519 if (!apdu)
1520 return -EINVAL;
1521
1522 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1523 if (!ctx)
1524 return -ENOMEM;
1525
1526 ctx->dev_idx = dev_idx;
1527 ctx->se_idx = se_idx;
1528
Christophe Ricardcd96db62014-12-02 21:27:51 +01001529 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001530}
1531
Samuel Ortiz9e580952014-10-14 02:19:46 +02001532static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1533 struct genl_info *info)
1534{
1535 struct nfc_dev *dev;
1536 struct nfc_vendor_cmd *cmd;
1537 u32 dev_idx, vid, subcmd;
1538 u8 *data;
1539 size_t data_len;
Christophe Ricard29e76922015-08-19 21:26:43 +02001540 int i, err;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001541
1542 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1543 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1544 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1545 return -EINVAL;
1546
1547 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1548 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1549 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1550
1551 dev = nfc_get_device(dev_idx);
1552 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1553 return -ENODEV;
1554
Christophe Ricardfe202fe2015-08-14 22:33:40 +02001555 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1556 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
Samuel Ortiz9e580952014-10-14 02:19:46 +02001557 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1558 if (data_len == 0)
1559 return -EINVAL;
1560 } else {
Christophe Ricardadca3c32015-08-17 08:33:43 +02001561 data = NULL;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001562 data_len = 0;
1563 }
1564
1565 for (i = 0; i < dev->n_vendor_cmds; i++) {
1566 cmd = &dev->vendor_cmds[i];
1567
1568 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1569 continue;
1570
Christophe Ricard29e76922015-08-19 21:26:43 +02001571 dev->cur_cmd_info = info;
1572 err = cmd->doit(dev, data, data_len);
1573 dev->cur_cmd_info = NULL;
1574 return err;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001575 }
1576
1577 return -EOPNOTSUPP;
1578}
1579
Christophe Ricard29e76922015-08-19 21:26:43 +02001580/* message building helper */
1581static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1582 int flags, u8 cmd)
1583{
1584 /* since there is no private header just add the generic one */
1585 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1586}
1587
1588static struct sk_buff *
1589__nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1590 u32 portid, u32 seq,
1591 enum nfc_attrs attr,
1592 u32 oui, u32 subcmd, gfp_t gfp)
1593{
1594 struct sk_buff *skb;
1595 void *hdr;
1596
1597 skb = nlmsg_new(approxlen + 100, gfp);
1598 if (!skb)
1599 return NULL;
1600
1601 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1602 if (!hdr) {
1603 kfree_skb(skb);
1604 return NULL;
1605 }
1606
1607 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1608 goto nla_put_failure;
1609 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1610 goto nla_put_failure;
1611 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1612 goto nla_put_failure;
1613
1614 ((void **)skb->cb)[0] = dev;
1615 ((void **)skb->cb)[1] = hdr;
1616
1617 return skb;
1618
1619nla_put_failure:
1620 kfree_skb(skb);
1621 return NULL;
1622}
1623
1624struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1625 enum nfc_attrs attr,
1626 u32 oui, u32 subcmd,
1627 int approxlen)
1628{
1629 if (WARN_ON(!dev->cur_cmd_info))
1630 return NULL;
1631
1632 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1633 dev->cur_cmd_info->snd_portid,
1634 dev->cur_cmd_info->snd_seq, attr,
1635 oui, subcmd, GFP_KERNEL);
1636}
1637EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1638
1639int nfc_vendor_cmd_reply(struct sk_buff *skb)
1640{
1641 struct nfc_dev *dev = ((void **)skb->cb)[0];
1642 void *hdr = ((void **)skb->cb)[1];
1643
1644 /* clear CB data for netlink core to own from now on */
1645 memset(skb->cb, 0, sizeof(skb->cb));
1646
1647 if (WARN_ON(!dev->cur_cmd_info)) {
1648 kfree_skb(skb);
1649 return -EINVAL;
1650 }
1651
1652 genlmsg_end(skb, hdr);
1653 return genlmsg_reply(skb, dev->cur_cmd_info);
1654}
1655EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1656
Johannes Berg4534de82013-11-14 17:14:46 +01001657static const struct genl_ops nfc_genl_ops[] = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001658 {
1659 .cmd = NFC_CMD_GET_DEVICE,
1660 .doit = nfc_genl_get_device,
1661 .dumpit = nfc_genl_dump_devices,
1662 .done = nfc_genl_dump_devices_done,
1663 .policy = nfc_genl_policy,
1664 },
1665 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001666 .cmd = NFC_CMD_DEV_UP,
1667 .doit = nfc_genl_dev_up,
1668 .policy = nfc_genl_policy,
1669 },
1670 {
1671 .cmd = NFC_CMD_DEV_DOWN,
1672 .doit = nfc_genl_dev_down,
1673 .policy = nfc_genl_policy,
1674 },
1675 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001676 .cmd = NFC_CMD_START_POLL,
1677 .doit = nfc_genl_start_poll,
1678 .policy = nfc_genl_policy,
1679 },
1680 {
1681 .cmd = NFC_CMD_STOP_POLL,
1682 .doit = nfc_genl_stop_poll,
1683 .policy = nfc_genl_policy,
1684 },
1685 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001686 .cmd = NFC_CMD_DEP_LINK_UP,
1687 .doit = nfc_genl_dep_link_up,
1688 .policy = nfc_genl_policy,
1689 },
1690 {
1691 .cmd = NFC_CMD_DEP_LINK_DOWN,
1692 .doit = nfc_genl_dep_link_down,
1693 .policy = nfc_genl_policy,
1694 },
1695 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001696 .cmd = NFC_CMD_GET_TARGET,
1697 .dumpit = nfc_genl_dump_targets,
1698 .done = nfc_genl_dump_targets_done,
1699 .policy = nfc_genl_policy,
1700 },
Thierry Escande52feb442012-10-17 14:43:39 +02001701 {
1702 .cmd = NFC_CMD_LLC_GET_PARAMS,
1703 .doit = nfc_genl_llc_get_params,
1704 .policy = nfc_genl_policy,
1705 },
1706 {
1707 .cmd = NFC_CMD_LLC_SET_PARAMS,
1708 .doit = nfc_genl_llc_set_params,
1709 .policy = nfc_genl_policy,
1710 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001711 {
1712 .cmd = NFC_CMD_LLC_SDREQ,
1713 .doit = nfc_genl_llc_sdreq,
1714 .policy = nfc_genl_policy,
1715 },
Eric Lapuyade9674da82013-04-29 17:13:27 +02001716 {
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001717 .cmd = NFC_CMD_FW_DOWNLOAD,
1718 .doit = nfc_genl_fw_download,
Eric Lapuyade9674da82013-04-29 17:13:27 +02001719 .policy = nfc_genl_policy,
1720 },
Samuel Ortizbe085652013-05-10 17:07:32 +02001721 {
1722 .cmd = NFC_CMD_ENABLE_SE,
1723 .doit = nfc_genl_enable_se,
1724 .policy = nfc_genl_policy,
1725 },
1726 {
1727 .cmd = NFC_CMD_DISABLE_SE,
1728 .doit = nfc_genl_disable_se,
1729 .policy = nfc_genl_policy,
1730 },
Samuel Ortizac22ac42013-07-24 18:10:50 +02001731 {
1732 .cmd = NFC_CMD_GET_SE,
1733 .dumpit = nfc_genl_dump_ses,
1734 .done = nfc_genl_dump_ses_done,
1735 .policy = nfc_genl_policy,
1736 },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001737 {
1738 .cmd = NFC_CMD_SE_IO,
1739 .doit = nfc_genl_se_io,
1740 .policy = nfc_genl_policy,
1741 },
Christophe Ricard3682f492014-12-02 21:27:50 +01001742 {
1743 .cmd = NFC_CMD_ACTIVATE_TARGET,
1744 .doit = nfc_genl_activate_target,
1745 .policy = nfc_genl_policy,
1746 },
Samuel Ortiz9e580952014-10-14 02:19:46 +02001747 {
1748 .cmd = NFC_CMD_VENDOR,
1749 .doit = nfc_genl_vendor_cmd,
1750 .policy = nfc_genl_policy,
1751 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001752};
1753
Johannes Berg56989f62016-10-24 14:40:05 +02001754static struct genl_family nfc_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001755 .hdrsize = 0,
1756 .name = NFC_GENL_NAME,
1757 .version = NFC_GENL_VERSION,
1758 .maxattr = NFC_ATTR_MAX,
1759 .module = THIS_MODULE,
1760 .ops = nfc_genl_ops,
1761 .n_ops = ARRAY_SIZE(nfc_genl_ops),
1762 .mcgrps = nfc_genl_mcgrps,
1763 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1764};
1765
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001766
1767struct urelease_work {
1768 struct work_struct w;
Richard Weinberger65bc4f92015-04-13 00:52:36 +02001769 u32 portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001770};
1771
1772static void nfc_urelease_event_work(struct work_struct *work)
1773{
1774 struct urelease_work *w = container_of(work, struct urelease_work, w);
1775 struct class_dev_iter iter;
1776 struct nfc_dev *dev;
1777
John W. Linvillec4876062012-09-28 11:11:16 -04001778 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001779
1780 mutex_lock(&nfc_devlist_mutex);
1781
1782 nfc_device_iter_init(&iter);
1783 dev = nfc_device_iter_next(&iter);
1784
1785 while (dev) {
1786 mutex_lock(&dev->genl_data.genl_data_mutex);
1787
John W. Linvillec4876062012-09-28 11:11:16 -04001788 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001789 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001790 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001791 }
1792
1793 mutex_unlock(&dev->genl_data.genl_data_mutex);
1794
1795 dev = nfc_device_iter_next(&iter);
1796 }
1797
1798 nfc_device_iter_exit(&iter);
1799
1800 mutex_unlock(&nfc_devlist_mutex);
1801
1802 kfree(w);
1803}
1804
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001805static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001806 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001807{
1808 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001809 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001810
1811 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1812 goto out;
1813
Eric W. Biederman15e47302012-09-07 20:12:54 +00001814 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001815
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001816 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1817 if (w) {
1818 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001819 w->portid = n->portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001820 schedule_work((struct work_struct *) w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001821 }
1822
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001823out:
1824 return NOTIFY_DONE;
1825}
1826
1827void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1828{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001829 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001830 mutex_init(&genl_data->genl_data_mutex);
1831}
1832
1833void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1834{
1835 mutex_destroy(&genl_data->genl_data_mutex);
1836}
1837
1838static struct notifier_block nl_notifier = {
1839 .notifier_call = nfc_genl_rcv_nl_event,
1840};
1841
1842/**
1843 * nfc_genl_init() - Initialize netlink interface
1844 *
1845 * This initialization function registers the nfc netlink family.
1846 */
1847int __init nfc_genl_init(void)
1848{
1849 int rc;
1850
Johannes Berg489111e2016-10-24 14:40:03 +02001851 rc = genl_register_family(&nfc_genl_family);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001852 if (rc)
1853 return rc;
1854
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001855 netlink_register_notifier(&nl_notifier);
1856
Johannes Berg2a94fe42013-11-19 15:19:39 +01001857 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001858}
1859
1860/**
1861 * nfc_genl_exit() - Deinitialize netlink interface
1862 *
1863 * This exit function unregisters the nfc netlink family.
1864 */
1865void nfc_genl_exit(void)
1866{
1867 netlink_unregister_notifier(&nl_notifier);
1868 genl_unregister_family(&nfc_genl_family);
1869}