l2cap_core.c 94.0 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 57
#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>

58
int disable_ertm;
59

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

63 64
static struct workqueue_struct *_busy_wq;

65
struct bt_sock_list l2cap_sk_list = {
66
	.lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock)
L
Linus Torvalds 已提交
67 68
};

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

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

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

77
/* ---- L2CAP channels ---- */
78
static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid)
79
{
80
	struct l2cap_chan *c;
81 82

	list_for_each_entry(c, &conn->chan_l, list) {
83
		if (c->dcid == cid)
84
			return c;
85
	}
86 87
	return NULL;

88 89
}

90
static struct l2cap_chan *__l2cap_get_chan_by_scid(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->scid == cid)
96
			return c;
97
	}
98
	return NULL;
99 100 101 102
}

/* Find channel with given SCID.
 * Returns locked socket */
103
static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
104
{
105
	struct l2cap_chan *c;
106 107 108

	read_lock(&conn->chan_lock);
	c = __l2cap_get_chan_by_scid(conn, cid);
109 110
	if (c)
		bh_lock_sock(c->sk);
111
	read_unlock(&conn->chan_lock);
112
	return c;
113 114
}

115
static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
116
{
117
	struct l2cap_chan *c;
118 119

	list_for_each_entry(c, &conn->chan_l, list) {
120
		if (c->ident == ident)
121
			return c;
122
	}
123
	return NULL;
124 125
}

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

	read_lock(&conn->chan_lock);
	c = __l2cap_get_chan_by_ident(conn, ident);
132 133
	if (c)
		bh_lock_sock(c->sk);
134
	read_unlock(&conn->chan_lock);
135
	return c;
136 137
}

138
static struct sock *__l2cap_get_sock_by_addr(__le16 psm, bdaddr_t *src)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
{
	struct sock *sk;
	struct hlist_node *node;
	sk_for_each(sk, node, &l2cap_sk_list.head) {
		struct l2cap_chan *chan = l2cap_pi(sk)->chan;

		if (chan->sport == psm && !bacmp(&bt_sk(sk)->src, src))
			goto found;
	}

	sk = NULL;
found:
	return sk;
}

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

158 159
	write_lock_bh(&l2cap_sk_list.lock);

160 161 162
	if (psm && __l2cap_get_sock_by_addr(psm, src)) {
		err = -EADDRINUSE;
		goto done;
163 164
	}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	if (psm) {
		chan->psm = psm;
		chan->sport = psm;
		err = 0;
	} else {
		u16 p;

		err = -EINVAL;
		for (p = 0x1001; p < 0x1100; p += 2)
			if (!__l2cap_get_sock_by_addr(cpu_to_le16(p), src)) {
				chan->psm   = cpu_to_le16(p);
				chan->sport = cpu_to_le16(p);
				err = 0;
				break;
			}
	}
181

182
done:
183
	write_unlock_bh(&l2cap_sk_list.lock);
184
	return err;
185 186 187 188 189 190 191 192 193 194 195 196 197
}

int l2cap_add_scid(struct l2cap_chan *chan,  __u16 scid)
{
	write_lock_bh(&l2cap_sk_list.lock);

	chan->scid = scid;

	write_unlock_bh(&l2cap_sk_list.lock);

	return 0;
}

198
static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
199
{
200
	u16 cid = L2CAP_CID_DYN_START;
201

202
	for (; cid < L2CAP_CID_DYN_END; cid++) {
203
		if (!__l2cap_get_chan_by_scid(conn, cid))
204 205 206 207 208 209
			return cid;
	}

	return 0;
}

210
struct l2cap_chan *l2cap_chan_alloc(struct sock *sk)
211 212 213 214 215 216 217 218 219 220 221 222
{
	struct l2cap_chan *chan;

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

	chan->sk = sk;

	return chan;
}

223 224 225 226 227
void l2cap_chan_free(struct l2cap_chan *chan)
{
	kfree(chan);
}

228
static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
229
{
230
	struct sock *sk = chan->sk;
231

232
	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
233
			chan->psm, chan->dcid);
234

235 236
	conn->disc_reason = 0x13;

237
	chan->conn = conn;
238

239
	if (sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM) {
240 241
		if (conn->hcon->type == LE_LINK) {
			/* LE connection */
242
			chan->omtu = L2CAP_LE_DEFAULT_MTU;
243 244
			chan->scid = L2CAP_CID_LE_DATA;
			chan->dcid = L2CAP_CID_LE_DATA;
245 246
		} else {
			/* Alloc CID for connection-oriented socket */
247
			chan->scid = l2cap_alloc_cid(conn);
248
			chan->omtu = L2CAP_DEFAULT_MTU;
249
		}
250 251
	} else if (sk->sk_type == SOCK_DGRAM) {
		/* Connectionless socket */
252 253
		chan->scid = L2CAP_CID_CONN_LESS;
		chan->dcid = L2CAP_CID_CONN_LESS;
254
		chan->omtu = L2CAP_DEFAULT_MTU;
255 256
	} else {
		/* Raw socket can send/recv signalling messages only */
257 258
		chan->scid = L2CAP_CID_SIGNALING;
		chan->dcid = L2CAP_CID_SIGNALING;
259
		chan->omtu = L2CAP_DEFAULT_MTU;
260 261
	}

262 263 264
	sock_hold(sk);

	list_add(&chan->list, &conn->chan_l);
265 266
}

267
/* Delete channel.
268
 * Must be called on the locked socket. */
269
void l2cap_chan_del(struct l2cap_chan *chan, int err)
270
{
271
	struct sock *sk = chan->sk;
272
	struct l2cap_conn *conn = chan->conn;
273 274 275 276
	struct sock *parent = bt_sk(sk)->parent;

	l2cap_sock_clear_timer(sk);

277
	BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
278

279
	if (conn) {
280 281 282 283 284 285
		/* Delete from channel list */
		write_lock_bh(&conn->chan_lock);
		list_del(&chan->list);
		write_unlock_bh(&conn->chan_lock);
		__sock_put(sk);

286
		chan->conn = NULL;
287 288 289
		hci_conn_put(conn->hcon);
	}

290
	sk->sk_state = BT_CLOSED;
291 292 293 294 295 296 297 298 299 300
	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);
301

302 303
	if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE &&
			chan->conf_state & L2CAP_CONF_INPUT_DONE))
304
		return;
305

306
	skb_queue_purge(&chan->tx_q);
307

308
	if (chan->mode == L2CAP_MODE_ERTM) {
309 310
		struct srej_list *l, *tmp;

311 312 313
		del_timer(&chan->retrans_timer);
		del_timer(&chan->monitor_timer);
		del_timer(&chan->ack_timer);
314

315 316
		skb_queue_purge(&chan->srej_q);
		skb_queue_purge(&chan->busy_q);
317

318
		list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
319 320 321 322
			list_del(&l->list);
			kfree(l);
		}
	}
323 324
}

325
static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
326
{
327 328
	struct sock *sk = chan->sk;

329
	if (sk->sk_type == SOCK_RAW) {
330
		switch (chan->sec_level) {
331 332 333 334 335 336 337
		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;
		}
338
	} else if (chan->psm == cpu_to_le16(0x0001)) {
339 340
		if (chan->sec_level == BT_SECURITY_LOW)
			chan->sec_level = BT_SECURITY_SDP;
341

342
		if (chan->sec_level == BT_SECURITY_HIGH)
343
			return HCI_AT_NO_BONDING_MITM;
344
		else
345
			return HCI_AT_NO_BONDING;
346
	} else {
347
		switch (chan->sec_level) {
348
		case BT_SECURITY_HIGH:
349
			return HCI_AT_GENERAL_BONDING_MITM;
350
		case BT_SECURITY_MEDIUM:
351
			return HCI_AT_GENERAL_BONDING;
352
		default:
353
			return HCI_AT_NO_BONDING;
354
		}
355
	}
356 357 358
}

/* Service level security */
359
static inline int l2cap_check_security(struct l2cap_chan *chan)
360
{
361
	struct l2cap_conn *conn = chan->conn;
362 363
	__u8 auth_type;

364
	auth_type = l2cap_get_auth_type(chan);
365

366
	return hci_conn_security(conn->hcon, chan->sec_level, auth_type);
367 368
}

369
u8 l2cap_get_ident(struct l2cap_conn *conn)
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
{
	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;
}

391
void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
392 393
{
	struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
394
	u8 flags;
395 396 397 398

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

	if (!skb)
399
		return;
400

401 402 403 404 405 406
	if (lmp_no_flush_capable(conn->hcon->hdev))
		flags = ACL_START_NO_FLUSH;
	else
		flags = ACL_START;

	hci_send_acl(conn->hcon, skb, flags);
407 408
}

409
static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control)
410 411 412
{
	struct sk_buff *skb;
	struct l2cap_hdr *lh;
413
	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);
414
	struct l2cap_conn *conn = chan->conn;
415
	struct sock *sk = (struct sock *)pi;
416
	int count, hlen = L2CAP_HDR_SIZE + 2;
417
	u8 flags;
418

419 420 421
	if (sk->sk_state != BT_CONNECTED)
		return;

422
	if (chan->fcs == L2CAP_FCS_CRC16)
423
		hlen += 2;
424

425
	BT_DBG("chan %p, control 0x%2.2x", chan, control);
426

427
	count = min_t(unsigned int, conn->mtu, hlen);
428 429
	control |= L2CAP_CTRL_FRAME_TYPE;

430
	if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
431
		control |= L2CAP_CTRL_FINAL;
432
		chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
433 434
	}

435
	if (chan->conn_state & L2CAP_CONN_SEND_PBIT) {
436
		control |= L2CAP_CTRL_POLL;
437
		chan->conn_state &= ~L2CAP_CONN_SEND_PBIT;
438 439
	}

440 441
	skb = bt_skb_alloc(count, GFP_ATOMIC);
	if (!skb)
442
		return;
443 444

	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
445
	lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
446
	lh->cid = cpu_to_le16(chan->dcid);
447 448
	put_unaligned_le16(control, skb_put(skb, 2));

449
	if (chan->fcs == L2CAP_FCS_CRC16) {
450 451 452 453
		u16 fcs = crc16(0, (u8 *)lh, count - 2);
		put_unaligned_le16(fcs, skb_put(skb, 2));
	}

454 455 456 457 458
	if (lmp_no_flush_capable(conn->hcon->hdev))
		flags = ACL_START_NO_FLUSH;
	else
		flags = ACL_START;

459
	hci_send_acl(chan->conn->hcon, skb, flags);
460 461
}

462
static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control)
463
{
464
	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
465
		control |= L2CAP_SUPER_RCV_NOT_READY;
466
		chan->conn_state |= L2CAP_CONN_RNR_SENT;
467
	} else
468 469
		control |= L2CAP_SUPER_RCV_READY;

470
	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
471

472
	l2cap_send_sframe(chan, control);
473 474
}

475
static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
476
{
477
	return !(chan->conf_state & L2CAP_CONF_CONNECT_PEND);
478 479
}

480
static void l2cap_do_start(struct l2cap_chan *chan)
481
{
482
	struct l2cap_conn *conn = chan->conn;
483 484

	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
485 486 487
		if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
			return;

488 489
		if (l2cap_check_security(chan) &&
				__l2cap_no_conn_pending(chan)) {
490
			struct l2cap_conn_req req;
491 492
			req.scid = cpu_to_le16(chan->scid);
			req.psm  = chan->psm;
493

494
			chan->ident = l2cap_get_ident(conn);
495
			chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
496

497 498
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
							sizeof(req), &req);
499
		}
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
	} 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);
	}
}

515 516 517
static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
{
	u32 local_feat_mask = l2cap_feat_mask;
518
	if (!disable_ertm)
519 520 521 522 523 524 525 526 527 528 529 530
		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;
	}
}

531
void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err)
532
{
533
	struct sock *sk;
534 535
	struct l2cap_disconn_req req;

536 537 538
	if (!conn)
		return;

539 540
	sk = chan->sk;

541
	if (chan->mode == L2CAP_MODE_ERTM) {
542 543 544
		del_timer(&chan->retrans_timer);
		del_timer(&chan->monitor_timer);
		del_timer(&chan->ack_timer);
545 546
	}

547 548
	req.dcid = cpu_to_le16(chan->dcid);
	req.scid = cpu_to_le16(chan->scid);
549 550
	l2cap_send_cmd(conn, l2cap_get_ident(conn),
			L2CAP_DISCONN_REQ, sizeof(req), &req);
551 552

	sk->sk_state = BT_DISCONN;
553
	sk->sk_err = err;
554 555
}

L
Linus Torvalds 已提交
556
/* ---- L2CAP connections ---- */
557 558
static void l2cap_conn_start(struct l2cap_conn *conn)
{
559
	struct l2cap_chan *chan, *tmp;
560 561 562

	BT_DBG("conn %p", conn);

563
	read_lock(&conn->chan_lock);
564

565
	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
566
		struct sock *sk = chan->sk;
567

568 569
		bh_lock_sock(sk);

570 571
		if (sk->sk_type != SOCK_SEQPACKET &&
				sk->sk_type != SOCK_STREAM) {
572 573 574 575 576
			bh_unlock_sock(sk);
			continue;
		}

		if (sk->sk_state == BT_CONNECT) {
577
			struct l2cap_conn_req req;
578

579
			if (!l2cap_check_security(chan) ||
580
					!__l2cap_no_conn_pending(chan)) {
581 582 583
				bh_unlock_sock(sk);
				continue;
			}
584

585
			if (!l2cap_mode_supported(chan->mode,
586
					conn->feat_mask)
587
					&& chan->conf_state &
588
					L2CAP_CONF_STATE2_DEVICE) {
589 590 591 592 593
				/* __l2cap_sock_close() calls list_del(chan)
				 * so release the lock */
				read_unlock_bh(&conn->chan_lock);
				 __l2cap_sock_close(sk, ECONNRESET);
				read_lock_bh(&conn->chan_lock);
594 595
				bh_unlock_sock(sk);
				continue;
596
			}
597

598 599
			req.scid = cpu_to_le16(chan->scid);
			req.psm  = chan->psm;
600

601
			chan->ident = l2cap_get_ident(conn);
602
			chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
603

604 605
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
							sizeof(req), &req);
606

607 608
		} else if (sk->sk_state == BT_CONNECT2) {
			struct l2cap_conn_rsp rsp;
609
			char buf[128];
610 611
			rsp.scid = cpu_to_le16(chan->dcid);
			rsp.dcid = cpu_to_le16(chan->scid);
612

613
			if (l2cap_check_security(chan)) {
614 615 616 617 618 619 620 621 622 623 624
				if (bt_sk(sk)->defer_setup) {
					struct sock *parent = bt_sk(sk)->parent;
					rsp.result = cpu_to_le16(L2CAP_CR_PEND);
					rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
					parent->sk_data_ready(parent, 0);

				} else {
					sk->sk_state = BT_CONFIG;
					rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
					rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
				}
625 626 627 628 629
			} else {
				rsp.result = cpu_to_le16(L2CAP_CR_PEND);
				rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
			}

630 631
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
							sizeof(rsp), &rsp);
