l2cap_core.c 92.9 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 u16 l2cap_alloc_cid(struct l2cap_conn *conn)
139
{
140
	u16 cid = L2CAP_CID_DYN_START;
141

142
	for (; cid < L2CAP_CID_DYN_END; cid++) {
143
		if (!__l2cap_get_chan_by_scid(conn, cid))
144 145 146 147 148 149
			return cid;
	}

	return 0;
}

150
struct l2cap_chan *l2cap_chan_alloc(struct sock *sk)
151 152 153 154 155 156 157 158 159 160 161 162 163
{
	struct l2cap_chan *chan;

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

	chan->sk = sk;

	return chan;
}

static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
164
{
165
	struct sock *sk = chan->sk;
166

167
	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
168
			chan->psm, chan->dcid);
169

170 171
	conn->disc_reason = 0x13;

172
	chan->conn = conn;
173

174
	if (sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM) {
175 176
		if (conn->hcon->type == LE_LINK) {
			/* LE connection */
177
			chan->omtu = L2CAP_LE_DEFAULT_MTU;
178 179
			chan->scid = L2CAP_CID_LE_DATA;
			chan->dcid = L2CAP_CID_LE_DATA;
180 181
		} else {
			/* Alloc CID for connection-oriented socket */
182
			chan->scid = l2cap_alloc_cid(conn);
183
			chan->omtu = L2CAP_DEFAULT_MTU;
184
		}
185 186
	} else if (sk->sk_type == SOCK_DGRAM) {
		/* Connectionless socket */
187 188
		chan->scid = L2CAP_CID_CONN_LESS;
		chan->dcid = L2CAP_CID_CONN_LESS;
189
		chan->omtu = L2CAP_DEFAULT_MTU;
190 191
	} else {
		/* Raw socket can send/recv signalling messages only */
192 193
		chan->scid = L2CAP_CID_SIGNALING;
		chan->dcid = L2CAP_CID_SIGNALING;
194
		chan->omtu = L2CAP_DEFAULT_MTU;
195 196
	}

197 198 199
	sock_hold(sk);

	list_add(&chan->list, &conn->chan_l);
200 201
}

202
/* Delete channel.
203
 * Must be called on the locked socket. */
204
void l2cap_chan_del(struct l2cap_chan *chan, int err)
205
{
206
	struct sock *sk = chan->sk;
207
	struct l2cap_conn *conn = chan->conn;
208 209 210 211
	struct sock *parent = bt_sk(sk)->parent;

	l2cap_sock_clear_timer(sk);

212
	BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
213

214
	if (conn) {
215 216 217 218 219 220
		/* Delete from channel list */
		write_lock_bh(&conn->chan_lock);
		list_del(&chan->list);
		write_unlock_bh(&conn->chan_lock);
		__sock_put(sk);

221
		chan->conn = NULL;
222 223 224
		hci_conn_put(conn->hcon);
	}

225
	sk->sk_state = BT_CLOSED;
226 227 228 229 230 231 232 233 234 235
	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);
236

237 238
	if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE &&
			chan->conf_state & L2CAP_CONF_INPUT_DONE))
239 240
		goto free;

241
	skb_queue_purge(&chan->tx_q);
242

243
	if (chan->mode == L2CAP_MODE_ERTM) {
244 245
		struct srej_list *l, *tmp;

246 247 248
		del_timer(&chan->retrans_timer);
		del_timer(&chan->monitor_timer);
		del_timer(&chan->ack_timer);
249

250 251
		skb_queue_purge(&chan->srej_q);
		skb_queue_purge(&chan->busy_q);
252

253
		list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
254 255 256 257
			list_del(&l->list);
			kfree(l);
		}
	}
258

259
free:
260
	kfree(chan);
261 262
}

263
static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
264
{
265 266
	struct sock *sk = chan->sk;

267
	if (sk->sk_type == SOCK_RAW) {
268
		switch (chan->sec_level) {
269 270 271 272 273 274 275
		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;
		}
276
	} else if (chan->psm == cpu_to_le16(0x0001)) {
277 278
		if (chan->sec_level == BT_SECURITY_LOW)
			chan->sec_level = BT_SECURITY_SDP;
279

280
		if (chan->sec_level == BT_SECURITY_HIGH)
281
			return HCI_AT_NO_BONDING_MITM;
282
		else
283
			return HCI_AT_NO_BONDING;
284
	} else {
285
		switch (chan->sec_level) {
286
		case BT_SECURITY_HIGH:
287
			return HCI_AT_GENERAL_BONDING_MITM;
288
		case BT_SECURITY_MEDIUM:
289
			return HCI_AT_GENERAL_BONDING;
290
		default:
291
			return HCI_AT_NO_BONDING;
292
		}
293
	}
294 295 296
}

/* Service level security */
297
static inline int l2cap_check_security(struct l2cap_chan *chan)
298
{
299
	struct l2cap_conn *conn = chan->conn;
300 301
	__u8 auth_type;

302
	auth_type = l2cap_get_auth_type(chan);
303

304
	return hci_conn_security(conn->hcon, chan->sec_level, auth_type);
305 306
}

307
u8 l2cap_get_ident(struct l2cap_conn *conn)
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
{
	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;
}

329
void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
330 331
{
	struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
332
	u8 flags;
333 334 335 336

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

	if (!skb)
337
		return;
338

339 340 341 342 343 344
	if (lmp_no_flush_capable(conn->hcon->hdev))
		flags = ACL_START_NO_FLUSH;
	else
		flags = ACL_START;

	hci_send_acl(conn->hcon, skb, flags);
345 346
}

347
static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control)
348 349 350
{
	struct sk_buff *skb;
	struct l2cap_hdr *lh;
351
	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);
352
	struct l2cap_conn *conn = chan->conn;
353
	struct sock *sk = (struct sock *)pi;
354
	int count, hlen = L2CAP_HDR_SIZE + 2;
355
	u8 flags;
356

357 358 359
	if (sk->sk_state != BT_CONNECTED)
		return;

360
	if (chan->fcs == L2CAP_FCS_CRC16)
361
		hlen += 2;
362

363
	BT_DBG("chan %p, control 0x%2.2x", chan, control);
364

365
	count = min_t(unsigned int, conn->mtu, hlen);
366 367
	control |= L2CAP_CTRL_FRAME_TYPE;

368
	if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
369
		control |= L2CAP_CTRL_FINAL;
370
		chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
371 372
	}

373
	if (chan->conn_state & L2CAP_CONN_SEND_PBIT) {
374
		control |= L2CAP_CTRL_POLL;
375
		chan->conn_state &= ~L2CAP_CONN_SEND_PBIT;
376 377
	}

378 379
	skb = bt_skb_alloc(count, GFP_ATOMIC);
	if (!skb)
380
		return;
381 382

	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
383
	lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
384
	lh->cid = cpu_to_le16(chan->dcid);
385 386
	put_unaligned_le16(control, skb_put(skb, 2));

387
	if (chan->fcs == L2CAP_FCS_CRC16) {
388 389 390 391
		u16 fcs = crc16(0, (u8 *)lh, count - 2);
		put_unaligned_le16(fcs, skb_put(skb, 2));
	}

392 393 394 395 396
	if (lmp_no_flush_capable(conn->hcon->hdev))
		flags = ACL_START_NO_FLUSH;
	else
		flags = ACL_START;

397
	hci_send_acl(chan->conn->hcon, skb, flags);
398 399
}

400
static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control)
401
{
402
	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
403
		control |= L2CAP_SUPER_RCV_NOT_READY;
404
		chan->conn_state |= L2CAP_CONN_RNR_SENT;
405
	} else
406 407
		control |= L2CAP_SUPER_RCV_READY;

408
	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
409

410
	l2cap_send_sframe(chan, control);
411 412
}

413
static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
414
{
415
	return !(chan->conf_state & L2CAP_CONF_CONNECT_PEND);
416 417
}

418
static void l2cap_do_start(struct l2cap_chan *chan)
419
{
420
	struct l2cap_conn *conn = chan->conn;
421 422

	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
423 424 425
		if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
			return;

426 427
		if (l2cap_check_security(chan) &&
				__l2cap_no_conn_pending(chan)) {
428
			struct l2cap_conn_req req;
429 430
			req.scid = cpu_to_le16(chan->scid);
			req.psm  = chan->psm;
431

432
			chan->ident = l2cap_get_ident(conn);
433
			chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
434

435 436
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
							sizeof(req), &req);
437
		}
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	} 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);
	}
}

453 454 455
static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
{
	u32 local_feat_mask = l2cap_feat_mask;
456
	if (!disable_ertm)
457 458 459 460 461 462 463 464 465 466 467 468
		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;
	}
}

469
void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err)
470
{
471
	struct sock *sk;
472 473
	struct l2cap_disconn_req req;

474 475 476
	if (!conn)
		return;

477 478
	sk = chan->sk;

479
	if (chan->mode == L2CAP_MODE_ERTM) {
480 481 482
		del_timer(&chan->retrans_timer);
		del_timer(&chan->monitor_timer);
		del_timer(&chan->ack_timer);
483 484
	}

485 486
	req.dcid = cpu_to_le16(chan->dcid);
	req.scid = cpu_to_le16(chan->scid);
487 488
	l2cap_send_cmd(conn, l2cap_get_ident(conn),
			L2CAP_DISCONN_REQ, sizeof(req), &req);
489 490

	sk->sk_state = BT_DISCONN;
491
	sk->sk_err = err;
492 493
}

