l2cap_core.c 107.8 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] = { L2CAP_FC_L2CAP, };
L
Linus Torvalds 已提交
63

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

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

75 76
static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb);

77
/* ---- L2CAP channels ---- */
78 79 80 81 82 83 84 85 86 87 88 89

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);
}

90
static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid)
91
{
92
	struct l2cap_chan *c;
93 94

	list_for_each_entry(c, &conn->chan_l, list) {
95
		if (c->dcid == cid)
96
			return c;
97
	}
98
	return NULL;
99 100
}

101
static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
102
{
103
	struct l2cap_chan *c;
104 105

	list_for_each_entry(c, &conn->chan_l, list) {
106
		if (c->scid == cid)
107
			return c;
108
	}
109
	return NULL;
110 111 112 113
}

/* Find channel with given SCID.
 * Returns locked socket */
114
static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
115
{
116
	struct l2cap_chan *c;
117 118 119

	read_lock(&conn->chan_lock);
	c = __l2cap_get_chan_by_scid(conn, cid);
120 121
	if (c)
		bh_lock_sock(c->sk);
122
	read_unlock(&conn->chan_lock);
123
	return c;
124 125
}

126
static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
127
{
128
	struct l2cap_chan *c;
129 130

	list_for_each_entry(c, &conn->chan_l, list) {
131
		if (c->ident == ident)
132
			return c;
133
	}
134
	return NULL;
135 136
}

137
static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
138
{
139
	struct l2cap_chan *c;
140 141 142

	read_lock(&conn->chan_lock);
	c = __l2cap_get_chan_by_ident(conn, ident);
143 144
	if (c)
		bh_lock_sock(c->sk);
145
	read_unlock(&conn->chan_lock);
146
	return c;
147 148
}

149
static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
150
{
151
	struct l2cap_chan *c;
152

153 154
	list_for_each_entry(c, &chan_list, global_l) {
		if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src))
155
			return c;
156
	}
157
	return NULL;
158 159 160 161
}

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

164
	write_lock_bh(&chan_list_lock);
165

166
	if (psm && __l2cap_global_chan_by_addr(psm, src)) {
167 168
		err = -EADDRINUSE;
		goto done;
169 170
	}

171 172 173 174 175 176 177 178 179
	if (psm) {
		chan->psm = psm;
		chan->sport = psm;
		err = 0;
	} else {
		u16 p;

		err = -EINVAL;
		for (p = 0x1001; p < 0x1100; p += 2)
180
			if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
181 182 183 184 185 186
				chan->psm   = cpu_to_le16(p);
				chan->sport = cpu_to_le16(p);
				err = 0;
				break;
			}
	}
187

188
done:
189
	write_unlock_bh(&chan_list_lock);
190
	return err;
191 192 193 194
}

int l2cap_add_scid(struct l2cap_chan *chan,  __u16 scid)
{
195
	write_lock_bh(&chan_list_lock);
196 197 198

	chan->scid = scid;

199
	write_unlock_bh(&chan_list_lock);
200 201 202 203

	return 0;
}

204
static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
205
{
206
	u16 cid = L2CAP_CID_DYN_START;
207

208
	for (; cid < L2CAP_CID_DYN_END; cid++) {
209
		if (!__l2cap_get_chan_by_scid(conn, cid))
210 211 212 213 214 215
			return cid;
	}

	return 0;
}

216
static void l2cap_set_timer(struct l2cap_chan *chan, struct timer_list *timer, long timeout)
217
{
218
	BT_DBG("chan %p state %d timeout %ld", chan, chan->state, timeout);
219

220
	if (!mod_timer(timer, jiffies + msecs_to_jiffies(timeout)))
221
		chan_hold(chan);
222 223
}

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

228 229
	if (timer_pending(timer) && del_timer(timer))
		chan_put(chan);
230 231
}

232 233 234 235 236 237
static void l2cap_state_change(struct l2cap_chan *chan, int state)
{
	chan->state = state;
	chan->ops->state_change(chan->data, state);
}

238 239 240 241 242 243
static void l2cap_chan_timeout(unsigned long arg)
{
	struct l2cap_chan *chan = (struct l2cap_chan *) arg;
	struct sock *sk = chan->sk;
	int reason;

244
	BT_DBG("chan %p state %d", chan, chan->state);
245 246 247 248 249

	bh_lock_sock(sk);

	if (sock_owned_by_user(sk)) {
		/* sk is owned by user. Try again later */
250
		__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
251
		bh_unlock_sock(sk);
252
		chan_put(chan);
253 254 255
		return;
	}

256
	if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
257
		reason = ECONNREFUSED;
258
	else if (chan->state == BT_CONNECT &&
259 260 261 262 263
					chan->sec_level != BT_SECURITY_SDP)
		reason = ECONNREFUSED;
	else
		reason = ETIMEDOUT;

264
	l2cap_chan_close(chan, reason);
265 266 267

	bh_unlock_sock(sk);

268
	chan->ops->close(chan->data);
269
	chan_put(chan);
270 271
}

272
struct l2cap_chan *l2cap_chan_create(struct sock *sk)
273 274 275 276 277 278 279 280 281
{
	struct l2cap_chan *chan;

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

	chan->sk = sk;

282 283 284 285
	write_lock_bh(&chan_list_lock);
	list_add(&chan->global_l, &chan_list);
	write_unlock_bh(&chan_list_lock);

286 287
	setup_timer(&chan->chan_timer, l2cap_chan_timeout, (unsigned long) chan);

288 289
	chan->state = BT_OPEN;

290 291
	atomic_set(&chan->refcnt, 1);

292 293
	BT_DBG("sk %p chan %p", sk, chan);

294 295 296
	return chan;
}

297
void l2cap_chan_destroy(struct l2cap_chan *chan)
298
{
299 300 301 302
	write_lock_bh(&chan_list_lock);
	list_del(&chan->global_l);
	write_unlock_bh(&chan_list_lock);

303
	chan_put(chan);
304 305
}

306
static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
307
{
308
	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
309
			chan->psm, chan->dcid);
310

311
	conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
312

313
	chan->conn = conn;
314

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

338 339 340 341 342 343 344
	chan->local_id		= L2CAP_BESTEFFORT_ID;
	chan->local_stype	= L2CAP_SERV_BESTEFFORT;
	chan->local_msdu	= L2CAP_DEFAULT_MAX_SDU_SIZE;
	chan->local_sdu_itime	= L2CAP_DEFAULT_SDU_ITIME;
	chan->local_acc_lat	= L2CAP_DEFAULT_ACC_LAT;
	chan->local_flush_to	= L2CAP_DEFAULT_FLUSH_TO;

345
	chan_hold(chan);
346 347

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

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

358
	__clear_chan_timer(chan);
359

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

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

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

373
	l2cap_state_change(chan, BT_CLOSED);
374 375 376 377 378 379 380 381 382 383
	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);
384

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

389
	skb_queue_purge(&chan->tx_q);
390

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

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

398
		skb_queue_purge(&chan->srej_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
int l2cap_chan_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
	bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
564
	skb->priority = HCI_PRIO_MAX;
565

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
	hci_send_acl(conn->hchan, skb, flags);
}

static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
{
	struct hci_conn *hcon = chan->conn->hcon;
	u16 flags;

	BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len,
							skb->priority);

	if (!test_bit(FLAG_FLUSHABLE, &chan->flags) &&
					lmp_no_flush_capable(hcon->hdev))
		flags = ACL_START_NO_FLUSH;
	else
		flags = ACL_START;
582

583 584
	bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
	hci_send_acl(chan->conn->hchan, skb, flags);
585 586
}

587
static inline void l2cap_send_sframe(struct l2cap_chan *chan, u32 control)
588 589 590
{
	struct sk_buff *skb;
	struct l2cap_hdr *lh;
591
	struct l2cap_conn *conn = chan->conn;
592
	int count, hlen;
593

594
	if (chan->state != BT_CONNECTED)
595 596
		return;

597 598 599 600 601
	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
		hlen = L2CAP_EXT_HDR_SIZE;
	else
		hlen = L2CAP_ENH_HDR_SIZE;

602
	if (chan->fcs == L2CAP_FCS_CRC16)
603
		hlen += L2CAP_FCS_SIZE;
604

605
	BT_DBG("chan %p, control 0x%8.8x", chan, control);
606

607
	count = min_t(unsigned int, conn->mtu, hlen);
608 609

	control |= __set_sframe(chan);
610

611
	if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
612
		control |= __set_ctrl_final(chan);
613

614
	if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state))
615
		control |= __set_ctrl_poll(chan);
616

617 618
	skb = bt_skb_alloc(count, GFP_ATOMIC);
	if (!skb)
619
		return;
620 621

	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
622
	lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
623
	lh->cid = cpu_to_le16(chan->dcid);
624 625

	__put_control(chan, control, skb_put(skb, __ctrl_size(chan)));
626

627
	if (chan->fcs == L2CAP_FCS_CRC16) {
628 629
		u16 fcs = crc16(0, (u8 *)lh, count - L2CAP_FCS_SIZE);
		put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
630 631
	}

632 633
	skb->priority = HCI_PRIO_MAX;
	l2cap_do_send(chan, skb);
634 635
}

636
static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u32 control)
637
{
638
	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
639
		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
640
		set_bit(CONN_RNR_SENT, &chan->conn_state);
641
	} else
642
		control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
643

644
	control |= __set_reqseq(chan, chan->buffer_seq);
645

646
	l2cap_send_sframe(chan, control);
647 648
}

649
static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
650
{
651
	return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
652 653
}

654
static void l2cap_do_start(struct l2cap_chan *chan)
655
{
656
	struct l2cap_conn *conn = chan->conn;
657 658

	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
659 660 661
		if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
			return;

662
		if (l2cap_chan_check_security(chan) &&
663
				__l2cap_no_conn_pending(chan)) {
664
			struct l2cap_conn_req req;
665 666
			req.scid = cpu_to_le16(chan->scid);
			req.psm  = chan->psm;
667

668
			chan->ident = l2cap_get_ident(conn);
669
			set_bit(CONF_CONNECT_PEND, &chan->conf_state);
670

671 672
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
							sizeof(req), &req);
673
		}
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
	} 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);
	}
}

689 690 691
static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
{
	u32 local_feat_mask = l2cap_feat_mask;
692
	if (!disable_ertm)
693 694 695 696 697 698 699 700 701 702 703 704
		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;
	}
}

705
static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err)
706
{
707
	struct sock *sk;
708 709
	struct l2cap_disconn_req req;

710 711 712
	if (!conn)
		return;

713 714
	sk = chan->sk;

715
	if (chan->mode == L2CAP_MODE_ERTM) {
716 717 718
		__clear_retrans_timer(chan);
		__clear_monitor_timer(chan);
		__clear_ack_timer(chan);
719 720
	}

721 722
	req.dcid = cpu_to_le16(chan->dcid);
	req.scid = cpu_to_le16(chan->scid);
723 724
	l2cap_send_cmd(conn, l2cap_get_ident(conn),
			L2CAP_DISCONN_REQ, sizeof(req), &req);
725

726
	l2cap_state_change(chan, BT_DISCONN);
727
	sk->sk_err = err;
728 729
}

L
Linus Torvalds 已提交
730
/* ---- L2CAP connections ---- */
731 732
static void l2cap_conn_start(struct l2cap_conn *conn)
{
733
	struct l2cap_chan *chan, *tmp;
734 735 736

	BT_DBG("conn %p", conn);

737
	read_lock(&conn->chan_lock);
738

739
	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
740
		struct sock *sk = chan->sk;
741

742 743
		bh_lock_sock(sk);

744
		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
745 746 747 748
			bh_unlock_sock(sk);
			continue;
		}

749
		if (chan->state == BT_CONNECT) {
750
			struct l2cap_conn_req req;
751

752
			if (!l2cap_chan_check_security(chan) ||
753
					!__l2cap_no_conn_pending(chan)) {
754 755 756
				bh_unlock_sock(sk);
				continue;
			}
757

758 759 760
			if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
					&& test_bit(CONF_STATE2_DEVICE,
					&chan->conf_state)) {
761
				/* l2cap_chan_close() calls list_del(chan)
762
				 * so release the lock */
763
				read_unlock(&conn->chan_lock);
764
				l2cap_chan_close(chan, ECONNRESET);
765
				read_lock(&conn->chan_lock);
766 767
				bh_unlock_sock(sk);
				continue;
768
			}
769

770 771
			req.scid = cpu_to_le16(chan->scid);
			req.psm  = chan->psm;
772

773
			chan->ident = l2cap_get_ident(conn);
774
			set_bit(CONF_CONNECT_PEND, &chan->conf_state);
775

776 777
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
							sizeof(req), &req);
778

779
		} else if (chan->state == BT_CONNECT2) {
780
			struct l2cap_conn_rsp rsp;
781
			char buf[128];
782 783
			rsp.scid = cpu_to_le16(chan->dcid);
			rsp.dcid = cpu_to_le16(chan->scid);
784

785
			if (l2cap_chan_check_security(chan)) {
786 787 788 789
				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);
790 791
					if (parent)
						parent->sk_data_ready(parent, 0);
792 793

				} else {
794
					l2cap_state_change(chan, BT_CONFIG);
795 796 797
					rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
					rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
				}
798 799 800 801 802
			} else {
				rsp.result = cpu_to_le16(L2CAP_CR_PEND);
				rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
			}

803 804
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
							sizeof(rsp), &rsp);
805

806
			if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
807 808 809 810 811
					rsp.result != L2CAP_CR_SUCCESS) {
				bh_unlock_sock(sk);
				continue;
			}

812
			set_bit(CONF_REQ_SENT, &chan->conf_state);
813
			l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
814 815
						l2cap_build_conf_req(chan, buf), buf);
			chan->num_conf_req++;
816 817 818 819 820
		}

		bh_unlock_sock(sk);
	}

821
	read_unlock(&conn->chan_lock);
822 823
}

824 825 826
/* Find socket with cid and source bdaddr.
 * Returns closest match, locked.
 */
827
static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src)
828
{
829
	struct l2cap_chan *c, *c1 = NULL;
830

831
	read_lock(&chan_list_lock);
832

833 834
	list_for_each_entry(c, &chan_list, global_l) {
		struct sock *sk = c->sk;
835

836
		if (state && c->state != state)
837 838
			continue;

839
		if (c->scid == cid) {
840
			/* Exact match. */
841 842 843 844
			if (!bacmp(&bt_sk(sk)->src, src)) {
				read_unlock(&chan_list_lock);
				return c;
			}
845 846 847

			/* Closest match */
			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
848
				c1 = c;
849 850
		}
	}
851

852
	read_unlock(&chan_list_lock);
853

854
	return c1;
855 856 857 858
}

