Commit | Line | Data |
---|---|---|
d646960f SO |
1 | /* |
2 | * Copyright (C) 2011 Intel Corporation. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the | |
16 | * Free Software Foundation, Inc., | |
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | */ | |
19 | ||
20 | #define pr_fmt(fmt) "llcp: %s: " fmt, __func__ | |
21 | ||
22 | #include <linux/init.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/list.h> | |
25 | #include <linux/nfc.h> | |
26 | ||
27 | #include "../nfc.h" | |
28 | #include "llcp.h" | |
29 | ||
30 | static u8 llcp_magic[3] = {0x46, 0x66, 0x6d}; | |
31 | ||
32 | static struct list_head llcp_devices; | |
33 | ||
a69f32af | 34 | void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk) |
d646960f | 35 | { |
a69f32af SO |
36 | write_lock(&l->lock); |
37 | sk_add_node(sk, &l->head); | |
38 | write_unlock(&l->lock); | |
39 | } | |
d646960f | 40 | |
a69f32af SO |
41 | void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk) |
42 | { | |
43 | write_lock(&l->lock); | |
44 | sk_del_node_init(sk); | |
45 | write_unlock(&l->lock); | |
46 | } | |
d646960f | 47 | |
f31652a5 SO |
48 | static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock) |
49 | { | |
50 | struct nfc_llcp_local *local = sock->local; | |
51 | struct sk_buff *s, *tmp; | |
52 | ||
53 | pr_debug("%p\n", &sock->sk); | |
54 | ||
55 | skb_queue_purge(&sock->tx_queue); | |
56 | skb_queue_purge(&sock->tx_pending_queue); | |
f31652a5 SO |
57 | |
58 | if (local == NULL) | |
59 | return; | |
60 | ||
61 | /* Search for local pending SKBs that are related to this socket */ | |
62 | skb_queue_walk_safe(&local->tx_queue, s, tmp) { | |
63 | if (s->sk != &sock->sk) | |
64 | continue; | |
65 | ||
66 | skb_unlink(s, &local->tx_queue); | |
67 | kfree_skb(s); | |
68 | } | |
69 | } | |
70 | ||
4d22ea15 | 71 | static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen) |
a69f32af SO |
72 | { |
73 | struct sock *sk; | |
74 | struct hlist_node *node, *tmp; | |
75 | struct nfc_llcp_sock *llcp_sock; | |
d646960f | 76 | |
f31652a5 SO |
77 | skb_queue_purge(&local->tx_queue); |
78 | ||
a69f32af | 79 | write_lock(&local->sockets.lock); |
40c75f81 | 80 | |
a69f32af SO |
81 | sk_for_each_safe(sk, node, tmp, &local->sockets.head) { |
82 | llcp_sock = nfc_llcp_sock(sk); | |
d646960f | 83 | |
50b78b2a | 84 | bh_lock_sock(sk); |
d646960f | 85 | |
f31652a5 SO |
86 | nfc_llcp_socket_purge(llcp_sock); |
87 | ||
a69f32af SO |
88 | if (sk->sk_state == LLCP_CONNECTED) |
89 | nfc_put_device(llcp_sock->dev); | |
d646960f | 90 | |
a69f32af | 91 | if (sk->sk_state == LLCP_LISTEN) { |
d646960f SO |
92 | struct nfc_llcp_sock *lsk, *n; |
93 | struct sock *accept_sk; | |
94 | ||
0f450772 SJ |
95 | list_for_each_entry_safe(lsk, n, |
96 | &llcp_sock->accept_queue, | |
427a2eb1 | 97 | accept_queue) { |
d646960f | 98 | accept_sk = &lsk->sk; |
50b78b2a | 99 | bh_lock_sock(accept_sk); |
d646960f SO |
100 | |
101 | nfc_llcp_accept_unlink(accept_sk); | |
102 | ||
103 | accept_sk->sk_state = LLCP_CLOSED; | |
d646960f | 104 | |
50b78b2a | 105 | bh_unlock_sock(accept_sk); |
d646960f SO |
106 | |
107 | sock_orphan(accept_sk); | |
108 | } | |
4d22ea15 SO |
109 | |
110 | if (listen == true) { | |
50b78b2a | 111 | bh_unlock_sock(sk); |
4d22ea15 SO |
112 | continue; |
113 | } | |
d646960f SO |
114 | } |
115 | ||
c8512be6 SO |
116 | /* |
117 | * If we have a connection less socket bound, we keep it alive | |
118 | * if the device is still present. | |
119 | */ | |
120 | if (sk->sk_state == LLCP_BOUND && sk->sk_type == SOCK_DGRAM && | |
121 | listen == true) { | |
122 | bh_unlock_sock(sk); | |
123 | continue; | |
124 | } | |
125 | ||
a69f32af | 126 | sk->sk_state = LLCP_CLOSED; |
d646960f | 127 | |
50b78b2a | 128 | bh_unlock_sock(sk); |
d646960f | 129 | |
a69f32af | 130 | sock_orphan(sk); |
40c75f81 | 131 | |
a69f32af | 132 | sk_del_node_init(sk); |
d646960f SO |
133 | } |
134 | ||
a69f32af | 135 | write_unlock(&local->sockets.lock); |
d646960f SO |
136 | } |
137 | ||
c7aa1225 SO |
138 | struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local) |
139 | { | |
140 | kref_get(&local->ref); | |
141 | ||
142 | return local; | |
143 | } | |
144 | ||
145 | static void local_release(struct kref *ref) | |
146 | { | |
147 | struct nfc_llcp_local *local; | |
148 | ||
149 | local = container_of(ref, struct nfc_llcp_local, ref); | |
150 | ||
151 | list_del(&local->list); | |
4d22ea15 | 152 | nfc_llcp_socket_release(local, false); |
c7aa1225 SO |
153 | del_timer_sync(&local->link_timer); |
154 | skb_queue_purge(&local->tx_queue); | |
474fee3d TH |
155 | cancel_work_sync(&local->tx_work); |
156 | cancel_work_sync(&local->rx_work); | |
157 | cancel_work_sync(&local->timeout_work); | |
c7aa1225 SO |
158 | kfree_skb(local->rx_pending); |
159 | kfree(local); | |
160 | } | |
161 | ||
162 | int nfc_llcp_local_put(struct nfc_llcp_local *local) | |
163 | { | |
a69f32af SO |
164 | if (local == NULL) |
165 | return 0; | |
166 | ||
c7aa1225 SO |
167 | return kref_put(&local->ref, local_release); |
168 | } | |
169 | ||
8f50020e SO |
170 | static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local, |
171 | u8 ssap, u8 dsap) | |
172 | { | |
173 | struct sock *sk; | |
174 | struct hlist_node *node; | |
a8df0f37 | 175 | struct nfc_llcp_sock *llcp_sock, *tmp_sock; |
8f50020e SO |
176 | |
177 | pr_debug("ssap dsap %d %d\n", ssap, dsap); | |
178 | ||
179 | if (ssap == 0 && dsap == 0) | |
180 | return NULL; | |
181 | ||
182 | read_lock(&local->sockets.lock); | |
183 | ||
184 | llcp_sock = NULL; | |
185 | ||
186 | sk_for_each(sk, node, &local->sockets.head) { | |
a8df0f37 | 187 | tmp_sock = nfc_llcp_sock(sk); |
8f50020e | 188 | |
a8df0f37 SO |
189 | if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) { |
190 | llcp_sock = tmp_sock; | |
8f50020e | 191 | break; |
a8df0f37 | 192 | } |
8f50020e SO |
193 | } |
194 | ||
195 | read_unlock(&local->sockets.lock); | |
196 | ||
197 | if (llcp_sock == NULL) | |
198 | return NULL; | |
199 | ||
200 | sock_hold(&llcp_sock->sk); | |
201 | ||
202 | return llcp_sock; | |
203 | } | |
204 | ||
205 | static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock) | |
206 | { | |
207 | sock_put(&sock->sk); | |
208 | } | |
209 | ||
d646960f SO |
210 | static void nfc_llcp_timeout_work(struct work_struct *work) |
211 | { | |
212 | struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, | |
427a2eb1 | 213 | timeout_work); |
d646960f SO |
214 | |
215 | nfc_dep_link_down(local->dev); | |
216 | } | |
217 | ||
218 | static void nfc_llcp_symm_timer(unsigned long data) | |
219 | { | |
220 | struct nfc_llcp_local *local = (struct nfc_llcp_local *) data; | |
221 | ||
222 | pr_err("SYMM timeout\n"); | |
223 | ||
916082b0 | 224 | schedule_work(&local->timeout_work); |
d646960f SO |
225 | } |
226 | ||
227 | struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev) | |
228 | { | |
229 | struct nfc_llcp_local *local, *n; | |
230 | ||
231 | list_for_each_entry_safe(local, n, &llcp_devices, list) | |
232 | if (local->dev == dev) | |
233 | return local; | |
234 | ||
235 | pr_debug("No device found\n"); | |
236 | ||
237 | return NULL; | |
238 | } | |
239 | ||
240 | static char *wks[] = { | |
241 | NULL, | |
242 | NULL, /* SDP */ | |
243 | "urn:nfc:sn:ip", | |
244 | "urn:nfc:sn:obex", | |
245 | "urn:nfc:sn:snep", | |
246 | }; | |
247 | ||
248 | static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len) | |
249 | { | |
250 | int sap, num_wks; | |
251 | ||
252 | pr_debug("%s\n", service_name); | |
253 | ||
254 | if (service_name == NULL) | |
255 | return -EINVAL; | |
256 | ||
257 | num_wks = ARRAY_SIZE(wks); | |
258 | ||
427a2eb1 | 259 | for (sap = 0; sap < num_wks; sap++) { |
d646960f SO |
260 | if (wks[sap] == NULL) |
261 | continue; | |
262 | ||
263 | if (strncmp(wks[sap], service_name, service_name_len) == 0) | |
264 | return sap; | |
265 | } | |
266 | ||
267 | return -EINVAL; | |
268 | } | |
269 | ||
8f50020e SO |
270 | static |
271 | struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local, | |
272 | u8 *sn, size_t sn_len) | |
273 | { | |
274 | struct sock *sk; | |
275 | struct hlist_node *node; | |
276 | struct nfc_llcp_sock *llcp_sock, *tmp_sock; | |
277 | ||
278 | pr_debug("sn %zd %p\n", sn_len, sn); | |
279 | ||
280 | if (sn == NULL || sn_len == 0) | |
281 | return NULL; | |
282 | ||
283 | read_lock(&local->sockets.lock); | |
284 | ||
285 | llcp_sock = NULL; | |
286 | ||
287 | sk_for_each(sk, node, &local->sockets.head) { | |
288 | tmp_sock = nfc_llcp_sock(sk); | |
289 | ||
290 | pr_debug("llcp sock %p\n", tmp_sock); | |
291 | ||
54292d64 SO |
292 | if (tmp_sock->sk.sk_type == SOCK_STREAM && |
293 | tmp_sock->sk.sk_state != LLCP_LISTEN) | |
294 | continue; | |
295 | ||
296 | if (tmp_sock->sk.sk_type == SOCK_DGRAM && | |
297 | tmp_sock->sk.sk_state != LLCP_BOUND) | |
8f50020e SO |
298 | continue; |
299 | ||
300 | if (tmp_sock->service_name == NULL || | |
301 | tmp_sock->service_name_len == 0) | |
302 | continue; | |
303 | ||
304 | if (tmp_sock->service_name_len != sn_len) | |
305 | continue; | |
306 | ||
307 | if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) { | |
308 | llcp_sock = tmp_sock; | |
309 | break; | |
310 | } | |
311 | } | |
312 | ||
313 | read_unlock(&local->sockets.lock); | |
314 | ||
315 | pr_debug("Found llcp sock %p\n", llcp_sock); | |
316 | ||
317 | return llcp_sock; | |
318 | } | |
319 | ||
d646960f | 320 | u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local, |
427a2eb1 | 321 | struct nfc_llcp_sock *sock) |
d646960f SO |
322 | { |
323 | mutex_lock(&local->sdp_lock); | |
324 | ||
325 | if (sock->service_name != NULL && sock->service_name_len > 0) { | |
326 | int ssap = nfc_llcp_wks_sap(sock->service_name, | |
427a2eb1 | 327 | sock->service_name_len); |
d646960f SO |
328 | |
329 | if (ssap > 0) { | |
330 | pr_debug("WKS %d\n", ssap); | |
331 | ||
332 | /* This is a WKS, let's check if it's free */ | |
333 | if (local->local_wks & BIT(ssap)) { | |
334 | mutex_unlock(&local->sdp_lock); | |
335 | ||
336 | return LLCP_SAP_MAX; | |
337 | } | |
338 | ||
1762c17c | 339 | set_bit(ssap, &local->local_wks); |
d646960f SO |
340 | mutex_unlock(&local->sdp_lock); |
341 | ||
342 | return ssap; | |
343 | } | |
344 | ||
345 | /* | |
8f50020e SO |
346 | * Check if there already is a non WKS socket bound |
347 | * to this service name. | |
d646960f | 348 | */ |
8f50020e SO |
349 | if (nfc_llcp_sock_from_sn(local, sock->service_name, |
350 | sock->service_name_len) != NULL) { | |
d646960f SO |
351 | mutex_unlock(&local->sdp_lock); |
352 | ||
353 | return LLCP_SAP_MAX; | |
354 | } | |
355 | ||
d646960f SO |
356 | mutex_unlock(&local->sdp_lock); |
357 | ||
8f50020e | 358 | return LLCP_SDP_UNBOUND; |
d646960f | 359 | |
ebbb16d9 SO |
360 | } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) { |
361 | if (!test_bit(sock->ssap, &local->local_wks)) { | |
362 | set_bit(sock->ssap, &local->local_wks); | |
363 | mutex_unlock(&local->sdp_lock); | |
d646960f | 364 | |
ebbb16d9 | 365 | return sock->ssap; |
d646960f SO |
366 | } |
367 | } | |
368 | ||
369 | mutex_unlock(&local->sdp_lock); | |
370 | ||
371 | return LLCP_SAP_MAX; | |
372 | } | |
373 | ||
374 | u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local) | |
375 | { | |
376 | u8 local_ssap; | |
377 | ||
378 | mutex_lock(&local->sdp_lock); | |
379 | ||
380 | local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP); | |
381 | if (local_ssap == LLCP_LOCAL_NUM_SAP) { | |
382 | mutex_unlock(&local->sdp_lock); | |
383 | return LLCP_SAP_MAX; | |
384 | } | |
385 | ||
1762c17c | 386 | set_bit(local_ssap, &local->local_sap); |
d646960f SO |
387 | |
388 | mutex_unlock(&local->sdp_lock); | |
389 | ||
390 | return local_ssap + LLCP_LOCAL_SAP_OFFSET; | |
391 | } | |
392 | ||
393 | void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap) | |
394 | { | |
395 | u8 local_ssap; | |
396 | unsigned long *sdp; | |
397 | ||
398 | if (ssap < LLCP_WKS_NUM_SAP) { | |
399 | local_ssap = ssap; | |
400 | sdp = &local->local_wks; | |
401 | } else if (ssap < LLCP_LOCAL_NUM_SAP) { | |
8f50020e SO |
402 | atomic_t *client_cnt; |
403 | ||
d646960f SO |
404 | local_ssap = ssap - LLCP_WKS_NUM_SAP; |
405 | sdp = &local->local_sdp; | |
8f50020e SO |
406 | client_cnt = &local->local_sdp_cnt[local_ssap]; |
407 | ||
408 | pr_debug("%d clients\n", atomic_read(client_cnt)); | |
409 | ||
410 | mutex_lock(&local->sdp_lock); | |
411 | ||
412 | if (atomic_dec_and_test(client_cnt)) { | |
413 | struct nfc_llcp_sock *l_sock; | |
414 | ||
415 | pr_debug("No more clients for SAP %d\n", ssap); | |
416 | ||
417 | clear_bit(local_ssap, sdp); | |
418 | ||
419 | /* Find the listening sock and set it back to UNBOUND */ | |
420 | l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP); | |
421 | if (l_sock) { | |
422 | l_sock->ssap = LLCP_SDP_UNBOUND; | |
423 | nfc_llcp_sock_put(l_sock); | |
424 | } | |
425 | } | |
426 | ||
427 | mutex_unlock(&local->sdp_lock); | |
428 | ||
429 | return; | |
d646960f SO |
430 | } else if (ssap < LLCP_MAX_SAP) { |
431 | local_ssap = ssap - LLCP_LOCAL_NUM_SAP; | |
432 | sdp = &local->local_sap; | |
433 | } else { | |
434 | return; | |
435 | } | |
436 | ||
437 | mutex_lock(&local->sdp_lock); | |
438 | ||
1762c17c | 439 | clear_bit(local_ssap, sdp); |
d646960f SO |
440 | |
441 | mutex_unlock(&local->sdp_lock); | |
442 | } | |
443 | ||
8f50020e SO |
444 | static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local) |
445 | { | |
446 | u8 ssap; | |
447 | ||
448 | mutex_lock(&local->sdp_lock); | |
449 | ||
450 | ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP); | |
451 | if (ssap == LLCP_SDP_NUM_SAP) { | |
452 | mutex_unlock(&local->sdp_lock); | |
453 | ||
454 | return LLCP_SAP_MAX; | |
455 | } | |
456 | ||
457 | pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap); | |
458 | ||
459 | set_bit(ssap, &local->local_sdp); | |
460 | ||
461 | mutex_unlock(&local->sdp_lock); | |
462 | ||
463 | return LLCP_WKS_NUM_SAP + ssap; | |
464 | } | |
465 | ||
d646960f SO |
466 | static int nfc_llcp_build_gb(struct nfc_llcp_local *local) |
467 | { | |
468 | u8 *gb_cur, *version_tlv, version, version_length; | |
52feb444 | 469 | u8 *lto_tlv, lto_length; |
d646960f | 470 | u8 *wks_tlv, wks_length; |
56d5876a | 471 | u8 *miux_tlv, miux_length; |
d646960f | 472 | u8 gb_len = 0; |
52da2449 | 473 | int ret = 0; |
d646960f SO |
474 | |
475 | version = LLCP_VERSION_11; | |
476 | version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version, | |
427a2eb1 | 477 | 1, &version_length); |
d646960f SO |
478 | gb_len += version_length; |
479 | ||
52feb444 | 480 | lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, <o_length); |
d646960f SO |
481 | gb_len += lto_length; |
482 | ||
483 | pr_debug("Local wks 0x%lx\n", local->local_wks); | |
484 | wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2, | |
427a2eb1 | 485 | &wks_length); |
d646960f SO |
486 | gb_len += wks_length; |
487 | ||
52feb444 | 488 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0, |
56d5876a SO |
489 | &miux_length); |
490 | gb_len += miux_length; | |
491 | ||
d646960f SO |
492 | gb_len += ARRAY_SIZE(llcp_magic); |
493 | ||
494 | if (gb_len > NFC_MAX_GT_LEN) { | |
52da2449 WY |
495 | ret = -EINVAL; |
496 | goto out; | |
d646960f SO |
497 | } |
498 | ||
499 | gb_cur = local->gb; | |
500 | ||
501 | memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic)); | |
502 | gb_cur += ARRAY_SIZE(llcp_magic); | |
503 | ||
504 | memcpy(gb_cur, version_tlv, version_length); | |
505 | gb_cur += version_length; | |
506 | ||
507 | memcpy(gb_cur, lto_tlv, lto_length); | |
508 | gb_cur += lto_length; | |
509 | ||
510 | memcpy(gb_cur, wks_tlv, wks_length); | |
511 | gb_cur += wks_length; | |
512 | ||
56d5876a SO |
513 | memcpy(gb_cur, miux_tlv, miux_length); |
514 | gb_cur += miux_length; | |
515 | ||
52da2449 WY |
516 | local->gb_len = gb_len; |
517 | ||
518 | out: | |
d646960f SO |
519 | kfree(version_tlv); |
520 | kfree(lto_tlv); | |
52da2449 WY |
521 | kfree(wks_tlv); |
522 | kfree(miux_tlv); | |
d646960f | 523 | |
52da2449 | 524 | return ret; |
d646960f SO |
525 | } |
526 | ||
b8e7a06d SO |
527 | u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len) |
528 | { | |
529 | struct nfc_llcp_local *local; | |
530 | ||
531 | local = nfc_llcp_find_local(dev); | |
532 | if (local == NULL) { | |
533 | *general_bytes_len = 0; | |
534 | return NULL; | |
535 | } | |
536 | ||
537 | nfc_llcp_build_gb(local); | |
538 | ||
539 | *general_bytes_len = local->gb_len; | |
540 | ||
541 | return local->gb; | |
542 | } | |
543 | ||
d646960f SO |
544 | int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len) |
545 | { | |
546 | struct nfc_llcp_local *local = nfc_llcp_find_local(dev); | |
547 | ||
548 | if (local == NULL) { | |
549 | pr_err("No LLCP device\n"); | |
550 | return -ENODEV; | |
551 | } | |
552 | ||
553 | memset(local->remote_gb, 0, NFC_MAX_GT_LEN); | |
554 | memcpy(local->remote_gb, gb, gb_len); | |
555 | local->remote_gb_len = gb_len; | |
556 | ||
427a2eb1 | 557 | if (local->remote_gb == NULL || local->remote_gb_len == 0) |
d646960f SO |
558 | return -ENODEV; |
559 | ||
560 | if (memcmp(local->remote_gb, llcp_magic, 3)) { | |
561 | pr_err("MAC does not support LLCP\n"); | |
562 | return -EINVAL; | |
563 | } | |
564 | ||
7a06e586 SO |
565 | return nfc_llcp_parse_gb_tlv(local, |
566 | &local->remote_gb[3], | |
567 | local->remote_gb_len - 3); | |
d646960f SO |
568 | } |
569 | ||
d646960f SO |
570 | static u8 nfc_llcp_dsap(struct sk_buff *pdu) |
571 | { | |
572 | return (pdu->data[0] & 0xfc) >> 2; | |
573 | } | |
574 | ||
575 | static u8 nfc_llcp_ptype(struct sk_buff *pdu) | |
576 | { | |
577 | return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6); | |
578 | } | |
579 | ||
580 | static u8 nfc_llcp_ssap(struct sk_buff *pdu) | |
581 | { | |
582 | return pdu->data[1] & 0x3f; | |
583 | } | |
584 | ||
585 | static u8 nfc_llcp_ns(struct sk_buff *pdu) | |
586 | { | |
587 | return pdu->data[2] >> 4; | |
588 | } | |
589 | ||
590 | static u8 nfc_llcp_nr(struct sk_buff *pdu) | |
591 | { | |
592 | return pdu->data[2] & 0xf; | |
593 | } | |
594 | ||
595 | static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu) | |
596 | { | |
279cf174 | 597 | pdu->data[2] = (sock->send_n << 4) | (sock->recv_n); |
d646960f SO |
598 | sock->send_n = (sock->send_n + 1) % 16; |
599 | sock->recv_ack_n = (sock->recv_n - 1) % 16; | |
600 | } | |
601 | ||
4463523b TE |
602 | void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local, |
603 | struct sk_buff *skb, u8 direction) | |
604 | { | |
605 | struct hlist_node *node; | |
606 | struct sk_buff *skb_copy = NULL, *nskb; | |
607 | struct sock *sk; | |
608 | u8 *data; | |
609 | ||
610 | read_lock(&local->raw_sockets.lock); | |
611 | ||
612 | sk_for_each(sk, node, &local->raw_sockets.head) { | |
613 | if (sk->sk_state != LLCP_BOUND) | |
614 | continue; | |
615 | ||
616 | if (skb_copy == NULL) { | |
617 | skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE, | |
618 | GFP_ATOMIC); | |
619 | ||
620 | if (skb_copy == NULL) | |
621 | continue; | |
622 | ||
623 | data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE); | |
624 | ||
625 | data[0] = local->dev ? local->dev->idx : 0xFF; | |
626 | data[1] = direction; | |
627 | } | |
628 | ||
629 | nskb = skb_clone(skb_copy, GFP_ATOMIC); | |
630 | if (!nskb) | |
631 | continue; | |
632 | ||
633 | if (sock_queue_rcv_skb(sk, nskb)) | |
634 | kfree_skb(nskb); | |
635 | } | |
636 | ||
637 | read_unlock(&local->raw_sockets.lock); | |
638 | ||
639 | kfree_skb(skb_copy); | |
640 | } | |
641 | ||
84457960 SO |
642 | static void nfc_llcp_tx_work(struct work_struct *work) |
643 | { | |
644 | struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, | |
645 | tx_work); | |
646 | struct sk_buff *skb; | |
647 | struct sock *sk; | |
648 | struct nfc_llcp_sock *llcp_sock; | |
649 | ||
650 | skb = skb_dequeue(&local->tx_queue); | |
651 | if (skb != NULL) { | |
652 | sk = skb->sk; | |
653 | llcp_sock = nfc_llcp_sock(sk); | |
a6a0915f SO |
654 | |
655 | if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) { | |
656 | nfc_llcp_send_symm(local->dev); | |
657 | } else { | |
be02b6b6 SO |
658 | struct sk_buff *copy_skb = NULL; |
659 | u8 ptype = nfc_llcp_ptype(skb); | |
84457960 SO |
660 | int ret; |
661 | ||
662 | pr_debug("Sending pending skb\n"); | |
663 | print_hex_dump(KERN_DEBUG, "LLCP Tx: ", | |
664 | DUMP_PREFIX_OFFSET, 16, 1, | |
665 | skb->data, skb->len, true); | |
666 | ||
be02b6b6 SO |
667 | if (ptype == LLCP_PDU_I) |
668 | copy_skb = skb_copy(skb, GFP_ATOMIC); | |
669 | ||
2c2d45bd TE |
670 | __net_timestamp(skb); |
671 | ||
4463523b TE |
672 | nfc_llcp_send_to_raw_sock(local, skb, |
673 | NFC_LLCP_DIRECTION_TX); | |
674 | ||
84457960 SO |
675 | ret = nfc_data_exchange(local->dev, local->target_idx, |
676 | skb, nfc_llcp_recv, local); | |
677 | ||
be02b6b6 SO |
678 | if (ret) { |
679 | kfree_skb(copy_skb); | |
680 | goto out; | |
84457960 | 681 | } |
be02b6b6 SO |
682 | |
683 | if (ptype == LLCP_PDU_I && copy_skb) | |
684 | skb_queue_tail(&llcp_sock->tx_pending_queue, | |
685 | copy_skb); | |
84457960 SO |
686 | } |
687 | } else { | |
688 | nfc_llcp_send_symm(local->dev); | |
689 | } | |
690 | ||
be02b6b6 | 691 | out: |
84457960 SO |
692 | mod_timer(&local->link_timer, |
693 | jiffies + msecs_to_jiffies(2 * local->remote_lto)); | |
694 | } | |
695 | ||
a69f32af SO |
696 | static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local, |
697 | u8 ssap) | |
698 | { | |
699 | struct sock *sk; | |
700 | struct nfc_llcp_sock *llcp_sock; | |
701 | struct hlist_node *node; | |
702 | ||
703 | read_lock(&local->connecting_sockets.lock); | |
704 | ||
705 | sk_for_each(sk, node, &local->connecting_sockets.head) { | |
706 | llcp_sock = nfc_llcp_sock(sk); | |
707 | ||
5a0f6f3b SO |
708 | if (llcp_sock->ssap == ssap) { |
709 | sock_hold(&llcp_sock->sk); | |
a69f32af | 710 | goto out; |
5a0f6f3b | 711 | } |
a69f32af SO |
712 | } |
713 | ||
714 | llcp_sock = NULL; | |
715 | ||
716 | out: | |
717 | read_unlock(&local->connecting_sockets.lock); | |
718 | ||
a69f32af SO |
719 | return llcp_sock; |
720 | } | |
721 | ||
a69f32af SO |
722 | static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local, |
723 | u8 *sn, size_t sn_len) | |
724 | { | |
a69f32af SO |
725 | struct nfc_llcp_sock *llcp_sock; |
726 | ||
8f50020e | 727 | llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len); |
d646960f | 728 | |
a69f32af SO |
729 | if (llcp_sock == NULL) |
730 | return NULL; | |
d646960f | 731 | |
a69f32af SO |
732 | sock_hold(&llcp_sock->sk); |
733 | ||
734 | return llcp_sock; | |
d646960f SO |
735 | } |
736 | ||
d646960f SO |
737 | static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len) |
738 | { | |
739 | u8 *tlv = &skb->data[2], type, length; | |
740 | size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0; | |
741 | ||
742 | while (offset < tlv_array_len) { | |
743 | type = tlv[0]; | |
744 | length = tlv[1]; | |
745 | ||
746 | pr_debug("type 0x%x length %d\n", type, length); | |
747 | ||
748 | if (type == LLCP_TLV_SN) { | |
749 | *sn_len = length; | |
750 | return &tlv[2]; | |
751 | } | |
752 | ||
753 | offset += length + 2; | |
754 | tlv += length + 2; | |
755 | } | |
756 | ||
757 | return NULL; | |
758 | } | |
759 | ||
968272bf SO |
760 | static void nfc_llcp_recv_ui(struct nfc_llcp_local *local, |
761 | struct sk_buff *skb) | |
762 | { | |
763 | struct nfc_llcp_sock *llcp_sock; | |
764 | struct nfc_llcp_ui_cb *ui_cb; | |
765 | u8 dsap, ssap; | |
766 | ||
767 | dsap = nfc_llcp_dsap(skb); | |
768 | ssap = nfc_llcp_ssap(skb); | |
769 | ||
770 | ui_cb = nfc_llcp_ui_skb_cb(skb); | |
771 | ui_cb->dsap = dsap; | |
772 | ui_cb->ssap = ssap; | |
773 | ||
774 | printk("%s %d %d\n", __func__, dsap, ssap); | |
775 | ||
776 | pr_debug("%d %d\n", dsap, ssap); | |
777 | ||
778 | /* We're looking for a bound socket, not a client one */ | |
779 | llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP); | |
780 | if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM) | |
781 | return; | |
782 | ||
783 | /* There is no sequence with UI frames */ | |
784 | skb_pull(skb, LLCP_HEADER_SIZE); | |
785 | if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) { | |
786 | pr_err("receive queue is full\n"); | |
2593c2c6 | 787 | kfree_skb(skb); |
968272bf SO |
788 | } |
789 | ||
790 | nfc_llcp_sock_put(llcp_sock); | |
791 | } | |
792 | ||
d646960f | 793 | static void nfc_llcp_recv_connect(struct nfc_llcp_local *local, |
427a2eb1 | 794 | struct sk_buff *skb) |
d646960f SO |
795 | { |
796 | struct sock *new_sk, *parent; | |
797 | struct nfc_llcp_sock *sock, *new_sock; | |
a69f32af | 798 | u8 dsap, ssap, reason; |
d646960f SO |
799 | |
800 | dsap = nfc_llcp_dsap(skb); | |
801 | ssap = nfc_llcp_ssap(skb); | |
802 | ||
803 | pr_debug("%d %d\n", dsap, ssap); | |
804 | ||
d646960f | 805 | if (dsap != LLCP_SAP_SDP) { |
a69f32af SO |
806 | sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP); |
807 | if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) { | |
d646960f SO |
808 | reason = LLCP_DM_NOBOUND; |
809 | goto fail; | |
810 | } | |
d646960f SO |
811 | } else { |
812 | u8 *sn; | |
813 | size_t sn_len; | |
814 | ||
815 | sn = nfc_llcp_connect_sn(skb, &sn_len); | |
816 | if (sn == NULL) { | |
817 | reason = LLCP_DM_NOBOUND; | |
818 | goto fail; | |
819 | } | |
820 | ||
821 | pr_debug("Service name length %zu\n", sn_len); | |
822 | ||
a69f32af SO |
823 | sock = nfc_llcp_sock_get_sn(local, sn, sn_len); |
824 | if (sock == NULL) { | |
825 | reason = LLCP_DM_NOBOUND; | |
826 | goto fail; | |
d646960f | 827 | } |
d646960f SO |
828 | } |
829 | ||
a69f32af | 830 | lock_sock(&sock->sk); |
d646960f | 831 | |
d646960f SO |
832 | parent = &sock->sk; |
833 | ||
834 | if (sk_acceptq_is_full(parent)) { | |
835 | reason = LLCP_DM_REJ; | |
836 | release_sock(&sock->sk); | |
837 | sock_put(&sock->sk); | |
838 | goto fail; | |
839 | } | |
840 | ||
8f50020e SO |
841 | if (sock->ssap == LLCP_SDP_UNBOUND) { |
842 | u8 ssap = nfc_llcp_reserve_sdp_ssap(local); | |
843 | ||
844 | pr_debug("First client, reserving %d\n", ssap); | |
845 | ||
846 | if (ssap == LLCP_SAP_MAX) { | |
847 | reason = LLCP_DM_REJ; | |
848 | release_sock(&sock->sk); | |
849 | sock_put(&sock->sk); | |
850 | goto fail; | |
851 | } | |
852 | ||
853 | sock->ssap = ssap; | |
854 | } | |
855 | ||
427a2eb1 | 856 | new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC); |
d646960f SO |
857 | if (new_sk == NULL) { |
858 | reason = LLCP_DM_REJ; | |
859 | release_sock(&sock->sk); | |
860 | sock_put(&sock->sk); | |
861 | goto fail; | |
862 | } | |
863 | ||
864 | new_sock = nfc_llcp_sock(new_sk); | |
865 | new_sock->dev = local->dev; | |
c7aa1225 | 866 | new_sock->local = nfc_llcp_local_get(local); |
93d7e490 | 867 | new_sock->miu = local->remote_miu; |
d646960f | 868 | new_sock->nfc_protocol = sock->nfc_protocol; |
d646960f | 869 | new_sock->dsap = ssap; |
025f1520 | 870 | new_sock->target_idx = local->target_idx; |
d646960f | 871 | new_sock->parent = parent; |
8f50020e SO |
872 | new_sock->ssap = sock->ssap; |
873 | if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) { | |
874 | atomic_t *client_count; | |
875 | ||
876 | pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock); | |
877 | ||
878 | client_count = | |
879 | &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP]; | |
880 | ||
881 | atomic_inc(client_count); | |
882 | new_sock->reserved_ssap = sock->ssap; | |
883 | } | |
d646960f | 884 | |
7a06e586 SO |
885 | nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE], |
886 | skb->len - LLCP_HEADER_SIZE); | |
887 | ||
d646960f SO |
888 | pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk); |
889 | ||
a69f32af | 890 | nfc_llcp_sock_link(&local->sockets, new_sk); |
d646960f SO |
891 | |
892 | nfc_llcp_accept_enqueue(&sock->sk, new_sk); | |
893 | ||
894 | nfc_get_device(local->dev->idx); | |
895 | ||
896 | new_sk->sk_state = LLCP_CONNECTED; | |
897 | ||
898 | /* Wake the listening processes */ | |
899 | parent->sk_data_ready(parent, 0); | |
900 | ||
901 | /* Send CC */ | |
902 | nfc_llcp_send_cc(new_sock); | |
903 | ||
904 | release_sock(&sock->sk); | |
905 | sock_put(&sock->sk); | |
906 | ||
907 | return; | |
908 | ||
909 | fail: | |
910 | /* Send DM */ | |
911 | nfc_llcp_send_dm(local, dsap, ssap, reason); | |
d646960f SO |
912 | } |
913 | ||
d094afa1 | 914 | int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock) |
4722d2b7 | 915 | { |
d094afa1 | 916 | int nr_frames = 0; |
4722d2b7 SO |
917 | struct nfc_llcp_local *local = sock->local; |
918 | ||
919 | pr_debug("Remote ready %d tx queue len %d remote rw %d", | |
427a2eb1 | 920 | sock->remote_ready, skb_queue_len(&sock->tx_pending_queue), |
7a06e586 | 921 | sock->rw); |
4722d2b7 SO |
922 | |
923 | /* Try to queue some I frames for transmission */ | |
924 | while (sock->remote_ready && | |
7a06e586 | 925 | skb_queue_len(&sock->tx_pending_queue) < sock->rw) { |
84457960 | 926 | struct sk_buff *pdu; |
4722d2b7 SO |
927 | |
928 | pdu = skb_dequeue(&sock->tx_queue); | |
929 | if (pdu == NULL) | |
930 | break; | |
931 | ||
932 | /* Update N(S)/N(R) */ | |
933 | nfc_llcp_set_nrns(sock, pdu); | |
934 | ||
4722d2b7 | 935 | skb_queue_tail(&local->tx_queue, pdu); |
d094afa1 | 936 | nr_frames++; |
4722d2b7 | 937 | } |
d094afa1 SO |
938 | |
939 | return nr_frames; | |
4722d2b7 SO |
940 | } |
941 | ||
d646960f | 942 | static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local, |
427a2eb1 | 943 | struct sk_buff *skb) |
d646960f SO |
944 | { |
945 | struct nfc_llcp_sock *llcp_sock; | |
946 | struct sock *sk; | |
947 | u8 dsap, ssap, ptype, ns, nr; | |
948 | ||
949 | ptype = nfc_llcp_ptype(skb); | |
950 | dsap = nfc_llcp_dsap(skb); | |
951 | ssap = nfc_llcp_ssap(skb); | |
952 | ns = nfc_llcp_ns(skb); | |
953 | nr = nfc_llcp_nr(skb); | |
954 | ||
955 | pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns); | |
956 | ||
957 | llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); | |
958 | if (llcp_sock == NULL) { | |
959 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); | |
960 | return; | |
961 | } | |
962 | ||
963 | sk = &llcp_sock->sk; | |
964 | lock_sock(sk); | |
965 | if (sk->sk_state == LLCP_CLOSED) { | |
966 | release_sock(sk); | |
967 | nfc_llcp_sock_put(llcp_sock); | |
968 | } | |
969 | ||
d646960f SO |
970 | /* Pass the payload upstream */ |
971 | if (ptype == LLCP_PDU_I) { | |
972 | pr_debug("I frame, queueing on %p\n", &llcp_sock->sk); | |
973 | ||
53aef920 SO |
974 | if (ns == llcp_sock->recv_n) |
975 | llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16; | |
976 | else | |
977 | pr_err("Received out of sequence I PDU\n"); | |
978 | ||
d646960f SO |
979 | skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE); |
980 | if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) { | |
981 | pr_err("receive queue is full\n"); | |
2593c2c6 | 982 | kfree_skb(skb); |
d646960f SO |
983 | } |
984 | } | |
985 | ||
986 | /* Remove skbs from the pending queue */ | |
987 | if (llcp_sock->send_ack_n != nr) { | |
988 | struct sk_buff *s, *tmp; | |
28981491 | 989 | u8 n; |
d646960f SO |
990 | |
991 | llcp_sock->send_ack_n = nr; | |
992 | ||
84457960 SO |
993 | /* Remove and free all skbs until ns == nr */ |
994 | skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) { | |
28981491 WR |
995 | n = nfc_llcp_ns(s); |
996 | ||
84457960 SO |
997 | skb_unlink(s, &llcp_sock->tx_pending_queue); |
998 | kfree_skb(s); | |
999 | ||
28981491 | 1000 | if (n == nr) |
84457960 SO |
1001 | break; |
1002 | } | |
1003 | ||
1004 | /* Re-queue the remaining skbs for transmission */ | |
1005 | skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue, | |
1006 | s, tmp) { | |
1007 | skb_unlink(s, &llcp_sock->tx_pending_queue); | |
1008 | skb_queue_head(&local->tx_queue, s); | |
1009 | } | |
d646960f SO |
1010 | } |
1011 | ||
53aef920 SO |
1012 | if (ptype == LLCP_PDU_RR) |
1013 | llcp_sock->remote_ready = true; | |
427a2eb1 | 1014 | else if (ptype == LLCP_PDU_RNR) |
53aef920 SO |
1015 | llcp_sock->remote_ready = false; |
1016 | ||
56af2568 | 1017 | if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I) |
d094afa1 | 1018 | nfc_llcp_send_rr(llcp_sock); |
d646960f SO |
1019 | |
1020 | release_sock(sk); | |
1021 | nfc_llcp_sock_put(llcp_sock); | |
1022 | } | |
1023 | ||
1024 | static void nfc_llcp_recv_disc(struct nfc_llcp_local *local, | |
427a2eb1 | 1025 | struct sk_buff *skb) |
d646960f SO |
1026 | { |
1027 | struct nfc_llcp_sock *llcp_sock; | |
1028 | struct sock *sk; | |
1029 | u8 dsap, ssap; | |
1030 | ||
1031 | dsap = nfc_llcp_dsap(skb); | |
1032 | ssap = nfc_llcp_ssap(skb); | |
1033 | ||
1034 | llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); | |
1035 | if (llcp_sock == NULL) { | |
1036 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); | |
1037 | return; | |
1038 | } | |
1039 | ||
1040 | sk = &llcp_sock->sk; | |
1041 | lock_sock(sk); | |
f31652a5 SO |
1042 | |
1043 | nfc_llcp_socket_purge(llcp_sock); | |
1044 | ||
d646960f SO |
1045 | if (sk->sk_state == LLCP_CLOSED) { |
1046 | release_sock(sk); | |
1047 | nfc_llcp_sock_put(llcp_sock); | |
1048 | } | |
1049 | ||
d646960f SO |
1050 | if (sk->sk_state == LLCP_CONNECTED) { |
1051 | nfc_put_device(local->dev); | |
1052 | sk->sk_state = LLCP_CLOSED; | |
1053 | sk->sk_state_change(sk); | |
1054 | } | |
1055 | ||
1056 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC); | |
1057 | ||
1058 | release_sock(sk); | |
1059 | nfc_llcp_sock_put(llcp_sock); | |
1060 | } | |
1061 | ||
427a2eb1 | 1062 | static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb) |
d646960f SO |
1063 | { |
1064 | struct nfc_llcp_sock *llcp_sock; | |
ff353d86 | 1065 | struct sock *sk; |
d646960f SO |
1066 | u8 dsap, ssap; |
1067 | ||
d646960f SO |
1068 | dsap = nfc_llcp_dsap(skb); |
1069 | ssap = nfc_llcp_ssap(skb); | |
1070 | ||
a69f32af | 1071 | llcp_sock = nfc_llcp_connecting_sock_get(local, dsap); |
d646960f SO |
1072 | if (llcp_sock == NULL) { |
1073 | pr_err("Invalid CC\n"); | |
1074 | nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN); | |
1075 | ||
1076 | return; | |
1077 | } | |
1078 | ||
ff353d86 | 1079 | sk = &llcp_sock->sk; |
d646960f | 1080 | |
a69f32af SO |
1081 | /* Unlink from connecting and link to the client array */ |
1082 | nfc_llcp_sock_unlink(&local->connecting_sockets, sk); | |
1083 | nfc_llcp_sock_link(&local->sockets, sk); | |
1084 | llcp_sock->dsap = ssap; | |
1085 | ||
7a06e586 SO |
1086 | nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE], |
1087 | skb->len - LLCP_HEADER_SIZE); | |
d646960f | 1088 | |
ff353d86 SO |
1089 | sk->sk_state = LLCP_CONNECTED; |
1090 | sk->sk_state_change(sk); | |
1091 | ||
d646960f SO |
1092 | nfc_llcp_sock_put(llcp_sock); |
1093 | } | |
1094 | ||
5c0560b7 SO |
1095 | static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb) |
1096 | { | |
1097 | struct nfc_llcp_sock *llcp_sock; | |
1098 | struct sock *sk; | |
1099 | u8 dsap, ssap, reason; | |
1100 | ||
1101 | dsap = nfc_llcp_dsap(skb); | |
1102 | ssap = nfc_llcp_ssap(skb); | |
1103 | reason = skb->data[2]; | |
1104 | ||
1105 | pr_debug("%d %d reason %d\n", ssap, dsap, reason); | |
1106 | ||
1107 | switch (reason) { | |
1108 | case LLCP_DM_NOBOUND: | |
1109 | case LLCP_DM_REJ: | |
1110 | llcp_sock = nfc_llcp_connecting_sock_get(local, dsap); | |
1111 | break; | |
1112 | ||
1113 | default: | |
1114 | llcp_sock = nfc_llcp_sock_get(local, dsap, ssap); | |
1115 | break; | |
1116 | } | |
1117 | ||
1118 | if (llcp_sock == NULL) { | |
a8df0f37 | 1119 | pr_debug("Already closed\n"); |
5c0560b7 SO |
1120 | return; |
1121 | } | |
1122 | ||
1123 | sk = &llcp_sock->sk; | |
1124 | ||
1125 | sk->sk_err = ENXIO; | |
1126 | sk->sk_state = LLCP_CLOSED; | |
1127 | sk->sk_state_change(sk); | |
1128 | ||
1129 | nfc_llcp_sock_put(llcp_sock); | |
5c0560b7 SO |
1130 | } |
1131 | ||
19cfe584 SO |
1132 | static void nfc_llcp_recv_snl(struct nfc_llcp_local *local, |
1133 | struct sk_buff *skb) | |
1134 | { | |
1135 | struct nfc_llcp_sock *llcp_sock; | |
1136 | u8 dsap, ssap, *tlv, type, length, tid, sap; | |
1137 | u16 tlv_len, offset; | |
1138 | char *service_name; | |
1139 | size_t service_name_len; | |
1140 | ||
1141 | dsap = nfc_llcp_dsap(skb); | |
1142 | ssap = nfc_llcp_ssap(skb); | |
1143 | ||
1144 | pr_debug("%d %d\n", dsap, ssap); | |
1145 | ||
1146 | if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) { | |
1147 | pr_err("Wrong SNL SAP\n"); | |
1148 | return; | |
1149 | } | |
1150 | ||
1151 | tlv = &skb->data[LLCP_HEADER_SIZE]; | |
1152 | tlv_len = skb->len - LLCP_HEADER_SIZE; | |
1153 | offset = 0; | |
1154 | ||
0f450772 | 1155 | while (offset < tlv_len) { |
19cfe584 SO |
1156 | type = tlv[0]; |
1157 | length = tlv[1]; | |
1158 | ||
1159 | switch (type) { | |
1160 | case LLCP_TLV_SDREQ: | |
1161 | tid = tlv[2]; | |
1162 | service_name = (char *) &tlv[3]; | |
1163 | service_name_len = length - 1; | |
1164 | ||
e6904081 | 1165 | pr_debug("Looking for %.16s\n", service_name); |
19cfe584 SO |
1166 | |
1167 | if (service_name_len == strlen("urn:nfc:sn:sdp") && | |
1168 | !strncmp(service_name, "urn:nfc:sn:sdp", | |
1169 | service_name_len)) { | |
1170 | sap = 1; | |
e6904081 SO |
1171 | goto send_snl; |
1172 | } | |
1173 | ||
1174 | llcp_sock = nfc_llcp_sock_from_sn(local, service_name, | |
1175 | service_name_len); | |
1176 | if (!llcp_sock) { | |
1177 | sap = 0; | |
1178 | goto send_snl; | |
1179 | } | |
1180 | ||
1181 | /* | |
1182 | * We found a socket but its ssap has not been reserved | |
1183 | * yet. We need to assign it for good and send a reply. | |
1184 | * The ssap will be freed when the socket is closed. | |
1185 | */ | |
1186 | if (llcp_sock->ssap == LLCP_SDP_UNBOUND) { | |
1187 | atomic_t *client_count; | |
1188 | ||
1189 | sap = nfc_llcp_reserve_sdp_ssap(local); | |
1190 | ||
1191 | pr_debug("Reserving %d\n", sap); | |
1192 | ||
1193 | if (sap == LLCP_SAP_MAX) { | |
1194 | sap = 0; | |
1195 | goto send_snl; | |
1196 | } | |
1197 | ||
1198 | client_count = | |
1199 | &local->local_sdp_cnt[sap - | |
1200 | LLCP_WKS_NUM_SAP]; | |
1201 | ||
1202 | atomic_inc(client_count); | |
1203 | ||
1204 | llcp_sock->ssap = sap; | |
1205 | llcp_sock->reserved_ssap = sap; | |
19cfe584 | 1206 | } else { |
e6904081 | 1207 | sap = llcp_sock->ssap; |
19cfe584 SO |
1208 | } |
1209 | ||
e6904081 SO |
1210 | pr_debug("%p %d\n", llcp_sock, sap); |
1211 | ||
0f450772 | 1212 | send_snl: |
19cfe584 SO |
1213 | nfc_llcp_send_snl(local, tid, sap); |
1214 | break; | |
1215 | ||
1216 | default: | |
1217 | pr_err("Invalid SNL tlv value 0x%x\n", type); | |
1218 | break; | |
1219 | } | |
1220 | ||
1221 | offset += length + 2; | |
1222 | tlv += length + 2; | |
1223 | } | |
1224 | } | |
1225 | ||
d646960f SO |
1226 | static void nfc_llcp_rx_work(struct work_struct *work) |
1227 | { | |
1228 | struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local, | |
427a2eb1 | 1229 | rx_work); |
d646960f SO |
1230 | u8 dsap, ssap, ptype; |
1231 | struct sk_buff *skb; | |
1232 | ||
1233 | skb = local->rx_pending; | |
1234 | if (skb == NULL) { | |
1235 | pr_debug("No pending SKB\n"); | |
1236 | return; | |
1237 | } | |
1238 | ||
1239 | ptype = nfc_llcp_ptype(skb); | |
1240 | dsap = nfc_llcp_dsap(skb); | |
1241 | ssap = nfc_llcp_ssap(skb); | |
1242 | ||
1243 | pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap); | |
1244 | ||
4be646ec SO |
1245 | if (ptype != LLCP_PDU_SYMM) |
1246 | print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET, | |
1247 | 16, 1, skb->data, skb->len, true); | |
1248 | ||
2c2d45bd TE |
1249 | __net_timestamp(skb); |
1250 | ||
4463523b TE |
1251 | nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX); |
1252 | ||
d646960f SO |
1253 | switch (ptype) { |
1254 | case LLCP_PDU_SYMM: | |
1255 | pr_debug("SYMM\n"); | |
1256 | break; | |
1257 | ||
968272bf SO |
1258 | case LLCP_PDU_UI: |
1259 | pr_debug("UI\n"); | |
1260 | nfc_llcp_recv_ui(local, skb); | |
1261 | break; | |
1262 | ||
d646960f SO |
1263 | case LLCP_PDU_CONNECT: |
1264 | pr_debug("CONNECT\n"); | |
1265 | nfc_llcp_recv_connect(local, skb); | |
1266 | break; | |
1267 | ||
1268 | case LLCP_PDU_DISC: | |
1269 | pr_debug("DISC\n"); | |
1270 | nfc_llcp_recv_disc(local, skb); | |
1271 | break; | |
1272 | ||
1273 | case LLCP_PDU_CC: | |
1274 | pr_debug("CC\n"); | |
1275 | nfc_llcp_recv_cc(local, skb); | |
1276 | break; | |
1277 | ||
5c0560b7 SO |
1278 | case LLCP_PDU_DM: |
1279 | pr_debug("DM\n"); | |
1280 | nfc_llcp_recv_dm(local, skb); | |
1281 | break; | |
1282 | ||
19cfe584 SO |
1283 | case LLCP_PDU_SNL: |
1284 | pr_debug("SNL\n"); | |
1285 | nfc_llcp_recv_snl(local, skb); | |
1286 | break; | |
1287 | ||
d646960f SO |
1288 | case LLCP_PDU_I: |
1289 | case LLCP_PDU_RR: | |
53aef920 | 1290 | case LLCP_PDU_RNR: |
d646960f SO |
1291 | pr_debug("I frame\n"); |
1292 | nfc_llcp_recv_hdlc(local, skb); | |
1293 | break; | |
1294 | ||
1295 | } | |
1296 | ||
916082b0 | 1297 | schedule_work(&local->tx_work); |
d646960f SO |
1298 | kfree_skb(local->rx_pending); |
1299 | local->rx_pending = NULL; | |
d646960f SO |
1300 | } |
1301 | ||
1302 | void nfc_llcp_recv(void *data, struct sk_buff *skb, int err) | |
1303 | { | |
1304 | struct nfc_llcp_local *local = (struct nfc_llcp_local *) data; | |
1305 | ||
1306 | pr_debug("Received an LLCP PDU\n"); | |
1307 | if (err < 0) { | |
427a2eb1 | 1308 | pr_err("err %d\n", err); |
d646960f SO |
1309 | return; |
1310 | } | |
1311 | ||
1312 | local->rx_pending = skb_get(skb); | |
1313 | del_timer(&local->link_timer); | |
916082b0 | 1314 | schedule_work(&local->rx_work); |
d646960f SO |
1315 | } |
1316 | ||
73167ced SO |
1317 | int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb) |
1318 | { | |
1319 | struct nfc_llcp_local *local; | |
1320 | ||
1321 | local = nfc_llcp_find_local(dev); | |
1322 | if (local == NULL) | |
1323 | return -ENODEV; | |
1324 | ||
1325 | local->rx_pending = skb_get(skb); | |
1326 | del_timer(&local->link_timer); | |
916082b0 | 1327 | schedule_work(&local->rx_work); |
73167ced SO |
1328 | |
1329 | return 0; | |
1330 | } | |
1331 | ||
d646960f SO |
1332 | void nfc_llcp_mac_is_down(struct nfc_dev *dev) |
1333 | { | |
1334 | struct nfc_llcp_local *local; | |
1335 | ||
1336 | local = nfc_llcp_find_local(dev); | |
1337 | if (local == NULL) | |
1338 | return; | |
1339 | ||
1340 | /* Close and purge all existing sockets */ | |
4d22ea15 | 1341 | nfc_llcp_socket_release(local, true); |
d646960f SO |
1342 | } |
1343 | ||
1344 | void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx, | |
1345 | u8 comm_mode, u8 rf_mode) | |
1346 | { | |
1347 | struct nfc_llcp_local *local; | |
1348 | ||
1349 | pr_debug("rf mode %d\n", rf_mode); | |
1350 | ||
1351 | local = nfc_llcp_find_local(dev); | |
1352 | if (local == NULL) | |
1353 | return; | |
1354 | ||
1355 | local->target_idx = target_idx; | |
1356 | local->comm_mode = comm_mode; | |
1357 | local->rf_mode = rf_mode; | |
1358 | ||
1359 | if (rf_mode == NFC_RF_INITIATOR) { | |
1360 | pr_debug("Queueing Tx work\n"); | |
1361 | ||
916082b0 | 1362 | schedule_work(&local->tx_work); |
d646960f SO |
1363 | } else { |
1364 | mod_timer(&local->link_timer, | |
427a2eb1 | 1365 | jiffies + msecs_to_jiffies(local->remote_lto)); |
d646960f SO |
1366 | } |
1367 | } | |
1368 | ||
1369 | int nfc_llcp_register_device(struct nfc_dev *ndev) | |
1370 | { | |
d646960f | 1371 | struct nfc_llcp_local *local; |
d646960f SO |
1372 | |
1373 | local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL); | |
1374 | if (local == NULL) | |
1375 | return -ENOMEM; | |
1376 | ||
1377 | local->dev = ndev; | |
1378 | INIT_LIST_HEAD(&local->list); | |
c7aa1225 | 1379 | kref_init(&local->ref); |
d646960f | 1380 | mutex_init(&local->sdp_lock); |
d646960f SO |
1381 | init_timer(&local->link_timer); |
1382 | local->link_timer.data = (unsigned long) local; | |
1383 | local->link_timer.function = nfc_llcp_symm_timer; | |
1384 | ||
1385 | skb_queue_head_init(&local->tx_queue); | |
1386 | INIT_WORK(&local->tx_work, nfc_llcp_tx_work); | |
d646960f SO |
1387 | |
1388 | local->rx_pending = NULL; | |
1389 | INIT_WORK(&local->rx_work, nfc_llcp_rx_work); | |
d646960f SO |
1390 | |
1391 | INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work); | |
d646960f | 1392 | |
fe235b58 SJ |
1393 | rwlock_init(&local->sockets.lock); |
1394 | rwlock_init(&local->connecting_sockets.lock); | |
4463523b | 1395 | rwlock_init(&local->raw_sockets.lock); |
a69f32af | 1396 | |
52feb444 TE |
1397 | local->lto = 150; /* 1500 ms */ |
1398 | local->rw = LLCP_MAX_RW; | |
1399 | local->miux = cpu_to_be16(LLCP_MAX_MIUX); | |
1400 | ||
d646960f SO |
1401 | nfc_llcp_build_gb(local); |
1402 | ||
1403 | local->remote_miu = LLCP_DEFAULT_MIU; | |
1404 | local->remote_lto = LLCP_DEFAULT_LTO; | |
d646960f | 1405 | |
16a78e9f | 1406 | list_add(&local->list, &llcp_devices); |
d646960f | 1407 | |
d646960f SO |
1408 | return 0; |
1409 | } | |
1410 | ||
1411 | void nfc_llcp_unregister_device(struct nfc_dev *dev) | |
1412 | { | |
1413 | struct nfc_llcp_local *local = nfc_llcp_find_local(dev); | |
1414 | ||
1415 | if (local == NULL) { | |
1416 | pr_debug("No such device\n"); | |
1417 | return; | |
1418 | } | |
1419 | ||
c7aa1225 | 1420 | nfc_llcp_local_put(local); |
d646960f SO |
1421 | } |
1422 | ||
1423 | int __init nfc_llcp_init(void) | |
1424 | { | |
1425 | INIT_LIST_HEAD(&llcp_devices); | |
1426 | ||
1427 | return nfc_llcp_sock_init(); | |
1428 | } | |
1429 | ||
1430 | void nfc_llcp_exit(void) | |
1431 | { | |
1432 | nfc_llcp_sock_exit(); | |
1433 | } |