9119898ef0409ef144e9fa3f13773782c3648c32
[platform/adaptation/renesas_rcar/renesas_kernel.git] / net / bluetooth / l2cap_sock.c
1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (C) 2000-2001 Qualcomm Incorporated
4    Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
5    Copyright (C) 2010 Google Inc.
6    Copyright (C) 2011 ProFUSION Embedded Systems
7
8    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License version 2 as
12    published by the Free Software Foundation;
13
14    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
17    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
18    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
19    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
24    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
25    SOFTWARE IS DISCLAIMED.
26 */
27
28 /* Bluetooth L2CAP sockets. */
29
30 #include <linux/export.h>
31
32 #include <net/bluetooth/bluetooth.h>
33 #include <net/bluetooth/hci_core.h>
34 #include <net/bluetooth/l2cap.h>
35 #include <net/bluetooth/smp.h>
36
37 static struct bt_sock_list l2cap_sk_list = {
38         .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock)
39 };
40
41 static const struct proto_ops l2cap_sock_ops;
42 static void l2cap_sock_init(struct sock *sk, struct sock *parent);
43 static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
44                                      int proto, gfp_t prio);
45
46 bool l2cap_is_socket(struct socket *sock)
47 {
48         return sock && sock->ops == &l2cap_sock_ops;
49 }
50 EXPORT_SYMBOL(l2cap_is_socket);
51
52 static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
53 {
54         struct sock *sk = sock->sk;
55         struct l2cap_chan *chan = l2cap_pi(sk)->chan;
56         struct sockaddr_l2 la;
57         int len, err = 0;
58
59         BT_DBG("sk %p", sk);
60
61         if (!addr || addr->sa_family != AF_BLUETOOTH)
62                 return -EINVAL;
63
64         memset(&la, 0, sizeof(la));
65         len = min_t(unsigned int, sizeof(la), alen);
66         memcpy(&la, addr, len);
67
68         if (la.l2_cid && la.l2_psm)
69                 return -EINVAL;
70
71         lock_sock(sk);
72
73         if (sk->sk_state != BT_OPEN) {
74                 err = -EBADFD;
75                 goto done;
76         }
77
78         if (la.l2_psm) {
79                 __u16 psm = __le16_to_cpu(la.l2_psm);
80
81                 /* PSM must be odd and lsb of upper byte must be 0 */
82                 if ((psm & 0x0101) != 0x0001) {
83                         err = -EINVAL;
84                         goto done;
85                 }
86
87                 /* Restrict usage of well-known PSMs */
88                 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
89                         err = -EACCES;
90                         goto done;
91                 }
92         }
93
94         if (la.l2_cid)
95                 err = l2cap_add_scid(chan, __le16_to_cpu(la.l2_cid));
96         else
97                 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
98
99         if (err < 0)
100                 goto done;
101
102         if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP ||
103             __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM)
104                 chan->sec_level = BT_SECURITY_SDP;
105
106         bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
107
108         chan->state = BT_BOUND;
109         sk->sk_state = BT_BOUND;
110
111 done:
112         release_sock(sk);
113         return err;
114 }
115
116 static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr,
117                               int alen, int flags)
118 {
119         struct sock *sk = sock->sk;
120         struct l2cap_chan *chan = l2cap_pi(sk)->chan;
121         struct sockaddr_l2 la;
122         int len, err = 0;
123
124         BT_DBG("sk %p", sk);
125
126         if (!addr || alen < sizeof(addr->sa_family) ||
127             addr->sa_family != AF_BLUETOOTH)
128                 return -EINVAL;
129
130         memset(&la, 0, sizeof(la));
131         len = min_t(unsigned int, sizeof(la), alen);
132         memcpy(&la, addr, len);
133
134         if (la.l2_cid && la.l2_psm)
135                 return -EINVAL;
136
137         err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid),
138                                  &la.l2_bdaddr, la.l2_bdaddr_type);
139         if (err)
140                 return err;
141
142         lock_sock(sk);
143
144         err = bt_sock_wait_state(sk, BT_CONNECTED,
145                                  sock_sndtimeo(sk, flags & O_NONBLOCK));
146
147         release_sock(sk);
148
149         return err;
150 }
151
152 static int l2cap_sock_listen(struct socket *sock, int backlog)
153 {
154         struct sock *sk = sock->sk;
155         struct l2cap_chan *chan = l2cap_pi(sk)->chan;
156         int err = 0;
157
158         BT_DBG("sk %p backlog %d", sk, backlog);
159
160         lock_sock(sk);
161
162         if (sk->sk_state != BT_BOUND) {
163                 err = -EBADFD;
164                 goto done;
165         }
166
167         if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) {
168                 err = -EINVAL;
169                 goto done;
170         }
171
172         switch (chan->mode) {
173         case L2CAP_MODE_BASIC:
174                 break;
175         case L2CAP_MODE_ERTM:
176         case L2CAP_MODE_STREAMING:
177                 if (!disable_ertm)
178                         break;
179                 /* fall through */
180         default:
181                 err = -ENOTSUPP;
182                 goto done;
183         }
184
185         sk->sk_max_ack_backlog = backlog;
186         sk->sk_ack_backlog = 0;
187
188         chan->state = BT_LISTEN;
189         sk->sk_state = BT_LISTEN;
190
191 done:
192         release_sock(sk);
193         return err;
194 }
195
196 static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
197                              int flags)
198 {
199         DECLARE_WAITQUEUE(wait, current);
200         struct sock *sk = sock->sk, *nsk;
201         long timeo;
202         int err = 0;
203
204         lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
205
206         timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
207
208         BT_DBG("sk %p timeo %ld", sk, timeo);
209
210         /* Wait for an incoming connection. (wake-one). */
211         add_wait_queue_exclusive(sk_sleep(sk), &wait);
212         while (1) {
213                 set_current_state(TASK_INTERRUPTIBLE);
214
215                 if (sk->sk_state != BT_LISTEN) {
216                         err = -EBADFD;
217                         break;
218                 }
219
220                 nsk = bt_accept_dequeue(sk, newsock);
221                 if (nsk)
222                         break;
223
224                 if (!timeo) {
225                         err = -EAGAIN;
226                         break;
227                 }
228
229                 if (signal_pending(current)) {
230                         err = sock_intr_errno(timeo);
231                         break;
232                 }
233
234                 release_sock(sk);
235                 timeo = schedule_timeout(timeo);
236                 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
237         }
238         __set_current_state(TASK_RUNNING);
239         remove_wait_queue(sk_sleep(sk), &wait);
240
241         if (err)
242                 goto done;
243
244         newsock->state = SS_CONNECTED;
245
246         BT_DBG("new socket %p", nsk);
247
248 done:
249         release_sock(sk);
250         return err;
251 }
252
253 static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr,
254                               int *len, int peer)
255 {
256         struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
257         struct sock *sk = sock->sk;
258         struct l2cap_chan *chan = l2cap_pi(sk)->chan;
259
260         BT_DBG("sock %p, sk %p", sock, sk);
261
262         memset(la, 0, sizeof(struct sockaddr_l2));
263         addr->sa_family = AF_BLUETOOTH;
264         *len = sizeof(struct sockaddr_l2);
265
266         if (peer) {
267                 la->l2_psm = chan->psm;
268                 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
269                 la->l2_cid = cpu_to_le16(chan->dcid);
270         } else {
271                 la->l2_psm = chan->sport;
272                 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
273                 la->l2_cid = cpu_to_le16(chan->scid);
274         }
275
276         return 0;
277 }
278
279 static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
280                                      char __user *optval, int __user *optlen)
281 {
282         struct sock *sk = sock->sk;
283         struct l2cap_chan *chan = l2cap_pi(sk)->chan;
284         struct l2cap_options opts;
285         struct l2cap_conninfo cinfo;
286         int len, err = 0;
287         u32 opt;
288
289         BT_DBG("sk %p", sk);
290
291         if (get_user(len, optlen))
292                 return -EFAULT;
293
294         lock_sock(sk);
295
296         switch (optname) {
297         case L2CAP_OPTIONS:
298                 memset(&opts, 0, sizeof(opts));
299                 opts.imtu     = chan->imtu;
300                 opts.omtu     = chan->omtu;
301                 opts.flush_to = chan->flush_to;
302                 opts.mode     = chan->mode;
303                 opts.fcs      = chan->fcs;
304                 opts.max_tx   = chan->max_tx;
305                 opts.txwin_size = chan->tx_win;
306
307                 len = min_t(unsigned int, len, sizeof(opts));
308                 if (copy_to_user(optval, (char *) &opts, len))
309                         err = -EFAULT;
310
311                 break;
312
313         case L2CAP_LM:
314                 switch (chan->sec_level) {
315                 case BT_SECURITY_LOW:
316                         opt = L2CAP_LM_AUTH;
317                         break;
318                 case BT_SECURITY_MEDIUM:
319                         opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
320                         break;
321                 case BT_SECURITY_HIGH:
322                         opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
323                               L2CAP_LM_SECURE;
324                         break;
325                 default:
326                         opt = 0;
327                         break;
328                 }
329
330                 if (test_bit(FLAG_ROLE_SWITCH, &chan->flags))
331                         opt |= L2CAP_LM_MASTER;
332
333                 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
334                         opt |= L2CAP_LM_RELIABLE;
335
336                 if (put_user(opt, (u32 __user *) optval))
337                         err = -EFAULT;
338                 break;
339
340         case L2CAP_CONNINFO:
341                 if (sk->sk_state != BT_CONNECTED &&
342                     !(sk->sk_state == BT_CONNECT2 &&
343                       test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
344                         err = -ENOTCONN;
345                         break;
346                 }
347
348                 memset(&cinfo, 0, sizeof(cinfo));
349                 cinfo.hci_handle = chan->conn->hcon->handle;
350                 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
351
352                 len = min_t(unsigned int, len, sizeof(cinfo));
353                 if (copy_to_user(optval, (char *) &cinfo, len))
354                         err = -EFAULT;
355
356                 break;
357
358         default:
359                 err = -ENOPROTOOPT;
360                 break;
361         }
362
363         release_sock(sk);
364         return err;
365 }
366
367 static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
368                                  char __user *optval, int __user *optlen)
369 {
370         struct sock *sk = sock->sk;
371         struct l2cap_chan *chan = l2cap_pi(sk)->chan;
372         struct bt_security sec;
373         struct bt_power pwr;
374         int len, err = 0;
375
376         BT_DBG("sk %p", sk);
377
378         if (level == SOL_L2CAP)
379                 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
380
381         if (level != SOL_BLUETOOTH)
382                 return -ENOPROTOOPT;
383
384         if (get_user(len, optlen))
385                 return -EFAULT;
386
387         lock_sock(sk);
388
389         switch (optname) {
390         case BT_SECURITY:
391                 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
392                     chan->chan_type != L2CAP_CHAN_RAW) {
393                         err = -EINVAL;
394                         break;
395                 }
396
397                 memset(&sec, 0, sizeof(sec));
398                 if (chan->conn) {
399                         sec.level = chan->conn->hcon->sec_level;
400
401                         if (sk->sk_state == BT_CONNECTED)
402                                 sec.key_size = chan->conn->hcon->enc_key_size;
403                 } else {
404                         sec.level = chan->sec_level;
405                 }
406
407                 len = min_t(unsigned int, len, sizeof(sec));
408                 if (copy_to_user(optval, (char *) &sec, len))
409                         err = -EFAULT;
410
411                 break;
412
413         case BT_DEFER_SETUP:
414                 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
415                         err = -EINVAL;
416                         break;
417                 }
418
419                 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
420                              (u32 __user *) optval))
421                         err = -EFAULT;
422
423                 break;
424
425         case BT_FLUSHABLE:
426                 if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags),
427                              (u32 __user *) optval))
428                         err = -EFAULT;
429
430                 break;
431
432         case BT_POWER:
433                 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
434                     && sk->sk_type != SOCK_RAW) {
435                         err = -EINVAL;
436                         break;
437                 }
438
439                 pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
440
441                 len = min_t(unsigned int, len, sizeof(pwr));
442                 if (copy_to_user(optval, (char *) &pwr, len))
443                         err = -EFAULT;
444
445                 break;
446
447         case BT_CHANNEL_POLICY:
448                 if (put_user(chan->chan_policy, (u32 __user *) optval))
449                         err = -EFAULT;
450                 break;
451
452         default:
453                 err = -ENOPROTOOPT;
454                 break;
455         }
456
457         release_sock(sk);
458         return err;
459 }
460
461 static bool l2cap_valid_mtu(struct l2cap_chan *chan, u16 mtu)
462 {
463         switch (chan->scid) {
464         case L2CAP_CID_ATT:
465                 if (mtu < L2CAP_LE_MIN_MTU)
466                         return false;
467                 break;
468
469         default:
470                 if (mtu < L2CAP_DEFAULT_MIN_MTU)
471                         return false;
472         }
473
474         return true;
475 }
476
477 static int l2cap_sock_setsockopt_old(struct socket *sock, int optname,
478                                      char __user *optval, unsigned int optlen)
479 {
480         struct sock *sk = sock->sk;
481         struct l2cap_chan *chan = l2cap_pi(sk)->chan;
482         struct l2cap_options opts;
483         int len, err = 0;
484         u32 opt;
485
486         BT_DBG("sk %p", sk);
487
488         lock_sock(sk);
489
490         switch (optname) {
491         case L2CAP_OPTIONS:
492                 if (sk->sk_state == BT_CONNECTED) {
493                         err = -EINVAL;
494                         break;
495                 }
496
497                 opts.imtu     = chan->imtu;
498                 opts.omtu     = chan->omtu;
499                 opts.flush_to = chan->flush_to;
500                 opts.mode     = chan->mode;
501                 opts.fcs      = chan->fcs;
502                 opts.max_tx   = chan->max_tx;
503                 opts.txwin_size = chan->tx_win;
504
505                 len = min_t(unsigned int, sizeof(opts), optlen);
506                 if (copy_from_user((char *) &opts, optval, len)) {
507                         err = -EFAULT;
508                         break;
509                 }
510
511                 if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) {
512                         err = -EINVAL;
513                         break;
514                 }
515
516                 if (!l2cap_valid_mtu(chan, opts.imtu)) {
517                         err = -EINVAL;
518                         break;
519                 }
520
521                 chan->mode = opts.mode;
522                 switch (chan->mode) {
523                 case L2CAP_MODE_BASIC:
524                         clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
525                         break;
526                 case L2CAP_MODE_ERTM:
527                 case L2CAP_MODE_STREAMING:
528                         if (!disable_ertm)
529                                 break;
530                         /* fall through */
531                 default:
532                         err = -EINVAL;
533                         break;
534                 }
535
536                 chan->imtu = opts.imtu;
537                 chan->omtu = opts.omtu;
538                 chan->fcs  = opts.fcs;
539                 chan->max_tx = opts.max_tx;
540                 chan->tx_win = opts.txwin_size;
541                 chan->flush_to = opts.flush_to;
542                 break;
543
544         case L2CAP_LM:
545                 if (get_user(opt, (u32 __user *) optval)) {
546                         err = -EFAULT;
547                         break;
548                 }
549
550                 if (opt & L2CAP_LM_AUTH)
551                         chan->sec_level = BT_SECURITY_LOW;
552                 if (opt & L2CAP_LM_ENCRYPT)
553                         chan->sec_level = BT_SECURITY_MEDIUM;
554                 if (opt & L2CAP_LM_SECURE)
555                         chan->sec_level = BT_SECURITY_HIGH;
556
557                 if (opt & L2CAP_LM_MASTER)
558                         set_bit(FLAG_ROLE_SWITCH, &chan->flags);
559                 else
560                         clear_bit(FLAG_ROLE_SWITCH, &chan->flags);
561
562                 if (opt & L2CAP_LM_RELIABLE)
563                         set_bit(FLAG_FORCE_RELIABLE, &chan->flags);
564                 else
565                         clear_bit(FLAG_FORCE_RELIABLE, &chan->flags);
566                 break;
567
568         default:
569                 err = -ENOPROTOOPT;
570                 break;
571         }
572
573         release_sock(sk);
574         return err;
575 }
576
577 static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
578                                  char __user *optval, unsigned int optlen)
579 {
580         struct sock *sk = sock->sk;
581         struct l2cap_chan *chan = l2cap_pi(sk)->chan;
582         struct bt_security sec;
583         struct bt_power pwr;
584         struct l2cap_conn *conn;
585         int len, err = 0;
586         u32 opt;
587
588         BT_DBG("sk %p", sk);
589
590         if (level == SOL_L2CAP)
591                 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
592
593         if (level != SOL_BLUETOOTH)
594                 return -ENOPROTOOPT;
595
596         lock_sock(sk);
597
598         switch (optname) {
599         case BT_SECURITY:
600                 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
601                     chan->chan_type != L2CAP_CHAN_RAW) {
602                         err = -EINVAL;
603                         break;
604                 }
605
606                 sec.level = BT_SECURITY_LOW;
607
608                 len = min_t(unsigned int, sizeof(sec), optlen);
609                 if (copy_from_user((char *) &sec, optval, len)) {
610                         err = -EFAULT;
611                         break;
612                 }
613
614                 if (sec.level < BT_SECURITY_LOW ||
615                     sec.level > BT_SECURITY_HIGH) {
616                         err = -EINVAL;
617                         break;
618                 }
619
620                 chan->sec_level = sec.level;
621
622                 if (!chan->conn)
623                         break;
624
625                 conn = chan->conn;
626
627                 /*change security for LE channels */
628                 if (chan->scid == L2CAP_CID_ATT) {
629                         if (!conn->hcon->out) {
630                                 err = -EINVAL;
631                                 break;
632                         }
633
634                         if (smp_conn_security(conn->hcon, sec.level))
635                                 break;
636                         sk->sk_state = BT_CONFIG;
637                         chan->state = BT_CONFIG;
638
639                 /* or for ACL link */
640                 } else if ((sk->sk_state == BT_CONNECT2 &&
641                             test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) ||
642                            sk->sk_state == BT_CONNECTED) {
643                         if (!l2cap_chan_check_security(chan))
644                                 set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
645                         else
646                                 sk->sk_state_change(sk);
647                 } else {
648                         err = -EINVAL;
649                 }
650                 break;
651
652         case BT_DEFER_SETUP:
653                 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
654                         err = -EINVAL;
655                         break;
656                 }
657
658                 if (get_user(opt, (u32 __user *) optval)) {
659                         err = -EFAULT;
660                         break;
661                 }
662
663                 if (opt)
664                         set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
665                 else
666                         clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
667                 break;
668
669         case BT_FLUSHABLE:
670                 if (get_user(opt, (u32 __user *) optval)) {
671                         err = -EFAULT;
672                         break;
673                 }
674
675                 if (opt > BT_FLUSHABLE_ON) {
676                         err = -EINVAL;
677                         break;
678                 }
679
680                 if (opt == BT_FLUSHABLE_OFF) {
681                         struct l2cap_conn *conn = chan->conn;
682                         /* proceed further only when we have l2cap_conn and
683                            No Flush support in the LM */
684                         if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
685                                 err = -EINVAL;
686                                 break;
687                         }
688                 }
689
690                 if (opt)
691                         set_bit(FLAG_FLUSHABLE, &chan->flags);
692                 else
693                         clear_bit(FLAG_FLUSHABLE, &chan->flags);
694                 break;
695
696         case BT_POWER:
697                 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
698                     chan->chan_type != L2CAP_CHAN_RAW) {
699                         err = -EINVAL;
700                         break;
701                 }
702
703                 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
704
705                 len = min_t(unsigned int, sizeof(pwr), optlen);
706                 if (copy_from_user((char *) &pwr, optval, len)) {
707                         err = -EFAULT;
708                         break;
709                 }
710
711                 if (pwr.force_active)
712                         set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
713                 else
714                         clear_bit(FLAG_FORCE_ACTIVE, &chan->flags);
715                 break;
716
717         case BT_CHANNEL_POLICY:
718                 if (get_user(opt, (u32 __user *) optval)) {
719                         err = -EFAULT;
720                         break;
721                 }
722
723                 if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) {
724                         err = -EINVAL;
725                         break;
726                 }
727
728                 if (chan->mode != L2CAP_MODE_ERTM &&
729                     chan->mode != L2CAP_MODE_STREAMING) {
730                         err = -EOPNOTSUPP;
731                         break;
732                 }
733
734                 chan->chan_policy = (u8) opt;
735
736                 if (sk->sk_state == BT_CONNECTED &&
737                     chan->move_role == L2CAP_MOVE_ROLE_NONE)
738                         l2cap_move_start(chan);
739
740                 break;
741
742         default:
743                 err = -ENOPROTOOPT;
744                 break;
745         }
746
747         release_sock(sk);
748         return err;
749 }
750
751 static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
752                               struct msghdr *msg, size_t len)
753 {
754         struct sock *sk = sock->sk;
755         struct l2cap_chan *chan = l2cap_pi(sk)->chan;
756         int err;
757
758         BT_DBG("sock %p, sk %p", sock, sk);
759
760         err = sock_error(sk);
761         if (err)
762                 return err;
763
764         if (msg->msg_flags & MSG_OOB)
765                 return -EOPNOTSUPP;
766
767         if (sk->sk_state != BT_CONNECTED)
768                 return -ENOTCONN;
769
770         lock_sock(sk);
771         err = bt_sock_wait_ready(sk, msg->msg_flags);
772         release_sock(sk);
773         if (err)
774                 return err;
775
776         l2cap_chan_lock(chan);
777         err = l2cap_chan_send(chan, msg, len, sk->sk_priority);
778         l2cap_chan_unlock(chan);
779
780         return err;
781 }
782
783 static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
784                               struct msghdr *msg, size_t len, int flags)
785 {
786         struct sock *sk = sock->sk;
787         struct l2cap_pinfo *pi = l2cap_pi(sk);
788         int err;
789
790         lock_sock(sk);
791
792         if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
793                                                     &bt_sk(sk)->flags)) {
794                 sk->sk_state = BT_CONFIG;
795                 pi->chan->state = BT_CONFIG;
796
797                 __l2cap_connect_rsp_defer(pi->chan);
798                 err = 0;
799                 goto done;
800         }
801
802         release_sock(sk);
803
804         if (sock->type == SOCK_STREAM)
805                 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
806         else
807                 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
808
809         if (pi->chan->mode != L2CAP_MODE_ERTM)
810                 return err;
811
812         /* Attempt to put pending rx data in the socket buffer */
813
814         lock_sock(sk);
815
816         if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
817                 goto done;
818
819         if (pi->rx_busy_skb) {
820                 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
821                         pi->rx_busy_skb = NULL;
822                 else
823                         goto done;
824         }
825
826         /* Restore data flow when half of the receive buffer is
827          * available.  This avoids resending large numbers of
828          * frames.
829          */
830         if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
831                 l2cap_chan_busy(pi->chan, 0);
832
833 done:
834         release_sock(sk);
835         return err;
836 }
837
838 /* Kill socket (only if zapped and orphan)
839  * Must be called on unlocked socket.
840  */
841 static void l2cap_sock_kill(struct sock *sk)
842 {
843         if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
844                 return;
845
846         BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
847
848         /* Kill poor orphan */
849
850         l2cap_chan_put(l2cap_pi(sk)->chan);
851         sock_set_flag(sk, SOCK_DEAD);
852         sock_put(sk);
853 }
854
855 static int l2cap_sock_shutdown(struct socket *sock, int how)
856 {
857         struct sock *sk = sock->sk;
858         struct l2cap_chan *chan;
859         struct l2cap_conn *conn;
860         int err = 0;
861
862         BT_DBG("sock %p, sk %p", sock, sk);
863
864         if (!sk)
865                 return 0;
866
867         chan = l2cap_pi(sk)->chan;
868         conn = chan->conn;
869
870         if (conn)
871                 mutex_lock(&conn->chan_lock);
872
873         l2cap_chan_lock(chan);
874         lock_sock(sk);
875
876         if (!sk->sk_shutdown) {
877                 if (chan->mode == L2CAP_MODE_ERTM)
878                         err = __l2cap_wait_ack(sk);
879
880                 sk->sk_shutdown = SHUTDOWN_MASK;
881
882                 release_sock(sk);
883                 l2cap_chan_close(chan, 0);
884                 lock_sock(sk);
885
886                 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
887                         err = bt_sock_wait_state(sk, BT_CLOSED,
888                                                  sk->sk_lingertime);
889         }
890
891         if (!err && sk->sk_err)
892                 err = -sk->sk_err;
893
894         release_sock(sk);
895         l2cap_chan_unlock(chan);
896
897         if (conn)
898                 mutex_unlock(&conn->chan_lock);
899
900         return err;
901 }
902
903 static int l2cap_sock_release(struct socket *sock)
904 {
905         struct sock *sk = sock->sk;
906         int err;
907
908         BT_DBG("sock %p, sk %p", sock, sk);
909
910         if (!sk)
911                 return 0;
912
913         bt_sock_unlink(&l2cap_sk_list, sk);
914
915         err = l2cap_sock_shutdown(sock, 2);
916
917         sock_orphan(sk);
918         l2cap_sock_kill(sk);
919         return err;
920 }
921
922 static void l2cap_sock_cleanup_listen(struct sock *parent)
923 {
924         struct sock *sk;
925
926         BT_DBG("parent %p", parent);
927
928         /* Close not yet accepted channels */
929         while ((sk = bt_accept_dequeue(parent, NULL))) {
930                 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
931
932                 l2cap_chan_lock(chan);
933                 __clear_chan_timer(chan);
934                 l2cap_chan_close(chan, ECONNRESET);
935                 l2cap_chan_unlock(chan);
936
937                 l2cap_sock_kill(sk);
938         }
939 }
940
941 static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
942 {
943         struct sock *sk, *parent = chan->data;
944
945         /* Check for backlog size */
946         if (sk_acceptq_is_full(parent)) {
947                 BT_DBG("backlog full %d", parent->sk_ack_backlog);
948                 return NULL;
949         }
950
951         sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
952                               GFP_ATOMIC);
953         if (!sk)
954                 return NULL;
955
956         bt_sock_reclassify_lock(sk, BTPROTO_L2CAP);
957
958         l2cap_sock_init(sk, parent);
959
960         bt_accept_enqueue(parent, sk);
961
962         return l2cap_pi(sk)->chan;
963 }
964
965 static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
966 {
967         int err;
968         struct sock *sk = chan->data;
969         struct l2cap_pinfo *pi = l2cap_pi(sk);
970
971         lock_sock(sk);
972
973         if (pi->rx_busy_skb) {
974                 err = -ENOMEM;
975                 goto done;
976         }
977
978         err = sock_queue_rcv_skb(sk, skb);
979
980         /* For ERTM, handle one skb that doesn't fit into the recv
981          * buffer.  This is important to do because the data frames
982          * have already been acked, so the skb cannot be discarded.
983          *
984          * Notify the l2cap core that the buffer is full, so the
985          * LOCAL_BUSY state is entered and no more frames are
986          * acked and reassembled until there is buffer space
987          * available.
988          */
989         if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
990                 pi->rx_busy_skb = skb;
991                 l2cap_chan_busy(pi->chan, 1);
992                 err = 0;
993         }
994
995 done:
996         release_sock(sk);
997
998         return err;
999 }
1000
1001 static void l2cap_sock_close_cb(struct l2cap_chan *chan)
1002 {
1003         struct sock *sk = chan->data;
1004
1005         l2cap_sock_kill(sk);
1006 }
1007
1008 static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
1009 {
1010         struct sock *sk = chan->data;
1011         struct sock *parent;
1012
1013         lock_sock(sk);
1014
1015         parent = bt_sk(sk)->parent;
1016
1017         sock_set_flag(sk, SOCK_ZAPPED);
1018
1019         switch (chan->state) {
1020         case BT_OPEN:
1021         case BT_BOUND:
1022         case BT_CLOSED:
1023                 break;
1024         case BT_LISTEN:
1025                 l2cap_sock_cleanup_listen(sk);
1026                 sk->sk_state = BT_CLOSED;
1027                 chan->state = BT_CLOSED;
1028
1029                 break;
1030         default:
1031                 sk->sk_state = BT_CLOSED;
1032                 chan->state = BT_CLOSED;
1033
1034                 sk->sk_err = err;
1035
1036                 if (parent) {
1037                         bt_accept_unlink(sk);
1038                         parent->sk_data_ready(parent, 0);
1039                 } else {
1040                         sk->sk_state_change(sk);
1041                 }
1042
1043                 break;
1044         }
1045
1046         release_sock(sk);
1047 }
1048
1049 static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state)
1050 {
1051         struct sock *sk = chan->data;
1052
1053         sk->sk_state = state;
1054 }
1055
1056 static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
1057                                                unsigned long len, int nb)
1058 {
1059         struct sk_buff *skb;
1060         int err;
1061
1062         l2cap_chan_unlock(chan);
1063         skb = bt_skb_send_alloc(chan->sk, len, nb, &err);
1064         l2cap_chan_lock(chan);
1065
1066         if (!skb)
1067                 return ERR_PTR(err);
1068
1069         return skb;
1070 }
1071
1072 static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
1073 {
1074         struct sock *sk = chan->data;
1075         struct sock *parent;
1076
1077         lock_sock(sk);
1078
1079         parent = bt_sk(sk)->parent;
1080
1081         BT_DBG("sk %p, parent %p", sk, parent);
1082
1083         sk->sk_state = BT_CONNECTED;
1084         sk->sk_state_change(sk);
1085
1086         if (parent)
1087                 parent->sk_data_ready(parent, 0);
1088
1089         release_sock(sk);
1090 }
1091
1092 static void l2cap_sock_defer_cb(struct l2cap_chan *chan)
1093 {
1094         struct sock *sk = chan->data;
1095         struct sock *parent = bt_sk(sk)->parent;
1096
1097         if (parent)
1098                 parent->sk_data_ready(parent, 0);
1099 }
1100
1101 static struct l2cap_ops l2cap_chan_ops = {
1102         .name           = "L2CAP Socket Interface",
1103         .new_connection = l2cap_sock_new_connection_cb,
1104         .recv           = l2cap_sock_recv_cb,
1105         .close          = l2cap_sock_close_cb,
1106         .teardown       = l2cap_sock_teardown_cb,
1107         .state_change   = l2cap_sock_state_change_cb,
1108         .ready          = l2cap_sock_ready_cb,
1109         .defer          = l2cap_sock_defer_cb,
1110         .alloc_skb      = l2cap_sock_alloc_skb_cb,
1111 };
1112
1113 static void l2cap_sock_destruct(struct sock *sk)
1114 {
1115         BT_DBG("sk %p", sk);
1116
1117         if (l2cap_pi(sk)->chan)
1118                 l2cap_chan_put(l2cap_pi(sk)->chan);
1119         if (l2cap_pi(sk)->rx_busy_skb) {
1120                 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
1121                 l2cap_pi(sk)->rx_busy_skb = NULL;
1122         }
1123
1124         skb_queue_purge(&sk->sk_receive_queue);
1125         skb_queue_purge(&sk->sk_write_queue);
1126 }
1127
1128 static void l2cap_sock_init(struct sock *sk, struct sock *parent)
1129 {
1130         struct l2cap_pinfo *pi = l2cap_pi(sk);
1131         struct l2cap_chan *chan = pi->chan;
1132
1133         BT_DBG("sk %p", sk);
1134
1135         if (parent) {
1136                 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
1137
1138                 sk->sk_type = parent->sk_type;
1139                 bt_sk(sk)->flags = bt_sk(parent)->flags;
1140
1141                 chan->chan_type = pchan->chan_type;
1142                 chan->imtu = pchan->imtu;
1143                 chan->omtu = pchan->omtu;
1144                 chan->conf_state = pchan->conf_state;
1145                 chan->mode = pchan->mode;
1146                 chan->fcs  = pchan->fcs;
1147                 chan->max_tx = pchan->max_tx;
1148                 chan->tx_win = pchan->tx_win;
1149                 chan->tx_win_max = pchan->tx_win_max;
1150                 chan->sec_level = pchan->sec_level;
1151                 chan->flags = pchan->flags;
1152
1153                 security_sk_clone(parent, sk);
1154         } else {
1155
1156                 switch (sk->sk_type) {
1157                 case SOCK_RAW:
1158                         chan->chan_type = L2CAP_CHAN_RAW;
1159                         break;
1160                 case SOCK_DGRAM:
1161                         chan->chan_type = L2CAP_CHAN_CONN_LESS;
1162                         break;
1163                 case SOCK_SEQPACKET:
1164                 case SOCK_STREAM:
1165                         chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
1166                         break;
1167                 }
1168
1169                 chan->imtu = L2CAP_DEFAULT_MTU;
1170                 chan->omtu = 0;
1171                 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
1172                         chan->mode = L2CAP_MODE_ERTM;
1173                         set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
1174                 } else {
1175                         chan->mode = L2CAP_MODE_BASIC;
1176                 }
1177
1178                 l2cap_chan_set_defaults(chan);
1179         }
1180
1181         /* Default config options */
1182         chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
1183
1184         chan->data = sk;
1185         chan->ops = &l2cap_chan_ops;
1186 }
1187
1188 static struct proto l2cap_proto = {
1189         .name           = "L2CAP",
1190         .owner          = THIS_MODULE,
1191         .obj_size       = sizeof(struct l2cap_pinfo)
1192 };
1193
1194 static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
1195                                      int proto, gfp_t prio)
1196 {
1197         struct sock *sk;
1198         struct l2cap_chan *chan;
1199
1200         sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1201         if (!sk)
1202                 return NULL;
1203
1204         sock_init_data(sock, sk);
1205         INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1206
1207         sk->sk_destruct = l2cap_sock_destruct;
1208         sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
1209
1210         sock_reset_flag(sk, SOCK_ZAPPED);
1211
1212         sk->sk_protocol = proto;
1213         sk->sk_state = BT_OPEN;
1214
1215         chan = l2cap_chan_create();
1216         if (!chan) {
1217                 sk_free(sk);
1218                 return NULL;
1219         }
1220
1221         l2cap_chan_hold(chan);
1222
1223         chan->sk = sk;
1224
1225         l2cap_pi(sk)->chan = chan;
1226
1227         return sk;
1228 }
1229
1230 static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1231                              int kern)
1232 {
1233         struct sock *sk;
1234
1235         BT_DBG("sock %p", sock);
1236
1237         sock->state = SS_UNCONNECTED;
1238
1239         if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1240             sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1241                 return -ESOCKTNOSUPPORT;
1242
1243         if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1244                 return -EPERM;
1245
1246         sock->ops = &l2cap_sock_ops;
1247
1248         sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1249         if (!sk)
1250                 return -ENOMEM;
1251
1252         l2cap_sock_init(sk, NULL);
1253         bt_sock_link(&l2cap_sk_list, sk);
1254         return 0;
1255 }
1256
1257 static const struct proto_ops l2cap_sock_ops = {
1258         .family         = PF_BLUETOOTH,
1259         .owner          = THIS_MODULE,
1260         .release        = l2cap_sock_release,
1261         .bind           = l2cap_sock_bind,
1262         .connect        = l2cap_sock_connect,
1263         .listen         = l2cap_sock_listen,
1264         .accept         = l2cap_sock_accept,
1265         .getname        = l2cap_sock_getname,
1266         .sendmsg        = l2cap_sock_sendmsg,
1267         .recvmsg        = l2cap_sock_recvmsg,
1268         .poll           = bt_sock_poll,
1269         .ioctl          = bt_sock_ioctl,
1270         .mmap           = sock_no_mmap,
1271         .socketpair     = sock_no_socketpair,
1272         .shutdown       = l2cap_sock_shutdown,
1273         .setsockopt     = l2cap_sock_setsockopt,
1274         .getsockopt     = l2cap_sock_getsockopt
1275 };
1276
1277 static const struct net_proto_family l2cap_sock_family_ops = {
1278         .family = PF_BLUETOOTH,
1279         .owner  = THIS_MODULE,
1280         .create = l2cap_sock_create,
1281 };
1282
1283 int __init l2cap_init_sockets(void)
1284 {
1285         int err;
1286
1287         err = proto_register(&l2cap_proto, 0);
1288         if (err < 0)
1289                 return err;
1290
1291         err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1292         if (err < 0) {
1293                 BT_ERR("L2CAP socket registration failed");
1294                 goto error;
1295         }
1296
1297         err = bt_procfs_init(&init_net, "l2cap", &l2cap_sk_list,
1298                              NULL);
1299         if (err < 0) {
1300                 BT_ERR("Failed to create L2CAP proc file");
1301                 bt_sock_unregister(BTPROTO_L2CAP);
1302                 goto error;
1303         }
1304
1305         BT_INFO("L2CAP socket layer initialized");
1306
1307         return 0;
1308
1309 error:
1310         proto_unregister(&l2cap_proto);
1311         return err;
1312 }
1313
1314 void l2cap_cleanup_sockets(void)
1315 {
1316         bt_procfs_cleanup(&init_net, "l2cap");
1317         bt_sock_unregister(BTPROTO_L2CAP);
1318         proto_unregister(&l2cap_proto);
1319 }