static void l2cap_le_conn_ready(struct l2cap_conn *conn)
{
859
	struct sock *parent, *sk;
860
	struct l2cap_chan *chan, *pchan;
861 862 863 864

	BT_DBG("");

	/* Check if we have socket listening on cid */
865
	pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA,
866
							conn->src);
867
	if (!pchan)
868 869
		return;

870 871
	parent = pchan->sk;

872 873
	bh_lock_sock(parent);

874 875 876 877 878 879
	/* Check for backlog size */
	if (sk_acceptq_is_full(parent)) {
		BT_DBG("backlog full %d", parent->sk_ack_backlog);
		goto clean;
	}

880 881
	chan = pchan->ops->new_connection(pchan->data);
	if (!chan)
882 883
		goto clean;

884
	sk = chan->sk;
885

886
	write_lock_bh(&conn->chan_lock);
887 888 889 890 891 892

	hci_conn_hold(conn->hcon);

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

893 894
	bt_accept_enqueue(parent, sk);

895 896
	__l2cap_chan_add(conn, chan);

897
	__set_chan_timer(chan, sk->sk_sndtimeo);
898

899
	l2cap_state_change(chan, BT_CONNECTED);
900 901
	parent->sk_data_ready(parent, 0);

902
	write_unlock_bh(&conn->chan_lock);
903 904 905 906 907

clean:
	bh_unlock_sock(parent);
}

908 909 910 911 912 913 914 915 916 917
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);

918
	l2cap_state_change(chan, BT_CONNECTED);
919 920 921 922 923 924
	sk->sk_state_change(sk);

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

925 926
static void l2cap_conn_ready(struct l2cap_conn *conn)
{
927
	struct l2cap_chan *chan;
928

929
	BT_DBG("conn %p", conn);
930

931 932 933
	if (!conn->hcon->out && conn->hcon->type == LE_LINK)
		l2cap_le_conn_ready(conn);

934 935 936
	if (conn->hcon->out && conn->hcon->type == LE_LINK)
		smp_conn_security(conn, conn->hcon->pending_sec_level);

937
	read_lock(&conn->chan_lock);
938

939
	list_for_each_entry(chan, &conn->chan_l, list) {
940
		struct sock *sk = chan->sk;
941

942
		bh_lock_sock(sk);
943

944
		if (conn->hcon->type == LE_LINK) {
945
			if (smp_conn_security(conn, chan->sec_level))
946
				l2cap_chan_ready(sk);
947

948
		} else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
949
			__clear_chan_timer(chan);
950
			l2cap_state_change(chan, BT_CONNECTED);
951
			sk->sk_state_change(sk);
952

953
		} else if (chan->state == BT_CONNECT)
954
			l2cap_do_start(chan);
955

956
		bh_unlock_sock(sk);
957
	}
958

959
	read_unlock(&conn->chan_lock);
960 961 962 963 964
}

/* Notify sockets that we cannot guaranty reliability anymore */
static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
{
965
	struct l2cap_chan *chan;
966 967 968

	BT_DBG("conn %p", conn);

969
	read_lock(&conn->chan_lock);
970

971
	list_for_each_entry(chan, &conn->chan_l, list) {
972
		struct sock *sk = chan->sk;
973

974
		if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
975 976 977
			sk->sk_err = err;
	}

978
	read_unlock(&conn->chan_lock);
979 980 981 982 983 984
}

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

985
	conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
986
	conn->info_ident = 0;
987

988 989 990
	l2cap_conn_start(conn);
}

991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
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);
	}

1013 1014
	hci_chan_del(conn->hchan);

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

1018
	if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend)) {
1019
		del_timer(&conn->security_timer);
1020
		smp_chan_destroy(conn);
1021
	}
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033

	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 已提交
1034 1035
static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
{
1036
	struct l2cap_conn *conn = hcon->l2cap_data;
1037
	struct hci_chan *hchan;
L
Linus Torvalds 已提交
1038

1039
	if (conn || status)
L
Linus Torvalds 已提交
1040 1041
		return conn;

1042 1043 1044 1045
	hchan = hci_chan_create(hcon);
	if (!hchan)
		return NULL;

1046
	conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
1047 1048
	if (!conn) {
		hci_chan_del(hchan);
L
Linus Torvalds 已提交
1049
		return NULL;
1050
	}
L
Linus Torvalds 已提交
1051 1052 1053

	hcon->l2cap_data = conn;
	conn->hcon = hcon;
1054
	conn->hchan = hchan;
L
Linus Torvalds 已提交
1055

1056
	BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
1057

1058 1059 1060 1061 1062
	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 已提交
1063 1064 1065
	conn->src = &hcon->hdev->bdaddr;
	conn->dst = &hcon->dst;

1066 1067
	conn->feat_mask = 0;

L
Linus Torvalds 已提交
1068
	spin_lock_init(&conn->lock);
1069 1070 1071
	rwlock_init(&conn->chan_lock);

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

1073 1074 1075 1076
	if (hcon->type == LE_LINK)
		setup_timer(&conn->security_timer, security_timeout,
						(unsigned long) conn);
	else
1077
		setup_timer(&conn->info_timer, l2cap_info_timeout,
D
Dave Young 已提交
1078 1079
						(unsigned long) conn);

1080
	conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
1081

L
Linus Torvalds 已提交
1082 1083 1084
	return conn;
}

1085
static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
L
Linus Torvalds 已提交
1086
{
1087
	write_lock_bh(&conn->chan_lock);
1088
	__l2cap_chan_add(conn, chan);
1089
	write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
1090 1091 1092 1093 1094 1095 1096
}

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

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

1101
	read_lock(&chan_list_lock);
1102

1103 1104
	list_for_each_entry(c, &chan_list, global_l) {
		struct sock *sk = c->sk;
1105

1106
		if (state && c->state != state)
L
Linus Torvalds 已提交
1107 1108
			continue;

1109
		if (c->psm == psm) {
L
Linus Torvalds 已提交
1110
			/* Exact match. */
1111
			if (!bacmp(&bt_sk(sk)->src, src)) {
1112
				read_unlock(&chan_list_lock);
1113 1114
				return c;
			}
L
Linus Torvalds 已提交
1115 1116 1117

			/* Closest match */
			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
1118
				c1 = c;
L
Linus Torvalds 已提交
1119 1120 1121
		}
	}

1122
	read_unlock(&chan_list_lock);
1123

1124
	return c1;
L
Linus Torvalds 已提交
1125 1126
}

1127
int l2cap_chan_connect(struct l2cap_chan *chan)
L
Linus Torvalds 已提交
1128
{
1129
	struct sock *sk = chan->sk;
L
Linus Torvalds 已提交
1130 1131 1132 1133 1134
	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;
1135
	__u8 auth_type;
1136
	int err;
L
Linus Torvalds 已提交
1137

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

1141 1142
	hdev = hci_get_route(dst, src);
	if (!hdev)
L
Linus Torvalds 已提交
1143 1144 1145 1146
		return -EHOSTUNREACH;

	hci_dev_lock_bh(hdev);

1147
	auth_type = l2cap_get_auth_type(chan);
1148

1149
	if (chan->dcid == L2CAP_CID_LE_DATA)
1150
		hcon = hci_connect(hdev, LE_LINK, dst,
1151
					chan->sec_level, auth_type);
1152 1153
	else
		hcon = hci_connect(hdev, ACL_LINK, dst,
1154
					chan->sec_level, auth_type);
1155

1156 1157
	if (IS_ERR(hcon)) {
		err = PTR_ERR(hcon);
L
Linus Torvalds 已提交
1158
		goto done;
1159
	}
L
Linus Torvalds 已提交
1160 1161 1162 1163

	conn = l2cap_conn_add(hcon, 0);
	if (!conn) {
		hci_conn_put(hcon);
1164
		err = -ENOMEM;
L
Linus Torvalds 已提交
1165 1166 1167 1168 1169 1170
		goto done;
	}

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

1171 1172
	l2cap_chan_add(conn, chan);

1173
	l2cap_state_change(chan, BT_CONNECT);
1174
	__set_chan_timer(chan, sk->sk_sndtimeo);
L
Linus Torvalds 已提交
1175 1176

	if (hcon->state == BT_CONNECTED) {
1177
		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
1178
			__clear_chan_timer(chan);
1179
			if (l2cap_chan_check_security(chan))
1180
				l2cap_state_change(chan, BT_CONNECTED);
1181
		} else
1182
			l2cap_do_start(chan);
L
Linus Torvalds 已提交
1183 1184
	}

1185 1186
	err = 0;

L
Linus Torvalds 已提交
1187 1188 1189 1190 1191 1192
done:
	hci_dev_unlock_bh(hdev);
	hci_dev_put(hdev);
	return err;
}

1193
int __l2cap_wait_ack(struct sock *sk)
1194
{
1195
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
1196 1197 1198 1199
	DECLARE_WAITQUEUE(wait, current);
	int err = 0;
	int timeo = HZ/5;

1200
	add_wait_queue(sk_sleep(sk), &wait);
1201 1202
	set_current_state(TASK_INTERRUPTIBLE);
	while (chan->unacked_frames > 0 && chan->conn) {
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
		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);
1214
		set_current_state(TASK_INTERRUPTIBLE);
1215 1216 1217 1218 1219 1220

		err = sock_error(sk);
		if (err)
			break;
	}
	set_current_state(TASK_RUNNING);
1221
	remove_wait_queue(sk_sleep(sk), &wait);
1222 1223 1224
	return err;
}

1225 1226
static void l2cap_monitor_timeout(unsigned long arg)
{
1227 1228
	struct l2cap_chan *chan = (void *) arg;
	struct sock *sk = chan->sk;
1229

1230
	BT_DBG("chan %p", chan);
1231

1232
	bh_lock_sock(sk);
1233
	if (chan->retry_count >= chan->remote_max_tx) {
1234
		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1235
		bh_unlock_sock(sk);
1236 1237 1238
		return;
	}

1239
	chan->retry_count++;
1240
	__set_monitor_timer(chan);
1241

1242
	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
1243
	bh_unlock_sock(sk);
1244 1245 1246 1247
}

static void l2cap_retrans_timeout(unsigned long arg)
{
1248 1249
	struct l2cap_chan *chan = (void *) arg;
	struct sock *sk = chan->sk;
1250

1251
	BT_DBG("chan %p", chan);
1252

1253
	bh_lock_sock(sk);
1254
	chan->retry_count = 1;
1255
	__set_monitor_timer(chan);
1256

1257
	set_bit(CONN_WAIT_F, &chan->conn_state);
1258

1259
	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
1260
	bh_unlock_sock(sk);
1261 1262
}

1263
static void l2cap_drop_acked_frames(struct l2cap_chan *chan)
L
Linus Torvalds 已提交
1264
{
1265
	struct sk_buff *skb;
L
Linus Torvalds 已提交
1266

1267
	while ((skb = skb_peek(&chan->tx_q)) &&
1268
			chan->unacked_frames) {
1269
		if (bt_cb(skb)->tx_seq == chan->expected_ack_seq)
1270
			break;
L
Linus Torvalds 已提交
1271

1272
		skb = skb_dequeue(&chan->tx_q);
1273
		kfree_skb(skb);
L
Linus Torvalds 已提交
1274

1275
		chan->unacked_frames--;
1276
	}
L
Linus Torvalds 已提交
1277

1278
	if (!chan->unacked_frames)
1279
		__clear_retrans_timer(chan);
1280
}
L
Linus Torvalds 已提交
1281

1282
static void l2cap_streaming_send(struct l2cap_chan *chan)
1283
{
1284
	struct sk_buff *skb;
1285 1286
	u32 control;
	u16 fcs;
1287

1288
	while ((skb = skb_dequeue(&chan->tx_q))) {
1289
		control = __get_control(chan, skb->data + L2CAP_HDR_SIZE);
1290
		control |= __set_txseq(chan, chan->next_tx_seq);
1291
		__put_control(chan, control, skb->data + L2CAP_HDR_SIZE);
1292

1293
		if (chan->fcs == L2CAP_FCS_CRC16) {
1294 1295 1296 1297
			fcs = crc16(0, (u8 *)skb->data,
						skb->len - L2CAP_FCS_SIZE);
			put_unaligned_le16(fcs,
					skb->data + skb->len - L2CAP_FCS_SIZE);
1298 1299
		}

1300
		l2cap_do_send(chan, skb);
1301

1302
		chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
1303 1304 1305
	}
}

1306
static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u16 tx_seq)
1307 1308
{
	struct sk_buff *skb, *tx_skb;
1309 1310
	u16 fcs;
	u32 control;
1311

1312
	skb = skb_peek(&chan->tx_q);
1313 1314
	if (!skb)
		return;
1315

1316
	while (bt_cb(skb)->tx_seq != tx_seq) {
1317
		if (skb_queue_is_last(&chan->tx_q, skb))
1318
			return;
1319

1320 1321
		skb = skb_queue_next(&chan->tx_q, skb);
	}
1322

1323 1324
	if (chan->remote_max_tx &&
			bt_cb(skb)->retries == chan->remote_max_tx) {
1325
		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1326 1327 1328 1329 1330
		return;
	}

	tx_skb = skb_clone(skb, GFP_ATOMIC);
	bt_cb(skb)->retries++;
1331 1332

	control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE);
1333
	control &= __get_sar_mask(chan);
1334

1335
	if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
1336
		control |= __set_ctrl_final(chan);
1337

1338
	control |= __set_reqseq(chan, chan->buffer_seq);
1339
	control |= __set_txseq(chan, tx_seq);
1340

1341
	__put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE);
1342

1343
	if (chan->fcs == L2CAP_FCS_CRC16) {
1344 1345 1346 1347
		fcs = crc16(0, (u8 *)tx_skb->data,
						tx_skb->len - L2CAP_FCS_SIZE);
		put_unaligned_le16(fcs,
				tx_skb->data + tx_skb->len - L2CAP_FCS_SIZE);
1348 1349
	}

1350
	l2cap_do_send(chan, tx_skb);
1351 1352
}

1353
static int l2cap_ertm_send(struct l2cap_chan *chan)
1354 1355
{
	struct sk_buff *skb, *tx_skb;
1356 1357
	u16 fcs;
	u32 control;
1358
	int nsent = 0;
1359

1360
	if (chan->state != BT_CONNECTED)
1361
		return -ENOTCONN;
1362

1363
	while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) {
1364

1365 1366
		if (chan->remote_max_tx &&
				bt_cb(skb)->retries == chan->remote_max_tx) {
1367
			l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1368 1369 1370
			break;
		}

1371 1372
		tx_skb = skb_clone(skb, GFP_ATOMIC);

1373 1374
		bt_cb(skb)->retries++;

1375
		control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE);
1376
		control &= __get_sar_mask(chan);
1377

1378
		if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
1379
			control |= __set_ctrl_final(chan);
1380

1381
		control |= __set_reqseq(chan, chan->buffer_seq);
1382
		control |= __set_txseq(chan, chan->next_tx_seq);