L
Linus Torvalds 已提交
494
/* ---- L2CAP connections ---- */
495 496
static void l2cap_conn_start(struct l2cap_conn *conn)
{
497
	struct l2cap_chan *chan, *tmp;
498 499 500

	BT_DBG("conn %p", conn);

501
	read_lock(&conn->chan_lock);
502

503
	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
504
		struct sock *sk = chan->sk;
505

506 507
		bh_lock_sock(sk);

508 509
		if (sk->sk_type != SOCK_SEQPACKET &&
				sk->sk_type != SOCK_STREAM) {
510 511 512 513 514
			bh_unlock_sock(sk);
			continue;
		}

		if (sk->sk_state == BT_CONNECT) {
515
			struct l2cap_conn_req req;
516

517
			if (!l2cap_check_security(chan) ||
518
					!__l2cap_no_conn_pending(chan)) {
519 520 521
				bh_unlock_sock(sk);
				continue;
			}
522

523
			if (!l2cap_mode_supported(chan->mode,
524
					conn->feat_mask)
525
					&& chan->conf_state &
526
					L2CAP_CONF_STATE2_DEVICE) {
527 528 529 530 531
				/* __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);
532 533
				bh_unlock_sock(sk);
				continue;
534
			}
535

536 537
			req.scid = cpu_to_le16(chan->scid);
			req.psm  = chan->psm;
538

539
			chan->ident = l2cap_get_ident(conn);
540
			chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
541

542 543
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
							sizeof(req), &req);
544

545 546
		} else if (sk->sk_state == BT_CONNECT2) {
			struct l2cap_conn_rsp rsp;
547
			char buf[128];
548 549
			rsp.scid = cpu_to_le16(chan->dcid);
			rsp.dcid = cpu_to_le16(chan->scid);
550

551
			if (l2cap_check_security(chan)) {
552 553 554 555 556 557 558 559 560 561 562
				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);
				}
563 564 565 566 567
			} else {
				rsp.result = cpu_to_le16(L2CAP_CR_PEND);
				rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
			}

568 569
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
							sizeof(rsp), &rsp);
570

571
			if (chan->conf_state & L2CAP_CONF_REQ_SENT ||
572 573 574 575 576
					rsp.result != L2CAP_CR_SUCCESS) {
				bh_unlock_sock(sk);
				continue;
			}

577
			chan->conf_state |= L2CAP_CONF_REQ_SENT;
578
			l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
579 580
						l2cap_build_conf_req(chan, buf), buf);
			chan->num_conf_req++;
581 582 583 584 585
		}

		bh_unlock_sock(sk);
	}

586
	read_unlock(&conn->chan_lock);
587 588
}

589 590 591 592 593
/* 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)
{
594
	struct sock *sk = NULL, *sk1 = NULL;
595 596 597 598 599
	struct hlist_node *node;

	read_lock(&l2cap_sk_list.lock);

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

602 603 604
		if (state && sk->sk_state != state)
			continue;

605
		if (chan->scid == cid) {
606 607 608 609 610 611 612 613 614
			/* Exact match. */
			if (!bacmp(&bt_sk(sk)->src, src))
				break;

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

616 617
	read_unlock(&l2cap_sk_list.lock);

618
	return node ? sk : sk1;
619 620 621 622
}

static void l2cap_le_conn_ready(struct l2cap_conn *conn)
{
623
	struct sock *parent, *sk;
624
	struct l2cap_chan *chan;
625 626 627 628 629 630 631 632 633

	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;

634 635
	bh_lock_sock(parent);

636 637 638 639 640 641 642 643 644 645
	/* 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;

646 647 648 649 650 651
	chan = l2cap_chan_alloc(sk);
	if (!chan) {
		l2cap_sock_kill(sk);
		goto clean;
	}

652 653
	l2cap_pi(sk)->chan = chan;

654
	write_lock_bh(&conn->chan_lock);
655 656 657 658

	hci_conn_hold(conn->hcon);

	l2cap_sock_init(sk, parent);
659

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

663 664
	bt_accept_enqueue(parent, sk);

665 666
	__l2cap_chan_add(conn, chan);

667 668 669 670 671
	l2cap_sock_set_timer(sk, sk->sk_sndtimeo);

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

672
	write_unlock_bh(&conn->chan_lock);
673 674 675 676 677

clean:
	bh_unlock_sock(parent);
}

678 679
static void l2cap_conn_ready(struct l2cap_conn *conn)
{
680
	struct l2cap_chan *chan;
681

682
	BT_DBG("conn %p", conn);
683

684 685 686
	if (!conn->hcon->out && conn->hcon->type == LE_LINK)
		l2cap_le_conn_ready(conn);

687
	read_lock(&conn->chan_lock);
688

689
	list_for_each_entry(chan, &conn->chan_l, list) {
690
		struct sock *sk = chan->sk;
691

692
		bh_lock_sock(sk);
693

694 695 696 697 698 699
		if (conn->hcon->type == LE_LINK) {
			l2cap_sock_clear_timer(sk);
			sk->sk_state = BT_CONNECTED;
			sk->sk_state_change(sk);
		}

700 701
		if (sk->sk_type != SOCK_SEQPACKET &&
				sk->sk_type != SOCK_STREAM) {
702 703 704 705
			l2cap_sock_clear_timer(sk);
			sk->sk_state = BT_CONNECTED;
			sk->sk_state_change(sk);
		} else if (sk->sk_state == BT_CONNECT)
706
			l2cap_do_start(chan);
707

708
		bh_unlock_sock(sk);
709
	}
710

711
	read_unlock(&conn->chan_lock);
712 713 714 715 716
}

/* Notify sockets that we cannot guaranty reliability anymore */
static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
{
717
	struct l2cap_chan *chan;
718 719 720

	BT_DBG("conn %p", conn);

721
	read_lock(&conn->chan_lock);
722

723
	list_for_each_entry(chan, &conn->chan_l, list) {
724
		struct sock *sk = chan->sk;
725

726
		if (chan->force_reliable)
727 728 729
			sk->sk_err = err;
	}

730
	read_unlock(&conn->chan_lock);
731 732 733 734 735 736
}

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

737
	conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
738
	conn->info_ident = 0;
739

740 741 742
	l2cap_conn_start(conn);
}

L
Linus Torvalds 已提交
743 744
static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
{
745
	struct l2cap_conn *conn = hcon->l2cap_data;
L
Linus Torvalds 已提交
746

747
	if (conn || status)
L
Linus Torvalds 已提交
748 749
		return conn;

750 751
	conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
	if (!conn)
L
Linus Torvalds 已提交
752 753 754 755 756
		return NULL;

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

757 758
	BT_DBG("hcon %p conn %p", hcon, conn);

759 760 761 762 763
	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 已提交
764 765 766
	conn->src = &hcon->hdev->bdaddr;
	conn->dst = &hcon->dst;

767 768
	conn->feat_mask = 0;

L
Linus Torvalds 已提交
769
	spin_lock_init(&conn->lock);
770 771 772
	rwlock_init(&conn->chan_lock);

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

774 775
	if (hcon->type != LE_LINK)
		setup_timer(&conn->info_timer, l2cap_info_timeout,
D
Dave Young 已提交
776 777
						(unsigned long) conn);

778 779
	conn->disc_reason = 0x13;

L
Linus Torvalds 已提交
780 781 782
	return conn;
}

783
static void l2cap_conn_del(struct hci_conn *hcon, int err)
L
Linus Torvalds 已提交
784
{
785
	struct l2cap_conn *conn = hcon->l2cap_data;
786
	struct l2cap_chan *chan, *l;
L
Linus Torvalds 已提交
787 788
	struct sock *sk;

789 790
	if (!conn)
		return;
L
Linus Torvalds 已提交
791 792 793

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

794
	kfree_skb(conn->rx_skb);
L
Linus Torvalds 已提交
795 796

	/* Kill channels */
797
	list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
798
		sk = chan->sk;
L
Linus Torvalds 已提交
799
		bh_lock_sock(sk);
800
		l2cap_chan_del(chan, err);
L
Linus Torvalds 已提交
801 802 803 804
		bh_unlock_sock(sk);
		l2cap_sock_kill(sk);
	}

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

L
Linus Torvalds 已提交
808 809 810 811
	hcon->l2cap_data = NULL;
	kfree(conn);
}

812
static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
L
Linus Torvalds 已提交
813
{
814
	write_lock_bh(&conn->chan_lock);
815
	__l2cap_chan_add(conn, chan);
816
	write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
817 818 819 820 821 822 823
}

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

/* Find socket with psm and source bdaddr.
 * Returns closest match.
 */
824
static struct sock *l2cap_get_sock_by_psm(int state, __le16 psm, bdaddr_t *src)
L
Linus Torvalds 已提交
825 826 827 828
{
	struct sock *sk = NULL, *sk1 = NULL;
	struct hlist_node *node;

829 830
	read_lock(&l2cap_sk_list.lock);

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

L
Linus Torvalds 已提交
834 835 836
		if (state && sk->sk_state != state)
			continue;

837
		if (chan->psm == psm) {
L
Linus Torvalds 已提交
838 839 840 841 842 843 844 845 846 847 848
			/* 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);
849 850

	return node ? sk : sk1;
L
Linus Torvalds 已提交
851 852
}

853
int l2cap_chan_connect(struct l2cap_chan *chan)
L
Linus Torvalds 已提交
854
{
855
	struct sock *sk = chan->sk;
L
Linus Torvalds 已提交
856 857 858 859 860
	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;
861
	__u8 auth_type;
862
	int err;
L
Linus Torvalds 已提交
863

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

867 868
	hdev = hci_get_route(dst, src);
	if (!hdev)
L
Linus Torvalds 已提交
869 870 871 872
		return -EHOSTUNREACH;

	hci_dev_lock_bh(hdev);

873
	auth_type = l2cap_get_auth_type(chan);
874

875
	if (chan->dcid == L2CAP_CID_LE_DATA)
876
		hcon = hci_connect(hdev, LE_LINK, dst,
877
					chan->sec_level, auth_type);
878 879
	else
		hcon = hci_connect(hdev, ACL_LINK, dst,
880
					chan->sec_level, auth_type);
881

882 883
	if (IS_ERR(hcon)) {
		err = PTR_ERR(hcon);
L
Linus Torvalds 已提交
884
		goto done;
885
	}
L
Linus Torvalds 已提交
886 887 888 889

	conn = l2cap_conn_add(hcon, 0);
	if (!conn) {
		hci_conn_put(hcon);
890
		err = -ENOMEM;
L
Linus Torvalds 已提交
891 892 893 894 895 896
		goto done;
	}

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

897 898
	l2cap_chan_add(conn, chan);

L
Linus Torvalds 已提交
899 900 901 902
	sk->sk_state = BT_CONNECT;
	l2cap_sock_set_timer(sk, sk->sk_sndtimeo);

	if (hcon->state == BT_CONNECTED) {
903 904
		if (sk->sk_type != SOCK_SEQPACKET &&
				sk->sk_type != SOCK_STREAM) {
L
Linus Torvalds 已提交
905
			l2cap_sock_clear_timer(sk);
906
			if (l2cap_check_security(chan))
907
				sk->sk_state = BT_CONNECTED;
908
		} else
909
			l2cap_do_start(chan);
L
Linus Torvalds 已提交
910 911
	}

912 913
	err = 0;

L
Linus Torvalds 已提交
914 915 916 917 918 919
done:
	hci_dev_unlock_bh(hdev);
	hci_dev_put(hdev);
	return err;
}

920
int __l2cap_wait_ack(struct sock *sk)
921
{
922
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
923 924 925 926
	DECLARE_WAITQUEUE(wait, current);
	int err = 0;
	int timeo = HZ/5;

927
	add_wait_queue(sk_sleep(sk), &wait);
928
	while ((chan->unacked_frames > 0 && chan->conn)) {
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
		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);
948
	remove_wait_queue(sk_sleep(sk), &wait);
949 950 951
	return err;
}

952 953
static void l2cap_monitor_timeout(unsigned long arg)
{
954 955
	struct l2cap_chan *chan = (void *) arg;
	struct sock *sk = chan->sk;
956

957
	BT_DBG("chan %p", chan);
958

959
	bh_lock_sock(sk);
960
	if (chan->retry_count >= chan->remote_max_tx) {
961
		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
962
		bh_unlock_sock(sk);
963 964 965
		return;
	}

966
	chan->retry_count++;
967 968
	__mod_monitor_timer();

969
	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
970
	bh_unlock_sock(sk);
971 972 973 974
}

static void l2cap_retrans_timeout(unsigned long arg)
{
975 976
	struct l2cap_chan *chan = (void *) arg;
	struct sock *sk = chan->sk;
977

978
	BT_DBG("chan %p", chan);
979

980
	bh_lock_sock(sk);
981
	chan->retry_count = 1;
982 983
	__mod_monitor_timer();

984
	chan->conn_state |= L2CAP_CONN_WAIT_F;
985

986
	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
987
	bh_unlock_sock(sk);
988 989
}

990
static void l2cap_drop_acked_frames(struct l2cap_chan *chan)
L
Linus Torvalds 已提交
991
{
992
	struct sk_buff *skb;
L
Linus Torvalds 已提交
993

994
	while ((skb = skb_peek(&chan->tx_q)) &&
995
			chan->unacked_frames) {
996
		if (bt_cb(skb)->tx_seq == chan->expected_ack_seq)
997
			break;
L
Linus Torvalds 已提交
998

999
		skb = skb_dequeue(&chan->tx_q);
1000
		kfree_skb(skb);
L
Linus Torvalds 已提交
1001

1002
		chan->unacked_frames--;
1003
	}
L
Linus Torvalds 已提交
1004

1005
	if (!chan->unacked_frames)
1006
		del_timer(&chan->retrans_timer);
1007
}
L
Linus Torvalds 已提交
1008

1009
void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
1010
{
1011
	struct hci_conn *hcon = chan->conn->hcon;
1012
	u16 flags;
1013

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

1016
	if (!chan->flushable && lmp_no_flush_capable(hcon->hdev))
1017 1018 1019 1020 1021
		flags = ACL_START_NO_FLUSH;
	else
		flags = ACL_START;

	hci_send_acl(hcon, skb, flags);
1022 1023
}

1024
void l2cap_streaming_send(struct l2cap_chan *chan)
1025
{
1026
	struct sk_buff *skb;
1027
	u16 control, fcs;
1028

1029
	while ((skb = skb_dequeue(&chan->tx_q))) {
1030
		control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE);
1031
		control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT;
1032
		put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE);
1033

1034
		if (chan->fcs == L2CAP_FCS_CRC16) {
1035 1036
			fcs = crc16(0, (u8 *)skb->data, skb->len - 2);
			put_unaligned_le16(fcs, skb->data + skb->len - 2);
1037 1038
		}

1039
		l2cap_do_send(chan, skb);
1040

1041
		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
1042 1043 1044
	}
}

1045
static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq)
1046 1047 1048 1049
{
	struct sk_buff *skb, *tx_skb;
	u16 control, fcs;

1050
	skb = skb_peek(&chan->tx_q);
1051 1052
	if (!skb)
		return;
1053

1054 1055
	do {
		if (bt_cb(skb)->tx_seq == tx_seq)
1056 1057
			break;

1058
		if (skb_queue_is_last(&chan->tx_q, skb))
1059
			return;
1060

1061
	} while ((skb = skb_queue_next(&chan->tx_q, skb)));
1062

1063 1064
	if (chan->remote_max_tx &&
			bt_cb(skb)->retries == chan->remote_max_tx) {
1065
		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1066 1067 1068 1069 1070 1071
		return;
	}

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

1074
	if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
1075
		control |= L2CAP_CTRL_FINAL;
1076
		chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
1077
	}
1078

1079
	control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
1080
			| (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
1081

1082 1083
	put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);

1084
	if (chan->fcs == L2CAP_FCS_CRC16) {
1085 1086 1087 1088
		fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2);
		put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2);
	}

1089
	l2cap_do_send(chan, tx_skb);
1090 1091
}

1092
int l2cap_ertm_send(struct l2cap_chan *chan)
1093 1094
{
	struct sk_buff *skb, *tx_skb;
1095
	struct sock *sk = chan->sk;
1096
	u16 control, fcs;
1097
	int nsent = 0;
1098

1099 1100
	if (sk->sk_state != BT_CONNECTED)
		return -ENOTCONN;
1101

1102
	while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) {
1103

1104 1105
		if (chan->remote_max_tx &&
				bt_cb(skb)->retries == chan->remote_max_tx) {
1106
			l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1107 1108 1109
			break;
		}

1110 1111
		tx_skb = skb_clone(skb, GFP_ATOMIC);

1112 1113
		bt_cb(skb)->retries++;

1114
		control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
1115 1116
		control &= L2CAP_CTRL_SAR;

1117
		if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
1118
			control |= L2CAP_CTRL_FINAL;
1119
			chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
1120
		}
1121 1122
		control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
				| (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
1123 1124
		put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);

1125

1126
		if (chan->fcs == L2CAP_FCS_CRC16) {
1127 1128 1129 1130
			fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2);
			put_unaligned_le16(fcs, skb->data + tx_skb->len - 2);
		}

1131
		l2cap_do_send(chan, tx_skb);
1132

1133
		__mod_retrans_timer();
1134

1135 1136
		bt_cb(skb)->tx_seq = chan->next_tx_seq;
		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
1137

1138
		if (bt_cb(skb)->retries == 1)
1139
			chan->unacked_frames++;
1140

1141
		chan->frames_sent++;
1142

1143 1144
		if (skb_queue_is_last(&chan->tx_q, skb))
			chan->tx_send_head = NULL;
1145
		else
1146
			chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
1147 1148

		nsent++;
1149 1150
	}

1151 1152 1153
	return nsent;
}

1154
static int l2cap_retransmit_frames(struct l2cap_chan *chan)
1155 1156 1157
{
	int ret;

1158 1159
	if (!skb_queue_empty(&chan->tx_q))
		chan->tx_send_head = chan->tx_q.next;
1160

1161
	chan->next_tx_seq = chan->expected_ack_seq;
1162
	ret = l2cap_ertm_send(chan);
1163 1164 1165
	return ret;
}

1166
static void l2cap_send_ack(struct l2cap_chan *chan)
1167 1168 1169
{
	u16 control = 0;

1170
	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
1171

1172
	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
1173
		control |= L2CAP_SUPER_RCV_NOT_READY;
1174 1175
		chan->conn_state |= L2CAP_CONN_RNR_SENT;
		l2cap_send_sframe(chan, control);
1176
		return;
1177
	}
1178

1179
	if (l2cap_ertm_send(chan) > 0)
1180 1181 1182
		return;

	control |= L2CAP_SUPER_RCV_READY;
1183
	l2cap_send_sframe(chan, control);
1184 1185
}

1186
static void l2cap_send_srejtail(struct l2cap_chan *chan)
1187 1188 1189 1190 1191 1192 1193
{
	struct srej_list *tail;
	u16 control;

	control = L2CAP_SUPER_SELECT_REJECT;
	control |= L2CAP_CTRL_FINAL;

1194
	tail = list_entry((&chan->srej_l)->prev, struct srej_list, list);
1195 1196
	control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;

1197
	l2cap_send_sframe(chan, control);
1198 1199
}

1200 1201
static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb)
{
1202
	struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
1203 1204
	struct sk_buff **frag;
	int err, sent = 0;
L
Linus Torvalds 已提交
1205

1206
	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
1207
		return -EFAULT;
L
Linus Torvalds 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

	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)
1219
			return err;
1220 1221
		if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
			return -EFAULT;
L
Linus Torvalds 已提交
1222 1223 1224 1225 1226 1227 1228 1229

		sent += count;
		len  -= count;

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

	return sent;
1230
}
L
Linus Torvalds 已提交
1231

1232
struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
1233
{
1234
	struct sock *sk = chan->sk;
1235
	struct l2cap_conn *conn = chan->conn;
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
	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)
1246
		return ERR_PTR(err);
1247 1248 1249

	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1250
	lh->cid = cpu_to_le16(chan->dcid);
1251
	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
1252
	put_unaligned_le16(chan->psm, skb_put(skb, 2));
1253 1254 1255 1256 1257 1258 1259 1260 1261

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

1262
struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
1263
{
1264
	struct sock *sk = chan->sk;
1265
	struct l2cap_conn *conn = chan->conn;
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
	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)
1276
		return ERR_PTR(err);
1277 1278 1279

	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1280
	lh->cid = cpu_to_le16(chan->dcid);
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
	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;
}

1291
struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen)
1292
{
1293
	struct sock *sk = chan->sk;
1294
	struct l2cap_conn *conn = chan->conn;
1295 1296 1297 1298 1299 1300
	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);

1301 1302 1303
	if (!conn)
		return ERR_PTR(-ENOTCONN);

1304 1305 1306
	if (sdulen)
		hlen += 2;

1307
	if (chan->fcs == L2CAP_FCS_CRC16)
1308 1309
		hlen += 2;

1310 1311 1312 1313
	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)
1314
		return ERR_PTR(err);
1315 1316 1317

	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1318
	lh->cid = cpu_to_le16(chan->dcid);
1319 1320
	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
	put_unaligned_le16(control, skb_put(skb, 2));
1321 1322
	if (sdulen)
		put_unaligned_le16(sdulen, skb_put(skb, 2));
1323 1324 1325 1326 1327 1328

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

1330
	if (chan->fcs == L2CAP_FCS_CRC16)
1331 1332
		put_unaligned_le16(0, skb_put(skb, 2));

1333
	bt_cb(skb)->retries = 0;
1334
	return skb;
L
Linus Torvalds 已提交
1335 1336
}

1337
int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
1338 1339 1340 1341 1342 1343
{
	struct sk_buff *skb;
	struct sk_buff_head sar_queue;
	u16 control;
	size_t size = 0;

1344
	skb_queue_head_init(&sar_queue);
1345
	control = L2CAP_SDU_START;
1346
	skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
1347 1348 1349 1350
	if (IS_ERR(skb))
		return PTR_ERR(skb);

	__skb_queue_tail(&sar_queue, skb);
1351 1352
	len -= chan->remote_mps;
	size += chan->remote_mps;
1353 1354 1355 1356

	while (len > 0) {
		size_t buflen;

1357
		if (len > chan->remote_mps) {
1358
			control = L2CAP_SDU_CONTINUE;
1359
			buflen = chan->remote_mps;
1360
		} else {
1361
			control = L2CAP_SDU_END;
1362 1363 1364
			buflen = len;
		}

1365
		skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0);
1366 1367 1368 1369 1370 1371 1372 1373 1374
		if (IS_ERR(skb)) {
			skb_queue_purge(&sar_queue);
			return PTR_ERR(skb);
		}

		__skb_queue_tail(&sar_queue, skb);
		len -= buflen;
		size += buflen;
	}
1375 1376 1377
	skb_queue_splice_tail(&sar_queue, &chan->tx_q);
	if (chan->tx_send_head == NULL)
		chan->tx_send_head = sar_queue.next;
1378 1379 1380 1381

	return size;
}

L
Linus Torvalds 已提交
1382 1383 1384
static void l2cap_chan_ready(struct sock *sk)
{
	struct sock *parent = bt_sk(sk)->parent;
1385
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
L
Linus Torvalds 已提交
1386 1387 1388

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

1389
	chan->conf_state = 0;
L
Linus Torvalds 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
	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;
1410
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
1411 1412 1413

	BT_DBG("conn %p", conn);

1414 1415
	read_lock(&conn->chan_lock);
	list_for_each_entry(chan, &conn->chan_l, list) {
1416
		struct sock *sk = chan->sk;
L
Linus Torvalds 已提交
1417 1418 1419 1420 1421 1422
		if (sk->sk_type != SOCK_RAW)
			continue;

		/* Don't send frame to the socket it came from */
		if (skb->sk == sk)
			continue;
1423 1424
		nskb = skb_clone(skb, GFP_ATOMIC);
		if (!nskb)
L
Linus Torvalds 已提交
1425 1426 1427 1428 1429
			continue;

		if (sock_queue_rcv_skb(sk, nskb))
			kfree_skb(nskb);
	}
1430
	read_unlock(&conn->chan_lock);
L
Linus Torvalds 已提交
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
}

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

1442 1443
	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
			conn, code, ident, dlen);
