blob: 7bba469b6828f1a37fa612f006378abf5c9ee04d [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth L2CAP core and sockets. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28
29#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080030#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
32#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/sched.h>
34#include <linux/slab.h>
35#include <linux/poll.h>
36#include <linux/fcntl.h>
37#include <linux/init.h>
38#include <linux/interrupt.h>
39#include <linux/socket.h>
40#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/list.h>
Marcel Holtmannbe9d1222005-11-08 09:57:38 -080042#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <net/sock.h>
44
45#include <asm/system.h>
46#include <asm/uaccess.h>
47#include <asm/unaligned.h>
48
49#include <net/bluetooth/bluetooth.h>
50#include <net/bluetooth/hci_core.h>
51#include <net/bluetooth/l2cap.h>
52
Marcel Holtmanne1027a72009-02-09 09:18:02 +010053#define VERSION "2.13"
Marcel Holtmannf0709e02007-10-20 13:38:51 +020054
Marcel Holtmanne1027a72009-02-09 09:18:02 +010055static u32 l2cap_feat_mask = 0x0080;
56static u8 l2cap_fixed_chan[8] = { 0x02, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080058static const struct proto_ops l2cap_sock_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60static struct bt_sock_list l2cap_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070061 .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void __l2cap_sock_close(struct sock *sk, int reason);
65static void l2cap_sock_close(struct sock *sk);
66static void l2cap_sock_kill(struct sock *sk);
67
68static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
69 u8 code, u8 ident, u16 dlen, void *data);
70
71/* ---- L2CAP timers ---- */
72static void l2cap_sock_timeout(unsigned long arg)
73{
74 struct sock *sk = (struct sock *) arg;
Marcel Holtmannb1235d7962008-07-14 20:13:54 +020075 int reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 BT_DBG("sock %p state %d", sk, sk->sk_state);
78
79 bh_lock_sock(sk);
Marcel Holtmannb1235d7962008-07-14 20:13:54 +020080
Marcel Holtmannf62e4322009-01-15 21:58:44 +010081 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG)
82 reason = ECONNREFUSED;
83 else if (sk->sk_state == BT_CONNECT &&
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +010084 l2cap_pi(sk)->sec_level != BT_SECURITY_SDP)
Marcel Holtmannb1235d7962008-07-14 20:13:54 +020085 reason = ECONNREFUSED;
86 else
87 reason = ETIMEDOUT;
88
89 __l2cap_sock_close(sk, reason);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 bh_unlock_sock(sk);
92
93 l2cap_sock_kill(sk);
94 sock_put(sk);
95}
96
97static void l2cap_sock_set_timer(struct sock *sk, long timeout)
98{
99 BT_DBG("sk %p state %d timeout %ld", sk, sk->sk_state, timeout);
100 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
101}
102
103static void l2cap_sock_clear_timer(struct sock *sk)
104{
105 BT_DBG("sock %p state %d", sk, sk->sk_state);
106 sk_stop_timer(sk, &sk->sk_timer);
107}
108
Marcel Holtmann01394182006-07-03 10:02:46 +0200109/* ---- L2CAP channels ---- */
110static struct sock *__l2cap_get_chan_by_dcid(struct l2cap_chan_list *l, u16 cid)
111{
112 struct sock *s;
113 for (s = l->head; s; s = l2cap_pi(s)->next_c) {
114 if (l2cap_pi(s)->dcid == cid)
115 break;
116 }
117 return s;
118}
119
120static struct sock *__l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
121{
122 struct sock *s;
123 for (s = l->head; s; s = l2cap_pi(s)->next_c) {
124 if (l2cap_pi(s)->scid == cid)
125 break;
126 }
127 return s;
128}
129
130/* Find channel with given SCID.
131 * Returns locked socket */
132static inline struct sock *l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
133{
134 struct sock *s;
135 read_lock(&l->lock);
136 s = __l2cap_get_chan_by_scid(l, cid);
137 if (s) bh_lock_sock(s);
138 read_unlock(&l->lock);
139 return s;
140}
141
142static struct sock *__l2cap_get_chan_by_ident(struct l2cap_chan_list *l, u8 ident)
143{
144 struct sock *s;
145 for (s = l->head; s; s = l2cap_pi(s)->next_c) {
146 if (l2cap_pi(s)->ident == ident)
147 break;
148 }
149 return s;
150}
151
152static inline struct sock *l2cap_get_chan_by_ident(struct l2cap_chan_list *l, u8 ident)
153{
154 struct sock *s;
155 read_lock(&l->lock);
156 s = __l2cap_get_chan_by_ident(l, ident);
157 if (s) bh_lock_sock(s);
158 read_unlock(&l->lock);
159 return s;
160}
161
162static u16 l2cap_alloc_cid(struct l2cap_chan_list *l)
163{
164 u16 cid = 0x0040;
165
166 for (; cid < 0xffff; cid++) {
167 if(!__l2cap_get_chan_by_scid(l, cid))
168 return cid;
169 }
170
171 return 0;
172}
173
174static inline void __l2cap_chan_link(struct l2cap_chan_list *l, struct sock *sk)
175{
176 sock_hold(sk);
177
178 if (l->head)
179 l2cap_pi(l->head)->prev_c = sk;
180
181 l2cap_pi(sk)->next_c = l->head;
182 l2cap_pi(sk)->prev_c = NULL;
183 l->head = sk;
184}
185
186static inline void l2cap_chan_unlink(struct l2cap_chan_list *l, struct sock *sk)
187{
188 struct sock *next = l2cap_pi(sk)->next_c, *prev = l2cap_pi(sk)->prev_c;
189
Marcel Holtmannfd1278d2006-07-12 23:00:07 +0200190 write_lock_bh(&l->lock);
Marcel Holtmann01394182006-07-03 10:02:46 +0200191 if (sk == l->head)
192 l->head = next;
193
194 if (next)
195 l2cap_pi(next)->prev_c = prev;
196 if (prev)
197 l2cap_pi(prev)->next_c = next;
Marcel Holtmannfd1278d2006-07-12 23:00:07 +0200198 write_unlock_bh(&l->lock);
Marcel Holtmann01394182006-07-03 10:02:46 +0200199
200 __sock_put(sk);
201}
202
203static void __l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent)
204{
205 struct l2cap_chan_list *l = &conn->chan_list;
206
207 BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, l2cap_pi(sk)->psm, l2cap_pi(sk)->dcid);
208
209 l2cap_pi(sk)->conn = conn;
210
211 if (sk->sk_type == SOCK_SEQPACKET) {
212 /* Alloc CID for connection-oriented socket */
213 l2cap_pi(sk)->scid = l2cap_alloc_cid(l);
214 } else if (sk->sk_type == SOCK_DGRAM) {
215 /* Connectionless socket */
216 l2cap_pi(sk)->scid = 0x0002;
217 l2cap_pi(sk)->dcid = 0x0002;
218 l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
219 } else {
220 /* Raw socket can send/recv signalling messages only */
221 l2cap_pi(sk)->scid = 0x0001;
222 l2cap_pi(sk)->dcid = 0x0001;
223 l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
224 }
225
226 __l2cap_chan_link(l, sk);
227
228 if (parent)
229 bt_accept_enqueue(parent, sk);
230}
231
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900232/* Delete channel.
Marcel Holtmann01394182006-07-03 10:02:46 +0200233 * Must be called on the locked socket. */
234static void l2cap_chan_del(struct sock *sk, int err)
235{
236 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
237 struct sock *parent = bt_sk(sk)->parent;
238
239 l2cap_sock_clear_timer(sk);
240
241 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
242
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900243 if (conn) {
Marcel Holtmann01394182006-07-03 10:02:46 +0200244 /* Unlink from channel list */
245 l2cap_chan_unlink(&conn->chan_list, sk);
246 l2cap_pi(sk)->conn = NULL;
247 hci_conn_put(conn->hcon);
248 }
249
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200250 sk->sk_state = BT_CLOSED;
Marcel Holtmann01394182006-07-03 10:02:46 +0200251 sock_set_flag(sk, SOCK_ZAPPED);
252
253 if (err)
254 sk->sk_err = err;
255
256 if (parent) {
257 bt_accept_unlink(sk);
258 parent->sk_data_ready(parent, 0);
259 } else
260 sk->sk_state_change(sk);
261}
262
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200263/* Service level security */
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100264static inline int l2cap_check_security(struct sock *sk)
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200265{
266 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100267 __u8 auth_type;
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200268
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100269 switch (l2cap_pi(sk)->sec_level) {
270 case BT_SECURITY_HIGH:
271 auth_type = HCI_AT_GENERAL_BONDING_MITM;
272 break;
273 case BT_SECURITY_MEDIUM:
274 auth_type = HCI_AT_GENERAL_BONDING;
275 break;
276 default:
277 auth_type = HCI_AT_NO_BONDING;
278 break;
279 }
280
281 return hci_conn_security(conn->hcon, l2cap_pi(sk)->sec_level,
282 auth_type);
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200283}
284
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200285static inline u8 l2cap_get_ident(struct l2cap_conn *conn)
286{
287 u8 id;
288
289 /* Get next available identificator.
290 * 1 - 128 are used by kernel.
291 * 129 - 199 are reserved.
292 * 200 - 254 are used by utilities like l2ping, etc.
293 */
294
295 spin_lock_bh(&conn->lock);
296
297 if (++conn->tx_ident > 128)
298 conn->tx_ident = 1;
299
300 id = conn->tx_ident;
301
302 spin_unlock_bh(&conn->lock);
303
304 return id;
305}
306
307static inline int l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
308{
309 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
310
311 BT_DBG("code 0x%2.2x", code);
312
313 if (!skb)
314 return -ENOMEM;
315
316 return hci_send_acl(conn->hcon, skb, 0);
317}
318
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200319static void l2cap_do_start(struct sock *sk)
320{
321 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
322
323 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
Marcel Holtmann984947d2009-02-06 23:35:19 +0100324 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
325 return;
326
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100327 if (l2cap_check_security(sk)) {
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200328 struct l2cap_conn_req req;
329 req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
330 req.psm = l2cap_pi(sk)->psm;
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200331
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200332 l2cap_pi(sk)->ident = l2cap_get_ident(conn);
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200333
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200334 l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200335 L2CAP_CONN_REQ, sizeof(req), &req);
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200336 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200337 } else {
338 struct l2cap_info_req req;
339 req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
340
341 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
342 conn->info_ident = l2cap_get_ident(conn);
343
344 mod_timer(&conn->info_timer, jiffies +
345 msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
346
347 l2cap_send_cmd(conn, conn->info_ident,
348 L2CAP_INFO_REQ, sizeof(req), &req);
349 }
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/* ---- L2CAP connections ---- */
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200353static void l2cap_conn_start(struct l2cap_conn *conn)
354{
355 struct l2cap_chan_list *l = &conn->chan_list;
356 struct sock *sk;
357
358 BT_DBG("conn %p", conn);
359
360 read_lock(&l->lock);
361
362 for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
363 bh_lock_sock(sk);
364
365 if (sk->sk_type != SOCK_SEQPACKET) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200366 bh_unlock_sock(sk);
367 continue;
368 }
369
370 if (sk->sk_state == BT_CONNECT) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100371 if (l2cap_check_security(sk)) {
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200372 struct l2cap_conn_req req;
373 req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
374 req.psm = l2cap_pi(sk)->psm;
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200375
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200376 l2cap_pi(sk)->ident = l2cap_get_ident(conn);
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200377
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200378 l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200379 L2CAP_CONN_REQ, sizeof(req), &req);
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200380 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200381 } else if (sk->sk_state == BT_CONNECT2) {
382 struct l2cap_conn_rsp rsp;
383 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
384 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
385
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100386 if (l2cap_check_security(sk)) {
Marcel Holtmannf66dc812009-01-15 21:57:00 +0100387 if (bt_sk(sk)->defer_setup) {
388 struct sock *parent = bt_sk(sk)->parent;
389 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
390 rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
391 parent->sk_data_ready(parent, 0);
392
393 } else {
394 sk->sk_state = BT_CONFIG;
395 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
396 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
397 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200398 } else {
399 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
400 rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
401 }
402
403 l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
404 L2CAP_CONN_RSP, sizeof(rsp), &rsp);
405 }
406
407 bh_unlock_sock(sk);
408 }
409
410 read_unlock(&l->lock);
411}
412
413static void l2cap_conn_ready(struct l2cap_conn *conn)
414{
415 struct l2cap_chan_list *l = &conn->chan_list;
416 struct sock *sk;
417
418 BT_DBG("conn %p", conn);
419
420 read_lock(&l->lock);
421
422 for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
423 bh_lock_sock(sk);
424
425 if (sk->sk_type != SOCK_SEQPACKET) {
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200426 l2cap_sock_clear_timer(sk);
427 sk->sk_state = BT_CONNECTED;
428 sk->sk_state_change(sk);
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200429 } else if (sk->sk_state == BT_CONNECT)
430 l2cap_do_start(sk);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200431
432 bh_unlock_sock(sk);
433 }
434
435 read_unlock(&l->lock);
436}
437
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200438/* Notify sockets that we cannot guaranty reliability anymore */
439static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
440{
441 struct l2cap_chan_list *l = &conn->chan_list;
442 struct sock *sk;
443
444 BT_DBG("conn %p", conn);
445
446 read_lock(&l->lock);
447
448 for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100449 if (l2cap_pi(sk)->force_reliable)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200450 sk->sk_err = err;
451 }
452
453 read_unlock(&l->lock);
454}
455
456static void l2cap_info_timeout(unsigned long arg)
457{
458 struct l2cap_conn *conn = (void *) arg;
459
Marcel Holtmann984947d2009-02-06 23:35:19 +0100460 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +0100461 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +0100462
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200463 l2cap_conn_start(conn);
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
467{
Marcel Holtmann01394182006-07-03 10:02:46 +0200468 struct l2cap_conn *conn = hcon->l2cap_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Marcel Holtmann01394182006-07-03 10:02:46 +0200470 if (conn || status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return conn;
472
Marcel Holtmann01394182006-07-03 10:02:46 +0200473 conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
474 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 hcon->l2cap_data = conn;
478 conn->hcon = hcon;
479
Marcel Holtmann01394182006-07-03 10:02:46 +0200480 BT_DBG("hcon %p conn %p", hcon, conn);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 conn->mtu = hcon->hdev->acl_mtu;
483 conn->src = &hcon->hdev->bdaddr;
484 conn->dst = &hcon->dst;
485
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200486 conn->feat_mask = 0;
487
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200488 setup_timer(&conn->info_timer, l2cap_info_timeout,
489 (unsigned long) conn);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 spin_lock_init(&conn->lock);
492 rwlock_init(&conn->chan_list.lock);
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return conn;
495}
496
Marcel Holtmann01394182006-07-03 10:02:46 +0200497static void l2cap_conn_del(struct hci_conn *hcon, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Marcel Holtmann01394182006-07-03 10:02:46 +0200499 struct l2cap_conn *conn = hcon->l2cap_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct sock *sk;
501
Marcel Holtmann01394182006-07-03 10:02:46 +0200502 if (!conn)
503 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
506
507 if (conn->rx_skb)
508 kfree_skb(conn->rx_skb);
509
510 /* Kill channels */
511 while ((sk = conn->chan_list.head)) {
512 bh_lock_sock(sk);
513 l2cap_chan_del(sk, err);
514 bh_unlock_sock(sk);
515 l2cap_sock_kill(sk);
516 }
517
Dave Young8e8440f2008-03-03 12:18:55 -0800518 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
519 del_timer_sync(&conn->info_timer);
Thomas Gleixner3ab22732008-02-26 17:42:56 -0800520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 hcon->l2cap_data = NULL;
522 kfree(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
525static inline void l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent)
526{
527 struct l2cap_chan_list *l = &conn->chan_list;
Marcel Holtmannfd1278d2006-07-12 23:00:07 +0200528 write_lock_bh(&l->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 __l2cap_chan_add(conn, sk, parent);
Marcel Holtmannfd1278d2006-07-12 23:00:07 +0200530 write_unlock_bh(&l->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/* ---- Socket interface ---- */
Al Viro8e036fc2007-07-29 00:16:36 -0700534static struct sock *__l2cap_get_sock_by_addr(__le16 psm, bdaddr_t *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct sock *sk;
537 struct hlist_node *node;
538 sk_for_each(sk, node, &l2cap_sk_list.head)
539 if (l2cap_pi(sk)->sport == psm && !bacmp(&bt_sk(sk)->src, src))
540 goto found;
541 sk = NULL;
542found:
543 return sk;
544}
545
546/* Find socket with psm and source bdaddr.
547 * Returns closest match.
548 */
Al Viro8e036fc2007-07-29 00:16:36 -0700549static struct sock *__l2cap_get_sock_by_psm(int state, __le16 psm, bdaddr_t *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct sock *sk = NULL, *sk1 = NULL;
552 struct hlist_node *node;
553
554 sk_for_each(sk, node, &l2cap_sk_list.head) {
555 if (state && sk->sk_state != state)
556 continue;
557
558 if (l2cap_pi(sk)->psm == psm) {
559 /* Exact match. */
560 if (!bacmp(&bt_sk(sk)->src, src))
561 break;
562
563 /* Closest match */
564 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
565 sk1 = sk;
566 }
567 }
568 return node ? sk : sk1;
569}
570
571/* Find socket with given address (psm, src).
572 * Returns locked socket */
Al Viro8e036fc2007-07-29 00:16:36 -0700573static inline struct sock *l2cap_get_sock_by_psm(int state, __le16 psm, bdaddr_t *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct sock *s;
576 read_lock(&l2cap_sk_list.lock);
577 s = __l2cap_get_sock_by_psm(state, psm, src);
578 if (s) bh_lock_sock(s);
579 read_unlock(&l2cap_sk_list.lock);
580 return s;
581}
582
583static void l2cap_sock_destruct(struct sock *sk)
584{
585 BT_DBG("sk %p", sk);
586
587 skb_queue_purge(&sk->sk_receive_queue);
588 skb_queue_purge(&sk->sk_write_queue);
589}
590
591static void l2cap_sock_cleanup_listen(struct sock *parent)
592{
593 struct sock *sk;
594
595 BT_DBG("parent %p", parent);
596
597 /* Close not yet accepted channels */
598 while ((sk = bt_accept_dequeue(parent, NULL)))
599 l2cap_sock_close(sk);
600
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200601 parent->sk_state = BT_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 sock_set_flag(parent, SOCK_ZAPPED);
603}
604
605/* Kill socket (only if zapped and orphan)
606 * Must be called on unlocked socket.
607 */
608static void l2cap_sock_kill(struct sock *sk)
609{
610 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
611 return;
612
613 BT_DBG("sk %p state %d", sk, sk->sk_state);
614
615 /* Kill poor orphan */
616 bt_sock_unlink(&l2cap_sk_list, sk);
617 sock_set_flag(sk, SOCK_DEAD);
618 sock_put(sk);
619}
620
621static void __l2cap_sock_close(struct sock *sk, int reason)
622{
623 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
624
625 switch (sk->sk_state) {
626 case BT_LISTEN:
627 l2cap_sock_cleanup_listen(sk);
628 break;
629
630 case BT_CONNECTED:
631 case BT_CONFIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (sk->sk_type == SOCK_SEQPACKET) {
633 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
634 struct l2cap_disconn_req req;
635
636 sk->sk_state = BT_DISCONN;
637 l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
638
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700639 req.dcid = cpu_to_le16(l2cap_pi(sk)->dcid);
640 req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 l2cap_send_cmd(conn, l2cap_get_ident(conn),
642 L2CAP_DISCONN_REQ, sizeof(req), &req);
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200643 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 l2cap_chan_del(sk, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646
Marcel Holtmannf66dc812009-01-15 21:57:00 +0100647 case BT_CONNECT2:
648 if (sk->sk_type == SOCK_SEQPACKET) {
649 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
650 struct l2cap_conn_rsp rsp;
651 __u16 result;
652
653 if (bt_sk(sk)->defer_setup)
654 result = L2CAP_CR_SEC_BLOCK;
655 else
656 result = L2CAP_CR_BAD_PSM;
657
658 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
659 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
660 rsp.result = cpu_to_le16(result);
661 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
662 l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
663 L2CAP_CONN_RSP, sizeof(rsp), &rsp);
664 } else
665 l2cap_chan_del(sk, reason);
666 break;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 case BT_CONNECT:
669 case BT_DISCONN:
670 l2cap_chan_del(sk, reason);
671 break;
672
673 default:
674 sock_set_flag(sk, SOCK_ZAPPED);
675 break;
676 }
677}
678
679/* Must be called on unlocked socket. */
680static void l2cap_sock_close(struct sock *sk)
681{
682 l2cap_sock_clear_timer(sk);
683 lock_sock(sk);
684 __l2cap_sock_close(sk, ECONNRESET);
685 release_sock(sk);
686 l2cap_sock_kill(sk);
687}
688
689static void l2cap_sock_init(struct sock *sk, struct sock *parent)
690{
691 struct l2cap_pinfo *pi = l2cap_pi(sk);
692
693 BT_DBG("sk %p", sk);
694
695 if (parent) {
696 sk->sk_type = parent->sk_type;
Marcel Holtmannf66dc812009-01-15 21:57:00 +0100697 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 pi->imtu = l2cap_pi(parent)->imtu;
700 pi->omtu = l2cap_pi(parent)->omtu;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100701 pi->sec_level = l2cap_pi(parent)->sec_level;
702 pi->role_switch = l2cap_pi(parent)->role_switch;
703 pi->force_reliable = l2cap_pi(parent)->force_reliable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 } else {
705 pi->imtu = L2CAP_DEFAULT_MTU;
706 pi->omtu = 0;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100707 pi->sec_level = BT_SECURITY_LOW;
708 pi->role_switch = 0;
709 pi->force_reliable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
712 /* Default config options */
Marcel Holtmann5dee9e72007-05-24 14:27:19 +0200713 pi->conf_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 pi->flush_to = L2CAP_DEFAULT_FLUSH_TO;
715}
716
717static struct proto l2cap_proto = {
718 .name = "L2CAP",
719 .owner = THIS_MODULE,
720 .obj_size = sizeof(struct l2cap_pinfo)
721};
722
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700723static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 struct sock *sk;
726
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700727 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (!sk)
729 return NULL;
730
731 sock_init_data(sock, sk);
732 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
733
734 sk->sk_destruct = l2cap_sock_destruct;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200735 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 sock_reset_flag(sk, SOCK_ZAPPED);
738
739 sk->sk_protocol = proto;
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200740 sk->sk_state = BT_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Marcel Holtmannb1235d7962008-07-14 20:13:54 +0200742 setup_timer(&sk->sk_timer, l2cap_sock_timeout, (unsigned long) sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 bt_sock_link(&l2cap_sk_list, sk);
745 return sk;
746}
747
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700748static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 struct sock *sk;
751
752 BT_DBG("sock %p", sock);
753
754 sock->state = SS_UNCONNECTED;
755
756 if (sock->type != SOCK_SEQPACKET &&
757 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
758 return -ESOCKTNOSUPPORT;
759
760 if (sock->type == SOCK_RAW && !capable(CAP_NET_RAW))
761 return -EPERM;
762
763 sock->ops = &l2cap_sock_ops;
764
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700765 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (!sk)
767 return -ENOMEM;
768
769 l2cap_sock_init(sk, NULL);
770 return 0;
771}
772
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100773static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct sock *sk = sock->sk;
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100776 struct sockaddr_l2 la;
777 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100779 BT_DBG("sk %p", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (!addr || addr->sa_family != AF_BLUETOOTH)
782 return -EINVAL;
783
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100784 memset(&la, 0, sizeof(la));
785 len = min_t(unsigned int, sizeof(la), alen);
786 memcpy(&la, addr, len);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 lock_sock(sk);
789
790 if (sk->sk_state != BT_OPEN) {
791 err = -EBADFD;
792 goto done;
793 }
794
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100795 if (la.l2_psm && btohs(la.l2_psm) < 0x1001 &&
Marcel Holtmann847641d2007-01-22 22:00:45 +0100796 !capable(CAP_NET_BIND_SERVICE)) {
797 err = -EACCES;
798 goto done;
799 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 write_lock_bh(&l2cap_sk_list.lock);
802
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100803 if (la.l2_psm && __l2cap_get_sock_by_addr(la.l2_psm, &la.l2_bdaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 err = -EADDRINUSE;
805 } else {
806 /* Save source address */
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100807 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
808 l2cap_pi(sk)->psm = la.l2_psm;
809 l2cap_pi(sk)->sport = la.l2_psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 sk->sk_state = BT_BOUND;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100811
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100812 if (btohs(la.l2_psm) == 0x0001 || btohs(la.l2_psm) == 0x0003)
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100813 l2cap_pi(sk)->sec_level = BT_SECURITY_SDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
816 write_unlock_bh(&l2cap_sk_list.lock);
817
818done:
819 release_sock(sk);
820 return err;
821}
822
823static int l2cap_do_connect(struct sock *sk)
824{
825 bdaddr_t *src = &bt_sk(sk)->src;
826 bdaddr_t *dst = &bt_sk(sk)->dst;
827 struct l2cap_conn *conn;
828 struct hci_conn *hcon;
829 struct hci_dev *hdev;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200830 __u8 auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 int err = 0;
832
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100833 BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst),
834 l2cap_pi(sk)->psm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (!(hdev = hci_get_route(dst, src)))
837 return -EHOSTUNREACH;
838
839 hci_dev_lock_bh(hdev);
840
841 err = -ENOMEM;
842
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100843 if (sk->sk_type == SOCK_RAW) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100844 switch (l2cap_pi(sk)->sec_level) {
845 case BT_SECURITY_HIGH:
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100846 auth_type = HCI_AT_DEDICATED_BONDING_MITM;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100847 break;
848 case BT_SECURITY_MEDIUM:
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100849 auth_type = HCI_AT_DEDICATED_BONDING;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100850 break;
851 default:
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100852 auth_type = HCI_AT_NO_BONDING;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100853 break;
854 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100855 } else if (l2cap_pi(sk)->psm == cpu_to_le16(0x0001)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100856 if (l2cap_pi(sk)->sec_level == BT_SECURITY_HIGH)
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200857 auth_type = HCI_AT_NO_BONDING_MITM;
858 else
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200859 auth_type = HCI_AT_NO_BONDING;
Marcel Holtmann435fef22009-02-09 03:55:28 +0100860
861 if (l2cap_pi(sk)->sec_level == BT_SECURITY_LOW)
862 l2cap_pi(sk)->sec_level = BT_SECURITY_SDP;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100863 } else {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100864 switch (l2cap_pi(sk)->sec_level) {
865 case BT_SECURITY_HIGH:
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100866 auth_type = HCI_AT_GENERAL_BONDING_MITM;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100867 break;
868 case BT_SECURITY_MEDIUM:
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200869 auth_type = HCI_AT_GENERAL_BONDING;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100870 break;
871 default:
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100872 auth_type = HCI_AT_NO_BONDING;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100873 break;
874 }
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200875 }
876
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +0100877 hcon = hci_connect(hdev, ACL_LINK, dst,
878 l2cap_pi(sk)->sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (!hcon)
880 goto done;
881
882 conn = l2cap_conn_add(hcon, 0);
883 if (!conn) {
884 hci_conn_put(hcon);
885 goto done;
886 }
887
888 err = 0;
889
890 /* Update source addr of the socket */
891 bacpy(src, conn->src);
892
893 l2cap_chan_add(conn, sk, NULL);
894
895 sk->sk_state = BT_CONNECT;
896 l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
897
898 if (hcon->state == BT_CONNECTED) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200899 if (sk->sk_type != SOCK_SEQPACKET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 l2cap_sock_clear_timer(sk);
901 sk->sk_state = BT_CONNECTED;
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200902 } else
903 l2cap_do_start(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
906done:
907 hci_dev_unlock_bh(hdev);
908 hci_dev_put(hdev);
909 return err;
910}
911
912static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
913{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct sock *sk = sock->sk;
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100915 struct sockaddr_l2 la;
916 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 lock_sock(sk);
919
920 BT_DBG("sk %p", sk);
921
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100922 if (!addr || addr->sa_family != AF_BLUETOOTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 err = -EINVAL;
924 goto done;
925 }
926
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100927 memset(&la, 0, sizeof(la));
928 len = min_t(unsigned int, sizeof(la), alen);
929 memcpy(&la, addr, len);
930
931 if (sk->sk_type == SOCK_SEQPACKET && !la.l2_psm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 err = -EINVAL;
933 goto done;
934 }
935
936 switch(sk->sk_state) {
937 case BT_CONNECT:
938 case BT_CONNECT2:
939 case BT_CONFIG:
940 /* Already connecting */
941 goto wait;
942
943 case BT_CONNECTED:
944 /* Already connected */
945 goto done;
946
947 case BT_OPEN:
948 case BT_BOUND:
949 /* Can connect */
950 break;
951
952 default:
953 err = -EBADFD;
954 goto done;
955 }
956
957 /* Set destination address and psm */
Marcel Holtmannf29972d2009-02-12 05:07:45 +0100958 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
959 l2cap_pi(sk)->psm = la.l2_psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if ((err = l2cap_do_connect(sk)))
962 goto done;
963
964wait:
965 err = bt_sock_wait_state(sk, BT_CONNECTED,
966 sock_sndtimeo(sk, flags & O_NONBLOCK));
967done:
968 release_sock(sk);
969 return err;
970}
971
972static int l2cap_sock_listen(struct socket *sock, int backlog)
973{
974 struct sock *sk = sock->sk;
975 int err = 0;
976
977 BT_DBG("sk %p backlog %d", sk, backlog);
978
979 lock_sock(sk);
980
981 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
982 err = -EBADFD;
983 goto done;
984 }
985
986 if (!l2cap_pi(sk)->psm) {
987 bdaddr_t *src = &bt_sk(sk)->src;
988 u16 psm;
989
990 err = -EINVAL;
991
992 write_lock_bh(&l2cap_sk_list.lock);
993
994 for (psm = 0x1001; psm < 0x1100; psm += 2)
Al Viro6dc0c202007-07-29 00:15:18 -0700995 if (!__l2cap_get_sock_by_addr(htobs(psm), src)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 l2cap_pi(sk)->psm = htobs(psm);
997 l2cap_pi(sk)->sport = htobs(psm);
998 err = 0;
999 break;
1000 }
1001
1002 write_unlock_bh(&l2cap_sk_list.lock);
1003
1004 if (err < 0)
1005 goto done;
1006 }
1007
1008 sk->sk_max_ack_backlog = backlog;
1009 sk->sk_ack_backlog = 0;
1010 sk->sk_state = BT_LISTEN;
1011
1012done:
1013 release_sock(sk);
1014 return err;
1015}
1016
1017static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
1018{
1019 DECLARE_WAITQUEUE(wait, current);
1020 struct sock *sk = sock->sk, *nsk;
1021 long timeo;
1022 int err = 0;
1023
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08001024 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (sk->sk_state != BT_LISTEN) {
1027 err = -EBADFD;
1028 goto done;
1029 }
1030
1031 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1032
1033 BT_DBG("sk %p timeo %ld", sk, timeo);
1034
1035 /* Wait for an incoming connection. (wake-one). */
1036 add_wait_queue_exclusive(sk->sk_sleep, &wait);
1037 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
1038 set_current_state(TASK_INTERRUPTIBLE);
1039 if (!timeo) {
1040 err = -EAGAIN;
1041 break;
1042 }
1043
1044 release_sock(sk);
1045 timeo = schedule_timeout(timeo);
Peter Zijlstrafcc70d52006-11-08 22:44:35 -08001046 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 if (sk->sk_state != BT_LISTEN) {
1049 err = -EBADFD;
1050 break;
1051 }
1052
1053 if (signal_pending(current)) {
1054 err = sock_intr_errno(timeo);
1055 break;
1056 }
1057 }
1058 set_current_state(TASK_RUNNING);
1059 remove_wait_queue(sk->sk_sleep, &wait);
1060
1061 if (err)
1062 goto done;
1063
1064 newsock->state = SS_CONNECTED;
1065
1066 BT_DBG("new socket %p", nsk);
1067
1068done:
1069 release_sock(sk);
1070 return err;
1071}
1072
1073static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
1074{
1075 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
1076 struct sock *sk = sock->sk;
1077
1078 BT_DBG("sock %p, sk %p", sock, sk);
1079
1080 addr->sa_family = AF_BLUETOOTH;
1081 *len = sizeof(struct sockaddr_l2);
1082
Marcel Holtmannf29972d2009-02-12 05:07:45 +01001083 if (peer) {
1084 la->l2_psm = l2cap_pi(sk)->psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Marcel Holtmannf29972d2009-02-12 05:07:45 +01001086 la->l2_cid = htobs(l2cap_pi(sk)->dcid);
1087 } else {
1088 la->l2_psm = l2cap_pi(sk)->sport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Marcel Holtmannf29972d2009-02-12 05:07:45 +01001090 la->l2_cid = htobs(l2cap_pi(sk)->scid);
1091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return 0;
1094}
1095
1096static inline int l2cap_do_send(struct sock *sk, struct msghdr *msg, int len)
1097{
1098 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
1099 struct sk_buff *skb, **frag;
1100 int err, hlen, count, sent=0;
1101 struct l2cap_hdr *lh;
1102
1103 BT_DBG("sk %p len %d", sk, len);
1104
1105 /* First fragment (with L2CAP header) */
1106 if (sk->sk_type == SOCK_DGRAM)
1107 hlen = L2CAP_HDR_SIZE + 2;
1108 else
1109 hlen = L2CAP_HDR_SIZE;
1110
1111 count = min_t(unsigned int, (conn->mtu - hlen), len);
1112
1113 skb = bt_skb_send_alloc(sk, hlen + count,
1114 msg->msg_flags & MSG_DONTWAIT, &err);
1115 if (!skb)
1116 return err;
1117
1118 /* Create L2CAP header */
1119 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001120 lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid);
1121 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 if (sk->sk_type == SOCK_DGRAM)
Al Viro8e036fc2007-07-29 00:16:36 -07001124 put_unaligned(l2cap_pi(sk)->psm, (__le16 *) skb_put(skb, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
1127 err = -EFAULT;
1128 goto fail;
1129 }
1130
1131 sent += count;
1132 len -= count;
1133
1134 /* Continuation fragments (no L2CAP header) */
1135 frag = &skb_shinfo(skb)->frag_list;
1136 while (len) {
1137 count = min_t(unsigned int, conn->mtu, len);
1138
1139 *frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err);
1140 if (!*frag)
1141 goto fail;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) {
1144 err = -EFAULT;
1145 goto fail;
1146 }
1147
1148 sent += count;
1149 len -= count;
1150
1151 frag = &(*frag)->next;
1152 }
1153
1154 if ((err = hci_send_acl(conn->hcon, skb, 0)) < 0)
1155 goto fail;
1156
1157 return sent;
1158
1159fail:
1160 kfree_skb(skb);
1161 return err;
1162}
1163
1164static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
1165{
1166 struct sock *sk = sock->sk;
1167 int err = 0;
1168
1169 BT_DBG("sock %p, sk %p", sock, sk);
1170
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -08001171 err = sock_error(sk);
1172 if (err)
1173 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 if (msg->msg_flags & MSG_OOB)
1176 return -EOPNOTSUPP;
1177
1178 /* Check outgoing MTU */
1179 if (sk->sk_type != SOCK_RAW && len > l2cap_pi(sk)->omtu)
1180 return -EINVAL;
1181
1182 lock_sock(sk);
1183
1184 if (sk->sk_state == BT_CONNECTED)
1185 err = l2cap_do_send(sk, msg, len);
1186 else
1187 err = -ENOTCONN;
1188
1189 release_sock(sk);
1190 return err;
1191}
1192
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001193static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
1194{
1195 struct sock *sk = sock->sk;
1196
1197 lock_sock(sk);
1198
1199 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
1200 struct l2cap_conn_rsp rsp;
1201
1202 sk->sk_state = BT_CONFIG;
1203
1204 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
1205 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
1206 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
1207 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
1208 l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident,
1209 L2CAP_CONN_RSP, sizeof(rsp), &rsp);
1210
1211 release_sock(sk);
1212 return 0;
1213 }
1214
1215 release_sock(sk);
1216
1217 return bt_sock_recvmsg(iocb, sock, msg, len, flags);
1218}
1219
Marcel Holtmannd58daf42009-01-15 21:52:14 +01001220static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 struct sock *sk = sock->sk;
1223 struct l2cap_options opts;
Marcel Holtmannf29972d2009-02-12 05:07:45 +01001224 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 u32 opt;
1226
1227 BT_DBG("sk %p", sk);
1228
1229 lock_sock(sk);
1230
1231 switch (optname) {
1232 case L2CAP_OPTIONS:
Marcel Holtmann0878b662007-05-05 00:35:59 +02001233 opts.imtu = l2cap_pi(sk)->imtu;
1234 opts.omtu = l2cap_pi(sk)->omtu;
1235 opts.flush_to = l2cap_pi(sk)->flush_to;
Marcel Holtmann6464f352007-10-20 13:39:51 +02001236 opts.mode = L2CAP_MODE_BASIC;
Marcel Holtmann0878b662007-05-05 00:35:59 +02001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 len = min_t(unsigned int, sizeof(opts), optlen);
1239 if (copy_from_user((char *) &opts, optval, len)) {
1240 err = -EFAULT;
1241 break;
1242 }
Marcel Holtmann0878b662007-05-05 00:35:59 +02001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 l2cap_pi(sk)->imtu = opts.imtu;
1245 l2cap_pi(sk)->omtu = opts.omtu;
1246 break;
1247
1248 case L2CAP_LM:
1249 if (get_user(opt, (u32 __user *) optval)) {
1250 err = -EFAULT;
1251 break;
1252 }
1253
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01001254 if (opt & L2CAP_LM_AUTH)
1255 l2cap_pi(sk)->sec_level = BT_SECURITY_LOW;
1256 if (opt & L2CAP_LM_ENCRYPT)
1257 l2cap_pi(sk)->sec_level = BT_SECURITY_MEDIUM;
1258 if (opt & L2CAP_LM_SECURE)
1259 l2cap_pi(sk)->sec_level = BT_SECURITY_HIGH;
1260
1261 l2cap_pi(sk)->role_switch = (opt & L2CAP_LM_MASTER);
1262 l2cap_pi(sk)->force_reliable = (opt & L2CAP_LM_RELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 break;
1264
1265 default:
1266 err = -ENOPROTOOPT;
1267 break;
1268 }
1269
1270 release_sock(sk);
1271 return err;
1272}
1273
Marcel Holtmannd58daf42009-01-15 21:52:14 +01001274static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
1275{
1276 struct sock *sk = sock->sk;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01001277 struct bt_security sec;
1278 int len, err = 0;
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001279 u32 opt;
Marcel Holtmannd58daf42009-01-15 21:52:14 +01001280
1281 BT_DBG("sk %p", sk);
1282
1283 if (level == SOL_L2CAP)
1284 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
1285
Marcel Holtmann0588d942009-01-16 10:06:13 +01001286 if (level != SOL_BLUETOOTH)
1287 return -ENOPROTOOPT;
1288
Marcel Holtmannd58daf42009-01-15 21:52:14 +01001289 lock_sock(sk);
1290
1291 switch (optname) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01001292 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +01001293 if (sk->sk_type != SOCK_SEQPACKET) {
1294 err = -EINVAL;
1295 break;
1296 }
1297
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01001298 sec.level = BT_SECURITY_LOW;
1299
1300 len = min_t(unsigned int, sizeof(sec), optlen);
1301 if (copy_from_user((char *) &sec, optval, len)) {
1302 err = -EFAULT;
1303 break;
1304 }
1305
1306 if (sec.level < BT_SECURITY_LOW ||
1307 sec.level > BT_SECURITY_HIGH) {
1308 err = -EINVAL;
1309 break;
1310 }
1311
1312 l2cap_pi(sk)->sec_level = sec.level;
1313 break;
1314
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001315 case BT_DEFER_SETUP:
1316 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1317 err = -EINVAL;
1318 break;
1319 }
1320
1321 if (get_user(opt, (u32 __user *) optval)) {
1322 err = -EFAULT;
1323 break;
1324 }
1325
1326 bt_sk(sk)->defer_setup = opt;
1327 break;
1328
Marcel Holtmannd58daf42009-01-15 21:52:14 +01001329 default:
1330 err = -ENOPROTOOPT;
1331 break;
1332 }
1333
1334 release_sock(sk);
1335 return err;
1336}
1337
1338static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct sock *sk = sock->sk;
1341 struct l2cap_options opts;
1342 struct l2cap_conninfo cinfo;
1343 int len, err = 0;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01001344 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 BT_DBG("sk %p", sk);
1347
1348 if (get_user(len, optlen))
1349 return -EFAULT;
1350
1351 lock_sock(sk);
1352
1353 switch (optname) {
1354 case L2CAP_OPTIONS:
1355 opts.imtu = l2cap_pi(sk)->imtu;
1356 opts.omtu = l2cap_pi(sk)->omtu;
1357 opts.flush_to = l2cap_pi(sk)->flush_to;
Marcel Holtmann6464f352007-10-20 13:39:51 +02001358 opts.mode = L2CAP_MODE_BASIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 len = min_t(unsigned int, len, sizeof(opts));
1361 if (copy_to_user(optval, (char *) &opts, len))
1362 err = -EFAULT;
1363
1364 break;
1365
1366 case L2CAP_LM:
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01001367 switch (l2cap_pi(sk)->sec_level) {
1368 case BT_SECURITY_LOW:
1369 opt = L2CAP_LM_AUTH;
1370 break;
1371 case BT_SECURITY_MEDIUM:
1372 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
1373 break;
1374 case BT_SECURITY_HIGH:
1375 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
1376 L2CAP_LM_SECURE;
1377 break;
1378 default:
1379 opt = 0;
1380 break;
1381 }
1382
1383 if (l2cap_pi(sk)->role_switch)
1384 opt |= L2CAP_LM_MASTER;
1385
1386 if (l2cap_pi(sk)->force_reliable)
1387 opt |= L2CAP_LM_RELIABLE;
1388
1389 if (put_user(opt, (u32 __user *) optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 err = -EFAULT;
1391 break;
1392
1393 case L2CAP_CONNINFO:
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001394 if (sk->sk_state != BT_CONNECTED &&
1395 !(sk->sk_state == BT_CONNECT2 &&
1396 bt_sk(sk)->defer_setup)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 err = -ENOTCONN;
1398 break;
1399 }
1400
1401 cinfo.hci_handle = l2cap_pi(sk)->conn->hcon->handle;
1402 memcpy(cinfo.dev_class, l2cap_pi(sk)->conn->hcon->dev_class, 3);
1403
1404 len = min_t(unsigned int, len, sizeof(cinfo));
1405 if (copy_to_user(optval, (char *) &cinfo, len))
1406 err = -EFAULT;
1407
1408 break;
1409
1410 default:
1411 err = -ENOPROTOOPT;
1412 break;
1413 }
1414
1415 release_sock(sk);
1416 return err;
1417}
1418
Marcel Holtmannd58daf42009-01-15 21:52:14 +01001419static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
1420{
1421 struct sock *sk = sock->sk;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01001422 struct bt_security sec;
Marcel Holtmannd58daf42009-01-15 21:52:14 +01001423 int len, err = 0;
1424
1425 BT_DBG("sk %p", sk);
1426
1427 if (level == SOL_L2CAP)
1428 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
1429
Marcel Holtmann0588d942009-01-16 10:06:13 +01001430 if (level != SOL_BLUETOOTH)
1431 return -ENOPROTOOPT;
1432
Marcel Holtmannd58daf42009-01-15 21:52:14 +01001433 if (get_user(len, optlen))
1434 return -EFAULT;
1435
1436 lock_sock(sk);
1437
1438 switch (optname) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01001439 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +01001440 if (sk->sk_type != SOCK_SEQPACKET) {
1441 err = -EINVAL;
1442 break;
1443 }
1444
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01001445 sec.level = l2cap_pi(sk)->sec_level;
1446
1447 len = min_t(unsigned int, len, sizeof(sec));
1448 if (copy_to_user(optval, (char *) &sec, len))
1449 err = -EFAULT;
1450
1451 break;
1452
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001453 case BT_DEFER_SETUP:
1454 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1455 err = -EINVAL;
1456 break;
1457 }
1458
1459 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
1460 err = -EFAULT;
1461
1462 break;
1463
Marcel Holtmannd58daf42009-01-15 21:52:14 +01001464 default:
1465 err = -ENOPROTOOPT;
1466 break;
1467 }
1468
1469 release_sock(sk);
1470 return err;
1471}
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473static int l2cap_sock_shutdown(struct socket *sock, int how)
1474{
1475 struct sock *sk = sock->sk;
1476 int err = 0;
1477
1478 BT_DBG("sock %p, sk %p", sock, sk);
1479
1480 if (!sk)
1481 return 0;
1482
1483 lock_sock(sk);
1484 if (!sk->sk_shutdown) {
1485 sk->sk_shutdown = SHUTDOWN_MASK;
1486 l2cap_sock_clear_timer(sk);
1487 __l2cap_sock_close(sk, 0);
1488
1489 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
Marcel Holtmannb1235d7962008-07-14 20:13:54 +02001490 err = bt_sock_wait_state(sk, BT_CLOSED,
1491 sk->sk_lingertime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493 release_sock(sk);
1494 return err;
1495}
1496
1497static int l2cap_sock_release(struct socket *sock)
1498{
1499 struct sock *sk = sock->sk;
1500 int err;
1501
1502 BT_DBG("sock %p, sk %p", sock, sk);
1503
1504 if (!sk)
1505 return 0;
1506
1507 err = l2cap_sock_shutdown(sock, 2);
1508
1509 sock_orphan(sk);
1510 l2cap_sock_kill(sk);
1511 return err;
1512}
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514static void l2cap_chan_ready(struct sock *sk)
1515{
1516 struct sock *parent = bt_sk(sk)->parent;
1517
1518 BT_DBG("sk %p, parent %p", sk, parent);
1519
1520 l2cap_pi(sk)->conf_state = 0;
1521 l2cap_sock_clear_timer(sk);
1522
1523 if (!parent) {
1524 /* Outgoing channel.
1525 * Wake up socket sleeping on connect.
1526 */
1527 sk->sk_state = BT_CONNECTED;
1528 sk->sk_state_change(sk);
1529 } else {
1530 /* Incoming channel.
1531 * Wake up socket sleeping on accept.
1532 */
1533 parent->sk_data_ready(parent, 0);
1534 }
1535}
1536
1537/* Copy frame to all raw sockets on that connection */
1538static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
1539{
1540 struct l2cap_chan_list *l = &conn->chan_list;
1541 struct sk_buff *nskb;
1542 struct sock * sk;
1543
1544 BT_DBG("conn %p", conn);
1545
1546 read_lock(&l->lock);
1547 for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
1548 if (sk->sk_type != SOCK_RAW)
1549 continue;
1550
1551 /* Don't send frame to the socket it came from */
1552 if (skb->sk == sk)
1553 continue;
1554
1555 if (!(nskb = skb_clone(skb, GFP_ATOMIC)))
1556 continue;
1557
1558 if (sock_queue_rcv_skb(sk, nskb))
1559 kfree_skb(nskb);
1560 }
1561 read_unlock(&l->lock);
1562}
1563
1564/* ---- L2CAP signalling commands ---- */
1565static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
1566 u8 code, u8 ident, u16 dlen, void *data)
1567{
1568 struct sk_buff *skb, **frag;
1569 struct l2cap_cmd_hdr *cmd;
1570 struct l2cap_hdr *lh;
1571 int len, count;
1572
1573 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", conn, code, ident, dlen);
1574
1575 len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
1576 count = min_t(unsigned int, conn->mtu, len);
1577
1578 skb = bt_skb_alloc(count, GFP_ATOMIC);
1579 if (!skb)
1580 return NULL;
1581
1582 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001583 lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
1584 lh->cid = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
1587 cmd->code = code;
1588 cmd->ident = ident;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001589 cmd->len = cpu_to_le16(dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 if (dlen) {
1592 count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
1593 memcpy(skb_put(skb, count), data, count);
1594 data += count;
1595 }
1596
1597 len -= skb->len;
1598
1599 /* Continuation fragments (no L2CAP header) */
1600 frag = &skb_shinfo(skb)->frag_list;
1601 while (len) {
1602 count = min_t(unsigned int, conn->mtu, len);
1603
1604 *frag = bt_skb_alloc(count, GFP_ATOMIC);
1605 if (!*frag)
1606 goto fail;
1607
1608 memcpy(skb_put(*frag, count), data, count);
1609
1610 len -= count;
1611 data += count;
1612
1613 frag = &(*frag)->next;
1614 }
1615
1616 return skb;
1617
1618fail:
1619 kfree_skb(skb);
1620 return NULL;
1621}
1622
1623static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
1624{
1625 struct l2cap_conf_opt *opt = *ptr;
1626 int len;
1627
1628 len = L2CAP_CONF_OPT_SIZE + opt->len;
1629 *ptr += len;
1630
1631 *type = opt->type;
1632 *olen = opt->len;
1633
1634 switch (opt->len) {
1635 case 1:
1636 *val = *((u8 *) opt->val);
1637 break;
1638
1639 case 2:
Marcel Holtmann861d6882007-10-20 13:37:06 +02001640 *val = __le16_to_cpu(*((__le16 *) opt->val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 break;
1642
1643 case 4:
Marcel Holtmann861d6882007-10-20 13:37:06 +02001644 *val = __le32_to_cpu(*((__le32 *) opt->val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 break;
1646
1647 default:
1648 *val = (unsigned long) opt->val;
1649 break;
1650 }
1651
1652 BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
1653 return len;
1654}
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
1657{
1658 struct l2cap_conf_opt *opt = *ptr;
1659
1660 BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);
1661
1662 opt->type = type;
1663 opt->len = len;
1664
1665 switch (len) {
1666 case 1:
1667 *((u8 *) opt->val) = val;
1668 break;
1669
1670 case 2:
Al Viro8e036fc2007-07-29 00:16:36 -07001671 *((__le16 *) opt->val) = cpu_to_le16(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 break;
1673
1674 case 4:
Al Viro8e036fc2007-07-29 00:16:36 -07001675 *((__le32 *) opt->val) = cpu_to_le32(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 break;
1677
1678 default:
1679 memcpy(opt->val, (void *) val, len);
1680 break;
1681 }
1682
1683 *ptr += L2CAP_CONF_OPT_SIZE + len;
1684}
1685
1686static int l2cap_build_conf_req(struct sock *sk, void *data)
1687{
1688 struct l2cap_pinfo *pi = l2cap_pi(sk);
1689 struct l2cap_conf_req *req = data;
1690 void *ptr = req->data;
1691
1692 BT_DBG("sk %p", sk);
1693
1694 if (pi->imtu != L2CAP_DEFAULT_MTU)
1695 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->imtu);
1696
1697 /* FIXME: Need actual value of the flush timeout */
1698 //if (flush_to != L2CAP_DEFAULT_FLUSH_TO)
1699 // l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 2, pi->flush_to);
1700
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001701 req->dcid = cpu_to_le16(pi->dcid);
1702 req->flags = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 return ptr - data;
1705}
1706
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001707static int l2cap_parse_conf_req(struct sock *sk, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 struct l2cap_pinfo *pi = l2cap_pi(sk);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001710 struct l2cap_conf_rsp *rsp = data;
1711 void *ptr = rsp->data;
1712 void *req = pi->conf_req;
1713 int len = pi->conf_len;
1714 int type, hint, olen;
1715 unsigned long val;
Marcel Holtmann6464f352007-10-20 13:39:51 +02001716 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Marcel Holtmann861d6882007-10-20 13:37:06 +02001717 u16 mtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001718 u16 result = L2CAP_CONF_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001720 BT_DBG("sk %p", sk);
Marcel Holtmann820ae1b2006-11-18 22:15:00 +01001721
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001722 while (len >= L2CAP_CONF_OPT_SIZE) {
1723 len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001725 hint = type & 0x80;
1726 type &= 0x7f;
1727
1728 switch (type) {
1729 case L2CAP_CONF_MTU:
Marcel Holtmann861d6882007-10-20 13:37:06 +02001730 mtu = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001731 break;
1732
1733 case L2CAP_CONF_FLUSH_TO:
1734 pi->flush_to = val;
1735 break;
1736
1737 case L2CAP_CONF_QOS:
1738 break;
1739
Marcel Holtmann6464f352007-10-20 13:39:51 +02001740 case L2CAP_CONF_RFC:
1741 if (olen == sizeof(rfc))
1742 memcpy(&rfc, (void *) val, olen);
1743 break;
1744
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001745 default:
1746 if (hint)
1747 break;
1748
1749 result = L2CAP_CONF_UNKNOWN;
1750 *((u8 *) ptr++) = type;
1751 break;
1752 }
1753 }
1754
1755 if (result == L2CAP_CONF_SUCCESS) {
1756 /* Configure output options and let the other side know
1757 * which ones we don't like. */
1758
Marcel Holtmann6464f352007-10-20 13:39:51 +02001759 if (rfc.mode == L2CAP_MODE_BASIC) {
1760 if (mtu < pi->omtu)
1761 result = L2CAP_CONF_UNACCEPT;
1762 else {
1763 pi->omtu = mtu;
1764 pi->conf_state |= L2CAP_CONF_OUTPUT_DONE;
1765 }
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001766
Marcel Holtmann6464f352007-10-20 13:39:51 +02001767 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->omtu);
1768 } else {
1769 result = L2CAP_CONF_UNACCEPT;
1770
1771 memset(&rfc, 0, sizeof(rfc));
1772 rfc.mode = L2CAP_MODE_BASIC;
1773
1774 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
1775 sizeof(rfc), (unsigned long) &rfc);
1776 }
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001777 }
1778
1779 rsp->scid = cpu_to_le16(pi->dcid);
1780 rsp->result = cpu_to_le16(result);
1781 rsp->flags = cpu_to_le16(0x0000);
1782
1783 return ptr - data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001786static int l2cap_build_conf_rsp(struct sock *sk, void *data, u16 result, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 struct l2cap_conf_rsp *rsp = data;
1789 void *ptr = rsp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001791 BT_DBG("sk %p", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001793 rsp->scid = cpu_to_le16(l2cap_pi(sk)->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001794 rsp->result = cpu_to_le16(result);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001795 rsp->flags = cpu_to_le16(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 return ptr - data;
1798}
1799
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001800static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
1801{
1802 struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data;
1803
1804 if (rej->reason != 0x0000)
1805 return 0;
1806
1807 if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
1808 cmd->ident == conn->info_ident) {
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001809 del_timer(&conn->info_timer);
Marcel Holtmann984947d2009-02-06 23:35:19 +01001810
1811 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01001812 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01001813
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001814 l2cap_conn_start(conn);
1815 }
1816
1817 return 0;
1818}
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
1821{
1822 struct l2cap_chan_list *list = &conn->chan_list;
1823 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
1824 struct l2cap_conn_rsp rsp;
1825 struct sock *sk, *parent;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02001826 int result, status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 u16 dcid = 0, scid = __le16_to_cpu(req->scid);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02001829 __le16 psm = req->psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);
1832
1833 /* Check if we have socket listening on psm */
1834 parent = l2cap_get_sock_by_psm(BT_LISTEN, psm, conn->src);
1835 if (!parent) {
1836 result = L2CAP_CR_BAD_PSM;
1837 goto sendresp;
1838 }
1839
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02001840 /* Check if the ACL is secure enough (if not SDP) */
1841 if (psm != cpu_to_le16(0x0001) &&
1842 !hci_conn_check_link_mode(conn->hcon)) {
1843 result = L2CAP_CR_SEC_BLOCK;
1844 goto response;
1845 }
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 result = L2CAP_CR_NO_MEM;
1848
1849 /* Check for backlog size */
1850 if (sk_acceptq_is_full(parent)) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001851 BT_DBG("backlog full %d", parent->sk_ack_backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto response;
1853 }
1854
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001855 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (!sk)
1857 goto response;
1858
Marcel Holtmannfd1278d2006-07-12 23:00:07 +02001859 write_lock_bh(&list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 /* Check if we already have channel with that dcid */
1862 if (__l2cap_get_chan_by_dcid(list, scid)) {
Marcel Holtmannfd1278d2006-07-12 23:00:07 +02001863 write_unlock_bh(&list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 sock_set_flag(sk, SOCK_ZAPPED);
1865 l2cap_sock_kill(sk);
1866 goto response;
1867 }
1868
1869 hci_conn_hold(conn->hcon);
1870
1871 l2cap_sock_init(sk, parent);
1872 bacpy(&bt_sk(sk)->src, conn->src);
1873 bacpy(&bt_sk(sk)->dst, conn->dst);
1874 l2cap_pi(sk)->psm = psm;
1875 l2cap_pi(sk)->dcid = scid;
1876
1877 __l2cap_chan_add(conn, sk, parent);
1878 dcid = l2cap_pi(sk)->scid;
1879
1880 l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 l2cap_pi(sk)->ident = cmd->ident;
1883
Marcel Holtmann984947d2009-02-06 23:35:19 +01001884 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01001885 if (l2cap_check_security(sk)) {
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001886 if (bt_sk(sk)->defer_setup) {
1887 sk->sk_state = BT_CONNECT2;
1888 result = L2CAP_CR_PEND;
1889 status = L2CAP_CS_AUTHOR_PEND;
1890 parent->sk_data_ready(parent, 0);
1891 } else {
1892 sk->sk_state = BT_CONFIG;
1893 result = L2CAP_CR_SUCCESS;
1894 status = L2CAP_CS_NO_INFO;
1895 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001896 } else {
1897 sk->sk_state = BT_CONNECT2;
1898 result = L2CAP_CR_PEND;
1899 status = L2CAP_CS_AUTHEN_PEND;
1900 }
1901 } else {
1902 sk->sk_state = BT_CONNECT2;
1903 result = L2CAP_CR_PEND;
1904 status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906
Marcel Holtmannfd1278d2006-07-12 23:00:07 +02001907 write_unlock_bh(&list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909response:
1910 bh_unlock_sock(parent);
1911
1912sendresp:
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001913 rsp.scid = cpu_to_le16(scid);
1914 rsp.dcid = cpu_to_le16(dcid);
1915 rsp.result = cpu_to_le16(result);
1916 rsp.status = cpu_to_le16(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001918
1919 if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
1920 struct l2cap_info_req info;
1921 info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
1922
1923 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
1924 conn->info_ident = l2cap_get_ident(conn);
1925
1926 mod_timer(&conn->info_timer, jiffies +
1927 msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
1928
1929 l2cap_send_cmd(conn, conn->info_ident,
1930 L2CAP_INFO_REQ, sizeof(info), &info);
1931 }
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return 0;
1934}
1935
1936static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
1937{
1938 struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
1939 u16 scid, dcid, result, status;
1940 struct sock *sk;
1941 u8 req[128];
1942
1943 scid = __le16_to_cpu(rsp->scid);
1944 dcid = __le16_to_cpu(rsp->dcid);
1945 result = __le16_to_cpu(rsp->result);
1946 status = __le16_to_cpu(rsp->status);
1947
1948 BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);
1949
1950 if (scid) {
1951 if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
1952 return 0;
1953 } else {
1954 if (!(sk = l2cap_get_chan_by_ident(&conn->chan_list, cmd->ident)))
1955 return 0;
1956 }
1957
1958 switch (result) {
1959 case L2CAP_CR_SUCCESS:
1960 sk->sk_state = BT_CONFIG;
1961 l2cap_pi(sk)->ident = 0;
1962 l2cap_pi(sk)->dcid = dcid;
1963 l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
1964
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01001965 l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_CONNECT_PEND;
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
1968 l2cap_build_conf_req(sk, req), req);
1969 break;
1970
1971 case L2CAP_CR_PEND:
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01001972 l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 break;
1974
1975 default:
1976 l2cap_chan_del(sk, ECONNREFUSED);
1977 break;
1978 }
1979
1980 bh_unlock_sock(sk);
1981 return 0;
1982}
1983
Al Viro88219a02007-07-29 00:17:25 -07001984static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
1987 u16 dcid, flags;
1988 u8 rsp[64];
1989 struct sock *sk;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02001990 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 dcid = __le16_to_cpu(req->dcid);
1993 flags = __le16_to_cpu(req->flags);
1994
1995 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
1996
1997 if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid)))
1998 return -ENOENT;
1999
Marcel Holtmann354f60a2006-11-18 22:15:20 +01002000 if (sk->sk_state == BT_DISCONN)
2001 goto unlock;
2002
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02002003 /* Reject if config buffer is too small. */
Al Viro88219a02007-07-29 00:17:25 -07002004 len = cmd_len - sizeof(*req);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02002005 if (l2cap_pi(sk)->conf_len + len > sizeof(l2cap_pi(sk)->conf_req)) {
2006 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2007 l2cap_build_conf_rsp(sk, rsp,
2008 L2CAP_CONF_REJECT, flags), rsp);
2009 goto unlock;
2010 }
2011
2012 /* Store config. */
2013 memcpy(l2cap_pi(sk)->conf_req + l2cap_pi(sk)->conf_len, req->data, len);
2014 l2cap_pi(sk)->conf_len += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 if (flags & 0x0001) {
2017 /* Incomplete config. Send empty response. */
2018 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02002019 l2cap_build_conf_rsp(sk, rsp,
2020 L2CAP_CONF_SUCCESS, 0x0001), rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 goto unlock;
2022 }
2023
2024 /* Complete config. */
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02002025 len = l2cap_parse_conf_req(sk, rsp);
2026 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 goto unlock;
2028
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02002029 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
2030
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02002031 /* Reset config buffer. */
2032 l2cap_pi(sk)->conf_len = 0;
2033
Marcel Holtmann876d9482007-10-20 13:35:42 +02002034 if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_OUTPUT_DONE))
2035 goto unlock;
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (l2cap_pi(sk)->conf_state & L2CAP_CONF_INPUT_DONE) {
2038 sk->sk_state = BT_CONNECTED;
2039 l2cap_chan_ready(sk);
Marcel Holtmann876d9482007-10-20 13:35:42 +02002040 goto unlock;
2041 }
2042
2043 if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT)) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02002044 u8 buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Marcel Holtmann79d554a2008-07-14 20:13:44 +02002046 l2cap_build_conf_req(sk, buf), buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048
2049unlock:
2050 bh_unlock_sock(sk);
2051 return 0;
2052}
2053
2054static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
2055{
2056 struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
2057 u16 scid, flags, result;
2058 struct sock *sk;
2059
2060 scid = __le16_to_cpu(rsp->scid);
2061 flags = __le16_to_cpu(rsp->flags);
2062 result = __le16_to_cpu(rsp->result);
2063
2064 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", scid, flags, result);
2065
2066 if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
2067 return 0;
2068
2069 switch (result) {
2070 case L2CAP_CONF_SUCCESS:
2071 break;
2072
2073 case L2CAP_CONF_UNACCEPT:
2074 if (++l2cap_pi(sk)->conf_retry < L2CAP_CONF_MAX_RETRIES) {
2075 char req[128];
2076 /* It does not make sense to adjust L2CAP parameters
2077 * that are currently defined in the spec. We simply
2078 * resend config request that we sent earlier. It is
2079 * stupid, but it helps qualification testing which
2080 * expects at least some response from us. */
2081 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2082 l2cap_build_conf_req(sk, req), req);
2083 goto done;
2084 }
2085
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002086 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 sk->sk_state = BT_DISCONN;
Marcel Holtmannb1235d7962008-07-14 20:13:54 +02002088 sk->sk_err = ECONNRESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 l2cap_sock_set_timer(sk, HZ * 5);
2090 {
2091 struct l2cap_disconn_req req;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002092 req.dcid = cpu_to_le16(l2cap_pi(sk)->dcid);
2093 req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 l2cap_send_cmd(conn, l2cap_get_ident(conn),
2095 L2CAP_DISCONN_REQ, sizeof(req), &req);
2096 }
2097 goto done;
2098 }
2099
2100 if (flags & 0x01)
2101 goto done;
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 l2cap_pi(sk)->conf_state |= L2CAP_CONF_INPUT_DONE;
2104
2105 if (l2cap_pi(sk)->conf_state & L2CAP_CONF_OUTPUT_DONE) {
2106 sk->sk_state = BT_CONNECTED;
2107 l2cap_chan_ready(sk);
2108 }
2109
2110done:
2111 bh_unlock_sock(sk);
2112 return 0;
2113}
2114
2115static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
2116{
2117 struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
2118 struct l2cap_disconn_rsp rsp;
2119 u16 dcid, scid;
2120 struct sock *sk;
2121
2122 scid = __le16_to_cpu(req->scid);
2123 dcid = __le16_to_cpu(req->dcid);
2124
2125 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
2126
2127 if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid)))
2128 return 0;
2129
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002130 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
2131 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
2133
2134 sk->sk_shutdown = SHUTDOWN_MASK;
2135
2136 l2cap_chan_del(sk, ECONNRESET);
2137 bh_unlock_sock(sk);
2138
2139 l2cap_sock_kill(sk);
2140 return 0;
2141}
2142
2143static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
2144{
2145 struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
2146 u16 dcid, scid;
2147 struct sock *sk;
2148
2149 scid = __le16_to_cpu(rsp->scid);
2150 dcid = __le16_to_cpu(rsp->dcid);
2151
2152 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
2153
2154 if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
2155 return 0;
2156
2157 l2cap_chan_del(sk, 0);
2158 bh_unlock_sock(sk);
2159
2160 l2cap_sock_kill(sk);
2161 return 0;
2162}
2163
2164static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
2165{
2166 struct l2cap_info_req *req = (struct l2cap_info_req *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 u16 type;
2168
2169 type = __le16_to_cpu(req->type);
2170
2171 BT_DBG("type 0x%4.4x", type);
2172
Marcel Holtmannf0709e02007-10-20 13:38:51 +02002173 if (type == L2CAP_IT_FEAT_MASK) {
2174 u8 buf[8];
2175 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
2176 rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
2177 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
2178 put_unaligned(cpu_to_le32(l2cap_feat_mask), (__le32 *) rsp->data);
2179 l2cap_send_cmd(conn, cmd->ident,
2180 L2CAP_INFO_RSP, sizeof(buf), buf);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01002181 } else if (type == L2CAP_IT_FIXED_CHAN) {
2182 u8 buf[12];
2183 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
2184 rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
2185 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
2186 memcpy(buf + 4, l2cap_fixed_chan, 8);
2187 l2cap_send_cmd(conn, cmd->ident,
2188 L2CAP_INFO_RSP, sizeof(buf), buf);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02002189 } else {
2190 struct l2cap_info_rsp rsp;
2191 rsp.type = cpu_to_le16(type);
2192 rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
2193 l2cap_send_cmd(conn, cmd->ident,
2194 L2CAP_INFO_RSP, sizeof(rsp), &rsp);
2195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 return 0;
2198}
2199
2200static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
2201{
2202 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
2203 u16 type, result;
2204
2205 type = __le16_to_cpu(rsp->type);
2206 result = __le16_to_cpu(rsp->result);
2207
2208 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
2209
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02002210 del_timer(&conn->info_timer);
2211
Marcel Holtmann984947d2009-02-06 23:35:19 +01002212 if (type == L2CAP_IT_FEAT_MASK) {
Harvey Harrison83985312008-05-02 16:25:46 -07002213 conn->feat_mask = get_unaligned_le32(rsp->data);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02002214
Marcel Holtmanne1027a72009-02-09 09:18:02 +01002215 if (conn->feat_mask & 0x0080) {
2216 struct l2cap_info_req req;
2217 req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
2218
2219 conn->info_ident = l2cap_get_ident(conn);
2220
2221 l2cap_send_cmd(conn, conn->info_ident,
2222 L2CAP_INFO_REQ, sizeof(req), &req);
2223 } else {
2224 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
2225 conn->info_ident = 0;
2226
2227 l2cap_conn_start(conn);
2228 }
2229 } else if (type == L2CAP_IT_FIXED_CHAN) {
Marcel Holtmann984947d2009-02-06 23:35:19 +01002230 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01002231 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01002232
2233 l2cap_conn_start(conn);
2234 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02002235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return 0;
2237}
2238
2239static inline void l2cap_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
2240{
2241 u8 *data = skb->data;
2242 int len = skb->len;
2243 struct l2cap_cmd_hdr cmd;
2244 int err = 0;
2245
2246 l2cap_raw_recv(conn, skb);
2247
2248 while (len >= L2CAP_CMD_HDR_SIZE) {
Al Viro88219a02007-07-29 00:17:25 -07002249 u16 cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
2251 data += L2CAP_CMD_HDR_SIZE;
2252 len -= L2CAP_CMD_HDR_SIZE;
2253
Al Viro88219a02007-07-29 00:17:25 -07002254 cmd_len = le16_to_cpu(cmd.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Al Viro88219a02007-07-29 00:17:25 -07002256 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Al Viro88219a02007-07-29 00:17:25 -07002258 if (cmd_len > len || !cmd.ident) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 BT_DBG("corrupted command");
2260 break;
2261 }
2262
2263 switch (cmd.code) {
2264 case L2CAP_COMMAND_REJ:
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02002265 l2cap_command_rej(conn, &cmd, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 break;
2267
2268 case L2CAP_CONN_REQ:
2269 err = l2cap_connect_req(conn, &cmd, data);
2270 break;
2271
2272 case L2CAP_CONN_RSP:
2273 err = l2cap_connect_rsp(conn, &cmd, data);
2274 break;
2275
2276 case L2CAP_CONF_REQ:
Al Viro88219a02007-07-29 00:17:25 -07002277 err = l2cap_config_req(conn, &cmd, cmd_len, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 break;
2279
2280 case L2CAP_CONF_RSP:
2281 err = l2cap_config_rsp(conn, &cmd, data);
2282 break;
2283
2284 case L2CAP_DISCONN_REQ:
2285 err = l2cap_disconnect_req(conn, &cmd, data);
2286 break;
2287
2288 case L2CAP_DISCONN_RSP:
2289 err = l2cap_disconnect_rsp(conn, &cmd, data);
2290 break;
2291
2292 case L2CAP_ECHO_REQ:
Al Viro88219a02007-07-29 00:17:25 -07002293 l2cap_send_cmd(conn, cmd.ident, L2CAP_ECHO_RSP, cmd_len, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 break;
2295
2296 case L2CAP_ECHO_RSP:
2297 break;
2298
2299 case L2CAP_INFO_REQ:
2300 err = l2cap_information_req(conn, &cmd, data);
2301 break;
2302
2303 case L2CAP_INFO_RSP:
2304 err = l2cap_information_rsp(conn, &cmd, data);
2305 break;
2306
2307 default:
2308 BT_ERR("Unknown signaling command 0x%2.2x", cmd.code);
2309 err = -EINVAL;
2310 break;
2311 }
2312
2313 if (err) {
2314 struct l2cap_cmd_rej rej;
2315 BT_DBG("error %d", err);
2316
2317 /* FIXME: Map err to a valid reason */
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002318 rej.reason = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
2320 }
2321
Al Viro88219a02007-07-29 00:17:25 -07002322 data += cmd_len;
2323 len -= cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325
2326 kfree_skb(skb);
2327}
2328
2329static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
2330{
2331 struct sock *sk;
2332
2333 sk = l2cap_get_chan_by_scid(&conn->chan_list, cid);
2334 if (!sk) {
2335 BT_DBG("unknown cid 0x%4.4x", cid);
2336 goto drop;
2337 }
2338
2339 BT_DBG("sk %p, len %d", sk, skb->len);
2340
2341 if (sk->sk_state != BT_CONNECTED)
2342 goto drop;
2343
2344 if (l2cap_pi(sk)->imtu < skb->len)
2345 goto drop;
2346
2347 /* If socket recv buffers overflows we drop data here
2348 * which is *bad* because L2CAP has to be reliable.
2349 * But we don't have any other choice. L2CAP doesn't
2350 * provide flow control mechanism. */
2351
2352 if (!sock_queue_rcv_skb(sk, skb))
2353 goto done;
2354
2355drop:
2356 kfree_skb(skb);
2357
2358done:
Marcel Holtmann01394182006-07-03 10:02:46 +02002359 if (sk)
2360 bh_unlock_sock(sk);
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return 0;
2363}
2364
Al Viro8e036fc2007-07-29 00:16:36 -07002365static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
2367 struct sock *sk;
2368
2369 sk = l2cap_get_sock_by_psm(0, psm, conn->src);
2370 if (!sk)
2371 goto drop;
2372
2373 BT_DBG("sk %p, len %d", sk, skb->len);
2374
2375 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED)
2376 goto drop;
2377
2378 if (l2cap_pi(sk)->imtu < skb->len)
2379 goto drop;
2380
2381 if (!sock_queue_rcv_skb(sk, skb))
2382 goto done;
2383
2384drop:
2385 kfree_skb(skb);
2386
2387done:
2388 if (sk) bh_unlock_sock(sk);
2389 return 0;
2390}
2391
2392static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
2393{
2394 struct l2cap_hdr *lh = (void *) skb->data;
Al Viro8e036fc2007-07-29 00:16:36 -07002395 u16 cid, len;
2396 __le16 psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 skb_pull(skb, L2CAP_HDR_SIZE);
2399 cid = __le16_to_cpu(lh->cid);
2400 len = __le16_to_cpu(lh->len);
2401
2402 BT_DBG("len %d, cid 0x%4.4x", len, cid);
2403
2404 switch (cid) {
2405 case 0x0001:
2406 l2cap_sig_channel(conn, skb);
2407 break;
2408
2409 case 0x0002:
Al Viro8e036fc2007-07-29 00:16:36 -07002410 psm = get_unaligned((__le16 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 skb_pull(skb, 2);
2412 l2cap_conless_channel(conn, psm, skb);
2413 break;
2414
2415 default:
2416 l2cap_data_channel(conn, cid, skb);
2417 break;
2418 }
2419}
2420
2421/* ---- L2CAP interface with lower layer (HCI) ---- */
2422
2423static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
2424{
2425 int exact = 0, lm1 = 0, lm2 = 0;
2426 register struct sock *sk;
2427 struct hlist_node *node;
2428
2429 if (type != ACL_LINK)
2430 return 0;
2431
2432 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
2433
2434 /* Find listening sockets and check their link_mode */
2435 read_lock(&l2cap_sk_list.lock);
2436 sk_for_each(sk, node, &l2cap_sk_list.head) {
2437 if (sk->sk_state != BT_LISTEN)
2438 continue;
2439
2440 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01002441 lm1 |= HCI_LM_ACCEPT;
2442 if (l2cap_pi(sk)->role_switch)
2443 lm1 |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 exact++;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01002445 } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
2446 lm2 |= HCI_LM_ACCEPT;
2447 if (l2cap_pi(sk)->role_switch)
2448 lm2 |= HCI_LM_MASTER;
2449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451 read_unlock(&l2cap_sk_list.lock);
2452
2453 return exact ? lm1 : lm2;
2454}
2455
2456static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
2457{
Marcel Holtmann01394182006-07-03 10:02:46 +02002458 struct l2cap_conn *conn;
2459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
2461
2462 if (hcon->type != ACL_LINK)
2463 return 0;
2464
2465 if (!status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 conn = l2cap_conn_add(hcon, status);
2467 if (conn)
2468 l2cap_conn_ready(conn);
Marcel Holtmann01394182006-07-03 10:02:46 +02002469 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 l2cap_conn_del(hcon, bt_err(status));
2471
2472 return 0;
2473}
2474
2475static int l2cap_disconn_ind(struct hci_conn *hcon, u8 reason)
2476{
2477 BT_DBG("hcon %p reason %d", hcon, reason);
2478
2479 if (hcon->type != ACL_LINK)
2480 return 0;
2481
2482 l2cap_conn_del(hcon, bt_err(reason));
Marcel Holtmann01394182006-07-03 10:02:46 +02002483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 return 0;
2485}
2486
Marcel Holtmannf62e4322009-01-15 21:58:44 +01002487static inline void l2cap_check_encryption(struct sock *sk, u8 encrypt)
2488{
Marcel Holtmann255c7602009-02-04 21:07:19 +01002489 if (sk->sk_type != SOCK_SEQPACKET)
2490 return;
2491
Marcel Holtmannf62e4322009-01-15 21:58:44 +01002492 if (encrypt == 0x00) {
2493 if (l2cap_pi(sk)->sec_level == BT_SECURITY_MEDIUM) {
2494 l2cap_sock_clear_timer(sk);
2495 l2cap_sock_set_timer(sk, HZ * 5);
2496 } else if (l2cap_pi(sk)->sec_level == BT_SECURITY_HIGH)
2497 __l2cap_sock_close(sk, ECONNREFUSED);
2498 } else {
2499 if (l2cap_pi(sk)->sec_level == BT_SECURITY_MEDIUM)
2500 l2cap_sock_clear_timer(sk);
2501 }
2502}
2503
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002504static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
2506 struct l2cap_chan_list *l;
Marcel Holtmann40be4922008-07-14 20:13:50 +02002507 struct l2cap_conn *conn = hcon->l2cap_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Marcel Holtmann01394182006-07-03 10:02:46 +02002510 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 return 0;
Marcel Holtmann01394182006-07-03 10:02:46 +02002512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 l = &conn->chan_list;
2514
2515 BT_DBG("conn %p", conn);
2516
2517 read_lock(&l->lock);
2518
2519 for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
2520 bh_lock_sock(sk);
2521
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01002522 if (l2cap_pi(sk)->conf_state & L2CAP_CONF_CONNECT_PEND) {
2523 bh_unlock_sock(sk);
2524 continue;
2525 }
2526
Marcel Holtmannf62e4322009-01-15 21:58:44 +01002527 if (!status && (sk->sk_state == BT_CONNECTED ||
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002528 sk->sk_state == BT_CONFIG)) {
Marcel Holtmannf62e4322009-01-15 21:58:44 +01002529 l2cap_check_encryption(sk, encrypt);
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02002530 bh_unlock_sock(sk);
2531 continue;
2532 }
2533
Marcel Holtmannb1235d7962008-07-14 20:13:54 +02002534 if (sk->sk_state == BT_CONNECT) {
2535 if (!status) {
2536 struct l2cap_conn_req req;
2537 req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
2538 req.psm = l2cap_pi(sk)->psm;
2539
2540 l2cap_pi(sk)->ident = l2cap_get_ident(conn);
2541
2542 l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
2543 L2CAP_CONN_REQ, sizeof(req), &req);
2544 } else {
2545 l2cap_sock_clear_timer(sk);
2546 l2cap_sock_set_timer(sk, HZ / 10);
2547 }
2548 } else if (sk->sk_state == BT_CONNECT2) {
2549 struct l2cap_conn_rsp rsp;
2550 __u16 result;
2551
2552 if (!status) {
2553 sk->sk_state = BT_CONFIG;
2554 result = L2CAP_CR_SUCCESS;
2555 } else {
2556 sk->sk_state = BT_DISCONN;
2557 l2cap_sock_set_timer(sk, HZ / 10);
2558 result = L2CAP_CR_SEC_BLOCK;
2559 }
2560
2561 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
2562 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
2563 rsp.result = cpu_to_le16(result);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02002564 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Marcel Holtmannb1235d7962008-07-14 20:13:54 +02002565 l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
2566 L2CAP_CONN_RSP, sizeof(rsp), &rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 bh_unlock_sock(sk);
2570 }
2571
2572 read_unlock(&l->lock);
Marcel Holtmannb1235d7962008-07-14 20:13:54 +02002573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return 0;
2575}
2576
2577static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
2578{
2579 struct l2cap_conn *conn = hcon->l2cap_data;
2580
2581 if (!conn && !(conn = l2cap_conn_add(hcon, 0)))
2582 goto drop;
2583
2584 BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
2585
2586 if (flags & ACL_START) {
2587 struct l2cap_hdr *hdr;
2588 int len;
2589
2590 if (conn->rx_len) {
2591 BT_ERR("Unexpected start frame (len %d)", skb->len);
2592 kfree_skb(conn->rx_skb);
2593 conn->rx_skb = NULL;
2594 conn->rx_len = 0;
2595 l2cap_conn_unreliable(conn, ECOMM);
2596 }
2597
2598 if (skb->len < 2) {
2599 BT_ERR("Frame is too short (len %d)", skb->len);
2600 l2cap_conn_unreliable(conn, ECOMM);
2601 goto drop;
2602 }
2603
2604 hdr = (struct l2cap_hdr *) skb->data;
2605 len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
2606
2607 if (len == skb->len) {
2608 /* Complete frame received */
2609 l2cap_recv_frame(conn, skb);
2610 return 0;
2611 }
2612
2613 BT_DBG("Start: total len %d, frag len %d", len, skb->len);
2614
2615 if (skb->len > len) {
2616 BT_ERR("Frame is too long (len %d, expected len %d)",
2617 skb->len, len);
2618 l2cap_conn_unreliable(conn, ECOMM);
2619 goto drop;
2620 }
2621
2622 /* Allocate skb for the complete frame (with header) */
2623 if (!(conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC)))
2624 goto drop;
2625
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002626 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Marcel Holtmanne1027a72009-02-09 09:18:02 +01002627 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 conn->rx_len = len - skb->len;
2629 } else {
2630 BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
2631
2632 if (!conn->rx_len) {
2633 BT_ERR("Unexpected continuation frame (len %d)", skb->len);
2634 l2cap_conn_unreliable(conn, ECOMM);
2635 goto drop;
2636 }
2637
2638 if (skb->len > conn->rx_len) {
2639 BT_ERR("Fragment is too long (len %d, expected %d)",
2640 skb->len, conn->rx_len);
2641 kfree_skb(conn->rx_skb);
2642 conn->rx_skb = NULL;
2643 conn->rx_len = 0;
2644 l2cap_conn_unreliable(conn, ECOMM);
2645 goto drop;
2646 }
2647
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002648 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Marcel Holtmanne1027a72009-02-09 09:18:02 +01002649 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 conn->rx_len -= skb->len;
2651
2652 if (!conn->rx_len) {
2653 /* Complete frame received */
2654 l2cap_recv_frame(conn, conn->rx_skb);
2655 conn->rx_skb = NULL;
2656 }
2657 }
2658
2659drop:
2660 kfree_skb(skb);
2661 return 0;
2662}
2663
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002664static ssize_t l2cap_sysfs_show(struct class *dev, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
2666 struct sock *sk;
2667 struct hlist_node *node;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002668 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 read_lock_bh(&l2cap_sk_list.lock);
2671
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002672 sk_for_each(sk, node, &l2cap_sk_list.head) {
2673 struct l2cap_pinfo *pi = l2cap_pi(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01002675 str += sprintf(str, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d\n",
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002676 batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
Marcel Holtmann73863972007-01-22 22:00:40 +01002677 sk->sk_state, btohs(pi->psm), pi->scid, pi->dcid,
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01002678 pi->imtu, pi->omtu, pi->sec_level);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 read_unlock_bh(&l2cap_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002682
2683 return (str - buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002686static CLASS_ATTR(l2cap, S_IRUGO, l2cap_sysfs_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002688static const struct proto_ops l2cap_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 .family = PF_BLUETOOTH,
2690 .owner = THIS_MODULE,
2691 .release = l2cap_sock_release,
2692 .bind = l2cap_sock_bind,
2693 .connect = l2cap_sock_connect,
2694 .listen = l2cap_sock_listen,
2695 .accept = l2cap_sock_accept,
2696 .getname = l2cap_sock_getname,
2697 .sendmsg = l2cap_sock_sendmsg,
Marcel Holtmannf66dc812009-01-15 21:57:00 +01002698 .recvmsg = l2cap_sock_recvmsg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 .poll = bt_sock_poll,
Marcel Holtmann3241ad82008-07-14 20:13:50 +02002700 .ioctl = bt_sock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 .mmap = sock_no_mmap,
2702 .socketpair = sock_no_socketpair,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 .shutdown = l2cap_sock_shutdown,
2704 .setsockopt = l2cap_sock_setsockopt,
2705 .getsockopt = l2cap_sock_getsockopt
2706};
2707
2708static struct net_proto_family l2cap_sock_family_ops = {
2709 .family = PF_BLUETOOTH,
2710 .owner = THIS_MODULE,
2711 .create = l2cap_sock_create,
2712};
2713
2714static struct hci_proto l2cap_hci_proto = {
2715 .name = "L2CAP",
2716 .id = HCI_PROTO_L2CAP,
2717 .connect_ind = l2cap_connect_ind,
2718 .connect_cfm = l2cap_connect_cfm,
2719 .disconn_ind = l2cap_disconn_ind,
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002720 .security_cfm = l2cap_security_cfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 .recv_acldata = l2cap_recv_acldata
2722};
2723
2724static int __init l2cap_init(void)
2725{
2726 int err;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 err = proto_register(&l2cap_proto, 0);
2729 if (err < 0)
2730 return err;
2731
2732 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
2733 if (err < 0) {
2734 BT_ERR("L2CAP socket registration failed");
2735 goto error;
2736 }
2737
2738 err = hci_register_proto(&l2cap_hci_proto);
2739 if (err < 0) {
2740 BT_ERR("L2CAP protocol registration failed");
2741 bt_sock_unregister(BTPROTO_L2CAP);
2742 goto error;
2743 }
2744
Marcel Holtmanndf5c37e2006-10-15 17:30:45 +02002745 if (class_create_file(bt_class, &class_attr_l2cap) < 0)
2746 BT_ERR("Failed to create L2CAP info file");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 BT_INFO("L2CAP ver %s", VERSION);
2749 BT_INFO("L2CAP socket layer initialized");
2750
2751 return 0;
2752
2753error:
2754 proto_unregister(&l2cap_proto);
2755 return err;
2756}
2757
2758static void __exit l2cap_exit(void)
2759{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +02002760 class_remove_file(bt_class, &class_attr_l2cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
2763 BT_ERR("L2CAP socket unregistration failed");
2764
2765 if (hci_unregister_proto(&l2cap_hci_proto) < 0)
2766 BT_ERR("L2CAP protocol unregistration failed");
2767
2768 proto_unregister(&l2cap_proto);
2769}
2770
2771void l2cap_load(void)
2772{
2773 /* Dummy function to trigger automatic L2CAP module loading by
2774 * other modules that use L2CAP sockets but don't use any other
2775 * symbols from it. */
2776 return;
2777}
2778EXPORT_SYMBOL(l2cap_load);
2779
2780module_init(l2cap_init);
2781module_exit(l2cap_exit);
2782
Marcel Holtmann63fbd242008-08-18 13:23:53 +02002783MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784MODULE_DESCRIPTION("Bluetooth L2CAP ver " VERSION);
2785MODULE_VERSION(VERSION);
2786MODULE_LICENSE("GPL");
2787MODULE_ALIAS("bt-proto-0");