blob: 85bc75c38dea67544a39d7e352b144f3a8f4af0e [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);
Samuel Ortizf31652a2012-10-26 18:20:10 +020057
58 if (local == NULL)
59 return;
60
61 /* Search for local pending SKBs that are related to this socket */
62 skb_queue_walk_safe(&local->tx_queue, s, tmp) {
63 if (s->sk != &sock->sk)
64 continue;
65
66 skb_unlink(s, &local->tx_queue);
67 kfree_skb(s);
68 }
69}
70
Samuel Ortiz4d22ea12012-06-22 14:40:34 +020071static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
Samuel Ortizd6469602011-12-14 16:43:12 +010072{
Samuel Ortiza69f32a2012-05-04 17:04:19 +020073 struct sock *sk;
74 struct hlist_node *node, *tmp;
75 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +010076
Samuel Ortizf31652a2012-10-26 18:20:10 +020077 skb_queue_purge(&local->tx_queue);
78
Samuel Ortiza69f32a2012-05-04 17:04:19 +020079 write_lock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +010080
Samuel Ortiza69f32a2012-05-04 17:04:19 +020081 sk_for_each_safe(sk, node, tmp, &local->sockets.head) {
82 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010083
Szymon Janc50b78b22012-09-26 14:22:10 +020084 bh_lock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010085
Samuel Ortizf31652a2012-10-26 18:20:10 +020086 nfc_llcp_socket_purge(llcp_sock);
87
Samuel Ortiza69f32a2012-05-04 17:04:19 +020088 if (sk->sk_state == LLCP_CONNECTED)
89 nfc_put_device(llcp_sock->dev);
Samuel Ortizd6469602011-12-14 16:43:12 +010090
Samuel Ortiza69f32a2012-05-04 17:04:19 +020091 if (sk->sk_state == LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +010092 struct nfc_llcp_sock *lsk, *n;
93 struct sock *accept_sk;
94
Szymon Janc0f450772012-10-17 15:23:39 +020095 list_for_each_entry_safe(lsk, n,
96 &llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +010097 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +010098 accept_sk = &lsk->sk;
Szymon Janc50b78b22012-09-26 14:22:10 +020099 bh_lock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100100
101 nfc_llcp_accept_unlink(accept_sk);
102
103 accept_sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +0100104
Szymon Janc50b78b22012-09-26 14:22:10 +0200105 bh_unlock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100106
107 sock_orphan(accept_sk);
108 }
Samuel Ortiz4d22ea12012-06-22 14:40:34 +0200109
110 if (listen == true) {
Szymon Janc50b78b22012-09-26 14:22:10 +0200111 bh_unlock_sock(sk);
Samuel Ortiz4d22ea12012-06-22 14:40:34 +0200112 continue;
113 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100114 }
115
Samuel Ortizc8512be2012-10-15 15:09:52 +0200116 /*
117 * If we have a connection less socket bound, we keep it alive
118 * if the device is still present.
119 */
120 if (sk->sk_state == LLCP_BOUND && sk->sk_type == SOCK_DGRAM &&
121 listen == true) {
122 bh_unlock_sock(sk);
123 continue;
124 }
125
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200126 sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +0100127
Szymon Janc50b78b22012-09-26 14:22:10 +0200128 bh_unlock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100129
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200130 sock_orphan(sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100131
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200132 sk_del_node_init(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100133 }
134
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200135 write_unlock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100136}
137
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200138struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
139{
140 kref_get(&local->ref);
141
142 return local;
143}
144
145static void local_release(struct kref *ref)
146{
147 struct nfc_llcp_local *local;
148
149 local = container_of(ref, struct nfc_llcp_local, ref);
150
151 list_del(&local->list);
Samuel Ortiz4d22ea12012-06-22 14:40:34 +0200152 nfc_llcp_socket_release(local, false);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200153 del_timer_sync(&local->link_timer);
154 skb_queue_purge(&local->tx_queue);
Tejun Heo474fee32012-08-22 16:22:16 -0700155 cancel_work_sync(&local->tx_work);
156 cancel_work_sync(&local->rx_work);
157 cancel_work_sync(&local->timeout_work);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200158 kfree_skb(local->rx_pending);
159 kfree(local);
160}
161
162int nfc_llcp_local_put(struct nfc_llcp_local *local)
163{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200164 if (local == NULL)
165 return 0;
166
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200167 return kref_put(&local->ref, local_release);
168}
169
Samuel Ortiz8f500202012-06-25 15:46:28 +0200170static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
171 u8 ssap, u8 dsap)
172{
173 struct sock *sk;
174 struct hlist_node *node;
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200175 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200176
177 pr_debug("ssap dsap %d %d\n", ssap, dsap);
178
179 if (ssap == 0 && dsap == 0)
180 return NULL;
181
182 read_lock(&local->sockets.lock);
183
184 llcp_sock = NULL;
185
186 sk_for_each(sk, node, &local->sockets.head) {
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200187 tmp_sock = nfc_llcp_sock(sk);
Samuel Ortiz8f500202012-06-25 15:46:28 +0200188
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200189 if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
190 llcp_sock = tmp_sock;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200191 break;
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200192 }
Samuel Ortiz8f500202012-06-25 15:46:28 +0200193 }
194
195 read_unlock(&local->sockets.lock);
196
197 if (llcp_sock == NULL)
198 return NULL;
199
200 sock_hold(&llcp_sock->sk);
201
202 return llcp_sock;
203}
204
205static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
206{
207 sock_put(&sock->sk);
208}
209
Samuel Ortizd6469602011-12-14 16:43:12 +0100210static void nfc_llcp_timeout_work(struct work_struct *work)
211{
212 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100213 timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100214
215 nfc_dep_link_down(local->dev);
216}
217
218static void nfc_llcp_symm_timer(unsigned long data)
219{
220 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
221
222 pr_err("SYMM timeout\n");
223
Linus Torvalds916082b2012-10-02 16:01:31 -0700224 schedule_work(&local->timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100225}
226
227struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
228{
229 struct nfc_llcp_local *local, *n;
230
231 list_for_each_entry_safe(local, n, &llcp_devices, list)
232 if (local->dev == dev)
233 return local;
234
235 pr_debug("No device found\n");
236
237 return NULL;
238}
239
240static char *wks[] = {
241 NULL,
242 NULL, /* SDP */
243 "urn:nfc:sn:ip",
244 "urn:nfc:sn:obex",
245 "urn:nfc:sn:snep",
246};
247
248static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
249{
250 int sap, num_wks;
251
252 pr_debug("%s\n", service_name);
253
254 if (service_name == NULL)
255 return -EINVAL;
256
257 num_wks = ARRAY_SIZE(wks);
258
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100259 for (sap = 0; sap < num_wks; sap++) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100260 if (wks[sap] == NULL)
261 continue;
262
263 if (strncmp(wks[sap], service_name, service_name_len) == 0)
264 return sap;
265 }
266
267 return -EINVAL;
268}
269
Samuel Ortiz8f500202012-06-25 15:46:28 +0200270static
271struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
272 u8 *sn, size_t sn_len)
273{
274 struct sock *sk;
275 struct hlist_node *node;
276 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
277
278 pr_debug("sn %zd %p\n", sn_len, sn);
279
280 if (sn == NULL || sn_len == 0)
281 return NULL;
282
283 read_lock(&local->sockets.lock);
284
285 llcp_sock = NULL;
286
287 sk_for_each(sk, node, &local->sockets.head) {
288 tmp_sock = nfc_llcp_sock(sk);
289
290 pr_debug("llcp sock %p\n", tmp_sock);
291
Samuel Ortiz54292d62012-10-15 15:08:29 +0200292 if (tmp_sock->sk.sk_type == SOCK_STREAM &&
293 tmp_sock->sk.sk_state != LLCP_LISTEN)
294 continue;
295
296 if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
297 tmp_sock->sk.sk_state != LLCP_BOUND)
Samuel Ortiz8f500202012-06-25 15:46:28 +0200298 continue;
299
300 if (tmp_sock->service_name == NULL ||
301 tmp_sock->service_name_len == 0)
302 continue;
303
304 if (tmp_sock->service_name_len != sn_len)
305 continue;
306
307 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
308 llcp_sock = tmp_sock;
309 break;
310 }
311 }
312
313 read_unlock(&local->sockets.lock);
314
315 pr_debug("Found llcp sock %p\n", llcp_sock);
316
317 return llcp_sock;
318}
319
Samuel Ortizd6469602011-12-14 16:43:12 +0100320u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100321 struct nfc_llcp_sock *sock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100322{
323 mutex_lock(&local->sdp_lock);
324
325 if (sock->service_name != NULL && sock->service_name_len > 0) {
326 int ssap = nfc_llcp_wks_sap(sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100327 sock->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100328
329 if (ssap > 0) {
330 pr_debug("WKS %d\n", ssap);
331
332 /* This is a WKS, let's check if it's free */
333 if (local->local_wks & BIT(ssap)) {
334 mutex_unlock(&local->sdp_lock);
335
336 return LLCP_SAP_MAX;
337 }
338
Samuel Ortiz1762c172012-03-05 01:03:38 +0100339 set_bit(ssap, &local->local_wks);
Samuel Ortizd6469602011-12-14 16:43:12 +0100340 mutex_unlock(&local->sdp_lock);
341
342 return ssap;
343 }
344
345 /*
Samuel Ortiz8f500202012-06-25 15:46:28 +0200346 * Check if there already is a non WKS socket bound
347 * to this service name.
Samuel Ortizd6469602011-12-14 16:43:12 +0100348 */
Samuel Ortiz8f500202012-06-25 15:46:28 +0200349 if (nfc_llcp_sock_from_sn(local, sock->service_name,
350 sock->service_name_len) != NULL) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100351 mutex_unlock(&local->sdp_lock);
352
353 return LLCP_SAP_MAX;
354 }
355
Samuel Ortizd6469602011-12-14 16:43:12 +0100356 mutex_unlock(&local->sdp_lock);
357
Samuel Ortiz8f500202012-06-25 15:46:28 +0200358 return LLCP_SDP_UNBOUND;
Samuel Ortizd6469602011-12-14 16:43:12 +0100359
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200360 } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
361 if (!test_bit(sock->ssap, &local->local_wks)) {
362 set_bit(sock->ssap, &local->local_wks);
363 mutex_unlock(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100364
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200365 return sock->ssap;
Samuel Ortizd6469602011-12-14 16:43:12 +0100366 }
367 }
368
369 mutex_unlock(&local->sdp_lock);
370
371 return LLCP_SAP_MAX;
372}
373
374u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
375{
376 u8 local_ssap;
377
378 mutex_lock(&local->sdp_lock);
379
380 local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
381 if (local_ssap == LLCP_LOCAL_NUM_SAP) {
382 mutex_unlock(&local->sdp_lock);
383 return LLCP_SAP_MAX;
384 }
385
Samuel Ortiz1762c172012-03-05 01:03:38 +0100386 set_bit(local_ssap, &local->local_sap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100387
388 mutex_unlock(&local->sdp_lock);
389
390 return local_ssap + LLCP_LOCAL_SAP_OFFSET;
391}
392
393void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
394{
395 u8 local_ssap;
396 unsigned long *sdp;
397
398 if (ssap < LLCP_WKS_NUM_SAP) {
399 local_ssap = ssap;
400 sdp = &local->local_wks;
401 } else if (ssap < LLCP_LOCAL_NUM_SAP) {
Samuel Ortiz8f500202012-06-25 15:46:28 +0200402 atomic_t *client_cnt;
403
Samuel Ortizd6469602011-12-14 16:43:12 +0100404 local_ssap = ssap - LLCP_WKS_NUM_SAP;
405 sdp = &local->local_sdp;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200406 client_cnt = &local->local_sdp_cnt[local_ssap];
407
408 pr_debug("%d clients\n", atomic_read(client_cnt));
409
410 mutex_lock(&local->sdp_lock);
411
412 if (atomic_dec_and_test(client_cnt)) {
413 struct nfc_llcp_sock *l_sock;
414
415 pr_debug("No more clients for SAP %d\n", ssap);
416
417 clear_bit(local_ssap, sdp);
418
419 /* Find the listening sock and set it back to UNBOUND */
420 l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
421 if (l_sock) {
422 l_sock->ssap = LLCP_SDP_UNBOUND;
423 nfc_llcp_sock_put(l_sock);
424 }
425 }
426
427 mutex_unlock(&local->sdp_lock);
428
429 return;
Samuel Ortizd6469602011-12-14 16:43:12 +0100430 } else if (ssap < LLCP_MAX_SAP) {
431 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
432 sdp = &local->local_sap;
433 } else {
434 return;
435 }
436
437 mutex_lock(&local->sdp_lock);
438
Samuel Ortiz1762c172012-03-05 01:03:38 +0100439 clear_bit(local_ssap, sdp);
Samuel Ortizd6469602011-12-14 16:43:12 +0100440
441 mutex_unlock(&local->sdp_lock);
442}
443
Samuel Ortiz8f500202012-06-25 15:46:28 +0200444static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
445{
446 u8 ssap;
447
448 mutex_lock(&local->sdp_lock);
449
450 ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
451 if (ssap == LLCP_SDP_NUM_SAP) {
452 mutex_unlock(&local->sdp_lock);
453
454 return LLCP_SAP_MAX;
455 }
456
457 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
458
459 set_bit(ssap, &local->local_sdp);
460
461 mutex_unlock(&local->sdp_lock);
462
463 return LLCP_WKS_NUM_SAP + ssap;
464}
465
Samuel Ortizd6469602011-12-14 16:43:12 +0100466static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
467{
468 u8 *gb_cur, *version_tlv, version, version_length;
Thierry Escande52feb442012-10-17 14:43:39 +0200469 u8 *lto_tlv, lto_length;
Samuel Ortizd6469602011-12-14 16:43:12 +0100470 u8 *wks_tlv, wks_length;
Samuel Ortiz56d58762012-04-10 19:43:19 +0200471 u8 *miux_tlv, miux_length;
Samuel Ortizd6469602011-12-14 16:43:12 +0100472 u8 gb_len = 0;
Wei Yongjun52da2442012-09-02 21:21:46 +0800473 int ret = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100474
475 version = LLCP_VERSION_11;
476 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100477 1, &version_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100478 gb_len += version_length;
479
Thierry Escande52feb442012-10-17 14:43:39 +0200480 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, &lto_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100481 gb_len += lto_length;
482
483 pr_debug("Local wks 0x%lx\n", local->local_wks);
484 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100485 &wks_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100486 gb_len += wks_length;
487
Thierry Escande52feb442012-10-17 14:43:39 +0200488 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
Samuel Ortiz56d58762012-04-10 19:43:19 +0200489 &miux_length);
490 gb_len += miux_length;
491
Samuel Ortizd6469602011-12-14 16:43:12 +0100492 gb_len += ARRAY_SIZE(llcp_magic);
493
494 if (gb_len > NFC_MAX_GT_LEN) {
Wei Yongjun52da2442012-09-02 21:21:46 +0800495 ret = -EINVAL;
496 goto out;
Samuel Ortizd6469602011-12-14 16:43:12 +0100497 }
498
499 gb_cur = local->gb;
500
501 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
502 gb_cur += ARRAY_SIZE(llcp_magic);
503
504 memcpy(gb_cur, version_tlv, version_length);
505 gb_cur += version_length;
506
507 memcpy(gb_cur, lto_tlv, lto_length);
508 gb_cur += lto_length;
509
510 memcpy(gb_cur, wks_tlv, wks_length);
511 gb_cur += wks_length;
512
Samuel Ortiz56d58762012-04-10 19:43:19 +0200513 memcpy(gb_cur, miux_tlv, miux_length);
514 gb_cur += miux_length;
515
Samuel Ortizd6469602011-12-14 16:43:12 +0100516 local->gb_len = gb_len;
517
Wei Yongjun52da2442012-09-02 21:21:46 +0800518out:
519 kfree(version_tlv);
520 kfree(lto_tlv);
521 kfree(wks_tlv);
522 kfree(miux_tlv);
523
524 return ret;
Samuel Ortizd6469602011-12-14 16:43:12 +0100525}
526
Samuel Ortizb8e7a062012-06-22 02:04:53 +0200527u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
528{
529 struct nfc_llcp_local *local;
530
531 local = nfc_llcp_find_local(dev);
532 if (local == NULL) {
533 *general_bytes_len = 0;
534 return NULL;
535 }
536
537 nfc_llcp_build_gb(local);
538
539 *general_bytes_len = local->gb_len;
540
541 return local->gb;
542}
543
Samuel Ortizd6469602011-12-14 16:43:12 +0100544int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
545{
546 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
547
548 if (local == NULL) {
549 pr_err("No LLCP device\n");
550 return -ENODEV;
551 }
552
553 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
554 memcpy(local->remote_gb, gb, gb_len);
555 local->remote_gb_len = gb_len;
556
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100557 if (local->remote_gb == NULL || local->remote_gb_len == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100558 return -ENODEV;
559
560 if (memcmp(local->remote_gb, llcp_magic, 3)) {
561 pr_err("MAC does not support LLCP\n");
562 return -EINVAL;
563 }
564
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200565 return nfc_llcp_parse_gb_tlv(local,
566 &local->remote_gb[3],
567 local->remote_gb_len - 3);
Samuel Ortizd6469602011-12-14 16:43:12 +0100568}
569
Samuel Ortizd6469602011-12-14 16:43:12 +0100570static u8 nfc_llcp_dsap(struct sk_buff *pdu)
571{
572 return (pdu->data[0] & 0xfc) >> 2;
573}
574
575static u8 nfc_llcp_ptype(struct sk_buff *pdu)
576{
577 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
578}
579
580static u8 nfc_llcp_ssap(struct sk_buff *pdu)
581{
582 return pdu->data[1] & 0x3f;
583}
584
585static u8 nfc_llcp_ns(struct sk_buff *pdu)
586{
587 return pdu->data[2] >> 4;
588}
589
590static u8 nfc_llcp_nr(struct sk_buff *pdu)
591{
592 return pdu->data[2] & 0xf;
593}
594
595static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
596{
Samuel Ortiz279cf172012-04-10 19:43:14 +0200597 pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
Samuel Ortizd6469602011-12-14 16:43:12 +0100598 sock->send_n = (sock->send_n + 1) % 16;
599 sock->recv_ack_n = (sock->recv_n - 1) % 16;
600}
601
Thierry Escande44635232012-09-26 18:16:44 +0200602void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
603 struct sk_buff *skb, u8 direction)
604{
605 struct hlist_node *node;
606 struct sk_buff *skb_copy = NULL, *nskb;
607 struct sock *sk;
608 u8 *data;
609
610 read_lock(&local->raw_sockets.lock);
611
612 sk_for_each(sk, node, &local->raw_sockets.head) {
613 if (sk->sk_state != LLCP_BOUND)
614 continue;
615
616 if (skb_copy == NULL) {
617 skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE,
618 GFP_ATOMIC);
619
620 if (skb_copy == NULL)
621 continue;
622
623 data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE);
624
625 data[0] = local->dev ? local->dev->idx : 0xFF;
626 data[1] = direction;
627 }
628
629 nskb = skb_clone(skb_copy, GFP_ATOMIC);
630 if (!nskb)
631 continue;
632
633 if (sock_queue_rcv_skb(sk, nskb))
634 kfree_skb(nskb);
635 }
636
637 read_unlock(&local->raw_sockets.lock);
638
639 kfree_skb(skb_copy);
640}
641
Samuel Ortiz84457962012-05-30 17:48:29 +0200642static void nfc_llcp_tx_work(struct work_struct *work)
643{
644 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
645 tx_work);
646 struct sk_buff *skb;
647 struct sock *sk;
648 struct nfc_llcp_sock *llcp_sock;
649
650 skb = skb_dequeue(&local->tx_queue);
651 if (skb != NULL) {
652 sk = skb->sk;
653 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiza6a09152012-10-05 00:37:22 +0200654
655 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
656 nfc_llcp_send_symm(local->dev);
657 } else {
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100658 struct sk_buff *copy_skb = NULL;
659 u8 ptype = nfc_llcp_ptype(skb);
Samuel Ortiz84457962012-05-30 17:48:29 +0200660 int ret;
661
662 pr_debug("Sending pending skb\n");
663 print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
664 DUMP_PREFIX_OFFSET, 16, 1,
665 skb->data, skb->len, true);
666
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100667 if (ptype == LLCP_PDU_I)
668 copy_skb = skb_copy(skb, GFP_ATOMIC);
669
Thierry Escande2c2d45b2012-11-27 15:44:24 +0100670 __net_timestamp(skb);
671
Thierry Escande44635232012-09-26 18:16:44 +0200672 nfc_llcp_send_to_raw_sock(local, skb,
673 NFC_LLCP_DIRECTION_TX);
674
Samuel Ortiz84457962012-05-30 17:48:29 +0200675 ret = nfc_data_exchange(local->dev, local->target_idx,
676 skb, nfc_llcp_recv, local);
677
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100678 if (ret) {
679 kfree_skb(copy_skb);
680 goto out;
Samuel Ortiz84457962012-05-30 17:48:29 +0200681 }
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100682
683 if (ptype == LLCP_PDU_I && copy_skb)
684 skb_queue_tail(&llcp_sock->tx_pending_queue,
685 copy_skb);
Samuel Ortiz84457962012-05-30 17:48:29 +0200686 }
687 } else {
688 nfc_llcp_send_symm(local->dev);
689 }
690
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100691out:
Samuel Ortiz84457962012-05-30 17:48:29 +0200692 mod_timer(&local->link_timer,
693 jiffies + msecs_to_jiffies(2 * local->remote_lto));
694}
695
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200696static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
697 u8 ssap)
698{
699 struct sock *sk;
700 struct nfc_llcp_sock *llcp_sock;
701 struct hlist_node *node;
702
703 read_lock(&local->connecting_sockets.lock);
704
705 sk_for_each(sk, node, &local->connecting_sockets.head) {
706 llcp_sock = nfc_llcp_sock(sk);
707
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200708 if (llcp_sock->ssap == ssap) {
709 sock_hold(&llcp_sock->sk);
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200710 goto out;
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200711 }
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200712 }
713
714 llcp_sock = NULL;
715
716out:
717 read_unlock(&local->connecting_sockets.lock);
718
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200719 return llcp_sock;
720}
721
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200722static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
723 u8 *sn, size_t sn_len)
724{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200725 struct nfc_llcp_sock *llcp_sock;
726
Samuel Ortiz8f500202012-06-25 15:46:28 +0200727 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100728
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200729 if (llcp_sock == NULL)
730 return NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100731
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200732 sock_hold(&llcp_sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100733
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200734 return llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +0100735}
736
Samuel Ortizd6469602011-12-14 16:43:12 +0100737static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
738{
739 u8 *tlv = &skb->data[2], type, length;
740 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
741
742 while (offset < tlv_array_len) {
743 type = tlv[0];
744 length = tlv[1];
745
746 pr_debug("type 0x%x length %d\n", type, length);
747
748 if (type == LLCP_TLV_SN) {
749 *sn_len = length;
750 return &tlv[2];
751 }
752
753 offset += length + 2;
754 tlv += length + 2;
755 }
756
757 return NULL;
758}
759
Samuel Ortiz968272b2012-10-15 16:14:37 +0200760static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
761 struct sk_buff *skb)
762{
763 struct nfc_llcp_sock *llcp_sock;
764 struct nfc_llcp_ui_cb *ui_cb;
765 u8 dsap, ssap;
766
767 dsap = nfc_llcp_dsap(skb);
768 ssap = nfc_llcp_ssap(skb);
769
770 ui_cb = nfc_llcp_ui_skb_cb(skb);
771 ui_cb->dsap = dsap;
772 ui_cb->ssap = ssap;
773
774 printk("%s %d %d\n", __func__, dsap, ssap);
775
776 pr_debug("%d %d\n", dsap, ssap);
777
778 /* We're looking for a bound socket, not a client one */
779 llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
780 if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
781 return;
782
783 /* There is no sequence with UI frames */
784 skb_pull(skb, LLCP_HEADER_SIZE);
Samuel Ortiz1727cf92012-12-07 16:37:30 +0100785 if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
786 /*
787 * UI frames will be freed from the socket layer, so we
788 * need to keep them alive until someone receives them.
789 */
790 skb_get(skb);
791 } else {
792 pr_err("Receive queue is full\n");
Samuel Ortiz2593c2c2012-12-07 15:55:19 +0100793 kfree_skb(skb);
Samuel Ortiz968272b2012-10-15 16:14:37 +0200794 }
795
796 nfc_llcp_sock_put(llcp_sock);
797}
798
Samuel Ortizd6469602011-12-14 16:43:12 +0100799static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100800 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100801{
802 struct sock *new_sk, *parent;
803 struct nfc_llcp_sock *sock, *new_sock;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200804 u8 dsap, ssap, reason;
Samuel Ortizd6469602011-12-14 16:43:12 +0100805
806 dsap = nfc_llcp_dsap(skb);
807 ssap = nfc_llcp_ssap(skb);
808
809 pr_debug("%d %d\n", dsap, ssap);
810
Samuel Ortizd6469602011-12-14 16:43:12 +0100811 if (dsap != LLCP_SAP_SDP) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200812 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
813 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100814 reason = LLCP_DM_NOBOUND;
815 goto fail;
816 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100817 } else {
818 u8 *sn;
819 size_t sn_len;
820
821 sn = nfc_llcp_connect_sn(skb, &sn_len);
822 if (sn == NULL) {
823 reason = LLCP_DM_NOBOUND;
824 goto fail;
825 }
826
827 pr_debug("Service name length %zu\n", sn_len);
828
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200829 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
830 if (sock == NULL) {
831 reason = LLCP_DM_NOBOUND;
832 goto fail;
Samuel Ortizd6469602011-12-14 16:43:12 +0100833 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100834 }
835
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200836 lock_sock(&sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100837
Samuel Ortizd6469602011-12-14 16:43:12 +0100838 parent = &sock->sk;
839
840 if (sk_acceptq_is_full(parent)) {
841 reason = LLCP_DM_REJ;
842 release_sock(&sock->sk);
843 sock_put(&sock->sk);
844 goto fail;
845 }
846
Samuel Ortiz8f500202012-06-25 15:46:28 +0200847 if (sock->ssap == LLCP_SDP_UNBOUND) {
848 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
849
850 pr_debug("First client, reserving %d\n", ssap);
851
852 if (ssap == LLCP_SAP_MAX) {
853 reason = LLCP_DM_REJ;
854 release_sock(&sock->sk);
855 sock_put(&sock->sk);
856 goto fail;
857 }
858
859 sock->ssap = ssap;
860 }
861
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100862 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC);
Samuel Ortizd6469602011-12-14 16:43:12 +0100863 if (new_sk == NULL) {
864 reason = LLCP_DM_REJ;
865 release_sock(&sock->sk);
866 sock_put(&sock->sk);
867 goto fail;
868 }
869
870 new_sock = nfc_llcp_sock(new_sk);
871 new_sock->dev = local->dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200872 new_sock->local = nfc_llcp_local_get(local);
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200873 new_sock->miu = local->remote_miu;
Samuel Ortizd6469602011-12-14 16:43:12 +0100874 new_sock->nfc_protocol = sock->nfc_protocol;
Samuel Ortizd6469602011-12-14 16:43:12 +0100875 new_sock->dsap = ssap;
Samuel Ortiz025f1522012-06-18 21:38:09 +0200876 new_sock->target_idx = local->target_idx;
Samuel Ortizd6469602011-12-14 16:43:12 +0100877 new_sock->parent = parent;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200878 new_sock->ssap = sock->ssap;
879 if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
880 atomic_t *client_count;
881
882 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
883
884 client_count =
885 &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
886
887 atomic_inc(client_count);
888 new_sock->reserved_ssap = sock->ssap;
889 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100890
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200891 nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
892 skb->len - LLCP_HEADER_SIZE);
893
Samuel Ortizd6469602011-12-14 16:43:12 +0100894 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
895
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200896 nfc_llcp_sock_link(&local->sockets, new_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100897
898 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
899
900 nfc_get_device(local->dev->idx);
901
902 new_sk->sk_state = LLCP_CONNECTED;
903
904 /* Wake the listening processes */
905 parent->sk_data_ready(parent, 0);
906
907 /* Send CC */
908 nfc_llcp_send_cc(new_sock);
909
910 release_sock(&sock->sk);
911 sock_put(&sock->sk);
912
913 return;
914
915fail:
916 /* Send DM */
917 nfc_llcp_send_dm(local, dsap, ssap, reason);
Samuel Ortizd6469602011-12-14 16:43:12 +0100918}
919
Samuel Ortizd094afa2012-03-05 01:03:42 +0100920int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100921{
Samuel Ortizd094afa2012-03-05 01:03:42 +0100922 int nr_frames = 0;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100923 struct nfc_llcp_local *local = sock->local;
924
925 pr_debug("Remote ready %d tx queue len %d remote rw %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100926 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200927 sock->rw);
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100928
929 /* Try to queue some I frames for transmission */
930 while (sock->remote_ready &&
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200931 skb_queue_len(&sock->tx_pending_queue) < sock->rw) {
Samuel Ortiz84457962012-05-30 17:48:29 +0200932 struct sk_buff *pdu;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100933
934 pdu = skb_dequeue(&sock->tx_queue);
935 if (pdu == NULL)
936 break;
937
938 /* Update N(S)/N(R) */
939 nfc_llcp_set_nrns(sock, pdu);
940
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100941 skb_queue_tail(&local->tx_queue, pdu);
Samuel Ortizd094afa2012-03-05 01:03:42 +0100942 nr_frames++;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100943 }
Samuel Ortizd094afa2012-03-05 01:03:42 +0100944
945 return nr_frames;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100946}
947
Samuel Ortizd6469602011-12-14 16:43:12 +0100948static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100949 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100950{
951 struct nfc_llcp_sock *llcp_sock;
952 struct sock *sk;
953 u8 dsap, ssap, ptype, ns, nr;
954
955 ptype = nfc_llcp_ptype(skb);
956 dsap = nfc_llcp_dsap(skb);
957 ssap = nfc_llcp_ssap(skb);
958 ns = nfc_llcp_ns(skb);
959 nr = nfc_llcp_nr(skb);
960
961 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
962
963 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
964 if (llcp_sock == NULL) {
965 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
966 return;
967 }
968
969 sk = &llcp_sock->sk;
970 lock_sock(sk);
971 if (sk->sk_state == LLCP_CLOSED) {
972 release_sock(sk);
973 nfc_llcp_sock_put(llcp_sock);
974 }
975
Samuel Ortizd6469602011-12-14 16:43:12 +0100976 /* Pass the payload upstream */
977 if (ptype == LLCP_PDU_I) {
978 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
979
Samuel Ortiz53aef922012-03-05 01:03:36 +0100980 if (ns == llcp_sock->recv_n)
981 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
982 else
983 pr_err("Received out of sequence I PDU\n");
984
Samuel Ortizd6469602011-12-14 16:43:12 +0100985 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
Samuel Ortiz1727cf92012-12-07 16:37:30 +0100986 if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
987 /*
988 * I frames will be freed from the socket layer, so we
989 * need to keep them alive until someone receives them.
990 */
991 skb_get(skb);
992 } else {
993 pr_err("Receive queue is full\n");
Samuel Ortiz2593c2c2012-12-07 15:55:19 +0100994 kfree_skb(skb);
Samuel Ortizd6469602011-12-14 16:43:12 +0100995 }
996 }
997
998 /* Remove skbs from the pending queue */
999 if (llcp_sock->send_ack_n != nr) {
1000 struct sk_buff *s, *tmp;
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +01001001 u8 n;
Samuel Ortizd6469602011-12-14 16:43:12 +01001002
1003 llcp_sock->send_ack_n = nr;
1004
Samuel Ortiz84457962012-05-30 17:48:29 +02001005 /* Remove and free all skbs until ns == nr */
1006 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +01001007 n = nfc_llcp_ns(s);
1008
Samuel Ortiz84457962012-05-30 17:48:29 +02001009 skb_unlink(s, &llcp_sock->tx_pending_queue);
1010 kfree_skb(s);
1011
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +01001012 if (n == nr)
Samuel Ortiz84457962012-05-30 17:48:29 +02001013 break;
1014 }
1015
1016 /* Re-queue the remaining skbs for transmission */
1017 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
1018 s, tmp) {
1019 skb_unlink(s, &llcp_sock->tx_pending_queue);
1020 skb_queue_head(&local->tx_queue, s);
1021 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001022 }
1023
Samuel Ortiz53aef922012-03-05 01:03:36 +01001024 if (ptype == LLCP_PDU_RR)
1025 llcp_sock->remote_ready = true;
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001026 else if (ptype == LLCP_PDU_RNR)
Samuel Ortiz53aef922012-03-05 01:03:36 +01001027 llcp_sock->remote_ready = false;
1028
Samuel Ortiz56af25682012-05-30 18:06:11 +02001029 if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
Samuel Ortizd094afa2012-03-05 01:03:42 +01001030 nfc_llcp_send_rr(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001031
1032 release_sock(sk);
1033 nfc_llcp_sock_put(llcp_sock);
1034}
1035
1036static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001037 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001038{
1039 struct nfc_llcp_sock *llcp_sock;
1040 struct sock *sk;
1041 u8 dsap, ssap;
1042
1043 dsap = nfc_llcp_dsap(skb);
1044 ssap = nfc_llcp_ssap(skb);
1045
1046 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1047 if (llcp_sock == NULL) {
1048 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1049 return;
1050 }
1051
1052 sk = &llcp_sock->sk;
1053 lock_sock(sk);
Samuel Ortizf31652a2012-10-26 18:20:10 +02001054
1055 nfc_llcp_socket_purge(llcp_sock);
1056
Samuel Ortizd6469602011-12-14 16:43:12 +01001057 if (sk->sk_state == LLCP_CLOSED) {
1058 release_sock(sk);
1059 nfc_llcp_sock_put(llcp_sock);
1060 }
1061
Samuel Ortizd6469602011-12-14 16:43:12 +01001062 if (sk->sk_state == LLCP_CONNECTED) {
1063 nfc_put_device(local->dev);
1064 sk->sk_state = LLCP_CLOSED;
1065 sk->sk_state_change(sk);
1066 }
1067
1068 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
1069
1070 release_sock(sk);
1071 nfc_llcp_sock_put(llcp_sock);
1072}
1073
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001074static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001075{
1076 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizff353d82012-05-07 12:31:19 +02001077 struct sock *sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001078 u8 dsap, ssap;
1079
Samuel Ortizd6469602011-12-14 16:43:12 +01001080 dsap = nfc_llcp_dsap(skb);
1081 ssap = nfc_llcp_ssap(skb);
1082
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001083 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
Samuel Ortizd6469602011-12-14 16:43:12 +01001084 if (llcp_sock == NULL) {
1085 pr_err("Invalid CC\n");
1086 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1087
1088 return;
1089 }
1090
Samuel Ortizff353d82012-05-07 12:31:19 +02001091 sk = &llcp_sock->sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001092
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001093 /* Unlink from connecting and link to the client array */
1094 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
1095 nfc_llcp_sock_link(&local->sockets, sk);
1096 llcp_sock->dsap = ssap;
1097
Samuel Ortiz7a06e582012-05-07 22:03:34 +02001098 nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
1099 skb->len - LLCP_HEADER_SIZE);
Samuel Ortizd6469602011-12-14 16:43:12 +01001100
Samuel Ortizff353d82012-05-07 12:31:19 +02001101 sk->sk_state = LLCP_CONNECTED;
1102 sk->sk_state_change(sk);
1103
Samuel Ortizd6469602011-12-14 16:43:12 +01001104 nfc_llcp_sock_put(llcp_sock);
1105}
1106
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001107static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1108{
1109 struct nfc_llcp_sock *llcp_sock;
1110 struct sock *sk;
1111 u8 dsap, ssap, reason;
1112
1113 dsap = nfc_llcp_dsap(skb);
1114 ssap = nfc_llcp_ssap(skb);
1115 reason = skb->data[2];
1116
1117 pr_debug("%d %d reason %d\n", ssap, dsap, reason);
1118
1119 switch (reason) {
1120 case LLCP_DM_NOBOUND:
1121 case LLCP_DM_REJ:
1122 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1123 break;
1124
1125 default:
1126 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1127 break;
1128 }
1129
1130 if (llcp_sock == NULL) {
Samuel Ortiza8df0f32012-10-16 21:15:59 +02001131 pr_debug("Already closed\n");
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001132 return;
1133 }
1134
1135 sk = &llcp_sock->sk;
1136
1137 sk->sk_err = ENXIO;
1138 sk->sk_state = LLCP_CLOSED;
1139 sk->sk_state_change(sk);
1140
1141 nfc_llcp_sock_put(llcp_sock);
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001142}
1143
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001144static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1145 struct sk_buff *skb)
1146{
1147 struct nfc_llcp_sock *llcp_sock;
1148 u8 dsap, ssap, *tlv, type, length, tid, sap;
1149 u16 tlv_len, offset;
1150 char *service_name;
1151 size_t service_name_len;
1152
1153 dsap = nfc_llcp_dsap(skb);
1154 ssap = nfc_llcp_ssap(skb);
1155
1156 pr_debug("%d %d\n", dsap, ssap);
1157
1158 if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1159 pr_err("Wrong SNL SAP\n");
1160 return;
1161 }
1162
1163 tlv = &skb->data[LLCP_HEADER_SIZE];
1164 tlv_len = skb->len - LLCP_HEADER_SIZE;
1165 offset = 0;
1166
Szymon Janc0f450772012-10-17 15:23:39 +02001167 while (offset < tlv_len) {
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001168 type = tlv[0];
1169 length = tlv[1];
1170
1171 switch (type) {
1172 case LLCP_TLV_SDREQ:
1173 tid = tlv[2];
1174 service_name = (char *) &tlv[3];
1175 service_name_len = length - 1;
1176
Samuel Ortize6904082012-10-15 14:28:13 +02001177 pr_debug("Looking for %.16s\n", service_name);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001178
1179 if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1180 !strncmp(service_name, "urn:nfc:sn:sdp",
1181 service_name_len)) {
1182 sap = 1;
Samuel Ortize6904082012-10-15 14:28:13 +02001183 goto send_snl;
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001184 }
1185
Samuel Ortize6904082012-10-15 14:28:13 +02001186 llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
1187 service_name_len);
1188 if (!llcp_sock) {
1189 sap = 0;
1190 goto send_snl;
1191 }
1192
1193 /*
1194 * We found a socket but its ssap has not been reserved
1195 * yet. We need to assign it for good and send a reply.
1196 * The ssap will be freed when the socket is closed.
1197 */
1198 if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1199 atomic_t *client_count;
1200
1201 sap = nfc_llcp_reserve_sdp_ssap(local);
1202
1203 pr_debug("Reserving %d\n", sap);
1204
1205 if (sap == LLCP_SAP_MAX) {
1206 sap = 0;
1207 goto send_snl;
1208 }
1209
1210 client_count =
1211 &local->local_sdp_cnt[sap -
1212 LLCP_WKS_NUM_SAP];
1213
1214 atomic_inc(client_count);
1215
1216 llcp_sock->ssap = sap;
1217 llcp_sock->reserved_ssap = sap;
1218 } else {
1219 sap = llcp_sock->ssap;
1220 }
1221
1222 pr_debug("%p %d\n", llcp_sock, sap);
1223
Szymon Janc0f450772012-10-17 15:23:39 +02001224send_snl:
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001225 nfc_llcp_send_snl(local, tid, sap);
1226 break;
1227
1228 default:
1229 pr_err("Invalid SNL tlv value 0x%x\n", type);
1230 break;
1231 }
1232
1233 offset += length + 2;
1234 tlv += length + 2;
1235 }
1236}
1237
Samuel Ortizd6469602011-12-14 16:43:12 +01001238static void nfc_llcp_rx_work(struct work_struct *work)
1239{
1240 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001241 rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001242 u8 dsap, ssap, ptype;
1243 struct sk_buff *skb;
1244
1245 skb = local->rx_pending;
1246 if (skb == NULL) {
1247 pr_debug("No pending SKB\n");
1248 return;
1249 }
1250
1251 ptype = nfc_llcp_ptype(skb);
1252 dsap = nfc_llcp_dsap(skb);
1253 ssap = nfc_llcp_ssap(skb);
1254
1255 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1256
Samuel Ortiz4be646e2012-04-10 19:43:13 +02001257 if (ptype != LLCP_PDU_SYMM)
1258 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
1259 16, 1, skb->data, skb->len, true);
1260
Thierry Escande2c2d45b2012-11-27 15:44:24 +01001261 __net_timestamp(skb);
1262
Thierry Escande44635232012-09-26 18:16:44 +02001263 nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX);
1264
Samuel Ortizd6469602011-12-14 16:43:12 +01001265 switch (ptype) {
1266 case LLCP_PDU_SYMM:
1267 pr_debug("SYMM\n");
1268 break;
1269
Samuel Ortiz968272b2012-10-15 16:14:37 +02001270 case LLCP_PDU_UI:
1271 pr_debug("UI\n");
1272 nfc_llcp_recv_ui(local, skb);
1273 break;
1274
Samuel Ortizd6469602011-12-14 16:43:12 +01001275 case LLCP_PDU_CONNECT:
1276 pr_debug("CONNECT\n");
1277 nfc_llcp_recv_connect(local, skb);
1278 break;
1279
1280 case LLCP_PDU_DISC:
1281 pr_debug("DISC\n");
1282 nfc_llcp_recv_disc(local, skb);
1283 break;
1284
1285 case LLCP_PDU_CC:
1286 pr_debug("CC\n");
1287 nfc_llcp_recv_cc(local, skb);
1288 break;
1289
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001290 case LLCP_PDU_DM:
1291 pr_debug("DM\n");
1292 nfc_llcp_recv_dm(local, skb);
1293 break;
1294
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001295 case LLCP_PDU_SNL:
1296 pr_debug("SNL\n");
1297 nfc_llcp_recv_snl(local, skb);
1298 break;
1299
Samuel Ortizd6469602011-12-14 16:43:12 +01001300 case LLCP_PDU_I:
1301 case LLCP_PDU_RR:
Samuel Ortiz53aef922012-03-05 01:03:36 +01001302 case LLCP_PDU_RNR:
Samuel Ortizd6469602011-12-14 16:43:12 +01001303 pr_debug("I frame\n");
1304 nfc_llcp_recv_hdlc(local, skb);
1305 break;
1306
1307 }
1308
Linus Torvalds916082b2012-10-02 16:01:31 -07001309 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001310 kfree_skb(local->rx_pending);
1311 local->rx_pending = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +01001312}
1313
Samuel Ortiz1727cf92012-12-07 16:37:30 +01001314static void __nfc_llcp_recv(struct nfc_llcp_local *local, struct sk_buff *skb)
1315{
1316 local->rx_pending = skb;
1317 del_timer(&local->link_timer);
1318 schedule_work(&local->rx_work);
1319}
1320
Samuel Ortizd6469602011-12-14 16:43:12 +01001321void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1322{
1323 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1324
1325 pr_debug("Received an LLCP PDU\n");
1326 if (err < 0) {
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001327 pr_err("err %d\n", err);
Samuel Ortizd6469602011-12-14 16:43:12 +01001328 return;
1329 }
1330
Samuel Ortiz1727cf92012-12-07 16:37:30 +01001331 __nfc_llcp_recv(local, skb);
Samuel Ortizd6469602011-12-14 16:43:12 +01001332}
1333
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001334int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1335{
1336 struct nfc_llcp_local *local;
1337
1338 local = nfc_llcp_find_local(dev);
1339 if (local == NULL)
1340 return -ENODEV;
1341
Samuel Ortiz1727cf92012-12-07 16:37:30 +01001342 __nfc_llcp_recv(local, skb);
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001343
1344 return 0;
1345}
1346
Samuel Ortizd6469602011-12-14 16:43:12 +01001347void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1348{
1349 struct nfc_llcp_local *local;
1350
1351 local = nfc_llcp_find_local(dev);
1352 if (local == NULL)
1353 return;
1354
1355 /* Close and purge all existing sockets */
Samuel Ortiz4d22ea12012-06-22 14:40:34 +02001356 nfc_llcp_socket_release(local, true);
Samuel Ortizd6469602011-12-14 16:43:12 +01001357}
1358
1359void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1360 u8 comm_mode, u8 rf_mode)
1361{
1362 struct nfc_llcp_local *local;
1363
1364 pr_debug("rf mode %d\n", rf_mode);
1365
1366 local = nfc_llcp_find_local(dev);
1367 if (local == NULL)
1368 return;
1369
1370 local->target_idx = target_idx;
1371 local->comm_mode = comm_mode;
1372 local->rf_mode = rf_mode;
1373
1374 if (rf_mode == NFC_RF_INITIATOR) {
1375 pr_debug("Queueing Tx work\n");
1376
Linus Torvalds916082b2012-10-02 16:01:31 -07001377 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001378 } else {
1379 mod_timer(&local->link_timer,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001380 jiffies + msecs_to_jiffies(local->remote_lto));
Samuel Ortizd6469602011-12-14 16:43:12 +01001381 }
1382}
1383
1384int nfc_llcp_register_device(struct nfc_dev *ndev)
1385{
Samuel Ortizd6469602011-12-14 16:43:12 +01001386 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +01001387
1388 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
1389 if (local == NULL)
1390 return -ENOMEM;
1391
1392 local->dev = ndev;
1393 INIT_LIST_HEAD(&local->list);
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001394 kref_init(&local->ref);
Samuel Ortizd6469602011-12-14 16:43:12 +01001395 mutex_init(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001396 init_timer(&local->link_timer);
1397 local->link_timer.data = (unsigned long) local;
1398 local->link_timer.function = nfc_llcp_symm_timer;
1399
1400 skb_queue_head_init(&local->tx_queue);
1401 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001402
1403 local->rx_pending = NULL;
1404 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001405
1406 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001407
Szymon Jancfe235b52012-09-25 12:42:50 +02001408 rwlock_init(&local->sockets.lock);
1409 rwlock_init(&local->connecting_sockets.lock);
Thierry Escande44635232012-09-26 18:16:44 +02001410 rwlock_init(&local->raw_sockets.lock);
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001411
Thierry Escande52feb442012-10-17 14:43:39 +02001412 local->lto = 150; /* 1500 ms */
1413 local->rw = LLCP_MAX_RW;
1414 local->miux = cpu_to_be16(LLCP_MAX_MIUX);
1415
Samuel Ortizd6469602011-12-14 16:43:12 +01001416 nfc_llcp_build_gb(local);
1417
1418 local->remote_miu = LLCP_DEFAULT_MIU;
1419 local->remote_lto = LLCP_DEFAULT_LTO;
Samuel Ortizd6469602011-12-14 16:43:12 +01001420
Thierry Escande16a78e92012-10-12 15:25:43 +02001421 list_add(&local->list, &llcp_devices);
Samuel Ortizd6469602011-12-14 16:43:12 +01001422
1423 return 0;
Samuel Ortizd6469602011-12-14 16:43:12 +01001424}
1425
1426void nfc_llcp_unregister_device(struct nfc_dev *dev)
1427{
1428 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1429
1430 if (local == NULL) {
1431 pr_debug("No such device\n");
1432 return;
1433 }
1434
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001435 nfc_llcp_local_put(local);
Samuel Ortizd6469602011-12-14 16:43:12 +01001436}
1437
1438int __init nfc_llcp_init(void)
1439{
1440 INIT_LIST_HEAD(&llcp_devices);
1441
1442 return nfc_llcp_sock_init();
1443}
1444
1445void nfc_llcp_exit(void)
1446{
1447 nfc_llcp_sock_exit();
1448}