632

633
			if (chan->conf_state & L2CAP_CONF_REQ_SENT ||
634 635 636 637 638
					rsp.result != L2CAP_CR_SUCCESS) {
				bh_unlock_sock(sk);
				continue;
			}

639
			chan->conf_state |= L2CAP_CONF_REQ_SENT;
640
			l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
641 642
						l2cap_build_conf_req(chan, buf), buf);
			chan->num_conf_req++;
643 644 645 646 647
		}

		bh_unlock_sock(sk);
	}

648
	read_unlock(&conn->chan_lock);
649 650
}

651 652 653 654 655
/* Find socket with cid and source bdaddr.
 * Returns closest match, locked.
 */
static struct sock *l2cap_get_sock_by_scid(int state, __le16 cid, bdaddr_t *src)
{
656
	struct sock *sk = NULL, *sk1 = NULL;
657 658 659 660 661
	struct hlist_node *node;

	read_lock(&l2cap_sk_list.lock);

	sk_for_each(sk, node, &l2cap_sk_list.head) {
662 663
		struct l2cap_chan *chan = l2cap_pi(sk)->chan;

664 665 666
		if (state && sk->sk_state != state)
			continue;

667
		if (chan->scid == cid) {
668 669 670 671 672 673 674 675 676
			/* Exact match. */
			if (!bacmp(&bt_sk(sk)->src, src))
				break;

			/* Closest match */
			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
				sk1 = sk;
		}
	}
677

678 679
	read_unlock(&l2cap_sk_list.lock);

680
	return node ? sk : sk1;
681 682 683 684
}

static void l2cap_le_conn_ready(struct l2cap_conn *conn)
{
685
	struct sock *parent, *sk;
686
	struct l2cap_chan *chan;
687 688 689 690 691 692 693 694 695

	BT_DBG("");

	/* Check if we have socket listening on cid */
	parent = l2cap_get_sock_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA,
							conn->src);
	if (!parent)
		return;

696 697
	bh_lock_sock(parent);

698 699 700 701 702 703 704 705 706 707
	/* Check for backlog size */
	if (sk_acceptq_is_full(parent)) {
		BT_DBG("backlog full %d", parent->sk_ack_backlog);
		goto clean;
	}

	sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC);
	if (!sk)
		goto clean;

708 709 710 711 712 713
	chan = l2cap_chan_alloc(sk);
	if (!chan) {
		l2cap_sock_kill(sk);
		goto clean;
	}

714 715
	l2cap_pi(sk)->chan = chan;

716
	write_lock_bh(&conn->chan_lock);
717 718 719 720

	hci_conn_hold(conn->hcon);

	l2cap_sock_init(sk, parent);
721

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

725 726
	bt_accept_enqueue(parent, sk);

727 728
	__l2cap_chan_add(conn, chan);

729 730 731 732 733
	l2cap_sock_set_timer(sk, sk->sk_sndtimeo);

	sk->sk_state = BT_CONNECTED;
	parent->sk_data_ready(parent, 0);

734
	write_unlock_bh(&conn->chan_lock);
735 736 737 738 739

clean:
	bh_unlock_sock(parent);
}

740 741
static void l2cap_conn_ready(struct l2cap_conn *conn)
{
742
	struct l2cap_chan *chan;
743

744
	BT_DBG("conn %p", conn);
745

746 747 748
	if (!conn->hcon->out && conn->hcon->type == LE_LINK)
		l2cap_le_conn_ready(conn);

749
	read_lock(&conn->chan_lock);
750

751
	list_for_each_entry(chan, &conn->chan_l, list) {
752
		struct sock *sk = chan->sk;
753

754
		bh_lock_sock(sk);
755

756 757 758 759 760 761
		if (conn->hcon->type == LE_LINK) {
			l2cap_sock_clear_timer(sk);
			sk->sk_state = BT_CONNECTED;
			sk->sk_state_change(sk);
		}

762 763
		if (sk->sk_type != SOCK_SEQPACKET &&
				sk->sk_type != SOCK_STREAM) {
764 765 766 767
			l2cap_sock_clear_timer(sk);
			sk->sk_state = BT_CONNECTED;
			sk->sk_state_change(sk);
		} else if (sk->sk_state == BT_CONNECT)
768
			l2cap_do_start(chan);
769

770
		bh_unlock_sock(sk);
771
	}
772

773
	read_unlock(&conn->chan_lock);
774 775 776 777 778
}

/* Notify sockets that we cannot guaranty reliability anymore */
static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
{
779
	struct l2cap_chan *chan;
780 781 782

	BT_DBG("conn %p", conn);

783
	read_lock(&conn->chan_lock);
784

785
	list_for_each_entry(chan, &conn->chan_l, list) {
786
		struct sock *sk = chan->sk;
787

788
		if (chan->force_reliable)
789 790 791
			sk->sk_err = err;
	}

792
	read_unlock(&conn->chan_lock);
793 794 795 796 797 798
}

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

799
	conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
800
	conn->info_ident = 0;
801

802 803 804
	l2cap_conn_start(conn);
}

L
Linus Torvalds 已提交
805 806
static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
{
807
	struct l2cap_conn *conn = hcon->l2cap_data;
L
Linus Torvalds 已提交
808

809
	if (conn || status)
L
Linus Torvalds 已提交
810 811
		return conn;

812 813
	conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
	if (!conn)
L
Linus Torvalds 已提交
814 815 816 817 818
		return NULL;

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

819 820
	BT_DBG("hcon %p conn %p", hcon, conn);

821 822 823 824 825
	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 已提交
826 827 828
	conn->src = &hcon->hdev->bdaddr;
	conn->dst = &hcon->dst;

829 830
	conn->feat_mask = 0;

L
Linus Torvalds 已提交
831
	spin_lock_init(&conn->lock);
832 833 834
	rwlock_init(&conn->chan_lock);

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

836 837
	if (hcon->type != LE_LINK)
		setup_timer(&conn->info_timer, l2cap_info_timeout,
D
Dave Young 已提交
838 839
						(unsigned long) conn);

840 841
	conn->disc_reason = 0x13;

L
Linus Torvalds 已提交
842 843 844
	return conn;
}

845
static void l2cap_conn_del(struct hci_conn *hcon, int err)
L
Linus Torvalds 已提交
846
{
847
	struct l2cap_conn *conn = hcon->l2cap_data;
848
	struct l2cap_chan *chan, *l;
L
Linus Torvalds 已提交
849 850
	struct sock *sk;

851 852
	if (!conn)
		return;
L
Linus Torvalds 已提交
853 854 855

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

856
	kfree_skb(conn->rx_skb);
L
Linus Torvalds 已提交
857 858

	/* Kill channels */
859
	list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
860
		sk = chan->sk;
L
Linus Torvalds 已提交
861
		bh_lock_sock(sk);
862
		l2cap_chan_del(chan, err);
L
Linus Torvalds 已提交
863 864 865 866
		bh_unlock_sock(sk);
		l2cap_sock_kill(sk);
	}

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

L
Linus Torvalds 已提交
870 871 872 873
	hcon->l2cap_data = NULL;
	kfree(conn);
}

874
static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
L
Linus Torvalds 已提交
875
{
876
	write_lock_bh(&conn->chan_lock);
877
	__l2cap_chan_add(conn, chan);
878
	write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
879 880 881 882 883 884 885
}

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

/* Find socket with psm and source bdaddr.
 * Returns closest match.
 */
886
static struct sock *l2cap_get_sock_by_psm(int state, __le16 psm, bdaddr_t *src)
L
Linus Torvalds 已提交
887 888 889 890
{
	struct sock *sk = NULL, *sk1 = NULL;
	struct hlist_node *node;

891 892
	read_lock(&l2cap_sk_list.lock);

L
Linus Torvalds 已提交
893
	sk_for_each(sk, node, &l2cap_sk_list.head) {
894 895
		struct l2cap_chan *chan = l2cap_pi(sk)->chan;

L
Linus Torvalds 已提交
896 897 898
		if (state && sk->sk_state != state)
			continue;

899
		if (chan->psm == psm) {
L
Linus Torvalds 已提交
900 901 902 903 904 905 906 907 908 909 910
			/* Exact match. */
			if (!bacmp(&bt_sk(sk)->src, src))
				break;

			/* Closest match */
			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
				sk1 = sk;
		}
	}

	read_unlock(&l2cap_sk_list.lock);
911 912

	return node ? sk : sk1;
L
Linus Torvalds 已提交
913 914
}

915
int l2cap_chan_connect(struct l2cap_chan *chan)
L
Linus Torvalds 已提交
916
{
917
	struct sock *sk = chan->sk;
L
Linus Torvalds 已提交
918 919 920 921 922
	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;
923
	__u8 auth_type;
924
	int err;
L
Linus Torvalds 已提交
925

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

929 930
	hdev = hci_get_route(dst, src);
	if (!hdev)
L
Linus Torvalds 已提交
931 932 933 934
		return -EHOSTUNREACH;

	hci_dev_lock_bh(hdev);

935
	auth_type = l2cap_get_auth_type(chan);
936

937
	if (chan->dcid == L2CAP_CID_LE_DATA)
938
		hcon = hci_connect(hdev, LE_LINK, dst,
939
					chan->sec_level, auth_type);
940 941
	else
		hcon = hci_connect(hdev, ACL_LINK, dst,
942
					chan->sec_level, auth_type);
943

944 945
	if (IS_ERR(hcon)) {
		err = PTR_ERR(hcon);
L
Linus Torvalds 已提交
946
		goto done;
947
	}
L
Linus Torvalds 已提交
948 949 950 951

	conn = l2cap_conn_add(hcon, 0);
	if (!conn) {
		hci_conn_put(hcon);
952
		err = -ENOMEM;
L
Linus Torvalds 已提交
953 954 955 956 957 958
		goto done;
	}

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

959 960
	l2cap_chan_add(conn, chan);

L
Linus Torvalds 已提交
961 962 963 964
	sk->sk_state = BT_CONNECT;
	l2cap_sock_set_timer(sk, sk->sk_sndtimeo);

	if (hcon->state == BT_CONNECTED) {
965 966
		if (sk->sk_type != SOCK_SEQPACKET &&
				sk->sk_type != SOCK_STREAM) {
L
Linus Torvalds 已提交
967
			l2cap_sock_clear_timer(sk);
968
			if (l2cap_check_security(chan))
969
				sk->sk_state = BT_CONNECTED;
970
		} else
971
			l2cap_do_start(chan);
L
Linus Torvalds 已提交
972 973
	}

974 975
	err = 0;

L
Linus Torvalds 已提交
976 977 978 979 980 981
done:
	hci_dev_unlock_bh(hdev);
	hci_dev_put(hdev);
	return err;
}

982
int __l2cap_wait_ack(struct sock *sk)
983
{
984
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
985 986 987 988
	DECLARE_WAITQUEUE(wait, current);
	int err = 0;
	int timeo = HZ/5;

989
	add_wait_queue(sk_sleep(sk), &wait);
990
	while ((chan->unacked_frames > 0 && chan->conn)) {
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
		set_current_state(TASK_INTERRUPTIBLE);

		if (!timeo)
			timeo = HZ/5;

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

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

		err = sock_error(sk);
		if (err)
			break;
	}
	set_current_state(TASK_RUNNING);
1010
	remove_wait_queue(sk_sleep(sk), &wait);
1011 1012 1013
	return err;
}

1014 1015
static void l2cap_monitor_timeout(unsigned long arg)
{
1016 1017
	struct l2cap_chan *chan = (void *) arg;
	struct sock *sk = chan->sk;
1018

1019
	BT_DBG("chan %p", chan);
1020

1021
	bh_lock_sock(sk);
1022
	if (chan->retry_count >= chan->remote_max_tx) {
1023
		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1024
		bh_unlock_sock(sk);
1025 1026 1027
		return;
	}

1028
	chan->retry_count++;
1029 1030
	__mod_monitor_timer();

1031
	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
1032
	bh_unlock_sock(sk);
1033 1034 1035 1036
}

static void l2cap_retrans_timeout(unsigned long arg)
{
1037 1038
	struct l2cap_chan *chan = (void *) arg;
	struct sock *sk = chan->sk;
1039

1040
	BT_DBG("chan %p", chan);
1041

1042
	bh_lock_sock(sk);
1043
	chan->retry_count = 1;
1044 1045
	__mod_monitor_timer();

1046
	chan->conn_state |= L2CAP_CONN_WAIT_F;
1047

1048
	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
1049
	bh_unlock_sock(sk);
1050 1051
}

