l2cap_core.c 93.4 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 173
	l2cap_pi(sk)->conn = conn;

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 208 209 210 211
	struct l2cap_conn *conn = l2cap_pi(sk)->conn;
	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 222 223 224
		l2cap_pi(sk)->conn = NULL;
		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 = l2cap_pi(chan->sk)->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 = pi->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 397
	if (lmp_no_flush_capable(conn->hcon->hdev))
		flags = ACL_START_NO_FLUSH;
	else
		flags = ACL_START;

	hci_send_acl(pi->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 sock *sk = chan->sk;
421 422 423
	struct l2cap_conn *conn = l2cap_pi(sk)->conn;

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

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

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

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

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

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

475 476 477
	if (!conn)
		return;

478 479
	sk = chan->sk;

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

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

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

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

	BT_DBG("conn %p", conn);

502
	read_lock(&conn->chan_lock);
503

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

507 508
		bh_lock_sock(sk);

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

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

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

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

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

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

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

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

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

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

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

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

		bh_unlock_sock(sk);
	}

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

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

	read_lock(&l2cap_sk_list.lock);

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

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

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

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

617 618
	read_unlock(&l2cap_sk_list.lock);

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

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

	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;

635 636
	bh_lock_sock(parent);

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

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

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

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

	hci_conn_hold(conn->hcon);

	l2cap_sock_init(sk, parent);
660

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

664 665
	bt_accept_enqueue(parent, sk);

666 667
	__l2cap_chan_add(conn, chan);

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

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

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

clean:
	bh_unlock_sock(parent);
}

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

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

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

688
	read_lock(&conn->chan_lock);
689

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

693
		bh_lock_sock(sk);
694

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

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

709
		bh_unlock_sock(sk);
710
	}
711

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

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

	BT_DBG("conn %p", conn);

722
	read_lock(&conn->chan_lock);
723

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

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

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

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

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

741 742 743
	l2cap_conn_start(conn);
}

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

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

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

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

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

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

768 769
	conn->feat_mask = 0;

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

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

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

779 780
	conn->disc_reason = 0x13;

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

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

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

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

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

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

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

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

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

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

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

830 831
	read_lock(&l2cap_sk_list.lock);

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

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

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

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

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

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

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

	hci_dev_lock_bh(hdev);

874
	auth_type = l2cap_get_auth_type(chan);
875

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

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

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

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

898 899
	l2cap_chan_add(conn, chan);

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

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

913 914
	err = 0;

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

921
int __l2cap_wait_ack(struct sock *sk)
922 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 ((l2cap_pi(sk)->chan->unacked_frames > 0 && l2cap_pi(sk)->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(l2cap_pi(sk)->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 1012
	struct sock *sk = chan->sk;
	struct hci_conn *hcon = l2cap_pi(sk)->conn->hcon;
1013
	u16 flags;
1014

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

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

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

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

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

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

1040
		l2cap_do_send(chan, skb);
1041

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

1046
static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq)
1047
{
1048
	struct sock *sk = chan->sk;
1049 1050 1051 1052
	struct l2cap_pinfo *pi = l2cap_pi(sk);
	struct sk_buff *skb, *tx_skb;
	u16 control, fcs;

1053
	skb = skb_peek(&chan->tx_q);
1054 1055
	if (!skb)
		return;
1056

1057 1058
	do {
		if (bt_cb(skb)->tx_seq == tx_seq)
1059 1060
			break;

1061
		if (skb_queue_is_last(&chan->tx_q, skb))
1062
			return;
1063

1064
	} while ((skb = skb_queue_next(&chan->tx_q, skb)));
1065

1066 1067
	if (chan->remote_max_tx &&
			bt_cb(skb)->retries == chan->remote_max_tx) {
1068
		l2cap_send_disconn_req(pi->conn, chan, ECONNABORTED);
1069 1070 1071 1072 1073 1074
		return;
	}

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

1077
	if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
1078
		control |= L2CAP_CTRL_FINAL;
1079
		chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
1080
	}
1081

1082
	control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
1083
			| (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
1084

1085 1086
	put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);

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

1092
	l2cap_do_send(chan, tx_skb);
1093 1094
}

1095
int l2cap_ertm_send(struct l2cap_chan *chan)
1096 1097
{
	struct sk_buff *skb, *tx_skb;
1098
	struct sock *sk = chan->sk;
1099
	struct l2cap_pinfo *pi = l2cap_pi(sk);
1100
	u16 control, fcs;
1101
	int nsent = 0;
1102

1103 1104
	if (sk->sk_state != BT_CONNECTED)
		return -ENOTCONN;
1105

1106
	while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) {
1107

1108 1109
		if (chan->remote_max_tx &&
				bt_cb(skb)->retries == chan->remote_max_tx) {
1110
			l2cap_send_disconn_req(pi->conn, chan, ECONNABORTED);
1111 1112 1113
			break;
		}

1114 1115
		tx_skb = skb_clone(skb, GFP_ATOMIC);

1116 1117
		bt_cb(skb)->retries++;

1118
		control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
1119 1120
		control &= L2CAP_CTRL_SAR;

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

1129

1130
		if (chan->fcs == L2CAP_FCS_CRC16) {
1131 1132 1133 1134
			fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2);
			put_unaligned_le16(fcs, skb->data + tx_skb->len - 2);
		}

1135
		l2cap_do_send(chan, tx_skb);
1136

1137
		__mod_retrans_timer();
1138

1139 1140
		bt_cb(skb)->tx_seq = chan->next_tx_seq;
		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
1141

1142
		if (bt_cb(skb)->retries == 1)
1143
			chan->unacked_frames++;
1144

1145
		chan->frames_sent++;
1146

1147 1148
		if (skb_queue_is_last(&chan->tx_q, skb))
			chan->tx_send_head = NULL;
1149
		else
1150
			chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
1151 1152

		nsent++;
1153 1154
	}

1155 1156 1157
	return nsent;
}

1158
static int l2cap_retransmit_frames(struct l2cap_chan *chan)
1159 1160 1161
{
	int ret;

1162 1163
	if (!skb_queue_empty(&chan->tx_q))
		chan->tx_send_head = chan->tx_q.next;
1164

1165
	chan->next_tx_seq = chan->expected_ack_seq;
1166
	ret = l2cap_ertm_send(chan);
1167 1168 1169
	return ret;
}

1170
static void l2cap_send_ack(struct l2cap_chan *chan)
1171 1172 1173
{
	u16 control = 0;

1174
	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
1175

1176
	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
1177
		control |= L2CAP_SUPER_RCV_NOT_READY;
1178 1179
		chan->conn_state |= L2CAP_CONN_RNR_SENT;
		l2cap_send_sframe(chan, control);
1180
		return;
1181
	}
1182

1183
	if (l2cap_ertm_send(chan) > 0)
1184 1185 1186
		return;

	control |= L2CAP_SUPER_RCV_READY;
1187
	l2cap_send_sframe(chan, control);
1188 1189
}

1190
static void l2cap_send_srejtail(struct l2cap_chan *chan)
1191 1192 1193 1194 1195 1196 1197
{
	struct srej_list *tail;
	u16 control;

	control = L2CAP_SUPER_SELECT_REJECT;
	control |= L2CAP_CTRL_FINAL;

1198
	tail = list_entry((&chan->srej_l)->prev, struct srej_list, list);
1199 1200
	control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;

1201
	l2cap_send_sframe(chan, control);
1202 1203
}

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

1210
	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
1211
		return -EFAULT;
L
Linus Torvalds 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

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

		sent += count;
		len  -= count;

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

	return sent;
1234
}
L
Linus Torvalds 已提交
1235

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

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

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

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

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

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

1305 1306 1307
	if (!conn)
		return ERR_PTR(-ENOTCONN);

1308 1309 1310
	if (sdulen)
		hlen += 2;

1311
	if (chan->fcs == L2CAP_FCS_CRC16)
1312 1313
		hlen += 2;

1314 1315 1316 1317
	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)
1318
		return ERR_PTR(err);
1319 1320 1321

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

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

1334
	if (chan->fcs == L2CAP_FCS_CRC16)
1335 1336
		put_unaligned_le16(0, skb_put(skb, 2));

1337
	bt_cb(skb)->retries = 0;
1338
	return skb;
L
Linus Torvalds 已提交
1339 1340
}

1341
int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
1342 1343 1344 1345 1346 1347
{
	struct sk_buff *skb;
	struct sk_buff_head sar_queue;
	u16 control;
	size_t size = 0;

1348
	skb_queue_head_init(&sar_queue);
1349
	control = L2CAP_SDU_START;
1350
	skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
1351 1352 1353 1354
	if (IS_ERR(skb))
		return PTR_ERR(skb);

	__skb_queue_tail(&sar_queue, skb);
1355 1356
	len -= chan->remote_mps;
	size += chan->remote_mps;
1357 1358 1359 1360

	while (len > 0) {
		size_t buflen;

1361
		if (len > chan->remote_mps) {
1362
			control = L2CAP_SDU_CONTINUE;
1363
			buflen = chan->remote_mps;
1364
		} else {
1365
			control = L2CAP_SDU_END;
1366 1367 1368
			buflen = len;
		}

1369
		skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0);
1370 1371 1372 1373 1374 1375 1376 1377 1378
		if (IS_ERR(skb)) {
			skb_queue_purge(&sar_queue);
			return PTR_ERR(skb);
		}

		__skb_queue_tail(&sar_queue, skb);
		len -= buflen;
		size += buflen;
	}
1379 1380 1381
	skb_queue_splice_tail(&sar_queue, &chan->tx_q);
	if (chan->tx_send_head == NULL)
		chan->tx_send_head = sar_queue.next;
