blob: 2e23bd348ebd515d1e76bc06846a0a005dd7d70e [file] [log] [blame]
Samuel Ortizd6469602011-12-14 16:43:12 +01001/*
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
30static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
31
32static struct list_head llcp_devices;
33
Samuel Ortiza69f32a2012-05-04 17:04:19 +020034void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk)
35{
36 write_lock(&l->lock);
37 sk_add_node(sk, &l->head);
38 write_unlock(&l->lock);
39}
40
41void 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}
47
Samuel Ortizf31652a2012-10-26 18:20:10 +020048static 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);
57 skb_queue_purge(&sock->tx_backlog_queue);
58
59 if (local == NULL)
60 return;
61
62 /* Search for local pending SKBs that are related to this socket */
63 skb_queue_walk_safe(&local->tx_queue, s, tmp) {
64 if (s->sk != &sock->sk)
65 continue;
66
67 skb_unlink(s, &local->tx_queue);
68 kfree_skb(s);
69 }
70}
71
Samuel Ortiz4d22ea12012-06-22 14:40:34 +020072static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
Samuel Ortizd6469602011-12-14 16:43:12 +010073{
Samuel Ortiza69f32a2012-05-04 17:04:19 +020074 struct sock *sk;
75 struct hlist_node *node, *tmp;
76 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +010077
Samuel Ortizf31652a2012-10-26 18:20:10 +020078 skb_queue_purge(&local->tx_queue);
79
Samuel Ortiza69f32a2012-05-04 17:04:19 +020080 write_lock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +010081
Samuel Ortiza69f32a2012-05-04 17:04:19 +020082 sk_for_each_safe(sk, node, tmp, &local->sockets.head) {
83 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010084
Szymon Janc50b78b22012-09-26 14:22:10 +020085 bh_lock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010086
Samuel Ortizf31652a2012-10-26 18:20:10 +020087 nfc_llcp_socket_purge(llcp_sock);
88
Samuel Ortiza69f32a2012-05-04 17:04:19 +020089 if (sk->sk_state == LLCP_CONNECTED)
90 nfc_put_device(llcp_sock->dev);
Samuel Ortizd6469602011-12-14 16:43:12 +010091
Samuel Ortiza69f32a2012-05-04 17:04:19 +020092 if (sk->sk_state == LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +010093 struct nfc_llcp_sock *lsk, *n;
94 struct sock *accept_sk;
95
Szymon Janc0f450772012-10-17 15:23:39 +020096 list_for_each_entry_safe(lsk, n,
97 &llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +010098 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +010099 accept_sk = &lsk->sk;
Szymon Janc50b78b22012-09-26 14:22:10 +0200100 bh_lock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100101
102 nfc_llcp_accept_unlink(accept_sk);
103
104 accept_sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +0100105
Szymon Janc50b78b22012-09-26 14:22:10 +0200106 bh_unlock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100107
108 sock_orphan(accept_sk);
109 }
Samuel Ortiz4d22ea12012-06-22 14:40:34 +0200110
111 if (listen == true) {
Szymon Janc50b78b22012-09-26 14:22:10 +0200112 bh_unlock_sock(sk);
Samuel Ortiz4d22ea12012-06-22 14:40:34 +0200113 continue;
114 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100115 }
116
Samuel Ortizc8512be2012-10-15 15:09:52 +0200117 /*
118 * If we have a connection less socket bound, we keep it alive
119 * if the device is still present.
120 */
121 if (sk->sk_state == LLCP_BOUND && sk->sk_type == SOCK_DGRAM &&
122 listen == true) {
123 bh_unlock_sock(sk);
124 continue;
125 }
126
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200127 sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +0100128
Szymon Janc50b78b22012-09-26 14:22:10 +0200129 bh_unlock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100130
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200131 sock_orphan(sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100132
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200133 sk_del_node_init(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100134 }
135
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200136 write_unlock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100137}
138
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200139struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
140{
141 kref_get(&local->ref);
142
143 return local;
144}
145
146static void local_release(struct kref *ref)
147{
148 struct nfc_llcp_local *local;
149
150 local = container_of(ref, struct nfc_llcp_local, ref);
151
152 list_del(&local->list);
Samuel Ortiz4d22ea12012-06-22 14:40:34 +0200153 nfc_llcp_socket_release(local, false);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200154 del_timer_sync(&local->link_timer);
155 skb_queue_purge(&local->tx_queue);
Tejun Heo474fee32012-08-22 16:22:16 -0700156 cancel_work_sync(&local->tx_work);
157 cancel_work_sync(&local->rx_work);
158 cancel_work_sync(&local->timeout_work);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200159 kfree_skb(local->rx_pending);
160 kfree(local);
161}
162
163int nfc_llcp_local_put(struct nfc_llcp_local *local)
164{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200165 if (local == NULL)
166 return 0;
167
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200168 return kref_put(&local->ref, local_release);
169}
170
Samuel Ortiz8f500202012-06-25 15:46:28 +0200171static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
172 u8 ssap, u8 dsap)
173{
174 struct sock *sk;
175 struct hlist_node *node;
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200176 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200177
178 pr_debug("ssap dsap %d %d\n", ssap, dsap);
179
180 if (ssap == 0 && dsap == 0)
181 return NULL;
182
183 read_lock(&local->sockets.lock);
184
185 llcp_sock = NULL;
186
187 sk_for_each(sk, node, &local->sockets.head) {
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200188 tmp_sock = nfc_llcp_sock(sk);
Samuel Ortiz8f500202012-06-25 15:46:28 +0200189
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200190 if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
191 llcp_sock = tmp_sock;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200192 break;
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200193 }
Samuel Ortiz8f500202012-06-25 15:46:28 +0200194 }
195
196 read_unlock(&local->sockets.lock);
197
198 if (llcp_sock == NULL)
199 return NULL;
200
201 sock_hold(&llcp_sock->sk);
202
203 return llcp_sock;
204}
205
206static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
207{
208 sock_put(&sock->sk);
209}
210
Samuel Ortizd6469602011-12-14 16:43:12 +0100211static void nfc_llcp_timeout_work(struct work_struct *work)
212{
213 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100214 timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100215
216 nfc_dep_link_down(local->dev);
217}
218
219static void nfc_llcp_symm_timer(unsigned long data)
220{
221 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
222
223 pr_err("SYMM timeout\n");
224
Linus Torvalds916082b2012-10-02 16:01:31 -0700225 schedule_work(&local->timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100226}
227
228struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
229{
230 struct nfc_llcp_local *local, *n;
231
232 list_for_each_entry_safe(local, n, &llcp_devices, list)
233 if (local->dev == dev)
234 return local;
235
236 pr_debug("No device found\n");
237
238 return NULL;
239}
240
241static char *wks[] = {
242 NULL,
243 NULL, /* SDP */
244 "urn:nfc:sn:ip",
245 "urn:nfc:sn:obex",
246 "urn:nfc:sn:snep",
247};
248
249static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
250{
251 int sap, num_wks;
252
253 pr_debug("%s\n", service_name);
254
255 if (service_name == NULL)
256 return -EINVAL;
257
258 num_wks = ARRAY_SIZE(wks);
259
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100260 for (sap = 0; sap < num_wks; sap++) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100261 if (wks[sap] == NULL)
262 continue;
263
264 if (strncmp(wks[sap], service_name, service_name_len) == 0)
265 return sap;
266 }
267
268 return -EINVAL;
269}
270
Samuel Ortiz8f500202012-06-25 15:46:28 +0200271static
272struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
273 u8 *sn, size_t sn_len)
274{
275 struct sock *sk;
276 struct hlist_node *node;
277 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
278
279 pr_debug("sn %zd %p\n", sn_len, sn);
280
281 if (sn == NULL || sn_len == 0)
282 return NULL;
283
284 read_lock(&local->sockets.lock);
285
286 llcp_sock = NULL;
287
288 sk_for_each(sk, node, &local->sockets.head) {
289 tmp_sock = nfc_llcp_sock(sk);
290
291 pr_debug("llcp sock %p\n", tmp_sock);
292
Samuel Ortiz54292d62012-10-15 15:08:29 +0200293 if (tmp_sock->sk.sk_type == SOCK_STREAM &&
294 tmp_sock->sk.sk_state != LLCP_LISTEN)
295 continue;
296
297 if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
298 tmp_sock->sk.sk_state != LLCP_BOUND)
Samuel Ortiz8f500202012-06-25 15:46:28 +0200299 continue;
300
301 if (tmp_sock->service_name == NULL ||
302 tmp_sock->service_name_len == 0)
303 continue;
304
305 if (tmp_sock->service_name_len != sn_len)
306 continue;
307
308 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
309 llcp_sock = tmp_sock;
310 break;
311 }
312 }
313
314 read_unlock(&local->sockets.lock);
315
316 pr_debug("Found llcp sock %p\n", llcp_sock);
317
318 return llcp_sock;
319}
320
Samuel Ortizd6469602011-12-14 16:43:12 +0100321u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100322 struct nfc_llcp_sock *sock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100323{
324 mutex_lock(&local->sdp_lock);
325
326 if (sock->service_name != NULL && sock->service_name_len > 0) {
327 int ssap = nfc_llcp_wks_sap(sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100328 sock->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100329
330 if (ssap > 0) {
331 pr_debug("WKS %d\n", ssap);
332
333 /* This is a WKS, let's check if it's free */
334 if (local->local_wks & BIT(ssap)) {
335 mutex_unlock(&local->sdp_lock);
336
337 return LLCP_SAP_MAX;
338 }
339
Samuel Ortiz1762c172012-03-05 01:03:38 +0100340 set_bit(ssap, &local->local_wks);
Samuel Ortizd6469602011-12-14 16:43:12 +0100341 mutex_unlock(&local->sdp_lock);
342
343 return ssap;
344 }
345
346 /*
Samuel Ortiz8f500202012-06-25 15:46:28 +0200347 * Check if there already is a non WKS socket bound
348 * to this service name.
Samuel Ortizd6469602011-12-14 16:43:12 +0100349 */
Samuel Ortiz8f500202012-06-25 15:46:28 +0200350 if (nfc_llcp_sock_from_sn(local, sock->service_name,
351 sock->service_name_len) != NULL) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100352 mutex_unlock(&local->sdp_lock);
353
354 return LLCP_SAP_MAX;
355 }
356
Samuel Ortizd6469602011-12-14 16:43:12 +0100357 mutex_unlock(&local->sdp_lock);
358
Samuel Ortiz8f500202012-06-25 15:46:28 +0200359 return LLCP_SDP_UNBOUND;
Samuel Ortizd6469602011-12-14 16:43:12 +0100360
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200361 } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
362 if (!test_bit(sock->ssap, &local->local_wks)) {
363 set_bit(sock->ssap, &local->local_wks);
364 mutex_unlock(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100365
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200366 return sock->ssap;
Samuel Ortizd6469602011-12-14 16:43:12 +0100367 }
368 }
369
370 mutex_unlock(&local->sdp_lock);
371
372 return LLCP_SAP_MAX;
373}
374
375u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
376{
377 u8 local_ssap;
378
379 mutex_lock(&local->sdp_lock);
380
381 local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
382 if (local_ssap == LLCP_LOCAL_NUM_SAP) {
383 mutex_unlock(&local->sdp_lock);
384 return LLCP_SAP_MAX;
385 }
386
Samuel Ortiz1762c172012-03-05 01:03:38 +0100387 set_bit(local_ssap, &local->local_sap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100388
389 mutex_unlock(&local->sdp_lock);
390
391 return local_ssap + LLCP_LOCAL_SAP_OFFSET;
392}
393
394void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
395{
396 u8 local_ssap;
397 unsigned long *sdp;
398
399 if (ssap < LLCP_WKS_NUM_SAP) {
400 local_ssap = ssap;
401 sdp = &local->local_wks;
402 } else if (ssap < LLCP_LOCAL_NUM_SAP) {
Samuel Ortiz8f500202012-06-25 15:46:28 +0200403 atomic_t *client_cnt;
404
Samuel Ortizd6469602011-12-14 16:43:12 +0100405 local_ssap = ssap - LLCP_WKS_NUM_SAP;
406 sdp = &local->local_sdp;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200407 client_cnt = &local->local_sdp_cnt[local_ssap];
408
409 pr_debug("%d clients\n", atomic_read(client_cnt));
410
411 mutex_lock(&local->sdp_lock);
412
413 if (atomic_dec_and_test(client_cnt)) {
414 struct nfc_llcp_sock *l_sock;
415
416 pr_debug("No more clients for SAP %d\n", ssap);
417
418 clear_bit(local_ssap, sdp);
419
420 /* Find the listening sock and set it back to UNBOUND */
421 l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
422 if (l_sock) {
423 l_sock->ssap = LLCP_SDP_UNBOUND;
424 nfc_llcp_sock_put(l_sock);
425 }
426 }
427
428 mutex_unlock(&local->sdp_lock);
429
430 return;
Samuel Ortizd6469602011-12-14 16:43:12 +0100431 } else if (ssap < LLCP_MAX_SAP) {
432 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
433 sdp = &local->local_sap;
434 } else {
435 return;
436 }
437
438 mutex_lock(&local->sdp_lock);
439
Samuel Ortiz1762c172012-03-05 01:03:38 +0100440 clear_bit(local_ssap, sdp);
Samuel Ortizd6469602011-12-14 16:43:12 +0100441
442 mutex_unlock(&local->sdp_lock);
443}
444
Samuel Ortiz8f500202012-06-25 15:46:28 +0200445static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
446{
447 u8 ssap;
448
449 mutex_lock(&local->sdp_lock);
450
451 ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
452 if (ssap == LLCP_SDP_NUM_SAP) {
453 mutex_unlock(&local->sdp_lock);
454
455 return LLCP_SAP_MAX;
456 }
457
458 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
459
460 set_bit(ssap, &local->local_sdp);
461
462 mutex_unlock(&local->sdp_lock);
463
464 return LLCP_WKS_NUM_SAP + ssap;
465}
466
Samuel Ortizd6469602011-12-14 16:43:12 +0100467static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
468{
469 u8 *gb_cur, *version_tlv, version, version_length;
470 u8 *lto_tlv, lto, lto_length;
471 u8 *wks_tlv, wks_length;
Samuel Ortiz56d58762012-04-10 19:43:19 +0200472 u8 *miux_tlv, miux_length;
473 __be16 miux;
Samuel Ortizd6469602011-12-14 16:43:12 +0100474 u8 gb_len = 0;
Wei Yongjun52da2442012-09-02 21:21:46 +0800475 int ret = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100476
477 version = LLCP_VERSION_11;
478 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100479 1, &version_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100480 gb_len += version_length;
481
482 /* 1500 ms */
483 lto = 150;
Samuel Ortiz91b0ade2012-04-10 19:43:20 +0200484 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &lto, 1, &lto_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100485 gb_len += lto_length;
486
487 pr_debug("Local wks 0x%lx\n", local->local_wks);
488 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100489 &wks_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100490 gb_len += wks_length;
491
Samuel Ortiz56d58762012-04-10 19:43:19 +0200492 miux = cpu_to_be16(LLCP_MAX_MIUX);
493 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
494 &miux_length);
495 gb_len += miux_length;
496
Samuel Ortizd6469602011-12-14 16:43:12 +0100497 gb_len += ARRAY_SIZE(llcp_magic);
498
499 if (gb_len > NFC_MAX_GT_LEN) {
Wei Yongjun52da2442012-09-02 21:21:46 +0800500 ret = -EINVAL;
501 goto out;
Samuel Ortizd6469602011-12-14 16:43:12 +0100502 }
503
504 gb_cur = local->gb;
505
506 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
507 gb_cur += ARRAY_SIZE(llcp_magic);
508
509 memcpy(gb_cur, version_tlv, version_length);
510 gb_cur += version_length;
511
512 memcpy(gb_cur, lto_tlv, lto_length);
513 gb_cur += lto_length;
514
515 memcpy(gb_cur, wks_tlv, wks_length);
516 gb_cur += wks_length;
517
Samuel Ortiz56d58762012-04-10 19:43:19 +0200518 memcpy(gb_cur, miux_tlv, miux_length);
519 gb_cur += miux_length;
520
Samuel Ortizd6469602011-12-14 16:43:12 +0100521 local->gb_len = gb_len;
522
Wei Yongjun52da2442012-09-02 21:21:46 +0800523out:
524 kfree(version_tlv);
525 kfree(lto_tlv);
526 kfree(wks_tlv);
527 kfree(miux_tlv);
528
529 return ret;
Samuel Ortizd6469602011-12-14 16:43:12 +0100530}
531
Samuel Ortizb8e7a062012-06-22 02:04:53 +0200532u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
533{
534 struct nfc_llcp_local *local;
535
536 local = nfc_llcp_find_local(dev);
537 if (local == NULL) {
538 *general_bytes_len = 0;
539 return NULL;
540 }
541
542 nfc_llcp_build_gb(local);
543
544 *general_bytes_len = local->gb_len;
545
546 return local->gb;
547}
548
Samuel Ortizd6469602011-12-14 16:43:12 +0100549int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
550{
551 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
552
553 if (local == NULL) {
554 pr_err("No LLCP device\n");
555 return -ENODEV;
556 }
557
558 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
559 memcpy(local->remote_gb, gb, gb_len);
560 local->remote_gb_len = gb_len;
561
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100562 if (local->remote_gb == NULL || local->remote_gb_len == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100563 return -ENODEV;
564
565 if (memcmp(local->remote_gb, llcp_magic, 3)) {
566 pr_err("MAC does not support LLCP\n");
567 return -EINVAL;
568 }
569
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200570 return nfc_llcp_parse_gb_tlv(local,
571 &local->remote_gb[3],
572 local->remote_gb_len - 3);
Samuel Ortizd6469602011-12-14 16:43:12 +0100573}
574
Samuel Ortizd6469602011-12-14 16:43:12 +0100575static u8 nfc_llcp_dsap(struct sk_buff *pdu)
576{
577 return (pdu->data[0] & 0xfc) >> 2;
578}
579
580static u8 nfc_llcp_ptype(struct sk_buff *pdu)
581{
582 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
583}
584
585static u8 nfc_llcp_ssap(struct sk_buff *pdu)
586{
587 return pdu->data[1] & 0x3f;
588}
589
590static u8 nfc_llcp_ns(struct sk_buff *pdu)
591{
592 return pdu->data[2] >> 4;
593}
594
595static u8 nfc_llcp_nr(struct sk_buff *pdu)
596{
597 return pdu->data[2] & 0xf;
598}
599
600static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
601{
Samuel Ortiz279cf172012-04-10 19:43:14 +0200602 pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
Samuel Ortizd6469602011-12-14 16:43:12 +0100603 sock->send_n = (sock->send_n + 1) % 16;
604 sock->recv_ack_n = (sock->recv_n - 1) % 16;
605}
606
Thierry Escande44635232012-09-26 18:16:44 +0200607void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
608 struct sk_buff *skb, u8 direction)
609{
610 struct hlist_node *node;
611 struct sk_buff *skb_copy = NULL, *nskb;
612 struct sock *sk;
613 u8 *data;
614
615 read_lock(&local->raw_sockets.lock);
616
617 sk_for_each(sk, node, &local->raw_sockets.head) {
618 if (sk->sk_state != LLCP_BOUND)
619 continue;
620
621 if (skb_copy == NULL) {
622 skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE,
623 GFP_ATOMIC);
624
625 if (skb_copy == NULL)
626 continue;
627
628 data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE);
629
630 data[0] = local->dev ? local->dev->idx : 0xFF;
631 data[1] = direction;
632 }
633
634 nskb = skb_clone(skb_copy, GFP_ATOMIC);
635 if (!nskb)
636 continue;
637
638 if (sock_queue_rcv_skb(sk, nskb))
639 kfree_skb(nskb);
640 }
641
642 read_unlock(&local->raw_sockets.lock);
643
644 kfree_skb(skb_copy);
645}
646
Samuel Ortiz84457962012-05-30 17:48:29 +0200647static void nfc_llcp_tx_work(struct work_struct *work)
648{
649 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
650 tx_work);
651 struct sk_buff *skb;
652 struct sock *sk;
653 struct nfc_llcp_sock *llcp_sock;
654
655 skb = skb_dequeue(&local->tx_queue);
656 if (skb != NULL) {
657 sk = skb->sk;
658 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiza6a09152012-10-05 00:37:22 +0200659
660 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
661 nfc_llcp_send_symm(local->dev);
662 } else {
Samuel Ortiz84457962012-05-30 17:48:29 +0200663 int ret;
664
665 pr_debug("Sending pending skb\n");
666 print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
667 DUMP_PREFIX_OFFSET, 16, 1,
668 skb->data, skb->len, true);
669
Thierry Escande44635232012-09-26 18:16:44 +0200670 nfc_llcp_send_to_raw_sock(local, skb,
671 NFC_LLCP_DIRECTION_TX);
672
Samuel Ortiz84457962012-05-30 17:48:29 +0200673 ret = nfc_data_exchange(local->dev, local->target_idx,
674 skb, nfc_llcp_recv, local);
675
676 if (!ret && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
677 skb = skb_get(skb);
678 skb_queue_tail(&llcp_sock->tx_pending_queue,
679 skb);
680 }
Samuel Ortiz84457962012-05-30 17:48:29 +0200681 }
682 } else {
683 nfc_llcp_send_symm(local->dev);
684 }
685
686 mod_timer(&local->link_timer,
687 jiffies + msecs_to_jiffies(2 * local->remote_lto));
688}
689
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200690static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
691 u8 ssap)
692{
693 struct sock *sk;
694 struct nfc_llcp_sock *llcp_sock;
695 struct hlist_node *node;
696
697 read_lock(&local->connecting_sockets.lock);
698
699 sk_for_each(sk, node, &local->connecting_sockets.head) {
700 llcp_sock = nfc_llcp_sock(sk);
701
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200702 if (llcp_sock->ssap == ssap) {
703 sock_hold(&llcp_sock->sk);
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200704 goto out;
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200705 }
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200706 }
707
708 llcp_sock = NULL;
709
710out:
711 read_unlock(&local->connecting_sockets.lock);
712
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200713 return llcp_sock;
714}
715
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200716static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
717 u8 *sn, size_t sn_len)
718{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200719 struct nfc_llcp_sock *llcp_sock;
720
Samuel Ortiz8f500202012-06-25 15:46:28 +0200721 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100722
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200723 if (llcp_sock == NULL)
724 return NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100725
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200726 sock_hold(&llcp_sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100727
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200728 return llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +0100729}
730
Samuel Ortizd6469602011-12-14 16:43:12 +0100731static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
732{
733 u8 *tlv = &skb->data[2], type, length;
734 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
735
736 while (offset < tlv_array_len) {
737 type = tlv[0];
738 length = tlv[1];
739
740 pr_debug("type 0x%x length %d\n", type, length);
741
742 if (type == LLCP_TLV_SN) {
743 *sn_len = length;
744 return &tlv[2];
745 }
746
747 offset += length + 2;
748 tlv += length + 2;
749 }
750
751 return NULL;
752}
753
Samuel Ortiz968272b2012-10-15 16:14:37 +0200754static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
755 struct sk_buff *skb)
756{
757 struct nfc_llcp_sock *llcp_sock;
758 struct nfc_llcp_ui_cb *ui_cb;
759 u8 dsap, ssap;
760
761 dsap = nfc_llcp_dsap(skb);
762 ssap = nfc_llcp_ssap(skb);
763
764 ui_cb = nfc_llcp_ui_skb_cb(skb);
765 ui_cb->dsap = dsap;
766 ui_cb->ssap = ssap;
767
768 printk("%s %d %d\n", __func__, dsap, ssap);
769
770 pr_debug("%d %d\n", dsap, ssap);
771
772 /* We're looking for a bound socket, not a client one */
773 llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
774 if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
775 return;
776
777 /* There is no sequence with UI frames */
778 skb_pull(skb, LLCP_HEADER_SIZE);
779 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
780 pr_err("receive queue is full\n");
781 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
782 }
783
784 nfc_llcp_sock_put(llcp_sock);
785}
786
Samuel Ortizd6469602011-12-14 16:43:12 +0100787static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100788 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100789{
790 struct sock *new_sk, *parent;
791 struct nfc_llcp_sock *sock, *new_sock;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200792 u8 dsap, ssap, reason;
Samuel Ortizd6469602011-12-14 16:43:12 +0100793
794 dsap = nfc_llcp_dsap(skb);
795 ssap = nfc_llcp_ssap(skb);
796
797 pr_debug("%d %d\n", dsap, ssap);
798
Samuel Ortizd6469602011-12-14 16:43:12 +0100799 if (dsap != LLCP_SAP_SDP) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200800 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
801 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100802 reason = LLCP_DM_NOBOUND;
803 goto fail;
804 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100805 } else {
806 u8 *sn;
807 size_t sn_len;
808
809 sn = nfc_llcp_connect_sn(skb, &sn_len);
810 if (sn == NULL) {
811 reason = LLCP_DM_NOBOUND;
812 goto fail;
813 }
814
815 pr_debug("Service name length %zu\n", sn_len);
816
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200817 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
818 if (sock == NULL) {
819 reason = LLCP_DM_NOBOUND;
820 goto fail;
Samuel Ortizd6469602011-12-14 16:43:12 +0100821 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100822 }
823
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200824 lock_sock(&sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100825
Samuel Ortizd6469602011-12-14 16:43:12 +0100826 parent = &sock->sk;
827
828 if (sk_acceptq_is_full(parent)) {
829 reason = LLCP_DM_REJ;
830 release_sock(&sock->sk);
831 sock_put(&sock->sk);
832 goto fail;
833 }
834
Samuel Ortiz8f500202012-06-25 15:46:28 +0200835 if (sock->ssap == LLCP_SDP_UNBOUND) {
836 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
837
838 pr_debug("First client, reserving %d\n", ssap);
839
840 if (ssap == LLCP_SAP_MAX) {
841 reason = LLCP_DM_REJ;
842 release_sock(&sock->sk);
843 sock_put(&sock->sk);
844 goto fail;
845 }
846
847 sock->ssap = ssap;
848 }
849
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100850 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC);
Samuel Ortizd6469602011-12-14 16:43:12 +0100851 if (new_sk == NULL) {
852 reason = LLCP_DM_REJ;
853 release_sock(&sock->sk);
854 sock_put(&sock->sk);
855 goto fail;
856 }
857
858 new_sock = nfc_llcp_sock(new_sk);
859 new_sock->dev = local->dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200860 new_sock->local = nfc_llcp_local_get(local);
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200861 new_sock->miu = local->remote_miu;
Samuel Ortizd6469602011-12-14 16:43:12 +0100862 new_sock->nfc_protocol = sock->nfc_protocol;
Samuel Ortizd6469602011-12-14 16:43:12 +0100863 new_sock->dsap = ssap;
Samuel Ortiz025f1522012-06-18 21:38:09 +0200864 new_sock->target_idx = local->target_idx;
Samuel Ortizd6469602011-12-14 16:43:12 +0100865 new_sock->parent = parent;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200866 new_sock->ssap = sock->ssap;
867 if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
868 atomic_t *client_count;
869
870 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
871
872 client_count =
873 &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
874
875 atomic_inc(client_count);
876 new_sock->reserved_ssap = sock->ssap;
877 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100878
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200879 nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
880 skb->len - LLCP_HEADER_SIZE);
881
Samuel Ortizd6469602011-12-14 16:43:12 +0100882 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
883
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200884 nfc_llcp_sock_link(&local->sockets, new_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100885
886 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
887
888 nfc_get_device(local->dev->idx);
889
890 new_sk->sk_state = LLCP_CONNECTED;
891
892 /* Wake the listening processes */
893 parent->sk_data_ready(parent, 0);
894
895 /* Send CC */
896 nfc_llcp_send_cc(new_sock);
897
898 release_sock(&sock->sk);
899 sock_put(&sock->sk);
900
901 return;
902
903fail:
904 /* Send DM */
905 nfc_llcp_send_dm(local, dsap, ssap, reason);
Samuel Ortizd6469602011-12-14 16:43:12 +0100906}
907
Samuel Ortizd094afa2012-03-05 01:03:42 +0100908int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100909{
Samuel Ortizd094afa2012-03-05 01:03:42 +0100910 int nr_frames = 0;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100911 struct nfc_llcp_local *local = sock->local;
912
913 pr_debug("Remote ready %d tx queue len %d remote rw %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100914 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200915 sock->rw);
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100916
917 /* Try to queue some I frames for transmission */
918 while (sock->remote_ready &&
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200919 skb_queue_len(&sock->tx_pending_queue) < sock->rw) {
Samuel Ortiz84457962012-05-30 17:48:29 +0200920 struct sk_buff *pdu;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100921
922 pdu = skb_dequeue(&sock->tx_queue);
923 if (pdu == NULL)
924 break;
925
926 /* Update N(S)/N(R) */
927 nfc_llcp_set_nrns(sock, pdu);
928
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100929 skb_queue_tail(&local->tx_queue, pdu);
Samuel Ortizd094afa2012-03-05 01:03:42 +0100930 nr_frames++;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100931 }
Samuel Ortizd094afa2012-03-05 01:03:42 +0100932
933 return nr_frames;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100934}
935
Samuel Ortizd6469602011-12-14 16:43:12 +0100936static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100937 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100938{
939 struct nfc_llcp_sock *llcp_sock;
940 struct sock *sk;
941 u8 dsap, ssap, ptype, ns, nr;
942
943 ptype = nfc_llcp_ptype(skb);
944 dsap = nfc_llcp_dsap(skb);
945 ssap = nfc_llcp_ssap(skb);
946 ns = nfc_llcp_ns(skb);
947 nr = nfc_llcp_nr(skb);
948
949 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
950
951 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
952 if (llcp_sock == NULL) {
953 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
954 return;
955 }
956
957 sk = &llcp_sock->sk;
958 lock_sock(sk);
959 if (sk->sk_state == LLCP_CLOSED) {
960 release_sock(sk);
961 nfc_llcp_sock_put(llcp_sock);
962 }
963
Samuel Ortizd6469602011-12-14 16:43:12 +0100964 /* Pass the payload upstream */
965 if (ptype == LLCP_PDU_I) {
966 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
967
Samuel Ortiz53aef922012-03-05 01:03:36 +0100968 if (ns == llcp_sock->recv_n)
969 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
970 else
971 pr_err("Received out of sequence I PDU\n");
972
Samuel Ortizd6469602011-12-14 16:43:12 +0100973 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
974 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
975 pr_err("receive queue is full\n");
976 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
977 }
978 }
979
980 /* Remove skbs from the pending queue */
981 if (llcp_sock->send_ack_n != nr) {
982 struct sk_buff *s, *tmp;
983
984 llcp_sock->send_ack_n = nr;
985
Samuel Ortiz84457962012-05-30 17:48:29 +0200986 /* Remove and free all skbs until ns == nr */
987 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
988 skb_unlink(s, &llcp_sock->tx_pending_queue);
989 kfree_skb(s);
990
991 if (nfc_llcp_ns(s) == nr)
992 break;
993 }
994
995 /* Re-queue the remaining skbs for transmission */
996 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
997 s, tmp) {
998 skb_unlink(s, &llcp_sock->tx_pending_queue);
999 skb_queue_head(&local->tx_queue, s);
1000 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001001 }
1002
Samuel Ortiz53aef922012-03-05 01:03:36 +01001003 if (ptype == LLCP_PDU_RR)
1004 llcp_sock->remote_ready = true;
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001005 else if (ptype == LLCP_PDU_RNR)
Samuel Ortiz53aef922012-03-05 01:03:36 +01001006 llcp_sock->remote_ready = false;
1007
Samuel Ortiz56af2562012-05-30 18:06:11 +02001008 if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
Samuel Ortizd094afa2012-03-05 01:03:42 +01001009 nfc_llcp_send_rr(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001010
1011 release_sock(sk);
1012 nfc_llcp_sock_put(llcp_sock);
1013}
1014
1015static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001016 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001017{
1018 struct nfc_llcp_sock *llcp_sock;
1019 struct sock *sk;
1020 u8 dsap, ssap;
1021
1022 dsap = nfc_llcp_dsap(skb);
1023 ssap = nfc_llcp_ssap(skb);
1024
1025 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1026 if (llcp_sock == NULL) {
1027 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1028 return;
1029 }
1030
1031 sk = &llcp_sock->sk;
1032 lock_sock(sk);
Samuel Ortizf31652a2012-10-26 18:20:10 +02001033
1034 nfc_llcp_socket_purge(llcp_sock);
1035
Samuel Ortizd6469602011-12-14 16:43:12 +01001036 if (sk->sk_state == LLCP_CLOSED) {
1037 release_sock(sk);
1038 nfc_llcp_sock_put(llcp_sock);
1039 }
1040
Samuel Ortizd6469602011-12-14 16:43:12 +01001041 if (sk->sk_state == LLCP_CONNECTED) {
1042 nfc_put_device(local->dev);
1043 sk->sk_state = LLCP_CLOSED;
1044 sk->sk_state_change(sk);
1045 }
1046
1047 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
1048
1049 release_sock(sk);
1050 nfc_llcp_sock_put(llcp_sock);
1051}
1052
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001053static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001054{
1055 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizff353d82012-05-07 12:31:19 +02001056 struct sock *sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001057 u8 dsap, ssap;
1058
Samuel Ortizd6469602011-12-14 16:43:12 +01001059 dsap = nfc_llcp_dsap(skb);
1060 ssap = nfc_llcp_ssap(skb);
1061
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001062 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
Samuel Ortizd6469602011-12-14 16:43:12 +01001063 if (llcp_sock == NULL) {
1064 pr_err("Invalid CC\n");
1065 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1066
1067 return;
1068 }
1069
Samuel Ortizff353d82012-05-07 12:31:19 +02001070 sk = &llcp_sock->sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001071
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001072 /* Unlink from connecting and link to the client array */
1073 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
1074 nfc_llcp_sock_link(&local->sockets, sk);
1075 llcp_sock->dsap = ssap;
1076
Samuel Ortiz7a06e582012-05-07 22:03:34 +02001077 nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
1078 skb->len - LLCP_HEADER_SIZE);
Samuel Ortizd6469602011-12-14 16:43:12 +01001079
Samuel Ortizff353d82012-05-07 12:31:19 +02001080 sk->sk_state = LLCP_CONNECTED;
1081 sk->sk_state_change(sk);
1082
Samuel Ortizd6469602011-12-14 16:43:12 +01001083 nfc_llcp_sock_put(llcp_sock);
1084}
1085
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001086static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1087{
1088 struct nfc_llcp_sock *llcp_sock;
1089 struct sock *sk;
1090 u8 dsap, ssap, reason;
1091
1092 dsap = nfc_llcp_dsap(skb);
1093 ssap = nfc_llcp_ssap(skb);
1094 reason = skb->data[2];
1095
1096 pr_debug("%d %d reason %d\n", ssap, dsap, reason);
1097
1098 switch (reason) {
1099 case LLCP_DM_NOBOUND:
1100 case LLCP_DM_REJ:
1101 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1102 break;
1103
1104 default:
1105 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1106 break;
1107 }
1108
1109 if (llcp_sock == NULL) {
Samuel Ortiza8df0f32012-10-16 21:15:59 +02001110 pr_debug("Already closed\n");
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001111 return;
1112 }
1113
1114 sk = &llcp_sock->sk;
1115
1116 sk->sk_err = ENXIO;
1117 sk->sk_state = LLCP_CLOSED;
1118 sk->sk_state_change(sk);
1119
1120 nfc_llcp_sock_put(llcp_sock);
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001121}
1122
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001123static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1124 struct sk_buff *skb)
1125{
1126 struct nfc_llcp_sock *llcp_sock;
1127 u8 dsap, ssap, *tlv, type, length, tid, sap;
1128 u16 tlv_len, offset;
1129 char *service_name;
1130 size_t service_name_len;
1131
1132 dsap = nfc_llcp_dsap(skb);
1133 ssap = nfc_llcp_ssap(skb);
1134
1135 pr_debug("%d %d\n", dsap, ssap);
1136
1137 if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1138 pr_err("Wrong SNL SAP\n");
1139 return;
1140 }
1141
1142 tlv = &skb->data[LLCP_HEADER_SIZE];
1143 tlv_len = skb->len - LLCP_HEADER_SIZE;
1144 offset = 0;
1145
Szymon Janc0f450772012-10-17 15:23:39 +02001146 while (offset < tlv_len) {
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001147 type = tlv[0];
1148 length = tlv[1];
1149
1150 switch (type) {
1151 case LLCP_TLV_SDREQ:
1152 tid = tlv[2];
1153 service_name = (char *) &tlv[3];
1154 service_name_len = length - 1;
1155
Samuel Ortize6904082012-10-15 14:28:13 +02001156 pr_debug("Looking for %.16s\n", service_name);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001157
1158 if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1159 !strncmp(service_name, "urn:nfc:sn:sdp",
1160 service_name_len)) {
1161 sap = 1;
Samuel Ortize6904082012-10-15 14:28:13 +02001162 goto send_snl;
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001163 }
1164
Samuel Ortize6904082012-10-15 14:28:13 +02001165 llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
1166 service_name_len);
1167 if (!llcp_sock) {
1168 sap = 0;
1169 goto send_snl;
1170 }
1171
1172 /*
1173 * We found a socket but its ssap has not been reserved
1174 * yet. We need to assign it for good and send a reply.
1175 * The ssap will be freed when the socket is closed.
1176 */
1177 if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1178 atomic_t *client_count;
1179
1180 sap = nfc_llcp_reserve_sdp_ssap(local);
1181
1182 pr_debug("Reserving %d\n", sap);
1183
1184 if (sap == LLCP_SAP_MAX) {
1185 sap = 0;
1186 goto send_snl;
1187 }
1188
1189 client_count =
1190 &local->local_sdp_cnt[sap -
1191 LLCP_WKS_NUM_SAP];
1192
1193 atomic_inc(client_count);
1194
1195 llcp_sock->ssap = sap;
1196 llcp_sock->reserved_ssap = sap;
1197 } else {
1198 sap = llcp_sock->ssap;
1199 }
1200
1201 pr_debug("%p %d\n", llcp_sock, sap);
1202
Szymon Janc0f450772012-10-17 15:23:39 +02001203send_snl:
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001204 nfc_llcp_send_snl(local, tid, sap);
1205 break;
1206
1207 default:
1208 pr_err("Invalid SNL tlv value 0x%x\n", type);
1209 break;
1210 }
1211
1212 offset += length + 2;
1213 tlv += length + 2;
1214 }
1215}
1216
Samuel Ortizd6469602011-12-14 16:43:12 +01001217static void nfc_llcp_rx_work(struct work_struct *work)
1218{
1219 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001220 rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001221 u8 dsap, ssap, ptype;
1222 struct sk_buff *skb;
1223
1224 skb = local->rx_pending;
1225 if (skb == NULL) {
1226 pr_debug("No pending SKB\n");
1227 return;
1228 }
1229
1230 ptype = nfc_llcp_ptype(skb);
1231 dsap = nfc_llcp_dsap(skb);
1232 ssap = nfc_llcp_ssap(skb);
1233
1234 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1235
Samuel Ortiz4be646e2012-04-10 19:43:13 +02001236 if (ptype != LLCP_PDU_SYMM)
1237 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
1238 16, 1, skb->data, skb->len, true);
1239
Thierry Escande44635232012-09-26 18:16:44 +02001240 nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX);
1241
Samuel Ortizd6469602011-12-14 16:43:12 +01001242 switch (ptype) {
1243 case LLCP_PDU_SYMM:
1244 pr_debug("SYMM\n");
1245 break;
1246
Samuel Ortiz968272b2012-10-15 16:14:37 +02001247 case LLCP_PDU_UI:
1248 pr_debug("UI\n");
1249 nfc_llcp_recv_ui(local, skb);
1250 break;
1251
Samuel Ortizd6469602011-12-14 16:43:12 +01001252 case LLCP_PDU_CONNECT:
1253 pr_debug("CONNECT\n");
1254 nfc_llcp_recv_connect(local, skb);
1255 break;
1256
1257 case LLCP_PDU_DISC:
1258 pr_debug("DISC\n");
1259 nfc_llcp_recv_disc(local, skb);
1260 break;
1261
1262 case LLCP_PDU_CC:
1263 pr_debug("CC\n");
1264 nfc_llcp_recv_cc(local, skb);
1265 break;
1266
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001267 case LLCP_PDU_DM:
1268 pr_debug("DM\n");
1269 nfc_llcp_recv_dm(local, skb);
1270 break;
1271
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001272 case LLCP_PDU_SNL:
1273 pr_debug("SNL\n");
1274 nfc_llcp_recv_snl(local, skb);
1275 break;
1276
Samuel Ortizd6469602011-12-14 16:43:12 +01001277 case LLCP_PDU_I:
1278 case LLCP_PDU_RR:
Samuel Ortiz53aef922012-03-05 01:03:36 +01001279 case LLCP_PDU_RNR:
Samuel Ortizd6469602011-12-14 16:43:12 +01001280 pr_debug("I frame\n");
1281 nfc_llcp_recv_hdlc(local, skb);
1282 break;
1283
1284 }
1285
Linus Torvalds916082b2012-10-02 16:01:31 -07001286 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001287 kfree_skb(local->rx_pending);
1288 local->rx_pending = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +01001289}
1290
1291void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1292{
1293 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1294
1295 pr_debug("Received an LLCP PDU\n");
1296 if (err < 0) {
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001297 pr_err("err %d\n", err);
Samuel Ortizd6469602011-12-14 16:43:12 +01001298 return;
1299 }
1300
1301 local->rx_pending = skb_get(skb);
1302 del_timer(&local->link_timer);
Linus Torvalds916082b2012-10-02 16:01:31 -07001303 schedule_work(&local->rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001304}
1305
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001306int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1307{
1308 struct nfc_llcp_local *local;
1309
1310 local = nfc_llcp_find_local(dev);
1311 if (local == NULL)
1312 return -ENODEV;
1313
1314 local->rx_pending = skb_get(skb);
1315 del_timer(&local->link_timer);
Linus Torvalds916082b2012-10-02 16:01:31 -07001316 schedule_work(&local->rx_work);
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001317
1318 return 0;
1319}
1320
Samuel Ortizd6469602011-12-14 16:43:12 +01001321void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1322{
1323 struct nfc_llcp_local *local;
1324
1325 local = nfc_llcp_find_local(dev);
1326 if (local == NULL)
1327 return;
1328
1329 /* Close and purge all existing sockets */
Samuel Ortiz4d22ea12012-06-22 14:40:34 +02001330 nfc_llcp_socket_release(local, true);
Samuel Ortizd6469602011-12-14 16:43:12 +01001331}
1332
1333void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1334 u8 comm_mode, u8 rf_mode)
1335{
1336 struct nfc_llcp_local *local;
1337
1338 pr_debug("rf mode %d\n", rf_mode);
1339
1340 local = nfc_llcp_find_local(dev);
1341 if (local == NULL)
1342 return;
1343
1344 local->target_idx = target_idx;
1345 local->comm_mode = comm_mode;
1346 local->rf_mode = rf_mode;
1347
1348 if (rf_mode == NFC_RF_INITIATOR) {
1349 pr_debug("Queueing Tx work\n");
1350
Linus Torvalds916082b2012-10-02 16:01:31 -07001351 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001352 } else {
1353 mod_timer(&local->link_timer,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001354 jiffies + msecs_to_jiffies(local->remote_lto));
Samuel Ortizd6469602011-12-14 16:43:12 +01001355 }
1356}
1357
1358int nfc_llcp_register_device(struct nfc_dev *ndev)
1359{
Samuel Ortizd6469602011-12-14 16:43:12 +01001360 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +01001361
1362 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
1363 if (local == NULL)
1364 return -ENOMEM;
1365
1366 local->dev = ndev;
1367 INIT_LIST_HEAD(&local->list);
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001368 kref_init(&local->ref);
Samuel Ortizd6469602011-12-14 16:43:12 +01001369 mutex_init(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001370 init_timer(&local->link_timer);
1371 local->link_timer.data = (unsigned long) local;
1372 local->link_timer.function = nfc_llcp_symm_timer;
1373
1374 skb_queue_head_init(&local->tx_queue);
1375 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001376
1377 local->rx_pending = NULL;
1378 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001379
1380 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001381
Szymon Jancfe235b52012-09-25 12:42:50 +02001382 rwlock_init(&local->sockets.lock);
1383 rwlock_init(&local->connecting_sockets.lock);
Thierry Escande44635232012-09-26 18:16:44 +02001384 rwlock_init(&local->raw_sockets.lock);
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001385
Samuel Ortizd6469602011-12-14 16:43:12 +01001386 nfc_llcp_build_gb(local);
1387
1388 local->remote_miu = LLCP_DEFAULT_MIU;
1389 local->remote_lto = LLCP_DEFAULT_LTO;
Samuel Ortizd6469602011-12-14 16:43:12 +01001390
1391 list_add(&llcp_devices, &local->list);
1392
1393 return 0;
Samuel Ortizd6469602011-12-14 16:43:12 +01001394}
1395
1396void nfc_llcp_unregister_device(struct nfc_dev *dev)
1397{
1398 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1399
1400 if (local == NULL) {
1401 pr_debug("No such device\n");
1402 return;
1403 }
1404
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001405 nfc_llcp_local_put(local);
Samuel Ortizd6469602011-12-14 16:43:12 +01001406}
1407
1408int __init nfc_llcp_init(void)
1409{
1410 INIT_LIST_HEAD(&llcp_devices);
1411
1412 return nfc_llcp_sock_init();
1413}
1414
1415void nfc_llcp_exit(void)
1416{
1417 nfc_llcp_sock_exit();
1418}