1052
static void l2cap_drop_acked_frames(struct l2cap_chan *chan)
L
Linus Torvalds 已提交
1053
{
1054
	struct sk_buff *skb;
L
Linus Torvalds 已提交
1055

1056
	while ((skb = skb_peek(&chan->tx_q)) &&
1057
			chan->unacked_frames) {
1058
		if (bt_cb(skb)->tx_seq == chan->expected_ack_seq)
1059
			break;
L
Linus Torvalds 已提交
1060

1061
		skb = skb_dequeue(&chan->tx_q);
1062
		kfree_skb(skb);
L
Linus Torvalds 已提交
1063

1064
		chan->unacked_frames--;
1065
	}
L
Linus Torvalds 已提交
1066

1067
	if (!chan->unacked_frames)
1068
		del_timer(&chan->retrans_timer);
1069
}
L
Linus Torvalds 已提交
1070

1071
void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
1072
{
1073
	struct hci_conn *hcon = chan->conn->hcon;
1074
	u16 flags;
1075

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

1078
	if (!chan->flushable && lmp_no_flush_capable(hcon->hdev))
1079 1080 1081 1082 1083
		flags = ACL_START_NO_FLUSH;
	else
		flags = ACL_START;

	hci_send_acl(hcon, skb, flags);
1084 1085
}

1086
void l2cap_streaming_send(struct l2cap_chan *chan)
1087
{
1088
	struct sk_buff *skb;
1089
	u16 control, fcs;
1090

1091
	while ((skb = skb_dequeue(&chan->tx_q))) {
1092
		control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE);
1093
		control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT;
1094
		put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE);
1095

1096
		if (chan->fcs == L2CAP_FCS_CRC16) {
1097 1098
			fcs = crc16(0, (u8 *)skb->data, skb->len - 2);
			put_unaligned_le16(fcs, skb->data + skb->len - 2);
1099 1100
		}

1101
		l2cap_do_send(chan, skb);
1102

1103
		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
1104 1105 1106
	}
}

1107
static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq)
1108 1109 1110 1111
{
	struct sk_buff *skb, *tx_skb;
	u16 control, fcs;

1112
	skb = skb_peek(&chan->tx_q);
1113 1114
	if (!skb)
		return;
1115

1116 1117
	do {
		if (bt_cb(skb)->tx_seq == tx_seq)
1118 1119
			break;

1120
		if (skb_queue_is_last(&chan->tx_q, skb))
1121
			return;
1122

1123
	} while ((skb = skb_queue_next(&chan->tx_q, skb)));
1124

1125 1126
	if (chan->remote_max_tx &&
			bt_cb(skb)->retries == chan->remote_max_tx) {
1127
		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1128 1129 1130 1131 1132 1133
		return;
	}

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

1136
	if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
1137
		control |= L2CAP_CTRL_FINAL;
1138
		chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
1139
	}
1140

1141
	control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
1142
			| (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
1143

1144 1145
	put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);

1146
	if (chan->fcs == L2CAP_FCS_CRC16) {
1147 1148 1149 1150
		fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2);
		put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2);
	}

1151
	l2cap_do_send(chan, tx_skb);
1152 1153
}

1154
int l2cap_ertm_send(struct l2cap_chan *chan)
1155 1156
{
	struct sk_buff *skb, *tx_skb;
1157
	struct sock *sk = chan->sk;
1158
	u16 control, fcs;
1159
	int nsent = 0;
1160

1161 1162
	if (sk->sk_state != BT_CONNECTED)
		return -ENOTCONN;
1163

1164
	while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) {
1165

1166 1167
		if (chan->remote_max_tx &&
				bt_cb(skb)->retries == chan->remote_max_tx) {
1168
			l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1169 1170 1171
			break;
		}

1172 1173
		tx_skb = skb_clone(skb, GFP_ATOMIC);

1174 1175
		bt_cb(skb)->retries++;

1176
		control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
1177 1178
		control &= L2CAP_CTRL_SAR;

1179
		if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
1180
			control |= L2CAP_CTRL_FINAL;
1181
			chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
1182
		}
1183 1184
		control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
				| (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
1185 1186
		put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);

1187

1188
		if (chan->fcs == L2CAP_FCS_CRC16) {
1189 1190 1191 1192
			fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2);
			put_unaligned_le16(fcs, skb->data + tx_skb->len - 2);
		}

1193
		l2cap_do_send(chan, tx_skb);
1194

1195
		__mod_retrans_timer();
1196

1197 1198
		bt_cb(skb)->tx_seq = chan->next_tx_seq;
		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
1199

1200
		if (bt_cb(skb)->retries == 1)
1201
			chan->unacked_frames++;
1202

1203
		chan->frames_sent++;
1204

1205 1206
		if (skb_queue_is_last(&chan->tx_q, skb))
			chan->tx_send_head = NULL;
1207
		else
1208
			chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
1209 1210

		nsent++;
1211 1212
	}

1213 1214 1215
	return nsent;
}

1216
static int l2cap_retransmit_frames(struct l2cap_chan *chan)
1217 1218 1219
{
	int ret;

1220 1221
	if (!skb_queue_empty(&chan->tx_q))
		chan->tx_send_head = chan->tx_q.next;
1222

1223
	chan->next_tx_seq = chan->expected_ack_seq;
1224
	ret = l2cap_ertm_send(chan);
1225 1226 1227
	return ret;
}

1228
static void l2cap_send_ack(struct l2cap_chan *chan)
1229 1230 1231
{
	u16 control = 0;

1232
	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
1233

1234
	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
1235
		control |= L2CAP_SUPER_RCV_NOT_READY;
1236 1237
		chan->conn_state |= L2CAP_CONN_RNR_SENT;
		l2cap_send_sframe(chan, control);
1238
		return;
1239
	}
1240

1241
	if (l2cap_ertm_send(chan) > 0)
1242 1243 1244
		return;

	control |= L2CAP_SUPER_RCV_READY;
1245
	l2cap_send_sframe(chan, control);
1246 1247
}

1248
static void l2cap_send_srejtail(struct l2cap_chan *chan)
1249 1250 1251 1252 1253 1254 1255
{
	struct srej_list *tail;
	u16 control;

	control = L2CAP_SUPER_SELECT_REJECT;
	control |= L2CAP_CTRL_FINAL;

1256
	tail = list_entry((&chan->srej_l)->prev, struct srej_list, list);
1257 1258
	control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;

1259
	l2cap_send_sframe(chan, control);
1260 1261
}

1262 1263
static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb)
{
1264
	struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
1265 1266
	struct sk_buff **frag;
	int err, sent = 0;
L
Linus Torvalds 已提交
1267

1268
	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
1269
		return -EFAULT;
L
Linus Torvalds 已提交
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280

	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)
1281
			return err;
1282 1283
		if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
			return -EFAULT;
L
Linus Torvalds 已提交
1284 1285 1286 1287 1288 1289 1290 1291

		sent += count;
		len  -= count;

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

	return sent;
1292
}
L
Linus Torvalds 已提交
1293

1294
struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
1295
{
1296
	struct sock *sk = chan->sk;
1297
	struct l2cap_conn *conn = chan->conn;
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
	struct sk_buff *skb;
	int err, count, hlen = L2CAP_HDR_SIZE + 2;
	struct l2cap_hdr *lh;

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

	count = min_t(unsigned int, (conn->mtu - hlen), len);
	skb = bt_skb_send_alloc(sk, count + hlen,
			msg->msg_flags & MSG_DONTWAIT, &err);
	if (!skb)
1308
		return ERR_PTR(err);
1309 1310 1311

	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1312
	lh->cid = cpu_to_le16(chan->dcid);
1313
	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
1314
	put_unaligned_le16(chan->psm, skb_put(skb, 2));
1315 1316 1317 1318 1319 1320 1321 1322 1323

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

1324
struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
1325
{
1326
	struct sock *sk = chan->sk;
1327
	struct l2cap_conn *conn = chan->conn;
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
	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)
1338
		return ERR_PTR(err);
1339 1340 1341

	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1342
	lh->cid = cpu_to_le16(chan->dcid);
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
	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;
}

1353
struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen)
1354
{
1355
	struct sock *sk = chan->sk;
1356
	struct l2cap_conn *conn = chan->conn;
1357 1358 1359 1360 1361 1362
	struct sk_buff *skb;
	int err, count, hlen = L2CAP_HDR_SIZE + 2;
	struct l2cap_hdr *lh;

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

1363 1364 1365
	if (!conn)
		return ERR_PTR(-ENOTCONN);

1366 1367 1368
	if (sdulen)
		hlen += 2;

1369
	if (chan->fcs == L2CAP_FCS_CRC16)
1370 1371
		hlen += 2;

1372 1373 1374 1375
	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)
1376
		return ERR_PTR(err);
1377 1378 1379

	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1380
	lh->cid = cpu_to_le16(chan->dcid);
1381 1382
	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
	put_unaligned_le16(control, skb_put(skb, 2));
1383 1384
	if (sdulen)
		put_unaligned_le16(sdulen, skb_put(skb, 2));
1385 1386 1387 1388 1389 1390

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

1392
	if (chan->fcs == L2CAP_FCS_CRC16)
1393 1394
		put_unaligned_le16(0, skb_put(skb, 2));

1395
	bt_cb(skb)->retries = 0;
1396
	return skb;
L
Linus Torvalds 已提交
1397 1398
}

1399
int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
1400 1401 1402 1403 1404 1405
{
	struct sk_buff *skb;
	struct sk_buff_head sar_queue;
	u16 control;
	size_t size = 0;

1406
	skb_queue_head_init(&sar_queue);
1407
	control = L2CAP_SDU_START;
1408
	skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
1409 1410 1411 1412
	if (IS_ERR(skb))
		return PTR_ERR(skb);

	__skb_queue_tail(&sar_queue, skb);
1413 1414
	len -= chan->remote_mps;
	size += chan->remote_mps;
1415 1416 1417 1418

	while (len > 0) {
		size_t buflen;

1419
		if (len > chan->remote_mps) {
1420
			control = L2CAP_SDU_CONTINUE;
1421
			buflen = chan->remote_mps;
1422
		} else {
1423
			control = L2CAP_SDU_END;
1424 1425 1426
			buflen = len;
		}

1427
		skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0);
1428 1429 1430 1431 1432 1433 1434 1435 1436
		if (IS_ERR(skb)) {
			skb_queue_purge(&sar_queue);
			return PTR_ERR(skb);
		}

		__skb_queue_tail(&sar_queue, skb);
		len -= buflen;
		size += buflen;
	}
1437 1438 1439
	skb_queue_splice_tail(&sar_queue, &chan->tx_q);
	if (chan->tx_send_head == NULL)
		chan->tx_send_head = sar_queue.next;
1440 1441 1442 1443

	return size;
}

L
Linus Torvalds 已提交
1444 1445 1446
static void l2cap_chan_ready(struct sock *sk)
{
	struct sock *parent = bt_sk(sk)->parent;
1447
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
L
Linus Torvalds 已提交
1448 1449 1450

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

1451
	chan->conf_state = 0;
L
Linus Torvalds 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
	l2cap_sock_clear_timer(sk);

	if (!parent) {
		/* Outgoing channel.
		 * Wake up socket sleeping on connect.
		 */
		sk->sk_state = BT_CONNECTED;
		sk->sk_state_change(sk);
	} else {
		/* Incoming channel.
		 * Wake up socket sleeping on accept.
		 */
		parent->sk_data_ready(parent, 0);
	}
}

/* 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;
1472
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
1473 1474 1475

	BT_DBG("conn %p", conn);

1476 1477
	read_lock(&conn->chan_lock);
	list_for_each_entry(chan, &conn->chan_l, list) {
1478
		struct sock *sk = chan->sk;
L
Linus Torvalds 已提交
1479 1480 1481 1482 1483 1484
		if (sk->sk_type != SOCK_RAW)
			continue;

		/* Don't send frame to the socket it came from */
		if (skb->sk == sk)
			continue;
1485 1486
		nskb = skb_clone(skb, GFP_ATOMIC);
		if (!nskb)
L
Linus Torvalds 已提交
1487 1488 1489 1490 1491
			continue;

		if (sock_queue_rcv_skb(sk, nskb))
			kfree_skb(nskb);
	}
1492
	read_unlock(&conn->chan_lock);
L
Linus Torvalds 已提交
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
}

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

1504 1505
	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
			conn, code, ident, dlen);
L
Linus Torvalds 已提交
1506 1507 1508 1509 1510 1511 1512 1513 1514

	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);
1515
	lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
1516 1517 1518 1519 1520

	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 已提交
1521 1522 1523 1524

	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
	cmd->code  = code;
	cmd->ident = ident;
1525
	cmd->len   = cpu_to_le16(dlen);
L
Linus Torvalds 已提交
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575

	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:
1576
		*val = get_unaligned_le16(opt->val);
L
Linus Torvalds 已提交
1577 1578 1579
		break;

	case 4:
1580
		*val = get_unaligned_le32(opt->val);
L
Linus Torvalds 已提交
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
		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:
1607
		put_unaligned_le16(val, opt->val);
L
Linus Torvalds 已提交
1608 1609 1610
		break;

	case 4:
1611
		put_unaligned_le32(val, opt->val);
L
Linus Torvalds 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
		break;

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

	*ptr += L2CAP_CONF_OPT_SIZE + len;
}

1622 1623
static void l2cap_ack_timeout(unsigned long arg)
{
1624
	struct l2cap_chan *chan = (void *) arg;
1625

1626 1627 1628
	bh_lock_sock(chan->sk);
	l2cap_send_ack(chan);
	bh_unlock_sock(chan->sk);
1629 1630
}

1631
static inline void l2cap_ertm_init(struct l2cap_chan *chan)
1632
{
1633 1634
	struct sock *sk = chan->sk;

1635
	chan->expected_ack_seq = 0;
1636
	chan->unacked_frames = 0;
1637
	chan->buffer_seq = 0;
1638 1639
	chan->num_acked = 0;
	chan->frames_sent = 0;
1640

1641 1642 1643 1644 1645
	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);
1646