1382 1383 1384 1385

	return size;
}

L
Linus Torvalds 已提交
1386 1387 1388
static void l2cap_chan_ready(struct sock *sk)
{
	struct sock *parent = bt_sk(sk)->parent;
1389
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
L
Linus Torvalds 已提交
1390 1391 1392

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

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

	BT_DBG("conn %p", conn);

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

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

		if (sock_queue_rcv_skb(sk, nskb))
			kfree_skb(nskb);
	}
1434
	read_unlock(&conn->chan_lock);
L
Linus Torvalds 已提交
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
}

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

1446 1447
	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
			conn, code, ident, dlen);
L
Linus Torvalds 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456

	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);
1457
	lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
1458 1459 1460 1461 1462

	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 已提交
1463 1464 1465 1466

	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
	cmd->code  = code;
	cmd->ident = ident;
1467
	cmd->len   = cpu_to_le16(dlen);
L
Linus Torvalds 已提交
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 1514 1515 1516 1517

	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:
1518
		*val = get_unaligned_le16(opt->val);
L
Linus Torvalds 已提交
1519 1520 1521
		break;

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

	case 4:
1553
		put_unaligned_le32(val, opt->val);
L
Linus Torvalds 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
		break;

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

	*ptr += L2CAP_CONF_OPT_SIZE + len;
}

1564 1565
static void l2cap_ack_timeout(unsigned long arg)
{
1566
	struct l2cap_chan *chan = (void *) arg;
1567

1568 1569 1570
	bh_lock_sock(chan->sk);
	l2cap_send_ack(chan);
	bh_unlock_sock(chan->sk);
1571 1572
}

1573
static inline void l2cap_ertm_init(struct l2cap_chan *chan)
1574
{
1575 1576
	struct sock *sk = chan->sk;

1577
	chan->expected_ack_seq = 0;
1578
	chan->unacked_frames = 0;
1579
	chan->buffer_seq = 0;
1580 1581
	chan->num_acked = 0;
	chan->frames_sent = 0;
1582

1583 1584 1585 1586 1587
	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);
1588

1589 1590
	skb_queue_head_init(&chan->srej_q);
	skb_queue_head_init(&chan->busy_q);
1591

1592 1593
	INIT_LIST_HEAD(&chan->srej_l);

1594
	INIT_WORK(&chan->busy_work, l2cap_busy_work);
1595 1596

	sk->sk_backlog_rcv = l2cap_ertm_data_rcv;
1597 1598
}

1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
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;
	}
}

1612
static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
L
Linus Torvalds 已提交
1613
{
1614
	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);
L
Linus Torvalds 已提交
1615
	struct l2cap_conf_req *req = data;
1616
	struct l2cap_conf_rfc rfc = { .mode = chan->mode };
L
Linus Torvalds 已提交
1617 1618
	void *ptr = req->data;

1619
	BT_DBG("chan %p", chan);
L
Linus Torvalds 已提交
1620

1621
	if (chan->num_conf_req || chan->num_conf_rsp)
1622 1623
		goto done;

1624
	switch (chan->mode) {
1625 1626
	case L2CAP_MODE_STREAMING:
	case L2CAP_MODE_ERTM:
1627
		if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE)
1628 1629
			break;

1630
		/* fall through */
1631
	default:
1632
		chan->mode = l2cap_select_mode(rfc.mode, pi->conn->feat_mask);
1633 1634 1635 1636
		break;
	}

done:
1637 1638
	if (chan->imtu != L2CAP_DEFAULT_MTU)
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
1639

1640
	switch (chan->mode) {
1641
	case L2CAP_MODE_BASIC:
1642 1643 1644 1645
		if (!(pi->conn->feat_mask & L2CAP_FEAT_ERTM) &&
				!(pi->conn->feat_mask & L2CAP_FEAT_STREAMING))
			break;

1646 1647 1648 1649 1650 1651 1652
		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;

1653 1654
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
							(unsigned long) &rfc);
1655 1656 1657 1658
		break;

	case L2CAP_MODE_ERTM:
		rfc.mode            = L2CAP_MODE_ERTM;
1659 1660
		rfc.txwin_size      = chan->tx_win;
		rfc.max_transmit    = chan->max_tx;
1661 1662
		rfc.retrans_timeout = 0;
		rfc.monitor_timeout = 0;
1663
		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
1664
		if (L2CAP_DEFAULT_MAX_PDU_SIZE > pi->conn->mtu - 10)
1665
			rfc.max_pdu_size = cpu_to_le16(pi->conn->mtu - 10);
1666

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

1670 1671 1672
		if (!(pi->conn->feat_mask & L2CAP_FEAT_FCS))
			break;

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

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

1693 1694 1695
		if (!(pi->conn->feat_mask & L2CAP_FEAT_FCS))
			break;

1696
		if (chan->fcs == L2CAP_FCS_NONE ||
1697
				chan->conf_state & L2CAP_CONF_NO_FCS_RECV) {
1698 1699
			chan->fcs = L2CAP_FCS_NONE;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
1700
		}
1701 1702
		break;
	}
L
Linus Torvalds 已提交
1703

1704
	req->dcid  = cpu_to_le16(chan->dcid);
1705
	req->flags = cpu_to_le16(0);
L
Linus Torvalds 已提交
1706 1707 1708 1709

	return ptr - data;
}

1710
static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
L
Linus Torvalds 已提交
1711
{
1712
	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);
1713 1714
	struct l2cap_conf_rsp *rsp = data;
	void *ptr = rsp->data;
1715 1716
	void *req = chan->conf_req;
	int len = chan->conf_len;
1717 1718
	int type, hint, olen;
	unsigned long val;
1719
	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
1720
	u16 mtu = L2CAP_DEFAULT_MTU;
1721
	u16 result = L2CAP_CONF_SUCCESS;
L
Linus Torvalds 已提交
1722

1723
	BT_DBG("chan %p", chan);
1724

1725 1726
	while (len >= L2CAP_CONF_OPT_SIZE) {
		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
L
Linus Torvalds 已提交
1727

1728
		hint  = type & L2CAP_CONF_HINT;
1729
		type &= L2CAP_CONF_MASK;
1730 1731 1732

		switch (type) {
		case L2CAP_CONF_MTU:
1733
			mtu = val;
1734 1735 1736
			break;

		case L2CAP_CONF_FLUSH_TO:
1737
			chan->flush_to = val;
1738 1739 1740 1741 1742
			break;

		case L2CAP_CONF_QOS:
			break;

1743 1744 1745 1746 1747
		case L2CAP_CONF_RFC:
			if (olen == sizeof(rfc))
				memcpy(&rfc, (void *) val, olen);
			break;

1748 1749
		case L2CAP_CONF_FCS:
			if (val == L2CAP_FCS_NONE)
1750
				chan->conf_state |= L2CAP_CONF_NO_FCS_RECV;
1751 1752 1753

			break;

1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
		default:
			if (hint)
				break;

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

1764
	if (chan->num_conf_rsp || chan->num_conf_req > 1)
1765 1766
		goto done;

1767
	switch (chan->mode) {
1768 1769
	case L2CAP_MODE_STREAMING:
	case L2CAP_MODE_ERTM:
1770
		if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) {
1771
			chan->mode = l2cap_select_mode(rfc.mode,
1772 1773 1774 1775
					pi->conn->feat_mask);
			break;
		}

1776
		if (chan->mode != rfc.mode)
1777
			return -ECONNREFUSED;
1778

1779 1780 1781 1782
		break;
	}

done:
1783
	if (chan->mode != rfc.mode) {
1784
		result = L2CAP_CONF_UNACCEPT;
1785
		rfc.mode = chan->mode;
1786

1787
		if (chan->num_conf_rsp == 1)
1788 1789 1790 1791 1792 1793 1794
			return -ECONNREFUSED;

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


1795 1796 1797 1798
	if (result == L2CAP_CONF_SUCCESS) {
		/* Configure output options and let the other side know
		 * which ones we don't like. */

1799 1800 1801
		if (mtu < L2CAP_DEFAULT_MIN_MTU)
			result = L2CAP_CONF_UNACCEPT;
		else {
1802
			chan->omtu = mtu;
1803
			chan->conf_state |= L2CAP_CONF_MTU_DONE;
1804
		}
1805
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
1806

1807 1808
		switch (rfc.mode) {
		case L2CAP_MODE_BASIC:
1809
			chan->fcs = L2CAP_FCS_NONE;
1810
			chan->conf_state |= L2CAP_CONF_MODE_DONE;
1811 1812 1813
			break;

		case L2CAP_MODE_ERTM:
1814 1815
			chan->remote_tx_win = rfc.txwin_size;
			chan->remote_max_tx = rfc.max_transmit;
1816 1817 1818

			if (le16_to_cpu(rfc.max_pdu_size) > pi->conn->mtu - 10)
				rfc.max_pdu_size = cpu_to_le16(pi->conn->mtu - 10);
1819

1820
			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
1821

1822 1823 1824 1825
			rfc.retrans_timeout =
				le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO);
			rfc.monitor_timeout =
				le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO);
1826

1827
			chan->conf_state |= L2CAP_CONF_MODE_DONE;
1828 1829 1830 1831

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

1832 1833 1834
			break;

		case L2CAP_MODE_STREAMING:
1835 1836
			if (le16_to_cpu(rfc.max_pdu_size) > pi->conn->mtu - 10)
				rfc.max_pdu_size = cpu_to_le16(pi->conn->mtu - 10);
1837

1838
			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
1839

1840
			chan->conf_state |= L2CAP_CONF_MODE_DONE;
1841 1842 1843 1844

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

1845 1846 1847
			break;

		default:
1848 1849
			result = L2CAP_CONF_UNACCEPT;

1850
			memset(&rfc, 0, sizeof(rfc));
1851
			rfc.mode = chan->mode;
1852
		}
1853

1854
		if (result == L2CAP_CONF_SUCCESS)
1855
			chan->conf_state |= L2CAP_CONF_OUTPUT_DONE;
1856
	}