1383

1384
		__put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE);
1385

1386
		if (chan->fcs == L2CAP_FCS_CRC16) {
1387 1388 1389 1390
			fcs = crc16(0, (u8 *)skb->data,
						tx_skb->len - L2CAP_FCS_SIZE);
			put_unaligned_le16(fcs, skb->data +
						tx_skb->len - L2CAP_FCS_SIZE);
1391 1392
		}

1393
		l2cap_do_send(chan, tx_skb);
1394

1395
		__set_retrans_timer(chan);
1396

1397
		bt_cb(skb)->tx_seq = chan->next_tx_seq;
1398 1399

		chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
1400

1401
		if (bt_cb(skb)->retries == 1)
1402
			chan->unacked_frames++;
1403

1404
		chan->frames_sent++;
1405

1406 1407
		if (skb_queue_is_last(&chan->tx_q, skb))
			chan->tx_send_head = NULL;
1408
		else
1409
			chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
1410 1411

		nsent++;
1412 1413
	}

1414 1415 1416
	return nsent;
}

1417
static int l2cap_retransmit_frames(struct l2cap_chan *chan)
1418 1419 1420
{
	int ret;

1421 1422
	if (!skb_queue_empty(&chan->tx_q))
		chan->tx_send_head = chan->tx_q.next;
1423

1424
	chan->next_tx_seq = chan->expected_ack_seq;
1425
	ret = l2cap_ertm_send(chan);
1426 1427 1428
	return ret;
}

1429
static void l2cap_send_ack(struct l2cap_chan *chan)
1430
{
1431
	u32 control = 0;
1432

1433
	control |= __set_reqseq(chan, chan->buffer_seq);
1434

1435
	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
1436
		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
1437
		set_bit(CONN_RNR_SENT, &chan->conn_state);
1438
		l2cap_send_sframe(chan, control);
1439
		return;
1440
	}
1441

1442
	if (l2cap_ertm_send(chan) > 0)
1443 1444
		return;

1445
	control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
1446
	l2cap_send_sframe(chan, control);
1447 1448
}

1449
static void l2cap_send_srejtail(struct l2cap_chan *chan)
1450 1451
{
	struct srej_list *tail;
1452
	u32 control;
1453

1454
	control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
1455
	control |= __set_ctrl_final(chan);
1456

1457
	tail = list_entry((&chan->srej_l)->prev, struct srej_list, list);
1458
	control |= __set_reqseq(chan, tail->tx_seq);
1459

1460
	l2cap_send_sframe(chan, control);
1461 1462
}

1463 1464
static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb)
{
1465
	struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
1466 1467
	struct sk_buff **frag;
	int err, sent = 0;
L
Linus Torvalds 已提交
1468

1469
	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
1470
		return -EFAULT;
L
Linus Torvalds 已提交
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481

	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)
1482
			return err;
1483 1484
		if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
			return -EFAULT;
L
Linus Torvalds 已提交
1485

1486 1487
		(*frag)->priority = skb->priority;

L
Linus Torvalds 已提交
1488 1489 1490 1491 1492 1493 1494
		sent += count;
		len  -= count;

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

	return sent;
1495
}
L
Linus Torvalds 已提交
1496

1497 1498 1499
static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
						struct msghdr *msg, size_t len,
						u32 priority)
1500
{
1501
	struct sock *sk = chan->sk;
1502
	struct l2cap_conn *conn = chan->conn;
1503
	struct sk_buff *skb;
1504
	int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
1505 1506
	struct l2cap_hdr *lh;

1507
	BT_DBG("sk %p len %d priority %u", sk, (int)len, priority);
1508 1509 1510 1511 1512

	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)
1513
		return ERR_PTR(err);
1514

1515 1516
	skb->priority = priority;

1517 1518
	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1519
	lh->cid = cpu_to_le16(chan->dcid);
1520
	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
1521
	put_unaligned_le16(chan->psm, skb_put(skb, 2));
1522 1523 1524 1525 1526 1527 1528 1529 1530

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

1531 1532 1533
static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
						struct msghdr *msg, size_t len,
						u32 priority)
1534
{
1535
	struct sock *sk = chan->sk;
1536
	struct l2cap_conn *conn = chan->conn;
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	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)
1547
		return ERR_PTR(err);
1548

1549 1550
	skb->priority = priority;

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 1556 1557 1558 1559 1560 1561 1562 1563
	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;
}

1564 1565
static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
						struct msghdr *msg, size_t len,
1566
						u32 control, u16 sdulen)
1567
{
1568
	struct sock *sk = chan->sk;
1569
	struct l2cap_conn *conn = chan->conn;
1570
	struct sk_buff *skb;
1571
	int err, count, hlen;
1572 1573 1574 1575
	struct l2cap_hdr *lh;

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

1576 1577 1578
	if (!conn)
		return ERR_PTR(-ENOTCONN);

1579 1580 1581 1582 1583
	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
		hlen = L2CAP_EXT_HDR_SIZE;
	else
		hlen = L2CAP_ENH_HDR_SIZE;

1584
	if (sdulen)
1585
		hlen += L2CAP_SDULEN_SIZE;
1586

1587
	if (chan->fcs == L2CAP_FCS_CRC16)
1588
		hlen += L2CAP_FCS_SIZE;
1589

1590 1591 1592 1593
	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)
1594
		return ERR_PTR(err);
1595 1596 1597

	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1598
	lh->cid = cpu_to_le16(chan->dcid);
1599
	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
1600 1601 1602

	__put_control(chan, control, skb_put(skb, __ctrl_size(chan)));

1603
	if (sdulen)
1604
		put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
1605 1606 1607 1608 1609 1610

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

1612
	if (chan->fcs == L2CAP_FCS_CRC16)
1613
		put_unaligned_le16(0, skb_put(skb, L2CAP_FCS_SIZE));
1614

1615
	bt_cb(skb)->retries = 0;
1616
	return skb;
L
Linus Torvalds 已提交
1617 1618
}

1619
static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
1620 1621 1622
{
	struct sk_buff *skb;
	struct sk_buff_head sar_queue;
1623
	u32 control;
1624 1625
	size_t size = 0;

1626
	skb_queue_head_init(&sar_queue);
1627
	control = __set_ctrl_sar(chan, L2CAP_SAR_START);
1628
	skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
1629 1630 1631 1632
	if (IS_ERR(skb))
		return PTR_ERR(skb);

	__skb_queue_tail(&sar_queue, skb);
1633 1634
	len -= chan->remote_mps;
	size += chan->remote_mps;
1635 1636 1637 1638

	while (len > 0) {
		size_t buflen;

1639
		if (len > chan->remote_mps) {
1640
			control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE);
1641
			buflen = chan->remote_mps;
1642
		} else {
1643
			control = __set_ctrl_sar(chan, L2CAP_SAR_END);
1644 1645 1646
			buflen = len;
		}

1647
		skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0);
1648 1649 1650 1651 1652 1653 1654 1655 1656
		if (IS_ERR(skb)) {
			skb_queue_purge(&sar_queue);
			return PTR_ERR(skb);
		}

		__skb_queue_tail(&sar_queue, skb);
		len -= buflen;
		size += buflen;
	}
1657 1658 1659
	skb_queue_splice_tail(&sar_queue, &chan->tx_q);
	if (chan->tx_send_head == NULL)
		chan->tx_send_head = sar_queue.next;
1660 1661 1662 1663

	return size;
}

1664 1665
int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,
								u32 priority)
1666 1667
{
	struct sk_buff *skb;
1668
	u32 control;
1669 1670 1671
	int err;

	/* Connectionless channel */
1672
	if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
1673
		skb = l2cap_create_connless_pdu(chan, msg, len, priority);
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
		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 */
1688
		skb = l2cap_create_basic_pdu(chan, msg, len, priority);
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
		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) {
1700
			control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED);
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
			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;
		}

1724 1725
		if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
				test_bit(CONN_WAIT_F, &chan->conn_state)) {
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
			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 已提交
1744 1745 1746 1747
/* 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;
1748
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
1749 1750 1751

	BT_DBG("conn %p", conn);

1752 1753
	read_lock(&conn->chan_lock);
	list_for_each_entry(chan, &conn->chan_l, list) {
1754
		struct sock *sk = chan->sk;
1755
		if (chan->chan_type != L2CAP_CHAN_RAW)
L
Linus Torvalds 已提交
1756 1757 1758 1759 1760
			continue;

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

1765
		if (chan->ops->recv(chan->data, nskb))
L
Linus Torvalds 已提交
1766 1767
			kfree_skb(nskb);
	}
1768
	read_unlock(&conn->chan_lock);
L
Linus Torvalds 已提交
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
}

/* ---- 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;

1780 1781
	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
			conn, code, ident, dlen);
L
Linus Torvalds 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790

	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);
1791
	lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
1792 1793 1794 1795 1796

	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 已提交
1797 1798 1799 1800

	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
	cmd->code  = code;
	cmd->ident = ident;
1801
	cmd->len   = cpu_to_le16(dlen);
L
Linus Torvalds 已提交
1802 1803 1804 1805 1806 1807 1808 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 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851

	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:
1852
		*val = get_unaligned_le16(opt->val);
L
Linus Torvalds 已提交
1853 1854 1855
		break;

	case 4:
1856
		*val = get_unaligned_le32(opt->val);
L
Linus Torvalds 已提交
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
		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:
1883
		put_unaligned_le16(val, opt->val);
L
Linus Torvalds 已提交
1884 1885 1886
		break;

	case 4:
1887
		put_unaligned_le32(val, opt->val);
L
Linus Torvalds 已提交
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
		break;

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

	*ptr += L2CAP_CONF_OPT_SIZE + len;
}

1898 1899 1900 1901
static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan)
{
	struct l2cap_conf_efs efs;

1902
	switch (chan->mode) {
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
	case L2CAP_MODE_ERTM:
		efs.id		= chan->local_id;
		efs.stype	= chan->local_stype;
		efs.msdu	= cpu_to_le16(chan->local_msdu);
		efs.sdu_itime	= cpu_to_le32(chan->local_sdu_itime);
		efs.acc_lat	= cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
		efs.flush_to	= cpu_to_le32(L2CAP_DEFAULT_FLUSH_TO);
		break;

	case L2CAP_MODE_STREAMING:
		efs.id		= 1;
		efs.stype	= L2CAP_SERV_BESTEFFORT;
		efs.msdu	= cpu_to_le16(chan->local_msdu);
		efs.sdu_itime	= cpu_to_le32(chan->local_sdu_itime);
		efs.acc_lat	= 0;
		efs.flush_to	= 0;
		break;

	default:
		return;
	}

	l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
							(unsigned long) &efs);
}

1929 1930
static void l2cap_ack_timeout(unsigned long arg)
{
1931
	struct l2cap_chan *chan = (void *) arg;
1932

1933 1934 1935
	bh_lock_sock(chan->sk);
	l2cap_send_ack(chan);
	bh_unlock_sock(chan->sk);
1936 1937
}

1938
static inline void l2cap_ertm_init(struct l2cap_chan *chan)
1939
{
1940 1941
	struct sock *sk = chan->sk;

1942
	chan->expected_ack_seq = 0;
1943
	chan->unacked_frames = 0;
1944
	chan->buffer_seq = 0;
1945 1946
	chan->num_acked = 0;
	chan->frames_sent = 0;
1947

1948 1949 1950 1951 1952
	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);
1953

1954
	skb_queue_head_init(&chan->srej_q);
1955

1956 1957
	INIT_LIST_HEAD(&chan->srej_l);

1958 1959

	sk->sk_backlog_rcv = l2cap_ertm_data_rcv;
1960 1961
}

1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
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;
	}
}

1975 1976 1977 1978 1979
static inline bool __l2cap_ews_supported(struct l2cap_chan *chan)
{
	return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW;
}

1980 1981 1982 1983 1984
static inline bool __l2cap_efs_supported(struct l2cap_chan *chan)
{
	return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW;
}

1985 1986 1987
static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
{
	if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
1988
						__l2cap_ews_supported(chan)) {
1989 1990
		/* use extended control field */
		set_bit(FLAG_EXT_CTRL, &chan->flags);
1991 1992
		chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
	} else {
1993 1994
		chan->tx_win = min_t(u16, chan->tx_win,
						L2CAP_DEFAULT_TX_WINDOW);
1995 1996
		chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
	}
1997 1998
}

1999
static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
L
Linus Torvalds 已提交
2000 2001
{
	struct l2cap_conf_req *req = data;
2002
	struct l2cap_conf_rfc rfc = { .mode = chan->mode };
L
Linus Torvalds 已提交
2003
	void *ptr = req->data;
2004
	u16 size;
L
Linus Torvalds 已提交
2005

2006
	BT_DBG("chan %p", chan);
L
Linus Torvalds 已提交
2007

2008
	if (chan->num_conf_req || chan->num_conf_rsp)
2009 2010
		goto done;

2011
	switch (chan->mode) {
2012 2013
	case L2CAP_MODE_STREAMING:
	case L2CAP_MODE_ERTM:
2014
		if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
2015 2016
			break;

2017 2018 2019
		if (__l2cap_efs_supported(chan))
			set_bit(FLAG_EFS_ENABLE, &chan->flags);

2020
		/* fall through */
2021
	default:
2022
		chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
2023 2024 2025 2026
		break;
	}

done:
2027 2028
	if (chan->imtu != L2CAP_DEFAULT_MTU)
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
2029

2030
	switch (chan->mode) {
2031
	case L2CAP_MODE_BASIC:
2032 2033
		if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
				!(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
2034 2035
			break;

2036 2037 2038 2039 2040 2041 2042
		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;

2043 2044
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
							(unsigned long) &rfc);
2045 2046 2047 2048
		break;

	case L2CAP_MODE_ERTM:
		rfc.mode            = L2CAP_MODE_ERTM;
2049
		rfc.max_transmit    = chan->max_tx;
2050 2051
		rfc.retrans_timeout = 0;
		rfc.monitor_timeout = 0;
2052 2053 2054 2055 2056 2057

		size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
						L2CAP_EXT_HDR_SIZE -
						L2CAP_SDULEN_SIZE -
						L2CAP_FCS_SIZE);
		rfc.max_pdu_size = cpu_to_le16(size);
2058

2059 2060 2061 2062
		l2cap_txwin_setup(chan);

		rfc.txwin_size = min_t(u16, chan->tx_win,
						L2CAP_DEFAULT_TX_WINDOW);
2063

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

2067 2068 2069
		if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
			l2cap_add_opt_efs(&ptr, chan);

2070
		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
2071 2072
			break;

2073
		if (chan->fcs == L2CAP_FCS_NONE ||
2074
				test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) {
2075 2076
			chan->fcs = L2CAP_FCS_NONE;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
2077
		}
2078 2079 2080 2081

		if (test_bit(FLAG_EXT_CTRL, &chan->flags))
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
								chan->tx_win);