L
Linus Torvalds 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452

	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);
1453
	lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
1454 1455 1456 1457 1458

	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 已提交
1459 1460 1461 1462

	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
	cmd->code  = code;
	cmd->ident = ident;
1463
	cmd->len   = cpu_to_le16(dlen);
L
Linus Torvalds 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513

	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:
1514
		*val = get_unaligned_le16(opt->val);
L
Linus Torvalds 已提交
1515 1516 1517
		break;

	case 4:
1518
		*val = get_unaligned_le32(opt->val);
L
Linus Torvalds 已提交
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
		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:
1545
		put_unaligned_le16(val, opt->val);
L
Linus Torvalds 已提交
1546 1547 1548
		break;

	case 4:
1549
		put_unaligned_le32(val, opt->val);
L
Linus Torvalds 已提交
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
		break;

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

	*ptr += L2CAP_CONF_OPT_SIZE + len;
}

1560 1561
static void l2cap_ack_timeout(unsigned long arg)
{
1562
	struct l2cap_chan *chan = (void *) arg;
1563

1564 1565 1566
	bh_lock_sock(chan->sk);
	l2cap_send_ack(chan);
	bh_unlock_sock(chan->sk);
1567 1568
}

1569
static inline void l2cap_ertm_init(struct l2cap_chan *chan)
1570
{
1571 1572
	struct sock *sk = chan->sk;

1573
	chan->expected_ack_seq = 0;
1574
	chan->unacked_frames = 0;
1575
	chan->buffer_seq = 0;
1576 1577
	chan->num_acked = 0;
	chan->frames_sent = 0;
1578

1579 1580 1581 1582 1583
	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);
1584

1585 1586
	skb_queue_head_init(&chan->srej_q);
	skb_queue_head_init(&chan->busy_q);
1587

1588 1589
	INIT_LIST_HEAD(&chan->srej_l);

1590
	INIT_WORK(&chan->busy_work, l2cap_busy_work);
1591 1592

	sk->sk_backlog_rcv = l2cap_ertm_data_rcv;
1593 1594
}

1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
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;
	}
}

1608
static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
L
Linus Torvalds 已提交
1609 1610
{
	struct l2cap_conf_req *req = data;
1611
	struct l2cap_conf_rfc rfc = { .mode = chan->mode };
L
Linus Torvalds 已提交
1612 1613
	void *ptr = req->data;

1614
	BT_DBG("chan %p", chan);
L
Linus Torvalds 已提交
1615

1616
	if (chan->num_conf_req || chan->num_conf_rsp)
1617 1618
		goto done;

1619
	switch (chan->mode) {
1620 1621
	case L2CAP_MODE_STREAMING:
	case L2CAP_MODE_ERTM:
1622
		if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE)
1623 1624
			break;

1625
		/* fall through */
1626
	default:
1627
		chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
1628 1629 1630 1631
		break;
	}

done:
1632 1633
	if (chan->imtu != L2CAP_DEFAULT_MTU)
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
1634

