l2cap_core.c 99.6 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3
   BlueZ - Bluetooth protocol stack for Linux
   Copyright (C) 2000-2001 Qualcomm Incorporated
4
   Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
5
   Copyright (C) 2010 Google Inc.
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16

   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 2 as
   published by the Free Software Foundation;

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
17 18 19
   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
L
Linus Torvalds 已提交
20 21
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

22 23
   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
L
Linus Torvalds 已提交
24 25 26
   SOFTWARE IS DISCLAIMED.
*/

27
/* Bluetooth L2CAP core. */
L
Linus Torvalds 已提交
28 29 30 31

#include <linux/module.h>

#include <linux/types.h>
32
#include <linux/capability.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40 41 42 43
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/fcntl.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/socket.h>
#include <linux/skbuff.h>
#include <linux/list.h>
44
#include <linux/device.h>
45 46
#include <linux/debugfs.h>
#include <linux/seq_file.h>
47
#include <linux/uaccess.h>
48
#include <linux/crc16.h>
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56
#include <net/sock.h>

#include <asm/system.h>
#include <asm/unaligned.h>

#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>
#include <net/bluetooth/l2cap.h>
57
#include <net/bluetooth/smp.h>
L
Linus Torvalds 已提交
58

59
int disable_ertm;
60

61
static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN;
62
static u8 l2cap_fixed_chan[8] = { 0x02, };
L
Linus Torvalds 已提交
63

64 65
static struct workqueue_struct *_busy_wq;

66 67
static LIST_HEAD(chan_list);
static DEFINE_RWLOCK(chan_list_lock);
L
Linus Torvalds 已提交
68

69 70
static void l2cap_busy_work(struct work_struct *work);

L
Linus Torvalds 已提交
71 72
static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
				u8 code, u8 ident, u16 dlen, void *data);
73 74
static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
								void *data);
75
static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data);
76 77
static void l2cap_send_disconn_req(struct l2cap_conn *conn,
				struct l2cap_chan *chan, int err);
L
Linus Torvalds 已提交
78

79 80
static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb);

81
/* ---- L2CAP channels ---- */
82 83 84 85 86 87 88 89 90 91 92 93

static inline void chan_hold(struct l2cap_chan *c)
{
	atomic_inc(&c->refcnt);
}

static inline void chan_put(struct l2cap_chan *c)
{
	if (atomic_dec_and_test(&c->refcnt))
		kfree(c);
}

94
static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid)
95
{
96
	struct l2cap_chan *c;
97 98

	list_for_each_entry(c, &conn->chan_l, list) {
99
		if (c->dcid == cid)
100
			return c;
101
	}
102 103
	return NULL;

104 105
}

106
static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
107
{
108
	struct l2cap_chan *c;
109 110

	list_for_each_entry(c, &conn->chan_l, list) {
111
		if (c->scid == cid)
112
			return c;
113
	}
114
	return NULL;
115 116 117 118
}

/* Find channel with given SCID.
 * Returns locked socket */
119
static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
120
{
121
	struct l2cap_chan *c;
122 123 124

	read_lock(&conn->chan_lock);
	c = __l2cap_get_chan_by_scid(conn, cid);
125 126
	if (c)
		bh_lock_sock(c->sk);
127
	read_unlock(&conn->chan_lock);
128
	return c;
129 130
}

131
static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
132
{
133
	struct l2cap_chan *c;
134 135

	list_for_each_entry(c, &conn->chan_l, list) {
136
		if (c->ident == ident)
137
			return c;
138
	}
139
	return NULL;
140 141
}

142
static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
143
{
144
	struct l2cap_chan *c;
145 146 147

	read_lock(&conn->chan_lock);
	c = __l2cap_get_chan_by_ident(conn, ident);
148 149
	if (c)
		bh_lock_sock(c->sk);
150
	read_unlock(&conn->chan_lock);
151
	return c;
152 153
}

154
static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
155
{
156
	struct l2cap_chan *c;
157

158 159
	list_for_each_entry(c, &chan_list, global_l) {
		if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src))
160 161 162
			goto found;
	}

163
	c = NULL;
164
found:
165
	return c;
166 167 168 169
}

int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
{
170 171
	int err;

172
	write_lock_bh(&chan_list_lock);
173

174
	if (psm && __l2cap_global_chan_by_addr(psm, src)) {
175 176
		err = -EADDRINUSE;
		goto done;
177 178
	}

179 180 181 182 183 184 185 186 187
	if (psm) {
		chan->psm = psm;
		chan->sport = psm;
		err = 0;
	} else {
		u16 p;

		err = -EINVAL;
		for (p = 0x1001; p < 0x1100; p += 2)
188
			if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
189 190 191 192 193 194
				chan->psm   = cpu_to_le16(p);
				chan->sport = cpu_to_le16(p);
				err = 0;
				break;
			}
	}
195

196
done:
197
	write_unlock_bh(&chan_list_lock);
198
	return err;
199 200 201 202
}

int l2cap_add_scid(struct l2cap_chan *chan,  __u16 scid)
{
203
	write_lock_bh(&chan_list_lock);
204 205 206

	chan->scid = scid;

207
	write_unlock_bh(&chan_list_lock);
208 209 210 211

	return 0;
}

212
static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
213
{
214
	u16 cid = L2CAP_CID_DYN_START;
215

216
	for (; cid < L2CAP_CID_DYN_END; cid++) {
217
		if (!__l2cap_get_chan_by_scid(conn, cid))
218 219 220 221 222 223
			return cid;
	}

	return 0;
}

224
static void l2cap_set_timer(struct l2cap_chan *chan, struct timer_list *timer, long timeout)
225
{
226
	BT_DBG("chan %p state %d timeout %ld", chan->sk, chan->state, timeout);
227

228
	if (!mod_timer(timer, jiffies + msecs_to_jiffies(timeout)))
229
		chan_hold(chan);
230 231
}

232
static void l2cap_clear_timer(struct l2cap_chan *chan, struct timer_list *timer)
233
{
234
	BT_DBG("chan %p state %d", chan, chan->state);
235

236 237
	if (timer_pending(timer) && del_timer(timer))
		chan_put(chan);
238 239
}

240 241 242 243 244 245
static void l2cap_state_change(struct l2cap_chan *chan, int state)
{
	chan->state = state;
	chan->ops->state_change(chan->data, state);
}

246 247 248 249 250 251
static void l2cap_chan_timeout(unsigned long arg)
{
	struct l2cap_chan *chan = (struct l2cap_chan *) arg;
	struct sock *sk = chan->sk;
	int reason;

252
	BT_DBG("chan %p state %d", chan, chan->state);
253 254 255 256 257

	bh_lock_sock(sk);

	if (sock_owned_by_user(sk)) {
		/* sk is owned by user. Try again later */
258
		__set_chan_timer(chan, HZ / 5);
259
		bh_unlock_sock(sk);
260
		chan_put(chan);
261 262 263
		return;
	}

264
	if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
265
		reason = ECONNREFUSED;
266
	else if (chan->state == BT_CONNECT &&
267 268 269 270 271
					chan->sec_level != BT_SECURITY_SDP)
		reason = ECONNREFUSED;
	else
		reason = ETIMEDOUT;

272
	l2cap_chan_close(chan, reason);
273 274 275

	bh_unlock_sock(sk);

276
	chan->ops->close(chan->data);
277
	chan_put(chan);
278 279
}

280
struct l2cap_chan *l2cap_chan_create(struct sock *sk)
281 282 283 284 285 286 287 288 289
{
	struct l2cap_chan *chan;

	chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
	if (!chan)
		return NULL;

	chan->sk = sk;

290 291 292 293
	write_lock_bh(&chan_list_lock);
	list_add(&chan->global_l, &chan_list);
	write_unlock_bh(&chan_list_lock);

294 295
	setup_timer(&chan->chan_timer, l2cap_chan_timeout, (unsigned long) chan);

296 297
	chan->state = BT_OPEN;

298 299
	atomic_set(&chan->refcnt, 1);

300 301 302
	return chan;
}

303
void l2cap_chan_destroy(struct l2cap_chan *chan)
304
{
305 306 307 308
	write_lock_bh(&chan_list_lock);
	list_del(&chan->global_l);
	write_unlock_bh(&chan_list_lock);

309
	chan_put(chan);
310 311
}

312
static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
313
{
314
	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
315
			chan->psm, chan->dcid);
316

317 318
	conn->disc_reason = 0x13;

319
	chan->conn = conn;
320

321
	if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
322 323
		if (conn->hcon->type == LE_LINK) {
			/* LE connection */
324
			chan->omtu = L2CAP_LE_DEFAULT_MTU;
325 326
			chan->scid = L2CAP_CID_LE_DATA;
			chan->dcid = L2CAP_CID_LE_DATA;
327 328
		} else {
			/* Alloc CID for connection-oriented socket */
329
			chan->scid = l2cap_alloc_cid(conn);
330
			chan->omtu = L2CAP_DEFAULT_MTU;
331
		}
332
	} else if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
333
		/* Connectionless socket */
334 335
		chan->scid = L2CAP_CID_CONN_LESS;
		chan->dcid = L2CAP_CID_CONN_LESS;
336
		chan->omtu = L2CAP_DEFAULT_MTU;
337 338
	} else {
		/* Raw socket can send/recv signalling messages only */
339 340
		chan->scid = L2CAP_CID_SIGNALING;
		chan->dcid = L2CAP_CID_SIGNALING;
341
		chan->omtu = L2CAP_DEFAULT_MTU;
342 343
	}

344
	chan_hold(chan);
345 346

	list_add(&chan->list, &conn->chan_l);
347 348
}

349
/* Delete channel.
350
 * Must be called on the locked socket. */
351
static void l2cap_chan_del(struct l2cap_chan *chan, int err)
352
{
353
	struct sock *sk = chan->sk;
354
	struct l2cap_conn *conn = chan->conn;
355 356
	struct sock *parent = bt_sk(sk)->parent;

357
	__clear_chan_timer(chan);
358

359
	BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
360

361
	if (conn) {
362 363 364 365
		/* Delete from channel list */
		write_lock_bh(&conn->chan_lock);
		list_del(&chan->list);
		write_unlock_bh(&conn->chan_lock);
366
		chan_put(chan);
367

368
		chan->conn = NULL;
369 370 371
		hci_conn_put(conn->hcon);
	}

372
	l2cap_state_change(chan, BT_CLOSED);
373 374 375 376 377 378 379 380 381 382
	sock_set_flag(sk, SOCK_ZAPPED);

	if (err)
		sk->sk_err = err;

	if (parent) {
		bt_accept_unlink(sk);
		parent->sk_data_ready(parent, 0);
	} else
		sk->sk_state_change(sk);
383

384 385
	if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) &&
			test_bit(CONF_INPUT_DONE, &chan->conf_state)))
386
		return;
387

388
	skb_queue_purge(&chan->tx_q);
389

390
	if (chan->mode == L2CAP_MODE_ERTM) {
391 392
		struct srej_list *l, *tmp;

393 394 395
		__clear_retrans_timer(chan);
		__clear_monitor_timer(chan);
		__clear_ack_timer(chan);
396

397 398
		skb_queue_purge(&chan->srej_q);
		skb_queue_purge(&chan->busy_q);
399

400
		list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
401 402 403 404
			list_del(&l->list);
			kfree(l);
		}
	}
405 406
}

407 408 409 410 411 412 413
static void l2cap_chan_cleanup_listen(struct sock *parent)
{
	struct sock *sk;

	BT_DBG("parent %p", parent);

	/* Close not yet accepted channels */
414
	while ((sk = bt_accept_dequeue(parent, NULL))) {
415
		struct l2cap_chan *chan = l2cap_pi(sk)->chan;
416
		__clear_chan_timer(chan);
417
		lock_sock(sk);
418
		l2cap_chan_close(chan, ECONNRESET);
419
		release_sock(sk);
420
		chan->ops->close(chan->data);
421
	}
422 423
}

424
void l2cap_chan_close(struct l2cap_chan *chan, int reason)
425 426 427 428
{
	struct l2cap_conn *conn = chan->conn;
	struct sock *sk = chan->sk;

429
	BT_DBG("chan %p state %d socket %p", chan, chan->state, sk->sk_socket);
430

431
	switch (chan->state) {
432 433
	case BT_LISTEN:
		l2cap_chan_cleanup_listen(sk);
434 435 436

		l2cap_state_change(chan, BT_CLOSED);
		sock_set_flag(sk, SOCK_ZAPPED);
437 438 439 440
		break;

	case BT_CONNECTED:
	case BT_CONFIG:
441
		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
442
					conn->hcon->type == ACL_LINK) {
443 444
			__clear_chan_timer(chan);
			__set_chan_timer(chan, sk->sk_sndtimeo);
445 446 447 448 449 450
			l2cap_send_disconn_req(conn, chan, reason);
		} else
			l2cap_chan_del(chan, reason);
		break;

	case BT_CONNECT2:
451
		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
452 453 454 455 456 457 458 459
					conn->hcon->type == ACL_LINK) {
			struct l2cap_conn_rsp rsp;
			__u16 result;

			if (bt_sk(sk)->defer_setup)
				result = L2CAP_CR_SEC_BLOCK;
			else
				result = L2CAP_CR_BAD_PSM;
460
			l2cap_state_change(chan, BT_DISCONN);
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

			rsp.scid   = cpu_to_le16(chan->dcid);
			rsp.dcid   = cpu_to_le16(chan->scid);
			rsp.result = cpu_to_le16(result);
			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
							sizeof(rsp), &rsp);
		}

		l2cap_chan_del(chan, reason);
		break;

	case BT_CONNECT:
	case BT_DISCONN:
		l2cap_chan_del(chan, reason);
		break;

	default:
		sock_set_flag(sk, SOCK_ZAPPED);
		break;
	}
}

484
static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
485
{
486
	if (chan->chan_type == L2CAP_CHAN_RAW) {
487
		switch (chan->sec_level) {
488 489 490 491 492 493 494
		case BT_SECURITY_HIGH:
			return HCI_AT_DEDICATED_BONDING_MITM;
		case BT_SECURITY_MEDIUM:
			return HCI_AT_DEDICATED_BONDING;
		default:
			return HCI_AT_NO_BONDING;
		}
495
	} else if (chan->psm == cpu_to_le16(0x0001)) {
496 497
		if (chan->sec_level == BT_SECURITY_LOW)
			chan->sec_level = BT_SECURITY_SDP;
498

499
		if (chan->sec_level == BT_SECURITY_HIGH)
500
			return HCI_AT_NO_BONDING_MITM;
501
		else
502
			return HCI_AT_NO_BONDING;
503
	} else {
504
		switch (chan->sec_level) {
505
		case BT_SECURITY_HIGH:
506
			return HCI_AT_GENERAL_BONDING_MITM;
507
		case BT_SECURITY_MEDIUM:
508
			return HCI_AT_GENERAL_BONDING;
509
		default:
510
			return HCI_AT_NO_BONDING;
511
		}
512
	}
513 514 515
}

/* Service level security */
516
static inline int l2cap_check_security(struct l2cap_chan *chan)
517
{
518
	struct l2cap_conn *conn = chan->conn;
519 520
	__u8 auth_type;

521
	auth_type = l2cap_get_auth_type(chan);
522

523
	return hci_conn_security(conn->hcon, chan->sec_level, auth_type);
524 525
}

526
static u8 l2cap_get_ident(struct l2cap_conn *conn)
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
{
	u8 id;

	/* Get next available identificator.
	 *    1 - 128 are used by kernel.
	 *  129 - 199 are reserved.
	 *  200 - 254 are used by utilities like l2ping, etc.
	 */

	spin_lock_bh(&conn->lock);

	if (++conn->tx_ident > 128)
		conn->tx_ident = 1;

	id = conn->tx_ident;

	spin_unlock_bh(&conn->lock);

	return id;
}

548
static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
549 550
{
	struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
551
	u8 flags;
552 553 554 555

	BT_DBG("code 0x%2.2x", code);

	if (!skb)
556
		return;
557

558 559 560 561 562
	if (lmp_no_flush_capable(conn->hcon->hdev))
		flags = ACL_START_NO_FLUSH;
	else
		flags = ACL_START;

563 564
	bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;

565
	hci_send_acl(conn->hcon, skb, flags);
566 567
}

568
static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control)
569 570 571
{
	struct sk_buff *skb;
	struct l2cap_hdr *lh;
572
	struct l2cap_conn *conn = chan->conn;
573
	int count, hlen = L2CAP_HDR_SIZE + 2;
574
	u8 flags;
575

576
	if (chan->state != BT_CONNECTED)
577 578
		return;

579
	if (chan->fcs == L2CAP_FCS_CRC16)
580
		hlen += 2;
581

582
	BT_DBG("chan %p, control 0x%2.2x", chan, control);
583

584
	count = min_t(unsigned int, conn->mtu, hlen);
585 586
	control |= L2CAP_CTRL_FRAME_TYPE;

587
	if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
588 589
		control |= L2CAP_CTRL_FINAL;

590
	if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state))
591 592
		control |= L2CAP_CTRL_POLL;

593 594
	skb = bt_skb_alloc(count, GFP_ATOMIC);
	if (!skb)
595
		return;
596 597

	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
598
	lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
599
	lh->cid = cpu_to_le16(chan->dcid);
600 601
	put_unaligned_le16(control, skb_put(skb, 2));

602
	if (chan->fcs == L2CAP_FCS_CRC16) {
603 604 605 606
		u16 fcs = crc16(0, (u8 *)lh, count - 2);
		put_unaligned_le16(fcs, skb_put(skb, 2));
	}