2082 2083 2084 2085 2086 2087 2088 2089
		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;
2090 2091 2092 2093 2094 2095

		size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
						L2CAP_EXT_HDR_SIZE -
						L2CAP_SDULEN_SIZE -
						L2CAP_FCS_SIZE);
		rfc.max_pdu_size = cpu_to_le16(size);
2096

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

2100 2101 2102
		if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
			l2cap_add_opt_efs(&ptr, chan);

2103
		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
2104 2105
			break;

2106
		if (chan->fcs == L2CAP_FCS_NONE ||
2107
				test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) {
2108 2109
			chan->fcs = L2CAP_FCS_NONE;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
2110
		}
2111 2112
		break;
	}
L
Linus Torvalds 已提交
2113

2114
	req->dcid  = cpu_to_le16(chan->dcid);
2115
	req->flags = cpu_to_le16(0);
L
Linus Torvalds 已提交
2116 2117 2118 2119

	return ptr - data;
}

2120
static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
L
Linus Torvalds 已提交
2121
{
2122 2123
	struct l2cap_conf_rsp *rsp = data;
	void *ptr = rsp->data;
2124 2125
	void *req = chan->conf_req;
	int len = chan->conf_len;
2126 2127
	int type, hint, olen;
	unsigned long val;
2128
	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
2129 2130
	struct l2cap_conf_efs efs;
	u8 remote_efs = 0;
2131
	u16 mtu = L2CAP_DEFAULT_MTU;
2132
	u16 result = L2CAP_CONF_SUCCESS;
2133
	u16 size;
L
Linus Torvalds 已提交
2134

2135
	BT_DBG("chan %p", chan);
2136

2137 2138
	while (len >= L2CAP_CONF_OPT_SIZE) {
		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
L
Linus Torvalds 已提交
2139

2140
		hint  = type & L2CAP_CONF_HINT;
2141
		type &= L2CAP_CONF_MASK;
2142 2143 2144

		switch (type) {
		case L2CAP_CONF_MTU:
2145
			mtu = val;
2146 2147 2148
			break;

		case L2CAP_CONF_FLUSH_TO:
2149
			chan->flush_to = val;
2150 2151 2152 2153 2154
			break;

		case L2CAP_CONF_QOS:
			break;

2155 2156 2157 2158 2159
		case L2CAP_CONF_RFC:
			if (olen == sizeof(rfc))
				memcpy(&rfc, (void *) val, olen);
			break;

2160 2161
		case L2CAP_CONF_FCS:
			if (val == L2CAP_FCS_NONE)
2162
				set_bit(CONF_NO_FCS_RECV, &chan->conf_state);
2163
			break;
2164

2165 2166 2167 2168
		case L2CAP_CONF_EFS:
			remote_efs = 1;
			if (olen == sizeof(efs))
				memcpy(&efs, (void *) val, olen);
2169 2170
			break;

2171 2172 2173
		case L2CAP_CONF_EWS:
			if (!enable_hs)
				return -ECONNREFUSED;
2174

2175 2176
			set_bit(FLAG_EXT_CTRL, &chan->flags);
			set_bit(CONF_EWS_RECV, &chan->conf_state);
2177
			chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
2178
			chan->remote_tx_win = val;
2179 2180
			break;

2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
		default:
			if (hint)
				break;

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

2191
	if (chan->num_conf_rsp || chan->num_conf_req > 1)
2192 2193
		goto done;

2194
	switch (chan->mode) {
2195 2196
	case L2CAP_MODE_STREAMING:
	case L2CAP_MODE_ERTM:
2197
		if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
2198
			chan->mode = l2cap_select_mode(rfc.mode,
2199
					chan->conn->feat_mask);
2200 2201 2202
			break;
		}

2203 2204 2205 2206 2207 2208 2209
		if (remote_efs) {
			if (__l2cap_efs_supported(chan))
				set_bit(FLAG_EFS_ENABLE, &chan->flags);
			else
				return -ECONNREFUSED;
		}

2210
		if (chan->mode != rfc.mode)
2211
			return -ECONNREFUSED;
2212

2213 2214 2215 2216
		break;
	}

done:
2217
	if (chan->mode != rfc.mode) {
2218
		result = L2CAP_CONF_UNACCEPT;
2219
		rfc.mode = chan->mode;
2220

2221
		if (chan->num_conf_rsp == 1)
2222 2223 2224 2225 2226 2227
			return -ECONNREFUSED;

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

2228 2229 2230 2231
	if (result == L2CAP_CONF_SUCCESS) {
		/* Configure output options and let the other side know
		 * which ones we don't like. */

2232 2233 2234
		if (mtu < L2CAP_DEFAULT_MIN_MTU)
			result = L2CAP_CONF_UNACCEPT;
		else {
2235
			chan->omtu = mtu;
2236
			set_bit(CONF_MTU_DONE, &chan->conf_state);
2237
		}
2238
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
2239

2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
		if (remote_efs) {
			if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
					efs.stype != L2CAP_SERV_NOTRAFIC &&
					efs.stype != chan->local_stype) {

				result = L2CAP_CONF_UNACCEPT;

				if (chan->num_conf_req >= 1)
					return -ECONNREFUSED;

				l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
2251
							sizeof(efs),
2252
							(unsigned long) &efs);
2253
			} else {
2254
				/* Send PENDING Conf Rsp */
2255 2256
				result = L2CAP_CONF_PENDING;
				set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
2257 2258 2259
			}
		}

2260 2261
		switch (rfc.mode) {
		case L2CAP_MODE_BASIC:
2262
			chan->fcs = L2CAP_FCS_NONE;
2263
			set_bit(CONF_MODE_DONE, &chan->conf_state);
2264 2265 2266
			break;

		case L2CAP_MODE_ERTM:
2267 2268 2269 2270
			if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
				chan->remote_tx_win = rfc.txwin_size;
			else
				rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
2271

2272
			chan->remote_max_tx = rfc.max_transmit;
2273

2274 2275 2276 2277 2278 2279 2280
			size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
						chan->conn->mtu -
						L2CAP_EXT_HDR_SIZE -
						L2CAP_SDULEN_SIZE -
						L2CAP_FCS_SIZE);
			rfc.max_pdu_size = cpu_to_le16(size);
			chan->remote_mps = size;
2281

2282 2283 2284 2285
			rfc.retrans_timeout =
				le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO);
			rfc.monitor_timeout =
				le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO);
2286

2287
			set_bit(CONF_MODE_DONE, &chan->conf_state);
2288 2289 2290 2291

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

2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
			if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
				chan->remote_id = efs.id;
				chan->remote_stype = efs.stype;
				chan->remote_msdu = le16_to_cpu(efs.msdu);
				chan->remote_flush_to =
						le32_to_cpu(efs.flush_to);
				chan->remote_acc_lat =
						le32_to_cpu(efs.acc_lat);
				chan->remote_sdu_itime =
					le32_to_cpu(efs.sdu_itime);
				l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
					sizeof(efs), (unsigned long) &efs);
			}
2305 2306 2307
			break;

		case L2CAP_MODE_STREAMING:
2308 2309 2310 2311 2312 2313 2314
			size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
						chan->conn->mtu -
						L2CAP_EXT_HDR_SIZE -
						L2CAP_SDULEN_SIZE -
						L2CAP_FCS_SIZE);
			rfc.max_pdu_size = cpu_to_le16(size);
			chan->remote_mps = size;
2315

2316
			set_bit(CONF_MODE_DONE, &chan->conf_state);
2317 2318 2319 2320

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

2321 2322 2323
			break;

		default:
2324 2325
			result = L2CAP_CONF_UNACCEPT;

2326
			memset(&rfc, 0, sizeof(rfc));
2327
			rfc.mode = chan->mode;
2328
		}
2329

2330
		if (result == L2CAP_CONF_SUCCESS)
2331
			set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
2332
	}
2333
	rsp->scid   = cpu_to_le16(chan->dcid);
2334 2335 2336 2337
	rsp->result = cpu_to_le16(result);
	rsp->flags  = cpu_to_le16(0x0000);

	return ptr - data;
L
Linus Torvalds 已提交
2338 2339
}

2340
static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result)
2341 2342 2343 2344 2345 2346
{
	struct l2cap_conf_req *req = data;
	void *ptr = req->data;
	int type, olen;
	unsigned long val;
	struct l2cap_conf_rfc rfc;
2347
	struct l2cap_conf_efs efs;
2348

2349
	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
2350 2351 2352 2353 2354 2355 2356 2357

	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;
2358
				chan->imtu = L2CAP_DEFAULT_MIN_MTU;
2359
			} else
2360 2361
				chan->imtu = val;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
2362 2363 2364
			break;

		case L2CAP_CONF_FLUSH_TO:
2365
			chan->flush_to = val;
2366
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
2367
							2, chan->flush_to);
2368 2369 2370 2371 2372 2373
			break;

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

2374
			if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
2375
							rfc.mode != chan->mode)
2376 2377
				return -ECONNREFUSED;

2378
			chan->fcs = 0;
2379 2380 2381 2382

			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
					sizeof(rfc), (unsigned long) &rfc);
			break;
2383 2384 2385 2386

		case L2CAP_CONF_EWS:
			chan->tx_win = min_t(u16, val,
						L2CAP_DEFAULT_EXT_WINDOW);
2387 2388
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
							chan->tx_win);
2389
			break;
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402

		case L2CAP_CONF_EFS:
			if (olen == sizeof(efs))
				memcpy(&efs, (void *)val, olen);

			if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
					efs.stype != L2CAP_SERV_NOTRAFIC &&
					efs.stype != chan->local_stype)
				return -ECONNREFUSED;

			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
					sizeof(efs), (unsigned long) &efs);
			break;
2403 2404 2405
		}
	}

2406
	if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
2407 2408
		return -ECONNREFUSED;

2409
	chan->mode = rfc.mode;
2410

2411
	if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
2412 2413
		switch (rfc.mode) {
		case L2CAP_MODE_ERTM:
2414 2415 2416
			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);
2417 2418 2419 2420 2421 2422 2423 2424 2425

			if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
				chan->local_msdu = le16_to_cpu(efs.msdu);
				chan->local_sdu_itime =
						le32_to_cpu(efs.sdu_itime);
				chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
				chan->local_flush_to =
						le32_to_cpu(efs.flush_to);
			}
2426
			break;
2427

2428
		case L2CAP_MODE_STREAMING:
2429
			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
2430 2431 2432
		}
	}

2433
	req->dcid   = cpu_to_le16(chan->dcid);
2434 2435 2436 2437 2438
	req->flags  = cpu_to_le16(0x0000);

	return ptr - data;
}

2439
static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags)
L
Linus Torvalds 已提交
2440 2441 2442 2443
{
	struct l2cap_conf_rsp *rsp = data;
	void *ptr = rsp->data;

2444
	BT_DBG("chan %p", chan);
L
Linus Torvalds 已提交
2445

2446
	rsp->scid   = cpu_to_le16(chan->dcid);
2447
	rsp->result = cpu_to_le16(result);
2448
	rsp->flags  = cpu_to_le16(flags);
L
Linus Torvalds 已提交
2449 2450 2451 2452

	return ptr - data;
}

2453
void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
2454 2455
{
	struct l2cap_conn_rsp rsp;
2456
	struct l2cap_conn *conn = chan->conn;
2457 2458
	u8 buf[128];

2459 2460
	rsp.scid   = cpu_to_le16(chan->dcid);
	rsp.dcid   = cpu_to_le16(chan->scid);
2461 2462 2463 2464 2465
	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);

2466
	if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
2467 2468 2469 2470 2471 2472 2473
		return;

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

2474
static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
2475 2476 2477 2478 2479
{
	int type, olen;
	unsigned long val;
	struct l2cap_conf_rfc rfc;

2480
	BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
2481

2482
	if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498
		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:
2499 2500 2501
		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);
2502 2503
		break;
	case L2CAP_MODE_STREAMING:
2504
		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
2505 2506 2507
	}
}

2508 2509
static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
{
2510
	struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
2511

2512
	if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
2513 2514 2515 2516 2517
		return 0;

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

		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
2520
		conn->info_ident = 0;
2521

2522 2523 2524 2525 2526 2527
		l2cap_conn_start(conn);
	}

	return 0;
}

L
Linus Torvalds 已提交
2528 2529 2530 2531
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;
2532
	struct l2cap_chan *chan = NULL, *pchan;
2533
	struct sock *parent, *sk = NULL;
2534
	int result, status = L2CAP_CS_NO_INFO;
L
Linus Torvalds 已提交
2535 2536

	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
2537
	__le16 psm = req->psm;
L
Linus Torvalds 已提交
2538 2539 2540 2541

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

	/* Check if we have socket listening on psm */
2542 2543
	pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src);
	if (!pchan) {
L
Linus Torvalds 已提交
2544 2545 2546 2547
		result = L2CAP_CR_BAD_PSM;
		goto sendresp;
	}

2548 2549
	parent = pchan->sk;

2550 2551
	bh_lock_sock(parent);

2552 2553 2554
	/* Check if the ACL is secure enough (if not SDP) */
	if (psm != cpu_to_le16(0x0001) &&
				!hci_conn_check_link_mode(conn->hcon)) {
2555
		conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
2556 2557 2558 2559
		result = L2CAP_CR_SEC_BLOCK;
		goto response;
	}

L
Linus Torvalds 已提交
2560 2561 2562 2563
	result = L2CAP_CR_NO_MEM;

	/* Check for backlog size */
	if (sk_acceptq_is_full(parent)) {
2564
		BT_DBG("backlog full %d", parent->sk_ack_backlog);
L
Linus Torvalds 已提交
2565 2566 2567
		goto response;
	}

2568 2569
	chan = pchan->ops->new_connection(pchan->data);
	if (!chan)
L
Linus Torvalds 已提交
2570 2571
		goto response;

2572 2573
	sk = chan->sk;

2574
	write_lock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2575 2576

	/* Check if we already have channel with that dcid */
2577 2578
	if (__l2cap_get_chan_by_dcid(conn, scid)) {
		write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2579
		sock_set_flag(sk, SOCK_ZAPPED);
2580
		chan->ops->close(chan->data);
L
Linus Torvalds 已提交
2581 2582 2583 2584 2585 2586 2587
		goto response;
	}

	hci_conn_hold(conn->hcon);

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

2591 2592
	bt_accept_enqueue(parent, sk);

2593 2594
	__l2cap_chan_add(conn, chan);

2595
	dcid = chan->scid;
L
Linus Torvalds 已提交
2596

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

2599
	chan->ident = cmd->ident;
L
Linus Torvalds 已提交
2600

2601
	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