1635
	switch (chan->mode) {
1636
	case L2CAP_MODE_BASIC:
1637 1638
		if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
				!(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
1639 1640
			break;

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

1648 1649
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
							(unsigned long) &rfc);
1650 1651 1652 1653
		break;

	case L2CAP_MODE_ERTM:
		rfc.mode            = L2CAP_MODE_ERTM;
1654 1655
		rfc.txwin_size      = chan->tx_win;
		rfc.max_transmit    = chan->max_tx;
1656 1657
		rfc.retrans_timeout = 0;
		rfc.monitor_timeout = 0;
1658
		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
1659 1660
		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
1661

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

1665
		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
1666 1667
			break;

1668
		if (chan->fcs == L2CAP_FCS_NONE ||
1669
				chan->conf_state & L2CAP_CONF_NO_FCS_RECV) {
1670 1671
			chan->fcs = L2CAP_FCS_NONE;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
1672
		}
1673 1674 1675 1676 1677 1678 1679 1680
		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;
1681
		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
1682 1683
		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
1684

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

1688
		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
1689 1690
			break;

1691
		if (chan->fcs == L2CAP_FCS_NONE ||
1692
				chan->conf_state & L2CAP_CONF_NO_FCS_RECV) {
1693 1694
			chan->fcs = L2CAP_FCS_NONE;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
1695
		}
1696 1697
		break;
	}
L
Linus Torvalds 已提交
1698

1699
	req->dcid  = cpu_to_le16(chan->dcid);
1700
	req->flags = cpu_to_le16(0);
L
Linus Torvalds 已提交
1701 1702 1703 1704

	return ptr - data;
}

1705
static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
L
Linus Torvalds 已提交
1706
{
1707 1708
	struct l2cap_conf_rsp *rsp = data;
	void *ptr = rsp->data;
1709 1710
	void *req = chan->conf_req;
	int len = chan->conf_len;
1711 1712
	int type, hint, olen;
	unsigned long val;
1713
	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
1714
	u16 mtu = L2CAP_DEFAULT_MTU;
1715
	u16 result = L2CAP_CONF_SUCCESS;
L
Linus Torvalds 已提交
1716

1717
	BT_DBG("chan %p", chan);
1718

1719 1720
	while (len >= L2CAP_CONF_OPT_SIZE) {
		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
L
Linus Torvalds 已提交
1721

1722
		hint  = type & L2CAP_CONF_HINT;
1723
		type &= L2CAP_CONF_MASK;
1724 1725 1726

		switch (type) {
		case L2CAP_CONF_MTU:
1727
			mtu = val;
1728 1729 1730
			break;

		case L2CAP_CONF_FLUSH_TO:
1731
			chan->flush_to = val;
1732 1733 1734 1735 1736
			break;

		case L2CAP_CONF_QOS:
			break;

1737 1738 1739 1740 1741
		case L2CAP_CONF_RFC:
			if (olen == sizeof(rfc))
				memcpy(&rfc, (void *) val, olen);
			break;

1742 1743
		case L2CAP_CONF_FCS:
			if (val == L2CAP_FCS_NONE)
1744
				chan->conf_state |= L2CAP_CONF_NO_FCS_RECV;
1745 1746 1747

			break;

1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
		default:
			if (hint)
				break;

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

1758
	if (chan->num_conf_rsp || chan->num_conf_req > 1)
1759 1760
		goto done;

1761
	switch (chan->mode) {
1762 1763
	case L2CAP_MODE_STREAMING:
	case L2CAP_MODE_ERTM:
1764
		if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) {
1765
			chan->mode = l2cap_select_mode(rfc.mode,
1766
					chan->conn->feat_mask);
1767 1768 1769
			break;
		}

1770
		if (chan->mode != rfc.mode)
1771
			return -ECONNREFUSED;
1772

1773 1774 1775 1776
		break;
	}

done:
1777
	if (chan->mode != rfc.mode) {
1778
		result = L2CAP_CONF_UNACCEPT;
1779
		rfc.mode = chan->mode;
1780

1781
		if (chan->num_conf_rsp == 1)
1782 1783 1784 1785 1786 1787 1788
			return -ECONNREFUSED;

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


1789 1790 1791 1792
	if (result == L2CAP_CONF_SUCCESS) {
		/* Configure output options and let the other side know
		 * which ones we don't like. */

1793 1794 1795
		if (mtu < L2CAP_DEFAULT_MIN_MTU)
			result = L2CAP_CONF_UNACCEPT;
		else {
1796
			chan->omtu = mtu;
1797
			chan->conf_state |= L2CAP_CONF_MTU_DONE;
1798
		}
1799
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
1800

1801 1802
		switch (rfc.mode) {
		case L2CAP_MODE_BASIC:
1803
			chan->fcs = L2CAP_FCS_NONE;
1804
			chan->conf_state |= L2CAP_CONF_MODE_DONE;
1805 1806 1807
			break;

		case L2CAP_MODE_ERTM:
1808 1809
			chan->remote_tx_win = rfc.txwin_size;
			chan->remote_max_tx = rfc.max_transmit;
1810

1811 1812
			if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10)
				rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
1813

1814
			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
1815

1816 1817 1818 1819
			rfc.retrans_timeout =
				le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO);
			rfc.monitor_timeout =
				le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO);
1820

1821
			chan->conf_state |= L2CAP_CONF_MODE_DONE;
1822 1823 1824 1825

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

1826 1827 1828
			break;

		case L2CAP_MODE_STREAMING:
1829 1830
			if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10)
				rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
1831

1832
			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
1833

1834
			chan->conf_state |= L2CAP_CONF_MODE_DONE;
1835 1836 1837 1838

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

1839 1840 1841
			break;

		default:
1842 1843
			result = L2CAP_CONF_UNACCEPT;

1844
			memset(&rfc, 0, sizeof(rfc));
1845
			rfc.mode = chan->mode;
1846
		}
1847

1848
		if (result == L2CAP_CONF_SUCCESS)
1849
			chan->conf_state |= L2CAP_CONF_OUTPUT_DONE;
1850
	}
1851
	rsp->scid   = cpu_to_le16(chan->dcid);
1852 1853 1854 1855
	rsp->result = cpu_to_le16(result);
	rsp->flags  = cpu_to_le16(0x0000);

	return ptr - data;
L
Linus Torvalds 已提交
1856 1857
}

1858
static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result)
1859 1860 1861 1862 1863 1864 1865
{
	struct l2cap_conf_req *req = data;
	void *ptr = req->data;
	int type, olen;
	unsigned long val;
	struct l2cap_conf_rfc rfc;

1866
	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
1867 1868 1869 1870 1871 1872 1873 1874

	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;
1875
				chan->imtu = L2CAP_DEFAULT_MIN_MTU;
1876
			} else
1877 1878
				chan->imtu = val;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
1879 1880 1881
			break;

		case L2CAP_CONF_FLUSH_TO:
1882
			chan->flush_to = val;
1883
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
1884
							2, chan->flush_to);
1885 1886 1887 1888 1889 1890
			break;

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

1891
			if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) &&
1892
							rfc.mode != chan->mode)
1893 1894
				return -ECONNREFUSED;

1895
			chan->fcs = 0;
1896 1897 1898 1899 1900 1901 1902

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

1903
	if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
1904 1905
		return -ECONNREFUSED;

1906
	chan->mode = rfc.mode;
1907

1908 1909 1910
	if (*result == L2CAP_CONF_SUCCESS) {
		switch (rfc.mode) {
		case L2CAP_MODE_ERTM:
1911 1912 1913
			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);
1914 1915
			break;
		case L2CAP_MODE_STREAMING:
1916
			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
1917 1918 1919
		}
	}

1920
	req->dcid   = cpu_to_le16(chan->dcid);
1921 1922 1923 1924 1925
	req->flags  = cpu_to_le16(0x0000);

	return ptr - data;
}

1926
static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags)
L
Linus Torvalds 已提交
1927 1928 1929 1930
{
	struct l2cap_conf_rsp *rsp = data;
	void *ptr = rsp->data;

1931
	BT_DBG("chan %p", chan);
L
Linus Torvalds 已提交
1932

1933
	rsp->scid   = cpu_to_le16(chan->dcid);
1934
	rsp->result = cpu_to_le16(result);
1935
	rsp->flags  = cpu_to_le16(flags);
L
Linus Torvalds 已提交
1936 1937 1938 1939

	return ptr - data;
}

1940
void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
1941 1942
{
	struct l2cap_conn_rsp rsp;
1943
	struct l2cap_conn *conn = chan->conn;
1944 1945
	u8 buf[128];

1946 1947
	rsp.scid   = cpu_to_le16(chan->dcid);
	rsp.dcid   = cpu_to_le16(chan->scid);
1948 1949 1950 1951 1952
	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);

1953
	if (chan->conf_state & L2CAP_CONF_REQ_SENT)
1954 1955
		return;

1956
	chan->conf_state |= L2CAP_CONF_REQ_SENT;
1957 1958 1959 1960 1961
	l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
			l2cap_build_conf_req(chan, buf), buf);
	chan->num_conf_req++;
}

1962
static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
1963 1964 1965 1966 1967
{
	int type, olen;
	unsigned long val;
	struct l2cap_conf_rfc rfc;

1968
	BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
1969

1970
	if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
		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:
1987 1988 1989
		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);
1990 1991
		break;
	case L2CAP_MODE_STREAMING:
1992
		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
1993 1994 1995
	}
}

1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
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);
2006 2007

		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
2008
		conn->info_ident = 0;
2009

2010 2011 2012 2013 2014 2015
		l2cap_conn_start(conn);
	}

	return 0;
}

L
Linus Torvalds 已提交
2016 2017 2018 2019
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;
2020
	struct l2cap_chan *chan = NULL;
2021
	struct sock *parent, *sk = NULL;
2022
	int result, status = L2CAP_CS_NO_INFO;
L
Linus Torvalds 已提交
2023 2024

	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
2025
	__le16 psm = req->psm;
L
Linus Torvalds 已提交
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035

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

2036 2037
	bh_lock_sock(parent);

2038 2039 2040
	/* Check if the ACL is secure enough (if not SDP) */
	if (psm != cpu_to_le16(0x0001) &&
				!hci_conn_check_link_mode(conn->hcon)) {
2041
		conn->disc_reason = 0x05;
2042 2043 2044 2045
		result = L2CAP_CR_SEC_BLOCK;
		goto response;
	}

L
Linus Torvalds 已提交
2046 2047 2048 2049
	result = L2CAP_CR_NO_MEM;

	/* Check for backlog size */
	if (sk_acceptq_is_full(parent)) {
2050
		BT_DBG("backlog full %d", parent->sk_ack_backlog);
L
Linus Torvalds 已提交
2051 2052 2053
		goto response;
	}

2054
	sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC);
L
Linus Torvalds 已提交
2055 2056 2057
	if (!sk)
		goto response;

2058 2059 2060 2061 2062 2063
	chan = l2cap_chan_alloc(sk);
	if (!chan) {
		l2cap_sock_kill(sk);
		goto response;
	}

2064 2065
	l2cap_pi(sk)->chan = chan;

2066
	write_lock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2067 2068

	/* Check if we already have channel with that dcid */
2069 2070
	if (__l2cap_get_chan_by_dcid(conn, scid)) {
		write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
		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);
2081 2082
	chan->psm  = psm;
	chan->dcid = scid;
L
Linus Torvalds 已提交
2083

2084 2085
	bt_accept_enqueue(parent, sk);

2086 2087
	__l2cap_chan_add(conn, chan);

2088
	dcid = chan->scid;
L
Linus Torvalds 已提交
2089 2090 2091

	l2cap_sock_set_timer(sk, sk->sk_sndtimeo);

2092
	chan->ident = cmd->ident;
L
Linus Torvalds 已提交
2093

2094
	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
2095
		if (l2cap_check_security(chan)) {
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
			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;
			}
2106 2107 2108 2109 2110 2111 2112 2113 2114
		} 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 已提交
2115 2116
	}

2117
	write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2118 2119 2120 2121 2122

response:
	bh_unlock_sock(parent);

sendresp:
2123 2124 2125 2126
	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 已提交
2127
	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142

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

2143
	if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) &&
2144 2145
				result == L2CAP_CR_SUCCESS) {
		u8 buf[128];
2146
		chan->conf_state |= L2CAP_CONF_REQ_SENT;
2147
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2148 2149
					l2cap_build_conf_req(chan, buf), buf);
		chan->num_conf_req++;
2150 2151
	}

L
Linus Torvalds 已提交
2152 2153 2154 2155 2156 2157 2158
	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;
2159
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
	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) {
2171
		chan = l2cap_get_chan_by_scid(conn, scid);
2172
		if (!chan)
2173
			return -EFAULT;
L
Linus Torvalds 已提交
2174
	} else {
2175
		chan = l2cap_get_chan_by_ident(conn, cmd->ident);
2176
		if (!chan)
2177
			return -EFAULT;
L
Linus Torvalds 已提交
2178 2179
	}

2180 2181
	sk = chan->sk;