607 608 609 610 611
	if (lmp_no_flush_capable(conn->hcon->hdev))
		flags = ACL_START_NO_FLUSH;
	else
		flags = ACL_START;

612 613
	bt_cb(skb)->force_active = chan->force_active;

614
	hci_send_acl(chan->conn->hcon, skb, flags);
615 616
}

617
static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control)
618
{
619
	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
620
		control |= L2CAP_SUPER_RCV_NOT_READY;
621
		set_bit(CONN_RNR_SENT, &chan->conn_state);
622
	} else
623 624
		control |= L2CAP_SUPER_RCV_READY;

625
	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
626

627
	l2cap_send_sframe(chan, control);
628 629
}

630
static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
631
{
632
	return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
633 634
}

635
static void l2cap_do_start(struct l2cap_chan *chan)
636
{
637
	struct l2cap_conn *conn = chan->conn;
638 639

	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
640 641 642
		if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
			return;

643 644
		if (l2cap_check_security(chan) &&
				__l2cap_no_conn_pending(chan)) {
645
			struct l2cap_conn_req req;
646 647
			req.scid = cpu_to_le16(chan->scid);
			req.psm  = chan->psm;
648

649
			chan->ident = l2cap_get_ident(conn);
650
			set_bit(CONF_CONNECT_PEND, &chan->conf_state);
651

652 653
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
							sizeof(req), &req);
654
		}
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
	} else {
		struct l2cap_info_req req;
		req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);

		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
		conn->info_ident = l2cap_get_ident(conn);

		mod_timer(&conn->info_timer, jiffies +
					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));

		l2cap_send_cmd(conn, conn->info_ident,
					L2CAP_INFO_REQ, sizeof(req), &req);
	}
}

670 671 672
static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
{
	u32 local_feat_mask = l2cap_feat_mask;
673
	if (!disable_ertm)
674 675 676 677 678 679 680 681 682 683 684 685
		local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;

	switch (mode) {
	case L2CAP_MODE_ERTM:
		return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
	case L2CAP_MODE_STREAMING:
		return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
	default:
		return 0x00;
	}
}

686
static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err)
687
{
688
	struct sock *sk;
689 690
	struct l2cap_disconn_req req;

691 692 693
	if (!conn)
		return;

694 695
	sk = chan->sk;

696
	if (chan->mode == L2CAP_MODE_ERTM) {
697 698 699
		__clear_retrans_timer(chan);
		__clear_monitor_timer(chan);
		__clear_ack_timer(chan);
700 701
	}

702 703
	req.dcid = cpu_to_le16(chan->dcid);
	req.scid = cpu_to_le16(chan->scid);
704 705
	l2cap_send_cmd(conn, l2cap_get_ident(conn),
			L2CAP_DISCONN_REQ, sizeof(req), &req);
706

707
	l2cap_state_change(chan, BT_DISCONN);
708
	sk->sk_err = err;
709 710
}

L
Linus Torvalds 已提交
711
/* ---- L2CAP connections ---- */
712 713
static void l2cap_conn_start(struct l2cap_conn *conn)
{
714
	struct l2cap_chan *chan, *tmp;
715 716 717

	BT_DBG("conn %p", conn);

718
	read_lock(&conn->chan_lock);
719

720
	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
721
		struct sock *sk = chan->sk;
722

723 724
		bh_lock_sock(sk);

725
		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
726 727 728 729
			bh_unlock_sock(sk);
			continue;
		}

730
		if (chan->state == BT_CONNECT) {
731
			struct l2cap_conn_req req;
732

733
			if (!l2cap_check_security(chan) ||
734
					!__l2cap_no_conn_pending(chan)) {
735 736 737
				bh_unlock_sock(sk);
				continue;
			}
738

739 740 741
			if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
					&& test_bit(CONF_STATE2_DEVICE,
					&chan->conf_state)) {
742
				/* l2cap_chan_close() calls list_del(chan)
743
				 * so release the lock */
744
				read_unlock(&conn->chan_lock);
745
				l2cap_chan_close(chan, ECONNRESET);
746
				read_lock(&conn->chan_lock);
747 748
				bh_unlock_sock(sk);
				continue;
749
			}
750

751 752
			req.scid = cpu_to_le16(chan->scid);
			req.psm  = chan->psm;
753

754
			chan->ident = l2cap_get_ident(conn);
755
			set_bit(CONF_CONNECT_PEND, &chan->conf_state);
756

757 758
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
							sizeof(req), &req);
759

760
		} else if (chan->state == BT_CONNECT2) {
761
			struct l2cap_conn_rsp rsp;
762
			char buf[128];
763 764
			rsp.scid = cpu_to_le16(chan->dcid);
			rsp.dcid = cpu_to_le16(chan->scid);
765

766
			if (l2cap_check_security(chan)) {
767 768 769 770 771 772 773
				if (bt_sk(sk)->defer_setup) {
					struct sock *parent = bt_sk(sk)->parent;
					rsp.result = cpu_to_le16(L2CAP_CR_PEND);
					rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
					parent->sk_data_ready(parent, 0);

				} else {
774
					l2cap_state_change(chan, BT_CONFIG);
775 776 777
					rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
					rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
				}
778 779 780 781 782
			} else {
				rsp.result = cpu_to_le16(L2CAP_CR_PEND);
				rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
			}

783 784
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
							sizeof(rsp), &rsp);
785

786
			if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
787 788 789 790 791
					rsp.result != L2CAP_CR_SUCCESS) {
				bh_unlock_sock(sk);
				continue;
			}

792
			set_bit(CONF_REQ_SENT, &chan->conf_state);
793
			l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
794 795
						l2cap_build_conf_req(chan, buf), buf);
			chan->num_conf_req++;
796 797 798 799 800
		}

		bh_unlock_sock(sk);
	}

801
	read_unlock(&conn->chan_lock);
802 803
}

804 805 806
/* Find socket with cid and source bdaddr.
 * Returns closest match, locked.
 */
807
static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src)
808
{
809
	struct l2cap_chan *c, *c1 = NULL;
810

811
	read_lock(&chan_list_lock);
812

813 814
	list_for_each_entry(c, &chan_list, global_l) {
		struct sock *sk = c->sk;
815

816
		if (state && c->state != state)
817 818
			continue;

819
		if (c->scid == cid) {
820
			/* Exact match. */
821 822 823 824
			if (!bacmp(&bt_sk(sk)->src, src)) {
				read_unlock(&chan_list_lock);
				return c;
			}
825 826 827

			/* Closest match */
			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
828
				c1 = c;
829 830
		}
	}
831

832
	read_unlock(&chan_list_lock);
833

834
	return c1;
835 836 837 838
}

static void l2cap_le_conn_ready(struct l2cap_conn *conn)
{
839
	struct sock *parent, *sk;
840
	struct l2cap_chan *chan, *pchan;
841 842 843 844

	BT_DBG("");

	/* Check if we have socket listening on cid */
845
	pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA,
846
							conn->src);
847
	if (!pchan)
848 849
		return;

850 851
	parent = pchan->sk;

852 853
	bh_lock_sock(parent);

854 855 856 857 858 859
	/* Check for backlog size */
	if (sk_acceptq_is_full(parent)) {
		BT_DBG("backlog full %d", parent->sk_ack_backlog);
		goto clean;
	}

860 861
	chan = pchan->ops->new_connection(pchan->data);
	if (!chan)
862 863
		goto clean;

864
	sk = chan->sk;
865

866
	write_lock_bh(&conn->chan_lock);
867 868 869 870 871 872

	hci_conn_hold(conn->hcon);

	bacpy(&bt_sk(sk)->src, conn->src);
	bacpy(&bt_sk(sk)->dst, conn->dst);

873 874
	bt_accept_enqueue(parent, sk);

875 876
	__l2cap_chan_add(conn, chan);

877
	__set_chan_timer(chan, sk->sk_sndtimeo);
878

879
	l2cap_state_change(chan, BT_CONNECTED);
880 881
	parent->sk_data_ready(parent, 0);

882
	write_unlock_bh(&conn->chan_lock);
883 884 885 886 887

clean:
	bh_unlock_sock(parent);
}

888 889 890 891 892 893 894 895 896 897
static void l2cap_chan_ready(struct sock *sk)
{
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
	struct sock *parent = bt_sk(sk)->parent;

	BT_DBG("sk %p, parent %p", sk, parent);

	chan->conf_state = 0;
	__clear_chan_timer(chan);

898
	l2cap_state_change(chan, BT_CONNECTED);
899 900 901 902 903 904
	sk->sk_state_change(sk);

	if (parent)
		parent->sk_data_ready(parent, 0);
}

905 906
static void l2cap_conn_ready(struct l2cap_conn *conn)
{
907
	struct l2cap_chan *chan;
908

909
	BT_DBG("conn %p", conn);
910

911 912 913
	if (!conn->hcon->out && conn->hcon->type == LE_LINK)
		l2cap_le_conn_ready(conn);

914
	read_lock(&conn->chan_lock);
915

916
	list_for_each_entry(chan, &conn->chan_l, list) {
917
		struct sock *sk = chan->sk;
918

919
		bh_lock_sock(sk);
920

921
		if (conn->hcon->type == LE_LINK) {
922
			if (smp_conn_security(conn, chan->sec_level))
923
				l2cap_chan_ready(sk);
924

925
		} else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
926
			__clear_chan_timer(chan);
927
			l2cap_state_change(chan, BT_CONNECTED);
928
			sk->sk_state_change(sk);
929

930
		} else if (chan->state == BT_CONNECT)
931
			l2cap_do_start(chan);
932

933
		bh_unlock_sock(sk);
934
	}
935

936
	read_unlock(&conn->chan_lock);
937 938 939 940 941
}

/* Notify sockets that we cannot guaranty reliability anymore */
static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
{
942
	struct l2cap_chan *chan;
943 944 945

	BT_DBG("conn %p", conn);

946
	read_lock(&conn->chan_lock);
947

948
	list_for_each_entry(chan, &conn->chan_l, list) {
949
		struct sock *sk = chan->sk;
950

951
		if (chan->force_reliable)
952 953 954
			sk->sk_err = err;
	}

955
	read_unlock(&conn->chan_lock);
956 957 958 959 960 961
}

static void l2cap_info_timeout(unsigned long arg)
{
	struct l2cap_conn *conn = (void *) arg;

962
	conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
963
	conn->info_ident = 0;
964

965 966 967
	l2cap_conn_start(conn);
}

968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
static void l2cap_conn_del(struct hci_conn *hcon, int err)
{
	struct l2cap_conn *conn = hcon->l2cap_data;
	struct l2cap_chan *chan, *l;
	struct sock *sk;

	if (!conn)
		return;

	BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);

	kfree_skb(conn->rx_skb);

	/* Kill channels */
	list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
		sk = chan->sk;
		bh_lock_sock(sk);
		l2cap_chan_del(chan, err);
		bh_unlock_sock(sk);
		chan->ops->close(chan->data);
	}

	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
		del_timer_sync(&conn->info_timer);

	if (test_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend))
		del_timer(&conn->security_timer);

	hcon->l2cap_data = NULL;
	kfree(conn);
}

static void security_timeout(unsigned long arg)
{
	struct l2cap_conn *conn = (void *) arg;

	l2cap_conn_del(conn->hcon, ETIMEDOUT);
}

L
Linus Torvalds 已提交
1007 1008
static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
{
1009
	struct l2cap_conn *conn = hcon->l2cap_data;
L
Linus Torvalds 已提交
1010

1011
	if (conn || status)
L
Linus Torvalds 已提交
1012 1013
		return conn;

1014 1015
	conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
	if (!conn)
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020
		return NULL;

	hcon->l2cap_data = conn;
	conn->hcon = hcon;

1021 1022
	BT_DBG("hcon %p conn %p", hcon, conn);

1023 1024 1025 1026 1027
	if (hcon->hdev->le_mtu && hcon->type == LE_LINK)
		conn->mtu = hcon->hdev->le_mtu;
	else
		conn->mtu = hcon->hdev->acl_mtu;

L
Linus Torvalds 已提交
1028 1029 1030
	conn->src = &hcon->hdev->bdaddr;
	conn->dst = &hcon->dst;

1031 1032
	conn->feat_mask = 0;

L
Linus Torvalds 已提交
1033
	spin_lock_init(&conn->lock);
1034 1035 1036
	rwlock_init(&conn->chan_lock);

	INIT_LIST_HEAD(&conn->chan_l);
L
Linus Torvalds 已提交
1037

1038 1039 1040 1041
	if (hcon->type == LE_LINK)
		setup_timer(&conn->security_timer, security_timeout,
						(unsigned long) conn);
	else
1042
		setup_timer(&conn->info_timer, l2cap_info_timeout,
D
Dave Young 已提交
1043 1044
						(unsigned long) conn);

1045 1046
	conn->disc_reason = 0x13;

L
Linus Torvalds 已提交
1047 1048 1049
	return conn;
}

1050
static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
L
Linus Torvalds 已提交
1051
{
1052
	write_lock_bh(&conn->chan_lock);
1053
	__l2cap_chan_add(conn, chan);
1054
	write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
1055 1056 1057 1058 1059 1060 1061
}

/* ---- Socket interface ---- */

/* Find socket with psm and source bdaddr.
 * Returns closest match.
 */
1062
static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src)
L
Linus Torvalds 已提交
1063
{
1064
	struct l2cap_chan *c, *c1 = NULL;
L
Linus Torvalds 已提交
1065

1066
	read_lock(&chan_list_lock);
1067

1068 1069
	list_for_each_entry(c, &chan_list, global_l) {
		struct sock *sk = c->sk;
1070

1071
		if (state && c->state != state)
L
Linus Torvalds 已提交
1072 1073
			continue;

1074
		if (c->psm == psm) {
L
Linus Torvalds 已提交
1075
			/* Exact match. */
1076
			if (!bacmp(&bt_sk(sk)->src, src)) {
1077
				read_unlock(&chan_list_lock);
1078 1079
				return c;
			}
L
Linus Torvalds 已提交
1080 1081 1082

			/* Closest match */
			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
1083
				c1 = c;
L
Linus Torvalds 已提交
1084 1085 1086
		}
	}

1087
	read_unlock(&chan_list_lock);
1088

1089
	return c1;
L
Linus Torvalds 已提交
1090 1091
}

1092
int l2cap_chan_connect(struct l2cap_chan *chan)
L
Linus Torvalds 已提交
1093
{
1094
	struct sock *sk = chan->sk;
L
Linus Torvalds 已提交
1095 1096 1097 1098 1099
	bdaddr_t *src = &bt_sk(sk)->src;
	bdaddr_t *dst = &bt_sk(sk)->dst;
	struct l2cap_conn *conn;
	struct hci_conn *hcon;
	struct hci_dev *hdev;
1100
	__u8 auth_type;
1101
	int err;
L
Linus Torvalds 已提交
1102

1103
	BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst),
1104
							chan->psm);
L
Linus Torvalds 已提交
1105

1106 1107
	hdev = hci_get_route(dst, src);
	if (!hdev)
L
Linus Torvalds 已提交
1108 1109 1110 1111
		return -EHOSTUNREACH;

	hci_dev_lock_bh(hdev);

1112
	auth_type = l2cap_get_auth_type(chan);
1113

1114
	if (chan->dcid == L2CAP_CID_LE_DATA)
1115
		hcon = hci_connect(hdev, LE_LINK, dst,
1116
					chan->sec_level, auth_type);
1117 1118
	else
		hcon = hci_connect(hdev, ACL_LINK, dst,
1119
					chan->sec_level, auth_type);
1120

1121 1122
	if (IS_ERR(hcon)) {
		err = PTR_ERR(hcon);
L
Linus Torvalds 已提交
1123
		goto done;
1124
	}
L
Linus Torvalds 已提交
1125 1126 1127 1128

	conn = l2cap_conn_add(hcon, 0);
	if (!conn) {
		hci_conn_put(hcon);
1129
		err = -ENOMEM;
L
Linus Torvalds 已提交
1130 1131 1132 1133 1134 1135
		goto done;
	}

	/* Update source addr of the socket */
	bacpy(src, conn->src);

1136 1137
	l2cap_chan_add(conn, chan);

1138
	l2cap_state_change(chan, BT_CONNECT);
1139
	__set_chan_timer(chan, sk->sk_sndtimeo);
L
Linus Torvalds 已提交
1140 1141

	if (hcon->state == BT_CONNECTED) {
1142
		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
1143
			__clear_chan_timer(chan);
1144
			if (l2cap_check_security(chan))
1145
				l2cap_state_change(chan, BT_CONNECTED);
1146
		} else
1147
			l2cap_do_start(chan);
L
Linus Torvalds 已提交
1148 1149
	}

1150 1151
	err = 0;

L
Linus Torvalds 已提交
1152 1153 1154 1155 1156 1157
done:
	hci_dev_unlock_bh(hdev);
	hci_dev_put(hdev);
	return err;
}

1158
int __l2cap_wait_ack(struct sock *sk)
1159
{
1160
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
1161 1162 1163 1164
	DECLARE_WAITQUEUE(wait, current);
	int err = 0;
	int timeo = HZ/5;

1165
	add_wait_queue(sk_sleep(sk), &wait);
1166
	while ((chan->unacked_frames > 0 && chan->conn)) {
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
		set_current_state(TASK_INTERRUPTIBLE);

		if (!timeo)
			timeo = HZ/5;

		if (signal_pending(current)) {
			err = sock_intr_errno(timeo);
			break;
		}

		release_sock(sk);
		timeo = schedule_timeout(timeo);
		lock_sock(sk);

		err = sock_error(sk);
		if (err)
			break;
	}
	set_current_state(TASK_RUNNING);
1186
	remove_wait_queue(sk_sleep(sk), &wait);
1187 1188 1189
	return err;
}

