blob: 98876274a1eea22b709403dde2c5702b1387a0be [file] [log] [blame]
Samuel Ortizd6469602011-12-14 16:43:12 +01001/*
2 * Copyright (C) 2011 Intel Corporation. All rights reserved.
Julien Lefrique966efbf2014-10-21 16:52:50 +02003 * Copyright (C) 2014 Marvell International Ltd.
Samuel Ortizd6469602011-12-14 16:43:12 +01004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Jeff Kirsher98b32de2013-12-06 08:56:16 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Samuel Ortizd6469602011-12-14 16:43:12 +010017 */
18
19#define pr_fmt(fmt) "llcp: %s: " fmt, __func__
20
21#include <linux/init.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
24#include <linux/nfc.h>
25
Samuel Ortiz30cc4582013-04-26 11:49:40 +020026#include "nfc.h"
Samuel Ortizd6469602011-12-14 16:43:12 +010027#include "llcp.h"
28
29static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
30
Axel Lin0b51fc52014-02-22 22:14:18 +080031static LIST_HEAD(llcp_devices);
Samuel Ortizd6469602011-12-14 16:43:12 +010032
Thierry Escande098dafc2013-03-29 11:47:43 +010033static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb);
34
Samuel Ortiza69f32a2012-05-04 17:04:19 +020035void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk)
36{
37 write_lock(&l->lock);
38 sk_add_node(sk, &l->head);
39 write_unlock(&l->lock);
40}
41
42void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk)
43{
44 write_lock(&l->lock);
45 sk_del_node_init(sk);
46 write_unlock(&l->lock);
47}
48
Thierry Escandeabd18d42013-04-02 10:25:15 +020049void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock *sock)
50{
51 sock->remote_rw = LLCP_DEFAULT_RW;
52 sock->remote_miu = LLCP_MAX_MIU + 1;
53}
54
Samuel Ortizf31652a2012-10-26 18:20:10 +020055static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock)
56{
57 struct nfc_llcp_local *local = sock->local;
58 struct sk_buff *s, *tmp;
59
60 pr_debug("%p\n", &sock->sk);
61
62 skb_queue_purge(&sock->tx_queue);
63 skb_queue_purge(&sock->tx_pending_queue);
Samuel Ortizf31652a2012-10-26 18:20:10 +020064
65 if (local == NULL)
66 return;
67
68 /* Search for local pending SKBs that are related to this socket */
69 skb_queue_walk_safe(&local->tx_queue, s, tmp) {
70 if (s->sk != &sock->sk)
71 continue;
72
73 skb_unlink(s, &local->tx_queue);
74 kfree_skb(s);
75 }
76}
77
Samuel Ortizb436a132013-04-03 16:34:19 +020078static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device,
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +010079 int err)
Samuel Ortizd6469602011-12-14 16:43:12 +010080{
Samuel Ortiza69f32a2012-05-04 17:04:19 +020081 struct sock *sk;
Sasha Levinb67bfe02013-02-27 17:06:00 -080082 struct hlist_node *tmp;
Samuel Ortiza69f32a2012-05-04 17:04:19 +020083 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +010084
Samuel Ortizf31652a2012-10-26 18:20:10 +020085 skb_queue_purge(&local->tx_queue);
86
Samuel Ortiza69f32a2012-05-04 17:04:19 +020087 write_lock(&local->sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +010088
Sasha Levinb67bfe02013-02-27 17:06:00 -080089 sk_for_each_safe(sk, tmp, &local->sockets.head) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +020090 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010091
Szymon Janc50b78b22012-09-26 14:22:10 +020092 bh_lock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +010093
Samuel Ortizf31652a2012-10-26 18:20:10 +020094 nfc_llcp_socket_purge(llcp_sock);
95
Samuel Ortiza69f32a2012-05-04 17:04:19 +020096 if (sk->sk_state == LLCP_CONNECTED)
97 nfc_put_device(llcp_sock->dev);
Samuel Ortizd6469602011-12-14 16:43:12 +010098
Samuel Ortiza69f32a2012-05-04 17:04:19 +020099 if (sk->sk_state == LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100100 struct nfc_llcp_sock *lsk, *n;
101 struct sock *accept_sk;
102
Szymon Janc0f450772012-10-17 15:23:39 +0200103 list_for_each_entry_safe(lsk, n,
104 &llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100105 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100106 accept_sk = &lsk->sk;
Szymon Janc50b78b22012-09-26 14:22:10 +0200107 bh_lock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100108
109 nfc_llcp_accept_unlink(accept_sk);
110
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +0100111 if (err)
112 accept_sk->sk_err = err;
Samuel Ortizd6469602011-12-14 16:43:12 +0100113 accept_sk->sk_state = LLCP_CLOSED;
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +0100114 accept_sk->sk_state_change(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100115
Szymon Janc50b78b22012-09-26 14:22:10 +0200116 bh_unlock_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100117 }
Samuel Ortizc8512be2012-10-15 15:09:52 +0200118 }
119
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +0100120 if (err)
121 sk->sk_err = err;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200122 sk->sk_state = LLCP_CLOSED;
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +0100123 sk->sk_state_change(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100124
Szymon Janc50b78b22012-09-26 14:22:10 +0200125 bh_unlock_sock(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100126
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200127 sk_del_node_init(sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100128 }
129
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200130 write_unlock(&local->sockets.lock);
Samuel Ortize6a3a4b2013-02-21 16:33:30 +0100131
Samuel Ortizb436a132013-04-03 16:34:19 +0200132 /* If we still have a device, we keep the RAW sockets alive */
133 if (device == true)
Samuel Ortize6a3a4b2013-02-21 16:33:30 +0100134 return;
135
136 write_lock(&local->raw_sockets.lock);
137
138 sk_for_each_safe(sk, tmp, &local->raw_sockets.head) {
139 llcp_sock = nfc_llcp_sock(sk);
140
141 bh_lock_sock(sk);
142
143 nfc_llcp_socket_purge(llcp_sock);
144
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +0100145 if (err)
146 sk->sk_err = err;
Samuel Ortize6a3a4b2013-02-21 16:33:30 +0100147 sk->sk_state = LLCP_CLOSED;
Samuel Ortize6a3a4b2013-02-21 16:33:30 +0100148 sk->sk_state_change(sk);
149
150 bh_unlock_sock(sk);
151
Samuel Ortize6a3a4b2013-02-21 16:33:30 +0100152 sk_del_node_init(sk);
153 }
154
155 write_unlock(&local->raw_sockets.lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100156}
157
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200158struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
159{
160 kref_get(&local->ref);
161
162 return local;
163}
164
Samuel Ortizc470e312013-04-03 16:40:52 +0200165static void local_cleanup(struct nfc_llcp_local *local)
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200166{
Samuel Ortizc470e312013-04-03 16:40:52 +0200167 nfc_llcp_socket_release(local, false, ENXIO);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200168 del_timer_sync(&local->link_timer);
169 skb_queue_purge(&local->tx_queue);
Tejun Heo474fee32012-08-22 16:22:16 -0700170 cancel_work_sync(&local->tx_work);
171 cancel_work_sync(&local->rx_work);
172 cancel_work_sync(&local->timeout_work);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200173 kfree_skb(local->rx_pending);
Thierry Escande40213fa2013-03-04 15:43:32 +0100174 del_timer_sync(&local->sdreq_timer);
175 cancel_work_sync(&local->sdreq_timeout_work);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +0100176 nfc_llcp_free_sdp_tlv_list(&local->pending_sdreqs);
Samuel Ortiz3536da02013-02-21 15:40:04 +0100177}
178
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200179static void local_release(struct kref *ref)
180{
181 struct nfc_llcp_local *local;
182
183 local = container_of(ref, struct nfc_llcp_local, ref);
184
185 list_del(&local->list);
Samuel Ortizc470e312013-04-03 16:40:52 +0200186 local_cleanup(local);
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200187 kfree(local);
188}
189
190int nfc_llcp_local_put(struct nfc_llcp_local *local)
191{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200192 if (local == NULL)
193 return 0;
194
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200195 return kref_put(&local->ref, local_release);
196}
197
Samuel Ortiz8f500202012-06-25 15:46:28 +0200198static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
199 u8 ssap, u8 dsap)
200{
201 struct sock *sk;
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200202 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200203
204 pr_debug("ssap dsap %d %d\n", ssap, dsap);
205
206 if (ssap == 0 && dsap == 0)
207 return NULL;
208
209 read_lock(&local->sockets.lock);
210
211 llcp_sock = NULL;
212
Sasha Levinb67bfe02013-02-27 17:06:00 -0800213 sk_for_each(sk, &local->sockets.head) {
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200214 tmp_sock = nfc_llcp_sock(sk);
Samuel Ortiz8f500202012-06-25 15:46:28 +0200215
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200216 if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
217 llcp_sock = tmp_sock;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200218 break;
Samuel Ortiza8df0f32012-10-16 21:15:59 +0200219 }
Samuel Ortiz8f500202012-06-25 15:46:28 +0200220 }
221
222 read_unlock(&local->sockets.lock);
223
224 if (llcp_sock == NULL)
225 return NULL;
226
227 sock_hold(&llcp_sock->sk);
228
229 return llcp_sock;
230}
231
232static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
233{
234 sock_put(&sock->sk);
235}
236
Samuel Ortizd6469602011-12-14 16:43:12 +0100237static void nfc_llcp_timeout_work(struct work_struct *work)
238{
239 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100240 timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100241
242 nfc_dep_link_down(local->dev);
243}
244
245static void nfc_llcp_symm_timer(unsigned long data)
246{
247 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
248
249 pr_err("SYMM timeout\n");
250
Linus Torvalds916082b2012-10-02 16:01:31 -0700251 schedule_work(&local->timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +0100252}
253
Thierry Escande40213fa2013-03-04 15:43:32 +0100254static void nfc_llcp_sdreq_timeout_work(struct work_struct *work)
255{
256 unsigned long time;
257 HLIST_HEAD(nl_sdres_list);
258 struct hlist_node *n;
259 struct nfc_llcp_sdp_tlv *sdp;
260 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
261 sdreq_timeout_work);
262
263 mutex_lock(&local->sdreq_lock);
264
265 time = jiffies - msecs_to_jiffies(3 * local->remote_lto);
266
267 hlist_for_each_entry_safe(sdp, n, &local->pending_sdreqs, node) {
268 if (time_after(sdp->time, time))
269 continue;
270
271 sdp->sap = LLCP_SDP_UNBOUND;
272
273 hlist_del(&sdp->node);
274
275 hlist_add_head(&sdp->node, &nl_sdres_list);
276 }
277
278 if (!hlist_empty(&local->pending_sdreqs))
279 mod_timer(&local->sdreq_timer,
280 jiffies + msecs_to_jiffies(3 * local->remote_lto));
281
282 mutex_unlock(&local->sdreq_lock);
283
284 if (!hlist_empty(&nl_sdres_list))
285 nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
286}
287
288static void nfc_llcp_sdreq_timer(unsigned long data)
289{
290 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
291
292 schedule_work(&local->sdreq_timeout_work);
293}
294
Samuel Ortizd6469602011-12-14 16:43:12 +0100295struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
296{
Axel Lin29e27dd2014-02-26 10:26:45 +0800297 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +0100298
Axel Lin29e27dd2014-02-26 10:26:45 +0800299 list_for_each_entry(local, &llcp_devices, list)
Samuel Ortizd6469602011-12-14 16:43:12 +0100300 if (local->dev == dev)
301 return local;
302
303 pr_debug("No device found\n");
304
305 return NULL;
306}
307
308static char *wks[] = {
309 NULL,
310 NULL, /* SDP */
311 "urn:nfc:sn:ip",
312 "urn:nfc:sn:obex",
313 "urn:nfc:sn:snep",
314};
315
316static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
317{
318 int sap, num_wks;
319
320 pr_debug("%s\n", service_name);
321
322 if (service_name == NULL)
323 return -EINVAL;
324
325 num_wks = ARRAY_SIZE(wks);
326
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100327 for (sap = 0; sap < num_wks; sap++) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100328 if (wks[sap] == NULL)
329 continue;
330
331 if (strncmp(wks[sap], service_name, service_name_len) == 0)
332 return sap;
333 }
334
335 return -EINVAL;
336}
337
Samuel Ortiz8f500202012-06-25 15:46:28 +0200338static
339struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
340 u8 *sn, size_t sn_len)
341{
342 struct sock *sk;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200343 struct nfc_llcp_sock *llcp_sock, *tmp_sock;
344
345 pr_debug("sn %zd %p\n", sn_len, sn);
346
347 if (sn == NULL || sn_len == 0)
348 return NULL;
349
350 read_lock(&local->sockets.lock);
351
352 llcp_sock = NULL;
353
Sasha Levinb67bfe02013-02-27 17:06:00 -0800354 sk_for_each(sk, &local->sockets.head) {
Samuel Ortiz8f500202012-06-25 15:46:28 +0200355 tmp_sock = nfc_llcp_sock(sk);
356
357 pr_debug("llcp sock %p\n", tmp_sock);
358
Samuel Ortiz54292d62012-10-15 15:08:29 +0200359 if (tmp_sock->sk.sk_type == SOCK_STREAM &&
360 tmp_sock->sk.sk_state != LLCP_LISTEN)
361 continue;
362
363 if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
364 tmp_sock->sk.sk_state != LLCP_BOUND)
Samuel Ortiz8f500202012-06-25 15:46:28 +0200365 continue;
366
367 if (tmp_sock->service_name == NULL ||
368 tmp_sock->service_name_len == 0)
369 continue;
370
371 if (tmp_sock->service_name_len != sn_len)
372 continue;
373
374 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
375 llcp_sock = tmp_sock;
376 break;
377 }
378 }
379
380 read_unlock(&local->sockets.lock);
381
382 pr_debug("Found llcp sock %p\n", llcp_sock);
383
384 return llcp_sock;
385}
386
Samuel Ortizd6469602011-12-14 16:43:12 +0100387u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100388 struct nfc_llcp_sock *sock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100389{
390 mutex_lock(&local->sdp_lock);
391
392 if (sock->service_name != NULL && sock->service_name_len > 0) {
393 int ssap = nfc_llcp_wks_sap(sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100394 sock->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100395
396 if (ssap > 0) {
397 pr_debug("WKS %d\n", ssap);
398
399 /* This is a WKS, let's check if it's free */
400 if (local->local_wks & BIT(ssap)) {
401 mutex_unlock(&local->sdp_lock);
402
403 return LLCP_SAP_MAX;
404 }
405
Samuel Ortiz1762c172012-03-05 01:03:38 +0100406 set_bit(ssap, &local->local_wks);
Samuel Ortizd6469602011-12-14 16:43:12 +0100407 mutex_unlock(&local->sdp_lock);
408
409 return ssap;
410 }
411
412 /*
Samuel Ortiz8f500202012-06-25 15:46:28 +0200413 * Check if there already is a non WKS socket bound
414 * to this service name.
Samuel Ortizd6469602011-12-14 16:43:12 +0100415 */
Samuel Ortiz8f500202012-06-25 15:46:28 +0200416 if (nfc_llcp_sock_from_sn(local, sock->service_name,
417 sock->service_name_len) != NULL) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100418 mutex_unlock(&local->sdp_lock);
419
420 return LLCP_SAP_MAX;
421 }
422
Samuel Ortizd6469602011-12-14 16:43:12 +0100423 mutex_unlock(&local->sdp_lock);
424
Samuel Ortiz8f500202012-06-25 15:46:28 +0200425 return LLCP_SDP_UNBOUND;
Samuel Ortizd6469602011-12-14 16:43:12 +0100426
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200427 } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
428 if (!test_bit(sock->ssap, &local->local_wks)) {
429 set_bit(sock->ssap, &local->local_wks);
430 mutex_unlock(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100431
Samuel Ortizebbb16d2012-06-22 17:15:20 +0200432 return sock->ssap;
Samuel Ortizd6469602011-12-14 16:43:12 +0100433 }
434 }
435
436 mutex_unlock(&local->sdp_lock);
437
438 return LLCP_SAP_MAX;
439}
440
441u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
442{
443 u8 local_ssap;
444
445 mutex_lock(&local->sdp_lock);
446
447 local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
448 if (local_ssap == LLCP_LOCAL_NUM_SAP) {
449 mutex_unlock(&local->sdp_lock);
450 return LLCP_SAP_MAX;
451 }
452
Samuel Ortiz1762c172012-03-05 01:03:38 +0100453 set_bit(local_ssap, &local->local_sap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100454
455 mutex_unlock(&local->sdp_lock);
456
457 return local_ssap + LLCP_LOCAL_SAP_OFFSET;
458}
459
460void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
461{
462 u8 local_ssap;
463 unsigned long *sdp;
464
465 if (ssap < LLCP_WKS_NUM_SAP) {
466 local_ssap = ssap;
467 sdp = &local->local_wks;
468 } else if (ssap < LLCP_LOCAL_NUM_SAP) {
Samuel Ortiz8f500202012-06-25 15:46:28 +0200469 atomic_t *client_cnt;
470
Samuel Ortizd6469602011-12-14 16:43:12 +0100471 local_ssap = ssap - LLCP_WKS_NUM_SAP;
472 sdp = &local->local_sdp;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200473 client_cnt = &local->local_sdp_cnt[local_ssap];
474
475 pr_debug("%d clients\n", atomic_read(client_cnt));
476
477 mutex_lock(&local->sdp_lock);
478
479 if (atomic_dec_and_test(client_cnt)) {
480 struct nfc_llcp_sock *l_sock;
481
482 pr_debug("No more clients for SAP %d\n", ssap);
483
484 clear_bit(local_ssap, sdp);
485
486 /* Find the listening sock and set it back to UNBOUND */
487 l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
488 if (l_sock) {
489 l_sock->ssap = LLCP_SDP_UNBOUND;
490 nfc_llcp_sock_put(l_sock);
491 }
492 }
493
494 mutex_unlock(&local->sdp_lock);
495
496 return;
Samuel Ortizd6469602011-12-14 16:43:12 +0100497 } else if (ssap < LLCP_MAX_SAP) {
498 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
499 sdp = &local->local_sap;
500 } else {
501 return;
502 }
503
504 mutex_lock(&local->sdp_lock);
505
Samuel Ortiz1762c172012-03-05 01:03:38 +0100506 clear_bit(local_ssap, sdp);
Samuel Ortizd6469602011-12-14 16:43:12 +0100507
508 mutex_unlock(&local->sdp_lock);
509}
510
Samuel Ortiz8f500202012-06-25 15:46:28 +0200511static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
512{
513 u8 ssap;
514
515 mutex_lock(&local->sdp_lock);
516
517 ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
518 if (ssap == LLCP_SDP_NUM_SAP) {
519 mutex_unlock(&local->sdp_lock);
520
521 return LLCP_SAP_MAX;
522 }
523
524 pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
525
526 set_bit(ssap, &local->local_sdp);
527
528 mutex_unlock(&local->sdp_lock);
529
530 return LLCP_WKS_NUM_SAP + ssap;
531}
532
Samuel Ortizd6469602011-12-14 16:43:12 +0100533static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
534{
535 u8 *gb_cur, *version_tlv, version, version_length;
Thierry Escande52feb442012-10-17 14:43:39 +0200536 u8 *lto_tlv, lto_length;
Samuel Ortizd6469602011-12-14 16:43:12 +0100537 u8 *wks_tlv, wks_length;
Samuel Ortiz56d58762012-04-10 19:43:19 +0200538 u8 *miux_tlv, miux_length;
Samuel Ortiz4ca546e2013-06-03 12:10:04 +0200539 __be16 wks = cpu_to_be16(local->local_wks);
Samuel Ortizd6469602011-12-14 16:43:12 +0100540 u8 gb_len = 0;
Wei Yongjun52da2442012-09-02 21:21:46 +0800541 int ret = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100542
543 version = LLCP_VERSION_11;
544 version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100545 1, &version_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100546 gb_len += version_length;
547
Thierry Escande52feb442012-10-17 14:43:39 +0200548 lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, &lto_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100549 gb_len += lto_length;
550
551 pr_debug("Local wks 0x%lx\n", local->local_wks);
Samuel Ortiz4ca546e2013-06-03 12:10:04 +0200552 wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&wks, 2, &wks_length);
Samuel Ortizd6469602011-12-14 16:43:12 +0100553 gb_len += wks_length;
554
Thierry Escande52feb442012-10-17 14:43:39 +0200555 miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
Samuel Ortiz56d58762012-04-10 19:43:19 +0200556 &miux_length);
557 gb_len += miux_length;
558
Samuel Ortizd6469602011-12-14 16:43:12 +0100559 gb_len += ARRAY_SIZE(llcp_magic);
560
561 if (gb_len > NFC_MAX_GT_LEN) {
Wei Yongjun52da2442012-09-02 21:21:46 +0800562 ret = -EINVAL;
563 goto out;
Samuel Ortizd6469602011-12-14 16:43:12 +0100564 }
565
566 gb_cur = local->gb;
567
568 memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
569 gb_cur += ARRAY_SIZE(llcp_magic);
570
571 memcpy(gb_cur, version_tlv, version_length);
572 gb_cur += version_length;
573
574 memcpy(gb_cur, lto_tlv, lto_length);
575 gb_cur += lto_length;
576
577 memcpy(gb_cur, wks_tlv, wks_length);
578 gb_cur += wks_length;
579
Samuel Ortiz56d58762012-04-10 19:43:19 +0200580 memcpy(gb_cur, miux_tlv, miux_length);
581 gb_cur += miux_length;
582
Samuel Ortizd6469602011-12-14 16:43:12 +0100583 local->gb_len = gb_len;
584
Wei Yongjun52da2442012-09-02 21:21:46 +0800585out:
586 kfree(version_tlv);
587 kfree(lto_tlv);
588 kfree(wks_tlv);
589 kfree(miux_tlv);
590
591 return ret;
Samuel Ortizd6469602011-12-14 16:43:12 +0100592}
593
Samuel Ortizb8e7a062012-06-22 02:04:53 +0200594u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
595{
596 struct nfc_llcp_local *local;
597
598 local = nfc_llcp_find_local(dev);
599 if (local == NULL) {
600 *general_bytes_len = 0;
601 return NULL;
602 }
603
604 nfc_llcp_build_gb(local);
605
606 *general_bytes_len = local->gb_len;
607
608 return local->gb;
609}
610
Samuel Ortizd6469602011-12-14 16:43:12 +0100611int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
612{
Axel Lin3143a4c2014-02-25 09:18:10 +0800613 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +0100614
Axel Lin3143a4c2014-02-25 09:18:10 +0800615 if (gb_len < 3 || gb_len > NFC_MAX_GT_LEN)
616 return -EINVAL;
617
618 local = nfc_llcp_find_local(dev);
Samuel Ortizd6469602011-12-14 16:43:12 +0100619 if (local == NULL) {
620 pr_err("No LLCP device\n");
621 return -ENODEV;
622 }
623
624 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
625 memcpy(local->remote_gb, gb, gb_len);
626 local->remote_gb_len = gb_len;
627
Samuel Ortizd6469602011-12-14 16:43:12 +0100628 if (memcmp(local->remote_gb, llcp_magic, 3)) {
629 pr_err("MAC does not support LLCP\n");
630 return -EINVAL;
631 }
632
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200633 return nfc_llcp_parse_gb_tlv(local,
634 &local->remote_gb[3],
635 local->remote_gb_len - 3);
Samuel Ortizd6469602011-12-14 16:43:12 +0100636}
637
Samuel Ortizd6469602011-12-14 16:43:12 +0100638static u8 nfc_llcp_dsap(struct sk_buff *pdu)
639{
640 return (pdu->data[0] & 0xfc) >> 2;
641}
642
643static u8 nfc_llcp_ptype(struct sk_buff *pdu)
644{
645 return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
646}
647
648static u8 nfc_llcp_ssap(struct sk_buff *pdu)
649{
650 return pdu->data[1] & 0x3f;
651}
652
653static u8 nfc_llcp_ns(struct sk_buff *pdu)
654{
655 return pdu->data[2] >> 4;
656}
657
658static u8 nfc_llcp_nr(struct sk_buff *pdu)
659{
660 return pdu->data[2] & 0xf;
661}
662
663static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
664{
Samuel Ortiz279cf172012-04-10 19:43:14 +0200665 pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
Samuel Ortizd6469602011-12-14 16:43:12 +0100666 sock->send_n = (sock->send_n + 1) % 16;
667 sock->recv_ack_n = (sock->recv_n - 1) % 16;
668}
669
Thierry Escande4463523b2012-09-26 18:16:44 +0200670void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
671 struct sk_buff *skb, u8 direction)
672{
Thierry Escande4463523b2012-09-26 18:16:44 +0200673 struct sk_buff *skb_copy = NULL, *nskb;
674 struct sock *sk;
675 u8 *data;
676
677 read_lock(&local->raw_sockets.lock);
678
Sasha Levinb67bfe02013-02-27 17:06:00 -0800679 sk_for_each(sk, &local->raw_sockets.head) {
Thierry Escande4463523b2012-09-26 18:16:44 +0200680 if (sk->sk_state != LLCP_BOUND)
681 continue;
682
683 if (skb_copy == NULL) {
Octavian Purdilabad93e92014-06-12 01:36:26 +0300684 skb_copy = __pskb_copy_fclone(skb, NFC_RAW_HEADER_SIZE,
685 GFP_ATOMIC, true);
Thierry Escande4463523b2012-09-26 18:16:44 +0200686
687 if (skb_copy == NULL)
688 continue;
689
Hiren Tandel57be1f32014-05-05 19:43:31 +0900690 data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
Thierry Escande4463523b2012-09-26 18:16:44 +0200691
692 data[0] = local->dev ? local->dev->idx : 0xFF;
Hiren Tandel57be1f32014-05-05 19:43:31 +0900693 data[1] = direction & 0x01;
694 data[1] |= (RAW_PAYLOAD_LLCP << 1);
Thierry Escande4463523b2012-09-26 18:16:44 +0200695 }
696
697 nskb = skb_clone(skb_copy, GFP_ATOMIC);
698 if (!nskb)
699 continue;
700
701 if (sock_queue_rcv_skb(sk, nskb))
702 kfree_skb(nskb);
703 }
704
705 read_unlock(&local->raw_sockets.lock);
706
707 kfree_skb(skb_copy);
708}
709
Samuel Ortiz84457962012-05-30 17:48:29 +0200710static void nfc_llcp_tx_work(struct work_struct *work)
711{
712 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
713 tx_work);
714 struct sk_buff *skb;
715 struct sock *sk;
716 struct nfc_llcp_sock *llcp_sock;
717
718 skb = skb_dequeue(&local->tx_queue);
719 if (skb != NULL) {
720 sk = skb->sk;
721 llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiza6a09152012-10-05 00:37:22 +0200722
723 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
Thierry Escandef1b79dc2013-06-05 17:15:59 +0200724 kfree_skb(skb);
Samuel Ortiza6a09152012-10-05 00:37:22 +0200725 nfc_llcp_send_symm(local->dev);
Samuel Ortiz2635a4b2013-05-28 15:03:17 +0200726 } else if (llcp_sock && !llcp_sock->remote_ready) {
727 skb_queue_head(&local->tx_queue, skb);
728 nfc_llcp_send_symm(local->dev);
Samuel Ortiza6a09152012-10-05 00:37:22 +0200729 } else {
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100730 struct sk_buff *copy_skb = NULL;
731 u8 ptype = nfc_llcp_ptype(skb);
Samuel Ortiz84457962012-05-30 17:48:29 +0200732 int ret;
733
734 pr_debug("Sending pending skb\n");
735 print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
736 DUMP_PREFIX_OFFSET, 16, 1,
737 skb->data, skb->len, true);
738
Thierry Escande17f7ae162013-06-04 11:34:51 +0200739 if (ptype == LLCP_PDU_DISC && sk != NULL &&
740 sk->sk_state == LLCP_DISCONNECTING) {
741 nfc_llcp_sock_unlink(&local->sockets, sk);
742 sock_orphan(sk);
743 sock_put(sk);
744 }
745
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100746 if (ptype == LLCP_PDU_I)
747 copy_skb = skb_copy(skb, GFP_ATOMIC);
748
Thierry Escande2c2d45b2012-11-27 15:44:24 +0100749 __net_timestamp(skb);
750
Thierry Escande4463523b2012-09-26 18:16:44 +0200751 nfc_llcp_send_to_raw_sock(local, skb,
Hiren Tandel57be1f32014-05-05 19:43:31 +0900752 NFC_DIRECTION_TX);
Thierry Escande4463523b2012-09-26 18:16:44 +0200753
Samuel Ortiz84457962012-05-30 17:48:29 +0200754 ret = nfc_data_exchange(local->dev, local->target_idx,
755 skb, nfc_llcp_recv, local);
756
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100757 if (ret) {
758 kfree_skb(copy_skb);
759 goto out;
Samuel Ortiz84457962012-05-30 17:48:29 +0200760 }
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100761
762 if (ptype == LLCP_PDU_I && copy_skb)
763 skb_queue_tail(&llcp_sock->tx_pending_queue,
764 copy_skb);
Samuel Ortiz84457962012-05-30 17:48:29 +0200765 }
766 } else {
767 nfc_llcp_send_symm(local->dev);
768 }
769
Samuel Ortizbe02b6b2012-11-01 23:36:07 +0100770out:
Samuel Ortiz84457962012-05-30 17:48:29 +0200771 mod_timer(&local->link_timer,
772 jiffies + msecs_to_jiffies(2 * local->remote_lto));
773}
774
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200775static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
776 u8 ssap)
777{
778 struct sock *sk;
779 struct nfc_llcp_sock *llcp_sock;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200780
781 read_lock(&local->connecting_sockets.lock);
782
Sasha Levinb67bfe02013-02-27 17:06:00 -0800783 sk_for_each(sk, &local->connecting_sockets.head) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200784 llcp_sock = nfc_llcp_sock(sk);
785
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200786 if (llcp_sock->ssap == ssap) {
787 sock_hold(&llcp_sock->sk);
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200788 goto out;
Samuel Ortiz5a0f6f32012-05-21 11:44:42 +0200789 }
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200790 }
791
792 llcp_sock = NULL;
793
794out:
795 read_unlock(&local->connecting_sockets.lock);
796
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200797 return llcp_sock;
798}
799
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200800static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
801 u8 *sn, size_t sn_len)
802{
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200803 struct nfc_llcp_sock *llcp_sock;
804
Samuel Ortiz8f500202012-06-25 15:46:28 +0200805 llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100806
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200807 if (llcp_sock == NULL)
808 return NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100809
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200810 sock_hold(&llcp_sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100811
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200812 return llcp_sock;
Samuel Ortizd6469602011-12-14 16:43:12 +0100813}
814
Samuel Ortizd6469602011-12-14 16:43:12 +0100815static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
816{
817 u8 *tlv = &skb->data[2], type, length;
818 size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
819
820 while (offset < tlv_array_len) {
821 type = tlv[0];
822 length = tlv[1];
823
824 pr_debug("type 0x%x length %d\n", type, length);
825
826 if (type == LLCP_TLV_SN) {
827 *sn_len = length;
828 return &tlv[2];
829 }
830
831 offset += length + 2;
832 tlv += length + 2;
833 }
834
835 return NULL;
836}
837
Samuel Ortiz968272b2012-10-15 16:14:37 +0200838static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
839 struct sk_buff *skb)
840{
841 struct nfc_llcp_sock *llcp_sock;
842 struct nfc_llcp_ui_cb *ui_cb;
843 u8 dsap, ssap;
844
845 dsap = nfc_llcp_dsap(skb);
846 ssap = nfc_llcp_ssap(skb);
847
848 ui_cb = nfc_llcp_ui_skb_cb(skb);
849 ui_cb->dsap = dsap;
850 ui_cb->ssap = ssap;
851
Samuel Ortiz968272b2012-10-15 16:14:37 +0200852 pr_debug("%d %d\n", dsap, ssap);
853
854 /* We're looking for a bound socket, not a client one */
855 llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
856 if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
857 return;
858
859 /* There is no sequence with UI frames */
860 skb_pull(skb, LLCP_HEADER_SIZE);
Samuel Ortiz1727cf92012-12-07 16:37:30 +0100861 if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
862 /*
863 * UI frames will be freed from the socket layer, so we
864 * need to keep them alive until someone receives them.
865 */
866 skb_get(skb);
867 } else {
868 pr_err("Receive queue is full\n");
Samuel Ortiz968272b2012-10-15 16:14:37 +0200869 }
870
871 nfc_llcp_sock_put(llcp_sock);
872}
873
Samuel Ortizd6469602011-12-14 16:43:12 +0100874static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100875 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +0100876{
877 struct sock *new_sk, *parent;
878 struct nfc_llcp_sock *sock, *new_sock;
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200879 u8 dsap, ssap, reason;
Samuel Ortizd6469602011-12-14 16:43:12 +0100880
881 dsap = nfc_llcp_dsap(skb);
882 ssap = nfc_llcp_ssap(skb);
883
884 pr_debug("%d %d\n", dsap, ssap);
885
Samuel Ortizd6469602011-12-14 16:43:12 +0100886 if (dsap != LLCP_SAP_SDP) {
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200887 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
888 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100889 reason = LLCP_DM_NOBOUND;
890 goto fail;
891 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100892 } else {
893 u8 *sn;
894 size_t sn_len;
895
896 sn = nfc_llcp_connect_sn(skb, &sn_len);
897 if (sn == NULL) {
898 reason = LLCP_DM_NOBOUND;
899 goto fail;
900 }
901
902 pr_debug("Service name length %zu\n", sn_len);
903
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200904 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
905 if (sock == NULL) {
906 reason = LLCP_DM_NOBOUND;
907 goto fail;
Samuel Ortizd6469602011-12-14 16:43:12 +0100908 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100909 }
910
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200911 lock_sock(&sock->sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100912
Samuel Ortizd6469602011-12-14 16:43:12 +0100913 parent = &sock->sk;
914
915 if (sk_acceptq_is_full(parent)) {
916 reason = LLCP_DM_REJ;
917 release_sock(&sock->sk);
918 sock_put(&sock->sk);
919 goto fail;
920 }
921
Samuel Ortiz8f500202012-06-25 15:46:28 +0200922 if (sock->ssap == LLCP_SDP_UNBOUND) {
923 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
924
925 pr_debug("First client, reserving %d\n", ssap);
926
927 if (ssap == LLCP_SAP_MAX) {
928 reason = LLCP_DM_REJ;
929 release_sock(&sock->sk);
930 sock_put(&sock->sk);
931 goto fail;
932 }
933
934 sock->ssap = ssap;
935 }
936
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500937 new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC, 0);
Samuel Ortizd6469602011-12-14 16:43:12 +0100938 if (new_sk == NULL) {
939 reason = LLCP_DM_REJ;
940 release_sock(&sock->sk);
941 sock_put(&sock->sk);
942 goto fail;
943 }
944
945 new_sock = nfc_llcp_sock(new_sk);
946 new_sock->dev = local->dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200947 new_sock->local = nfc_llcp_local_get(local);
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100948 new_sock->rw = sock->rw;
949 new_sock->miux = sock->miux;
Samuel Ortizd6469602011-12-14 16:43:12 +0100950 new_sock->nfc_protocol = sock->nfc_protocol;
Samuel Ortizd6469602011-12-14 16:43:12 +0100951 new_sock->dsap = ssap;
Samuel Ortiz025f1522012-06-18 21:38:09 +0200952 new_sock->target_idx = local->target_idx;
Samuel Ortizd6469602011-12-14 16:43:12 +0100953 new_sock->parent = parent;
Samuel Ortiz8f500202012-06-25 15:46:28 +0200954 new_sock->ssap = sock->ssap;
955 if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
956 atomic_t *client_count;
957
958 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
959
960 client_count =
961 &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
962
963 atomic_inc(client_count);
964 new_sock->reserved_ssap = sock->ssap;
965 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100966
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200967 nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
968 skb->len - LLCP_HEADER_SIZE);
969
Samuel Ortizd6469602011-12-14 16:43:12 +0100970 pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
971
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200972 nfc_llcp_sock_link(&local->sockets, new_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100973
974 nfc_llcp_accept_enqueue(&sock->sk, new_sk);
975
976 nfc_get_device(local->dev->idx);
977
978 new_sk->sk_state = LLCP_CONNECTED;
979
980 /* Wake the listening processes */
David S. Miller676d2362014-04-11 16:15:36 -0400981 parent->sk_data_ready(parent);
Samuel Ortizd6469602011-12-14 16:43:12 +0100982
983 /* Send CC */
984 nfc_llcp_send_cc(new_sock);
985
986 release_sock(&sock->sk);
987 sock_put(&sock->sk);
988
989 return;
990
991fail:
992 /* Send DM */
993 nfc_llcp_send_dm(local, dsap, ssap, reason);
Samuel Ortizd6469602011-12-14 16:43:12 +0100994}
995
Samuel Ortizd094afa2012-03-05 01:03:42 +0100996int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100997{
Samuel Ortizd094afa2012-03-05 01:03:42 +0100998 int nr_frames = 0;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +0100999 struct nfc_llcp_local *local = sock->local;
1000
1001 pr_debug("Remote ready %d tx queue len %d remote rw %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001002 sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
Samuel Ortize4306be2013-02-22 01:12:28 +01001003 sock->remote_rw);
Samuel Ortiz4722d2b2012-03-05 01:03:35 +01001004
1005 /* Try to queue some I frames for transmission */
1006 while (sock->remote_ready &&
Samuel Ortize4306be2013-02-22 01:12:28 +01001007 skb_queue_len(&sock->tx_pending_queue) < sock->remote_rw) {
Samuel Ortiz84457962012-05-30 17:48:29 +02001008 struct sk_buff *pdu;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +01001009
1010 pdu = skb_dequeue(&sock->tx_queue);
1011 if (pdu == NULL)
1012 break;
1013
1014 /* Update N(S)/N(R) */
1015 nfc_llcp_set_nrns(sock, pdu);
1016
Samuel Ortiz4722d2b2012-03-05 01:03:35 +01001017 skb_queue_tail(&local->tx_queue, pdu);
Samuel Ortizd094afa2012-03-05 01:03:42 +01001018 nr_frames++;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +01001019 }
Samuel Ortizd094afa2012-03-05 01:03:42 +01001020
1021 return nr_frames;
Samuel Ortiz4722d2b2012-03-05 01:03:35 +01001022}
1023
Samuel Ortizd6469602011-12-14 16:43:12 +01001024static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001025 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001026{
1027 struct nfc_llcp_sock *llcp_sock;
1028 struct sock *sk;
1029 u8 dsap, ssap, ptype, ns, nr;
1030
1031 ptype = nfc_llcp_ptype(skb);
1032 dsap = nfc_llcp_dsap(skb);
1033 ssap = nfc_llcp_ssap(skb);
1034 ns = nfc_llcp_ns(skb);
1035 nr = nfc_llcp_nr(skb);
1036
1037 pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
1038
1039 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1040 if (llcp_sock == NULL) {
1041 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1042 return;
1043 }
1044
1045 sk = &llcp_sock->sk;
1046 lock_sock(sk);
1047 if (sk->sk_state == LLCP_CLOSED) {
1048 release_sock(sk);
1049 nfc_llcp_sock_put(llcp_sock);
1050 }
1051
Samuel Ortizd6469602011-12-14 16:43:12 +01001052 /* Pass the payload upstream */
1053 if (ptype == LLCP_PDU_I) {
1054 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
1055
Samuel Ortiz53aef922012-03-05 01:03:36 +01001056 if (ns == llcp_sock->recv_n)
1057 llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
1058 else
1059 pr_err("Received out of sequence I PDU\n");
1060
Samuel Ortizd6469602011-12-14 16:43:12 +01001061 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
Samuel Ortiz1727cf92012-12-07 16:37:30 +01001062 if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
1063 /*
1064 * I frames will be freed from the socket layer, so we
1065 * need to keep them alive until someone receives them.
1066 */
1067 skb_get(skb);
1068 } else {
1069 pr_err("Receive queue is full\n");
Samuel Ortizd6469602011-12-14 16:43:12 +01001070 }
1071 }
1072
1073 /* Remove skbs from the pending queue */
1074 if (llcp_sock->send_ack_n != nr) {
1075 struct sk_buff *s, *tmp;
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +01001076 u8 n;
Samuel Ortizd6469602011-12-14 16:43:12 +01001077
1078 llcp_sock->send_ack_n = nr;
1079
Samuel Ortiz84457962012-05-30 17:48:29 +02001080 /* Remove and free all skbs until ns == nr */
1081 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +01001082 n = nfc_llcp_ns(s);
1083
Samuel Ortiz84457962012-05-30 17:48:29 +02001084 skb_unlink(s, &llcp_sock->tx_pending_queue);
1085 kfree_skb(s);
1086
Waldemar Rymarkiewicz28981492012-11-26 08:40:04 +01001087 if (n == nr)
Samuel Ortiz84457962012-05-30 17:48:29 +02001088 break;
1089 }
1090
1091 /* Re-queue the remaining skbs for transmission */
1092 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
1093 s, tmp) {
1094 skb_unlink(s, &llcp_sock->tx_pending_queue);
1095 skb_queue_head(&local->tx_queue, s);
1096 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001097 }
1098
Samuel Ortiz53aef922012-03-05 01:03:36 +01001099 if (ptype == LLCP_PDU_RR)
1100 llcp_sock->remote_ready = true;
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001101 else if (ptype == LLCP_PDU_RNR)
Samuel Ortiz53aef922012-03-05 01:03:36 +01001102 llcp_sock->remote_ready = false;
1103
Samuel Ortiz56af2562012-05-30 18:06:11 +02001104 if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
Samuel Ortizd094afa2012-03-05 01:03:42 +01001105 nfc_llcp_send_rr(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001106
1107 release_sock(sk);
1108 nfc_llcp_sock_put(llcp_sock);
1109}
1110
1111static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001112 struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001113{
1114 struct nfc_llcp_sock *llcp_sock;
1115 struct sock *sk;
1116 u8 dsap, ssap;
1117
1118 dsap = nfc_llcp_dsap(skb);
1119 ssap = nfc_llcp_ssap(skb);
1120
Samuel Ortiz6d2cd972013-04-03 16:44:44 +02001121 if ((dsap == 0) && (ssap == 0)) {
1122 pr_debug("Connection termination");
1123 nfc_dep_link_down(local->dev);
1124 return;
1125 }
1126
Samuel Ortizd6469602011-12-14 16:43:12 +01001127 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1128 if (llcp_sock == NULL) {
1129 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1130 return;
1131 }
1132
1133 sk = &llcp_sock->sk;
1134 lock_sock(sk);
Samuel Ortizf31652a2012-10-26 18:20:10 +02001135
1136 nfc_llcp_socket_purge(llcp_sock);
1137
Samuel Ortizd6469602011-12-14 16:43:12 +01001138 if (sk->sk_state == LLCP_CLOSED) {
1139 release_sock(sk);
1140 nfc_llcp_sock_put(llcp_sock);
1141 }
1142
Samuel Ortizd6469602011-12-14 16:43:12 +01001143 if (sk->sk_state == LLCP_CONNECTED) {
1144 nfc_put_device(local->dev);
1145 sk->sk_state = LLCP_CLOSED;
1146 sk->sk_state_change(sk);
1147 }
1148
1149 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
1150
1151 release_sock(sk);
1152 nfc_llcp_sock_put(llcp_sock);
1153}
1154
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001155static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001156{
1157 struct nfc_llcp_sock *llcp_sock;
Samuel Ortizff353d82012-05-07 12:31:19 +02001158 struct sock *sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001159 u8 dsap, ssap;
1160
Samuel Ortizd6469602011-12-14 16:43:12 +01001161 dsap = nfc_llcp_dsap(skb);
1162 ssap = nfc_llcp_ssap(skb);
1163
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001164 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
Samuel Ortizd6469602011-12-14 16:43:12 +01001165 if (llcp_sock == NULL) {
1166 pr_err("Invalid CC\n");
1167 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1168
1169 return;
1170 }
1171
Samuel Ortizff353d82012-05-07 12:31:19 +02001172 sk = &llcp_sock->sk;
Samuel Ortizd6469602011-12-14 16:43:12 +01001173
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001174 /* Unlink from connecting and link to the client array */
1175 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
1176 nfc_llcp_sock_link(&local->sockets, sk);
1177 llcp_sock->dsap = ssap;
1178
Samuel Ortiz7a06e582012-05-07 22:03:34 +02001179 nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
1180 skb->len - LLCP_HEADER_SIZE);
Samuel Ortizd6469602011-12-14 16:43:12 +01001181
Samuel Ortizff353d82012-05-07 12:31:19 +02001182 sk->sk_state = LLCP_CONNECTED;
1183 sk->sk_state_change(sk);
1184
Samuel Ortizd6469602011-12-14 16:43:12 +01001185 nfc_llcp_sock_put(llcp_sock);
1186}
1187
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001188static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1189{
1190 struct nfc_llcp_sock *llcp_sock;
1191 struct sock *sk;
1192 u8 dsap, ssap, reason;
1193
1194 dsap = nfc_llcp_dsap(skb);
1195 ssap = nfc_llcp_ssap(skb);
1196 reason = skb->data[2];
1197
1198 pr_debug("%d %d reason %d\n", ssap, dsap, reason);
1199
1200 switch (reason) {
1201 case LLCP_DM_NOBOUND:
1202 case LLCP_DM_REJ:
1203 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1204 break;
1205
1206 default:
1207 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1208 break;
1209 }
1210
1211 if (llcp_sock == NULL) {
Samuel Ortiza8df0f32012-10-16 21:15:59 +02001212 pr_debug("Already closed\n");
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001213 return;
1214 }
1215
1216 sk = &llcp_sock->sk;
1217
1218 sk->sk_err = ENXIO;
1219 sk->sk_state = LLCP_CLOSED;
1220 sk->sk_state_change(sk);
1221
1222 nfc_llcp_sock_put(llcp_sock);
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001223}
1224
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001225static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1226 struct sk_buff *skb)
1227{
1228 struct nfc_llcp_sock *llcp_sock;
1229 u8 dsap, ssap, *tlv, type, length, tid, sap;
1230 u16 tlv_len, offset;
1231 char *service_name;
1232 size_t service_name_len;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001233 struct nfc_llcp_sdp_tlv *sdp;
1234 HLIST_HEAD(llc_sdres_list);
1235 size_t sdres_tlvs_len;
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001236 HLIST_HEAD(nl_sdres_list);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001237
1238 dsap = nfc_llcp_dsap(skb);
1239 ssap = nfc_llcp_ssap(skb);
1240
1241 pr_debug("%d %d\n", dsap, ssap);
1242
1243 if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1244 pr_err("Wrong SNL SAP\n");
1245 return;
1246 }
1247
1248 tlv = &skb->data[LLCP_HEADER_SIZE];
1249 tlv_len = skb->len - LLCP_HEADER_SIZE;
1250 offset = 0;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001251 sdres_tlvs_len = 0;
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001252
Szymon Janc0f450772012-10-17 15:23:39 +02001253 while (offset < tlv_len) {
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001254 type = tlv[0];
1255 length = tlv[1];
1256
1257 switch (type) {
1258 case LLCP_TLV_SDREQ:
1259 tid = tlv[2];
1260 service_name = (char *) &tlv[3];
1261 service_name_len = length - 1;
1262
Samuel Ortize6904082012-10-15 14:28:13 +02001263 pr_debug("Looking for %.16s\n", service_name);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001264
1265 if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1266 !strncmp(service_name, "urn:nfc:sn:sdp",
1267 service_name_len)) {
1268 sap = 1;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001269 goto add_snl;
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001270 }
1271
Samuel Ortize6904082012-10-15 14:28:13 +02001272 llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
1273 service_name_len);
1274 if (!llcp_sock) {
1275 sap = 0;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001276 goto add_snl;
Samuel Ortize6904082012-10-15 14:28:13 +02001277 }
1278
1279 /*
1280 * We found a socket but its ssap has not been reserved
1281 * yet. We need to assign it for good and send a reply.
1282 * The ssap will be freed when the socket is closed.
1283 */
1284 if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1285 atomic_t *client_count;
1286
1287 sap = nfc_llcp_reserve_sdp_ssap(local);
1288
1289 pr_debug("Reserving %d\n", sap);
1290
1291 if (sap == LLCP_SAP_MAX) {
1292 sap = 0;
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001293 goto add_snl;
Samuel Ortize6904082012-10-15 14:28:13 +02001294 }
1295
1296 client_count =
1297 &local->local_sdp_cnt[sap -
1298 LLCP_WKS_NUM_SAP];
1299
1300 atomic_inc(client_count);
1301
1302 llcp_sock->ssap = sap;
1303 llcp_sock->reserved_ssap = sap;
1304 } else {
1305 sap = llcp_sock->ssap;
1306 }
1307
1308 pr_debug("%p %d\n", llcp_sock, sap);
1309
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001310add_snl:
1311 sdp = nfc_llcp_build_sdres_tlv(tid, sap);
1312 if (sdp == NULL)
1313 goto exit;
1314
1315 sdres_tlvs_len += sdp->tlv_len;
1316 hlist_add_head(&sdp->node, &llc_sdres_list);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001317 break;
1318
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001319 case LLCP_TLV_SDRES:
1320 mutex_lock(&local->sdreq_lock);
1321
1322 pr_debug("LLCP_TLV_SDRES: searching tid %d\n", tlv[2]);
1323
1324 hlist_for_each_entry(sdp, &local->pending_sdreqs, node) {
1325 if (sdp->tid != tlv[2])
1326 continue;
1327
1328 sdp->sap = tlv[3];
1329
1330 pr_debug("Found: uri=%s, sap=%d\n",
1331 sdp->uri, sdp->sap);
1332
1333 hlist_del(&sdp->node);
1334
1335 hlist_add_head(&sdp->node, &nl_sdres_list);
1336
1337 break;
1338 }
1339
1340 mutex_unlock(&local->sdreq_lock);
1341 break;
1342
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001343 default:
1344 pr_err("Invalid SNL tlv value 0x%x\n", type);
1345 break;
1346 }
1347
1348 offset += length + 2;
1349 tlv += length + 2;
1350 }
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001351
1352exit:
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001353 if (!hlist_empty(&nl_sdres_list))
1354 nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
1355
Thierry Escandee0ae7ba2013-02-15 10:43:05 +01001356 if (!hlist_empty(&llc_sdres_list))
1357 nfc_llcp_send_snl_sdres(local, &llc_sdres_list, sdres_tlvs_len);
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001358}
1359
Thierry Escande098dafc2013-03-29 11:47:43 +01001360static void nfc_llcp_recv_agf(struct nfc_llcp_local *local, struct sk_buff *skb)
Samuel Ortizd6469602011-12-14 16:43:12 +01001361{
Thierry Escande098dafc2013-03-29 11:47:43 +01001362 u8 ptype;
1363 u16 pdu_len;
1364 struct sk_buff *new_skb;
Samuel Ortizd6469602011-12-14 16:43:12 +01001365
Thierry Escande098dafc2013-03-29 11:47:43 +01001366 if (skb->len <= LLCP_HEADER_SIZE) {
1367 pr_err("Malformed AGF PDU\n");
Samuel Ortizd6469602011-12-14 16:43:12 +01001368 return;
1369 }
1370
Thierry Escande098dafc2013-03-29 11:47:43 +01001371 skb_pull(skb, LLCP_HEADER_SIZE);
1372
1373 while (skb->len > LLCP_AGF_PDU_HEADER_SIZE) {
1374 pdu_len = skb->data[0] << 8 | skb->data[1];
1375
1376 skb_pull(skb, LLCP_AGF_PDU_HEADER_SIZE);
1377
1378 if (pdu_len < LLCP_HEADER_SIZE || pdu_len > skb->len) {
1379 pr_err("Malformed AGF PDU\n");
1380 return;
1381 }
1382
1383 ptype = nfc_llcp_ptype(skb);
1384
1385 if (ptype == LLCP_PDU_SYMM || ptype == LLCP_PDU_AGF)
1386 goto next;
1387
1388 new_skb = nfc_alloc_recv_skb(pdu_len, GFP_KERNEL);
1389 if (new_skb == NULL) {
1390 pr_err("Could not allocate PDU\n");
1391 return;
1392 }
1393
1394 memcpy(skb_put(new_skb, pdu_len), skb->data, pdu_len);
1395
1396 nfc_llcp_rx_skb(local, new_skb);
1397
1398 kfree_skb(new_skb);
1399next:
1400 skb_pull(skb, pdu_len);
1401 }
1402}
1403
1404static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb)
1405{
1406 u8 dsap, ssap, ptype;
1407
Samuel Ortizd6469602011-12-14 16:43:12 +01001408 ptype = nfc_llcp_ptype(skb);
1409 dsap = nfc_llcp_dsap(skb);
1410 ssap = nfc_llcp_ssap(skb);
1411
1412 pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1413
Samuel Ortiz4be646e2012-04-10 19:43:13 +02001414 if (ptype != LLCP_PDU_SYMM)
1415 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
1416 16, 1, skb->data, skb->len, true);
1417
Samuel Ortizd6469602011-12-14 16:43:12 +01001418 switch (ptype) {
1419 case LLCP_PDU_SYMM:
1420 pr_debug("SYMM\n");
1421 break;
1422
Samuel Ortiz968272b2012-10-15 16:14:37 +02001423 case LLCP_PDU_UI:
1424 pr_debug("UI\n");
1425 nfc_llcp_recv_ui(local, skb);
1426 break;
1427
Samuel Ortizd6469602011-12-14 16:43:12 +01001428 case LLCP_PDU_CONNECT:
1429 pr_debug("CONNECT\n");
1430 nfc_llcp_recv_connect(local, skb);
1431 break;
1432
1433 case LLCP_PDU_DISC:
1434 pr_debug("DISC\n");
1435 nfc_llcp_recv_disc(local, skb);
1436 break;
1437
1438 case LLCP_PDU_CC:
1439 pr_debug("CC\n");
1440 nfc_llcp_recv_cc(local, skb);
1441 break;
1442
Samuel Ortiz5c0560b2012-06-26 16:13:29 +02001443 case LLCP_PDU_DM:
1444 pr_debug("DM\n");
1445 nfc_llcp_recv_dm(local, skb);
1446 break;
1447
Samuel Ortiz19cfe582012-10-05 01:21:47 +02001448 case LLCP_PDU_SNL:
1449 pr_debug("SNL\n");
1450 nfc_llcp_recv_snl(local, skb);
1451 break;
1452
Samuel Ortizd6469602011-12-14 16:43:12 +01001453 case LLCP_PDU_I:
1454 case LLCP_PDU_RR:
Samuel Ortiz53aef922012-03-05 01:03:36 +01001455 case LLCP_PDU_RNR:
Samuel Ortizd6469602011-12-14 16:43:12 +01001456 pr_debug("I frame\n");
1457 nfc_llcp_recv_hdlc(local, skb);
1458 break;
1459
Thierry Escande098dafc2013-03-29 11:47:43 +01001460 case LLCP_PDU_AGF:
1461 pr_debug("AGF frame\n");
1462 nfc_llcp_recv_agf(local, skb);
1463 break;
Samuel Ortizd6469602011-12-14 16:43:12 +01001464 }
Thierry Escande098dafc2013-03-29 11:47:43 +01001465}
1466
1467static void nfc_llcp_rx_work(struct work_struct *work)
1468{
1469 struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
1470 rx_work);
1471 struct sk_buff *skb;
1472
1473 skb = local->rx_pending;
1474 if (skb == NULL) {
1475 pr_debug("No pending SKB\n");
1476 return;
1477 }
1478
1479 __net_timestamp(skb);
1480
Hiren Tandel57be1f32014-05-05 19:43:31 +09001481 nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_RX);
Thierry Escande098dafc2013-03-29 11:47:43 +01001482
1483 nfc_llcp_rx_skb(local, skb);
Samuel Ortizd6469602011-12-14 16:43:12 +01001484
Linus Torvalds916082b2012-10-02 16:01:31 -07001485 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001486 kfree_skb(local->rx_pending);
1487 local->rx_pending = NULL;
Samuel Ortizd6469602011-12-14 16:43:12 +01001488}
1489
Samuel Ortiz1727cf92012-12-07 16:37:30 +01001490static void __nfc_llcp_recv(struct nfc_llcp_local *local, struct sk_buff *skb)
1491{
1492 local->rx_pending = skb;
1493 del_timer(&local->link_timer);
1494 schedule_work(&local->rx_work);
1495}
1496
Samuel Ortizd6469602011-12-14 16:43:12 +01001497void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1498{
1499 struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1500
1501 pr_debug("Received an LLCP PDU\n");
1502 if (err < 0) {
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001503 pr_err("err %d\n", err);
Samuel Ortizd6469602011-12-14 16:43:12 +01001504 return;
1505 }
1506
Samuel Ortiz1727cf92012-12-07 16:37:30 +01001507 __nfc_llcp_recv(local, skb);
Samuel Ortizd6469602011-12-14 16:43:12 +01001508}
1509
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001510int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1511{
1512 struct nfc_llcp_local *local;
1513
1514 local = nfc_llcp_find_local(dev);
Julien Lefrique966efbf2014-10-21 16:52:50 +02001515 if (local == NULL) {
1516 kfree_skb(skb);
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001517 return -ENODEV;
Julien Lefrique966efbf2014-10-21 16:52:50 +02001518 }
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001519
Samuel Ortiz1727cf92012-12-07 16:37:30 +01001520 __nfc_llcp_recv(local, skb);
Samuel Ortiz73167ce2012-05-31 00:05:50 +02001521
1522 return 0;
1523}
1524
Samuel Ortizd6469602011-12-14 16:43:12 +01001525void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1526{
1527 struct nfc_llcp_local *local;
1528
1529 local = nfc_llcp_find_local(dev);
1530 if (local == NULL)
1531 return;
1532
Thierry Escandeabd18d42013-04-02 10:25:15 +02001533 local->remote_miu = LLCP_DEFAULT_MIU;
1534 local->remote_lto = LLCP_DEFAULT_LTO;
1535
Samuel Ortizd6469602011-12-14 16:43:12 +01001536 /* Close and purge all existing sockets */
Samuel Ortiz3bbc0ce2013-02-21 17:01:06 +01001537 nfc_llcp_socket_release(local, true, 0);
Samuel Ortizd6469602011-12-14 16:43:12 +01001538}
1539
1540void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1541 u8 comm_mode, u8 rf_mode)
1542{
1543 struct nfc_llcp_local *local;
1544
1545 pr_debug("rf mode %d\n", rf_mode);
1546
1547 local = nfc_llcp_find_local(dev);
1548 if (local == NULL)
1549 return;
1550
1551 local->target_idx = target_idx;
1552 local->comm_mode = comm_mode;
1553 local->rf_mode = rf_mode;
1554
1555 if (rf_mode == NFC_RF_INITIATOR) {
1556 pr_debug("Queueing Tx work\n");
1557
Linus Torvalds916082b2012-10-02 16:01:31 -07001558 schedule_work(&local->tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001559 } else {
1560 mod_timer(&local->link_timer,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001561 jiffies + msecs_to_jiffies(local->remote_lto));
Samuel Ortizd6469602011-12-14 16:43:12 +01001562 }
1563}
1564
1565int nfc_llcp_register_device(struct nfc_dev *ndev)
1566{
Samuel Ortizd6469602011-12-14 16:43:12 +01001567 struct nfc_llcp_local *local;
Samuel Ortizd6469602011-12-14 16:43:12 +01001568
1569 local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
1570 if (local == NULL)
1571 return -ENOMEM;
1572
1573 local->dev = ndev;
1574 INIT_LIST_HEAD(&local->list);
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001575 kref_init(&local->ref);
Samuel Ortizd6469602011-12-14 16:43:12 +01001576 mutex_init(&local->sdp_lock);
Samuel Ortizd6469602011-12-14 16:43:12 +01001577 init_timer(&local->link_timer);
1578 local->link_timer.data = (unsigned long) local;
1579 local->link_timer.function = nfc_llcp_symm_timer;
1580
1581 skb_queue_head_init(&local->tx_queue);
1582 INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001583
1584 local->rx_pending = NULL;
1585 INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001586
1587 INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
Samuel Ortizd6469602011-12-14 16:43:12 +01001588
Szymon Jancfe235b52012-09-25 12:42:50 +02001589 rwlock_init(&local->sockets.lock);
1590 rwlock_init(&local->connecting_sockets.lock);
Thierry Escande4463523b2012-09-26 18:16:44 +02001591 rwlock_init(&local->raw_sockets.lock);
Samuel Ortiza69f32a2012-05-04 17:04:19 +02001592
Thierry Escande52feb442012-10-17 14:43:39 +02001593 local->lto = 150; /* 1500 ms */
1594 local->rw = LLCP_MAX_RW;
1595 local->miux = cpu_to_be16(LLCP_MAX_MIUX);
Samuel Ortizf768b342013-05-28 15:41:32 +02001596 local->local_wks = 0x1; /* LLC Link Management */
Thierry Escande52feb442012-10-17 14:43:39 +02001597
Samuel Ortizd6469602011-12-14 16:43:12 +01001598 nfc_llcp_build_gb(local);
1599
1600 local->remote_miu = LLCP_DEFAULT_MIU;
1601 local->remote_lto = LLCP_DEFAULT_LTO;
Samuel Ortizd6469602011-12-14 16:43:12 +01001602
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001603 mutex_init(&local->sdreq_lock);
1604 INIT_HLIST_HEAD(&local->pending_sdreqs);
Thierry Escande40213fa2013-03-04 15:43:32 +01001605 init_timer(&local->sdreq_timer);
1606 local->sdreq_timer.data = (unsigned long) local;
1607 local->sdreq_timer.function = nfc_llcp_sdreq_timer;
1608 INIT_WORK(&local->sdreq_timeout_work, nfc_llcp_sdreq_timeout_work);
Thierry Escanded9b8d8e2013-02-15 10:43:06 +01001609
Thierry Escande16a78e92012-10-12 15:25:43 +02001610 list_add(&local->list, &llcp_devices);
Samuel Ortizd6469602011-12-14 16:43:12 +01001611
1612 return 0;
Samuel Ortizd6469602011-12-14 16:43:12 +01001613}
1614
1615void nfc_llcp_unregister_device(struct nfc_dev *dev)
1616{
1617 struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1618
1619 if (local == NULL) {
1620 pr_debug("No such device\n");
1621 return;
1622 }
1623
Samuel Ortizc470e312013-04-03 16:40:52 +02001624 local_cleanup(local);
Samuel Ortiz3536da02013-02-21 15:40:04 +01001625
Samuel Ortizc7aa1222012-05-04 11:24:16 +02001626 nfc_llcp_local_put(local);
Samuel Ortizd6469602011-12-14 16:43:12 +01001627}
1628
1629int __init nfc_llcp_init(void)
1630{
Samuel Ortizd6469602011-12-14 16:43:12 +01001631 return nfc_llcp_sock_init();
1632}
1633
1634void nfc_llcp_exit(void)
1635{
1636 nfc_llcp_sock_exit();
1637}