1857
	rsp->scid   = cpu_to_le16(chan->dcid);
1858 1859 1860 1861
	rsp->result = cpu_to_le16(result);
	rsp->flags  = cpu_to_le16(0x0000);

	return ptr - data;
L
Linus Torvalds 已提交
1862 1863
}

1864
static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result)
1865 1866 1867 1868 1869 1870 1871
{
	struct l2cap_conf_req *req = data;
	void *ptr = req->data;
	int type, olen;
	unsigned long val;
	struct l2cap_conf_rfc rfc;

1872
	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
1873 1874 1875 1876 1877 1878 1879 1880

	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;
1881
				chan->imtu = L2CAP_DEFAULT_MIN_MTU;
1882
			} else
1883 1884
				chan->imtu = val;
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
1885 1886 1887
			break;

		case L2CAP_CONF_FLUSH_TO:
1888
			chan->flush_to = val;
1889
			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
1890
							2, chan->flush_to);
1891 1892 1893 1894 1895 1896
			break;

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

1897
			if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) &&
1898
							rfc.mode != chan->mode)
1899 1900
				return -ECONNREFUSED;

1901
			chan->fcs = 0;
1902 1903 1904 1905 1906 1907 1908

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

1909
	if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
1910 1911
		return -ECONNREFUSED;

1912
	chan->mode = rfc.mode;
1913

1914 1915 1916
	if (*result == L2CAP_CONF_SUCCESS) {
		switch (rfc.mode) {
		case L2CAP_MODE_ERTM:
1917 1918 1919
			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);
1920 1921
			break;
		case L2CAP_MODE_STREAMING:
1922
			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
1923 1924 1925
		}
	}

1926
	req->dcid   = cpu_to_le16(chan->dcid);
1927 1928 1929 1930 1931
	req->flags  = cpu_to_le16(0x0000);

	return ptr - data;
}

1932
static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags)
L
Linus Torvalds 已提交
1933 1934 1935 1936
{
	struct l2cap_conf_rsp *rsp = data;
	void *ptr = rsp->data;

1937
	BT_DBG("chan %p", chan);
L
Linus Torvalds 已提交
1938

1939
	rsp->scid   = cpu_to_le16(chan->dcid);
1940
	rsp->result = cpu_to_le16(result);
1941
	rsp->flags  = cpu_to_le16(flags);
L
Linus Torvalds 已提交
1942 1943 1944 1945

	return ptr - data;
}

1946 1947 1948 1949 1950 1951 1952 1953 1954
void __l2cap_connect_rsp_defer(struct sock *sk)
{
	struct l2cap_conn_rsp rsp;
	struct l2cap_conn *conn = l2cap_pi(sk)->conn;
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
	u8 buf[128];

	sk->sk_state = BT_CONFIG;

1955 1956
	rsp.scid   = cpu_to_le16(chan->dcid);
	rsp.dcid   = cpu_to_le16(chan->scid);
1957 1958 1959 1960 1961
	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);

1962
	if (chan->conf_state & L2CAP_CONF_REQ_SENT)
1963 1964
		return;

1965
	chan->conf_state |= L2CAP_CONF_REQ_SENT;
1966 1967 1968 1969 1970
	l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
			l2cap_build_conf_req(chan, buf), buf);
	chan->num_conf_req++;
}

1971
static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
1972 1973 1974 1975 1976
{
	int type, olen;
	unsigned long val;
	struct l2cap_conf_rfc rfc;

1977
	BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
1978

1979
	if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
		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:
1996 1997 1998
		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);
1999 2000
		break;
	case L2CAP_MODE_STREAMING:
2001
		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
2002 2003 2004
	}
}

2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
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);
2015 2016

		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
2017
		conn->info_ident = 0;
2018

2019 2020 2021 2022 2023 2024
		l2cap_conn_start(conn);
	}

	return 0;
}

L
Linus Torvalds 已提交
2025 2026 2027 2028
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;
2029
	struct l2cap_chan *chan = NULL;
2030
	struct sock *parent, *sk = NULL;
2031
	int result, status = L2CAP_CS_NO_INFO;
L
Linus Torvalds 已提交
2032 2033

	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
2034
	__le16 psm = req->psm;
L
Linus Torvalds 已提交
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044

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

2045 2046
	bh_lock_sock(parent);

2047 2048 2049
	/* Check if the ACL is secure enough (if not SDP) */
	if (psm != cpu_to_le16(0x0001) &&
				!hci_conn_check_link_mode(conn->hcon)) {
2050
		conn->disc_reason = 0x05;
2051 2052 2053 2054
		result = L2CAP_CR_SEC_BLOCK;
		goto response;
	}

L
Linus Torvalds 已提交
2055 2056 2057 2058
	result = L2CAP_CR_NO_MEM;

	/* Check for backlog size */
	if (sk_acceptq_is_full(parent)) {
2059
		BT_DBG("backlog full %d", parent->sk_ack_backlog);
L
Linus Torvalds 已提交
2060 2061 2062
		goto response;
	}

2063
	sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC);
L
Linus Torvalds 已提交
2064 2065 2066
	if (!sk)
		goto response;

2067 2068 2069 2070 2071 2072
	chan = l2cap_chan_alloc(sk);
	if (!chan) {
		l2cap_sock_kill(sk);
		goto response;
	}

2073 2074
	l2cap_pi(sk)->chan = chan;

2075
	write_lock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2076 2077

	/* Check if we already have channel with that dcid */
2078 2079
	if (__l2cap_get_chan_by_dcid(conn, scid)) {
		write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
		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);
2090 2091
	chan->psm  = psm;
	chan->dcid = scid;
L
Linus Torvalds 已提交
2092

2093 2094
	bt_accept_enqueue(parent, sk);

2095 2096
	__l2cap_chan_add(conn, chan);

2097
	dcid = chan->scid;
L
Linus Torvalds 已提交
2098 2099 2100

	l2cap_sock_set_timer(sk, sk->sk_sndtimeo);

2101
	chan->ident = cmd->ident;
L
Linus Torvalds 已提交
2102

2103
	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
2104
		if (l2cap_check_security(chan)) {
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
			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;
			}
2115 2116 2117 2118 2119 2120 2121 2122 2123
		} 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 已提交
2124 2125
	}

2126
	write_unlock_bh(&conn->chan_lock);
L
Linus Torvalds 已提交
2127 2128 2129 2130 2131

response:
	bh_unlock_sock(parent);

sendresp:
2132 2133 2134 2135
	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 已提交
2136
	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151

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

2152
	if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) &&
2153 2154
				result == L2CAP_CR_SUCCESS) {
		u8 buf[128];
2155
		chan->conf_state |= L2CAP_CONF_REQ_SENT;
2156
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2157 2158
					l2cap_build_conf_req(chan, buf), buf);
		chan->num_conf_req++;
2159 2160
	}

L
Linus Torvalds 已提交
2161 2162 2163 2164 2165 2166 2167
	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;
2168
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
	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) {
2180
		chan = l2cap_get_chan_by_scid(conn, scid);
2181
		if (!chan)
2182
			return -EFAULT;
L
Linus Torvalds 已提交
2183
	} else {
2184
		chan = l2cap_get_chan_by_ident(conn, cmd->ident);
2185
		if (!chan)
2186
			return -EFAULT;
L
Linus Torvalds 已提交
2187 2188
	}

2189 2190
	sk = chan->sk;

L
Linus Torvalds 已提交
2191 2192 2193
	switch (result) {
	case L2CAP_CR_SUCCESS:
		sk->sk_state = BT_CONFIG;
2194
		chan->ident = 0;
2195
		chan->dcid = dcid;
2196
		chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND;
2197

2198
		if (chan->conf_state & L2CAP_CONF_REQ_SENT)
2199 2200
			break;

2201
		chan->conf_state |= L2CAP_CONF_REQ_SENT;
2202

L
Linus Torvalds 已提交
2203
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2204 2205
					l2cap_build_conf_req(chan, req), req);
		chan->num_conf_req++;
L
Linus Torvalds 已提交
2206 2207 2208
		break;

	case L2CAP_CR_PEND:
2209
		chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
L
Linus Torvalds 已提交
2210 2211 2212
		break;

	default:
2213 2214 2215 2216 2217 2218 2219 2220
		/* 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;
		}

2221
		l2cap_chan_del(chan, ECONNREFUSED);
L
Linus Torvalds 已提交
2222 2223 2224 2225 2226 2227 2228
		break;
	}

	bh_unlock_sock(sk);
	return 0;
}

2229
static inline void set_default_fcs(struct l2cap_chan *chan)
2230
{
2231 2232
	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);

2233 2234 2235
	/* FCS is enabled only in ERTM or streaming mode, if one or both
	 * sides request it.
	 */
2236
	if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
2237
		chan->fcs = L2CAP_FCS_NONE;
2238
	else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV))
2239
		chan->fcs = L2CAP_FCS_CRC16;
2240 2241
}

2242
static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
L
Linus Torvalds 已提交
2243 2244 2245 2246
{
	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
	u16 dcid, flags;
	u8 rsp[64];
2247
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2248
	struct sock *sk;
2249
	int len;
L
Linus Torvalds 已提交
2250 2251 2252 2253 2254 2255

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

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

2256
	chan = l2cap_get_chan_by_scid(conn, dcid);
2257
	if (!chan)
L
Linus Torvalds 已提交
2258 2259
		return -ENOENT;

2260 2261
	sk = chan->sk;

2262 2263 2264 2265 2266 2267
	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);
