diff options
Diffstat (limited to 'net/bluetooth/l2cap.c')
-rw-r--r-- | net/bluetooth/l2cap.c | 602 |
1 files changed, 440 insertions, 162 deletions
diff --git a/net/bluetooth/l2cap.c b/net/bluetooth/l2cap.c index b93748e224ff..ca4d3b40d5ce 100644 --- a/net/bluetooth/l2cap.c +++ b/net/bluetooth/l2cap.c | |||
@@ -50,9 +50,10 @@ | |||
50 | #include <net/bluetooth/hci_core.h> | 50 | #include <net/bluetooth/hci_core.h> |
51 | #include <net/bluetooth/l2cap.h> | 51 | #include <net/bluetooth/l2cap.h> |
52 | 52 | ||
53 | #define VERSION "2.11" | 53 | #define VERSION "2.13" |
54 | 54 | ||
55 | static u32 l2cap_feat_mask = 0x0000; | 55 | static u32 l2cap_feat_mask = 0x0080; |
56 | static u8 l2cap_fixed_chan[8] = { 0x02, }; | ||
56 | 57 | ||
57 | static const struct proto_ops l2cap_sock_ops; | 58 | static const struct proto_ops l2cap_sock_ops; |
58 | 59 | ||
@@ -77,9 +78,10 @@ static void l2cap_sock_timeout(unsigned long arg) | |||
77 | 78 | ||
78 | bh_lock_sock(sk); | 79 | bh_lock_sock(sk); |
79 | 80 | ||
80 | if (sk->sk_state == BT_CONNECT && | 81 | if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG) |
81 | (l2cap_pi(sk)->link_mode & (L2CAP_LM_AUTH | | 82 | reason = ECONNREFUSED; |
82 | L2CAP_LM_ENCRYPT | L2CAP_LM_SECURE))) | 83 | else if (sk->sk_state == BT_CONNECT && |
84 | l2cap_pi(sk)->sec_level != BT_SECURITY_SDP) | ||
83 | reason = ECONNREFUSED; | 85 | reason = ECONNREFUSED; |
84 | else | 86 | else |
85 | reason = ETIMEDOUT; | 87 | reason = ETIMEDOUT; |
@@ -204,6 +206,8 @@ static void __l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct so | |||
204 | 206 | ||
205 | BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, l2cap_pi(sk)->psm, l2cap_pi(sk)->dcid); | 207 | BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, l2cap_pi(sk)->psm, l2cap_pi(sk)->dcid); |
206 | 208 | ||
209 | conn->disc_reason = 0x13; | ||
210 | |||
207 | l2cap_pi(sk)->conn = conn; | 211 | l2cap_pi(sk)->conn = conn; |
208 | 212 | ||
209 | if (sk->sk_type == SOCK_SEQPACKET) { | 213 | if (sk->sk_type == SOCK_SEQPACKET) { |
@@ -259,18 +263,35 @@ static void l2cap_chan_del(struct sock *sk, int err) | |||
259 | } | 263 | } |
260 | 264 | ||
261 | /* Service level security */ | 265 | /* Service level security */ |
262 | static inline int l2cap_check_link_mode(struct sock *sk) | 266 | static inline int l2cap_check_security(struct sock *sk) |
263 | { | 267 | { |
264 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; | 268 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; |
269 | __u8 auth_type; | ||
265 | 270 | ||
266 | if ((l2cap_pi(sk)->link_mode & L2CAP_LM_ENCRYPT) || | 271 | if (l2cap_pi(sk)->psm == cpu_to_le16(0x0001)) { |
267 | (l2cap_pi(sk)->link_mode & L2CAP_LM_SECURE)) | 272 | if (l2cap_pi(sk)->sec_level == BT_SECURITY_HIGH) |
268 | return hci_conn_encrypt(conn->hcon); | 273 | auth_type = HCI_AT_NO_BONDING_MITM; |
274 | else | ||
275 | auth_type = HCI_AT_NO_BONDING; | ||
269 | 276 | ||
270 | if (l2cap_pi(sk)->link_mode & L2CAP_LM_AUTH) | 277 | if (l2cap_pi(sk)->sec_level == BT_SECURITY_LOW) |
271 | return hci_conn_auth(conn->hcon); | 278 | l2cap_pi(sk)->sec_level = BT_SECURITY_SDP; |
279 | } else { | ||
280 | switch (l2cap_pi(sk)->sec_level) { | ||
281 | case BT_SECURITY_HIGH: | ||
282 | auth_type = HCI_AT_GENERAL_BONDING_MITM; | ||
283 | break; | ||
284 | case BT_SECURITY_MEDIUM: | ||
285 | auth_type = HCI_AT_GENERAL_BONDING; | ||
286 | break; | ||
287 | default: | ||
288 | auth_type = HCI_AT_NO_BONDING; | ||
289 | break; | ||
290 | } | ||
291 | } | ||
272 | 292 | ||
273 | return 1; | 293 | return hci_conn_security(conn->hcon, l2cap_pi(sk)->sec_level, |
294 | auth_type); | ||
274 | } | 295 | } |
275 | 296 | ||
276 | static inline u8 l2cap_get_ident(struct l2cap_conn *conn) | 297 | static inline u8 l2cap_get_ident(struct l2cap_conn *conn) |
@@ -312,7 +333,10 @@ static void l2cap_do_start(struct sock *sk) | |||
312 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; | 333 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; |
313 | 334 | ||
314 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { | 335 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { |
315 | if (l2cap_check_link_mode(sk)) { | 336 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
337 | return; | ||
338 | |||
339 | if (l2cap_check_security(sk)) { | ||
316 | struct l2cap_conn_req req; | 340 | struct l2cap_conn_req req; |
317 | req.scid = cpu_to_le16(l2cap_pi(sk)->scid); | 341 | req.scid = cpu_to_le16(l2cap_pi(sk)->scid); |
318 | req.psm = l2cap_pi(sk)->psm; | 342 | req.psm = l2cap_pi(sk)->psm; |
@@ -356,7 +380,7 @@ static void l2cap_conn_start(struct l2cap_conn *conn) | |||
356 | } | 380 | } |
357 | 381 | ||
358 | if (sk->sk_state == BT_CONNECT) { | 382 | if (sk->sk_state == BT_CONNECT) { |
359 | if (l2cap_check_link_mode(sk)) { | 383 | if (l2cap_check_security(sk)) { |
360 | struct l2cap_conn_req req; | 384 | struct l2cap_conn_req req; |
361 | req.scid = cpu_to_le16(l2cap_pi(sk)->scid); | 385 | req.scid = cpu_to_le16(l2cap_pi(sk)->scid); |
362 | req.psm = l2cap_pi(sk)->psm; | 386 | req.psm = l2cap_pi(sk)->psm; |
@@ -371,10 +395,18 @@ static void l2cap_conn_start(struct l2cap_conn *conn) | |||
371 | rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); | 395 | rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); |
372 | rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); | 396 | rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); |
373 | 397 | ||
374 | if (l2cap_check_link_mode(sk)) { | 398 | if (l2cap_check_security(sk)) { |
375 | sk->sk_state = BT_CONFIG; | 399 | if (bt_sk(sk)->defer_setup) { |
376 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); | 400 | struct sock *parent = bt_sk(sk)->parent; |
377 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); | 401 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
402 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); | ||
403 | parent->sk_data_ready(parent, 0); | ||
404 | |||
405 | } else { | ||
406 | sk->sk_state = BT_CONFIG; | ||
407 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); | ||
408 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); | ||
409 | } | ||
378 | } else { | 410 | } else { |
379 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); | 411 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
380 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); | 412 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
@@ -426,7 +458,7 @@ static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) | |||
426 | read_lock(&l->lock); | 458 | read_lock(&l->lock); |
427 | 459 | ||
428 | for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) { | 460 | for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) { |
429 | if (l2cap_pi(sk)->link_mode & L2CAP_LM_RELIABLE) | 461 | if (l2cap_pi(sk)->force_reliable) |
430 | sk->sk_err = err; | 462 | sk->sk_err = err; |
431 | } | 463 | } |
432 | 464 | ||
@@ -437,6 +469,7 @@ static void l2cap_info_timeout(unsigned long arg) | |||
437 | { | 469 | { |
438 | struct l2cap_conn *conn = (void *) arg; | 470 | struct l2cap_conn *conn = (void *) arg; |
439 | 471 | ||
472 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; | ||
440 | conn->info_ident = 0; | 473 | conn->info_ident = 0; |
441 | 474 | ||
442 | l2cap_conn_start(conn); | 475 | l2cap_conn_start(conn); |
@@ -470,6 +503,8 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) | |||
470 | spin_lock_init(&conn->lock); | 503 | spin_lock_init(&conn->lock); |
471 | rwlock_init(&conn->chan_list.lock); | 504 | rwlock_init(&conn->chan_list.lock); |
472 | 505 | ||
506 | conn->disc_reason = 0x13; | ||
507 | |||
473 | return conn; | 508 | return conn; |
474 | } | 509 | } |
475 | 510 | ||
@@ -483,8 +518,7 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err) | |||
483 | 518 | ||
484 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); | 519 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
485 | 520 | ||
486 | if (conn->rx_skb) | 521 | kfree_skb(conn->rx_skb); |
487 | kfree_skb(conn->rx_skb); | ||
488 | 522 | ||
489 | /* Kill channels */ | 523 | /* Kill channels */ |
490 | while ((sk = conn->chan_list.head)) { | 524 | while ((sk = conn->chan_list.head)) { |
@@ -608,7 +642,6 @@ static void __l2cap_sock_close(struct sock *sk, int reason) | |||
608 | 642 | ||
609 | case BT_CONNECTED: | 643 | case BT_CONNECTED: |
610 | case BT_CONFIG: | 644 | case BT_CONFIG: |
611 | case BT_CONNECT2: | ||
612 | if (sk->sk_type == SOCK_SEQPACKET) { | 645 | if (sk->sk_type == SOCK_SEQPACKET) { |
613 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; | 646 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; |
614 | struct l2cap_disconn_req req; | 647 | struct l2cap_disconn_req req; |
@@ -624,6 +657,27 @@ static void __l2cap_sock_close(struct sock *sk, int reason) | |||
624 | l2cap_chan_del(sk, reason); | 657 | l2cap_chan_del(sk, reason); |
625 | break; | 658 | break; |
626 | 659 | ||
660 | case BT_CONNECT2: | ||
661 | if (sk->sk_type == SOCK_SEQPACKET) { | ||
662 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; | ||
663 | struct l2cap_conn_rsp rsp; | ||
664 | __u16 result; | ||
665 | |||
666 | if (bt_sk(sk)->defer_setup) | ||
667 | result = L2CAP_CR_SEC_BLOCK; | ||
668 | else | ||
669 | result = L2CAP_CR_BAD_PSM; | ||
670 | |||
671 | rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); | ||
672 | rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); | ||
673 | rsp.result = cpu_to_le16(result); | ||
674 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); | ||
675 | l2cap_send_cmd(conn, l2cap_pi(sk)->ident, | ||
676 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); | ||
677 | } else | ||
678 | l2cap_chan_del(sk, reason); | ||
679 | break; | ||
680 | |||
627 | case BT_CONNECT: | 681 | case BT_CONNECT: |
628 | case BT_DISCONN: | 682 | case BT_DISCONN: |
629 | l2cap_chan_del(sk, reason); | 683 | l2cap_chan_del(sk, reason); |
@@ -653,13 +707,19 @@ static void l2cap_sock_init(struct sock *sk, struct sock *parent) | |||
653 | 707 | ||
654 | if (parent) { | 708 | if (parent) { |
655 | sk->sk_type = parent->sk_type; | 709 | sk->sk_type = parent->sk_type; |
710 | bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup; | ||
711 | |||
656 | pi->imtu = l2cap_pi(parent)->imtu; | 712 | pi->imtu = l2cap_pi(parent)->imtu; |
657 | pi->omtu = l2cap_pi(parent)->omtu; | 713 | pi->omtu = l2cap_pi(parent)->omtu; |
658 | pi->link_mode = l2cap_pi(parent)->link_mode; | 714 | pi->sec_level = l2cap_pi(parent)->sec_level; |
715 | pi->role_switch = l2cap_pi(parent)->role_switch; | ||
716 | pi->force_reliable = l2cap_pi(parent)->force_reliable; | ||
659 | } else { | 717 | } else { |
660 | pi->imtu = L2CAP_DEFAULT_MTU; | 718 | pi->imtu = L2CAP_DEFAULT_MTU; |
661 | pi->omtu = 0; | 719 | pi->omtu = 0; |
662 | pi->link_mode = 0; | 720 | pi->sec_level = BT_SECURITY_LOW; |
721 | pi->role_switch = 0; | ||
722 | pi->force_reliable = 0; | ||
663 | } | 723 | } |
664 | 724 | ||
665 | /* Default config options */ | 725 | /* Default config options */ |
@@ -723,17 +783,24 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol) | |||
723 | return 0; | 783 | return 0; |
724 | } | 784 | } |
725 | 785 | ||
726 | static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len) | 786 | static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) |
727 | { | 787 | { |
728 | struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr; | ||
729 | struct sock *sk = sock->sk; | 788 | struct sock *sk = sock->sk; |
730 | int err = 0; | 789 | struct sockaddr_l2 la; |
790 | int len, err = 0; | ||
731 | 791 | ||
732 | BT_DBG("sk %p, %s %d", sk, batostr(&la->l2_bdaddr), la->l2_psm); | 792 | BT_DBG("sk %p", sk); |
733 | 793 | ||
734 | if (!addr || addr->sa_family != AF_BLUETOOTH) | 794 | if (!addr || addr->sa_family != AF_BLUETOOTH) |
735 | return -EINVAL; | 795 | return -EINVAL; |
736 | 796 | ||
797 | memset(&la, 0, sizeof(la)); | ||
798 | len = min_t(unsigned int, sizeof(la), alen); | ||
799 | memcpy(&la, addr, len); | ||
800 | |||
801 | if (la.l2_cid) | ||
802 | return -EINVAL; | ||
803 | |||
737 | lock_sock(sk); | 804 | lock_sock(sk); |
738 | 805 | ||
739 | if (sk->sk_state != BT_OPEN) { | 806 | if (sk->sk_state != BT_OPEN) { |
@@ -741,7 +808,7 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_ | |||
741 | goto done; | 808 | goto done; |
742 | } | 809 | } |
743 | 810 | ||
744 | if (la->l2_psm && btohs(la->l2_psm) < 0x1001 && | 811 | if (la.l2_psm && btohs(la.l2_psm) < 0x1001 && |
745 | !capable(CAP_NET_BIND_SERVICE)) { | 812 | !capable(CAP_NET_BIND_SERVICE)) { |
746 | err = -EACCES; | 813 | err = -EACCES; |
747 | goto done; | 814 | goto done; |
@@ -749,14 +816,17 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_ | |||
749 | 816 | ||
750 | write_lock_bh(&l2cap_sk_list.lock); | 817 | write_lock_bh(&l2cap_sk_list.lock); |
751 | 818 | ||
752 | if (la->l2_psm && __l2cap_get_sock_by_addr(la->l2_psm, &la->l2_bdaddr)) { | 819 | if (la.l2_psm && __l2cap_get_sock_by_addr(la.l2_psm, &la.l2_bdaddr)) { |
753 | err = -EADDRINUSE; | 820 | err = -EADDRINUSE; |
754 | } else { | 821 | } else { |
755 | /* Save source address */ | 822 | /* Save source address */ |
756 | bacpy(&bt_sk(sk)->src, &la->l2_bdaddr); | 823 | bacpy(&bt_sk(sk)->src, &la.l2_bdaddr); |
757 | l2cap_pi(sk)->psm = la->l2_psm; | 824 | l2cap_pi(sk)->psm = la.l2_psm; |
758 | l2cap_pi(sk)->sport = la->l2_psm; | 825 | l2cap_pi(sk)->sport = la.l2_psm; |
759 | sk->sk_state = BT_BOUND; | 826 | sk->sk_state = BT_BOUND; |
827 | |||
828 | if (btohs(la.l2_psm) == 0x0001 || btohs(la.l2_psm) == 0x0003) | ||
829 | l2cap_pi(sk)->sec_level = BT_SECURITY_SDP; | ||
760 | } | 830 | } |
761 | 831 | ||
762 | write_unlock_bh(&l2cap_sk_list.lock); | 832 | write_unlock_bh(&l2cap_sk_list.lock); |
@@ -776,7 +846,8 @@ static int l2cap_do_connect(struct sock *sk) | |||
776 | __u8 auth_type; | 846 | __u8 auth_type; |
777 | int err = 0; | 847 | int err = 0; |
778 | 848 | ||
779 | BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), l2cap_pi(sk)->psm); | 849 | BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), |
850 | l2cap_pi(sk)->psm); | ||
780 | 851 | ||
781 | if (!(hdev = hci_get_route(dst, src))) | 852 | if (!(hdev = hci_get_route(dst, src))) |
782 | return -EHOSTUNREACH; | 853 | return -EHOSTUNREACH; |
@@ -785,21 +856,42 @@ static int l2cap_do_connect(struct sock *sk) | |||
785 | 856 | ||
786 | err = -ENOMEM; | 857 | err = -ENOMEM; |
787 | 858 | ||
788 | if (l2cap_pi(sk)->link_mode & L2CAP_LM_AUTH || | 859 | if (sk->sk_type == SOCK_RAW) { |
789 | l2cap_pi(sk)->link_mode & L2CAP_LM_ENCRYPT || | 860 | switch (l2cap_pi(sk)->sec_level) { |
790 | l2cap_pi(sk)->link_mode & L2CAP_LM_SECURE) { | 861 | case BT_SECURITY_HIGH: |
791 | if (l2cap_pi(sk)->psm == cpu_to_le16(0x0001)) | 862 | auth_type = HCI_AT_DEDICATED_BONDING_MITM; |
863 | break; | ||
864 | case BT_SECURITY_MEDIUM: | ||
865 | auth_type = HCI_AT_DEDICATED_BONDING; | ||
866 | break; | ||
867 | default: | ||
868 | auth_type = HCI_AT_NO_BONDING; | ||
869 | break; | ||
870 | } | ||
871 | } else if (l2cap_pi(sk)->psm == cpu_to_le16(0x0001)) { | ||
872 | if (l2cap_pi(sk)->sec_level == BT_SECURITY_HIGH) | ||
792 | auth_type = HCI_AT_NO_BONDING_MITM; | 873 | auth_type = HCI_AT_NO_BONDING_MITM; |
793 | else | 874 | else |
794 | auth_type = HCI_AT_GENERAL_BONDING_MITM; | ||
795 | } else { | ||
796 | if (l2cap_pi(sk)->psm == cpu_to_le16(0x0001)) | ||
797 | auth_type = HCI_AT_NO_BONDING; | 875 | auth_type = HCI_AT_NO_BONDING; |
798 | else | 876 | |
877 | if (l2cap_pi(sk)->sec_level == BT_SECURITY_LOW) | ||
878 | l2cap_pi(sk)->sec_level = BT_SECURITY_SDP; | ||
879 | } else { | ||
880 | switch (l2cap_pi(sk)->sec_level) { | ||
881 | case BT_SECURITY_HIGH: | ||
882 | auth_type = HCI_AT_GENERAL_BONDING_MITM; | ||
883 | break; | ||
884 | case BT_SECURITY_MEDIUM: | ||
799 | auth_type = HCI_AT_GENERAL_BONDING; | 885 | auth_type = HCI_AT_GENERAL_BONDING; |
886 | break; | ||
887 | default: | ||
888 | auth_type = HCI_AT_NO_BONDING; | ||
889 | break; | ||
890 | } | ||
800 | } | 891 | } |
801 | 892 | ||
802 | hcon = hci_connect(hdev, ACL_LINK, dst, auth_type); | 893 | hcon = hci_connect(hdev, ACL_LINK, dst, |
894 | l2cap_pi(sk)->sec_level, auth_type); | ||
803 | if (!hcon) | 895 | if (!hcon) |
804 | goto done; | 896 | goto done; |
805 | 897 | ||
@@ -835,20 +927,25 @@ done: | |||
835 | 927 | ||
836 | static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) | 928 | static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) |
837 | { | 929 | { |
838 | struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr; | ||
839 | struct sock *sk = sock->sk; | 930 | struct sock *sk = sock->sk; |
840 | int err = 0; | 931 | struct sockaddr_l2 la; |
841 | 932 | int len, err = 0; | |
842 | lock_sock(sk); | ||
843 | 933 | ||
844 | BT_DBG("sk %p", sk); | 934 | BT_DBG("sk %p", sk); |
845 | 935 | ||
846 | if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_l2)) { | 936 | if (!addr || addr->sa_family != AF_BLUETOOTH) |
847 | err = -EINVAL; | 937 | return -EINVAL; |
848 | goto done; | 938 | |
849 | } | 939 | memset(&la, 0, sizeof(la)); |
940 | len = min_t(unsigned int, sizeof(la), alen); | ||
941 | memcpy(&la, addr, len); | ||
942 | |||
943 | if (la.l2_cid) | ||
944 | return -EINVAL; | ||
945 | |||
946 | lock_sock(sk); | ||
850 | 947 | ||
851 | if (sk->sk_type == SOCK_SEQPACKET && !la->l2_psm) { | 948 | if (sk->sk_type == SOCK_SEQPACKET && !la.l2_psm) { |
852 | err = -EINVAL; | 949 | err = -EINVAL; |
853 | goto done; | 950 | goto done; |
854 | } | 951 | } |
@@ -875,8 +972,8 @@ static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int al | |||
875 | } | 972 | } |
876 | 973 | ||
877 | /* Set destination address and psm */ | 974 | /* Set destination address and psm */ |
878 | bacpy(&bt_sk(sk)->dst, &la->l2_bdaddr); | 975 | bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr); |
879 | l2cap_pi(sk)->psm = la->l2_psm; | 976 | l2cap_pi(sk)->psm = la.l2_psm; |
880 | 977 | ||
881 | if ((err = l2cap_do_connect(sk))) | 978 | if ((err = l2cap_do_connect(sk))) |
882 | goto done; | 979 | goto done; |
@@ -1000,12 +1097,16 @@ static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *l | |||
1000 | addr->sa_family = AF_BLUETOOTH; | 1097 | addr->sa_family = AF_BLUETOOTH; |
1001 | *len = sizeof(struct sockaddr_l2); | 1098 | *len = sizeof(struct sockaddr_l2); |
1002 | 1099 | ||
1003 | if (peer) | 1100 | if (peer) { |
1101 | la->l2_psm = l2cap_pi(sk)->psm; | ||
1004 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst); | 1102 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst); |
1005 | else | 1103 | la->l2_cid = htobs(l2cap_pi(sk)->dcid); |
1104 | } else { | ||
1105 | la->l2_psm = l2cap_pi(sk)->sport; | ||
1006 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->src); | 1106 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->src); |
1107 | la->l2_cid = htobs(l2cap_pi(sk)->scid); | ||
1108 | } | ||
1007 | 1109 | ||
1008 | la->l2_psm = l2cap_pi(sk)->psm; | ||
1009 | return 0; | 1110 | return 0; |
1010 | } | 1111 | } |
1011 | 1112 | ||
@@ -1106,11 +1207,38 @@ static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct ms | |||
1106 | return err; | 1207 | return err; |
1107 | } | 1208 | } |
1108 | 1209 | ||
1109 | static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen) | 1210 | static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags) |
1211 | { | ||
1212 | struct sock *sk = sock->sk; | ||
1213 | |||
1214 | lock_sock(sk); | ||
1215 | |||
1216 | if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) { | ||
1217 | struct l2cap_conn_rsp rsp; | ||
1218 | |||
1219 | sk->sk_state = BT_CONFIG; | ||
1220 | |||
1221 | rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); | ||
1222 | rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); | ||
1223 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); | ||
1224 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); | ||
1225 | l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident, | ||
1226 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); | ||
1227 | |||
1228 | release_sock(sk); | ||
1229 | return 0; | ||
1230 | } | ||
1231 | |||
1232 | release_sock(sk); | ||
1233 | |||
1234 | return bt_sock_recvmsg(iocb, sock, msg, len, flags); | ||
1235 | } | ||
1236 | |||
1237 | static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, int optlen) | ||
1110 | { | 1238 | { |
1111 | struct sock *sk = sock->sk; | 1239 | struct sock *sk = sock->sk; |
1112 | struct l2cap_options opts; | 1240 | struct l2cap_options opts; |
1113 | int err = 0, len; | 1241 | int len, err = 0; |
1114 | u32 opt; | 1242 | u32 opt; |
1115 | 1243 | ||
1116 | BT_DBG("sk %p", sk); | 1244 | BT_DBG("sk %p", sk); |
@@ -1140,7 +1268,15 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, ch | |||
1140 | break; | 1268 | break; |
1141 | } | 1269 | } |
1142 | 1270 | ||
1143 | l2cap_pi(sk)->link_mode = opt; | 1271 | if (opt & L2CAP_LM_AUTH) |
1272 | l2cap_pi(sk)->sec_level = BT_SECURITY_LOW; | ||
1273 | if (opt & L2CAP_LM_ENCRYPT) | ||
1274 | l2cap_pi(sk)->sec_level = BT_SECURITY_MEDIUM; | ||
1275 | if (opt & L2CAP_LM_SECURE) | ||
1276 | l2cap_pi(sk)->sec_level = BT_SECURITY_HIGH; | ||
1277 | |||
1278 | l2cap_pi(sk)->role_switch = (opt & L2CAP_LM_MASTER); | ||
1279 | l2cap_pi(sk)->force_reliable = (opt & L2CAP_LM_RELIABLE); | ||
1144 | break; | 1280 | break; |
1145 | 1281 | ||
1146 | default: | 1282 | default: |
@@ -1152,12 +1288,77 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, ch | |||
1152 | return err; | 1288 | return err; |
1153 | } | 1289 | } |
1154 | 1290 | ||
1155 | static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) | 1291 | static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen) |
1292 | { | ||
1293 | struct sock *sk = sock->sk; | ||
1294 | struct bt_security sec; | ||
1295 | int len, err = 0; | ||
1296 | u32 opt; | ||
1297 | |||
1298 | BT_DBG("sk %p", sk); | ||
1299 | |||
1300 | if (level == SOL_L2CAP) | ||
1301 | return l2cap_sock_setsockopt_old(sock, optname, optval, optlen); | ||
1302 | |||
1303 | if (level != SOL_BLUETOOTH) | ||
1304 | return -ENOPROTOOPT; | ||
1305 | |||
1306 | lock_sock(sk); | ||
1307 | |||
1308 | switch (optname) { | ||
1309 | case BT_SECURITY: | ||
1310 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_RAW) { | ||
1311 | err = -EINVAL; | ||
1312 | break; | ||
1313 | } | ||
1314 | |||
1315 | sec.level = BT_SECURITY_LOW; | ||
1316 | |||
1317 | len = min_t(unsigned int, sizeof(sec), optlen); | ||
1318 | if (copy_from_user((char *) &sec, optval, len)) { | ||
1319 | err = -EFAULT; | ||
1320 | break; | ||
1321 | } | ||
1322 | |||
1323 | if (sec.level < BT_SECURITY_LOW || | ||
1324 | sec.level > BT_SECURITY_HIGH) { | ||
1325 | err = -EINVAL; | ||
1326 | break; | ||
1327 | } | ||
1328 | |||
1329 | l2cap_pi(sk)->sec_level = sec.level; | ||
1330 | break; | ||
1331 | |||
1332 | case BT_DEFER_SETUP: | ||
1333 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { | ||
1334 | err = -EINVAL; | ||
1335 | break; | ||
1336 | } | ||
1337 | |||
1338 | if (get_user(opt, (u32 __user *) optval)) { | ||
1339 | err = -EFAULT; | ||
1340 | break; | ||
1341 | } | ||
1342 | |||
1343 | bt_sk(sk)->defer_setup = opt; | ||
1344 | break; | ||
1345 | |||
1346 | default: | ||
1347 | err = -ENOPROTOOPT; | ||
1348 | break; | ||
1349 | } | ||
1350 | |||
1351 | release_sock(sk); | ||
1352 | return err; | ||
1353 | } | ||
1354 | |||
1355 | static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen) | ||
1156 | { | 1356 | { |
1157 | struct sock *sk = sock->sk; | 1357 | struct sock *sk = sock->sk; |
1158 | struct l2cap_options opts; | 1358 | struct l2cap_options opts; |
1159 | struct l2cap_conninfo cinfo; | 1359 | struct l2cap_conninfo cinfo; |
1160 | int len, err = 0; | 1360 | int len, err = 0; |
1361 | u32 opt; | ||
1161 | 1362 | ||
1162 | BT_DBG("sk %p", sk); | 1363 | BT_DBG("sk %p", sk); |
1163 | 1364 | ||
@@ -1180,12 +1381,36 @@ static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, ch | |||
1180 | break; | 1381 | break; |
1181 | 1382 | ||
1182 | case L2CAP_LM: | 1383 | case L2CAP_LM: |
1183 | if (put_user(l2cap_pi(sk)->link_mode, (u32 __user *) optval)) | 1384 | switch (l2cap_pi(sk)->sec_level) { |
1385 | case BT_SECURITY_LOW: | ||
1386 | opt = L2CAP_LM_AUTH; | ||
1387 | break; | ||
1388 | case BT_SECURITY_MEDIUM: | ||
1389 | opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT; | ||
1390 | break; | ||
1391 | case BT_SECURITY_HIGH: | ||
1392 | opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT | | ||
1393 | L2CAP_LM_SECURE; | ||
1394 | break; | ||
1395 | default: | ||
1396 | opt = 0; | ||
1397 | break; | ||
1398 | } | ||
1399 | |||
1400 | if (l2cap_pi(sk)->role_switch) | ||
1401 | opt |= L2CAP_LM_MASTER; | ||
1402 | |||
1403 | if (l2cap_pi(sk)->force_reliable) | ||
1404 | opt |= L2CAP_LM_RELIABLE; | ||
1405 | |||
1406 | if (put_user(opt, (u32 __user *) optval)) | ||
1184 | err = -EFAULT; | 1407 | err = -EFAULT; |
1185 | break; | 1408 | break; |
1186 | 1409 | ||
1187 | case L2CAP_CONNINFO: | 1410 | case L2CAP_CONNINFO: |
1188 | if (sk->sk_state != BT_CONNECTED) { | 1411 | if (sk->sk_state != BT_CONNECTED && |
1412 | !(sk->sk_state == BT_CONNECT2 && | ||
1413 | bt_sk(sk)->defer_setup)) { | ||
1189 | err = -ENOTCONN; | 1414 | err = -ENOTCONN; |
1190 | break; | 1415 | break; |
1191 | } | 1416 | } |
@@ -1208,6 +1433,60 @@ static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, ch | |||
1208 | return err; | 1433 | return err; |
1209 | } | 1434 | } |
1210 | 1435 | ||
1436 | static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) | ||
1437 | { | ||
1438 | struct sock *sk = sock->sk; | ||
1439 | struct bt_security sec; | ||
1440 | int len, err = 0; | ||
1441 | |||
1442 | BT_DBG("sk %p", sk); | ||
1443 | |||
1444 | if (level == SOL_L2CAP) | ||
1445 | return l2cap_sock_getsockopt_old(sock, optname, optval, optlen); | ||
1446 | |||
1447 | if (level != SOL_BLUETOOTH) | ||
1448 | return -ENOPROTOOPT; | ||
1449 | |||
1450 | if (get_user(len, optlen)) | ||
1451 | return -EFAULT; | ||
1452 | |||
1453 | lock_sock(sk); | ||
1454 | |||
1455 | switch (optname) { | ||
1456 | case BT_SECURITY: | ||
1457 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_RAW) { | ||
1458 | err = -EINVAL; | ||
1459 | break; | ||
1460 | } | ||
1461 | |||
1462 | sec.level = l2cap_pi(sk)->sec_level; | ||
1463 | |||
1464 | len = min_t(unsigned int, len, sizeof(sec)); | ||
1465 | if (copy_to_user(optval, (char *) &sec, len)) | ||
1466 | err = -EFAULT; | ||
1467 | |||
1468 | break; | ||
1469 | |||
1470 | case BT_DEFER_SETUP: | ||
1471 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { | ||
1472 | err = -EINVAL; | ||
1473 | break; | ||
1474 | } | ||
1475 | |||
1476 | if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval)) | ||
1477 | err = -EFAULT; | ||
1478 | |||
1479 | break; | ||
1480 | |||
1481 | default: | ||
1482 | err = -ENOPROTOOPT; | ||
1483 | break; | ||
1484 | } | ||
1485 | |||
1486 | release_sock(sk); | ||
1487 | return err; | ||
1488 | } | ||
1489 | |||
1211 | static int l2cap_sock_shutdown(struct socket *sock, int how) | 1490 | static int l2cap_sock_shutdown(struct socket *sock, int how) |
1212 | { | 1491 | { |
1213 | struct sock *sk = sock->sk; | 1492 | struct sock *sk = sock->sk; |
@@ -1270,11 +1549,6 @@ static void l2cap_chan_ready(struct sock *sk) | |||
1270 | */ | 1549 | */ |
1271 | parent->sk_data_ready(parent, 0); | 1550 | parent->sk_data_ready(parent, 0); |
1272 | } | 1551 | } |
1273 | |||
1274 | if (l2cap_pi(sk)->link_mode & L2CAP_LM_SECURE) { | ||
1275 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; | ||
1276 | hci_conn_change_link_key(conn->hcon); | ||
1277 | } | ||
1278 | } | 1552 | } |
1279 | 1553 | ||
1280 | /* Copy frame to all raw sockets on that connection */ | 1554 | /* Copy frame to all raw sockets on that connection */ |
@@ -1549,8 +1823,11 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hd | |||
1549 | 1823 | ||
1550 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && | 1824 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
1551 | cmd->ident == conn->info_ident) { | 1825 | cmd->ident == conn->info_ident) { |
1552 | conn->info_ident = 0; | ||
1553 | del_timer(&conn->info_timer); | 1826 | del_timer(&conn->info_timer); |
1827 | |||
1828 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; | ||
1829 | conn->info_ident = 0; | ||
1830 | |||
1554 | l2cap_conn_start(conn); | 1831 | l2cap_conn_start(conn); |
1555 | } | 1832 | } |
1556 | 1833 | ||
@@ -1580,6 +1857,7 @@ static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hd | |||
1580 | /* Check if the ACL is secure enough (if not SDP) */ | 1857 | /* Check if the ACL is secure enough (if not SDP) */ |
1581 | if (psm != cpu_to_le16(0x0001) && | 1858 | if (psm != cpu_to_le16(0x0001) && |
1582 | !hci_conn_check_link_mode(conn->hcon)) { | 1859 | !hci_conn_check_link_mode(conn->hcon)) { |
1860 | conn->disc_reason = 0x05; | ||
1583 | result = L2CAP_CR_SEC_BLOCK; | 1861 | result = L2CAP_CR_SEC_BLOCK; |
1584 | goto response; | 1862 | goto response; |
1585 | } | 1863 | } |
@@ -1621,11 +1899,18 @@ static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hd | |||
1621 | 1899 | ||
1622 | l2cap_pi(sk)->ident = cmd->ident; | 1900 | l2cap_pi(sk)->ident = cmd->ident; |
1623 | 1901 | ||
1624 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { | 1902 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
1625 | if (l2cap_check_link_mode(sk)) { | 1903 | if (l2cap_check_security(sk)) { |
1626 | sk->sk_state = BT_CONFIG; | 1904 | if (bt_sk(sk)->defer_setup) { |
1627 | result = L2CAP_CR_SUCCESS; | 1905 | sk->sk_state = BT_CONNECT2; |
1628 | status = L2CAP_CS_NO_INFO; | 1906 | result = L2CAP_CR_PEND; |
1907 | status = L2CAP_CS_AUTHOR_PEND; | ||
1908 | parent->sk_data_ready(parent, 0); | ||
1909 | } else { | ||
1910 | sk->sk_state = BT_CONFIG; | ||
1911 | result = L2CAP_CR_SUCCESS; | ||
1912 | status = L2CAP_CS_NO_INFO; | ||
1913 | } | ||
1629 | } else { | 1914 | } else { |
1630 | sk->sk_state = BT_CONNECT2; | 1915 | sk->sk_state = BT_CONNECT2; |
1631 | result = L2CAP_CR_PEND; | 1916 | result = L2CAP_CR_PEND; |
@@ -1695,11 +1980,14 @@ static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hd | |||
1695 | l2cap_pi(sk)->dcid = dcid; | 1980 | l2cap_pi(sk)->dcid = dcid; |
1696 | l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT; | 1981 | l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT; |
1697 | 1982 | ||
1983 | l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_CONNECT_PEND; | ||
1984 | |||
1698 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, | 1985 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
1699 | l2cap_build_conf_req(sk, req), req); | 1986 | l2cap_build_conf_req(sk, req), req); |
1700 | break; | 1987 | break; |
1701 | 1988 | ||
1702 | case L2CAP_CR_PEND: | 1989 | case L2CAP_CR_PEND: |
1990 | l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND; | ||
1703 | break; | 1991 | break; |
1704 | 1992 | ||
1705 | default: | 1993 | default: |
@@ -1908,6 +2196,14 @@ static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cm | |||
1908 | put_unaligned(cpu_to_le32(l2cap_feat_mask), (__le32 *) rsp->data); | 2196 | put_unaligned(cpu_to_le32(l2cap_feat_mask), (__le32 *) rsp->data); |
1909 | l2cap_send_cmd(conn, cmd->ident, | 2197 | l2cap_send_cmd(conn, cmd->ident, |
1910 | L2CAP_INFO_RSP, sizeof(buf), buf); | 2198 | L2CAP_INFO_RSP, sizeof(buf), buf); |
2199 | } else if (type == L2CAP_IT_FIXED_CHAN) { | ||
2200 | u8 buf[12]; | ||
2201 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; | ||
2202 | rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); | ||
2203 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); | ||
2204 | memcpy(buf + 4, l2cap_fixed_chan, 8); | ||
2205 | l2cap_send_cmd(conn, cmd->ident, | ||
2206 | L2CAP_INFO_RSP, sizeof(buf), buf); | ||
1911 | } else { | 2207 | } else { |
1912 | struct l2cap_info_rsp rsp; | 2208 | struct l2cap_info_rsp rsp; |
1913 | rsp.type = cpu_to_le16(type); | 2209 | rsp.type = cpu_to_le16(type); |
@@ -1929,14 +2225,31 @@ static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cm | |||
1929 | 2225 | ||
1930 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); | 2226 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
1931 | 2227 | ||
1932 | conn->info_ident = 0; | ||
1933 | |||
1934 | del_timer(&conn->info_timer); | 2228 | del_timer(&conn->info_timer); |
1935 | 2229 | ||
1936 | if (type == L2CAP_IT_FEAT_MASK) | 2230 | if (type == L2CAP_IT_FEAT_MASK) { |
1937 | conn->feat_mask = get_unaligned_le32(rsp->data); | 2231 | conn->feat_mask = get_unaligned_le32(rsp->data); |
1938 | 2232 | ||
1939 | l2cap_conn_start(conn); | 2233 | if (conn->feat_mask & 0x0080) { |
2234 | struct l2cap_info_req req; | ||
2235 | req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); | ||
2236 | |||
2237 | conn->info_ident = l2cap_get_ident(conn); | ||
2238 | |||
2239 | l2cap_send_cmd(conn, conn->info_ident, | ||
2240 | L2CAP_INFO_REQ, sizeof(req), &req); | ||
2241 | } else { | ||
2242 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; | ||
2243 | conn->info_ident = 0; | ||
2244 | |||
2245 | l2cap_conn_start(conn); | ||
2246 | } | ||
2247 | } else if (type == L2CAP_IT_FIXED_CHAN) { | ||
2248 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; | ||
2249 | conn->info_ident = 0; | ||
2250 | |||
2251 | l2cap_conn_start(conn); | ||
2252 | } | ||
1940 | 2253 | ||
1941 | return 0; | 2254 | return 0; |
1942 | } | 2255 | } |
@@ -2143,10 +2456,15 @@ static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) | |||
2143 | continue; | 2456 | continue; |
2144 | 2457 | ||
2145 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { | 2458 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { |
2146 | lm1 |= (HCI_LM_ACCEPT | l2cap_pi(sk)->link_mode); | 2459 | lm1 |= HCI_LM_ACCEPT; |
2460 | if (l2cap_pi(sk)->role_switch) | ||
2461 | lm1 |= HCI_LM_MASTER; | ||
2147 | exact++; | 2462 | exact++; |
2148 | } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) | 2463 | } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
2149 | lm2 |= (HCI_LM_ACCEPT | l2cap_pi(sk)->link_mode); | 2464 | lm2 |= HCI_LM_ACCEPT; |
2465 | if (l2cap_pi(sk)->role_switch) | ||
2466 | lm2 |= HCI_LM_MASTER; | ||
2467 | } | ||
2150 | } | 2468 | } |
2151 | read_unlock(&l2cap_sk_list.lock); | 2469 | read_unlock(&l2cap_sk_list.lock); |
2152 | 2470 | ||
@@ -2172,89 +2490,48 @@ static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) | |||
2172 | return 0; | 2490 | return 0; |
2173 | } | 2491 | } |
2174 | 2492 | ||
2175 | static int l2cap_disconn_ind(struct hci_conn *hcon, u8 reason) | 2493 | static int l2cap_disconn_ind(struct hci_conn *hcon) |
2176 | { | 2494 | { |
2177 | BT_DBG("hcon %p reason %d", hcon, reason); | 2495 | struct l2cap_conn *conn = hcon->l2cap_data; |
2178 | 2496 | ||
2179 | if (hcon->type != ACL_LINK) | 2497 | BT_DBG("hcon %p", hcon); |
2180 | return 0; | ||
2181 | 2498 | ||
2182 | l2cap_conn_del(hcon, bt_err(reason)); | 2499 | if (hcon->type != ACL_LINK || !conn) |
2500 | return 0x13; | ||
2183 | 2501 | ||
2184 | return 0; | 2502 | return conn->disc_reason; |
2185 | } | 2503 | } |
2186 | 2504 | ||
2187 | static int l2cap_auth_cfm(struct hci_conn *hcon, u8 status) | 2505 | static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
2188 | { | 2506 | { |
2189 | struct l2cap_chan_list *l; | 2507 | BT_DBG("hcon %p reason %d", hcon, reason); |
2190 | struct l2cap_conn *conn = hcon->l2cap_data; | ||
2191 | struct sock *sk; | ||
2192 | 2508 | ||
2193 | if (!conn) | 2509 | if (hcon->type != ACL_LINK) |
2194 | return 0; | 2510 | return 0; |
2195 | 2511 | ||
2196 | l = &conn->chan_list; | 2512 | l2cap_conn_del(hcon, bt_err(reason)); |
2197 | |||
2198 | BT_DBG("conn %p", conn); | ||
2199 | |||
2200 | read_lock(&l->lock); | ||
2201 | |||
2202 | for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) { | ||
2203 | struct l2cap_pinfo *pi = l2cap_pi(sk); | ||
2204 | |||
2205 | bh_lock_sock(sk); | ||
2206 | |||
2207 | if ((pi->link_mode & (L2CAP_LM_ENCRYPT | L2CAP_LM_SECURE)) && | ||
2208 | !(hcon->link_mode & HCI_LM_ENCRYPT) && | ||
2209 | !status) { | ||
2210 | bh_unlock_sock(sk); | ||
2211 | continue; | ||
2212 | } | ||
2213 | |||
2214 | if (sk->sk_state == BT_CONNECT) { | ||
2215 | if (!status) { | ||
2216 | struct l2cap_conn_req req; | ||
2217 | req.scid = cpu_to_le16(l2cap_pi(sk)->scid); | ||
2218 | req.psm = l2cap_pi(sk)->psm; | ||
2219 | |||
2220 | l2cap_pi(sk)->ident = l2cap_get_ident(conn); | ||
2221 | |||
2222 | l2cap_send_cmd(conn, l2cap_pi(sk)->ident, | ||
2223 | L2CAP_CONN_REQ, sizeof(req), &req); | ||
2224 | } else { | ||
2225 | l2cap_sock_clear_timer(sk); | ||
2226 | l2cap_sock_set_timer(sk, HZ / 10); | ||
2227 | } | ||
2228 | } else if (sk->sk_state == BT_CONNECT2) { | ||
2229 | struct l2cap_conn_rsp rsp; | ||
2230 | __u16 result; | ||
2231 | 2513 | ||
2232 | if (!status) { | 2514 | return 0; |
2233 | sk->sk_state = BT_CONFIG; | 2515 | } |
2234 | result = L2CAP_CR_SUCCESS; | ||
2235 | } else { | ||
2236 | sk->sk_state = BT_DISCONN; | ||
2237 | l2cap_sock_set_timer(sk, HZ / 10); | ||
2238 | result = L2CAP_CR_SEC_BLOCK; | ||
2239 | } | ||
2240 | 2516 | ||
2241 | rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); | 2517 | static inline void l2cap_check_encryption(struct sock *sk, u8 encrypt) |
2242 | rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); | 2518 | { |
2243 | rsp.result = cpu_to_le16(result); | 2519 | if (sk->sk_type != SOCK_SEQPACKET) |
2244 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); | 2520 | return; |
2245 | l2cap_send_cmd(conn, l2cap_pi(sk)->ident, | ||
2246 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); | ||
2247 | } | ||
2248 | 2521 | ||
2249 | bh_unlock_sock(sk); | 2522 | if (encrypt == 0x00) { |
2523 | if (l2cap_pi(sk)->sec_level == BT_SECURITY_MEDIUM) { | ||
2524 | l2cap_sock_clear_timer(sk); | ||
2525 | l2cap_sock_set_timer(sk, HZ * 5); | ||
2526 | } else if (l2cap_pi(sk)->sec_level == BT_SECURITY_HIGH) | ||
2527 | __l2cap_sock_close(sk, ECONNREFUSED); | ||
2528 | } else { | ||
2529 | if (l2cap_pi(sk)->sec_level == BT_SECURITY_MEDIUM) | ||
2530 | l2cap_sock_clear_timer(sk); | ||
2250 | } | 2531 | } |
2251 | |||
2252 | read_unlock(&l->lock); | ||
2253 | |||
2254 | return 0; | ||
2255 | } | 2532 | } |
2256 | 2533 | ||
2257 | static int l2cap_encrypt_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) | 2534 | static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
2258 | { | 2535 | { |
2259 | struct l2cap_chan_list *l; | 2536 | struct l2cap_chan_list *l; |
2260 | struct l2cap_conn *conn = hcon->l2cap_data; | 2537 | struct l2cap_conn *conn = hcon->l2cap_data; |
@@ -2270,15 +2547,16 @@ static int l2cap_encrypt_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) | |||
2270 | read_lock(&l->lock); | 2547 | read_lock(&l->lock); |
2271 | 2548 | ||
2272 | for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) { | 2549 | for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) { |
2273 | struct l2cap_pinfo *pi = l2cap_pi(sk); | ||
2274 | |||
2275 | bh_lock_sock(sk); | 2550 | bh_lock_sock(sk); |
2276 | 2551 | ||
2277 | if ((pi->link_mode & (L2CAP_LM_ENCRYPT | L2CAP_LM_SECURE)) && | 2552 | if (l2cap_pi(sk)->conf_state & L2CAP_CONF_CONNECT_PEND) { |
2278 | (sk->sk_state == BT_CONNECTED || | 2553 | bh_unlock_sock(sk); |
2279 | sk->sk_state == BT_CONFIG) && | 2554 | continue; |
2280 | !status && encrypt == 0x00) { | 2555 | } |
2281 | __l2cap_sock_close(sk, ECONNREFUSED); | 2556 | |
2557 | if (!status && (sk->sk_state == BT_CONNECTED || | ||
2558 | sk->sk_state == BT_CONFIG)) { | ||
2559 | l2cap_check_encryption(sk, encrypt); | ||
2282 | bh_unlock_sock(sk); | 2560 | bh_unlock_sock(sk); |
2283 | continue; | 2561 | continue; |
2284 | } | 2562 | } |
@@ -2376,7 +2654,7 @@ static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 fl | |||
2376 | goto drop; | 2654 | goto drop; |
2377 | 2655 | ||
2378 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), | 2656 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), |
2379 | skb->len); | 2657 | skb->len); |
2380 | conn->rx_len = len - skb->len; | 2658 | conn->rx_len = len - skb->len; |
2381 | } else { | 2659 | } else { |
2382 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); | 2660 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
@@ -2398,7 +2676,7 @@ static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 fl | |||
2398 | } | 2676 | } |
2399 | 2677 | ||
2400 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), | 2678 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), |
2401 | skb->len); | 2679 | skb->len); |
2402 | conn->rx_len -= skb->len; | 2680 | conn->rx_len -= skb->len; |
2403 | 2681 | ||
2404 | if (!conn->rx_len) { | 2682 | if (!conn->rx_len) { |
@@ -2424,10 +2702,10 @@ static ssize_t l2cap_sysfs_show(struct class *dev, char *buf) | |||
2424 | sk_for_each(sk, node, &l2cap_sk_list.head) { | 2702 | sk_for_each(sk, node, &l2cap_sk_list.head) { |
2425 | struct l2cap_pinfo *pi = l2cap_pi(sk); | 2703 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
2426 | 2704 | ||
2427 | str += sprintf(str, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d 0x%x\n", | 2705 | str += sprintf(str, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d\n", |
2428 | batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst), | 2706 | batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst), |
2429 | sk->sk_state, btohs(pi->psm), pi->scid, pi->dcid, | 2707 | sk->sk_state, btohs(pi->psm), pi->scid, pi->dcid, |
2430 | pi->imtu, pi->omtu, pi->link_mode); | 2708 | pi->imtu, pi->omtu, pi->sec_level); |
2431 | } | 2709 | } |
2432 | 2710 | ||
2433 | read_unlock_bh(&l2cap_sk_list.lock); | 2711 | read_unlock_bh(&l2cap_sk_list.lock); |
@@ -2447,7 +2725,7 @@ static const struct proto_ops l2cap_sock_ops = { | |||
2447 | .accept = l2cap_sock_accept, | 2725 | .accept = l2cap_sock_accept, |
2448 | .getname = l2cap_sock_getname, | 2726 | .getname = l2cap_sock_getname, |
2449 | .sendmsg = l2cap_sock_sendmsg, | 2727 | .sendmsg = l2cap_sock_sendmsg, |
2450 | .recvmsg = bt_sock_recvmsg, | 2728 | .recvmsg = l2cap_sock_recvmsg, |
2451 | .poll = bt_sock_poll, | 2729 | .poll = bt_sock_poll, |
2452 | .ioctl = bt_sock_ioctl, | 2730 | .ioctl = bt_sock_ioctl, |
2453 | .mmap = sock_no_mmap, | 2731 | .mmap = sock_no_mmap, |
@@ -2469,8 +2747,8 @@ static struct hci_proto l2cap_hci_proto = { | |||
2469 | .connect_ind = l2cap_connect_ind, | 2747 | .connect_ind = l2cap_connect_ind, |
2470 | .connect_cfm = l2cap_connect_cfm, | 2748 | .connect_cfm = l2cap_connect_cfm, |
2471 | .disconn_ind = l2cap_disconn_ind, | 2749 | .disconn_ind = l2cap_disconn_ind, |
2472 | .auth_cfm = l2cap_auth_cfm, | 2750 | .disconn_cfm = l2cap_disconn_cfm, |
2473 | .encrypt_cfm = l2cap_encrypt_cfm, | 2751 | .security_cfm = l2cap_security_cfm, |
2474 | .recv_acldata = l2cap_recv_acldata | 2752 | .recv_acldata = l2cap_recv_acldata |
2475 | }; | 2753 | }; |
2476 | 2754 | ||