blob: 7f92a857b31965d6900a09d69098d02a8ba0ea10 [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 Ortiz4d22ea12012-06-22 14:40:34 +020048static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
Samuel Ortizd6469602011-12-14 16:43:12 +010049{
Samuel Ortiza69f32a2012-05-04 17:04:19 +020050 struct sock *sk;
51 struct hlist_node *node, *tmp;
52 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +010053
Samuel Ortiza69f32a2012-05-04 17:04:19 +020054 write_lock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +010055
Samuel Ortiza69f32a2012-05-04 17:04:19 +020056 sk_for_each_safe(sk, node, tmp, &local->sockets.head) {
57 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010058
Szymon Janc50b78b22012-09-26 14:22:10 +020059 bh_lock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010060
Samuel Ortiza69f32a2012-05-04 17:04:19 +020061 if (sk->sk_state == LLCP_CONNECTED)
62 nfc_put_device(llcp_sock->dev);
Samuel Ortizd6469602011-12-14 16:43:12 +010063
Samuel Ortiza69f32a2012-05-04 17:04:19 +020064 if (sk->sk_state == LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +010065 struct nfc_llcp_sock *lsk, *n;
66 struct sock *accept_sk;
67
Samuel Ortiza69f32a2012-05-04 17:04:19 +020068 list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +010069 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +010070 accept_sk = &lsk->sk;
Szymon Janc50b78b22012-09-26 14:22:10 +020071 bh_lock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010072
73 nfc_llcp_accept_unlink(accept_sk);
74
75 accept_sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +010076
Szymon Janc50b78b22012-09-26 14:22:10 +020077 bh_unlock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010078
79 sock_orphan(accept_sk);
80 }
Samuel Ortiz4d22ea12012-06-22 14:40:34 +020081
82 if (listen == true) {
Szymon Janc50b78b22012-09-26 14:22:10 +020083 bh_unlock_sock(sk);
Samuel Ortiz4d22ea12012-06-22 14:40:34 +020084 continue;
85 }
Samuel Ortizd6469602011-12-14 16:43:12 +010086 }
87
Samuel Ortizc8512be2012-10-15 15:09:52 +020088 /*
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
Samuel Ortiza69f32a2012-05-04 17:04:19 +020098 sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +010099
Szymon Janc50b78b22012-09-26 14:22:10 +0200100 bh_unlock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100101
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200102 sock_orphan(sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100103
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200104 sk_del_node_init(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100105 }
106
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200107 write_unlock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100108}
109
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200110struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
111{
112 kref_get(&local->ref);
113
114 return local;
115}
116
117static 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);
Samuel Ortiz4d22ea12012-06-22 14:40:34 +0200124 nfc_llcp_socket_release(local, false);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200125 del_timer_sync(&local->link_timer);
126 skb_queue_purge(&local->tx_queue);
Tejun Heo474fee32012-08-22 16:22:16 -0700127 cancel_work_sync(&local->tx_work);
128 cancel_work_sync(&local->rx_work);
129 cancel_work_sync(&local->timeout_work);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200130 kfree_skb(local->rx_pending);
131 kfree(local);
132}
133
134int nfc_llcp_local_put(struct nfc_llcp_local *local)
135{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200136 if (local == NULL)
137 return 0;
138
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200139 return kref_put(&local->ref, local_release);
140}
141
Samuel Ortiz8f500202012-06-25 15:46:28 +0200142static 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;
147 struct nfc_llcp_sock *llcp_sock;
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) {
159 llcp_sock = nfc_llcp_sock(sk);
160
161 if (llcp_sock->ssap == ssap && llcp_sock->dsap == dsap)
162 break;
163 }
164
165 read_unlock(&local->sockets.lock);
166
167 if (llcp_sock == NULL)
168 return NULL;
169
170 sock_hold(&llcp_sock->sk);
171
172 return llcp_sock;
173}
174
175static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
176{
177 sock_put(&sock->sk);
178}
179
Samuel Ortizd6469602011-12-14 16:43:12 +0100180static void nfc_llcp_timeout_work(struct work_struct *work)
181{
182 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100183 timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100184
185 nfc_dep_link_down(local->dev);
186}
187
188static void nfc_llcp_symm_timer(unsigned long data)
189{
190 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
191
192 pr_err("SYMM timeout\n");
193
Linus Torvalds916082b2012-10-02 16:01:31 -0700194 schedule_work(&local->timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100195}
196
197struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
198{
199 struct nfc_llcp_local *local, *n;
200
201 list_for_each_entry_safe(local, n, &llcp_devices, list)
202 if (local->dev == dev)
203 return local;
204
205 pr_debug("No device found\n");
206
207 return NULL;
208}
209
210static char *wks[] = {
211 NULL,
212 NULL, /* SDP */
213 "urn:nfc:sn:ip",
214 "urn:nfc:sn:obex",
215 "urn:nfc:sn:snep",
216};
217
218static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
219{
220 int sap, num_wks;
221
222 pr_debug("%s\n", service_name);
223
224 if (service_name == NULL)
225 return -EINVAL;
226
227 num_wks = ARRAY_SIZE(wks);
228
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100229 for (sap = 0; sap < num_wks; sap++) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100230 if (wks[sap] == NULL)
231 continue;
232
233 if (strncmp(wks[sap], service_name, service_name_len) == 0)
234 return sap;
235 }
236
237 return -EINVAL;
238}
239
Samuel Ortiz8f500202012-06-25 15:46:28 +0200240static
241struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
242 u8 *sn, size_t sn_len)
243{
244 struct sock *sk;
245 struct hlist_node *node;
246 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
247
248 pr_debug("sn %zd %p\n", sn_len, sn);
249
250 if (sn == NULL || sn_len == 0)
251 return NULL;
252
253 read_lock(&local->sockets.lock);
254
255 llcp_sock = NULL;
256
257 sk_for_each(sk, node, &local->sockets.head) {
258 tmp_sock = nfc_llcp_sock(sk);
259
260 pr_debug("llcp sock %p\n", tmp_sock);
261
Samuel Ortiz54292d62012-10-15 15:08:29 +0200262 if (tmp_sock->sk.sk_type == SOCK_STREAM &&
263 tmp_sock->sk.sk_state != LLCP_LISTEN)
264 continue;
265
266 if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
267 tmp_sock->sk.sk_state != LLCP_BOUND)
Samuel Ortiz8f500202012-06-25 15:46:28 +0200268 continue;
269
270 if (tmp_sock->service_name == NULL ||
271 tmp_sock->service_name_len == 0)
272 continue;
273
274 if (tmp_sock->service_name_len != sn_len)
275 continue;
276
277 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
278 llcp_sock = tmp_sock;
279 break;
280 }
281 }
282
283 read_unlock(&local->sockets.lock);
284
285 pr_debug("Found llcp sock %p\n", llcp_sock);
286
287 return llcp_sock;
288}
289
Samuel Ortizd6469602011-12-14 16:43:12 +0100290u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100291 struct nfc_llcp_sock *sock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100292{
293 mutex_lock(&local->sdp_lock);
294
295 if (sock->service_name != NULL && sock->service_name_len > 0) {
296 int ssap = nfc_llcp_wks_sap(sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100297 sock->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100298
299 if (ssap > 0) {
300 pr_debug("WKS %d\n", ssap);
301
302 /* This is a WKS, let's check if it's free */
303 if (local->local_wks & BIT(ssap)) {
304 mutex_unlock(&local->sdp_lock);
305
306 return LLCP_SAP_MAX;
307 }
308
Samuel Ortiz1762c172012-03-05 01:03:38 +0100309 set_bit(ssap, &local->local_wks);
Samuel Ortizd6469602011-12-14 16:43:12 +0100310 mutex_unlock(&local->sdp_lock);
311
312 return ssap;
313 }
314
315 /*
Samuel Ortiz8f500202012-06-25 15:46:28 +0200316 * Check if there already is a non WKS socket bound
317 * to this service name.
Samuel Ortizd6469602011-12-14 16:43:12 +0100318 */
Samuel Ortiz8f500202012-06-25 15:46:28 +0200319 if (nfc_llcp_sock_from_sn(local, sock->service_name,
320 sock->service_name_len) != NULL) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100321 mutex_unlock(&local->sdp_lock);
322
323 return LLCP_SAP_MAX;
324 }
325
Samuel Ortizd6469602011-12-14 16:43:12 +0100326 mutex_unlock(&local->sdp_lock);
327
Samuel Ortiz8f500202012-06-25 15:46:28 +0200328 return LLCP_SDP_UNBOUND;
Samuel Ortizd6469602011-12-14 16:43:12 +0100329
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200330 } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
331 if (!test_bit(sock->ssap, &local->local_wks)) {
332 set_bit(sock->ssap, &local->local_wks);
333 mutex_unlock(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100334
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200335 return sock->ssap;
Samuel Ortizd6469602011-12-14 16:43:12 +0100336 }
337 }
338
339 mutex_unlock(&local->sdp_lock);
340
341 return LLCP_SAP_MAX;
342}
343
344u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
345{
346 u8 local_ssap;
347
348 mutex_lock(&local->sdp_lock);
349
350 local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
351 if (local_ssap == LLCP_LOCAL_NUM_SAP) {
352 mutex_unlock(&local->sdp_lock);
353 return LLCP_SAP_MAX;
354 }
355
Samuel Ortiz1762c172012-03-05 01:03:38 +0100356 set_bit(local_ssap, &local->local_sap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100357
358 mutex_unlock(&local->sdp_lock);
359
360 return local_ssap + LLCP_LOCAL_SAP_OFFSET;
361}
362
363void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
364{
365 u8 local_ssap;
366 unsigned long *sdp;
367
368 if (ssap < LLCP_WKS_NUM_SAP) {
369 local_ssap = ssap;
370 sdp = &local->local_wks;
371 } else if (ssap < LLCP_LOCAL_NUM_SAP) {
Samuel Ortiz8f500202012-06-25 15:46:28 +0200372 atomic_t *client_cnt;
373
Samuel Ortizd6469602011-12-14 16:43:12 +0100374 local_ssap = ssap - LLCP_WKS_NUM_SAP;
375 sdp = &local->local_sdp;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200376 client_cnt = &local->local_sdp_cnt[local_ssap];
377
378 pr_debug("%d clients\n", atomic_read(client_cnt));
379
380 mutex_lock(&local->sdp_lock);
381
382 if (atomic_dec_and_test(client_cnt)) {
383 struct nfc_llcp_sock *l_sock;
384
385 pr_debug("No more clients for SAP %d\n", ssap);
386
387 clear_bit(local_ssap, sdp);
388
389 /* Find the listening sock and set it back to UNBOUND */
390 l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
391 if (l_sock) {
392 l_sock->ssap = LLCP_SDP_UNBOUND;
393 nfc_llcp_sock_put(l_sock);
394 }
395 }
396
397 mutex_unlock(&local->sdp_lock);
398
399 return;
Samuel Ortizd6469602011-12-14 16:43:12 +0100400 } else if (ssap < LLCP_MAX_SAP) {
401 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
402 sdp = &local->local_sap;
403 } else {
404 return;
405 }
406
407 mutex_lock(&local->sdp_lock);
408
Samuel Ortiz1762c172012-03-05 01:03:38 +0100409 clear_bit(local_ssap, sdp);
Samuel Ortizd6469602011-12-14 16:43:12 +0100410
411 mutex_unlock(&local->sdp_lock);
412}
413
Samuel Ortiz8f500202012-06-25 15:46:28 +0200414static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
415{
416 u8 ssap;
417
418 mutex_lock(&local->sdp_lock);
419
420 ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
421 if (ssap == LLCP_SDP_NUM_SAP) {
422 mutex_unlock(&local->sdp_lock);
423
424 return LLCP_SAP_MAX;
425 }
426
427 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
428
429 set_bit(ssap, &local->local_sdp);
430
431 mutex_unlock(&local->sdp_lock);
432
433 return LLCP_WKS_NUM_SAP + ssap;
434}
435
Samuel Ortizd6469602011-12-14 16:43:12 +0100436static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
437{
438 u8 *gb_cur, *version_tlv, version, version_length;
439 u8 *lto_tlv, lto, lto_length;
440 u8 *wks_tlv, wks_length;
Samuel Ortiz56d58762012-04-10 19:43:19 +0200441 u8 *miux_tlv, miux_length;
442 __be16 miux;
Samuel Ortizd6469602011-12-14 16:43:12 +0100443 u8 gb_len = 0;
Wei Yongjun52da2442012-09-02 21:21:46 +0800444 int ret = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100445
446 version = LLCP_VERSION_11;
447 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100448 1, &version_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100449 gb_len += version_length;
450
451 /* 1500 ms */
452 lto = 150;
Samuel Ortiz91b0ade2012-04-10 19:43:20 +0200453 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &lto, 1, &lto_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100454 gb_len += lto_length;
455
456 pr_debug("Local wks 0x%lx\n", local->local_wks);
457 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100458 &wks_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100459 gb_len += wks_length;
460
Samuel Ortiz56d58762012-04-10 19:43:19 +0200461 miux = cpu_to_be16(LLCP_MAX_MIUX);
462 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
463 &miux_length);
464 gb_len += miux_length;
465
Samuel Ortizd6469602011-12-14 16:43:12 +0100466 gb_len += ARRAY_SIZE(llcp_magic);
467
468 if (gb_len > NFC_MAX_GT_LEN) {
Wei Yongjun52da2442012-09-02 21:21:46 +0800469 ret = -EINVAL;
470 goto out;
Samuel Ortizd6469602011-12-14 16:43:12 +0100471 }
472
473 gb_cur = local->gb;
474
475 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
476 gb_cur += ARRAY_SIZE(llcp_magic);
477
478 memcpy(gb_cur, version_tlv, version_length);
479 gb_cur += version_length;
480
481 memcpy(gb_cur, lto_tlv, lto_length);
482 gb_cur += lto_length;
483
484 memcpy(gb_cur, wks_tlv, wks_length);
485 gb_cur += wks_length;
486
Samuel Ortiz56d58762012-04-10 19:43:19 +0200487 memcpy(gb_cur, miux_tlv, miux_length);
488 gb_cur += miux_length;
489
Samuel Ortizd6469602011-12-14 16:43:12 +0100490 local->gb_len = gb_len;
491
Wei Yongjun52da2442012-09-02 21:21:46 +0800492out:
493 kfree(version_tlv);
494 kfree(lto_tlv);
495 kfree(wks_tlv);
496 kfree(miux_tlv);
497
498 return ret;
Samuel Ortizd6469602011-12-14 16:43:12 +0100499}
500
Samuel Ortizb8e7a062012-06-22 02:04:53 +0200501u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
502{
503 struct nfc_llcp_local *local;
504
505 local = nfc_llcp_find_local(dev);
506 if (local == NULL) {
507 *general_bytes_len = 0;
508 return NULL;
509 }
510
511 nfc_llcp_build_gb(local);
512
513 *general_bytes_len = local->gb_len;
514
515 return local->gb;
516}
517
Samuel Ortizd6469602011-12-14 16:43:12 +0100518int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
519{
520 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
521
522 if (local == NULL) {
523 pr_err("No LLCP device\n");
524 return -ENODEV;
525 }
526
527 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
528 memcpy(local->remote_gb, gb, gb_len);
529 local->remote_gb_len = gb_len;
530
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100531 if (local->remote_gb == NULL || local->remote_gb_len == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100532 return -ENODEV;
533
534 if (memcmp(local->remote_gb, llcp_magic, 3)) {
535 pr_err("MAC does not support LLCP\n");
536 return -EINVAL;
537 }
538
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200539 return nfc_llcp_parse_gb_tlv(local,
540 &local->remote_gb[3],
541 local->remote_gb_len - 3);
Samuel Ortizd6469602011-12-14 16:43:12 +0100542}
543
Samuel Ortizd6469602011-12-14 16:43:12 +0100544static u8 nfc_llcp_dsap(struct sk_buff *pdu)
545{
546 return (pdu->data[0] & 0xfc) >> 2;
547}
548
549static u8 nfc_llcp_ptype(struct sk_buff *pdu)
550{
551 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
552}
553
554static u8 nfc_llcp_ssap(struct sk_buff *pdu)
555{
556 return pdu->data[1] & 0x3f;
557}
558
559static u8 nfc_llcp_ns(struct sk_buff *pdu)
560{
561 return pdu->data[2] >> 4;
562}
563
564static u8 nfc_llcp_nr(struct sk_buff *pdu)
565{
566 return pdu->data[2] & 0xf;
567}
568
569static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
570{
Samuel Ortiz279cf172012-04-10 19:43:14 +0200571 pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
Samuel Ortizd6469602011-12-14 16:43:12 +0100572 sock->send_n = (sock->send_n + 1) % 16;
573 sock->recv_ack_n = (sock->recv_n - 1) % 16;
574}
575
Thierry Escande44635232012-09-26 18:16:44 +0200576void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
577 struct sk_buff *skb, u8 direction)
578{
579 struct hlist_node *node;
580 struct sk_buff *skb_copy = NULL, *nskb;
581 struct sock *sk;
582 u8 *data;
583
584 read_lock(&local->raw_sockets.lock);
585
586 sk_for_each(sk, node, &local->raw_sockets.head) {
587 if (sk->sk_state != LLCP_BOUND)
588 continue;
589
590 if (skb_copy == NULL) {
591 skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE,
592 GFP_ATOMIC);
593
594 if (skb_copy == NULL)
595 continue;
596
597 data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE);
598
599 data[0] = local->dev ? local->dev->idx : 0xFF;
600 data[1] = direction;
601 }
602
603 nskb = skb_clone(skb_copy, GFP_ATOMIC);
604 if (!nskb)
605 continue;
606
607 if (sock_queue_rcv_skb(sk, nskb))
608 kfree_skb(nskb);
609 }
610
611 read_unlock(&local->raw_sockets.lock);
612
613 kfree_skb(skb_copy);
614}
615
Samuel Ortiz84457962012-05-30 17:48:29 +0200616static void nfc_llcp_tx_work(struct work_struct *work)
617{
618 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
619 tx_work);
620 struct sk_buff *skb;
621 struct sock *sk;
622 struct nfc_llcp_sock *llcp_sock;
623
624 skb = skb_dequeue(&local->tx_queue);
625 if (skb != NULL) {
626 sk = skb->sk;
627 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiza6a09152012-10-05 00:37:22 +0200628
629 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
630 nfc_llcp_send_symm(local->dev);
631 } else {
Samuel Ortiz84457962012-05-30 17:48:29 +0200632 int ret;
633
634 pr_debug("Sending pending skb\n");
635 print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
636 DUMP_PREFIX_OFFSET, 16, 1,
637 skb->data, skb->len, true);
638
Thierry Escande44635232012-09-26 18:16:44 +0200639 nfc_llcp_send_to_raw_sock(local, skb,
640 NFC_LLCP_DIRECTION_TX);
641
Samuel Ortiz84457962012-05-30 17:48:29 +0200642 ret = nfc_data_exchange(local->dev, local->target_idx,
643 skb, nfc_llcp_recv, local);
644
645 if (!ret && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
646 skb = skb_get(skb);
647 skb_queue_tail(&llcp_sock->tx_pending_queue,
648 skb);
649 }
Samuel Ortiz84457962012-05-30 17:48:29 +0200650 }
651 } else {
652 nfc_llcp_send_symm(local->dev);
653 }
654
655 mod_timer(&local->link_timer,
656 jiffies + msecs_to_jiffies(2 * local->remote_lto));
657}
658
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200659static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
660 u8 ssap)
661{
662 struct sock *sk;
663 struct nfc_llcp_sock *llcp_sock;
664 struct hlist_node *node;
665
666 read_lock(&local->connecting_sockets.lock);
667
668 sk_for_each(sk, node, &local->connecting_sockets.head) {
669 llcp_sock = nfc_llcp_sock(sk);
670
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200671 if (llcp_sock->ssap == ssap) {
672 sock_hold(&llcp_sock->sk);
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200673 goto out;
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200674 }
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200675 }
676
677 llcp_sock = NULL;
678
679out:
680 read_unlock(&local->connecting_sockets.lock);
681
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200682 return llcp_sock;
683}
684
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200685static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
686 u8 *sn, size_t sn_len)
687{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200688 struct nfc_llcp_sock *llcp_sock;
689
Samuel Ortiz8f500202012-06-25 15:46:28 +0200690 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100691
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200692 if (llcp_sock == NULL)
693 return NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100694
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200695 sock_hold(&llcp_sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100696
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200697 return llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +0100698}
699
Samuel Ortizd6469602011-12-14 16:43:12 +0100700static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
701{
702 u8 *tlv = &skb->data[2], type, length;
703 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
704
705 while (offset < tlv_array_len) {
706 type = tlv[0];
707 length = tlv[1];
708
709 pr_debug("type 0x%x length %d\n", type, length);
710
711 if (type == LLCP_TLV_SN) {
712 *sn_len = length;
713 return &tlv[2];
714 }
715
716 offset += length + 2;
717 tlv += length + 2;
718 }
719
720 return NULL;
721}
722
723static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100724 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100725{
726 struct sock *new_sk, *parent;
727 struct nfc_llcp_sock *sock, *new_sock;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200728 u8 dsap, ssap, reason;
Samuel Ortizd6469602011-12-14 16:43:12 +0100729
730 dsap = nfc_llcp_dsap(skb);
731 ssap = nfc_llcp_ssap(skb);
732
733 pr_debug("%d %d\n", dsap, ssap);
734
Samuel Ortizd6469602011-12-14 16:43:12 +0100735 if (dsap != LLCP_SAP_SDP) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200736 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
737 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100738 reason = LLCP_DM_NOBOUND;
739 goto fail;
740 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100741 } else {
742 u8 *sn;
743 size_t sn_len;
744
745 sn = nfc_llcp_connect_sn(skb, &sn_len);
746 if (sn == NULL) {
747 reason = LLCP_DM_NOBOUND;
748 goto fail;
749 }
750
751 pr_debug("Service name length %zu\n", sn_len);
752
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200753 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
754 if (sock == NULL) {
755 reason = LLCP_DM_NOBOUND;
756 goto fail;
Samuel Ortizd6469602011-12-14 16:43:12 +0100757 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100758 }
759
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200760 lock_sock(&sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100761
Samuel Ortizd6469602011-12-14 16:43:12 +0100762 parent = &sock->sk;
763
764 if (sk_acceptq_is_full(parent)) {
765 reason = LLCP_DM_REJ;
766 release_sock(&sock->sk);
767 sock_put(&sock->sk);
768 goto fail;
769 }
770
Samuel Ortiz8f500202012-06-25 15:46:28 +0200771 if (sock->ssap == LLCP_SDP_UNBOUND) {
772 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
773
774 pr_debug("First client, reserving %d\n", ssap);
775
776 if (ssap == LLCP_SAP_MAX) {
777 reason = LLCP_DM_REJ;
778 release_sock(&sock->sk);
779 sock_put(&sock->sk);
780 goto fail;
781 }
782
783 sock->ssap = ssap;
784 }
785
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100786 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC);
Samuel Ortizd6469602011-12-14 16:43:12 +0100787 if (new_sk == NULL) {
788 reason = LLCP_DM_REJ;
789 release_sock(&sock->sk);
790 sock_put(&sock->sk);
791 goto fail;
792 }
793
794 new_sock = nfc_llcp_sock(new_sk);
795 new_sock->dev = local->dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200796 new_sock->local = nfc_llcp_local_get(local);
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200797 new_sock->miu = local->remote_miu;
Samuel Ortizd6469602011-12-14 16:43:12 +0100798 new_sock->nfc_protocol = sock->nfc_protocol;
Samuel Ortizd6469602011-12-14 16:43:12 +0100799 new_sock->dsap = ssap;
Samuel Ortiz025f1522012-06-18 21:38:09 +0200800 new_sock->target_idx = local->target_idx;
Samuel Ortizd6469602011-12-14 16:43:12 +0100801 new_sock->parent = parent;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200802 new_sock->ssap = sock->ssap;
803 if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
804 atomic_t *client_count;
805
806 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
807
808 client_count =
809 &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
810
811 atomic_inc(client_count);
812 new_sock->reserved_ssap = sock->ssap;
813 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100814
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200815 nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
816 skb->len - LLCP_HEADER_SIZE);
817
Samuel Ortizd6469602011-12-14 16:43:12 +0100818 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
819
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200820 nfc_llcp_sock_link(&local->sockets, new_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100821
822 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
823
824 nfc_get_device(local->dev->idx);
825
826 new_sk->sk_state = LLCP_CONNECTED;
827
828 /* Wake the listening processes */
829 parent->sk_data_ready(parent, 0);
830
831 /* Send CC */
832 nfc_llcp_send_cc(new_sock);
833
834 release_sock(&sock->sk);
835 sock_put(&sock->sk);
836
837 return;
838
839fail:
840 /* Send DM */
841 nfc_llcp_send_dm(local, dsap, ssap, reason);
Samuel Ortizd6469602011-12-14 16:43:12 +0100842}
843
Samuel Ortizd094afa2012-03-05 01:03:42 +0100844int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100845{
Samuel Ortizd094afa2012-03-05 01:03:42 +0100846 int nr_frames = 0;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100847 struct nfc_llcp_local *local = sock->local;
848
849 pr_debug("Remote ready %d tx queue len %d remote rw %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100850 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200851 sock->rw);
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100852
853 /* Try to queue some I frames for transmission */
854 while (sock->remote_ready &&
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200855 skb_queue_len(&sock->tx_pending_queue) < sock->rw) {
Samuel Ortiz84457962012-05-30 17:48:29 +0200856 struct sk_buff *pdu;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100857
858 pdu = skb_dequeue(&sock->tx_queue);
859 if (pdu == NULL)
860 break;
861
862 /* Update N(S)/N(R) */
863 nfc_llcp_set_nrns(sock, pdu);
864
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100865 skb_queue_tail(&local->tx_queue, pdu);
Samuel Ortizd094afa2012-03-05 01:03:42 +0100866 nr_frames++;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100867 }
Samuel Ortizd094afa2012-03-05 01:03:42 +0100868
869 return nr_frames;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100870}
871
Samuel Ortizd6469602011-12-14 16:43:12 +0100872static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100873 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100874{
875 struct nfc_llcp_sock *llcp_sock;
876 struct sock *sk;
877 u8 dsap, ssap, ptype, ns, nr;
878
879 ptype = nfc_llcp_ptype(skb);
880 dsap = nfc_llcp_dsap(skb);
881 ssap = nfc_llcp_ssap(skb);
882 ns = nfc_llcp_ns(skb);
883 nr = nfc_llcp_nr(skb);
884
885 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
886
887 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
888 if (llcp_sock == NULL) {
889 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
890 return;
891 }
892
893 sk = &llcp_sock->sk;
894 lock_sock(sk);
895 if (sk->sk_state == LLCP_CLOSED) {
896 release_sock(sk);
897 nfc_llcp_sock_put(llcp_sock);
898 }
899
Samuel Ortizd6469602011-12-14 16:43:12 +0100900 /* Pass the payload upstream */
901 if (ptype == LLCP_PDU_I) {
902 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
903
Samuel Ortiz53aef922012-03-05 01:03:36 +0100904 if (ns == llcp_sock->recv_n)
905 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
906 else
907 pr_err("Received out of sequence I PDU\n");
908
Samuel Ortizd6469602011-12-14 16:43:12 +0100909 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
910 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
911 pr_err("receive queue is full\n");
912 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
913 }
914 }
915
916 /* Remove skbs from the pending queue */
917 if (llcp_sock->send_ack_n != nr) {
918 struct sk_buff *s, *tmp;
919
920 llcp_sock->send_ack_n = nr;
921
Samuel Ortiz84457962012-05-30 17:48:29 +0200922 /* Remove and free all skbs until ns == nr */
923 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
924 skb_unlink(s, &llcp_sock->tx_pending_queue);
925 kfree_skb(s);
926
927 if (nfc_llcp_ns(s) == nr)
928 break;
929 }
930
931 /* Re-queue the remaining skbs for transmission */
932 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
933 s, tmp) {
934 skb_unlink(s, &llcp_sock->tx_pending_queue);
935 skb_queue_head(&local->tx_queue, s);
936 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100937 }
938
Samuel Ortiz53aef922012-03-05 01:03:36 +0100939 if (ptype == LLCP_PDU_RR)
940 llcp_sock->remote_ready = true;
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100941 else if (ptype == LLCP_PDU_RNR)
Samuel Ortiz53aef922012-03-05 01:03:36 +0100942 llcp_sock->remote_ready = false;
943
Samuel Ortiz56af25682012-05-30 18:06:11 +0200944 if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
Samuel Ortizd094afa2012-03-05 01:03:42 +0100945 nfc_llcp_send_rr(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100946
947 release_sock(sk);
948 nfc_llcp_sock_put(llcp_sock);
949}
950
951static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100952 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100953{
954 struct nfc_llcp_sock *llcp_sock;
955 struct sock *sk;
956 u8 dsap, ssap;
957
958 dsap = nfc_llcp_dsap(skb);
959 ssap = nfc_llcp_ssap(skb);
960
961 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
962 if (llcp_sock == NULL) {
963 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
964 return;
965 }
966
967 sk = &llcp_sock->sk;
968 lock_sock(sk);
969 if (sk->sk_state == LLCP_CLOSED) {
970 release_sock(sk);
971 nfc_llcp_sock_put(llcp_sock);
972 }
973
Samuel Ortizd6469602011-12-14 16:43:12 +0100974 if (sk->sk_state == LLCP_CONNECTED) {
975 nfc_put_device(local->dev);
976 sk->sk_state = LLCP_CLOSED;
977 sk->sk_state_change(sk);
978 }
979
980 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
981
982 release_sock(sk);
983 nfc_llcp_sock_put(llcp_sock);
984}
985
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100986static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100987{
988 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizff353d82012-05-07 12:31:19 +0200989 struct sock *sk;
Samuel Ortizd6469602011-12-14 16:43:12 +0100990 u8 dsap, ssap;
991
Samuel Ortizd6469602011-12-14 16:43:12 +0100992 dsap = nfc_llcp_dsap(skb);
993 ssap = nfc_llcp_ssap(skb);
994
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200995 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100996 if (llcp_sock == NULL) {
997 pr_err("Invalid CC\n");
998 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
999
1000 return;
1001 }
1002
Samuel Ortizff353d82012-05-07 12:31:19 +02001003 sk = &llcp_sock->sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001004
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001005 /* Unlink from connecting and link to the client array */
1006 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
1007 nfc_llcp_sock_link(&local->sockets, sk);
1008 llcp_sock->dsap = ssap;
1009
Samuel Ortiz7a06e582012-05-07 22:03:34 +02001010 nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
1011 skb->len - LLCP_HEADER_SIZE);
Samuel Ortizd6469602011-12-14 16:43:12 +01001012
Samuel Ortizff353d82012-05-07 12:31:19 +02001013 sk->sk_state = LLCP_CONNECTED;
1014 sk->sk_state_change(sk);
1015
Samuel Ortizd6469602011-12-14 16:43:12 +01001016 nfc_llcp_sock_put(llcp_sock);
1017}
1018
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001019static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1020{
1021 struct nfc_llcp_sock *llcp_sock;
1022 struct sock *sk;
1023 u8 dsap, ssap, reason;
1024
1025 dsap = nfc_llcp_dsap(skb);
1026 ssap = nfc_llcp_ssap(skb);
1027 reason = skb->data[2];
1028
1029 pr_debug("%d %d reason %d\n", ssap, dsap, reason);
1030
1031 switch (reason) {
1032 case LLCP_DM_NOBOUND:
1033 case LLCP_DM_REJ:
1034 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1035 break;
1036
1037 default:
1038 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1039 break;
1040 }
1041
1042 if (llcp_sock == NULL) {
1043 pr_err("Invalid DM\n");
1044 return;
1045 }
1046
1047 sk = &llcp_sock->sk;
1048
1049 sk->sk_err = ENXIO;
1050 sk->sk_state = LLCP_CLOSED;
1051 sk->sk_state_change(sk);
1052
1053 nfc_llcp_sock_put(llcp_sock);
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001054}
1055
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001056static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1057 struct sk_buff *skb)
1058{
1059 struct nfc_llcp_sock *llcp_sock;
1060 u8 dsap, ssap, *tlv, type, length, tid, sap;
1061 u16 tlv_len, offset;
1062 char *service_name;
1063 size_t service_name_len;
1064
1065 dsap = nfc_llcp_dsap(skb);
1066 ssap = nfc_llcp_ssap(skb);
1067
1068 pr_debug("%d %d\n", dsap, ssap);
1069
1070 if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1071 pr_err("Wrong SNL SAP\n");
1072 return;
1073 }
1074
1075 tlv = &skb->data[LLCP_HEADER_SIZE];
1076 tlv_len = skb->len - LLCP_HEADER_SIZE;
1077 offset = 0;
1078
1079 while(offset < tlv_len) {
1080 type = tlv[0];
1081 length = tlv[1];
1082
1083 switch (type) {
1084 case LLCP_TLV_SDREQ:
1085 tid = tlv[2];
1086 service_name = (char *) &tlv[3];
1087 service_name_len = length - 1;
1088
Samuel Ortize6904082012-10-15 14:28:13 +02001089 pr_debug("Looking for %.16s\n", service_name);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001090
1091 if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1092 !strncmp(service_name, "urn:nfc:sn:sdp",
1093 service_name_len)) {
1094 sap = 1;
Samuel Ortize6904082012-10-15 14:28:13 +02001095 goto send_snl;
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001096 }
1097
Samuel Ortize6904082012-10-15 14:28:13 +02001098 llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
1099 service_name_len);
1100 if (!llcp_sock) {
1101 sap = 0;
1102 goto send_snl;
1103 }
1104
1105 /*
1106 * We found a socket but its ssap has not been reserved
1107 * yet. We need to assign it for good and send a reply.
1108 * The ssap will be freed when the socket is closed.
1109 */
1110 if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1111 atomic_t *client_count;
1112
1113 sap = nfc_llcp_reserve_sdp_ssap(local);
1114
1115 pr_debug("Reserving %d\n", sap);
1116
1117 if (sap == LLCP_SAP_MAX) {
1118 sap = 0;
1119 goto send_snl;
1120 }
1121
1122 client_count =
1123 &local->local_sdp_cnt[sap -
1124 LLCP_WKS_NUM_SAP];
1125
1126 atomic_inc(client_count);
1127
1128 llcp_sock->ssap = sap;
1129 llcp_sock->reserved_ssap = sap;
1130 } else {
1131 sap = llcp_sock->ssap;
1132 }
1133
1134 pr_debug("%p %d\n", llcp_sock, sap);
1135
1136 send_snl:
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001137 nfc_llcp_send_snl(local, tid, sap);
1138 break;
1139
1140 default:
1141 pr_err("Invalid SNL tlv value 0x%x\n", type);
1142 break;
1143 }
1144
1145 offset += length + 2;
1146 tlv += length + 2;
1147 }
1148}
1149
Samuel Ortizd6469602011-12-14 16:43:12 +01001150static void nfc_llcp_rx_work(struct work_struct *work)
1151{
1152 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001153 rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001154 u8 dsap, ssap, ptype;
1155 struct sk_buff *skb;
1156
1157 skb = local->rx_pending;
1158 if (skb == NULL) {
1159 pr_debug("No pending SKB\n");
1160 return;
1161 }
1162
1163 ptype = nfc_llcp_ptype(skb);
1164 dsap = nfc_llcp_dsap(skb);
1165 ssap = nfc_llcp_ssap(skb);
1166
1167 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1168
Samuel Ortiz4be646e2012-04-10 19:43:13 +02001169 if (ptype != LLCP_PDU_SYMM)
1170 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
1171 16, 1, skb->data, skb->len, true);
1172
Thierry Escande44635232012-09-26 18:16:44 +02001173 nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX);
1174
Samuel Ortizd6469602011-12-14 16:43:12 +01001175 switch (ptype) {
1176 case LLCP_PDU_SYMM:
1177 pr_debug("SYMM\n");
1178 break;
1179
1180 case LLCP_PDU_CONNECT:
1181 pr_debug("CONNECT\n");
1182 nfc_llcp_recv_connect(local, skb);
1183 break;
1184
1185 case LLCP_PDU_DISC:
1186 pr_debug("DISC\n");
1187 nfc_llcp_recv_disc(local, skb);
1188 break;
1189
1190 case LLCP_PDU_CC:
1191 pr_debug("CC\n");
1192 nfc_llcp_recv_cc(local, skb);
1193 break;
1194
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001195 case LLCP_PDU_DM:
1196 pr_debug("DM\n");
1197 nfc_llcp_recv_dm(local, skb);
1198 break;
1199
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001200 case LLCP_PDU_SNL:
1201 pr_debug("SNL\n");
1202 nfc_llcp_recv_snl(local, skb);
1203 break;
1204
Samuel Ortizd6469602011-12-14 16:43:12 +01001205 case LLCP_PDU_I:
1206 case LLCP_PDU_RR:
Samuel Ortiz53aef922012-03-05 01:03:36 +01001207 case LLCP_PDU_RNR:
Samuel Ortizd6469602011-12-14 16:43:12 +01001208 pr_debug("I frame\n");
1209 nfc_llcp_recv_hdlc(local, skb);
1210 break;
1211
1212 }
1213
Linus Torvalds916082b2012-10-02 16:01:31 -07001214 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001215 kfree_skb(local->rx_pending);
1216 local->rx_pending = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +01001217}
1218
1219void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1220{
1221 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1222
1223 pr_debug("Received an LLCP PDU\n");
1224 if (err < 0) {
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001225 pr_err("err %d\n", err);
Samuel Ortizd6469602011-12-14 16:43:12 +01001226 return;
1227 }
1228
1229 local->rx_pending = skb_get(skb);
1230 del_timer(&local->link_timer);
Linus Torvalds916082b2012-10-02 16:01:31 -07001231 schedule_work(&local->rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001232}
1233
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001234int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1235{
1236 struct nfc_llcp_local *local;
1237
1238 local = nfc_llcp_find_local(dev);
1239 if (local == NULL)
1240 return -ENODEV;
1241
1242 local->rx_pending = skb_get(skb);
1243 del_timer(&local->link_timer);
Linus Torvalds916082b2012-10-02 16:01:31 -07001244 schedule_work(&local->rx_work);
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001245
1246 return 0;
1247}
1248
Samuel Ortizd6469602011-12-14 16:43:12 +01001249void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1250{
1251 struct nfc_llcp_local *local;
1252
1253 local = nfc_llcp_find_local(dev);
1254 if (local == NULL)
1255 return;
1256
1257 /* Close and purge all existing sockets */
Samuel Ortiz4d22ea12012-06-22 14:40:34 +02001258 nfc_llcp_socket_release(local, true);
Samuel Ortizd6469602011-12-14 16:43:12 +01001259}
1260
1261void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1262 u8 comm_mode, u8 rf_mode)
1263{
1264 struct nfc_llcp_local *local;
1265
1266 pr_debug("rf mode %d\n", rf_mode);
1267
1268 local = nfc_llcp_find_local(dev);
1269 if (local == NULL)
1270 return;
1271
1272 local->target_idx = target_idx;
1273 local->comm_mode = comm_mode;
1274 local->rf_mode = rf_mode;
1275
1276 if (rf_mode == NFC_RF_INITIATOR) {
1277 pr_debug("Queueing Tx work\n");
1278
Linus Torvalds916082b2012-10-02 16:01:31 -07001279 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001280 } else {
1281 mod_timer(&local->link_timer,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001282 jiffies + msecs_to_jiffies(local->remote_lto));
Samuel Ortizd6469602011-12-14 16:43:12 +01001283 }
1284}
1285
1286int nfc_llcp_register_device(struct nfc_dev *ndev)
1287{
Samuel Ortizd6469602011-12-14 16:43:12 +01001288 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +01001289
1290 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
1291 if (local == NULL)
1292 return -ENOMEM;
1293
1294 local->dev = ndev;
1295 INIT_LIST_HEAD(&local->list);
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001296 kref_init(&local->ref);
Samuel Ortizd6469602011-12-14 16:43:12 +01001297 mutex_init(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001298 init_timer(&local->link_timer);
1299 local->link_timer.data = (unsigned long) local;
1300 local->link_timer.function = nfc_llcp_symm_timer;
1301
1302 skb_queue_head_init(&local->tx_queue);
1303 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001304
1305 local->rx_pending = NULL;
1306 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001307
1308 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001309
Szymon Jancfe235b52012-09-25 12:42:50 +02001310 rwlock_init(&local->sockets.lock);
1311 rwlock_init(&local->connecting_sockets.lock);
Thierry Escande44635232012-09-26 18:16:44 +02001312 rwlock_init(&local->raw_sockets.lock);
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001313
Samuel Ortizd6469602011-12-14 16:43:12 +01001314 nfc_llcp_build_gb(local);
1315
1316 local->remote_miu = LLCP_DEFAULT_MIU;
1317 local->remote_lto = LLCP_DEFAULT_LTO;
Samuel Ortizd6469602011-12-14 16:43:12 +01001318
1319 list_add(&llcp_devices, &local->list);
1320
1321 return 0;
Samuel Ortizd6469602011-12-14 16:43:12 +01001322}
1323
1324void nfc_llcp_unregister_device(struct nfc_dev *dev)
1325{
1326 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1327
1328 if (local == NULL) {
1329 pr_debug("No such device\n");
1330 return;
1331 }
1332
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001333 nfc_llcp_local_put(local);
Samuel Ortizd6469602011-12-14 16:43:12 +01001334}
1335
1336int __init nfc_llcp_init(void)
1337{
1338 INIT_LIST_HEAD(&llcp_devices);
1339
1340 return nfc_llcp_sock_init();
1341}
1342
1343void nfc_llcp_exit(void)
1344{
1345 nfc_llcp_sock_exit();
1346}