aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth/l2cap.c
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2007-10-20 07:37:56 -0400
committerDavid S. Miller <davem@sunset.davemloft.net>2007-10-22 05:59:41 -0400
commit4e8402a3f884427f9233ba436459c158d1f2e114 (patch)
tree3853523eaba309ea8aff89ed4868770cf82d40b2 /net/bluetooth/l2cap.c
parent861d6882b3dfe1710b35dbddf1b395b962061413 (diff)
[Bluetooth] Retrieve L2CAP features mask on connection setup
The Bluetooth 1.2 specification introduced a specific features mask value to interoperate with newer versions of the specification. So far this piece of information was never needed, but future extensions will rely on it. This patch adds a generic way to retrieve this information only once per connection setup. Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Diffstat (limited to 'net/bluetooth/l2cap.c')
-rw-r--r--net/bluetooth/l2cap.c233
1 files changed, 151 insertions, 82 deletions
diff --git a/net/bluetooth/l2cap.c b/net/bluetooth/l2cap.c
index fde1606a4f31..896ae5a9c07a 100644
--- a/net/bluetooth/l2cap.c
+++ b/net/bluetooth/l2cap.c
@@ -258,7 +258,119 @@ static void l2cap_chan_del(struct sock *sk, int err)
258 sk->sk_state_change(sk); 258 sk->sk_state_change(sk);
259} 259}
260 260
261static inline u8 l2cap_get_ident(struct l2cap_conn *conn)
262{
263 u8 id;
264
265 /* Get next available identificator.
266 * 1 - 128 are used by kernel.
267 * 129 - 199 are reserved.
268 * 200 - 254 are used by utilities like l2ping, etc.
269 */
270
271 spin_lock_bh(&conn->lock);
272
273 if (++conn->tx_ident > 128)
274 conn->tx_ident = 1;
275
276 id = conn->tx_ident;
277
278 spin_unlock_bh(&conn->lock);
279
280 return id;
281}
282
283static inline int l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
284{
285 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
286
287 BT_DBG("code 0x%2.2x", code);
288
289 if (!skb)
290 return -ENOMEM;
291
292 return hci_send_acl(conn->hcon, skb, 0);
293}
294
261/* ---- L2CAP connections ---- */ 295/* ---- L2CAP connections ---- */
296static void l2cap_conn_start(struct l2cap_conn *conn)
297{
298 struct l2cap_chan_list *l = &conn->chan_list;
299 struct sock *sk;
300
301 BT_DBG("conn %p", conn);
302
303 read_lock(&l->lock);
304
305 for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
306 bh_lock_sock(sk);
307
308 if (sk->sk_type != SOCK_SEQPACKET) {
309 l2cap_sock_clear_timer(sk);
310 sk->sk_state = BT_CONNECTED;
311 sk->sk_state_change(sk);
312 } else if (sk->sk_state == BT_CONNECT) {
313 struct l2cap_conn_req req;
314 l2cap_pi(sk)->ident = l2cap_get_ident(conn);
315 req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
316 req.psm = l2cap_pi(sk)->psm;
317 l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
318 L2CAP_CONN_REQ, sizeof(req), &req);
319 }
320
321 bh_unlock_sock(sk);
322 }
323
324 read_unlock(&l->lock);
325}
326
327static void l2cap_conn_ready(struct l2cap_conn *conn)
328{
329 BT_DBG("conn %p", conn);
330
331 if (conn->chan_list.head || !hlist_empty(&l2cap_sk_list.head)) {
332 struct l2cap_info_req req;
333
334 req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
335
336 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
337 conn->info_ident = l2cap_get_ident(conn);
338
339 mod_timer(&conn->info_timer,
340 jiffies + msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
341
342 l2cap_send_cmd(conn, conn->info_ident,
343 L2CAP_INFO_REQ, sizeof(req), &req);
344 }
345}
346
347/* Notify sockets that we cannot guaranty reliability anymore */
348static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
349{
350 struct l2cap_chan_list *l = &conn->chan_list;
351 struct sock *sk;
352
353 BT_DBG("conn %p", conn);
354
355 read_lock(&l->lock);
356
357 for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
358 if (l2cap_pi(sk)->link_mode & L2CAP_LM_RELIABLE)
359 sk->sk_err = err;
360 }
361
362 read_unlock(&l->lock);
363}
364
365static void l2cap_info_timeout(unsigned long arg)
366{
367 struct l2cap_conn *conn = (void *) arg;
368
369 conn->info_ident = 0;
370
371 l2cap_conn_start(conn);
372}
373
262static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) 374static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
263{ 375{
264 struct l2cap_conn *conn = hcon->l2cap_data; 376 struct l2cap_conn *conn = hcon->l2cap_data;
@@ -279,6 +391,12 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
279 conn->src = &hcon->hdev->bdaddr; 391 conn->src = &hcon->hdev->bdaddr;
280 conn->dst = &hcon->dst; 392 conn->dst = &hcon->dst;
281 393
394 conn->feat_mask = 0;
395
396 init_timer(&conn->info_timer);
397 conn->info_timer.function = l2cap_info_timeout;
398 conn->info_timer.data = (unsigned long) conn;
399
282 spin_lock_init(&conn->lock); 400 spin_lock_init(&conn->lock);
283 rwlock_init(&conn->chan_list.lock); 401 rwlock_init(&conn->chan_list.lock);
284 402
@@ -318,40 +436,6 @@ static inline void l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, stru
318 write_unlock_bh(&l->lock); 436 write_unlock_bh(&l->lock);
319} 437}
320 438
321static inline u8 l2cap_get_ident(struct l2cap_conn *conn)
322{
323 u8 id;
324
325 /* Get next available identificator.
326 * 1 - 128 are used by kernel.
327 * 129 - 199 are reserved.
328 * 200 - 254 are used by utilities like l2ping, etc.
329 */
330
331 spin_lock_bh(&conn->lock);
332
333 if (++conn->tx_ident > 128)
334 conn->tx_ident = 1;
335
336 id = conn->tx_ident;
337
338 spin_unlock_bh(&conn->lock);
339
340 return id;
341}
342
343static inline int l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
344{
345 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
346
347 BT_DBG("code 0x%2.2x", code);
348
349 if (!skb)
350 return -ENOMEM;
351
352 return hci_send_acl(conn->hcon, skb, 0);
353}
354
355/* ---- Socket interface ---- */ 439/* ---- Socket interface ---- */
356static struct sock *__l2cap_get_sock_by_addr(__le16 psm, bdaddr_t *src) 440static struct sock *__l2cap_get_sock_by_addr(__le16 psm, bdaddr_t *src)
357{ 441{
@@ -529,7 +613,7 @@ static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int p
529 INIT_LIST_HEAD(&bt_sk(sk)->accept_q); 613 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
530 614
531 sk->sk_destruct = l2cap_sock_destruct; 615 sk->sk_destruct = l2cap_sock_destruct;
532 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT; 616 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
533 617
534 sock_reset_flag(sk, SOCK_ZAPPED); 618 sock_reset_flag(sk, SOCK_ZAPPED);
535 619
@@ -649,6 +733,11 @@ static int l2cap_do_connect(struct sock *sk)
649 l2cap_sock_set_timer(sk, sk->sk_sndtimeo); 733 l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
650 734
651 if (hcon->state == BT_CONNECTED) { 735 if (hcon->state == BT_CONNECTED) {
736 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)) {
737 l2cap_conn_ready(conn);
738 goto done;
739 }
740
652 if (sk->sk_type == SOCK_SEQPACKET) { 741 if (sk->sk_type == SOCK_SEQPACKET) {
653 struct l2cap_conn_req req; 742 struct l2cap_conn_req req;
654 l2cap_pi(sk)->ident = l2cap_get_ident(conn); 743 l2cap_pi(sk)->ident = l2cap_get_ident(conn);
@@ -1083,52 +1172,6 @@ static int l2cap_sock_release(struct socket *sock)
1083 return err; 1172 return err;
1084} 1173}
1085 1174
1086static void l2cap_conn_ready(struct l2cap_conn *conn)
1087{
1088 struct l2cap_chan_list *l = &conn->chan_list;
1089 struct sock *sk;
1090
1091 BT_DBG("conn %p", conn);
1092
1093 read_lock(&l->lock);
1094
1095 for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
1096 bh_lock_sock(sk);
1097
1098 if (sk->sk_type != SOCK_SEQPACKET) {
1099 l2cap_sock_clear_timer(sk);
1100 sk->sk_state = BT_CONNECTED;
1101 sk->sk_state_change(sk);
1102 } else if (sk->sk_state == BT_CONNECT) {
1103 struct l2cap_conn_req req;
1104 l2cap_pi(sk)->ident = l2cap_get_ident(conn);
1105 req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
1106 req.psm = l2cap_pi(sk)->psm;
1107 l2cap_send_cmd(conn, l2cap_pi(sk)->ident, L2CAP_CONN_REQ, sizeof(req), &req);
1108 }
1109
1110 bh_unlock_sock(sk);
1111 }
1112
1113 read_unlock(&l->lock);
1114}
1115
1116/* Notify sockets that we cannot guaranty reliability anymore */
1117static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
1118{
1119 struct l2cap_chan_list *l = &conn->chan_list;
1120 struct sock *sk;
1121
1122 BT_DBG("conn %p", conn);
1123
1124 read_lock(&l->lock);
1125 for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
1126 if (l2cap_pi(sk)->link_mode & L2CAP_LM_RELIABLE)
1127 sk->sk_err = err;
1128 }
1129 read_unlock(&l->lock);
1130}
1131
1132static void l2cap_chan_ready(struct sock *sk) 1175static void l2cap_chan_ready(struct sock *sk)
1133{ 1176{
1134 struct sock *parent = bt_sk(sk)->parent; 1177 struct sock *parent = bt_sk(sk)->parent;
@@ -1399,6 +1442,23 @@ static int l2cap_build_conf_rsp(struct sock *sk, void *data, u16 result, u16 fla
1399 return ptr - data; 1442 return ptr - data;
1400} 1443}
1401 1444
1445static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
1446{
1447 struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data;
1448
1449 if (rej->reason != 0x0000)
1450 return 0;
1451
1452 if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
1453 cmd->ident == conn->info_ident) {
1454 conn->info_ident = 0;
1455 del_timer(&conn->info_timer);
1456 l2cap_conn_start(conn);
1457 }
1458
1459 return 0;
1460}
1461
1402static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 1462static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
1403{ 1463{
1404 struct l2cap_chan_list *list = &conn->chan_list; 1464 struct l2cap_chan_list *list = &conn->chan_list;
@@ -1739,6 +1799,15 @@ static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cm
1739 1799
1740 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 1800 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
1741 1801
1802 conn->info_ident = 0;
1803
1804 del_timer(&conn->info_timer);
1805
1806 if (type == L2CAP_IT_FEAT_MASK)
1807 conn->feat_mask = __le32_to_cpu(get_unaligned((__le32 *) rsp->data));
1808
1809 l2cap_conn_start(conn);
1810
1742 return 0; 1811 return 0;
1743} 1812}
1744 1813
@@ -1768,7 +1837,7 @@ static inline void l2cap_sig_channel(struct l2cap_conn *conn, struct sk_buff *sk
1768 1837
1769 switch (cmd.code) { 1838 switch (cmd.code) {
1770 case L2CAP_COMMAND_REJ: 1839 case L2CAP_COMMAND_REJ:
1771 /* FIXME: We should process this */ 1840 l2cap_command_rej(conn, &cmd, data);
1772 break; 1841 break;
1773 1842
1774 case L2CAP_CONN_REQ: 1843 case L2CAP_CONN_REQ: