blob: 6c5971437826a7b5f0b365a956012257efaf373d [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;
Thierry Escande52feb442012-10-17 14:43:39 +0200470 u8 *lto_tlv, lto_length;
Samuel Ortizd6469602011-12-14 16:43:12 +0100471 u8 *wks_tlv, wks_length;
Samuel Ortiz56d58762012-04-10 19:43:19 +0200472 u8 *miux_tlv, miux_length;
Samuel Ortizd6469602011-12-14 16:43:12 +0100473 u8 gb_len = 0;
Wei Yongjun52da2442012-09-02 21:21:46 +0800474 int ret = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100475
476 version = LLCP_VERSION_11;
477 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100478 1, &version_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100479 gb_len += version_length;
480
Thierry Escande52feb442012-10-17 14:43:39 +0200481 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, &lto_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100482 gb_len += lto_length;
483
484 pr_debug("Local wks 0x%lx\n", local->local_wks);
485 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100486 &wks_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100487 gb_len += wks_length;
488
Thierry Escande52feb442012-10-17 14:43:39 +0200489 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
Samuel Ortiz56d58762012-04-10 19:43:19 +0200490 &miux_length);
491 gb_len += miux_length;
492
Samuel Ortizd6469602011-12-14 16:43:12 +0100493 gb_len += ARRAY_SIZE(llcp_magic);
494
495 if (gb_len > NFC_MAX_GT_LEN) {
Wei Yongjun52da2442012-09-02 21:21:46 +0800496 ret = -EINVAL;
497 goto out;
Samuel Ortizd6469602011-12-14 16:43:12 +0100498 }
499
500 gb_cur = local->gb;
501
502 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
503 gb_cur += ARRAY_SIZE(llcp_magic);
504
505 memcpy(gb_cur, version_tlv, version_length);
506 gb_cur += version_length;
507
508 memcpy(gb_cur, lto_tlv, lto_length);
509 gb_cur += lto_length;
510
511 memcpy(gb_cur, wks_tlv, wks_length);
512 gb_cur += wks_length;
513
Samuel Ortiz56d58762012-04-10 19:43:19 +0200514 memcpy(gb_cur, miux_tlv, miux_length);
515 gb_cur += miux_length;
516
Samuel Ortizd6469602011-12-14 16:43:12 +0100517 local->gb_len = gb_len;
518
Wei Yongjun52da2442012-09-02 21:21:46 +0800519out:
520 kfree(version_tlv);
521 kfree(lto_tlv);
522 kfree(wks_tlv);
523 kfree(miux_tlv);
524
525 return ret;
Samuel Ortizd6469602011-12-14 16:43:12 +0100526}
527
Samuel Ortizb8e7a062012-06-22 02:04:53 +0200528u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
529{
530 struct nfc_llcp_local *local;
531
532 local = nfc_llcp_find_local(dev);
533 if (local == NULL) {
534 *general_bytes_len = 0;
535 return NULL;
536 }
537
538 nfc_llcp_build_gb(local);
539
540 *general_bytes_len = local->gb_len;
541
542 return local->gb;
543}
544
Samuel Ortizd6469602011-12-14 16:43:12 +0100545int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
546{
547 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
548
549 if (local == NULL) {
550 pr_err("No LLCP device\n");
551 return -ENODEV;
552 }
553
554 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
555 memcpy(local->remote_gb, gb, gb_len);
556 local->remote_gb_len = gb_len;
557
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100558 if (local->remote_gb == NULL || local->remote_gb_len == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100559 return -ENODEV;
560
561 if (memcmp(local->remote_gb, llcp_magic, 3)) {
562 pr_err("MAC does not support LLCP\n");
563 return -EINVAL;
564 }
565
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200566 return nfc_llcp_parse_gb_tlv(local,
567 &local->remote_gb[3],
568 local->remote_gb_len - 3);
Samuel Ortizd6469602011-12-14 16:43:12 +0100569}
570
Samuel Ortizd6469602011-12-14 16:43:12 +0100571static u8 nfc_llcp_dsap(struct sk_buff *pdu)
572{
573 return (pdu->data[0] & 0xfc) >> 2;
574}
575
576static u8 nfc_llcp_ptype(struct sk_buff *pdu)
577{
578 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
579}
580
581static u8 nfc_llcp_ssap(struct sk_buff *pdu)
582{
583 return pdu->data[1] & 0x3f;
584}
585
586static u8 nfc_llcp_ns(struct sk_buff *pdu)
587{
588 return pdu->data[2] >> 4;
589}
590
591static u8 nfc_llcp_nr(struct sk_buff *pdu)
592{
593 return pdu->data[2] & 0xf;
594}
595
596static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
597{
Samuel Ortiz279cf172012-04-10 19:43:14 +0200598 pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
Samuel Ortizd6469602011-12-14 16:43:12 +0100599 sock->send_n = (sock->send_n + 1) % 16;
600 sock->recv_ack_n = (sock->recv_n - 1) % 16;
601}
602
Thierry Escande44635232012-09-26 18:16:44 +0200603void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
604 struct sk_buff *skb, u8 direction)
605{
606 struct hlist_node *node;
607 struct sk_buff *skb_copy = NULL, *nskb;
608 struct sock *sk;
609 u8 *data;
610
611 read_lock(&local->raw_sockets.lock);
612
613 sk_for_each(sk, node, &local->raw_sockets.head) {
614 if (sk->sk_state != LLCP_BOUND)
615 continue;
616
617 if (skb_copy == NULL) {
618 skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE,
619 GFP_ATOMIC);
620
621 if (skb_copy == NULL)
622 continue;
623
624 data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE);
625
626 data[0] = local->dev ? local->dev->idx : 0xFF;
627 data[1] = direction;
628 }
629
630 nskb = skb_clone(skb_copy, GFP_ATOMIC);
631 if (!nskb)
632 continue;
633
634 if (sock_queue_rcv_skb(sk, nskb))
635 kfree_skb(nskb);
636 }
637
638 read_unlock(&local->raw_sockets.lock);
639
640 kfree_skb(skb_copy);
641}
642
Samuel Ortiz84457962012-05-30 17:48:29 +0200643static void nfc_llcp_tx_work(struct work_struct *work)
644{
645 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
646 tx_work);
647 struct sk_buff *skb;
648 struct sock *sk;
649 struct nfc_llcp_sock *llcp_sock;
650
651 skb = skb_dequeue(&local->tx_queue);
652 if (skb != NULL) {
653 sk = skb->sk;
654 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiza6a09152012-10-05 00:37:22 +0200655
656 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
657 nfc_llcp_send_symm(local->dev);
658 } else {
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100659 struct sk_buff *copy_skb = NULL;
660 u8 ptype = nfc_llcp_ptype(skb);
Samuel Ortiz84457962012-05-30 17:48:29 +0200661 int ret;
662
663 pr_debug("Sending pending skb\n");
664 print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
665 DUMP_PREFIX_OFFSET, 16, 1,
666 skb->data, skb->len, true);
667
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100668 if (ptype == LLCP_PDU_I)
669 copy_skb = skb_copy(skb, GFP_ATOMIC);
670
Thierry Escande2c2d45b2012-11-27 15:44:24 +0100671 __net_timestamp(skb);
672
Thierry Escande44635232012-09-26 18:16:44 +0200673 nfc_llcp_send_to_raw_sock(local, skb,
674 NFC_LLCP_DIRECTION_TX);
675
Samuel Ortiz84457962012-05-30 17:48:29 +0200676 ret = nfc_data_exchange(local->dev, local->target_idx,
677 skb, nfc_llcp_recv, local);
678
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100679 if (ret) {
680 kfree_skb(copy_skb);
681 goto out;
Samuel Ortiz84457962012-05-30 17:48:29 +0200682 }
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100683
684 if (ptype == LLCP_PDU_I && copy_skb)
685 skb_queue_tail(&llcp_sock->tx_pending_queue,
686 copy_skb);
Samuel Ortiz84457962012-05-30 17:48:29 +0200687 }
688 } else {
689 nfc_llcp_send_symm(local->dev);
690 }
691
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100692out:
Samuel Ortiz84457962012-05-30 17:48:29 +0200693 mod_timer(&local->link_timer,
694 jiffies + msecs_to_jiffies(2 * local->remote_lto));
695}
696
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200697static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
698 u8 ssap)
699{
700 struct sock *sk;
701 struct nfc_llcp_sock *llcp_sock;
702 struct hlist_node *node;
703
704 read_lock(&local->connecting_sockets.lock);
705
706 sk_for_each(sk, node, &local->connecting_sockets.head) {
707 llcp_sock = nfc_llcp_sock(sk);
708
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200709 if (llcp_sock->ssap == ssap) {
710 sock_hold(&llcp_sock->sk);
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200711 goto out;
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200712 }
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200713 }
714
715 llcp_sock = NULL;
716
717out:
718 read_unlock(&local->connecting_sockets.lock);
719
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200720 return llcp_sock;
721}
722
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200723static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
724 u8 *sn, size_t sn_len)
725{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200726 struct nfc_llcp_sock *llcp_sock;
727
Samuel Ortiz8f500202012-06-25 15:46:28 +0200728 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100729
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200730 if (llcp_sock == NULL)
731 return NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100732
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200733 sock_hold(&llcp_sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100734
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200735 return llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +0100736}
737
Samuel Ortizd6469602011-12-14 16:43:12 +0100738static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
739{
740 u8 *tlv = &skb->data[2], type, length;
741 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
742
743 while (offset < tlv_array_len) {
744 type = tlv[0];
745 length = tlv[1];
746
747 pr_debug("type 0x%x length %d\n", type, length);
748
749 if (type == LLCP_TLV_SN) {
750 *sn_len = length;
751 return &tlv[2];
752 }
753
754 offset += length + 2;
755 tlv += length + 2;
756 }
757
758 return NULL;
759}
760
Samuel Ortiz968272b2012-10-15 16:14:37 +0200761static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
762 struct sk_buff *skb)
763{
764 struct nfc_llcp_sock *llcp_sock;
765 struct nfc_llcp_ui_cb *ui_cb;
766 u8 dsap, ssap;
767
768 dsap = nfc_llcp_dsap(skb);
769 ssap = nfc_llcp_ssap(skb);
770
771 ui_cb = nfc_llcp_ui_skb_cb(skb);
772 ui_cb->dsap = dsap;
773 ui_cb->ssap = ssap;
774
775 printk("%s %d %d\n", __func__, dsap, ssap);
776
777 pr_debug("%d %d\n", dsap, ssap);
778
779 /* We're looking for a bound socket, not a client one */
780 llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
781 if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
782 return;
783
784 /* There is no sequence with UI frames */
785 skb_pull(skb, LLCP_HEADER_SIZE);
786 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
787 pr_err("receive queue is full\n");
788 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
789 }
790
791 nfc_llcp_sock_put(llcp_sock);
792}
793
Samuel Ortizd6469602011-12-14 16:43:12 +0100794static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100795 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100796{
797 struct sock *new_sk, *parent;
798 struct nfc_llcp_sock *sock, *new_sock;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200799 u8 dsap, ssap, reason;
Samuel Ortizd6469602011-12-14 16:43:12 +0100800
801 dsap = nfc_llcp_dsap(skb);
802 ssap = nfc_llcp_ssap(skb);
803
804 pr_debug("%d %d\n", dsap, ssap);
805
Samuel Ortizd6469602011-12-14 16:43:12 +0100806 if (dsap != LLCP_SAP_SDP) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200807 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
808 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100809 reason = LLCP_DM_NOBOUND;
810 goto fail;
811 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100812 } else {
813 u8 *sn;
814 size_t sn_len;
815
816 sn = nfc_llcp_connect_sn(skb, &sn_len);
817 if (sn == NULL) {
818 reason = LLCP_DM_NOBOUND;
819 goto fail;
820 }
821
822 pr_debug("Service name length %zu\n", sn_len);
823
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200824 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
825 if (sock == NULL) {
826 reason = LLCP_DM_NOBOUND;
827 goto fail;
Samuel Ortizd6469602011-12-14 16:43:12 +0100828 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100829 }
830
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200831 lock_sock(&sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100832
Samuel Ortizd6469602011-12-14 16:43:12 +0100833 parent = &sock->sk;
834
835 if (sk_acceptq_is_full(parent)) {
836 reason = LLCP_DM_REJ;
837 release_sock(&sock->sk);
838 sock_put(&sock->sk);
839 goto fail;
840 }
841
Samuel Ortiz8f500202012-06-25 15:46:28 +0200842 if (sock->ssap == LLCP_SDP_UNBOUND) {
843 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
844
845 pr_debug("First client, reserving %d\n", ssap);
846
847 if (ssap == LLCP_SAP_MAX) {
848 reason = LLCP_DM_REJ;
849 release_sock(&sock->sk);
850 sock_put(&sock->sk);
851 goto fail;
852 }
853
854 sock->ssap = ssap;
855 }
856
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100857 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC);
Samuel Ortizd6469602011-12-14 16:43:12 +0100858 if (new_sk == NULL) {
859 reason = LLCP_DM_REJ;
860 release_sock(&sock->sk);
861 sock_put(&sock->sk);
862 goto fail;
863 }
864
865 new_sock = nfc_llcp_sock(new_sk);
866 new_sock->dev = local->dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200867 new_sock->local = nfc_llcp_local_get(local);
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200868 new_sock->miu = local->remote_miu;
Samuel Ortizd6469602011-12-14 16:43:12 +0100869 new_sock->nfc_protocol = sock->nfc_protocol;
Samuel Ortizd6469602011-12-14 16:43:12 +0100870 new_sock->dsap = ssap;
Samuel Ortiz025f1522012-06-18 21:38:09 +0200871 new_sock->target_idx = local->target_idx;
Samuel Ortizd6469602011-12-14 16:43:12 +0100872 new_sock->parent = parent;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200873 new_sock->ssap = sock->ssap;
874 if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
875 atomic_t *client_count;
876
877 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
878
879 client_count =
880 &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
881
882 atomic_inc(client_count);
883 new_sock->reserved_ssap = sock->ssap;
884 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100885
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200886 nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
887 skb->len - LLCP_HEADER_SIZE);
888
Samuel Ortizd6469602011-12-14 16:43:12 +0100889 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
890
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200891 nfc_llcp_sock_link(&local->sockets, new_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100892
893 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
894
895 nfc_get_device(local->dev->idx);
896
897 new_sk->sk_state = LLCP_CONNECTED;
898
899 /* Wake the listening processes */
900 parent->sk_data_ready(parent, 0);
901
902 /* Send CC */
903 nfc_llcp_send_cc(new_sock);
904
905 release_sock(&sock->sk);
906 sock_put(&sock->sk);
907
908 return;
909
910fail:
911 /* Send DM */
912 nfc_llcp_send_dm(local, dsap, ssap, reason);
Samuel Ortizd6469602011-12-14 16:43:12 +0100913}
914
Samuel Ortizd094afa2012-03-05 01:03:42 +0100915int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100916{
Samuel Ortizd094afa2012-03-05 01:03:42 +0100917 int nr_frames = 0;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100918 struct nfc_llcp_local *local = sock->local;
919
920 pr_debug("Remote ready %d tx queue len %d remote rw %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100921 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200922 sock->rw);
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100923
924 /* Try to queue some I frames for transmission */
925 while (sock->remote_ready &&
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200926 skb_queue_len(&sock->tx_pending_queue) < sock->rw) {
Samuel Ortiz84457962012-05-30 17:48:29 +0200927 struct sk_buff *pdu;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100928
929 pdu = skb_dequeue(&sock->tx_queue);
930 if (pdu == NULL)
931 break;
932
933 /* Update N(S)/N(R) */
934 nfc_llcp_set_nrns(sock, pdu);
935
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100936 skb_queue_tail(&local->tx_queue, pdu);
Samuel Ortizd094afa2012-03-05 01:03:42 +0100937 nr_frames++;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100938 }
Samuel Ortizd094afa2012-03-05 01:03:42 +0100939
940 return nr_frames;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100941}
942
Samuel Ortizd6469602011-12-14 16:43:12 +0100943static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100944 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100945{
946 struct nfc_llcp_sock *llcp_sock;
947 struct sock *sk;
948 u8 dsap, ssap, ptype, ns, nr;
949
950 ptype = nfc_llcp_ptype(skb);
951 dsap = nfc_llcp_dsap(skb);
952 ssap = nfc_llcp_ssap(skb);
953 ns = nfc_llcp_ns(skb);
954 nr = nfc_llcp_nr(skb);
955
956 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
957
958 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
959 if (llcp_sock == NULL) {
960 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
961 return;
962 }
963
964 sk = &llcp_sock->sk;
965 lock_sock(sk);
966 if (sk->sk_state == LLCP_CLOSED) {
967 release_sock(sk);
968 nfc_llcp_sock_put(llcp_sock);
969 }
970
Samuel Ortizd6469602011-12-14 16:43:12 +0100971 /* Pass the payload upstream */
972 if (ptype == LLCP_PDU_I) {
973 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
974
Samuel Ortiz53aef922012-03-05 01:03:36 +0100975 if (ns == llcp_sock->recv_n)
976 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
977 else
978 pr_err("Received out of sequence I PDU\n");
979
Samuel Ortizd6469602011-12-14 16:43:12 +0100980 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
981 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
982 pr_err("receive queue is full\n");
983 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
984 }
985 }
986
987 /* Remove skbs from the pending queue */
988 if (llcp_sock->send_ack_n != nr) {
989 struct sk_buff *s, *tmp;
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +0100990 u8 n;
Samuel Ortizd6469602011-12-14 16:43:12 +0100991
992 llcp_sock->send_ack_n = nr;
993
Samuel Ortiz84457962012-05-30 17:48:29 +0200994 /* Remove and free all skbs until ns == nr */
995 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +0100996 n = nfc_llcp_ns(s);
997
Samuel Ortiz84457962012-05-30 17:48:29 +0200998 skb_unlink(s, &llcp_sock->tx_pending_queue);
999 kfree_skb(s);
1000
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +01001001 if (n == nr)
Samuel Ortiz84457962012-05-30 17:48:29 +02001002 break;
1003 }
1004
1005 /* Re-queue the remaining skbs for transmission */
1006 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
1007 s, tmp) {
1008 skb_unlink(s, &llcp_sock->tx_pending_queue);
1009 skb_queue_head(&local->tx_queue, s);
1010 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001011 }
1012
Samuel Ortiz53aef922012-03-05 01:03:36 +01001013 if (ptype == LLCP_PDU_RR)
1014 llcp_sock->remote_ready = true;
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001015 else if (ptype == LLCP_PDU_RNR)
Samuel Ortiz53aef922012-03-05 01:03:36 +01001016 llcp_sock->remote_ready = false;
1017
Samuel Ortiz56af25682012-05-30 18:06:11 +02001018 if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
Samuel Ortizd094afa2012-03-05 01:03:42 +01001019 nfc_llcp_send_rr(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001020
1021 release_sock(sk);
1022 nfc_llcp_sock_put(llcp_sock);
1023}
1024
1025static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001026 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001027{
1028 struct nfc_llcp_sock *llcp_sock;
1029 struct sock *sk;
1030 u8 dsap, ssap;
1031
1032 dsap = nfc_llcp_dsap(skb);
1033 ssap = nfc_llcp_ssap(skb);
1034
1035 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1036 if (llcp_sock == NULL) {
1037 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1038 return;
1039 }
1040
1041 sk = &llcp_sock->sk;
1042 lock_sock(sk);
Samuel Ortizf31652a2012-10-26 18:20:10 +02001043
1044 nfc_llcp_socket_purge(llcp_sock);
1045
Samuel Ortizd6469602011-12-14 16:43:12 +01001046 if (sk->sk_state == LLCP_CLOSED) {
1047 release_sock(sk);
1048 nfc_llcp_sock_put(llcp_sock);
1049 }
1050
Samuel Ortizd6469602011-12-14 16:43:12 +01001051 if (sk->sk_state == LLCP_CONNECTED) {
1052 nfc_put_device(local->dev);
1053 sk->sk_state = LLCP_CLOSED;
1054 sk->sk_state_change(sk);
1055 }
1056
1057 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
1058
1059 release_sock(sk);
1060 nfc_llcp_sock_put(llcp_sock);
1061}
1062
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001063static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001064{
1065 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizff353d82012-05-07 12:31:19 +02001066 struct sock *sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001067 u8 dsap, ssap;
1068
Samuel Ortizd6469602011-12-14 16:43:12 +01001069 dsap = nfc_llcp_dsap(skb);
1070 ssap = nfc_llcp_ssap(skb);
1071
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001072 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
Samuel Ortizd6469602011-12-14 16:43:12 +01001073 if (llcp_sock == NULL) {
1074 pr_err("Invalid CC\n");
1075 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1076
1077 return;
1078 }
1079
Samuel Ortizff353d82012-05-07 12:31:19 +02001080 sk = &llcp_sock->sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001081
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001082 /* Unlink from connecting and link to the client array */
1083 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
1084 nfc_llcp_sock_link(&local->sockets, sk);
1085 llcp_sock->dsap = ssap;
1086
Samuel Ortiz7a06e582012-05-07 22:03:34 +02001087 nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
1088 skb->len - LLCP_HEADER_SIZE);
Samuel Ortizd6469602011-12-14 16:43:12 +01001089
Samuel Ortizff353d82012-05-07 12:31:19 +02001090 sk->sk_state = LLCP_CONNECTED;
1091 sk->sk_state_change(sk);
1092
Samuel Ortizd6469602011-12-14 16:43:12 +01001093 nfc_llcp_sock_put(llcp_sock);
1094}
1095
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001096static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1097{
1098 struct nfc_llcp_sock *llcp_sock;
1099 struct sock *sk;
1100 u8 dsap, ssap, reason;
1101
1102 dsap = nfc_llcp_dsap(skb);
1103 ssap = nfc_llcp_ssap(skb);
1104 reason = skb->data[2];
1105
1106 pr_debug("%d %d reason %d\n", ssap, dsap, reason);
1107
1108 switch (reason) {
1109 case LLCP_DM_NOBOUND:
1110 case LLCP_DM_REJ:
1111 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1112 break;
1113
1114 default:
1115 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1116 break;
1117 }
1118
1119 if (llcp_sock == NULL) {
Samuel Ortiza8df0f32012-10-16 21:15:59 +02001120 pr_debug("Already closed\n");
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001121 return;
1122 }
1123
1124 sk = &llcp_sock->sk;
1125
1126 sk->sk_err = ENXIO;
1127 sk->sk_state = LLCP_CLOSED;
1128 sk->sk_state_change(sk);
1129
1130 nfc_llcp_sock_put(llcp_sock);
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001131}
1132
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001133static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1134 struct sk_buff *skb)
1135{
1136 struct nfc_llcp_sock *llcp_sock;
1137 u8 dsap, ssap, *tlv, type, length, tid, sap;
1138 u16 tlv_len, offset;
1139 char *service_name;
1140 size_t service_name_len;
1141
1142 dsap = nfc_llcp_dsap(skb);
1143 ssap = nfc_llcp_ssap(skb);
1144
1145 pr_debug("%d %d\n", dsap, ssap);
1146
1147 if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1148 pr_err("Wrong SNL SAP\n");
1149 return;
1150 }
1151
1152 tlv = &skb->data[LLCP_HEADER_SIZE];
1153 tlv_len = skb->len - LLCP_HEADER_SIZE;
1154 offset = 0;
1155
Szymon Janc0f450772012-10-17 15:23:39 +02001156 while (offset < tlv_len) {
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001157 type = tlv[0];
1158 length = tlv[1];
1159
1160 switch (type) {
1161 case LLCP_TLV_SDREQ:
1162 tid = tlv[2];
1163 service_name = (char *) &tlv[3];
1164 service_name_len = length - 1;
1165
Samuel Ortize6904082012-10-15 14:28:13 +02001166 pr_debug("Looking for %.16s\n", service_name);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001167
1168 if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1169 !strncmp(service_name, "urn:nfc:sn:sdp",
1170 service_name_len)) {
1171 sap = 1;
Samuel Ortize6904082012-10-15 14:28:13 +02001172 goto send_snl;
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001173 }
1174
Samuel Ortize6904082012-10-15 14:28:13 +02001175 llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
1176 service_name_len);
1177 if (!llcp_sock) {
1178 sap = 0;
1179 goto send_snl;
1180 }
1181
1182 /*
1183 * We found a socket but its ssap has not been reserved
1184 * yet. We need to assign it for good and send a reply.
1185 * The ssap will be freed when the socket is closed.
1186 */
1187 if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1188 atomic_t *client_count;
1189
1190 sap = nfc_llcp_reserve_sdp_ssap(local);
1191
1192 pr_debug("Reserving %d\n", sap);
1193
1194 if (sap == LLCP_SAP_MAX) {
1195 sap = 0;
1196 goto send_snl;
1197 }
1198
1199 client_count =
1200 &local->local_sdp_cnt[sap -
1201 LLCP_WKS_NUM_SAP];
1202
1203 atomic_inc(client_count);
1204
1205 llcp_sock->ssap = sap;
1206 llcp_sock->reserved_ssap = sap;
1207 } else {
1208 sap = llcp_sock->ssap;
1209 }
1210
1211 pr_debug("%p %d\n", llcp_sock, sap);
1212
Szymon Janc0f450772012-10-17 15:23:39 +02001213send_snl:
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001214 nfc_llcp_send_snl(local, tid, sap);
1215 break;
1216
1217 default:
1218 pr_err("Invalid SNL tlv value 0x%x\n", type);
1219 break;
1220 }
1221
1222 offset += length + 2;
1223 tlv += length + 2;
1224 }
1225}
1226
Samuel Ortizd6469602011-12-14 16:43:12 +01001227static void nfc_llcp_rx_work(struct work_struct *work)
1228{
1229 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001230 rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001231 u8 dsap, ssap, ptype;
1232 struct sk_buff *skb;
1233
1234 skb = local->rx_pending;
1235 if (skb == NULL) {
1236 pr_debug("No pending SKB\n");
1237 return;
1238 }
1239
1240 ptype = nfc_llcp_ptype(skb);
1241 dsap = nfc_llcp_dsap(skb);
1242 ssap = nfc_llcp_ssap(skb);
1243
1244 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1245
Samuel Ortiz4be646e2012-04-10 19:43:13 +02001246 if (ptype != LLCP_PDU_SYMM)
1247 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
1248 16, 1, skb->data, skb->len, true);
1249
Thierry Escande2c2d45b2012-11-27 15:44:24 +01001250 __net_timestamp(skb);
1251
Thierry Escande44635232012-09-26 18:16:44 +02001252 nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX);
1253
Samuel Ortizd6469602011-12-14 16:43:12 +01001254 switch (ptype) {
1255 case LLCP_PDU_SYMM:
1256 pr_debug("SYMM\n");
1257 break;
1258
Samuel Ortiz968272b2012-10-15 16:14:37 +02001259 case LLCP_PDU_UI:
1260 pr_debug("UI\n");
1261 nfc_llcp_recv_ui(local, skb);
1262 break;
1263
Samuel Ortizd6469602011-12-14 16:43:12 +01001264 case LLCP_PDU_CONNECT:
1265 pr_debug("CONNECT\n");
1266 nfc_llcp_recv_connect(local, skb);
1267 break;
1268
1269 case LLCP_PDU_DISC:
1270 pr_debug("DISC\n");
1271 nfc_llcp_recv_disc(local, skb);
1272 break;
1273
1274 case LLCP_PDU_CC:
1275 pr_debug("CC\n");
1276 nfc_llcp_recv_cc(local, skb);
1277 break;
1278
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001279 case LLCP_PDU_DM:
1280 pr_debug("DM\n");
1281 nfc_llcp_recv_dm(local, skb);
1282 break;
1283
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001284 case LLCP_PDU_SNL:
1285 pr_debug("SNL\n");
1286 nfc_llcp_recv_snl(local, skb);
1287 break;
1288
Samuel Ortizd6469602011-12-14 16:43:12 +01001289 case LLCP_PDU_I:
1290 case LLCP_PDU_RR:
Samuel Ortiz53aef922012-03-05 01:03:36 +01001291 case LLCP_PDU_RNR:
Samuel Ortizd6469602011-12-14 16:43:12 +01001292 pr_debug("I frame\n");
1293 nfc_llcp_recv_hdlc(local, skb);
1294 break;
1295
1296 }
1297
Linus Torvalds916082b2012-10-02 16:01:31 -07001298 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001299 kfree_skb(local->rx_pending);
1300 local->rx_pending = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +01001301}
1302
1303void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1304{
1305 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1306
1307 pr_debug("Received an LLCP PDU\n");
1308 if (err < 0) {
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001309 pr_err("err %d\n", err);
Samuel Ortizd6469602011-12-14 16:43:12 +01001310 return;
1311 }
1312
1313 local->rx_pending = skb_get(skb);
1314 del_timer(&local->link_timer);
Linus Torvalds916082b2012-10-02 16:01:31 -07001315 schedule_work(&local->rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001316}
1317
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001318int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1319{
1320 struct nfc_llcp_local *local;
1321
1322 local = nfc_llcp_find_local(dev);
1323 if (local == NULL)
1324 return -ENODEV;
1325
1326 local->rx_pending = skb_get(skb);
1327 del_timer(&local->link_timer);
Linus Torvalds916082b2012-10-02 16:01:31 -07001328 schedule_work(&local->rx_work);
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001329
1330 return 0;
1331}
1332
Samuel Ortizd6469602011-12-14 16:43:12 +01001333void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1334{
1335 struct nfc_llcp_local *local;
1336
1337 local = nfc_llcp_find_local(dev);
1338 if (local == NULL)
1339 return;
1340
1341 /* Close and purge all existing sockets */
Samuel Ortiz4d22ea12012-06-22 14:40:34 +02001342 nfc_llcp_socket_release(local, true);
Samuel Ortizd6469602011-12-14 16:43:12 +01001343}
1344
1345void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1346 u8 comm_mode, u8 rf_mode)
1347{
1348 struct nfc_llcp_local *local;
1349
1350 pr_debug("rf mode %d\n", rf_mode);
1351
1352 local = nfc_llcp_find_local(dev);
1353 if (local == NULL)
1354 return;
1355
1356 local->target_idx = target_idx;
1357 local->comm_mode = comm_mode;
1358 local->rf_mode = rf_mode;
1359
1360 if (rf_mode == NFC_RF_INITIATOR) {
1361 pr_debug("Queueing Tx work\n");
1362
Linus Torvalds916082b2012-10-02 16:01:31 -07001363 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001364 } else {
1365 mod_timer(&local->link_timer,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001366 jiffies + msecs_to_jiffies(local->remote_lto));
Samuel Ortizd6469602011-12-14 16:43:12 +01001367 }
1368}
1369
1370int nfc_llcp_register_device(struct nfc_dev *ndev)
1371{
Samuel Ortizd6469602011-12-14 16:43:12 +01001372 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +01001373
1374 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
1375 if (local == NULL)
1376 return -ENOMEM;
1377
1378 local->dev = ndev;
1379 INIT_LIST_HEAD(&local->list);
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001380 kref_init(&local->ref);
Samuel Ortizd6469602011-12-14 16:43:12 +01001381 mutex_init(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001382 init_timer(&local->link_timer);
1383 local->link_timer.data = (unsigned long) local;
1384 local->link_timer.function = nfc_llcp_symm_timer;
1385
1386 skb_queue_head_init(&local->tx_queue);
1387 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001388
1389 local->rx_pending = NULL;
1390 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001391
1392 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001393
Szymon Jancfe235b52012-09-25 12:42:50 +02001394 rwlock_init(&local->sockets.lock);
1395 rwlock_init(&local->connecting_sockets.lock);
Thierry Escande44635232012-09-26 18:16:44 +02001396 rwlock_init(&local->raw_sockets.lock);
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001397
Thierry Escande52feb442012-10-17 14:43:39 +02001398 local->lto = 150; /* 1500 ms */
1399 local->rw = LLCP_MAX_RW;
1400 local->miux = cpu_to_be16(LLCP_MAX_MIUX);
1401
Samuel Ortizd6469602011-12-14 16:43:12 +01001402 nfc_llcp_build_gb(local);
1403
1404 local->remote_miu = LLCP_DEFAULT_MIU;
1405 local->remote_lto = LLCP_DEFAULT_LTO;
Samuel Ortizd6469602011-12-14 16:43:12 +01001406
Thierry Escande16a78e92012-10-12 15:25:43 +02001407 list_add(&local->list, &llcp_devices);
Samuel Ortizd6469602011-12-14 16:43:12 +01001408
1409 return 0;
Samuel Ortizd6469602011-12-14 16:43:12 +01001410}
1411
1412void nfc_llcp_unregister_device(struct nfc_dev *dev)
1413{
1414 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1415
1416 if (local == NULL) {
1417 pr_debug("No such device\n");
1418 return;
1419 }
1420
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001421 nfc_llcp_local_put(local);
Samuel Ortizd6469602011-12-14 16:43:12 +01001422}
1423
1424int __init nfc_llcp_init(void)
1425{
1426 INIT_LIST_HEAD(&llcp_devices);
1427
1428 return nfc_llcp_sock_init();
1429}
1430
1431void nfc_llcp_exit(void)
1432{
1433 nfc_llcp_sock_exit();
1434}