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