2602
		if (l2cap_chan_check_security(chan)) {
2603
			if (bt_sk(sk)->defer_setup) {
2604
				l2cap_state_change(chan, BT_CONNECT2);
2605 2606 2607 2608
				result = L2CAP_CR_PEND;
				status = L2CAP_CS_AUTHOR_PEND;
				parent->sk_data_ready(parent, 0);
			} else {
2609
				l2cap_state_change(chan, BT_CONFIG);
2610 2611 2612
				result = L2CAP_CR_SUCCESS;
				status = L2CAP_CS_NO_INFO;
			}
2613
		} else {
2614
			l2cap_state_change(chan, BT_CONNECT2);
2615 2616 2617 2618
			result = L2CAP_CR_PEND;
			status = L2CAP_CS_AUTHEN_PEND;
		}
	} else {
2619
		l2cap_state_change(chan, BT_CONNECT2);
2620 2621
		result = L2CAP_CR_PEND;
		status = L2CAP_CS_NO_INFO;
L
Linus Torvalds 已提交
2622 2623
	}

2624
	write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2625 2626 2627 2628 2629

response:
	bh_unlock_sock(parent);

sendresp:
2630 2631 2632 2633
	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 已提交
2634
	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649

	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);
	}

2650
	if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
2651 2652
				result == L2CAP_CR_SUCCESS) {
		u8 buf[128];
2653
		set_bit(CONF_REQ_SENT, &chan->conf_state);
2654
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2655 2656
					l2cap_build_conf_req(chan, buf), buf);
		chan->num_conf_req++;
2657 2658
	}

L
Linus Torvalds 已提交
2659 2660 2661 2662 2663 2664 2665
	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;
2666
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
	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) {
2678
		chan = l2cap_get_chan_by_scid(conn, scid);
2679
		if (!chan)
2680
			return -EFAULT;
L
Linus Torvalds 已提交
2681
	} else {
2682
		chan = l2cap_get_chan_by_ident(conn, cmd->ident);
2683
		if (!chan)
2684
			return -EFAULT;
L
Linus Torvalds 已提交
2685 2686
	}

2687 2688
	sk = chan->sk;

L
Linus Torvalds 已提交
2689 2690
	switch (result) {
	case L2CAP_CR_SUCCESS:
2691
		l2cap_state_change(chan, BT_CONFIG);
2692
		chan->ident = 0;
2693
		chan->dcid = dcid;
2694
		clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
2695

2696
		if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
2697 2698
			break;

L
Linus Torvalds 已提交
2699
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2700 2701
					l2cap_build_conf_req(chan, req), req);
		chan->num_conf_req++;
L
Linus Torvalds 已提交
2702 2703 2704
		break;

	case L2CAP_CR_PEND:
2705
		set_bit(CONF_CONNECT_PEND, &chan->conf_state);
L
Linus Torvalds 已提交
2706 2707 2708
		break;

	default:
2709 2710
		/* don't delete l2cap channel if sk is owned by user */
		if (sock_owned_by_user(sk)) {
2711
			l2cap_state_change(chan, BT_DISCONN);
2712
			__clear_chan_timer(chan);
2713
			__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
2714 2715 2716
			break;
		}

2717
		l2cap_chan_del(chan, ECONNREFUSED);
L
Linus Torvalds 已提交
2718 2719 2720 2721 2722 2723 2724
		break;
	}

	bh_unlock_sock(sk);
	return 0;
}

2725
static inline void set_default_fcs(struct l2cap_chan *chan)
2726 2727 2728 2729
{
	/* FCS is enabled only in ERTM or streaming mode, if one or both
	 * sides request it.
	 */
2730
	if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
2731
		chan->fcs = L2CAP_FCS_NONE;
2732
	else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state))
2733
		chan->fcs = L2CAP_FCS_CRC16;
2734 2735
}

2736
static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
L
Linus Torvalds 已提交
2737 2738 2739 2740
{
	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
	u16 dcid, flags;
	u8 rsp[64];
2741
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2742
	struct sock *sk;
2743
	int len;
L
Linus Torvalds 已提交
2744 2745 2746 2747 2748 2749

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

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

2750
	chan = l2cap_get_chan_by_scid(conn, dcid);
2751
	if (!chan)
L
Linus Torvalds 已提交
2752 2753
		return -ENOENT;

2754 2755
	sk = chan->sk;

2756
	if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
2757 2758 2759 2760 2761
		struct l2cap_cmd_rej_cid rej;

		rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID);
		rej.scid = cpu_to_le16(chan->scid);
		rej.dcid = cpu_to_le16(chan->dcid);
2762 2763 2764

		l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
				sizeof(rej), &rej);
2765
		goto unlock;
2766
	}
2767

2768
	/* Reject if config buffer is too small. */
2769
	len = cmd_len - sizeof(*req);
2770
	if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) {
2771
		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2772
				l2cap_build_conf_rsp(chan, rsp,
2773 2774 2775 2776 2777
					L2CAP_CONF_REJECT, flags), rsp);
		goto unlock;
	}

	/* Store config. */
2778 2779
	memcpy(chan->conf_req + chan->conf_len, req->data, len);
	chan->conf_len += len;
L
Linus Torvalds 已提交
2780 2781 2782 2783

	if (flags & 0x0001) {
		/* Incomplete config. Send empty response. */
		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2784
				l2cap_build_conf_rsp(chan, rsp,
2785
					L2CAP_CONF_SUCCESS, 0x0001), rsp);
L
Linus Torvalds 已提交
2786 2787 2788 2789
		goto unlock;
	}

	/* Complete config. */
2790
	len = l2cap_parse_conf_req(chan, rsp);
2791
	if (len < 0) {
2792
		l2cap_send_disconn_req(conn, chan, ECONNRESET);
L
Linus Torvalds 已提交
2793
		goto unlock;
2794
	}
L
Linus Torvalds 已提交
2795

2796
	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
2797
	chan->num_conf_rsp++;
2798 2799

	/* Reset config buffer. */
2800
	chan->conf_len = 0;
2801

2802
	if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
2803 2804
		goto unlock;

2805
	if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
2806
		set_default_fcs(chan);
2807

2808
		l2cap_state_change(chan, BT_CONNECTED);
2809

2810 2811
		chan->next_tx_seq = 0;
		chan->expected_tx_seq = 0;
2812
		skb_queue_head_init(&chan->tx_q);
2813
		if (chan->mode == L2CAP_MODE_ERTM)
2814
			l2cap_ertm_init(chan);
2815

L
Linus Torvalds 已提交
2816
		l2cap_chan_ready(sk);
2817 2818 2819
		goto unlock;
	}

2820
	if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
2821
		u8 buf[64];
L
Linus Torvalds 已提交
2822
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2823 2824
					l2cap_build_conf_req(chan, buf), buf);
		chan->num_conf_req++;
L
Linus Torvalds 已提交
2825 2826
	}

2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837
	/* Got Conf Rsp PENDING from remote side and asume we sent
	   Conf Rsp PENDING in the code above */
	if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
			test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {

		/* check compatibility */

		clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
		set_bit(CONF_OUTPUT_DONE, &chan->conf_state);

		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2838
					l2cap_build_conf_rsp(chan, rsp,
2839 2840 2841
					L2CAP_CONF_SUCCESS, 0x0000), rsp);
	}

L
Linus Torvalds 已提交
2842 2843 2844 2845 2846 2847 2848 2849 2850
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;
2851
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2852
	struct sock *sk;
2853
	int len = cmd->len - sizeof(*rsp);
L
Linus Torvalds 已提交
2854 2855 2856 2857 2858

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

2859 2860
	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
			scid, flags, result);
L
Linus Torvalds 已提交
2861

2862
	chan = l2cap_get_chan_by_scid(conn, scid);
2863
	if (!chan)
L
Linus Torvalds 已提交
2864 2865
		return 0;

2866 2867
	sk = chan->sk;

L
Linus Torvalds 已提交
2868 2869
	switch (result) {
	case L2CAP_CONF_SUCCESS:
2870
		l2cap_conf_rfc_get(chan, rsp->data, len);
2871
		clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
L
Linus Torvalds 已提交
2872 2873
		break;

2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892
	case L2CAP_CONF_PENDING:
		set_bit(CONF_REM_CONF_PEND, &chan->conf_state);

		if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
			char buf[64];

			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
								buf, &result);
			if (len < 0) {
				l2cap_send_disconn_req(conn, chan, ECONNRESET);
				goto done;
			}

			/* check compatibility */

			clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
			set_bit(CONF_OUTPUT_DONE, &chan->conf_state);

			l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2893
						l2cap_build_conf_rsp(chan, buf,
2894 2895 2896 2897
						L2CAP_CONF_SUCCESS, 0x0000), buf);
		}
		goto done;

L
Linus Torvalds 已提交
2898
	case L2CAP_CONF_UNACCEPT:
2899
		if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
2900 2901
			char req[64];

2902
			if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
2903
				l2cap_send_disconn_req(conn, chan, ECONNRESET);
2904 2905 2906
				goto done;
			}

2907 2908
			/* throw out any old stored conf requests */
			result = L2CAP_CONF_SUCCESS;
2909 2910
			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
								req, &result);
2911
			if (len < 0) {
2912
				l2cap_send_disconn_req(conn, chan, ECONNRESET);
2913 2914 2915 2916 2917
				goto done;
			}

			l2cap_send_cmd(conn, l2cap_get_ident(conn),
						L2CAP_CONF_REQ, len, req);
2918
			chan->num_conf_req++;
2919 2920 2921
			if (result != L2CAP_CONF_SUCCESS)
				goto done;
			break;
L
Linus Torvalds 已提交
2922 2923
		}

2924
	default:
2925
		sk->sk_err = ECONNRESET;
2926
		__set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT);
2927
		l2cap_send_disconn_req(conn, chan, ECONNRESET);
L
Linus Torvalds 已提交
2928 2929 2930 2931 2932 2933
		goto done;
	}

	if (flags & 0x01)
		goto done;

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

2936
	if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
2937
		set_default_fcs(chan);
2938

2939
		l2cap_state_change(chan, BT_CONNECTED);
2940 2941
		chan->next_tx_seq = 0;
		chan->expected_tx_seq = 0;
2942
		skb_queue_head_init(&chan->tx_q);
2943
		if (chan->mode ==  L2CAP_MODE_ERTM)
2944
			l2cap_ertm_init(chan);
2945

L
Linus Torvalds 已提交
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
		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;
2959
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2960 2961 2962 2963 2964 2965 2966
	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);

2967
	chan = l2cap_get_chan_by_scid(conn, dcid);
2968
	if (!chan)
L
Linus Torvalds 已提交
2969 2970
		return 0;

2971 2972
	sk = chan->sk;

2973 2974
	rsp.dcid = cpu_to_le16(chan->scid);
	rsp.scid = cpu_to_le16(chan->dcid);
L
Linus Torvalds 已提交
2975 2976 2977 2978
	l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);

	sk->sk_shutdown = SHUTDOWN_MASK;

2979 2980
	/* don't delete l2cap channel if sk is owned by user */
	if (sock_owned_by_user(sk)) {
2981
		l2cap_state_change(chan, BT_DISCONN);
2982
		__clear_chan_timer(chan);
2983
		__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
2984 2985 2986 2987
		bh_unlock_sock(sk);
		return 0;
	}

2988
	l2cap_chan_del(chan, ECONNRESET);
L
Linus Torvalds 已提交
2989 2990
	bh_unlock_sock(sk);

2991
	chan->ops->close(chan->data);
L
Linus Torvalds 已提交
2992 2993 2994 2995 2996 2997 2998
	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;
2999
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
3000 3001 3002 3003 3004 3005 3006
	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);

3007
	chan = l2cap_get_chan_by_scid(conn, scid);
3008
	if (!chan)
L
Linus Torvalds 已提交
3009 3010
		return 0;

3011 3012
	sk = chan->sk;

3013 3014
	/* don't delete l2cap channel if sk is owned by user */
	if (sock_owned_by_user(sk)) {
3015
		l2cap_state_change(chan, BT_DISCONN);
3016
		__clear_chan_timer(chan);
3017
		__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
3018 3019 3020 3021
		bh_unlock_sock(sk);
		return 0;
	}

3022
	l2cap_chan_del(chan, 0);
L
Linus Torvalds 已提交
3023 3024
	bh_unlock_sock(sk);

3025
	chan->ops->close(chan->data);
L
Linus Torvalds 已提交
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037
	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);

3038 3039
	if (type == L2CAP_IT_FEAT_MASK) {
		u8 buf[8];
3040
		u32 feat_mask = l2cap_feat_mask;
3041 3042 3043
		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);
3044
		if (!disable_ertm)
3045 3046
			feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
							 | L2CAP_FEAT_FCS;
3047
		if (enable_hs)
3048 3049
			feat_mask |= L2CAP_FEAT_EXT_FLOW
						| L2CAP_FEAT_EXT_WINDOW;
3050

3051
		put_unaligned_le32(feat_mask, rsp->data);
3052 3053
		l2cap_send_cmd(conn, cmd->ident,
					L2CAP_INFO_RSP, sizeof(buf), buf);
3054 3055 3056
	} else if (type == L2CAP_IT_FIXED_CHAN) {
		u8 buf[12];
		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
3057 3058 3059 3060 3061 3062

		if (enable_hs)
			l2cap_fixed_chan[0] |= L2CAP_FC_A2MP;
		else
			l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP;

3063 3064
		rsp->type   = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
3065
		memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan));
3066 3067
		l2cap_send_cmd(conn, cmd->ident,
					L2CAP_INFO_RSP, sizeof(buf), buf);
3068 3069 3070 3071 3072 3073 3074
	} 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 已提交
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088

	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);

3089 3090 3091 3092 3093
	/* 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;

3094 3095
	del_timer(&conn->info_timer);

3096 3097 3098 3099 3100 3101 3102 3103 3104
	if (result != L2CAP_IR_SUCCESS) {
		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
		conn->info_ident = 0;

		l2cap_conn_start(conn);

		return 0;
	}

3105
	if (type == L2CAP_IT_FEAT_MASK) {
3106
		conn->feat_mask = get_unaligned_le32(rsp->data);
3107

3108
		if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122
			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) {
3123
		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
3124
		conn->info_ident = 0;
3125 3126 3127

		l2cap_conn_start(conn);
	}
3128

L
Linus Torvalds 已提交
3129 3130 3131
	return 0;
}

3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170
static inline int l2cap_create_channel_req(struct l2cap_conn *conn,
					struct l2cap_cmd_hdr *cmd, u16 cmd_len,
					void *data)
{
	struct l2cap_create_chan_req *req = data;
	struct l2cap_create_chan_rsp rsp;
	u16 psm, scid;

	if (cmd_len != sizeof(*req))
		return -EPROTO;

	if (!enable_hs)
		return -EINVAL;

	psm = le16_to_cpu(req->psm);
	scid = le16_to_cpu(req->scid);

	BT_DBG("psm %d, scid %d, amp_id %d", psm, scid, req->amp_id);

	/* Placeholder: Always reject */
	rsp.dcid = 0;
	rsp.scid = cpu_to_le16(scid);
	rsp.result = L2CAP_CR_NO_MEM;
	rsp.status = L2CAP_CS_NO_INFO;

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

	return 0;
}

