blob: 92988aa620dcb04d73ae45eb573249bddac0a6cf [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
34static void nfc_llcp_socket_release(struct nfc_llcp_local *local)
35{
36 struct nfc_llcp_sock *parent, *s, *n;
37 struct sock *sk, *parent_sk;
38 int i;
39
Samuel Ortizd6469602011-12-14 16:43:12 +010040 mutex_lock(&local->socket_lock);
41
42 for (i = 0; i < LLCP_MAX_SAP; i++) {
43 parent = local->sockets[i];
44 if (parent == NULL)
45 continue;
46
47 /* Release all child sockets */
48 list_for_each_entry_safe(s, n, &parent->list, list) {
Samuel Ortiz40c75f82012-03-05 01:03:51 +010049 list_del_init(&s->list);
Samuel Ortizd6469602011-12-14 16:43:12 +010050 sk = &s->sk;
51
52 lock_sock(sk);
53
54 if (sk->sk_state == LLCP_CONNECTED)
55 nfc_put_device(s->dev);
56
57 sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +010058
59 release_sock(sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +010060
61 sock_orphan(sk);
62
63 s->local = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +010064 }
65
66 parent_sk = &parent->sk;
67
68 lock_sock(parent_sk);
69
70 if (parent_sk->sk_state == LLCP_LISTEN) {
71 struct nfc_llcp_sock *lsk, *n;
72 struct sock *accept_sk;
73
74 list_for_each_entry_safe(lsk, n, &parent->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +010075 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +010076 accept_sk = &lsk->sk;
77 lock_sock(accept_sk);
78
79 nfc_llcp_accept_unlink(accept_sk);
80
81 accept_sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +010082
83 release_sock(accept_sk);
84
85 sock_orphan(accept_sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +010086
87 lsk->local = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +010088 }
89 }
90
91 if (parent_sk->sk_state == LLCP_CONNECTED)
92 nfc_put_device(parent->dev);
93
94 parent_sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +010095
96 release_sock(parent_sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +010097
98 sock_orphan(parent_sk);
99
100 parent->local = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100101 }
102
103 mutex_unlock(&local->socket_lock);
104}
105
Samuel Ortizb9a76f12012-03-05 01:03:40 +0100106static void nfc_llcp_clear_sdp(struct nfc_llcp_local *local)
107{
108 mutex_lock(&local->sdp_lock);
109
110 local->local_wks = 0;
111 local->local_sdp = 0;
112 local->local_sap = 0;
113
114 mutex_unlock(&local->sdp_lock);
115}
116
Samuel Ortizd6469602011-12-14 16:43:12 +0100117static void nfc_llcp_timeout_work(struct work_struct *work)
118{
119 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100120 timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100121
122 nfc_dep_link_down(local->dev);
123}
124
125static void nfc_llcp_symm_timer(unsigned long data)
126{
127 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
128
129 pr_err("SYMM timeout\n");
130
131 queue_work(local->timeout_wq, &local->timeout_work);
132}
133
134struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
135{
136 struct nfc_llcp_local *local, *n;
137
138 list_for_each_entry_safe(local, n, &llcp_devices, list)
139 if (local->dev == dev)
140 return local;
141
142 pr_debug("No device found\n");
143
144 return NULL;
145}
146
147static char *wks[] = {
148 NULL,
149 NULL, /* SDP */
150 "urn:nfc:sn:ip",
151 "urn:nfc:sn:obex",
152 "urn:nfc:sn:snep",
153};
154
155static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
156{
157 int sap, num_wks;
158
159 pr_debug("%s\n", service_name);
160
161 if (service_name == NULL)
162 return -EINVAL;
163
164 num_wks = ARRAY_SIZE(wks);
165
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100166 for (sap = 0; sap < num_wks; sap++) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100167 if (wks[sap] == NULL)
168 continue;
169
170 if (strncmp(wks[sap], service_name, service_name_len) == 0)
171 return sap;
172 }
173
174 return -EINVAL;
175}
176
177u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100178 struct nfc_llcp_sock *sock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100179{
180 mutex_lock(&local->sdp_lock);
181
182 if (sock->service_name != NULL && sock->service_name_len > 0) {
183 int ssap = nfc_llcp_wks_sap(sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100184 sock->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100185
186 if (ssap > 0) {
187 pr_debug("WKS %d\n", ssap);
188
189 /* This is a WKS, let's check if it's free */
190 if (local->local_wks & BIT(ssap)) {
191 mutex_unlock(&local->sdp_lock);
192
193 return LLCP_SAP_MAX;
194 }
195
Samuel Ortiz1762c172012-03-05 01:03:38 +0100196 set_bit(ssap, &local->local_wks);
Samuel Ortizd6469602011-12-14 16:43:12 +0100197 mutex_unlock(&local->sdp_lock);
198
199 return ssap;
200 }
201
202 /*
203 * This is not a well known service,
204 * we should try to find a local SDP free spot
205 */
206 ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
207 if (ssap == LLCP_SDP_NUM_SAP) {
208 mutex_unlock(&local->sdp_lock);
209
210 return LLCP_SAP_MAX;
211 }
212
213 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
214
Samuel Ortiz1762c172012-03-05 01:03:38 +0100215 set_bit(ssap, &local->local_sdp);
Samuel Ortizd6469602011-12-14 16:43:12 +0100216 mutex_unlock(&local->sdp_lock);
217
218 return LLCP_WKS_NUM_SAP + ssap;
219
220 } else if (sock->ssap != 0) {
221 if (sock->ssap < LLCP_WKS_NUM_SAP) {
Samuel Ortiz1762c172012-03-05 01:03:38 +0100222 if (!test_bit(sock->ssap, &local->local_wks)) {
223 set_bit(sock->ssap, &local->local_wks);
Samuel Ortizd6469602011-12-14 16:43:12 +0100224 mutex_unlock(&local->sdp_lock);
225
226 return sock->ssap;
227 }
228
229 } else if (sock->ssap < LLCP_SDP_NUM_SAP) {
Samuel Ortiz1762c172012-03-05 01:03:38 +0100230 if (!test_bit(sock->ssap - LLCP_WKS_NUM_SAP,
231 &local->local_sdp)) {
232 set_bit(sock->ssap - LLCP_WKS_NUM_SAP,
233 &local->local_sdp);
Samuel Ortizd6469602011-12-14 16:43:12 +0100234 mutex_unlock(&local->sdp_lock);
235
236 return sock->ssap;
237 }
238 }
239 }
240
241 mutex_unlock(&local->sdp_lock);
242
243 return LLCP_SAP_MAX;
244}
245
246u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
247{
248 u8 local_ssap;
249
250 mutex_lock(&local->sdp_lock);
251
252 local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
253 if (local_ssap == LLCP_LOCAL_NUM_SAP) {
254 mutex_unlock(&local->sdp_lock);
255 return LLCP_SAP_MAX;
256 }
257
Samuel Ortiz1762c172012-03-05 01:03:38 +0100258 set_bit(local_ssap, &local->local_sap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100259
260 mutex_unlock(&local->sdp_lock);
261
262 return local_ssap + LLCP_LOCAL_SAP_OFFSET;
263}
264
265void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
266{
267 u8 local_ssap;
268 unsigned long *sdp;
269
270 if (ssap < LLCP_WKS_NUM_SAP) {
271 local_ssap = ssap;
272 sdp = &local->local_wks;
273 } else if (ssap < LLCP_LOCAL_NUM_SAP) {
274 local_ssap = ssap - LLCP_WKS_NUM_SAP;
275 sdp = &local->local_sdp;
276 } else if (ssap < LLCP_MAX_SAP) {
277 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
278 sdp = &local->local_sap;
279 } else {
280 return;
281 }
282
283 mutex_lock(&local->sdp_lock);
284
Samuel Ortiz1762c172012-03-05 01:03:38 +0100285 clear_bit(local_ssap, sdp);
Samuel Ortizd6469602011-12-14 16:43:12 +0100286
287 mutex_unlock(&local->sdp_lock);
288}
289
Samuel Ortiz47807d32012-03-05 01:03:50 +0100290u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
Samuel Ortizd6469602011-12-14 16:43:12 +0100291{
292 struct nfc_llcp_local *local;
293
294 local = nfc_llcp_find_local(dev);
295 if (local == NULL) {
296 *general_bytes_len = 0;
297 return NULL;
298 }
299
300 *general_bytes_len = local->gb_len;
301
302 return local->gb;
303}
304
305static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
306{
307 u8 *gb_cur, *version_tlv, version, version_length;
308 u8 *lto_tlv, lto, lto_length;
309 u8 *wks_tlv, wks_length;
Samuel Ortiz56d58762012-04-10 19:43:19 +0200310 u8 *miux_tlv, miux_length;
311 __be16 miux;
Samuel Ortizd6469602011-12-14 16:43:12 +0100312 u8 gb_len = 0;
313
314 version = LLCP_VERSION_11;
315 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100316 1, &version_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100317 gb_len += version_length;
318
319 /* 1500 ms */
320 lto = 150;
Samuel Ortiz91b0ade2012-04-10 19:43:20 +0200321 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &lto, 1, &lto_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100322 gb_len += lto_length;
323
324 pr_debug("Local wks 0x%lx\n", local->local_wks);
325 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100326 &wks_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100327 gb_len += wks_length;
328
Samuel Ortiz56d58762012-04-10 19:43:19 +0200329 miux = cpu_to_be16(LLCP_MAX_MIUX);
330 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
331 &miux_length);
332 gb_len += miux_length;
333
Samuel Ortizd6469602011-12-14 16:43:12 +0100334 gb_len += ARRAY_SIZE(llcp_magic);
335
336 if (gb_len > NFC_MAX_GT_LEN) {
337 kfree(version_tlv);
338 return -EINVAL;
339 }
340
341 gb_cur = local->gb;
342
343 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
344 gb_cur += ARRAY_SIZE(llcp_magic);
345
346 memcpy(gb_cur, version_tlv, version_length);
347 gb_cur += version_length;
348
349 memcpy(gb_cur, lto_tlv, lto_length);
350 gb_cur += lto_length;
351
352 memcpy(gb_cur, wks_tlv, wks_length);
353 gb_cur += wks_length;
354
Samuel Ortiz56d58762012-04-10 19:43:19 +0200355 memcpy(gb_cur, miux_tlv, miux_length);
356 gb_cur += miux_length;
357
Samuel Ortizd6469602011-12-14 16:43:12 +0100358 kfree(version_tlv);
359 kfree(lto_tlv);
360
361 local->gb_len = gb_len;
362
363 return 0;
364}
365
366int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
367{
368 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
369
370 if (local == NULL) {
371 pr_err("No LLCP device\n");
372 return -ENODEV;
373 }
374
375 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
376 memcpy(local->remote_gb, gb, gb_len);
377 local->remote_gb_len = gb_len;
378
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100379 if (local->remote_gb == NULL || local->remote_gb_len == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100380 return -ENODEV;
381
382 if (memcmp(local->remote_gb, llcp_magic, 3)) {
383 pr_err("MAC does not support LLCP\n");
384 return -EINVAL;
385 }
386
387 return nfc_llcp_parse_tlv(local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100388 &local->remote_gb[3],
389 local->remote_gb_len - 3);
Samuel Ortizd6469602011-12-14 16:43:12 +0100390}
391
392static void nfc_llcp_tx_work(struct work_struct *work)
393{
394 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100395 tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100396 struct sk_buff *skb;
397
398 skb = skb_dequeue(&local->tx_queue);
399 if (skb != NULL) {
400 pr_debug("Sending pending skb\n");
Samuel Ortiz4be646e2012-04-10 19:43:13 +0200401 print_hex_dump(KERN_DEBUG, "LLCP Tx: ", DUMP_PREFIX_OFFSET,
402 16, 1, skb->data, skb->len, true);
403
Samuel Ortizd6469602011-12-14 16:43:12 +0100404 nfc_data_exchange(local->dev, local->target_idx,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100405 skb, nfc_llcp_recv, local);
Samuel Ortizd6469602011-12-14 16:43:12 +0100406 } else {
407 nfc_llcp_send_symm(local->dev);
408 }
409
410 mod_timer(&local->link_timer,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100411 jiffies + msecs_to_jiffies(local->remote_lto));
Samuel Ortizd6469602011-12-14 16:43:12 +0100412}
413
414static u8 nfc_llcp_dsap(struct sk_buff *pdu)
415{
416 return (pdu->data[0] & 0xfc) >> 2;
417}
418
419static u8 nfc_llcp_ptype(struct sk_buff *pdu)
420{
421 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
422}
423
424static u8 nfc_llcp_ssap(struct sk_buff *pdu)
425{
426 return pdu->data[1] & 0x3f;
427}
428
429static u8 nfc_llcp_ns(struct sk_buff *pdu)
430{
431 return pdu->data[2] >> 4;
432}
433
434static u8 nfc_llcp_nr(struct sk_buff *pdu)
435{
436 return pdu->data[2] & 0xf;
437}
438
439static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
440{
Samuel Ortiz279cf172012-04-10 19:43:14 +0200441 pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
Samuel Ortizd6469602011-12-14 16:43:12 +0100442 sock->send_n = (sock->send_n + 1) % 16;
443 sock->recv_ack_n = (sock->recv_n - 1) % 16;
444}
445
446static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100447 u8 ssap, u8 dsap)
Samuel Ortizd6469602011-12-14 16:43:12 +0100448{
449 struct nfc_llcp_sock *sock, *llcp_sock, *n;
450
451 if (ssap == 0 && dsap == 0)
452 return NULL;
453
454 mutex_lock(&local->socket_lock);
455 sock = local->sockets[ssap];
456 if (sock == NULL) {
457 mutex_unlock(&local->socket_lock);
458 return NULL;
459 }
460
461 pr_debug("root dsap %d (%d)\n", sock->dsap, dsap);
462
463 if (sock->dsap == dsap) {
464 sock_hold(&sock->sk);
465 mutex_unlock(&local->socket_lock);
466 return sock;
467 }
468
469 list_for_each_entry_safe(llcp_sock, n, &sock->list, list) {
470 pr_debug("llcp_sock %p sk %p dsap %d\n", llcp_sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100471 &llcp_sock->sk, llcp_sock->dsap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100472 if (llcp_sock->dsap == dsap) {
473 sock_hold(&llcp_sock->sk);
474 mutex_unlock(&local->socket_lock);
475 return llcp_sock;
476 }
477 }
478
479 pr_err("Could not find socket for %d %d\n", ssap, dsap);
480
481 mutex_unlock(&local->socket_lock);
482
483 return NULL;
484}
485
486static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
487{
488 sock_put(&sock->sk);
489}
490
491static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
492{
493 u8 *tlv = &skb->data[2], type, length;
494 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
495
496 while (offset < tlv_array_len) {
497 type = tlv[0];
498 length = tlv[1];
499
500 pr_debug("type 0x%x length %d\n", type, length);
501
502 if (type == LLCP_TLV_SN) {
503 *sn_len = length;
504 return &tlv[2];
505 }
506
507 offset += length + 2;
508 tlv += length + 2;
509 }
510
511 return NULL;
512}
513
514static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100515 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100516{
517 struct sock *new_sk, *parent;
518 struct nfc_llcp_sock *sock, *new_sock;
519 u8 dsap, ssap, bound_sap, reason;
520
521 dsap = nfc_llcp_dsap(skb);
522 ssap = nfc_llcp_ssap(skb);
523
524 pr_debug("%d %d\n", dsap, ssap);
525
526 nfc_llcp_parse_tlv(local, &skb->data[LLCP_HEADER_SIZE],
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100527 skb->len - LLCP_HEADER_SIZE);
Samuel Ortizd6469602011-12-14 16:43:12 +0100528
529 if (dsap != LLCP_SAP_SDP) {
530 bound_sap = dsap;
531
532 mutex_lock(&local->socket_lock);
533 sock = local->sockets[dsap];
534 if (sock == NULL) {
535 mutex_unlock(&local->socket_lock);
536 reason = LLCP_DM_NOBOUND;
537 goto fail;
538 }
539
540 sock_hold(&sock->sk);
541 mutex_unlock(&local->socket_lock);
542
543 lock_sock(&sock->sk);
544
545 if (sock->dsap == LLCP_SAP_SDP &&
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100546 sock->sk.sk_state == LLCP_LISTEN)
Samuel Ortizd6469602011-12-14 16:43:12 +0100547 goto enqueue;
548 } else {
549 u8 *sn;
550 size_t sn_len;
551
552 sn = nfc_llcp_connect_sn(skb, &sn_len);
553 if (sn == NULL) {
554 reason = LLCP_DM_NOBOUND;
555 goto fail;
556 }
557
558 pr_debug("Service name length %zu\n", sn_len);
559
560 mutex_lock(&local->socket_lock);
561 for (bound_sap = 0; bound_sap < LLCP_LOCAL_SAP_OFFSET;
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100562 bound_sap++) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100563 sock = local->sockets[bound_sap];
564 if (sock == NULL)
565 continue;
566
567 if (sock->service_name == NULL ||
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100568 sock->service_name_len == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100569 continue;
570
571 if (sock->service_name_len != sn_len)
572 continue;
573
574 if (sock->dsap == LLCP_SAP_SDP &&
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100575 sock->sk.sk_state == LLCP_LISTEN &&
576 !memcmp(sn, sock->service_name, sn_len)) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100577 pr_debug("Found service name at SAP %d\n",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100578 bound_sap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100579 sock_hold(&sock->sk);
580 mutex_unlock(&local->socket_lock);
581
582 lock_sock(&sock->sk);
583
584 goto enqueue;
585 }
586 }
Dan Carpenter341ee432011-12-16 23:25:29 +0300587 mutex_unlock(&local->socket_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100588 }
589
Samuel Ortizd6469602011-12-14 16:43:12 +0100590 reason = LLCP_DM_NOBOUND;
591 goto fail;
592
593enqueue:
594 parent = &sock->sk;
595
596 if (sk_acceptq_is_full(parent)) {
597 reason = LLCP_DM_REJ;
598 release_sock(&sock->sk);
599 sock_put(&sock->sk);
600 goto fail;
601 }
602
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100603 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC);
Samuel Ortizd6469602011-12-14 16:43:12 +0100604 if (new_sk == NULL) {
605 reason = LLCP_DM_REJ;
606 release_sock(&sock->sk);
607 sock_put(&sock->sk);
608 goto fail;
609 }
610
611 new_sock = nfc_llcp_sock(new_sk);
612 new_sock->dev = local->dev;
613 new_sock->local = local;
614 new_sock->nfc_protocol = sock->nfc_protocol;
615 new_sock->ssap = bound_sap;
616 new_sock->dsap = ssap;
617 new_sock->parent = parent;
618
619 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
620
621 list_add_tail(&new_sock->list, &sock->list);
622
623 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
624
625 nfc_get_device(local->dev->idx);
626
627 new_sk->sk_state = LLCP_CONNECTED;
628
629 /* Wake the listening processes */
630 parent->sk_data_ready(parent, 0);
631
632 /* Send CC */
633 nfc_llcp_send_cc(new_sock);
634
635 release_sock(&sock->sk);
636 sock_put(&sock->sk);
637
638 return;
639
640fail:
641 /* Send DM */
642 nfc_llcp_send_dm(local, dsap, ssap, reason);
643
644 return;
645
646}
647
Samuel Ortizd094afa2012-03-05 01:03:42 +0100648int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100649{
Samuel Ortizd094afa2012-03-05 01:03:42 +0100650 int nr_frames = 0;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100651 struct nfc_llcp_local *local = sock->local;
652
653 pr_debug("Remote ready %d tx queue len %d remote rw %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100654 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
655 local->remote_rw);
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100656
657 /* Try to queue some I frames for transmission */
658 while (sock->remote_ready &&
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100659 skb_queue_len(&sock->tx_pending_queue) < local->remote_rw) {
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100660 struct sk_buff *pdu, *pending_pdu;
661
662 pdu = skb_dequeue(&sock->tx_queue);
663 if (pdu == NULL)
664 break;
665
666 /* Update N(S)/N(R) */
667 nfc_llcp_set_nrns(sock, pdu);
668
669 pending_pdu = skb_clone(pdu, GFP_KERNEL);
670
671 skb_queue_tail(&local->tx_queue, pdu);
672 skb_queue_tail(&sock->tx_pending_queue, pending_pdu);
Samuel Ortizd094afa2012-03-05 01:03:42 +0100673 nr_frames++;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100674 }
Samuel Ortizd094afa2012-03-05 01:03:42 +0100675
676 return nr_frames;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100677}
678
Samuel Ortizd6469602011-12-14 16:43:12 +0100679static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100680 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100681{
682 struct nfc_llcp_sock *llcp_sock;
683 struct sock *sk;
684 u8 dsap, ssap, ptype, ns, nr;
685
686 ptype = nfc_llcp_ptype(skb);
687 dsap = nfc_llcp_dsap(skb);
688 ssap = nfc_llcp_ssap(skb);
689 ns = nfc_llcp_ns(skb);
690 nr = nfc_llcp_nr(skb);
691
692 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
693
694 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
695 if (llcp_sock == NULL) {
696 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
697 return;
698 }
699
700 sk = &llcp_sock->sk;
701 lock_sock(sk);
702 if (sk->sk_state == LLCP_CLOSED) {
703 release_sock(sk);
704 nfc_llcp_sock_put(llcp_sock);
705 }
706
Samuel Ortizd6469602011-12-14 16:43:12 +0100707 /* Pass the payload upstream */
708 if (ptype == LLCP_PDU_I) {
709 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
710
Samuel Ortiz53aef922012-03-05 01:03:36 +0100711 if (ns == llcp_sock->recv_n)
712 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
713 else
714 pr_err("Received out of sequence I PDU\n");
715
Samuel Ortizd6469602011-12-14 16:43:12 +0100716 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
717 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
718 pr_err("receive queue is full\n");
719 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
720 }
721 }
722
723 /* Remove skbs from the pending queue */
724 if (llcp_sock->send_ack_n != nr) {
725 struct sk_buff *s, *tmp;
726
727 llcp_sock->send_ack_n = nr;
728
729 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp)
730 if (nfc_llcp_ns(s) <= nr) {
731 skb_unlink(s, &llcp_sock->tx_pending_queue);
732 kfree_skb(s);
733 }
734 }
735
Samuel Ortiz53aef922012-03-05 01:03:36 +0100736 if (ptype == LLCP_PDU_RR)
737 llcp_sock->remote_ready = true;
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100738 else if (ptype == LLCP_PDU_RNR)
Samuel Ortiz53aef922012-03-05 01:03:36 +0100739 llcp_sock->remote_ready = false;
740
Samuel Ortizd094afa2012-03-05 01:03:42 +0100741 if (nfc_llcp_queue_i_frames(llcp_sock) == 0)
742 nfc_llcp_send_rr(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100743
744 release_sock(sk);
745 nfc_llcp_sock_put(llcp_sock);
746}
747
748static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100749 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100750{
751 struct nfc_llcp_sock *llcp_sock;
752 struct sock *sk;
753 u8 dsap, ssap;
754
755 dsap = nfc_llcp_dsap(skb);
756 ssap = nfc_llcp_ssap(skb);
757
758 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
759 if (llcp_sock == NULL) {
760 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
761 return;
762 }
763
764 sk = &llcp_sock->sk;
765 lock_sock(sk);
766 if (sk->sk_state == LLCP_CLOSED) {
767 release_sock(sk);
768 nfc_llcp_sock_put(llcp_sock);
769 }
770
Samuel Ortizd6469602011-12-14 16:43:12 +0100771 if (sk->sk_state == LLCP_CONNECTED) {
772 nfc_put_device(local->dev);
773 sk->sk_state = LLCP_CLOSED;
774 sk->sk_state_change(sk);
775 }
776
777 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
778
779 release_sock(sk);
780 nfc_llcp_sock_put(llcp_sock);
781}
782
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100783static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100784{
785 struct nfc_llcp_sock *llcp_sock;
786 u8 dsap, ssap;
787
Samuel Ortizd6469602011-12-14 16:43:12 +0100788 dsap = nfc_llcp_dsap(skb);
789 ssap = nfc_llcp_ssap(skb);
790
791 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
792
793 if (llcp_sock == NULL)
794 llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
795
796 if (llcp_sock == NULL) {
797 pr_err("Invalid CC\n");
798 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
799
800 return;
801 }
802
803 llcp_sock->dsap = ssap;
804
805 nfc_llcp_parse_tlv(local, &skb->data[LLCP_HEADER_SIZE],
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100806 skb->len - LLCP_HEADER_SIZE);
Samuel Ortizd6469602011-12-14 16:43:12 +0100807
808 nfc_llcp_sock_put(llcp_sock);
809}
810
811static void nfc_llcp_rx_work(struct work_struct *work)
812{
813 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100814 rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100815 u8 dsap, ssap, ptype;
816 struct sk_buff *skb;
817
818 skb = local->rx_pending;
819 if (skb == NULL) {
820 pr_debug("No pending SKB\n");
821 return;
822 }
823
824 ptype = nfc_llcp_ptype(skb);
825 dsap = nfc_llcp_dsap(skb);
826 ssap = nfc_llcp_ssap(skb);
827
828 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
829
Samuel Ortiz4be646e2012-04-10 19:43:13 +0200830 if (ptype != LLCP_PDU_SYMM)
831 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
832 16, 1, skb->data, skb->len, true);
833
Samuel Ortizd6469602011-12-14 16:43:12 +0100834 switch (ptype) {
835 case LLCP_PDU_SYMM:
836 pr_debug("SYMM\n");
837 break;
838
839 case LLCP_PDU_CONNECT:
840 pr_debug("CONNECT\n");
841 nfc_llcp_recv_connect(local, skb);
842 break;
843
844 case LLCP_PDU_DISC:
845 pr_debug("DISC\n");
846 nfc_llcp_recv_disc(local, skb);
847 break;
848
849 case LLCP_PDU_CC:
850 pr_debug("CC\n");
851 nfc_llcp_recv_cc(local, skb);
852 break;
853
854 case LLCP_PDU_I:
855 case LLCP_PDU_RR:
Samuel Ortiz53aef922012-03-05 01:03:36 +0100856 case LLCP_PDU_RNR:
Samuel Ortizd6469602011-12-14 16:43:12 +0100857 pr_debug("I frame\n");
858 nfc_llcp_recv_hdlc(local, skb);
859 break;
860
861 }
862
863 queue_work(local->tx_wq, &local->tx_work);
864 kfree_skb(local->rx_pending);
865 local->rx_pending = NULL;
866
867 return;
868}
869
870void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
871{
872 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
873
874 pr_debug("Received an LLCP PDU\n");
875 if (err < 0) {
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100876 pr_err("err %d\n", err);
Samuel Ortizd6469602011-12-14 16:43:12 +0100877 return;
878 }
879
880 local->rx_pending = skb_get(skb);
881 del_timer(&local->link_timer);
882 queue_work(local->rx_wq, &local->rx_work);
883
884 return;
885}
886
887void nfc_llcp_mac_is_down(struct nfc_dev *dev)
888{
889 struct nfc_llcp_local *local;
890
891 local = nfc_llcp_find_local(dev);
892 if (local == NULL)
893 return;
894
Samuel Ortizb9a76f12012-03-05 01:03:40 +0100895 nfc_llcp_clear_sdp(local);
896
Samuel Ortizd6469602011-12-14 16:43:12 +0100897 /* Close and purge all existing sockets */
898 nfc_llcp_socket_release(local);
899}
900
901void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
902 u8 comm_mode, u8 rf_mode)
903{
904 struct nfc_llcp_local *local;
905
906 pr_debug("rf mode %d\n", rf_mode);
907
908 local = nfc_llcp_find_local(dev);
909 if (local == NULL)
910 return;
911
912 local->target_idx = target_idx;
913 local->comm_mode = comm_mode;
914 local->rf_mode = rf_mode;
915
916 if (rf_mode == NFC_RF_INITIATOR) {
917 pr_debug("Queueing Tx work\n");
918
919 queue_work(local->tx_wq, &local->tx_work);
920 } else {
921 mod_timer(&local->link_timer,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100922 jiffies + msecs_to_jiffies(local->remote_lto));
Samuel Ortizd6469602011-12-14 16:43:12 +0100923 }
924}
925
926int nfc_llcp_register_device(struct nfc_dev *ndev)
927{
928 struct device *dev = &ndev->dev;
929 struct nfc_llcp_local *local;
930 char name[32];
931 int err;
932
933 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
934 if (local == NULL)
935 return -ENOMEM;
936
937 local->dev = ndev;
938 INIT_LIST_HEAD(&local->list);
939 mutex_init(&local->sdp_lock);
940 mutex_init(&local->socket_lock);
941 init_timer(&local->link_timer);
942 local->link_timer.data = (unsigned long) local;
943 local->link_timer.function = nfc_llcp_symm_timer;
944
945 skb_queue_head_init(&local->tx_queue);
946 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
947 snprintf(name, sizeof(name), "%s_llcp_tx_wq", dev_name(dev));
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100948 local->tx_wq =
949 alloc_workqueue(name,
950 WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM,
951 1);
Samuel Ortizd6469602011-12-14 16:43:12 +0100952 if (local->tx_wq == NULL) {
953 err = -ENOMEM;
954 goto err_local;
955 }
956
957 local->rx_pending = NULL;
958 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
959 snprintf(name, sizeof(name), "%s_llcp_rx_wq", dev_name(dev));
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100960 local->rx_wq =
961 alloc_workqueue(name,
962 WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM,
963 1);
Samuel Ortizd6469602011-12-14 16:43:12 +0100964 if (local->rx_wq == NULL) {
965 err = -ENOMEM;
966 goto err_tx_wq;
967 }
968
969 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
970 snprintf(name, sizeof(name), "%s_llcp_timeout_wq", dev_name(dev));
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100971 local->timeout_wq =
972 alloc_workqueue(name,
973 WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM,
974 1);
Samuel Ortizd6469602011-12-14 16:43:12 +0100975 if (local->timeout_wq == NULL) {
976 err = -ENOMEM;
977 goto err_rx_wq;
978 }
979
980 nfc_llcp_build_gb(local);
981
982 local->remote_miu = LLCP_DEFAULT_MIU;
983 local->remote_lto = LLCP_DEFAULT_LTO;
984 local->remote_rw = LLCP_DEFAULT_RW;
985
986 list_add(&llcp_devices, &local->list);
987
988 return 0;
989
990err_rx_wq:
991 destroy_workqueue(local->rx_wq);
992
993err_tx_wq:
994 destroy_workqueue(local->tx_wq);
995
996err_local:
997 kfree(local);
998
999 return 0;
1000}
1001
1002void nfc_llcp_unregister_device(struct nfc_dev *dev)
1003{
1004 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1005
1006 if (local == NULL) {
1007 pr_debug("No such device\n");
1008 return;
1009 }
1010
1011 list_del(&local->list);
1012 nfc_llcp_socket_release(local);
1013 del_timer_sync(&local->link_timer);
1014 skb_queue_purge(&local->tx_queue);
1015 destroy_workqueue(local->tx_wq);
1016 destroy_workqueue(local->rx_wq);
Dan Carpenter5b68a7c2011-12-16 23:26:00 +03001017 kfree_skb(local->rx_pending);
Samuel Ortizd6469602011-12-14 16:43:12 +01001018 kfree(local);
1019}
1020
1021int __init nfc_llcp_init(void)
1022{
1023 INIT_LIST_HEAD(&llcp_devices);
1024
1025 return nfc_llcp_sock_init();
1026}
1027
1028void nfc_llcp_exit(void)
1029{
1030 nfc_llcp_sock_exit();
1031}