1647 1648
	skb_queue_head_init(&chan->srej_q);
	skb_queue_head_init(&chan->busy_q);
1649

1650 1651
	INIT_LIST_HEAD(&chan->srej_l);

1652
	INIT_WORK(&chan->busy_work, l2cap_busy_work);
1653 1654

	sk->sk_backlog_rcv = l2cap_ertm_data_rcv;
1655 1656
}

1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
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;
	}
}

1670
static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
L
Linus Torvalds 已提交
1671 1672
{
	struct l2cap_conf_req *req = data;
1673
	struct l2cap_conf_rfc rfc = { .mode = chan->mode };
L
Linus Torvalds 已提交
1674 1675
	void *ptr = req->data;

1676
	BT_DBG("chan %p", chan);
L
Linus Torvalds 已提交
1677

1678
	if (chan->num_conf_req || chan->num_conf_rsp)
1679 1680
		goto done;

1681
	switch (chan->mode) {
1682 1683
	case L2CAP_MODE_STREAMING:
	case L2CAP_MODE_ERTM:
1684
		if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE)
1685 1686
			break;

1687
		/* fall through */
1688
	default:
1689
		chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
1690 1691 1692 1693
		break;
	}

done:
1694 1695
	if (chan->imtu != L2CAP_DEFAULT_MTU)
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
1696

1697
	switch (chan->mode) {
1698
	case L2CAP_MODE_BASIC:
1699 1700
		if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
				!(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
1701 1702
			break;

1703 1704 1705 1706 1707 1708 1709
		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;

1710 1711
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
							(unsigned long) &rfc);
1712 1713 1714 1715
		break;

	case L2CAP_MODE_ERTM:
		rfc.mode            = L2CAP_MODE_ERTM;
1716 1717
		rfc.txwin_size      = chan->tx_win;
		rfc.max_transmit    = chan->max_tx;
1718 1719
		rfc.retrans_timeout = 0;
		rfc.monitor_timeout = 0;
1720
		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
1721 1722
		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
1723

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

1727
		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
1728 1729
			break;

1730
		if (chan->fcs == L2CAP_FCS_NONE ||
1731
				chan->conf_state & L2CAP_CONF_NO_FCS_RECV) {
1732 1733
			chan->fcs = L2CAP_FCS_NONE;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
1734
		}
1735 1736 1737 1738 1739 1740 1741 1742
		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;
1743
		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
1744 1745
		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
1746

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

1750
		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
1751 1752
			break;

1753
		if (chan->fcs == L2CAP_FCS_NONE ||
1754
				chan->conf_state & L2CAP_CONF_NO_FCS_RECV) {
1755 1756
			chan->fcs = L2CAP_FCS_NONE;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
1757
		}
1758 1759
		break;
	}
L
Linus Torvalds 已提交
1760

1761
	req->dcid  = cpu_to_le16(chan->dcid);
1762
	req->flags = cpu_to_le16(0);
L
Linus Torvalds 已提交
1763 1764 1765 1766

	return ptr - data;
}

1767
static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
L
Linus Torvalds 已提交
1768
{
1769 1770
	struct l2cap_conf_rsp *rsp = data;
	void *ptr = rsp->data;
1771 1772
	void *req = chan->conf_req;
	int len = chan->conf_len;
1773 1774
	int type, hint, olen;
	unsigned long val;
1775
	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
1776
	u16 mtu = L2CAP_DEFAULT_MTU;
1777
	u16 result = L2CAP_CONF_SUCCESS;
L
Linus Torvalds 已提交
1778

1779
	BT_DBG("chan %p", chan);
1780

1781 1782
	while (len >= L2CAP_CONF_OPT_SIZE) {
		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
L
Linus Torvalds 已提交
1783

1784
		hint  = type & L2CAP_CONF_HINT;
1785
		type &= L2CAP_CONF_MASK;
1786 1787 1788

		switch (type) {
		case L2CAP_CONF_MTU:
1789
			mtu = val;
1790 1791 1792
			break;

		case L2CAP_CONF_FLUSH_TO:
1793
			chan->flush_to = val;
1794 1795 1796 1797 1798
			break;

		case L2CAP_CONF_QOS:
			break;

1799 1800 1801 1802 1803
		case L2CAP_CONF_RFC:
			if (olen == sizeof(rfc))
				memcpy(&rfc, (void *) val, olen);
			break;

1804 1805
		case L2CAP_CONF_FCS:
			if (val == L2CAP_FCS_NONE)
1806
				chan->conf_state |= L2CAP_CONF_NO_FCS_RECV;
1807 1808 1809

			break;

1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
		default:
			if (hint)
				break;

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

1820
	if (chan->num_conf_rsp || chan->num_conf_req > 1)
1821 1822
		goto done;

1823
	switch (chan->mode) {
1824 1825
	case L2CAP_MODE_STREAMING:
	case L2CAP_MODE_ERTM:
1826
		if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) {
1827
			chan->mode = l2cap_select_mode(rfc.mode,
1828
					chan->conn->feat_mask);
1829 1830 1831
			break;
		}

1832
		if (chan->mode != rfc.mode)
1833
			return -ECONNREFUSED;
1834

1835 1836 1837 1838
		break;
	}

done:
1839
	if (chan->mode != rfc.mode) {
1840
		result = L2CAP_CONF_UNACCEPT;
1841
		rfc.mode = chan->mode;
1842

1843
		if (chan->num_conf_rsp == 1)
1844 1845 1846 1847 1848 1849 1850
			return -ECONNREFUSED;

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


1851 1852 1853 1854
	if (result == L2CAP_CONF_SUCCESS) {
		/* Configure output options and let the other side know
		 * which ones we don't like. */

1855 1856 1857
		if (mtu < L2CAP_DEFAULT_MIN_MTU)
			result = L2CAP_CONF_UNACCEPT;
		else {
1858
			chan->omtu = mtu;
1859
			chan->conf_state |= L2CAP_CONF_MTU_DONE;
1860
		}
1861
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
1862

1863 1864
		switch (rfc.mode) {
		case L2CAP_MODE_BASIC:
1865
			chan->fcs = L2CAP_FCS_NONE;
1866
			chan->conf_state |= L2CAP_CONF_MODE_DONE;
1867 1868 1869
			break;

		case L2CAP_MODE_ERTM:
1870 1871
			chan->remote_tx_win = rfc.txwin_size;
			chan->remote_max_tx = rfc.max_transmit;
1872

1873 1874
			if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10)
				rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
1875

1876
			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
1877

1878 1879 1880 1881
			rfc.retrans_timeout =
				le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO);
			rfc.monitor_timeout =
				le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO);
1882

1883
			chan->conf_state |= L2CAP_CONF_MODE_DONE;
1884 1885 1886 1887

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

1888 1889 1890
			break;

		case L2CAP_MODE_STREAMING:
1891 1892
			if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10)
				rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
1893

1894
			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
1895

1896
			chan->conf_state |= L2CAP_CONF_MODE_DONE;
1897 1898 1899 1900

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

1901 1902 1903
			break;

		default:
1904 1905
			result = L2CAP_CONF_UNACCEPT;

1906
			memset(&rfc, 0, sizeof(rfc));
1907
			rfc.mode = chan->mode;
1908
		}
1909

1910
		if (result == L2CAP_CONF_SUCCESS)
1911
			chan->conf_state |= L2CAP_CONF_OUTPUT_DONE;
1912
	}
1913
	rsp->scid   = cpu_to_le16(chan->dcid);
1914 1915 1916 1917
	rsp->result = cpu_to_le16(result);
	rsp->flags  = cpu_to_le16(0x0000);

	return ptr - data;
L
Linus Torvalds 已提交
1918 1919
}

1920
static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result)
1921 1922 1923 1924 1925 1926 1927
{
	struct l2cap_conf_req *req = data;
	void *ptr = req->data;
	int type, olen;
	unsigned long val;
	struct l2cap_conf_rfc rfc;

1928
	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
1929 1930 1931 1932 1933 1934 1935 1936

	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;
1937
				chan->imtu = L2CAP_DEFAULT_MIN_MTU;
1938
			} else
1939 1940
				chan->imtu = val;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
1941 1942 1943
			break;

		case L2CAP_CONF_FLUSH_TO:
1944
			chan->flush_to = val;
1945
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
1946
							2, chan->flush_to);
1947 1948 1949 1950 1951 1952
			break;

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

1953
			if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) &&
1954
							rfc.mode != chan->mode)
1955 1956
				return -ECONNREFUSED;

1957
			chan->fcs = 0;
1958 1959 1960 1961 1962 1963 1964

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

1965
	if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
1966 1967
		return -ECONNREFUSED;

1968
	chan->mode = rfc.mode;
1969

1970 1971 1972
	if (*result == L2CAP_CONF_SUCCESS) {
		switch (rfc.mode) {
		case L2CAP_MODE_ERTM:
1973 1974 1975
			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);
1976 1977
			break;
		case L2CAP_MODE_STREAMING:
1978
			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
1979 1980 1981
		}
	}

1982
	req->dcid   = cpu_to_le16(chan->dcid);
1983 1984 1985 1986 1987
	req->flags  = cpu_to_le16(0x0000);

	return ptr - data;
}

1988
static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags)
L
Linus Torvalds 已提交
1989 1990 1991 1992
{
	struct l2cap_conf_rsp *rsp = data;
	void *ptr = rsp->data;

1993
	BT_DBG("chan %p", chan);
L
Linus Torvalds 已提交
1994

1995
	rsp->scid   = cpu_to_le16(chan->dcid);
1996
	rsp->result = cpu_to_le16(result);
1997
	rsp->flags  = cpu_to_le16(flags);
L
Linus Torvalds 已提交
1998 1999 2000 2001

	return ptr - data;
}

2002
void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
2003 2004
{
	struct l2cap_conn_rsp rsp;
2005
	struct l2cap_conn *conn = chan->conn;
2006 2007
	u8 buf[128];

2008 2009
	rsp.scid   = cpu_to_le16(chan->dcid);
	rsp.dcid   = cpu_to_le16(chan->scid);
2010 2011 2012 2013 2014
	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);

2015
	if (chan->conf_state & L2CAP_CONF_REQ_SENT)
2016 2017
		return;

2018
	chan->conf_state |= L2CAP_CONF_REQ_SENT;
2019 2020 2021 2022 2023
	l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
			l2cap_build_conf_req(chan, buf), buf);
	chan->num_conf_req++;
}

2024
static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
2025 2026 2027 2028 2029
{
	int type, olen;
	unsigned long val;
	struct l2cap_conf_rfc rfc;

2030
	BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
2031

2032
	if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
		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:
2049 2050 2051
		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);
2052 2053
		break;
	case L2CAP_MODE_STREAMING:
2054
		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
2055 2056 2057
	}
}

2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
{
	struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data;

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

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

		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
2070
		conn->info_ident = 0;
2071

2072 2073 2074 2075 2076 2077
		l2cap_conn_start(conn);
	}

	return 0;
}

L
Linus Torvalds 已提交
2078 2079 2080 2081
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;
2082
	struct l2cap_chan *chan = NULL;
2083
	struct sock *parent, *sk = NULL;
2084
	int result, status = L2CAP_CS_NO_INFO;
L
Linus Torvalds 已提交
2085 2086

	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
2087
	__le16 psm = req->psm;
L
Linus Torvalds 已提交
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097

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

	/* Check if we have socket listening on psm */
	parent = l2cap_get_sock_by_psm(BT_LISTEN, psm, conn->src);
	if (!parent) {
		result = L2CAP_CR_BAD_PSM;
		goto sendresp;
	}

2098 2099
	bh_lock_sock(parent);

2100 2101 2102
	/* Check if the ACL is secure enough (if not SDP) */
	if (psm != cpu_to_le16(0x0001) &&
				!hci_conn_check_link_mode(conn->hcon)) {
2103
		conn->disc_reason = 0x05;
2104 2105 2106 2107
		result = L2CAP_CR_SEC_BLOCK;
		goto response;
	}

L
Linus Torvalds 已提交
2108 2109 2110 2111
	result = L2CAP_CR_NO_MEM;

	/* Check for backlog size */
	if (sk_acceptq_is_full(parent)) {
2112
		BT_DBG("backlog full %d", parent->sk_ack_backlog);
L
Linus Torvalds 已提交
2113 2114 2115
		goto response;
	}

2116
	sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC);
L
Linus Torvalds 已提交
2117 2118 2119
	if (!sk)
		goto response;

2120 2121 2122 2123 2124 2125
	chan = l2cap_chan_alloc(sk);
	if (!chan) {
		l2cap_sock_kill(sk);
		goto response;
	}

2126 2127
	l2cap_pi(sk)->chan = chan;

2128
	write_lock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2129 2130

	/* Check if we already have channel with that dcid */
2131 2132
	if (__l2cap_get_chan_by_dcid(conn, scid)) {
		write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
		sock_set_flag(sk, SOCK_ZAPPED);
		l2cap_sock_kill(sk);
		goto response;
	}

	hci_conn_hold(conn->hcon);

	l2cap_sock_init(sk, parent);
	bacpy(&bt_sk(sk)->src, conn->src);
	bacpy(&bt_sk(sk)->dst, conn->dst);
2143 2144
	chan->psm  = psm;
	chan->dcid = scid;
L
Linus Torvalds 已提交
2145

2146 2147
	bt_accept_enqueue(parent, sk);

2148 2149
	__l2cap_chan_add(conn, chan);

2150
	dcid = chan->scid;
L
Linus Torvalds 已提交
2151 2152 2153

	l2cap_sock_set_timer(sk, sk->sk_sndtimeo);

2154
	chan->ident = cmd->ident;
L
Linus Torvalds 已提交
2155

2156
	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