static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn,
					struct l2cap_cmd_hdr *cmd, void *data)
{
	BT_DBG("conn %p", conn);

	return l2cap_connect_rsp(conn, cmd, data);
}

3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290
static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident,
							u16 icid, u16 result)
{
	struct l2cap_move_chan_rsp rsp;

	BT_DBG("icid %d, result %d", icid, result);

	rsp.icid = cpu_to_le16(icid);
	rsp.result = cpu_to_le16(result);

	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp);
}

static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn,
				struct l2cap_chan *chan, u16 icid, u16 result)
{
	struct l2cap_move_chan_cfm cfm;
	u8 ident;

	BT_DBG("icid %d, result %d", icid, result);

	ident = l2cap_get_ident(conn);
	if (chan)
		chan->ident = ident;

	cfm.icid = cpu_to_le16(icid);
	cfm.result = cpu_to_le16(result);

	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm);
}

static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
								u16 icid)
{
	struct l2cap_move_chan_cfm_rsp rsp;

	BT_DBG("icid %d", icid);

	rsp.icid = cpu_to_le16(icid);
	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
}

static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
{
	struct l2cap_move_chan_req *req = data;
	u16 icid = 0;
	u16 result = L2CAP_MR_NOT_ALLOWED;

	if (cmd_len != sizeof(*req))
		return -EPROTO;

	icid = le16_to_cpu(req->icid);

	BT_DBG("icid %d, dest_amp_id %d", icid, req->dest_amp_id);

	if (!enable_hs)
		return -EINVAL;

	/* Placeholder: Always refuse */
	l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result);

	return 0;
}

static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn,
			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
{
	struct l2cap_move_chan_rsp *rsp = data;
	u16 icid, result;

	if (cmd_len != sizeof(*rsp))
		return -EPROTO;

	icid = le16_to_cpu(rsp->icid);
	result = le16_to_cpu(rsp->result);

	BT_DBG("icid %d, result %d", icid, result);

	/* Placeholder: Always unconfirmed */
	l2cap_send_move_chan_cfm(conn, NULL, icid, L2CAP_MC_UNCONFIRMED);

	return 0;
}

static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn,
			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
{
	struct l2cap_move_chan_cfm *cfm = data;
	u16 icid, result;

	if (cmd_len != sizeof(*cfm))
		return -EPROTO;

	icid = le16_to_cpu(cfm->icid);
	result = le16_to_cpu(cfm->result);

	BT_DBG("icid %d, result %d", icid, result);

	l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);

	return 0;
}

static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
{
	struct l2cap_move_chan_cfm_rsp *rsp = data;
	u16 icid;

	if (cmd_len != sizeof(*rsp))
		return -EPROTO;

	icid = le16_to_cpu(rsp->icid);

	BT_DBG("icid %d", icid);

	return 0;
}

3291
static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318
							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;
3319
	int err;
3320 3321 3322 3323 3324 3325 3326 3327 3328

	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;
3329 3330
	min		= __le16_to_cpu(req->min);
	max		= __le16_to_cpu(req->max);
3331 3332 3333 3334 3335 3336 3337
	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));
3338 3339 3340

	err = l2cap_check_conn_param(min, max, latency, to_multiplier);
	if (err)
3341 3342 3343 3344 3345 3346 3347
		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);

3348 3349 3350
	if (!err)
		hci_le_conn_update(hcon, min, max, latency, to_multiplier);

3351 3352 3353
	return 0;
}

3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
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;

3403 3404 3405 3406 3407 3408 3409 3410
	case L2CAP_CREATE_CHAN_REQ:
		err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
		break;

	case L2CAP_CREATE_CHAN_RSP:
		err = l2cap_create_channel_rsp(conn, cmd, data);
		break;

3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426
	case L2CAP_MOVE_CHAN_REQ:
		err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
		break;

	case L2CAP_MOVE_CHAN_RSP:
		err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
		break;

	case L2CAP_MOVE_CHAN_CFM:
		err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
		break;

	case L2CAP_MOVE_CHAN_CFM_RSP:
		err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
		break;

3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443
	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:
3444
		return l2cap_conn_param_update_req(conn, cmd, data);
3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456

	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 已提交
3457 3458 3459 3460
{
	u8 *data = skb->data;
	int len = skb->len;
	struct l2cap_cmd_hdr cmd;
3461
	int err;
L
Linus Torvalds 已提交
3462 3463 3464 3465

	l2cap_raw_recv(conn, skb);

	while (len >= L2CAP_CMD_HDR_SIZE) {
3466
		u16 cmd_len;
L
Linus Torvalds 已提交
3467 3468 3469 3470
		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
		data += L2CAP_CMD_HDR_SIZE;
		len  -= L2CAP_CMD_HDR_SIZE;

3471
		cmd_len = le16_to_cpu(cmd.len);
L
Linus Torvalds 已提交
3472

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

3475
		if (cmd_len > len || !cmd.ident) {
L
Linus Torvalds 已提交
3476 3477 3478 3479
			BT_DBG("corrupted command");
			break;
		}

3480 3481 3482 3483
		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 已提交
3484 3485

		if (err) {
3486
			struct l2cap_cmd_rej_unk rej;
3487 3488

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

			/* FIXME: Map err to a valid reason */
3491
			rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
L
Linus Torvalds 已提交
3492 3493 3494
			l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
		}

3495 3496
		data += cmd_len;
		len  -= cmd_len;
L
Linus Torvalds 已提交
3497 3498 3499 3500 3501
	}

	kfree_skb(skb);
}

3502
static int l2cap_check_fcs(struct l2cap_chan *chan,  struct sk_buff *skb)
3503 3504
{
	u16 our_fcs, rcv_fcs;
3505 3506 3507 3508 3509 3510
	int hdr_size;

	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
		hdr_size = L2CAP_EXT_HDR_SIZE;
	else
		hdr_size = L2CAP_ENH_HDR_SIZE;
3511

3512
	if (chan->fcs == L2CAP_FCS_CRC16) {
3513
		skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
3514 3515 3516 3517
		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)
3518
			return -EBADMSG;
3519 3520 3521 3522
	}
	return 0;
}

3523
static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
3524
{
3525
	u32 control = 0;
3526

3527
	chan->frames_sent = 0;
3528

3529
	control |= __set_reqseq(chan, chan->buffer_seq);
3530

3531
	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
3532
		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
3533
		l2cap_send_sframe(chan, control);
3534
		set_bit(CONN_RNR_SENT, &chan->conn_state);
3535 3536
	}

3537
	if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
3538
		l2cap_retransmit_frames(chan);
3539

3540
	l2cap_ertm_send(chan);
3541

3542
	if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
3543
			chan->frames_sent == 0) {
3544
		control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
3545
		l2cap_send_sframe(chan, control);
3546 3547 3548
	}
}

3549
static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u16 tx_seq, u8 sar)
3550 3551
{
	struct sk_buff *next_skb;
3552
	int tx_seq_offset, next_tx_seq_offset;
3553 3554 3555 3556

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

3557
	next_skb = skb_peek(&chan->srej_q);
3558

3559
	tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq);
3560

3561
	while (next_skb) {
3562 3563 3564
		if (bt_cb(next_skb)->tx_seq == tx_seq)
			return -EINVAL;

3565 3566
		next_tx_seq_offset = __seq_offset(chan,
				bt_cb(next_skb)->tx_seq, chan->buffer_seq);
3567 3568

		if (next_tx_seq_offset > tx_seq_offset) {
3569
			__skb_queue_before(&chan->srej_q, next_skb, skb);
3570
			return 0;
3571 3572
		}

3573
		if (skb_queue_is_last(&chan->srej_q, next_skb))
3574 3575 3576 3577
			next_skb = NULL;
		else
			next_skb = skb_queue_next(&chan->srej_q, next_skb);
	}
3578

3579
	__skb_queue_tail(&chan->srej_q, skb);
3580 3581

	return 0;
3582 3583
}

3584 3585
static void append_skb_frag(struct sk_buff *skb,
			struct sk_buff *new_frag, struct sk_buff **last_frag)
3586
{
3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602
	/* skb->len reflects data in skb as well as all fragments
	 * skb->data_len reflects only data in fragments
	 */
	if (!skb_has_frag_list(skb))
		skb_shinfo(skb)->frag_list = new_frag;

	new_frag->next = NULL;

	(*last_frag)->next = new_frag;
	*last_frag = new_frag;

	skb->len += new_frag->len;
	skb->data_len += new_frag->len;
	skb->truesize += new_frag->truesize;
}

3603
static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u32 control)
3604 3605
{
	int err = -EINVAL;
3606

3607 3608
	switch (__get_ctrl_sar(chan, control)) {
	case L2CAP_SAR_UNSEGMENTED:
3609 3610
		if (chan->sdu)
			break;
3611

3612 3613
		err = chan->ops->recv(chan->data, skb);
		break;
3614

3615
	case L2CAP_SAR_START:
3616 3617
		if (chan->sdu)
			break;
3618

3619
		chan->sdu_len = get_unaligned_le16(skb->data);
3620
		skb_pull(skb, L2CAP_SDULEN_SIZE);
3621

3622 3623 3624 3625
		if (chan->sdu_len > chan->imtu) {
			err = -EMSGSIZE;
			break;
		}
3626

3627 3628
		if (skb->len >= chan->sdu_len)
			break;
3629

3630 3631
		chan->sdu = skb;
		chan->sdu_last_frag = skb;
3632

3633 3634
		skb = NULL;
		err = 0;
3635 3636
		break;

3637
	case L2CAP_SAR_CONTINUE:
3638
		if (!chan->sdu)
3639
			break;
3640

3641 3642 3643
		append_skb_frag(chan->sdu, skb,
				&chan->sdu_last_frag);
		skb = NULL;
3644

3645 3646
		if (chan->sdu->len >= chan->sdu_len)
			break;
3647

3648
		err = 0;
3649 3650
		break;

3651
	case L2CAP_SAR_END:
3652
		if (!chan->sdu)
3653
			break;
3654

3655 3656 3657
		append_skb_frag(chan->sdu, skb,
				&chan->sdu_last_frag);
		skb = NULL;
3658

3659 3660
		if (chan->sdu->len != chan->sdu_len)
			break;
3661

3662
		err = chan->ops->recv(chan->data, chan->sdu);
3663

3664 3665 3666 3667 3668
		if (!err) {
			/* Reassembly complete */
			chan->sdu = NULL;
			chan->sdu_last_frag = NULL;
			chan->sdu_len = 0;
3669
		}
3670 3671 3672
		break;
	}

3673 3674 3675 3676 3677 3678 3679
	if (err) {
		kfree_skb(skb);
		kfree_skb(chan->sdu);
		chan->sdu = NULL;
		chan->sdu_last_frag = NULL;
		chan->sdu_len = 0;
	}
3680

3681
	return err;
3682 3683
}

3684
static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan)
3685
{
3686
	u32 control;
3687

3688
	BT_DBG("chan %p, Enter local busy", chan);
3689

3690 3691
	set_bit(CONN_LOCAL_BUSY, &chan->conn_state);

3692
	control = __set_reqseq(chan, chan->buffer_seq);
3693
	control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
3694 3695 3696 3697 3698 3699 3700 3701 3702
	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)
{
3703
	u32 control;
3704

3705
	if (!test_bit(CONN_RNR_SENT, &chan->conn_state))
3706 3707
		goto done;

3708
	control = __set_reqseq(chan, chan->buffer_seq);
3709
	control |= __set_ctrl_poll(chan);
3710
	control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
3711
	l2cap_send_sframe(chan, control);
3712
	chan->retry_count = 1;
3713

3714 3715
	__clear_retrans_timer(chan);
	__set_monitor_timer(chan);
3716

3717
	set_bit(CONN_WAIT_F, &chan->conn_state);
3718 3719

done:
3720 3721
	clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
	clear_bit(CONN_RNR_SENT, &chan->conn_state);
3722

3723
	BT_DBG("chan %p, Exit local busy", chan);
3724 3725
}

3726
void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
3727
{
3728 3729 3730 3731 3732
	if (chan->mode == L2CAP_MODE_ERTM) {
		if (busy)
			l2cap_ertm_enter_local_busy(chan);
		else
			l2cap_ertm_exit_local_busy(chan);
3733 3734 3735
	}
}

3736
static void l2cap_check_srej_gap(struct l2cap_chan *chan, u16 tx_seq)
3737 3738
{
	struct sk_buff *skb;
3739
	u32 control;
3740

3741 3742 3743 3744
	while ((skb = skb_peek(&chan->srej_q)) &&
			!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
		int err;

3745 3746 3747
		if (bt_cb(skb)->tx_seq != tx_seq)
			break;

3748
		skb = skb_dequeue(&chan->srej_q);
3749
		control = __set_ctrl_sar(chan, bt_cb(skb)->sar);
3750
		err = l2cap_reassemble_sdu(chan, skb, control);
3751 3752 3753 3754 3755 3756

		if (err < 0) {
			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
			break;
		}

3757 3758
		chan->buffer_seq_srej = __next_seq(chan, chan->buffer_seq_srej);
		tx_seq = __next_seq(chan, tx_seq);
3759 3760 3761
	}
}

3762
static void l2cap_resend_srejframe(struct l2cap_chan *chan, u16 tx_seq)
3763 3764
{
	struct srej_list *l, *tmp;
3765
	u32 control;
3766

3767
	list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
3768 3769 3770 3771 3772
		if (l->tx_seq == tx_seq) {
			list_del(&l->list);
			kfree(l);
			return;
		}
3773
		control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
3774
		control |= __set_reqseq(chan, l->tx_seq);
3775
		l2cap_send_sframe(chan, control);
3776
		list_del(&l->list);
3777
		list_add_tail(&l->list, &chan->srej_l);
3778 3779 3780
	}
}

3781
static int l2cap_send_srejframe(struct l2cap_chan *chan, u16 tx_seq)
3782 3783
{
	struct srej_list *new;
3784
	u32 control;
3785

3786
	while (tx_seq != chan->expected_tx_seq) {
3787
		control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
3788
		control |= __set_reqseq(chan, chan->expected_tx_seq);
3789
		l2cap_send_sframe(chan, control);
3790 3791

		new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC);
3792 3793 3794
		if (!new)
			return -ENOMEM;

3795
		new->tx_seq = chan->expected_tx_seq;
3796 3797 3798

		chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);

3799
		list_add_tail(&new->list, &chan->srej_l);
3800
	}
3801 3802

	chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);
3803 3804

	return 0;
