forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 072de836f53be56a70cecf70b43ae43b7ce17376
kernel/net/bluetooth/l2cap_core.c
....@@ -45,14 +45,12 @@
4545 #define LE_FLOWCTL_MAX_CREDITS 65535
4646
4747 bool disable_ertm;
48
+bool enable_ecred;
4849
4950 static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD;
5051
5152 static LIST_HEAD(chan_list);
5253 static DEFINE_RWLOCK(chan_list_lock);
53
-
54
-static u16 le_max_credits = L2CAP_LE_MAX_CREDITS;
55
-static u16 le_default_mps = L2CAP_LE_DEFAULT_MPS;
5654
5755 static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
5856 u8 code, u8 ident, u16 dlen, void *data);
....@@ -63,6 +61,9 @@
6361
6462 static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6563 struct sk_buff_head *skbs, u8 event);
64
+static void l2cap_retrans_timeout(struct work_struct *work);
65
+static void l2cap_monitor_timeout(struct work_struct *work);
66
+static void l2cap_ack_timeout(struct work_struct *work);
6667
6768 static inline u8 bdaddr_type(u8 link_type, u8 bdaddr_type)
6869 {
....@@ -113,7 +114,8 @@
113114 }
114115
115116 /* Find channel with given SCID.
116
- * Returns locked channel. */
117
+ * Returns a reference locked channel.
118
+ */
117119 static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn,
118120 u16 cid)
119121 {
....@@ -121,15 +123,19 @@
121123
122124 mutex_lock(&conn->chan_lock);
123125 c = __l2cap_get_chan_by_scid(conn, cid);
124
- if (c)
125
- l2cap_chan_lock(c);
126
+ if (c) {
127
+ /* Only lock if chan reference is not 0 */
128
+ c = l2cap_chan_hold_unless_zero(c);
129
+ if (c)
130
+ l2cap_chan_lock(c);
131
+ }
126132 mutex_unlock(&conn->chan_lock);
127133
128134 return c;
129135 }
130136
131137 /* Find channel with given DCID.
132
- * Returns locked channel.
138
+ * Returns a reference locked channel.
133139 */
134140 static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
135141 u16 cid)
....@@ -138,8 +144,12 @@
138144
139145 mutex_lock(&conn->chan_lock);
140146 c = __l2cap_get_chan_by_dcid(conn, cid);
141
- if (c)
142
- l2cap_chan_lock(c);
147
+ if (c) {
148
+ /* Only lock if chan reference is not 0 */
149
+ c = l2cap_chan_hold_unless_zero(c);
150
+ if (c)
151
+ l2cap_chan_lock(c);
152
+ }
143153 mutex_unlock(&conn->chan_lock);
144154
145155 return c;
....@@ -164,18 +174,29 @@
164174
165175 mutex_lock(&conn->chan_lock);
166176 c = __l2cap_get_chan_by_ident(conn, ident);
167
- if (c)
168
- l2cap_chan_lock(c);
177
+ if (c) {
178
+ /* Only lock if chan reference is not 0 */
179
+ c = l2cap_chan_hold_unless_zero(c);
180
+ if (c)
181
+ l2cap_chan_lock(c);
182
+ }
169183 mutex_unlock(&conn->chan_lock);
170184
171185 return c;
172186 }
173187
174
-static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
188
+static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src,
189
+ u8 src_type)
175190 {
176191 struct l2cap_chan *c;
177192
178193 list_for_each_entry(c, &chan_list, global_l) {
194
+ if (src_type == BDADDR_BREDR && c->src_type != BDADDR_BREDR)
195
+ continue;
196
+
197
+ if (src_type != BDADDR_BREDR && c->src_type == BDADDR_BREDR)
198
+ continue;
199
+
179200 if (c->sport == psm && !bacmp(&c->src, src))
180201 return c;
181202 }
....@@ -188,7 +209,7 @@
188209
189210 write_lock(&chan_list_lock);
190211
191
- if (psm && __l2cap_global_chan_by_addr(psm, src)) {
212
+ if (psm && __l2cap_global_chan_by_addr(psm, src, chan->src_type)) {
192213 err = -EADDRINUSE;
193214 goto done;
194215 }
....@@ -212,7 +233,8 @@
212233
213234 err = -EINVAL;
214235 for (p = start; p <= end; p += incr)
215
- if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
236
+ if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src,
237
+ chan->src_type)) {
216238 chan->psm = cpu_to_le16(p);
217239 chan->sport = cpu_to_le16(p);
218240 err = 0;
....@@ -457,6 +479,9 @@
457479 write_unlock(&chan_list_lock);
458480
459481 INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout);
482
+ INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout);
483
+ INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout);
484
+ INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout);
460485
461486 chan->state = BT_OPEN;
462487
....@@ -491,6 +516,16 @@
491516 kref_get(&c->kref);
492517 }
493518
519
+struct l2cap_chan *l2cap_chan_hold_unless_zero(struct l2cap_chan *c)
520
+{
521
+ BT_DBG("chan %p orig refcnt %u", c, kref_read(&c->kref));
522
+
523
+ if (!kref_get_unless_zero(&c->kref))
524
+ return NULL;
525
+
526
+ return c;
527
+}
528
+
494529 void l2cap_chan_put(struct l2cap_chan *c)
495530 {
496531 BT_DBG("chan %p orig refcnt %d", c, kref_read(&c->kref));
....@@ -520,16 +555,29 @@
520555 }
521556 EXPORT_SYMBOL_GPL(l2cap_chan_set_defaults);
522557
523
-static void l2cap_le_flowctl_init(struct l2cap_chan *chan)
558
+static void l2cap_le_flowctl_init(struct l2cap_chan *chan, u16 tx_credits)
524559 {
525560 chan->sdu = NULL;
526561 chan->sdu_last_frag = NULL;
527562 chan->sdu_len = 0;
528
- chan->tx_credits = 0;
529
- chan->rx_credits = le_max_credits;
530
- chan->mps = min_t(u16, chan->imtu, le_default_mps);
563
+ chan->tx_credits = tx_credits;
564
+ /* Derive MPS from connection MTU to stop HCI fragmentation */
565
+ chan->mps = min_t(u16, chan->imtu, chan->conn->mtu - L2CAP_HDR_SIZE);
566
+ /* Give enough credits for a full packet */
567
+ chan->rx_credits = (chan->imtu / chan->mps) + 1;
531568
532569 skb_queue_head_init(&chan->tx_q);
570
+}
571
+
572
+static void l2cap_ecred_init(struct l2cap_chan *chan, u16 tx_credits)
573
+{
574
+ l2cap_le_flowctl_init(chan, tx_credits);
575
+
576
+ /* L2CAP implementations shall support a minimum MPS of 64 octets */
577
+ if (chan->mps < L2CAP_ECRED_MIN_MPS) {
578
+ chan->mps = L2CAP_ECRED_MIN_MPS;
579
+ chan->rx_credits = (chan->imtu / chan->mps) + 1;
580
+ }
533581 }
534582
535583 void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
....@@ -638,6 +686,7 @@
638686 break;
639687
640688 case L2CAP_MODE_LE_FLOWCTL:
689
+ case L2CAP_MODE_EXT_FLOWCTL:
641690 skb_queue_purge(&chan->tx_q);
642691 break;
643692
....@@ -650,8 +699,7 @@
650699
651700 l2cap_seq_list_free(&chan->srej_list);
652701 l2cap_seq_list_free(&chan->retrans_list);
653
-
654
- /* fall through */
702
+ fallthrough;
655703
656704 case L2CAP_MODE_STREAMING:
657705 skb_queue_purge(&chan->tx_q);
....@@ -661,6 +709,29 @@
661709 return;
662710 }
663711 EXPORT_SYMBOL_GPL(l2cap_chan_del);
712
+
713
+static void __l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func,
714
+ void *data)
715
+{
716
+ struct l2cap_chan *chan;
717
+
718
+ list_for_each_entry(chan, &conn->chan_l, list) {
719
+ func(chan, data);
720
+ }
721
+}
722
+
723
+void l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func,
724
+ void *data)
725
+{
726
+ if (!conn)
727
+ return;
728
+
729
+ mutex_lock(&conn->chan_lock);
730
+ __l2cap_chan_list(conn, func, data);
731
+ mutex_unlock(&conn->chan_lock);
732
+}
733
+
734
+EXPORT_SYMBOL_GPL(l2cap_chan_list);
664735
665736 static void l2cap_conn_update_id_addr(struct work_struct *work)
666737 {
....@@ -688,9 +759,9 @@
688759 u16 result;
689760
690761 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
691
- result = L2CAP_CR_AUTHORIZATION;
762
+ result = L2CAP_CR_LE_AUTHORIZATION;
692763 else
693
- result = L2CAP_CR_BAD_PSM;
764
+ result = L2CAP_CR_LE_BAD_PSM;
694765
695766 l2cap_state_change(chan, BT_DISCONN);
696767
....@@ -698,6 +769,27 @@
698769 rsp.mtu = cpu_to_le16(chan->imtu);
699770 rsp.mps = cpu_to_le16(chan->mps);
700771 rsp.credits = cpu_to_le16(chan->rx_credits);
772
+ rsp.result = cpu_to_le16(result);
773
+
774
+ l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
775
+ &rsp);
776
+}
777
+
778
+static void l2cap_chan_ecred_connect_reject(struct l2cap_chan *chan)
779
+{
780
+ struct l2cap_conn *conn = chan->conn;
781
+ struct l2cap_ecred_conn_rsp rsp;
782
+ u16 result;
783
+
784
+ if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
785
+ result = L2CAP_CR_LE_AUTHORIZATION;
786
+ else
787
+ result = L2CAP_CR_LE_BAD_PSM;
788
+
789
+ l2cap_state_change(chan, BT_DISCONN);
790
+
791
+ memset(&rsp, 0, sizeof(rsp));
792
+
701793 rsp.result = cpu_to_le16(result);
702794
703795 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
....@@ -749,8 +841,16 @@
749841 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
750842 if (conn->hcon->type == ACL_LINK)
751843 l2cap_chan_connect_reject(chan);
752
- else if (conn->hcon->type == LE_LINK)
753
- l2cap_chan_le_connect_reject(chan);
844
+ else if (conn->hcon->type == LE_LINK) {
845
+ switch (chan->mode) {
846
+ case L2CAP_MODE_LE_FLOWCTL:
847
+ l2cap_chan_le_connect_reject(chan);
848
+ break;
849
+ case L2CAP_MODE_EXT_FLOWCTL:
850
+ l2cap_chan_ecred_connect_reject(chan);
851
+ break;
852
+ }
853
+ }
754854 }
755855
756856 l2cap_chan_del(chan, reason);
....@@ -804,7 +904,8 @@
804904 else
805905 return HCI_AT_NO_BONDING;
806906 }
807
- /* fall through */
907
+ fallthrough;
908
+
808909 default:
809910 switch (chan->sec_level) {
810911 case BT_SECURITY_HIGH:
....@@ -1273,8 +1374,13 @@
12731374 chan->conf_state = 0;
12741375 __clear_chan_timer(chan);
12751376
1276
- if (chan->mode == L2CAP_MODE_LE_FLOWCTL && !chan->tx_credits)
1277
- chan->ops->suspend(chan);
1377
+ switch (chan->mode) {
1378
+ case L2CAP_MODE_LE_FLOWCTL:
1379
+ case L2CAP_MODE_EXT_FLOWCTL:
1380
+ if (!chan->tx_credits)
1381
+ chan->ops->suspend(chan);
1382
+ break;
1383
+ }
12781384
12791385 chan->state = BT_CONNECTED;
12801386
....@@ -1289,6 +1395,11 @@
12891395 if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags))
12901396 return;
12911397
1398
+ if (!chan->imtu)
1399
+ chan->imtu = chan->conn->mtu;
1400
+
1401
+ l2cap_le_flowctl_init(chan, 0);
1402
+
12921403 req.psm = chan->psm;
12931404 req.scid = cpu_to_le16(chan->scid);
12941405 req.mtu = cpu_to_le16(chan->imtu);
....@@ -1299,6 +1410,82 @@
12991410
13001411 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ,
13011412 sizeof(req), &req);
1413
+}
1414
+
1415
+struct l2cap_ecred_conn_data {
1416
+ struct {
1417
+ struct l2cap_ecred_conn_req req;
1418
+ __le16 scid[5];
1419
+ } __packed pdu;
1420
+ struct l2cap_chan *chan;
1421
+ struct pid *pid;
1422
+ int count;
1423
+};
1424
+
1425
+static void l2cap_ecred_defer_connect(struct l2cap_chan *chan, void *data)
1426
+{
1427
+ struct l2cap_ecred_conn_data *conn = data;
1428
+ struct pid *pid;
1429
+
1430
+ if (chan == conn->chan)
1431
+ return;
1432
+
1433
+ if (!test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags))
1434
+ return;
1435
+
1436
+ pid = chan->ops->get_peer_pid(chan);
1437
+
1438
+ /* Only add deferred channels with the same PID/PSM */
1439
+ if (conn->pid != pid || chan->psm != conn->chan->psm || chan->ident ||
1440
+ chan->mode != L2CAP_MODE_EXT_FLOWCTL || chan->state != BT_CONNECT)
1441
+ return;
1442
+
1443
+ if (test_and_set_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags))
1444
+ return;
1445
+
1446
+ l2cap_ecred_init(chan, 0);
1447
+
1448
+ /* Set the same ident so we can match on the rsp */
1449
+ chan->ident = conn->chan->ident;
1450
+
1451
+ /* Include all channels deferred */
1452
+ conn->pdu.scid[conn->count] = cpu_to_le16(chan->scid);
1453
+
1454
+ conn->count++;
1455
+}
1456
+
1457
+static void l2cap_ecred_connect(struct l2cap_chan *chan)
1458
+{
1459
+ struct l2cap_conn *conn = chan->conn;
1460
+ struct l2cap_ecred_conn_data data;
1461
+
1462
+ if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
1463
+ return;
1464
+
1465
+ if (test_and_set_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags))
1466
+ return;
1467
+
1468
+ l2cap_ecred_init(chan, 0);
1469
+
1470
+ memset(&data, 0, sizeof(data));
1471
+ data.pdu.req.psm = chan->psm;
1472
+ data.pdu.req.mtu = cpu_to_le16(chan->imtu);
1473
+ data.pdu.req.mps = cpu_to_le16(chan->mps);
1474
+ data.pdu.req.credits = cpu_to_le16(chan->rx_credits);
1475
+ data.pdu.scid[0] = cpu_to_le16(chan->scid);
1476
+
1477
+ chan->ident = l2cap_get_ident(conn);
1478
+ data.pid = chan->ops->get_peer_pid(chan);
1479
+
1480
+ data.count = 1;
1481
+ data.chan = chan;
1482
+ data.pid = chan->ops->get_peer_pid(chan);
1483
+
1484
+ __l2cap_chan_list(conn, l2cap_ecred_defer_connect, &data);
1485
+
1486
+ l2cap_send_cmd(conn, chan->ident, L2CAP_ECRED_CONN_REQ,
1487
+ sizeof(data.pdu.req) + data.count * sizeof(__le16),
1488
+ &data.pdu);
13021489 }
13031490
13041491 static void l2cap_le_start(struct l2cap_chan *chan)
....@@ -1313,8 +1500,12 @@
13131500 return;
13141501 }
13151502
1316
- if (chan->state == BT_CONNECT)
1317
- l2cap_le_connect(chan);
1503
+ if (chan->state == BT_CONNECT) {
1504
+ if (chan->mode == L2CAP_MODE_EXT_FLOWCTL)
1505
+ l2cap_ecred_connect(chan);
1506
+ else
1507
+ l2cap_le_connect(chan);
1508
+ }
13181509 }
13191510
13201511 static void l2cap_start_connection(struct l2cap_chan *chan)
....@@ -1359,7 +1550,7 @@
13591550 * actually encrypted before enforcing a key size.
13601551 */
13611552 return (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags) ||
1362
- hcon->enc_key_size >= HCI_MIN_ENC_KEY_SIZE);
1553
+ hcon->enc_key_size >= hcon->hdev->min_enc_key_size);
13631554 }
13641555
13651556 static void l2cap_do_start(struct l2cap_chan *chan)
....@@ -1526,8 +1717,8 @@
15261717 if (hcon->out)
15271718 smp_conn_security(hcon, hcon->pending_sec_level);
15281719
1529
- /* For LE slave connections, make sure the connection interval
1530
- * is in the range of the minium and maximum interval that has
1720
+ /* For LE peripheral connections, make sure the connection interval
1721
+ * is in the range of the minimum and maximum interval that has
15311722 * been configured for this connection. If not, then trigger
15321723 * the connection update procedure.
15331724 */
....@@ -1781,11 +1972,11 @@
17811972 bdaddr_t *dst,
17821973 u8 link_type)
17831974 {
1784
- struct l2cap_chan *c, *c1 = NULL;
1975
+ struct l2cap_chan *c, *tmp, *c1 = NULL;
17851976
17861977 read_lock(&chan_list_lock);
17871978
1788
- list_for_each_entry(c, &chan_list, global_l) {
1979
+ list_for_each_entry_safe(c, tmp, &chan_list, global_l) {
17891980 if (state && c->state != state)
17901981 continue;
17911982
....@@ -1795,7 +1986,7 @@
17951986 if (link_type == LE_LINK && c->src_type == BDADDR_BREDR)
17961987 continue;
17971988
1798
- if (c->psm == psm) {
1989
+ if (c->chan_type != L2CAP_CHAN_FIXED && c->psm == psm) {
17991990 int src_match, dst_match;
18001991 int src_any, dst_any;
18011992
....@@ -1803,7 +1994,9 @@
18031994 src_match = !bacmp(&c->src, src);
18041995 dst_match = !bacmp(&c->dst, dst);
18051996 if (src_match && dst_match) {
1806
- l2cap_chan_hold(c);
1997
+ if (!l2cap_chan_hold_unless_zero(c))
1998
+ continue;
1999
+
18072000 read_unlock(&chan_list_lock);
18082001 return c;
18092002 }
....@@ -1818,7 +2011,7 @@
18182011 }
18192012
18202013 if (c1)
1821
- l2cap_chan_hold(c1);
2014
+ c1 = l2cap_chan_hold_unless_zero(c1);
18222015
18232016 read_unlock(&chan_list_lock);
18242017
....@@ -2500,6 +2693,7 @@
25002693
25012694 switch (chan->mode) {
25022695 case L2CAP_MODE_LE_FLOWCTL:
2696
+ case L2CAP_MODE_EXT_FLOWCTL:
25032697 /* Check outgoing MTU */
25042698 if (len > chan->omtu)
25052699 return -EMSGSIZE;
....@@ -2825,8 +3019,7 @@
28253019 break;
28263020 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
28273021 l2cap_process_reqseq(chan, control->reqseq);
2828
-
2829
- /* Fall through */
3022
+ fallthrough;
28303023
28313024 case L2CAP_EV_RECV_FBIT:
28323025 if (control && control->final) {
....@@ -3129,10 +3322,6 @@
31293322 chan->rx_state = L2CAP_RX_STATE_RECV;
31303323 chan->tx_state = L2CAP_TX_STATE_XMIT;
31313324
3132
- INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout);
3133
- INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout);
3134
- INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout);
3135
-
31363325 skb_queue_head_init(&chan->srej_q);
31373326
31383327 err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win);
....@@ -3153,7 +3342,7 @@
31533342 case L2CAP_MODE_ERTM:
31543343 if (l2cap_mode_supported(mode, remote_feat_mask))
31553344 return mode;
3156
- /* fall through */
3345
+ fallthrough;
31573346 default:
31583347 return L2CAP_MODE_BASIC;
31593348 }
....@@ -3224,6 +3413,49 @@
32243413 chan->ack_win = chan->tx_win;
32253414 }
32263415
3416
+static void l2cap_mtu_auto(struct l2cap_chan *chan)
3417
+{
3418
+ struct hci_conn *conn = chan->conn->hcon;
3419
+
3420
+ chan->imtu = L2CAP_DEFAULT_MIN_MTU;
3421
+
3422
+ /* The 2-DH1 packet has between 2 and 56 information bytes
3423
+ * (including the 2-byte payload header)
3424
+ */
3425
+ if (!(conn->pkt_type & HCI_2DH1))
3426
+ chan->imtu = 54;
3427
+
3428
+ /* The 3-DH1 packet has between 2 and 85 information bytes
3429
+ * (including the 2-byte payload header)
3430
+ */
3431
+ if (!(conn->pkt_type & HCI_3DH1))
3432
+ chan->imtu = 83;
3433
+
3434
+ /* The 2-DH3 packet has between 2 and 369 information bytes
3435
+ * (including the 2-byte payload header)
3436
+ */
3437
+ if (!(conn->pkt_type & HCI_2DH3))
3438
+ chan->imtu = 367;
3439
+
3440
+ /* The 3-DH3 packet has between 2 and 554 information bytes
3441
+ * (including the 2-byte payload header)
3442
+ */
3443
+ if (!(conn->pkt_type & HCI_3DH3))
3444
+ chan->imtu = 552;
3445
+
3446
+ /* The 2-DH5 packet has between 2 and 681 information bytes
3447
+ * (including the 2-byte payload header)
3448
+ */
3449
+ if (!(conn->pkt_type & HCI_2DH5))
3450
+ chan->imtu = 679;
3451
+
3452
+ /* The 3-DH5 packet has between 2 and 1023 information bytes
3453
+ * (including the 2-byte payload header)
3454
+ */
3455
+ if (!(conn->pkt_type & HCI_3DH5))
3456
+ chan->imtu = 1021;
3457
+}
3458
+
32273459 static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data, size_t data_size)
32283460 {
32293461 struct l2cap_conf_req *req = data;
....@@ -3246,15 +3478,19 @@
32463478 if (__l2cap_efs_supported(chan->conn))
32473479 set_bit(FLAG_EFS_ENABLE, &chan->flags);
32483480
3249
- /* fall through */
3481
+ fallthrough;
32503482 default:
32513483 chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
32523484 break;
32533485 }
32543486
32553487 done:
3256
- if (chan->imtu != L2CAP_DEFAULT_MTU)
3257
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu, endptr - ptr);
3488
+ if (chan->imtu != L2CAP_DEFAULT_MTU) {
3489
+ if (!chan->imtu)
3490
+ l2cap_mtu_auto(chan);
3491
+ l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu,
3492
+ endptr - ptr);
3493
+ }
32583494
32593495 switch (chan->mode) {
32603496 case L2CAP_MODE_BASIC:
....@@ -3524,7 +3760,8 @@
35243760 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
35253761 sizeof(rfc), (unsigned long) &rfc, endptr - ptr);
35263762
3527
- if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3763
+ if (remote_efs &&
3764
+ test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
35283765 chan->remote_id = efs.id;
35293766 chan->remote_stype = efs.stype;
35303767 chan->remote_msdu = le16_to_cpu(efs.msdu);
....@@ -3715,10 +3952,49 @@
37153952 rsp.mtu = cpu_to_le16(chan->imtu);
37163953 rsp.mps = cpu_to_le16(chan->mps);
37173954 rsp.credits = cpu_to_le16(chan->rx_credits);
3718
- rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
3955
+ rsp.result = cpu_to_le16(L2CAP_CR_LE_SUCCESS);
37193956
37203957 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
37213958 &rsp);
3959
+}
3960
+
3961
+void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan)
3962
+{
3963
+ struct {
3964
+ struct l2cap_ecred_conn_rsp rsp;
3965
+ __le16 dcid[5];
3966
+ } __packed pdu;
3967
+ struct l2cap_conn *conn = chan->conn;
3968
+ u16 ident = chan->ident;
3969
+ int i = 0;
3970
+
3971
+ if (!ident)
3972
+ return;
3973
+
3974
+ BT_DBG("chan %p ident %d", chan, ident);
3975
+
3976
+ pdu.rsp.mtu = cpu_to_le16(chan->imtu);
3977
+ pdu.rsp.mps = cpu_to_le16(chan->mps);
3978
+ pdu.rsp.credits = cpu_to_le16(chan->rx_credits);
3979
+ pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_SUCCESS);
3980
+
3981
+ mutex_lock(&conn->chan_lock);
3982
+
3983
+ list_for_each_entry(chan, &conn->chan_l, list) {
3984
+ if (chan->ident != ident)
3985
+ continue;
3986
+
3987
+ /* Reset ident so only one response is sent */
3988
+ chan->ident = 0;
3989
+
3990
+ /* Include all channels pending with the same ident */
3991
+ pdu.dcid[i++] = cpu_to_le16(chan->scid);
3992
+ }
3993
+
3994
+ mutex_unlock(&conn->chan_lock);
3995
+
3996
+ l2cap_send_cmd(conn, ident, L2CAP_ECRED_CONN_RSP,
3997
+ sizeof(pdu.rsp) + i * sizeof(__le16), &pdu);
37223998 }
37233999
37244000 void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
....@@ -3866,9 +4142,17 @@
38664142
38674143 result = L2CAP_CR_NO_MEM;
38684144
3869
- /* Check if we already have channel with that dcid */
3870
- if (__l2cap_get_chan_by_dcid(conn, scid))
4145
+ /* Check for valid dynamic CID range (as per Erratum 3253) */
4146
+ if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_DYN_END) {
4147
+ result = L2CAP_CR_INVALID_SCID;
38714148 goto response;
4149
+ }
4150
+
4151
+ /* Check if we already have channel with that dcid */
4152
+ if (__l2cap_get_chan_by_dcid(conn, scid)) {
4153
+ result = L2CAP_CR_SCID_IN_USE;
4154
+ goto response;
4155
+ }
38724156
38734157 chan = pchan->ops->new_connection(pchan);
38744158 if (!chan)
....@@ -4022,6 +4306,12 @@
40224306 }
40234307 }
40244308
4309
+ chan = l2cap_chan_hold_unless_zero(chan);
4310
+ if (!chan) {
4311
+ err = -EBADSLT;
4312
+ goto unlock;
4313
+ }
4314
+
40254315 err = 0;
40264316
40274317 l2cap_chan_lock(chan);
....@@ -4051,6 +4341,7 @@
40514341 }
40524342
40534343 l2cap_chan_unlock(chan);
4344
+ l2cap_chan_put(chan);
40544345
40554346 unlock:
40564347 mutex_unlock(&conn->chan_lock);
....@@ -4158,7 +4449,8 @@
41584449
41594450 chan->ident = cmd->ident;
41604451 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
4161
- chan->num_conf_rsp++;
4452
+ if (chan->num_conf_rsp < L2CAP_CONF_MAX_CONF_RSP)
4453
+ chan->num_conf_rsp++;
41624454
41634455 /* Reset config buffer. */
41644456 chan->conf_len = 0;
....@@ -4204,6 +4496,7 @@
42044496
42054497 unlock:
42064498 l2cap_chan_unlock(chan);
4499
+ l2cap_chan_put(chan);
42074500 return err;
42084501 }
42094502
....@@ -4287,6 +4580,7 @@
42874580 goto done;
42884581 break;
42894582 }
4583
+ fallthrough;
42904584
42914585 default:
42924586 l2cap_chan_set_err(chan, ECONNRESET);
....@@ -4316,6 +4610,7 @@
43164610
43174611 done:
43184612 l2cap_chan_unlock(chan);
4613
+ l2cap_chan_put(chan);
43194614 return err;
43204615 }
43214616
....@@ -5022,7 +5317,6 @@
50225317 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
50235318 l2cap_move_setup(chan);
50245319 chan->move_id = req->dest_amp_id;
5025
- icid = chan->dcid;
50265320
50275321 if (req->dest_amp_id == AMP_ID_BREDR) {
50285322 /* Moving to BR/EDR */
....@@ -5044,6 +5338,7 @@
50445338 l2cap_send_move_chan_rsp(chan, result);
50455339
50465340 l2cap_chan_unlock(chan);
5341
+ l2cap_chan_put(chan);
50475342
50485343 return 0;
50495344 }
....@@ -5136,6 +5431,7 @@
51365431 }
51375432
51385433 l2cap_chan_unlock(chan);
5434
+ l2cap_chan_put(chan);
51395435 }
51405436
51415437 static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid,
....@@ -5165,6 +5461,7 @@
51655461 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
51665462
51675463 l2cap_chan_unlock(chan);
5464
+ l2cap_chan_put(chan);
51685465 }
51695466
51705467 static int l2cap_move_channel_rsp(struct l2cap_conn *conn,
....@@ -5228,6 +5525,7 @@
52285525 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
52295526
52305527 l2cap_chan_unlock(chan);
5528
+ l2cap_chan_put(chan);
52315529
52325530 return 0;
52335531 }
....@@ -5263,6 +5561,7 @@
52635561 }
52645562
52655563 l2cap_chan_unlock(chan);
5564
+ l2cap_chan_put(chan);
52665565
52675566 return 0;
52685567 }
....@@ -5336,7 +5635,7 @@
53365635 credits = __le16_to_cpu(rsp->credits);
53375636 result = __le16_to_cpu(rsp->result);
53385637
5339
- if (result == L2CAP_CR_SUCCESS && (mtu < 23 || mps < 23 ||
5638
+ if (result == L2CAP_CR_LE_SUCCESS && (mtu < 23 || mps < 23 ||
53405639 dcid < L2CAP_CID_DYN_START ||
53415640 dcid > L2CAP_CID_LE_DYN_END))
53425641 return -EPROTO;
....@@ -5357,7 +5656,7 @@
53575656 l2cap_chan_lock(chan);
53585657
53595658 switch (result) {
5360
- case L2CAP_CR_SUCCESS:
5659
+ case L2CAP_CR_LE_SUCCESS:
53615660 if (__l2cap_get_chan_by_dcid(conn, dcid)) {
53625661 err = -EBADSLT;
53635662 break;
....@@ -5371,8 +5670,8 @@
53715670 l2cap_chan_ready(chan);
53725671 break;
53735672
5374
- case L2CAP_CR_AUTHENTICATION:
5375
- case L2CAP_CR_ENCRYPTION:
5673
+ case L2CAP_CR_LE_AUTHENTICATION:
5674
+ case L2CAP_CR_LE_ENCRYPTION:
53765675 /* If we already have MITM protection we can't do
53775676 * anything.
53785677 */
....@@ -5511,11 +5810,24 @@
55115810 BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm),
55125811 scid, mtu, mps);
55135812
5813
+ /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 3, Part A
5814
+ * page 1059:
5815
+ *
5816
+ * Valid range: 0x0001-0x00ff
5817
+ *
5818
+ * Table 4.15: L2CAP_LE_CREDIT_BASED_CONNECTION_REQ SPSM ranges
5819
+ */
5820
+ if (!psm || __le16_to_cpu(psm) > L2CAP_PSM_LE_DYN_END) {
5821
+ result = L2CAP_CR_LE_BAD_PSM;
5822
+ chan = NULL;
5823
+ goto response;
5824
+ }
5825
+
55145826 /* Check if we have socket listening on psm */
55155827 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
55165828 &conn->hcon->dst, LE_LINK);
55175829 if (!pchan) {
5518
- result = L2CAP_CR_BAD_PSM;
5830
+ result = L2CAP_CR_LE_BAD_PSM;
55195831 chan = NULL;
55205832 goto response;
55215833 }
....@@ -5525,32 +5837,30 @@
55255837
55265838 if (!smp_sufficient_security(conn->hcon, pchan->sec_level,
55275839 SMP_ALLOW_STK)) {
5528
- result = L2CAP_CR_AUTHENTICATION;
5840
+ result = L2CAP_CR_LE_AUTHENTICATION;
55295841 chan = NULL;
55305842 goto response_unlock;
55315843 }
55325844
55335845 /* Check for valid dynamic CID range */
55345846 if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_LE_DYN_END) {
5535
- result = L2CAP_CR_INVALID_SCID;
5847
+ result = L2CAP_CR_LE_INVALID_SCID;
55365848 chan = NULL;
55375849 goto response_unlock;
55385850 }
55395851
55405852 /* Check if we already have channel with that dcid */
55415853 if (__l2cap_get_chan_by_dcid(conn, scid)) {
5542
- result = L2CAP_CR_SCID_IN_USE;
5854
+ result = L2CAP_CR_LE_SCID_IN_USE;
55435855 chan = NULL;
55445856 goto response_unlock;
55455857 }
55465858
55475859 chan = pchan->ops->new_connection(pchan);
55485860 if (!chan) {
5549
- result = L2CAP_CR_NO_MEM;
5861
+ result = L2CAP_CR_LE_NO_MEM;
55505862 goto response_unlock;
55515863 }
5552
-
5553
- l2cap_le_flowctl_init(chan);
55545864
55555865 bacpy(&chan->src, &conn->hcon->src);
55565866 bacpy(&chan->dst, &conn->hcon->dst);
....@@ -5560,9 +5870,11 @@
55605870 chan->dcid = scid;
55615871 chan->omtu = mtu;
55625872 chan->remote_mps = mps;
5563
- chan->tx_credits = __le16_to_cpu(req->credits);
55645873
55655874 __l2cap_chan_add(conn, chan);
5875
+
5876
+ l2cap_le_flowctl_init(chan, __le16_to_cpu(req->credits));
5877
+
55665878 dcid = chan->scid;
55675879 credits = chan->rx_credits;
55685880
....@@ -5581,7 +5893,7 @@
55815893 chan->ops->defer(chan);
55825894 } else {
55835895 l2cap_chan_ready(chan);
5584
- result = L2CAP_CR_SUCCESS;
5896
+ result = L2CAP_CR_LE_SUCCESS;
55855897 }
55865898
55875899 response_unlock:
....@@ -5635,12 +5947,11 @@
56355947 if (credits > max_credits) {
56365948 BT_ERR("LE credits overflow");
56375949 l2cap_send_disconn_req(chan, ECONNRESET);
5638
- l2cap_chan_unlock(chan);
56395950
56405951 /* Return 0 so that we don't trigger an unnecessary
56415952 * command reject packet.
56425953 */
5643
- return 0;
5954
+ goto unlock;
56445955 }
56455956
56465957 chan->tx_credits += credits;
....@@ -5651,7 +5962,371 @@
56515962 if (chan->tx_credits)
56525963 chan->ops->resume(chan);
56535964
5965
+unlock:
56545966 l2cap_chan_unlock(chan);
5967
+ l2cap_chan_put(chan);
5968
+
5969
+ return 0;
5970
+}
5971
+
5972
+static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,
5973
+ struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5974
+ u8 *data)
5975
+{
5976
+ struct l2cap_ecred_conn_req *req = (void *) data;
5977
+ struct {
5978
+ struct l2cap_ecred_conn_rsp rsp;
5979
+ __le16 dcid[5];
5980
+ } __packed pdu;
5981
+ struct l2cap_chan *chan, *pchan;
5982
+ u16 mtu, mps;
5983
+ __le16 psm;
5984
+ u8 result, len = 0;
5985
+ int i, num_scid;
5986
+ bool defer = false;
5987
+
5988
+ if (!enable_ecred)
5989
+ return -EINVAL;
5990
+
5991
+ if (cmd_len < sizeof(*req) || (cmd_len - sizeof(*req)) % sizeof(u16)) {
5992
+ result = L2CAP_CR_LE_INVALID_PARAMS;
5993
+ goto response;
5994
+ }
5995
+
5996
+ mtu = __le16_to_cpu(req->mtu);
5997
+ mps = __le16_to_cpu(req->mps);
5998
+
5999
+ if (mtu < L2CAP_ECRED_MIN_MTU || mps < L2CAP_ECRED_MIN_MPS) {
6000
+ result = L2CAP_CR_LE_UNACCEPT_PARAMS;
6001
+ goto response;
6002
+ }
6003
+
6004
+ psm = req->psm;
6005
+
6006
+ /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 3, Part A
6007
+ * page 1059:
6008
+ *
6009
+ * Valid range: 0x0001-0x00ff
6010
+ *
6011
+ * Table 4.15: L2CAP_LE_CREDIT_BASED_CONNECTION_REQ SPSM ranges
6012
+ */
6013
+ if (!psm || __le16_to_cpu(psm) > L2CAP_PSM_LE_DYN_END) {
6014
+ result = L2CAP_CR_LE_BAD_PSM;
6015
+ goto response;
6016
+ }
6017
+
6018
+ BT_DBG("psm 0x%2.2x mtu %u mps %u", __le16_to_cpu(psm), mtu, mps);
6019
+
6020
+ memset(&pdu, 0, sizeof(pdu));
6021
+
6022
+ /* Check if we have socket listening on psm */
6023
+ pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
6024
+ &conn->hcon->dst, LE_LINK);
6025
+ if (!pchan) {
6026
+ result = L2CAP_CR_LE_BAD_PSM;
6027
+ goto response;
6028
+ }
6029
+
6030
+ mutex_lock(&conn->chan_lock);
6031
+ l2cap_chan_lock(pchan);
6032
+
6033
+ if (!smp_sufficient_security(conn->hcon, pchan->sec_level,
6034
+ SMP_ALLOW_STK)) {
6035
+ result = L2CAP_CR_LE_AUTHENTICATION;
6036
+ goto unlock;
6037
+ }
6038
+
6039
+ result = L2CAP_CR_LE_SUCCESS;
6040
+ cmd_len -= sizeof(*req);
6041
+ num_scid = cmd_len / sizeof(u16);
6042
+
6043
+ for (i = 0; i < num_scid; i++) {
6044
+ u16 scid = __le16_to_cpu(req->scid[i]);
6045
+
6046
+ BT_DBG("scid[%d] 0x%4.4x", i, scid);
6047
+
6048
+ pdu.dcid[i] = 0x0000;
6049
+ len += sizeof(*pdu.dcid);
6050
+
6051
+ /* Check for valid dynamic CID range */
6052
+ if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_LE_DYN_END) {
6053
+ result = L2CAP_CR_LE_INVALID_SCID;
6054
+ continue;
6055
+ }
6056
+
6057
+ /* Check if we already have channel with that dcid */
6058
+ if (__l2cap_get_chan_by_dcid(conn, scid)) {
6059
+ result = L2CAP_CR_LE_SCID_IN_USE;
6060
+ continue;
6061
+ }
6062
+
6063
+ chan = pchan->ops->new_connection(pchan);
6064
+ if (!chan) {
6065
+ result = L2CAP_CR_LE_NO_MEM;
6066
+ continue;
6067
+ }
6068
+
6069
+ bacpy(&chan->src, &conn->hcon->src);
6070
+ bacpy(&chan->dst, &conn->hcon->dst);
6071
+ chan->src_type = bdaddr_src_type(conn->hcon);
6072
+ chan->dst_type = bdaddr_dst_type(conn->hcon);
6073
+ chan->psm = psm;
6074
+ chan->dcid = scid;
6075
+ chan->omtu = mtu;
6076
+ chan->remote_mps = mps;
6077
+
6078
+ __l2cap_chan_add(conn, chan);
6079
+
6080
+ l2cap_ecred_init(chan, __le16_to_cpu(req->credits));
6081
+
6082
+ /* Init response */
6083
+ if (!pdu.rsp.credits) {
6084
+ pdu.rsp.mtu = cpu_to_le16(chan->imtu);
6085
+ pdu.rsp.mps = cpu_to_le16(chan->mps);
6086
+ pdu.rsp.credits = cpu_to_le16(chan->rx_credits);
6087
+ }
6088
+
6089
+ pdu.dcid[i] = cpu_to_le16(chan->scid);
6090
+
6091
+ __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
6092
+
6093
+ chan->ident = cmd->ident;
6094
+
6095
+ if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
6096
+ l2cap_state_change(chan, BT_CONNECT2);
6097
+ defer = true;
6098
+ chan->ops->defer(chan);
6099
+ } else {
6100
+ l2cap_chan_ready(chan);
6101
+ }
6102
+ }
6103
+
6104
+unlock:
6105
+ l2cap_chan_unlock(pchan);
6106
+ mutex_unlock(&conn->chan_lock);
6107
+ l2cap_chan_put(pchan);
6108
+
6109
+response:
6110
+ pdu.rsp.result = cpu_to_le16(result);
6111
+
6112
+ if (defer)
6113
+ return 0;
6114
+
6115
+ l2cap_send_cmd(conn, cmd->ident, L2CAP_ECRED_CONN_RSP,
6116
+ sizeof(pdu.rsp) + len, &pdu);
6117
+
6118
+ return 0;
6119
+}
6120
+
6121
+static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn,
6122
+ struct l2cap_cmd_hdr *cmd, u16 cmd_len,
6123
+ u8 *data)
6124
+{
6125
+ struct l2cap_ecred_conn_rsp *rsp = (void *) data;
6126
+ struct hci_conn *hcon = conn->hcon;
6127
+ u16 mtu, mps, credits, result;
6128
+ struct l2cap_chan *chan, *tmp;
6129
+ int err = 0, sec_level;
6130
+ int i = 0;
6131
+
6132
+ if (cmd_len < sizeof(*rsp))
6133
+ return -EPROTO;
6134
+
6135
+ mtu = __le16_to_cpu(rsp->mtu);
6136
+ mps = __le16_to_cpu(rsp->mps);
6137
+ credits = __le16_to_cpu(rsp->credits);
6138
+ result = __le16_to_cpu(rsp->result);
6139
+
6140
+ BT_DBG("mtu %u mps %u credits %u result 0x%4.4x", mtu, mps, credits,
6141
+ result);
6142
+
6143
+ mutex_lock(&conn->chan_lock);
6144
+
6145
+ cmd_len -= sizeof(*rsp);
6146
+
6147
+ list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
6148
+ u16 dcid;
6149
+
6150
+ if (chan->ident != cmd->ident ||
6151
+ chan->mode != L2CAP_MODE_EXT_FLOWCTL ||
6152
+ chan->state == BT_CONNECTED)
6153
+ continue;
6154
+
6155
+ l2cap_chan_lock(chan);
6156
+
6157
+ /* Check that there is a dcid for each pending channel */
6158
+ if (cmd_len < sizeof(dcid)) {
6159
+ l2cap_chan_del(chan, ECONNREFUSED);
6160
+ l2cap_chan_unlock(chan);
6161
+ continue;
6162
+ }
6163
+
6164
+ dcid = __le16_to_cpu(rsp->dcid[i++]);
6165
+ cmd_len -= sizeof(u16);
6166
+
6167
+ BT_DBG("dcid[%d] 0x%4.4x", i, dcid);
6168
+
6169
+ /* Check if dcid is already in use */
6170
+ if (dcid && __l2cap_get_chan_by_dcid(conn, dcid)) {
6171
+ /* If a device receives a
6172
+ * L2CAP_CREDIT_BASED_CONNECTION_RSP packet with an
6173
+ * already-assigned Destination CID, then both the
6174
+ * original channel and the new channel shall be
6175
+ * immediately discarded and not used.
6176
+ */
6177
+ l2cap_chan_del(chan, ECONNREFUSED);
6178
+ l2cap_chan_unlock(chan);
6179
+ chan = __l2cap_get_chan_by_dcid(conn, dcid);
6180
+ l2cap_chan_lock(chan);
6181
+ l2cap_chan_del(chan, ECONNRESET);
6182
+ l2cap_chan_unlock(chan);
6183
+ continue;
6184
+ }
6185
+
6186
+ switch (result) {
6187
+ case L2CAP_CR_LE_AUTHENTICATION:
6188
+ case L2CAP_CR_LE_ENCRYPTION:
6189
+ /* If we already have MITM protection we can't do
6190
+ * anything.
6191
+ */
6192
+ if (hcon->sec_level > BT_SECURITY_MEDIUM) {
6193
+ l2cap_chan_del(chan, ECONNREFUSED);
6194
+ break;
6195
+ }
6196
+
6197
+ sec_level = hcon->sec_level + 1;
6198
+ if (chan->sec_level < sec_level)
6199
+ chan->sec_level = sec_level;
6200
+
6201
+ /* We'll need to send a new Connect Request */
6202
+ clear_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags);
6203
+
6204
+ smp_conn_security(hcon, chan->sec_level);
6205
+ break;
6206
+
6207
+ case L2CAP_CR_LE_BAD_PSM:
6208
+ l2cap_chan_del(chan, ECONNREFUSED);
6209
+ break;
6210
+
6211
+ default:
6212
+ /* If dcid was not set it means channels was refused */
6213
+ if (!dcid) {
6214
+ l2cap_chan_del(chan, ECONNREFUSED);
6215
+ break;
6216
+ }
6217
+
6218
+ chan->ident = 0;
6219
+ chan->dcid = dcid;
6220
+ chan->omtu = mtu;
6221
+ chan->remote_mps = mps;
6222
+ chan->tx_credits = credits;
6223
+ l2cap_chan_ready(chan);
6224
+ break;
6225
+ }
6226
+
6227
+ l2cap_chan_unlock(chan);
6228
+ }
6229
+
6230
+ mutex_unlock(&conn->chan_lock);
6231
+
6232
+ return err;
6233
+}
6234
+
6235
+static inline int l2cap_ecred_reconf_req(struct l2cap_conn *conn,
6236
+ struct l2cap_cmd_hdr *cmd, u16 cmd_len,
6237
+ u8 *data)
6238
+{
6239
+ struct l2cap_ecred_reconf_req *req = (void *) data;
6240
+ struct l2cap_ecred_reconf_rsp rsp;
6241
+ u16 mtu, mps, result;
6242
+ struct l2cap_chan *chan;
6243
+ int i, num_scid;
6244
+
6245
+ if (!enable_ecred)
6246
+ return -EINVAL;
6247
+
6248
+ if (cmd_len < sizeof(*req) || cmd_len - sizeof(*req) % sizeof(u16)) {
6249
+ result = L2CAP_CR_LE_INVALID_PARAMS;
6250
+ goto respond;
6251
+ }
6252
+
6253
+ mtu = __le16_to_cpu(req->mtu);
6254
+ mps = __le16_to_cpu(req->mps);
6255
+
6256
+ BT_DBG("mtu %u mps %u", mtu, mps);
6257
+
6258
+ if (mtu < L2CAP_ECRED_MIN_MTU) {
6259
+ result = L2CAP_RECONF_INVALID_MTU;
6260
+ goto respond;
6261
+ }
6262
+
6263
+ if (mps < L2CAP_ECRED_MIN_MPS) {
6264
+ result = L2CAP_RECONF_INVALID_MPS;
6265
+ goto respond;
6266
+ }
6267
+
6268
+ cmd_len -= sizeof(*req);
6269
+ num_scid = cmd_len / sizeof(u16);
6270
+ result = L2CAP_RECONF_SUCCESS;
6271
+
6272
+ for (i = 0; i < num_scid; i++) {
6273
+ u16 scid;
6274
+
6275
+ scid = __le16_to_cpu(req->scid[i]);
6276
+ if (!scid)
6277
+ return -EPROTO;
6278
+
6279
+ chan = __l2cap_get_chan_by_dcid(conn, scid);
6280
+ if (!chan)
6281
+ continue;
6282
+
6283
+ /* If the MTU value is decreased for any of the included
6284
+ * channels, then the receiver shall disconnect all
6285
+ * included channels.
6286
+ */
6287
+ if (chan->omtu > mtu) {
6288
+ BT_ERR("chan %p decreased MTU %u -> %u", chan,
6289
+ chan->omtu, mtu);
6290
+ result = L2CAP_RECONF_INVALID_MTU;
6291
+ }
6292
+
6293
+ chan->omtu = mtu;
6294
+ chan->remote_mps = mps;
6295
+ }
6296
+
6297
+respond:
6298
+ rsp.result = cpu_to_le16(result);
6299
+
6300
+ l2cap_send_cmd(conn, cmd->ident, L2CAP_ECRED_RECONF_RSP, sizeof(rsp),
6301
+ &rsp);
6302
+
6303
+ return 0;
6304
+}
6305
+
6306
+static inline int l2cap_ecred_reconf_rsp(struct l2cap_conn *conn,
6307
+ struct l2cap_cmd_hdr *cmd, u16 cmd_len,
6308
+ u8 *data)
6309
+{
6310
+ struct l2cap_chan *chan, *tmp;
6311
+ struct l2cap_ecred_conn_rsp *rsp = (void *) data;
6312
+ u16 result;
6313
+
6314
+ if (cmd_len < sizeof(*rsp))
6315
+ return -EPROTO;
6316
+
6317
+ result = __le16_to_cpu(rsp->result);
6318
+
6319
+ BT_DBG("result 0x%4.4x", rsp->result);
6320
+
6321
+ if (!result)
6322
+ return 0;
6323
+
6324
+ list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
6325
+ if (chan->ident != cmd->ident)
6326
+ continue;
6327
+
6328
+ l2cap_chan_del(chan, ECONNRESET);
6329
+ }
56556330
56566331 return 0;
56576332 }
....@@ -5709,6 +6384,22 @@
57096384
57106385 case L2CAP_LE_CREDITS:
57116386 err = l2cap_le_credits(conn, cmd, cmd_len, data);
6387
+ break;
6388
+
6389
+ case L2CAP_ECRED_CONN_REQ:
6390
+ err = l2cap_ecred_conn_req(conn, cmd, cmd_len, data);
6391
+ break;
6392
+
6393
+ case L2CAP_ECRED_CONN_RSP:
6394
+ err = l2cap_ecred_conn_rsp(conn, cmd, cmd_len, data);
6395
+ break;
6396
+
6397
+ case L2CAP_ECRED_RECONF_REQ:
6398
+ err = l2cap_ecred_reconf_req(conn, cmd, cmd_len, data);
6399
+ break;
6400
+
6401
+ case L2CAP_ECRED_RECONF_RSP:
6402
+ err = l2cap_ecred_reconf_rsp(conn, cmd, cmd_len, data);
57126403 break;
57136404
57146405 case L2CAP_DISCONN_REQ:
....@@ -5773,9 +6464,7 @@
57736464 struct sk_buff *skb)
57746465 {
57756466 struct hci_conn *hcon = conn->hcon;
5776
- u8 *data = skb->data;
5777
- int len = skb->len;
5778
- struct l2cap_cmd_hdr cmd;
6467
+ struct l2cap_cmd_hdr *cmd;
57796468 int err;
57806469
57816470 l2cap_raw_recv(conn, skb);
....@@ -5783,35 +6472,34 @@
57836472 if (hcon->type != ACL_LINK)
57846473 goto drop;
57856474
5786
- while (len >= L2CAP_CMD_HDR_SIZE) {
5787
- u16 cmd_len;
5788
- memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
5789
- data += L2CAP_CMD_HDR_SIZE;
5790
- len -= L2CAP_CMD_HDR_SIZE;
6475
+ while (skb->len >= L2CAP_CMD_HDR_SIZE) {
6476
+ u16 len;
57916477
5792
- cmd_len = le16_to_cpu(cmd.len);
6478
+ cmd = (void *) skb->data;
6479
+ skb_pull(skb, L2CAP_CMD_HDR_SIZE);
57936480
5794
- BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len,
5795
- cmd.ident);
6481
+ len = le16_to_cpu(cmd->len);
57966482
5797
- if (cmd_len > len || !cmd.ident) {
6483
+ BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len,
6484
+ cmd->ident);
6485
+
6486
+ if (len > skb->len || !cmd->ident) {
57986487 BT_DBG("corrupted command");
57996488 break;
58006489 }
58016490
5802
- err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
6491
+ err = l2cap_bredr_sig_cmd(conn, cmd, len, skb->data);
58036492 if (err) {
58046493 struct l2cap_cmd_rej_unk rej;
58056494
58066495 BT_ERR("Wrong link type (%d)", err);
58076496
58086497 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
5809
- l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
6498
+ l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
58106499 sizeof(rej), &rej);
58116500 }
58126501
5813
- data += cmd_len;
5814
- len -= cmd_len;
6502
+ skb_pull(skb, len);
58156503 }
58166504
58176505 drop:
....@@ -6213,6 +6901,7 @@
62136901 struct l2cap_ctrl *control,
62146902 struct sk_buff *skb, u8 event)
62156903 {
6904
+ struct l2cap_ctrl local_control;
62166905 int err = 0;
62176906 bool skb_in_use = false;
62186907
....@@ -6237,15 +6926,32 @@
62376926 chan->buffer_seq = chan->expected_tx_seq;
62386927 skb_in_use = true;
62396928
6929
+ /* l2cap_reassemble_sdu may free skb, hence invalidate
6930
+ * control, so make a copy in advance to use it after
6931
+ * l2cap_reassemble_sdu returns and to avoid the race
6932
+ * condition, for example:
6933
+ *
6934
+ * The current thread calls:
6935
+ * l2cap_reassemble_sdu
6936
+ * chan->ops->recv == l2cap_sock_recv_cb
6937
+ * __sock_queue_rcv_skb
6938
+ * Another thread calls:
6939
+ * bt_sock_recvmsg
6940
+ * skb_recv_datagram
6941
+ * skb_free_datagram
6942
+ * Then the current thread tries to access control, but
6943
+ * it was freed by skb_free_datagram.
6944
+ */
6945
+ local_control = *control;
62406946 err = l2cap_reassemble_sdu(chan, skb, control);
62416947 if (err)
62426948 break;
62436949
6244
- if (control->final) {
6950
+ if (local_control.final) {
62456951 if (!test_and_clear_bit(CONN_REJ_ACT,
62466952 &chan->conn_state)) {
6247
- control->final = 0;
6248
- l2cap_retransmit_all(chan, control);
6953
+ local_control.final = 0;
6954
+ l2cap_retransmit_all(chan, &local_control);
62496955 l2cap_ertm_send(chan);
62506956 }
62516957 }
....@@ -6625,11 +7331,27 @@
66257331 static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
66267332 struct sk_buff *skb)
66277333 {
7334
+ /* l2cap_reassemble_sdu may free skb, hence invalidate control, so store
7335
+ * the txseq field in advance to use it after l2cap_reassemble_sdu
7336
+ * returns and to avoid the race condition, for example:
7337
+ *
7338
+ * The current thread calls:
7339
+ * l2cap_reassemble_sdu
7340
+ * chan->ops->recv == l2cap_sock_recv_cb
7341
+ * __sock_queue_rcv_skb
7342
+ * Another thread calls:
7343
+ * bt_sock_recvmsg
7344
+ * skb_recv_datagram
7345
+ * skb_free_datagram
7346
+ * Then the current thread tries to access control, but it was freed by
7347
+ * skb_free_datagram.
7348
+ */
7349
+ u16 txseq = control->txseq;
7350
+
66287351 BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb,
66297352 chan->rx_state);
66307353
6631
- if (l2cap_classify_txseq(chan, control->txseq) ==
6632
- L2CAP_TXSEQ_EXPECTED) {
7354
+ if (l2cap_classify_txseq(chan, txseq) == L2CAP_TXSEQ_EXPECTED) {
66337355 l2cap_pass_to_tx(chan, control);
66347356
66357357 BT_DBG("buffer_seq %d->%d", chan->buffer_seq,
....@@ -6652,8 +7374,8 @@
66527374 }
66537375 }
66547376
6655
- chan->last_acked_seq = control->txseq;
6656
- chan->expected_tx_seq = __next_seq(chan, control->txseq);
7377
+ chan->last_acked_seq = txseq;
7378
+ chan->expected_tx_seq = __next_seq(chan, txseq);
66577379
66587380 return 0;
66597381 }
....@@ -6757,13 +7479,12 @@
67577479 struct l2cap_le_credits pkt;
67587480 u16 return_credits;
67597481
6760
- /* We return more credits to the sender only after the amount of
6761
- * credits falls below half of the initial amount.
6762
- */
6763
- if (chan->rx_credits >= (le_max_credits + 1) / 2)
7482
+ return_credits = (chan->imtu / chan->mps) + 1;
7483
+
7484
+ if (chan->rx_credits >= return_credits)
67647485 return;
67657486
6766
- return_credits = le_max_credits - chan->rx_credits;
7487
+ return_credits -= chan->rx_credits;
67677488
67687489 BT_DBG("chan %p returning %u credits to sender", chan, return_credits);
67697490
....@@ -6777,7 +7498,22 @@
67777498 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt);
67787499 }
67797500
6780
-static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
7501
+static int l2cap_ecred_recv(struct l2cap_chan *chan, struct sk_buff *skb)
7502
+{
7503
+ int err;
7504
+
7505
+ BT_DBG("SDU reassemble complete: chan %p skb->len %u", chan, skb->len);
7506
+
7507
+ /* Wait recv to confirm reception before updating the credits */
7508
+ err = chan->ops->recv(chan, skb);
7509
+
7510
+ /* Update credits whenever an SDU is received */
7511
+ l2cap_chan_le_send_credits(chan);
7512
+
7513
+ return err;
7514
+}
7515
+
7516
+static int l2cap_ecred_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
67817517 {
67827518 int err;
67837519
....@@ -6795,7 +7531,11 @@
67957531 chan->rx_credits--;
67967532 BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits);
67977533
6798
- l2cap_chan_le_send_credits(chan);
7534
+ /* Update if remote had run out of credits, this should only happens
7535
+ * if the remote is not using the entire MPS.
7536
+ */
7537
+ if (!chan->rx_credits)
7538
+ l2cap_chan_le_send_credits(chan);
67997539
68007540 err = 0;
68017541
....@@ -6821,7 +7561,7 @@
68217561 }
68227562
68237563 if (skb->len == sdu_len)
6824
- return chan->ops->recv(chan, skb);
7564
+ return l2cap_ecred_recv(chan, skb);
68257565
68267566 chan->sdu = skb;
68277567 chan->sdu_len = sdu_len;
....@@ -6853,7 +7593,7 @@
68537593 skb = NULL;
68547594
68557595 if (chan->sdu->len == chan->sdu_len) {
6856
- err = chan->ops->recv(chan, chan->sdu);
7596
+ err = l2cap_ecred_recv(chan, chan->sdu);
68577597 if (!err) {
68587598 chan->sdu = NULL;
68597599 chan->sdu_last_frag = NULL;
....@@ -6891,6 +7631,7 @@
68917631 return;
68927632 }
68937633
7634
+ l2cap_chan_hold(chan);
68947635 l2cap_chan_lock(chan);
68957636 } else {
68967637 BT_DBG("unknown cid 0x%4.4x", cid);
....@@ -6903,7 +7644,7 @@
69037644 BT_DBG("chan %p, len %d", chan, skb->len);
69047645
69057646 /* If we receive data on a fixed channel before the info req/rsp
6906
- * procdure is done simply assume that the channel is supported
7647
+ * procedure is done simply assume that the channel is supported
69077648 * and mark it as ready.
69087649 */
69097650 if (chan->chan_type == L2CAP_CHAN_FIXED)
....@@ -6914,7 +7655,8 @@
69147655
69157656 switch (chan->mode) {
69167657 case L2CAP_MODE_LE_FLOWCTL:
6917
- if (l2cap_le_data_rcv(chan, skb) < 0)
7658
+ case L2CAP_MODE_EXT_FLOWCTL:
7659
+ if (l2cap_ecred_data_rcv(chan, skb) < 0)
69187660 goto drop;
69197661
69207662 goto done;
....@@ -6949,6 +7691,7 @@
69497691
69507692 done:
69517693 l2cap_chan_unlock(chan);
7694
+ l2cap_chan_put(chan);
69527695 }
69537696
69547697 static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
....@@ -7086,7 +7829,7 @@
70867829 conn->mtu = hcon->hdev->le_mtu;
70877830 break;
70887831 }
7089
- /* fall through */
7832
+ fallthrough;
70907833 default:
70917834 conn->mtu = hcon->hdev->acl_mtu;
70927835 break;
....@@ -7133,6 +7876,33 @@
71337876 return ((psm & 0x0101) == 0x0001);
71347877 }
71357878
7879
+struct l2cap_chan_data {
7880
+ struct l2cap_chan *chan;
7881
+ struct pid *pid;
7882
+ int count;
7883
+};
7884
+
7885
+static void l2cap_chan_by_pid(struct l2cap_chan *chan, void *data)
7886
+{
7887
+ struct l2cap_chan_data *d = data;
7888
+ struct pid *pid;
7889
+
7890
+ if (chan == d->chan)
7891
+ return;
7892
+
7893
+ if (!test_bit(FLAG_DEFER_SETUP, &chan->flags))
7894
+ return;
7895
+
7896
+ pid = chan->ops->get_peer_pid(chan);
7897
+
7898
+ /* Only count deferred channels with the same PID/PSM */
7899
+ if (d->pid != pid || chan->psm != d->chan->psm || chan->ident ||
7900
+ chan->mode != L2CAP_MODE_EXT_FLOWCTL || chan->state != BT_CONNECT)
7901
+ return;
7902
+
7903
+ d->count++;
7904
+}
7905
+
71367906 int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
71377907 bdaddr_t *dst, u8 dst_type)
71387908 {
....@@ -7141,8 +7911,8 @@
71417911 struct hci_dev *hdev;
71427912 int err;
71437913
7144
- BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst,
7145
- dst_type, __le16_to_cpu(psm));
7914
+ BT_DBG("%pMR -> %pMR (type %u) psm 0x%4.4x mode 0x%2.2x", &chan->src,
7915
+ dst, dst_type, __le16_to_cpu(psm), chan->mode);
71467916
71477917 hdev = hci_get_route(dst, &chan->src, chan->src_type);
71487918 if (!hdev)
....@@ -7170,13 +7940,18 @@
71707940 case L2CAP_MODE_BASIC:
71717941 break;
71727942 case L2CAP_MODE_LE_FLOWCTL:
7173
- l2cap_le_flowctl_init(chan);
7943
+ break;
7944
+ case L2CAP_MODE_EXT_FLOWCTL:
7945
+ if (!enable_ecred) {
7946
+ err = -EOPNOTSUPP;
7947
+ goto done;
7948
+ }
71747949 break;
71757950 case L2CAP_MODE_ERTM:
71767951 case L2CAP_MODE_STREAMING:
71777952 if (!disable_ertm)
71787953 break;
7179
- /* fall through */
7954
+ fallthrough;
71807955 default:
71817956 err = -EOPNOTSUPP;
71827957 goto done;
....@@ -7228,11 +8003,13 @@
72288003 else
72298004 hcon = hci_connect_le_scan(hdev, dst, dst_type,
72308005 chan->sec_level,
7231
- HCI_LE_CONN_TIMEOUT);
8006
+ HCI_LE_CONN_TIMEOUT,
8007
+ CONN_REASON_L2CAP_CHAN);
72328008
72338009 } else {
72348010 u8 auth_type = l2cap_get_auth_type(chan);
7235
- hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type);
8011
+ hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type,
8012
+ CONN_REASON_L2CAP_CHAN);
72368013 }
72378014
72388015 if (IS_ERR(hcon)) {
....@@ -7245,6 +8022,23 @@
72458022 hci_conn_drop(hcon);
72468023 err = -ENOMEM;
72478024 goto done;
8025
+ }
8026
+
8027
+ if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) {
8028
+ struct l2cap_chan_data data;
8029
+
8030
+ data.chan = chan;
8031
+ data.pid = chan->ops->get_peer_pid(chan);
8032
+ data.count = 1;
8033
+
8034
+ l2cap_chan_list(conn, l2cap_chan_by_pid, &data);
8035
+
8036
+ /* Check if there isn't too many channels being connected */
8037
+ if (data.count > L2CAP_ECRED_CONN_SCID_MAX) {
8038
+ hci_conn_drop(hcon);
8039
+ err = -EPROTO;
8040
+ goto done;
8041
+ }
72488042 }
72498043
72508044 mutex_lock(&conn->chan_lock);
....@@ -7295,6 +8089,38 @@
72958089 return err;
72968090 }
72978091 EXPORT_SYMBOL_GPL(l2cap_chan_connect);
8092
+
8093
+static void l2cap_ecred_reconfigure(struct l2cap_chan *chan)
8094
+{
8095
+ struct l2cap_conn *conn = chan->conn;
8096
+ struct {
8097
+ struct l2cap_ecred_reconf_req req;
8098
+ __le16 scid;
8099
+ } pdu;
8100
+
8101
+ pdu.req.mtu = cpu_to_le16(chan->imtu);
8102
+ pdu.req.mps = cpu_to_le16(chan->mps);
8103
+ pdu.scid = cpu_to_le16(chan->scid);
8104
+
8105
+ chan->ident = l2cap_get_ident(conn);
8106
+
8107
+ l2cap_send_cmd(conn, chan->ident, L2CAP_ECRED_RECONF_REQ,
8108
+ sizeof(pdu), &pdu);
8109
+}
8110
+
8111
+int l2cap_chan_reconfigure(struct l2cap_chan *chan, __u16 mtu)
8112
+{
8113
+ if (chan->imtu > mtu)
8114
+ return -EINVAL;
8115
+
8116
+ BT_DBG("chan %p mtu 0x%4.4x", chan, mtu);
8117
+
8118
+ chan->imtu = mtu;
8119
+
8120
+ l2cap_ecred_reconfigure(chan);
8121
+
8122
+ return 0;
8123
+}
72988124
72998125 /* ---- L2CAP interface with lower layer (HCI) ---- */
73008126
....@@ -7353,7 +8179,7 @@
73538179 if (src_type != c->src_type)
73548180 continue;
73558181
7356
- l2cap_chan_hold(c);
8182
+ c = l2cap_chan_hold_unless_zero(c);
73578183 read_unlock(&chan_list_lock);
73588184 return c;
73598185 }
....@@ -7507,7 +8333,8 @@
75078333 else
75088334 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
75098335 } else if (chan->state == BT_CONNECT2 &&
7510
- chan->mode != L2CAP_MODE_LE_FLOWCTL) {
8336
+ !(chan->mode == L2CAP_MODE_EXT_FLOWCTL ||
8337
+ chan->mode == L2CAP_MODE_LE_FLOWCTL)) {
75118338 struct l2cap_conn_rsp rsp;
75128339 __u16 res, stat;
75138340
....@@ -7683,17 +8510,7 @@
76838510 return 0;
76848511 }
76858512
7686
-static int l2cap_debugfs_open(struct inode *inode, struct file *file)
7687
-{
7688
- return single_open(file, l2cap_debugfs_show, inode->i_private);
7689
-}
7690
-
7691
-static const struct file_operations l2cap_debugfs_fops = {
7692
- .open = l2cap_debugfs_open,
7693
- .read = seq_read,
7694
- .llseek = seq_lseek,
7695
- .release = single_release,
7696
-};
8513
+DEFINE_SHOW_ATTRIBUTE(l2cap_debugfs);
76978514
76988515 static struct dentry *l2cap_debugfs;
76998516
....@@ -7713,11 +8530,6 @@
77138530 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
77148531 NULL, &l2cap_debugfs_fops);
77158532
7716
- debugfs_create_u16("l2cap_le_max_credits", 0644, bt_debugfs,
7717
- &le_max_credits);
7718
- debugfs_create_u16("l2cap_le_default_mps", 0644, bt_debugfs,
7719
- &le_default_mps);
7720
-
77218533 return 0;
77228534 }
77238535
....@@ -7730,3 +8542,6 @@
77308542
77318543 module_param(disable_ertm, bool, 0644);
77328544 MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");
8545
+
8546
+module_param(enable_ecred, bool, 0644);
8547
+MODULE_PARM_DESC(enable_ecred, "Enable enhanced credit flow control mode");