2157
		if (l2cap_check_security(chan)) {
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
			if (bt_sk(sk)->defer_setup) {
				sk->sk_state = BT_CONNECT2;
				result = L2CAP_CR_PEND;
				status = L2CAP_CS_AUTHOR_PEND;
				parent->sk_data_ready(parent, 0);
			} else {
				sk->sk_state = BT_CONFIG;
				result = L2CAP_CR_SUCCESS;
				status = L2CAP_CS_NO_INFO;
			}
2168 2169 2170 2171 2172 2173 2174 2175 2176
		} else {
			sk->sk_state = BT_CONNECT2;
			result = L2CAP_CR_PEND;
			status = L2CAP_CS_AUTHEN_PEND;
		}
	} else {
		sk->sk_state = BT_CONNECT2;
		result = L2CAP_CR_PEND;
		status = L2CAP_CS_NO_INFO;
L
Linus Torvalds 已提交
2177 2178
	}

2179
	write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2180 2181 2182 2183 2184

response:
	bh_unlock_sock(parent);

sendresp:
2185 2186 2187 2188
	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 已提交
2189
	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204

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

2205
	if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) &&
2206 2207
				result == L2CAP_CR_SUCCESS) {
		u8 buf[128];
2208
		chan->conf_state |= L2CAP_CONF_REQ_SENT;
2209
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2210 2211
					l2cap_build_conf_req(chan, buf), buf);
		chan->num_conf_req++;
2212 2213
	}

L
Linus Torvalds 已提交
2214 2215 2216 2217 2218 2219 2220
	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;
2221
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232
	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) {
2233
		chan = l2cap_get_chan_by_scid(conn, scid);
2234
		if (!chan)
2235
			return -EFAULT;
L
Linus Torvalds 已提交
2236
	} else {
2237
		chan = l2cap_get_chan_by_ident(conn, cmd->ident);
2238
		if (!chan)
2239
			return -EFAULT;
L
Linus Torvalds 已提交
2240 2241
	}

2242 2243
	sk = chan->sk;

L
Linus Torvalds 已提交
2244 2245 2246
	switch (result) {
	case L2CAP_CR_SUCCESS:
		sk->sk_state = BT_CONFIG;
2247
		chan->ident = 0;
2248
		chan->dcid = dcid;
2249
		chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND;
2250

2251
		if (chan->conf_state & L2CAP_CONF_REQ_SENT)
2252 2253
			break;

2254
		chan->conf_state |= L2CAP_CONF_REQ_SENT;
2255

L
Linus Torvalds 已提交
2256
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2257 2258
					l2cap_build_conf_req(chan, req), req);
		chan->num_conf_req++;
L
Linus Torvalds 已提交
2259 2260 2261
		break;

	case L2CAP_CR_PEND:
2262
		chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
L
Linus Torvalds 已提交
2263 2264 2265
		break;

	default:
2266 2267 2268 2269 2270 2271 2272 2273
		/* don't delete l2cap channel if sk is owned by user */
		if (sock_owned_by_user(sk)) {
			sk->sk_state = BT_DISCONN;
			l2cap_sock_clear_timer(sk);
			l2cap_sock_set_timer(sk, HZ / 5);
			break;
		}

2274
		l2cap_chan_del(chan, ECONNREFUSED);
L
Linus Torvalds 已提交
2275 2276 2277 2278 2279 2280 2281
		break;
	}

	bh_unlock_sock(sk);
	return 0;
}

2282
static inline void set_default_fcs(struct l2cap_chan *chan)
2283
{
2284 2285
	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);

2286 2287 2288
	/* FCS is enabled only in ERTM or streaming mode, if one or both
	 * sides request it.
	 */
2289
	if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
2290
		chan->fcs = L2CAP_FCS_NONE;
2291
	else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV))
2292
		chan->fcs = L2CAP_FCS_CRC16;
2293 2294
}

2295
static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
L
Linus Torvalds 已提交
2296 2297 2298 2299
{
	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
	u16 dcid, flags;
	u8 rsp[64];
2300
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2301
	struct sock *sk;
2302
	int len;
L
Linus Torvalds 已提交
2303 2304 2305 2306 2307 2308

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

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

2309
	chan = l2cap_get_chan_by_scid(conn, dcid);
2310
	if (!chan)
L
Linus Torvalds 已提交
2311 2312
		return -ENOENT;

2313 2314
	sk = chan->sk;

2315 2316 2317 2318 2319 2320
	if (sk->sk_state != BT_CONFIG) {
		struct l2cap_cmd_rej rej;

		rej.reason = cpu_to_le16(0x0002);
		l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
				sizeof(rej), &rej);
2321
		goto unlock;
2322
	}
2323

2324
	/* Reject if config buffer is too small. */
2325
	len = cmd_len - sizeof(*req);
2326
	if (chan->conf_len + len > sizeof(chan->conf_req)) {
2327
		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2328
				l2cap_build_conf_rsp(chan, rsp,
2329 2330 2331 2332 2333
					L2CAP_CONF_REJECT, flags), rsp);
		goto unlock;
	}

	/* Store config. */
2334 2335
	memcpy(chan->conf_req + chan->conf_len, req->data, len);
	chan->conf_len += len;
L
Linus Torvalds 已提交
2336 2337 2338 2339

	if (flags & 0x0001) {
		/* Incomplete config. Send empty response. */
		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2340
				l2cap_build_conf_rsp(chan, rsp,
2341
					L2CAP_CONF_SUCCESS, 0x0001), rsp);
L
Linus Torvalds 已提交
2342 2343 2344 2345
		goto unlock;
	}

	/* Complete config. */
2346
	len = l2cap_parse_conf_req(chan, rsp);
2347
	if (len < 0) {
2348
		l2cap_send_disconn_req(conn, chan, ECONNRESET);
L
Linus Torvalds 已提交
2349
		goto unlock;
2350
	}
L
Linus Torvalds 已提交
2351

2352
	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
2353
	chan->num_conf_rsp++;
2354 2355

	/* Reset config buffer. */
2356
	chan->conf_len = 0;
2357

2358
	if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE))
2359 2360
		goto unlock;

2361
	if (chan->conf_state & L2CAP_CONF_INPUT_DONE) {
2362
		set_default_fcs(chan);
2363

L
Linus Torvalds 已提交
2364
		sk->sk_state = BT_CONNECTED;
2365

2366 2367
		chan->next_tx_seq = 0;
		chan->expected_tx_seq = 0;
2368
		skb_queue_head_init(&chan->tx_q);
2369
		if (chan->mode == L2CAP_MODE_ERTM)
2370
			l2cap_ertm_init(chan);
2371

L
Linus Torvalds 已提交
2372
		l2cap_chan_ready(sk);
2373 2374 2375
		goto unlock;
	}

2376
	if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) {
2377
		u8 buf[64];
2378
		chan->conf_state |= L2CAP_CONF_REQ_SENT;
L
Linus Torvalds 已提交
2379
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2380 2381
					l2cap_build_conf_req(chan, buf), buf);
		chan->num_conf_req++;
L
Linus Torvalds 已提交
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
	}

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;
2393
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2394
	struct sock *sk;
2395
	int len = cmd->len - sizeof(*rsp);
L
Linus Torvalds 已提交
2396 2397 2398 2399 2400

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

2401 2402
	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
			scid, flags, result);
L
Linus Torvalds 已提交
2403

2404
	chan = l2cap_get_chan_by_scid(conn, scid);
2405
	if (!chan)
L
Linus Torvalds 已提交
2406 2407
		return 0;

2408 2409
	sk = chan->sk;

L
Linus Torvalds 已提交
2410 2411
	switch (result) {
	case L2CAP_CONF_SUCCESS:
2412
		l2cap_conf_rfc_get(chan, rsp->data, len);
L
Linus Torvalds 已提交
2413 2414 2415
		break;

	case L2CAP_CONF_UNACCEPT:
2416
		if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
2417 2418
			char req[64];

2419
			if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
2420
				l2cap_send_disconn_req(conn, chan, ECONNRESET);
2421 2422 2423
				goto done;
			}

2424 2425
			/* throw out any old stored conf requests */
			result = L2CAP_CONF_SUCCESS;
2426 2427
			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
								req, &result);
2428
			if (len < 0) {
2429
				l2cap_send_disconn_req(conn, chan, ECONNRESET);
2430 2431 2432 2433 2434
				goto done;
			}

			l2cap_send_cmd(conn, l2cap_get_ident(conn),
						L2CAP_CONF_REQ, len, req);
2435
			chan->num_conf_req++;
2436 2437 2438
			if (result != L2CAP_CONF_SUCCESS)
				goto done;
			break;
L
Linus Torvalds 已提交
2439 2440
		}

2441
	default:
2442
		sk->sk_err = ECONNRESET;
L
Linus Torvalds 已提交
2443
		l2cap_sock_set_timer(sk, HZ * 5);
2444
		l2cap_send_disconn_req(conn, chan, ECONNRESET);
L
Linus Torvalds 已提交
2445 2446 2447 2448 2449 2450
		goto done;
	}

	if (flags & 0x01)
		goto done;

2451
	chan->conf_state |= L2CAP_CONF_INPUT_DONE;
L
Linus Torvalds 已提交
2452

2453
	if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) {
2454
		set_default_fcs(chan);
2455

L
Linus Torvalds 已提交
2456
		sk->sk_state = BT_CONNECTED;
2457 2458
		chan->next_tx_seq = 0;
		chan->expected_tx_seq = 0;
2459
		skb_queue_head_init(&chan->tx_q);
2460
		if (chan->mode ==  L2CAP_MODE_ERTM)
2461
			l2cap_ertm_init(chan);
2462

L
Linus Torvalds 已提交
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475
		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;
2476
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2477 2478 2479 2480 2481 2482 2483
	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);

2484
	chan = l2cap_get_chan_by_scid(conn, dcid);
2485
	if (!chan)
L
Linus Torvalds 已提交
2486 2487
		return 0;

2488 2489
	sk = chan->sk;

2490 2491
	rsp.dcid = cpu_to_le16(chan->scid);
	rsp.scid = cpu_to_le16(chan->dcid);
L
Linus Torvalds 已提交
2492 2493 2494 2495
	l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);

	sk->sk_shutdown = SHUTDOWN_MASK;

2496 2497 2498 2499 2500 2501 2502 2503 2504
	/* don't delete l2cap channel if sk is owned by user */
	if (sock_owned_by_user(sk)) {
		sk->sk_state = BT_DISCONN;
		l2cap_sock_clear_timer(sk);
		l2cap_sock_set_timer(sk, HZ / 5);
		bh_unlock_sock(sk);
		return 0;
	}

2505
	l2cap_chan_del(chan, ECONNRESET);
L
Linus Torvalds 已提交
2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
	bh_unlock_sock(sk);

	l2cap_sock_kill(sk);
	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;
2516
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2517 2518 2519 2520 2521 2522 2523
	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);

2524
	chan = l2cap_get_chan_by_scid(conn, scid);
2525
	if (!chan)
L
Linus Torvalds 已提交
2526 2527
		return 0;

2528 2529
	sk = chan->sk;

2530 2531 2532 2533 2534 2535 2536 2537 2538
	/* don't delete l2cap channel if sk is owned by user */
	if (sock_owned_by_user(sk)) {
		sk->sk_state = BT_DISCONN;
		l2cap_sock_clear_timer(sk);
		l2cap_sock_set_timer(sk, HZ / 5);
		bh_unlock_sock(sk);
		return 0;
	}

2539
	l2cap_chan_del(chan, 0);
L
Linus Torvalds 已提交
2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554
	bh_unlock_sock(sk);

	l2cap_sock_kill(sk);
	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);

2555 2556
	if (type == L2CAP_IT_FEAT_MASK) {
		u8 buf[8];
2557
		u32 feat_mask = l2cap_feat_mask;
2558 2559 2560
		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);
2561
		if (!disable_ertm)
2562 2563
			feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
							 | L2CAP_FEAT_FCS;
2564
		put_unaligned_le32(feat_mask, rsp->data);
2565 2566
		l2cap_send_cmd(conn, cmd->ident,
					L2CAP_INFO_RSP, sizeof(buf), buf);
2567 2568 2569 2570 2571 2572 2573 2574
	} else if (type == L2CAP_IT_FIXED_CHAN) {
		u8 buf[12];
		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
		rsp->type   = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
		memcpy(buf + 4, l2cap_fixed_chan, 8);
		l2cap_send_cmd(conn, cmd->ident,
					L2CAP_INFO_RSP, sizeof(buf), buf);
2575 2576 2577 2578 2579 2580 2581
	} 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 已提交
2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595

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