3805 3806
}

3807
static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb)
3808
{
3809
	u16 tx_seq = __get_txseq(chan, rx_control);
3810
	u16 req_seq = __get_reqseq(chan, rx_control);
3811
	u8 sar = __get_ctrl_sar(chan, rx_control);
3812
	int tx_seq_offset, expected_tx_seq_offset;
3813
	int num_to_ack = (chan->tx_win/6) + 1;
3814 3815
	int err = 0;

3816
	BT_DBG("chan %p len %d tx_seq %d rx_control 0x%8.8x", chan, skb->len,
3817
							tx_seq, rx_control);
3818

3819
	if (__is_ctrl_final(chan, rx_control) &&
3820
			test_bit(CONN_WAIT_F, &chan->conn_state)) {
3821
		__clear_monitor_timer(chan);
3822
		if (chan->unacked_frames > 0)
3823
			__set_retrans_timer(chan);
3824
		clear_bit(CONN_WAIT_F, &chan->conn_state);
3825 3826
	}

3827 3828
	chan->expected_ack_seq = req_seq;
	l2cap_drop_acked_frames(chan);
3829

3830
	tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq);
3831 3832

	/* invalid tx_seq */
3833
	if (tx_seq_offset >= chan->tx_win) {
3834
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3835 3836 3837
		goto drop;
	}

3838
	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
3839 3840
		goto drop;

3841 3842 3843
	if (tx_seq == chan->expected_tx_seq)
		goto expected;

3844
	if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
3845
		struct srej_list *first;
3846

3847
		first = list_first_entry(&chan->srej_l,
3848 3849
				struct srej_list, list);
		if (tx_seq == first->tx_seq) {
3850
			l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3851
			l2cap_check_srej_gap(chan, tx_seq);
3852 3853 3854 3855

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

3856
			if (list_empty(&chan->srej_l)) {
3857
				chan->buffer_seq = chan->buffer_seq_srej;
3858
				clear_bit(CONN_SREJ_SENT, &chan->conn_state);
3859
				l2cap_send_ack(chan);
3860
				BT_DBG("chan %p, Exit SREJ_SENT", chan);
3861 3862 3863
			}
		} else {
			struct srej_list *l;
3864 3865

			/* duplicated tx_seq */
3866
			if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0)
3867
				goto drop;
3868

3869
			list_for_each_entry(l, &chan->srej_l, list) {
3870
				if (l->tx_seq == tx_seq) {
3871
					l2cap_resend_srejframe(chan, tx_seq);
3872 3873 3874
					return 0;
				}
			}
3875 3876 3877 3878 3879 3880

			err = l2cap_send_srejframe(chan, tx_seq);
			if (err < 0) {
				l2cap_send_disconn_req(chan->conn, chan, -err);
				return err;
			}
3881 3882
		}
	} else {
3883 3884
		expected_tx_seq_offset = __seq_offset(chan,
				chan->expected_tx_seq, chan->buffer_seq);
3885 3886 3887 3888 3889

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

3890
		set_bit(CONN_SREJ_SENT, &chan->conn_state);
3891

3892
		BT_DBG("chan %p, Enter SREJ", chan);
3893

3894
		INIT_LIST_HEAD(&chan->srej_l);
3895
		chan->buffer_seq_srej = chan->buffer_seq;
3896

3897
		__skb_queue_head_init(&chan->srej_q);
3898
		l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3899

3900
		set_bit(CONN_SEND_PBIT, &chan->conn_state);
3901

3902 3903 3904 3905 3906
		err = l2cap_send_srejframe(chan, tx_seq);
		if (err < 0) {
			l2cap_send_disconn_req(chan->conn, chan, -err);
			return err;
		}
3907

3908
		__clear_ack_timer(chan);
3909
	}
3910 3911
	return 0;

3912
expected:
3913
	chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);
3914

3915
	if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
3916 3917
		bt_cb(skb)->tx_seq = tx_seq;
		bt_cb(skb)->sar = sar;
3918
		__skb_queue_tail(&chan->srej_q, skb);
3919 3920 3921
		return 0;
	}

3922
	err = l2cap_reassemble_sdu(chan, skb, rx_control);
3923 3924
	chan->buffer_seq = __next_seq(chan, chan->buffer_seq);

3925 3926 3927 3928
	if (err < 0) {
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
		return err;
	}
3929

3930
	if (__is_ctrl_final(chan, rx_control)) {
3931
		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
3932
			l2cap_retransmit_frames(chan);
3933 3934
	}

3935

3936 3937
	chan->num_acked = (chan->num_acked + 1) % num_to_ack;
	if (chan->num_acked == num_to_ack - 1)
3938
		l2cap_send_ack(chan);
3939 3940
	else
		__set_ack_timer(chan);
3941

3942
	return 0;
3943 3944 3945 3946

drop:
	kfree_skb(skb);
	return 0;
3947 3948
}

3949
static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u32 rx_control)
3950
{
3951
	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan,
3952
				__get_reqseq(chan, rx_control), rx_control);
3953

3954
	chan->expected_ack_seq = __get_reqseq(chan, rx_control);
3955
	l2cap_drop_acked_frames(chan);
3956

3957
	if (__is_ctrl_poll(chan, rx_control)) {
3958 3959 3960
		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) &&
3961
					(chan->unacked_frames > 0))
3962
				__set_retrans_timer(chan);
3963

3964
			clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
3965
			l2cap_send_srejtail(chan);
3966
		} else {
3967
			l2cap_send_i_or_rr_or_rnr(chan);
3968
		}
3969

3970
	} else if (__is_ctrl_final(chan, rx_control)) {
3971
		clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
3972

3973
		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
3974
			l2cap_retransmit_frames(chan);
3975

3976
	} else {
3977
		if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
3978
				(chan->unacked_frames > 0))
3979
			__set_retrans_timer(chan);
3980

3981 3982
		clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
		if (test_bit(CONN_SREJ_SENT, &chan->conn_state))
3983
			l2cap_send_ack(chan);
3984
		else
3985
			l2cap_ertm_send(chan);
3986 3987
	}
}
3988

3989
static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u32 rx_control)
3990
{
3991
	u16 tx_seq = __get_reqseq(chan, rx_control);
3992

3993
	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
3994

3995
	clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
3996

3997 3998
	chan->expected_ack_seq = tx_seq;
	l2cap_drop_acked_frames(chan);
3999

4000
	if (__is_ctrl_final(chan, rx_control)) {
4001
		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
4002
			l2cap_retransmit_frames(chan);
4003
	} else {
4004
		l2cap_retransmit_frames(chan);
4005

4006 4007
		if (test_bit(CONN_WAIT_F, &chan->conn_state))
			set_bit(CONN_REJ_ACT, &chan->conn_state);
4008 4009
	}
}
4010
static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u32 rx_control)
4011
{
4012
	u16 tx_seq = __get_reqseq(chan, rx_control);
4013

4014
	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
4015

4016
	clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
4017

4018
	if (__is_ctrl_poll(chan, rx_control)) {
4019 4020
		chan->expected_ack_seq = tx_seq;
		l2cap_drop_acked_frames(chan);
4021

4022
		set_bit(CONN_SEND_FBIT, &chan->conn_state);
4023
		l2cap_retransmit_one_frame(chan, tx_seq);
4024

4025
		l2cap_ertm_send(chan);
4026

4027
		if (test_bit(CONN_WAIT_F, &chan->conn_state)) {
4028
			chan->srej_save_reqseq = tx_seq;
4029
			set_bit(CONN_SREJ_ACT, &chan->conn_state);
4030
		}
4031
	} else if (__is_ctrl_final(chan, rx_control)) {
4032
		if (test_bit(CONN_SREJ_ACT, &chan->conn_state) &&
4033
				chan->srej_save_reqseq == tx_seq)
4034
			clear_bit(CONN_SREJ_ACT, &chan->conn_state);
4035
		else
4036
			l2cap_retransmit_one_frame(chan, tx_seq);
4037
	} else {
4038
		l2cap_retransmit_one_frame(chan, tx_seq);
4039
		if (test_bit(CONN_WAIT_F, &chan->conn_state)) {
4040
			chan->srej_save_reqseq = tx_seq;
4041
			set_bit(CONN_SREJ_ACT, &chan->conn_state);
4042
		}
4043 4044 4045
	}
}

4046
static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u32 rx_control)
4047
{
4048
	u16 tx_seq = __get_reqseq(chan, rx_control);
4049

4050
	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
4051

4052
	set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
4053 4054
	chan->expected_ack_seq = tx_seq;
	l2cap_drop_acked_frames(chan);
4055

4056
	if (__is_ctrl_poll(chan, rx_control))
4057
		set_bit(CONN_SEND_FBIT, &chan->conn_state);
4058

4059
	if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
4060
		__clear_retrans_timer(chan);
4061
		if (__is_ctrl_poll(chan, rx_control))
4062
			l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL);
4063
		return;
4064
	}
4065

4066
	if (__is_ctrl_poll(chan, rx_control)) {
4067
		l2cap_send_srejtail(chan);
4068 4069 4070 4071
	} else {
		rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR);
		l2cap_send_sframe(chan, rx_control);
	}
4072 4073
}

4074
static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb)
4075
{
4076
	BT_DBG("chan %p rx_control 0x%8.8x len %d", chan, rx_control, skb->len);
4077

4078
	if (__is_ctrl_final(chan, rx_control) &&
4079
			test_bit(CONN_WAIT_F, &chan->conn_state)) {
4080
		__clear_monitor_timer(chan);
4081
		if (chan->unacked_frames > 0)
4082
			__set_retrans_timer(chan);
4083
		clear_bit(CONN_WAIT_F, &chan->conn_state);
4084 4085
	}

4086 4087
	switch (__get_ctrl_super(chan, rx_control)) {
	case L2CAP_SUPER_RR:
4088
		l2cap_data_channel_rrframe(chan, rx_control);
4089 4090
		break;

4091
	case L2CAP_SUPER_REJ:
4092
		l2cap_data_channel_rejframe(chan, rx_control);
4093
		break;
4094

4095
	case L2CAP_SUPER_SREJ:
4096
		l2cap_data_channel_srejframe(chan, rx_control);
4097 4098
		break;

4099
	case L2CAP_SUPER_RNR:
4100
		l2cap_data_channel_rnrframe(chan, rx_control);
4101 4102 4103
		break;
	}

4104
	kfree_skb(skb);
4105 4106 4107
	return 0;
}

4108 4109
static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb)
{
4110
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
4111
	u32 control;
4112
	u16 req_seq;
4113 4114
	int len, next_tx_seq_offset, req_seq_offset;

4115 4116
	control = __get_control(chan, skb->data);
	skb_pull(skb, __ctrl_size(chan));
4117 4118 4119 4120 4121 4122 4123
	len = skb->len;

	/*
	 * We can just drop the corrupted I-frame here.
	 * Receiver will miss it and start proper recovery
	 * procedures and ask retransmission.
	 */
4124
	if (l2cap_check_fcs(chan, skb))
4125 4126
		goto drop;

4127
	if (__is_sar_start(chan, control) && !__is_sframe(chan, control))
4128
		len -= L2CAP_SDULEN_SIZE;
4129

4130
	if (chan->fcs == L2CAP_FCS_CRC16)
4131
		len -= L2CAP_FCS_SIZE;
4132

4133
	if (len > chan->mps) {
4134
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
4135 4136 4137
		goto drop;
	}

4138
	req_seq = __get_reqseq(chan, control);
4139

4140 4141 4142 4143
	req_seq_offset = __seq_offset(chan, req_seq, chan->expected_ack_seq);

	next_tx_seq_offset = __seq_offset(chan, chan->next_tx_seq,
						chan->expected_ack_seq);
4144 4145 4146

	/* check for invalid req-seq */
	if (req_seq_offset > next_tx_seq_offset) {
4147
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
4148 4149 4150
		goto drop;
	}

4151
	if (!__is_sframe(chan, control)) {
4152
		if (len < 0) {
4153
			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
4154 4155 4156
			goto drop;
		}

4157
		l2cap_data_channel_iframe(chan, control, skb);
4158 4159 4160
	} else {
		if (len != 0) {
			BT_ERR("%d", len);
4161
			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
4162 4163 4164
			goto drop;
		}

4165
		l2cap_data_channel_sframe(chan, control, skb);
4166 4167 4168 4169 4170 4171 4172 4173 4174
	}

	return 0;

drop:
	kfree_skb(skb);
	return 0;
}

L
Linus Torvalds 已提交
4175 4176
static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
{
4177
	struct l2cap_chan *chan;
4178
	struct sock *sk = NULL;
4179
	u32 control;
4180
	u16 tx_seq;
4181
	int len;
L
Linus Torvalds 已提交
4182

4183
	chan = l2cap_get_chan_by_scid(conn, cid);
4184
	if (!chan) {
L
Linus Torvalds 已提交
4185 4186 4187 4188
		BT_DBG("unknown cid 0x%4.4x", cid);
		goto drop;
	}

4189
	sk = chan->sk;
4190

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

4193
	if (chan->state != BT_CONNECTED)
L
Linus Torvalds 已提交
4194 4195
		goto drop;

4196
	switch (chan->mode) {
4197 4198 4199 4200 4201
	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 已提交
4202

4203
		if (chan->imtu < skb->len)
4204
			goto drop;
L
Linus Torvalds 已提交
4205

4206
		if (!chan->ops->recv(chan->data, skb))
4207 4208 4209 4210
			goto done;
		break;

	case L2CAP_MODE_ERTM:
4211 4212
		if (!sock_owned_by_user(sk)) {
			l2cap_ertm_data_rcv(sk, skb);
4213
		} else {
4214
			if (sk_add_backlog(sk, skb))
4215 4216
				goto drop;
		}
4217

4218
		goto done;
4219

4220
	case L2CAP_MODE_STREAMING:
4221 4222
		control = __get_control(chan, skb->data);
		skb_pull(skb, __ctrl_size(chan));
4223 4224
		len = skb->len;

4225
		if (l2cap_check_fcs(chan, skb))
4226 4227
			goto drop;

4228
		if (__is_sar_start(chan, control))
4229
			len -= L2CAP_SDULEN_SIZE;
4230

4231
		if (chan->fcs == L2CAP_FCS_CRC16)
4232
			len -= L2CAP_FCS_SIZE;
4233

4234
		if (len > chan->mps || len < 0 || __is_sframe(chan, control))
4235 4236
			goto drop;

4237
		tx_seq = __get_txseq(chan, control);
4238

4239 4240 4241 4242 4243 4244
		if (chan->expected_tx_seq != tx_seq) {
			/* Frame(s) missing - must discard partial SDU */
			kfree_skb(chan->sdu);
			chan->sdu = NULL;
			chan->sdu_last_frag = NULL;
			chan->sdu_len = 0;
4245

4246 4247 4248
			/* TODO: Notify userland of missing data */
		}

4249
		chan->expected_tx_seq = __next_seq(chan, tx_seq);
4250 4251 4252

		if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE)
			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
4253 4254 4255

		goto done;

4256
	default:
4257
		BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
4258 4259
		break;
	}