1190 1191
static void l2cap_monitor_timeout(unsigned long arg)
{
1192 1193
	struct l2cap_chan *chan = (void *) arg;
	struct sock *sk = chan->sk;
1194

1195
	BT_DBG("chan %p", chan);
1196

1197
	bh_lock_sock(sk);
1198
	if (chan->retry_count >= chan->remote_max_tx) {
1199
		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1200
		bh_unlock_sock(sk);
1201 1202 1203
		return;
	}

1204
	chan->retry_count++;
1205
	__set_monitor_timer(chan);
1206

1207
	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
1208
	bh_unlock_sock(sk);
1209 1210 1211 1212
}

static void l2cap_retrans_timeout(unsigned long arg)
{
1213 1214
	struct l2cap_chan *chan = (void *) arg;
	struct sock *sk = chan->sk;
1215

1216
	BT_DBG("chan %p", chan);
1217

1218
	bh_lock_sock(sk);
1219
	chan->retry_count = 1;
1220
	__set_monitor_timer(chan);
1221

1222
	set_bit(CONN_WAIT_F, &chan->conn_state);
1223

1224
	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
1225
	bh_unlock_sock(sk);
1226 1227
}

1228
static void l2cap_drop_acked_frames(struct l2cap_chan *chan)
L
Linus Torvalds 已提交
1229
{
1230
	struct sk_buff *skb;
L
Linus Torvalds 已提交
1231

1232
	while ((skb = skb_peek(&chan->tx_q)) &&
1233
			chan->unacked_frames) {
1234
		if (bt_cb(skb)->tx_seq == chan->expected_ack_seq)
1235
			break;
L
Linus Torvalds 已提交
1236

1237
		skb = skb_dequeue(&chan->tx_q);
1238
		kfree_skb(skb);
L
Linus Torvalds 已提交
1239

1240
		chan->unacked_frames--;
1241
	}
L
Linus Torvalds 已提交
1242

1243
	if (!chan->unacked_frames)
1244
		__clear_retrans_timer(chan);
1245
}
L
Linus Torvalds 已提交
1246

1247
void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
1248
{
1249
	struct hci_conn *hcon = chan->conn->hcon;
1250
	u16 flags;
1251

1252
	BT_DBG("chan %p, skb %p len %d", chan, skb, skb->len);
L
Linus Torvalds 已提交
1253

1254
	if (!chan->flushable && lmp_no_flush_capable(hcon->hdev))
1255 1256 1257 1258
		flags = ACL_START_NO_FLUSH;
	else
		flags = ACL_START;

1259
	bt_cb(skb)->force_active = chan->force_active;
1260
	hci_send_acl(hcon, skb, flags);
1261 1262
}

1263
void l2cap_streaming_send(struct l2cap_chan *chan)
1264
{
1265
	struct sk_buff *skb;
1266
	u16 control, fcs;
1267

1268
	while ((skb = skb_dequeue(&chan->tx_q))) {
1269
		control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE);
1270
		control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT;
1271
		put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE);
1272

1273
		if (chan->fcs == L2CAP_FCS_CRC16) {
1274 1275
			fcs = crc16(0, (u8 *)skb->data, skb->len - 2);
			put_unaligned_le16(fcs, skb->data + skb->len - 2);
1276 1277
		}

1278
		l2cap_do_send(chan, skb);
1279

1280
		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
1281 1282 1283
	}
}

1284
static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq)
1285 1286 1287 1288
{
	struct sk_buff *skb, *tx_skb;
	u16 control, fcs;

1289
	skb = skb_peek(&chan->tx_q);
1290 1291
	if (!skb)
		return;
1292

1293 1294
	do {
		if (bt_cb(skb)->tx_seq == tx_seq)
1295 1296
			break;

1297
		if (skb_queue_is_last(&chan->tx_q, skb))
1298
			return;
1299

1300
	} while ((skb = skb_queue_next(&chan->tx_q, skb)));
1301

1302 1303
	if (chan->remote_max_tx &&
			bt_cb(skb)->retries == chan->remote_max_tx) {
1304
		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1305 1306 1307 1308 1309 1310
		return;
	}

	tx_skb = skb_clone(skb, GFP_ATOMIC);
	bt_cb(skb)->retries++;
	control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
1311
	control &= L2CAP_CTRL_SAR;
1312

1313
	if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
1314
		control |= L2CAP_CTRL_FINAL;
1315

1316
	control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
1317
			| (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
1318

1319 1320
	put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);

1321
	if (chan->fcs == L2CAP_FCS_CRC16) {
1322 1323 1324 1325
		fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2);
		put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2);
	}

1326
	l2cap_do_send(chan, tx_skb);
1327 1328
}

1329
int l2cap_ertm_send(struct l2cap_chan *chan)
1330 1331
{
	struct sk_buff *skb, *tx_skb;
1332
	u16 control, fcs;
1333
	int nsent = 0;
1334

1335
	if (chan->state != BT_CONNECTED)
1336
		return -ENOTCONN;
1337

1338
	while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) {
1339

1340 1341
		if (chan->remote_max_tx &&
				bt_cb(skb)->retries == chan->remote_max_tx) {
1342
			l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1343 1344 1345
			break;
		}

1346 1347
		tx_skb = skb_clone(skb, GFP_ATOMIC);

1348 1349
		bt_cb(skb)->retries++;

1350
		control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
1351 1352
		control &= L2CAP_CTRL_SAR;

1353
		if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
1354
			control |= L2CAP_CTRL_FINAL;
1355

1356 1357
		control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
				| (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
1358 1359
		put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);

1360

1361
		if (chan->fcs == L2CAP_FCS_CRC16) {
1362 1363 1364 1365
			fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2);
			put_unaligned_le16(fcs, skb->data + tx_skb->len - 2);
		}

1366
		l2cap_do_send(chan, tx_skb);
1367

1368
		__set_retrans_timer(chan);
1369

1370 1371
		bt_cb(skb)->tx_seq = chan->next_tx_seq;
		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
1372

1373
		if (bt_cb(skb)->retries == 1)
1374
			chan->unacked_frames++;
1375

1376
		chan->frames_sent++;
1377

1378 1379
		if (skb_queue_is_last(&chan->tx_q, skb))
			chan->tx_send_head = NULL;
1380
		else
1381
			chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
1382 1383

		nsent++;
1384 1385
	}

1386 1387 1388
	return nsent;
}

1389
static int l2cap_retransmit_frames(struct l2cap_chan *chan)
1390 1391 1392
{
	int ret;

1393 1394
	if (!skb_queue_empty(&chan->tx_q))
		chan->tx_send_head = chan->tx_q.next;
1395

1396
	chan->next_tx_seq = chan->expected_ack_seq;
1397
	ret = l2cap_ertm_send(chan);
1398 1399 1400
	return ret;
}

1401
static void l2cap_send_ack(struct l2cap_chan *chan)
1402 1403 1404
{
	u16 control = 0;

1405
	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
1406

1407
	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
1408
		control |= L2CAP_SUPER_RCV_NOT_READY;
1409
		set_bit(CONN_RNR_SENT, &chan->conn_state);
1410
		l2cap_send_sframe(chan, control);
1411
		return;
1412
	}
1413

1414
	if (l2cap_ertm_send(chan) > 0)
1415 1416 1417
		return;

	control |= L2CAP_SUPER_RCV_READY;
1418
	l2cap_send_sframe(chan, control);
1419 1420
}

1421
static void l2cap_send_srejtail(struct l2cap_chan *chan)
1422 1423 1424 1425 1426 1427 1428
{
	struct srej_list *tail;
	u16 control;

	control = L2CAP_SUPER_SELECT_REJECT;
	control |= L2CAP_CTRL_FINAL;

1429
	tail = list_entry((&chan->srej_l)->prev, struct srej_list, list);
1430 1431
	control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;

1432
	l2cap_send_sframe(chan, control);
1433 1434
}

1435 1436
static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb)
{
1437
	struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
1438 1439
	struct sk_buff **frag;
	int err, sent = 0;
L
Linus Torvalds 已提交
1440

1441
	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
1442
		return -EFAULT;
L
Linus Torvalds 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453

	sent += count;
	len  -= count;

	/* Continuation fragments (no L2CAP header) */
	frag = &skb_shinfo(skb)->frag_list;
	while (len) {
		count = min_t(unsigned int, conn->mtu, len);

		*frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err);
		if (!*frag)
1454
			return err;
1455 1456
		if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
			return -EFAULT;
L
Linus Torvalds 已提交
1457 1458 1459 1460 1461 1462 1463 1464

		sent += count;
		len  -= count;

		frag = &(*frag)->next;
	}

	return sent;
1465
}
L
Linus Torvalds 已提交
1466

1467
struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
1468
{
1469
	struct sock *sk = chan->sk;
1470
	struct l2cap_conn *conn = chan->conn;
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
	struct sk_buff *skb;
	int err, count, hlen = L2CAP_HDR_SIZE + 2;
	struct l2cap_hdr *lh;

	BT_DBG("sk %p len %d", sk, (int)len);

	count = min_t(unsigned int, (conn->mtu - hlen), len);
	skb = bt_skb_send_alloc(sk, count + hlen,
			msg->msg_flags & MSG_DONTWAIT, &err);
	if (!skb)
1481
		return ERR_PTR(err);
1482 1483 1484

	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1485
	lh->cid = cpu_to_le16(chan->dcid);
1486
	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
1487
	put_unaligned_le16(chan->psm, skb_put(skb, 2));
1488 1489 1490 1491 1492 1493 1494 1495 1496

	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
	if (unlikely(err < 0)) {
		kfree_skb(skb);
		return ERR_PTR(err);
	}
	return skb;
}

1497
struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
1498
{
1499
	struct sock *sk = chan->sk;
1500
	struct l2cap_conn *conn = chan->conn;
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
	struct sk_buff *skb;
	int err, count, hlen = L2CAP_HDR_SIZE;
	struct l2cap_hdr *lh;

	BT_DBG("sk %p len %d", sk, (int)len);

	count = min_t(unsigned int, (conn->mtu - hlen), len);
	skb = bt_skb_send_alloc(sk, count + hlen,
			msg->msg_flags & MSG_DONTWAIT, &err);
	if (!skb)
1511
		return ERR_PTR(err);
1512 1513 1514

	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1515
	lh->cid = cpu_to_le16(chan->dcid);
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));

	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
	if (unlikely(err < 0)) {
		kfree_skb(skb);
		return ERR_PTR(err);
	}
	return skb;
}

1526
struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen)
1527
{
1528
	struct sock *sk = chan->sk;
1529
	struct l2cap_conn *conn = chan->conn;
1530 1531 1532 1533 1534 1535
	struct sk_buff *skb;
	int err, count, hlen = L2CAP_HDR_SIZE + 2;
	struct l2cap_hdr *lh;

	BT_DBG("sk %p len %d", sk, (int)len);

1536 1537 1538
	if (!conn)
		return ERR_PTR(-ENOTCONN);

1539 1540 1541
	if (sdulen)
		hlen += 2;

1542
	if (chan->fcs == L2CAP_FCS_CRC16)
1543 1544
		hlen += 2;

1545 1546 1547 1548
	count = min_t(unsigned int, (conn->mtu - hlen), len);
	skb = bt_skb_send_alloc(sk, count + hlen,
			msg->msg_flags & MSG_DONTWAIT, &err);
	if (!skb)
1549
		return ERR_PTR(err);
1550 1551 1552

	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1553
	lh->cid = cpu_to_le16(chan->dcid);
1554 1555
	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
	put_unaligned_le16(control, skb_put(skb, 2));
1556 1557
	if (sdulen)
		put_unaligned_le16(sdulen, skb_put(skb, 2));
1558 1559 1560 1561 1562 1563

	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
	if (unlikely(err < 0)) {
		kfree_skb(skb);
		return ERR_PTR(err);
	}
1564

1565
	if (chan->fcs == L2CAP_FCS_CRC16)
1566 1567
		put_unaligned_le16(0, skb_put(skb, 2));

1568
	bt_cb(skb)->retries = 0;
1569
	return skb;
L
Linus Torvalds 已提交
1570 1571
}

1572
int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
1573 1574 1575 1576 1577 1578
{
	struct sk_buff *skb;
	struct sk_buff_head sar_queue;
	u16 control;
	size_t size = 0;

1579
	skb_queue_head_init(&sar_queue);
1580
	control = L2CAP_SDU_START;
1581
	skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
1582 1583 1584 1585
	if (IS_ERR(skb))
		return PTR_ERR(skb);

	__skb_queue_tail(&sar_queue, skb);
1586 1587
	len -= chan->remote_mps;
	size += chan->remote_mps;
1588 1589 1590 1591

	while (len > 0) {
		size_t buflen;

1592
		if (len > chan->remote_mps) {
1593
			control = L2CAP_SDU_CONTINUE;
1594
			buflen = chan->remote_mps;
1595
		} else {
1596
			control = L2CAP_SDU_END;
1597 1598 1599
			buflen = len;
		}

1600
		skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0);
1601 1602 1603 1604 1605 1606 1607 1608 1609
		if (IS_ERR(skb)) {
			skb_queue_purge(&sar_queue);
			return PTR_ERR(skb);
		}

		__skb_queue_tail(&sar_queue, skb);
		len -= buflen;
		size += buflen;
	}
1610 1611 1612
	skb_queue_splice_tail(&sar_queue, &chan->tx_q);
	if (chan->tx_send_head == NULL)
		chan->tx_send_head = sar_queue.next;
1613 1614 1615 1616

	return size;
}

1617 1618 1619 1620 1621 1622 1623
int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
{
	struct sk_buff *skb;
	u16 control;
	int err;

	/* Connectionless channel */
1624
	if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
		skb = l2cap_create_connless_pdu(chan, msg, len);
		if (IS_ERR(skb))
			return PTR_ERR(skb);

		l2cap_do_send(chan, skb);
		return len;
	}

	switch (chan->mode) {
	case L2CAP_MODE_BASIC:
		/* Check outgoing MTU */
		if (len > chan->omtu)
			return -EMSGSIZE;

		/* Create a basic PDU */
		skb = l2cap_create_basic_pdu(chan, msg, len);
		if (IS_ERR(skb))
			return PTR_ERR(skb);

		l2cap_do_send(chan, skb);
		err = len;
		break;

	case L2CAP_MODE_ERTM:
	case L2CAP_MODE_STREAMING:
		/* Entire SDU fits into one PDU */
		if (len <= chan->remote_mps) {
			control = L2CAP_SDU_UNSEGMENTED;
			skb = l2cap_create_iframe_pdu(chan, msg, len, control,
									0);
			if (IS_ERR(skb))
				return PTR_ERR(skb);

			__skb_queue_tail(&chan->tx_q, skb);

			if (chan->tx_send_head == NULL)
				chan->tx_send_head = skb;

		} else {
			/* Segment SDU into multiples PDUs */
			err = l2cap_sar_segment_sdu(chan, msg, len);
			if (err < 0)
				return err;
		}

		if (chan->mode == L2CAP_MODE_STREAMING) {
			l2cap_streaming_send(chan);
			err = len;
			break;
		}

1676 1677
		if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
				test_bit(CONN_WAIT_F, &chan->conn_state)) {
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
			err = len;
			break;
		}

		err = l2cap_ertm_send(chan);
		if (err >= 0)
			err = len;

		break;

	default:
		BT_DBG("bad state %1.1x", chan->mode);
		err = -EBADFD;
	}

	return err;
}

L
Linus Torvalds 已提交
1696 1697 1698 1699
/* Copy frame to all raw sockets on that connection */
static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
{
	struct sk_buff *nskb;
1700
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
1701 1702 1703

	BT_DBG("conn %p", conn);

1704 1705
	read_lock(&conn->chan_lock);
	list_for_each_entry(chan, &conn->chan_l, list) {
1706
		struct sock *sk = chan->sk;
1707
		if (chan->chan_type != L2CAP_CHAN_RAW)
L
Linus Torvalds 已提交
1708 1709 1710 1711 1712
			continue;

		/* Don't send frame to the socket it came from */
		if (skb->sk == sk)
			continue;
1713 1714
		nskb = skb_clone(skb, GFP_ATOMIC);
		if (!nskb)
L
Linus Torvalds 已提交
1715 1716
			continue;

1717
		if (chan->ops->recv(chan->data, nskb))
L
Linus Torvalds 已提交
1718 1719
			kfree_skb(nskb);
	}
1720
	read_unlock(&conn->chan_lock);
L
Linus Torvalds 已提交
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
}

/* ---- L2CAP signalling commands ---- */
static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
				u8 code, u8 ident, u16 dlen, void *data)
{
	struct sk_buff *skb, **frag;
	struct l2cap_cmd_hdr *cmd;
	struct l2cap_hdr *lh;
	int len, count;

1732 1733
	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
			conn, code, ident, dlen);
L
Linus Torvalds 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742

	len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
	count = min_t(unsigned int, conn->mtu, len);

	skb = bt_skb_alloc(count, GFP_ATOMIC);
	if (!skb)
		return NULL;

	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1743
	lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
1744 1745 1746 1747 1748

	if (conn->hcon->type == LE_LINK)
		lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
	else
		lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
L
Linus Torvalds 已提交
1749 1750 1751 1752

	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
	cmd->code  = code;
	cmd->ident = ident;
1753
	cmd->len   = cpu_to_le16(dlen);
