This source file includes following definitions.
- nfc_genl_send_target
- __get_device_from_cb
- nfc_genl_dump_targets
- nfc_genl_dump_targets_done
- nfc_genl_targets_found
- nfc_genl_target_lost
- nfc_genl_tm_activated
- nfc_genl_tm_deactivated
- nfc_genl_setup_device_added
- nfc_genl_device_added
- nfc_genl_device_removed
- nfc_genl_llc_send_sdres
- nfc_genl_se_added
- nfc_genl_se_removed
- nfc_genl_se_transaction
- nfc_genl_se_connectivity
- nfc_genl_send_device
- nfc_genl_dump_devices
- nfc_genl_dump_devices_done
- nfc_genl_dep_link_up_event
- nfc_genl_dep_link_down_event
- nfc_genl_get_device
- nfc_genl_dev_up
- nfc_genl_dev_down
- nfc_genl_start_poll
- nfc_genl_stop_poll
- nfc_genl_activate_target
- nfc_genl_deactivate_target
- nfc_genl_dep_link_up
- nfc_genl_dep_link_down
- nfc_genl_send_params
- nfc_genl_llc_get_params
- nfc_genl_llc_set_params
- nfc_genl_llc_sdreq
- nfc_genl_fw_download
- nfc_genl_fw_download_done
- nfc_genl_enable_se
- nfc_genl_disable_se
- nfc_genl_send_se
- nfc_genl_dump_ses
- nfc_genl_dump_ses_done
- nfc_se_io
- se_io_cb
- nfc_genl_se_io
- nfc_genl_vendor_cmd
- nfc_hdr_put
- __nfc_alloc_vendor_cmd_skb
- __nfc_alloc_vendor_cmd_reply_skb
- nfc_vendor_cmd_reply
- nfc_urelease_event_work
- nfc_genl_rcv_nl_event
- nfc_genl_data_init
- nfc_genl_data_exit
- nfc_genl_init
- nfc_genl_exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
17
18 #include <net/genetlink.h>
19 #include <linux/nfc.h>
20 #include <linux/slab.h>
21
22 #include "nfc.h"
23 #include "llcp.h"
24
25 static const struct genl_multicast_group nfc_genl_mcgrps[] = {
26 { .name = NFC_GENL_MCAST_EVENT_NAME, },
27 };
28
29 static struct genl_family nfc_genl_family;
30 static 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 },
35 [NFC_ATTR_TARGET_INDEX] = { .type = NLA_U32 },
36 [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
37 [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
38 [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
39 [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
40 [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
41 [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 },
44 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
45 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
46 .len = NFC_FIRMWARE_NAME_MAXSIZE },
47 [NFC_ATTR_SE_INDEX] = { .type = NLA_U32 },
48 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
49 [NFC_ATTR_VENDOR_ID] = { .type = NLA_U32 },
50 [NFC_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
51 [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
52
53 };
54
55 static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
56 [NFC_SDP_ATTR_URI] = { .type = NLA_STRING,
57 .len = U8_MAX - 4 },
58 [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
59 };
60
61 static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
62 struct netlink_callback *cb, int flags)
63 {
64 void *hdr;
65
66 hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
67 &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
68 if (!hdr)
69 return -EMSGSIZE;
70
71 genl_dump_check_consistent(cb, hdr);
72
73 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;
90
91 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
99 genlmsg_end(msg, hdr);
100 return 0;
101
102 nla_put_failure:
103 genlmsg_cancel(msg, hdr);
104 return -EMSGSIZE;
105 }
106
107 static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
108 {
109 struct nlattr **attrbuf = genl_family_attrbuf(&nfc_genl_family);
110 struct nfc_dev *dev;
111 int rc;
112 u32 idx;
113
114 rc = nlmsg_parse_deprecated(cb->nlh,
115 GENL_HDRLEN + nfc_genl_family.hdrsize,
116 attrbuf, nfc_genl_family.maxattr,
117 nfc_genl_policy, NULL);
118 if (rc < 0)
119 return ERR_PTR(rc);
120
121 if (!attrbuf[NFC_ATTR_DEVICE_INDEX])
122 return ERR_PTR(-EINVAL);
123
124 idx = nla_get_u32(attrbuf[NFC_ATTR_DEVICE_INDEX]);
125
126 dev = nfc_get_device(idx);
127 if (!dev)
128 return ERR_PTR(-ENODEV);
129
130 return dev;
131 }
132
133 static int nfc_genl_dump_targets(struct sk_buff *skb,
134 struct netlink_callback *cb)
135 {
136 int i = cb->args[0];
137 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
138 int rc;
139
140 if (!dev) {
141 dev = __get_device_from_cb(cb);
142 if (IS_ERR(dev))
143 return PTR_ERR(dev);
144
145 cb->args[1] = (long) dev;
146 }
147
148 device_lock(&dev->dev);
149
150 cb->seq = dev->targets_generation;
151
152 while (i < dev->n_targets) {
153 rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
154 NLM_F_MULTI);
155 if (rc < 0)
156 break;
157
158 i++;
159 }
160
161 device_unlock(&dev->dev);
162
163 cb->args[0] = i;
164
165 return skb->len;
166 }
167
168 static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
169 {
170 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
171
172 if (dev)
173 nfc_put_device(dev);
174
175 return 0;
176 }
177
178 int nfc_genl_targets_found(struct nfc_dev *dev)
179 {
180 struct sk_buff *msg;
181 void *hdr;
182
183 dev->genl_data.poll_req_portid = 0;
184
185 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
186 if (!msg)
187 return -ENOMEM;
188
189 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
190 NFC_EVENT_TARGETS_FOUND);
191 if (!hdr)
192 goto free_msg;
193
194 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
195 goto nla_put_failure;
196
197 genlmsg_end(msg, hdr);
198
199 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
200
201 nla_put_failure:
202 free_msg:
203 nlmsg_free(msg);
204 return -EMSGSIZE;
205 }
206
207 int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
208 {
209 struct sk_buff *msg;
210 void *hdr;
211
212 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
213 if (!msg)
214 return -ENOMEM;
215
216 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
217 NFC_EVENT_TARGET_LOST);
218 if (!hdr)
219 goto free_msg;
220
221 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
222 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
223 goto nla_put_failure;
224
225 genlmsg_end(msg, hdr);
226
227 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
228
229 return 0;
230
231 nla_put_failure:
232 free_msg:
233 nlmsg_free(msg);
234 return -EMSGSIZE;
235 }
236
237 int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
238 {
239 struct sk_buff *msg;
240 void *hdr;
241
242 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
243 if (!msg)
244 return -ENOMEM;
245
246 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
247 NFC_EVENT_TM_ACTIVATED);
248 if (!hdr)
249 goto free_msg;
250
251 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
252 goto nla_put_failure;
253 if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
254 goto nla_put_failure;
255
256 genlmsg_end(msg, hdr);
257
258 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
259
260 return 0;
261
262 nla_put_failure:
263 free_msg:
264 nlmsg_free(msg);
265 return -EMSGSIZE;
266 }
267
268 int nfc_genl_tm_deactivated(struct nfc_dev *dev)
269 {
270 struct sk_buff *msg;
271 void *hdr;
272
273 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
274 if (!msg)
275 return -ENOMEM;
276
277 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
278 NFC_EVENT_TM_DEACTIVATED);
279 if (!hdr)
280 goto free_msg;
281
282 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
283 goto nla_put_failure;
284
285 genlmsg_end(msg, hdr);
286
287 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
288
289 return 0;
290
291 nla_put_failure:
292 free_msg:
293 nlmsg_free(msg);
294 return -EMSGSIZE;
295 }
296
297 static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg)
298 {
299 if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
300 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
301 nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
302 nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
303 nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
304 return -1;
305 return 0;
306 }
307
308 int nfc_genl_device_added(struct nfc_dev *dev)
309 {
310 struct sk_buff *msg;
311 void *hdr;
312
313 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
314 if (!msg)
315 return -ENOMEM;
316
317 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
318 NFC_EVENT_DEVICE_ADDED);
319 if (!hdr)
320 goto free_msg;
321
322 if (nfc_genl_setup_device_added(dev, msg))
323 goto nla_put_failure;
324
325 genlmsg_end(msg, hdr);
326
327 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
328
329 return 0;
330
331 nla_put_failure:
332 free_msg:
333 nlmsg_free(msg);
334 return -EMSGSIZE;
335 }
336
337 int nfc_genl_device_removed(struct nfc_dev *dev)
338 {
339 struct sk_buff *msg;
340 void *hdr;
341
342 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
343 if (!msg)
344 return -ENOMEM;
345
346 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
347 NFC_EVENT_DEVICE_REMOVED);
348 if (!hdr)
349 goto free_msg;
350
351 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
352 goto nla_put_failure;
353
354 genlmsg_end(msg, hdr);
355
356 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
357
358 return 0;
359
360 nla_put_failure:
361 free_msg:
362 nlmsg_free(msg);
363 return -EMSGSIZE;
364 }
365
366 int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
367 {
368 struct sk_buff *msg;
369 struct nlattr *sdp_attr, *uri_attr;
370 struct nfc_llcp_sdp_tlv *sdres;
371 struct hlist_node *n;
372 void *hdr;
373 int rc = -EMSGSIZE;
374 int i;
375
376 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
377 if (!msg)
378 return -ENOMEM;
379
380 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
381 NFC_EVENT_LLC_SDRES);
382 if (!hdr)
383 goto free_msg;
384
385 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
386 goto nla_put_failure;
387
388 sdp_attr = nla_nest_start_noflag(msg, NFC_ATTR_LLC_SDP);
389 if (sdp_attr == NULL) {
390 rc = -ENOMEM;
391 goto nla_put_failure;
392 }
393
394 i = 1;
395 hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
396 pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
397
398 uri_attr = nla_nest_start_noflag(msg, i++);
399 if (uri_attr == NULL) {
400 rc = -ENOMEM;
401 goto nla_put_failure;
402 }
403
404 if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
405 goto nla_put_failure;
406
407 if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
408 goto nla_put_failure;
409
410 nla_nest_end(msg, uri_attr);
411
412 hlist_del(&sdres->node);
413
414 nfc_llcp_free_sdp_tlv(sdres);
415 }
416
417 nla_nest_end(msg, sdp_attr);
418
419 genlmsg_end(msg, hdr);
420
421 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
422
423 nla_put_failure:
424 free_msg:
425 nlmsg_free(msg);
426
427 nfc_llcp_free_sdp_tlv_list(sdres_list);
428
429 return rc;
430 }
431
432 int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
433 {
434 struct sk_buff *msg;
435 void *hdr;
436
437 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
438 if (!msg)
439 return -ENOMEM;
440
441 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
442 NFC_EVENT_SE_ADDED);
443 if (!hdr)
444 goto free_msg;
445
446 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
447 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
448 nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
449 goto nla_put_failure;
450
451 genlmsg_end(msg, hdr);
452
453 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
454
455 return 0;
456
457 nla_put_failure:
458 free_msg:
459 nlmsg_free(msg);
460 return -EMSGSIZE;
461 }
462
463 int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
464 {
465 struct sk_buff *msg;
466 void *hdr;
467
468 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
469 if (!msg)
470 return -ENOMEM;
471
472 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
473 NFC_EVENT_SE_REMOVED);
474 if (!hdr)
475 goto free_msg;
476
477 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
478 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
479 goto nla_put_failure;
480
481 genlmsg_end(msg, hdr);
482
483 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
484
485 return 0;
486
487 nla_put_failure:
488 free_msg:
489 nlmsg_free(msg);
490 return -EMSGSIZE;
491 }
492
493 int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
494 struct nfc_evt_transaction *evt_transaction)
495 {
496 struct nfc_se *se;
497 struct sk_buff *msg;
498 void *hdr;
499
500 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
501 if (!msg)
502 return -ENOMEM;
503
504 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
505 NFC_EVENT_SE_TRANSACTION);
506 if (!hdr)
507 goto free_msg;
508
509 se = nfc_find_se(dev, se_idx);
510 if (!se)
511 goto free_msg;
512
513 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
514 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
515 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
516 nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
517 evt_transaction->aid) ||
518 nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
519 evt_transaction->params))
520 goto nla_put_failure;
521
522
523 devm_kfree(&dev->dev, evt_transaction);
524
525 genlmsg_end(msg, hdr);
526
527 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
528
529 return 0;
530
531 nla_put_failure:
532 free_msg:
533
534 devm_kfree(&dev->dev, evt_transaction);
535 nlmsg_free(msg);
536 return -EMSGSIZE;
537 }
538
539 int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx)
540 {
541 struct nfc_se *se;
542 struct sk_buff *msg;
543 void *hdr;
544
545 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
546 if (!msg)
547 return -ENOMEM;
548
549 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
550 NFC_EVENT_SE_CONNECTIVITY);
551 if (!hdr)
552 goto free_msg;
553
554 se = nfc_find_se(dev, se_idx);
555 if (!se)
556 goto free_msg;
557
558 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
559 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
560 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
561 goto nla_put_failure;
562
563 genlmsg_end(msg, hdr);
564
565 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
566
567 return 0;
568
569 nla_put_failure:
570 free_msg:
571 nlmsg_free(msg);
572 return -EMSGSIZE;
573 }
574
575 static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
576 u32 portid, u32 seq,
577 struct netlink_callback *cb,
578 int flags)
579 {
580 void *hdr;
581
582 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
583 NFC_CMD_GET_DEVICE);
584 if (!hdr)
585 return -EMSGSIZE;
586
587 if (cb)
588 genl_dump_check_consistent(cb, hdr);
589
590 if (nfc_genl_setup_device_added(dev, msg))
591 goto nla_put_failure;
592
593 genlmsg_end(msg, hdr);
594 return 0;
595
596 nla_put_failure:
597 genlmsg_cancel(msg, hdr);
598 return -EMSGSIZE;
599 }
600
601 static int nfc_genl_dump_devices(struct sk_buff *skb,
602 struct netlink_callback *cb)
603 {
604 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
605 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
606 bool first_call = false;
607
608 if (!iter) {
609 first_call = true;
610 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
611 if (!iter)
612 return -ENOMEM;
613 cb->args[0] = (long) iter;
614 }
615
616 mutex_lock(&nfc_devlist_mutex);
617
618 cb->seq = nfc_devlist_generation;
619
620 if (first_call) {
621 nfc_device_iter_init(iter);
622 dev = nfc_device_iter_next(iter);
623 }
624
625 while (dev) {
626 int rc;
627
628 rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
629 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
630 if (rc < 0)
631 break;
632
633 dev = nfc_device_iter_next(iter);
634 }
635
636 mutex_unlock(&nfc_devlist_mutex);
637
638 cb->args[1] = (long) dev;
639
640 return skb->len;
641 }
642
643 static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
644 {
645 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
646
647 nfc_device_iter_exit(iter);
648 kfree(iter);
649
650 return 0;
651 }
652
653 int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
654 u8 comm_mode, u8 rf_mode)
655 {
656 struct sk_buff *msg;
657 void *hdr;
658
659 pr_debug("DEP link is up\n");
660
661 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
662 if (!msg)
663 return -ENOMEM;
664
665 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
666 if (!hdr)
667 goto free_msg;
668
669 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
670 goto nla_put_failure;
671 if (rf_mode == NFC_RF_INITIATOR &&
672 nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
673 goto nla_put_failure;
674 if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
675 nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
676 goto nla_put_failure;
677
678 genlmsg_end(msg, hdr);
679
680 dev->dep_link_up = true;
681
682 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
683
684 return 0;
685
686 nla_put_failure:
687 free_msg:
688 nlmsg_free(msg);
689 return -EMSGSIZE;
690 }
691
692 int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
693 {
694 struct sk_buff *msg;
695 void *hdr;
696
697 pr_debug("DEP link is down\n");
698
699 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
700 if (!msg)
701 return -ENOMEM;
702
703 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
704 NFC_CMD_DEP_LINK_DOWN);
705 if (!hdr)
706 goto free_msg;
707
708 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
709 goto nla_put_failure;
710
711 genlmsg_end(msg, hdr);
712
713 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
714
715 return 0;
716
717 nla_put_failure:
718 free_msg:
719 nlmsg_free(msg);
720 return -EMSGSIZE;
721 }
722
723 static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
724 {
725 struct sk_buff *msg;
726 struct nfc_dev *dev;
727 u32 idx;
728 int rc = -ENOBUFS;
729
730 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
731 return -EINVAL;
732
733 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
734
735 dev = nfc_get_device(idx);
736 if (!dev)
737 return -ENODEV;
738
739 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
740 if (!msg) {
741 rc = -ENOMEM;
742 goto out_putdev;
743 }
744
745 rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
746 NULL, 0);
747 if (rc < 0)
748 goto out_free;
749
750 nfc_put_device(dev);
751
752 return genlmsg_reply(msg, info);
753
754 out_free:
755 nlmsg_free(msg);
756 out_putdev:
757 nfc_put_device(dev);
758 return rc;
759 }
760
761 static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
762 {
763 struct nfc_dev *dev;
764 int rc;
765 u32 idx;
766
767 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
768 return -EINVAL;
769
770 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
771
772 dev = nfc_get_device(idx);
773 if (!dev)
774 return -ENODEV;
775
776 rc = nfc_dev_up(dev);
777
778 nfc_put_device(dev);
779 return rc;
780 }
781
782 static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
783 {
784 struct nfc_dev *dev;
785 int rc;
786 u32 idx;
787
788 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
789 return -EINVAL;
790
791 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
792
793 dev = nfc_get_device(idx);
794 if (!dev)
795 return -ENODEV;
796
797 rc = nfc_dev_down(dev);
798
799 nfc_put_device(dev);
800 return rc;
801 }
802
803 static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
804 {
805 struct nfc_dev *dev;
806 int rc;
807 u32 idx;
808 u32 im_protocols = 0, tm_protocols = 0;
809
810 pr_debug("Poll start\n");
811
812 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
813 ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
814 !info->attrs[NFC_ATTR_PROTOCOLS]) &&
815 !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
816 return -EINVAL;
817
818 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
819
820 if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
821 tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
822
823 if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
824 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
825 else if (info->attrs[NFC_ATTR_PROTOCOLS])
826 im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
827
828 dev = nfc_get_device(idx);
829 if (!dev)
830 return -ENODEV;
831
832 mutex_lock(&dev->genl_data.genl_data_mutex);
833
834 rc = nfc_start_poll(dev, im_protocols, tm_protocols);
835 if (!rc)
836 dev->genl_data.poll_req_portid = info->snd_portid;
837
838 mutex_unlock(&dev->genl_data.genl_data_mutex);
839
840 nfc_put_device(dev);
841 return rc;
842 }
843
844 static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
845 {
846 struct nfc_dev *dev;
847 int rc;
848 u32 idx;
849
850 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
851 return -EINVAL;
852
853 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
854
855 dev = nfc_get_device(idx);
856 if (!dev)
857 return -ENODEV;
858
859 device_lock(&dev->dev);
860
861 if (!dev->polling) {
862 device_unlock(&dev->dev);
863 return -EINVAL;
864 }
865
866 device_unlock(&dev->dev);
867
868 mutex_lock(&dev->genl_data.genl_data_mutex);
869
870 if (dev->genl_data.poll_req_portid != info->snd_portid) {
871 rc = -EBUSY;
872 goto out;
873 }
874
875 rc = nfc_stop_poll(dev);
876 dev->genl_data.poll_req_portid = 0;
877
878 out:
879 mutex_unlock(&dev->genl_data.genl_data_mutex);
880 nfc_put_device(dev);
881 return rc;
882 }
883
884 static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
885 {
886 struct nfc_dev *dev;
887 u32 device_idx, target_idx, protocol;
888 int rc;
889
890 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
891 !info->attrs[NFC_ATTR_TARGET_INDEX] ||
892 !info->attrs[NFC_ATTR_PROTOCOLS])
893 return -EINVAL;
894
895 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
896
897 dev = nfc_get_device(device_idx);
898 if (!dev)
899 return -ENODEV;
900
901 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
902 protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
903
904 nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
905 rc = nfc_activate_target(dev, target_idx, protocol);
906
907 nfc_put_device(dev);
908 return rc;
909 }
910
911 static int nfc_genl_deactivate_target(struct sk_buff *skb,
912 struct genl_info *info)
913 {
914 struct nfc_dev *dev;
915 u32 device_idx, target_idx;
916 int rc;
917
918 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
919 !info->attrs[NFC_ATTR_TARGET_INDEX])
920 return -EINVAL;
921
922 device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
923
924 dev = nfc_get_device(device_idx);
925 if (!dev)
926 return -ENODEV;
927
928 target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
929
930 rc = nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP);
931
932 nfc_put_device(dev);
933 return rc;
934 }
935
936 static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
937 {
938 struct nfc_dev *dev;
939 int rc, tgt_idx;
940 u32 idx;
941 u8 comm;
942
943 pr_debug("DEP link up\n");
944
945 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
946 !info->attrs[NFC_ATTR_COMM_MODE])
947 return -EINVAL;
948
949 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
950 if (!info->attrs[NFC_ATTR_TARGET_INDEX])
951 tgt_idx = NFC_TARGET_IDX_ANY;
952 else
953 tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
954
955 comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
956
957 if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
958 return -EINVAL;
959
960 dev = nfc_get_device(idx);
961 if (!dev)
962 return -ENODEV;
963
964 rc = nfc_dep_link_up(dev, tgt_idx, comm);
965
966 nfc_put_device(dev);
967
968 return rc;
969 }
970
971 static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
972 {
973 struct nfc_dev *dev;
974 int rc;
975 u32 idx;
976
977 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
978 !info->attrs[NFC_ATTR_TARGET_INDEX])
979 return -EINVAL;
980
981 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
982
983 dev = nfc_get_device(idx);
984 if (!dev)
985 return -ENODEV;
986
987 rc = nfc_dep_link_down(dev);
988
989 nfc_put_device(dev);
990 return rc;
991 }
992
993 static int nfc_genl_send_params(struct sk_buff *msg,
994 struct nfc_llcp_local *local,
995 u32 portid, u32 seq)
996 {
997 void *hdr;
998
999 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
1000 NFC_CMD_LLC_GET_PARAMS);
1001 if (!hdr)
1002 return -EMSGSIZE;
1003
1004 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
1005 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
1006 nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
1007 nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
1008 goto nla_put_failure;
1009
1010 genlmsg_end(msg, hdr);
1011 return 0;
1012
1013 nla_put_failure:
1014 genlmsg_cancel(msg, hdr);
1015 return -EMSGSIZE;
1016 }
1017
1018 static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
1019 {
1020 struct nfc_dev *dev;
1021 struct nfc_llcp_local *local;
1022 int rc = 0;
1023 struct sk_buff *msg = NULL;
1024 u32 idx;
1025
1026 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1027 !info->attrs[NFC_ATTR_FIRMWARE_NAME])
1028 return -EINVAL;
1029
1030 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1031
1032 dev = nfc_get_device(idx);
1033 if (!dev)
1034 return -ENODEV;
1035
1036 device_lock(&dev->dev);
1037
1038 local = nfc_llcp_find_local(dev);
1039 if (!local) {
1040 rc = -ENODEV;
1041 goto exit;
1042 }
1043
1044 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1045 if (!msg) {
1046 rc = -ENOMEM;
1047 goto exit;
1048 }
1049
1050 rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
1051
1052 exit:
1053 device_unlock(&dev->dev);
1054
1055 nfc_put_device(dev);
1056
1057 if (rc < 0) {
1058 if (msg)
1059 nlmsg_free(msg);
1060
1061 return rc;
1062 }
1063
1064 return genlmsg_reply(msg, info);
1065 }
1066
1067 static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
1068 {
1069 struct nfc_dev *dev;
1070 struct nfc_llcp_local *local;
1071 u8 rw = 0;
1072 u16 miux = 0;
1073 u32 idx;
1074 int rc = 0;
1075
1076 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1077 (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
1078 !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
1079 !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
1080 return -EINVAL;
1081
1082 if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
1083 rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
1084
1085 if (rw > LLCP_MAX_RW)
1086 return -EINVAL;
1087 }
1088
1089 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
1090 miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
1091
1092 if (miux > LLCP_MAX_MIUX)
1093 return -EINVAL;
1094 }
1095
1096 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1097
1098 dev = nfc_get_device(idx);
1099 if (!dev)
1100 return -ENODEV;
1101
1102 device_lock(&dev->dev);
1103
1104 local = nfc_llcp_find_local(dev);
1105 if (!local) {
1106 rc = -ENODEV;
1107 goto exit;
1108 }
1109
1110 if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
1111 if (dev->dep_link_up) {
1112 rc = -EINPROGRESS;
1113 goto exit;
1114 }
1115
1116 local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
1117 }
1118
1119 if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
1120 local->rw = rw;
1121
1122 if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
1123 local->miux = cpu_to_be16(miux);
1124
1125 exit:
1126 device_unlock(&dev->dev);
1127
1128 nfc_put_device(dev);
1129
1130 return rc;
1131 }
1132
1133 static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
1134 {
1135 struct nfc_dev *dev;
1136 struct nfc_llcp_local *local;
1137 struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
1138 u32 idx;
1139 u8 tid;
1140 char *uri;
1141 int rc = 0, rem;
1142 size_t uri_len, tlvs_len;
1143 struct hlist_head sdreq_list;
1144 struct nfc_llcp_sdp_tlv *sdreq;
1145
1146 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1147 !info->attrs[NFC_ATTR_LLC_SDP])
1148 return -EINVAL;
1149
1150 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1151
1152 dev = nfc_get_device(idx);
1153 if (!dev)
1154 return -ENODEV;
1155
1156 device_lock(&dev->dev);
1157
1158 if (dev->dep_link_up == false) {
1159 rc = -ENOLINK;
1160 goto exit;
1161 }
1162
1163 local = nfc_llcp_find_local(dev);
1164 if (!local) {
1165 rc = -ENODEV;
1166 goto exit;
1167 }
1168
1169 INIT_HLIST_HEAD(&sdreq_list);
1170
1171 tlvs_len = 0;
1172
1173 nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
1174 rc = nla_parse_nested_deprecated(sdp_attrs, NFC_SDP_ATTR_MAX,
1175 attr, nfc_sdp_genl_policy,
1176 info->extack);
1177
1178 if (rc != 0) {
1179 rc = -EINVAL;
1180 goto exit;
1181 }
1182
1183 if (!sdp_attrs[NFC_SDP_ATTR_URI])
1184 continue;
1185
1186 uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
1187 if (uri_len == 0)
1188 continue;
1189
1190 uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
1191 if (uri == NULL || *uri == 0)
1192 continue;
1193
1194 tid = local->sdreq_next_tid++;
1195
1196 sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
1197 if (sdreq == NULL) {
1198 rc = -ENOMEM;
1199 goto exit;
1200 }
1201
1202 tlvs_len += sdreq->tlv_len;
1203
1204 hlist_add_head(&sdreq->node, &sdreq_list);
1205 }
1206
1207 if (hlist_empty(&sdreq_list)) {
1208 rc = -EINVAL;
1209 goto exit;
1210 }
1211
1212 rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
1213 exit:
1214 device_unlock(&dev->dev);
1215
1216 nfc_put_device(dev);
1217
1218 return rc;
1219 }
1220
1221 static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
1222 {
1223 struct nfc_dev *dev;
1224 int rc;
1225 u32 idx;
1226 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1227
1228 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1229 return -EINVAL;
1230
1231 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1232
1233 dev = nfc_get_device(idx);
1234 if (!dev)
1235 return -ENODEV;
1236
1237 nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1238 sizeof(firmware_name));
1239
1240 rc = nfc_fw_download(dev, firmware_name);
1241
1242 nfc_put_device(dev);
1243 return rc;
1244 }
1245
1246 int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1247 u32 result)
1248 {
1249 struct sk_buff *msg;
1250 void *hdr;
1251
1252 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1253 if (!msg)
1254 return -ENOMEM;
1255
1256 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1257 NFC_CMD_FW_DOWNLOAD);
1258 if (!hdr)
1259 goto free_msg;
1260
1261 if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1262 nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
1263 nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1264 goto nla_put_failure;
1265
1266 genlmsg_end(msg, hdr);
1267
1268 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1269
1270 return 0;
1271
1272 nla_put_failure:
1273 free_msg:
1274 nlmsg_free(msg);
1275 return -EMSGSIZE;
1276 }
1277
1278 static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
1279 {
1280 struct nfc_dev *dev;
1281 int rc;
1282 u32 idx, se_idx;
1283
1284 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1285 !info->attrs[NFC_ATTR_SE_INDEX])
1286 return -EINVAL;
1287
1288 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1289 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1290
1291 dev = nfc_get_device(idx);
1292 if (!dev)
1293 return -ENODEV;
1294
1295 rc = nfc_enable_se(dev, se_idx);
1296
1297 nfc_put_device(dev);
1298 return rc;
1299 }
1300
1301 static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
1302 {
1303 struct nfc_dev *dev;
1304 int rc;
1305 u32 idx, se_idx;
1306
1307 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1308 !info->attrs[NFC_ATTR_SE_INDEX])
1309 return -EINVAL;
1310
1311 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1312 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1313
1314 dev = nfc_get_device(idx);
1315 if (!dev)
1316 return -ENODEV;
1317
1318 rc = nfc_disable_se(dev, se_idx);
1319
1320 nfc_put_device(dev);
1321 return rc;
1322 }
1323
1324 static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
1325 u32 portid, u32 seq,
1326 struct netlink_callback *cb,
1327 int flags)
1328 {
1329 void *hdr;
1330 struct nfc_se *se, *n;
1331
1332 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
1333 hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
1334 NFC_CMD_GET_SE);
1335 if (!hdr)
1336 goto nla_put_failure;
1337
1338 if (cb)
1339 genl_dump_check_consistent(cb, hdr);
1340
1341 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
1342 nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
1343 nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
1344 goto nla_put_failure;
1345
1346 genlmsg_end(msg, hdr);
1347 }
1348
1349 return 0;
1350
1351 nla_put_failure:
1352 genlmsg_cancel(msg, hdr);
1353 return -EMSGSIZE;
1354 }
1355
1356 static int nfc_genl_dump_ses(struct sk_buff *skb,
1357 struct netlink_callback *cb)
1358 {
1359 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1360 struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
1361 bool first_call = false;
1362
1363 if (!iter) {
1364 first_call = true;
1365 iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
1366 if (!iter)
1367 return -ENOMEM;
1368 cb->args[0] = (long) iter;
1369 }
1370
1371 mutex_lock(&nfc_devlist_mutex);
1372
1373 cb->seq = nfc_devlist_generation;
1374
1375 if (first_call) {
1376 nfc_device_iter_init(iter);
1377 dev = nfc_device_iter_next(iter);
1378 }
1379
1380 while (dev) {
1381 int rc;
1382
1383 rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
1384 cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
1385 if (rc < 0)
1386 break;
1387
1388 dev = nfc_device_iter_next(iter);
1389 }
1390
1391 mutex_unlock(&nfc_devlist_mutex);
1392
1393 cb->args[1] = (long) dev;
1394
1395 return skb->len;
1396 }
1397
1398 static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1399 {
1400 struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
1401
1402 nfc_device_iter_exit(iter);
1403 kfree(iter);
1404
1405 return 0;
1406 }
1407
1408 static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
1409 u8 *apdu, size_t apdu_length,
1410 se_io_cb_t cb, void *cb_context)
1411 {
1412 struct nfc_se *se;
1413 int rc;
1414
1415 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
1416
1417 device_lock(&dev->dev);
1418
1419 if (!device_is_registered(&dev->dev)) {
1420 rc = -ENODEV;
1421 goto error;
1422 }
1423
1424 if (!dev->dev_up) {
1425 rc = -ENODEV;
1426 goto error;
1427 }
1428
1429 if (!dev->ops->se_io) {
1430 rc = -EOPNOTSUPP;
1431 goto error;
1432 }
1433
1434 se = nfc_find_se(dev, se_idx);
1435 if (!se) {
1436 rc = -EINVAL;
1437 goto error;
1438 }
1439
1440 if (se->state != NFC_SE_ENABLED) {
1441 rc = -ENODEV;
1442 goto error;
1443 }
1444
1445 rc = dev->ops->se_io(dev, se_idx, apdu,
1446 apdu_length, cb, cb_context);
1447
1448 error:
1449 device_unlock(&dev->dev);
1450 return rc;
1451 }
1452
1453 struct se_io_ctx {
1454 u32 dev_idx;
1455 u32 se_idx;
1456 };
1457
1458 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1459 {
1460 struct se_io_ctx *ctx = context;
1461 struct sk_buff *msg;
1462 void *hdr;
1463
1464 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1465 if (!msg) {
1466 kfree(ctx);
1467 return;
1468 }
1469
1470 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1471 NFC_CMD_SE_IO);
1472 if (!hdr)
1473 goto free_msg;
1474
1475 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1476 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1477 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1478 goto nla_put_failure;
1479
1480 genlmsg_end(msg, hdr);
1481
1482 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1483
1484 kfree(ctx);
1485
1486 return;
1487
1488 nla_put_failure:
1489 free_msg:
1490 nlmsg_free(msg);
1491 kfree(ctx);
1492
1493 return;
1494 }
1495
1496 static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1497 {
1498 struct nfc_dev *dev;
1499 struct se_io_ctx *ctx;
1500 u32 dev_idx, se_idx;
1501 u8 *apdu;
1502 size_t apdu_len;
1503
1504 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1505 !info->attrs[NFC_ATTR_SE_INDEX] ||
1506 !info->attrs[NFC_ATTR_SE_APDU])
1507 return -EINVAL;
1508
1509 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1510 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1511
1512 dev = nfc_get_device(dev_idx);
1513 if (!dev)
1514 return -ENODEV;
1515
1516 if (!dev->ops || !dev->ops->se_io)
1517 return -ENOTSUPP;
1518
1519 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1520 if (apdu_len == 0)
1521 return -EINVAL;
1522
1523 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1524 if (!apdu)
1525 return -EINVAL;
1526
1527 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1528 if (!ctx)
1529 return -ENOMEM;
1530
1531 ctx->dev_idx = dev_idx;
1532 ctx->se_idx = se_idx;
1533
1534 return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1535 }
1536
1537 static int nfc_genl_vendor_cmd(struct sk_buff *skb,
1538 struct genl_info *info)
1539 {
1540 struct nfc_dev *dev;
1541 struct nfc_vendor_cmd *cmd;
1542 u32 dev_idx, vid, subcmd;
1543 u8 *data;
1544 size_t data_len;
1545 int i, err;
1546
1547 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1548 !info->attrs[NFC_ATTR_VENDOR_ID] ||
1549 !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
1550 return -EINVAL;
1551
1552 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1553 vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
1554 subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
1555
1556 dev = nfc_get_device(dev_idx);
1557 if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
1558 return -ENODEV;
1559
1560 if (info->attrs[NFC_ATTR_VENDOR_DATA]) {
1561 data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
1562 data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
1563 if (data_len == 0)
1564 return -EINVAL;
1565 } else {
1566 data = NULL;
1567 data_len = 0;
1568 }
1569
1570 for (i = 0; i < dev->n_vendor_cmds; i++) {
1571 cmd = &dev->vendor_cmds[i];
1572
1573 if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
1574 continue;
1575
1576 dev->cur_cmd_info = info;
1577 err = cmd->doit(dev, data, data_len);
1578 dev->cur_cmd_info = NULL;
1579 return err;
1580 }
1581
1582 return -EOPNOTSUPP;
1583 }
1584
1585
1586 static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1587 int flags, u8 cmd)
1588 {
1589
1590 return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd);
1591 }
1592
1593 static struct sk_buff *
1594 __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen,
1595 u32 portid, u32 seq,
1596 enum nfc_attrs attr,
1597 u32 oui, u32 subcmd, gfp_t gfp)
1598 {
1599 struct sk_buff *skb;
1600 void *hdr;
1601
1602 skb = nlmsg_new(approxlen + 100, gfp);
1603 if (!skb)
1604 return NULL;
1605
1606 hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR);
1607 if (!hdr) {
1608 kfree_skb(skb);
1609 return NULL;
1610 }
1611
1612 if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx))
1613 goto nla_put_failure;
1614 if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui))
1615 goto nla_put_failure;
1616 if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd))
1617 goto nla_put_failure;
1618
1619 ((void **)skb->cb)[0] = dev;
1620 ((void **)skb->cb)[1] = hdr;
1621
1622 return skb;
1623
1624 nla_put_failure:
1625 kfree_skb(skb);
1626 return NULL;
1627 }
1628
1629 struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
1630 enum nfc_attrs attr,
1631 u32 oui, u32 subcmd,
1632 int approxlen)
1633 {
1634 if (WARN_ON(!dev->cur_cmd_info))
1635 return NULL;
1636
1637 return __nfc_alloc_vendor_cmd_skb(dev, approxlen,
1638 dev->cur_cmd_info->snd_portid,
1639 dev->cur_cmd_info->snd_seq, attr,
1640 oui, subcmd, GFP_KERNEL);
1641 }
1642 EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb);
1643
1644 int nfc_vendor_cmd_reply(struct sk_buff *skb)
1645 {
1646 struct nfc_dev *dev = ((void **)skb->cb)[0];
1647 void *hdr = ((void **)skb->cb)[1];
1648
1649
1650 memset(skb->cb, 0, sizeof(skb->cb));
1651
1652 if (WARN_ON(!dev->cur_cmd_info)) {
1653 kfree_skb(skb);
1654 return -EINVAL;
1655 }
1656
1657 genlmsg_end(skb, hdr);
1658 return genlmsg_reply(skb, dev->cur_cmd_info);
1659 }
1660 EXPORT_SYMBOL(nfc_vendor_cmd_reply);
1661
1662 static const struct genl_ops nfc_genl_ops[] = {
1663 {
1664 .cmd = NFC_CMD_GET_DEVICE,
1665 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1666 .doit = nfc_genl_get_device,
1667 .dumpit = nfc_genl_dump_devices,
1668 .done = nfc_genl_dump_devices_done,
1669 },
1670 {
1671 .cmd = NFC_CMD_DEV_UP,
1672 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1673 .doit = nfc_genl_dev_up,
1674 },
1675 {
1676 .cmd = NFC_CMD_DEV_DOWN,
1677 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1678 .doit = nfc_genl_dev_down,
1679 },
1680 {
1681 .cmd = NFC_CMD_START_POLL,
1682 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1683 .doit = nfc_genl_start_poll,
1684 },
1685 {
1686 .cmd = NFC_CMD_STOP_POLL,
1687 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1688 .doit = nfc_genl_stop_poll,
1689 },
1690 {
1691 .cmd = NFC_CMD_DEP_LINK_UP,
1692 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1693 .doit = nfc_genl_dep_link_up,
1694 },
1695 {
1696 .cmd = NFC_CMD_DEP_LINK_DOWN,
1697 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1698 .doit = nfc_genl_dep_link_down,
1699 },
1700 {
1701 .cmd = NFC_CMD_GET_TARGET,
1702 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1703 .dumpit = nfc_genl_dump_targets,
1704 .done = nfc_genl_dump_targets_done,
1705 },
1706 {
1707 .cmd = NFC_CMD_LLC_GET_PARAMS,
1708 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1709 .doit = nfc_genl_llc_get_params,
1710 },
1711 {
1712 .cmd = NFC_CMD_LLC_SET_PARAMS,
1713 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1714 .doit = nfc_genl_llc_set_params,
1715 },
1716 {
1717 .cmd = NFC_CMD_LLC_SDREQ,
1718 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1719 .doit = nfc_genl_llc_sdreq,
1720 },
1721 {
1722 .cmd = NFC_CMD_FW_DOWNLOAD,
1723 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1724 .doit = nfc_genl_fw_download,
1725 },
1726 {
1727 .cmd = NFC_CMD_ENABLE_SE,
1728 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1729 .doit = nfc_genl_enable_se,
1730 },
1731 {
1732 .cmd = NFC_CMD_DISABLE_SE,
1733 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1734 .doit = nfc_genl_disable_se,
1735 },
1736 {
1737 .cmd = NFC_CMD_GET_SE,
1738 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1739 .dumpit = nfc_genl_dump_ses,
1740 .done = nfc_genl_dump_ses_done,
1741 },
1742 {
1743 .cmd = NFC_CMD_SE_IO,
1744 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1745 .doit = nfc_genl_se_io,
1746 },
1747 {
1748 .cmd = NFC_CMD_ACTIVATE_TARGET,
1749 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1750 .doit = nfc_genl_activate_target,
1751 },
1752 {
1753 .cmd = NFC_CMD_VENDOR,
1754 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1755 .doit = nfc_genl_vendor_cmd,
1756 },
1757 {
1758 .cmd = NFC_CMD_DEACTIVATE_TARGET,
1759 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1760 .doit = nfc_genl_deactivate_target,
1761 },
1762 };
1763
1764 static struct genl_family nfc_genl_family __ro_after_init = {
1765 .hdrsize = 0,
1766 .name = NFC_GENL_NAME,
1767 .version = NFC_GENL_VERSION,
1768 .maxattr = NFC_ATTR_MAX,
1769 .policy = nfc_genl_policy,
1770 .module = THIS_MODULE,
1771 .ops = nfc_genl_ops,
1772 .n_ops = ARRAY_SIZE(nfc_genl_ops),
1773 .mcgrps = nfc_genl_mcgrps,
1774 .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps),
1775 };
1776
1777
1778 struct urelease_work {
1779 struct work_struct w;
1780 u32 portid;
1781 };
1782
1783 static void nfc_urelease_event_work(struct work_struct *work)
1784 {
1785 struct urelease_work *w = container_of(work, struct urelease_work, w);
1786 struct class_dev_iter iter;
1787 struct nfc_dev *dev;
1788
1789 pr_debug("portid %d\n", w->portid);
1790
1791 mutex_lock(&nfc_devlist_mutex);
1792
1793 nfc_device_iter_init(&iter);
1794 dev = nfc_device_iter_next(&iter);
1795
1796 while (dev) {
1797 mutex_lock(&dev->genl_data.genl_data_mutex);
1798
1799 if (dev->genl_data.poll_req_portid == w->portid) {
1800 nfc_stop_poll(dev);
1801 dev->genl_data.poll_req_portid = 0;
1802 }
1803
1804 mutex_unlock(&dev->genl_data.genl_data_mutex);
1805
1806 dev = nfc_device_iter_next(&iter);
1807 }
1808
1809 nfc_device_iter_exit(&iter);
1810
1811 mutex_unlock(&nfc_devlist_mutex);
1812
1813 kfree(w);
1814 }
1815
1816 static int nfc_genl_rcv_nl_event(struct notifier_block *this,
1817 unsigned long event, void *ptr)
1818 {
1819 struct netlink_notify *n = ptr;
1820 struct urelease_work *w;
1821
1822 if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
1823 goto out;
1824
1825 pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
1826
1827 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1828 if (w) {
1829 INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
1830 w->portid = n->portid;
1831 schedule_work((struct work_struct *) w);
1832 }
1833
1834 out:
1835 return NOTIFY_DONE;
1836 }
1837
1838 void nfc_genl_data_init(struct nfc_genl_data *genl_data)
1839 {
1840 genl_data->poll_req_portid = 0;
1841 mutex_init(&genl_data->genl_data_mutex);
1842 }
1843
1844 void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
1845 {
1846 mutex_destroy(&genl_data->genl_data_mutex);
1847 }
1848
1849 static struct notifier_block nl_notifier = {
1850 .notifier_call = nfc_genl_rcv_nl_event,
1851 };
1852
1853
1854
1855
1856
1857
1858 int __init nfc_genl_init(void)
1859 {
1860 int rc;
1861
1862 rc = genl_register_family(&nfc_genl_family);
1863 if (rc)
1864 return rc;
1865
1866 netlink_register_notifier(&nl_notifier);
1867
1868 return 0;
1869 }
1870
1871
1872
1873
1874
1875
1876 void nfc_genl_exit(void)
1877 {
1878 netlink_unregister_notifier(&nl_notifier);
1879 genl_unregister_family(&nfc_genl_family);
1880 }