L
Linus Torvalds 已提交
4260 4261 4262 4263 4264

drop:
	kfree_skb(skb);

done:
4265 4266 4267
	if (sk)
		bh_unlock_sock(sk);

L
Linus Torvalds 已提交
4268 4269 4270
	return 0;
}

4271
static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
L
Linus Torvalds 已提交
4272
{
4273
	struct sock *sk = NULL;
4274
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
4275

4276 4277
	chan = l2cap_global_chan_by_psm(0, psm, conn->src);
	if (!chan)
L
Linus Torvalds 已提交
4278 4279
		goto drop;

4280 4281
	sk = chan->sk;

4282 4283
	bh_lock_sock(sk);

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

4286
	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
L
Linus Torvalds 已提交
4287 4288
		goto drop;

4289
	if (chan->imtu < skb->len)
L
Linus Torvalds 已提交
4290 4291
		goto drop;

4292
	if (!chan->ops->recv(chan->data, skb))
L
Linus Torvalds 已提交
4293 4294 4295 4296 4297 4298
		goto done;

drop:
	kfree_skb(skb);

done:
4299 4300
	if (sk)
		bh_unlock_sock(sk);
L
Linus Torvalds 已提交
4301 4302 4303
	return 0;
}

4304 4305
static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb)
{
4306
	struct sock *sk = NULL;
4307
	struct l2cap_chan *chan;
4308

4309 4310
	chan = l2cap_global_chan_by_scid(0, cid, conn->src);
	if (!chan)
4311 4312
		goto drop;

4313 4314
	sk = chan->sk;

4315 4316 4317 4318
	bh_lock_sock(sk);

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

4319
	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
4320 4321
		goto drop;

4322
	if (chan->imtu < skb->len)
4323 4324
		goto drop;

4325
	if (!chan->ops->recv(chan->data, skb))
4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336
		goto done;

drop:
	kfree_skb(skb);

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

L
Linus Torvalds 已提交
4337 4338 4339
static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
{
	struct l2cap_hdr *lh = (void *) skb->data;
4340 4341
	u16 cid, len;
	__le16 psm;
L
Linus Torvalds 已提交
4342 4343 4344 4345 4346

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

4347 4348 4349 4350 4351
	if (len != skb->len) {
		kfree_skb(skb);
		return;
	}

L
Linus Torvalds 已提交
4352 4353 4354
	BT_DBG("len %d, cid 0x%4.4x", len, cid);

	switch (cid) {
4355
	case L2CAP_CID_LE_SIGNALING:
4356
	case L2CAP_CID_SIGNALING:
L
Linus Torvalds 已提交
4357 4358 4359
		l2cap_sig_channel(conn, skb);
		break;

4360
	case L2CAP_CID_CONN_LESS:
4361
		psm = get_unaligned_le16(skb->data);
L
Linus Torvalds 已提交
4362 4363 4364 4365
		skb_pull(skb, 2);
		l2cap_conless_channel(conn, psm, skb);
		break;

4366 4367 4368 4369
	case L2CAP_CID_LE_DATA:
		l2cap_att_channel(conn, cid, skb);
		break;

4370 4371 4372 4373 4374
	case L2CAP_CID_SMP:
		if (smp_sig_channel(conn, skb))
			l2cap_conn_del(conn->hcon, EACCES);
		break;

L
Linus Torvalds 已提交
4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385
	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;
4386
	struct l2cap_chan *c;
L
Linus Torvalds 已提交
4387 4388

	if (type != ACL_LINK)
4389
		return -EINVAL;
L
Linus Torvalds 已提交
4390 4391 4392 4393

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

	/* Find listening sockets and check their link_mode */
4394 4395 4396
	read_lock(&chan_list_lock);
	list_for_each_entry(c, &chan_list, global_l) {
		struct sock *sk = c->sk;
4397

4398
		if (c->state != BT_LISTEN)
L
Linus Torvalds 已提交
4399 4400 4401
			continue;

		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
4402
			lm1 |= HCI_LM_ACCEPT;
4403
			if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
4404
				lm1 |= HCI_LM_MASTER;
L
Linus Torvalds 已提交
4405
			exact++;
4406 4407
		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
			lm2 |= HCI_LM_ACCEPT;
4408
			if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
4409 4410
				lm2 |= HCI_LM_MASTER;
		}
L
Linus Torvalds 已提交
4411
	}
4412
	read_unlock(&chan_list_lock);
L
Linus Torvalds 已提交
4413 4414 4415 4416 4417 4418

	return exact ? lm1 : lm2;
}

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

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

4423
	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
4424
		return -EINVAL;
L
Linus Torvalds 已提交
4425 4426 4427 4428 4429

	if (!status) {
		conn = l2cap_conn_add(hcon, status);
		if (conn)
			l2cap_conn_ready(conn);
4430
	} else
4431
		l2cap_conn_del(hcon, bt_to_errno(status));
L
Linus Torvalds 已提交
4432 4433 4434 4435

	return 0;
}

4436 4437 4438 4439 4440 4441
static int l2cap_disconn_ind(struct hci_conn *hcon)
{
	struct l2cap_conn *conn = hcon->l2cap_data;

	BT_DBG("hcon %p", hcon);

4442
	if ((hcon->type != ACL_LINK && hcon->type != LE_LINK) || !conn)
4443
		return HCI_ERROR_REMOTE_USER_TERM;
4444 4445 4446 4447 4448

	return conn->disc_reason;
}

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

4452
	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
4453
		return -EINVAL;
L
Linus Torvalds 已提交
4454

4455
	l2cap_conn_del(hcon, bt_to_errno(reason));
4456

L
Linus Torvalds 已提交
4457 4458 4459
	return 0;
}

4460
static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
4461
{
4462
	if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
4463 4464
		return;

4465
	if (encrypt == 0x00) {
4466
		if (chan->sec_level == BT_SECURITY_MEDIUM) {
4467
			__clear_chan_timer(chan);
4468
			__set_chan_timer(chan, L2CAP_ENC_TIMEOUT);
4469
		} else if (chan->sec_level == BT_SECURITY_HIGH)
4470
			l2cap_chan_close(chan, ECONNREFUSED);
4471
	} else {
4472
		if (chan->sec_level == BT_SECURITY_MEDIUM)
4473
			__clear_chan_timer(chan);
4474 4475 4476
	}
}

4477
static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
L
Linus Torvalds 已提交
4478
{
4479
	struct l2cap_conn *conn = hcon->l2cap_data;
4480
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
4481

4482
	if (!conn)
L
Linus Torvalds 已提交
4483
		return 0;
4484

L
Linus Torvalds 已提交
4485 4486
	BT_DBG("conn %p", conn);

4487 4488 4489 4490 4491
	if (hcon->type == LE_LINK) {
		smp_distribute_keys(conn, 0);
		del_timer(&conn->security_timer);
	}

4492
	read_lock(&conn->chan_lock);
L
Linus Torvalds 已提交
4493

4494
	list_for_each_entry(chan, &conn->chan_l, list) {
4495
		struct sock *sk = chan->sk;
4496

L
Linus Torvalds 已提交
4497 4498
		bh_lock_sock(sk);

4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510
		BT_DBG("chan->scid %d", chan->scid);

		if (chan->scid == L2CAP_CID_LE_DATA) {
			if (!status && encrypt) {
				chan->sec_level = hcon->sec_level;
				l2cap_chan_ready(sk);
			}

			bh_unlock_sock(sk);
			continue;
		}

4511
		if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) {
4512 4513 4514 4515
			bh_unlock_sock(sk);
			continue;
		}

4516 4517
		if (!status && (chan->state == BT_CONNECTED ||
						chan->state == BT_CONFIG)) {
4518
			l2cap_check_encryption(chan, encrypt);
4519 4520 4521 4522
			bh_unlock_sock(sk);
			continue;
		}

4523
		if (chan->state == BT_CONNECT) {
4524 4525
			if (!status) {
				struct l2cap_conn_req req;
4526 4527
				req.scid = cpu_to_le16(chan->scid);
				req.psm  = chan->psm;
L
Linus Torvalds 已提交
4528

4529
				chan->ident = l2cap_get_ident(conn);
4530
				set_bit(CONF_CONNECT_PEND, &chan->conf_state);
L
Linus Torvalds 已提交
4531

4532
				l2cap_send_cmd(conn, chan->ident,
4533 4534
					L2CAP_CONN_REQ, sizeof(req), &req);
			} else {
4535
				__clear_chan_timer(chan);
4536
				__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
4537
			}
4538
		} else if (chan->state == BT_CONNECT2) {
4539
			struct l2cap_conn_rsp rsp;
4540
			__u16 res, stat;
L
Linus Torvalds 已提交
4541

4542
			if (!status) {
4543 4544 4545 4546
				if (bt_sk(sk)->defer_setup) {
					struct sock *parent = bt_sk(sk)->parent;
					res = L2CAP_CR_PEND;
					stat = L2CAP_CS_AUTHOR_PEND;
4547 4548
					if (parent)
						parent->sk_data_ready(parent, 0);
4549
				} else {
4550
					l2cap_state_change(chan, BT_CONFIG);
4551 4552 4553
					res = L2CAP_CR_SUCCESS;
					stat = L2CAP_CS_NO_INFO;
				}
4554
			} else {
4555
				l2cap_state_change(chan, BT_DISCONN);
4556
				__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
4557 4558
				res = L2CAP_CR_SEC_BLOCK;
				stat = L2CAP_CS_NO_INFO;
4559 4560
			}

4561 4562
			rsp.scid   = cpu_to_le16(chan->dcid);
			rsp.dcid   = cpu_to_le16(chan->scid);
4563 4564
			rsp.result = cpu_to_le16(res);
			rsp.status = cpu_to_le16(stat);
4565 4566
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
							sizeof(rsp), &rsp);
4567
		}
L
Linus Torvalds 已提交
4568 4569 4570 4571

		bh_unlock_sock(sk);
	}

4572
	read_unlock(&conn->chan_lock);
4573

L
Linus Torvalds 已提交
4574 4575 4576 4577 4578 4579 4580
	return 0;
}

static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
{
	struct l2cap_conn *conn = hcon->l2cap_data;

4581 4582 4583 4584
	if (!conn)
		conn = l2cap_conn_add(hcon, 0);

	if (!conn)
L
Linus Torvalds 已提交
4585 4586 4587 4588
		goto drop;

	BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);

4589
	if (!(flags & ACL_CONT)) {
L
Linus Torvalds 已提交
4590
		struct l2cap_hdr *hdr;
4591
		struct l2cap_chan *chan;
4592
		u16 cid;
L
Linus Torvalds 已提交
4593 4594 4595 4596 4597 4598 4599 4600 4601 4602
		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);
		}

4603 4604
		/* Start fragment always begin with Basic L2CAP header */
		if (skb->len < L2CAP_HDR_SIZE) {
L
Linus Torvalds 已提交
4605 4606 4607 4608 4609 4610 4611
			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;
4612
		cid = __le16_to_cpu(hdr->cid);
L
Linus Torvalds 已提交
4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628

		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;
		}

4629
		chan = l2cap_get_chan_by_scid(conn, cid);
4630

4631 4632
		if (chan && chan->sk) {
			struct sock *sk = chan->sk;
4633

4634
			if (chan->imtu < len - L2CAP_HDR_SIZE) {
4635 4636
				BT_ERR("Frame exceeding recv MTU (len %d, "
							"MTU %d)", len,
4637
							chan->imtu);
4638 4639 4640 4641
				bh_unlock_sock(sk);
				l2cap_conn_unreliable(conn, ECOMM);
				goto drop;
			}
4642
			bh_unlock_sock(sk);
4643
		}
4644

L
Linus Torvalds 已提交
4645
		/* Allocate skb for the complete frame (with header) */
4646 4647
		conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
		if (!conn->rx_skb)
L
Linus Torvalds 已提交
4648 4649
			goto drop;

4650
		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
4651
								skb->len);
L
Linus Torvalds 已提交
4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671
		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;
		}

4672
		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
4673
								skb->len);
L
Linus Torvalds 已提交
4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687
		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;
}

4688
static int l2cap_debugfs_show(struct seq_file *f, void *p)
L
Linus Torvalds 已提交
4689
{
4690
	struct l2cap_chan *c;
L
Linus Torvalds 已提交
4691

4692
	read_lock_bh(&chan_list_lock);
L
Linus Torvalds 已提交
4693

4694 4695
	list_for_each_entry(c, &chan_list, global_l) {
		struct sock *sk = c->sk;
4696

4697
		seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
4698 4699
					batostr(&bt_sk(sk)->src),
					batostr(&bt_sk(sk)->dst),
4700
					c->state, __le16_to_cpu(c->psm),
4701 4702
					c->scid, c->dcid, c->imtu, c->omtu,
					c->sec_level, c->mode);
4703
}
L
Linus Torvalds 已提交
4704

4705
	read_unlock_bh(&chan_list_lock);
L
Linus Torvalds 已提交
4706

4707
	return 0;
L
Linus Torvalds 已提交
4708 4709
}

4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722
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 已提交
4723 4724 4725 4726 4727 4728 4729

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,
4730
	.disconn_cfm	= l2cap_disconn_cfm,
4731
	.security_cfm	= l2cap_security_cfm,
L
Linus Torvalds 已提交
4732 4733 4734
	.recv_acldata	= l2cap_recv_acldata
};

4735
int __init l2cap_init(void)
L
Linus Torvalds 已提交
4736 4737
{
	int err;
4738

4739
	err = l2cap_init_sockets();
L
Linus Torvalds 已提交
4740 4741 4742 4743 4744 4745 4746 4747 4748 4749
	if (err < 0)
		return err;

	err = hci_register_proto(&l2cap_hci_proto);
	if (err < 0) {
		BT_ERR("L2CAP protocol registration failed");
		bt_sock_unregister(BTPROTO_L2CAP);
		goto error;
	}

4750 4751 4752 4753 4754 4755
	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 已提交
4756 4757 4758 4759

	return 0;

error:
4760
	l2cap_cleanup_sockets();
L
Linus Torvalds 已提交
4761 4762 4763
	return err;
}

4764
void l2cap_exit(void)
L
Linus Torvalds 已提交
4765
{
4766
	debugfs_remove(l2cap_debugfs);
L
Linus Torvalds 已提交
4767 4768 4769 4770

	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
		BT_ERR("L2CAP protocol unregistration failed");

4771
	l2cap_cleanup_sockets();
L
Linus Torvalds 已提交
4772 4773
}

4774 4775
module_param(disable_ertm, bool, 0644);
MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");