L
Linus Torvalds 已提交
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803

	if (dlen) {
		count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
		memcpy(skb_put(skb, count), data, count);
		data += count;
	}

	len -= skb->len;

	/* Continuation fragments (no L2CAP header) */
	frag = &skb_shinfo(skb)->frag_list;
	while (len) {
		count = min_t(unsigned int, conn->mtu, len);

		*frag = bt_skb_alloc(count, GFP_ATOMIC);
		if (!*frag)
			goto fail;

		memcpy(skb_put(*frag, count), data, count);

		len  -= count;
		data += count;

		frag = &(*frag)->next;
	}

	return skb;

fail:
	kfree_skb(skb);
	return NULL;
}

static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
{
	struct l2cap_conf_opt *opt = *ptr;
	int len;

	len = L2CAP_CONF_OPT_SIZE + opt->len;
	*ptr += len;

	*type = opt->type;
	*olen = opt->len;

	switch (opt->len) {
	case 1:
		*val = *((u8 *) opt->val);
		break;

	case 2:
1804
		*val = get_unaligned_le16(opt->val);
L
Linus Torvalds 已提交
1805 1806 1807
		break;

	case 4:
1808
		*val = get_unaligned_le32(opt->val);
L
Linus Torvalds 已提交
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
		break;

	default:
		*val = (unsigned long) opt->val;
		break;
	}

	BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
	return len;
}

static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
{
	struct l2cap_conf_opt *opt = *ptr;

	BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);

	opt->type = type;
	opt->len  = len;

	switch (len) {
	case 1:
		*((u8 *) opt->val)  = val;
		break;

	case 2:
1835
		put_unaligned_le16(val, opt->val);
L
Linus Torvalds 已提交
1836 1837 1838
		break;

	case 4:
1839
		put_unaligned_le32(val, opt->val);
L
Linus Torvalds 已提交
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
		break;

	default:
		memcpy(opt->val, (void *) val, len);
		break;
	}

	*ptr += L2CAP_CONF_OPT_SIZE + len;
}

1850 1851
static void l2cap_ack_timeout(unsigned long arg)
{
1852
	struct l2cap_chan *chan = (void *) arg;
1853

1854 1855 1856
	bh_lock_sock(chan->sk);
	l2cap_send_ack(chan);
	bh_unlock_sock(chan->sk);
1857 1858
}

1859
static inline void l2cap_ertm_init(struct l2cap_chan *chan)
1860
{
1861 1862
	struct sock *sk = chan->sk;

1863
	chan->expected_ack_seq = 0;
1864
	chan->unacked_frames = 0;
1865
	chan->buffer_seq = 0;
1866 1867
	chan->num_acked = 0;
	chan->frames_sent = 0;
1868

1869 1870 1871 1872 1873
	setup_timer(&chan->retrans_timer, l2cap_retrans_timeout,
							(unsigned long) chan);
	setup_timer(&chan->monitor_timer, l2cap_monitor_timeout,
							(unsigned long) chan);
	setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan);
1874

1875 1876
	skb_queue_head_init(&chan->srej_q);
	skb_queue_head_init(&chan->busy_q);
1877

1878 1879
	INIT_LIST_HEAD(&chan->srej_l);

1880
	INIT_WORK(&chan->busy_work, l2cap_busy_work);
1881 1882

	sk->sk_backlog_rcv = l2cap_ertm_data_rcv;
1883 1884
}

1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
{
	switch (mode) {
	case L2CAP_MODE_STREAMING:
	case L2CAP_MODE_ERTM:
		if (l2cap_mode_supported(mode, remote_feat_mask))
			return mode;
		/* fall through */
	default:
		return L2CAP_MODE_BASIC;
	}
}

1898
static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
L
Linus Torvalds 已提交
1899 1900
{
	struct l2cap_conf_req *req = data;
1901
	struct l2cap_conf_rfc rfc = { .mode = chan->mode };
L
Linus Torvalds 已提交
1902 1903
	void *ptr = req->data;

1904
	BT_DBG("chan %p", chan);
L
Linus Torvalds 已提交
1905

1906
	if (chan->num_conf_req || chan->num_conf_rsp)
1907 1908
		goto done;

1909
	switch (chan->mode) {
1910 1911
	case L2CAP_MODE_STREAMING:
	case L2CAP_MODE_ERTM:
1912
		if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
1913 1914
			break;

1915
		/* fall through */
1916
	default:
1917
		chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
1918 1919 1920 1921
		break;
	}

done:
1922 1923
	if (chan->imtu != L2CAP_DEFAULT_MTU)
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
1924

1925
	switch (chan->mode) {
1926
	case L2CAP_MODE_BASIC:
1927 1928
		if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
				!(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
1929 1930
			break;

1931 1932 1933 1934 1935 1936 1937
		rfc.mode            = L2CAP_MODE_BASIC;
		rfc.txwin_size      = 0;
		rfc.max_transmit    = 0;
		rfc.retrans_timeout = 0;
		rfc.monitor_timeout = 0;
		rfc.max_pdu_size    = 0;

1938 1939
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
							(unsigned long) &rfc);
1940 1941 1942 1943
		break;

	case L2CAP_MODE_ERTM:
		rfc.mode            = L2CAP_MODE_ERTM;
1944 1945
		rfc.txwin_size      = chan->tx_win;
		rfc.max_transmit    = chan->max_tx;
1946 1947
		rfc.retrans_timeout = 0;
		rfc.monitor_timeout = 0;
1948
		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
1949 1950
		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
1951

1952 1953 1954
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
							(unsigned long) &rfc);

1955
		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
1956 1957
			break;

1958
		if (chan->fcs == L2CAP_FCS_NONE ||
1959
				test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) {
1960 1961
			chan->fcs = L2CAP_FCS_NONE;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
1962
		}
1963 1964 1965 1966 1967 1968 1969 1970
		break;

	case L2CAP_MODE_STREAMING:
		rfc.mode            = L2CAP_MODE_STREAMING;
		rfc.txwin_size      = 0;
		rfc.max_transmit    = 0;
		rfc.retrans_timeout = 0;
		rfc.monitor_timeout = 0;
1971
		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
1972 1973
		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
1974

1975 1976 1977
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
							(unsigned long) &rfc);

1978
		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
1979 1980
			break;

1981
		if (chan->fcs == L2CAP_FCS_NONE ||
1982
				test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) {
1983 1984
			chan->fcs = L2CAP_FCS_NONE;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
1985
		}
1986 1987
		break;
	}
L
Linus Torvalds 已提交
1988

1989
	req->dcid  = cpu_to_le16(chan->dcid);
1990
	req->flags = cpu_to_le16(0);
L
Linus Torvalds 已提交
1991 1992 1993 1994

	return ptr - data;
}

1995
static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
L
Linus Torvalds 已提交
1996
{
1997 1998
	struct l2cap_conf_rsp *rsp = data;
	void *ptr = rsp->data;
1999 2000
	void *req = chan->conf_req;
	int len = chan->conf_len;
2001 2002
	int type, hint, olen;
	unsigned long val;
2003
	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
2004
	u16 mtu = L2CAP_DEFAULT_MTU;
2005
	u16 result = L2CAP_CONF_SUCCESS;
L
Linus Torvalds 已提交
2006

2007
	BT_DBG("chan %p", chan);
2008

2009 2010
	while (len >= L2CAP_CONF_OPT_SIZE) {
		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
L
Linus Torvalds 已提交
2011

2012
		hint  = type & L2CAP_CONF_HINT;
2013
		type &= L2CAP_CONF_MASK;
2014 2015 2016

		switch (type) {
		case L2CAP_CONF_MTU:
2017
			mtu = val;
2018 2019 2020
			break;

		case L2CAP_CONF_FLUSH_TO:
2021
			chan->flush_to = val;
2022 2023 2024 2025 2026
			break;

		case L2CAP_CONF_QOS:
			break;

2027 2028 2029 2030 2031
		case L2CAP_CONF_RFC:
			if (olen == sizeof(rfc))
				memcpy(&rfc, (void *) val, olen);
			break;

2032 2033
		case L2CAP_CONF_FCS:
			if (val == L2CAP_FCS_NONE)
2034
				set_bit(CONF_NO_FCS_RECV, &chan->conf_state);
2035 2036 2037

			break;

2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
		default:
			if (hint)
				break;

			result = L2CAP_CONF_UNKNOWN;
			*((u8 *) ptr++) = type;
			break;
		}
	}

2048
	if (chan->num_conf_rsp || chan->num_conf_req > 1)
2049 2050
		goto done;

2051
	switch (chan->mode) {
2052 2053
	case L2CAP_MODE_STREAMING:
	case L2CAP_MODE_ERTM:
2054
		if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
2055
			chan->mode = l2cap_select_mode(rfc.mode,
2056
					chan->conn->feat_mask);
2057 2058 2059
			break;
		}

2060
		if (chan->mode != rfc.mode)
2061
			return -ECONNREFUSED;
2062

2063 2064 2065 2066
		break;
	}

done:
2067
	if (chan->mode != rfc.mode) {
2068
		result = L2CAP_CONF_UNACCEPT;
2069
		rfc.mode = chan->mode;
2070

2071
		if (chan->num_conf_rsp == 1)
2072 2073 2074 2075 2076 2077 2078
			return -ECONNREFUSED;

		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
					sizeof(rfc), (unsigned long) &rfc);
	}


2079 2080 2081 2082
	if (result == L2CAP_CONF_SUCCESS) {
		/* Configure output options and let the other side know
		 * which ones we don't like. */

2083 2084 2085
		if (mtu < L2CAP_DEFAULT_MIN_MTU)
			result = L2CAP_CONF_UNACCEPT;
		else {
2086
			chan->omtu = mtu;
2087
			set_bit(CONF_MTU_DONE, &chan->conf_state);
2088
		}
2089
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
2090

2091 2092
		switch (rfc.mode) {
		case L2CAP_MODE_BASIC:
2093
			chan->fcs = L2CAP_FCS_NONE;
2094
			set_bit(CONF_MODE_DONE, &chan->conf_state);
2095 2096 2097
			break;

		case L2CAP_MODE_ERTM:
2098 2099
			chan->remote_tx_win = rfc.txwin_size;
			chan->remote_max_tx = rfc.max_transmit;
2100

2101 2102
			if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10)
				rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
2103

2104
			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
2105

2106 2107 2108 2109
			rfc.retrans_timeout =
				le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO);
			rfc.monitor_timeout =
				le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO);
2110

2111
			set_bit(CONF_MODE_DONE, &chan->conf_state);
2112 2113 2114 2115

			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
					sizeof(rfc), (unsigned long) &rfc);

2116 2117 2118
			break;

		case L2CAP_MODE_STREAMING:
2119 2120
			if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10)
				rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
2121

2122
			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
2123

2124
			set_bit(CONF_MODE_DONE, &chan->conf_state);
2125 2126 2127 2128

			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
					sizeof(rfc), (unsigned long) &rfc);

2129 2130 2131
			break;

		default:
2132 2133
			result = L2CAP_CONF_UNACCEPT;

2134
			memset(&rfc, 0, sizeof(rfc));
2135
			rfc.mode = chan->mode;
2136
		}
2137

2138
		if (result == L2CAP_CONF_SUCCESS)
2139
			set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
2140
	}
2141
	rsp->scid   = cpu_to_le16(chan->dcid);
2142 2143 2144 2145
	rsp->result = cpu_to_le16(result);
	rsp->flags  = cpu_to_le16(0x0000);

	return ptr - data;
L
Linus Torvalds 已提交
2146 2147
}

2148
static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result)
2149 2150 2151 2152 2153 2154 2155
{
	struct l2cap_conf_req *req = data;
	void *ptr = req->data;
	int type, olen;
	unsigned long val;
	struct l2cap_conf_rfc rfc;

2156
	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
2157 2158 2159 2160 2161 2162 2163 2164

	while (len >= L2CAP_CONF_OPT_SIZE) {
		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);

		switch (type) {
		case L2CAP_CONF_MTU:
			if (val < L2CAP_DEFAULT_MIN_MTU) {
				*result = L2CAP_CONF_UNACCEPT;
2165
				chan->imtu = L2CAP_DEFAULT_MIN_MTU;
2166
			} else
2167 2168
				chan->imtu = val;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
2169 2170 2171
			break;

		case L2CAP_CONF_FLUSH_TO:
2172
			chan->flush_to = val;
2173
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
2174
							2, chan->flush_to);
2175 2176 2177 2178 2179 2180
			break;

		case L2CAP_CONF_RFC:
			if (olen == sizeof(rfc))
				memcpy(&rfc, (void *)val, olen);

2181
			if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
2182
							rfc.mode != chan->mode)
2183 2184
				return -ECONNREFUSED;

2185
			chan->fcs = 0;
2186 2187 2188 2189 2190 2191 2192

			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
					sizeof(rfc), (unsigned long) &rfc);
			break;
		}
	}

2193
	if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
2194 2195
		return -ECONNREFUSED;

2196
	chan->mode = rfc.mode;
2197

2198 2199 2200
	if (*result == L2CAP_CONF_SUCCESS) {
		switch (rfc.mode) {
		case L2CAP_MODE_ERTM:
2201 2202 2203
			chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
			chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
2204 2205
			break;
		case L2CAP_MODE_STREAMING:
2206
			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
2207 2208 2209
		}
	}

2210
	req->dcid   = cpu_to_le16(chan->dcid);
2211 2212 2213 2214 2215
	req->flags  = cpu_to_le16(0x0000);

	return ptr - data;
}

2216
static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags)
L
Linus Torvalds 已提交
2217 2218 2219 2220
{
	struct l2cap_conf_rsp *rsp = data;
	void *ptr = rsp->data;

2221
	BT_DBG("chan %p", chan);
L
Linus Torvalds 已提交
2222

2223
	rsp->scid   = cpu_to_le16(chan->dcid);
2224
	rsp->result = cpu_to_le16(result);
2225
	rsp->flags  = cpu_to_le16(flags);
L
Linus Torvalds 已提交
2226 2227 2228 2229

	return ptr - data;
}

2230
void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
2231 2232
{
	struct l2cap_conn_rsp rsp;
2233
	struct l2cap_conn *conn = chan->conn;
2234 2235
	u8 buf[128];

2236 2237
	rsp.scid   = cpu_to_le16(chan->dcid);
	rsp.dcid   = cpu_to_le16(chan->scid);
2238 2239 2240 2241 2242
	rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
	rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
	l2cap_send_cmd(conn, chan->ident,
				L2CAP_CONN_RSP, sizeof(rsp), &rsp);

2243
	if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
2244 2245 2246 2247 2248 2249 2250
		return;

	l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
			l2cap_build_conf_req(chan, buf), buf);
	chan->num_conf_req++;
}

2251
static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
2252 2253 2254 2255 2256
{
	int type, olen;
	unsigned long val;
	struct l2cap_conf_rfc rfc;

2257
	BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
2258

2259
	if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
		return;

	while (len >= L2CAP_CONF_OPT_SIZE) {
		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);

		switch (type) {
		case L2CAP_CONF_RFC:
			if (olen == sizeof(rfc))
				memcpy(&rfc, (void *)val, olen);
			goto done;
		}
	}

done:
	switch (rfc.mode) {
	case L2CAP_MODE_ERTM:
2276 2277 2278
		chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
		chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
2279 2280
		break;
	case L2CAP_MODE_STREAMING:
2281
		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
2282 2283 2284
	}
}

2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
{
	struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data;

	if (rej->reason != 0x0000)
		return 0;

	if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
					cmd->ident == conn->info_ident) {
		del_timer(&conn->info_timer);
2295 2296

		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
2297
		conn->info_ident = 0;
2298

2299 2300 2301 2302 2303 2304
		l2cap_conn_start(conn);
	}

	return 0;
}

L
Linus Torvalds 已提交
2305 2306 2307 2308
static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
{
	struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
	struct l2cap_conn_rsp rsp;
2309
	struct l2cap_chan *chan = NULL, *pchan;
2310
	struct sock *parent, *sk = NULL;
2311
	int result, status = L2CAP_CS_NO_INFO;
L
Linus Torvalds 已提交
2312 2313

	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
2314
	__le16 psm = req->psm;
L
Linus Torvalds 已提交
2315 2316 2317 2318

	BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);

	/* Check if we have socket listening on psm */
2319 2320
	pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src);
	if (!pchan) {
L
Linus Torvalds 已提交
2321 2322 2323 2324
		result = L2CAP_CR_BAD_PSM;
		goto sendresp;
	}

2325 2326
	parent = pchan->sk;

2327 2328
	bh_lock_sock(parent);

2329 2330 2331
	/* Check if the ACL is secure enough (if not SDP) */
	if (psm != cpu_to_le16(0x0001) &&
				!hci_conn_check_link_mode(conn->hcon)) {
2332
		conn->disc_reason = 0x05;
2333 2334 2335 2336
		result = L2CAP_CR_SEC_BLOCK;
		goto response;
	}

L
Linus Torvalds 已提交
2337 2338 2339 2340
	result = L2CAP_CR_NO_MEM;

	/* Check for backlog size */
	if (sk_acceptq_is_full(parent)) {
2341
		BT_DBG("backlog full %d", parent->sk_ack_backlog);
L
Linus Torvalds 已提交
2342 2343 2344
		goto response;
	}

2345 2346
	chan = pchan->ops->new_connection(pchan->data);
	if (!chan)
L
Linus Torvalds 已提交
2347 2348
		goto response;

2349 2350
	sk = chan->sk;

2351
	write_lock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2352 2353

	/* Check if we already have channel with that dcid */