L
Linus Torvalds 已提交
2182 2183 2184
	switch (result) {
	case L2CAP_CR_SUCCESS:
		sk->sk_state = BT_CONFIG;
2185
		chan->ident = 0;
2186
		chan->dcid = dcid;
2187
		chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND;
2188

2189
		if (chan->conf_state & L2CAP_CONF_REQ_SENT)
2190 2191
			break;

2192
		chan->conf_state |= L2CAP_CONF_REQ_SENT;
2193

L
Linus Torvalds 已提交
2194
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2195 2196
					l2cap_build_conf_req(chan, req), req);
		chan->num_conf_req++;
L
Linus Torvalds 已提交
2197 2198 2199
		break;

	case L2CAP_CR_PEND:
2200
		chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
L
Linus Torvalds 已提交
2201 2202 2203
		break;

	default:
2204 2205 2206 2207 2208 2209 2210 2211
		/* 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;
		}

2212
		l2cap_chan_del(chan, ECONNREFUSED);
L
Linus Torvalds 已提交
2213 2214 2215 2216 2217 2218 2219
		break;
	}

	bh_unlock_sock(sk);
	return 0;
}

2220
static inline void set_default_fcs(struct l2cap_chan *chan)
2221
{
2222 2223
	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);

2224 2225 2226
	/* FCS is enabled only in ERTM or streaming mode, if one or both
	 * sides request it.
	 */
2227
	if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
2228
		chan->fcs = L2CAP_FCS_NONE;
2229
	else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV))
2230
		chan->fcs = L2CAP_FCS_CRC16;
2231 2232
}

2233
static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
L
Linus Torvalds 已提交
2234 2235 2236 2237
{
	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
	u16 dcid, flags;
	u8 rsp[64];
2238
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2239
	struct sock *sk;
2240
	int len;
L
Linus Torvalds 已提交
2241 2242 2243 2244 2245 2246

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

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

2247
	chan = l2cap_get_chan_by_scid(conn, dcid);
2248
	if (!chan)
L
Linus Torvalds 已提交
2249 2250
		return -ENOENT;

2251 2252
	sk = chan->sk;

2253 2254 2255 2256 2257 2258
	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);
2259
		goto unlock;
2260
	}
2261

2262
	/* Reject if config buffer is too small. */
2263
	len = cmd_len - sizeof(*req);
2264
	if (chan->conf_len + len > sizeof(chan->conf_req)) {
2265
		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2266
				l2cap_build_conf_rsp(chan, rsp,
2267 2268 2269 2270 2271
					L2CAP_CONF_REJECT, flags), rsp);
		goto unlock;
	}

	/* Store config. */
2272 2273
	memcpy(chan->conf_req + chan->conf_len, req->data, len);
	chan->conf_len += len;
L
Linus Torvalds 已提交
2274 2275 2276 2277

	if (flags & 0x0001) {
		/* Incomplete config. Send empty response. */
		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2278
				l2cap_build_conf_rsp(chan, rsp,
2279
					L2CAP_CONF_SUCCESS, 0x0001), rsp);
L
Linus Torvalds 已提交
2280 2281 2282 2283
		goto unlock;
	}

	/* Complete config. */
2284
	len = l2cap_parse_conf_req(chan, rsp);
2285
	if (len < 0) {
2286
		l2cap_send_disconn_req(conn, chan, ECONNRESET);
L
Linus Torvalds 已提交
2287
		goto unlock;
2288
	}
L
Linus Torvalds 已提交
2289

2290
	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
2291
	chan->num_conf_rsp++;
2292 2293

	/* Reset config buffer. */
2294
	chan->conf_len = 0;
2295

2296
	if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE))
2297 2298
		goto unlock;

2299
	if (chan->conf_state & L2CAP_CONF_INPUT_DONE) {
2300
		set_default_fcs(chan);
2301

L
Linus Torvalds 已提交
2302
		sk->sk_state = BT_CONNECTED;
2303

2304 2305
		chan->next_tx_seq = 0;
		chan->expected_tx_seq = 0;
2306
		skb_queue_head_init(&chan->tx_q);
2307
		if (chan->mode == L2CAP_MODE_ERTM)
2308
			l2cap_ertm_init(chan);
2309

L
Linus Torvalds 已提交
2310
		l2cap_chan_ready(sk);
2311 2312 2313
		goto unlock;
	}

2314
	if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) {
2315
		u8 buf[64];
2316
		chan->conf_state |= L2CAP_CONF_REQ_SENT;
L
Linus Torvalds 已提交
2317
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2318 2319
					l2cap_build_conf_req(chan, buf), buf);
		chan->num_conf_req++;
L
Linus Torvalds 已提交
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
	}

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;
2331
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2332
	struct sock *sk;
2333
	int len = cmd->len - sizeof(*rsp);
L
Linus Torvalds 已提交
2334 2335 2336 2337 2338

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

2339 2340
	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
			scid, flags, result);
L
Linus Torvalds 已提交
2341

2342
	chan = l2cap_get_chan_by_scid(conn, scid);
2343
	if (!chan)
L
Linus Torvalds 已提交
2344 2345
		return 0;

2346 2347
	sk = chan->sk;

L
Linus Torvalds 已提交
2348 2349
	switch (result) {
	case L2CAP_CONF_SUCCESS:
2350
		l2cap_conf_rfc_get(chan, rsp->data, len);
L
Linus Torvalds 已提交
2351 2352 2353
		break;

	case L2CAP_CONF_UNACCEPT:
2354
		if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
2355 2356
			char req[64];

2357
			if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
2358
				l2cap_send_disconn_req(conn, chan, ECONNRESET);
2359 2360 2361
				goto done;
			}

2362 2363
			/* throw out any old stored conf requests */
			result = L2CAP_CONF_SUCCESS;
2364 2365
			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
								req, &result);
2366
			if (len < 0) {
2367
				l2cap_send_disconn_req(conn, chan, ECONNRESET);
2368 2369 2370 2371 2372
				goto done;
			}

			l2cap_send_cmd(conn, l2cap_get_ident(conn),
						L2CAP_CONF_REQ, len, req);
2373
			chan->num_conf_req++;
2374 2375 2376
			if (result != L2CAP_CONF_SUCCESS)
				goto done;
			break;
L
Linus Torvalds 已提交
2377 2378
		}

2379
	default:
2380
		sk->sk_err = ECONNRESET;
L
Linus Torvalds 已提交
2381
		l2cap_sock_set_timer(sk, HZ * 5);
2382
		l2cap_send_disconn_req(conn, chan, ECONNRESET);
L
Linus Torvalds 已提交
2383 2384 2385 2386 2387 2388
		goto done;
	}

	if (flags & 0x01)
		goto done;

2389
	chan->conf_state |= L2CAP_CONF_INPUT_DONE;
L
Linus Torvalds 已提交
2390

2391
	if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) {
2392
		set_default_fcs(chan);
2393

L
Linus Torvalds 已提交
2394
		sk->sk_state = BT_CONNECTED;
2395 2396
		chan->next_tx_seq = 0;
		chan->expected_tx_seq = 0;
2397
		skb_queue_head_init(&chan->tx_q);
2398
		if (chan->mode ==  L2CAP_MODE_ERTM)
2399
			l2cap_ertm_init(chan);
2400

L
Linus Torvalds 已提交
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413
		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;
2414
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2415 2416 2417 2418 2419 2420 2421
	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);

2422
	chan = l2cap_get_chan_by_scid(conn, dcid);
2423
	if (!chan)
L
Linus Torvalds 已提交
2424 2425
		return 0;

2426 2427
	sk = chan->sk;

2428 2429
	rsp.dcid = cpu_to_le16(chan->scid);
	rsp.scid = cpu_to_le16(chan->dcid);
L
Linus Torvalds 已提交
2430 2431 2432 2433
	l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);

	sk->sk_shutdown = SHUTDOWN_MASK;