2596 2597 2598 2599 2600
	/* 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;

2601 2602
	del_timer(&conn->info_timer);

2603 2604 2605 2606 2607 2608 2609 2610 2611
	if (result != L2CAP_IR_SUCCESS) {
		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
		conn->info_ident = 0;

		l2cap_conn_start(conn);

		return 0;
	}

2612
	if (type == L2CAP_IT_FEAT_MASK) {
2613
		conn->feat_mask = get_unaligned_le32(rsp->data);
2614

2615
		if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
			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) {
2630
		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
2631
		conn->info_ident = 0;
2632 2633 2634

		l2cap_conn_start(conn);
	}
2635

L
Linus Torvalds 已提交
2636 2637 2638
	return 0;
}

2639
static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
							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;
2667
	int err;
2668 2669 2670 2671 2672 2673 2674 2675 2676

	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;
2677 2678
	min		= __le16_to_cpu(req->min);
	max		= __le16_to_cpu(req->max);
2679 2680 2681 2682 2683 2684 2685
	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));
2686 2687 2688

	err = l2cap_check_conn_param(min, max, latency, to_multiplier);
	if (err)
2689 2690 2691 2692 2693 2694 2695
		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);

2696 2697 2698
	if (!err)
		hci_le_conn_update(hcon, min, max, latency, to_multiplier);

2699 2700 2701
	return 0;
}

2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
			struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
{
	int err = 0;

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

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

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

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

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

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

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

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

	case L2CAP_ECHO_RSP:
		break;

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

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

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

	return err;
}

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

	case L2CAP_CONN_PARAM_UPDATE_REQ:
2768
		return l2cap_conn_param_update_req(conn, cmd, data);
2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780

	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 已提交
2781 2782 2783 2784
{
	u8 *data = skb->data;
	int len = skb->len;
	struct l2cap_cmd_hdr cmd;
2785
	int err;
L
Linus Torvalds 已提交
2786 2787 2788 2789

	l2cap_raw_recv(conn, skb);

	while (len >= L2CAP_CMD_HDR_SIZE) {
2790
		u16 cmd_len;
L
Linus Torvalds 已提交
2791 2792 2793 2794
		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
		data += L2CAP_CMD_HDR_SIZE;
		len  -= L2CAP_CMD_HDR_SIZE;

2795
		cmd_len = le16_to_cpu(cmd.len);
L
Linus Torvalds 已提交
2796

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

2799
		if (cmd_len > len || !cmd.ident) {
L
Linus Torvalds 已提交
2800 2801 2802 2803
			BT_DBG("corrupted command");
			break;
		}

2804 2805 2806 2807
		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 已提交
2808 2809 2810

		if (err) {
			struct l2cap_cmd_rej rej;
2811 2812

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

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

2819 2820
		data += cmd_len;
		len  -= cmd_len;
L
Linus Torvalds 已提交
2821 2822 2823 2824 2825
	}

	kfree_skb(skb);
}

2826
static int l2cap_check_fcs(struct l2cap_chan *chan,  struct sk_buff *skb)
2827 2828 2829 2830
{
	u16 our_fcs, rcv_fcs;
	int hdr_size = L2CAP_HDR_SIZE + 2;

2831
	if (chan->fcs == L2CAP_FCS_CRC16) {
2832 2833 2834 2835 2836
		skb_trim(skb, skb->len - 2);
		rcv_fcs = get_unaligned_le16(skb->data + skb->len);
		our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);

		if (our_fcs != rcv_fcs)
2837
			return -EBADMSG;
2838 2839 2840 2841
	}
	return 0;
}

2842
static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
2843 2844 2845
{
	u16 control = 0;

2846
	chan->frames_sent = 0;
2847

2848
	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
2849

2850
	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
2851
		control |= L2CAP_SUPER_RCV_NOT_READY;
2852 2853
		l2cap_send_sframe(chan, control);
		chan->conn_state |= L2CAP_CONN_RNR_SENT;
2854 2855
	}

2856 2857
	if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY)
		l2cap_retransmit_frames(chan);
2858

2859
	l2cap_ertm_send(chan);
2860

2861
	if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
2862
			chan->frames_sent == 0) {
2863
		control |= L2CAP_SUPER_RCV_READY;
2864
		l2cap_send_sframe(chan, control);
2865 2866 2867
	}
}

2868
static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar)
2869 2870
{
	struct sk_buff *next_skb;
2871
	int tx_seq_offset, next_tx_seq_offset;
2872 2873 2874 2875

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

2876
	next_skb = skb_peek(&chan->srej_q);
2877
	if (!next_skb) {
2878
		__skb_queue_tail(&chan->srej_q, skb);
2879
		return 0;
2880 2881
	}

2882
	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
2883 2884 2885
	if (tx_seq_offset < 0)
		tx_seq_offset += 64;

2886
	do {
2887 2888 2889
		if (bt_cb(next_skb)->tx_seq == tx_seq)
			return -EINVAL;

2890
		next_tx_seq_offset = (bt_cb(next_skb)->tx_seq -
2891
						chan->buffer_seq) % 64;
2892 2893 2894 2895
		if (next_tx_seq_offset < 0)
			next_tx_seq_offset += 64;

		if (next_tx_seq_offset > tx_seq_offset) {
2896
			__skb_queue_before(&chan->srej_q, next_skb, skb);
2897
			return 0;
2898 2899
		}

2900
		if (skb_queue_is_last(&chan->srej_q, next_skb))
2901 2902
			break;

2903
	} while ((next_skb = skb_queue_next(&chan->srej_q, next_skb)));
2904

2905
	__skb_queue_tail(&chan->srej_q, skb);
2906 2907

	return 0;
2908 2909
}

2910
static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
2911 2912
{
	struct sk_buff *_skb;
2913
	int err;
2914 2915 2916

	switch (control & L2CAP_CTRL_SAR) {
	case L2CAP_SDU_UNSEGMENTED:
2917
		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
2918 2919
			goto drop;

2920
		err = sock_queue_rcv_skb(chan->sk, skb);
2921 2922 2923 2924 2925 2926
		if (!err)
			return err;

		break;

	case L2CAP_SDU_START:
2927
		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
2928 2929
			goto drop;

2930
		chan->sdu_len = get_unaligned_le16(skb->data);
2931

2932
		if (chan->sdu_len > chan->imtu)
2933 2934
			goto disconnect;

2935 2936
		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
		if (!chan->sdu)
2937 2938 2939 2940 2941 2942
			return -ENOMEM;

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

2944
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
2945

2946
		chan->conn_state |= L2CAP_CONN_SAR_SDU;
2947
		chan->partial_sdu_len = skb->len;
2948 2949 2950
		break;

	case L2CAP_SDU_CONTINUE:
2951
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
2952 2953
			goto disconnect;

2954
		if (!chan->sdu)
2955 2956
			goto disconnect;

2957 2958
		chan->partial_sdu_len += skb->len;
		if (chan->partial_sdu_len > chan->sdu_len)
2959 2960
			goto drop;

2961
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
2962

2963 2964 2965
		break;

	case L2CAP_SDU_END:
2966
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
2967 2968
			goto disconnect;

2969
		if (!chan->sdu)
2970 2971
			goto disconnect;

2972
		if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) {
2973
			chan->partial_sdu_len += skb->len;
2974

2975
			if (chan->partial_sdu_len > chan->imtu)
2976
				goto drop;
2977

2978
			if (chan->partial_sdu_len != chan->sdu_len)
2979
				goto drop;
2980

2981
			memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
2982
		}
2983

2984
		_skb = skb_clone(chan->sdu, GFP_ATOMIC);
2985
		if (!_skb) {
2986
			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
2987 2988 2989
			return -ENOMEM;
		}

2990
		err = sock_queue_rcv_skb(chan->sk, _skb);
2991
		if (err < 0) {
2992
			kfree_skb(_skb);
2993
			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
2994 2995 2996
			return err;
		}

2997 2998
		chan->conn_state &= ~L2CAP_CONN_SAR_RETRY;
		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
2999

3000
		kfree_skb(chan->sdu);
3001 3002 3003 3004
		break;
	}

	kfree_skb(skb);
3005
	return 0;
3006 3007

drop:
3008 3009
	kfree_skb(chan->sdu);
	chan->sdu = NULL;
3010 3011

disconnect:
3012
	l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3013 3014 3015 3016
	kfree_skb(skb);
	return 0;
}

3017
static int l2cap_try_push_rx_skb(struct l2cap_chan *chan)
3018 3019 3020 3021 3022
{
	struct sk_buff *skb;
	u16 control;
	int err;

3023
	while ((skb = skb_dequeue(&chan->busy_q))) {
3024
		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3025
		err = l2cap_ertm_reassembly_sdu(chan, skb, control);
3026
		if (err < 0) {
3027
			skb_queue_head(&chan->busy_q, skb);
3028 3029 3030
			return -EBUSY;
		}

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

3034
	if (!(chan->conn_state & L2CAP_CONN_RNR_SENT))
3035 3036
		goto done;

3037
	control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3038
	control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL;
3039
	l2cap_send_sframe(chan, control);
3040
	chan->retry_count = 1;
3041

3042
	del_timer(&chan->retrans_timer);
3043 3044
	__mod_monitor_timer();

3045
	chan->conn_state |= L2CAP_CONN_WAIT_F;
3046 3047

done:
3048 3049
	chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY;
	chan->conn_state &= ~L2CAP_CONN_RNR_SENT;
3050

3051
	BT_DBG("chan %p, Exit local busy", chan);
3052 3053 3054 3055

	return 0;
}

3056 3057 3058
static void l2cap_busy_work(struct work_struct *work)
{
	DECLARE_WAITQUEUE(wait, current);
3059 3060 3061
	struct l2cap_chan *chan =
		container_of(work, struct l2cap_chan, busy_work);
	struct sock *sk = chan->sk;
3062 3063 3064 3065 3066
	int n_tries = 0, timeo = HZ/5, err;
	struct sk_buff *skb;

	lock_sock(sk);

3067
	add_wait_queue(sk_sleep(sk), &wait);
3068
	while ((skb = skb_peek(&chan->busy_q))) {
3069 3070 3071 3072
		set_current_state(TASK_INTERRUPTIBLE);

		if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) {
			err = -EBUSY;
3073
			l2cap_send_disconn_req(chan->conn, chan, EBUSY);
3074
			break;
3075 3076 3077 3078 3079 3080 3081
		}

		if (!timeo)
			timeo = HZ/5;

		if (signal_pending(current)) {
			err = sock_intr_errno(timeo);
3082
			break;
3083 3084 3085 3086 3087 3088 3089 3090
		}

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

		err = sock_error(sk);
		if (err)
3091
			break;
3092

3093
		if (l2cap_try_push_rx_skb(chan) == 0)
3094 3095 3096 3097
			break;
	}

	set_current_state(TASK_RUNNING);
3098
	remove_wait_queue(sk_sleep(sk), &wait);
3099 3100 3101 3102

	release_sock(sk);
}

3103
static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
3104 3105 3106
{
	int sctrl, err;

3107
	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
3108
		bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3109
		__skb_queue_tail(&chan->busy_q, skb);
3110
		return l2cap_try_push_rx_skb(chan);
3111 3112


3113 3114
	}

3115
	err = l2cap_ertm_reassembly_sdu(chan, skb, control);
3116
	if (err >= 0) {
3117
		chan->buffer_seq = (chan->buffer_seq + 1) % 64;
3118 3119 3120 3121
		return err;
	}

	/* Busy Condition */
3122
	BT_DBG("chan %p, Enter local busy", chan);
3123

3124
	chan->conn_state |= L2CAP_CONN_LOCAL_BUSY;
3125
	bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3126
	__skb_queue_tail(&chan->busy_q, skb);
3127

3128
	sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3129
	sctrl |= L2CAP_SUPER_RCV_NOT_READY;
3130
	l2cap_send_sframe(chan, sctrl);
3131

3132
	chan->conn_state |= L2CAP_CONN_RNR_SENT;
3133

3134
	del_timer(&chan->ack_timer);
3135

3136
	queue_work(_busy_wq, &chan->busy_work);
3137 3138 3139 3140

	return err;
}

3141
static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
3142 3143 3144 3145
{
	struct sk_buff *_skb;
	int err = -EINVAL;

3146 3147 3148 3149 3150
	/*
	 * TODO: We have to notify the userland if some data is lost with the
	 * Streaming Mode.
	 */

3151 3152
	switch (control & L2CAP_CTRL_SAR) {
	case L2CAP_SDU_UNSEGMENTED:
3153
		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
3154
			kfree_skb(chan->sdu);
3155 3156 3157
			break;
		}

3158
		err = sock_queue_rcv_skb(chan->sk, skb);
3159 3160 3161 3162 3163 3164
		if (!err)
			return 0;

		break;

	case L2CAP_SDU_START:
3165
		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
3166
			kfree_skb(chan->sdu);
3167 3168 3169
			break;
		}

3170
		chan->sdu_len = get_unaligned_le16(skb->data);
3171 3172
		skb_pull(skb, 2);

3173
		if (chan->sdu_len > chan->imtu) {
3174 3175 3176 3177
			err = -EMSGSIZE;
			break;
		}

3178 3179
		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
		if (!chan->sdu) {
3180 3181 3182 3183
			err = -ENOMEM;
			break;
		}

3184
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3185

3186
		chan->conn_state |= L2CAP_CONN_SAR_SDU;
3187
		chan->partial_sdu_len = skb->len;
3188 3189 3190 3191
		err = 0;
		break;

	case L2CAP_SDU_CONTINUE:
3192
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
3193 3194
			break;

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

3197 3198 3199
		chan->partial_sdu_len += skb->len;
		if (chan->partial_sdu_len > chan->sdu_len)
			kfree_skb(chan->sdu);
3200 3201 3202 3203 3204 3205
		else
			err = 0;

		break;

	case L2CAP_SDU_END:
3206
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
3207 3208
			break;

3209
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3210

3211
		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
3212
		chan->partial_sdu_len += skb->len;
3213

3214
		if (chan->partial_sdu_len > chan->imtu)
3215 3216
			goto drop;

3217 3218
		if (chan->partial_sdu_len == chan->sdu_len) {
			_skb = skb_clone(chan->sdu, GFP_ATOMIC);
3219
			err = sock_queue_rcv_skb(chan->sk, _skb);
3220 3221 3222 3223 3224
			if (err < 0)
				kfree_skb(_skb);
		}
		err = 0;

3225
drop:
3226
		kfree_skb(chan->sdu);
3227 3228 3229 3230 3231 3232 3233
		break;
	}

	kfree_skb(skb);
	return err;
}

3234
static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq)
3235 3236
{
	struct sk_buff *skb;
3237
	u16 control;
3238

3239
	while ((skb = skb_peek(&chan->srej_q))) {
3240 3241 3242
		if (bt_cb(skb)->tx_seq != tx_seq)
			break;

3243
		skb = skb_dequeue(&chan->srej_q);
3244
		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3245
		l2cap_ertm_reassembly_sdu(chan, skb, control);
3246 3247
		chan->buffer_seq_srej =
			(chan->buffer_seq_srej + 1) % 64;
3248
		tx_seq = (tx_seq + 1) % 64;
3249 3250 3251
	}
}