2354 2355
	if (__l2cap_get_chan_by_dcid(conn, scid)) {
		write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2356
		sock_set_flag(sk, SOCK_ZAPPED);
2357
		chan->ops->close(chan->data);
L
Linus Torvalds 已提交
2358 2359 2360 2361 2362 2363 2364
		goto response;
	}

	hci_conn_hold(conn->hcon);

	bacpy(&bt_sk(sk)->src, conn->src);
	bacpy(&bt_sk(sk)->dst, conn->dst);
2365 2366
	chan->psm  = psm;
	chan->dcid = scid;
L
Linus Torvalds 已提交
2367

2368 2369
	bt_accept_enqueue(parent, sk);

2370 2371
	__l2cap_chan_add(conn, chan);

2372
	dcid = chan->scid;
L
Linus Torvalds 已提交
2373

2374
	__set_chan_timer(chan, sk->sk_sndtimeo);
L
Linus Torvalds 已提交
2375

2376
	chan->ident = cmd->ident;
L
Linus Torvalds 已提交
2377

2378
	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
2379
		if (l2cap_check_security(chan)) {
2380
			if (bt_sk(sk)->defer_setup) {
2381
				l2cap_state_change(chan, BT_CONNECT2);
2382 2383 2384 2385
				result = L2CAP_CR_PEND;
				status = L2CAP_CS_AUTHOR_PEND;
				parent->sk_data_ready(parent, 0);
			} else {
2386
				l2cap_state_change(chan, BT_CONFIG);
2387 2388 2389
				result = L2CAP_CR_SUCCESS;
				status = L2CAP_CS_NO_INFO;
			}
2390
		} else {
2391
			l2cap_state_change(chan, BT_CONNECT2);
2392 2393 2394 2395
			result = L2CAP_CR_PEND;
			status = L2CAP_CS_AUTHEN_PEND;
		}
	} else {
2396
		l2cap_state_change(chan, BT_CONNECT2);
2397 2398
		result = L2CAP_CR_PEND;
		status = L2CAP_CS_NO_INFO;
L
Linus Torvalds 已提交
2399 2400
	}

2401
	write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2402 2403 2404 2405 2406

response:
	bh_unlock_sock(parent);

sendresp:
2407 2408 2409 2410
	rsp.scid   = cpu_to_le16(scid);
	rsp.dcid   = cpu_to_le16(dcid);
	rsp.result = cpu_to_le16(result);
	rsp.status = cpu_to_le16(status);
L
Linus Torvalds 已提交
2411
	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426

	if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
		struct l2cap_info_req info;
		info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);

		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
		conn->info_ident = l2cap_get_ident(conn);

		mod_timer(&conn->info_timer, jiffies +
					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));

		l2cap_send_cmd(conn, conn->info_ident,
					L2CAP_INFO_REQ, sizeof(info), &info);
	}

2427
	if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
2428 2429
				result == L2CAP_CR_SUCCESS) {
		u8 buf[128];
2430
		set_bit(CONF_REQ_SENT, &chan->conf_state);
2431
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2432 2433
					l2cap_build_conf_req(chan, buf), buf);
		chan->num_conf_req++;
2434 2435
	}

L
Linus Torvalds 已提交
2436 2437 2438 2439 2440 2441 2442
	return 0;
}

static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
{
	struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
	u16 scid, dcid, result, status;
2443
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
	struct sock *sk;
	u8 req[128];

	scid   = __le16_to_cpu(rsp->scid);
	dcid   = __le16_to_cpu(rsp->dcid);
	result = __le16_to_cpu(rsp->result);
	status = __le16_to_cpu(rsp->status);

	BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);

	if (scid) {
2455
		chan = l2cap_get_chan_by_scid(conn, scid);
2456
		if (!chan)
2457
			return -EFAULT;
L
Linus Torvalds 已提交
2458
	} else {
2459
		chan = l2cap_get_chan_by_ident(conn, cmd->ident);
2460
		if (!chan)
2461
			return -EFAULT;
L
Linus Torvalds 已提交
2462 2463
	}

2464 2465
	sk = chan->sk;

L
Linus Torvalds 已提交
2466 2467
	switch (result) {
	case L2CAP_CR_SUCCESS:
2468
		l2cap_state_change(chan, BT_CONFIG);
2469
		chan->ident = 0;
2470
		chan->dcid = dcid;
2471
		clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
2472

2473
		if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
2474 2475
			break;

L
Linus Torvalds 已提交
2476
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2477 2478
					l2cap_build_conf_req(chan, req), req);
		chan->num_conf_req++;
L
Linus Torvalds 已提交
2479 2480 2481
		break;

	case L2CAP_CR_PEND:
2482
		set_bit(CONF_CONNECT_PEND, &chan->conf_state);
L
Linus Torvalds 已提交
2483 2484 2485
		break;

	default:
2486 2487
		/* don't delete l2cap channel if sk is owned by user */
		if (sock_owned_by_user(sk)) {
2488
			l2cap_state_change(chan, BT_DISCONN);
2489 2490
			__clear_chan_timer(chan);
			__set_chan_timer(chan, HZ / 5);
2491 2492 2493
			break;
		}

2494
		l2cap_chan_del(chan, ECONNREFUSED);
L
Linus Torvalds 已提交
2495 2496 2497 2498 2499 2500 2501
		break;
	}

	bh_unlock_sock(sk);
	return 0;
}

2502
static inline void set_default_fcs(struct l2cap_chan *chan)
2503 2504 2505 2506
{
	/* FCS is enabled only in ERTM or streaming mode, if one or both
	 * sides request it.
	 */
2507
	if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
2508
		chan->fcs = L2CAP_FCS_NONE;
2509
	else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state))
2510
		chan->fcs = L2CAP_FCS_CRC16;
2511 2512
}

2513
static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
L
Linus Torvalds 已提交
2514 2515 2516 2517
{
	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
	u16 dcid, flags;
	u8 rsp[64];
2518
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2519
	struct sock *sk;
2520
	int len;
L
Linus Torvalds 已提交
2521 2522 2523 2524 2525 2526

	dcid  = __le16_to_cpu(req->dcid);
	flags = __le16_to_cpu(req->flags);

	BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);

2527
	chan = l2cap_get_chan_by_scid(conn, dcid);
2528
	if (!chan)
L
Linus Torvalds 已提交
2529 2530
		return -ENOENT;

2531 2532
	sk = chan->sk;

2533
	if (chan->state != BT_CONFIG) {
2534 2535 2536 2537 2538
		struct l2cap_cmd_rej rej;

		rej.reason = cpu_to_le16(0x0002);
		l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
				sizeof(rej), &rej);
2539
		goto unlock;
2540
	}
2541

2542
	/* Reject if config buffer is too small. */
2543
	len = cmd_len - sizeof(*req);
2544
	if (chan->conf_len + len > sizeof(chan->conf_req)) {
2545
		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2546
				l2cap_build_conf_rsp(chan, rsp,
2547 2548 2549 2550 2551
					L2CAP_CONF_REJECT, flags), rsp);
		goto unlock;
	}

	/* Store config. */
2552 2553
	memcpy(chan->conf_req + chan->conf_len, req->data, len);
	chan->conf_len += len;
L
Linus Torvalds 已提交
2554 2555 2556 2557

	if (flags & 0x0001) {
		/* Incomplete config. Send empty response. */
		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2558
				l2cap_build_conf_rsp(chan, rsp,
2559
					L2CAP_CONF_SUCCESS, 0x0001), rsp);
L
Linus Torvalds 已提交
2560 2561 2562 2563
		goto unlock;
	}

	/* Complete config. */
2564
	len = l2cap_parse_conf_req(chan, rsp);
2565
	if (len < 0) {
2566
		l2cap_send_disconn_req(conn, chan, ECONNRESET);
L
Linus Torvalds 已提交
2567
		goto unlock;
2568
	}
L
Linus Torvalds 已提交
2569

2570
	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
2571
	chan->num_conf_rsp++;
2572 2573

	/* Reset config buffer. */
2574
	chan->conf_len = 0;
2575

2576
	if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
2577 2578
		goto unlock;

2579
	if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
2580
		set_default_fcs(chan);
2581

2582
		l2cap_state_change(chan, BT_CONNECTED);
2583

2584 2585
		chan->next_tx_seq = 0;
		chan->expected_tx_seq = 0;
2586
		skb_queue_head_init(&chan->tx_q);
2587
		if (chan->mode == L2CAP_MODE_ERTM)
2588
			l2cap_ertm_init(chan);
2589

L
Linus Torvalds 已提交
2590
		l2cap_chan_ready(sk);
2591 2592 2593
		goto unlock;
	}

2594
	if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
2595
		u8 buf[64];
L
Linus Torvalds 已提交
2596
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2597 2598
					l2cap_build_conf_req(chan, buf), buf);
		chan->num_conf_req++;
L
Linus Torvalds 已提交
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
	}

unlock:
	bh_unlock_sock(sk);
	return 0;
}

static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
{
	struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
	u16 scid, flags, result;
2610
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2611
	struct sock *sk;
2612
	int len = cmd->len - sizeof(*rsp);
L
Linus Torvalds 已提交
2613 2614 2615 2616 2617

	scid   = __le16_to_cpu(rsp->scid);
	flags  = __le16_to_cpu(rsp->flags);
	result = __le16_to_cpu(rsp->result);

2618 2619
	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
			scid, flags, result);
L
Linus Torvalds 已提交
2620

2621
	chan = l2cap_get_chan_by_scid(conn, scid);
2622
	if (!chan)
L
Linus Torvalds 已提交
2623 2624
		return 0;

2625 2626
	sk = chan->sk;

L
Linus Torvalds 已提交
2627 2628
	switch (result) {
	case L2CAP_CONF_SUCCESS:
2629
		l2cap_conf_rfc_get(chan, rsp->data, len);
L
Linus Torvalds 已提交
2630 2631 2632
		break;

	case L2CAP_CONF_UNACCEPT:
2633
		if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
2634 2635
			char req[64];

2636
			if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
2637
				l2cap_send_disconn_req(conn, chan, ECONNRESET);
2638 2639 2640
				goto done;
			}

2641 2642
			/* throw out any old stored conf requests */
			result = L2CAP_CONF_SUCCESS;
2643 2644
			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
								req, &result);
2645
			if (len < 0) {
2646
				l2cap_send_disconn_req(conn, chan, ECONNRESET);
2647 2648 2649 2650 2651
				goto done;
			}

			l2cap_send_cmd(conn, l2cap_get_ident(conn),
						L2CAP_CONF_REQ, len, req);
2652
			chan->num_conf_req++;
2653 2654 2655
			if (result != L2CAP_CONF_SUCCESS)
				goto done;
			break;
L
Linus Torvalds 已提交
2656 2657
		}

2658
	default:
2659
		sk->sk_err = ECONNRESET;
2660
		__set_chan_timer(chan, HZ * 5);
2661
		l2cap_send_disconn_req(conn, chan, ECONNRESET);
L
Linus Torvalds 已提交
2662 2663 2664 2665 2666 2667
		goto done;
	}

	if (flags & 0x01)
		goto done;

2668
	set_bit(CONF_INPUT_DONE, &chan->conf_state);
L
Linus Torvalds 已提交
2669

2670
	if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
2671
		set_default_fcs(chan);
2672

2673
		l2cap_state_change(chan, BT_CONNECTED);
2674 2675
		chan->next_tx_seq = 0;
		chan->expected_tx_seq = 0;
2676
		skb_queue_head_init(&chan->tx_q);
2677
		if (chan->mode ==  L2CAP_MODE_ERTM)
2678
			l2cap_ertm_init(chan);
2679

L
Linus Torvalds 已提交
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692
		l2cap_chan_ready(sk);
	}

done:
	bh_unlock_sock(sk);
	return 0;
}

static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
{
	struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
	struct l2cap_disconn_rsp rsp;
	u16 dcid, scid;
2693
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2694 2695 2696 2697 2698 2699 2700
	struct sock *sk;

	scid = __le16_to_cpu(req->scid);
	dcid = __le16_to_cpu(req->dcid);

	BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);

2701
	chan = l2cap_get_chan_by_scid(conn, dcid);
2702
	if (!chan)
L
Linus Torvalds 已提交
2703 2704
		return 0;

2705 2706
	sk = chan->sk;

2707 2708
	rsp.dcid = cpu_to_le16(chan->scid);
	rsp.scid = cpu_to_le16(chan->dcid);
L
Linus Torvalds 已提交
2709 2710 2711 2712
	l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);

	sk->sk_shutdown = SHUTDOWN_MASK;

2713 2714
	/* don't delete l2cap channel if sk is owned by user */
	if (sock_owned_by_user(sk)) {
2715
		l2cap_state_change(chan, BT_DISCONN);
2716 2717
		__clear_chan_timer(chan);
		__set_chan_timer(chan, HZ / 5);
2718 2719 2720 2721
		bh_unlock_sock(sk);
		return 0;
	}

2722
	l2cap_chan_del(chan, ECONNRESET);
L
Linus Torvalds 已提交
2723 2724
	bh_unlock_sock(sk);

2725
	chan->ops->close(chan->data);
L
Linus Torvalds 已提交
2726 2727 2728 2729 2730 2731 2732
	return 0;
}

static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
{
	struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
	u16 dcid, scid;
2733
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2734 2735 2736 2737 2738 2739 2740
	struct sock *sk;

	scid = __le16_to_cpu(rsp->scid);
	dcid = __le16_to_cpu(rsp->dcid);

	BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);

2741
	chan = l2cap_get_chan_by_scid(conn, scid);
2742
	if (!chan)
L
Linus Torvalds 已提交
2743 2744
		return 0;

2745 2746
	sk = chan->sk;

2747 2748
	/* don't delete l2cap channel if sk is owned by user */
	if (sock_owned_by_user(sk)) {
2749
		l2cap_state_change(chan,BT_DISCONN);
2750 2751
		__clear_chan_timer(chan);
		__set_chan_timer(chan, HZ / 5);
2752 2753 2754 2755
		bh_unlock_sock(sk);
		return 0;
	}

2756
	l2cap_chan_del(chan, 0);
L
Linus Torvalds 已提交
2757 2758
	bh_unlock_sock(sk);

2759
	chan->ops->close(chan->data);
L
Linus Torvalds 已提交
2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
	return 0;
}

static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
{
	struct l2cap_info_req *req = (struct l2cap_info_req *) data;
	u16 type;

	type = __le16_to_cpu(req->type);

	BT_DBG("type 0x%4.4x", type);

2772 2773
	if (type == L2CAP_IT_FEAT_MASK) {
		u8 buf[8];
2774
		u32 feat_mask = l2cap_feat_mask;
2775 2776 2777
		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
		rsp->type   = cpu_to_le16(L2CAP_IT_FEAT_MASK);
		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
2778
		if (!disable_ertm)
2779 2780
			feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
							 | L2CAP_FEAT_FCS;
2781
		put_unaligned_le32(feat_mask, rsp->data);
2782 2783
		l2cap_send_cmd(conn, cmd->ident,
					L2CAP_INFO_RSP, sizeof(buf), buf);
2784 2785 2786 2787 2788 2789 2790 2791
	} else if (type == L2CAP_IT_FIXED_CHAN) {
		u8 buf[12];
		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
		rsp->type   = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
		memcpy(buf + 4, l2cap_fixed_chan, 8);
		l2cap_send_cmd(conn, cmd->ident,
					L2CAP_INFO_RSP, sizeof(buf), buf);
2792 2793 2794 2795 2796 2797 2798
	} else {
		struct l2cap_info_rsp rsp;
		rsp.type   = cpu_to_le16(type);
		rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
		l2cap_send_cmd(conn, cmd->ident,
					L2CAP_INFO_RSP, sizeof(rsp), &rsp);
	}
L
Linus Torvalds 已提交
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812

	return 0;
}

static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
{
	struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
	u16 type, result;

	type   = __le16_to_cpu(rsp->type);
	result = __le16_to_cpu(rsp->result);

	BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);

2813 2814 2815 2816 2817
	/* L2CAP Info req/rsp are unbound to channels, add extra checks */
	if (cmd->ident != conn->info_ident ||
			conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
		return 0;

2818 2819
	del_timer(&conn->info_timer);

2820 2821 2822 2823 2824 2825 2826 2827 2828
	if (result != L2CAP_IR_SUCCESS) {
		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
		conn->info_ident = 0;

		l2cap_conn_start(conn);

		return 0;
	}

2829
	if (type == L2CAP_IT_FEAT_MASK) {
2830
		conn->feat_mask = get_unaligned_le32(rsp->data);
2831

2832
		if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
			struct l2cap_info_req req;
			req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);

			conn->info_ident = l2cap_get_ident(conn);

			l2cap_send_cmd(conn, conn->info_ident,
					L2CAP_INFO_REQ, sizeof(req), &req);
		} else {
			conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
			conn->info_ident = 0;

			l2cap_conn_start(conn);
		}
	} else if (type == L2CAP_IT_FIXED_CHAN) {
2847
		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
2848
		conn->info_ident = 0;
2849 2850 2851

		l2cap_conn_start(conn);
	}
2852

L
Linus Torvalds 已提交
2853 2854 2855
	return 0;
}

2856
static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
							u16 to_multiplier)
{
	u16 max_latency;

	if (min > max || min < 6 || max > 3200)
		return -EINVAL;

	if (to_multiplier < 10 || to_multiplier > 3200)
		return -EINVAL;

	if (max >= to_multiplier * 8)
		return -EINVAL;

	max_latency = (to_multiplier * 8 / max) - 1;
	if (latency > 499 || latency > max_latency)
		return -EINVAL;

	return 0;
}

