Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-3.10.git] / net / nfc / llcp / llcp.c
1 /*
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
30 static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
31
32 static struct list_head llcp_devices;
33
34 void 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
41 void 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
48 static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock)
49 {
50         struct nfc_llcp_local *local = sock->local;
51         struct sk_buff *s, *tmp;
52
53         pr_debug("%p\n", &sock->sk);
54
55         skb_queue_purge(&sock->tx_queue);
56         skb_queue_purge(&sock->tx_pending_queue);
57         skb_queue_purge(&sock->tx_backlog_queue);
58
59         if (local == NULL)
60                 return;
61
62         /* Search for local pending SKBs that are related to this socket */
63         skb_queue_walk_safe(&local->tx_queue, s, tmp) {
64                 if (s->sk != &sock->sk)
65                         continue;
66
67                 skb_unlink(s, &local->tx_queue);
68                 kfree_skb(s);
69         }
70 }
71
72 static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool listen)
73 {
74         struct sock *sk;
75         struct hlist_node *node, *tmp;
76         struct nfc_llcp_sock *llcp_sock;
77
78         skb_queue_purge(&local->tx_queue);
79
80         write_lock(&local->sockets.lock);
81
82         sk_for_each_safe(sk, node, tmp, &local->sockets.head) {
83                 llcp_sock = nfc_llcp_sock(sk);
84
85                 bh_lock_sock(sk);
86
87                 nfc_llcp_socket_purge(llcp_sock);
88
89                 if (sk->sk_state == LLCP_CONNECTED)
90                         nfc_put_device(llcp_sock->dev);
91
92                 if (sk->sk_state == LLCP_LISTEN) {
93                         struct nfc_llcp_sock *lsk, *n;
94                         struct sock *accept_sk;
95
96                         list_for_each_entry_safe(lsk, n,
97                                                  &llcp_sock->accept_queue,
98                                                  accept_queue) {
99                                 accept_sk = &lsk->sk;
100                                 bh_lock_sock(accept_sk);
101
102                                 nfc_llcp_accept_unlink(accept_sk);
103
104                                 accept_sk->sk_state = LLCP_CLOSED;
105
106                                 bh_unlock_sock(accept_sk);
107
108                                 sock_orphan(accept_sk);
109                         }
110
111                         if (listen == true) {
112                                 bh_unlock_sock(sk);
113                                 continue;
114                         }
115                 }
116
117                 /*
118                  * If we have a connection less socket bound, we keep it alive
119                  * if the device is still present.
120                  */
121                 if (sk->sk_state == LLCP_BOUND && sk->sk_type == SOCK_DGRAM &&
122                     listen == true) {
123                         bh_unlock_sock(sk);
124                         continue;
125                 }
126
127                 sk->sk_state = LLCP_CLOSED;
128
129                 bh_unlock_sock(sk);
130
131                 sock_orphan(sk);
132
133                 sk_del_node_init(sk);
134         }
135
136         write_unlock(&local->sockets.lock);
137 }
138
139 struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
140 {
141         kref_get(&local->ref);
142
143         return local;
144 }
145
146 static void local_release(struct kref *ref)
147 {
148         struct nfc_llcp_local *local;
149
150         local = container_of(ref, struct nfc_llcp_local, ref);
151
152         list_del(&local->list);
153         nfc_llcp_socket_release(local, false);
154         del_timer_sync(&local->link_timer);
155         skb_queue_purge(&local->tx_queue);
156         cancel_work_sync(&local->tx_work);
157         cancel_work_sync(&local->rx_work);
158         cancel_work_sync(&local->timeout_work);
159         kfree_skb(local->rx_pending);
160         kfree(local);
161 }
162
163 int nfc_llcp_local_put(struct nfc_llcp_local *local)
164 {
165         if (local == NULL)
166                 return 0;
167
168         return kref_put(&local->ref, local_release);
169 }
170
171 static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
172                                                u8 ssap, u8 dsap)
173 {
174         struct sock *sk;
175         struct hlist_node *node;
176         struct nfc_llcp_sock *llcp_sock, *tmp_sock;
177
178         pr_debug("ssap dsap %d %d\n", ssap, dsap);
179
180         if (ssap == 0 && dsap == 0)
181                 return NULL;
182
183         read_lock(&local->sockets.lock);
184
185         llcp_sock = NULL;
186
187         sk_for_each(sk, node, &local->sockets.head) {
188                 tmp_sock = nfc_llcp_sock(sk);
189
190                 if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
191                         llcp_sock = tmp_sock;
192                         break;
193                 }
194         }
195
196         read_unlock(&local->sockets.lock);
197
198         if (llcp_sock == NULL)
199                 return NULL;
200
201         sock_hold(&llcp_sock->sk);
202
203         return llcp_sock;
204 }
205
206 static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
207 {
208         sock_put(&sock->sk);
209 }
210
211 static void nfc_llcp_timeout_work(struct work_struct *work)
212 {
213         struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
214                                                     timeout_work);
215
216         nfc_dep_link_down(local->dev);
217 }
218
219 static void nfc_llcp_symm_timer(unsigned long data)
220 {
221         struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
222
223         pr_err("SYMM timeout\n");
224
225         schedule_work(&local->timeout_work);
226 }
227
228 struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
229 {
230         struct nfc_llcp_local *local, *n;
231
232         list_for_each_entry_safe(local, n, &llcp_devices, list)
233                 if (local->dev == dev)
234                         return local;
235
236         pr_debug("No device found\n");
237
238         return NULL;
239 }
240
241 static char *wks[] = {
242         NULL,
243         NULL, /* SDP */
244         "urn:nfc:sn:ip",
245         "urn:nfc:sn:obex",
246         "urn:nfc:sn:snep",
247 };
248
249 static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
250 {
251         int sap, num_wks;
252
253         pr_debug("%s\n", service_name);
254
255         if (service_name == NULL)
256                 return -EINVAL;
257
258         num_wks = ARRAY_SIZE(wks);
259
260         for (sap = 0; sap < num_wks; sap++) {
261                 if (wks[sap] == NULL)
262                         continue;
263
264                 if (strncmp(wks[sap], service_name, service_name_len) == 0)
265                         return sap;
266         }
267
268         return -EINVAL;
269 }
270
271 static
272 struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
273                                             u8 *sn, size_t sn_len)
274 {
275         struct sock *sk;
276         struct hlist_node *node;
277         struct nfc_llcp_sock *llcp_sock, *tmp_sock;
278
279         pr_debug("sn %zd %p\n", sn_len, sn);
280
281         if (sn == NULL || sn_len == 0)
282                 return NULL;
283
284         read_lock(&local->sockets.lock);
285
286         llcp_sock = NULL;
287
288         sk_for_each(sk, node, &local->sockets.head) {
289                 tmp_sock = nfc_llcp_sock(sk);
290
291                 pr_debug("llcp sock %p\n", tmp_sock);
292
293                 if (tmp_sock->sk.sk_type == SOCK_STREAM &&
294                     tmp_sock->sk.sk_state != LLCP_LISTEN)
295                         continue;
296
297                 if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
298                     tmp_sock->sk.sk_state != LLCP_BOUND)
299                         continue;
300
301                 if (tmp_sock->service_name == NULL ||
302                     tmp_sock->service_name_len == 0)
303                         continue;
304
305                 if (tmp_sock->service_name_len != sn_len)
306                         continue;
307
308                 if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
309                         llcp_sock = tmp_sock;
310                         break;
311                 }
312         }
313
314         read_unlock(&local->sockets.lock);
315
316         pr_debug("Found llcp sock %p\n", llcp_sock);
317
318         return llcp_sock;
319 }
320
321 u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
322                          struct nfc_llcp_sock *sock)
323 {
324         mutex_lock(&local->sdp_lock);
325
326         if (sock->service_name != NULL && sock->service_name_len > 0) {
327                 int ssap = nfc_llcp_wks_sap(sock->service_name,
328                                             sock->service_name_len);
329
330                 if (ssap > 0) {
331                         pr_debug("WKS %d\n", ssap);
332
333                         /* This is a WKS, let's check if it's free */
334                         if (local->local_wks & BIT(ssap)) {
335                                 mutex_unlock(&local->sdp_lock);
336
337                                 return LLCP_SAP_MAX;
338                         }
339
340                         set_bit(ssap, &local->local_wks);
341                         mutex_unlock(&local->sdp_lock);
342
343                         return ssap;
344                 }
345
346                 /*
347                  * Check if there already is a non WKS socket bound
348                  * to this service name.
349                  */
350                 if (nfc_llcp_sock_from_sn(local, sock->service_name,
351                                           sock->service_name_len) != NULL) {
352                         mutex_unlock(&local->sdp_lock);
353
354                         return LLCP_SAP_MAX;
355                 }
356
357                 mutex_unlock(&local->sdp_lock);
358
359                 return LLCP_SDP_UNBOUND;
360
361         } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
362                 if (!test_bit(sock->ssap, &local->local_wks)) {
363                         set_bit(sock->ssap, &local->local_wks);
364                         mutex_unlock(&local->sdp_lock);
365
366                         return sock->ssap;
367                 }
368         }
369
370         mutex_unlock(&local->sdp_lock);
371
372         return LLCP_SAP_MAX;
373 }
374
375 u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
376 {
377         u8 local_ssap;
378
379         mutex_lock(&local->sdp_lock);
380
381         local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
382         if (local_ssap == LLCP_LOCAL_NUM_SAP) {
383                 mutex_unlock(&local->sdp_lock);
384                 return LLCP_SAP_MAX;
385         }
386
387         set_bit(local_ssap, &local->local_sap);
388
389         mutex_unlock(&local->sdp_lock);
390
391         return local_ssap + LLCP_LOCAL_SAP_OFFSET;
392 }
393
394 void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
395 {
396         u8 local_ssap;
397         unsigned long *sdp;
398
399         if (ssap < LLCP_WKS_NUM_SAP) {
400                 local_ssap = ssap;
401                 sdp = &local->local_wks;
402         } else if (ssap < LLCP_LOCAL_NUM_SAP) {
403                 atomic_t *client_cnt;
404
405                 local_ssap = ssap - LLCP_WKS_NUM_SAP;
406                 sdp = &local->local_sdp;
407                 client_cnt = &local->local_sdp_cnt[local_ssap];
408
409                 pr_debug("%d clients\n", atomic_read(client_cnt));
410
411                 mutex_lock(&local->sdp_lock);
412
413                 if (atomic_dec_and_test(client_cnt)) {
414                         struct nfc_llcp_sock *l_sock;
415
416                         pr_debug("No more clients for SAP %d\n", ssap);
417
418                         clear_bit(local_ssap, sdp);
419
420                         /* Find the listening sock and set it back to UNBOUND */
421                         l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
422                         if (l_sock) {
423                                 l_sock->ssap = LLCP_SDP_UNBOUND;
424                                 nfc_llcp_sock_put(l_sock);
425                         }
426                 }
427
428                 mutex_unlock(&local->sdp_lock);
429
430                 return;
431         } else if (ssap < LLCP_MAX_SAP) {
432                 local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
433                 sdp = &local->local_sap;
434         } else {
435                 return;
436         }
437
438         mutex_lock(&local->sdp_lock);
439
440         clear_bit(local_ssap, sdp);
441
442         mutex_unlock(&local->sdp_lock);
443 }
444
445 static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
446 {
447         u8 ssap;
448
449         mutex_lock(&local->sdp_lock);
450
451         ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
452         if (ssap == LLCP_SDP_NUM_SAP) {
453                 mutex_unlock(&local->sdp_lock);
454
455                 return LLCP_SAP_MAX;
456         }
457
458         pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
459
460         set_bit(ssap, &local->local_sdp);
461
462         mutex_unlock(&local->sdp_lock);
463
464         return LLCP_WKS_NUM_SAP + ssap;
465 }
466
467 static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
468 {
469         u8 *gb_cur, *version_tlv, version, version_length;
470         u8 *lto_tlv, lto_length;
471         u8 *wks_tlv, wks_length;
472         u8 *miux_tlv, miux_length;
473         u8 gb_len = 0;
474         int ret = 0;
475
476         version = LLCP_VERSION_11;
477         version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
478                                          1, &version_length);
479         gb_len += version_length;
480
481         lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, &lto_length);
482         gb_len += lto_length;
483
484         pr_debug("Local wks 0x%lx\n", local->local_wks);
485         wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&local->local_wks, 2,
486                                      &wks_length);
487         gb_len += wks_length;
488
489         miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
490                                       &miux_length);
491         gb_len += miux_length;
492
493         gb_len += ARRAY_SIZE(llcp_magic);
494
495         if (gb_len > NFC_MAX_GT_LEN) {
496                 ret = -EINVAL;
497                 goto out;
498         }
499
500         gb_cur = local->gb;
501
502         memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
503         gb_cur += ARRAY_SIZE(llcp_magic);
504
505         memcpy(gb_cur, version_tlv, version_length);
506         gb_cur += version_length;
507
508         memcpy(gb_cur, lto_tlv, lto_length);
509         gb_cur += lto_length;
510
511         memcpy(gb_cur, wks_tlv, wks_length);
512         gb_cur += wks_length;
513
514         memcpy(gb_cur, miux_tlv, miux_length);
515         gb_cur += miux_length;
516
517         local->gb_len = gb_len;
518
519 out:
520         kfree(version_tlv);
521         kfree(lto_tlv);
522         kfree(wks_tlv);
523         kfree(miux_tlv);
524
525         return ret;
526 }
527
528 u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
529 {
530         struct nfc_llcp_local *local;
531
532         local = nfc_llcp_find_local(dev);
533         if (local == NULL) {
534                 *general_bytes_len = 0;
535                 return NULL;
536         }
537
538         nfc_llcp_build_gb(local);
539
540         *general_bytes_len = local->gb_len;
541
542         return local->gb;
543 }
544
545 int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
546 {
547         struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
548
549         if (local == NULL) {
550                 pr_err("No LLCP device\n");
551                 return -ENODEV;
552         }
553
554         memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
555         memcpy(local->remote_gb, gb, gb_len);
556         local->remote_gb_len = gb_len;
557
558         if (local->remote_gb == NULL || local->remote_gb_len == 0)
559                 return -ENODEV;
560
561         if (memcmp(local->remote_gb, llcp_magic, 3)) {
562                 pr_err("MAC does not support LLCP\n");
563                 return -EINVAL;
564         }
565
566         return nfc_llcp_parse_gb_tlv(local,
567                                      &local->remote_gb[3],
568                                      local->remote_gb_len - 3);
569 }
570
571 static u8 nfc_llcp_dsap(struct sk_buff *pdu)
572 {
573         return (pdu->data[0] & 0xfc) >> 2;
574 }
575
576 static u8 nfc_llcp_ptype(struct sk_buff *pdu)
577 {
578         return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
579 }
580
581 static u8 nfc_llcp_ssap(struct sk_buff *pdu)
582 {
583         return pdu->data[1] & 0x3f;
584 }
585
586 static u8 nfc_llcp_ns(struct sk_buff *pdu)
587 {
588         return pdu->data[2] >> 4;
589 }
590
591 static u8 nfc_llcp_nr(struct sk_buff *pdu)
592 {
593         return pdu->data[2] & 0xf;
594 }
595
596 static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
597 {
598         pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
599         sock->send_n = (sock->send_n + 1) % 16;
600         sock->recv_ack_n = (sock->recv_n - 1) % 16;
601 }
602
603 void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
604                                struct sk_buff *skb, u8 direction)
605 {
606         struct hlist_node *node;
607         struct sk_buff *skb_copy = NULL, *nskb;
608         struct sock *sk;
609         u8 *data;
610
611         read_lock(&local->raw_sockets.lock);
612
613         sk_for_each(sk, node, &local->raw_sockets.head) {
614                 if (sk->sk_state != LLCP_BOUND)
615                         continue;
616
617                 if (skb_copy == NULL) {
618                         skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE,
619                                                GFP_ATOMIC);
620
621                         if (skb_copy == NULL)
622                                 continue;
623
624                         data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE);
625
626                         data[0] = local->dev ? local->dev->idx : 0xFF;
627                         data[1] = direction;
628                 }
629
630                 nskb = skb_clone(skb_copy, GFP_ATOMIC);
631                 if (!nskb)
632                         continue;
633
634                 if (sock_queue_rcv_skb(sk, nskb))
635                         kfree_skb(nskb);
636         }
637
638         read_unlock(&local->raw_sockets.lock);
639
640         kfree_skb(skb_copy);
641 }
642
643 static void nfc_llcp_tx_work(struct work_struct *work)
644 {
645         struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
646                                                     tx_work);
647         struct sk_buff *skb;
648         struct sock *sk;
649         struct nfc_llcp_sock *llcp_sock;
650
651         skb = skb_dequeue(&local->tx_queue);
652         if (skb != NULL) {
653                 sk = skb->sk;
654                 llcp_sock = nfc_llcp_sock(sk);
655
656                 if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
657                         nfc_llcp_send_symm(local->dev);
658                 } else {
659                         int ret;
660
661                         pr_debug("Sending pending skb\n");
662                         print_hex_dump(KERN_DEBUG, "LLCP Tx: ",
663                                        DUMP_PREFIX_OFFSET, 16, 1,
664                                        skb->data, skb->len, true);
665
666                         nfc_llcp_send_to_raw_sock(local, skb,
667                                                   NFC_LLCP_DIRECTION_TX);
668
669                         ret = nfc_data_exchange(local->dev, local->target_idx,
670                                                 skb, nfc_llcp_recv, local);
671
672                         if (!ret && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
673                                 skb = skb_get(skb);
674                                 skb_queue_tail(&llcp_sock->tx_pending_queue,
675                                                skb);
676                         }
677                 }
678         } else {
679                 nfc_llcp_send_symm(local->dev);
680         }
681
682         mod_timer(&local->link_timer,
683                   jiffies + msecs_to_jiffies(2 * local->remote_lto));
684 }
685
686 static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
687                                                           u8 ssap)
688 {
689         struct sock *sk;
690         struct nfc_llcp_sock *llcp_sock;
691         struct hlist_node *node;
692
693         read_lock(&local->connecting_sockets.lock);
694
695         sk_for_each(sk, node, &local->connecting_sockets.head) {
696                 llcp_sock = nfc_llcp_sock(sk);
697
698                 if (llcp_sock->ssap == ssap) {
699                         sock_hold(&llcp_sock->sk);
700                         goto out;
701                 }
702         }
703
704         llcp_sock = NULL;
705
706 out:
707         read_unlock(&local->connecting_sockets.lock);
708
709         return llcp_sock;
710 }
711
712 static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
713                                                   u8 *sn, size_t sn_len)
714 {
715         struct nfc_llcp_sock *llcp_sock;
716
717         llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
718
719         if (llcp_sock == NULL)
720                 return NULL;
721
722         sock_hold(&llcp_sock->sk);
723
724         return llcp_sock;
725 }
726
727 static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
728 {
729         u8 *tlv = &skb->data[2], type, length;
730         size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
731
732         while (offset < tlv_array_len) {
733                 type = tlv[0];
734                 length = tlv[1];
735
736                 pr_debug("type 0x%x length %d\n", type, length);
737
738                 if (type == LLCP_TLV_SN) {
739                         *sn_len = length;
740                         return &tlv[2];
741                 }
742
743                 offset += length + 2;
744                 tlv += length + 2;
745         }
746
747         return NULL;
748 }
749
750 static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
751                              struct sk_buff *skb)
752 {
753         struct nfc_llcp_sock *llcp_sock;
754         struct nfc_llcp_ui_cb *ui_cb;
755         u8 dsap, ssap;
756
757         dsap = nfc_llcp_dsap(skb);
758         ssap = nfc_llcp_ssap(skb);
759
760         ui_cb = nfc_llcp_ui_skb_cb(skb);
761         ui_cb->dsap = dsap;
762         ui_cb->ssap = ssap;
763
764         printk("%s %d %d\n", __func__, dsap, ssap);
765
766         pr_debug("%d %d\n", dsap, ssap);
767
768         /* We're looking for a bound socket, not a client one */
769         llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
770         if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
771                 return;
772
773         /* There is no sequence with UI frames */
774         skb_pull(skb, LLCP_HEADER_SIZE);
775         if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
776                 pr_err("receive queue is full\n");
777                 skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
778         }
779
780         nfc_llcp_sock_put(llcp_sock);
781 }
782
783 static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
784                                   struct sk_buff *skb)
785 {
786         struct sock *new_sk, *parent;
787         struct nfc_llcp_sock *sock, *new_sock;
788         u8 dsap, ssap, reason;
789
790         dsap = nfc_llcp_dsap(skb);
791         ssap = nfc_llcp_ssap(skb);
792
793         pr_debug("%d %d\n", dsap, ssap);
794
795         if (dsap != LLCP_SAP_SDP) {
796                 sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
797                 if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
798                         reason = LLCP_DM_NOBOUND;
799                         goto fail;
800                 }
801         } else {
802                 u8 *sn;
803                 size_t sn_len;
804
805                 sn = nfc_llcp_connect_sn(skb, &sn_len);
806                 if (sn == NULL) {
807                         reason = LLCP_DM_NOBOUND;
808                         goto fail;
809                 }
810
811                 pr_debug("Service name length %zu\n", sn_len);
812
813                 sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
814                 if (sock == NULL) {
815                         reason = LLCP_DM_NOBOUND;
816                         goto fail;
817                 }
818         }
819
820         lock_sock(&sock->sk);
821
822         parent = &sock->sk;
823
824         if (sk_acceptq_is_full(parent)) {
825                 reason = LLCP_DM_REJ;
826                 release_sock(&sock->sk);
827                 sock_put(&sock->sk);
828                 goto fail;
829         }
830
831         if (sock->ssap == LLCP_SDP_UNBOUND) {
832                 u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
833
834                 pr_debug("First client, reserving %d\n", ssap);
835
836                 if (ssap == LLCP_SAP_MAX) {
837                         reason = LLCP_DM_REJ;
838                         release_sock(&sock->sk);
839                         sock_put(&sock->sk);
840                         goto fail;
841                 }
842
843                 sock->ssap = ssap;
844         }
845
846         new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC);
847         if (new_sk == NULL) {
848                 reason = LLCP_DM_REJ;
849                 release_sock(&sock->sk);
850                 sock_put(&sock->sk);
851                 goto fail;
852         }
853
854         new_sock = nfc_llcp_sock(new_sk);
855         new_sock->dev = local->dev;
856         new_sock->local = nfc_llcp_local_get(local);
857         new_sock->miu = local->remote_miu;
858         new_sock->nfc_protocol = sock->nfc_protocol;
859         new_sock->dsap = ssap;
860         new_sock->target_idx = local->target_idx;
861         new_sock->parent = parent;
862         new_sock->ssap = sock->ssap;
863         if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
864                 atomic_t *client_count;
865
866                 pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
867
868                 client_count =
869                         &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
870
871                 atomic_inc(client_count);
872                 new_sock->reserved_ssap = sock->ssap;
873         }
874
875         nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
876                                       skb->len - LLCP_HEADER_SIZE);
877
878         pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
879
880         nfc_llcp_sock_link(&local->sockets, new_sk);
881
882         nfc_llcp_accept_enqueue(&sock->sk, new_sk);
883
884         nfc_get_device(local->dev->idx);
885
886         new_sk->sk_state = LLCP_CONNECTED;
887
888         /* Wake the listening processes */
889         parent->sk_data_ready(parent, 0);
890
891         /* Send CC */
892         nfc_llcp_send_cc(new_sock);
893
894         release_sock(&sock->sk);
895         sock_put(&sock->sk);
896
897         return;
898
899 fail:
900         /* Send DM */
901         nfc_llcp_send_dm(local, dsap, ssap, reason);
902 }
903
904 int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
905 {
906         int nr_frames = 0;
907         struct nfc_llcp_local *local = sock->local;
908
909         pr_debug("Remote ready %d tx queue len %d remote rw %d",
910                  sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
911                  sock->rw);
912
913         /* Try to queue some I frames for transmission */
914         while (sock->remote_ready &&
915                skb_queue_len(&sock->tx_pending_queue) < sock->rw) {
916                 struct sk_buff *pdu;
917
918                 pdu = skb_dequeue(&sock->tx_queue);
919                 if (pdu == NULL)
920                         break;
921
922                 /* Update N(S)/N(R) */
923                 nfc_llcp_set_nrns(sock, pdu);
924
925                 skb_queue_tail(&local->tx_queue, pdu);
926                 nr_frames++;
927         }
928
929         return nr_frames;
930 }
931
932 static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
933                                struct sk_buff *skb)
934 {
935         struct nfc_llcp_sock *llcp_sock;
936         struct sock *sk;
937         u8 dsap, ssap, ptype, ns, nr;
938
939         ptype = nfc_llcp_ptype(skb);
940         dsap = nfc_llcp_dsap(skb);
941         ssap = nfc_llcp_ssap(skb);
942         ns = nfc_llcp_ns(skb);
943         nr = nfc_llcp_nr(skb);
944
945         pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
946
947         llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
948         if (llcp_sock == NULL) {
949                 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
950                 return;
951         }
952
953         sk = &llcp_sock->sk;
954         lock_sock(sk);
955         if (sk->sk_state == LLCP_CLOSED) {
956                 release_sock(sk);
957                 nfc_llcp_sock_put(llcp_sock);
958         }
959
960         /* Pass the payload upstream */
961         if (ptype == LLCP_PDU_I) {
962                 pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
963
964                 if (ns == llcp_sock->recv_n)
965                         llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
966                 else
967                         pr_err("Received out of sequence I PDU\n");
968
969                 skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
970                 if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
971                         pr_err("receive queue is full\n");
972                         skb_queue_head(&llcp_sock->tx_backlog_queue, skb);
973                 }
974         }
975
976         /* Remove skbs from the pending queue */
977         if (llcp_sock->send_ack_n != nr) {
978                 struct sk_buff *s, *tmp;
979
980                 llcp_sock->send_ack_n = nr;
981
982                 /* Remove and free all skbs until ns == nr */
983                 skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
984                         skb_unlink(s, &llcp_sock->tx_pending_queue);
985                         kfree_skb(s);
986
987                         if (nfc_llcp_ns(s) == nr)
988                                 break;
989                 }
990
991                 /* Re-queue the remaining skbs for transmission */
992                 skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
993                                             s, tmp) {
994                         skb_unlink(s, &llcp_sock->tx_pending_queue);
995                         skb_queue_head(&local->tx_queue, s);
996                 }
997         }
998
999         if (ptype == LLCP_PDU_RR)
1000                 llcp_sock->remote_ready = true;
1001         else if (ptype == LLCP_PDU_RNR)
1002                 llcp_sock->remote_ready = false;
1003
1004         if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
1005                 nfc_llcp_send_rr(llcp_sock);
1006
1007         release_sock(sk);
1008         nfc_llcp_sock_put(llcp_sock);
1009 }
1010
1011 static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
1012                                struct sk_buff *skb)
1013 {
1014         struct nfc_llcp_sock *llcp_sock;
1015         struct sock *sk;
1016         u8 dsap, ssap;
1017
1018         dsap = nfc_llcp_dsap(skb);
1019         ssap = nfc_llcp_ssap(skb);
1020
1021         llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1022         if (llcp_sock == NULL) {
1023                 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1024                 return;
1025         }
1026
1027         sk = &llcp_sock->sk;
1028         lock_sock(sk);
1029
1030         nfc_llcp_socket_purge(llcp_sock);
1031
1032         if (sk->sk_state == LLCP_CLOSED) {
1033                 release_sock(sk);
1034                 nfc_llcp_sock_put(llcp_sock);
1035         }
1036
1037         if (sk->sk_state == LLCP_CONNECTED) {
1038                 nfc_put_device(local->dev);
1039                 sk->sk_state = LLCP_CLOSED;
1040                 sk->sk_state_change(sk);
1041         }
1042
1043         nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
1044
1045         release_sock(sk);
1046         nfc_llcp_sock_put(llcp_sock);
1047 }
1048
1049 static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
1050 {
1051         struct nfc_llcp_sock *llcp_sock;
1052         struct sock *sk;
1053         u8 dsap, ssap;
1054
1055         dsap = nfc_llcp_dsap(skb);
1056         ssap = nfc_llcp_ssap(skb);
1057
1058         llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1059         if (llcp_sock == NULL) {
1060                 pr_err("Invalid CC\n");
1061                 nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
1062
1063                 return;
1064         }
1065
1066         sk = &llcp_sock->sk;
1067
1068         /* Unlink from connecting and link to the client array */
1069         nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
1070         nfc_llcp_sock_link(&local->sockets, sk);
1071         llcp_sock->dsap = ssap;
1072
1073         nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
1074                                       skb->len - LLCP_HEADER_SIZE);
1075
1076         sk->sk_state = LLCP_CONNECTED;
1077         sk->sk_state_change(sk);
1078
1079         nfc_llcp_sock_put(llcp_sock);
1080 }
1081
1082 static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
1083 {
1084         struct nfc_llcp_sock *llcp_sock;
1085         struct sock *sk;
1086         u8 dsap, ssap, reason;
1087
1088         dsap = nfc_llcp_dsap(skb);
1089         ssap = nfc_llcp_ssap(skb);
1090         reason = skb->data[2];
1091
1092         pr_debug("%d %d reason %d\n", ssap, dsap, reason);
1093
1094         switch (reason) {
1095         case LLCP_DM_NOBOUND:
1096         case LLCP_DM_REJ:
1097                 llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
1098                 break;
1099
1100         default:
1101                 llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
1102                 break;
1103         }
1104
1105         if (llcp_sock == NULL) {
1106                 pr_debug("Already closed\n");
1107                 return;
1108         }
1109
1110         sk = &llcp_sock->sk;
1111
1112         sk->sk_err = ENXIO;
1113         sk->sk_state = LLCP_CLOSED;
1114         sk->sk_state_change(sk);
1115
1116         nfc_llcp_sock_put(llcp_sock);
1117 }
1118
1119 static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
1120                               struct sk_buff *skb)
1121 {
1122         struct nfc_llcp_sock *llcp_sock;
1123         u8 dsap, ssap, *tlv, type, length, tid, sap;
1124         u16 tlv_len, offset;
1125         char *service_name;
1126         size_t service_name_len;
1127
1128         dsap = nfc_llcp_dsap(skb);
1129         ssap = nfc_llcp_ssap(skb);
1130
1131         pr_debug("%d %d\n", dsap, ssap);
1132
1133         if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
1134                 pr_err("Wrong SNL SAP\n");
1135                 return;
1136         }
1137
1138         tlv = &skb->data[LLCP_HEADER_SIZE];
1139         tlv_len = skb->len - LLCP_HEADER_SIZE;
1140         offset = 0;
1141
1142         while (offset < tlv_len) {
1143                 type = tlv[0];
1144                 length = tlv[1];
1145
1146                 switch (type) {
1147                 case LLCP_TLV_SDREQ:
1148                         tid = tlv[2];
1149                         service_name = (char *) &tlv[3];
1150                         service_name_len = length - 1;
1151
1152                         pr_debug("Looking for %.16s\n", service_name);
1153
1154                         if (service_name_len == strlen("urn:nfc:sn:sdp") &&
1155                             !strncmp(service_name, "urn:nfc:sn:sdp",
1156                                      service_name_len)) {
1157                                 sap = 1;
1158                                 goto send_snl;
1159                         }
1160
1161                         llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
1162                                                           service_name_len);
1163                         if (!llcp_sock) {
1164                                 sap = 0;
1165                                 goto send_snl;
1166                         }
1167
1168                         /*
1169                          * We found a socket but its ssap has not been reserved
1170                          * yet. We need to assign it for good and send a reply.
1171                          * The ssap will be freed when the socket is closed.
1172                          */
1173                         if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
1174                                 atomic_t *client_count;
1175
1176                                 sap = nfc_llcp_reserve_sdp_ssap(local);
1177
1178                                 pr_debug("Reserving %d\n", sap);
1179
1180                                 if (sap == LLCP_SAP_MAX) {
1181                                         sap = 0;
1182                                         goto send_snl;
1183                                 }
1184
1185                                 client_count =
1186                                         &local->local_sdp_cnt[sap -
1187                                                               LLCP_WKS_NUM_SAP];
1188
1189                                 atomic_inc(client_count);
1190
1191                                 llcp_sock->ssap = sap;
1192                                 llcp_sock->reserved_ssap = sap;
1193                         } else {
1194                                 sap = llcp_sock->ssap;
1195                         }
1196
1197                         pr_debug("%p %d\n", llcp_sock, sap);
1198
1199 send_snl:
1200                         nfc_llcp_send_snl(local, tid, sap);
1201                         break;
1202
1203                 default:
1204                         pr_err("Invalid SNL tlv value 0x%x\n", type);
1205                         break;
1206                 }
1207
1208                 offset += length + 2;
1209                 tlv += length + 2;
1210         }
1211 }
1212
1213 static void nfc_llcp_rx_work(struct work_struct *work)
1214 {
1215         struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
1216                                                     rx_work);
1217         u8 dsap, ssap, ptype;
1218         struct sk_buff *skb;
1219
1220         skb = local->rx_pending;
1221         if (skb == NULL) {
1222                 pr_debug("No pending SKB\n");
1223                 return;
1224         }
1225
1226         ptype = nfc_llcp_ptype(skb);
1227         dsap = nfc_llcp_dsap(skb);
1228         ssap = nfc_llcp_ssap(skb);
1229
1230         pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
1231
1232         if (ptype != LLCP_PDU_SYMM)
1233                 print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
1234                                16, 1, skb->data, skb->len, true);
1235
1236         nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX);
1237
1238         switch (ptype) {
1239         case LLCP_PDU_SYMM:
1240                 pr_debug("SYMM\n");
1241                 break;
1242
1243         case LLCP_PDU_UI:
1244                 pr_debug("UI\n");
1245                 nfc_llcp_recv_ui(local, skb);
1246                 break;
1247
1248         case LLCP_PDU_CONNECT:
1249                 pr_debug("CONNECT\n");
1250                 nfc_llcp_recv_connect(local, skb);
1251                 break;
1252
1253         case LLCP_PDU_DISC:
1254                 pr_debug("DISC\n");
1255                 nfc_llcp_recv_disc(local, skb);
1256                 break;
1257
1258         case LLCP_PDU_CC:
1259                 pr_debug("CC\n");
1260                 nfc_llcp_recv_cc(local, skb);
1261                 break;
1262
1263         case LLCP_PDU_DM:
1264                 pr_debug("DM\n");
1265                 nfc_llcp_recv_dm(local, skb);
1266                 break;
1267
1268         case LLCP_PDU_SNL:
1269                 pr_debug("SNL\n");
1270                 nfc_llcp_recv_snl(local, skb);
1271                 break;
1272
1273         case LLCP_PDU_I:
1274         case LLCP_PDU_RR:
1275         case LLCP_PDU_RNR:
1276                 pr_debug("I frame\n");
1277                 nfc_llcp_recv_hdlc(local, skb);
1278                 break;
1279
1280         }
1281
1282         schedule_work(&local->tx_work);
1283         kfree_skb(local->rx_pending);
1284         local->rx_pending = NULL;
1285 }
1286
1287 void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
1288 {
1289         struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
1290
1291         pr_debug("Received an LLCP PDU\n");
1292         if (err < 0) {
1293                 pr_err("err %d\n", err);
1294                 return;
1295         }
1296
1297         local->rx_pending = skb_get(skb);
1298         del_timer(&local->link_timer);
1299         schedule_work(&local->rx_work);
1300 }
1301
1302 int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
1303 {
1304         struct nfc_llcp_local *local;
1305
1306         local = nfc_llcp_find_local(dev);
1307         if (local == NULL)
1308                 return -ENODEV;
1309
1310         local->rx_pending = skb_get(skb);
1311         del_timer(&local->link_timer);
1312         schedule_work(&local->rx_work);
1313
1314         return 0;
1315 }
1316
1317 void nfc_llcp_mac_is_down(struct nfc_dev *dev)
1318 {
1319         struct nfc_llcp_local *local;
1320
1321         local = nfc_llcp_find_local(dev);
1322         if (local == NULL)
1323                 return;
1324
1325         /* Close and purge all existing sockets */
1326         nfc_llcp_socket_release(local, true);
1327 }
1328
1329 void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
1330                         u8 comm_mode, u8 rf_mode)
1331 {
1332         struct nfc_llcp_local *local;
1333
1334         pr_debug("rf mode %d\n", rf_mode);
1335
1336         local = nfc_llcp_find_local(dev);
1337         if (local == NULL)
1338                 return;
1339
1340         local->target_idx = target_idx;
1341         local->comm_mode = comm_mode;
1342         local->rf_mode = rf_mode;
1343
1344         if (rf_mode == NFC_RF_INITIATOR) {
1345                 pr_debug("Queueing Tx work\n");
1346
1347                 schedule_work(&local->tx_work);
1348         } else {
1349                 mod_timer(&local->link_timer,
1350                           jiffies + msecs_to_jiffies(local->remote_lto));
1351         }
1352 }
1353
1354 int nfc_llcp_register_device(struct nfc_dev *ndev)
1355 {
1356         struct nfc_llcp_local *local;
1357
1358         local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
1359         if (local == NULL)
1360                 return -ENOMEM;
1361
1362         local->dev = ndev;
1363         INIT_LIST_HEAD(&local->list);
1364         kref_init(&local->ref);
1365         mutex_init(&local->sdp_lock);
1366         init_timer(&local->link_timer);
1367         local->link_timer.data = (unsigned long) local;
1368         local->link_timer.function = nfc_llcp_symm_timer;
1369
1370         skb_queue_head_init(&local->tx_queue);
1371         INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
1372
1373         local->rx_pending = NULL;
1374         INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
1375
1376         INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
1377
1378         rwlock_init(&local->sockets.lock);
1379         rwlock_init(&local->connecting_sockets.lock);
1380         rwlock_init(&local->raw_sockets.lock);
1381
1382         local->lto = 150; /* 1500 ms */
1383         local->rw = LLCP_MAX_RW;
1384         local->miux = cpu_to_be16(LLCP_MAX_MIUX);
1385
1386         nfc_llcp_build_gb(local);
1387
1388         local->remote_miu = LLCP_DEFAULT_MIU;
1389         local->remote_lto = LLCP_DEFAULT_LTO;
1390
1391         list_add(&local->list, &llcp_devices);
1392
1393         return 0;
1394 }
1395
1396 void nfc_llcp_unregister_device(struct nfc_dev *dev)
1397 {
1398         struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
1399
1400         if (local == NULL) {
1401                 pr_debug("No such device\n");
1402                 return;
1403         }
1404
1405         nfc_llcp_local_put(local);
1406 }
1407
1408 int __init nfc_llcp_init(void)
1409 {
1410         INIT_LIST_HEAD(&llcp_devices);
1411
1412         return nfc_llcp_sock_init();
1413 }
1414
1415 void nfc_llcp_exit(void)
1416 {
1417         nfc_llcp_sock_exit();
1418 }