3252
static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq)
3253 3254 3255 3256
{
	struct srej_list *l, *tmp;
	u16 control;

3257
	list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
3258 3259 3260 3261 3262 3263 3264
		if (l->tx_seq == tx_seq) {
			list_del(&l->list);
			kfree(l);
			return;
		}
		control = L2CAP_SUPER_SELECT_REJECT;
		control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3265
		l2cap_send_sframe(chan, control);
3266
		list_del(&l->list);
3267
		list_add_tail(&l->list, &chan->srej_l);
3268 3269 3270
	}
}

3271
static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq)
3272 3273 3274 3275
{
	struct srej_list *new;
	u16 control;

3276
	while (tx_seq != chan->expected_tx_seq) {
3277
		control = L2CAP_SUPER_SELECT_REJECT;
3278
		control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3279
		l2cap_send_sframe(chan, control);
3280 3281

		new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC);
3282 3283
		new->tx_seq = chan->expected_tx_seq;
		chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3284
		list_add_tail(&new->list, &chan->srej_l);
3285
	}
3286
	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3287 3288
}

3289
static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
3290 3291
{
	u8 tx_seq = __get_txseq(rx_control);
3292
	u8 req_seq = __get_reqseq(rx_control);
3293
	u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT;
3294
	int tx_seq_offset, expected_tx_seq_offset;
3295
	int num_to_ack = (chan->tx_win/6) + 1;
3296 3297
	int err = 0;

3298 3299
	BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len,
							tx_seq, rx_control);
3300

3301
	if (L2CAP_CTRL_FINAL & rx_control &&
3302
			chan->conn_state & L2CAP_CONN_WAIT_F) {
3303
		del_timer(&chan->monitor_timer);
3304
		if (chan->unacked_frames > 0)
3305
			__mod_retrans_timer();
3306
		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
3307 3308
	}

3309 3310
	chan->expected_ack_seq = req_seq;
	l2cap_drop_acked_frames(chan);
3311

3312
	if (tx_seq == chan->expected_tx_seq)
3313
		goto expected;
3314

3315
	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
3316 3317 3318 3319
	if (tx_seq_offset < 0)
		tx_seq_offset += 64;

	/* invalid tx_seq */
3320
	if (tx_seq_offset >= chan->tx_win) {
3321
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3322 3323 3324
		goto drop;
	}

3325
	if (chan->conn_state == L2CAP_CONN_LOCAL_BUSY)
3326 3327
		goto drop;

3328
	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
3329
		struct srej_list *first;
3330

3331
		first = list_first_entry(&chan->srej_l,
3332 3333
				struct srej_list, list);
		if (tx_seq == first->tx_seq) {
3334
			l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3335
			l2cap_check_srej_gap(chan, tx_seq);
3336 3337 3338 3339

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

3340
			if (list_empty(&chan->srej_l)) {
3341
				chan->buffer_seq = chan->buffer_seq_srej;
3342 3343
				chan->conn_state &= ~L2CAP_CONN_SREJ_SENT;
				l2cap_send_ack(chan);
3344
				BT_DBG("chan %p, Exit SREJ_SENT", chan);
3345 3346 3347
			}
		} else {
			struct srej_list *l;
3348 3349

			/* duplicated tx_seq */
3350
			if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0)
3351
				goto drop;
3352

3353
			list_for_each_entry(l, &chan->srej_l, list) {
3354
				if (l->tx_seq == tx_seq) {
3355
					l2cap_resend_srejframe(chan, tx_seq);
3356 3357 3358
					return 0;
				}
			}
3359
			l2cap_send_srejframe(chan, tx_seq);
3360 3361
		}
	} else {
3362
		expected_tx_seq_offset =
3363
			(chan->expected_tx_seq - chan->buffer_seq) % 64;
3364 3365 3366 3367 3368 3369 3370
		if (expected_tx_seq_offset < 0)
			expected_tx_seq_offset += 64;

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

3371
		chan->conn_state |= L2CAP_CONN_SREJ_SENT;
3372

3373
		BT_DBG("chan %p, Enter SREJ", chan);
3374

3375
		INIT_LIST_HEAD(&chan->srej_l);
3376
		chan->buffer_seq_srej = chan->buffer_seq;
3377

3378 3379
		__skb_queue_head_init(&chan->srej_q);
		__skb_queue_head_init(&chan->busy_q);
3380
		l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3381

3382
		chan->conn_state |= L2CAP_CONN_SEND_PBIT;
3383

3384
		l2cap_send_srejframe(chan, tx_seq);
3385

3386
		del_timer(&chan->ack_timer);
3387
	}
3388 3389
	return 0;

3390
expected:
3391
	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3392

3393
	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
3394 3395
		bt_cb(skb)->tx_seq = tx_seq;
		bt_cb(skb)->sar = sar;
3396
		__skb_queue_tail(&chan->srej_q, skb);
3397 3398 3399
		return 0;
	}

3400
	err = l2cap_push_rx_skb(chan, skb, rx_control);
3401 3402 3403
	if (err < 0)
		return 0;

3404
	if (rx_control & L2CAP_CTRL_FINAL) {
3405 3406
		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
3407
		else
3408
			l2cap_retransmit_frames(chan);
3409 3410
	}

3411 3412
	__mod_ack_timer();

3413 3414
	chan->num_acked = (chan->num_acked + 1) % num_to_ack;
	if (chan->num_acked == num_to_ack - 1)
3415
		l2cap_send_ack(chan);
3416

3417
	return 0;
3418 3419 3420 3421

drop:
	kfree_skb(skb);
	return 0;
3422 3423
}

3424
static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control)
3425
{
3426
	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control),
3427 3428
						rx_control);

3429 3430
	chan->expected_ack_seq = __get_reqseq(rx_control);
	l2cap_drop_acked_frames(chan);
3431

3432
	if (rx_control & L2CAP_CTRL_POLL) {
3433 3434 3435
		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
		if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
			if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
3436
					(chan->unacked_frames > 0))
3437 3438
				__mod_retrans_timer();

3439 3440
			chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
			l2cap_send_srejtail(chan);
3441
		} else {
3442
			l2cap_send_i_or_rr_or_rnr(chan);
3443
		}
3444

3445
	} else if (rx_control & L2CAP_CTRL_FINAL) {
3446
		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3447

3448 3449
		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
3450
		else
3451
			l2cap_retransmit_frames(chan);
3452

3453
	} else {
3454
		if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
3455
				(chan->unacked_frames > 0))
3456
			__mod_retrans_timer();
3457

3458 3459 3460
		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
		if (chan->conn_state & L2CAP_CONN_SREJ_SENT)
			l2cap_send_ack(chan);
3461
		else
3462
			l2cap_ertm_send(chan);
3463 3464
	}
}
3465

3466
static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control)
3467 3468
{
	u8 tx_seq = __get_reqseq(rx_control);
3469

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

3472
	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3473

3474 3475
	chan->expected_ack_seq = tx_seq;
	l2cap_drop_acked_frames(chan);
3476 3477

	if (rx_control & L2CAP_CTRL_FINAL) {
3478 3479
		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
3480
		else
3481
			l2cap_retransmit_frames(chan);
3482
	} else {
3483
		l2cap_retransmit_frames(chan);
3484

3485 3486
		if (chan->conn_state & L2CAP_CONN_WAIT_F)
			chan->conn_state |= L2CAP_CONN_REJ_ACT;
3487 3488
	}
}
3489
static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control)
3490 3491
{
	u8 tx_seq = __get_reqseq(rx_control);
3492

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

3495
	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3496

3497
	if (rx_control & L2CAP_CTRL_POLL) {
3498 3499
		chan->expected_ack_seq = tx_seq;
		l2cap_drop_acked_frames(chan);
3500

3501 3502
		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
		l2cap_retransmit_one_frame(chan, tx_seq);
3503

3504
		l2cap_ertm_send(chan);
3505

3506
		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
3507
			chan->srej_save_reqseq = tx_seq;
3508
			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
3509
		}
3510
	} else if (rx_control & L2CAP_CTRL_FINAL) {
3511
		if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) &&
3512
				chan->srej_save_reqseq == tx_seq)
3513
			chan->conn_state &= ~L2CAP_CONN_SREJ_ACT;
3514
		else
3515
			l2cap_retransmit_one_frame(chan, tx_seq);
3516
	} else {
3517 3518
		l2cap_retransmit_one_frame(chan, tx_seq);
		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
3519
			chan->srej_save_reqseq = tx_seq;
3520
			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
3521
		}
3522 3523 3524
	}
}

3525
static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control)
3526 3527 3528
{
	u8 tx_seq = __get_reqseq(rx_control);

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

3531
	chan->conn_state |= L2CAP_CONN_REMOTE_BUSY;
3532 3533
	chan->expected_ack_seq = tx_seq;
	l2cap_drop_acked_frames(chan);
3534

3535
	if (rx_control & L2CAP_CTRL_POLL)
3536
		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
3537

3538
	if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) {
3539
		del_timer(&chan->retrans_timer);
3540
		if (rx_control & L2CAP_CTRL_POLL)
3541
			l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL);
3542
		return;
3543
	}
3544 3545

	if (rx_control & L2CAP_CTRL_POLL)
3546
		l2cap_send_srejtail(chan);
3547
	else
3548
		l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY);
3549 3550
}

3551
static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
3552
{
3553
	BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len);
3554

3555
	if (L2CAP_CTRL_FINAL & rx_control &&
3556
			chan->conn_state & L2CAP_CONN_WAIT_F) {
3557
		del_timer(&chan->monitor_timer);
3558
		if (chan->unacked_frames > 0)
3559
			__mod_retrans_timer();
3560
		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
3561 3562 3563 3564
	}

	switch (rx_control & L2CAP_CTRL_SUPERVISE) {
	case L2CAP_SUPER_RCV_READY:
3565
		l2cap_data_channel_rrframe(chan, rx_control);
3566 3567
		break;

3568
	case L2CAP_SUPER_REJECT:
3569
		l2cap_data_channel_rejframe(chan, rx_control);
3570
		break;
3571

3572
	case L2CAP_SUPER_SELECT_REJECT:
3573
		l2cap_data_channel_srejframe(chan, rx_control);
3574 3575 3576
		break;

	case L2CAP_SUPER_RCV_NOT_READY:
3577
		l2cap_data_channel_rnrframe(chan, rx_control);
3578 3579 3580
		break;
	}

3581
	kfree_skb(skb);
3582 3583 3584
	return 0;
}

3585 3586
static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb)
{
3587
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600
	u16 control;
	u8 req_seq;
	int len, next_tx_seq_offset, req_seq_offset;

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

	/*
	 * We can just drop the corrupted I-frame here.
	 * Receiver will miss it and start proper recovery
	 * procedures and ask retransmission.
	 */
3601
	if (l2cap_check_fcs(chan, skb))
3602 3603 3604 3605 3606
		goto drop;

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

3607
	if (chan->fcs == L2CAP_FCS_CRC16)
3608 3609
		len -= 2;

3610
	if (len > chan->mps) {
3611
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3612 3613 3614 3615
		goto drop;
	}

	req_seq = __get_reqseq(control);
3616
	req_seq_offset = (req_seq - chan->expected_ack_seq) % 64;
3617 3618 3619 3620
	if (req_seq_offset < 0)
		req_seq_offset += 64;

	next_tx_seq_offset =
3621
		(chan->next_tx_seq - chan->expected_ack_seq) % 64;
3622 3623 3624 3625 3626
	if (next_tx_seq_offset < 0)
		next_tx_seq_offset += 64;

	/* check for invalid req-seq */
	if (req_seq_offset > next_tx_seq_offset) {
3627
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3628 3629 3630 3631 3632
		goto drop;
	}

	if (__is_iframe(control)) {
		if (len < 0) {
3633
			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3634 3635 3636
			goto drop;
		}

3637
		l2cap_data_channel_iframe(chan, control, skb);
3638 3639 3640
	} else {
		if (len != 0) {
			BT_ERR("%d", len);
3641
			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3642 3643 3644
			goto drop;
		}

3645
		l2cap_data_channel_sframe(chan, control, skb);
3646 3647 3648 3649 3650 3651 3652 3653 3654
	}

	return 0;

drop:
	kfree_skb(skb);
	return 0;
}

L
Linus Torvalds 已提交
3655 3656
static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
{
3657
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
3658
	struct sock *sk;
3659
	struct l2cap_pinfo *pi;
3660
	u16 control;
3661 3662
	u8 tx_seq;
	int len;
L
Linus Torvalds 已提交
3663

3664
	chan = l2cap_get_chan_by_scid(conn, cid);
3665
	if (!chan) {
L
Linus Torvalds 已提交
3666 3667 3668 3669
		BT_DBG("unknown cid 0x%4.4x", cid);
		goto drop;
	}

3670
	sk = chan->sk;
3671 3672
	pi = l2cap_pi(sk);

3673
	BT_DBG("chan %p, len %d", chan, skb->len);
L
Linus Torvalds 已提交
3674 3675 3676 3677

	if (sk->sk_state != BT_CONNECTED)
		goto drop;

3678
	switch (chan->mode) {
3679 3680 3681 3682 3683
	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 已提交
3684

3685
		if (chan->imtu < skb->len)
3686
			goto drop;
L
Linus Torvalds 已提交
3687

3688 3689 3690 3691 3692
		if (!sock_queue_rcv_skb(sk, skb))
			goto done;
		break;

	case L2CAP_MODE_ERTM:
3693 3694
		if (!sock_owned_by_user(sk)) {
			l2cap_ertm_data_rcv(sk, skb);
3695
		} else {
3696
			if (sk_add_backlog(sk, skb))
3697 3698
				goto drop;
		}
3699

3700
		goto done;
3701

3702 3703 3704 3705 3706
	case L2CAP_MODE_STREAMING:
		control = get_unaligned_le16(skb->data);
		skb_pull(skb, 2);
		len = skb->len;

3707
		if (l2cap_check_fcs(chan, skb))
3708 3709
			goto drop;

3710 3711 3712
		if (__is_sar_start(control))
			len -= 2;

3713
		if (chan->fcs == L2CAP_FCS_CRC16)
3714 3715
			len -= 2;

3716
		if (len > chan->mps || len < 0 || __is_sframe(control))
3717 3718 3719 3720
			goto drop;

		tx_seq = __get_txseq(control);

3721 3722
		if (chan->expected_tx_seq == tx_seq)
			chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3723
		else
3724
			chan->expected_tx_seq = (tx_seq + 1) % 64;
3725

3726
		l2cap_streaming_reassembly_sdu(chan, skb, control);
3727 3728 3729

		goto done;

3730
	default:
3731
		BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
3732 3733
		break;
	}
L
Linus Torvalds 已提交
3734 3735 3736 3737 3738

drop:
	kfree_skb(skb);

done:
3739 3740 3741
	if (sk)
		bh_unlock_sock(sk);

L
Linus Torvalds 已提交
3742 3743 3744
	return 0;
}