static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
					struct l2cap_cmd_hdr *cmd, u8 *data)
{
	struct hci_conn *hcon = conn->hcon;
	struct l2cap_conn_param_update_req *req;
	struct l2cap_conn_param_update_rsp rsp;
	u16 min, max, latency, to_multiplier, cmd_len;
2884
	int err;
2885 2886 2887 2888 2889 2890 2891 2892 2893

	if (!(hcon->link_mode & HCI_LM_MASTER))
		return -EINVAL;

	cmd_len = __le16_to_cpu(cmd->len);
	if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
		return -EPROTO;

	req = (struct l2cap_conn_param_update_req *) data;
2894 2895
	min		= __le16_to_cpu(req->min);
	max		= __le16_to_cpu(req->max);
2896 2897 2898 2899 2900 2901 2902
	latency		= __le16_to_cpu(req->latency);
	to_multiplier	= __le16_to_cpu(req->to_multiplier);

	BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
						min, max, latency, to_multiplier);

	memset(&rsp, 0, sizeof(rsp));
2903 2904 2905

	err = l2cap_check_conn_param(min, max, latency, to_multiplier);
	if (err)
2906 2907 2908 2909 2910 2911 2912
		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
	else
		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);

	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
							sizeof(rsp), &rsp);

2913 2914 2915
	if (!err)
		hci_le_conn_update(hcon, min, max, latency, to_multiplier);

2916 2917 2918
	return 0;
}

2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
			struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
{
	int err = 0;

	switch (cmd->code) {
	case L2CAP_COMMAND_REJ:
		l2cap_command_rej(conn, cmd, data);
		break;

	case L2CAP_CONN_REQ:
		err = l2cap_connect_req(conn, cmd, data);
		break;

	case L2CAP_CONN_RSP:
		err = l2cap_connect_rsp(conn, cmd, data);
		break;

	case L2CAP_CONF_REQ:
		err = l2cap_config_req(conn, cmd, cmd_len, data);
		break;

	case L2CAP_CONF_RSP:
		err = l2cap_config_rsp(conn, cmd, data);
		break;

	case L2CAP_DISCONN_REQ:
		err = l2cap_disconnect_req(conn, cmd, data);
		break;

	case L2CAP_DISCONN_RSP:
		err = l2cap_disconnect_rsp(conn, cmd, data);
		break;

	case L2CAP_ECHO_REQ:
		l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
		break;

	case L2CAP_ECHO_RSP:
		break;

	case L2CAP_INFO_REQ:
		err = l2cap_information_req(conn, cmd, data);
		break;

	case L2CAP_INFO_RSP:
		err = l2cap_information_rsp(conn, cmd, data);
		break;

	default:
		BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
		err = -EINVAL;
		break;
	}

	return err;
}

static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
					struct l2cap_cmd_hdr *cmd, u8 *data)
{
	switch (cmd->code) {
	case L2CAP_COMMAND_REJ:
		return 0;

	case L2CAP_CONN_PARAM_UPDATE_REQ:
2985
		return l2cap_conn_param_update_req(conn, cmd, data);
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997

	case L2CAP_CONN_PARAM_UPDATE_RSP:
		return 0;

	default:
		BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
		return -EINVAL;
	}
}

static inline void l2cap_sig_channel(struct l2cap_conn *conn,
							struct sk_buff *skb)
L
Linus Torvalds 已提交
2998 2999 3000 3001
{
	u8 *data = skb->data;
	int len = skb->len;
	struct l2cap_cmd_hdr cmd;
3002
	int err;
L
Linus Torvalds 已提交
3003 3004 3005 3006

	l2cap_raw_recv(conn, skb);

	while (len >= L2CAP_CMD_HDR_SIZE) {
3007
		u16 cmd_len;
L
Linus Torvalds 已提交
3008 3009 3010 3011
		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
		data += L2CAP_CMD_HDR_SIZE;
		len  -= L2CAP_CMD_HDR_SIZE;

3012
		cmd_len = le16_to_cpu(cmd.len);
L
Linus Torvalds 已提交
3013

3014
		BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident);
L
Linus Torvalds 已提交
3015

3016
		if (cmd_len > len || !cmd.ident) {
L
Linus Torvalds 已提交
3017 3018 3019 3020
			BT_DBG("corrupted command");
			break;
		}

3021 3022 3023 3024
		if (conn->hcon->type == LE_LINK)
			err = l2cap_le_sig_cmd(conn, &cmd, data);
		else
			err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
L
Linus Torvalds 已提交
3025 3026 3027

		if (err) {
			struct l2cap_cmd_rej rej;
3028 3029

			BT_ERR("Wrong link type (%d)", err);
L
Linus Torvalds 已提交
3030 3031

			/* FIXME: Map err to a valid reason */
3032
			rej.reason = cpu_to_le16(0);
L
Linus Torvalds 已提交
3033 3034 3035
			l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
		}

3036 3037
		data += cmd_len;
		len  -= cmd_len;
L
Linus Torvalds 已提交
3038 3039 3040 3041 3042
	}

	kfree_skb(skb);
}

3043
static int l2cap_check_fcs(struct l2cap_chan *chan,  struct sk_buff *skb)
3044 3045 3046 3047
{
	u16 our_fcs, rcv_fcs;
	int hdr_size = L2CAP_HDR_SIZE + 2;

3048
	if (chan->fcs == L2CAP_FCS_CRC16) {
3049 3050 3051 3052 3053
		skb_trim(skb, skb->len - 2);
		rcv_fcs = get_unaligned_le16(skb->data + skb->len);
		our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);

		if (our_fcs != rcv_fcs)
3054
			return -EBADMSG;
3055 3056 3057 3058
	}
	return 0;
}

3059
static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
3060 3061 3062
{
	u16 control = 0;

3063
	chan->frames_sent = 0;
3064

3065
	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3066

3067
	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
3068
		control |= L2CAP_SUPER_RCV_NOT_READY;
3069
		l2cap_send_sframe(chan, control);
3070
		set_bit(CONN_RNR_SENT, &chan->conn_state);
3071 3072
	}

3073
	if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
3074
		l2cap_retransmit_frames(chan);
3075

3076
	l2cap_ertm_send(chan);
3077

3078
	if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
3079
			chan->frames_sent == 0) {
3080
		control |= L2CAP_SUPER_RCV_READY;
3081
		l2cap_send_sframe(chan, control);
3082 3083 3084
	}
}

3085
static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar)
3086 3087
{
	struct sk_buff *next_skb;
3088
	int tx_seq_offset, next_tx_seq_offset;
3089 3090 3091 3092

	bt_cb(skb)->tx_seq = tx_seq;
	bt_cb(skb)->sar = sar;

3093
	next_skb = skb_peek(&chan->srej_q);
3094
	if (!next_skb) {
3095
		__skb_queue_tail(&chan->srej_q, skb);
3096
		return 0;
3097 3098
	}

3099
	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
3100 3101 3102
	if (tx_seq_offset < 0)
		tx_seq_offset += 64;

3103
	do {
3104 3105 3106
		if (bt_cb(next_skb)->tx_seq == tx_seq)
			return -EINVAL;

3107
		next_tx_seq_offset = (bt_cb(next_skb)->tx_seq -
3108
						chan->buffer_seq) % 64;
3109 3110 3111 3112
		if (next_tx_seq_offset < 0)
			next_tx_seq_offset += 64;

		if (next_tx_seq_offset > tx_seq_offset) {
3113
			__skb_queue_before(&chan->srej_q, next_skb, skb);
3114
			return 0;
3115 3116
		}

3117
		if (skb_queue_is_last(&chan->srej_q, next_skb))
3118 3119
			break;

3120
	} while ((next_skb = skb_queue_next(&chan->srej_q, next_skb)));
3121

3122
	__skb_queue_tail(&chan->srej_q, skb);
3123 3124

	return 0;
3125 3126
}

3127
static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
3128 3129
{
	struct sk_buff *_skb;
3130
	int err;
3131 3132 3133

	switch (control & L2CAP_CTRL_SAR) {
	case L2CAP_SDU_UNSEGMENTED:
3134
		if (test_bit(CONN_SAR_SDU, &chan->conn_state))
3135 3136
			goto drop;

3137
		return chan->ops->recv(chan->data, skb);
3138 3139

	case L2CAP_SDU_START:
3140
		if (test_bit(CONN_SAR_SDU, &chan->conn_state))
3141 3142
			goto drop;

3143
		chan->sdu_len = get_unaligned_le16(skb->data);
3144

3145
		if (chan->sdu_len > chan->imtu)
3146 3147
			goto disconnect;

3148 3149
		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
		if (!chan->sdu)
3150 3151 3152 3153 3154 3155
			return -ENOMEM;

		/* pull sdu_len bytes only after alloc, because of Local Busy
		 * condition we have to be sure that this will be executed
		 * only once, i.e., when alloc does not fail */
		skb_pull(skb, 2);
3156

3157
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3158

3159
		set_bit(CONN_SAR_SDU, &chan->conn_state);
3160
		chan->partial_sdu_len = skb->len;
3161 3162 3163
		break;

	case L2CAP_SDU_CONTINUE:
3164
		if (!test_bit(CONN_SAR_SDU, &chan->conn_state))
3165 3166
			goto disconnect;

3167
		if (!chan->sdu)
3168 3169
			goto disconnect;

3170 3171
		chan->partial_sdu_len += skb->len;
		if (chan->partial_sdu_len > chan->sdu_len)
3172 3173
			goto drop;

3174
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3175

3176 3177 3178
		break;

	case L2CAP_SDU_END:
3179
		if (!test_bit(CONN_SAR_SDU, &chan->conn_state))
3180 3181
			goto disconnect;

3182
		if (!chan->sdu)
3183 3184
			goto disconnect;

3185
		if (!test_bit(CONN_SAR_RETRY, &chan->conn_state)) {
3186
			chan->partial_sdu_len += skb->len;
3187

3188
			if (chan->partial_sdu_len > chan->imtu)
3189
				goto drop;
3190

3191
			if (chan->partial_sdu_len != chan->sdu_len)
3192
				goto drop;
3193

3194
			memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3195
		}
3196

3197
		_skb = skb_clone(chan->sdu, GFP_ATOMIC);
3198
		if (!_skb) {
3199
			set_bit(CONN_SAR_RETRY, &chan->conn_state);
3200 3201 3202
			return -ENOMEM;
		}

3203
		err = chan->ops->recv(chan->data, _skb);
3204
		if (err < 0) {
3205
			kfree_skb(_skb);
3206
			set_bit(CONN_SAR_RETRY, &chan->conn_state);
3207 3208 3209
			return err;
		}

3210 3211
		clear_bit(CONN_SAR_RETRY, &chan->conn_state);
		clear_bit(CONN_SAR_SDU, &chan->conn_state);
3212

3213
		kfree_skb(chan->sdu);
3214 3215 3216 3217
		break;
	}

	kfree_skb(skb);
3218
	return 0;
3219 3220

drop:
3221 3222
	kfree_skb(chan->sdu);
	chan->sdu = NULL;
3223 3224

disconnect:
3225
	l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3226 3227 3228 3229
	kfree_skb(skb);
	return 0;
}

3230
static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan)
3231 3232 3233
{
	u16 control;

3234
	BT_DBG("chan %p, Enter local busy", chan);
3235

3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249
	set_bit(CONN_LOCAL_BUSY, &chan->conn_state);

	control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
	control |= L2CAP_SUPER_RCV_NOT_READY;
	l2cap_send_sframe(chan, control);

	set_bit(CONN_RNR_SENT, &chan->conn_state);

	__clear_ack_timer(chan);
}

static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan)
{
	u16 control;
3250

3251
	if (!test_bit(CONN_RNR_SENT, &chan->conn_state))
3252 3253
		goto done;

3254
	control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3255
	control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL;
3256
	l2cap_send_sframe(chan, control);
3257
	chan->retry_count = 1;
3258

3259 3260
	__clear_retrans_timer(chan);
	__set_monitor_timer(chan);
3261

3262
	set_bit(CONN_WAIT_F, &chan->conn_state);
3263 3264

done:
3265 3266
	clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
	clear_bit(CONN_RNR_SENT, &chan->conn_state);
3267

3268
	BT_DBG("chan %p, Exit local busy", chan);
3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288
}

static int l2cap_try_push_rx_skb(struct l2cap_chan *chan)
{
	struct sk_buff *skb;
	u16 control;
	int err;

	while ((skb = skb_dequeue(&chan->busy_q))) {
		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
		err = l2cap_ertm_reassembly_sdu(chan, skb, control);
		if (err < 0) {
			skb_queue_head(&chan->busy_q, skb);
			return -EBUSY;
		}

		chan->buffer_seq = (chan->buffer_seq + 1) % 64;
	}

	l2cap_ertm_exit_local_busy(chan);
3289 3290 3291 3292

	return 0;
}

3293 3294 3295
static void l2cap_busy_work(struct work_struct *work)
{
	DECLARE_WAITQUEUE(wait, current);
3296 3297 3298
	struct l2cap_chan *chan =
		container_of(work, struct l2cap_chan, busy_work);
	struct sock *sk = chan->sk;
3299 3300 3301 3302 3303
	int n_tries = 0, timeo = HZ/5, err;
	struct sk_buff *skb;

	lock_sock(sk);

3304
	add_wait_queue(sk_sleep(sk), &wait);
3305
	while ((skb = skb_peek(&chan->busy_q))) {
3306 3307 3308 3309
		set_current_state(TASK_INTERRUPTIBLE);

		if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) {
			err = -EBUSY;
3310
			l2cap_send_disconn_req(chan->conn, chan, EBUSY);
3311
			break;
3312 3313 3314 3315 3316 3317 3318
		}

		if (!timeo)
			timeo = HZ/5;

		if (signal_pending(current)) {
			err = sock_intr_errno(timeo);
3319
			break;
3320 3321 3322 3323 3324 3325 3326 3327
		}

		release_sock(sk);
		timeo = schedule_timeout(timeo);
		lock_sock(sk);

		err = sock_error(sk);
		if (err)
3328
			break;
3329

3330
		if (l2cap_try_push_rx_skb(chan) == 0)
3331 3332 3333 3334
			break;
	}

	set_current_state(TASK_RUNNING);
3335
	remove_wait_queue(sk_sleep(sk), &wait);
3336 3337 3338 3339

	release_sock(sk);
}

3340
static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
3341
{
3342
	int err;
3343

3344
	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
3345
		bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3346
		__skb_queue_tail(&chan->busy_q, skb);
3347
		return l2cap_try_push_rx_skb(chan);
3348 3349


3350 3351
	}

3352
	err = l2cap_ertm_reassembly_sdu(chan, skb, control);
3353
	if (err >= 0) {
3354
		chan->buffer_seq = (chan->buffer_seq + 1) % 64;
3355 3356 3357
		return err;
	}

3358
	l2cap_ertm_enter_local_busy(chan);
3359

3360
	bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3361
	__skb_queue_tail(&chan->busy_q, skb);
3362

3363
	queue_work(_busy_wq, &chan->busy_work);
3364 3365 3366 3367

	return err;
}

3368
static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
3369 3370 3371 3372
{
	struct sk_buff *_skb;
	int err = -EINVAL;

3373 3374 3375 3376 3377
	/*
	 * TODO: We have to notify the userland if some data is lost with the
	 * Streaming Mode.
	 */

3378 3379
	switch (control & L2CAP_CTRL_SAR) {
	case L2CAP_SDU_UNSEGMENTED:
3380
		if (test_bit(CONN_SAR_SDU, &chan->conn_state)) {
3381
			kfree_skb(chan->sdu);
3382 3383 3384
			break;
		}

3385
		err = chan->ops->recv(chan->data, skb);
3386 3387 3388 3389 3390 3391
		if (!err)
			return 0;

		break;

	case L2CAP_SDU_START:
3392
		if (test_bit(CONN_SAR_SDU, &chan->conn_state)) {
3393
			kfree_skb(chan->sdu);
3394 3395 3396
			break;
		}

3397
		chan->sdu_len = get_unaligned_le16(skb->data);
3398 3399
		skb_pull(skb, 2);

3400
		if (chan->sdu_len > chan->imtu) {
3401 3402 3403 3404
			err = -EMSGSIZE;
			break;
		}

3405 3406
		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
		if (!chan->sdu) {
3407 3408 3409 3410
			err = -ENOMEM;
			break;
		}

3411
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3412

3413
		set_bit(CONN_SAR_SDU, &chan->conn_state);
3414
		chan->partial_sdu_len = skb->len;
3415 3416 3417 3418
		err = 0;
		break;

	case L2CAP_SDU_CONTINUE:
3419
		if (!test_bit(CONN_SAR_SDU, &chan->conn_state))
3420 3421
			break;

3422
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3423

3424 3425 3426
		chan->partial_sdu_len += skb->len;
		if (chan->partial_sdu_len > chan->sdu_len)
			kfree_skb(chan->sdu);
3427 3428 3429 3430 3431 3432
		else
			err = 0;

		break;

	case L2CAP_SDU_END:
3433
		if (!test_bit(CONN_SAR_SDU, &chan->conn_state))
3434 3435
			break;

3436
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3437

3438
		clear_bit(CONN_SAR_SDU, &chan->conn_state);
3439
		chan->partial_sdu_len += skb->len;
3440

3441
		if (chan->partial_sdu_len > chan->imtu)
3442 3443
			goto drop;

3444 3445
		if (chan->partial_sdu_len == chan->sdu_len) {
			_skb = skb_clone(chan->sdu, GFP_ATOMIC);
3446
			err = chan->ops->recv(chan->data, _skb);
3447 3448 3449 3450 3451
			if (err < 0)
				kfree_skb(_skb);
		}
		err = 0;

3452
drop:
3453
		kfree_skb(chan->sdu);
3454 3455 3456 3457 3458 3459 3460
		break;
	}

	kfree_skb(skb);
	return err;
}