2268
		goto unlock;
2269
	}
2270

2271
	/* Reject if config buffer is too small. */
2272
	len = cmd_len - sizeof(*req);
2273
	if (chan->conf_len + len > sizeof(chan->conf_req)) {
2274
		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2275
				l2cap_build_conf_rsp(chan, rsp,
2276 2277 2278 2279 2280
					L2CAP_CONF_REJECT, flags), rsp);
		goto unlock;
	}

	/* Store config. */
2281 2282
	memcpy(chan->conf_req + chan->conf_len, req->data, len);
	chan->conf_len += len;
L
Linus Torvalds 已提交
2283 2284 2285 2286

	if (flags & 0x0001) {
		/* Incomplete config. Send empty response. */
		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2287
				l2cap_build_conf_rsp(chan, rsp,
2288
					L2CAP_CONF_SUCCESS, 0x0001), rsp);
L
Linus Torvalds 已提交
2289 2290 2291 2292
		goto unlock;
	}

	/* Complete config. */
2293
	len = l2cap_parse_conf_req(chan, rsp);
2294
	if (len < 0) {
2295
		l2cap_send_disconn_req(conn, chan, ECONNRESET);
L
Linus Torvalds 已提交
2296
		goto unlock;
2297
	}
L
Linus Torvalds 已提交
2298

2299
	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
2300
	chan->num_conf_rsp++;
2301 2302

	/* Reset config buffer. */
2303
	chan->conf_len = 0;
2304

2305
	if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE))
2306 2307
		goto unlock;

2308
	if (chan->conf_state & L2CAP_CONF_INPUT_DONE) {
2309
		set_default_fcs(chan);
2310

L
Linus Torvalds 已提交
2311
		sk->sk_state = BT_CONNECTED;
2312

2313 2314
		chan->next_tx_seq = 0;
		chan->expected_tx_seq = 0;
2315
		skb_queue_head_init(&chan->tx_q);
2316
		if (chan->mode == L2CAP_MODE_ERTM)
2317
			l2cap_ertm_init(chan);
2318

L
Linus Torvalds 已提交
2319
		l2cap_chan_ready(sk);
2320 2321 2322
		goto unlock;
	}

2323
	if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) {
2324
		u8 buf[64];
2325
		chan->conf_state |= L2CAP_CONF_REQ_SENT;
L
Linus Torvalds 已提交
2326
		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2327 2328
					l2cap_build_conf_req(chan, buf), buf);
		chan->num_conf_req++;
L
Linus Torvalds 已提交
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
	}

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;
2340
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2341
	struct sock *sk;
2342
	int len = cmd->len - sizeof(*rsp);
L
Linus Torvalds 已提交
2343 2344 2345 2346 2347

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

2348 2349
	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
			scid, flags, result);
L
Linus Torvalds 已提交
2350

2351
	chan = l2cap_get_chan_by_scid(conn, scid);
2352
	if (!chan)
L
Linus Torvalds 已提交
2353 2354
		return 0;

2355 2356
	sk = chan->sk;

L
Linus Torvalds 已提交
2357 2358
	switch (result) {
	case L2CAP_CONF_SUCCESS:
2359
		l2cap_conf_rfc_get(chan, rsp->data, len);
L
Linus Torvalds 已提交
2360 2361 2362
		break;

	case L2CAP_CONF_UNACCEPT:
2363
		if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
2364 2365
			char req[64];

2366
			if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
2367
				l2cap_send_disconn_req(conn, chan, ECONNRESET);
2368 2369 2370
				goto done;
			}

2371 2372
			/* throw out any old stored conf requests */
			result = L2CAP_CONF_SUCCESS;
2373 2374
			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
								req, &result);
2375
			if (len < 0) {
2376
				l2cap_send_disconn_req(conn, chan, ECONNRESET);
2377 2378 2379 2380 2381
				goto done;
			}

			l2cap_send_cmd(conn, l2cap_get_ident(conn),
						L2CAP_CONF_REQ, len, req);
2382
			chan->num_conf_req++;
2383 2384 2385
			if (result != L2CAP_CONF_SUCCESS)
				goto done;
			break;
L
Linus Torvalds 已提交
2386 2387
		}

2388
	default:
2389
		sk->sk_err = ECONNRESET;
L
Linus Torvalds 已提交
2390
		l2cap_sock_set_timer(sk, HZ * 5);
2391
		l2cap_send_disconn_req(conn, chan, ECONNRESET);
L
Linus Torvalds 已提交
2392 2393 2394 2395 2396 2397
		goto done;
	}

	if (flags & 0x01)
		goto done;

2398
	chan->conf_state |= L2CAP_CONF_INPUT_DONE;
L
Linus Torvalds 已提交
2399

2400
	if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) {
2401
		set_default_fcs(chan);
2402

L
Linus Torvalds 已提交
2403
		sk->sk_state = BT_CONNECTED;
2404 2405
		chan->next_tx_seq = 0;
		chan->expected_tx_seq = 0;
2406
		skb_queue_head_init(&chan->tx_q);
2407
		if (chan->mode ==  L2CAP_MODE_ERTM)
2408
			l2cap_ertm_init(chan);
2409

L
Linus Torvalds 已提交
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422
		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;
2423
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2424 2425 2426 2427 2428 2429 2430
	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);

2431
	chan = l2cap_get_chan_by_scid(conn, dcid);
2432
	if (!chan)
L
Linus Torvalds 已提交
2433 2434
		return 0;

2435 2436
	sk = chan->sk;

2437 2438
	rsp.dcid = cpu_to_le16(chan->scid);
	rsp.scid = cpu_to_le16(chan->dcid);
L
Linus Torvalds 已提交
2439 2440 2441 2442
	l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);

	sk->sk_shutdown = SHUTDOWN_MASK;

