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