2434 2435 2436 2437 2438 2439 2440 2441 2442
	/* 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;
	}

2443
	l2cap_chan_del(chan, ECONNRESET);
L
Linus Torvalds 已提交
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453
	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;
2454
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2455 2456 2457 2458 2459 2460 2461
	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);

2462
	chan = l2cap_get_chan_by_scid(conn, scid);
2463
	if (!chan)
L
Linus Torvalds 已提交
2464 2465
		return 0;

2466 2467
	sk = chan->sk;

2468 2469 2470 2471 2472 2473 2474 2475 2476
	/* 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;
	}

2477
	l2cap_chan_del(chan, 0);
L
Linus Torvalds 已提交
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
	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);

2493 2494
	if (type == L2CAP_IT_FEAT_MASK) {
		u8 buf[8];
2495
		u32 feat_mask = l2cap_feat_mask;
2496 2497 2498
		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);
2499
		if (!disable_ertm)
2500 2501
			feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
							 | L2CAP_FEAT_FCS;
2502
		put_unaligned_le32(feat_mask, rsp->data);
2503 2504
		l2cap_send_cmd(conn, cmd->ident,
					L2CAP_INFO_RSP, sizeof(buf), buf);
2505 2506 2507 2508 2509 2510 2511 2512
	} 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);
2513 2514 2515 2516 2517 2518 2519
	} 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 已提交
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533

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

2534 2535 2536 2537 2538
	/* 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;

2539 2540
	del_timer(&conn->info_timer);

2541 2542 2543 2544 2545 2546 2547 2548 2549
	if (result != L2CAP_IR_SUCCESS) {
		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
		conn->info_ident = 0;

		l2cap_conn_start(conn);

		return 0;
	}

2550
	if (type == L2CAP_IT_FEAT_MASK) {
2551
		conn->feat_mask = get_unaligned_le32(rsp->data);
2552

2553
		if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567
			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) {
2568
		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
2569
		conn->info_ident = 0;
2570 2571 2572

		l2cap_conn_start(conn);
	}
2573

L
Linus Torvalds 已提交
2574 2575 2576
	return 0;
}

2577
static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
							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;
2605
	int err;
2606 2607 2608 2609 2610 2611 2612 2613 2614

	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;
2615 2616
	min		= __le16_to_cpu(req->min);
	max		= __le16_to_cpu(req->max);
2617 2618 2619 2620 2621 2622 2623
	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));
2624 2625 2626

	err = l2cap_check_conn_param(min, max, latency, to_multiplier);
	if (err)
2627 2628 2629 2630 2631 2632 2633
		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);

2634 2635 2636
	if (!err)
		hci_le_conn_update(hcon, min, max, latency, to_multiplier);

2637 2638 2639
	return 0;
}

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 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705
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:
2706
		return l2cap_conn_param_update_req(conn, cmd, data);
2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718

	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 已提交
2719 2720 2721 2722
{
	u8 *data = skb->data;
	int len = skb->len;
	struct l2cap_cmd_hdr cmd;
2723
	int err;
L
Linus Torvalds 已提交
2724 2725 2726 2727

	l2cap_raw_recv(conn, skb);

	while (len >= L2CAP_CMD_HDR_SIZE) {
2728
		u16 cmd_len;
L
Linus Torvalds 已提交
2729 2730 2731 2732
		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
		data += L2CAP_CMD_HDR_SIZE;
		len  -= L2CAP_CMD_HDR_SIZE;

2733
		cmd_len = le16_to_cpu(cmd.len);
L
Linus Torvalds 已提交
2734

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

2737
		if (cmd_len > len || !cmd.ident) {
L
Linus Torvalds 已提交
2738 2739 2740 2741
			BT_DBG("corrupted command");
			break;
		}

2742 2743 2744 2745
		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 已提交
2746 2747 2748

		if (err) {
			struct l2cap_cmd_rej rej;
2749 2750

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

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

2757 2758
		data += cmd_len;
		len  -= cmd_len;
L
Linus Torvalds 已提交
2759 2760 2761 2762 2763
	}

	kfree_skb(skb);
}

2764
static int l2cap_check_fcs(struct l2cap_chan *chan,  struct sk_buff *skb)
2765 2766 2767 2768
{
	u16 our_fcs, rcv_fcs;
	int hdr_size = L2CAP_HDR_SIZE + 2;

2769
	if (chan->fcs == L2CAP_FCS_CRC16) {
2770 2771 2772 2773 2774
		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)
2775
			return -EBADMSG;
2776 2777 2778 2779
	}
	return 0;
}

2780
static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
2781 2782 2783
{
	u16 control = 0;

2784
	chan->frames_sent = 0;
2785

2786
	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
2787

2788
	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
2789
		control |= L2CAP_SUPER_RCV_NOT_READY;
2790 2791
		l2cap_send_sframe(chan, control);
		chan->conn_state |= L2CAP_CONN_RNR_SENT;
2792 2793
	}

2794 2795
	if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY)
		l2cap_retransmit_frames(chan);
2796

2797
	l2cap_ertm_send(chan);
2798

2799
	if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
2800
			chan->frames_sent == 0) {
2801
		control |= L2CAP_SUPER_RCV_READY;
2802
		l2cap_send_sframe(chan, control);
2803 2804 2805
	}
}

2806
static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar)
2807 2808
{
	struct sk_buff *next_skb;
2809
	int tx_seq_offset, next_tx_seq_offset;
2810 2811 2812 2813

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

2814
	next_skb = skb_peek(&chan->srej_q);
2815
	if (!next_skb) {
2816
		__skb_queue_tail(&chan->srej_q, skb);
2817
		return 0;
2818 2819
	}

2820
	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
2821 2822 2823
	if (tx_seq_offset < 0)
		tx_seq_offset += 64;

2824
	do {
2825 2826 2827
		if (bt_cb(next_skb)->tx_seq == tx_seq)
			return -EINVAL;

2828
		next_tx_seq_offset = (bt_cb(next_skb)->tx_seq -
2829
						chan->buffer_seq) % 64;
2830 2831 2832 2833
		if (next_tx_seq_offset < 0)
			next_tx_seq_offset += 64;

		if (next_tx_seq_offset > tx_seq_offset) {
2834
			__skb_queue_before(&chan->srej_q, next_skb, skb);
2835
			return 0;
2836 2837
		}

2838
		if (skb_queue_is_last(&chan->srej_q, next_skb))
2839 2840
			break;

2841
	} while ((next_skb = skb_queue_next(&chan->srej_q, next_skb)));
2842

2843
	__skb_queue_tail(&chan->srej_q, skb);
2844 2845

	return 0;
2846 2847
}

2848
static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
2849 2850
{
	struct sk_buff *_skb;
2851
	int err;
2852 2853 2854

	switch (control & L2CAP_CTRL_SAR) {
	case L2CAP_SDU_UNSEGMENTED:
2855
		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
2856 2857
			goto drop;

2858
		err = sock_queue_rcv_skb(chan->sk, skb);
2859 2860 2861 2862 2863 2864
		if (!err)
			return err;

		break;

	case L2CAP_SDU_START:
2865
		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
2866 2867
			goto drop;

2868
		chan->sdu_len = get_unaligned_le16(skb->data);
2869

2870
		if (chan->sdu_len > chan->imtu)
2871 2872
			goto disconnect;

2873 2874
		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
		if (!chan->sdu)
2875 2876 2877 2878 2879 2880
			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);
2881

2882
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
2883

2884
		chan->conn_state |= L2CAP_CONN_SAR_SDU;
2885
		chan->partial_sdu_len = skb->len;
2886 2887 2888
		break;

	case L2CAP_SDU_CONTINUE:
2889
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
2890 2891
			goto disconnect;

2892
		if (!chan->sdu)
2893 2894
			goto disconnect;

2895 2896
		chan->partial_sdu_len += skb->len;
		if (chan->partial_sdu_len > chan->sdu_len)
2897 2898
			goto drop;

2899
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
2900

2901 2902 2903
		break;

	case L2CAP_SDU_END:
2904
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
2905 2906
			goto disconnect;

2907
		if (!chan->sdu)
2908 2909
			goto disconnect;

2910
		if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) {
2911
			chan->partial_sdu_len += skb->len;
2912

2913
			if (chan->partial_sdu_len > chan->imtu)
2914
				goto drop;
2915

2916
			if (chan->partial_sdu_len != chan->sdu_len)
2917
				goto drop;
2918

2919
			memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
2920
		}
2921

2922
		_skb = skb_clone(chan->sdu, GFP_ATOMIC);
2923
		if (!_skb) {
2924
			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
2925 2926 2927
			return -ENOMEM;
		}

2928
		err = sock_queue_rcv_skb(chan->sk, _skb);
2929
		if (err < 0) {
2930
			kfree_skb(_skb);
2931
			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
2932 2933 2934
			return err;
		}

2935 2936
		chan->conn_state &= ~L2CAP_CONN_SAR_RETRY;
		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
2937

2938
		kfree_skb(chan->sdu);
2939 2940 2941 2942
		break;
	}

	kfree_skb(skb);
2943
	return 0;
2944 2945

drop:
2946 2947
	kfree_skb(chan->sdu);
	chan->sdu = NULL;
2948 2949

disconnect:
2950
	l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
2951 2952 2953 2954
	kfree_skb(skb);
	return 0;
}

2955
static int l2cap_try_push_rx_skb(struct l2cap_chan *chan)
2956 2957 2958 2959 2960
{
	struct sk_buff *skb;
	u16 control;
	int err;

2961
	while ((skb = skb_dequeue(&chan->busy_q))) {
2962
		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
2963
		err = l2cap_ertm_reassembly_sdu(chan, skb, control);
2964
		if (err < 0) {
2965
			skb_queue_head(&chan->busy_q, skb);
2966 2967 2968
			return -EBUSY;
		}

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

2972
	if (!(chan->conn_state & L2CAP_CONN_RNR_SENT))
2973 2974
		goto done;

2975
	control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
2976
	control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL;
2977
	l2cap_send_sframe(chan, control);
2978
	chan->retry_count = 1;
2979

2980
	del_timer(&chan->retrans_timer);
2981 2982
	__mod_monitor_timer();

2983
	chan->conn_state |= L2CAP_CONN_WAIT_F;
2984 2985

done:
2986 2987
	chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY;
	chan->conn_state &= ~L2CAP_CONN_RNR_SENT;
2988

2989
	BT_DBG("chan %p, Exit local busy", chan);
2990 2991 2992 2993

	return 0;
}

2994 2995 2996
static void l2cap_busy_work(struct work_struct *work)
{
	DECLARE_WAITQUEUE(wait, current);
2997 2998 2999
	struct l2cap_chan *chan =
		container_of(work, struct l2cap_chan, busy_work);
	struct sock *sk = chan->sk;
3000 3001 3002 3003 3004
	int n_tries = 0, timeo = HZ/5, err;
	struct sk_buff *skb;

	lock_sock(sk);

3005
	add_wait_queue(sk_sleep(sk), &wait);
3006
	while ((skb = skb_peek(&chan->busy_q))) {
3007 3008 3009 3010
		set_current_state(TASK_INTERRUPTIBLE);

		if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) {
			err = -EBUSY;
3011
			l2cap_send_disconn_req(chan->conn, chan, EBUSY);
3012
			break;
3013 3014 3015 3016 3017 3018 3019
		}

		if (!timeo)
			timeo = HZ/5;

		if (signal_pending(current)) {
			err = sock_intr_errno(timeo);
3020
			break;
3021 3022 3023 3024 3025 3026 3027 3028
		}

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

		err = sock_error(sk);
		if (err)
3029
			break;
3030

3031
		if (l2cap_try_push_rx_skb(chan) == 0)
3032 3033 3034 3035
			break;
	}

	set_current_state(TASK_RUNNING);
3036
	remove_wait_queue(sk_sleep(sk), &wait);
3037 3038 3039 3040

	release_sock(sk);
}

3041
static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
3042 3043 3044
{
	int sctrl, err;

3045
	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
3046
		bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3047
		__skb_queue_tail(&chan->busy_q, skb);
3048
		return l2cap_try_push_rx_skb(chan);
3049 3050


3051 3052
	}

3053
	err = l2cap_ertm_reassembly_sdu(chan, skb, control);
3054
	if (err >= 0) {
3055
		chan->buffer_seq = (chan->buffer_seq + 1) % 64;
3056 3057 3058 3059
		return err;
	}

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

3062
	chan->conn_state |= L2CAP_CONN_LOCAL_BUSY;
3063
	bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3064
	__skb_queue_tail(&chan->busy_q, skb);
3065

3066
	sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3067
	sctrl |= L2CAP_SUPER_RCV_NOT_READY;
3068
	l2cap_send_sframe(chan, sctrl);
3069

3070
	chan->conn_state |= L2CAP_CONN_RNR_SENT;
3071

3072
	del_timer(&chan->ack_timer);
3073

3074
	queue_work(_busy_wq, &chan->busy_work);
3075 3076 3077 3078

	return err;
}

3079
static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
3080 3081 3082 3083
{
	struct sk_buff *_skb;
	int err = -EINVAL;

3084 3085 3086 3087 3088
	/*
	 * TODO: We have to notify the userland if some data is lost with the
	 * Streaming Mode.
	 */

3089 3090
	switch (control & L2CAP_CTRL_SAR) {
	case L2CAP_SDU_UNSEGMENTED:
3091
		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
3092
			kfree_skb(chan->sdu);
3093 3094 3095
			break;
		}

3096
		err = sock_queue_rcv_skb(chan->sk, skb);
3097 3098 3099 3100 3101 3102
		if (!err)
			return 0;

		break;

	case L2CAP_SDU_START:
3103
		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
3104
			kfree_skb(chan->sdu);
3105 3106 3107
			break;
		}

3108
		chan->sdu_len = get_unaligned_le16(skb->data);
3109 3110
		skb_pull(skb, 2);

3111
		if (chan->sdu_len > chan->imtu) {
3112 3113 3114 3115
			err = -EMSGSIZE;
			break;
		}

3116 3117
		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
		if (!chan->sdu) {
3118 3119 3120 3121
			err = -ENOMEM;
			break;
		}

3122
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3123

3124
		chan->conn_state |= L2CAP_CONN_SAR_SDU;
3125
		chan->partial_sdu_len = skb->len;
3126 3127 3128 3129
		err = 0;
		break;

	case L2CAP_SDU_CONTINUE:
3130
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
3131 3132
			break;

3133
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3134

3135 3136 3137
		chan->partial_sdu_len += skb->len;
		if (chan->partial_sdu_len > chan->sdu_len)
			kfree_skb(chan->sdu);
3138 3139 3140 3141 3142 3143
		else
			err = 0;

		break;

	case L2CAP_SDU_END:
3144
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
3145 3146
			break;

3147
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3148

3149
		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
3150
		chan->partial_sdu_len += skb->len;
3151

3152
		if (chan->partial_sdu_len > chan->imtu)
3153 3154
			goto drop;

3155 3156
		if (chan->partial_sdu_len == chan->sdu_len) {
			_skb = skb_clone(chan->sdu, GFP_ATOMIC);
3157
			err = sock_queue_rcv_skb(chan->sk, _skb);
3158 3159 3160 3161 3162
			if (err < 0)
				kfree_skb(_skb);
		}
		err = 0;

3163
drop:
3164
		kfree_skb(chan->sdu);
3165 3166 3167 3168 3169 3170 3171
		break;
	}

	kfree_skb(skb);
	return err;
}

3172
static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq)
3173 3174
{
	struct sk_buff *skb;
3175
	u16 control;
3176

3177
	while ((skb = skb_peek(&chan->srej_q))) {
3178 3179 3180
		if (bt_cb(skb)->tx_seq != tx_seq)
			break;

3181
		skb = skb_dequeue(&chan->srej_q);
3182
		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3183
		l2cap_ertm_reassembly_sdu(chan, skb, control);
3184 3185
		chan->buffer_seq_srej =
			(chan->buffer_seq_srej + 1) % 64;
3186
		tx_seq = (tx_seq + 1) % 64;
3187 3188 3189
	}
}

3190
static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq)
3191 3192 3193 3194
{
	struct srej_list *l, *tmp;
	u16 control;

3195
	list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
3196 3197 3198 3199 3200 3201 3202
		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;
3203
		l2cap_send_sframe(chan, control);
3204
		list_del(&l->list);
3205
		list_add_tail(&l->list, &chan->srej_l);
3206 3207 3208
	}
}

3209
static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq)
3210 3211 3212 3213
{
	struct srej_list *new;
	u16 control;

3214
	while (tx_seq != chan->expected_tx_seq) {
3215
		control = L2CAP_SUPER_SELECT_REJECT;
3216
		control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3217
		l2cap_send_sframe(chan, control);
3218 3219

		new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC);
3220 3221
		new->tx_seq = chan->expected_tx_seq;
		chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3222
		list_add_tail(&new->list, &chan->srej_l);
3223
	}
3224
	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3225 3226
}