2443 2444 2445 2446 2447 2448 2449 2450 2451
	/* 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;
	}

2452
	l2cap_chan_del(chan, ECONNRESET);
L
Linus Torvalds 已提交
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
	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;
2463
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
2464 2465 2466 2467 2468 2469 2470
	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);

2471
	chan = l2cap_get_chan_by_scid(conn, scid);
2472
	if (!chan)
L
Linus Torvalds 已提交
2473 2474
		return 0;

2475 2476
	sk = chan->sk;

2477 2478 2479 2480 2481 2482 2483 2484 2485
	/* 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;
	}

2486
	l2cap_chan_del(chan, 0);
L
Linus Torvalds 已提交
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
	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);

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

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

2543 2544 2545 2546 2547
	/* 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;

2548 2549
	del_timer(&conn->info_timer);

2550 2551 2552 2553 2554 2555 2556 2557 2558
	if (result != L2CAP_IR_SUCCESS) {
		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
		conn->info_ident = 0;

		l2cap_conn_start(conn);

		return 0;
	}

2559
	if (type == L2CAP_IT_FEAT_MASK) {
2560
		conn->feat_mask = get_unaligned_le32(rsp->data);
2561

2562
		if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576
			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) {
2577
		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
2578
		conn->info_ident = 0;
2579 2580 2581

		l2cap_conn_start(conn);
	}
2582

L
Linus Torvalds 已提交
2583 2584 2585
	return 0;
}

2586
static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
							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;
2614
	int err;
2615 2616 2617 2618 2619 2620 2621 2622 2623

	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;
2624 2625
	min		= __le16_to_cpu(req->min);
	max		= __le16_to_cpu(req->max);
2626 2627 2628 2629 2630 2631 2632
	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));
2633 2634 2635

	err = l2cap_check_conn_param(min, max, latency, to_multiplier);
	if (err)
2636 2637 2638 2639 2640 2641 2642
		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);

2643 2644 2645
	if (!err)
		hci_le_conn_update(hcon, min, max, latency, to_multiplier);

2646 2647 2648
	return 0;
}

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 2706 2707 2708 2709 2710 2711 2712 2713 2714
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:
2715
		return l2cap_conn_param_update_req(conn, cmd, data);
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727

	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 已提交
2728 2729 2730 2731
{
	u8 *data = skb->data;
	int len = skb->len;
	struct l2cap_cmd_hdr cmd;
2732
	int err;
L
Linus Torvalds 已提交
2733 2734 2735 2736

	l2cap_raw_recv(conn, skb);

	while (len >= L2CAP_CMD_HDR_SIZE) {
2737
		u16 cmd_len;
L
Linus Torvalds 已提交
2738 2739 2740 2741
		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
		data += L2CAP_CMD_HDR_SIZE;
		len  -= L2CAP_CMD_HDR_SIZE;

2742
		cmd_len = le16_to_cpu(cmd.len);
L
Linus Torvalds 已提交
2743

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

2746
		if (cmd_len > len || !cmd.ident) {
L
Linus Torvalds 已提交
2747 2748 2749 2750
			BT_DBG("corrupted command");
			break;
		}

2751 2752 2753 2754
		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 已提交
2755 2756 2757

		if (err) {
			struct l2cap_cmd_rej rej;
2758 2759

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

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

2766 2767
		data += cmd_len;
		len  -= cmd_len;
L
Linus Torvalds 已提交
2768 2769 2770 2771 2772
	}

	kfree_skb(skb);
}

2773
static int l2cap_check_fcs(struct l2cap_chan *chan,  struct sk_buff *skb)
2774 2775 2776 2777
{
	u16 our_fcs, rcv_fcs;
	int hdr_size = L2CAP_HDR_SIZE + 2;

2778
	if (chan->fcs == L2CAP_FCS_CRC16) {
2779 2780 2781 2782 2783
		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)
2784
			return -EBADMSG;
2785 2786 2787 2788
	}
	return 0;
}

2789
static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
2790 2791 2792
{
	u16 control = 0;

2793
	chan->frames_sent = 0;
2794

2795
	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
2796

2797
	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
2798
		control |= L2CAP_SUPER_RCV_NOT_READY;
2799 2800
		l2cap_send_sframe(chan, control);
		chan->conn_state |= L2CAP_CONN_RNR_SENT;
2801 2802
	}

2803 2804
	if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY)
		l2cap_retransmit_frames(chan);
2805

2806
	l2cap_ertm_send(chan);
2807

2808
	if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
2809
			chan->frames_sent == 0) {
2810
		control |= L2CAP_SUPER_RCV_READY;
2811
		l2cap_send_sframe(chan, control);
2812 2813 2814
	}
}

2815
static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar)
2816 2817
{
	struct sk_buff *next_skb;
2818
	int tx_seq_offset, next_tx_seq_offset;
2819 2820 2821 2822

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

2823
	next_skb = skb_peek(&chan->srej_q);
2824
	if (!next_skb) {
2825
		__skb_queue_tail(&chan->srej_q, skb);
2826
		return 0;
2827 2828
	}

2829
	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
2830 2831 2832
	if (tx_seq_offset < 0)
		tx_seq_offset += 64;

2833
	do {
2834 2835 2836
		if (bt_cb(next_skb)->tx_seq == tx_seq)
			return -EINVAL;

2837
		next_tx_seq_offset = (bt_cb(next_skb)->tx_seq -
2838
						chan->buffer_seq) % 64;
2839 2840 2841 2842
		if (next_tx_seq_offset < 0)
			next_tx_seq_offset += 64;

		if (next_tx_seq_offset > tx_seq_offset) {
2843
			__skb_queue_before(&chan->srej_q, next_skb, skb);
2844
			return 0;
2845 2846
		}

2847
		if (skb_queue_is_last(&chan->srej_q, next_skb))
2848 2849
			break;

2850
	} while ((next_skb = skb_queue_next(&chan->srej_q, next_skb)));
2851

2852
	__skb_queue_tail(&chan->srej_q, skb);
2853 2854

	return 0;
2855 2856
}

2857
static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
2858
{
2859
	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);
2860
	struct sk_buff *_skb;
2861
	int err;
2862 2863 2864

	switch (control & L2CAP_CTRL_SAR) {
	case L2CAP_SDU_UNSEGMENTED:
2865
		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
2866 2867
			goto drop;

2868
		err = sock_queue_rcv_skb(chan->sk, skb);
2869 2870 2871 2872 2873 2874
		if (!err)
			return err;

		break;

	case L2CAP_SDU_START:
2875
		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
2876 2877
			goto drop;

2878
		chan->sdu_len = get_unaligned_le16(skb->data);
2879

2880
		if (chan->sdu_len > chan->imtu)
2881 2882
			goto disconnect;

2883 2884
		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
		if (!chan->sdu)
2885 2886 2887 2888 2889 2890
			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);
2891

2892
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
2893

2894
		chan->conn_state |= L2CAP_CONN_SAR_SDU;
2895
		chan->partial_sdu_len = skb->len;
2896 2897 2898
		break;

	case L2CAP_SDU_CONTINUE:
2899
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
2900 2901
			goto disconnect;

2902
		if (!chan->sdu)
2903 2904
			goto disconnect;

2905 2906
		chan->partial_sdu_len += skb->len;
		if (chan->partial_sdu_len > chan->sdu_len)
2907 2908
			goto drop;

2909
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
2910

2911 2912 2913
		break;

	case L2CAP_SDU_END:
2914
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
2915 2916
			goto disconnect;

2917
		if (!chan->sdu)
2918 2919
			goto disconnect;

2920
		if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) {
2921
			chan->partial_sdu_len += skb->len;
2922

2923
			if (chan->partial_sdu_len > chan->imtu)
2924
				goto drop;
2925

2926
			if (chan->partial_sdu_len != chan->sdu_len)
2927
				goto drop;
2928

2929
			memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
2930
		}
2931

2932
		_skb = skb_clone(chan->sdu, GFP_ATOMIC);
2933
		if (!_skb) {
2934
			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
2935 2936 2937
			return -ENOMEM;
		}

2938
		err = sock_queue_rcv_skb(chan->sk, _skb);
2939
		if (err < 0) {
2940
			kfree_skb(_skb);
2941
			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
2942 2943 2944
			return err;
		}

2945 2946
		chan->conn_state &= ~L2CAP_CONN_SAR_RETRY;
		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
2947

2948
		kfree_skb(chan->sdu);
2949 2950 2951 2952
		break;
	}

	kfree_skb(skb);
2953
	return 0;
2954 2955

drop:
2956 2957
	kfree_skb(chan->sdu);
	chan->sdu = NULL;
2958 2959

disconnect:
2960
	l2cap_send_disconn_req(pi->conn, chan, ECONNRESET);
2961 2962 2963 2964
	kfree_skb(skb);
	return 0;
}

2965
static int l2cap_try_push_rx_skb(struct l2cap_chan *chan)
2966 2967 2968 2969 2970
{
	struct sk_buff *skb;
	u16 control;
	int err;

2971
	while ((skb = skb_dequeue(&chan->busy_q))) {
2972
		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
2973
		err = l2cap_ertm_reassembly_sdu(chan, skb, control);
2974
		if (err < 0) {
2975
			skb_queue_head(&chan->busy_q, skb);
2976 2977 2978
			return -EBUSY;
		}

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

2982
	if (!(chan->conn_state & L2CAP_CONN_RNR_SENT))
2983 2984
		goto done;

2985
	control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
2986
	control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL;
2987
	l2cap_send_sframe(chan, control);
2988
	chan->retry_count = 1;
2989

2990
	del_timer(&chan->retrans_timer);
2991 2992
	__mod_monitor_timer();

2993
	chan->conn_state |= L2CAP_CONN_WAIT_F;
2994 2995

done:
2996 2997
	chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY;
	chan->conn_state &= ~L2CAP_CONN_RNR_SENT;
2998

2999
	BT_DBG("chan %p, Exit local busy", chan);
3000 3001 3002 3003

	return 0;
}

3004 3005 3006
static void l2cap_busy_work(struct work_struct *work)
{
	DECLARE_WAITQUEUE(wait, current);
3007 3008 3009
	struct l2cap_chan *chan =
		container_of(work, struct l2cap_chan, busy_work);
	struct sock *sk = chan->sk;
3010 3011 3012 3013 3014
	int n_tries = 0, timeo = HZ/5, err;
	struct sk_buff *skb;

	lock_sock(sk);

3015
	add_wait_queue(sk_sleep(sk), &wait);
3016
	while ((skb = skb_peek(&chan->busy_q))) {
3017 3018 3019 3020
		set_current_state(TASK_INTERRUPTIBLE);

		if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) {
			err = -EBUSY;
3021
			l2cap_send_disconn_req(l2cap_pi(sk)->conn, chan, EBUSY);
3022
			break;
3023 3024 3025 3026 3027 3028 3029
		}

		if (!timeo)
			timeo = HZ/5;

		if (signal_pending(current)) {
			err = sock_intr_errno(timeo);
3030
			break;
3031 3032 3033 3034 3035 3036 3037 3038
		}

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

		err = sock_error(sk);
		if (err)
3039
			break;
3040

3041
		if (l2cap_try_push_rx_skb(chan) == 0)
3042 3043 3044 3045
			break;
	}

	set_current_state(TASK_RUNNING);
3046
	remove_wait_queue(sk_sleep(sk), &wait);
3047 3048 3049 3050

	release_sock(sk);
}

3051
static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
3052 3053 3054
{
	int sctrl, err;

3055
	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
3056
		bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3057
		__skb_queue_tail(&chan->busy_q, skb);
3058
		return l2cap_try_push_rx_skb(chan);
3059 3060


3061 3062
	}

3063
	err = l2cap_ertm_reassembly_sdu(chan, skb, control);
3064
	if (err >= 0) {
3065
		chan->buffer_seq = (chan->buffer_seq + 1) % 64;
3066 3067 3068 3069
		return err;
	}

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

3072
	chan->conn_state |= L2CAP_CONN_LOCAL_BUSY;
3073
	bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3074
	__skb_queue_tail(&chan->busy_q, skb);
3075

3076
	sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3077
	sctrl |= L2CAP_SUPER_RCV_NOT_READY;
3078
	l2cap_send_sframe(chan, sctrl);
3079

3080
	chan->conn_state |= L2CAP_CONN_RNR_SENT;
3081

3082
	del_timer(&chan->ack_timer);
3083

3084
	queue_work(_busy_wq, &chan->busy_work);
3085 3086 3087 3088

	return err;
}

3089
static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
3090 3091 3092 3093
{
	struct sk_buff *_skb;
	int err = -EINVAL;

3094 3095 3096 3097 3098
	/*
	 * TODO: We have to notify the userland if some data is lost with the
	 * Streaming Mode.
	 */

3099 3100
	switch (control & L2CAP_CTRL_SAR) {
	case L2CAP_SDU_UNSEGMENTED:
3101
		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
3102
			kfree_skb(chan->sdu);
3103 3104 3105
			break;
		}

3106
		err = sock_queue_rcv_skb(chan->sk, skb);
3107 3108 3109 3110 3111 3112
		if (!err)
			return 0;

		break;

	case L2CAP_SDU_START:
3113
		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
3114
			kfree_skb(chan->sdu);
