xref: /linux/net/nfc/netlink.c (revision 9ffd93852fc66f2bdf61a574db107f106e9fb2ff)
1 /*
2  * Copyright (C) 2011 Instituto Nokia de Tecnologia
3  *
4  * Authors:
5  *    Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6  *    Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7  *
8  * Vendor commands implementation based on net/wireless/nl80211.c
9  * which is:
10  *
11  * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
12  * Copyright 2013-2014  Intel Mobile Communications GmbH
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
29 
30 #include <net/genetlink.h>
31 #include <linux/nfc.h>
32 #include <linux/slab.h>
33 
34 #include "nfc.h"
35 #include "llcp.h"
36 
37 static const struct genl_multicast_group nfc_genl_mcgrps[] = {
38 	{ .name = NFC_GENL_MCAST_EVENT_NAME, },
39 };
40 
41 static struct genl_family nfc_genl_family;
42 static 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 },
47 	[NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
48 	[NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
49 	[NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
50 	[NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
51 	[NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
52 	[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 },
55 	[NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
56 	[NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
57 				     .len = NFC_FIRMWARE_NAME_MAXSIZE },
58 	[NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
59 	[NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
60 
61 };
62 
63 static 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 },
66 };
67 
68 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
69 				struct netlink_callback *cb, int flags)
70 {
71 	void *hdr;
72 
73 	hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
74 			  &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
75 	if (!hdr)
76 		return -EMSGSIZE;
77 
78 	genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
79 
80 	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;
97 
98 	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 
106 	genlmsg_end(msg, hdr);
107 	return 0;
108 
109 nla_put_failure:
110 	genlmsg_cancel(msg, hdr);
111 	return -EMSGSIZE;
112 }
113 
114 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
115 {
116 	struct nlattr **attrbuf = genl_family_attrbuf(&nfc_genl_family);
117 	struct nfc_dev *dev;
118 	int rc;
119 	u32 idx;
120 
121 	rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
122 			 attrbuf, nfc_genl_family.maxattr, nfc_genl_policy,
123 			 NULL);
124 	if (rc < 0)
125 		return ERR_PTR(rc);
126 
127 	if (!attrbuf[NFC_ATTR_DEVICE_INDEX])
128 		return ERR_PTR(-EINVAL);
129 
130 	idx = nla_get_u32(attrbuf[NFC_ATTR_DEVICE_INDEX]);
131 
132 	dev = nfc_get_device(idx);
133 	if (!dev)
134 		return ERR_PTR(-ENODEV);
135 
136 	return dev;
137 }
138 
139 static int nfc_genl_dump_targets(struct sk_buff *skb,
140 				 struct netlink_callback *cb)
141 {
142 	int i = cb->args[0];
143 	struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
144 	int rc;
145 
146 	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 
154 	device_lock(&dev->dev);
155 
156 	cb->seq = dev->targets_generation;
157 
158 	while (i < dev->n_targets) {
159 		rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
160 					  NLM_F_MULTI);
161 		if (rc < 0)
162 			break;
163 
164 		i++;
165 	}
166 
167 	device_unlock(&dev->dev);
168 
169 	cb->args[0] = i;
170 
171 	return skb->len;
172 }
173 
174 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
175 {
176 	struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
177 
178 	if (dev)
179 		nfc_put_device(dev);
180 
181 	return 0;
182 }
183 
184 int nfc_genl_targets_found(struct nfc_dev *dev)
185 {
186 	struct sk_buff *msg;
187 	void *hdr;
188 
189 	dev->genl_data.poll_req_portid = 0;
190 
191 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
192 	if (!msg)
193 		return -ENOMEM;
194 
195 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
196 			  NFC_EVENT_TARGETS_FOUND);
197 	if (!hdr)
198 		goto free_msg;
199 
200 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
201 		goto nla_put_failure;
202 
203 	genlmsg_end(msg, hdr);
204 
205 	return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
206 
207 nla_put_failure:
208 	genlmsg_cancel(msg, hdr);
209 free_msg:
210 	nlmsg_free(msg);
211 	return -EMSGSIZE;
212 }
213 
214 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
215 {
216 	struct sk_buff *msg;
217 	void *hdr;
218 
219 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
220 	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 
228 	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;
231 
232 	genlmsg_end(msg, hdr);
233 
234 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
235 
236 	return 0;
237 
238 nla_put_failure:
239 	genlmsg_cancel(msg, hdr);
240 free_msg:
241 	nlmsg_free(msg);
242 	return -EMSGSIZE;
243 }
244 
245 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
246 {
247 	struct sk_buff *msg;
248 	void *hdr;
249 
250 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
251 	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 
266 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
267 
268 	return 0;
269 
270 nla_put_failure:
271 	genlmsg_cancel(msg, hdr);
272 free_msg:
273 	nlmsg_free(msg);
274 	return -EMSGSIZE;
275 }
276 
277 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
278 {
279 	struct sk_buff *msg;
280 	void *hdr;
281 
282 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
283 	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 
296 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
297 
298 	return 0;
299 
300 nla_put_failure:
301 	genlmsg_cancel(msg, hdr);
302 free_msg:
303 	nlmsg_free(msg);
304 	return -EMSGSIZE;
305 }
306 
307 static 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 
318 int nfc_genl_device_added(struct nfc_dev *dev)
319 {
320 	struct sk_buff *msg;
321 	void *hdr;
322 
323 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
324 	if (!msg)
325 		return -ENOMEM;
326 
327 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
328 			  NFC_EVENT_DEVICE_ADDED);
329 	if (!hdr)
330 		goto free_msg;
331 
332 	if (nfc_genl_setup_device_added(dev, msg))
333 		goto nla_put_failure;
334 
335 	genlmsg_end(msg, hdr);
336 
337 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
338 
339 	return 0;
340 
341 nla_put_failure:
342 	genlmsg_cancel(msg, hdr);
343 free_msg:
344 	nlmsg_free(msg);
345 	return -EMSGSIZE;
346 }
347 
348 int nfc_genl_device_removed(struct nfc_dev *dev)
349 {
350 	struct sk_buff *msg;
351 	void *hdr;
352 
353 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
354 	if (!msg)
355 		return -ENOMEM;
356 
357 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
358 			  NFC_EVENT_DEVICE_REMOVED);
359 	if (!hdr)
360 		goto free_msg;
361 
362 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
363 		goto nla_put_failure;
364 
365 	genlmsg_end(msg, hdr);
366 
367 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
368 
369 	return 0;
370 
371 nla_put_failure:
372 	genlmsg_cancel(msg, hdr);
373 free_msg:
374 	nlmsg_free(msg);
375 	return -EMSGSIZE;
376 }
377 
378 int 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 
433 	return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
434 
435 nla_put_failure:
436 	genlmsg_cancel(msg, hdr);
437 
438 free_msg:
439 	nlmsg_free(msg);
440 
441 	nfc_llcp_free_sdp_tlv_list(sdres_list);
442 
443 	return rc;
444 }
445 
446 int 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 
467 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
468 
469 	return 0;
470 
471 nla_put_failure:
472 	genlmsg_cancel(msg, hdr);
473 free_msg:
474 	nlmsg_free(msg);
475 	return -EMSGSIZE;
476 }
477 
478 int 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 
498 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
499 
500 	return 0;
501 
502 nla_put_failure:
503 	genlmsg_cancel(msg, hdr);
504 free_msg:
505 	nlmsg_free(msg);
506 	return -EMSGSIZE;
507 }
508 
509 int 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 
547 nla_put_failure:
548 	genlmsg_cancel(msg, hdr);
549 free_msg:
550 	/* evt_transaction is no more used */
551 	devm_kfree(&dev->dev, evt_transaction);
552 	nlmsg_free(msg);
553 	return -EMSGSIZE;
554 }
555 
556 int 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 
586 nla_put_failure:
587 	genlmsg_cancel(msg, hdr);
588 free_msg:
589 	nlmsg_free(msg);
590 	return -EMSGSIZE;
591 }
592 
593 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
594 				u32 portid, u32 seq,
595 				struct netlink_callback *cb,
596 				int flags)
597 {
598 	void *hdr;
599 
600 	hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
601 			  NFC_CMD_GET_DEVICE);
602 	if (!hdr)
603 		return -EMSGSIZE;
604 
605 	if (cb)
606 		genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
607 
608 	if (nfc_genl_setup_device_added(dev, msg))
609 		goto nla_put_failure;
610 
611 	genlmsg_end(msg, hdr);
612 	return 0;
613 
614 nla_put_failure:
615 	genlmsg_cancel(msg, hdr);
616 	return -EMSGSIZE;
617 }
618 
619 static int nfc_genl_dump_devices(struct sk_buff *skb,
620 				 struct netlink_callback *cb)
621 {
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 
626 	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 
646 		rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
647 					  cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
648 		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 
661 static 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 
665 	nfc_device_iter_exit(iter);
666 	kfree(iter);
667 
668 	return 0;
669 }
670 
671 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
672 			       u8 comm_mode, u8 rf_mode)
673 {
674 	struct sk_buff *msg;
675 	void *hdr;
676 
677 	pr_debug("DEP link is up\n");
678 
679 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
680 	if (!msg)
681 		return -ENOMEM;
682 
683 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
684 	if (!hdr)
685 		goto free_msg;
686 
687 	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;
695 
696 	genlmsg_end(msg, hdr);
697 
698 	dev->dep_link_up = true;
699 
700 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
701 
702 	return 0;
703 
704 nla_put_failure:
705 	genlmsg_cancel(msg, hdr);
706 free_msg:
707 	nlmsg_free(msg);
708 	return -EMSGSIZE;
709 }
710 
711 int 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 
718 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
719 	if (!msg)
720 		return -ENOMEM;
721 
722 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
723 			  NFC_CMD_DEP_LINK_DOWN);
724 	if (!hdr)
725 		goto free_msg;
726 
727 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
728 		goto nla_put_failure;
729 
730 	genlmsg_end(msg, hdr);
731 
732 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
733 
734 	return 0;
735 
736 nla_put_failure:
737 	genlmsg_cancel(msg, hdr);
738 free_msg:
739 	nlmsg_free(msg);
740 	return -EMSGSIZE;
741 }
742 
743 static 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 
750 	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 
759 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
760 	if (!msg) {
761 		rc = -ENOMEM;
762 		goto out_putdev;
763 	}
764 
765 	rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
766 				  NULL, 0);
767 	if (rc < 0)
768 		goto out_free;
769 
770 	nfc_put_device(dev);
771 
772 	return genlmsg_reply(msg, info);
773 
774 out_free:
775 	nlmsg_free(msg);
776 out_putdev:
777 	nfc_put_device(dev);
778 	return rc;
779 }
780 
781 static 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 
787 	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 
802 static 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 
808 	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 
823 static 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;
828 	u32 im_protocols = 0, tm_protocols = 0;
829 
830 	pr_debug("Poll start\n");
831 
832 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
833 	    ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
834 	      !info->attrs[NFC_ATTR_PROTOCOLS]) &&
835 	      !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
836 		return -EINVAL;
837 
838 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
839 
840 	if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
841 		tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
842 
843 	if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
844 		im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
845 	else if (info->attrs[NFC_ATTR_PROTOCOLS])
846 		im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
847 
848 	dev = nfc_get_device(idx);
849 	if (!dev)
850 		return -ENODEV;
851 
852 	mutex_lock(&dev->genl_data.genl_data_mutex);
853 
854 	rc = nfc_start_poll(dev, im_protocols, tm_protocols);
855 	if (!rc)
856 		dev->genl_data.poll_req_portid = info->snd_portid;
857 
858 	mutex_unlock(&dev->genl_data.genl_data_mutex);
859 
860 	nfc_put_device(dev);
861 	return rc;
862 }
863 
864 static 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 
870 	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 
879 	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 
888 	mutex_lock(&dev->genl_data.genl_data_mutex);
889 
890 	if (dev->genl_data.poll_req_portid != info->snd_portid) {
891 		rc = -EBUSY;
892 		goto out;
893 	}
894 
895 	rc = nfc_stop_poll(dev);
896 	dev->genl_data.poll_req_portid = 0;
897 
898 out:
899 	mutex_unlock(&dev->genl_data.genl_data_mutex);
900 	nfc_put_device(dev);
901 	return rc;
902 }
903 
904 static 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 	    !info->attrs[NFC_ATTR_TARGET_INDEX] ||
912 	    !info->attrs[NFC_ATTR_PROTOCOLS])
913 		return -EINVAL;
914 
915 	device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
916 
917 	dev = nfc_get_device(device_idx);
918 	if (!dev)
919 		return -ENODEV;
920 
921 	target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
922 	protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
923 
924 	nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
925 	rc = nfc_activate_target(dev, target_idx, protocol);
926 
927 	nfc_put_device(dev);
928 	return rc;
929 }
930 
931 static 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;
936 	u8 comm;
937 
938 	pr_debug("DEP link up\n");
939 
940 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
941 	    !info->attrs[NFC_ATTR_COMM_MODE])
942 		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]);
951 
952 	if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
953 		return -EINVAL;
954 
955 	dev = nfc_get_device(idx);
956 	if (!dev)
957 		return -ENODEV;
958 
959 	rc = nfc_dep_link_up(dev, tgt_idx, comm);
960 
961 	nfc_put_device(dev);
962 
963 	return rc;
964 }
965 
966 static 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 
972 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
973 		return -EINVAL;
974 
975 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
976 
977 	dev = nfc_get_device(idx);
978 	if (!dev)
979 		return -ENODEV;
980 
981 	rc = nfc_dep_link_down(dev);
982 
983 	nfc_put_device(dev);
984 	return rc;
985 }
986 
987 static int nfc_genl_send_params(struct sk_buff *msg,
988 				struct nfc_llcp_local *local,
989 				u32 portid, u32 seq)
990 {
991 	void *hdr;
992 
993 	hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
994 			  NFC_CMD_LLC_GET_PARAMS);
995 	if (!hdr)
996 		return -EMSGSIZE;
997 
998 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
999 	    nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
1000 	    nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
1001 	    nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1002 		goto nla_put_failure;
1003 
1004 	genlmsg_end(msg, hdr);
1005 	return 0;
1006 
1007 nla_put_failure:
1008 
1009 	genlmsg_cancel(msg, hdr);
1010 	return -EMSGSIZE;
1011 }
1012 
1013 static 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 
1021 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1022 		return -EINVAL;
1023 
1024 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1025 
1026 	dev = nfc_get_device(idx);
1027 	if (!dev)
1028 		return -ENODEV;
1029 
1030 	device_lock(&dev->dev);
1031 
1032 	local = nfc_llcp_find_local(dev);
1033 	if (!local) {
1034 		rc = -ENODEV;
1035 		goto exit;
1036 	}
1037 
1038 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1039 	if (!msg) {
1040 		rc = -ENOMEM;
1041 		goto exit;
1042 	}
1043 
1044 	rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1045 
1046 exit:
1047 	device_unlock(&dev->dev);
1048 
1049 	nfc_put_device(dev);
1050 
1051 	if (rc < 0) {
1052 		if (msg)
1053 			nlmsg_free(msg);
1054 
1055 		return rc;
1056 	}
1057 
1058 	return genlmsg_reply(msg, info);
1059 }
1060 
1061 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1062 {
1063 	struct nfc_dev *dev;
1064 	struct nfc_llcp_local *local;
1065 	u8 rw = 0;
1066 	u16 miux = 0;
1067 	u32 idx;
1068 	int rc = 0;
1069 
1070 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1071 	    (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1072 	     !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1073 	     !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1074 		return -EINVAL;
1075 
1076 	if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1077 		rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1078 
1079 		if (rw > LLCP_MAX_RW)
1080 			return -EINVAL;
1081 	}
1082 
1083 	if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1084 		miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1085 
1086 		if (miux > LLCP_MAX_MIUX)
1087 			return -EINVAL;
1088 	}
1089 
1090 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1091 
1092 	dev = nfc_get_device(idx);
1093 	if (!dev)
1094 		return -ENODEV;
1095 
1096 	device_lock(&dev->dev);
1097 
1098 	local = nfc_llcp_find_local(dev);
1099 	if (!local) {
1100 		nfc_put_device(dev);
1101 		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 
1120 exit:
1121 	device_unlock(&dev->dev);
1122 
1123 	nfc_put_device(dev);
1124 
1125 	return rc;
1126 }
1127 
1128 static 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);
1148 	if (!dev)
1149 		return -ENODEV;
1150 
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) {
1160 		nfc_put_device(dev);
1161 		rc = -ENODEV;
1162 		goto exit;
1163 	}
1164 
1165 	INIT_HLIST_HEAD(&sdreq_list);
1166 
1167 	tlvs_len = 0;
1168 
1169 	nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1170 		rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
1171 				      nfc_sdp_genl_policy, info->extack);
1172 
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);
1208 exit:
1209 	device_unlock(&dev->dev);
1210 
1211 	nfc_put_device(dev);
1212 
1213 	return rc;
1214 }
1215 
1216 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1217 {
1218 	struct nfc_dev *dev;
1219 	int rc;
1220 	u32 idx;
1221 	char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1222 
1223 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1224 		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 
1232 	nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1233 		    sizeof(firmware_name));
1234 
1235 	rc = nfc_fw_download(dev, firmware_name);
1236 
1237 	nfc_put_device(dev);
1238 	return rc;
1239 }
1240 
1241 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1242 			      u32 result)
1243 {
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,
1252 			  NFC_CMD_FW_DOWNLOAD);
1253 	if (!hdr)
1254 		goto free_msg;
1255 
1256 	if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1257 	    nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1258 	    nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1259 		goto nla_put_failure;
1260 
1261 	genlmsg_end(msg, hdr);
1262 
1263 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1264 
1265 	return 0;
1266 
1267 nla_put_failure:
1268 	genlmsg_cancel(msg, hdr);
1269 free_msg:
1270 	nlmsg_free(msg);
1271 	return -EMSGSIZE;
1272 }
1273 
1274 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1275 {
1276 	struct nfc_dev *dev;
1277 	int rc;
1278 	u32 idx, se_idx;
1279 
1280 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1281 	    !info->attrs[NFC_ATTR_SE_INDEX])
1282 		return -EINVAL;
1283 
1284 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1285 	se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1286 
1287 	dev = nfc_get_device(idx);
1288 	if (!dev)
1289 		return -ENODEV;
1290 
1291 	rc = nfc_enable_se(dev, se_idx);
1292 
1293 	nfc_put_device(dev);
1294 	return rc;
1295 }
1296 
1297 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1298 {
1299 	struct nfc_dev *dev;
1300 	int rc;
1301 	u32 idx, se_idx;
1302 
1303 	if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1304 	    !info->attrs[NFC_ATTR_SE_INDEX])
1305 		return -EINVAL;
1306 
1307 	idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1308 	se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1309 
1310 	dev = nfc_get_device(idx);
1311 	if (!dev)
1312 		return -ENODEV;
1313 
1314 	rc = nfc_disable_se(dev, se_idx);
1315 
1316 	nfc_put_device(dev);
1317 	return rc;
1318 }
1319 
1320 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1321 				u32 portid, u32 seq,
1322 				struct netlink_callback *cb,
1323 				int flags)
1324 {
1325 	void *hdr;
1326 	struct nfc_se *se, *n;
1327 
1328 	list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1329 		hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1330 				  NFC_CMD_GET_SE);
1331 		if (!hdr)
1332 			goto nla_put_failure;
1333 
1334 		if (cb)
1335 			genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
1336 
1337 		if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1338 		    nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1339 		    nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1340 			goto nla_put_failure;
1341 
1342 		genlmsg_end(msg, hdr);
1343 	}
1344 
1345 	return 0;
1346 
1347 nla_put_failure:
1348 	genlmsg_cancel(msg, hdr);
1349 	return -EMSGSIZE;
1350 }
1351 
1352 static int nfc_genl_dump_ses(struct sk_buff *skb,
1353 				 struct netlink_callback *cb)
1354 {
1355 	struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1356 	struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1357 	bool first_call = false;
1358 
1359 	if (!iter) {
1360 		first_call = true;
1361 		iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1362 		if (!iter)
1363 			return -ENOMEM;
1364 		cb->args[0] = (long) iter;
1365 	}
1366 
1367 	mutex_lock(&nfc_devlist_mutex);
1368 
1369 	cb->seq = nfc_devlist_generation;
1370 
1371 	if (first_call) {
1372 		nfc_device_iter_init(iter);
1373 		dev = nfc_device_iter_next(iter);
1374 	}
1375 
1376 	while (dev) {
1377 		int rc;
1378 
1379 		rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1380 					  cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1381 		if (rc < 0)
1382 			break;
1383 
1384 		dev = nfc_device_iter_next(iter);
1385 	}
1386 
1387 	mutex_unlock(&nfc_devlist_mutex);
1388 
1389 	cb->args[1] = (long) dev;
1390 
1391 	return skb->len;
1392 }
1393 
1394 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1395 {
1396 	struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1397 
1398 	nfc_device_iter_exit(iter);
1399 	kfree(iter);
1400 
1401 	return 0;
1402 }
1403 
1404 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1405 		     u8 *apdu, size_t apdu_length,
1406 		     se_io_cb_t cb, void *cb_context)
1407 {
1408 	struct nfc_se *se;
1409 	int rc;
1410 
1411 	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1412 
1413 	device_lock(&dev->dev);
1414 
1415 	if (!device_is_registered(&dev->dev)) {
1416 		rc = -ENODEV;
1417 		goto error;
1418 	}
1419 
1420 	if (!dev->dev_up) {
1421 		rc = -ENODEV;
1422 		goto error;
1423 	}
1424 
1425 	if (!dev->ops->se_io) {
1426 		rc = -EOPNOTSUPP;
1427 		goto error;
1428 	}
1429 
1430 	se = nfc_find_se(dev, se_idx);
1431 	if (!se) {
1432 		rc = -EINVAL;
1433 		goto error;
1434 	}
1435 
1436 	if (se->state != NFC_SE_ENABLED) {
1437 		rc = -ENODEV;
1438 		goto error;
1439 	}
1440 
1441 	rc = dev->ops->se_io(dev, se_idx, apdu,
1442 			apdu_length, cb, cb_context);
1443 
1444 error:
1445 	device_unlock(&dev->dev);
1446 	return rc;
1447 }
1448 
1449 struct se_io_ctx {
1450 	u32 dev_idx;
1451 	u32 se_idx;
1452 };
1453 
1454 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1455 {
1456 	struct se_io_ctx *ctx = context;
1457 	struct sk_buff *msg;
1458 	void *hdr;
1459 
1460 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1461 	if (!msg) {
1462 		kfree(ctx);
1463 		return;
1464 	}
1465 
1466 	hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1467 			  NFC_CMD_SE_IO);
1468 	if (!hdr)
1469 		goto free_msg;
1470 
1471 	if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1472 	    nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1473 	    nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1474 		goto nla_put_failure;
1475 
1476 	genlmsg_end(msg, hdr);
1477 
1478 	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1479 
1480 	kfree(ctx);
1481 
1482 	return;
1483 
1484 nla_put_failure:
1485 	genlmsg_cancel(msg, hdr);
1486 free_msg:
1487 	nlmsg_free(msg);
1488 	kfree(ctx);
1489 
1490 	return;
1491 }
1492 
1493 static 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 
1531 	return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1532 }
1533 
1534 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1535 			       struct genl_info *info)
1536 {
1537 	struct nfc_dev *dev;
1538 	struct nfc_vendor_cmd *cmd;
1539 	u32 dev_idx, vid, subcmd;
1540 	u8 *data;
1541 	size_t data_len;
1542 	int i, err;
1543 
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 
1557 	if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1558 		data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1559 		data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1560 		if (data_len == 0)
1561 			return -EINVAL;
1562 	} else {
1563 		data = NULL;
1564 		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 
1573 		dev->cur_cmd_info = info;
1574 		err = cmd->doit(dev, data, data_len);
1575 		dev->cur_cmd_info = NULL;
1576 		return err;
1577 	}
1578 
1579 	return -EOPNOTSUPP;
1580 }
1581 
1582 /* message building helper */
1583 static 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 
1590 static 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 
1621 nla_put_failure:
1622 	kfree_skb(skb);
1623 	return NULL;
1624 }
1625 
1626 struct 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 }
1639 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1640 
1641 int 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 }
1657 EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1658 
1659 static const struct genl_ops nfc_genl_ops[] = {
1660 	{
1661 		.cmd = NFC_CMD_GET_DEVICE,
1662 		.doit = nfc_genl_get_device,
1663 		.dumpit = nfc_genl_dump_devices,
1664 		.done = nfc_genl_dump_devices_done,
1665 		.policy = nfc_genl_policy,
1666 	},
1667 	{
1668 		.cmd = NFC_CMD_DEV_UP,
1669 		.doit = nfc_genl_dev_up,
1670 		.policy = nfc_genl_policy,
1671 	},
1672 	{
1673 		.cmd = NFC_CMD_DEV_DOWN,
1674 		.doit = nfc_genl_dev_down,
1675 		.policy = nfc_genl_policy,
1676 	},
1677 	{
1678 		.cmd = NFC_CMD_START_POLL,
1679 		.doit = nfc_genl_start_poll,
1680 		.policy = nfc_genl_policy,
1681 	},
1682 	{
1683 		.cmd = NFC_CMD_STOP_POLL,
1684 		.doit = nfc_genl_stop_poll,
1685 		.policy = nfc_genl_policy,
1686 	},
1687 	{
1688 		.cmd = NFC_CMD_DEP_LINK_UP,
1689 		.doit = nfc_genl_dep_link_up,
1690 		.policy = nfc_genl_policy,
1691 	},
1692 	{
1693 		.cmd = NFC_CMD_DEP_LINK_DOWN,
1694 		.doit = nfc_genl_dep_link_down,
1695 		.policy = nfc_genl_policy,
1696 	},
1697 	{
1698 		.cmd = NFC_CMD_GET_TARGET,
1699 		.dumpit = nfc_genl_dump_targets,
1700 		.done = nfc_genl_dump_targets_done,
1701 		.policy = nfc_genl_policy,
1702 	},
1703 	{
1704 		.cmd = NFC_CMD_LLC_GET_PARAMS,
1705 		.doit = nfc_genl_llc_get_params,
1706 		.policy = nfc_genl_policy,
1707 	},
1708 	{
1709 		.cmd = NFC_CMD_LLC_SET_PARAMS,
1710 		.doit = nfc_genl_llc_set_params,
1711 		.policy = nfc_genl_policy,
1712 	},
1713 	{
1714 		.cmd = NFC_CMD_LLC_SDREQ,
1715 		.doit = nfc_genl_llc_sdreq,
1716 		.policy = nfc_genl_policy,
1717 	},
1718 	{
1719 		.cmd = NFC_CMD_FW_DOWNLOAD,
1720 		.doit = nfc_genl_fw_download,
1721 		.policy = nfc_genl_policy,
1722 	},
1723 	{
1724 		.cmd = NFC_CMD_ENABLE_SE,
1725 		.doit = nfc_genl_enable_se,
1726 		.policy = nfc_genl_policy,
1727 	},
1728 	{
1729 		.cmd = NFC_CMD_DISABLE_SE,
1730 		.doit = nfc_genl_disable_se,
1731 		.policy = nfc_genl_policy,
1732 	},
1733 	{
1734 		.cmd = NFC_CMD_GET_SE,
1735 		.dumpit = nfc_genl_dump_ses,
1736 		.done = nfc_genl_dump_ses_done,
1737 		.policy = nfc_genl_policy,
1738 	},
1739 	{
1740 		.cmd = NFC_CMD_SE_IO,
1741 		.doit = nfc_genl_se_io,
1742 		.policy = nfc_genl_policy,
1743 	},
1744 	{
1745 		.cmd = NFC_CMD_ACTIVATE_TARGET,
1746 		.doit = nfc_genl_activate_target,
1747 		.policy = nfc_genl_policy,
1748 	},
1749 	{
1750 		.cmd = NFC_CMD_VENDOR,
1751 		.doit = nfc_genl_vendor_cmd,
1752 		.policy = nfc_genl_policy,
1753 	},
1754 };
1755 
1756 static struct genl_family nfc_genl_family __ro_after_init = {
1757 	.hdrsize = 0,
1758 	.name = NFC_GENL_NAME,
1759 	.version = NFC_GENL_VERSION,
1760 	.maxattr = NFC_ATTR_MAX,
1761 	.module = THIS_MODULE,
1762 	.ops = nfc_genl_ops,
1763 	.n_ops = ARRAY_SIZE(nfc_genl_ops),
1764 	.mcgrps = nfc_genl_mcgrps,
1765 	.n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1766 };
1767 
1768 
1769 struct urelease_work {
1770 	struct	work_struct w;
1771 	u32	portid;
1772 };
1773 
1774 static void nfc_urelease_event_work(struct work_struct *work)
1775 {
1776 	struct urelease_work *w = container_of(work, struct urelease_work, w);
1777 	struct class_dev_iter iter;
1778 	struct nfc_dev *dev;
1779 
1780 	pr_debug("portid %d\n", w->portid);
1781 
1782 	mutex_lock(&nfc_devlist_mutex);
1783 
1784 	nfc_device_iter_init(&iter);
1785 	dev = nfc_device_iter_next(&iter);
1786 
1787 	while (dev) {
1788 		mutex_lock(&dev->genl_data.genl_data_mutex);
1789 
1790 		if (dev->genl_data.poll_req_portid == w->portid) {
1791 			nfc_stop_poll(dev);
1792 			dev->genl_data.poll_req_portid = 0;
1793 		}
1794 
1795 		mutex_unlock(&dev->genl_data.genl_data_mutex);
1796 
1797 		dev = nfc_device_iter_next(&iter);
1798 	}
1799 
1800 	nfc_device_iter_exit(&iter);
1801 
1802 	mutex_unlock(&nfc_devlist_mutex);
1803 
1804 	kfree(w);
1805 }
1806 
1807 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1808 				 unsigned long event, void *ptr)
1809 {
1810 	struct netlink_notify *n = ptr;
1811 	struct urelease_work *w;
1812 
1813 	if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1814 		goto out;
1815 
1816 	pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1817 
1818 	w = kmalloc(sizeof(*w), GFP_ATOMIC);
1819 	if (w) {
1820 		INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1821 		w->portid = n->portid;
1822 		schedule_work((struct work_struct *) w);
1823 	}
1824 
1825 out:
1826 	return NOTIFY_DONE;
1827 }
1828 
1829 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1830 {
1831 	genl_data->poll_req_portid = 0;
1832 	mutex_init(&genl_data->genl_data_mutex);
1833 }
1834 
1835 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1836 {
1837 	mutex_destroy(&genl_data->genl_data_mutex);
1838 }
1839 
1840 static struct notifier_block nl_notifier = {
1841 	.notifier_call  = nfc_genl_rcv_nl_event,
1842 };
1843 
1844 /**
1845  * nfc_genl_init() - Initialize netlink interface
1846  *
1847  * This initialization function registers the nfc netlink family.
1848  */
1849 int __init nfc_genl_init(void)
1850 {
1851 	int rc;
1852 
1853 	rc = genl_register_family(&nfc_genl_family);
1854 	if (rc)
1855 		return rc;
1856 
1857 	netlink_register_notifier(&nl_notifier);
1858 
1859 	return 0;
1860 }
1861 
1862 /**
1863  * nfc_genl_exit() - Deinitialize netlink interface
1864  *
1865  * This exit function unregisters the nfc netlink family.
1866  */
1867 void nfc_genl_exit(void)
1868 {
1869 	netlink_unregister_notifier(&nl_notifier);
1870 	genl_unregister_family(&nfc_genl_family);
1871 }
1872