3745
static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
L
Linus Torvalds 已提交
3746 3747 3748 3749 3750 3751 3752
{
	struct sock *sk;

	sk = l2cap_get_sock_by_psm(0, psm, conn->src);
	if (!sk)
		goto drop;

3753 3754
	bh_lock_sock(sk);

L
Linus Torvalds 已提交
3755 3756 3757 3758 3759
	BT_DBG("sk %p, len %d", sk, skb->len);

	if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED)
		goto drop;

3760
	if (l2cap_pi(sk)->chan->imtu < skb->len)
L
Linus Torvalds 已提交
3761 3762 3763 3764 3765 3766 3767 3768 3769
		goto drop;

	if (!sock_queue_rcv_skb(sk, skb))
		goto done;

drop:
	kfree_skb(skb);

done:
3770 3771
	if (sk)
		bh_unlock_sock(sk);
L
Linus Torvalds 已提交
3772 3773 3774
	return 0;
}

3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789
static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb)
{
	struct sock *sk;

	sk = l2cap_get_sock_by_scid(0, cid, conn->src);
	if (!sk)
		goto drop;

	bh_lock_sock(sk);

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

	if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED)
		goto drop;

3790
	if (l2cap_pi(sk)->chan->imtu < skb->len)
3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804
		goto drop;

	if (!sock_queue_rcv_skb(sk, skb))
		goto done;

drop:
	kfree_skb(skb);

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

L
Linus Torvalds 已提交
3805 3806 3807
static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
{
	struct l2cap_hdr *lh = (void *) skb->data;
3808 3809
	u16 cid, len;
	__le16 psm;
L
Linus Torvalds 已提交
3810 3811 3812 3813 3814

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

3815 3816 3817 3818 3819
	if (len != skb->len) {
		kfree_skb(skb);
		return;
	}

L
Linus Torvalds 已提交
3820 3821 3822
	BT_DBG("len %d, cid 0x%4.4x", len, cid);

	switch (cid) {
3823
	case L2CAP_CID_LE_SIGNALING:
3824
	case L2CAP_CID_SIGNALING:
L
Linus Torvalds 已提交
3825 3826 3827
		l2cap_sig_channel(conn, skb);
		break;

3828
	case L2CAP_CID_CONN_LESS:
3829
		psm = get_unaligned_le16(skb->data);
L
Linus Torvalds 已提交
3830 3831 3832 3833
		skb_pull(skb, 2);
		l2cap_conless_channel(conn, psm, skb);
		break;

3834 3835 3836 3837
	case L2CAP_CID_LE_DATA:
		l2cap_att_channel(conn, cid, skb);
		break;

L
Linus Torvalds 已提交
3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852
	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;
	register struct sock *sk;
	struct hlist_node *node;

	if (type != ACL_LINK)
3853
		return -EINVAL;
L
Linus Torvalds 已提交
3854 3855 3856 3857 3858 3859

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

	/* Find listening sockets and check their link_mode */
	read_lock(&l2cap_sk_list.lock);
	sk_for_each(sk, node, &l2cap_sk_list.head) {
3860 3861
		struct l2cap_chan *chan = l2cap_pi(sk)->chan;

L
Linus Torvalds 已提交
3862 3863 3864 3865
		if (sk->sk_state != BT_LISTEN)
			continue;

		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
3866
			lm1 |= HCI_LM_ACCEPT;
3867
			if (chan->role_switch)
3868
				lm1 |= HCI_LM_MASTER;
L
Linus Torvalds 已提交
3869
			exact++;
3870 3871
		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
			lm2 |= HCI_LM_ACCEPT;
3872
			if (chan->role_switch)
3873 3874
				lm2 |= HCI_LM_MASTER;
		}
L
Linus Torvalds 已提交
3875 3876 3877 3878 3879 3880 3881 3882
	}
	read_unlock(&l2cap_sk_list.lock);

	return exact ? lm1 : lm2;
}

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

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

3887
	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
3888
		return -EINVAL;
L
Linus Torvalds 已提交
3889 3890 3891 3892 3893

	if (!status) {
		conn = l2cap_conn_add(hcon, status);
		if (conn)
			l2cap_conn_ready(conn);
3894
	} else
L
Linus Torvalds 已提交
3895 3896 3897 3898 3899
		l2cap_conn_del(hcon, bt_err(status));

	return 0;
}

3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912
static int l2cap_disconn_ind(struct hci_conn *hcon)
{
	struct l2cap_conn *conn = hcon->l2cap_data;

	BT_DBG("hcon %p", hcon);

	if (hcon->type != ACL_LINK || !conn)
		return 0x13;

	return conn->disc_reason;
}

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

3916
	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
3917
		return -EINVAL;
L
Linus Torvalds 已提交
3918 3919

	l2cap_conn_del(hcon, bt_err(reason));
3920

L
Linus Torvalds 已提交
3921 3922 3923
	return 0;
}

3924
static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
3925
{
3926 3927
	struct sock *sk = chan->sk;

3928
	if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM)
3929 3930
		return;

3931
	if (encrypt == 0x00) {
3932
		if (chan->sec_level == BT_SECURITY_MEDIUM) {
3933 3934
			l2cap_sock_clear_timer(sk);
			l2cap_sock_set_timer(sk, HZ * 5);
3935
		} else if (chan->sec_level == BT_SECURITY_HIGH)
3936 3937
			__l2cap_sock_close(sk, ECONNREFUSED);
	} else {
3938
		if (chan->sec_level == BT_SECURITY_MEDIUM)
3939 3940 3941 3942
			l2cap_sock_clear_timer(sk);
	}
}

3943
static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
L
Linus Torvalds 已提交
3944
{
3945
	struct l2cap_conn *conn = hcon->l2cap_data;
3946
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
3947

3948
	if (!conn)
L
Linus Torvalds 已提交
3949
		return 0;
3950

L
Linus Torvalds 已提交
3951 3952
	BT_DBG("conn %p", conn);

3953
	read_lock(&conn->chan_lock);
L
Linus Torvalds 已提交
3954

3955
	list_for_each_entry(chan, &conn->chan_l, list) {
3956
		struct sock *sk = chan->sk;
3957

L
Linus Torvalds 已提交
3958 3959
		bh_lock_sock(sk);

3960
		if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) {
3961 3962 3963 3964
			bh_unlock_sock(sk);
			continue;
		}

3965
		if (!status && (sk->sk_state == BT_CONNECTED ||
3966
						sk->sk_state == BT_CONFIG)) {
3967
			l2cap_check_encryption(chan, encrypt);
3968 3969 3970 3971
			bh_unlock_sock(sk);
			continue;
		}

3972 3973 3974
		if (sk->sk_state == BT_CONNECT) {
			if (!status) {
				struct l2cap_conn_req req;
3975 3976
				req.scid = cpu_to_le16(chan->scid);
				req.psm  = chan->psm;
L
Linus Torvalds 已提交
3977

3978
				chan->ident = l2cap_get_ident(conn);
3979
				chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
L
Linus Torvalds 已提交
3980

3981
				l2cap_send_cmd(conn, chan->ident,
3982 3983 3984 3985 3986 3987 3988 3989
					L2CAP_CONN_REQ, sizeof(req), &req);
			} else {
				l2cap_sock_clear_timer(sk);
				l2cap_sock_set_timer(sk, HZ / 10);
			}
		} else if (sk->sk_state == BT_CONNECT2) {
			struct l2cap_conn_rsp rsp;
			__u16 result;
L
Linus Torvalds 已提交
3990

3991 3992 3993 3994 3995 3996 3997 3998 3999
			if (!status) {
				sk->sk_state = BT_CONFIG;
				result = L2CAP_CR_SUCCESS;
			} else {
				sk->sk_state = BT_DISCONN;
				l2cap_sock_set_timer(sk, HZ / 10);
				result = L2CAP_CR_SEC_BLOCK;
			}

4000 4001
			rsp.scid   = cpu_to_le16(chan->dcid);
			rsp.dcid   = cpu_to_le16(chan->scid);
4002
			rsp.result = cpu_to_le16(result);
4003
			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
4004 4005
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
							sizeof(rsp), &rsp);
4006
		}
L
Linus Torvalds 已提交
4007 4008 4009 4010

		bh_unlock_sock(sk);
	}

4011
	read_unlock(&conn->chan_lock);
4012

L
Linus Torvalds 已提交
4013 4014 4015 4016 4017 4018 4019
	return 0;
}

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

4020 4021 4022 4023
	if (!conn)
		conn = l2cap_conn_add(hcon, 0);

	if (!conn)
L
Linus Torvalds 已提交
4024 4025 4026 4027
		goto drop;

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

4028
	if (!(flags & ACL_CONT)) {
L
Linus Torvalds 已提交
4029
		struct l2cap_hdr *hdr;
4030
		struct l2cap_chan *chan;
4031
		u16 cid;
L
Linus Torvalds 已提交
4032 4033 4034 4035 4036 4037 4038 4039 4040 4041
		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);
		}

4042 4043
		/* Start fragment always begin with Basic L2CAP header */
		if (skb->len < L2CAP_HDR_SIZE) {
L
Linus Torvalds 已提交
4044 4045 4046 4047 4048 4049 4050
			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;
4051
		cid = __le16_to_cpu(hdr->cid);
L
Linus Torvalds 已提交
4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067

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

4068
		chan = l2cap_get_chan_by_scid(conn, cid);
4069

4070 4071
		if (chan && chan->sk) {
			struct sock *sk = chan->sk;
4072

4073
			if (chan->imtu < len - L2CAP_HDR_SIZE) {
4074 4075
				BT_ERR("Frame exceeding recv MTU (len %d, "
							"MTU %d)", len,
4076
							chan->imtu);
4077 4078 4079 4080
				bh_unlock_sock(sk);
				l2cap_conn_unreliable(conn, ECOMM);
				goto drop;
			}
4081
			bh_unlock_sock(sk);
4082
		}
4083

L
Linus Torvalds 已提交
4084
		/* Allocate skb for the complete frame (with header) */
4085 4086
		conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
		if (!conn->rx_skb)
L
Linus Torvalds 已提交
4087 4088
			goto drop;

4089
		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
4090
								skb->len);
L
Linus Torvalds 已提交
4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110
		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;
		}

4111
		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
4112
								skb->len);
L
Linus Torvalds 已提交
4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126
		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;
}

4127
static int l2cap_debugfs_show(struct seq_file *f, void *p)
L
Linus Torvalds 已提交
4128 4129 4130 4131 4132 4133
{
	struct sock *sk;
	struct hlist_node *node;

	read_lock_bh(&l2cap_sk_list.lock);

4134 4135
	sk_for_each(sk, node, &l2cap_sk_list.head) {
		struct l2cap_pinfo *pi = l2cap_pi(sk);
4136
		struct l2cap_chan *chan = pi->chan;
4137

4138
		seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
4139 4140
					batostr(&bt_sk(sk)->src),
					batostr(&bt_sk(sk)->dst),
4141 4142
					sk->sk_state, __le16_to_cpu(chan->psm),
					chan->scid, chan->dcid,
4143 4144
					chan->imtu, chan->omtu, chan->sec_level,
					chan->mode);
4145
	}
L
Linus Torvalds 已提交
4146 4147 4148

	read_unlock_bh(&l2cap_sk_list.lock);

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

4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164
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 已提交
4165 4166 4167 4168 4169 4170 4171

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,
4172
	.disconn_cfm	= l2cap_disconn_cfm,
4173
	.security_cfm	= l2cap_security_cfm,
L
Linus Torvalds 已提交
4174 4175 4176
	.recv_acldata	= l2cap_recv_acldata
};

4177
int __init l2cap_init(void)
L
Linus Torvalds 已提交
4178 4179
{
	int err;
4180

4181
	err = l2cap_init_sockets();
L
Linus Torvalds 已提交
4182 4183 4184
	if (err < 0)
		return err;

4185
	_busy_wq = create_singlethread_workqueue("l2cap");
4186
	if (!_busy_wq) {
4187
		err = -ENOMEM;
L
Linus Torvalds 已提交
4188 4189 4190 4191 4192 4193 4194 4195 4196 4197
		goto error;
	}

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

4198 4199 4200 4201 4202 4203
	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 已提交
4204 4205 4206 4207

	return 0;

error:
4208
	destroy_workqueue(_busy_wq);
4209
	l2cap_cleanup_sockets();
L
Linus Torvalds 已提交
4210 4211 4212
	return err;
}

4213
void l2cap_exit(void)
L
Linus Torvalds 已提交
4214
{
4215
	debugfs_remove(l2cap_debugfs);
L
Linus Torvalds 已提交
4216

4217 4218 4219
	flush_workqueue(_busy_wq);
	destroy_workqueue(_busy_wq);

L
Linus Torvalds 已提交
4220 4221 4222
	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
		BT_ERR("L2CAP protocol unregistration failed");

4223
	l2cap_cleanup_sockets();
L
Linus Torvalds 已提交
4224 4225
}

4226 4227
module_param(disable_ertm, bool, 0644);
MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");