3115 3116 3117
			break;
		}

3118
		chan->sdu_len = get_unaligned_le16(skb->data);
3119 3120
		skb_pull(skb, 2);

3121
		if (chan->sdu_len > chan->imtu) {
3122 3123 3124 3125
			err = -EMSGSIZE;
			break;
		}

3126 3127
		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
		if (!chan->sdu) {
3128 3129 3130 3131
			err = -ENOMEM;
			break;
		}

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

3134
		chan->conn_state |= L2CAP_CONN_SAR_SDU;
3135
		chan->partial_sdu_len = skb->len;
3136 3137 3138 3139
		err = 0;
		break;

	case L2CAP_SDU_CONTINUE:
3140
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
3141 3142
			break;

3143
		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
3144

3145 3146 3147
		chan->partial_sdu_len += skb->len;
		if (chan->partial_sdu_len > chan->sdu_len)
			kfree_skb(chan->sdu);
3148 3149 3150 3151 3152 3153
		else
			err = 0;

		break;

	case L2CAP_SDU_END:
3154
		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
3155 3156
			break;

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

3159
		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
3160
		chan->partial_sdu_len += skb->len;
3161

3162
		if (chan->partial_sdu_len > chan->imtu)
3163 3164
			goto drop;

3165 3166
		if (chan->partial_sdu_len == chan->sdu_len) {
			_skb = skb_clone(chan->sdu, GFP_ATOMIC);
3167
			err = sock_queue_rcv_skb(chan->sk, _skb);
3168 3169 3170 3171 3172
			if (err < 0)
				kfree_skb(_skb);
		}
		err = 0;

3173
drop:
3174
		kfree_skb(chan->sdu);
3175 3176 3177 3178 3179 3180 3181
		break;
	}

	kfree_skb(skb);
	return err;
}

3182
static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq)
3183 3184
{
	struct sk_buff *skb;
3185
	u16 control;
3186

3187
	while ((skb = skb_peek(&chan->srej_q))) {
3188 3189 3190
		if (bt_cb(skb)->tx_seq != tx_seq)
			break;

3191
		skb = skb_dequeue(&chan->srej_q);
3192
		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3193
		l2cap_ertm_reassembly_sdu(chan, skb, control);
3194 3195
		chan->buffer_seq_srej =
			(chan->buffer_seq_srej + 1) % 64;
3196
		tx_seq = (tx_seq + 1) % 64;
3197 3198 3199
	}
}

3200
static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq)
3201 3202 3203 3204
{
	struct srej_list *l, *tmp;
	u16 control;

3205
	list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
3206 3207 3208 3209 3210 3211 3212
		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;
3213
		l2cap_send_sframe(chan, control);
3214
		list_del(&l->list);
3215
		list_add_tail(&l->list, &chan->srej_l);
3216 3217 3218
	}
}

3219
static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq)
3220 3221 3222 3223
{
	struct srej_list *new;
	u16 control;

3224
	while (tx_seq != chan->expected_tx_seq) {
3225
		control = L2CAP_SUPER_SELECT_REJECT;
3226
		control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3227
		l2cap_send_sframe(chan, control);
3228 3229

		new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC);
3230 3231
		new->tx_seq = chan->expected_tx_seq;
		chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3232
		list_add_tail(&new->list, &chan->srej_l);
3233
	}
3234
	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3235 3236
}

3237
static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
3238
{
3239
	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);
3240
	u8 tx_seq = __get_txseq(rx_control);
3241
	u8 req_seq = __get_reqseq(rx_control);
3242
	u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT;
3243
	int tx_seq_offset, expected_tx_seq_offset;
3244
	int num_to_ack = (chan->tx_win/6) + 1;
3245 3246
	int err = 0;

3247 3248
	BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len,
							tx_seq, rx_control);
3249

3250
	if (L2CAP_CTRL_FINAL & rx_control &&
3251
			chan->conn_state & L2CAP_CONN_WAIT_F) {
3252
		del_timer(&chan->monitor_timer);
3253
		if (chan->unacked_frames > 0)
3254
			__mod_retrans_timer();
3255
		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
3256 3257
	}

3258 3259
	chan->expected_ack_seq = req_seq;
	l2cap_drop_acked_frames(chan);
3260

3261
	if (tx_seq == chan->expected_tx_seq)
3262
		goto expected;
3263

3264
	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
3265 3266 3267 3268
	if (tx_seq_offset < 0)
		tx_seq_offset += 64;

	/* invalid tx_seq */
3269
	if (tx_seq_offset >= chan->tx_win) {
3270
		l2cap_send_disconn_req(pi->conn, chan, ECONNRESET);
3271 3272 3273
		goto drop;
	}

3274
	if (chan->conn_state == L2CAP_CONN_LOCAL_BUSY)
3275 3276
		goto drop;

3277
	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
3278
		struct srej_list *first;
3279

3280
		first = list_first_entry(&chan->srej_l,
3281 3282
				struct srej_list, list);
		if (tx_seq == first->tx_seq) {
3283
			l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3284
			l2cap_check_srej_gap(chan, tx_seq);
3285 3286 3287 3288

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

3289
			if (list_empty(&chan->srej_l)) {
3290
				chan->buffer_seq = chan->buffer_seq_srej;
3291 3292
				chan->conn_state &= ~L2CAP_CONN_SREJ_SENT;
				l2cap_send_ack(chan);
3293
				BT_DBG("chan %p, Exit SREJ_SENT", chan);
3294 3295 3296
			}
		} else {
			struct srej_list *l;
3297 3298

			/* duplicated tx_seq */
3299
			if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0)
3300
				goto drop;
3301

3302
			list_for_each_entry(l, &chan->srej_l, list) {
3303
				if (l->tx_seq == tx_seq) {
3304
					l2cap_resend_srejframe(chan, tx_seq);
3305 3306 3307
					return 0;
				}
			}
3308
			l2cap_send_srejframe(chan, tx_seq);
3309 3310
		}
	} else {
3311
		expected_tx_seq_offset =
3312
			(chan->expected_tx_seq - chan->buffer_seq) % 64;
3313 3314 3315 3316 3317 3318 3319
		if (expected_tx_seq_offset < 0)
			expected_tx_seq_offset += 64;

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

3320
		chan->conn_state |= L2CAP_CONN_SREJ_SENT;
3321

3322
		BT_DBG("chan %p, Enter SREJ", chan);
3323

3324
		INIT_LIST_HEAD(&chan->srej_l);
3325
		chan->buffer_seq_srej = chan->buffer_seq;
3326

3327 3328
		__skb_queue_head_init(&chan->srej_q);
		__skb_queue_head_init(&chan->busy_q);
3329
		l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3330

3331
		chan->conn_state |= L2CAP_CONN_SEND_PBIT;
3332

3333
		l2cap_send_srejframe(chan, tx_seq);
3334

3335
		del_timer(&chan->ack_timer);
3336
	}
3337 3338
	return 0;

3339
expected:
3340
	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3341

3342
	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
3343 3344
		bt_cb(skb)->tx_seq = tx_seq;
		bt_cb(skb)->sar = sar;
3345
		__skb_queue_tail(&chan->srej_q, skb);
3346 3347 3348
		return 0;
	}

3349
	err = l2cap_push_rx_skb(chan, skb, rx_control);
3350 3351 3352
	if (err < 0)
		return 0;

3353
	if (rx_control & L2CAP_CTRL_FINAL) {
3354 3355
		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
3356
		else
3357
			l2cap_retransmit_frames(chan);
3358 3359
	}

3360 3361
	__mod_ack_timer();

3362 3363
	chan->num_acked = (chan->num_acked + 1) % num_to_ack;
	if (chan->num_acked == num_to_ack - 1)
3364
		l2cap_send_ack(chan);
3365

3366
	return 0;
3367 3368 3369 3370

drop:
	kfree_skb(skb);
	return 0;
3371 3372
}

3373
static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control)
3374
{
3375
	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control),
3376 3377
						rx_control);

3378 3379
	chan->expected_ack_seq = __get_reqseq(rx_control);
	l2cap_drop_acked_frames(chan);
3380

3381
	if (rx_control & L2CAP_CTRL_POLL) {
3382 3383 3384
		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
		if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
			if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
3385
					(chan->unacked_frames > 0))
3386 3387
				__mod_retrans_timer();

3388 3389
			chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
			l2cap_send_srejtail(chan);
3390
		} else {
3391
			l2cap_send_i_or_rr_or_rnr(chan);
3392
		}
3393

3394
	} else if (rx_control & L2CAP_CTRL_FINAL) {
3395
		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3396

3397 3398
		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
3399
		else
3400
			l2cap_retransmit_frames(chan);
3401

3402
	} else {
3403
		if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
3404
				(chan->unacked_frames > 0))
3405
			__mod_retrans_timer();
3406

3407 3408 3409
		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
		if (chan->conn_state & L2CAP_CONN_SREJ_SENT)
			l2cap_send_ack(chan);
3410
		else
3411
			l2cap_ertm_send(chan);
3412 3413
	}
}
3414

