blob: f184b0db79d4026418ffa4fbf80bd6b38bdd3d0a [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03002/*
3 * Copyright (C) 2011 Instituto Nokia de Tecnologia
4 *
5 * Authors:
6 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
7 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
8 *
Samuel Ortiz9e580952014-10-14 02:19:46 +02009 * Vendor commands implementation based on net/wireless/nl80211.c
10 * which is:
11 *
12 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
13 * Copyright 2013-2014 Intel Mobile Communications GmbH
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030014 */
15
Samuel Ortiz52858b52011-12-14 16:43:05 +010016#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perches20c239c2011-11-29 11:37:33 -080017
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030018#include <net/genetlink.h>
19#include <linux/nfc.h>
20#include <linux/slab.h>
21
22#include "nfc.h"
Samuel Ortiz30cc4582013-04-26 11:49:40 +020023#include "llcp.h"
Thierry Escande52feb442012-10-17 14:43:39 +020024
Johannes Berg2a94fe42013-11-19 15:19:39 +010025static const struct genl_multicast_group nfc_genl_mcgrps[] = {
26 { .name = NFC_GENL_MCAST_EVENT_NAME, },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030027};
28
Johannes Berg489111e2016-10-24 14:40:03 +020029static struct genl_family nfc_genl_family;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030030static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
31 [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
32 [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
33 .len = NFC_DEVICE_NAME_MAXSIZE },
34 [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
Jakub Kicinski88e706d2020-03-02 21:05:25 -080035 [NFC_ATTR_TARGET_INDEX] = { .type = NLA_U32 },
Samuel Ortiz1ed28f62011-12-14 16:43:09 +010036 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
37 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
Samuel Ortizc970a1a2012-03-05 01:03:34 +010038 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
Samuel Ortizfe7c5802012-05-15 15:57:06 +020039 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
40 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
Thierry Escande8af362d2013-02-15 10:42:52 +010041 [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
42 [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
43 [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010044 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
Eric Lapuyade9674da82013-04-29 17:13:27 +020045 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
46 .len = NFC_FIRMWARE_NAME_MAXSIZE },
Jakub Kicinski361d23e2020-03-02 21:05:24 -080047 [NFC_ATTR_SE_INDEX] = { .type = NLA_U32 },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +020048 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
Jakub Kicinski6ba3da42020-03-02 21:05:26 -080049 [NFC_ATTR_VENDOR_ID] = { .type = NLA_U32 },
50 [NFC_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
Christophe Ricard29e76922015-08-19 21:26:43 +020051 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
52
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010053};
54
55static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
Kees Cookfe9c8422018-02-14 15:45:07 -080056 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING,
57 .len = U8_MAX - 4 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +010058 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030059};
60
61static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010062 struct netlink_callback *cb, int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030063{
64 void *hdr;
65
Eric W. Biederman15e47302012-09-07 20:12:54 +000066 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +010067 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030068 if (!hdr)
69 return -EMSGSIZE;
70
Michal Kubecek0a833c22017-11-15 13:09:32 +010071 genl_dump_check_consistent(cb, hdr);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030072
David S. Miller1e6428d2012-03-29 23:23:57 -040073 if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
74 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
75 nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
76 nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
77 goto nla_put_failure;
78 if (target->nfcid1_len > 0 &&
79 nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
80 target->nfcid1))
81 goto nla_put_failure;
82 if (target->sensb_res_len > 0 &&
83 nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
84 target->sensb_res))
85 goto nla_put_failure;
86 if (target->sensf_res_len > 0 &&
87 nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
88 target->sensf_res))
89 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -030090
Mark A. Greerf5f68722014-01-14 17:52:11 -070091 if (target->is_iso15693) {
92 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
93 target->iso15693_dsfid) ||
94 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
95 sizeof(target->iso15693_uid), target->iso15693_uid))
96 goto nla_put_failure;
97 }
98
Johannes Berg053c0952015-01-16 22:09:00 +010099 genlmsg_end(msg, hdr);
100 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300101
102nla_put_failure:
103 genlmsg_cancel(msg, hdr);
104 return -EMSGSIZE;
105}
106
107static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
108{
Jiri Pirko4495af32019-10-05 20:04:38 +0200109 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300110 struct nfc_dev *dev;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300111 u32 idx;
112
Jiri Pirko4495af32019-10-05 20:04:38 +0200113 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300114 return ERR_PTR(-EINVAL);
115
Jiri Pirko4495af32019-10-05 20:04:38 +0200116 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300117
118 dev = nfc_get_device(idx);
119 if (!dev)
120 return ERR_PTR(-ENODEV);
121
122 return dev;
123}
124
125static int nfc_genl_dump_targets(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100126 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300127{
128 int i = cb->args[0];
129 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
130 int rc;
131
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300132 if (!dev) {
133 dev = __get_device_from_cb(cb);
134 if (IS_ERR(dev))
135 return PTR_ERR(dev);
136
137 cb->args[1] = (long) dev;
138 }
139
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200140 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300141
142 cb->seq = dev->targets_generation;
143
144 while (i < dev->n_targets) {
145 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100146 NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300147 if (rc < 0)
148 break;
149
150 i++;
151 }
152
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200153 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300154
155 cb->args[0] = i;
156
157 return skb->len;
158}
159
160static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
161{
162 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
163
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300164 if (dev)
165 nfc_put_device(dev);
166
167 return 0;
168}
169
170int nfc_genl_targets_found(struct nfc_dev *dev)
171{
172 struct sk_buff *msg;
173 void *hdr;
174
Eric W. Biederman15e47302012-09-07 20:12:54 +0000175 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300176
Thomas Graf58050fc2012-06-28 03:57:45 +0000177 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300178 if (!msg)
179 return -ENOMEM;
180
181 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100182 NFC_EVENT_TARGETS_FOUND);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300183 if (!hdr)
184 goto free_msg;
185
David S. Miller1e6428d2012-03-29 23:23:57 -0400186 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
187 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300188
189 genlmsg_end(msg, hdr);
190
Johannes Berg2a94fe42013-11-19 15:19:39 +0100191 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300192
193nla_put_failure:
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300194free_msg:
195 nlmsg_free(msg);
196 return -EMSGSIZE;
197}
198
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200199int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
200{
201 struct sk_buff *msg;
202 void *hdr;
203
Thomas Graf58050fc2012-06-28 03:57:45 +0000204 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200205 if (!msg)
206 return -ENOMEM;
207
208 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
209 NFC_EVENT_TARGET_LOST);
210 if (!hdr)
211 goto free_msg;
212
John W. Linville59ef43e2012-04-18 14:17:13 -0400213 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
214 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
215 goto nla_put_failure;
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200216
217 genlmsg_end(msg, hdr);
218
Johannes Berg2a94fe42013-11-19 15:19:39 +0100219 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200220
221 return 0;
222
223nla_put_failure:
Samuel Ortiz8112a5c2012-04-10 19:43:04 +0200224free_msg:
225 nlmsg_free(msg);
226 return -EMSGSIZE;
227}
228
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200229int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
230{
231 struct sk_buff *msg;
232 void *hdr;
233
Thomas Graf58050fc2012-06-28 03:57:45 +0000234 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200235 if (!msg)
236 return -ENOMEM;
237
238 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
239 NFC_EVENT_TM_ACTIVATED);
240 if (!hdr)
241 goto free_msg;
242
243 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
244 goto nla_put_failure;
245 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
246 goto nla_put_failure;
247
248 genlmsg_end(msg, hdr);
249
Johannes Berg2a94fe42013-11-19 15:19:39 +0100250 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200251
252 return 0;
253
254nla_put_failure:
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200255free_msg:
256 nlmsg_free(msg);
257 return -EMSGSIZE;
258}
259
260int nfc_genl_tm_deactivated(struct nfc_dev *dev)
261{
262 struct sk_buff *msg;
263 void *hdr;
264
Thomas Graf58050fc2012-06-28 03:57:45 +0000265 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200266 if (!msg)
267 return -ENOMEM;
268
269 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
270 NFC_EVENT_TM_DEACTIVATED);
271 if (!hdr)
272 goto free_msg;
273
274 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
275 goto nla_put_failure;
276
277 genlmsg_end(msg, hdr);
278
Johannes Berg2a94fe42013-11-19 15:19:39 +0100279 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200280
281 return 0;
282
283nla_put_failure:
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200284free_msg:
285 nlmsg_free(msg);
286 return -EMSGSIZE;
287}
288
OGAWA Hirofumi85a25662017-02-04 10:15:55 +0900289static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg)
290{
291 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
292 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
293 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
294 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
295 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
296 return -1;
297 return 0;
298}
299
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300300int nfc_genl_device_added(struct nfc_dev *dev)
301{
302 struct sk_buff *msg;
303 void *hdr;
304
Thomas Graf58050fc2012-06-28 03:57:45 +0000305 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300306 if (!msg)
307 return -ENOMEM;
308
309 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100310 NFC_EVENT_DEVICE_ADDED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300311 if (!hdr)
312 goto free_msg;
313
OGAWA Hirofumi85a25662017-02-04 10:15:55 +0900314 if (nfc_genl_setup_device_added(dev, msg))
David S. Miller1e6428d2012-03-29 23:23:57 -0400315 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300316
317 genlmsg_end(msg, hdr);
318
Johannes Berg2a94fe42013-11-19 15:19:39 +0100319 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300320
321 return 0;
322
323nla_put_failure:
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300324free_msg:
325 nlmsg_free(msg);
326 return -EMSGSIZE;
327}
328
329int nfc_genl_device_removed(struct nfc_dev *dev)
330{
331 struct sk_buff *msg;
332 void *hdr;
333
Thomas Graf58050fc2012-06-28 03:57:45 +0000334 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300335 if (!msg)
336 return -ENOMEM;
337
338 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100339 NFC_EVENT_DEVICE_REMOVED);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300340 if (!hdr)
341 goto free_msg;
342
David S. Miller1e6428d2012-03-29 23:23:57 -0400343 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
344 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300345
346 genlmsg_end(msg, hdr);
347
Johannes Berg2a94fe42013-11-19 15:19:39 +0100348 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300349
350 return 0;
351
352nla_put_failure:
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300353free_msg:
354 nlmsg_free(msg);
355 return -EMSGSIZE;
356}
357
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100358int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
359{
360 struct sk_buff *msg;
361 struct nlattr *sdp_attr, *uri_attr;
362 struct nfc_llcp_sdp_tlv *sdres;
363 struct hlist_node *n;
364 void *hdr;
365 int rc = -EMSGSIZE;
366 int i;
367
368 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
369 if (!msg)
370 return -ENOMEM;
371
372 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
373 NFC_EVENT_LLC_SDRES);
374 if (!hdr)
375 goto free_msg;
376
377 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
378 goto nla_put_failure;
379
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200380 sdp_attr = nla_nest_start_noflag(msg, NFC_ATTR_LLC_SDP);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100381 if (sdp_attr == NULL) {
382 rc = -ENOMEM;
383 goto nla_put_failure;
384 }
385
386 i = 1;
387 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
388 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
389
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200390 uri_attr = nla_nest_start_noflag(msg, i++);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100391 if (uri_attr == NULL) {
392 rc = -ENOMEM;
393 goto nla_put_failure;
394 }
395
396 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
397 goto nla_put_failure;
398
399 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
400 goto nla_put_failure;
401
402 nla_nest_end(msg, uri_attr);
403
404 hlist_del(&sdres->node);
405
406 nfc_llcp_free_sdp_tlv(sdres);
407 }
408
409 nla_nest_end(msg, sdp_attr);
410
411 genlmsg_end(msg, hdr);
412
Johannes Berg2a94fe42013-11-19 15:19:39 +0100413 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100414
415nla_put_failure:
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100416free_msg:
417 nlmsg_free(msg);
418
419 nfc_llcp_free_sdp_tlv_list(sdres_list);
420
421 return rc;
422}
423
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200424int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
425{
426 struct sk_buff *msg;
427 void *hdr;
428
429 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
430 if (!msg)
431 return -ENOMEM;
432
433 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
434 NFC_EVENT_SE_ADDED);
435 if (!hdr)
436 goto free_msg;
437
438 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
439 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
440 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
441 goto nla_put_failure;
442
443 genlmsg_end(msg, hdr);
444
Johannes Berg2a94fe42013-11-19 15:19:39 +0100445 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200446
447 return 0;
448
449nla_put_failure:
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200450free_msg:
451 nlmsg_free(msg);
452 return -EMSGSIZE;
453}
454
455int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
456{
457 struct sk_buff *msg;
458 void *hdr;
459
460 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
461 if (!msg)
462 return -ENOMEM;
463
464 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
465 NFC_EVENT_SE_REMOVED);
466 if (!hdr)
467 goto free_msg;
468
469 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
470 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
471 goto nla_put_failure;
472
473 genlmsg_end(msg, hdr);
474
Johannes Berg2a94fe42013-11-19 15:19:39 +0100475 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200476
477 return 0;
478
479nla_put_failure:
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200480free_msg:
481 nlmsg_free(msg);
482 return -EMSGSIZE;
483}
484
Christophe Ricard447b27c2015-02-01 22:26:16 +0100485int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
486 struct nfc_evt_transaction *evt_transaction)
487{
488 struct nfc_se *se;
489 struct sk_buff *msg;
490 void *hdr;
491
492 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
493 if (!msg)
494 return -ENOMEM;
495
496 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
497 NFC_EVENT_SE_TRANSACTION);
498 if (!hdr)
499 goto free_msg;
500
501 se = nfc_find_se(dev, se_idx);
502 if (!se)
503 goto free_msg;
504
505 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
506 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
507 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
508 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
509 evt_transaction->aid) ||
510 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
511 evt_transaction->params))
512 goto nla_put_failure;
513
514 /* evt_transaction is no more used */
515 devm_kfree(&dev->dev, evt_transaction);
516
517 genlmsg_end(msg, hdr);
518
519 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
520
521 return 0;
522
523nla_put_failure:
Christophe Ricard447b27c2015-02-01 22:26:16 +0100524free_msg:
525 /* evt_transaction is no more used */
526 devm_kfree(&dev->dev, evt_transaction);
527 nlmsg_free(msg);
528 return -EMSGSIZE;
529}
530
Christophe Ricard9afec6d2015-12-23 23:45:18 +0100531int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
532{
Krzysztof Kozlowskif2479c02021-07-30 16:42:00 +0200533 const struct nfc_se *se;
Christophe Ricard9afec6d2015-12-23 23:45:18 +0100534 struct sk_buff *msg;
535 void *hdr;
536
537 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
538 if (!msg)
539 return -ENOMEM;
540
541 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
542 NFC_EVENT_SE_CONNECTIVITY);
543 if (!hdr)
544 goto free_msg;
545
546 se = nfc_find_se(dev, se_idx);
547 if (!se)
548 goto free_msg;
549
550 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
551 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
552 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
553 goto nla_put_failure;
554
555 genlmsg_end(msg, hdr);
556
557 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
558
559 return 0;
560
561nla_put_failure:
Christophe Ricard9afec6d2015-12-23 23:45:18 +0100562free_msg:
563 nlmsg_free(msg);
564 return -EMSGSIZE;
565}
566
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300567static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000568 u32 portid, u32 seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100569 struct netlink_callback *cb,
570 int flags)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300571{
572 void *hdr;
573
Eric W. Biederman15e47302012-09-07 20:12:54 +0000574 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100575 NFC_CMD_GET_DEVICE);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300576 if (!hdr)
577 return -EMSGSIZE;
578
579 if (cb)
Michal Kubecek0a833c22017-11-15 13:09:32 +0100580 genl_dump_check_consistent(cb, hdr);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300581
OGAWA Hirofumi85a25662017-02-04 10:15:55 +0900582 if (nfc_genl_setup_device_added(dev, msg))
David S. Miller1e6428d2012-03-29 23:23:57 -0400583 goto nla_put_failure;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300584
Johannes Berg053c0952015-01-16 22:09:00 +0100585 genlmsg_end(msg, hdr);
586 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300587
588nla_put_failure:
589 genlmsg_cancel(msg, hdr);
590 return -EMSGSIZE;
591}
592
593static int nfc_genl_dump_devices(struct sk_buff *skb,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100594 struct netlink_callback *cb)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300595{
596 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
597 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
598 bool first_call = false;
599
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300600 if (!iter) {
601 first_call = true;
602 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
603 if (!iter)
604 return -ENOMEM;
605 cb->args[0] = (long) iter;
606 }
607
608 mutex_lock(&nfc_devlist_mutex);
609
610 cb->seq = nfc_devlist_generation;
611
612 if (first_call) {
613 nfc_device_iter_init(iter);
614 dev = nfc_device_iter_next(iter);
615 }
616
617 while (dev) {
618 int rc;
619
Eric W. Biederman15e47302012-09-07 20:12:54 +0000620 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100621 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300622 if (rc < 0)
623 break;
624
625 dev = nfc_device_iter_next(iter);
626 }
627
628 mutex_unlock(&nfc_devlist_mutex);
629
630 cb->args[1] = (long) dev;
631
632 return skb->len;
633}
634
635static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
636{
637 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
638
Tadeusz Strukfd79a0c2021-12-08 10:27:42 -0800639 if (iter) {
640 nfc_device_iter_exit(iter);
641 kfree(iter);
642 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300643
644 return 0;
645}
646
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100647int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100648 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100649{
650 struct sk_buff *msg;
651 void *hdr;
652
653 pr_debug("DEP link is up\n");
654
Thomas Graf58050fc2012-06-28 03:57:45 +0000655 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100656 if (!msg)
657 return -ENOMEM;
658
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100659 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100660 if (!hdr)
661 goto free_msg;
662
David S. Miller1e6428d2012-03-29 23:23:57 -0400663 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
664 goto nla_put_failure;
665 if (rf_mode == NFC_RF_INITIATOR &&
666 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
667 goto nla_put_failure;
668 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
669 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
670 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100671
672 genlmsg_end(msg, hdr);
673
674 dev->dep_link_up = true;
675
Johannes Berg2a94fe42013-11-19 15:19:39 +0100676 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100677
678 return 0;
679
680nla_put_failure:
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100681free_msg:
682 nlmsg_free(msg);
683 return -EMSGSIZE;
684}
685
686int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
687{
688 struct sk_buff *msg;
689 void *hdr;
690
691 pr_debug("DEP link is down\n");
692
Thomas Graf58050fc2012-06-28 03:57:45 +0000693 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100694 if (!msg)
695 return -ENOMEM;
696
697 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100698 NFC_CMD_DEP_LINK_DOWN);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100699 if (!hdr)
700 goto free_msg;
701
David S. Miller1e6428d2012-03-29 23:23:57 -0400702 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
703 goto nla_put_failure;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100704
705 genlmsg_end(msg, hdr);
706
Johannes Berg2a94fe42013-11-19 15:19:39 +0100707 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100708
709 return 0;
710
711nla_put_failure:
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100712free_msg:
713 nlmsg_free(msg);
714 return -EMSGSIZE;
715}
716
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300717static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
718{
719 struct sk_buff *msg;
720 struct nfc_dev *dev;
721 u32 idx;
722 int rc = -ENOBUFS;
723
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300724 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
725 return -EINVAL;
726
727 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
728
729 dev = nfc_get_device(idx);
730 if (!dev)
731 return -ENODEV;
732
Thomas Graf58050fc2012-06-28 03:57:45 +0000733 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300734 if (!msg) {
735 rc = -ENOMEM;
736 goto out_putdev;
737 }
738
Eric W. Biederman15e47302012-09-07 20:12:54 +0000739 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100740 NULL, 0);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300741 if (rc < 0)
742 goto out_free;
743
744 nfc_put_device(dev);
745
746 return genlmsg_reply(msg, info);
747
748out_free:
749 nlmsg_free(msg);
750out_putdev:
751 nfc_put_device(dev);
752 return rc;
753}
754
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300755static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
756{
757 struct nfc_dev *dev;
758 int rc;
759 u32 idx;
760
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300761 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
762 return -EINVAL;
763
764 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
765
766 dev = nfc_get_device(idx);
767 if (!dev)
768 return -ENODEV;
769
770 rc = nfc_dev_up(dev);
771
772 nfc_put_device(dev);
773 return rc;
774}
775
776static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
777{
778 struct nfc_dev *dev;
779 int rc;
780 u32 idx;
781
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300782 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
783 return -EINVAL;
784
785 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
786
787 dev = nfc_get_device(idx);
788 if (!dev)
789 return -ENODEV;
790
791 rc = nfc_dev_down(dev);
792
793 nfc_put_device(dev);
794 return rc;
795}
796
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300797static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
798{
799 struct nfc_dev *dev;
800 int rc;
801 u32 idx;
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200802 u32 im_protocols = 0, tm_protocols = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300803
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100804 pr_debug("Poll start\n");
805
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300806 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200807 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
808 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
Szymon Janc0f450772012-10-17 15:23:39 +0200809 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300810 return -EINVAL;
811
812 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200813
814 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
815 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200816
817 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
818 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
Samuel Ortiz5e50ee32012-05-31 11:48:58 +0200819 else if (info->attrs[NFC_ATTR_PROTOCOLS])
820 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300821
822 dev = nfc_get_device(idx);
823 if (!dev)
824 return -ENODEV;
825
826 mutex_lock(&dev->genl_data.genl_data_mutex);
827
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200828 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300829 if (!rc)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000830 dev->genl_data.poll_req_portid = info->snd_portid;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300831
832 mutex_unlock(&dev->genl_data.genl_data_mutex);
833
834 nfc_put_device(dev);
835 return rc;
836}
837
838static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
839{
840 struct nfc_dev *dev;
841 int rc;
842 u32 idx;
843
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300844 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
845 return -EINVAL;
846
847 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
848
849 dev = nfc_get_device(idx);
850 if (!dev)
851 return -ENODEV;
852
Samuel Ortiza831b912012-06-28 16:41:57 +0200853 device_lock(&dev->dev);
854
855 if (!dev->polling) {
856 device_unlock(&dev->dev);
Pan Biand8f923c2021-01-21 07:37:45 -0800857 nfc_put_device(dev);
Samuel Ortiza831b912012-06-28 16:41:57 +0200858 return -EINVAL;
859 }
860
861 device_unlock(&dev->dev);
862
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300863 mutex_lock(&dev->genl_data.genl_data_mutex);
864
Eric W. Biederman15e47302012-09-07 20:12:54 +0000865 if (dev->genl_data.poll_req_portid != info->snd_portid) {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300866 rc = -EBUSY;
867 goto out;
868 }
869
870 rc = nfc_stop_poll(dev);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000871 dev->genl_data.poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300872
873out:
874 mutex_unlock(&dev->genl_data.genl_data_mutex);
875 nfc_put_device(dev);
876 return rc;
877}
878
Christophe Ricard3682f492014-12-02 21:27:50 +0100879static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
880{
881 struct nfc_dev *dev;
882 u32 device_idx, target_idx, protocol;
883 int rc;
884
Mateusz Jurczyka0323b92017-05-24 12:42:26 +0200885 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
886 !info->attrs[NFC_ATTR_TARGET_INDEX] ||
887 !info->attrs[NFC_ATTR_PROTOCOLS])
Christophe Ricard3682f492014-12-02 21:27:50 +0100888 return -EINVAL;
889
890 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
891
892 dev = nfc_get_device(device_idx);
893 if (!dev)
894 return -ENODEV;
895
896 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
897 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
898
Christophe Ricard96d45812015-10-25 22:54:43 +0100899 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
Christophe Ricard3682f492014-12-02 21:27:50 +0100900 rc = nfc_activate_target(dev, target_idx, protocol);
901
902 nfc_put_device(dev);
Andy Shevchenko32671832017-03-22 21:20:58 +0200903 return rc;
Christophe Ricard3682f492014-12-02 21:27:50 +0100904}
905
Mark Greer4d63adf2017-06-15 20:34:22 -0700906static int nfc_genl_deactivate_target(struct sk_buff *skb,
907 struct genl_info *info)
908{
909 struct nfc_dev *dev;
910 u32 device_idx, target_idx;
911 int rc;
912
Young Xiao385097a2019-06-14 15:13:02 +0800913 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
914 !info->attrs[NFC_ATTR_TARGET_INDEX])
Mark Greer4d63adf2017-06-15 20:34:22 -0700915 return -EINVAL;
916
917 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
918
919 dev = nfc_get_device(device_idx);
920 if (!dev)
921 return -ENODEV;
922
923 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
924
925 rc = nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
926
927 nfc_put_device(dev);
928 return rc;
929}
930
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100931static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
932{
933 struct nfc_dev *dev;
934 int rc, tgt_idx;
935 u32 idx;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100936 u8 comm;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100937
938 pr_debug("DEP link up\n");
939
940 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
Samuel Ortiz47807d32012-03-05 01:03:50 +0100941 !info->attrs[NFC_ATTR_COMM_MODE])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100942 return -EINVAL;
943
944 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
945 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
946 tgt_idx = NFC_TARGET_IDX_ANY;
947 else
948 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
949
950 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100951
952 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
953 return -EINVAL;
954
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100955 dev = nfc_get_device(idx);
956 if (!dev)
957 return -ENODEV;
958
Samuel Ortiz47807d32012-03-05 01:03:50 +0100959 rc = nfc_dep_link_up(dev, tgt_idx, comm);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100960
961 nfc_put_device(dev);
962
963 return rc;
964}
965
966static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
967{
968 struct nfc_dev *dev;
969 int rc;
970 u32 idx;
971
Andrey Konovalov18917d52019-07-29 16:35:01 +0300972 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
973 !info->attrs[NFC_ATTR_TARGET_INDEX])
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100974 return -EINVAL;
975
976 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
977
978 dev = nfc_get_device(idx);
979 if (!dev)
980 return -ENODEV;
981
982 rc = nfc_dep_link_down(dev);
983
984 nfc_put_device(dev);
985 return rc;
986}
987
Thierry Escande52feb442012-10-17 14:43:39 +0200988static int nfc_genl_send_params(struct sk_buff *msg,
989 struct nfc_llcp_local *local,
990 u32 portid, u32 seq)
991{
992 void *hdr;
993
994 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
995 NFC_CMD_LLC_GET_PARAMS);
996 if (!hdr)
997 return -EMSGSIZE;
998
999 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
1000 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
1001 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
1002 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1003 goto nla_put_failure;
1004
Johannes Berg053c0952015-01-16 22:09:00 +01001005 genlmsg_end(msg, hdr);
1006 return 0;
Thierry Escande52feb442012-10-17 14:43:39 +02001007
1008nla_put_failure:
Thierry Escande52feb442012-10-17 14:43:39 +02001009 genlmsg_cancel(msg, hdr);
1010 return -EMSGSIZE;
1011}
1012
1013static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1014{
1015 struct nfc_dev *dev;
1016 struct nfc_llcp_local *local;
1017 int rc = 0;
1018 struct sk_buff *msg = NULL;
1019 u32 idx;
1020
Andrey Konovalov18917d52019-07-29 16:35:01 +03001021 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1022 !info->attrs[NFC_ATTR_FIRMWARE_NAME])
Thierry Escande52feb442012-10-17 14:43:39 +02001023 return -EINVAL;
1024
1025 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1026
1027 dev = nfc_get_device(idx);
1028 if (!dev)
1029 return -ENODEV;
1030
1031 device_lock(&dev->dev);
1032
1033 local = nfc_llcp_find_local(dev);
1034 if (!local) {
1035 rc = -ENODEV;
1036 goto exit;
1037 }
1038
1039 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1040 if (!msg) {
1041 rc = -ENOMEM;
1042 goto exit;
1043 }
1044
1045 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1046
1047exit:
1048 device_unlock(&dev->dev);
1049
1050 nfc_put_device(dev);
1051
1052 if (rc < 0) {
1053 if (msg)
1054 nlmsg_free(msg);
1055
1056 return rc;
1057 }
1058
1059 return genlmsg_reply(msg, info);
1060}
1061
1062static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1063{
1064 struct nfc_dev *dev;
1065 struct nfc_llcp_local *local;
1066 u8 rw = 0;
1067 u16 miux = 0;
1068 u32 idx;
1069 int rc = 0;
1070
1071 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1072 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1073 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1074 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1075 return -EINVAL;
1076
1077 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1078 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1079
1080 if (rw > LLCP_MAX_RW)
1081 return -EINVAL;
1082 }
1083
1084 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1085 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1086
1087 if (miux > LLCP_MAX_MIUX)
1088 return -EINVAL;
1089 }
1090
1091 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1092
1093 dev = nfc_get_device(idx);
1094 if (!dev)
1095 return -ENODEV;
1096
1097 device_lock(&dev->dev);
1098
1099 local = nfc_llcp_find_local(dev);
1100 if (!local) {
Thierry Escande52feb442012-10-17 14:43:39 +02001101 rc = -ENODEV;
1102 goto exit;
1103 }
1104
1105 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1106 if (dev->dep_link_up) {
1107 rc = -EINPROGRESS;
1108 goto exit;
1109 }
1110
1111 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1112 }
1113
1114 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1115 local->rw = rw;
1116
1117 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1118 local->miux = cpu_to_be16(miux);
1119
1120exit:
1121 device_unlock(&dev->dev);
1122
1123 nfc_put_device(dev);
1124
1125 return rc;
1126}
1127
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001128static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1129{
1130 struct nfc_dev *dev;
1131 struct nfc_llcp_local *local;
1132 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1133 u32 idx;
1134 u8 tid;
1135 char *uri;
1136 int rc = 0, rem;
1137 size_t uri_len, tlvs_len;
1138 struct hlist_head sdreq_list;
1139 struct nfc_llcp_sdp_tlv *sdreq;
1140
1141 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1142 !info->attrs[NFC_ATTR_LLC_SDP])
1143 return -EINVAL;
1144
1145 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1146
1147 dev = nfc_get_device(idx);
Julia Lawall9abebb82015-10-17 11:32:19 +02001148 if (!dev)
1149 return -ENODEV;
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001150
1151 device_lock(&dev->dev);
1152
1153 if (dev->dep_link_up == false) {
1154 rc = -ENOLINK;
1155 goto exit;
1156 }
1157
1158 local = nfc_llcp_find_local(dev);
1159 if (!local) {
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001160 rc = -ENODEV;
1161 goto exit;
1162 }
1163
1164 INIT_HLIST_HEAD(&sdreq_list);
1165
1166 tlvs_len = 0;
1167
1168 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
Johannes Berg8cb08172019-04-26 14:07:28 +02001169 rc = nla_parse_nested_deprecated(sdp_attrs, NFC_SDP_ATTR_MAX,
1170 attr, nfc_sdp_genl_policy,
1171 info->extack);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001172
1173 if (rc != 0) {
1174 rc = -EINVAL;
1175 goto exit;
1176 }
1177
1178 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1179 continue;
1180
1181 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1182 if (uri_len == 0)
1183 continue;
1184
1185 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1186 if (uri == NULL || *uri == 0)
1187 continue;
1188
1189 tid = local->sdreq_next_tid++;
1190
1191 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1192 if (sdreq == NULL) {
1193 rc = -ENOMEM;
1194 goto exit;
1195 }
1196
1197 tlvs_len += sdreq->tlv_len;
1198
1199 hlist_add_head(&sdreq->node, &sdreq_list);
1200 }
1201
1202 if (hlist_empty(&sdreq_list)) {
1203 rc = -EINVAL;
1204 goto exit;
1205 }
1206
1207 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1208exit:
1209 device_unlock(&dev->dev);
1210
1211 nfc_put_device(dev);
1212
1213 return rc;
1214}
1215
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001216static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001217{
1218 struct nfc_dev *dev;
1219 int rc;
1220 u32 idx;
1221 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1222
Defang Bo280e3eb2020-10-19 19:38:58 +08001223 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || !info->attrs[NFC_ATTR_FIRMWARE_NAME])
Eric Lapuyade9674da82013-04-29 17:13:27 +02001224 return -EINVAL;
1225
1226 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1227
1228 dev = nfc_get_device(idx);
1229 if (!dev)
1230 return -ENODEV;
1231
Francis Laniel872f6902020-11-15 18:08:06 +01001232 nla_strscpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
Eric Lapuyade9674da82013-04-29 17:13:27 +02001233 sizeof(firmware_name));
1234
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001235 rc = nfc_fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001236
1237 nfc_put_device(dev);
1238 return rc;
1239}
1240
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001241int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1242 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +02001243{
1244 struct sk_buff *msg;
1245 void *hdr;
1246
1247 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1248 if (!msg)
1249 return -ENOMEM;
1250
1251 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001252 NFC_CMD_FW_DOWNLOAD);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001253 if (!hdr)
1254 goto free_msg;
1255
1256 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
Eric Lapuyade352a5f52013-07-19 14:57:55 +02001257 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
Eric Lapuyade9674da82013-04-29 17:13:27 +02001258 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1259 goto nla_put_failure;
1260
1261 genlmsg_end(msg, hdr);
1262
Johannes Berg2a94fe42013-11-19 15:19:39 +01001263 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Eric Lapuyade9674da82013-04-29 17:13:27 +02001264
1265 return 0;
1266
1267nla_put_failure:
Eric Lapuyade9674da82013-04-29 17:13:27 +02001268free_msg:
1269 nlmsg_free(msg);
1270 return -EMSGSIZE;
1271}
1272
Samuel Ortizbe085652013-05-10 17:07:32 +02001273static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1274{
1275 struct nfc_dev *dev;
1276 int rc;
1277 u32 idx, se_idx;
1278
1279 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1280 !info->attrs[NFC_ATTR_SE_INDEX])
1281 return -EINVAL;
1282
1283 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1284 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1285
1286 dev = nfc_get_device(idx);
1287 if (!dev)
1288 return -ENODEV;
1289
1290 rc = nfc_enable_se(dev, se_idx);
1291
1292 nfc_put_device(dev);
1293 return rc;
1294}
1295
1296static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1297{
1298 struct nfc_dev *dev;
1299 int rc;
1300 u32 idx, se_idx;
1301
1302 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1303 !info->attrs[NFC_ATTR_SE_INDEX])
1304 return -EINVAL;
1305
1306 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1307 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1308
1309 dev = nfc_get_device(idx);
1310 if (!dev)
1311 return -ENODEV;
1312
1313 rc = nfc_disable_se(dev, se_idx);
1314
1315 nfc_put_device(dev);
1316 return rc;
1317}
1318
Samuel Ortizac22ac42013-07-24 18:10:50 +02001319static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1320 u32 portid, u32 seq,
1321 struct netlink_callback *cb,
1322 int flags)
1323{
1324 void *hdr;
1325 struct nfc_se *se, *n;
1326
1327 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1328 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1329 NFC_CMD_GET_SE);
1330 if (!hdr)
1331 goto nla_put_failure;
1332
1333 if (cb)
Michal Kubecek0a833c22017-11-15 13:09:32 +01001334 genl_dump_check_consistent(cb, hdr);
Samuel Ortizac22ac42013-07-24 18:10:50 +02001335
1336 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1337 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1338 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1339 goto nla_put_failure;
1340
Johannes Berg053c0952015-01-16 22:09:00 +01001341 genlmsg_end(msg, hdr);
Samuel Ortizac22ac42013-07-24 18:10:50 +02001342 }
1343
1344 return 0;
1345
1346nla_put_failure:
1347 genlmsg_cancel(msg, hdr);
1348 return -EMSGSIZE;
1349}
1350
1351static int nfc_genl_dump_ses(struct sk_buff *skb,
1352 struct netlink_callback *cb)
1353{
1354 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1355 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1356 bool first_call = false;
1357
1358 if (!iter) {
1359 first_call = true;
1360 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1361 if (!iter)
1362 return -ENOMEM;
1363 cb->args[0] = (long) iter;
1364 }
1365
1366 mutex_lock(&nfc_devlist_mutex);
1367
1368 cb->seq = nfc_devlist_generation;
1369
1370 if (first_call) {
1371 nfc_device_iter_init(iter);
1372 dev = nfc_device_iter_next(iter);
1373 }
1374
1375 while (dev) {
1376 int rc;
1377
1378 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1379 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1380 if (rc < 0)
1381 break;
1382
1383 dev = nfc_device_iter_next(iter);
1384 }
1385
1386 mutex_unlock(&nfc_devlist_mutex);
1387
1388 cb->args[1] = (long) dev;
1389
1390 return skb->len;
1391}
1392
1393static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1394{
1395 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1396
Krzysztof Kozlowski4cd83712021-12-09 09:13:07 +01001397 if (iter) {
1398 nfc_device_iter_exit(iter);
1399 kfree(iter);
1400 }
Samuel Ortizac22ac42013-07-24 18:10:50 +02001401
1402 return 0;
1403}
1404
Christophe Ricardcd96db62014-12-02 21:27:51 +01001405static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1406 u8 *apdu, size_t apdu_length,
1407 se_io_cb_t cb, void *cb_context)
1408{
1409 struct nfc_se *se;
1410 int rc;
1411
1412 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1413
1414 device_lock(&dev->dev);
1415
1416 if (!device_is_registered(&dev->dev)) {
1417 rc = -ENODEV;
1418 goto error;
1419 }
1420
1421 if (!dev->dev_up) {
1422 rc = -ENODEV;
1423 goto error;
1424 }
1425
1426 if (!dev->ops->se_io) {
1427 rc = -EOPNOTSUPP;
1428 goto error;
1429 }
1430
1431 se = nfc_find_se(dev, se_idx);
1432 if (!se) {
1433 rc = -EINVAL;
1434 goto error;
1435 }
1436
1437 if (se->state != NFC_SE_ENABLED) {
1438 rc = -ENODEV;
1439 goto error;
1440 }
1441
1442 rc = dev->ops->se_io(dev, se_idx, apdu,
1443 apdu_length, cb, cb_context);
1444
1445error:
1446 device_unlock(&dev->dev);
1447 return rc;
1448}
1449
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001450struct se_io_ctx {
1451 u32 dev_idx;
1452 u32 se_idx;
1453};
1454
Samuel Ortizddc1a702013-10-07 14:18:44 +02001455static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001456{
1457 struct se_io_ctx *ctx = context;
1458 struct sk_buff *msg;
1459 void *hdr;
1460
1461 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1462 if (!msg) {
1463 kfree(ctx);
1464 return;
1465 }
1466
1467 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1468 NFC_CMD_SE_IO);
1469 if (!hdr)
1470 goto free_msg;
1471
1472 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1473 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1474 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1475 goto nla_put_failure;
1476
1477 genlmsg_end(msg, hdr);
1478
Johannes Berg2a94fe42013-11-19 15:19:39 +01001479 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001480
1481 kfree(ctx);
1482
1483 return;
1484
1485nla_put_failure:
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001486free_msg:
1487 nlmsg_free(msg);
1488 kfree(ctx);
1489
1490 return;
1491}
1492
1493static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1494{
1495 struct nfc_dev *dev;
1496 struct se_io_ctx *ctx;
1497 u32 dev_idx, se_idx;
1498 u8 *apdu;
1499 size_t apdu_len;
1500
1501 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1502 !info->attrs[NFC_ATTR_SE_INDEX] ||
1503 !info->attrs[NFC_ATTR_SE_APDU])
1504 return -EINVAL;
1505
1506 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1507 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1508
1509 dev = nfc_get_device(dev_idx);
1510 if (!dev)
1511 return -ENODEV;
1512
1513 if (!dev->ops || !dev->ops->se_io)
1514 return -ENOTSUPP;
1515
1516 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1517 if (apdu_len == 0)
1518 return -EINVAL;
1519
1520 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1521 if (!apdu)
1522 return -EINVAL;
1523
1524 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1525 if (!ctx)
1526 return -ENOMEM;
1527
1528 ctx->dev_idx = dev_idx;
1529 ctx->se_idx = se_idx;
1530
Christophe Ricardcd96db62014-12-02 21:27:51 +01001531 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001532}
1533
Samuel Ortiz9e580952014-10-14 02:19:46 +02001534static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1535 struct genl_info *info)
1536{
1537 struct nfc_dev *dev;
Krzysztof Kozlowski15944ad2021-07-24 23:49:23 +02001538 const struct nfc_vendor_cmd *cmd;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001539 u32 dev_idx, vid, subcmd;
1540 u8 *data;
1541 size_t data_len;
Christophe Ricard29e76922015-08-19 21:26:43 +02001542 int i, err;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001543
1544 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1545 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1546 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1547 return -EINVAL;
1548
1549 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1550 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1551 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1552
1553 dev = nfc_get_device(dev_idx);
1554 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1555 return -ENODEV;
1556
Christophe Ricardfe202fe2015-08-14 22:33:40 +02001557 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1558 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
Samuel Ortiz9e580952014-10-14 02:19:46 +02001559 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1560 if (data_len == 0)
1561 return -EINVAL;
1562 } else {
Christophe Ricardadca3c32015-08-17 08:33:43 +02001563 data = NULL;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001564 data_len = 0;
1565 }
1566
1567 for (i = 0; i < dev->n_vendor_cmds; i++) {
1568 cmd = &dev->vendor_cmds[i];
1569
1570 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1571 continue;
1572
Christophe Ricard29e76922015-08-19 21:26:43 +02001573 dev->cur_cmd_info = info;
1574 err = cmd->doit(dev, data, data_len);
1575 dev->cur_cmd_info = NULL;
1576 return err;
Samuel Ortiz9e580952014-10-14 02:19:46 +02001577 }
1578
1579 return -EOPNOTSUPP;
1580}
1581
Christophe Ricard29e76922015-08-19 21:26:43 +02001582/* message building helper */
1583static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1584 int flags, u8 cmd)
1585{
1586 /* since there is no private header just add the generic one */
1587 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1588}
1589
1590static struct sk_buff *
1591__nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1592 u32 portid, u32 seq,
1593 enum nfc_attrs attr,
1594 u32 oui, u32 subcmd, gfp_t gfp)
1595{
1596 struct sk_buff *skb;
1597 void *hdr;
1598
1599 skb = nlmsg_new(approxlen + 100, gfp);
1600 if (!skb)
1601 return NULL;
1602
1603 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1604 if (!hdr) {
1605 kfree_skb(skb);
1606 return NULL;
1607 }
1608
1609 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1610 goto nla_put_failure;
1611 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1612 goto nla_put_failure;
1613 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1614 goto nla_put_failure;
1615
1616 ((void **)skb->cb)[0] = dev;
1617 ((void **)skb->cb)[1] = hdr;
1618
1619 return skb;
1620
1621nla_put_failure:
1622 kfree_skb(skb);
1623 return NULL;
1624}
1625
1626struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1627 enum nfc_attrs attr,
1628 u32 oui, u32 subcmd,
1629 int approxlen)
1630{
1631 if (WARN_ON(!dev->cur_cmd_info))
1632 return NULL;
1633
1634 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1635 dev->cur_cmd_info->snd_portid,
1636 dev->cur_cmd_info->snd_seq, attr,
1637 oui, subcmd, GFP_KERNEL);
1638}
1639EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1640
1641int nfc_vendor_cmd_reply(struct sk_buff *skb)
1642{
1643 struct nfc_dev *dev = ((void **)skb->cb)[0];
1644 void *hdr = ((void **)skb->cb)[1];
1645
1646 /* clear CB data for netlink core to own from now on */
1647 memset(skb->cb, 0, sizeof(skb->cb));
1648
1649 if (WARN_ON(!dev->cur_cmd_info)) {
1650 kfree_skb(skb);
1651 return -EINVAL;
1652 }
1653
1654 genlmsg_end(skb, hdr);
1655 return genlmsg_reply(skb, dev->cur_cmd_info);
1656}
1657EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1658
Johannes Berg4534de82013-11-14 17:14:46 +01001659static const struct genl_ops nfc_genl_ops[] = {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001660 {
1661 .cmd = NFC_CMD_GET_DEVICE,
Johannes Bergef6243a2019-04-26 14:07:31 +02001662 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001663 .doit = nfc_genl_get_device,
1664 .dumpit = nfc_genl_dump_devices,
1665 .done = nfc_genl_dump_devices_done,
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001666 },
1667 {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001668 .cmd = NFC_CMD_DEV_UP,
Johannes Bergef6243a2019-04-26 14:07:31 +02001669 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001670 .doit = nfc_genl_dev_up,
Lin Maaedddb42021-11-02 16:10:21 +08001671 .flags = GENL_ADMIN_PERM,
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001672 },
1673 {
1674 .cmd = NFC_CMD_DEV_DOWN,
Johannes Bergef6243a2019-04-26 14:07:31 +02001675 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001676 .doit = nfc_genl_dev_down,
Lin Maaedddb42021-11-02 16:10:21 +08001677 .flags = GENL_ADMIN_PERM,
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001678 },
1679 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001680 .cmd = NFC_CMD_START_POLL,
Johannes Bergef6243a2019-04-26 14:07:31 +02001681 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001682 .doit = nfc_genl_start_poll,
Lin Maaedddb42021-11-02 16:10:21 +08001683 .flags = GENL_ADMIN_PERM,
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001684 },
1685 {
1686 .cmd = NFC_CMD_STOP_POLL,
Johannes Bergef6243a2019-04-26 14:07:31 +02001687 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001688 .doit = nfc_genl_stop_poll,
Lin Maaedddb42021-11-02 16:10:21 +08001689 .flags = GENL_ADMIN_PERM,
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001690 },
1691 {
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001692 .cmd = NFC_CMD_DEP_LINK_UP,
Johannes Bergef6243a2019-04-26 14:07:31 +02001693 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001694 .doit = nfc_genl_dep_link_up,
Lin Maaedddb42021-11-02 16:10:21 +08001695 .flags = GENL_ADMIN_PERM,
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001696 },
1697 {
1698 .cmd = NFC_CMD_DEP_LINK_DOWN,
Johannes Bergef6243a2019-04-26 14:07:31 +02001699 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001700 .doit = nfc_genl_dep_link_down,
Lin Maaedddb42021-11-02 16:10:21 +08001701 .flags = GENL_ADMIN_PERM,
Samuel Ortiz1ed28f62011-12-14 16:43:09 +01001702 },
1703 {
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001704 .cmd = NFC_CMD_GET_TARGET,
Jiri Pirko4495af32019-10-05 20:04:38 +02001705 .validate = GENL_DONT_VALIDATE_STRICT |
1706 GENL_DONT_VALIDATE_DUMP_STRICT,
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001707 .dumpit = nfc_genl_dump_targets,
1708 .done = nfc_genl_dump_targets_done,
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001709 },
Thierry Escande52feb442012-10-17 14:43:39 +02001710 {
1711 .cmd = NFC_CMD_LLC_GET_PARAMS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001712 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thierry Escande52feb442012-10-17 14:43:39 +02001713 .doit = nfc_genl_llc_get_params,
Thierry Escande52feb442012-10-17 14:43:39 +02001714 },
1715 {
1716 .cmd = NFC_CMD_LLC_SET_PARAMS,
Johannes Bergef6243a2019-04-26 14:07:31 +02001717 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thierry Escande52feb442012-10-17 14:43:39 +02001718 .doit = nfc_genl_llc_set_params,
Lin Maaedddb42021-11-02 16:10:21 +08001719 .flags = GENL_ADMIN_PERM,
Thierry Escande52feb442012-10-17 14:43:39 +02001720 },
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001721 {
1722 .cmd = NFC_CMD_LLC_SDREQ,
Johannes Bergef6243a2019-04-26 14:07:31 +02001723 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001724 .doit = nfc_genl_llc_sdreq,
Lin Maaedddb42021-11-02 16:10:21 +08001725 .flags = GENL_ADMIN_PERM,
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001726 },
Eric Lapuyade9674da82013-04-29 17:13:27 +02001727 {
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001728 .cmd = NFC_CMD_FW_DOWNLOAD,
Johannes Bergef6243a2019-04-26 14:07:31 +02001729 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Samuel Ortiz9ea71872013-07-31 01:19:43 +02001730 .doit = nfc_genl_fw_download,
Lin Maaedddb42021-11-02 16:10:21 +08001731 .flags = GENL_ADMIN_PERM,
Eric Lapuyade9674da82013-04-29 17:13:27 +02001732 },
Samuel Ortizbe085652013-05-10 17:07:32 +02001733 {
1734 .cmd = NFC_CMD_ENABLE_SE,
Johannes Bergef6243a2019-04-26 14:07:31 +02001735 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Samuel Ortizbe085652013-05-10 17:07:32 +02001736 .doit = nfc_genl_enable_se,
Lin Maaedddb42021-11-02 16:10:21 +08001737 .flags = GENL_ADMIN_PERM,
Samuel Ortizbe085652013-05-10 17:07:32 +02001738 },
1739 {
1740 .cmd = NFC_CMD_DISABLE_SE,
Johannes Bergef6243a2019-04-26 14:07:31 +02001741 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Samuel Ortizbe085652013-05-10 17:07:32 +02001742 .doit = nfc_genl_disable_se,
Lin Maaedddb42021-11-02 16:10:21 +08001743 .flags = GENL_ADMIN_PERM,
Samuel Ortizbe085652013-05-10 17:07:32 +02001744 },
Samuel Ortizac22ac42013-07-24 18:10:50 +02001745 {
1746 .cmd = NFC_CMD_GET_SE,
Johannes Bergef6243a2019-04-26 14:07:31 +02001747 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Samuel Ortizac22ac42013-07-24 18:10:50 +02001748 .dumpit = nfc_genl_dump_ses,
1749 .done = nfc_genl_dump_ses_done,
Samuel Ortizac22ac42013-07-24 18:10:50 +02001750 },
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001751 {
1752 .cmd = NFC_CMD_SE_IO,
Johannes Bergef6243a2019-04-26 14:07:31 +02001753 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001754 .doit = nfc_genl_se_io,
Lin Maaedddb42021-11-02 16:10:21 +08001755 .flags = GENL_ADMIN_PERM,
Samuel Ortiz5ce3f322013-08-28 00:47:24 +02001756 },
Christophe Ricard3682f492014-12-02 21:27:50 +01001757 {
1758 .cmd = NFC_CMD_ACTIVATE_TARGET,
Johannes Bergef6243a2019-04-26 14:07:31 +02001759 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Christophe Ricard3682f492014-12-02 21:27:50 +01001760 .doit = nfc_genl_activate_target,
Lin Maaedddb42021-11-02 16:10:21 +08001761 .flags = GENL_ADMIN_PERM,
Christophe Ricard3682f492014-12-02 21:27:50 +01001762 },
Samuel Ortiz9e580952014-10-14 02:19:46 +02001763 {
1764 .cmd = NFC_CMD_VENDOR,
Johannes Bergef6243a2019-04-26 14:07:31 +02001765 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Samuel Ortiz9e580952014-10-14 02:19:46 +02001766 .doit = nfc_genl_vendor_cmd,
Lin Maaedddb42021-11-02 16:10:21 +08001767 .flags = GENL_ADMIN_PERM,
Samuel Ortiz9e580952014-10-14 02:19:46 +02001768 },
Mark Greer4d63adf2017-06-15 20:34:22 -07001769 {
1770 .cmd = NFC_CMD_DEACTIVATE_TARGET,
Johannes Bergef6243a2019-04-26 14:07:31 +02001771 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Mark Greer4d63adf2017-06-15 20:34:22 -07001772 .doit = nfc_genl_deactivate_target,
Lin Maaedddb42021-11-02 16:10:21 +08001773 .flags = GENL_ADMIN_PERM,
Mark Greer4d63adf2017-06-15 20:34:22 -07001774 },
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001775};
1776
Johannes Berg56989f62016-10-24 14:40:05 +02001777static struct genl_family nfc_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001778 .hdrsize = 0,
1779 .name = NFC_GENL_NAME,
1780 .version = NFC_GENL_VERSION,
1781 .maxattr = NFC_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01001782 .policy = nfc_genl_policy,
Johannes Berg489111e2016-10-24 14:40:03 +02001783 .module = THIS_MODULE,
1784 .ops = nfc_genl_ops,
1785 .n_ops = ARRAY_SIZE(nfc_genl_ops),
1786 .mcgrps = nfc_genl_mcgrps,
1787 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1788};
1789
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001790
1791struct urelease_work {
1792 struct work_struct w;
Richard Weinberger65bc4f92015-04-13 00:52:36 +02001793 u32 portid;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001794};
1795
1796static void nfc_urelease_event_work(struct work_struct *work)
1797{
1798 struct urelease_work *w = container_of(work, struct urelease_work, w);
1799 struct class_dev_iter iter;
1800 struct nfc_dev *dev;
1801
John W. Linvillec4876062012-09-28 11:11:16 -04001802 pr_debug("portid %d\n", w->portid);
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001803
1804 mutex_lock(&nfc_devlist_mutex);
1805
1806 nfc_device_iter_init(&iter);
1807 dev = nfc_device_iter_next(&iter);
1808
1809 while (dev) {
1810 mutex_lock(&dev->genl_data.genl_data_mutex);
1811
John W. Linvillec4876062012-09-28 11:11:16 -04001812 if (dev->genl_data.poll_req_portid == w->portid) {
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001813 nfc_stop_poll(dev);
John W. Linvillec4876062012-09-28 11:11:16 -04001814 dev->genl_data.poll_req_portid = 0;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001815 }
1816
1817 mutex_unlock(&dev->genl_data.genl_data_mutex);
1818
1819 dev = nfc_device_iter_next(&iter);
1820 }
1821
1822 nfc_device_iter_exit(&iter);
1823
1824 mutex_unlock(&nfc_devlist_mutex);
1825
1826 kfree(w);
1827}
1828
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001829static int nfc_genl_rcv_nl_event(struct notifier_block *this,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001830 unsigned long event, void *ptr)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001831{
1832 struct netlink_notify *n = ptr;
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001833 struct urelease_work *w;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001834
1835 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1836 goto out;
1837
Eric W. Biederman15e47302012-09-07 20:12:54 +00001838 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001839
Szymon Janc3c0cc8a2012-09-26 14:17:12 +02001840 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1841 if (w) {
Geliang Tang32d91b42021-01-15 12:14:53 +08001842 INIT_WORK(&w->w, nfc_urelease_event_work);
John W. Linvillec4876062012-09-28 11:11:16 -04001843 w->portid = n->portid;
Geliang Tang32d91b42021-01-15 12:14:53 +08001844 schedule_work(&w->w);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001845 }
1846
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001847out:
1848 return NOTIFY_DONE;
1849}
1850
1851void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1852{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001853 genl_data->poll_req_portid = 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001854 mutex_init(&genl_data->genl_data_mutex);
1855}
1856
1857void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1858{
1859 mutex_destroy(&genl_data->genl_data_mutex);
1860}
1861
1862static struct notifier_block nl_notifier = {
1863 .notifier_call = nfc_genl_rcv_nl_event,
1864};
1865
1866/**
1867 * nfc_genl_init() - Initialize netlink interface
1868 *
1869 * This initialization function registers the nfc netlink family.
1870 */
1871int __init nfc_genl_init(void)
1872{
1873 int rc;
1874
Johannes Berg489111e2016-10-24 14:40:03 +02001875 rc = genl_register_family(&nfc_genl_family);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001876 if (rc)
1877 return rc;
1878
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001879 netlink_register_notifier(&nl_notifier);
1880
Johannes Berg2a94fe42013-11-19 15:19:39 +01001881 return 0;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001882}
1883
1884/**
1885 * nfc_genl_exit() - Deinitialize netlink interface
1886 *
1887 * This exit function unregisters the nfc netlink family.
1888 */
1889void nfc_genl_exit(void)
1890{
1891 netlink_unregister_notifier(&nl_notifier);
1892 genl_unregister_family(&nfc_genl_family);
1893}