aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth/l2cap_sock.c
diff options
context:
space:
mode:
authorJohn W. Linville <linville@tuxdriver.com>2011-05-05 13:32:35 -0400
committerJohn W. Linville <linville@tuxdriver.com>2011-05-05 13:32:35 -0400
commita70171dce9cd44cb06c7d299eba9fa87a8933045 (patch)
tree5425df5f33fadc617c7dec99578d06f0d933578e /net/bluetooth/l2cap_sock.c
parent5a412ad7f4c95bb5b756aa12b52646e857e7c75d (diff)
parenteaef6a93bd52a2cc47b9fce201310010707afdb4 (diff)
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless-next-2.6 into for-davem
Conflicts: drivers/net/wireless/libertas/if_cs.c drivers/net/wireless/rtlwifi/pci.c net/bluetooth/l2cap_sock.c
Diffstat (limited to 'net/bluetooth/l2cap_sock.c')
-rw-r--r--net/bluetooth/l2cap_sock.c242
1 files changed, 137 insertions, 105 deletions
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index 47394a178bd5..7c4a9ae9b3ce 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -30,6 +30,8 @@
30#include <net/bluetooth/hci_core.h> 30#include <net/bluetooth/hci_core.h>
31#include <net/bluetooth/l2cap.h> 31#include <net/bluetooth/l2cap.h>
32 32
33static const struct proto_ops l2cap_sock_ops;
34
33/* ---- L2CAP timers ---- */ 35/* ---- L2CAP timers ---- */
34static void l2cap_sock_timeout(unsigned long arg) 36static void l2cap_sock_timeout(unsigned long arg)
35{ 37{
@@ -51,7 +53,7 @@ static void l2cap_sock_timeout(unsigned long arg)
51 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG) 53 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG)
52 reason = ECONNREFUSED; 54 reason = ECONNREFUSED;
53 else if (sk->sk_state == BT_CONNECT && 55 else if (sk->sk_state == BT_CONNECT &&
54 l2cap_pi(sk)->sec_level != BT_SECURITY_SDP) 56 l2cap_pi(sk)->chan->sec_level != BT_SECURITY_SDP)
55 reason = ECONNREFUSED; 57 reason = ECONNREFUSED;
56 else 58 else
57 reason = ETIMEDOUT; 59 reason = ETIMEDOUT;
@@ -80,9 +82,13 @@ static struct sock *__l2cap_get_sock_by_addr(__le16 psm, bdaddr_t *src)
80{ 82{
81 struct sock *sk; 83 struct sock *sk;
82 struct hlist_node *node; 84 struct hlist_node *node;
83 sk_for_each(sk, node, &l2cap_sk_list.head) 85 sk_for_each(sk, node, &l2cap_sk_list.head) {
84 if (l2cap_pi(sk)->sport == psm && !bacmp(&bt_sk(sk)->src, src)) 86 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
87
88 if (chan->sport == psm && !bacmp(&bt_sk(sk)->src, src))
85 goto found; 89 goto found;
90 }
91
86 sk = NULL; 92 sk = NULL;
87found: 93found:
88 return sk; 94 return sk;
@@ -91,6 +97,7 @@ found:
91static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) 97static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
92{ 98{
93 struct sock *sk = sock->sk; 99 struct sock *sk = sock->sk;
100 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
94 struct sockaddr_l2 la; 101 struct sockaddr_l2 la;
95 int len, err = 0; 102 int len, err = 0;
96 103
@@ -136,17 +143,17 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
136 } else { 143 } else {
137 /* Save source address */ 144 /* Save source address */
138 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr); 145 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
139 l2cap_pi(sk)->psm = la.l2_psm; 146 chan->psm = la.l2_psm;
140 l2cap_pi(sk)->sport = la.l2_psm; 147 chan->sport = la.l2_psm;
141 sk->sk_state = BT_BOUND; 148 sk->sk_state = BT_BOUND;
142 149
143 if (__le16_to_cpu(la.l2_psm) == 0x0001 || 150 if (__le16_to_cpu(la.l2_psm) == 0x0001 ||
144 __le16_to_cpu(la.l2_psm) == 0x0003) 151 __le16_to_cpu(la.l2_psm) == 0x0003)
145 l2cap_pi(sk)->sec_level = BT_SECURITY_SDP; 152 chan->sec_level = BT_SECURITY_SDP;
146 } 153 }
147 154
148 if (la.l2_cid) 155 if (la.l2_cid)
149 l2cap_pi(sk)->scid = la.l2_cid; 156 chan->scid = la.l2_cid;
150 157
151 write_unlock_bh(&l2cap_sk_list.lock); 158 write_unlock_bh(&l2cap_sk_list.lock);
152 159
@@ -158,6 +165,7 @@ done:
158static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) 165static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
159{ 166{
160 struct sock *sk = sock->sk; 167 struct sock *sk = sock->sk;
168 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
161 struct sockaddr_l2 la; 169 struct sockaddr_l2 la;
162 int len, err = 0; 170 int len, err = 0;
163 171
@@ -182,7 +190,7 @@ static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int al
182 goto done; 190 goto done;
183 } 191 }
184 192
185 switch (l2cap_pi(sk)->mode) { 193 switch (chan->mode) {
186 case L2CAP_MODE_BASIC: 194 case L2CAP_MODE_BASIC:
187 break; 195 break;
188 case L2CAP_MODE_ERTM: 196 case L2CAP_MODE_ERTM:
@@ -226,10 +234,10 @@ static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int al
226 234
227 /* Set destination address and psm */ 235 /* Set destination address and psm */
228 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr); 236 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
229 l2cap_pi(sk)->psm = la.l2_psm; 237 chan->psm = la.l2_psm;
230 l2cap_pi(sk)->dcid = la.l2_cid; 238 chan->dcid = la.l2_cid;
231 239
232 err = l2cap_do_connect(sk); 240 err = l2cap_chan_connect(l2cap_pi(sk)->chan);
233 if (err) 241 if (err)
234 goto done; 242 goto done;
235 243
@@ -244,6 +252,7 @@ done:
244static int l2cap_sock_listen(struct socket *sock, int backlog) 252static int l2cap_sock_listen(struct socket *sock, int backlog)
245{ 253{
246 struct sock *sk = sock->sk; 254 struct sock *sk = sock->sk;
255 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
247 int err = 0; 256 int err = 0;
248 257
249 BT_DBG("sk %p backlog %d", sk, backlog); 258 BT_DBG("sk %p backlog %d", sk, backlog);
@@ -256,7 +265,7 @@ static int l2cap_sock_listen(struct socket *sock, int backlog)
256 goto done; 265 goto done;
257 } 266 }
258 267
259 switch (l2cap_pi(sk)->mode) { 268 switch (chan->mode) {
260 case L2CAP_MODE_BASIC: 269 case L2CAP_MODE_BASIC:
261 break; 270 break;
262 case L2CAP_MODE_ERTM: 271 case L2CAP_MODE_ERTM:
@@ -269,7 +278,7 @@ static int l2cap_sock_listen(struct socket *sock, int backlog)
269 goto done; 278 goto done;
270 } 279 }
271 280
272 if (!l2cap_pi(sk)->psm && !l2cap_pi(sk)->scid) { 281 if (!chan->psm && !chan->scid) {
273 bdaddr_t *src = &bt_sk(sk)->src; 282 bdaddr_t *src = &bt_sk(sk)->src;
274 u16 psm; 283 u16 psm;
275 284
@@ -279,8 +288,8 @@ static int l2cap_sock_listen(struct socket *sock, int backlog)
279 288
280 for (psm = 0x1001; psm < 0x1100; psm += 2) 289 for (psm = 0x1001; psm < 0x1100; psm += 2)
281 if (!__l2cap_get_sock_by_addr(cpu_to_le16(psm), src)) { 290 if (!__l2cap_get_sock_by_addr(cpu_to_le16(psm), src)) {
282 l2cap_pi(sk)->psm = cpu_to_le16(psm); 291 chan->psm = cpu_to_le16(psm);
283 l2cap_pi(sk)->sport = cpu_to_le16(psm); 292 chan->sport = cpu_to_le16(psm);
284 err = 0; 293 err = 0;
285 break; 294 break;
286 } 295 }
@@ -360,6 +369,7 @@ static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *l
360{ 369{
361 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr; 370 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
362 struct sock *sk = sock->sk; 371 struct sock *sk = sock->sk;
372 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
363 373
364 BT_DBG("sock %p, sk %p", sock, sk); 374 BT_DBG("sock %p, sk %p", sock, sk);
365 375
@@ -367,13 +377,13 @@ static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *l
367 *len = sizeof(struct sockaddr_l2); 377 *len = sizeof(struct sockaddr_l2);
368 378
369 if (peer) { 379 if (peer) {
370 la->l2_psm = l2cap_pi(sk)->psm; 380 la->l2_psm = chan->psm;
371 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst); 381 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
372 la->l2_cid = cpu_to_le16(l2cap_pi(sk)->dcid); 382 la->l2_cid = cpu_to_le16(chan->dcid);
373 } else { 383 } else {
374 la->l2_psm = l2cap_pi(sk)->sport; 384 la->l2_psm = chan->sport;
375 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src); 385 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
376 la->l2_cid = cpu_to_le16(l2cap_pi(sk)->scid); 386 la->l2_cid = cpu_to_le16(chan->scid);
377 } 387 }
378 388
379 return 0; 389 return 0;
@@ -382,6 +392,7 @@ static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *l
382static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen) 392static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
383{ 393{
384 struct sock *sk = sock->sk; 394 struct sock *sk = sock->sk;
395 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
385 struct l2cap_options opts; 396 struct l2cap_options opts;
386 struct l2cap_conninfo cinfo; 397 struct l2cap_conninfo cinfo;
387 int len, err = 0; 398 int len, err = 0;
@@ -397,13 +408,13 @@ static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __us
397 switch (optname) { 408 switch (optname) {
398 case L2CAP_OPTIONS: 409 case L2CAP_OPTIONS:
399 memset(&opts, 0, sizeof(opts)); 410 memset(&opts, 0, sizeof(opts));
400 opts.imtu = l2cap_pi(sk)->imtu; 411 opts.imtu = chan->imtu;
401 opts.omtu = l2cap_pi(sk)->omtu; 412 opts.omtu = chan->omtu;
402 opts.flush_to = l2cap_pi(sk)->flush_to; 413 opts.flush_to = chan->flush_to;
403 opts.mode = l2cap_pi(sk)->mode; 414 opts.mode = chan->mode;
404 opts.fcs = l2cap_pi(sk)->fcs; 415 opts.fcs = chan->fcs;
405 opts.max_tx = l2cap_pi(sk)->max_tx; 416 opts.max_tx = chan->max_tx;
406 opts.txwin_size = (__u16)l2cap_pi(sk)->tx_win; 417 opts.txwin_size = (__u16)chan->tx_win;
407 418
408 len = min_t(unsigned int, len, sizeof(opts)); 419 len = min_t(unsigned int, len, sizeof(opts));
409 if (copy_to_user(optval, (char *) &opts, len)) 420 if (copy_to_user(optval, (char *) &opts, len))
@@ -412,7 +423,7 @@ static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __us
412 break; 423 break;
413 424
414 case L2CAP_LM: 425 case L2CAP_LM:
415 switch (l2cap_pi(sk)->sec_level) { 426 switch (chan->sec_level) {
416 case BT_SECURITY_LOW: 427 case BT_SECURITY_LOW:
417 opt = L2CAP_LM_AUTH; 428 opt = L2CAP_LM_AUTH;
418 break; 429 break;
@@ -428,10 +439,10 @@ static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __us
428 break; 439 break;
429 } 440 }
430 441
431 if (l2cap_pi(sk)->role_switch) 442 if (chan->role_switch)
432 opt |= L2CAP_LM_MASTER; 443 opt |= L2CAP_LM_MASTER;
433 444
434 if (l2cap_pi(sk)->force_reliable) 445 if (chan->force_reliable)
435 opt |= L2CAP_LM_RELIABLE; 446 opt |= L2CAP_LM_RELIABLE;
436 447
437 if (put_user(opt, (u32 __user *) optval)) 448 if (put_user(opt, (u32 __user *) optval))
@@ -446,8 +457,8 @@ static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __us
446 break; 457 break;
447 } 458 }
448 459
449 cinfo.hci_handle = l2cap_pi(sk)->conn->hcon->handle; 460 cinfo.hci_handle = chan->conn->hcon->handle;
450 memcpy(cinfo.dev_class, l2cap_pi(sk)->conn->hcon->dev_class, 3); 461 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
451 462
452 len = min_t(unsigned int, len, sizeof(cinfo)); 463 len = min_t(unsigned int, len, sizeof(cinfo));
453 if (copy_to_user(optval, (char *) &cinfo, len)) 464 if (copy_to_user(optval, (char *) &cinfo, len))
@@ -467,6 +478,7 @@ static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __us
467static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) 478static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
468{ 479{
469 struct sock *sk = sock->sk; 480 struct sock *sk = sock->sk;
481 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
470 struct bt_security sec; 482 struct bt_security sec;
471 int len, err = 0; 483 int len, err = 0;
472 484
@@ -491,7 +503,7 @@ static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, ch
491 break; 503 break;
492 } 504 }
493 505
494 sec.level = l2cap_pi(sk)->sec_level; 506 sec.level = chan->sec_level;
495 507
496 len = min_t(unsigned int, len, sizeof(sec)); 508 len = min_t(unsigned int, len, sizeof(sec));
497 if (copy_to_user(optval, (char *) &sec, len)) 509 if (copy_to_user(optval, (char *) &sec, len))
@@ -511,7 +523,7 @@ static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, ch
511 break; 523 break;
512 524
513 case BT_FLUSHABLE: 525 case BT_FLUSHABLE:
514 if (put_user(l2cap_pi(sk)->flushable, (u32 __user *) optval)) 526 if (put_user(chan->flushable, (u32 __user *) optval))
515 err = -EFAULT; 527 err = -EFAULT;
516 528
517 break; 529 break;
@@ -528,6 +540,7 @@ static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, ch
528static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen) 540static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
529{ 541{
530 struct sock *sk = sock->sk; 542 struct sock *sk = sock->sk;
543 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
531 struct l2cap_options opts; 544 struct l2cap_options opts;
532 int len, err = 0; 545 int len, err = 0;
533 u32 opt; 546 u32 opt;
@@ -543,13 +556,13 @@ static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __us
543 break; 556 break;
544 } 557 }
545 558
546 opts.imtu = l2cap_pi(sk)->imtu; 559 opts.imtu = chan->imtu;
547 opts.omtu = l2cap_pi(sk)->omtu; 560 opts.omtu = chan->omtu;
548 opts.flush_to = l2cap_pi(sk)->flush_to; 561 opts.flush_to = chan->flush_to;
549 opts.mode = l2cap_pi(sk)->mode; 562 opts.mode = chan->mode;
550 opts.fcs = l2cap_pi(sk)->fcs; 563 opts.fcs = chan->fcs;
551 opts.max_tx = l2cap_pi(sk)->max_tx; 564 opts.max_tx = chan->max_tx;
552 opts.txwin_size = (__u16)l2cap_pi(sk)->tx_win; 565 opts.txwin_size = (__u16)chan->tx_win;
553 566
554 len = min_t(unsigned int, sizeof(opts), optlen); 567 len = min_t(unsigned int, sizeof(opts), optlen);
555 if (copy_from_user((char *) &opts, optval, len)) { 568 if (copy_from_user((char *) &opts, optval, len)) {
@@ -562,10 +575,10 @@ static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __us
562 break; 575 break;
563 } 576 }
564 577
565 l2cap_pi(sk)->mode = opts.mode; 578 chan->mode = opts.mode;
566 switch (l2cap_pi(sk)->mode) { 579 switch (chan->mode) {
567 case L2CAP_MODE_BASIC: 580 case L2CAP_MODE_BASIC:
568 l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_STATE2_DEVICE; 581 chan->conf_state &= ~L2CAP_CONF_STATE2_DEVICE;
569 break; 582 break;
570 case L2CAP_MODE_ERTM: 583 case L2CAP_MODE_ERTM:
571 case L2CAP_MODE_STREAMING: 584 case L2CAP_MODE_STREAMING:
@@ -577,11 +590,11 @@ static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __us
577 break; 590 break;
578 } 591 }
579 592
580 l2cap_pi(sk)->imtu = opts.imtu; 593 chan->imtu = opts.imtu;
581 l2cap_pi(sk)->omtu = opts.omtu; 594 chan->omtu = opts.omtu;
582 l2cap_pi(sk)->fcs = opts.fcs; 595 chan->fcs = opts.fcs;
583 l2cap_pi(sk)->max_tx = opts.max_tx; 596 chan->max_tx = opts.max_tx;
584 l2cap_pi(sk)->tx_win = (__u8)opts.txwin_size; 597 chan->tx_win = (__u8)opts.txwin_size;
585 break; 598 break;
586 599
587 case L2CAP_LM: 600 case L2CAP_LM:
@@ -591,14 +604,14 @@ static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __us
591 } 604 }
592 605
593 if (opt & L2CAP_LM_AUTH) 606 if (opt & L2CAP_LM_AUTH)
594 l2cap_pi(sk)->sec_level = BT_SECURITY_LOW; 607 chan->sec_level = BT_SECURITY_LOW;
595 if (opt & L2CAP_LM_ENCRYPT) 608 if (opt & L2CAP_LM_ENCRYPT)
596 l2cap_pi(sk)->sec_level = BT_SECURITY_MEDIUM; 609 chan->sec_level = BT_SECURITY_MEDIUM;
597 if (opt & L2CAP_LM_SECURE) 610 if (opt & L2CAP_LM_SECURE)
598 l2cap_pi(sk)->sec_level = BT_SECURITY_HIGH; 611 chan->sec_level = BT_SECURITY_HIGH;
599 612
600 l2cap_pi(sk)->role_switch = (opt & L2CAP_LM_MASTER); 613 chan->role_switch = (opt & L2CAP_LM_MASTER);
601 l2cap_pi(sk)->force_reliable = (opt & L2CAP_LM_RELIABLE); 614 chan->force_reliable = (opt & L2CAP_LM_RELIABLE);
602 break; 615 break;
603 616
604 default: 617 default:
@@ -613,6 +626,7 @@ static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __us
613static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) 626static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
614{ 627{
615 struct sock *sk = sock->sk; 628 struct sock *sk = sock->sk;
629 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
616 struct bt_security sec; 630 struct bt_security sec;
617 int len, err = 0; 631 int len, err = 0;
618 u32 opt; 632 u32 opt;
@@ -649,7 +663,7 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, ch
649 break; 663 break;
650 } 664 }
651 665
652 l2cap_pi(sk)->sec_level = sec.level; 666 chan->sec_level = sec.level;
653 break; 667 break;
654 668
655 case BT_DEFER_SETUP: 669 case BT_DEFER_SETUP:
@@ -678,7 +692,7 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, ch
678 } 692 }
679 693
680 if (opt == BT_FLUSHABLE_OFF) { 694 if (opt == BT_FLUSHABLE_OFF) {
681 struct l2cap_conn *conn = l2cap_pi(sk)->conn; 695 struct l2cap_conn *conn = chan->conn;
682 /* proceed further only when we have l2cap_conn and 696 /* proceed further only when we have l2cap_conn and
683 No Flush support in the LM */ 697 No Flush support in the LM */
684 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) { 698 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
@@ -687,7 +701,7 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, ch
687 } 701 }
688 } 702 }
689 703
690 l2cap_pi(sk)->flushable = opt; 704 chan->flushable = opt;
691 break; 705 break;
692 706
693 default: 707 default:
@@ -702,7 +716,7 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, ch
702static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len) 716static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
703{ 717{
704 struct sock *sk = sock->sk; 718 struct sock *sk = sock->sk;
705 struct l2cap_pinfo *pi = l2cap_pi(sk); 719 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
706 struct sk_buff *skb; 720 struct sk_buff *skb;
707 u16 control; 721 u16 control;
708 int err; 722 int err;
@@ -725,76 +739,77 @@ static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct ms
725 739
726 /* Connectionless channel */ 740 /* Connectionless channel */
727 if (sk->sk_type == SOCK_DGRAM) { 741 if (sk->sk_type == SOCK_DGRAM) {
728 skb = l2cap_create_connless_pdu(sk, msg, len); 742 skb = l2cap_create_connless_pdu(chan, msg, len);
729 if (IS_ERR(skb)) { 743 if (IS_ERR(skb)) {
730 err = PTR_ERR(skb); 744 err = PTR_ERR(skb);
731 } else { 745 } else {
732 l2cap_do_send(sk, skb); 746 l2cap_do_send(chan, skb);
733 err = len; 747 err = len;
734 } 748 }
735 goto done; 749 goto done;
736 } 750 }
737 751
738 switch (pi->mode) { 752 switch (chan->mode) {
739 case L2CAP_MODE_BASIC: 753 case L2CAP_MODE_BASIC:
740 /* Check outgoing MTU */ 754 /* Check outgoing MTU */
741 if (len > pi->omtu) { 755 if (len > chan->omtu) {
742 err = -EMSGSIZE; 756 err = -EMSGSIZE;
743 goto done; 757 goto done;
744 } 758 }
745 759
746 /* Create a basic PDU */ 760 /* Create a basic PDU */
747 skb = l2cap_create_basic_pdu(sk, msg, len); 761 skb = l2cap_create_basic_pdu(chan, msg, len);
748 if (IS_ERR(skb)) { 762 if (IS_ERR(skb)) {
749 err = PTR_ERR(skb); 763 err = PTR_ERR(skb);
750 goto done; 764 goto done;
751 } 765 }
752 766
753 l2cap_do_send(sk, skb); 767 l2cap_do_send(chan, skb);
754 err = len; 768 err = len;
755 break; 769 break;
756 770
757 case L2CAP_MODE_ERTM: 771 case L2CAP_MODE_ERTM:
758 case L2CAP_MODE_STREAMING: 772 case L2CAP_MODE_STREAMING:
759 /* Entire SDU fits into one PDU */ 773 /* Entire SDU fits into one PDU */
760 if (len <= pi->chan->remote_mps) { 774 if (len <= chan->remote_mps) {
761 control = L2CAP_SDU_UNSEGMENTED; 775 control = L2CAP_SDU_UNSEGMENTED;
762 skb = l2cap_create_iframe_pdu(sk, msg, len, control, 0); 776 skb = l2cap_create_iframe_pdu(chan, msg, len, control,
777 0);
763 if (IS_ERR(skb)) { 778 if (IS_ERR(skb)) {
764 err = PTR_ERR(skb); 779 err = PTR_ERR(skb);
765 goto done; 780 goto done;
766 } 781 }
767 __skb_queue_tail(&pi->chan->tx_q, skb); 782 __skb_queue_tail(&chan->tx_q, skb);
768 783
769 if (pi->chan->tx_send_head == NULL) 784 if (chan->tx_send_head == NULL)
770 pi->chan->tx_send_head = skb; 785 chan->tx_send_head = skb;
771 786
772 } else { 787 } else {
773 /* Segment SDU into multiples PDUs */ 788 /* Segment SDU into multiples PDUs */
774 err = l2cap_sar_segment_sdu(pi->chan, msg, len); 789 err = l2cap_sar_segment_sdu(chan, msg, len);
775 if (err < 0) 790 if (err < 0)
776 goto done; 791 goto done;
777 } 792 }
778 793
779 if (pi->mode == L2CAP_MODE_STREAMING) { 794 if (chan->mode == L2CAP_MODE_STREAMING) {
780 l2cap_streaming_send(pi->chan); 795 l2cap_streaming_send(chan);
781 err = len; 796 err = len;
782 break; 797 break;
783 } 798 }
784 799
785 if ((pi->chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 800 if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
786 (pi->chan->conn_state & L2CAP_CONN_WAIT_F)) { 801 (chan->conn_state & L2CAP_CONN_WAIT_F)) {
787 err = len; 802 err = len;
788 break; 803 break;
789 } 804 }
790 err = l2cap_ertm_send(pi->chan); 805 err = l2cap_ertm_send(chan);
791 806
792 if (err >= 0) 807 if (err >= 0)
793 err = len; 808 err = len;
794 break; 809 break;
795 810
796 default: 811 default:
797 BT_DBG("bad state %1.1x", pi->mode); 812 BT_DBG("bad state %1.1x", chan->mode);
798 err = -EBADFD; 813 err = -EBADFD;
799 } 814 }
800 815
@@ -810,7 +825,9 @@ static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct ms
810 lock_sock(sk); 825 lock_sock(sk);
811 826
812 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) { 827 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
813 __l2cap_connect_rsp_defer(sk); 828 sk->sk_state = BT_CONFIG;
829
830 __l2cap_connect_rsp_defer(l2cap_pi(sk)->chan);
814 release_sock(sk); 831 release_sock(sk);
815 return 0; 832 return 0;
816 } 833 }
@@ -834,6 +851,8 @@ void l2cap_sock_kill(struct sock *sk)
834 BT_DBG("sk %p state %d", sk, sk->sk_state); 851 BT_DBG("sk %p state %d", sk, sk->sk_state);
835 852
836 /* Kill poor orphan */ 853 /* Kill poor orphan */
854
855 l2cap_chan_free(l2cap_pi(sk)->chan);
837 bt_sock_unlink(&l2cap_sk_list, sk); 856 bt_sock_unlink(&l2cap_sk_list, sk);
838 sock_set_flag(sk, SOCK_DEAD); 857 sock_set_flag(sk, SOCK_DEAD);
839 sock_put(sk); 858 sock_put(sk);
@@ -865,8 +884,8 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)
865 884
866void __l2cap_sock_close(struct sock *sk, int reason) 885void __l2cap_sock_close(struct sock *sk, int reason)
867{ 886{
868 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
869 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 887 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
888 struct l2cap_conn *conn = chan->conn;
870 889
871 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket); 890 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
872 891
@@ -898,8 +917,8 @@ void __l2cap_sock_close(struct sock *sk, int reason)
898 else 917 else
899 result = L2CAP_CR_BAD_PSM; 918 result = L2CAP_CR_BAD_PSM;
900 919
901 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); 920 rsp.scid = cpu_to_le16(chan->dcid);
902 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); 921 rsp.dcid = cpu_to_le16(chan->scid);
903 rsp.result = cpu_to_le16(result); 922 rsp.result = cpu_to_le16(result);
904 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 923 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
905 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 924 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
@@ -923,6 +942,7 @@ void __l2cap_sock_close(struct sock *sk, int reason)
923static int l2cap_sock_shutdown(struct socket *sock, int how) 942static int l2cap_sock_shutdown(struct socket *sock, int how)
924{ 943{
925 struct sock *sk = sock->sk; 944 struct sock *sk = sock->sk;
945 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
926 int err = 0; 946 int err = 0;
927 947
928 BT_DBG("sock %p, sk %p", sock, sk); 948 BT_DBG("sock %p, sk %p", sock, sk);
@@ -932,7 +952,7 @@ static int l2cap_sock_shutdown(struct socket *sock, int how)
932 952
933 lock_sock(sk); 953 lock_sock(sk);
934 if (!sk->sk_shutdown) { 954 if (!sk->sk_shutdown) {
935 if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) 955 if (chan->mode == L2CAP_MODE_ERTM)
936 err = __l2cap_wait_ack(sk); 956 err = __l2cap_wait_ack(sk);
937 957
938 sk->sk_shutdown = SHUTDOWN_MASK; 958 sk->sk_shutdown = SHUTDOWN_MASK;
@@ -979,44 +999,47 @@ static void l2cap_sock_destruct(struct sock *sk)
979void l2cap_sock_init(struct sock *sk, struct sock *parent) 999void l2cap_sock_init(struct sock *sk, struct sock *parent)
980{ 1000{
981 struct l2cap_pinfo *pi = l2cap_pi(sk); 1001 struct l2cap_pinfo *pi = l2cap_pi(sk);
1002 struct l2cap_chan *chan = pi->chan;
982 1003
983 BT_DBG("sk %p", sk); 1004 BT_DBG("sk %p", sk);
984 1005
985 if (parent) { 1006 if (parent) {
1007 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
1008
986 sk->sk_type = parent->sk_type; 1009 sk->sk_type = parent->sk_type;
987 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup; 1010 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
988 1011
989 pi->imtu = l2cap_pi(parent)->imtu; 1012 chan->imtu = pchan->imtu;
990 pi->omtu = l2cap_pi(parent)->omtu; 1013 chan->omtu = pchan->omtu;
991 pi->conf_state = l2cap_pi(parent)->conf_state; 1014 chan->conf_state = pchan->conf_state;
992 pi->mode = l2cap_pi(parent)->mode; 1015 chan->mode = pchan->mode;
993 pi->fcs = l2cap_pi(parent)->fcs; 1016 chan->fcs = pchan->fcs;
994 pi->max_tx = l2cap_pi(parent)->max_tx; 1017 chan->max_tx = pchan->max_tx;
995 pi->tx_win = l2cap_pi(parent)->tx_win; 1018 chan->tx_win = pchan->tx_win;
996 pi->sec_level = l2cap_pi(parent)->sec_level; 1019 chan->sec_level = pchan->sec_level;
997 pi->role_switch = l2cap_pi(parent)->role_switch; 1020 chan->role_switch = pchan->role_switch;
998 pi->force_reliable = l2cap_pi(parent)->force_reliable; 1021 chan->force_reliable = pchan->force_reliable;
999 pi->flushable = l2cap_pi(parent)->flushable; 1022 chan->flushable = pchan->flushable;
1000 } else { 1023 } else {
1001 pi->imtu = L2CAP_DEFAULT_MTU; 1024 chan->imtu = L2CAP_DEFAULT_MTU;
1002 pi->omtu = 0; 1025 chan->omtu = 0;
1003 if (!disable_ertm && sk->sk_type == SOCK_STREAM) { 1026 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
1004 pi->mode = L2CAP_MODE_ERTM; 1027 chan->mode = L2CAP_MODE_ERTM;
1005 pi->conf_state |= L2CAP_CONF_STATE2_DEVICE; 1028 chan->conf_state |= L2CAP_CONF_STATE2_DEVICE;
1006 } else { 1029 } else {
1007 pi->mode = L2CAP_MODE_BASIC; 1030 chan->mode = L2CAP_MODE_BASIC;
1008 } 1031 }
1009 pi->max_tx = L2CAP_DEFAULT_MAX_TX; 1032 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
1010 pi->fcs = L2CAP_FCS_CRC16; 1033 chan->fcs = L2CAP_FCS_CRC16;
1011 pi->tx_win = L2CAP_DEFAULT_TX_WINDOW; 1034 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
1012 pi->sec_level = BT_SECURITY_LOW; 1035 chan->sec_level = BT_SECURITY_LOW;
1013 pi->role_switch = 0; 1036 chan->role_switch = 0;
1014 pi->force_reliable = 0; 1037 chan->force_reliable = 0;
1015 pi->flushable = BT_FLUSHABLE_OFF; 1038 chan->flushable = BT_FLUSHABLE_OFF;
1016 } 1039 }
1017 1040
1018 /* Default config options */ 1041 /* Default config options */
1019 pi->flush_to = L2CAP_DEFAULT_FLUSH_TO; 1042 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
1020} 1043}
1021 1044
1022static struct proto l2cap_proto = { 1045static struct proto l2cap_proto = {
@@ -1054,6 +1077,7 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1054 int kern) 1077 int kern)
1055{ 1078{
1056 struct sock *sk; 1079 struct sock *sk;
1080 struct l2cap_chan *chan;
1057 1081
1058 BT_DBG("sock %p", sock); 1082 BT_DBG("sock %p", sock);
1059 1083
@@ -1072,11 +1096,19 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1072 if (!sk) 1096 if (!sk)
1073 return -ENOMEM; 1097 return -ENOMEM;
1074 1098
1099 chan = l2cap_chan_alloc(sk);
1100 if (!chan) {
1101 l2cap_sock_kill(sk);
1102 return -ENOMEM;
1103 }
1104
1105 l2cap_pi(sk)->chan = chan;
1106
1075 l2cap_sock_init(sk, NULL); 1107 l2cap_sock_init(sk, NULL);
1076 return 0; 1108 return 0;
1077} 1109}
1078 1110
1079const struct proto_ops l2cap_sock_ops = { 1111static const struct proto_ops l2cap_sock_ops = {
1080 .family = PF_BLUETOOTH, 1112 .family = PF_BLUETOOTH,
1081 .owner = THIS_MODULE, 1113 .owner = THIS_MODULE,
1082 .release = l2cap_sock_release, 1114 .release = l2cap_sock_release,