3415
static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control)
3416 3417
{
	u8 tx_seq = __get_reqseq(rx_control);
3418

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

3421
	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3422

3423 3424
	chan->expected_ack_seq = tx_seq;
	l2cap_drop_acked_frames(chan);
3425 3426

	if (rx_control & L2CAP_CTRL_FINAL) {
3427 3428
		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
3429
		else
3430
			l2cap_retransmit_frames(chan);
3431
	} else {
3432
		l2cap_retransmit_frames(chan);
3433

3434 3435
		if (chan->conn_state & L2CAP_CONN_WAIT_F)
			chan->conn_state |= L2CAP_CONN_REJ_ACT;
3436 3437
	}
}
3438
static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control)
3439 3440
{
	u8 tx_seq = __get_reqseq(rx_control);
3441

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

3444
	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3445

3446
	if (rx_control & L2CAP_CTRL_POLL) {
3447 3448
		chan->expected_ack_seq = tx_seq;
		l2cap_drop_acked_frames(chan);
3449

3450 3451
		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
		l2cap_retransmit_one_frame(chan, tx_seq);
3452

3453
		l2cap_ertm_send(chan);
3454

3455
		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
3456
			chan->srej_save_reqseq = tx_seq;
3457
			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
3458
		}
3459
	} else if (rx_control & L2CAP_CTRL_FINAL) {
3460
		if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) &&
3461
				chan->srej_save_reqseq == tx_seq)
3462
			chan->conn_state &= ~L2CAP_CONN_SREJ_ACT;
3463
		else
3464
			l2cap_retransmit_one_frame(chan, tx_seq);
3465
	} else {
3466 3467
		l2cap_retransmit_one_frame(chan, tx_seq);
		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
3468
			chan->srej_save_reqseq = tx_seq;
3469
			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
3470
		}
3471 3472 3473
	}
}

3474
static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control)
3475 3476 3477
{
	u8 tx_seq = __get_reqseq(rx_control);

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

3480
	chan->conn_state |= L2CAP_CONN_REMOTE_BUSY;
3481 3482
	chan->expected_ack_seq = tx_seq;
	l2cap_drop_acked_frames(chan);
3483

3484
	if (rx_control & L2CAP_CTRL_POLL)
3485
		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
3486

3487
	if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) {
3488
		del_timer(&chan->retrans_timer);
3489
		if (rx_control & L2CAP_CTRL_POLL)
3490
			l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL);
3491
		return;
3492
	}
3493 3494

	if (rx_control & L2CAP_CTRL_POLL)
3495
		l2cap_send_srejtail(chan);
3496
	else
3497
		l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY);
3498 3499
}

3500
static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
3501
{
3502
	BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len);
3503

3504
	if (L2CAP_CTRL_FINAL & rx_control &&
3505
			chan->conn_state & L2CAP_CONN_WAIT_F) {
3506
		del_timer(&chan->monitor_timer);
3507
		if (chan->unacked_frames > 0)
3508
			__mod_retrans_timer();
3509
		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
3510 3511 3512 3513
	}

	switch (rx_control & L2CAP_CTRL_SUPERVISE) {
	case L2CAP_SUPER_RCV_READY:
3514
		l2cap_data_channel_rrframe(chan, rx_control);
3515 3516
		break;

3517
	case L2CAP_SUPER_REJECT:
3518
		l2cap_data_channel_rejframe(chan, rx_control);
3519
		break;
3520

3521
	case L2CAP_SUPER_SELECT_REJECT:
3522
		l2cap_data_channel_srejframe(chan, rx_control);
3523 3524 3525
		break;

	case L2CAP_SUPER_RCV_NOT_READY:
3526
		l2cap_data_channel_rnrframe(chan, rx_control);
3527 3528 3529
		break;
	}

3530
	kfree_skb(skb);
3531 3532 3533
	return 0;
}

3534 3535
static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb)
{
3536
	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550
	struct l2cap_pinfo *pi = l2cap_pi(sk);
	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.
	 */
3551
	if (l2cap_check_fcs(chan, skb))
3552 3553 3554 3555 3556
		goto drop;

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

3557
	if (chan->fcs == L2CAP_FCS_CRC16)
3558 3559
		len -= 2;

3560
	if (len > chan->mps) {
3561
		l2cap_send_disconn_req(pi->conn, chan, ECONNRESET);
3562 3563 3564 3565
		goto drop;
	}

	req_seq = __get_reqseq(control);
3566
	req_seq_offset = (req_seq - chan->expected_ack_seq) % 64;
3567 3568 3569 3570
	if (req_seq_offset < 0)
		req_seq_offset += 64;

	next_tx_seq_offset =
3571
		(chan->next_tx_seq - chan->expected_ack_seq) % 64;
3572 3573 3574 3575 3576
	if (next_tx_seq_offset < 0)
		next_tx_seq_offset += 64;

	/* check for invalid req-seq */
	if (req_seq_offset > next_tx_seq_offset) {
3577
		l2cap_send_disconn_req(pi->conn, chan, ECONNRESET);
3578 3579 3580 3581 3582
		goto drop;
	}

	if (__is_iframe(control)) {
		if (len < 0) {
3583
			l2cap_send_disconn_req(pi->conn, chan, ECONNRESET);
3584 3585 3586
			goto drop;
		}

3587
		l2cap_data_channel_iframe(chan, control, skb);
3588 3589 3590
	} else {
		if (len != 0) {
			BT_ERR("%d", len);
3591
			l2cap_send_disconn_req(pi->conn, chan, ECONNRESET);
3592 3593 3594
			goto drop;
		}

3595
		l2cap_data_channel_sframe(chan, control, skb);
3596 3597 3598 3599 3600 3601 3602 3603 3604
	}

	return 0;

drop:
	kfree_skb(skb);
	return 0;
}

L
Linus Torvalds 已提交
3605 3606
static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
{
3607
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
3608
	struct sock *sk;
3609
	struct l2cap_pinfo *pi;
3610
	u16 control;
3611 3612
	u8 tx_seq;
	int len;
L
Linus Torvalds 已提交
3613

3614
	chan = l2cap_get_chan_by_scid(conn, cid);
3615
	if (!chan) {
L
Linus Torvalds 已提交
3616 3617 3618 3619
		BT_DBG("unknown cid 0x%4.4x", cid);
		goto drop;
	}

3620
	sk = chan->sk;
3621 3622
	pi = l2cap_pi(sk);

3623
	BT_DBG("chan %p, len %d", chan, skb->len);
L
Linus Torvalds 已提交
3624 3625 3626 3627

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

3628
	switch (chan->mode) {
3629 3630 3631 3632 3633
	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 已提交
3634

3635
		if (chan->imtu < skb->len)
3636
			goto drop;
L
Linus Torvalds 已提交
3637

3638 3639 3640 3641 3642
		if (!sock_queue_rcv_skb(sk, skb))
			goto done;
		break;

	case L2CAP_MODE_ERTM:
3643 3644
		if (!sock_owned_by_user(sk)) {
			l2cap_ertm_data_rcv(sk, skb);
3645
		} else {
3646
			if (sk_add_backlog(sk, skb))
3647 3648
				goto drop;
		}
3649

3650
		goto done;
3651

3652 3653 3654 3655 3656
	case L2CAP_MODE_STREAMING:
		control = get_unaligned_le16(skb->data);
		skb_pull(skb, 2);
		len = skb->len;

3657
		if (l2cap_check_fcs(chan, skb))
3658 3659
			goto drop;

3660 3661 3662
		if (__is_sar_start(control))
			len -= 2;

3663
		if (chan->fcs == L2CAP_FCS_CRC16)
3664 3665
			len -= 2;

3666
		if (len > chan->mps || len < 0 || __is_sframe(control))
3667 3668 3669 3670
			goto drop;

		tx_seq = __get_txseq(control);

3671 3672
		if (chan->expected_tx_seq == tx_seq)
			chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
3673
		else
3674
			chan->expected_tx_seq = (tx_seq + 1) % 64;
3675

3676
		l2cap_streaming_reassembly_sdu(chan, skb, control);
3677 3678 3679

		goto done;

3680
	default:
3681
		BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
3682 3683
		break;
	}
L
Linus Torvalds 已提交
3684 3685 3686 3687 3688

drop:
	kfree_skb(skb);

done:
3689 3690 3691
	if (sk)
		bh_unlock_sock(sk);

L
Linus Torvalds 已提交
3692 3693 3694
	return 0;
}

3695
static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
L
Linus Torvalds 已提交
3696 3697 3698 3699 3700 3701 3702
{
	struct sock *sk;

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

3703 3704
	bh_lock_sock(sk);

L
Linus Torvalds 已提交
3705 3706 3707 3708 3709
	BT_DBG("sk %p, len %d", sk, skb->len);

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

3710
	if (l2cap_pi(sk)->chan->imtu < skb->len)
L
Linus Torvalds 已提交
3711 3712 3713 3714 3715 3716 3717 3718 3719
		goto drop;

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

drop:
	kfree_skb(skb);

done:
3720 3721
	if (sk)
		bh_unlock_sock(sk);
L
Linus Torvalds 已提交
3722 3723 3724
	return 0;
}

3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739
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;

3740
	if (l2cap_pi(sk)->chan->imtu < skb->len)
3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754
		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 已提交
3755 3756 3757
static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
{
	struct l2cap_hdr *lh = (void *) skb->data;
3758 3759
	u16 cid, len;
	__le16 psm;
L
Linus Torvalds 已提交
3760 3761 3762 3763 3764

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

3765 3766 3767 3768 3769
	if (len != skb->len) {
		kfree_skb(skb);
		return;
	}

L
Linus Torvalds 已提交
3770 3771 3772
	BT_DBG("len %d, cid 0x%4.4x", len, cid);

	switch (cid) {
3773
	case L2CAP_CID_LE_SIGNALING:
3774
	case L2CAP_CID_SIGNALING:
L
Linus Torvalds 已提交
3775 3776 3777
		l2cap_sig_channel(conn, skb);
		break;

3778
	case L2CAP_CID_CONN_LESS:
3779
		psm = get_unaligned_le16(skb->data);
L
Linus Torvalds 已提交
3780 3781 3782 3783
		skb_pull(skb, 2);
		l2cap_conless_channel(conn, psm, skb);
		break;

3784 3785 3786 3787
	case L2CAP_CID_LE_DATA:
		l2cap_att_channel(conn, cid, skb);
		break;

L
Linus Torvalds 已提交
3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802
	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)
3803
		return -EINVAL;
L
Linus Torvalds 已提交
3804 3805 3806 3807 3808 3809

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

L
Linus Torvalds 已提交
3812 3813 3814 3815
		if (sk->sk_state != BT_LISTEN)
			continue;

		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
3816
			lm1 |= HCI_LM_ACCEPT;
3817
			if (chan->role_switch)
3818
				lm1 |= HCI_LM_MASTER;
L
Linus Torvalds 已提交
3819
			exact++;
3820 3821
		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
			lm2 |= HCI_LM_ACCEPT;
3822
			if (chan->role_switch)
3823 3824
				lm2 |= HCI_LM_MASTER;
		}
