Commit | Line | Data |
---|---|---|
4d12b8b1 LRV |
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 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the | |
20 | * Free Software Foundation, Inc., | |
21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 | */ | |
23 | ||
52858b51 | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
20c239c1 | 25 | |
4d12b8b1 LRV |
26 | #include <net/genetlink.h> |
27 | #include <linux/nfc.h> | |
28 | #include <linux/slab.h> | |
29 | ||
30 | #include "nfc.h" | |
30cc4587 | 31 | #include "llcp.h" |
52feb444 | 32 | |
4d12b8b1 LRV |
33 | static struct genl_multicast_group nfc_genl_event_mcgrp = { |
34 | .name = NFC_GENL_MCAST_EVENT_NAME, | |
35 | }; | |
36 | ||
e5fe4cf8 | 37 | static struct genl_family nfc_genl_family = { |
4d12b8b1 LRV |
38 | .id = GENL_ID_GENERATE, |
39 | .hdrsize = 0, | |
40 | .name = NFC_GENL_NAME, | |
41 | .version = NFC_GENL_VERSION, | |
42 | .maxattr = NFC_ATTR_MAX, | |
43 | }; | |
44 | ||
45 | static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = { | |
46 | [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 }, | |
47 | [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING, | |
48 | .len = NFC_DEVICE_NAME_MAXSIZE }, | |
49 | [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 }, | |
1ed28f61 SO |
50 | [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 }, |
51 | [NFC_ATTR_RF_MODE] = { .type = NLA_U8 }, | |
c970a1ac | 52 | [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 }, |
fe7c5800 SO |
53 | [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 }, |
54 | [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 }, | |
8af362d1 TE |
55 | [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 }, |
56 | [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 }, | |
57 | [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 }, | |
d9b8d8e1 | 58 | [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED }, |
9674da87 EL |
59 | [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING, |
60 | .len = NFC_FIRMWARE_NAME_MAXSIZE }, | |
5ce3f32b | 61 | [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY }, |
d9b8d8e1 TE |
62 | }; |
63 | ||
64 | static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = { | |
65 | [NFC_SDP_ATTR_URI] = { .type = NLA_STRING }, | |
66 | [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 }, | |
4d12b8b1 LRV |
67 | }; |
68 | ||
69 | static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target, | |
0a40acb2 | 70 | struct netlink_callback *cb, int flags) |
4d12b8b1 LRV |
71 | { |
72 | void *hdr; | |
73 | ||
15e47304 | 74 | hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
0a40acb2 | 75 | &nfc_genl_family, flags, NFC_CMD_GET_TARGET); |
4d12b8b1 LRV |
76 | if (!hdr) |
77 | return -EMSGSIZE; | |
78 | ||
79 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); | |
80 | ||
1e6428d8 DM |
81 | if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) || |
82 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) || | |
83 | nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) || | |
84 | nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res)) | |
85 | goto nla_put_failure; | |
86 | if (target->nfcid1_len > 0 && | |
87 | nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len, | |
88 | target->nfcid1)) | |
89 | goto nla_put_failure; | |
90 | if (target->sensb_res_len > 0 && | |
91 | nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len, | |
92 | target->sensb_res)) | |
93 | goto nla_put_failure; | |
94 | if (target->sensf_res_len > 0 && | |
95 | nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len, | |
96 | target->sensf_res)) | |
97 | goto nla_put_failure; | |
4d12b8b1 LRV |
98 | |
99 | return genlmsg_end(msg, hdr); | |
100 | ||
101 | nla_put_failure: | |
102 | genlmsg_cancel(msg, hdr); | |
103 | return -EMSGSIZE; | |
104 | } | |
105 | ||
106 | static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb) | |
107 | { | |
108 | struct nfc_dev *dev; | |
109 | int rc; | |
110 | u32 idx; | |
111 | ||
112 | rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize, | |
0a40acb2 SO |
113 | nfc_genl_family.attrbuf, |
114 | nfc_genl_family.maxattr, | |
115 | nfc_genl_policy); | |
4d12b8b1 LRV |
116 | if (rc < 0) |
117 | return ERR_PTR(rc); | |
118 | ||
119 | if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]) | |
120 | return ERR_PTR(-EINVAL); | |
121 | ||
122 | idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]); | |
123 | ||
124 | dev = nfc_get_device(idx); | |
125 | if (!dev) | |
126 | return ERR_PTR(-ENODEV); | |
127 | ||
128 | return dev; | |
129 | } | |
130 | ||
131 | static int nfc_genl_dump_targets(struct sk_buff *skb, | |
0a40acb2 | 132 | struct netlink_callback *cb) |
4d12b8b1 LRV |
133 | { |
134 | int i = cb->args[0]; | |
135 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
136 | int rc; | |
137 | ||
4d12b8b1 LRV |
138 | if (!dev) { |
139 | dev = __get_device_from_cb(cb); | |
140 | if (IS_ERR(dev)) | |
141 | return PTR_ERR(dev); | |
142 | ||
143 | cb->args[1] = (long) dev; | |
144 | } | |
145 | ||
d4ccb132 | 146 | device_lock(&dev->dev); |
4d12b8b1 LRV |
147 | |
148 | cb->seq = dev->targets_generation; | |
149 | ||
150 | while (i < dev->n_targets) { | |
151 | rc = nfc_genl_send_target(skb, &dev->targets[i], cb, | |
0a40acb2 | 152 | NLM_F_MULTI); |
4d12b8b1 LRV |
153 | if (rc < 0) |
154 | break; | |
155 | ||
156 | i++; | |
157 | } | |
158 | ||
d4ccb132 | 159 | device_unlock(&dev->dev); |
4d12b8b1 LRV |
160 | |
161 | cb->args[0] = i; | |
162 | ||
163 | return skb->len; | |
164 | } | |
165 | ||
166 | static int nfc_genl_dump_targets_done(struct netlink_callback *cb) | |
167 | { | |
168 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
169 | ||
4d12b8b1 LRV |
170 | if (dev) |
171 | nfc_put_device(dev); | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
176 | int nfc_genl_targets_found(struct nfc_dev *dev) | |
177 | { | |
178 | struct sk_buff *msg; | |
179 | void *hdr; | |
180 | ||
15e47304 | 181 | dev->genl_data.poll_req_portid = 0; |
4d12b8b1 | 182 | |
58050fce | 183 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
4d12b8b1 LRV |
184 | if (!msg) |
185 | return -ENOMEM; | |
186 | ||
187 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 188 | NFC_EVENT_TARGETS_FOUND); |
4d12b8b1 LRV |
189 | if (!hdr) |
190 | goto free_msg; | |
191 | ||
1e6428d8 DM |
192 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
193 | goto nla_put_failure; | |
4d12b8b1 LRV |
194 | |
195 | genlmsg_end(msg, hdr); | |
196 | ||
68eb5503 JB |
197 | return genlmsg_multicast(&nfc_genl_family, msg, 0, |
198 | nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
4d12b8b1 LRV |
199 | |
200 | nla_put_failure: | |
201 | genlmsg_cancel(msg, hdr); | |
202 | free_msg: | |
203 | nlmsg_free(msg); | |
204 | return -EMSGSIZE; | |
205 | } | |
206 | ||
8112a5c9 SO |
207 | int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx) |
208 | { | |
209 | struct sk_buff *msg; | |
210 | void *hdr; | |
211 | ||
58050fce | 212 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
8112a5c9 SO |
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 | ||
59ef43e6 JL |
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; | |
8112a5c9 SO |
224 | |
225 | genlmsg_end(msg, hdr); | |
226 | ||
68eb5503 JB |
227 | genlmsg_multicast(&nfc_genl_family, msg, 0, |
228 | nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
8112a5c9 SO |
229 | |
230 | return 0; | |
231 | ||
fc40a8c1 SO |
232 | nla_put_failure: |
233 | genlmsg_cancel(msg, hdr); | |
234 | free_msg: | |
235 | nlmsg_free(msg); | |
236 | return -EMSGSIZE; | |
237 | } | |
238 | ||
239 | int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol) | |
240 | { | |
241 | struct sk_buff *msg; | |
242 | void *hdr; | |
243 | ||
58050fce | 244 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
fc40a8c1 SO |
245 | if (!msg) |
246 | return -ENOMEM; | |
247 | ||
248 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
249 | NFC_EVENT_TM_ACTIVATED); | |
250 | if (!hdr) | |
251 | goto free_msg; | |
252 | ||
253 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) | |
254 | goto nla_put_failure; | |
255 | if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol)) | |
256 | goto nla_put_failure; | |
257 | ||
258 | genlmsg_end(msg, hdr); | |
259 | ||
68eb5503 JB |
260 | genlmsg_multicast(&nfc_genl_family, msg, 0, |
261 | nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
fc40a8c1 SO |
262 | |
263 | return 0; | |
264 | ||
265 | nla_put_failure: | |
266 | genlmsg_cancel(msg, hdr); | |
267 | free_msg: | |
268 | nlmsg_free(msg); | |
269 | return -EMSGSIZE; | |
270 | } | |
271 | ||
272 | int nfc_genl_tm_deactivated(struct nfc_dev *dev) | |
273 | { | |
274 | struct sk_buff *msg; | |
275 | void *hdr; | |
276 | ||
58050fce | 277 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
fc40a8c1 SO |
278 | if (!msg) |
279 | return -ENOMEM; | |
280 | ||
281 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
282 | NFC_EVENT_TM_DEACTIVATED); | |
283 | if (!hdr) | |
284 | goto free_msg; | |
285 | ||
286 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) | |
287 | goto nla_put_failure; | |
288 | ||
289 | genlmsg_end(msg, hdr); | |
290 | ||
68eb5503 JB |
291 | genlmsg_multicast(&nfc_genl_family, msg, 0, |
292 | nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
fc40a8c1 SO |
293 | |
294 | return 0; | |
295 | ||
8112a5c9 SO |
296 | nla_put_failure: |
297 | genlmsg_cancel(msg, hdr); | |
298 | free_msg: | |
299 | nlmsg_free(msg); | |
300 | return -EMSGSIZE; | |
301 | } | |
302 | ||
4d12b8b1 LRV |
303 | int nfc_genl_device_added(struct nfc_dev *dev) |
304 | { | |
305 | struct sk_buff *msg; | |
306 | void *hdr; | |
307 | ||
58050fce | 308 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
4d12b8b1 LRV |
309 | if (!msg) |
310 | return -ENOMEM; | |
311 | ||
312 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 313 | NFC_EVENT_DEVICE_ADDED); |
4d12b8b1 LRV |
314 | if (!hdr) |
315 | goto free_msg; | |
316 | ||
1e6428d8 DM |
317 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
318 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || | |
319 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || | |
320 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up)) | |
321 | goto nla_put_failure; | |
4d12b8b1 LRV |
322 | |
323 | genlmsg_end(msg, hdr); | |
324 | ||
68eb5503 JB |
325 | genlmsg_multicast(&nfc_genl_family, msg, 0, |
326 | nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
4d12b8b1 LRV |
327 | |
328 | return 0; | |
329 | ||
330 | nla_put_failure: | |
331 | genlmsg_cancel(msg, hdr); | |
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 | ||
58050fce | 342 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
4d12b8b1 LRV |
343 | if (!msg) |
344 | return -ENOMEM; | |
345 | ||
346 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 347 | NFC_EVENT_DEVICE_REMOVED); |
4d12b8b1 LRV |
348 | if (!hdr) |
349 | goto free_msg; | |
350 | ||
1e6428d8 DM |
351 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
352 | goto nla_put_failure; | |
4d12b8b1 LRV |
353 | |
354 | genlmsg_end(msg, hdr); | |
355 | ||
68eb5503 JB |
356 | genlmsg_multicast(&nfc_genl_family, msg, 0, |
357 | nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
4d12b8b1 LRV |
358 | |
359 | return 0; | |
360 | ||
361 | nla_put_failure: | |
362 | genlmsg_cancel(msg, hdr); | |
363 | free_msg: | |
364 | nlmsg_free(msg); | |
365 | return -EMSGSIZE; | |
366 | } | |
367 | ||
d9b8d8e1 TE |
368 | int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list) |
369 | { | |
370 | struct sk_buff *msg; | |
371 | struct nlattr *sdp_attr, *uri_attr; | |
372 | struct nfc_llcp_sdp_tlv *sdres; | |
373 | struct hlist_node *n; | |
374 | void *hdr; | |
375 | int rc = -EMSGSIZE; | |
376 | int i; | |
377 | ||
378 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
379 | if (!msg) | |
380 | return -ENOMEM; | |
381 | ||
382 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
383 | NFC_EVENT_LLC_SDRES); | |
384 | if (!hdr) | |
385 | goto free_msg; | |
386 | ||
387 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) | |
388 | goto nla_put_failure; | |
389 | ||
390 | sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP); | |
391 | if (sdp_attr == NULL) { | |
392 | rc = -ENOMEM; | |
393 | goto nla_put_failure; | |
394 | } | |
395 | ||
396 | i = 1; | |
397 | hlist_for_each_entry_safe(sdres, n, sdres_list, node) { | |
398 | pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap); | |
399 | ||
400 | uri_attr = nla_nest_start(msg, i++); | |
401 | if (uri_attr == NULL) { | |
402 | rc = -ENOMEM; | |
403 | goto nla_put_failure; | |
404 | } | |
405 | ||
406 | if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap)) | |
407 | goto nla_put_failure; | |
408 | ||
409 | if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri)) | |
410 | goto nla_put_failure; | |
411 | ||
412 | nla_nest_end(msg, uri_attr); | |
413 | ||
414 | hlist_del(&sdres->node); | |
415 | ||
416 | nfc_llcp_free_sdp_tlv(sdres); | |
417 | } | |
418 | ||
419 | nla_nest_end(msg, sdp_attr); | |
420 | ||
421 | genlmsg_end(msg, hdr); | |
422 | ||
68eb5503 JB |
423 | return genlmsg_multicast(&nfc_genl_family, msg, 0, |
424 | nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
d9b8d8e1 TE |
425 | |
426 | nla_put_failure: | |
427 | genlmsg_cancel(msg, hdr); | |
428 | ||
429 | free_msg: | |
430 | nlmsg_free(msg); | |
431 | ||
432 | nfc_llcp_free_sdp_tlv_list(sdres_list); | |
433 | ||
434 | return rc; | |
435 | } | |
436 | ||
2757c372 SO |
437 | int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type) |
438 | { | |
439 | struct sk_buff *msg; | |
440 | void *hdr; | |
441 | ||
442 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
443 | if (!msg) | |
444 | return -ENOMEM; | |
445 | ||
446 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
447 | NFC_EVENT_SE_ADDED); | |
448 | if (!hdr) | |
449 | goto free_msg; | |
450 | ||
451 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || | |
452 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) || | |
453 | nla_put_u8(msg, NFC_ATTR_SE_TYPE, type)) | |
454 | goto nla_put_failure; | |
455 | ||
456 | genlmsg_end(msg, hdr); | |
457 | ||
68eb5503 JB |
458 | genlmsg_multicast(&nfc_genl_family, msg, 0, |
459 | nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
2757c372 SO |
460 | |
461 | return 0; | |
462 | ||
463 | nla_put_failure: | |
464 | genlmsg_cancel(msg, hdr); | |
465 | free_msg: | |
466 | nlmsg_free(msg); | |
467 | return -EMSGSIZE; | |
468 | } | |
469 | ||
470 | int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx) | |
471 | { | |
472 | struct sk_buff *msg; | |
473 | void *hdr; | |
474 | ||
475 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
476 | if (!msg) | |
477 | return -ENOMEM; | |
478 | ||
479 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
480 | NFC_EVENT_SE_REMOVED); | |
481 | if (!hdr) | |
482 | goto free_msg; | |
483 | ||
484 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || | |
485 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx)) | |
486 | goto nla_put_failure; | |
487 | ||
488 | genlmsg_end(msg, hdr); | |
489 | ||
68eb5503 JB |
490 | genlmsg_multicast(&nfc_genl_family, msg, 0, |
491 | nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
2757c372 SO |
492 | |
493 | return 0; | |
494 | ||
495 | nla_put_failure: | |
496 | genlmsg_cancel(msg, hdr); | |
497 | free_msg: | |
498 | nlmsg_free(msg); | |
499 | return -EMSGSIZE; | |
500 | } | |
501 | ||
4d12b8b1 | 502 | static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev, |
15e47304 | 503 | u32 portid, u32 seq, |
0a40acb2 SO |
504 | struct netlink_callback *cb, |
505 | int flags) | |
4d12b8b1 LRV |
506 | { |
507 | void *hdr; | |
508 | ||
15e47304 | 509 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, |
0a40acb2 | 510 | NFC_CMD_GET_DEVICE); |
4d12b8b1 LRV |
511 | if (!hdr) |
512 | return -EMSGSIZE; | |
513 | ||
514 | if (cb) | |
515 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); | |
516 | ||
1e6428d8 DM |
517 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
518 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || | |
519 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || | |
7ad39395 TE |
520 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) || |
521 | nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode)) | |
1e6428d8 | 522 | goto nla_put_failure; |
4d12b8b1 LRV |
523 | |
524 | return genlmsg_end(msg, hdr); | |
525 | ||
526 | nla_put_failure: | |
527 | genlmsg_cancel(msg, hdr); | |
528 | return -EMSGSIZE; | |
529 | } | |
530 | ||
531 | static int nfc_genl_dump_devices(struct sk_buff *skb, | |
0a40acb2 | 532 | struct netlink_callback *cb) |
4d12b8b1 LRV |
533 | { |
534 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | |
535 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
536 | bool first_call = false; | |
537 | ||
4d12b8b1 LRV |
538 | if (!iter) { |
539 | first_call = true; | |
540 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); | |
541 | if (!iter) | |
542 | return -ENOMEM; | |
543 | cb->args[0] = (long) iter; | |
544 | } | |
545 | ||
546 | mutex_lock(&nfc_devlist_mutex); | |
547 | ||
548 | cb->seq = nfc_devlist_generation; | |
549 | ||
550 | if (first_call) { | |
551 | nfc_device_iter_init(iter); | |
552 | dev = nfc_device_iter_next(iter); | |
553 | } | |
554 | ||
555 | while (dev) { | |
556 | int rc; | |
557 | ||
15e47304 | 558 | rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid, |
0a40acb2 | 559 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
4d12b8b1 LRV |
560 | if (rc < 0) |
561 | break; | |
562 | ||
563 | dev = nfc_device_iter_next(iter); | |
564 | } | |
565 | ||
566 | mutex_unlock(&nfc_devlist_mutex); | |
567 | ||
568 | cb->args[1] = (long) dev; | |
569 | ||
570 | return skb->len; | |
571 | } | |
572 | ||
573 | static int nfc_genl_dump_devices_done(struct netlink_callback *cb) | |
574 | { | |
575 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | |
576 | ||
4d12b8b1 LRV |
577 | nfc_device_iter_exit(iter); |
578 | kfree(iter); | |
579 | ||
580 | return 0; | |
581 | } | |
582 | ||
1ed28f61 | 583 | int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx, |
0a40acb2 | 584 | u8 comm_mode, u8 rf_mode) |
1ed28f61 SO |
585 | { |
586 | struct sk_buff *msg; | |
587 | void *hdr; | |
588 | ||
589 | pr_debug("DEP link is up\n"); | |
590 | ||
58050fce | 591 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1ed28f61 SO |
592 | if (!msg) |
593 | return -ENOMEM; | |
594 | ||
0a40acb2 | 595 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP); |
1ed28f61 SO |
596 | if (!hdr) |
597 | goto free_msg; | |
598 | ||
1e6428d8 DM |
599 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
600 | goto nla_put_failure; | |
601 | if (rf_mode == NFC_RF_INITIATOR && | |
602 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) | |
603 | goto nla_put_failure; | |
604 | if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) || | |
605 | nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode)) | |
606 | goto nla_put_failure; | |
1ed28f61 SO |
607 | |
608 | genlmsg_end(msg, hdr); | |
609 | ||
610 | dev->dep_link_up = true; | |
611 | ||
68eb5503 JB |
612 | genlmsg_multicast(&nfc_genl_family, msg, 0, |
613 | nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
1ed28f61 SO |
614 | |
615 | return 0; | |
616 | ||
617 | nla_put_failure: | |
618 | genlmsg_cancel(msg, hdr); | |
619 | free_msg: | |
620 | nlmsg_free(msg); | |
621 | return -EMSGSIZE; | |
622 | } | |
623 | ||
624 | int nfc_genl_dep_link_down_event(struct nfc_dev *dev) | |
625 | { | |
626 | struct sk_buff *msg; | |
627 | void *hdr; | |
628 | ||
629 | pr_debug("DEP link is down\n"); | |
630 | ||
58050fce | 631 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1ed28f61 SO |
632 | if (!msg) |
633 | return -ENOMEM; | |
634 | ||
635 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 636 | NFC_CMD_DEP_LINK_DOWN); |
1ed28f61 SO |
637 | if (!hdr) |
638 | goto free_msg; | |
639 | ||
1e6428d8 DM |
640 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
641 | goto nla_put_failure; | |
1ed28f61 SO |
642 | |
643 | genlmsg_end(msg, hdr); | |
644 | ||
68eb5503 JB |
645 | genlmsg_multicast(&nfc_genl_family, msg, 0, |
646 | nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
1ed28f61 SO |
647 | |
648 | return 0; | |
649 | ||
650 | nla_put_failure: | |
651 | genlmsg_cancel(msg, hdr); | |
652 | free_msg: | |
653 | nlmsg_free(msg); | |
654 | return -EMSGSIZE; | |
655 | } | |
656 | ||
4d12b8b1 LRV |
657 | static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) |
658 | { | |
659 | struct sk_buff *msg; | |
660 | struct nfc_dev *dev; | |
661 | u32 idx; | |
662 | int rc = -ENOBUFS; | |
663 | ||
4d12b8b1 LRV |
664 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
665 | return -EINVAL; | |
666 | ||
667 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
668 | ||
669 | dev = nfc_get_device(idx); | |
670 | if (!dev) | |
671 | return -ENODEV; | |
672 | ||
58050fce | 673 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
4d12b8b1 LRV |
674 | if (!msg) { |
675 | rc = -ENOMEM; | |
676 | goto out_putdev; | |
677 | } | |
678 | ||
15e47304 | 679 | rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq, |
0a40acb2 | 680 | NULL, 0); |
4d12b8b1 LRV |
681 | if (rc < 0) |
682 | goto out_free; | |
683 | ||
684 | nfc_put_device(dev); | |
685 | ||
686 | return genlmsg_reply(msg, info); | |
687 | ||
688 | out_free: | |
689 | nlmsg_free(msg); | |
690 | out_putdev: | |
691 | nfc_put_device(dev); | |
692 | return rc; | |
693 | } | |
694 | ||
8b3fe7b5 IE |
695 | static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) |
696 | { | |
697 | struct nfc_dev *dev; | |
698 | int rc; | |
699 | u32 idx; | |
700 | ||
8b3fe7b5 IE |
701 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
702 | return -EINVAL; | |
703 | ||
704 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
705 | ||
706 | dev = nfc_get_device(idx); | |
707 | if (!dev) | |
708 | return -ENODEV; | |
709 | ||
710 | rc = nfc_dev_up(dev); | |
711 | ||
712 | nfc_put_device(dev); | |
713 | return rc; | |
714 | } | |
715 | ||
716 | static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) | |
717 | { | |
718 | struct nfc_dev *dev; | |
719 | int rc; | |
720 | u32 idx; | |
721 | ||
8b3fe7b5 IE |
722 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
723 | return -EINVAL; | |
724 | ||
725 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
726 | ||
727 | dev = nfc_get_device(idx); | |
728 | if (!dev) | |
729 | return -ENODEV; | |
730 | ||
731 | rc = nfc_dev_down(dev); | |
732 | ||
733 | nfc_put_device(dev); | |
734 | return rc; | |
735 | } | |
736 | ||
4d12b8b1 LRV |
737 | static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) |
738 | { | |
739 | struct nfc_dev *dev; | |
740 | int rc; | |
741 | u32 idx; | |
fe7c5800 | 742 | u32 im_protocols = 0, tm_protocols = 0; |
4d12b8b1 | 743 | |
1ed28f61 SO |
744 | pr_debug("Poll start\n"); |
745 | ||
4d12b8b1 | 746 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
fe7c5800 SO |
747 | ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] && |
748 | !info->attrs[NFC_ATTR_PROTOCOLS]) && | |
0f450772 | 749 | !info->attrs[NFC_ATTR_TM_PROTOCOLS])) |
4d12b8b1 LRV |
750 | return -EINVAL; |
751 | ||
752 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
fe7c5800 SO |
753 | |
754 | if (info->attrs[NFC_ATTR_TM_PROTOCOLS]) | |
755 | tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]); | |
fe7c5800 SO |
756 | |
757 | if (info->attrs[NFC_ATTR_IM_PROTOCOLS]) | |
758 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]); | |
5e50ee3a SO |
759 | else if (info->attrs[NFC_ATTR_PROTOCOLS]) |
760 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); | |
4d12b8b1 LRV |
761 | |
762 | dev = nfc_get_device(idx); | |
763 | if (!dev) | |
764 | return -ENODEV; | |
765 | ||
766 | mutex_lock(&dev->genl_data.genl_data_mutex); | |
767 | ||
fe7c5800 | 768 | rc = nfc_start_poll(dev, im_protocols, tm_protocols); |
4d12b8b1 | 769 | if (!rc) |
15e47304 | 770 | dev->genl_data.poll_req_portid = info->snd_portid; |
4d12b8b1 LRV |
771 | |
772 | mutex_unlock(&dev->genl_data.genl_data_mutex); | |
773 | ||
774 | nfc_put_device(dev); | |
775 | return rc; | |
776 | } | |
777 | ||
778 | static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) | |
779 | { | |
780 | struct nfc_dev *dev; | |
781 | int rc; | |
782 | u32 idx; | |
783 | ||
4d12b8b1 LRV |
784 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
785 | return -EINVAL; | |
786 | ||
787 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
788 | ||
789 | dev = nfc_get_device(idx); | |
790 | if (!dev) | |
791 | return -ENODEV; | |
792 | ||
a831b913 SO |
793 | device_lock(&dev->dev); |
794 | ||
795 | if (!dev->polling) { | |
796 | device_unlock(&dev->dev); | |
797 | return -EINVAL; | |
798 | } | |
799 | ||
800 | device_unlock(&dev->dev); | |
801 | ||
4d12b8b1 LRV |
802 | mutex_lock(&dev->genl_data.genl_data_mutex); |
803 | ||
15e47304 | 804 | if (dev->genl_data.poll_req_portid != info->snd_portid) { |
4d12b8b1 LRV |
805 | rc = -EBUSY; |
806 | goto out; | |
807 | } | |
808 | ||
809 | rc = nfc_stop_poll(dev); | |
15e47304 | 810 | dev->genl_data.poll_req_portid = 0; |
4d12b8b1 LRV |
811 | |
812 | out: | |
813 | mutex_unlock(&dev->genl_data.genl_data_mutex); | |
814 | nfc_put_device(dev); | |
815 | return rc; | |
816 | } | |
817 | ||
1ed28f61 SO |
818 | static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info) |
819 | { | |
820 | struct nfc_dev *dev; | |
821 | int rc, tgt_idx; | |
822 | u32 idx; | |
47807d3d | 823 | u8 comm; |
1ed28f61 SO |
824 | |
825 | pr_debug("DEP link up\n"); | |
826 | ||
827 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | |
47807d3d | 828 | !info->attrs[NFC_ATTR_COMM_MODE]) |
1ed28f61 SO |
829 | return -EINVAL; |
830 | ||
831 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
832 | if (!info->attrs[NFC_ATTR_TARGET_INDEX]) | |
833 | tgt_idx = NFC_TARGET_IDX_ANY; | |
834 | else | |
835 | tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); | |
836 | ||
837 | comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]); | |
1ed28f61 SO |
838 | |
839 | if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE) | |
840 | return -EINVAL; | |
841 | ||
1ed28f61 SO |
842 | dev = nfc_get_device(idx); |
843 | if (!dev) | |
844 | return -ENODEV; | |
845 | ||
47807d3d | 846 | rc = nfc_dep_link_up(dev, tgt_idx, comm); |
1ed28f61 SO |
847 | |
848 | nfc_put_device(dev); | |
849 | ||
850 | return rc; | |
851 | } | |
852 | ||
853 | static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info) | |
854 | { | |
855 | struct nfc_dev *dev; | |
856 | int rc; | |
857 | u32 idx; | |
858 | ||
859 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) | |
860 | return -EINVAL; | |
861 | ||
862 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
863 | ||
864 | dev = nfc_get_device(idx); | |
865 | if (!dev) | |
866 | return -ENODEV; | |
867 | ||
868 | rc = nfc_dep_link_down(dev); | |
869 | ||
870 | nfc_put_device(dev); | |
871 | return rc; | |
872 | } | |
873 | ||
52feb444 TE |
874 | static int nfc_genl_send_params(struct sk_buff *msg, |
875 | struct nfc_llcp_local *local, | |
876 | u32 portid, u32 seq) | |
877 | { | |
878 | void *hdr; | |
879 | ||
880 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0, | |
881 | NFC_CMD_LLC_GET_PARAMS); | |
882 | if (!hdr) | |
883 | return -EMSGSIZE; | |
884 | ||
885 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) || | |
886 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) || | |
887 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) || | |
888 | nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux))) | |
889 | goto nla_put_failure; | |
890 | ||
891 | return genlmsg_end(msg, hdr); | |
892 | ||
893 | nla_put_failure: | |
894 | ||
895 | genlmsg_cancel(msg, hdr); | |
896 | return -EMSGSIZE; | |
897 | } | |
898 | ||
899 | static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info) | |
900 | { | |
901 | struct nfc_dev *dev; | |
902 | struct nfc_llcp_local *local; | |
903 | int rc = 0; | |
904 | struct sk_buff *msg = NULL; | |
905 | u32 idx; | |
906 | ||
907 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) | |
908 | return -EINVAL; | |
909 | ||
910 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
911 | ||
912 | dev = nfc_get_device(idx); | |
913 | if (!dev) | |
914 | return -ENODEV; | |
915 | ||
916 | device_lock(&dev->dev); | |
917 | ||
918 | local = nfc_llcp_find_local(dev); | |
919 | if (!local) { | |
920 | rc = -ENODEV; | |
921 | goto exit; | |
922 | } | |
923 | ||
924 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
925 | if (!msg) { | |
926 | rc = -ENOMEM; | |
927 | goto exit; | |
928 | } | |
929 | ||
930 | rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq); | |
931 | ||
932 | exit: | |
933 | device_unlock(&dev->dev); | |
934 | ||
935 | nfc_put_device(dev); | |
936 | ||
937 | if (rc < 0) { | |
938 | if (msg) | |
939 | nlmsg_free(msg); | |
940 | ||
941 | return rc; | |
942 | } | |
943 | ||
944 | return genlmsg_reply(msg, info); | |
945 | } | |
946 | ||
947 | static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info) | |
948 | { | |
949 | struct nfc_dev *dev; | |
950 | struct nfc_llcp_local *local; | |
951 | u8 rw = 0; | |
952 | u16 miux = 0; | |
953 | u32 idx; | |
954 | int rc = 0; | |
955 | ||
956 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | |
957 | (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] && | |
958 | !info->attrs[NFC_ATTR_LLC_PARAM_RW] && | |
959 | !info->attrs[NFC_ATTR_LLC_PARAM_MIUX])) | |
960 | return -EINVAL; | |
961 | ||
962 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) { | |
963 | rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]); | |
964 | ||
965 | if (rw > LLCP_MAX_RW) | |
966 | return -EINVAL; | |
967 | } | |
968 | ||
969 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) { | |
970 | miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]); | |
971 | ||
972 | if (miux > LLCP_MAX_MIUX) | |
973 | return -EINVAL; | |
974 | } | |
975 | ||
976 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
977 | ||
978 | dev = nfc_get_device(idx); | |
979 | if (!dev) | |
980 | return -ENODEV; | |
981 | ||
982 | device_lock(&dev->dev); | |
983 | ||
984 | local = nfc_llcp_find_local(dev); | |
985 | if (!local) { | |
986 | nfc_put_device(dev); | |
987 | rc = -ENODEV; | |
988 | goto exit; | |
989 | } | |
990 | ||
991 | if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) { | |
992 | if (dev->dep_link_up) { | |
993 | rc = -EINPROGRESS; | |
994 | goto exit; | |
995 | } | |
996 | ||
997 | local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]); | |
998 | } | |
999 | ||
1000 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) | |
1001 | local->rw = rw; | |
1002 | ||
1003 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) | |
1004 | local->miux = cpu_to_be16(miux); | |
1005 | ||
1006 | exit: | |
1007 | device_unlock(&dev->dev); | |
1008 | ||
1009 | nfc_put_device(dev); | |
1010 | ||
1011 | return rc; | |
1012 | } | |
1013 | ||
d9b8d8e1 TE |
1014 | static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info) |
1015 | { | |
1016 | struct nfc_dev *dev; | |
1017 | struct nfc_llcp_local *local; | |
1018 | struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1]; | |
1019 | u32 idx; | |
1020 | u8 tid; | |
1021 | char *uri; | |
1022 | int rc = 0, rem; | |
1023 | size_t uri_len, tlvs_len; | |
1024 | struct hlist_head sdreq_list; | |
1025 | struct nfc_llcp_sdp_tlv *sdreq; | |
1026 | ||
1027 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | |
1028 | !info->attrs[NFC_ATTR_LLC_SDP]) | |
1029 | return -EINVAL; | |
1030 | ||
1031 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
1032 | ||
1033 | dev = nfc_get_device(idx); | |
1034 | if (!dev) { | |
1035 | rc = -ENODEV; | |
1036 | goto exit; | |
1037 | } | |
1038 | ||
1039 | device_lock(&dev->dev); | |
1040 | ||
1041 | if (dev->dep_link_up == false) { | |
1042 | rc = -ENOLINK; | |
1043 | goto exit; | |
1044 | } | |
1045 | ||
1046 | local = nfc_llcp_find_local(dev); | |
1047 | if (!local) { | |
1048 | nfc_put_device(dev); | |
1049 | rc = -ENODEV; | |
1050 | goto exit; | |
1051 | } | |
1052 | ||
1053 | INIT_HLIST_HEAD(&sdreq_list); | |
1054 | ||
1055 | tlvs_len = 0; | |
1056 | ||
1057 | nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) { | |
1058 | rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr, | |
1059 | nfc_sdp_genl_policy); | |
1060 | ||
1061 | if (rc != 0) { | |
1062 | rc = -EINVAL; | |
1063 | goto exit; | |
1064 | } | |
1065 | ||
1066 | if (!sdp_attrs[NFC_SDP_ATTR_URI]) | |
1067 | continue; | |
1068 | ||
1069 | uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]); | |
1070 | if (uri_len == 0) | |
1071 | continue; | |
1072 | ||
1073 | uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]); | |
1074 | if (uri == NULL || *uri == 0) | |
1075 | continue; | |
1076 | ||
1077 | tid = local->sdreq_next_tid++; | |
1078 | ||
1079 | sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len); | |
1080 | if (sdreq == NULL) { | |
1081 | rc = -ENOMEM; | |
1082 | goto exit; | |
1083 | } | |
1084 | ||
1085 | tlvs_len += sdreq->tlv_len; | |
1086 | ||
1087 | hlist_add_head(&sdreq->node, &sdreq_list); | |
1088 | } | |
1089 | ||
1090 | if (hlist_empty(&sdreq_list)) { | |
1091 | rc = -EINVAL; | |
1092 | goto exit; | |
1093 | } | |
1094 | ||
1095 | rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len); | |
1096 | exit: | |
1097 | device_unlock(&dev->dev); | |
1098 | ||
1099 | nfc_put_device(dev); | |
1100 | ||
1101 | return rc; | |
1102 | } | |
1103 | ||
9ea7187c | 1104 | static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info) |
9674da87 EL |
1105 | { |
1106 | struct nfc_dev *dev; | |
1107 | int rc; | |
1108 | u32 idx; | |
1109 | char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1]; | |
1110 | ||
1111 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) | |
1112 | return -EINVAL; | |
1113 | ||
1114 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
1115 | ||
1116 | dev = nfc_get_device(idx); | |
1117 | if (!dev) | |
1118 | return -ENODEV; | |
1119 | ||
1120 | nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME], | |
1121 | sizeof(firmware_name)); | |
1122 | ||
9ea7187c | 1123 | rc = nfc_fw_download(dev, firmware_name); |
9674da87 EL |
1124 | |
1125 | nfc_put_device(dev); | |
1126 | return rc; | |
1127 | } | |
1128 | ||
352a5f5f EL |
1129 | int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name, |
1130 | u32 result) | |
9674da87 EL |
1131 | { |
1132 | struct sk_buff *msg; | |
1133 | void *hdr; | |
1134 | ||
1135 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
1136 | if (!msg) | |
1137 | return -ENOMEM; | |
1138 | ||
1139 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
9ea7187c | 1140 | NFC_CMD_FW_DOWNLOAD); |
9674da87 EL |
1141 | if (!hdr) |
1142 | goto free_msg; | |
1143 | ||
1144 | if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) || | |
352a5f5f | 1145 | nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) || |
9674da87 EL |
1146 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
1147 | goto nla_put_failure; | |
1148 | ||
1149 | genlmsg_end(msg, hdr); | |
1150 | ||
68eb5503 JB |
1151 | genlmsg_multicast(&nfc_genl_family, msg, 0, |
1152 | nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
9674da87 EL |
1153 | |
1154 | return 0; | |
1155 | ||
1156 | nla_put_failure: | |
1157 | genlmsg_cancel(msg, hdr); | |
1158 | free_msg: | |
1159 | nlmsg_free(msg); | |
1160 | return -EMSGSIZE; | |
1161 | } | |
1162 | ||
be085653 SO |
1163 | static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info) |
1164 | { | |
1165 | struct nfc_dev *dev; | |
1166 | int rc; | |
1167 | u32 idx, se_idx; | |
1168 | ||
1169 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | |
1170 | !info->attrs[NFC_ATTR_SE_INDEX]) | |
1171 | return -EINVAL; | |
1172 | ||
1173 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
1174 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); | |
1175 | ||
1176 | dev = nfc_get_device(idx); | |
1177 | if (!dev) | |
1178 | return -ENODEV; | |
1179 | ||
1180 | rc = nfc_enable_se(dev, se_idx); | |
1181 | ||
1182 | nfc_put_device(dev); | |
1183 | return rc; | |
1184 | } | |
1185 | ||
1186 | static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info) | |
1187 | { | |
1188 | struct nfc_dev *dev; | |
1189 | int rc; | |
1190 | u32 idx, se_idx; | |
1191 | ||
1192 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | |
1193 | !info->attrs[NFC_ATTR_SE_INDEX]) | |
1194 | return -EINVAL; | |
1195 | ||
1196 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
1197 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); | |
1198 | ||
1199 | dev = nfc_get_device(idx); | |
1200 | if (!dev) | |
1201 | return -ENODEV; | |
1202 | ||
1203 | rc = nfc_disable_se(dev, se_idx); | |
1204 | ||
1205 | nfc_put_device(dev); | |
1206 | return rc; | |
1207 | } | |
1208 | ||
ac22ac46 SO |
1209 | static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev, |
1210 | u32 portid, u32 seq, | |
1211 | struct netlink_callback *cb, | |
1212 | int flags) | |
1213 | { | |
1214 | void *hdr; | |
1215 | struct nfc_se *se, *n; | |
1216 | ||
1217 | list_for_each_entry_safe(se, n, &dev->secure_elements, list) { | |
1218 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, | |
1219 | NFC_CMD_GET_SE); | |
1220 | if (!hdr) | |
1221 | goto nla_put_failure; | |
1222 | ||
1223 | if (cb) | |
1224 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); | |
1225 | ||
1226 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || | |
1227 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) || | |
1228 | nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type)) | |
1229 | goto nla_put_failure; | |
1230 | ||
1231 | if (genlmsg_end(msg, hdr) < 0) | |
1232 | goto nla_put_failure; | |
1233 | } | |
1234 | ||
1235 | return 0; | |
1236 | ||
1237 | nla_put_failure: | |
1238 | genlmsg_cancel(msg, hdr); | |
1239 | return -EMSGSIZE; | |
1240 | } | |
1241 | ||
1242 | static int nfc_genl_dump_ses(struct sk_buff *skb, | |
1243 | struct netlink_callback *cb) | |
1244 | { | |
1245 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | |
1246 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
1247 | bool first_call = false; | |
1248 | ||
1249 | if (!iter) { | |
1250 | first_call = true; | |
1251 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); | |
1252 | if (!iter) | |
1253 | return -ENOMEM; | |
1254 | cb->args[0] = (long) iter; | |
1255 | } | |
1256 | ||
1257 | mutex_lock(&nfc_devlist_mutex); | |
1258 | ||
1259 | cb->seq = nfc_devlist_generation; | |
1260 | ||
1261 | if (first_call) { | |
1262 | nfc_device_iter_init(iter); | |
1263 | dev = nfc_device_iter_next(iter); | |
1264 | } | |
1265 | ||
1266 | while (dev) { | |
1267 | int rc; | |
1268 | ||
1269 | rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid, | |
1270 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); | |
1271 | if (rc < 0) | |
1272 | break; | |
1273 | ||
1274 | dev = nfc_device_iter_next(iter); | |
1275 | } | |
1276 | ||
1277 | mutex_unlock(&nfc_devlist_mutex); | |
1278 | ||
1279 | cb->args[1] = (long) dev; | |
1280 | ||
1281 | return skb->len; | |
1282 | } | |
1283 | ||
1284 | static int nfc_genl_dump_ses_done(struct netlink_callback *cb) | |
1285 | { | |
1286 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | |
1287 | ||
1288 | nfc_device_iter_exit(iter); | |
1289 | kfree(iter); | |
1290 | ||
1291 | return 0; | |
1292 | } | |
1293 | ||
5ce3f32b SO |
1294 | struct se_io_ctx { |
1295 | u32 dev_idx; | |
1296 | u32 se_idx; | |
1297 | }; | |
1298 | ||
ddc1a70b | 1299 | static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err) |
5ce3f32b SO |
1300 | { |
1301 | struct se_io_ctx *ctx = context; | |
1302 | struct sk_buff *msg; | |
1303 | void *hdr; | |
1304 | ||
1305 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
1306 | if (!msg) { | |
1307 | kfree(ctx); | |
1308 | return; | |
1309 | } | |
1310 | ||
1311 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
1312 | NFC_CMD_SE_IO); | |
1313 | if (!hdr) | |
1314 | goto free_msg; | |
1315 | ||
1316 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) || | |
1317 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) || | |
1318 | nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu)) | |
1319 | goto nla_put_failure; | |
1320 | ||
1321 | genlmsg_end(msg, hdr); | |
1322 | ||
68eb5503 JB |
1323 | genlmsg_multicast(&nfc_genl_family, msg, 0, |
1324 | nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
5ce3f32b SO |
1325 | |
1326 | kfree(ctx); | |
1327 | ||
1328 | return; | |
1329 | ||
1330 | nla_put_failure: | |
1331 | genlmsg_cancel(msg, hdr); | |
1332 | free_msg: | |
1333 | nlmsg_free(msg); | |
1334 | kfree(ctx); | |
1335 | ||
1336 | return; | |
1337 | } | |
1338 | ||
1339 | static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info) | |
1340 | { | |
1341 | struct nfc_dev *dev; | |
1342 | struct se_io_ctx *ctx; | |
1343 | u32 dev_idx, se_idx; | |
1344 | u8 *apdu; | |
1345 | size_t apdu_len; | |
1346 | ||
1347 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | |
1348 | !info->attrs[NFC_ATTR_SE_INDEX] || | |
1349 | !info->attrs[NFC_ATTR_SE_APDU]) | |
1350 | return -EINVAL; | |
1351 | ||
1352 | dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
1353 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); | |
1354 | ||
1355 | dev = nfc_get_device(dev_idx); | |
1356 | if (!dev) | |
1357 | return -ENODEV; | |
1358 | ||
1359 | if (!dev->ops || !dev->ops->se_io) | |
1360 | return -ENOTSUPP; | |
1361 | ||
1362 | apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]); | |
1363 | if (apdu_len == 0) | |
1364 | return -EINVAL; | |
1365 | ||
1366 | apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]); | |
1367 | if (!apdu) | |
1368 | return -EINVAL; | |
1369 | ||
1370 | ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL); | |
1371 | if (!ctx) | |
1372 | return -ENOMEM; | |
1373 | ||
1374 | ctx->dev_idx = dev_idx; | |
1375 | ctx->se_idx = se_idx; | |
1376 | ||
1377 | return dev->ops->se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx); | |
1378 | } | |
1379 | ||
4534de83 | 1380 | static const struct genl_ops nfc_genl_ops[] = { |
4d12b8b1 LRV |
1381 | { |
1382 | .cmd = NFC_CMD_GET_DEVICE, | |
1383 | .doit = nfc_genl_get_device, | |
1384 | .dumpit = nfc_genl_dump_devices, | |
1385 | .done = nfc_genl_dump_devices_done, | |
1386 | .policy = nfc_genl_policy, | |
8b3fe7b5 IE |
1387 | }, |
1388 | { | |
1389 | .cmd = NFC_CMD_DEV_UP, | |
1390 | .doit = nfc_genl_dev_up, | |
1391 | .policy = nfc_genl_policy, | |
1392 | }, | |
1393 | { | |
1394 | .cmd = NFC_CMD_DEV_DOWN, | |
1395 | .doit = nfc_genl_dev_down, | |
1396 | .policy = nfc_genl_policy, | |
4d12b8b1 LRV |
1397 | }, |
1398 | { | |
1399 | .cmd = NFC_CMD_START_POLL, | |
1400 | .doit = nfc_genl_start_poll, | |
1401 | .policy = nfc_genl_policy, | |
1402 | }, | |
1403 | { | |
1404 | .cmd = NFC_CMD_STOP_POLL, | |
1405 | .doit = nfc_genl_stop_poll, | |
1406 | .policy = nfc_genl_policy, | |
1407 | }, | |
1ed28f61 SO |
1408 | { |
1409 | .cmd = NFC_CMD_DEP_LINK_UP, | |
1410 | .doit = nfc_genl_dep_link_up, | |
1411 | .policy = nfc_genl_policy, | |
1412 | }, | |
1413 | { | |
1414 | .cmd = NFC_CMD_DEP_LINK_DOWN, | |
1415 | .doit = nfc_genl_dep_link_down, | |
1416 | .policy = nfc_genl_policy, | |
1417 | }, | |
4d12b8b1 LRV |
1418 | { |
1419 | .cmd = NFC_CMD_GET_TARGET, | |
1420 | .dumpit = nfc_genl_dump_targets, | |
1421 | .done = nfc_genl_dump_targets_done, | |
1422 | .policy = nfc_genl_policy, | |
1423 | }, | |
52feb444 TE |
1424 | { |
1425 | .cmd = NFC_CMD_LLC_GET_PARAMS, | |
1426 | .doit = nfc_genl_llc_get_params, | |
1427 | .policy = nfc_genl_policy, | |
1428 | }, | |
1429 | { | |
1430 | .cmd = NFC_CMD_LLC_SET_PARAMS, | |
1431 | .doit = nfc_genl_llc_set_params, | |
1432 | .policy = nfc_genl_policy, | |
1433 | }, | |
d9b8d8e1 TE |
1434 | { |
1435 | .cmd = NFC_CMD_LLC_SDREQ, | |
1436 | .doit = nfc_genl_llc_sdreq, | |
1437 | .policy = nfc_genl_policy, | |
1438 | }, | |
9674da87 | 1439 | { |
9ea7187c SO |
1440 | .cmd = NFC_CMD_FW_DOWNLOAD, |
1441 | .doit = nfc_genl_fw_download, | |
9674da87 EL |
1442 | .policy = nfc_genl_policy, |
1443 | }, | |
be085653 SO |
1444 | { |
1445 | .cmd = NFC_CMD_ENABLE_SE, | |
1446 | .doit = nfc_genl_enable_se, | |
1447 | .policy = nfc_genl_policy, | |
1448 | }, | |
1449 | { | |
1450 | .cmd = NFC_CMD_DISABLE_SE, | |
1451 | .doit = nfc_genl_disable_se, | |
1452 | .policy = nfc_genl_policy, | |
1453 | }, | |
ac22ac46 SO |
1454 | { |
1455 | .cmd = NFC_CMD_GET_SE, | |
1456 | .dumpit = nfc_genl_dump_ses, | |
1457 | .done = nfc_genl_dump_ses_done, | |
1458 | .policy = nfc_genl_policy, | |
1459 | }, | |
5ce3f32b SO |
1460 | { |
1461 | .cmd = NFC_CMD_SE_IO, | |
1462 | .doit = nfc_genl_se_io, | |
1463 | .policy = nfc_genl_policy, | |
1464 | }, | |
4d12b8b1 LRV |
1465 | }; |
1466 | ||
3c0cc8aa SJ |
1467 | |
1468 | struct urelease_work { | |
1469 | struct work_struct w; | |
c487606f | 1470 | int portid; |
3c0cc8aa SJ |
1471 | }; |
1472 | ||
1473 | static void nfc_urelease_event_work(struct work_struct *work) | |
4d12b8b1 | 1474 | { |
3c0cc8aa | 1475 | struct urelease_work *w = container_of(work, struct urelease_work, w); |
4d12b8b1 LRV |
1476 | struct class_dev_iter iter; |
1477 | struct nfc_dev *dev; | |
1478 | ||
c487606f | 1479 | pr_debug("portid %d\n", w->portid); |
4d12b8b1 | 1480 | |
3c0cc8aa | 1481 | mutex_lock(&nfc_devlist_mutex); |
4d12b8b1 LRV |
1482 | |
1483 | nfc_device_iter_init(&iter); | |
1484 | dev = nfc_device_iter_next(&iter); | |
1485 | ||
1486 | while (dev) { | |
3c0cc8aa SJ |
1487 | mutex_lock(&dev->genl_data.genl_data_mutex); |
1488 | ||
c487606f | 1489 | if (dev->genl_data.poll_req_portid == w->portid) { |
4d12b8b1 | 1490 | nfc_stop_poll(dev); |
15e47304 | 1491 | dev->genl_data.poll_req_portid = 0; |
4d12b8b1 | 1492 | } |
3c0cc8aa SJ |
1493 | |
1494 | mutex_unlock(&dev->genl_data.genl_data_mutex); | |
1495 | ||
4d12b8b1 LRV |
1496 | dev = nfc_device_iter_next(&iter); |
1497 | } | |
1498 | ||
1499 | nfc_device_iter_exit(&iter); | |
1500 | ||
3c0cc8aa SJ |
1501 | mutex_unlock(&nfc_devlist_mutex); |
1502 | ||
1503 | kfree(w); | |
1504 | } | |
1505 | ||
1506 | static int nfc_genl_rcv_nl_event(struct notifier_block *this, | |
1507 | unsigned long event, void *ptr) | |
1508 | { | |
1509 | struct netlink_notify *n = ptr; | |
1510 | struct urelease_work *w; | |
1511 | ||
1512 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) | |
1513 | goto out; | |
1514 | ||
c487606f | 1515 | pr_debug("NETLINK_URELEASE event from id %d\n", n->portid); |
3c0cc8aa SJ |
1516 | |
1517 | w = kmalloc(sizeof(*w), GFP_ATOMIC); | |
1518 | if (w) { | |
1519 | INIT_WORK((struct work_struct *) w, nfc_urelease_event_work); | |
c487606f | 1520 | w->portid = n->portid; |
3c0cc8aa SJ |
1521 | schedule_work((struct work_struct *) w); |
1522 | } | |
1523 | ||
4d12b8b1 LRV |
1524 | out: |
1525 | return NOTIFY_DONE; | |
1526 | } | |
1527 | ||
1528 | void nfc_genl_data_init(struct nfc_genl_data *genl_data) | |
1529 | { | |
15e47304 | 1530 | genl_data->poll_req_portid = 0; |
4d12b8b1 LRV |
1531 | mutex_init(&genl_data->genl_data_mutex); |
1532 | } | |
1533 | ||
1534 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data) | |
1535 | { | |
1536 | mutex_destroy(&genl_data->genl_data_mutex); | |
1537 | } | |
1538 | ||
1539 | static struct notifier_block nl_notifier = { | |
1540 | .notifier_call = nfc_genl_rcv_nl_event, | |
1541 | }; | |
1542 | ||
1543 | /** | |
1544 | * nfc_genl_init() - Initialize netlink interface | |
1545 | * | |
1546 | * This initialization function registers the nfc netlink family. | |
1547 | */ | |
1548 | int __init nfc_genl_init(void) | |
1549 | { | |
1550 | int rc; | |
1551 | ||
c53ed742 | 1552 | rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops); |
4d12b8b1 LRV |
1553 | if (rc) |
1554 | return rc; | |
1555 | ||
1556 | rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp); | |
1557 | ||
1558 | netlink_register_notifier(&nl_notifier); | |
1559 | ||
1560 | return rc; | |
1561 | } | |
1562 | ||
1563 | /** | |
1564 | * nfc_genl_exit() - Deinitialize netlink interface | |
1565 | * | |
1566 | * This exit function unregisters the nfc netlink family. | |
1567 | */ | |
1568 | void nfc_genl_exit(void) | |
1569 | { | |
1570 | netlink_unregister_notifier(&nl_notifier); | |
1571 | genl_unregister_family(&nfc_genl_family); | |
1572 | } |