3227
static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
3228 3229
{
	u8 tx_seq = __get_txseq(rx_control);
3230
	u8 req_seq = __get_reqseq(rx_control);
3231
	u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT;
3232
	int tx_seq_offset, expected_tx_seq_offset;
3233
	int num_to_ack = (chan->tx_win/6) + 1;
3234 3235
	int err = 0;

3236 3237
	BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len,
							tx_seq, rx_control);
3238

3239
	if (L2CAP_CTRL_FINAL & rx_control &&
3240
			chan->conn_state & L2CAP_CONN_WAIT_F) {
3241
		del_timer(&chan->monitor_timer);
3242
		if (chan->unacked_frames > 0)
3243
			__mod_retrans_timer();
3244
		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
3245 3246
	}

3247 3248
	chan->expected_ack_seq = req_seq;
	l2cap_drop_acked_frames(chan);
3249

3250
	if (tx_seq == chan->expected_tx_seq)
3251
		goto expected;
3252

3253
	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
3254 3255 3256 3257
	if (tx_seq_offset < 0)
		tx_seq_offset += 64;

	/* invalid tx_seq */
3258
	if (tx_seq_offset >= chan->tx_win) {
3259
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3260 3261 3262
		goto drop;
	}

3263
	if (chan->conn_state == L2CAP_CONN_LOCAL_BUSY)
3264 3265
		goto drop;

3266
	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
3267
		struct srej_list *first;
3268

3269
		first = list_first_entry(&chan->srej_l,
3270 3271
				struct srej_list, list);
		if (tx_seq == first->tx_seq) {
3272
			l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3273
			l2cap_check_srej_gap(chan, tx_seq);
3274 3275 3276 3277

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

3278
			if (list_empty(&chan->srej_l)) {
3279
				chan->buffer_seq = chan->buffer_seq_srej;
3280 3281
				chan->conn_state &= ~L2CAP_CONN_SREJ_SENT;
				l2cap_send_ack(chan);
3282
				BT_DBG("chan %p, Exit SREJ_SENT", chan);
3283 3284 3285
			}
		} else {
			struct srej_list *l;
3286 3287

			/* duplicated tx_seq */
3288
			if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0)
3289
				goto drop;
3290

3291
			list_for_each_entry(l, &chan->srej_l, list) {
3292
				if (l->tx_seq == tx_seq) {
3293
					l2cap_resend_srejframe(chan, tx_seq);
3294 3295 3296
					return 0;
				}
			}
3297
			l2cap_send_srejframe(chan, tx_seq);
3298 3299
		}
	} else {
3300
		expected_tx_seq_offset =
3301
			(chan->expected_tx_seq - chan->buffer_seq) % 64;
3302 3303 3304 3305 3306 3307 3308
		if (expected_tx_seq_offset < 0)
			expected_tx_seq_offset += 64;

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

3309
		chan->conn_state |= L2CAP_CONN_SREJ_SENT;
3310

3311
		BT_DBG("chan %p, Enter SREJ", chan);
3312

3313
		INIT_LIST_HEAD(&chan->srej_l);
3314
		chan->buffer_seq_srej = chan->buffer_seq;
3315

3316 3317
		__skb_queue_head_init(&chan->srej_q);
		__skb_queue_head_init(&chan->busy_q);
3318
		l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3319

3320
		chan->conn_state |= L2CAP_CONN_SEND_PBIT;
3321

3322
		l2cap_send_srejframe(chan, tx_seq);
3323

3324
		del_timer(&chan->ack_timer);
3325
	}
3326 3327
	return 0;

3328
expected:
3329
	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3330

3331
	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
3332 3333
		bt_cb(skb)->tx_seq = tx_seq;
		bt_cb(skb)->sar = sar;
3334
		__skb_queue_tail(&chan->srej_q, skb);
3335 3336 3337
		return 0;
	}

3338
	err = l2cap_push_rx_skb(chan, skb, rx_control);
3339 3340 3341
	if (err < 0)
		return 0;

3342
	if (rx_control & L2CAP_CTRL_FINAL) {
3343 3344
		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
3345
		else
3346
			l2cap_retransmit_frames(chan);
3347 3348
	}

3349 3350
	__mod_ack_timer();

3351 3352
	chan->num_acked = (chan->num_acked + 1) % num_to_ack;
	if (chan->num_acked == num_to_ack - 1)
3353
		l2cap_send_ack(chan);
3354

3355
	return 0;
3356 3357 3358 3359

drop:
	kfree_skb(skb);
	return 0;
3360 3361
}

3362
static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control)
3363
{
3364
	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control),
3365 3366
						rx_control);

3367 3368
	chan->expected_ack_seq = __get_reqseq(rx_control);
	l2cap_drop_acked_frames(chan);
3369

3370
	if (rx_control & L2CAP_CTRL_POLL) {
3371 3372 3373
		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
		if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
			if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
3374
					(chan->unacked_frames > 0))
3375 3376
				__mod_retrans_timer();

3377 3378
			chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
			l2cap_send_srejtail(chan);
3379
		} else {
3380
			l2cap_send_i_or_rr_or_rnr(chan);
3381
		}
3382

3383
	} else if (rx_control & L2CAP_CTRL_FINAL) {
3384
		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3385

3386 3387
		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
3388
		else
3389
			l2cap_retransmit_frames(chan);
3390

3391
	} else {
3392
		if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
3393
				(chan->unacked_frames > 0))
3394
			__mod_retrans_timer();
3395

3396 3397 3398
		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
		if (chan->conn_state & L2CAP_CONN_SREJ_SENT)
			l2cap_send_ack(chan);
3399
		else
3400
			l2cap_ertm_send(chan);
3401 3402
	}
}
3403

3404
static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control)
3405 3406
{
	u8 tx_seq = __get_reqseq(rx_control);
3407

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

3410
	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3411

3412 3413
	chan->expected_ack_seq = tx_seq;
	l2cap_drop_acked_frames(chan);
3414 3415

	if (rx_control & L2CAP_CTRL_FINAL) {
3416 3417
		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
3418
		else
3419
			l2cap_retransmit_frames(chan);
3420
	} else {
3421
		l2cap_retransmit_frames(chan);
3422

3423 3424
		if (chan->conn_state & L2CAP_CONN_WAIT_F)
			chan->conn_state |= L2CAP_CONN_REJ_ACT;
3425 3426
	}
}
3427
static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control)
3428 3429
{
	u8 tx_seq = __get_reqseq(rx_control);
3430

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

3433
	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3434

3435
	if (rx_control & L2CAP_CTRL_POLL) {
3436 3437
		chan->expected_ack_seq = tx_seq;
		l2cap_drop_acked_frames(chan);
3438

3439 3440
		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
		l2cap_retransmit_one_frame(chan, tx_seq);
3441

3442
		l2cap_ertm_send(chan);
3443

3444
		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
3445
			chan->srej_save_reqseq = tx_seq;
3446
			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
3447
		}
3448
	} else if (rx_control & L2CAP_CTRL_FINAL) {
3449
		if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) &&
3450
				chan->srej_save_reqseq == tx_seq)
3451
			chan->conn_state &= ~L2CAP_CONN_SREJ_ACT;
3452
		else
3453
			l2cap_retransmit_one_frame(chan, tx_seq);
3454
	} else {
3455 3456
		l2cap_retransmit_one_frame(chan, tx_seq);
		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
3457
			chan->srej_save_reqseq = tx_seq;
3458
			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
3459
		}
3460 3461 3462
	}
}

3463
static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control)
3464 3465 3466
{
	u8 tx_seq = __get_reqseq(rx_control);

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

3469
	chan->conn_state |= L2CAP_CONN_REMOTE_BUSY;
3470 3471
	chan->expected_ack_seq = tx_seq;
	l2cap_drop_acked_frames(chan);
3472

3473
	if (rx_control & L2CAP_CTRL_POLL)
3474
		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
3475

3476
	if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) {
3477
		del_timer(&chan->retrans_timer);
3478
		if (rx_control & L2CAP_CTRL_POLL)
3479
			l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL);
3480
		return;
3481
	}
3482 3483

	if (rx_control & L2CAP_CTRL_POLL)
3484
		l2cap_send_srejtail(chan);
3485
	else
3486
		l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY);
3487 3488
}

3489
static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
3490
{
3491
	BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len);
3492

3493
	if (L2CAP_CTRL_FINAL & rx_control &&
3494
			chan->conn_state & L2CAP_CONN_WAIT_F) {
3495
		del_timer(&chan->monitor_timer);
3496
		if (chan->unacked_frames > 0)
3497
			__mod_retrans_timer();
3498
		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
3499 3500 3501 3502
	}

	switch (rx_control & L2CAP_CTRL_SUPERVISE) {
	case L2CAP_SUPER_RCV_READY:
3503
		l2cap_data_channel_rrframe(chan, rx_control);
3504 3505
		break;

3506
	case L2CAP_SUPER_REJECT:
3507
		l2cap_data_channel_rejframe(chan, rx_control);
3508
		break;
3509

3510
	case L2CAP_SUPER_SELECT_REJECT:
3511
		l2cap_data_channel_srejframe(chan, rx_control);
3512 3513 3514
		break;

	case L2CAP_SUPER_RCV_NOT_READY:
3515
		l2cap_data_channel_rnrframe(chan, rx_control);
3516 3517 3518
		break;
	}

3519
	kfree_skb(skb);
3520 3521 3522
	return 0;
}

3523 3524
static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb)
{
3525
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538
	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.
	 */
3539
	if (l2cap_check_fcs(chan, skb))
3540 3541 3542 3543 3544
		goto drop;

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

3545
	if (chan->fcs == L2CAP_FCS_CRC16)
3546 3547
		len -= 2;

3548
	if (len > chan->mps) {
3549
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3550 3551 3552 3553
		goto drop;
	}

	req_seq = __get_reqseq(control);
3554
	req_seq_offset = (req_seq - chan->expected_ack_seq) % 64;
3555 3556 3557 3558
	if (req_seq_offset < 0)
		req_seq_offset += 64;

	next_tx_seq_offset =
3559
		(chan->next_tx_seq - chan->expected_ack_seq) % 64;
3560 3561 3562 3563 3564
	if (next_tx_seq_offset < 0)
		next_tx_seq_offset += 64;

	/* check for invalid req-seq */
	if (req_seq_offset > next_tx_seq_offset) {
3565
		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3566 3567 3568 3569 3570
		goto drop;
	}

	if (__is_iframe(control)) {
		if (len < 0) {
3571
			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3572 3573 3574
			goto drop;
		}

3575
		l2cap_data_channel_iframe(chan, control, skb);
3576 3577 3578
	} else {
		if (len != 0) {
			BT_ERR("%d", len);
3579
			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3580 3581 3582
			goto drop;
		}

3583
		l2cap_data_channel_sframe(chan, control, skb);
3584 3585 3586 3587 3588 3589 3590 3591 3592
	}

	return 0;

drop:
	kfree_skb(skb);
	return 0;
}

L
Linus Torvalds 已提交
3593 3594
static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
{
3595
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
3596
	struct sock *sk;
3597
	struct l2cap_pinfo *pi;
3598
	u16 control;
3599 3600
	u8 tx_seq;
	int len;
L
Linus Torvalds 已提交
3601

3602
	chan = l2cap_get_chan_by_scid(conn, cid);
3603
	if (!chan) {
L
Linus Torvalds 已提交
3604 3605 3606 3607
		BT_DBG("unknown cid 0x%4.4x", cid);
		goto drop;
	}

3608
	sk = chan->sk;
3609 3610
	pi = l2cap_pi(sk);

3611
	BT_DBG("chan %p, len %d", chan, skb->len);
L
Linus Torvalds 已提交
3612 3613 3614 3615

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

3616
	switch (chan->mode) {
3617 3618 3619 3620 3621
	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 已提交
3622

3623
		if (chan->imtu < skb->len)
3624
			goto drop;
L
Linus Torvalds 已提交
3625

3626 3627 3628 3629 3630
		if (!sock_queue_rcv_skb(sk, skb))
			goto done;
		break;

	case L2CAP_MODE_ERTM:
3631 3632
		if (!sock_owned_by_user(sk)) {
			l2cap_ertm_data_rcv(sk, skb);
3633
		} else {
3634
			if (sk_add_backlog(sk, skb))
3635 3636
				goto drop;
		}
3637

3638
		goto done;
3639

3640 3641 3642 3643 3644
	case L2CAP_MODE_STREAMING:
		control = get_unaligned_le16(skb->data);
		skb_pull(skb, 2);
		len = skb->len;

3645
		if (l2cap_check_fcs(chan, skb))
3646 3647
			goto drop;

3648 3649 3650
		if (__is_sar_start(control))
			len -= 2;

3651
		if (chan->fcs == L2CAP_FCS_CRC16)
3652 3653
			len -= 2;

3654
		if (len > chan->mps || len < 0 || __is_sframe(control))
3655 3656 3657 3658
			goto drop;

		tx_seq = __get_txseq(control);

3659 3660
		if (chan->expected_tx_seq == tx_seq)
			chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3661
		else
3662
			chan->expected_tx_seq = (tx_seq + 1) % 64;
3663

3664
		l2cap_streaming_reassembly_sdu(chan, skb, control);
3665 3666 3667

		goto done;

3668
	default:
3669
		BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
3670 3671
		break;
	}
L
Linus Torvalds 已提交
3672 3673 3674 3675 3676

drop:
	kfree_skb(skb);

done:
3677 3678 3679
	if (sk)
		bh_unlock_sock(sk);

L
Linus Torvalds 已提交
3680 3681 3682
	return 0;
}

