31 #include <linux/module.h>
55 static void l2cap_send_disconn_req(
struct l2cap_conn *conn,
92 c = __l2cap_get_chan_by_scid(conn, cid);
105 if (c->
ident == ident)
128 if (psm && __l2cap_global_chan_by_addr(psm, src)) {
141 for (p = 0x1001; p < 0x1100; p += 2)
142 if (!__l2cap_global_chan_by_addr(
cpu_to_le16(p), src)) {
171 if (!__l2cap_get_chan_by_scid(conn, cid))
180 BT_DBG(
"chan %p %s -> %s", chan, state_to_string(chan->
state),
181 state_to_string(state));
184 chan->
ops->state_change(chan, state);
187 static void l2cap_state_change(
struct l2cap_chan *chan,
int state)
192 __l2cap_state_change(chan, state);
196 static inline void __l2cap_chan_set_err(
struct l2cap_chan *chan,
int err)
203 static inline void l2cap_chan_set_err(
struct l2cap_chan *chan,
int err)
208 __l2cap_chan_set_err(chan, err);
212 static void __set_retrans_timer(
struct l2cap_chan *chan)
221 static void __set_monitor_timer(
struct l2cap_chan *chan)
235 skb_queue_walk(head, skb) {
256 size_t alloc_size,
i;
268 seq_list->
mask = alloc_size - 1;
271 for (i = 0; i < alloc_size; i++)
277 static inline void l2cap_seq_list_free(
struct l2cap_seq_list *seq_list)
282 static inline bool l2cap_seq_list_contains(
struct l2cap_seq_list *seq_list,
296 }
else if (seq_list->
head == seq) {
308 while (seq_list->
list[prev & mask] != seq) {
317 if (seq_list->
tail == seq)
326 return l2cap_seq_list_remove(seq_list, seq_list->
head);
336 for (i = 0; i <= seq_list->
mask; i++)
353 seq_list->
head = seq;
357 seq_list->
tail = seq;
368 BT_DBG(
"chan %p state %s", chan, state_to_string(chan->
state));
371 l2cap_chan_lock(chan);
383 l2cap_chan_unlock(chan);
385 chan->
ops->close(chan);
402 list_add(&chan->
global_l, &chan_list);
409 kref_init(&chan->
kref);
419 static void l2cap_chan_destroy(
struct kref *
kref)
443 kref_put(&c->
kref, l2cap_chan_destroy);
460 BT_DBG(
"conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
476 chan->
scid = l2cap_alloc_cid(conn);
517 __l2cap_chan_add(conn, chan);
527 BT_DBG(
"chan %p, conn %p, err %d", chan, conn, err);
538 hci_conn_put(conn->
hcon);
541 if (chan->
ops->teardown)
542 chan->
ops->teardown(chan, err);
576 BT_DBG(
"chan %p state %s sk %p", chan,
577 state_to_string(chan->
state), sk);
579 switch (chan->
state) {
581 if (chan->
ops->teardown)
582 chan->
ops->teardown(chan, 0);
590 l2cap_send_disconn_req(conn, chan, reason);
624 if (chan->
ops->teardown)
625 chan->
ops->teardown(chan, 0);
630 static inline u8 l2cap_get_auth_type(
struct l2cap_chan *chan)
667 auth_type = l2cap_get_auth_type(chan);
682 spin_lock(&conn->
lock);
689 spin_unlock(&conn->
lock);
696 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
699 BT_DBG(
"code 0x%2.2x", code);
720 BT_DBG(
"chan %p, skb %p len %d priority %u", chan, skb, skb->
len,
781 static inline void __unpack_control(
struct l2cap_chan *chan,
786 &
bt_cb(skb)->control);
789 __unpack_enhanced_control(get_unaligned_le16(skb->
data),
790 &
bt_cb(skb)->control);
795 static u32 __pack_extended_control(
struct l2cap_ctrl *control)
814 static u16 __pack_enhanced_control(
struct l2cap_ctrl *control)
833 static inline void __pack_control(
struct l2cap_chan *chan,
841 put_unaligned_le16(__pack_enhanced_control(control),
846 static inline unsigned int __ertm_hdr_size(
struct l2cap_chan *chan)
859 int hlen = __ertm_hdr_size(chan);
887 static void l2cap_send_sframe(
struct l2cap_chan *chan,
893 BT_DBG(
"chan %p, control %p", chan, control);
912 BT_DBG(
"reqseq %d, final %d, poll %d, super %d", control->
reqseq,
916 control_field = __pack_extended_control(control);
918 control_field = __pack_enhanced_control(control);
920 skb = l2cap_create_sframe_pdu(chan, control_field);
922 l2cap_do_send(chan, skb);
925 static void l2cap_send_rr_or_rnr(
struct l2cap_chan *chan,
bool poll)
929 BT_DBG(
"chan %p, poll %d", chan, poll);
931 memset(&control, 0,
sizeof(control));
941 l2cap_send_sframe(chan, &control);
944 static inline int __l2cap_no_conn_pending(
struct l2cap_chan *chan)
949 static void l2cap_send_conn_req(
struct l2cap_chan *chan)
957 chan->
ident = l2cap_get_ident(conn);
964 static void l2cap_chan_ready(
struct l2cap_chan *chan)
972 chan->
ops->ready(chan);
975 static void l2cap_do_start(
struct l2cap_chan *chan)
980 l2cap_chan_ready(chan);
989 __l2cap_no_conn_pending(chan))
990 l2cap_send_conn_req(chan);
1005 static inline int l2cap_mode_supported(
__u8 mode,
__u32 feat_mask)
1007 u32 local_feat_mask = l2cap_feat_mask;
1042 l2cap_send_cmd(conn, l2cap_get_ident(conn),
1047 __l2cap_chan_set_err(chan, err);
1052 static void l2cap_conn_start(
struct l2cap_conn *conn)
1061 struct sock *sk = chan->
sk;
1063 l2cap_chan_lock(chan);
1066 l2cap_chan_unlock(chan);
1072 !__l2cap_no_conn_pending(chan)) {
1073 l2cap_chan_unlock(chan);
1081 l2cap_chan_unlock(chan);
1085 l2cap_send_conn_req(chan);
1096 &
bt_sk(sk)->flags)) {
1119 l2cap_chan_unlock(chan);
1125 l2cap_build_conf_req(chan, buf), buf);
1129 l2cap_chan_unlock(chan);
1138 static struct l2cap_chan *l2cap_global_chan_by_scid(
int state,
u16 cid,
1149 if (state && c->
state != state)
1152 if (c->
scid == cid) {
1153 int src_match, dst_match;
1154 int src_any, dst_any;
1157 src_match = !bacmp(&
bt_sk(sk)->src, src);
1158 dst_match = !bacmp(&
bt_sk(sk)->dst, dst);
1159 if (src_match && dst_match) {
1167 if ((src_match && dst_any) || (src_any && dst_match) ||
1168 (src_any && dst_any))
1178 static void l2cap_le_conn_ready(
struct l2cap_conn *conn)
1180 struct sock *parent, *
sk;
1195 chan = pchan->
ops->new_connection(pchan);
1201 hci_conn_hold(conn->
hcon);
1211 l2cap_chan_ready(chan);
1217 static void l2cap_conn_ready(
struct l2cap_conn *conn)
1225 l2cap_le_conn_ready(conn);
1234 l2cap_chan_lock(chan);
1237 l2cap_chan_unlock(chan);
1243 l2cap_chan_ready(chan);
1246 struct sock *sk = chan->
sk;
1254 l2cap_do_start(chan);
1256 l2cap_chan_unlock(chan);
1263 static void l2cap_conn_unreliable(
struct l2cap_conn *conn,
int err)
1273 __l2cap_chan_set_err(chan, err);
1279 static void l2cap_info_timeout(
struct work_struct *work)
1287 l2cap_conn_start(conn);
1290 static void l2cap_conn_del(
struct hci_conn *hcon,
int err)
1298 BT_DBG(
"hcon %p conn %p, err %d", hcon, conn, err);
1307 l2cap_chan_lock(chan);
1311 l2cap_chan_unlock(chan);
1313 chan->
ops->close(chan);
1333 static void security_timeout(
struct work_struct *work)
1366 conn->
hchan = hchan;
1368 BT_DBG(
"hcon %p conn %p hchan %p", hcon, conn, hchan);
1371 conn->
mtu = hcon->
hdev->le_mtu;
1373 conn->
mtu = hcon->
hdev->acl_mtu;
1375 conn->
src = &hcon->
hdev->bdaddr;
1383 INIT_LIST_HEAD(&conn->
chan_l);
1411 if (state && c->
state != state)
1414 if (c->
psm == psm) {
1415 int src_match, dst_match;
1416 int src_any, dst_any;
1419 src_match = !bacmp(&
bt_sk(sk)->src, src);
1420 dst_match = !bacmp(&
bt_sk(sk)->dst, dst);
1421 if (src_match && dst_match) {
1429 if ((src_match && dst_any) || (src_any && dst_match) ||
1430 (src_any && dst_any))
1441 bdaddr_t *dst,
u8 dst_type)
1443 struct sock *sk = chan->
sk;
1444 bdaddr_t *src = &
bt_sk(sk)->src;
1460 l2cap_chan_lock(chan);
1474 switch (chan->
mode) {
1487 switch (chan->
state) {
1512 bacpy(&
bt_sk(sk)->dst, dst);
1518 auth_type = l2cap_get_auth_type(chan);
1528 err = PTR_ERR(hcon);
1532 conn = l2cap_conn_add(hcon, 0);
1542 if (!list_empty(&conn->
chan_l)) {
1552 bacpy(src, conn->
src);
1554 l2cap_chan_unlock(chan);
1556 l2cap_chan_lock(chan);
1567 l2cap_do_start(chan);
1573 l2cap_chan_unlock(chan);
1592 if (signal_pending(
current)) {
1593 err = sock_intr_errno(timeo);
1602 err = sock_error(sk);
1611 static void l2cap_monitor_timeout(
struct work_struct *work)
1618 l2cap_chan_lock(chan);
1621 l2cap_chan_unlock(chan);
1628 l2cap_chan_unlock(chan);
1632 static void l2cap_retrans_timeout(
struct work_struct *work)
1639 l2cap_chan_lock(chan);
1642 l2cap_chan_unlock(chan);
1648 l2cap_chan_unlock(chan);
1652 static void l2cap_streaming_send(
struct l2cap_chan *chan,
1658 BT_DBG(
"chan %p, skbs %p", chan, skbs);
1660 skb_queue_splice_tail_init(skbs, &chan->
tx_q);
1662 while (!skb_queue_empty(&chan->
tx_q)) {
1666 bt_cb(skb)->control.retries = 1;
1667 control = &
bt_cb(skb)->control;
1672 __pack_control(chan, control, skb);
1679 l2cap_do_send(chan, skb);
1688 static int l2cap_ertm_send(
struct l2cap_chan *chan)
1708 bt_cb(skb)->control.retries = 1;
1709 control = &
bt_cb(skb)->control;
1718 __pack_control(chan, control, skb);
1733 __set_retrans_timer(chan);
1740 if (skb_queue_is_last(&chan->
tx_q, skb))
1745 l2cap_do_send(chan, tx_skb);
1749 BT_DBG(
"Sent %d, %u unacked, %u in ERTM queue", sent,
1755 static void l2cap_ertm_resend(
struct l2cap_chan *chan)
1770 skb = l2cap_ertm_seq_in_queue(&chan->
tx_q, seq);
1772 BT_DBG(
"Error: Can't retransmit seq %d, frame missing",
1777 bt_cb(skb)->control.retries++;
1778 control =
bt_cb(skb)->control;
1794 if (skb_cloned(skb)) {
1813 put_unaligned_le16(__pack_enhanced_control(&control),
1819 put_unaligned_le16(fcs,
skb_put(tx_skb,
1823 l2cap_do_send(chan, tx_skb);
1831 static void l2cap_retransmit(
struct l2cap_chan *chan,
1834 BT_DBG(
"chan %p, control %p", chan, control);
1837 l2cap_ertm_resend(chan);
1840 static void l2cap_retransmit_all(
struct l2cap_chan *chan,
1845 BT_DBG(
"chan %p, control %p", chan, control);
1856 skb_queue_walk(&chan->
tx_q, skb) {
1862 skb_queue_walk_from(&chan->
tx_q, skb) {
1870 l2cap_ertm_resend(chan);
1874 static void l2cap_send_ack(
struct l2cap_chan *chan)
1881 BT_DBG(
"chan %p last_acked_seq %d buffer_seq %d",
1884 memset(&control, 0,
sizeof(control));
1892 l2cap_send_sframe(chan, &control);
1895 l2cap_ertm_send(chan);
1905 threshold += threshold << 1;
1908 BT_DBG(
"frames_to_ack %u, threshold %d", frames_to_ack,
1911 if (frames_to_ack >= threshold) {
1915 l2cap_send_sframe(chan, &control);
1924 static inline int l2cap_skbuff_fromiovec(
struct l2cap_chan *chan,
1939 frag = &skb_shinfo(skb)->frag_list;
1943 count =
min_t(
unsigned int, conn->
mtu, len);
1945 tmp = chan->
ops->alloc_skb(chan, count,
1948 return PTR_ERR(tmp);
1960 skb->
len += (*frag)->len;
1963 frag = &(*frag)->
next;
1970 struct msghdr *msg,
size_t len,
1978 BT_DBG(
"chan %p len %zu priority %u", chan, len, priority);
1980 count =
min_t(
unsigned int, (conn->
mtu - hlen), len);
1982 skb = chan->
ops->alloc_skb(chan, count + hlen,
1995 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
1998 return ERR_PTR(err);
2004 struct msghdr *msg,
size_t len,
2012 BT_DBG(
"chan %p len %zu", chan, len);
2028 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2031 return ERR_PTR(err);
2037 struct msghdr *msg,
size_t len,
2045 BT_DBG(
"chan %p len %zu", chan, len);
2050 hlen = __ertm_hdr_size(chan);
2058 count =
min_t(
unsigned int, (conn->
mtu - hlen), len);
2060 skb = chan->
ops->alloc_skb(chan, count + hlen,
2079 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2082 return ERR_PTR(err);
2085 bt_cb(skb)->control.fcs = chan->
fcs;
2086 bt_cb(skb)->control.retries = 0;
2090 static int l2cap_segment_sdu(
struct l2cap_chan *chan,
2092 struct msghdr *msg,
size_t len)
2099 BT_DBG(
"chan %p, msg %p, len %zu", chan, msg, len);
2107 pdu_len = chan->
conn->mtu;
2115 pdu_len -= __ertm_hdr_size(chan);
2120 if (len <= pdu_len) {
2131 skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len);
2134 __skb_queue_purge(seg_queue);
2135 return PTR_ERR(skb);
2139 __skb_queue_tail(seg_queue, skb);
2147 if (len <= pdu_len) {
2167 skb = l2cap_create_connless_pdu(chan, msg, len, priority);
2169 return PTR_ERR(skb);
2171 l2cap_do_send(chan, skb);
2175 switch (chan->
mode) {
2178 if (len > chan->
omtu)
2182 skb = l2cap_create_basic_pdu(chan, msg, len, priority);
2184 return PTR_ERR(skb);
2186 l2cap_do_send(chan, skb);
2193 if (len > chan->
omtu) {
2198 __skb_queue_head_init(&seg_queue);
2204 err = l2cap_segment_sdu(chan, &seg_queue, msg, len);
2210 __skb_queue_purge(&seg_queue);
2220 l2cap_streaming_send(chan, &seg_queue);
2227 __skb_queue_purge(&seg_queue);
2238 static void l2cap_send_srej(
struct l2cap_chan *chan,
u16 txseq)
2243 BT_DBG(
"chan %p, txseq %u", chan, txseq);
2245 memset(&control, 0,
sizeof(control));
2250 seq = __next_seq(chan, seq)) {
2251 if (!l2cap_ertm_seq_in_queue(&chan->
srej_q, seq)) {
2253 l2cap_send_sframe(chan, &control);
2254 l2cap_seq_list_append(&chan->
srej_list, seq);
2261 static void l2cap_send_srej_tail(
struct l2cap_chan *chan)
2270 memset(&control, 0,
sizeof(control));
2274 l2cap_send_sframe(chan, &control);
2277 static void l2cap_send_srej_list(
struct l2cap_chan *chan,
u16 txseq)
2283 BT_DBG(
"chan %p, txseq %u", chan, txseq);
2285 memset(&control, 0,
sizeof(control));
2293 seq = l2cap_seq_list_pop(&chan->
srej_list);
2298 l2cap_send_sframe(chan, &control);
2299 l2cap_seq_list_append(&chan->
srej_list, seq);
2300 }
while (chan->
srej_list.head != initial_head);
2308 BT_DBG(
"chan %p, reqseq %u", chan, reqseq);
2313 BT_DBG(
"expected_ack_seq %u, unacked_frames %u",
2317 ackseq = __next_seq(chan, ackseq)) {
2319 acked_skb = l2cap_ertm_seq_in_queue(&chan->
tx_q, ackseq);
2335 static void l2cap_abort_rx_srej_sent(
struct l2cap_chan *chan)
2345 static void l2cap_tx_state_xmit(
struct l2cap_chan *chan,
2349 BT_DBG(
"chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2357 skb_queue_splice_tail_init(skbs, &chan->
tx_q);
2358 l2cap_ertm_send(chan);
2361 BT_DBG(
"Enter LOCAL_BUSY");
2368 l2cap_abort_rx_srej_sent(chan);
2371 l2cap_send_ack(chan);
2375 BT_DBG(
"Exit LOCAL_BUSY");
2381 memset(&local_control, 0,
sizeof(local_control));
2382 local_control.sframe = 1;
2384 local_control.poll = 1;
2386 l2cap_send_sframe(chan, &local_control);
2389 __set_monitor_timer(chan);
2394 l2cap_process_reqseq(chan, control->
reqseq);
2397 l2cap_send_rr_or_rnr(chan, 1);
2399 __set_monitor_timer(chan);
2404 l2cap_send_rr_or_rnr(chan, 1);
2406 __set_monitor_timer(chan);
2417 static void l2cap_tx_state_wait_f(
struct l2cap_chan *chan,
2421 BT_DBG(
"chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2429 skb_queue_splice_tail_init(skbs, &chan->
tx_q);
2432 BT_DBG(
"Enter LOCAL_BUSY");
2439 l2cap_abort_rx_srej_sent(chan);
2442 l2cap_send_ack(chan);
2446 BT_DBG(
"Exit LOCAL_BUSY");
2451 memset(&local_control, 0,
sizeof(local_control));
2452 local_control.sframe = 1;
2454 local_control.poll = 1;
2456 l2cap_send_sframe(chan, &local_control);
2459 __set_monitor_timer(chan);
2464 l2cap_process_reqseq(chan, control->
reqseq);
2469 if (control && control->
final) {
2472 __set_retrans_timer(chan);
2483 l2cap_send_rr_or_rnr(chan, 1);
2484 __set_monitor_timer(chan);
2498 BT_DBG(
"chan %p, control %p, skbs %p, event %d, state %d",
2499 chan, control, skbs, event, chan->
tx_state);
2503 l2cap_tx_state_xmit(chan, control, skbs, event);
2506 l2cap_tx_state_wait_f(chan, control, skbs, event);
2514 static void l2cap_pass_to_tx(
struct l2cap_chan *chan,
2517 BT_DBG(
"chan %p, control %p", chan, control);
2521 static void l2cap_pass_to_tx_fbit(
struct l2cap_chan *chan,
2524 BT_DBG(
"chan %p, control %p", chan, control);
2539 struct sock *sk = chan->
sk;
2550 if (chan->
ops->recv(chan, nskb))
2566 BT_DBG(
"conn %p, code 0x%2.2x, ident 0x%2.2x, len %u",
2567 conn, code, ident, dlen);
2570 count =
min_t(
unsigned int, conn->
mtu, len);
2598 frag = &skb_shinfo(skb)->frag_list;
2600 count =
min_t(
unsigned int, conn->
mtu, len);
2611 frag = &(*frag)->
next;
2621 static inline int l2cap_get_conf_opt(
void **
ptr,
int *
type,
int *olen,
unsigned long *
val)
2634 *val = *((
u8 *) opt->
val);
2638 *val = get_unaligned_le16(opt->
val);
2646 *val = (
unsigned long) opt->
val;
2650 BT_DBG(
"type 0x%2.2x len %u val 0x%lx", *type, opt->
len, *val);
2654 static void l2cap_add_conf_opt(
void **ptr,
u8 type,
u8 len,
unsigned long val)
2658 BT_DBG(
"type 0x%2.2x len %u val 0x%lx", type, len, val);
2669 put_unaligned_le16(val, opt->
val);
2684 static void l2cap_add_opt_efs(
void **ptr,
struct l2cap_chan *chan)
2688 switch (chan->
mode) {
2712 (
unsigned long) &efs);
2715 static void l2cap_ack_timeout(
struct work_struct *work)
2723 l2cap_chan_lock(chan);
2725 frames_to_ack = __seq_offset(chan, chan->
buffer_seq,
2729 l2cap_send_rr_or_rnr(chan, 0);
2731 l2cap_chan_unlock(chan);
2750 skb_queue_head_init(&chan->
tx_q);
2762 skb_queue_head_init(&chan->
srej_q);
2775 static inline __u8 l2cap_select_mode(
__u8 mode,
__u16 remote_feat_mask)
2780 if (l2cap_mode_supported(mode, remote_feat_mask))
2788 static inline bool __l2cap_ews_supported(
struct l2cap_chan *chan)
2793 static inline bool __l2cap_efs_supported(
struct l2cap_chan *chan)
2798 static inline void l2cap_txwin_setup(
struct l2cap_chan *chan)
2801 __l2cap_ews_supported(chan)) {
2813 static int l2cap_build_conf_req(
struct l2cap_chan *chan,
void *data)
2817 void *ptr = req->
data;
2825 switch (chan->
mode) {
2831 if (__l2cap_efs_supported(chan))
2836 chan->
mode = l2cap_select_mode(rfc.
mode, chan->
conn->feat_mask);
2844 switch (chan->
mode) {
2858 (
unsigned long) &rfc);
2873 l2cap_txwin_setup(chan);
2879 (
unsigned long) &rfc);
2882 l2cap_add_opt_efs(&ptr, chan);
2899 l2cap_txwin_setup(chan);
2913 (
unsigned long) &rfc);
2916 l2cap_add_opt_efs(&ptr, chan);
2935 static int l2cap_parse_conf_req(
struct l2cap_chan *chan,
void *data)
2938 void *ptr = rsp->
data;
2941 int type, hint, olen;
2953 len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
2971 if (olen ==
sizeof(rfc))
2972 memcpy(&rfc, (
void *) val, olen);
2982 if (olen ==
sizeof(efs))
2983 memcpy(&efs, (
void *) val, olen);
3009 switch (chan->
mode) {
3013 chan->
mode = l2cap_select_mode(rfc.
mode,
3014 chan->
conn->feat_mask);
3019 if (__l2cap_efs_supported(chan))
3040 sizeof(rfc), (
unsigned long) &rfc);
3067 (
unsigned long) &efs);
3105 sizeof(rfc), (
unsigned long) &rfc);
3118 sizeof(efs), (
unsigned long) &efs);
3134 sizeof(rfc), (
unsigned long) &rfc);
3141 memset(&rfc, 0,
sizeof(rfc));
3155 static int l2cap_parse_conf_rsp(
struct l2cap_chan *chan,
void *rsp,
int len,
void *data,
u16 *result)
3158 void *ptr = req->
data;
3164 BT_DBG(
"chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
3167 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3186 if (olen ==
sizeof(rfc))
3187 memcpy(&rfc, (
void *)val, olen);
3196 sizeof(rfc), (
unsigned long) &rfc);
3206 if (olen ==
sizeof(efs))
3207 memcpy(&efs, (
void *)val, olen);
3215 sizeof(efs), (
unsigned long) &efs);
3256 static int l2cap_build_conf_rsp(
struct l2cap_chan *chan,
void *data,
u16 result,
u16 flags)
3259 void *ptr = rsp->
data;
3280 l2cap_send_cmd(conn, chan->
ident,
3287 l2cap_build_conf_req(chan, buf), buf);
3291 static void l2cap_conf_rfc_get(
struct l2cap_chan *chan,
void *rsp,
int len)
3307 BT_DBG(
"chan %p, rsp %p, len %d", chan, rsp, len);
3313 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3317 if (olen ==
sizeof(rfc))
3318 memcpy(&rfc, (
void *)val, olen);
3356 l2cap_conn_start(conn);
3376 pchan = l2cap_global_chan_by_psm(
BT_LISTEN, psm, conn->
src, conn->
dst);
3398 if (__l2cap_get_chan_by_dcid(conn, scid))
3401 chan = pchan->
ops->new_connection(pchan);
3407 hci_conn_hold(conn->
hcon);
3416 __l2cap_chan_add(conn, chan);
3476 l2cap_build_conf_req(chan, buf), buf);
3496 BT_DBG(
"dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x",
3497 dcid, scid, result, status);
3502 chan = __l2cap_get_chan_by_scid(conn, scid);
3508 chan = __l2cap_get_chan_by_ident(conn, cmd->
ident);
3517 l2cap_chan_lock(chan);
3530 l2cap_build_conf_req(chan, req), req);
3543 l2cap_chan_unlock(chan);
3551 static inline void set_default_fcs(
struct l2cap_chan *chan)
3573 BT_DBG(
"dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
3575 chan = l2cap_get_chan_by_scid(conn, dcid);
3592 len = cmd_len -
sizeof(*req);
3593 if (len < 0 || chan->conf_len + len >
sizeof(chan->
conf_req)) {
3595 l2cap_build_conf_rsp(chan, rsp,
3607 l2cap_build_conf_rsp(chan, rsp,
3613 len = l2cap_parse_conf_req(chan, rsp);
3615 l2cap_send_disconn_req(conn, chan,
ECONNRESET);
3629 set_default_fcs(chan);
3636 l2cap_send_disconn_req(chan->
conn, chan, -err);
3638 l2cap_chan_ready(chan);
3646 l2cap_build_conf_req(chan, buf), buf);
3661 l2cap_build_conf_rsp(chan, rsp,
3666 l2cap_chan_unlock(chan);
3682 BT_DBG(
"scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags,
3685 chan = l2cap_get_chan_by_scid(conn, scid);
3691 l2cap_conf_rfc_get(chan, rsp->
data, len);
3701 len = l2cap_parse_conf_rsp(chan, rsp->
data, len,
3704 l2cap_send_disconn_req(conn, chan,
ECONNRESET);
3714 l2cap_build_conf_rsp(chan, buf,
3724 l2cap_send_disconn_req(conn, chan,
ECONNRESET);
3730 len = l2cap_parse_conf_rsp(chan, rsp->
data, len,
3733 l2cap_send_disconn_req(conn, chan,
ECONNRESET);
3737 l2cap_send_cmd(conn, l2cap_get_ident(conn),
3749 l2cap_send_disconn_req(conn, chan,
ECONNRESET);
3753 if (flags & L2CAP_CONF_FLAG_CONTINUATION)
3759 set_default_fcs(chan);
3766 l2cap_send_disconn_req(chan->
conn, chan, -err);
3768 l2cap_chan_ready(chan);
3772 l2cap_chan_unlock(chan);
3787 BT_DBG(
"scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
3791 chan = __l2cap_get_chan_by_scid(conn, dcid);
3797 l2cap_chan_lock(chan);
3812 l2cap_chan_unlock(chan);
3814 chan->
ops->close(chan);
3831 BT_DBG(
"dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
3835 chan = __l2cap_get_chan_by_scid(conn, scid);
3841 l2cap_chan_lock(chan);
3846 l2cap_chan_unlock(chan);
3848 chan->
ops->close(chan);
3863 BT_DBG(
"type 0x%4.4x", type);
3867 u32 feat_mask = l2cap_feat_mask;
3879 l2cap_send_cmd(conn, cmd->
ident,
3892 memcpy(rsp->
data, l2cap_fixed_chan,
sizeof(l2cap_fixed_chan));
3893 l2cap_send_cmd(conn, cmd->
ident,
3899 l2cap_send_cmd(conn, cmd->
ident,
3914 BT_DBG(
"type 0x%4.4x result 0x%2.2x", type, result);
3927 l2cap_conn_start(conn);
3948 l2cap_conn_start(conn);
3957 l2cap_conn_start(conn);
3964 static inline int l2cap_create_channel_req(
struct l2cap_conn *conn,
3972 if (cmd_len !=
sizeof(*req))
3981 BT_DBG(
"psm 0x%2.2x, scid 0x%4.4x, amp_id %d", psm, scid, req->
amp_id);
3995 static inline int l2cap_create_channel_rsp(
struct l2cap_conn *conn,
4000 return l2cap_connect_rsp(conn, cmd, data);
4003 static void l2cap_send_move_chan_rsp(
struct l2cap_conn *conn,
u8 ident,
4008 BT_DBG(
"icid 0x%4.4x, result 0x%4.4x", icid, result);
4016 static void l2cap_send_move_chan_cfm(
struct l2cap_conn *conn,
4023 BT_DBG(
"icid 0x%4.4x, result 0x%4.4x", icid, result);
4025 ident = l2cap_get_ident(conn);
4035 static void l2cap_send_move_chan_cfm_rsp(
struct l2cap_conn *conn,
u8 ident,
4040 BT_DBG(
"icid 0x%4.4x", icid);
4046 static inline int l2cap_move_channel_req(
struct l2cap_conn *conn,
4048 u16 cmd_len,
void *data)
4054 if (cmd_len !=
sizeof(*req))
4065 l2cap_send_move_chan_rsp(conn, cmd->
ident, icid, result);
4070 static inline int l2cap_move_channel_rsp(
struct l2cap_conn *conn,
4072 u16 cmd_len,
void *data)
4077 if (cmd_len !=
sizeof(*rsp))
4083 BT_DBG(
"icid 0x%4.4x, result 0x%4.4x", icid, result);
4091 static inline int l2cap_move_channel_confirm(
struct l2cap_conn *conn,
4093 u16 cmd_len,
void *data)
4098 if (cmd_len !=
sizeof(*cfm))
4104 BT_DBG(
"icid 0x%4.4x, result 0x%4.4x", icid, result);
4106 l2cap_send_move_chan_cfm_rsp(conn, cmd->
ident, icid);
4111 static inline int l2cap_move_channel_confirm_rsp(
struct l2cap_conn *conn,
4113 u16 cmd_len,
void *data)
4118 if (cmd_len !=
sizeof(*rsp))
4123 BT_DBG(
"icid 0x%4.4x", icid);
4133 if (min > max || min < 6 || max > 3200)
4136 if (to_multiplier < 10 || to_multiplier > 3200)
4139 if (max >= to_multiplier * 8)
4142 max_latency = (to_multiplier * 8 /
max) - 1;
4143 if (latency > 499 || latency > max_latency)
4171 BT_DBG(
"min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
4172 min, max, latency, to_multiplier);
4174 memset(&rsp, 0,
sizeof(rsp));
4176 err = l2cap_check_conn_param(min, max, latency, to_multiplier);
4191 static inline int l2cap_bredr_sig_cmd(
struct l2cap_conn *conn,
4196 switch (cmd->
code) {
4198 l2cap_command_rej(conn, cmd, data);
4202 err = l2cap_connect_req(conn, cmd, data);
4206 err = l2cap_connect_rsp(conn, cmd, data);
4210 err = l2cap_config_req(conn, cmd, cmd_len, data);
4214 err = l2cap_config_rsp(conn, cmd, data);
4218 err = l2cap_disconnect_req(conn, cmd, data);
4222 err = l2cap_disconnect_rsp(conn, cmd, data);
4233 err = l2cap_information_req(conn, cmd, data);
4237 err = l2cap_information_rsp(conn, cmd, data);
4241 err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
4245 err = l2cap_create_channel_rsp(conn, cmd, data);
4249 err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
4253 err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
4257 err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
4261 err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
4265 BT_ERR(
"Unknown BR/EDR signaling command 0x%2.2x", cmd->
code);
4273 static inline int l2cap_le_sig_cmd(
struct l2cap_conn *conn,
4276 switch (cmd->
code) {
4287 BT_ERR(
"Unknown LE signaling command 0x%2.2x", cmd->
code);
4292 static inline void l2cap_sig_channel(
struct l2cap_conn *conn,
4300 l2cap_raw_recv(conn, skb);
4302 while (len >= L2CAP_CMD_HDR_SIZE) {
4304 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
4310 BT_DBG(
"code 0x%2.2x len %d id 0x%2.2x", cmd.
code, cmd_len, cmd.
ident);
4312 if (cmd_len > len || !cmd.
ident) {
4313 BT_DBG(
"corrupted command");
4318 err = l2cap_le_sig_cmd(conn, &cmd, data);
4320 err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
4325 BT_ERR(
"Wrong link type (%d)", err);
4341 u16 our_fcs, rcv_fcs;
4351 rcv_fcs = get_unaligned_le16(skb->
data + skb->
len);
4352 our_fcs =
crc16(0, skb->
data - hdr_size, skb->
len + hdr_size);
4354 if (our_fcs != rcv_fcs)
4360 static void l2cap_send_i_or_rr_or_rnr(
struct l2cap_chan *chan)
4366 memset(&control, 0,
sizeof(control));
4374 l2cap_send_sframe(chan, &control);
4379 __set_retrans_timer(chan);
4382 l2cap_ertm_send(chan);
4390 l2cap_send_sframe(chan, &control);
4394 static void append_skb_frag(
struct sk_buff *skb,
4400 if (!skb_has_frag_list(skb))
4401 skb_shinfo(skb)->frag_list = new_frag;
4405 (*last_frag)->next = new_frag;
4406 *last_frag = new_frag;
4408 skb->
len += new_frag->
len;
4418 switch (control->
sar) {
4423 err = chan->
ops->recv(chan, skb);
4452 append_skb_frag(chan->
sdu, skb,
4466 append_skb_frag(chan->
sdu, skb,
4473 err = chan->
ops->recv(chan, chan->
sdu);
4506 static int l2cap_rx_queued_iframes(
struct l2cap_chan *chan)
4517 BT_DBG(
"Searching for skb with txseq %d (queue len %d)",
4527 err = l2cap_reassemble_sdu(chan, skb, &
bt_cb(skb)->control);
4532 if (skb_queue_empty(&chan->
srej_q)) {
4534 l2cap_send_ack(chan);
4540 static void l2cap_handle_srej(
struct l2cap_chan *chan,
4545 BT_DBG(
"chan %p, control %p", chan, control);
4548 BT_DBG(
"Invalid reqseq %d, disconnecting", control->
reqseq);
4553 skb = l2cap_ertm_seq_in_queue(&chan->
tx_q, control->
reqseq);
4556 BT_DBG(
"Seq %d not available for retransmission",
4569 if (control->
poll) {
4570 l2cap_pass_to_tx(chan, control);
4573 l2cap_retransmit(chan, control);
4574 l2cap_ertm_send(chan);
4581 l2cap_pass_to_tx_fbit(chan, control);
4583 if (control->
final) {
4587 l2cap_retransmit(chan, control);
4589 l2cap_retransmit(chan, control);
4598 static void l2cap_handle_rej(
struct l2cap_chan *chan,
4603 BT_DBG(
"chan %p, control %p", chan, control);
4606 BT_DBG(
"Invalid reqseq %d, disconnecting", control->
reqseq);
4611 skb = l2cap_ertm_seq_in_queue(&chan->
tx_q, control->
reqseq);
4613 if (chan->
max_tx && skb &&
4622 l2cap_pass_to_tx(chan, control);
4624 if (control->
final) {
4626 l2cap_retransmit_all(chan, control);
4628 l2cap_retransmit_all(chan, control);
4629 l2cap_ertm_send(chan);
4637 BT_DBG(
"chan %p, txseq %d", chan, txseq);
4649 BT_DBG(
"Invalid/Ignore - after SREJ");
4652 BT_DBG(
"Invalid - in window after SREJ sent");
4662 if (l2cap_ertm_seq_in_queue(&chan->
srej_q, txseq)) {
4663 BT_DBG(
"Duplicate SREJ - txseq already stored");
4667 if (l2cap_seq_list_contains(&chan->
srej_list, txseq)) {
4668 BT_DBG(
"Unexpected SREJ - not requested");
4676 BT_DBG(
"Invalid - txseq outside tx window");
4687 BT_DBG(
"Duplicate - expected_tx_seq later than txseq");
4710 BT_DBG(
"Invalid/Ignore - txseq outside tx window");
4713 BT_DBG(
"Invalid - txseq outside tx window");
4717 BT_DBG(
"Unexpected - txseq indicates missing frames");
4722 static int l2cap_rx_state_recv(
struct l2cap_chan *chan,
4727 bool skb_in_use = 0;
4729 BT_DBG(
"chan %p, control %p, skb %p, event %d", chan, control, skb,
4734 switch (l2cap_classify_txseq(chan, control->
txseq)) {
4736 l2cap_pass_to_tx(chan, control);
4739 BT_DBG(
"Busy, discarding expected seq %d",
4750 err = l2cap_reassemble_sdu(chan, skb, control);
4754 if (control->
final) {
4758 l2cap_retransmit_all(chan, control);
4759 l2cap_ertm_send(chan);
4764 l2cap_send_ack(chan);
4767 l2cap_pass_to_tx(chan, control);
4774 BT_DBG(
"Busy, discarding unexpected seq %d",
4785 BT_DBG(
"Queued %p (queue len %d)", skb,
4786 skb_queue_len(&chan->
srej_q));
4790 l2cap_send_srej(chan, control->
txseq);
4795 l2cap_pass_to_tx(chan, control);
4801 l2cap_send_disconn_req(chan->
conn, chan,
4807 l2cap_pass_to_tx(chan, control);
4808 if (control->
final) {
4814 l2cap_retransmit_all(chan, control);
4817 l2cap_ertm_send(chan);
4818 }
else if (control->
poll) {
4819 l2cap_send_i_or_rr_or_rnr(chan);
4824 __set_retrans_timer(chan);
4826 l2cap_ertm_send(chan);
4831 l2cap_pass_to_tx(chan, control);
4832 if (control && control->
poll) {
4834 l2cap_send_rr_or_rnr(chan, 0);
4840 l2cap_handle_rej(chan, control);
4843 l2cap_handle_srej(chan, control);
4849 if (skb && !skb_in_use) {
4850 BT_DBG(
"Freeing %p", skb);
4857 static int l2cap_rx_state_srej_sent(
struct l2cap_chan *chan,
4863 bool skb_in_use = 0;
4865 BT_DBG(
"chan %p, control %p, skb %p, event %d", chan, control, skb,
4870 switch (l2cap_classify_txseq(chan, txseq)) {
4873 l2cap_pass_to_tx(chan, control);
4876 BT_DBG(
"Queued %p (queue len %d)", skb,
4877 skb_queue_len(&chan->
srej_q));
4884 l2cap_pass_to_tx(chan, control);
4887 BT_DBG(
"Queued %p (queue len %d)", skb,
4888 skb_queue_len(&chan->
srej_q));
4890 err = l2cap_rx_queued_iframes(chan);
4902 BT_DBG(
"Queued %p (queue len %d)", skb,
4903 skb_queue_len(&chan->
srej_q));
4905 l2cap_pass_to_tx(chan, control);
4906 l2cap_send_srej(chan, control->
txseq);
4916 BT_DBG(
"Queued %p (queue len %d)", skb,
4917 skb_queue_len(&chan->
srej_q));
4919 l2cap_pass_to_tx(chan, control);
4920 l2cap_send_srej_list(chan, control->
txseq);
4924 l2cap_pass_to_tx(chan, control);
4935 l2cap_send_disconn_req(chan->
conn, chan,
4941 l2cap_pass_to_tx(chan, control);
4942 if (control->
final) {
4948 l2cap_retransmit_all(chan, control);
4951 l2cap_ertm_send(chan);
4952 }
else if (control->
poll) {
4956 __set_retrans_timer(chan);
4960 l2cap_send_srej_tail(chan);
4965 __set_retrans_timer(chan);
4967 l2cap_send_ack(chan);
4972 l2cap_pass_to_tx(chan, control);
4973 if (control->
poll) {
4974 l2cap_send_srej_tail(chan);
4977 memset(&rr_control, 0,
sizeof(rr_control));
4978 rr_control.sframe = 1;
4981 l2cap_send_sframe(chan, &rr_control);
4986 l2cap_handle_rej(chan, control);
4989 l2cap_handle_srej(chan, control);
4993 if (skb && !skb_in_use) {
4994 BT_DBG(
"Freeing %p", skb);
5001 static bool __valid_reqseq(
struct l2cap_chan *chan,
u16 reqseq)
5007 return __seq_offset(chan, chan->
next_tx_seq, reqseq) <= unacked;
5015 BT_DBG(
"chan %p, control %p, skb %p, event %d, state %d", chan,
5016 control, skb, event, chan->
rx_state);
5018 if (__valid_reqseq(chan, control->
reqseq)) {
5021 err = l2cap_rx_state_recv(chan, control, skb, event);
5024 err = l2cap_rx_state_srej_sent(chan, control, skb,
5032 BT_DBG(
"Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d",
5046 BT_DBG(
"chan %p, control %p, skb %p, state %d", chan, control, skb,
5049 if (l2cap_classify_txseq(chan, control->
txseq) ==
5051 l2cap_pass_to_tx(chan, control);
5058 l2cap_reassemble_sdu(chan, skb, control);
5068 BT_DBG(
"Freeing %p", skb);
5085 __unpack_control(chan, skb);
5094 if (l2cap_check_fcs(chan, skb))
5103 if (len > chan->
mps) {
5111 BT_DBG(
"iframe sar %d, reqseq %d, final %d, txseq %d",
5123 err = l2cap_rx(chan, control, skb, event);
5125 err = l2cap_stream_rx(chan, control, skb);
5129 l2cap_send_disconn_req(chan->
conn, chan,
5132 const u8 rx_func_to_event[4] = {
5141 BT_DBG(
"sframe reqseq %d, final %d, poll %d, super %d",
5156 event = rx_func_to_event[control->
super];
5157 if (l2cap_rx(chan, control, skb, event))
5168 static void l2cap_data_channel(
struct l2cap_conn *conn,
u16 cid,
5173 chan = l2cap_get_chan_by_scid(conn, cid);
5182 l2cap_chan_lock(chan);
5184 BT_DBG(
"unknown cid 0x%4.4x", cid);
5191 BT_DBG(
"chan %p, len %d", chan, skb->
len);
5196 switch (chan->
mode) {
5206 if (!chan->
ops->recv(chan, skb))
5212 l2cap_data_rcv(chan, skb);
5216 BT_DBG(
"chan %p: bad mode 0x%2.2x", chan, chan->
mode);
5224 l2cap_chan_unlock(chan);
5232 chan = l2cap_global_chan_by_psm(0, psm, conn->
src, conn->
dst);
5236 BT_DBG(
"chan %p, len %d", chan, skb->
len);
5244 if (!chan->
ops->recv(chan, skb))
5251 static void l2cap_att_channel(
struct l2cap_conn *conn,
u16 cid,
5256 chan = l2cap_global_chan_by_scid(0, cid, conn->
src, conn->
dst);
5260 BT_DBG(
"chan %p, len %d", chan, skb->
len);
5268 if (!chan->
ops->recv(chan, skb))
5285 if (len != skb->
len) {
5290 BT_DBG(
"len %d, cid 0x%4.4x", len, cid);
5295 l2cap_sig_channel(conn, skb);
5301 l2cap_conless_channel(conn, psm, skb);
5305 l2cap_att_channel(conn, cid, skb);
5314 l2cap_data_channel(conn, cid, skb);
5323 int exact = 0, lm1 = 0, lm2 = 0;
5349 return exact ? lm1 : lm2;
5359 conn = l2cap_conn_add(hcon, status);
5361 l2cap_conn_ready(conn);
5380 BT_DBG(
"hcon %p reason %d", hcon, reason);
5390 if (encrypt == 0x00) {
5409 BT_DBG(
"conn %p status 0x%2.2x encrypt %u", conn, status, encrypt);
5412 if (!status && encrypt)
5420 l2cap_chan_lock(chan);
5422 BT_DBG(
"chan %p scid 0x%4.4x state %s", chan, chan->
scid,
5423 state_to_string(chan->
state));
5426 l2cap_chan_unlock(chan);
5431 if (!status && encrypt) {
5433 l2cap_chan_ready(chan);
5436 l2cap_chan_unlock(chan);
5441 l2cap_chan_unlock(chan);
5447 struct sock *sk = chan->
sk;
5452 l2cap_check_encryption(chan, encrypt);
5453 l2cap_chan_unlock(chan);
5459 l2cap_send_conn_req(chan);
5464 struct sock *sk = chan->
sk;
5472 &
bt_sk(sk)->flags)) {
5473 struct sock *parent =
bt_sk(sk)->parent;
5503 l2cap_send_cmd(conn, l2cap_get_ident(conn),
5505 l2cap_build_conf_req(chan, buf),
5511 l2cap_chan_unlock(chan);
5524 conn = l2cap_conn_add(hcon, 0);
5529 BT_DBG(
"conn %p len %d flags 0x%x", conn, skb->
len, flags);
5536 BT_ERR(
"Unexpected start frame (len %d)", skb->
len);
5540 l2cap_conn_unreliable(conn,
ECOMM);
5545 BT_ERR(
"Frame is too short (len %d)", skb->
len);
5546 l2cap_conn_unreliable(conn,
ECOMM);
5553 if (len == skb->
len) {
5555 l2cap_recv_frame(conn, skb);
5559 BT_DBG(
"Start: total len %d, frag len %d", len, skb->
len);
5561 if (skb->
len > len) {
5562 BT_ERR(
"Frame is too long (len %d, expected len %d)",
5564 l2cap_conn_unreliable(conn,
ECOMM);
5580 BT_ERR(
"Unexpected continuation frame (len %d)", skb->
len);
5581 l2cap_conn_unreliable(conn,
ECOMM);
5586 BT_ERR(
"Fragment is too long (len %d, expected %d)",
5591 l2cap_conn_unreliable(conn,
ECOMM);
5601 l2cap_recv_frame(conn, conn->
rx_skb);
5611 static int l2cap_debugfs_show(
struct seq_file *
f,
void *
p)
5620 seq_printf(f,
"%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
5639 .open = l2cap_debugfs_open,
5645 static struct dentry *l2cap_debugfs;
5659 BT_ERR(
"Failed to create L2CAP debug file");