Commit 5d41ce1dd91bce01d50aff79786dc5d5eedcfab7
1 parent
828d7d7b19
Exists in
master
and in
7 other branches
Bluetooth: Refactor L2CAP channel allocation
If the allocation happens at l2cap_sock_create() will be able to use the struct l2cap_chan to store channel info that comes from the user via setsockopt. Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
Showing 3 changed files with 19 additions and 18 deletions Side-by-side Diff
include/net/bluetooth/l2cap.h
... | ... | @@ -465,8 +465,9 @@ |
465 | 465 | struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, |
466 | 466 | int proto, gfp_t prio); |
467 | 467 | void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err); |
468 | +struct l2cap_chan *l2cap_chan_alloc(struct sock *sk); | |
468 | 469 | void l2cap_chan_del(struct l2cap_chan *chan, int err); |
469 | -int l2cap_do_connect(struct sock *sk); | |
470 | +int l2cap_do_connect(struct l2cap_chan *chan); | |
470 | 471 | |
471 | 472 | #endif /* __L2CAP_H */ |
net/bluetooth/l2cap_core.c
... | ... | @@ -149,7 +149,7 @@ |
149 | 149 | return 0; |
150 | 150 | } |
151 | 151 | |
152 | -static struct l2cap_chan *l2cap_chan_alloc(struct sock *sk) | |
152 | +struct l2cap_chan *l2cap_chan_alloc(struct sock *sk) | |
153 | 153 | { |
154 | 154 | struct l2cap_chan *chan; |
155 | 155 | |
... | ... | @@ -648,6 +648,8 @@ |
648 | 648 | goto clean; |
649 | 649 | } |
650 | 650 | |
651 | + l2cap_pi(sk)->chan = chan; | |
652 | + | |
651 | 653 | write_lock_bh(&conn->chan_lock); |
652 | 654 | |
653 | 655 | hci_conn_hold(conn->hcon); |
... | ... | @@ -661,8 +663,6 @@ |
661 | 663 | |
662 | 664 | __l2cap_chan_add(conn, chan); |
663 | 665 | |
664 | - l2cap_pi(sk)->chan = chan; | |
665 | - | |
666 | 666 | l2cap_sock_set_timer(sk, sk->sk_sndtimeo); |
667 | 667 | |
668 | 668 | sk->sk_state = BT_CONNECTED; |
669 | 669 | |
670 | 670 | |
... | ... | @@ -847,12 +847,12 @@ |
847 | 847 | return node ? sk : sk1; |
848 | 848 | } |
849 | 849 | |
850 | -int l2cap_do_connect(struct sock *sk) | |
850 | +int l2cap_do_connect(struct l2cap_chan *chan) | |
851 | 851 | { |
852 | + struct sock *sk = chan->sk; | |
852 | 853 | bdaddr_t *src = &bt_sk(sk)->src; |
853 | 854 | bdaddr_t *dst = &bt_sk(sk)->dst; |
854 | 855 | struct l2cap_conn *conn; |
855 | - struct l2cap_chan *chan; | |
856 | 856 | struct hci_conn *hcon; |
857 | 857 | struct hci_dev *hdev; |
858 | 858 | __u8 auth_type; |
859 | 859 | |
... | ... | @@ -888,20 +888,11 @@ |
888 | 888 | goto done; |
889 | 889 | } |
890 | 890 | |
891 | - chan = l2cap_chan_alloc(sk); | |
892 | - if (!chan) { | |
893 | - hci_conn_put(hcon); | |
894 | - err = -ENOMEM; | |
895 | - goto done; | |
896 | - } | |
897 | - | |
898 | 891 | /* Update source addr of the socket */ |
899 | 892 | bacpy(src, conn->src); |
900 | 893 | |
901 | 894 | l2cap_chan_add(conn, chan); |
902 | 895 | |
903 | - l2cap_pi(sk)->chan = chan; | |
904 | - | |
905 | 896 | sk->sk_state = BT_CONNECT; |
906 | 897 | l2cap_sock_set_timer(sk, sk->sk_sndtimeo); |
907 | 898 | |
... | ... | @@ -2076,6 +2067,8 @@ |
2076 | 2067 | goto response; |
2077 | 2068 | } |
2078 | 2069 | |
2070 | + l2cap_pi(sk)->chan = chan; | |
2071 | + | |
2079 | 2072 | write_lock_bh(&conn->chan_lock); |
2080 | 2073 | |
2081 | 2074 | /* Check if we already have channel with that dcid */ |
... | ... | @@ -2097,8 +2090,6 @@ |
2097 | 2090 | bt_accept_enqueue(parent, sk); |
2098 | 2091 | |
2099 | 2092 | __l2cap_chan_add(conn, chan); |
2100 | - | |
2101 | - l2cap_pi(sk)->chan = chan; | |
2102 | 2093 | |
2103 | 2094 | dcid = l2cap_pi(sk)->scid; |
2104 | 2095 |
net/bluetooth/l2cap_sock.c
... | ... | @@ -229,7 +229,7 @@ |
229 | 229 | l2cap_pi(sk)->psm = la.l2_psm; |
230 | 230 | l2cap_pi(sk)->dcid = la.l2_cid; |
231 | 231 | |
232 | - err = l2cap_do_connect(sk); | |
232 | + err = l2cap_do_connect(l2cap_pi(sk)->chan); | |
233 | 233 | if (err) |
234 | 234 | goto done; |
235 | 235 | |
... | ... | @@ -1054,6 +1054,7 @@ |
1054 | 1054 | int kern) |
1055 | 1055 | { |
1056 | 1056 | struct sock *sk; |
1057 | + struct l2cap_chan *chan; | |
1057 | 1058 | |
1058 | 1059 | BT_DBG("sock %p", sock); |
1059 | 1060 | |
... | ... | @@ -1071,6 +1072,14 @@ |
1071 | 1072 | sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC); |
1072 | 1073 | if (!sk) |
1073 | 1074 | return -ENOMEM; |
1075 | + | |
1076 | + chan = l2cap_chan_alloc(sk); | |
1077 | + if (!chan) { | |
1078 | + l2cap_sock_kill(sk); | |
1079 | + return -ENOMEM; | |
1080 | + } | |
1081 | + | |
1082 | + l2cap_pi(sk)->chan = chan; | |
1074 | 1083 | |
1075 | 1084 | l2cap_sock_init(sk, NULL); |
1076 | 1085 | return 0; |