3683
static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
L
Linus Torvalds 已提交
3684 3685 3686 3687 3688 3689 3690
{
	struct sock *sk;

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

3691 3692
	bh_lock_sock(sk);

L
Linus Torvalds 已提交
3693 3694 3695 3696 3697
	BT_DBG("sk %p, len %d", sk, skb->len);

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

3698
	if (l2cap_pi(sk)->chan->imtu < skb->len)
L
Linus Torvalds 已提交
3699 3700 3701 3702 3703 3704 3705 3706 3707
		goto drop;

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

drop:
	kfree_skb(skb);

done:
3708 3709
	if (sk)
		bh_unlock_sock(sk);
L
Linus Torvalds 已提交
3710 3711 3712
	return 0;
}

3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727
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;

3728
	if (l2cap_pi(sk)->chan->imtu < skb->len)
3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
		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 已提交
3743 3744 3745
static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
{
	struct l2cap_hdr *lh = (void *) skb->data;
3746 3747
	u16 cid, len;
	__le16 psm;
L
Linus Torvalds 已提交
3748 3749 3750 3751 3752

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

3753 3754 3755 3756 3757
	if (len != skb->len) {
		kfree_skb(skb);
		return;
	}

L
Linus Torvalds 已提交
3758 3759 3760
	BT_DBG("len %d, cid 0x%4.4x", len, cid);

	switch (cid) {
3761
	case L2CAP_CID_LE_SIGNALING:
3762
	case L2CAP_CID_SIGNALING:
L
Linus Torvalds 已提交
3763 3764 3765
		l2cap_sig_channel(conn, skb);
		break;

3766
	case L2CAP_CID_CONN_LESS:
3767
		psm = get_unaligned_le16(skb->data);
L
Linus Torvalds 已提交
3768 3769 3770 3771
		skb_pull(skb, 2);
		l2cap_conless_channel(conn, psm, skb);
		break;

3772 3773 3774 3775
	case L2CAP_CID_LE_DATA:
		l2cap_att_channel(conn, cid, skb);
		break;

L
Linus Torvalds 已提交
3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790
	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)
3791
		return -EINVAL;
L
Linus Torvalds 已提交
3792 3793 3794 3795 3796 3797

	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) {
3798 3799
		struct l2cap_chan *chan = l2cap_pi(sk)->chan;

L
Linus Torvalds 已提交
3800 3801 3802 3803
		if (sk->sk_state != BT_LISTEN)
			continue;

		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
3804
			lm1 |= HCI_LM_ACCEPT;
3805
			if (chan->role_switch)
3806
				lm1 |= HCI_LM_MASTER;
L
Linus Torvalds 已提交
3807
			exact++;
3808 3809
		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
			lm2 |= HCI_LM_ACCEPT;
3810
			if (chan->role_switch)
3811 3812
				lm2 |= HCI_LM_MASTER;
		}
L
Linus Torvalds 已提交
3813 3814 3815 3816 3817 3818 3819 3820
	}
	read_unlock(&l2cap_sk_list.lock);

	return exact ? lm1 : lm2;
}

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

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

3825
	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
3826
		return -EINVAL;
L
Linus Torvalds 已提交
3827 3828 3829 3830 3831

	if (!status) {
		conn = l2cap_conn_add(hcon, status);
		if (conn)
			l2cap_conn_ready(conn);
3832
	} else
L
Linus Torvalds 已提交
3833 3834 3835 3836 3837
		l2cap_conn_del(hcon, bt_err(status));

	return 0;
}

3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850
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 已提交
3851 3852 3853
{
	BT_DBG("hcon %p reason %d", hcon, reason);

3854
	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
3855
		return -EINVAL;
L
Linus Torvalds 已提交
3856 3857

	l2cap_conn_del(hcon, bt_err(reason));
3858

L
Linus Torvalds 已提交
3859 3860 3861
	return 0;
}

3862
static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
3863
{
3864 3865
	struct sock *sk = chan->sk;

3866
	if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM)
3867 3868
		return;

3869
	if (encrypt == 0x00) {
3870
		if (chan->sec_level == BT_SECURITY_MEDIUM) {
3871 3872
			l2cap_sock_clear_timer(sk);
			l2cap_sock_set_timer(sk, HZ * 5);
3873
		} else if (chan->sec_level == BT_SECURITY_HIGH)
3874 3875
			__l2cap_sock_close(sk, ECONNREFUSED);
	} else {
3876
		if (chan->sec_level == BT_SECURITY_MEDIUM)
3877 3878 3879 3880
			l2cap_sock_clear_timer(sk);
	}
}

3881
static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
L
Linus Torvalds 已提交
3882
{
3883
	struct l2cap_conn *conn = hcon->l2cap_data;
3884
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
3885

3886
	if (!conn)
L
Linus Torvalds 已提交
3887
		return 0;
3888

L
Linus Torvalds 已提交
3889 3890
	BT_DBG("conn %p", conn);

3891
	read_lock(&conn->chan_lock);
L
Linus Torvalds 已提交
3892

3893
	list_for_each_entry(chan, &conn->chan_l, list) {
3894
		struct sock *sk = chan->sk;
3895

L
Linus Torvalds 已提交
3896 3897
		bh_lock_sock(sk);

3898
		if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) {
3899 3900 3901 3902
			bh_unlock_sock(sk);
			continue;
		}

3903
		if (!status && (sk->sk_state == BT_CONNECTED ||
3904
						sk->sk_state == BT_CONFIG)) {
3905
			l2cap_check_encryption(chan, encrypt);
3906 3907 3908 3909
			bh_unlock_sock(sk);
			continue;
		}

3910 3911 3912
		if (sk->sk_state == BT_CONNECT) {
			if (!status) {
				struct l2cap_conn_req req;
3913 3914
				req.scid = cpu_to_le16(chan->scid);
				req.psm  = chan->psm;
L
Linus Torvalds 已提交
3915

3916
				chan->ident = l2cap_get_ident(conn);
3917
				chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
L
Linus Torvalds 已提交
3918

3919
				l2cap_send_cmd(conn, chan->ident,
3920 3921 3922 3923 3924 3925 3926 3927
					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 已提交
3928

3929 3930 3931 3932 3933 3934 3935 3936 3937
			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;
			}

3938 3939
			rsp.scid   = cpu_to_le16(chan->dcid);
			rsp.dcid   = cpu_to_le16(chan->scid);
3940
			rsp.result = cpu_to_le16(result);
3941
			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
3942 3943
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
							sizeof(rsp), &rsp);
3944
		}
L
Linus Torvalds 已提交
3945 3946 3947 3948

		bh_unlock_sock(sk);
	}

3949
	read_unlock(&conn->chan_lock);
3950

L
Linus Torvalds 已提交
3951 3952 3953 3954 3955 3956 3957
	return 0;
}

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

3958 3959 3960 3961
	if (!conn)
		conn = l2cap_conn_add(hcon, 0);

	if (!conn)
L
Linus Torvalds 已提交
3962 3963 3964 3965
		goto drop;

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

3966
	if (!(flags & ACL_CONT)) {
L
Linus Torvalds 已提交
3967
		struct l2cap_hdr *hdr;
3968
		struct l2cap_chan *chan;
3969
		u16 cid;
L
Linus Torvalds 已提交
3970 3971 3972 3973 3974 3975 3976 3977 3978 3979
		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);
		}

3980 3981
		/* Start fragment always begin with Basic L2CAP header */
		if (skb->len < L2CAP_HDR_SIZE) {
L
Linus Torvalds 已提交
3982 3983 3984 3985 3986 3987 3988
			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;
3989
		cid = __le16_to_cpu(hdr->cid);
L
Linus Torvalds 已提交
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005

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

4006
		chan = l2cap_get_chan_by_scid(conn, cid);
4007

4008 4009
		if (chan && chan->sk) {
			struct sock *sk = chan->sk;
4010

4011
			if (chan->imtu < len - L2CAP_HDR_SIZE) {
4012 4013
				BT_ERR("Frame exceeding recv MTU (len %d, "
							"MTU %d)", len,
4014
							chan->imtu);
4015 4016 4017 4018
				bh_unlock_sock(sk);
				l2cap_conn_unreliable(conn, ECOMM);
				goto drop;
			}
4019
			bh_unlock_sock(sk);
4020
		}
4021

L
Linus Torvalds 已提交
4022
		/* Allocate skb for the complete frame (with header) */
4023 4024
		conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
		if (!conn->rx_skb)
L
Linus Torvalds 已提交
4025 4026
			goto drop;

4027
		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
4028
								skb->len);
L
Linus Torvalds 已提交
4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048
		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;
		}

4049
		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
4050
								skb->len);
L
Linus Torvalds 已提交
4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064
		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;
}

4065
static int l2cap_debugfs_show(struct seq_file *f, void *p)
L
Linus Torvalds 已提交
4066 4067 4068 4069 4070 4071
{
	struct sock *sk;
	struct hlist_node *node;

	read_lock_bh(&l2cap_sk_list.lock);

4072 4073
	sk_for_each(sk, node, &l2cap_sk_list.head) {
		struct l2cap_pinfo *pi = l2cap_pi(sk);
4074
		struct l2cap_chan *chan = pi->chan;
4075

4076
		seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
4077 4078
					batostr(&bt_sk(sk)->src),
					batostr(&bt_sk(sk)->dst),
4079 4080
					sk->sk_state, __le16_to_cpu(chan->psm),
					chan->scid, chan->dcid,
4081 4082
					chan->imtu, chan->omtu, chan->sec_level,
					chan->mode);
4083
	}
L
Linus Torvalds 已提交
4084 4085 4086

	read_unlock_bh(&l2cap_sk_list.lock);

4087
	return 0;
L
Linus Torvalds 已提交
4088 4089
}

4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102
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 已提交
4103 4104 4105 4106 4107 4108 4109

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,
4110
	.disconn_cfm	= l2cap_disconn_cfm,
4111
	.security_cfm	= l2cap_security_cfm,
L
Linus Torvalds 已提交
4112 4113 4114
	.recv_acldata	= l2cap_recv_acldata
};

4115
int __init l2cap_init(void)
L
Linus Torvalds 已提交
4116 4117
{
	int err;
4118

4119
	err = l2cap_init_sockets();
L
Linus Torvalds 已提交
4120 4121 4122
	if (err < 0)
		return err;

4123
	_busy_wq = create_singlethread_workqueue("l2cap");
4124
	if (!_busy_wq) {
4125
		err = -ENOMEM;
L
Linus Torvalds 已提交
4126 4127 4128 4129 4130 4131 4132 4133 4134 4135
		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;
	}

4136 4137 4138 4139 4140 4141
	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 已提交
4142 4143 4144 4145

	return 0;

error:
4146
	destroy_workqueue(_busy_wq);
4147
	l2cap_cleanup_sockets();
L
Linus Torvalds 已提交
4148 4149 4150
	return err;
}

4151
void l2cap_exit(void)
L
Linus Torvalds 已提交
4152
{
4153
	debugfs_remove(l2cap_debugfs);
L
Linus Torvalds 已提交
4154

4155 4156 4157
	flush_workqueue(_busy_wq);
	destroy_workqueue(_busy_wq);

L
Linus Torvalds 已提交
4158 4159 4160
	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
		BT_ERR("L2CAP protocol unregistration failed");

4161
	l2cap_cleanup_sockets();
L
Linus Torvalds 已提交
4162 4163
}

4164 4165
module_param(disable_ertm, bool, 0644);
MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");