blob: 285bba8c7afc415d0ed021f894ae667e768c6391 [file] [log] [blame]
Samuel Ortizd6469602011-12-14 16:43:12 +01001/*
2 * Copyright (C) 2011 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#define pr_fmt(fmt) "llcp: %s: " fmt, __func__
21
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/list.h>
25#include <linux/nfc.h>
26
27#include "../nfc.h"
28#include "llcp.h"
29
30static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
31
32static struct list_head llcp_devices;
33
Samuel Ortiza69f32a2012-05-04 17:04:19 +020034void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk)
35{
36 write_lock(&l->lock);
37 sk_add_node(sk, &l->head);
38 write_unlock(&l->lock);
39}
40
41void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk)
42{
43 write_lock(&l->lock);
44 sk_del_node_init(sk);
45 write_unlock(&l->lock);
46}
47
Samuel Ortiz4d22ea12012-06-22 14:40:34 +020048static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
Samuel Ortizd6469602011-12-14 16:43:12 +010049{
Samuel Ortiza69f32a2012-05-04 17:04:19 +020050 struct sock *sk;
51 struct hlist_node *node, *tmp;
52 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +010053
Samuel Ortiza69f32a2012-05-04 17:04:19 +020054 write_lock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +010055
Samuel Ortiza69f32a2012-05-04 17:04:19 +020056 sk_for_each_safe(sk, node, tmp, &local->sockets.head) {
57 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010058
Szymon Janc50b78b22012-09-26 14:22:10 +020059 bh_lock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010060
Samuel Ortiza69f32a2012-05-04 17:04:19 +020061 if (sk->sk_state == LLCP_CONNECTED)
62 nfc_put_device(llcp_sock->dev);
Samuel Ortizd6469602011-12-14 16:43:12 +010063
Samuel Ortiza69f32a2012-05-04 17:04:19 +020064 if (sk->sk_state == LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +010065 struct nfc_llcp_sock *lsk, *n;
66 struct sock *accept_sk;
67
Samuel Ortiza69f32a2012-05-04 17:04:19 +020068 list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +010069 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +010070 accept_sk = &lsk->sk;
Szymon Janc50b78b22012-09-26 14:22:10 +020071 bh_lock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010072
73 nfc_llcp_accept_unlink(accept_sk);
74
75 accept_sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +010076
Szymon Janc50b78b22012-09-26 14:22:10 +020077 bh_unlock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010078
79 sock_orphan(accept_sk);
80 }
Samuel Ortiz4d22ea12012-06-22 14:40:34 +020081
82 if (listen == true) {
Szymon Janc50b78b22012-09-26 14:22:10 +020083 bh_unlock_sock(sk);
Samuel Ortiz4d22ea12012-06-22 14:40:34 +020084 continue;
85 }
Samuel Ortizd6469602011-12-14 16:43:12 +010086 }
87
Samuel Ortizc8512be2012-10-15 15:09:52 +020088 /*
89 * If we have a connection less socket bound, we keep it alive
90 * if the device is still present.
91 */
92 if (sk->sk_state == LLCP_BOUND && sk->sk_type == SOCK_DGRAM &&
93 listen == true) {
94 bh_unlock_sock(sk);
95 continue;
96 }
97
Samuel Ortiza69f32a2012-05-04 17:04:19 +020098 sk->sk_state = LLCP_CLOSED;
Samuel Ortizd6469602011-12-14 16:43:12 +010099
Szymon Janc50b78b22012-09-26 14:22:10 +0200100 bh_unlock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100101
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200102 sock_orphan(sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100103
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200104 sk_del_node_init(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100105 }
106
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200107 write_unlock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100108}
109
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200110struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
111{
112 kref_get(&local->ref);
113
114 return local;
115}
116
117static void local_release(struct kref *ref)
118{
119 struct nfc_llcp_local *local;
120
121 local = container_of(ref, struct nfc_llcp_local, ref);
122
123 list_del(&local->list);
Samuel Ortiz4d22ea12012-06-22 14:40:34 +0200124 nfc_llcp_socket_release(local, false);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200125 del_timer_sync(&local->link_timer);
126 skb_queue_purge(&local->tx_queue);
Tejun Heo474fee32012-08-22 16:22:16 -0700127 cancel_work_sync(&local->tx_work);
128 cancel_work_sync(&local->rx_work);
129 cancel_work_sync(&local->timeout_work);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200130 kfree_skb(local->rx_pending);
131 kfree(local);
132}
133
134int nfc_llcp_local_put(struct nfc_llcp_local *local)
135{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200136 if (local == NULL)
137 return 0;
138
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200139 return kref_put(&local->ref, local_release);
140}
141
Samuel Ortiz8f500202012-06-25 15:46:28 +0200142static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
143 u8 ssap, u8 dsap)
144{
145 struct sock *sk;
146 struct hlist_node *node;
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200147 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200148
149 pr_debug("ssap dsap %d %d\n", ssap, dsap);
150
151 if (ssap == 0 && dsap == 0)
152 return NULL;
153
154 read_lock(&local->sockets.lock);
155
156 llcp_sock = NULL;
157
158 sk_for_each(sk, node, &local->sockets.head) {
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200159 tmp_sock = nfc_llcp_sock(sk);
Samuel Ortiz8f500202012-06-25 15:46:28 +0200160
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200161 if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
162 llcp_sock = tmp_sock;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200163 break;
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200164 }
Samuel Ortiz8f500202012-06-25 15:46:28 +0200165 }
166
167 read_unlock(&local->sockets.lock);
168
169 if (llcp_sock == NULL)
170 return NULL;
171
172 sock_hold(&llcp_sock->sk);
173
174 return llcp_sock;
175}
176
177static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
178{
179 sock_put(&sock->sk);
180}
181
Samuel Ortizd6469602011-12-14 16:43:12 +0100182static void nfc_llcp_timeout_work(struct work_struct *work)
183{
184 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100185 timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100186
187 nfc_dep_link_down(local->dev);
188}
189
190static void nfc_llcp_symm_timer(unsigned long data)
191{
192 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
193
194 pr_err("SYMM timeout\n");
195
Linus Torvalds916082b2012-10-02 16:01:31 -0700196 schedule_work(&local->timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100197}
198
199struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
200{
201 struct nfc_llcp_local *local, *n;
202
203 list_for_each_entry_safe(local, n, &llcp_devices, list)
204 if (local->dev == dev)
205 return local;
206
207 pr_debug("No device found\n");
208
209 return NULL;
210}
211
212static char *wks[] = {
213 NULL,
214 NULL, /* SDP */
215 "urn:nfc:sn:ip",
216 "urn:nfc:sn:obex",
217 "urn:nfc:sn:snep",
218};
219
220static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
221{
222 int sap, num_wks;
223
224 pr_debug("%s\n", service_name);
225
226 if (service_name == NULL)
227 return -EINVAL;
228
229 num_wks = ARRAY_SIZE(wks);
230
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100231 for (sap = 0; sap < num_wks; sap++) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100232 if (wks[sap] == NULL)
233 continue;
234
235 if (strncmp(wks[sap], service_name, service_name_len) == 0)
236 return sap;
237 }
238
239 return -EINVAL;
240}
241
Samuel Ortiz8f500202012-06-25 15:46:28 +0200242static
243struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
244 u8 *sn, size_t sn_len)
245{
246 struct sock *sk;
247 struct hlist_node *node;
248 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
249
250 pr_debug("sn %zd %p\n", sn_len, sn);
251
252 if (sn == NULL || sn_len == 0)
253 return NULL;
254
255 read_lock(&local->sockets.lock);
256
257 llcp_sock = NULL;
258
259 sk_for_each(sk, node, &local->sockets.head) {
260 tmp_sock = nfc_llcp_sock(sk);
261
262 pr_debug("llcp sock %p\n", tmp_sock);
263
Samuel Ortiz54292d62012-10-15 15:08:29 +0200264 if (tmp_sock->sk.sk_type == SOCK_STREAM &&
265 tmp_sock->sk.sk_state != LLCP_LISTEN)
266 continue;
267
268 if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
269 tmp_sock->sk.sk_state != LLCP_BOUND)
Samuel Ortiz8f500202012-06-25 15:46:28 +0200270 continue;
271
272 if (tmp_sock->service_name == NULL ||
273 tmp_sock->service_name_len == 0)
274 continue;
275
276 if (tmp_sock->service_name_len != sn_len)
277 continue;
278
279 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
280 llcp_sock = tmp_sock;
281 break;
282 }
283 }
284
285 read_unlock(&local->sockets.lock);
286
287 pr_debug("Found llcp sock %p\n", llcp_sock);
288
289 return llcp_sock;
290}
291
Samuel Ortizd6469602011-12-14 16:43:12 +0100292u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100293 struct nfc_llcp_sock *sock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100294{
295 mutex_lock(&local->sdp_lock);
296
297 if (sock->service_name != NULL && sock->service_name_len > 0) {
298 int ssap = nfc_llcp_wks_sap(sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100299 sock->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100300
301 if (ssap > 0) {
302 pr_debug("WKS %d\n", ssap);
303
304 /* This is a WKS, let's check if it's free */
305 if (local->local_wks & BIT(ssap)) {
306 mutex_unlock(&local->sdp_lock);
307
308 return LLCP_SAP_MAX;
309 }
310
Samuel Ortiz1762c172012-03-05 01:03:38 +0100311 set_bit(ssap, &local->local_wks);
Samuel Ortizd6469602011-12-14 16:43:12 +0100312 mutex_unlock(&local->sdp_lock);
313
314 return ssap;
315 }
316
317 /*
Samuel Ortiz8f500202012-06-25 15:46:28 +0200318 * Check if there already is a non WKS socket bound
319 * to this service name.
Samuel Ortizd6469602011-12-14 16:43:12 +0100320 */
Samuel Ortiz8f500202012-06-25 15:46:28 +0200321 if (nfc_llcp_sock_from_sn(local, sock->service_name,
322 sock->service_name_len) != NULL) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100323 mutex_unlock(&local->sdp_lock);
324
325 return LLCP_SAP_MAX;
326 }
327
Samuel Ortizd6469602011-12-14 16:43:12 +0100328 mutex_unlock(&local->sdp_lock);
329
Samuel Ortiz8f500202012-06-25 15:46:28 +0200330 return LLCP_SDP_UNBOUND;
Samuel Ortizd6469602011-12-14 16:43:12 +0100331
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200332 } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
333 if (!test_bit(sock->ssap, &local->local_wks)) {
334 set_bit(sock->ssap, &local->local_wks);
335 mutex_unlock(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100336
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200337 return sock->ssap;
Samuel Ortizd6469602011-12-14 16:43:12 +0100338 }
339 }
340
341 mutex_unlock(&local->sdp_lock);
342
343 return LLCP_SAP_MAX;
344}
345
346u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
347{
348 u8 local_ssap;
349
350 mutex_lock(&local->sdp_lock);
351
352 local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
353 if (local_ssap == LLCP_LOCAL_NUM_SAP) {
354 mutex_unlock(&local->sdp_lock);
355 return LLCP_SAP_MAX;
356 }
357
Samuel Ortiz1762c172012-03-05 01:03:38 +0100358 set_bit(local_ssap, &local->local_sap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100359
360 mutex_unlock(&local->sdp_lock);
361
362 return local_ssap + LLCP_LOCAL_SAP_OFFSET;
363}
364
365void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
366{
367 u8 local_ssap;
368 unsigned long *sdp;
369
370 if (ssap < LLCP_WKS_NUM_SAP) {
371 local_ssap = ssap;
372 sdp = &local->local_wks;
373 } else if (ssap < LLCP_LOCAL_NUM_SAP) {
Samuel Ortiz8f500202012-06-25 15:46:28 +0200374 atomic_t *client_cnt;
375
Samuel Ortizd6469602011-12-14 16:43:12 +0100376 local_ssap = ssap - LLCP_WKS_NUM_SAP;
377 sdp = &local->local_sdp;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200378 client_cnt = &local->local_sdp_cnt[local_ssap];
379
380 pr_debug("%d clients\n", atomic_read(client_cnt));
381
382 mutex_lock(&local->sdp_lock);
383
384 if (atomic_dec_and_test(client_cnt)) {
385 struct nfc_llcp_sock *l_sock;
386
387 pr_debug("No more clients for SAP %d\n", ssap);
388
389 clear_bit(local_ssap, sdp);
390
391 /* Find the listening sock and set it back to UNBOUND */
392 l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
393 if (l_sock) {
394 l_sock->ssap = LLCP_SDP_UNBOUND;
395 nfc_llcp_sock_put(l_sock);
396 }
397 }
398
399 mutex_unlock(&local->sdp_lock);
400
401 return;
Samuel Ortizd6469602011-12-14 16:43:12 +0100402 } else if (ssap < LLCP_MAX_SAP) {
403 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
404 sdp = &local->local_sap;
405 } else {
406 return;
407 }
408
409 mutex_lock(&local->sdp_lock);
410
Samuel Ortiz1762c172012-03-05 01:03:38 +0100411 clear_bit(local_ssap, sdp);
Samuel Ortizd6469602011-12-14 16:43:12 +0100412
413 mutex_unlock(&local->sdp_lock);
414}
415
Samuel Ortiz8f500202012-06-25 15:46:28 +0200416static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
417{
418 u8 ssap;
419
420 mutex_lock(&local->sdp_lock);
421
422 ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
423 if (ssap == LLCP_SDP_NUM_SAP) {
424 mutex_unlock(&local->sdp_lock);
425
426 return LLCP_SAP_MAX;
427 }
428
429 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
430
431 set_bit(ssap, &local->local_sdp);
432
433 mutex_unlock(&local->sdp_lock);
434
435 return LLCP_WKS_NUM_SAP + ssap;
436}
437
Samuel Ortizd6469602011-12-14 16:43:12 +0100438static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
439{
440 u8 *gb_cur, *version_tlv, version, version_length;
441 u8 *lto_tlv, lto, lto_length;
442 u8 *wks_tlv, wks_length;
Samuel Ortiz56d58762012-04-10 19:43:19 +0200443 u8 *miux_tlv, miux_length;
444 __be16 miux;
Samuel Ortizd6469602011-12-14 16:43:12 +0100445 u8 gb_len = 0;
Wei Yongjun52da2442012-09-02 21:21:46 +0800446 int ret = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100447
448 version = LLCP_VERSION_11;
449 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100450 1, &version_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100451 gb_len += version_length;
452
453 /* 1500 ms */
454 lto = 150;
Samuel Ortiz91b0ade2012-04-10 19:43:20 +0200455 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &lto, 1, &lto_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100456 gb_len += lto_length;
457
458 pr_debug("Local wks 0x%lx\n", local->local_wks);
459 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100460 &wks_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100461 gb_len += wks_length;
462
Samuel Ortiz56d58762012-04-10 19:43:19 +0200463 miux = cpu_to_be16(LLCP_MAX_MIUX);
464 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
465 &miux_length);
466 gb_len += miux_length;
467
Samuel Ortizd6469602011-12-14 16:43:12 +0100468 gb_len += ARRAY_SIZE(llcp_magic);
469
470 if (gb_len > NFC_MAX_GT_LEN) {
Wei Yongjun52da2442012-09-02 21:21:46 +0800471 ret = -EINVAL;
472 goto out;
Samuel Ortizd6469602011-12-14 16:43:12 +0100473 }
474
475 gb_cur = local->gb;
476
477 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
478 gb_cur += ARRAY_SIZE(llcp_magic);
479
480 memcpy(gb_cur, version_tlv, version_length);
481 gb_cur += version_length;
482
483 memcpy(gb_cur, lto_tlv, lto_length);
484 gb_cur += lto_length;
485
486 memcpy(gb_cur, wks_tlv, wks_length);
487 gb_cur += wks_length;
488
Samuel Ortiz56d58762012-04-10 19:43:19 +0200489 memcpy(gb_cur, miux_tlv, miux_length);
490 gb_cur += miux_length;
491
Samuel Ortizd6469602011-12-14 16:43:12 +0100492 local->gb_len = gb_len;
493
Wei Yongjun52da2442012-09-02 21:21:46 +0800494out:
495 kfree(version_tlv);
496 kfree(lto_tlv);
497 kfree(wks_tlv);
498 kfree(miux_tlv);
499
500 return ret;
Samuel Ortizd6469602011-12-14 16:43:12 +0100501}
502
Samuel Ortizb8e7a062012-06-22 02:04:53 +0200503u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
504{
505 struct nfc_llcp_local *local;
506
507 local = nfc_llcp_find_local(dev);
508 if (local == NULL) {
509 *general_bytes_len = 0;
510 return NULL;
511 }
512
513 nfc_llcp_build_gb(local);
514
515 *general_bytes_len = local->gb_len;
516
517 return local->gb;
518}
519
Samuel Ortizd6469602011-12-14 16:43:12 +0100520int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
521{
522 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
523
524 if (local == NULL) {
525 pr_err("No LLCP device\n");
526 return -ENODEV;
527 }
528
529 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
530 memcpy(local->remote_gb, gb, gb_len);
531 local->remote_gb_len = gb_len;
532
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100533 if (local->remote_gb == NULL || local->remote_gb_len == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100534 return -ENODEV;
535
536 if (memcmp(local->remote_gb, llcp_magic, 3)) {
537 pr_err("MAC does not support LLCP\n");
538 return -EINVAL;
539 }
540
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200541 return nfc_llcp_parse_gb_tlv(local,
542 &local->remote_gb[3],
543 local->remote_gb_len - 3);
Samuel Ortizd6469602011-12-14 16:43:12 +0100544}
545
Samuel Ortizd6469602011-12-14 16:43:12 +0100546static u8 nfc_llcp_dsap(struct sk_buff *pdu)
547{
548 return (pdu->data[0] & 0xfc) >> 2;
549}
550
551static u8 nfc_llcp_ptype(struct sk_buff *pdu)
552{
553 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
554}
555
556static u8 nfc_llcp_ssap(struct sk_buff *pdu)
557{
558 return pdu->data[1] & 0x3f;
559}
560
561static u8 nfc_llcp_ns(struct sk_buff *pdu)
562{
563 return pdu->data[2] >> 4;
564}
565
566static u8 nfc_llcp_nr(struct sk_buff *pdu)
567{
568 return pdu->data[2] & 0xf;
569}
570
571static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
572{
Samuel Ortiz279cf172012-04-10 19:43:14 +0200573 pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
Samuel Ortizd6469602011-12-14 16:43:12 +0100574 sock->send_n = (sock->send_n + 1) % 16;
575 sock->recv_ack_n = (sock->recv_n - 1) % 16;
576}
577
Thierry Escande44635232012-09-26 18:16:44 +0200578void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
579 struct sk_buff *skb, u8 direction)
580{
581 struct hlist_node *node;
582 struct sk_buff *skb_copy = NULL, *nskb;
583 struct sock *sk;
584 u8 *data;
585
586 read_lock(&local->raw_sockets.lock);
587
588 sk_for_each(sk, node, &local->raw_sockets.head) {
589 if (sk->sk_state != LLCP_BOUND)
590 continue;
591
592 if (skb_copy == NULL) {
593 skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE,
594 GFP_ATOMIC);
595
596 if (skb_copy == NULL)
597 continue;
598
599 data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE);
600
601 data[0] = local->dev ? local->dev->idx : 0xFF;
602 data[1] = direction;
603 }
604
605 nskb = skb_clone(skb_copy, GFP_ATOMIC);
606 if (!nskb)
607 continue;
608
609 if (sock_queue_rcv_skb(sk, nskb))
610 kfree_skb(nskb);
611 }
612
613 read_unlock(&local->raw_sockets.lock);
614
615 kfree_skb(skb_copy);
616}
617
Samuel Ortiz84457962012-05-30 17:48:29 +0200618static void nfc_llcp_tx_work(struct work_struct *work)
619{
620 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
621 tx_work);
622 struct sk_buff *skb;
623 struct sock *sk;
624 struct nfc_llcp_sock *llcp_sock;
625
626 skb = skb_dequeue(&local->tx_queue);
627 if (skb != NULL) {
628 sk = skb->sk;
629 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiza6a09152012-10-05 00:37:22 +0200630
631 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
632 nfc_llcp_send_symm(local->dev);
633 } else {
Samuel Ortiz84457962012-05-30 17:48:29 +0200634 int ret;
635
636 pr_debug("Sending pending skb\n");
637 print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
638 DUMP_PREFIX_OFFSET, 16, 1,
639 skb->data, skb->len, true);
640
Thierry Escande44635232012-09-26 18:16:44 +0200641 nfc_llcp_send_to_raw_sock(local, skb,
642 NFC_LLCP_DIRECTION_TX);
643
Samuel Ortiz84457962012-05-30 17:48:29 +0200644 ret = nfc_data_exchange(local->dev, local->target_idx,
645 skb, nfc_llcp_recv, local);
646
647 if (!ret && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
648 skb = skb_get(skb);
649 skb_queue_tail(&llcp_sock->tx_pending_queue,
650 skb);
651 }
Samuel Ortiz84457962012-05-30 17:48:29 +0200652 }
653 } else {
654 nfc_llcp_send_symm(local->dev);
655 }
656
657 mod_timer(&local->link_timer,
658 jiffies + msecs_to_jiffies(2 * local->remote_lto));
659}
660
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200661static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
662 u8 ssap)
663{
664 struct sock *sk;
665 struct nfc_llcp_sock *llcp_sock;
666 struct hlist_node *node;
667
668 read_lock(&local->connecting_sockets.lock);
669
670 sk_for_each(sk, node, &local->connecting_sockets.head) {
671 llcp_sock = nfc_llcp_sock(sk);
672
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200673 if (llcp_sock->ssap == ssap) {
674 sock_hold(&llcp_sock->sk);
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200675 goto out;
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200676 }
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200677 }
678
679 llcp_sock = NULL;
680
681out:
682 read_unlock(&local->connecting_sockets.lock);
683
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200684 return llcp_sock;
685}
686
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200687static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
688 u8 *sn, size_t sn_len)
689{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200690 struct nfc_llcp_sock *llcp_sock;
691
Samuel Ortiz8f500202012-06-25 15:46:28 +0200692 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100693
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200694 if (llcp_sock == NULL)
695 return NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100696
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200697 sock_hold(&llcp_sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100698
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200699 return llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +0100700}
701
Samuel Ortizd6469602011-12-14 16:43:12 +0100702static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
703{
704 u8 *tlv = &skb->data[2], type, length;
705 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
706
707 while (offset < tlv_array_len) {
708 type = tlv[0];
709 length = tlv[1];
710
711 pr_debug("type 0x%x length %d\n", type, length);
712
713 if (type == LLCP_TLV_SN) {
714 *sn_len = length;
715 return &tlv[2];
716 }
717
718 offset += length + 2;
719 tlv += length + 2;
720 }
721
722 return NULL;
723}
724
Samuel Ortiz968272b2012-10-15 16:14:37 +0200725static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
726 struct sk_buff *skb)
727{
728 struct nfc_llcp_sock *llcp_sock;
729 struct nfc_llcp_ui_cb *ui_cb;
730 u8 dsap, ssap;
731
732 dsap = nfc_llcp_dsap(skb);
733 ssap = nfc_llcp_ssap(skb);
734
735 ui_cb = nfc_llcp_ui_skb_cb(skb);
736 ui_cb->dsap = dsap;
737 ui_cb->ssap = ssap;
738
739 printk("%s %d %d\n", __func__, dsap, ssap);
740
741 pr_debug("%d %d\n", dsap, ssap);
742
743 /* We're looking for a bound socket, not a client one */
744 llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
745 if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
746 return;
747
748 /* There is no sequence with UI frames */
749 skb_pull(skb, LLCP_HEADER_SIZE);
750 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
751 pr_err("receive queue is full\n");
752 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
753 }
754
755 nfc_llcp_sock_put(llcp_sock);
756}
757
Samuel Ortizd6469602011-12-14 16:43:12 +0100758static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100759 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100760{
761 struct sock *new_sk, *parent;
762 struct nfc_llcp_sock *sock, *new_sock;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200763 u8 dsap, ssap, reason;
Samuel Ortizd6469602011-12-14 16:43:12 +0100764
765 dsap = nfc_llcp_dsap(skb);
766 ssap = nfc_llcp_ssap(skb);
767
768 pr_debug("%d %d\n", dsap, ssap);
769
Samuel Ortizd6469602011-12-14 16:43:12 +0100770 if (dsap != LLCP_SAP_SDP) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200771 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
772 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100773 reason = LLCP_DM_NOBOUND;
774 goto fail;
775 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100776 } else {
777 u8 *sn;
778 size_t sn_len;
779
780 sn = nfc_llcp_connect_sn(skb, &sn_len);
781 if (sn == NULL) {
782 reason = LLCP_DM_NOBOUND;
783 goto fail;
784 }
785
786 pr_debug("Service name length %zu\n", sn_len);
787
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200788 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
789 if (sock == NULL) {
790 reason = LLCP_DM_NOBOUND;
791 goto fail;
Samuel Ortizd6469602011-12-14 16:43:12 +0100792 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100793 }
794
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200795 lock_sock(&sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100796
Samuel Ortizd6469602011-12-14 16:43:12 +0100797 parent = &sock->sk;
798
799 if (sk_acceptq_is_full(parent)) {
800 reason = LLCP_DM_REJ;
801 release_sock(&sock->sk);
802 sock_put(&sock->sk);
803 goto fail;
804 }
805
Samuel Ortiz8f500202012-06-25 15:46:28 +0200806 if (sock->ssap == LLCP_SDP_UNBOUND) {
807 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
808
809 pr_debug("First client, reserving %d\n", ssap);
810
811 if (ssap == LLCP_SAP_MAX) {
812 reason = LLCP_DM_REJ;
813 release_sock(&sock->sk);
814 sock_put(&sock->sk);
815 goto fail;
816 }
817
818 sock->ssap = ssap;
819 }
820
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100821 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC);
Samuel Ortizd6469602011-12-14 16:43:12 +0100822 if (new_sk == NULL) {
823 reason = LLCP_DM_REJ;
824 release_sock(&sock->sk);
825 sock_put(&sock->sk);
826 goto fail;
827 }
828
829 new_sock = nfc_llcp_sock(new_sk);
830 new_sock->dev = local->dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200831 new_sock->local = nfc_llcp_local_get(local);
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200832 new_sock->miu = local->remote_miu;
Samuel Ortizd6469602011-12-14 16:43:12 +0100833 new_sock->nfc_protocol = sock->nfc_protocol;
Samuel Ortizd6469602011-12-14 16:43:12 +0100834 new_sock->dsap = ssap;
Samuel Ortiz025f1522012-06-18 21:38:09 +0200835 new_sock->target_idx = local->target_idx;
Samuel Ortizd6469602011-12-14 16:43:12 +0100836 new_sock->parent = parent;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200837 new_sock->ssap = sock->ssap;
838 if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
839 atomic_t *client_count;
840
841 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
842
843 client_count =
844 &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
845
846 atomic_inc(client_count);
847 new_sock->reserved_ssap = sock->ssap;
848 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100849
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200850 nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
851 skb->len - LLCP_HEADER_SIZE);
852
Samuel Ortizd6469602011-12-14 16:43:12 +0100853 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
854
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200855 nfc_llcp_sock_link(&local->sockets, new_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100856
857 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
858
859 nfc_get_device(local->dev->idx);
860
861 new_sk->sk_state = LLCP_CONNECTED;
862
863 /* Wake the listening processes */
864 parent->sk_data_ready(parent, 0);
865
866 /* Send CC */
867 nfc_llcp_send_cc(new_sock);
868
869 release_sock(&sock->sk);
870 sock_put(&sock->sk);
871
872 return;
873
874fail:
875 /* Send DM */
876 nfc_llcp_send_dm(local, dsap, ssap, reason);
Samuel Ortizd6469602011-12-14 16:43:12 +0100877}
878
Samuel Ortizd094afa2012-03-05 01:03:42 +0100879int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100880{
Samuel Ortizd094afa2012-03-05 01:03:42 +0100881 int nr_frames = 0;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100882 struct nfc_llcp_local *local = sock->local;
883
884 pr_debug("Remote ready %d tx queue len %d remote rw %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100885 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200886 sock->rw);
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100887
888 /* Try to queue some I frames for transmission */
889 while (sock->remote_ready &&
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200890 skb_queue_len(&sock->tx_pending_queue) < sock->rw) {
Samuel Ortiz84457962012-05-30 17:48:29 +0200891 struct sk_buff *pdu;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100892
893 pdu = skb_dequeue(&sock->tx_queue);
894 if (pdu == NULL)
895 break;
896
897 /* Update N(S)/N(R) */
898 nfc_llcp_set_nrns(sock, pdu);
899
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100900 skb_queue_tail(&local->tx_queue, pdu);
Samuel Ortizd094afa2012-03-05 01:03:42 +0100901 nr_frames++;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100902 }
Samuel Ortizd094afa2012-03-05 01:03:42 +0100903
904 return nr_frames;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100905}
906
Samuel Ortizd6469602011-12-14 16:43:12 +0100907static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100908 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100909{
910 struct nfc_llcp_sock *llcp_sock;
911 struct sock *sk;
912 u8 dsap, ssap, ptype, ns, nr;
913
914 ptype = nfc_llcp_ptype(skb);
915 dsap = nfc_llcp_dsap(skb);
916 ssap = nfc_llcp_ssap(skb);
917 ns = nfc_llcp_ns(skb);
918 nr = nfc_llcp_nr(skb);
919
920 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
921
922 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
923 if (llcp_sock == NULL) {
924 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
925 return;
926 }
927
928 sk = &llcp_sock->sk;
929 lock_sock(sk);
930 if (sk->sk_state == LLCP_CLOSED) {
931 release_sock(sk);
932 nfc_llcp_sock_put(llcp_sock);
933 }
934
Samuel Ortizd6469602011-12-14 16:43:12 +0100935 /* Pass the payload upstream */
936 if (ptype == LLCP_PDU_I) {
937 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
938
Samuel Ortiz53aef922012-03-05 01:03:36 +0100939 if (ns == llcp_sock->recv_n)
940 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
941 else
942 pr_err("Received out of sequence I PDU\n");
943
Samuel Ortizd6469602011-12-14 16:43:12 +0100944 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
945 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
946 pr_err("receive queue is full\n");
947 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
948 }
949 }
950
951 /* Remove skbs from the pending queue */
952 if (llcp_sock->send_ack_n != nr) {
953 struct sk_buff *s, *tmp;
954
955 llcp_sock->send_ack_n = nr;
956
Samuel Ortiz84457962012-05-30 17:48:29 +0200957 /* Remove and free all skbs until ns == nr */
958 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
959 skb_unlink(s, &llcp_sock->tx_pending_queue);
960 kfree_skb(s);
961
962 if (nfc_llcp_ns(s) == nr)
963 break;
964 }
965
966 /* Re-queue the remaining skbs for transmission */
967 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
968 s, tmp) {
969 skb_unlink(s, &llcp_sock->tx_pending_queue);
970 skb_queue_head(&local->tx_queue, s);
971 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100972 }
973
Samuel Ortiz53aef922012-03-05 01:03:36 +0100974 if (ptype == LLCP_PDU_RR)
975 llcp_sock->remote_ready = true;
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100976 else if (ptype == LLCP_PDU_RNR)
Samuel Ortiz53aef922012-03-05 01:03:36 +0100977 llcp_sock->remote_ready = false;
978
Samuel Ortiz56af2562012-05-30 18:06:11 +0200979 if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
Samuel Ortizd094afa2012-03-05 01:03:42 +0100980 nfc_llcp_send_rr(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100981
982 release_sock(sk);
983 nfc_llcp_sock_put(llcp_sock);
984}
985
986static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100987 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100988{
989 struct nfc_llcp_sock *llcp_sock;
990 struct sock *sk;
991 u8 dsap, ssap;
992
993 dsap = nfc_llcp_dsap(skb);
994 ssap = nfc_llcp_ssap(skb);
995
996 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
997 if (llcp_sock == NULL) {
998 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
999 return;
1000 }
1001
1002 sk = &llcp_sock->sk;
1003 lock_sock(sk);
1004 if (sk->sk_state == LLCP_CLOSED) {
1005 release_sock(sk);
1006 nfc_llcp_sock_put(llcp_sock);
1007 }
1008
Samuel Ortizd6469602011-12-14 16:43:12 +01001009 if (sk->sk_state == LLCP_CONNECTED) {
1010 nfc_put_device(local->dev);
1011 sk->sk_state = LLCP_CLOSED;
1012 sk->sk_state_change(sk);
1013 }
1014
1015 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
1016
1017 release_sock(sk);
1018 nfc_llcp_sock_put(llcp_sock);
1019}
1020
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001021static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001022{
1023 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizff353d82012-05-07 12:31:19 +02001024 struct sock *sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001025 u8 dsap, ssap;
1026
Samuel Ortizd6469602011-12-14 16:43:12 +01001027 dsap = nfc_llcp_dsap(skb);
1028 ssap = nfc_llcp_ssap(skb);
1029
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001030 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
Samuel Ortizd6469602011-12-14 16:43:12 +01001031 if (llcp_sock == NULL) {
1032 pr_err("Invalid CC\n");
1033 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1034
1035 return;
1036 }
1037
Samuel Ortizff353d82012-05-07 12:31:19 +02001038 sk = &llcp_sock->sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001039
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001040 /* Unlink from connecting and link to the client array */
1041 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
1042 nfc_llcp_sock_link(&local->sockets, sk);
1043 llcp_sock->dsap = ssap;
1044
Samuel Ortiz7a06e582012-05-07 22:03:34 +02001045 nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
1046 skb->len - LLCP_HEADER_SIZE);
Samuel Ortizd6469602011-12-14 16:43:12 +01001047
Samuel Ortizff353d82012-05-07 12:31:19 +02001048 sk->sk_state = LLCP_CONNECTED;
1049 sk->sk_state_change(sk);
1050
Samuel Ortizd6469602011-12-14 16:43:12 +01001051 nfc_llcp_sock_put(llcp_sock);
1052}
1053
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001054static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1055{
1056 struct nfc_llcp_sock *llcp_sock;
1057 struct sock *sk;
1058 u8 dsap, ssap, reason;
1059
1060 dsap = nfc_llcp_dsap(skb);
1061 ssap = nfc_llcp_ssap(skb);
1062 reason = skb->data[2];
1063
1064 pr_debug("%d %d reason %d\n", ssap, dsap, reason);
1065
1066 switch (reason) {
1067 case LLCP_DM_NOBOUND:
1068 case LLCP_DM_REJ:
1069 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1070 break;
1071
1072 default:
1073 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1074 break;
1075 }
1076
1077 if (llcp_sock == NULL) {
Samuel Ortiza8df0f32012-10-16 21:15:59 +02001078 pr_debug("Already closed\n");
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001079 return;
1080 }
1081
1082 sk = &llcp_sock->sk;
1083
1084 sk->sk_err = ENXIO;
1085 sk->sk_state = LLCP_CLOSED;
1086 sk->sk_state_change(sk);
1087
1088 nfc_llcp_sock_put(llcp_sock);
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001089}
1090
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001091static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1092 struct sk_buff *skb)
1093{
1094 struct nfc_llcp_sock *llcp_sock;
1095 u8 dsap, ssap, *tlv, type, length, tid, sap;
1096 u16 tlv_len, offset;
1097 char *service_name;
1098 size_t service_name_len;
1099
1100 dsap = nfc_llcp_dsap(skb);
1101 ssap = nfc_llcp_ssap(skb);
1102
1103 pr_debug("%d %d\n", dsap, ssap);
1104
1105 if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1106 pr_err("Wrong SNL SAP\n");
1107 return;
1108 }
1109
1110 tlv = &skb->data[LLCP_HEADER_SIZE];
1111 tlv_len = skb->len - LLCP_HEADER_SIZE;
1112 offset = 0;
1113
1114 while(offset < tlv_len) {
1115 type = tlv[0];
1116 length = tlv[1];
1117
1118 switch (type) {
1119 case LLCP_TLV_SDREQ:
1120 tid = tlv[2];
1121 service_name = (char *) &tlv[3];
1122 service_name_len = length - 1;
1123
Samuel Ortize6904082012-10-15 14:28:13 +02001124 pr_debug("Looking for %.16s\n", service_name);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001125
1126 if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1127 !strncmp(service_name, "urn:nfc:sn:sdp",
1128 service_name_len)) {
1129 sap = 1;
Samuel Ortize6904082012-10-15 14:28:13 +02001130 goto send_snl;
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001131 }
1132
Samuel Ortize6904082012-10-15 14:28:13 +02001133 llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
1134 service_name_len);
1135 if (!llcp_sock) {
1136 sap = 0;
1137 goto send_snl;
1138 }
1139
1140 /*
1141 * We found a socket but its ssap has not been reserved
1142 * yet. We need to assign it for good and send a reply.
1143 * The ssap will be freed when the socket is closed.
1144 */
1145 if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1146 atomic_t *client_count;
1147
1148 sap = nfc_llcp_reserve_sdp_ssap(local);
1149
1150 pr_debug("Reserving %d\n", sap);
1151
1152 if (sap == LLCP_SAP_MAX) {
1153 sap = 0;
1154 goto send_snl;
1155 }
1156
1157 client_count =
1158 &local->local_sdp_cnt[sap -
1159 LLCP_WKS_NUM_SAP];
1160
1161 atomic_inc(client_count);
1162
1163 llcp_sock->ssap = sap;
1164 llcp_sock->reserved_ssap = sap;
1165 } else {
1166 sap = llcp_sock->ssap;
1167 }
1168
1169 pr_debug("%p %d\n", llcp_sock, sap);
1170
1171 send_snl:
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001172 nfc_llcp_send_snl(local, tid, sap);
1173 break;
1174
1175 default:
1176 pr_err("Invalid SNL tlv value 0x%x\n", type);
1177 break;
1178 }
1179
1180 offset += length + 2;
1181 tlv += length + 2;
1182 }
1183}
1184
Samuel Ortizd6469602011-12-14 16:43:12 +01001185static void nfc_llcp_rx_work(struct work_struct *work)
1186{
1187 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001188 rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001189 u8 dsap, ssap, ptype;
1190 struct sk_buff *skb;
1191
1192 skb = local->rx_pending;
1193 if (skb == NULL) {
1194 pr_debug("No pending SKB\n");
1195 return;
1196 }
1197
1198 ptype = nfc_llcp_ptype(skb);
1199 dsap = nfc_llcp_dsap(skb);
1200 ssap = nfc_llcp_ssap(skb);
1201
1202 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1203
Samuel Ortiz4be646e2012-04-10 19:43:13 +02001204 if (ptype != LLCP_PDU_SYMM)
1205 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
1206 16, 1, skb->data, skb->len, true);
1207
Thierry Escande44635232012-09-26 18:16:44 +02001208 nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX);
1209
Samuel Ortizd6469602011-12-14 16:43:12 +01001210 switch (ptype) {
1211 case LLCP_PDU_SYMM:
1212 pr_debug("SYMM\n");
1213 break;
1214
Samuel Ortiz968272b2012-10-15 16:14:37 +02001215 case LLCP_PDU_UI:
1216 pr_debug("UI\n");
1217 nfc_llcp_recv_ui(local, skb);
1218 break;
1219
Samuel Ortizd6469602011-12-14 16:43:12 +01001220 case LLCP_PDU_CONNECT:
1221 pr_debug("CONNECT\n");
1222 nfc_llcp_recv_connect(local, skb);
1223 break;
1224
1225 case LLCP_PDU_DISC:
1226 pr_debug("DISC\n");
1227 nfc_llcp_recv_disc(local, skb);
1228 break;
1229
1230 case LLCP_PDU_CC:
1231 pr_debug("CC\n");
1232 nfc_llcp_recv_cc(local, skb);
1233 break;
1234
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001235 case LLCP_PDU_DM:
1236 pr_debug("DM\n");
1237 nfc_llcp_recv_dm(local, skb);
1238 break;
1239
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001240 case LLCP_PDU_SNL:
1241 pr_debug("SNL\n");
1242 nfc_llcp_recv_snl(local, skb);
1243 break;
1244
Samuel Ortizd6469602011-12-14 16:43:12 +01001245 case LLCP_PDU_I:
1246 case LLCP_PDU_RR:
Samuel Ortiz53aef922012-03-05 01:03:36 +01001247 case LLCP_PDU_RNR:
Samuel Ortizd6469602011-12-14 16:43:12 +01001248 pr_debug("I frame\n");
1249 nfc_llcp_recv_hdlc(local, skb);
1250 break;
1251
1252 }
1253
Linus Torvalds916082b2012-10-02 16:01:31 -07001254 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001255 kfree_skb(local->rx_pending);
1256 local->rx_pending = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +01001257}
1258
1259void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1260{
1261 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1262
1263 pr_debug("Received an LLCP PDU\n");
1264 if (err < 0) {
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001265 pr_err("err %d\n", err);
Samuel Ortizd6469602011-12-14 16:43:12 +01001266 return;
1267 }
1268
1269 local->rx_pending = skb_get(skb);
1270 del_timer(&local->link_timer);
Linus Torvalds916082b2012-10-02 16:01:31 -07001271 schedule_work(&local->rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001272}
1273
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001274int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1275{
1276 struct nfc_llcp_local *local;
1277
1278 local = nfc_llcp_find_local(dev);
1279 if (local == NULL)
1280 return -ENODEV;
1281
1282 local->rx_pending = skb_get(skb);
1283 del_timer(&local->link_timer);
Linus Torvalds916082b2012-10-02 16:01:31 -07001284 schedule_work(&local->rx_work);
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001285
1286 return 0;
1287}
1288
Samuel Ortizd6469602011-12-14 16:43:12 +01001289void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1290{
1291 struct nfc_llcp_local *local;
1292
1293 local = nfc_llcp_find_local(dev);
1294 if (local == NULL)
1295 return;
1296
1297 /* Close and purge all existing sockets */
Samuel Ortiz4d22ea12012-06-22 14:40:34 +02001298 nfc_llcp_socket_release(local, true);
Samuel Ortizd6469602011-12-14 16:43:12 +01001299}
1300
1301void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1302 u8 comm_mode, u8 rf_mode)
1303{
1304 struct nfc_llcp_local *local;
1305
1306 pr_debug("rf mode %d\n", rf_mode);
1307
1308 local = nfc_llcp_find_local(dev);
1309 if (local == NULL)
1310 return;
1311
1312 local->target_idx = target_idx;
1313 local->comm_mode = comm_mode;
1314 local->rf_mode = rf_mode;
1315
1316 if (rf_mode == NFC_RF_INITIATOR) {
1317 pr_debug("Queueing Tx work\n");
1318
Linus Torvalds916082b2012-10-02 16:01:31 -07001319 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001320 } else {
1321 mod_timer(&local->link_timer,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001322 jiffies + msecs_to_jiffies(local->remote_lto));
Samuel Ortizd6469602011-12-14 16:43:12 +01001323 }
1324}
1325
1326int nfc_llcp_register_device(struct nfc_dev *ndev)
1327{
Samuel Ortizd6469602011-12-14 16:43:12 +01001328 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +01001329
1330 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
1331 if (local == NULL)
1332 return -ENOMEM;
1333
1334 local->dev = ndev;
1335 INIT_LIST_HEAD(&local->list);
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001336 kref_init(&local->ref);
Samuel Ortizd6469602011-12-14 16:43:12 +01001337 mutex_init(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001338 init_timer(&local->link_timer);
1339 local->link_timer.data = (unsigned long) local;
1340 local->link_timer.function = nfc_llcp_symm_timer;
1341
1342 skb_queue_head_init(&local->tx_queue);
1343 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001344
1345 local->rx_pending = NULL;
1346 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001347
1348 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001349
Szymon Jancfe235b52012-09-25 12:42:50 +02001350 rwlock_init(&local->sockets.lock);
1351 rwlock_init(&local->connecting_sockets.lock);
Thierry Escande44635232012-09-26 18:16:44 +02001352 rwlock_init(&local->raw_sockets.lock);
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001353
Samuel Ortizd6469602011-12-14 16:43:12 +01001354 nfc_llcp_build_gb(local);
1355
1356 local->remote_miu = LLCP_DEFAULT_MIU;
1357 local->remote_lto = LLCP_DEFAULT_LTO;
Samuel Ortizd6469602011-12-14 16:43:12 +01001358
1359 list_add(&llcp_devices, &local->list);
1360
1361 return 0;
Samuel Ortizd6469602011-12-14 16:43:12 +01001362}
1363
1364void nfc_llcp_unregister_device(struct nfc_dev *dev)
1365{
1366 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1367
1368 if (local == NULL) {
1369 pr_debug("No such device\n");
1370 return;
1371 }
1372
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001373 nfc_llcp_local_put(local);
Samuel Ortizd6469602011-12-14 16:43:12 +01001374}
1375
1376int __init nfc_llcp_init(void)
1377{
1378 INIT_LIST_HEAD(&llcp_devices);
1379
1380 return nfc_llcp_sock_init();
1381}
1382
1383void nfc_llcp_exit(void)
1384{
1385 nfc_llcp_sock_exit();
1386}