3461
static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq)
3462 3463
{
	struct sk_buff *skb;
3464
	u16 control;
3465

3466
	while ((skb = skb_peek(&chan->srej_q))) {
3467 3468 3469
		if (bt_cb(skb)->tx_seq != tx_seq)
			break;

3470
		skb = skb_dequeue(&chan->srej_q);
3471
		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3472
		l2cap_ertm_reassembly_sdu(chan, skb, control);
3473 3474
		chan->buffer_seq_srej =
			(chan->buffer_seq_srej + 1) % 64;
3475
		tx_seq = (tx_seq + 1) % 64;
3476 3477 3478
	}
}

3479
static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq)
3480 3481 3482 3483
{
	struct srej_list *l, *tmp;
	u16 control;

3484
	list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
3485 3486 3487 3488 3489 3490 3491
		if (l->tx_seq == tx_seq) {
			list_del(&l->list);
			kfree(l);
			return;
		}
		control = L2CAP_SUPER_SELECT_REJECT;
		control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3492
		l2cap_send_sframe(chan, control);
3493
		list_del(&l->list);
3494
		list_add_tail(&l->list, &chan->srej_l);
3495 3496 3497
	}
}

3498
static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq)
3499 3500 3501 3502
{
	struct srej_list *new;
	u16 control;

3503
	while (tx_seq != chan->expected_tx_seq) {
3504
		control = L2CAP_SUPER_SELECT_REJECT;
3505
		control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3506
		l2cap_send_sframe(chan, control);
3507 3508

		new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC);
3509 3510
		new->tx_seq = chan->expected_tx_seq;
		chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3511
		list_add_tail(&new->list, &chan->srej_l);
3512
	}
3513
	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3514 3515
}

3516
static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
3517 3518
{
	u8 tx_seq = __get_txseq(rx_control);
3519
	u8 req_seq = __get_reqseq(rx_control);
3520
	u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT;
3521
	int tx_seq_offset, expected_tx_seq_offset;
3522
	int num_to_ack = (chan->tx_win/6) + 1;
3523 3524
	int err = 0;

3525 3526
	BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len,
							tx_seq, rx_control);
3527

3528
	if (L2CAP_CTRL_FINAL & rx_control &&
3529
			test_bit(CONN_WAIT_F, &chan->conn_state)) {
3530
		__clear_monitor_timer(chan);
3531
		if (chan->unacked_frames > 0)
3532
			__set_retrans_timer(chan);
3533
		clear_bit(CONN_WAIT_F, &chan->conn_state);
3534 3535
	}

3536 3537
	chan->expected_ack_seq = req_seq;
	l2cap_drop_acked_frames(chan);
3538

3539
	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
3540 3541 3542 3543
	if (tx_seq_offset < 0)
		tx_seq_offset += 64;

	/* invalid tx_seq */
3544
	if (tx_seq_offset >= chan->tx_win) {
3545
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3546 3547 3548
		goto drop;
	}

3549
	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
3550 3551
		goto drop;

3552 3553 3554
	if (tx_seq == chan->expected_tx_seq)
		goto expected;

3555
	if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
3556
		struct srej_list *first;
3557

3558
		first = list_first_entry(&chan->srej_l,
3559 3560
				struct srej_list, list);
		if (tx_seq == first->tx_seq) {
3561
			l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3562
			l2cap_check_srej_gap(chan, tx_seq);
3563 3564 3565 3566

			list_del(&first->list);
			kfree(first);

3567
			if (list_empty(&chan->srej_l)) {
3568
				chan->buffer_seq = chan->buffer_seq_srej;
3569
				clear_bit(CONN_SREJ_SENT, &chan->conn_state);
3570
				l2cap_send_ack(chan);
3571
				BT_DBG("chan %p, Exit SREJ_SENT", chan);
3572 3573 3574
			}
		} else {
			struct srej_list *l;
3575 3576

			/* duplicated tx_seq */
3577
			if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0)
3578
				goto drop;
3579

3580
			list_for_each_entry(l, &chan->srej_l, list) {
3581
				if (l->tx_seq == tx_seq) {
3582
					l2cap_resend_srejframe(chan, tx_seq);
3583 3584 3585
					return 0;
				}
			}
3586
			l2cap_send_srejframe(chan, tx_seq);
3587 3588
		}
	} else {
3589
		expected_tx_seq_offset =
3590
			(chan->expected_tx_seq - chan->buffer_seq) % 64;
3591 3592 3593 3594 3595 3596 3597
		if (expected_tx_seq_offset < 0)
			expected_tx_seq_offset += 64;

		/* duplicated tx_seq */
		if (tx_seq_offset < expected_tx_seq_offset)
			goto drop;

3598
		set_bit(CONN_SREJ_SENT, &chan->conn_state);
3599

3600
		BT_DBG("chan %p, Enter SREJ", chan);
3601

3602
		INIT_LIST_HEAD(&chan->srej_l);
3603
		chan->buffer_seq_srej = chan->buffer_seq;
3604

3605 3606
		__skb_queue_head_init(&chan->srej_q);
		__skb_queue_head_init(&chan->busy_q);
3607
		l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3608

3609
		set_bit(CONN_SEND_PBIT, &chan->conn_state);
3610

3611
		l2cap_send_srejframe(chan, tx_seq);
3612

3613
		__clear_ack_timer(chan);
3614
	}
3615 3616
	return 0;

3617
expected:
3618
	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3619

3620
	if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
3621 3622
		bt_cb(skb)->tx_seq = tx_seq;
		bt_cb(skb)->sar = sar;
3623
		__skb_queue_tail(&chan->srej_q, skb);
3624 3625 3626
		return 0;
	}

3627
	err = l2cap_push_rx_skb(chan, skb, rx_control);
3628 3629 3630
	if (err < 0)
		return 0;

3631
	if (rx_control & L2CAP_CTRL_FINAL) {
3632
		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
3633
			l2cap_retransmit_frames(chan);
3634 3635
	}

3636
	__set_ack_timer(chan);
3637

3638 3639
	chan->num_acked = (chan->num_acked + 1) % num_to_ack;
	if (chan->num_acked == num_to_ack - 1)
3640
		l2cap_send_ack(chan);
3641

3642
	return 0;
3643 3644 3645 3646

drop:
	kfree_skb(skb);
	return 0;
3647 3648
}

3649
static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control)
3650
{
3651
	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control),
3652 3653
						rx_control);

3654 3655
	chan->expected_ack_seq = __get_reqseq(rx_control);
	l2cap_drop_acked_frames(chan);
3656

3657
	if (rx_control & L2CAP_CTRL_POLL) {
3658 3659 3660
		set_bit(CONN_SEND_FBIT, &chan->conn_state);
		if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
			if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
3661
					(chan->unacked_frames > 0))
3662
				__set_retrans_timer(chan);
3663

3664
			clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
3665
			l2cap_send_srejtail(chan);
3666
		} else {
3667
			l2cap_send_i_or_rr_or_rnr(chan);
3668
		}
3669

3670
	} else if (rx_control & L2CAP_CTRL_FINAL) {
3671
		clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
3672

3673
		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
3674
			l2cap_retransmit_frames(chan);
3675

3676
	} else {
3677
		if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
3678
				(chan->unacked_frames > 0))
3679
			__set_retrans_timer(chan);
3680

3681 3682
		clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
		if (test_bit(CONN_SREJ_SENT, &chan->conn_state))
3683
			l2cap_send_ack(chan);
3684
		else
3685
			l2cap_ertm_send(chan);
3686 3687
	}
}
3688

3689
static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control)
3690 3691
{
	u8 tx_seq = __get_reqseq(rx_control);
3692

3693
	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
3694

3695
	clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
3696

3697 3698
	chan->expected_ack_seq = tx_seq;
	l2cap_drop_acked_frames(chan);
3699 3700

	if (rx_control & L2CAP_CTRL_FINAL) {
3701
		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
3702
			l2cap_retransmit_frames(chan);
3703
	} else {
3704
		l2cap_retransmit_frames(chan);
3705

3706 3707
		if (test_bit(CONN_WAIT_F, &chan->conn_state))
			set_bit(CONN_REJ_ACT, &chan->conn_state);
3708 3709
	}
}
3710
static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control)
3711 3712
{
	u8 tx_seq = __get_reqseq(rx_control);
3713

3714
	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
3715

3716
	clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
3717

3718
	if (rx_control & L2CAP_CTRL_POLL) {
3719 3720
		chan->expected_ack_seq = tx_seq;
		l2cap_drop_acked_frames(chan);
3721

3722
		set_bit(CONN_SEND_FBIT, &chan->conn_state);
3723
		l2cap_retransmit_one_frame(chan, tx_seq);
3724

3725
		l2cap_ertm_send(chan);
3726

3727
		if (test_bit(CONN_WAIT_F, &chan->conn_state)) {
3728
			chan->srej_save_reqseq = tx_seq;
3729
			set_bit(CONN_SREJ_ACT, &chan->conn_state);
3730
		}
3731
	} else if (rx_control & L2CAP_CTRL_FINAL) {
3732
		if (test_bit(CONN_SREJ_ACT, &chan->conn_state) &&
3733
				chan->srej_save_reqseq == tx_seq)
3734
			clear_bit(CONN_SREJ_ACT, &chan->conn_state);
3735
		else
3736
			l2cap_retransmit_one_frame(chan, tx_seq);
3737
	} else {
3738
		l2cap_retransmit_one_frame(chan, tx_seq);
3739
		if (test_bit(CONN_WAIT_F, &chan->conn_state)) {
3740
			chan->srej_save_reqseq = tx_seq;
3741
			set_bit(CONN_SREJ_ACT, &chan->conn_state);
3742
		}
3743 3744 3745
	}
}

3746
static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control)
3747 3748 3749
{
	u8 tx_seq = __get_reqseq(rx_control);

3750
	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
3751

3752
	set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
3753 3754
	chan->expected_ack_seq = tx_seq;
	l2cap_drop_acked_frames(chan);
3755

3756
	if (rx_control & L2CAP_CTRL_POLL)
3757
		set_bit(CONN_SEND_FBIT, &chan->conn_state);
3758

3759
	if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
3760
		__clear_retrans_timer(chan);
3761
		if (rx_control & L2CAP_CTRL_POLL)
3762
			l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL);
3763
		return;
3764
	}
3765 3766

	if (rx_control & L2CAP_CTRL_POLL)
3767
		l2cap_send_srejtail(chan);
3768
	else
3769
		l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY);
3770 3771
}

3772
static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
3773
{
3774
	BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len);
3775

3776
	if (L2CAP_CTRL_FINAL & rx_control &&
3777
			test_bit(CONN_WAIT_F, &chan->conn_state)) {
3778
		__clear_monitor_timer(chan);
3779
		if (chan->unacked_frames > 0)
3780
			__set_retrans_timer(chan);
3781
		clear_bit(CONN_WAIT_F, &chan->conn_state);
3782 3783 3784 3785
	}

	switch (rx_control & L2CAP_CTRL_SUPERVISE) {
	case L2CAP_SUPER_RCV_READY:
3786
		l2cap_data_channel_rrframe(chan, rx_control);
3787 3788
		break;

3789
	case L2CAP_SUPER_REJECT:
3790
		l2cap_data_channel_rejframe(chan, rx_control);
3791
		break;
3792

3793
	case L2CAP_SUPER_SELECT_REJECT:
3794
		l2cap_data_channel_srejframe(chan, rx_control);
3795 3796 3797
		break;

	case L2CAP_SUPER_RCV_NOT_READY:
3798
		l2cap_data_channel_rnrframe(chan, rx_control);
3799 3800 3801
		break;
	}

3802
	kfree_skb(skb);
3803 3804 3805
	return 0;
}

3806 3807
static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb)
{
3808
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821
	u16 control;
	u8 req_seq;
	int len, next_tx_seq_offset, req_seq_offset;

	control = get_unaligned_le16(skb->data);
	skb_pull(skb, 2);
	len = skb->len;

	/*
	 * We can just drop the corrupted I-frame here.
	 * Receiver will miss it and start proper recovery
	 * procedures and ask retransmission.
	 */
3822
	if (l2cap_check_fcs(chan, skb))
3823 3824 3825 3826 3827
		goto drop;

	if (__is_sar_start(control) && __is_iframe(control))
		len -= 2;

3828
	if (chan->fcs == L2CAP_FCS_CRC16)
3829 3830
		len -= 2;

3831
	if (len > chan->mps) {
3832
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3833 3834 3835 3836
		goto drop;
	}

	req_seq = __get_reqseq(control);
3837
	req_seq_offset = (req_seq - chan->expected_ack_seq) % 64;
3838 3839 3840 3841
	if (req_seq_offset < 0)
		req_seq_offset += 64;

	next_tx_seq_offset =
3842
		(chan->next_tx_seq - chan->expected_ack_seq) % 64;
3843 3844 3845 3846 3847
	if (next_tx_seq_offset < 0)
		next_tx_seq_offset += 64;

	/* check for invalid req-seq */
	if (req_seq_offset > next_tx_seq_offset) {
3848
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3849 3850 3851 3852 3853
		goto drop;
	}

	if (__is_iframe(control)) {
		if (len < 0) {
3854
			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3855 3856 3857
			goto drop;
		}

3858
		l2cap_data_channel_iframe(chan, control, skb);
3859 3860 3861
	} else {
		if (len != 0) {
			BT_ERR("%d", len);
3862
			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3863 3864 3865
			goto drop;
		}

3866
		l2cap_data_channel_sframe(chan, control, skb);
3867 3868 3869 3870 3871 3872 3873 3874 3875
	}

	return 0;

drop:
	kfree_skb(skb);
	return 0;
}

L
Linus Torvalds 已提交
3876 3877
static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
{
3878
	struct l2cap_chan *chan;
3879
	struct sock *sk = NULL;
3880
	u16 control;
3881 3882
	u8 tx_seq;
	int len;
L
Linus Torvalds 已提交
3883

3884
	chan = l2cap_get_chan_by_scid(conn, cid);
3885
	if (!chan) {
L
Linus Torvalds 已提交
3886 3887 3888 3889
		BT_DBG("unknown cid 0x%4.4x", cid);
		goto drop;
	}

3890
	sk = chan->sk;
3891

3892
	BT_DBG("chan %p, len %d", chan, skb->len);
L
Linus Torvalds 已提交
3893

3894
	if (chan->state != BT_CONNECTED)
L
Linus Torvalds 已提交
3895 3896
		goto drop;

3897
	switch (chan->mode) {
3898 3899 3900 3901 3902
	case L2CAP_MODE_BASIC:
		/* If socket recv buffers overflows we drop data here
		 * which is *bad* because L2CAP has to be reliable.
		 * But we don't have any other choice. L2CAP doesn't
		 * provide flow control mechanism. */
L
Linus Torvalds 已提交
3903

3904
		if (chan->imtu < skb->len)
3905
			goto drop;
L
Linus Torvalds 已提交
3906

3907
		if (!chan->ops->recv(chan->data, skb))
3908 3909 3910 3911
			goto done;
		break;

	case L2CAP_MODE_ERTM:
3912 3913
		if (!sock_owned_by_user(sk)) {
			l2cap_ertm_data_rcv(sk, skb);
3914
		} else {
3915
			if (sk_add_backlog(sk, skb))
3916 3917
				goto drop;
		}
3918

3919
		goto done;
3920

3921 3922 3923 3924 3925
	case L2CAP_MODE_STREAMING:
		control = get_unaligned_le16(skb->data);
		skb_pull(skb, 2);
		len = skb->len;

3926
		if (l2cap_check_fcs(chan, skb))
3927 3928
			goto drop;

3929 3930 3931
		if (__is_sar_start(control))
			len -= 2;

3932
		if (chan->fcs == L2CAP_FCS_CRC16)
3933 3934
			len -= 2;

3935
		if (len > chan->mps || len < 0 || __is_sframe(control))
3936 3937 3938 3939
			goto drop;

		tx_seq = __get_txseq(control);

3940 3941
		if (chan->expected_tx_seq == tx_seq)
			chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3942
		else
3943
			chan->expected_tx_seq = (tx_seq + 1) % 64;
3944

3945
		l2cap_streaming_reassembly_sdu(chan, skb, control);
3946 3947 3948

		goto done;

3949
	default:
3950
		BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
3951 3952
		break;
	}
L
Linus Torvalds 已提交
3953 3954 3955 3956 3957

drop:
	kfree_skb(skb);

done:
3958 3959 3960
	if (sk)
		bh_unlock_sock(sk);

L
Linus Torvalds 已提交
3961 3962 3963
	return 0;
}

3964
static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
L
Linus Torvalds 已提交
3965
{
3966
	struct sock *sk = NULL;
3967
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
3968

3969 3970
	chan = l2cap_global_chan_by_psm(0, psm, conn->src);
	if (!chan)
L
Linus Torvalds 已提交
3971 3972
		goto drop;

3973 3974
	sk = chan->sk;

3975 3976
	bh_lock_sock(sk);

L
Linus Torvalds 已提交
3977 3978
	BT_DBG("sk %p, len %d", sk, skb->len);

3979
	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
L
Linus Torvalds 已提交
3980 3981
		goto drop;

3982
	if (chan->imtu < skb->len)
L
Linus Torvalds 已提交
3983 3984
		goto drop;

3985
	if (!chan->ops->recv(chan->data, skb))
L
Linus Torvalds 已提交
3986 3987 3988 3989 3990 3991
		goto done;

drop:
	kfree_skb(skb);

done:
3992 3993
	if (sk)
		bh_unlock_sock(sk);
L
Linus Torvalds 已提交
3994 3995 3996
	return 0;
}