L
Linus Torvalds 已提交
3825 3826 3827 3828 3829 3830 3831 3832
	}
	read_unlock(&l2cap_sk_list.lock);

	return exact ? lm1 : lm2;
}

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

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

3837
	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
3838
		return -EINVAL;
L
Linus Torvalds 已提交
3839 3840 3841 3842 3843

	if (!status) {
		conn = l2cap_conn_add(hcon, status);
		if (conn)
			l2cap_conn_ready(conn);
3844
	} else
L
Linus Torvalds 已提交
3845 3846 3847 3848 3849
		l2cap_conn_del(hcon, bt_err(status));

	return 0;
}

3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862
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 已提交
3863 3864 3865
{
	BT_DBG("hcon %p reason %d", hcon, reason);

3866
	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
3867
		return -EINVAL;
L
Linus Torvalds 已提交
3868 3869

	l2cap_conn_del(hcon, bt_err(reason));
3870

L
Linus Torvalds 已提交
3871 3872 3873
	return 0;
}

3874
static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
3875
{
3876 3877
	struct sock *sk = chan->sk;

3878
	if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM)
3879 3880
		return;

3881
	if (encrypt == 0x00) {
3882
		if (chan->sec_level == BT_SECURITY_MEDIUM) {
3883 3884
			l2cap_sock_clear_timer(sk);
			l2cap_sock_set_timer(sk, HZ * 5);
3885
		} else if (chan->sec_level == BT_SECURITY_HIGH)
3886 3887
			__l2cap_sock_close(sk, ECONNREFUSED);
	} else {
3888
		if (chan->sec_level == BT_SECURITY_MEDIUM)
3889 3890 3891 3892
			l2cap_sock_clear_timer(sk);
	}
}

3893
static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
L
Linus Torvalds 已提交
3894
{
3895
	struct l2cap_conn *conn = hcon->l2cap_data;
3896
	struct l2cap_chan *chan;
L
Linus Torvalds 已提交
3897

3898
	if (!conn)
L
Linus Torvalds 已提交
3899
		return 0;
3900

L
Linus Torvalds 已提交
3901 3902
	BT_DBG("conn %p", conn);

3903
	read_lock(&conn->chan_lock);
L
Linus Torvalds 已提交
3904

3905
	list_for_each_entry(chan, &conn->chan_l, list) {
3906
		struct sock *sk = chan->sk;
3907

L
Linus Torvalds 已提交
3908 3909
		bh_lock_sock(sk);

3910
		if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) {
3911 3912 3913 3914
			bh_unlock_sock(sk);
			continue;
		}

3915
		if (!status && (sk->sk_state == BT_CONNECTED ||
3916
						sk->sk_state == BT_CONFIG)) {
3917
			l2cap_check_encryption(chan, encrypt);
3918 3919 3920 3921
			bh_unlock_sock(sk);
			continue;
		}

3922 3923 3924
		if (sk->sk_state == BT_CONNECT) {
			if (!status) {
				struct l2cap_conn_req req;
3925 3926
				req.scid = cpu_to_le16(chan->scid);
				req.psm  = chan->psm;
L
Linus Torvalds 已提交
3927

3928
				chan->ident = l2cap_get_ident(conn);
3929
				chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
L
Linus Torvalds 已提交
3930

3931
				l2cap_send_cmd(conn, chan->ident,
3932 3933 3934 3935 3936 3937 3938 3939
					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 已提交
3940

3941 3942 3943 3944 3945 3946 3947 3948 3949
			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;
			}

3950 3951
			rsp.scid   = cpu_to_le16(chan->dcid);
			rsp.dcid   = cpu_to_le16(chan->scid);
3952
			rsp.result = cpu_to_le16(result);
3953
			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
3954 3955
			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
							sizeof(rsp), &rsp);
3956
		}
L
Linus Torvalds 已提交
3957 3958 3959 3960

		bh_unlock_sock(sk);
	}

3961
	read_unlock(&conn->chan_lock);
3962

L
Linus Torvalds 已提交
3963 3964 3965 3966 3967 3968 3969
	return 0;
}

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

3970 3971 3972 3973
	if (!conn)
		conn = l2cap_conn_add(hcon, 0);

	if (!conn)
L
Linus Torvalds 已提交
3974 3975 3976 3977
		goto drop;

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

3978
	if (!(flags & ACL_CONT)) {
L
Linus Torvalds 已提交
3979
		struct l2cap_hdr *hdr;
3980
		struct l2cap_chan *chan;
3981
		u16 cid;
L
Linus Torvalds 已提交
3982 3983 3984 3985 3986 3987 3988 3989 3990 3991
		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);
		}

3992 3993
		/* Start fragment always begin with Basic L2CAP header */
		if (skb->len < L2CAP_HDR_SIZE) {
L
Linus Torvalds 已提交
3994 3995 3996 3997 3998 3999 4000
			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;
4001
		cid = __le16_to_cpu(hdr->cid);
L
Linus Torvalds 已提交
4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017

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

4018
		chan = l2cap_get_chan_by_scid(conn, cid);
4019

4020 4021
		if (chan && chan->sk) {
			struct sock *sk = chan->sk;
4022

4023
			if (chan->imtu < len - L2CAP_HDR_SIZE) {
4024 4025
				BT_ERR("Frame exceeding recv MTU (len %d, "
							"MTU %d)", len,
4026
							chan->imtu);
4027 4028 4029 4030
				bh_unlock_sock(sk);
				l2cap_conn_unreliable(conn, ECOMM);
				goto drop;
			}
4031
			bh_unlock_sock(sk);
4032
		}
4033

L
Linus Torvalds 已提交
4034
		/* Allocate skb for the complete frame (with header) */
4035 4036
		conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
		if (!conn->rx_skb)
L
Linus Torvalds 已提交
4037 4038
			goto drop;

4039
		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
4040
								skb->len);
L
Linus Torvalds 已提交
4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060
		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;
		}

4061
		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
4062
								skb->len);
L
Linus Torvalds 已提交
4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076
		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;
}

4077
static int l2cap_debugfs_show(struct seq_file *f, void *p)
L
Linus Torvalds 已提交
4078 4079 4080 4081 4082 4083
{
	struct sock *sk;
	struct hlist_node *node;

	read_lock_bh(&l2cap_sk_list.lock);

4084 4085
	sk_for_each(sk, node, &l2cap_sk_list.head) {
		struct l2cap_pinfo *pi = l2cap_pi(sk);
4086
		struct l2cap_chan *chan = pi->chan;
4087

4088
		seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
4089 4090
					batostr(&bt_sk(sk)->src),
					batostr(&bt_sk(sk)->dst),
4091 4092
					sk->sk_state, __le16_to_cpu(chan->psm),
					chan->scid, chan->dcid,
4093 4094
					chan->imtu, chan->omtu, chan->sec_level,
					chan->mode);
4095
	}
L
Linus Torvalds 已提交
4096 4097 4098

	read_unlock_bh(&l2cap_sk_list.lock);

4099
	return 0;
L
Linus Torvalds 已提交
4100 4101
}

4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114
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 已提交
4115 4116 4117 4118 4119 4120 4121

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,
4122
	.disconn_cfm	= l2cap_disconn_cfm,
4123
	.security_cfm	= l2cap_security_cfm,
L
Linus Torvalds 已提交
4124 4125 4126
	.recv_acldata	= l2cap_recv_acldata
};

4127
int __init l2cap_init(void)
L
Linus Torvalds 已提交
4128 4129
{
	int err;
4130

4131
	err = l2cap_init_sockets();
L
Linus Torvalds 已提交
4132 4133 4134
	if (err < 0)
		return err;

4135
	_busy_wq = create_singlethread_workqueue("l2cap");
4136
	if (!_busy_wq) {
4137
		err = -ENOMEM;
L
Linus Torvalds 已提交
4138 4139 4140 4141 4142 4143 4144 4145 4146 4147
		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;
	}

4148 4149 4150 4151 4152 4153
	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 已提交
4154 4155 4156 4157

	return 0;

error:
4158
	destroy_workqueue(_busy_wq);
4159
	l2cap_cleanup_sockets();
L
Linus Torvalds 已提交
4160 4161 4162
	return err;
}

4163
void l2cap_exit(void)
L
Linus Torvalds 已提交
4164
{
4165
	debugfs_remove(l2cap_debugfs);
L
Linus Torvalds 已提交
4166

4167 4168 4169
	flush_workqueue(_busy_wq);
	destroy_workqueue(_busy_wq);

L
Linus Torvalds 已提交
4170 4171 4172
	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
		BT_ERR("L2CAP protocol unregistration failed");

4173
	l2cap_cleanup_sockets();
L
Linus Torvalds 已提交
4174 4175
}

4176 4177
module_param(disable_ertm, bool, 0644);
MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");