3997 3998
static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb)
{
3999
	struct sock *sk = NULL;
4000
	struct l2cap_chan *chan;
4001

4002 4003
	chan = l2cap_global_chan_by_scid(0, cid, conn->src);
	if (!chan)
4004 4005
		goto drop;

4006 4007
	sk = chan->sk;

4008 4009 4010 4011
	bh_lock_sock(sk);

	BT_DBG("sk %p, len %d", sk, skb->len);

4012
	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
4013 4014
		goto drop;

4015
	if (chan->imtu < skb->len)
4016 4017
		goto drop;

4018
	if (!chan->ops->recv(chan->data, skb))
4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029
		goto done;

drop:
	kfree_skb(skb);

done:
	if (sk)
		bh_unlock_sock(sk);
	return 0;
}

L
Linus Torvalds 已提交
4030 4031 4032
static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
{
	struct l2cap_hdr *lh = (void *) skb->data;
4033 4034
	u16 cid, len;
	__le16 psm;
L
Linus Torvalds 已提交
4035 4036 4037 4038 4039

	skb_pull(skb, L2CAP_HDR_SIZE);
	cid = __le16_to_cpu(lh->cid);
	len = __le16_to_cpu(lh->len);

4040 4041 4042 4043 4044
	if (len != skb->len) {
		kfree_skb(skb);
		return;
	}

L
Linus Torvalds 已提交
4045 4046 4047
	BT_DBG("len %d, cid 0x%4.4x", len, cid);

	switch (cid) {
4048
	case L2CAP_CID_LE_SIGNALING:
4049
	case L2CAP_CID_SIGNALING:
L
Linus Torvalds 已提交
4050 4051 4052
		l2cap_sig_channel(conn, skb);
		break;

4053
	case L2CAP_CID_CONN_LESS:
4054
		psm = get_unaligned_le16(skb->data);
L
Linus Torvalds 已提交
4055 4056 4057 4058
		skb_pull(skb, 2);
		l2cap_conless_channel(conn, psm, skb);
		break;

4059 4060 4061 4062
	case L2CAP_CID_LE_DATA:
		l2cap_att_channel(conn, cid, skb);
		break;

4063 4064 4065 4066 4067
	case L2CAP_CID_SMP:
		if (smp_sig_channel(conn, skb))
			l2cap_conn_del(conn->hcon, EACCES);
		break;

L
Linus Torvalds 已提交
4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078
	default:
		l2cap_data_channel(conn, cid, skb);
		break;
	}
}

/* ---- L2CAP interface with lower layer (HCI) ---- */

static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
{
	int exact = 0, lm1 = 0, lm2 = 0;
4079
	struct l2cap_chan *c;
L
Linus Torvalds 已提交
4080 4081

	if (type != ACL_LINK)
4082
		return -EINVAL;
L
Linus Torvalds 已提交
4083 4084 4085 4086

	BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));

	/* Find listening sockets and check their link_mode */
4087 4088 4089
	read_lock(&chan_list_lock);
	list_for_each_entry(c, &chan_list, global_l) {
		struct sock *sk = c->sk;
4090

4091
		if (c->state != BT_LISTEN)
L
Linus Torvalds 已提交
4092 4093 4094
			continue;

		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
4095
			lm1 |= HCI_LM_ACCEPT;
4096
			if (c->role_switch)
4097
				lm1 |= HCI_LM_MASTER;
L
Linus Torvalds 已提交
4098
			exact++;
4099 4100
		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
			lm2 |= HCI_LM_ACCEPT;
4101
			if (c->role_switch)
4102 4103
				lm2 |= HCI_LM_MASTER;
		}
L
Linus Torvalds 已提交
4104
	}
4105
	read_unlock(&chan_list_lock);
L
Linus Torvalds 已提交
4106 4107 4108 4109 4110 4111

	return exact ? lm1 : lm2;
}

static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
{
4112 4113
	struct l2cap_conn *conn;

L
Linus Torvalds 已提交
4114 4115
	BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);

4116
	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
4117
		return -EINVAL;
L
Linus Torvalds 已提交
4118 4119 4120 4121 4122

	if (!status) {
		conn = l2cap_conn_add(hcon, status);
		if (conn)
			l2cap_conn_ready(conn);
4123
	} else
4124
		l2cap_conn_del(hcon, bt_to_errno(status));
L
Linus Torvalds 已提交
4125 4126 4127 4128

	return 0;
}

4129 4130 4131 4132 4133 4134
static int l2cap_disconn_ind(struct hci_conn *hcon)
{
	struct l2cap_conn *conn = hcon->l2cap_data;

	BT_DBG("hcon %p", hcon);

4135
	if ((hcon->type != ACL_LINK && hcon->type != LE_LINK) || !conn)
4136 4137 4138 4139 4140 4141
		return 0x13;

	return conn->disc_reason;
}

static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
L
Linus Torvalds 已提交
4142 4143 4144
{
	BT_DBG("hcon %p reason %d", hcon, reason);

4145
	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
4146
		return -EINVAL;
L
Linus Torvalds 已提交
4147

4148
	l2cap_conn_del(hcon, bt_to_errno(reason));
4149

L
Linus Torvalds 已提交
4150 4151 4152
	return 0;
}

4153
static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
4154
{
4155
	if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
4156 4157
		return;

4158
	if (encrypt == 0x00) {
4159
		if (chan->sec_level == BT_SECURITY_MEDIUM) {
4160 4161
			__clear_chan_timer(chan);
			__set_chan_timer(chan, HZ * 5);
4162
		} else if (chan->sec_level == BT_SECURITY_HIGH)
4163
			l2cap_chan_close(chan, ECONNREFUSED);
4164
	} else {
4165
		if (chan->sec_level == BT_SECURITY_MEDIUM)
4166
			__clear_chan_timer(chan);
4167 4168 4169
	}
}

4170
static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
L
Linus Torvalds 已提交
4171
{
4172
	struct l2cap_conn *conn = hcon->l2cap_data;
4173
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
4174

4175
	if (!conn)
L
Linus Torvalds 已提交
4176
		return 0;
4177

L
Linus Torvalds 已提交
4178 4179
	BT_DBG("conn %p", conn);

4180
	read_lock(&conn->chan_lock);
L
Linus Torvalds 已提交
4181

4182
	list_for_each_entry(chan, &conn->chan_l, list) {
4183
		struct sock *sk = chan->sk;
4184

L
Linus Torvalds 已提交
4185 4186
		bh_lock_sock(sk);

4187 4188 4189 4190 4191
		BT_DBG("chan->scid %d", chan->scid);

		if (chan->scid == L2CAP_CID_LE_DATA) {
			if (!status && encrypt) {
				chan->sec_level = hcon->sec_level;
4192
				del_timer(&conn->security_timer);
4193 4194 4195 4196 4197 4198 4199
				l2cap_chan_ready(sk);
			}

			bh_unlock_sock(sk);
			continue;
		}

4200
		if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) {
4201 4202 4203 4204
			bh_unlock_sock(sk);
			continue;
		}

4205 4206
		if (!status && (chan->state == BT_CONNECTED ||
						chan->state == BT_CONFIG)) {
4207
			l2cap_check_encryption(chan, encrypt);
4208 4209 4210 4211
			bh_unlock_sock(sk);
			continue;
		}

4212
		if (chan->state == BT_CONNECT) {
4213 4214
			if (!status) {
				struct l2cap_conn_req req;
4215 4216
				req.scid = cpu_to_le16(chan->scid);
				req.psm  = chan->psm;
L
Linus Torvalds 已提交
4217

4218
				chan->ident = l2cap_get_ident(conn);
4219
				set_bit(CONF_CONNECT_PEND, &chan->conf_state);
L
Linus Torvalds 已提交
4220

4221
				l2cap_send_cmd(conn, chan->ident,
4222 4223
					L2CAP_CONN_REQ, sizeof(req), &req);
			} else {
4224 4225
				__clear_chan_timer(chan);
				__set_chan_timer(chan, HZ / 10);
4226
			}
4227
		} else if (chan->state == BT_CONNECT2) {
4228
			struct l2cap_conn_rsp rsp;
4229
			__u16 res, stat;
L
Linus Torvalds 已提交
4230

4231
			if (!status) {
4232 4233 4234 4235 4236 4237
				if (bt_sk(sk)->defer_setup) {
					struct sock *parent = bt_sk(sk)->parent;
					res = L2CAP_CR_PEND;
					stat = L2CAP_CS_AUTHOR_PEND;
					parent->sk_data_ready(parent, 0);
				} else {
4238
					l2cap_state_change(chan, BT_CONFIG);
4239 4240 4241
					res = L2CAP_CR_SUCCESS;
					stat = L2CAP_CS_NO_INFO;
				}
4242
			} else {
4243
				l2cap_state_change(chan, BT_DISCONN);
4244
				__set_chan_timer(chan, HZ / 10);
4245 4246
				res = L2CAP_CR_SEC_BLOCK;
				stat = L2CAP_CS_NO_INFO;
4247 4248
			}

4249 4250
			rsp.scid   = cpu_to_le16(chan->dcid);
			rsp.dcid   = cpu_to_le16(chan->scid);
4251 4252
			rsp.result = cpu_to_le16(res);
			rsp.status = cpu_to_le16(stat);
4253 4254
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
							sizeof(rsp), &rsp);
4255
		}
L
Linus Torvalds 已提交
4256 4257 4258 4259

		bh_unlock_sock(sk);
	}

4260
	read_unlock(&conn->chan_lock);
4261

L
Linus Torvalds 已提交
4262 4263 4264 4265 4266 4267 4268
	return 0;
}

static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
{
	struct l2cap_conn *conn = hcon->l2cap_data;

4269 4270 4271 4272
	if (!conn)
		conn = l2cap_conn_add(hcon, 0);

	if (!conn)
L
Linus Torvalds 已提交
4273 4274 4275 4276
		goto drop;

	BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);

4277
	if (!(flags & ACL_CONT)) {
L
Linus Torvalds 已提交
4278
		struct l2cap_hdr *hdr;
4279
		struct l2cap_chan *chan;
4280
		u16 cid;
L
Linus Torvalds 已提交
4281 4282 4283 4284 4285 4286 4287 4288 4289 4290
		int len;

		if (conn->rx_len) {
			BT_ERR("Unexpected start frame (len %d)", skb->len);
			kfree_skb(conn->rx_skb);
			conn->rx_skb = NULL;
			conn->rx_len = 0;
			l2cap_conn_unreliable(conn, ECOMM);
		}

4291 4292
		/* Start fragment always begin with Basic L2CAP header */
		if (skb->len < L2CAP_HDR_SIZE) {
L
Linus Torvalds 已提交
4293 4294 4295 4296 4297 4298 4299
			BT_ERR("Frame is too short (len %d)", skb->len);
			l2cap_conn_unreliable(conn, ECOMM);
			goto drop;
		}

		hdr = (struct l2cap_hdr *) skb->data;
		len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
4300
		cid = __le16_to_cpu(hdr->cid);
L
Linus Torvalds 已提交
4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316

		if (len == skb->len) {
			/* Complete frame received */
			l2cap_recv_frame(conn, skb);
			return 0;
		}

		BT_DBG("Start: total len %d, frag len %d", len, skb->len);

		if (skb->len > len) {
			BT_ERR("Frame is too long (len %d, expected len %d)",
				skb->len, len);
			l2cap_conn_unreliable(conn, ECOMM);
			goto drop;
		}

4317
		chan = l2cap_get_chan_by_scid(conn, cid);
4318

4319 4320
		if (chan && chan->sk) {
			struct sock *sk = chan->sk;
4321

4322
			if (chan->imtu < len - L2CAP_HDR_SIZE) {
4323 4324
				BT_ERR("Frame exceeding recv MTU (len %d, "
							"MTU %d)", len,
4325
							chan->imtu);
4326 4327 4328 4329
				bh_unlock_sock(sk);
				l2cap_conn_unreliable(conn, ECOMM);
				goto drop;
			}
4330
			bh_unlock_sock(sk);
4331
		}
4332

L
Linus Torvalds 已提交
4333
		/* Allocate skb for the complete frame (with header) */
4334 4335
		conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
		if (!conn->rx_skb)
L
Linus Torvalds 已提交
4336 4337
			goto drop;

4338
		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
4339
								skb->len);
L
Linus Torvalds 已提交
4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359
		conn->rx_len = len - skb->len;
	} else {
		BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);

		if (!conn->rx_len) {
			BT_ERR("Unexpected continuation frame (len %d)", skb->len);
			l2cap_conn_unreliable(conn, ECOMM);
			goto drop;
		}

		if (skb->len > conn->rx_len) {
			BT_ERR("Fragment is too long (len %d, expected %d)",
					skb->len, conn->rx_len);
			kfree_skb(conn->rx_skb);
			conn->rx_skb = NULL;
			conn->rx_len = 0;
			l2cap_conn_unreliable(conn, ECOMM);
			goto drop;
		}

4360
		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
4361
								skb->len);
L
Linus Torvalds 已提交
4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375
		conn->rx_len -= skb->len;

		if (!conn->rx_len) {
			/* Complete frame received */
			l2cap_recv_frame(conn, conn->rx_skb);
			conn->rx_skb = NULL;
		}
	}

drop:
	kfree_skb(skb);
	return 0;
}

4376
static int l2cap_debugfs_show(struct seq_file *f, void *p)
L
Linus Torvalds 已提交
4377
{
4378
	struct l2cap_chan *c;
L
Linus Torvalds 已提交
4379

4380
	read_lock_bh(&chan_list_lock);
L
Linus Torvalds 已提交
4381

4382 4383
	list_for_each_entry(c, &chan_list, global_l) {
		struct sock *sk = c->sk;
4384

4385
		seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
4386 4387
					batostr(&bt_sk(sk)->src),
					batostr(&bt_sk(sk)->dst),
4388
					c->state, __le16_to_cpu(c->psm),
4389 4390
					c->scid, c->dcid, c->imtu, c->omtu,
					c->sec_level, c->mode);
4391
}
L
Linus Torvalds 已提交
4392

4393
	read_unlock_bh(&chan_list_lock);
L
Linus Torvalds 已提交
4394

4395
	return 0;
L
Linus Torvalds 已提交
4396 4397
}

4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410
static int l2cap_debugfs_open(struct inode *inode, struct file *file)
{
	return single_open(file, l2cap_debugfs_show, inode->i_private);
}

static const struct file_operations l2cap_debugfs_fops = {
	.open		= l2cap_debugfs_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static struct dentry *l2cap_debugfs;
L
Linus Torvalds 已提交
4411 4412 4413 4414 4415 4416 4417

static struct hci_proto l2cap_hci_proto = {
	.name		= "L2CAP",
	.id		= HCI_PROTO_L2CAP,
	.connect_ind	= l2cap_connect_ind,
	.connect_cfm	= l2cap_connect_cfm,
	.disconn_ind	= l2cap_disconn_ind,
4418
	.disconn_cfm	= l2cap_disconn_cfm,
4419
	.security_cfm	= l2cap_security_cfm,
L
Linus Torvalds 已提交
4420 4421 4422
	.recv_acldata	= l2cap_recv_acldata
};

4423
int __init l2cap_init(void)
L
Linus Torvalds 已提交
4424 4425
{
	int err;
4426

4427
	err = l2cap_init_sockets();
L
Linus Torvalds 已提交
4428 4429 4430
	if (err < 0)
		return err;

4431
	_busy_wq = create_singlethread_workqueue("l2cap");
4432
	if (!_busy_wq) {
4433
		err = -ENOMEM;
L
Linus Torvalds 已提交
4434 4435 4436 4437 4438 4439 4440 4441 4442 4443
		goto error;
	}

	err = hci_register_proto(&l2cap_hci_proto);
	if (err < 0) {
		BT_ERR("L2CAP protocol registration failed");
		bt_sock_unregister(BTPROTO_L2CAP);
		goto error;
	}

4444 4445 4446 4447 4448 4449
	if (bt_debugfs) {
		l2cap_debugfs = debugfs_create_file("l2cap", 0444,
					bt_debugfs, NULL, &l2cap_debugfs_fops);
		if (!l2cap_debugfs)
			BT_ERR("Failed to create L2CAP debug file");
	}
L
Linus Torvalds 已提交
4450 4451 4452 4453

	return 0;

error:
4454
	destroy_workqueue(_busy_wq);
4455
	l2cap_cleanup_sockets();
L
Linus Torvalds 已提交
4456 4457 4458
	return err;
}

4459
void l2cap_exit(void)
L
Linus Torvalds 已提交
4460
{
4461
	debugfs_remove(l2cap_debugfs);
L
Linus Torvalds 已提交
4462

4463 4464 4465
	flush_workqueue(_busy_wq);
	destroy_workqueue(_busy_wq);

L
Linus Torvalds 已提交
4466 4467 4468
	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
		BT_ERR("L2CAP protocol unregistration failed");

4469
	l2cap_cleanup_sockets();
L
Linus Torvalds 已提交
4470 4471
}

4472 4473
module_param(disable_ertm, bool, 0644);
MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");