l2cap.c 47.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* 
   BlueZ - Bluetooth protocol stack for Linux
   Copyright (C) 2000-2001 Qualcomm Incorporated

   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
   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 
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
   SOFTWARE IS DISCLAIMED.
*/

/*
26
 * Bluetooth L2CAP core and sockets.
Linus Torvalds's avatar
Linus Torvalds committed
27
 *
28
 * $Id: l2cap.c,v 1.15 2002/09/09 01:14:52 maxk Exp $
Linus Torvalds's avatar
Linus Torvalds committed
29
 */
30
#define VERSION "2.1"
Linus Torvalds's avatar
Linus Torvalds committed
31 32 33 34 35 36 37 38 39

#include <linux/config.h>
#include <linux/module.h>

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
Linus Torvalds's avatar
Linus Torvalds committed
40
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
41 42 43 44 45 46 47
#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/proc_fs.h>
48
#include <linux/seq_file.h>
Linus Torvalds's avatar
Linus Torvalds committed
49 50 51 52 53
#include <linux/list.h>
#include <net/sock.h>

#include <asm/system.h>
#include <asm/uaccess.h>
54
#include <asm/unaligned.h>
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58 59

#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>
#include <net/bluetooth/l2cap.h>

60
#ifndef CONFIG_BT_L2CAP_DEBUG
61 62
#undef  BT_DBG
#define BT_DBG( A... )
Linus Torvalds's avatar
Linus Torvalds committed
63 64
#endif

65
static struct proto_ops l2cap_sock_ops;
Linus Torvalds's avatar
Linus Torvalds committed
66

67
struct bt_sock_list l2cap_sk_list = {
68
	.lock = RW_LOCK_UNLOCKED
Linus Torvalds's avatar
Linus Torvalds committed
69 70
};

71
static int l2cap_conn_del(struct hci_conn *conn, int err);
Linus Torvalds's avatar
Linus Torvalds committed
72

73
static void __l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent);
Linus Torvalds's avatar
Linus Torvalds committed
74 75
static void l2cap_chan_del(struct sock *sk, int err);

76
static void __l2cap_sock_close(struct sock *sk, int reason);
Linus Torvalds's avatar
Linus Torvalds committed
77 78 79
static void l2cap_sock_close(struct sock *sk);
static void l2cap_sock_kill(struct sock *sk);

80 81
static int l2cap_send_req(struct l2cap_conn *conn, u8 code, u16 len, void *data);
static int l2cap_send_rsp(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data);
Linus Torvalds's avatar
Linus Torvalds committed
82

83
/* ---- L2CAP timers ---- */
Linus Torvalds's avatar
Linus Torvalds committed
84
static void l2cap_sock_timeout(unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
85 86 87
{
	struct sock *sk = (struct sock *) arg;

88
	BT_DBG("sock %p state %d", sk, sk->sk_state);
Linus Torvalds's avatar
Linus Torvalds committed
89 90

	bh_lock_sock(sk);
91
	__l2cap_sock_close(sk, ETIMEDOUT);
Linus Torvalds's avatar
Linus Torvalds committed
92 93 94 95 96 97
	bh_unlock_sock(sk);

	l2cap_sock_kill(sk);
	sock_put(sk);
}

Linus Torvalds's avatar
Linus Torvalds committed
98
static void l2cap_sock_set_timer(struct sock *sk, long timeout)
Linus Torvalds's avatar
Linus Torvalds committed
99
{
100
	BT_DBG("sk %p state %d timeout %ld", sk, sk->sk_state, timeout);
Linus Torvalds's avatar
Linus Torvalds committed
101

102
	if (!mod_timer(&sk->sk_timer, jiffies + timeout))
Linus Torvalds's avatar
Linus Torvalds committed
103 104 105
		sock_hold(sk);
}

Linus Torvalds's avatar
Linus Torvalds committed
106
static void l2cap_sock_clear_timer(struct sock *sk)
Linus Torvalds's avatar
Linus Torvalds committed
107
{
108
	BT_DBG("sock %p state %d", sk, sk->sk_state);
Linus Torvalds's avatar
Linus Torvalds committed
109

110
	if (timer_pending(&sk->sk_timer) && del_timer(&sk->sk_timer))
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113
		__sock_put(sk);
}

Linus Torvalds's avatar
Linus Torvalds committed
114
static void l2cap_sock_init_timer(struct sock *sk)
Linus Torvalds's avatar
Linus Torvalds committed
115
{
116 117 118
	init_timer(&sk->sk_timer);
	sk->sk_timer.function = l2cap_sock_timeout;
	sk->sk_timer.data = (unsigned long)sk;
Linus Torvalds's avatar
Linus Torvalds committed
119 120
}

121 122
/* ---- L2CAP connections ---- */
static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
Linus Torvalds's avatar
Linus Torvalds committed
123 124 125
{
	struct l2cap_conn *conn;

126 127
	if ((conn = hcon->l2cap_data))
		return conn;
Linus Torvalds's avatar
Linus Torvalds committed
128

129 130
	if (status)
		return conn;
Linus Torvalds's avatar
Linus Torvalds committed
131

132 133 134
	if (!(conn = kmalloc(sizeof(struct l2cap_conn), GFP_ATOMIC)))
		return NULL;
	memset(conn, 0, sizeof(struct l2cap_conn));
Linus Torvalds's avatar
Linus Torvalds committed
135

136 137 138 139 140 141 142
	hcon->l2cap_data = conn;
	conn->hcon = hcon;
	
	conn->mtu = hcon->hdev->acl_mtu;
	conn->src = &hcon->hdev->bdaddr;
	conn->dst = &hcon->dst;
	
Linus Torvalds's avatar
Linus Torvalds committed
143 144 145
	spin_lock_init(&conn->lock);
	conn->chan_list.lock = RW_LOCK_UNLOCKED;

146
	BT_DBG("hcon %p conn %p", hcon, conn);
Linus Torvalds's avatar
Linus Torvalds committed
147 148 149
	return conn;
}

150
static int l2cap_conn_del(struct hci_conn *hcon, int err)
Linus Torvalds's avatar
Linus Torvalds committed
151
{
152
	struct l2cap_conn *conn;
Linus Torvalds's avatar
Linus Torvalds committed
153 154
	struct sock *sk;

155 156
	if (!(conn = hcon->l2cap_data)) 
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
157

158
	BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
Linus Torvalds's avatar
Linus Torvalds committed
159 160

	if (conn->rx_skb)
Linus Torvalds's avatar
Linus Torvalds committed
161
		kfree_skb(conn->rx_skb);
Linus Torvalds's avatar
Linus Torvalds committed
162 163 164 165 166 167 168 169 170

	/* Kill channels */
	while ((sk = conn->chan_list.head)) {
		bh_lock_sock(sk);
		l2cap_chan_del(sk, err);
		bh_unlock_sock(sk);
		l2cap_sock_kill(sk);
	}

171
	hcon->l2cap_data = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
172 173 174 175
	kfree(conn);
	return 0;
}

176 177 178 179 180 181 182 183
static inline void l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent)
{
	struct l2cap_chan_list *l = &conn->chan_list;
	write_lock(&l->lock);
	__l2cap_chan_add(conn, sk, parent);
	write_unlock(&l->lock);
}

184 185
/* ---- Socket interface ---- */
static struct sock *__l2cap_get_sock_by_addr(u16 psm, bdaddr_t *src)
Linus Torvalds's avatar
Linus Torvalds committed
186 187
{
	struct sock *sk;
188 189
	struct hlist_node *node;
	sk_for_each(sk, node, &l2cap_sk_list.head)
190
		if (l2cap_pi(sk)->sport == psm && !bacmp(&bt_sk(sk)->src, src))
191 192 193
			goto found;
	sk = NULL;
found:
Linus Torvalds's avatar
Linus Torvalds committed
194 195 196
	return sk;
}

197
/* Find socket with psm and source bdaddr.
Linus Torvalds's avatar
Linus Torvalds committed
198 199
 * Returns closest match.
 */
200
static struct sock *__l2cap_get_sock_by_psm(int state, u16 psm, bdaddr_t *src)
Linus Torvalds's avatar
Linus Torvalds committed
201
{
202
	struct sock *sk = NULL, *sk1 = NULL;
203
	struct hlist_node *node;
Linus Torvalds's avatar
Linus Torvalds committed
204

205
	sk_for_each(sk, node, &l2cap_sk_list.head) {
206
		if (state && sk->sk_state != state)
Linus Torvalds's avatar
Linus Torvalds committed
207 208
			continue;

209
		if (l2cap_pi(sk)->psm == psm) {
Linus Torvalds's avatar
Linus Torvalds committed
210
			/* Exact match. */
211
			if (!bacmp(&bt_sk(sk)->src, src))
Linus Torvalds's avatar
Linus Torvalds committed
212 213 214
				break;

			/* Closest match */
215
			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
Linus Torvalds's avatar
Linus Torvalds committed
216 217 218
				sk1 = sk;
		}
	}
219
	return node ? sk : sk1;
220
}
Linus Torvalds's avatar
Linus Torvalds committed
221

222 223
/* Find socket with given address (psm, src).
 * Returns locked socket */
224
static inline struct sock *l2cap_get_sock_by_psm(int state, u16 psm, bdaddr_t *src)
225 226 227 228 229
{
	struct sock *s;
	read_lock(&l2cap_sk_list.lock);
	s = __l2cap_get_sock_by_psm(state, psm, src);
	if (s) bh_lock_sock(s);
Linus Torvalds's avatar
Linus Torvalds committed
230
	read_unlock(&l2cap_sk_list.lock);
231
	return s;
Linus Torvalds's avatar
Linus Torvalds committed
232 233 234 235
}

static void l2cap_sock_destruct(struct sock *sk)
{
236
	BT_DBG("sk %p", sk);
Linus Torvalds's avatar
Linus Torvalds committed
237

238 239
	skb_queue_purge(&sk->sk_receive_queue);
	skb_queue_purge(&sk->sk_write_queue);
240

241 242
	if (sk->sk_protinfo)
		kfree(sk->sk_protinfo);
Linus Torvalds's avatar
Linus Torvalds committed
243 244 245 246 247 248
}

static void l2cap_sock_cleanup_listen(struct sock *parent)
{
	struct sock *sk;

249
	BT_DBG("parent %p", parent);
Linus Torvalds's avatar
Linus Torvalds committed
250 251

	/* Close not yet accepted channels */
252
	while ((sk = bt_accept_dequeue(parent, NULL)))
Linus Torvalds's avatar
Linus Torvalds committed
253 254
		l2cap_sock_close(sk);

255 256
	parent->sk_state  = BT_CLOSED;
	parent->sk_zapped = 1;
Linus Torvalds's avatar
Linus Torvalds committed
257 258 259 260 261 262 263
}

/* Kill socket (only if zapped and orphan)
 * Must be called on unlocked socket.
 */
static void l2cap_sock_kill(struct sock *sk)
{
264
	if (!sk->sk_zapped || sk->sk_socket)
Linus Torvalds's avatar
Linus Torvalds committed
265 266
		return;

267
	BT_DBG("sk %p state %d", sk, sk->sk_state);
Linus Torvalds's avatar
Linus Torvalds committed
268 269

	/* Kill poor orphan */
270
	bt_sock_unlink(&l2cap_sk_list, sk);
271
	sock_set_flag(sk, SOCK_DEAD);
Linus Torvalds's avatar
Linus Torvalds committed
272 273 274
	sock_put(sk);
}

275
static void __l2cap_sock_close(struct sock *sk, int reason)
Linus Torvalds's avatar
Linus Torvalds committed
276
{
277
	BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
Linus Torvalds's avatar
Linus Torvalds committed
278

279
	switch (sk->sk_state) {
Linus Torvalds's avatar
Linus Torvalds committed
280 281 282 283 284 285
	case BT_LISTEN:
		l2cap_sock_cleanup_listen(sk);
		break;

	case BT_CONNECTED:
	case BT_CONFIG:
286
	case BT_CONNECT2:
287
		if (sk->sk_type == SOCK_SEQPACKET) {
288
			struct l2cap_conn *conn = l2cap_pi(sk)->conn;
289
			struct l2cap_disconn_req req;
Linus Torvalds's avatar
Linus Torvalds committed
290

291
			sk->sk_state = BT_DISCONN;
292
			l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
Linus Torvalds's avatar
Linus Torvalds committed
293 294 295

			req.dcid = __cpu_to_le16(l2cap_pi(sk)->dcid);
			req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
296
			l2cap_send_req(conn, L2CAP_DISCONN_REQ, sizeof(req), &req);
Linus Torvalds's avatar
Linus Torvalds committed
297
		} else {
298
			l2cap_chan_del(sk, reason);
Linus Torvalds's avatar
Linus Torvalds committed
299 300 301 302 303
		}
		break;

	case BT_CONNECT:
	case BT_DISCONN:
304
		l2cap_chan_del(sk, reason);
Linus Torvalds's avatar
Linus Torvalds committed
305 306 307
		break;

	default:
308
		sk->sk_zapped = 1;
Linus Torvalds's avatar
Linus Torvalds committed
309
		break;
310
	}
311 312 313 314 315 316 317 318
}

/* Must be called on unlocked socket. */
static void l2cap_sock_close(struct sock *sk)
{
	l2cap_sock_clear_timer(sk);
	lock_sock(sk);
	__l2cap_sock_close(sk, ECONNRESET);
Linus Torvalds's avatar
Linus Torvalds committed
319 320 321 322 323 324 325 326
	release_sock(sk);
	l2cap_sock_kill(sk);
}

static void l2cap_sock_init(struct sock *sk, struct sock *parent)
{
	struct l2cap_pinfo *pi = l2cap_pi(sk);

327
	BT_DBG("sk %p", sk);
Linus Torvalds's avatar
Linus Torvalds committed
328 329

	if (parent) {
330
		sk->sk_type = parent->sk_type;
Linus Torvalds's avatar
Linus Torvalds committed
331 332
		pi->imtu = l2cap_pi(parent)->imtu;
		pi->omtu = l2cap_pi(parent)->omtu;
333
		pi->link_mode = l2cap_pi(parent)->link_mode;
Linus Torvalds's avatar
Linus Torvalds committed
334 335 336
	} else {
		pi->imtu = L2CAP_DEFAULT_MTU;
		pi->omtu = 0;
337
		pi->link_mode = 0;
Linus Torvalds's avatar
Linus Torvalds committed
338 339 340 341 342 343 344 345 346 347 348
	}

	/* Default config options */
	pi->conf_mtu = L2CAP_DEFAULT_MTU;
	pi->flush_to = L2CAP_DEFAULT_FLUSH_TO;
}

static struct sock *l2cap_sock_alloc(struct socket *sock, int proto, int prio)
{
	struct sock *sk;

349
	sk = bt_sock_alloc(sock, proto, sizeof(struct l2cap_pinfo), prio);
350
	if (!sk)
Linus Torvalds's avatar
Linus Torvalds committed
351 352
		return NULL;

353 354
	sk_set_owner(sk, THIS_MODULE);

355 356
	sk->sk_destruct = l2cap_sock_destruct;
	sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
357

358 359
	sk->sk_protocol = proto;
	sk->sk_state    = BT_OPEN;
Linus Torvalds's avatar
Linus Torvalds committed
360

Linus Torvalds's avatar
Linus Torvalds committed
361
	l2cap_sock_init_timer(sk);
Linus Torvalds's avatar
Linus Torvalds committed
362

363
	bt_sock_link(&l2cap_sk_list, sk);
Linus Torvalds's avatar
Linus Torvalds committed
364 365 366 367 368 369 370
	return sk;
}

static int l2cap_sock_create(struct socket *sock, int protocol)
{
	struct sock *sk;

371
	BT_DBG("sock %p", sock);
Linus Torvalds's avatar
Linus Torvalds committed
372

373 374 375
	sock->state = SS_UNCONNECTED;

	if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
Linus Torvalds's avatar
Linus Torvalds committed
376
		return -ESOCKTNOSUPPORT;
377 378 379 380
	
	if (sock->type == SOCK_RAW && !capable(CAP_NET_RAW))
		return -EPERM;
	
381
	sock->ops = &l2cap_sock_ops;
Linus Torvalds's avatar
Linus Torvalds committed
382

383 384
	sk = l2cap_sock_alloc(sock, protocol, GFP_KERNEL);
	if (!sk)
Linus Torvalds's avatar
Linus Torvalds committed
385 386 387 388 389 390 391 392 393 394 395 396
		return -ENOMEM;

	l2cap_sock_init(sk, NULL);
	return 0;
}

static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
{
	struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
	struct sock *sk = sock->sk;
	int err = 0;

397
	BT_DBG("sk %p, %s %d", sk, batostr(&la->l2_bdaddr), la->l2_psm);
Linus Torvalds's avatar
Linus Torvalds committed
398 399 400 401 402 403

	if (!addr || addr->sa_family != AF_BLUETOOTH)
		return -EINVAL;

	lock_sock(sk);

404
	if (sk->sk_state != BT_OPEN) {
Linus Torvalds's avatar
Linus Torvalds committed
405 406 407 408
		err = -EBADFD;
		goto done;
	}

409 410
	write_lock_bh(&l2cap_sk_list.lock);
	if (la->l2_psm && __l2cap_get_sock_by_addr(la->l2_psm, &la->l2_bdaddr)) {
Linus Torvalds's avatar
Linus Torvalds committed
411
		err = -EADDRINUSE;
412 413
	} else {
		/* Save source address */
414
		bacpy(&bt_sk(sk)->src, &la->l2_bdaddr);
415 416
		l2cap_pi(sk)->psm   = la->l2_psm;
		l2cap_pi(sk)->sport = la->l2_psm;
417
		sk->sk_state = BT_BOUND;
Linus Torvalds's avatar
Linus Torvalds committed
418
	}
419
	write_unlock_bh(&l2cap_sk_list.lock);
Linus Torvalds's avatar
Linus Torvalds committed
420 421 422 423 424 425

done:
	release_sock(sk);
	return err;
}

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
static int l2cap_do_connect(struct sock *sk)
{
	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;
	int err = 0;

	BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), l2cap_pi(sk)->psm);

	if (!(hdev = hci_get_route(dst, src)))
		return -EHOSTUNREACH;

	hci_dev_lock_bh(hdev);

	err = -ENOMEM;

	hcon = hci_connect(hdev, ACL_LINK, dst);
	if (!hcon)
		goto done;

	conn = l2cap_conn_add(hcon, 0);
	if (!conn) {
		hci_conn_put(hcon);
		goto done;
	}

	err = 0;

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

	l2cap_chan_add(conn, sk, NULL);

461 462
	sk->sk_state = BT_CONNECT;
	l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
463 464

	if (hcon->state == BT_CONNECTED) {
465
		if (sk->sk_type == SOCK_SEQPACKET) {
466 467 468 469 470 471
			struct l2cap_conn_req req;
			req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
			req.psm  = l2cap_pi(sk)->psm;
			l2cap_send_req(conn, L2CAP_CONN_REQ, sizeof(req), &req);
		} else {
			l2cap_sock_clear_timer(sk);
472
			sk->sk_state = BT_CONNECTED;
473 474 475 476 477 478 479 480 481
		}
	}

done:
	hci_dev_unlock_bh(hdev);
	hci_dev_put(hdev);
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
482 483 484 485 486 487 488 489
static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
{
	struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
	struct sock *sk = sock->sk;
	int err = 0;

	lock_sock(sk);

490
	BT_DBG("sk %p", sk);
Linus Torvalds's avatar
Linus Torvalds committed
491 492 493 494 495 496

	if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_l2)) {
		err = -EINVAL;
		goto done;
	}

497
	if (sk->sk_type == SOCK_SEQPACKET && !la->l2_psm) {
498
		err = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
499 500 501
		goto done;
	}

502
	switch(sk->sk_state) {
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	case BT_CONNECT:
	case BT_CONNECT2:
	case BT_CONFIG:
		/* Already connecting */
		goto wait;

	case BT_CONNECTED:
		/* Already connected */
		goto done;

	case BT_OPEN:
	case BT_BOUND:
		/* Can connect */
		break;

	default:
		err = -EBADFD;
Linus Torvalds's avatar
Linus Torvalds committed
520 521 522 523
		goto done;
	}

	/* Set destination address and psm */
524
	bacpy(&bt_sk(sk)->dst, &la->l2_bdaddr);
Linus Torvalds's avatar
Linus Torvalds committed
525 526
	l2cap_pi(sk)->psm = la->l2_psm;

527
	if ((err = l2cap_do_connect(sk)))
Linus Torvalds's avatar
Linus Torvalds committed
528 529
		goto done;

530
wait:
531 532
	err = bt_sock_wait_state(sk, BT_CONNECTED,
			sock_sndtimeo(sk, flags & O_NONBLOCK));
Linus Torvalds's avatar
Linus Torvalds committed
533 534 535 536 537 538 539 540 541 542
done:
	release_sock(sk);
	return err;
}

int l2cap_sock_listen(struct socket *sock, int backlog)
{
	struct sock *sk = sock->sk;
	int err = 0;

543
	BT_DBG("sk %p backlog %d", sk, backlog);
Linus Torvalds's avatar
Linus Torvalds committed
544 545 546

	lock_sock(sk);

547
	if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
Linus Torvalds's avatar
Linus Torvalds committed
548 549 550 551 552 553 554 555 556
		err = -EBADFD;
		goto done;
	}

	if (!l2cap_pi(sk)->psm) {
		err = -EINVAL;
		goto done;
	}

557 558 559
	sk->sk_max_ack_backlog = backlog;
	sk->sk_ack_backlog = 0;
	sk->sk_state = BT_LISTEN;
Linus Torvalds's avatar
Linus Torvalds committed
560 561 562 563 564 565 566 567 568

done:
	release_sock(sk);
	return err;
}

int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
{
	DECLARE_WAITQUEUE(wait, current);
569
	struct sock *sk = sock->sk, *nsk;
Linus Torvalds's avatar
Linus Torvalds committed
570 571 572 573 574
	long timeo;
	int err = 0;

	lock_sock(sk);

575
	if (sk->sk_state != BT_LISTEN) {
Linus Torvalds's avatar
Linus Torvalds committed
576 577 578 579 580 581
		err = -EBADFD;
		goto done;
	}

	timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);

582
	BT_DBG("sk %p timeo %ld", sk, timeo);
Linus Torvalds's avatar
Linus Torvalds committed
583 584

	/* Wait for an incoming connection. (wake-one). */
585
	add_wait_queue_exclusive(sk->sk_sleep, &wait);
586
	while (!(nsk = bt_accept_dequeue(sk, newsock))) {
587
		set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds's avatar
Linus Torvalds committed
588 589 590 591 592 593 594 595 596
		if (!timeo) {
			err = -EAGAIN;
			break;
		}

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

597
		if (sk->sk_state != BT_LISTEN) {
Linus Torvalds's avatar
Linus Torvalds committed
598 599 600 601 602 603 604 605 606
			err = -EBADFD;
			break;
		}

		if (signal_pending(current)) {
			err = sock_intr_errno(timeo);
			break;
		}
	}
607
	set_current_state(TASK_RUNNING);
608
	remove_wait_queue(sk->sk_sleep, &wait);
Linus Torvalds's avatar
Linus Torvalds committed
609 610 611 612 613 614

	if (err)
		goto done;

	newsock->state = SS_CONNECTED;

615
	BT_DBG("new socket %p", nsk);
Linus Torvalds's avatar
Linus Torvalds committed
616 617 618 619 620 621 622 623 624 625 626

done:
	release_sock(sk);
	return err;
}

static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
{
	struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
	struct sock *sk = sock->sk;

627
	BT_DBG("sock %p, sk %p", sock, sk);
Linus Torvalds's avatar
Linus Torvalds committed
628 629 630 631 632

	addr->sa_family = AF_BLUETOOTH;
	*len = sizeof(struct sockaddr_l2);

	if (peer)
633
		bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Linus Torvalds's avatar
Linus Torvalds committed
634
	else
635
		bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Linus Torvalds's avatar
Linus Torvalds committed
636 637 638 639 640

	la->l2_psm = l2cap_pi(sk)->psm;
	return 0;
}

641 642 643 644 645 646 647 648 649 650
static inline int l2cap_do_send(struct sock *sk, struct msghdr *msg, int len)
{
	struct l2cap_conn *conn = l2cap_pi(sk)->conn;
	struct sk_buff *skb, **frag;
	int err, hlen, count, sent=0;
	struct l2cap_hdr *lh;

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

	/* First fragment (with L2CAP header) */
651
	if (sk->sk_type == SOCK_DGRAM)
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
		hlen = L2CAP_HDR_SIZE + 2;
	else
		hlen = L2CAP_HDR_SIZE;

	count = min_t(unsigned int, (conn->mtu - hlen), len);

	skb = bt_skb_send_alloc(sk, hlen + count,
			msg->msg_flags & MSG_DONTWAIT, &err);
	if (!skb)
		return err;

	/* Create L2CAP header */
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
	lh->cid = __cpu_to_le16(l2cap_pi(sk)->dcid);
	lh->len = __cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));

668
	if (sk->sk_type == SOCK_DGRAM)
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
		put_unaligned(l2cap_pi(sk)->psm, (u16 *) skb_put(skb, 2));

	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
		err = -EFAULT;
		goto fail;
	}

	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)
			goto fail;
		
		if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) {
			err = -EFAULT;
			goto fail;
		}

		sent += count;
		len  -= count;

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

	if ((err = hci_send_acl(conn->hcon, skb, 0)) < 0)
		goto fail;

	return sent;

fail:
	kfree_skb(skb);
	return err;
}

709
static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, int len)
Linus Torvalds's avatar
Linus Torvalds committed
710 711 712 713
{
	struct sock *sk = sock->sk;
	int err = 0;

714
	BT_DBG("sock %p, sk %p", sock, sk);
Linus Torvalds's avatar
Linus Torvalds committed
715

716
	if (sk->sk_err)
Linus Torvalds's avatar
Linus Torvalds committed
717 718 719 720 721
		return sock_error(sk);

	if (msg->msg_flags & MSG_OOB)
		return -EOPNOTSUPP;

722 723 724 725
	/* Check outgoing MTU */
	if (len > l2cap_pi(sk)->omtu)
		return -EINVAL;

Linus Torvalds's avatar
Linus Torvalds committed
726 727
	lock_sock(sk);

728
	if (sk->sk_state == BT_CONNECTED)
729
		err = l2cap_do_send(sk, msg, len);
Linus Torvalds's avatar
Linus Torvalds committed
730 731 732 733 734 735 736
	else
		err = -ENOTCONN;

	release_sock(sk);
	return err;
}

737
static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
Linus Torvalds's avatar
Linus Torvalds committed
738 739 740
{
	struct sock *sk = sock->sk;
	struct l2cap_options opts;
741
	int err = 0, len;
742
	u32 opt;
Linus Torvalds's avatar
Linus Torvalds committed
743

744
	BT_DBG("sk %p", sk);
Linus Torvalds's avatar
Linus Torvalds committed
745 746 747 748 749

	lock_sock(sk);

	switch (optname) {
	case L2CAP_OPTIONS:
750
		len = min_t(unsigned int, sizeof(opts), optlen);
751
		if (copy_from_user((char *)&opts, optval, len)) {
Linus Torvalds's avatar
Linus Torvalds committed
752 753 754
			err = -EFAULT;
			break;
		}
755 756 757 758 759
		l2cap_pi(sk)->imtu  = opts.imtu;
		l2cap_pi(sk)->omtu  = opts.omtu;
		break;

	case L2CAP_LM:
760
		if (get_user(opt, (u32 *)optval)) {
761 762 763 764 765
			err = -EFAULT;
			break;
		}

		l2cap_pi(sk)->link_mode = opt;
Linus Torvalds's avatar
Linus Torvalds committed
766 767 768 769 770
		break;

	default:
		err = -ENOPROTOOPT;
		break;
771
	}
Linus Torvalds's avatar
Linus Torvalds committed
772 773 774 775 776

	release_sock(sk);
	return err;
}

777
static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
Linus Torvalds's avatar
Linus Torvalds committed
778 779 780
{
	struct sock *sk = sock->sk;
	struct l2cap_options opts;
Linus Torvalds's avatar
Linus Torvalds committed
781 782
	struct l2cap_conninfo cinfo;
	int len, err = 0; 
Linus Torvalds's avatar
Linus Torvalds committed
783 784 785 786

	if (get_user(len, optlen))
		return -EFAULT;

Linus Torvalds's avatar
Linus Torvalds committed
787 788
	lock_sock(sk);

Linus Torvalds's avatar
Linus Torvalds committed
789 790 791 792 793 794
	switch (optname) {
	case L2CAP_OPTIONS:
		opts.imtu     = l2cap_pi(sk)->imtu;
		opts.omtu     = l2cap_pi(sk)->omtu;
		opts.flush_to = l2cap_pi(sk)->flush_to;

795
		len = min_t(unsigned int, len, sizeof(opts));
Linus Torvalds's avatar
Linus Torvalds committed
796
		if (copy_to_user(optval, (char *)&opts, len))
Linus Torvalds's avatar
Linus Torvalds committed
797 798 799 800
			err = -EFAULT;

		break;

801
	case L2CAP_LM:
802
		if (put_user(l2cap_pi(sk)->link_mode, (u32 *)optval))
803 804 805
			err = -EFAULT;
		break;

Linus Torvalds's avatar
Linus Torvalds committed
806
	case L2CAP_CONNINFO:
807
		if (sk->sk_state != BT_CONNECTED) {
Linus Torvalds's avatar
Linus Torvalds committed
808 809 810 811
			err = -ENOTCONN;
			break;
		}

812
		cinfo.hci_handle = l2cap_pi(sk)->conn->hcon->handle;
Linus Torvalds's avatar
Linus Torvalds committed
813

814
		len = min_t(unsigned int, len, sizeof(cinfo));
Linus Torvalds's avatar
Linus Torvalds committed
815 816
		if (copy_to_user(optval, (char *)&cinfo, len))
			err = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
817 818 819 820

		break;

	default:
Linus Torvalds's avatar
Linus Torvalds committed
821
		err = -ENOPROTOOPT;
Linus Torvalds's avatar
Linus Torvalds committed
822
		break;
823
	}
Linus Torvalds's avatar
Linus Torvalds committed
824

825 826
	release_sock(sk);
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
827 828
}

829 830 831
static int l2cap_sock_shutdown(struct socket *sock, int how)
{
	struct sock *sk = sock->sk;
832
	int err = 0;
833 834 835 836 837 838

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

	if (!sk) return 0;

	lock_sock(sk);
839 840 841 842
	if (!sk->sk_shutdown) {
		sk->sk_shutdown = SHUTDOWN_MASK;
		l2cap_sock_clear_timer(sk);
		__l2cap_sock_close(sk, 0);
843

844 845 846 847 848
		if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
			err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
	}
	release_sock(sk);
	return err;
849 850
}

Linus Torvalds's avatar
Linus Torvalds committed
851 852 853
static int l2cap_sock_release(struct socket *sock)
{
	struct sock *sk = sock->sk;
854
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
855

856
	BT_DBG("sock %p, sk %p", sock, sk);
Linus Torvalds's avatar
Linus Torvalds committed
857

858
	if (!sk) return 0;
Linus Torvalds's avatar
Linus Torvalds committed
859

860 861
	err = l2cap_sock_shutdown(sock, 2);

Linus Torvalds's avatar
Linus Torvalds committed
862
	sock_orphan(sk);
863 864
	l2cap_sock_kill(sk);
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
865 866
}

867 868
/* ---- L2CAP channels ---- */
static struct sock *__l2cap_get_chan_by_dcid(struct l2cap_chan_list *l, u16 cid)
Linus Torvalds's avatar
Linus Torvalds committed
869 870 871 872 873 874 875 876 877
{
	struct sock *s;
	for (s = l->head; s; s = l2cap_pi(s)->next_c) {
		if (l2cap_pi(s)->dcid == cid)
			break;
	}
	return s;
}

878
static struct sock *__l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
Linus Torvalds's avatar
Linus Torvalds committed
879 880 881 882 883 884 885 886
{
	struct sock *s;
	for (s = l->head; s; s = l2cap_pi(s)->next_c) {
		if (l2cap_pi(s)->scid == cid)
			break;
	}
	return s;
}
887 888 889

/* Find channel with given SCID.
 * Returns locked socket */
890
static inline struct sock *l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
Linus Torvalds's avatar
Linus Torvalds committed
891 892 893 894
{
	struct sock *s;
	read_lock(&l->lock);
	s = __l2cap_get_chan_by_scid(l, cid);
895
	if (s) bh_lock_sock(s);
Linus Torvalds's avatar
Linus Torvalds committed
896 897 898 899
	read_unlock(&l->lock);
	return s;
}

900
static u16 l2cap_alloc_cid(struct l2cap_chan_list *l)
Linus Torvalds's avatar
Linus Torvalds committed
901
{
902
	u16 cid = 0x0040;
Linus Torvalds's avatar
Linus Torvalds committed
903 904 905 906 907 908 909 910 911

	for (; cid < 0xffff; cid++) {
		if(!__l2cap_get_chan_by_scid(l, cid))
			return cid;
	}

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
912
static inline void __l2cap_chan_link(struct l2cap_chan_list *l, struct sock *sk)
Linus Torvalds's avatar
Linus Torvalds committed
913 914 915 916 917 918 919 920 921 922 923
{
	sock_hold(sk);

	if (l->head)
		l2cap_pi(l->head)->prev_c = sk;

	l2cap_pi(sk)->next_c = l->head;
	l2cap_pi(sk)->prev_c = NULL;
	l->head = sk;
}

Linus Torvalds's avatar
Linus Torvalds committed
924
static inline void l2cap_chan_unlink(struct l2cap_chan_list *l, struct sock *sk)
Linus Torvalds's avatar
Linus Torvalds committed
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
{
	struct sock *next = l2cap_pi(sk)->next_c, *prev = l2cap_pi(sk)->prev_c;

	write_lock(&l->lock);
	if (sk == l->head)
		l->head = next;

	if (next)
		l2cap_pi(next)->prev_c = prev;
	if (prev)
		l2cap_pi(prev)->next_c = next;
	write_unlock(&l->lock);

	__sock_put(sk);
}

static void __l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent)
{
	struct l2cap_chan_list *l = &conn->chan_list;

945
	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, l2cap_pi(sk)->psm, l2cap_pi(sk)->dcid);
Linus Torvalds's avatar
Linus Torvalds committed
946 947 948

	l2cap_pi(sk)->conn = conn;

949
	if (sk->sk_type == SOCK_SEQPACKET) {
950
		/* Alloc CID for connection-oriented socket */
Linus Torvalds's avatar
Linus Torvalds committed
951
		l2cap_pi(sk)->scid = l2cap_alloc_cid(l);
952
	} else if (sk->sk_type == SOCK_DGRAM) {
953 954 955 956
		/* Connectionless socket */
		l2cap_pi(sk)->scid = 0x0002;
		l2cap_pi(sk)->dcid = 0x0002;
		l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
Linus Torvalds's avatar
Linus Torvalds committed
957
	} else {
958
		/* Raw socket can send/recv signalling messages only */
Linus Torvalds's avatar
Linus Torvalds committed
959 960 961 962 963 964 965 966
		l2cap_pi(sk)->scid = 0x0001;
		l2cap_pi(sk)->dcid = 0x0001;
		l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
	}

	__l2cap_chan_link(l, sk);

	if (parent)
967
		bt_accept_enqueue(parent, sk);
Linus Torvalds's avatar
Linus Torvalds committed
968 969
}

Linus Torvalds's avatar
Linus Torvalds committed
970 971
/* Delete channel. 
 * Must be called on the locked socket. */
Linus Torvalds's avatar
Linus Torvalds committed
972 973
static void l2cap_chan_del(struct sock *sk, int err)
{
974
	struct l2cap_conn *conn = l2cap_pi(sk)->conn;
975
	struct sock *parent = bt_sk(sk)->parent;
Linus Torvalds's avatar
Linus Torvalds committed
976

977
	l2cap_sock_clear_timer(sk);
Linus Torvalds's avatar
Linus Torvalds committed
978

979
	BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
Linus Torvalds's avatar
Linus Torvalds committed
980

Linus Torvalds's avatar
Linus Torvalds committed
981
	if (conn) { 
Linus Torvalds's avatar
Linus Torvalds committed
982 983 984
		/* Unlink from channel list */
		l2cap_chan_unlink(&conn->chan_list, sk);
		l2cap_pi(sk)->conn = NULL;
985
		hci_conn_put(conn->hcon);
Linus Torvalds's avatar
Linus Torvalds committed
986 987
	}

988
	sk->sk_state  = BT_CLOSED;
989
	sk->sk_zapped = 1;
990 991 992
	
	if (err)
		sk->sk_err = err;
993 994

	if (parent)
995
		parent->sk_data_ready(parent, 0);
996
	else
997
		sk->sk_state_change(sk);
Linus Torvalds's avatar
Linus Torvalds committed
998 999 1000 1001 1002 1003 1004
}

static void l2cap_conn_ready(struct l2cap_conn *conn)
{
	struct l2cap_chan_list *l = &conn->chan_list;
	struct sock *sk;

1005
	BT_DBG("conn %p", conn);
Linus Torvalds's avatar
Linus Torvalds committed
1006 1007 1008 1009 1010 1011

	read_lock(&l->lock);

	for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
		bh_lock_sock(sk);

1012
		if (sk->sk_type != SOCK_SEQPACKET) {
1013
			l2cap_sock_clear_timer(sk);
1014 1015 1016
			sk->sk_state = BT_CONNECTED;
			sk->sk_state_change(sk);
		} else if (sk->sk_state == BT_CONNECT) {
1017
			struct l2cap_conn_req req;
Linus Torvalds's avatar
Linus Torvalds committed
1018 1019
			req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
			req.psm  = l2cap_pi(sk)->psm;
1020
			l2cap_send_req(conn, L2CAP_CONN_REQ, sizeof(req), &req);
Linus Torvalds's avatar
Linus Torvalds committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
		}

		bh_unlock_sock(sk);
	}

	read_unlock(&l->lock);
}

static void l2cap_chan_ready(struct sock *sk)
{
1031
	struct sock *parent = bt_sk(sk)->parent;
Linus Torvalds's avatar
Linus Torvalds committed
1032

1033
	BT_DBG("sk %p, parent %p", sk, parent);
Linus Torvalds's avatar
Linus Torvalds committed
1034 1035

	l2cap_pi(sk)->conf_state = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1036
	l2cap_sock_clear_timer(sk);
Linus Torvalds's avatar
Linus Torvalds committed
1037 1038 1039 1040 1041

	if (!parent) {
		/* Outgoing channel.
		 * Wake up socket sleeping on connect.
		 */
1042 1043
		sk->sk_state = BT_CONNECTED;
		sk->sk_state_change(sk);
Linus Torvalds's avatar
Linus Torvalds committed
1044
	} else {
Steven Cole's avatar
Steven Cole committed
1045
		/* Incoming channel.
Linus Torvalds's avatar
Linus Torvalds committed
1046 1047
		 * Wake up socket sleeping on accept.
		 */
1048
		parent->sk_data_ready(parent, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	}
}

/* Copy frame to all raw sockets on that connection */
void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
{
	struct l2cap_chan_list *l = &conn->chan_list;
	struct sk_buff *nskb;
	struct sock * sk;

1059
	BT_DBG("conn %p", conn);
Linus Torvalds's avatar
Linus Torvalds committed
1060 1061 1062

	read_lock(&l->lock);
	for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
1063
		if (sk->sk_type != SOCK_RAW)
Linus Torvalds's avatar
Linus Torvalds committed
1064 1065 1066 1067 1068 1069
			continue;

		/* Don't send frame to the socket it came from */
		if (skb->sk == sk)
			continue;

Linus Torvalds's avatar
Linus Torvalds committed
1070
		if (!(nskb = skb_clone(skb, GFP_ATOMIC)))
Linus Torvalds's avatar
Linus Torvalds committed
1071 1072
			continue;

1073 1074
		if (sock_queue_rcv_skb(sk, nskb))
			kfree_skb(nskb);
Linus Torvalds's avatar
Linus Torvalds committed
1075 1076 1077 1078
	}
	read_unlock(&l->lock);
}

1079 1080
/* ---- L2CAP signalling commands ---- */
static inline u8 l2cap_get_ident(struct l2cap_conn *conn)
Linus Torvalds's avatar
Linus Torvalds committed
1081
{
1082
	u8 id;
Linus Torvalds's avatar
Linus Torvalds committed
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

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

	spin_lock(&conn->lock);

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

	id = conn->tx_ident;

	spin_unlock(&conn->lock);

	return id;
}

1101
static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
1102
				u8 code, u8 ident, u16 dlen, void *data)
Linus Torvalds's avatar
Linus Torvalds committed
1103
{
1104
	struct sk_buff *skb, **frag;
1105 1106
	struct l2cap_cmd_hdr *cmd;
	struct l2cap_hdr *lh;
1107
	int len, count;
Linus Torvalds's avatar
Linus Torvalds committed
1108

1109
	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", conn, code, ident, dlen);
Linus Torvalds's avatar
Linus Torvalds committed
1110

1111
	len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
1112
	count = min_t(unsigned int, conn->mtu, len);
1113
	
1114
	skb = bt_skb_alloc(count, GFP_ATOMIC);
1115
	if (!skb)
Linus Torvalds's avatar
Linus Torvalds committed
1116 1117
		return NULL;

1118
	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1119
	lh->len = __cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
Linus Torvalds's avatar
Linus Torvalds committed
1120 1121
	lh->cid = __cpu_to_le16(0x0001);

1122
	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
Linus Torvalds's avatar
Linus Torvalds committed
1123 1124
	cmd->code  = code;
	cmd->ident = ident;
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	cmd->len   = __cpu_to_le16(dlen);

	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) {
1138
		count = min_t(unsigned int, conn->mtu, len);
1139

1140
		*frag = bt_skb_alloc(count, GFP_ATOMIC);
1141 1142 1143 1144
		if (!*frag)
			goto fail;
		
		memcpy(skb_put(*frag, count), data, count);
Linus Torvalds's avatar
Linus Torvalds committed
1145

1146 1147 1148 1149 1150
		len  -= count;
		data += count;
		
		frag = &(*frag)->next;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1151 1152

	return skb;
1153 1154 1155 1156

fail:
	kfree_skb(skb);
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1157 1158
}

1159
static int l2cap_send_req(struct l2cap_conn *conn, u8 code, u16 len, void *data)
Linus Torvalds's avatar
Linus Torvalds committed
1160
{
1161
	u8 ident = l2cap_get_ident(conn);
1162
	struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
Linus Torvalds's avatar
Linus Torvalds committed
1163

1164
	BT_DBG("code 0x%2.2x", code);
Linus Torvalds's avatar
Linus Torvalds committed
1165

1166
	if (!skb)
Linus Torvalds's avatar
Linus Torvalds committed
1167
		return -ENOMEM;
1168
	return hci_send_acl(conn->hcon, skb, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1169 1170
}

1171
static int l2cap_send_rsp(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
Linus Torvalds's avatar
Linus Torvalds committed
1172
{
1173
	struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
Linus Torvalds's avatar
Linus Torvalds committed
1174

1175
	BT_DBG("code 0x%2.2x", code);
Linus Torvalds's avatar
Linus Torvalds committed
1176

1177
	if (!skb)
Linus Torvalds's avatar
Linus Torvalds committed
1178
		return -ENOMEM;
1179
	return hci_send_acl(conn->hcon, skb, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1180 1181
}

1182
static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
Linus Torvalds's avatar
Linus Torvalds committed
1183
{
1184
	struct l2cap_conf_opt *opt = *ptr;
Linus Torvalds's avatar
Linus Torvalds committed
1185 1186
	int len;

1187 1188 1189
	len = L2CAP_CONF_OPT_SIZE + opt->len;
	*ptr += len;

Linus Torvalds's avatar
Linus Torvalds committed
1190
	*type = opt->type;
1191 1192
	*olen = opt->len;

Linus Torvalds's avatar
Linus Torvalds committed
1193 1194
	switch (opt->len) {
	case 1:
1195
		*val = *((u8 *) opt->val);
Linus Torvalds's avatar
Linus Torvalds committed
1196 1197 1198
		break;

	case 2:
1199
		*val = __le16_to_cpu(*((u16 *)opt->val));
Linus Torvalds's avatar
Linus Torvalds committed
1200 1201 1202
		break;

	case 4:
1203
		*val = __le32_to_cpu(*((u32 *)opt->val));
Linus Torvalds's avatar
Linus Torvalds committed
1204 1205 1206
		break;

	default:
1207
		*val = (unsigned long) opt->val;
Linus Torvalds's avatar
Linus Torvalds committed
1208
		break;
1209
	}
Linus Torvalds's avatar
Linus Torvalds committed
1210

1211
	BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
Linus Torvalds's avatar
Linus Torvalds committed
1212 1213 1214
	return len;
}

1215
static inline void l2cap_parse_conf_req(struct sock *sk, void *data, int len)
Linus Torvalds's avatar
Linus Torvalds committed
1216
{
1217 1218 1219
	int type, hint, olen; 
	unsigned long val;
	void *ptr = data;
Linus Torvalds's avatar
Linus Torvalds committed
1220

1221
	BT_DBG("sk %p len %d", sk, len);
Linus Torvalds's avatar
Linus Torvalds committed
1222 1223

	while (len >= L2CAP_CONF_OPT_SIZE) {
1224
		len -= l2cap_get_conf_opt(&ptr, &type, &olen, &val);
Linus Torvalds's avatar
Linus Torvalds committed
1225

Linus Torvalds's avatar
Linus Torvalds committed
1226 1227 1228
		hint  = type & 0x80;
		type &= 0x7f;

Linus Torvalds's avatar
Linus Torvalds committed
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
		switch (type) {
		case L2CAP_CONF_MTU:
			l2cap_pi(sk)->conf_mtu = val;
			break;

		case L2CAP_CONF_FLUSH_TO:
			l2cap_pi(sk)->flush_to = val;
			break;

		case L2CAP_CONF_QOS:
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1240 1241 1242 1243 1244
		
		default:
			if (hint)
				break;

1245
			/* FIXME: Reject unknown option */
Linus Torvalds's avatar
Linus Torvalds committed
1246
			break;
1247
		}
Linus Torvalds's avatar
Linus Torvalds committed
1248 1249 1250
	}
}

1251
static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
Linus Torvalds's avatar
Linus Torvalds committed
1252
{
1253
	struct l2cap_conf_opt *opt = *ptr;
Linus Torvalds's avatar
Linus Torvalds committed
1254

1255
	BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);
Linus Torvalds's avatar
Linus Torvalds committed
1256 1257 1258

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

Linus Torvalds's avatar
Linus Torvalds committed
1260 1261
	switch (len) {
	case 1:
1262
		*((u8 *) opt->val)  = val;
Linus Torvalds's avatar
Linus Torvalds committed
1263 1264 1265
		break;

	case 2:
1266
		*((u16 *) opt->val) = __cpu_to_le16(val);
Linus Torvalds's avatar
Linus Torvalds committed
1267 1268 1269
		break;

	case 4:
1270
		*((u32 *) opt->val) = __cpu_to_le32(val);
Linus Torvalds's avatar
Linus Torvalds committed
1271
		break;
1272 1273 1274 1275

	default:
		memcpy(opt->val, (void *) val, len);
		break;
1276
	}
Linus Torvalds's avatar
Linus Torvalds committed
1277 1278 1279 1280

	*ptr += L2CAP_CONF_OPT_SIZE + len;
}

1281
static int l2cap_build_conf_req(struct sock *sk, void *data)
Linus Torvalds's avatar
Linus Torvalds committed
1282 1283
{
	struct l2cap_pinfo *pi = l2cap_pi(sk);
1284
	struct l2cap_conf_req *req = data;
1285
	void *ptr = req->data;
Linus Torvalds's avatar
Linus Torvalds committed
1286

1287
	BT_DBG("sk %p", sk);
Linus Torvalds's avatar
Linus Torvalds committed
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301

	if (pi->imtu != L2CAP_DEFAULT_MTU)
		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->imtu);

	/* FIXME. Need actual value of the flush timeout */
	//if (flush_to != L2CAP_DEFAULT_FLUSH_TO)
	//   l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 2, pi->flush_to);

	req->dcid  = __cpu_to_le16(pi->dcid);
	req->flags = __cpu_to_le16(0);

	return ptr - data;
}

1302
static inline int l2cap_conf_output(struct sock *sk, void **ptr)
Linus Torvalds's avatar
Linus Torvalds committed
1303 1304 1305 1306
{
	struct l2cap_pinfo *pi = l2cap_pi(sk);
	int result = 0;

1307
	/* Configure output options and let the other side know
Linus Torvalds's avatar
Linus Torvalds committed
1308 1309 1310
	 * which ones we don't like.
	 */
	if (pi->conf_mtu < pi->omtu) {
1311
		l2cap_add_conf_opt(ptr, L2CAP_CONF_MTU, 2, pi->omtu);
Linus Torvalds's avatar
Linus Torvalds committed
1312 1313 1314 1315 1316
		result = L2CAP_CONF_UNACCEPT;
	} else {
		pi->omtu = pi->conf_mtu;
	}

1317
	BT_DBG("sk %p result %d", sk, result);
Linus Torvalds's avatar
Linus Torvalds committed
1318 1319 1320
	return result;
}

1321
static int l2cap_build_conf_rsp(struct sock *sk, void *data, int *result)
Linus Torvalds's avatar
Linus Torvalds committed
1322
{
1323
	struct l2cap_conf_rsp *rsp = data;
1324
	void *ptr = rsp->data;
1325
	u16 flags = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1326

1327
	BT_DBG("sk %p complete %d", sk, result ? 1 : 0);
Linus Torvalds's avatar
Linus Torvalds committed
1328 1329 1330

	if (result)
		*result = l2cap_conf_output(sk, &ptr);
1331 1332
	else
		flags = 0x0001;
Linus Torvalds's avatar
Linus Torvalds committed
1333 1334 1335

	rsp->scid   = __cpu_to_le16(l2cap_pi(sk)->dcid);
	rsp->result = __cpu_to_le16(result ? *result : 0);
1336
	rsp->flags  = __cpu_to_le16(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1337 1338 1339 1340

	return ptr - data;
}

1341
static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
Linus Torvalds's avatar
Linus Torvalds committed
1342 1343
{
	struct l2cap_chan_list *list = &conn->chan_list;
1344 1345
	struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
	struct l2cap_conn_rsp rsp;
Linus Torvalds's avatar
Linus Torvalds committed
1346
	struct sock *sk, *parent;
1347
	int result = 0, status = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1348

1349 1350
	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
	u16 psm  = req->psm;
Linus Torvalds's avatar
Linus Torvalds committed
1351

1352
	BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);
Linus Torvalds's avatar
Linus Torvalds committed
1353 1354

	/* Check if we have socket listening on psm */
1355 1356
	parent = l2cap_get_sock_by_psm(BT_LISTEN, psm, conn->src);
	if (!parent) {
1357
		result = L2CAP_CR_BAD_PSM;
1358
		goto sendresp;
1359
	}
Linus Torvalds's avatar
Linus Torvalds committed
1360

1361
	result = L2CAP_CR_NO_MEM;
Linus Torvalds's avatar
Linus Torvalds committed
1362 1363

	/* Check for backlog size */
1364 1365
	if (parent->sk_ack_backlog > parent->sk_max_ack_backlog) {
		BT_DBG("backlog full %d", parent->sk_ack_backlog); 
1366
		goto response;
1367
	}
Linus Torvalds's avatar
Linus Torvalds committed
1368

1369 1370 1371
	sk = l2cap_sock_alloc(NULL, BTPROTO_L2CAP, GFP_ATOMIC);
	if (!sk)
		goto response;
Linus Torvalds's avatar
Linus Torvalds committed
1372

1373 1374 1375 1376 1377
	write_lock(&list->lock);

	/* Check if we already have channel with that dcid */
	if (__l2cap_get_chan_by_dcid(list, scid)) {
		write_unlock(&list->lock);
1378
		sk->sk_zapped = 1;
1379 1380 1381
		l2cap_sock_kill(sk);
		goto response;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1382

1383 1384 1385
	hci_conn_hold(conn->hcon);

	l2cap_sock_init(sk, parent);
1386 1387
	bacpy(&bt_sk(sk)->src, conn->src);
	bacpy(&bt_sk(sk)->dst, conn->dst);
Linus Torvalds's avatar
Linus Torvalds committed
1388 1389 1390 1391
	l2cap_pi(sk)->psm  = psm;
	l2cap_pi(sk)->dcid = scid;

	__l2cap_chan_add(conn, sk, parent);
1392
	dcid = l2cap_pi(sk)->scid;
Linus Torvalds's avatar
Linus Torvalds committed
1393

1394
	l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
Linus Torvalds's avatar
Linus Torvalds committed
1395

1396 1397 1398
	/* Service level security */
	result = L2CAP_CR_PEND;
	status = L2CAP_CS_AUTHEN_PEND;
1399
	sk->sk_state = BT_CONNECT2;
1400 1401 1402 1403
	l2cap_pi(sk)->ident = cmd->ident;
	
	if (l2cap_pi(sk)->link_mode & L2CAP_LM_ENCRYPT) {
		if (!hci_conn_encrypt(conn->hcon))
1404
			goto done;
1405 1406
	} else if (l2cap_pi(sk)->link_mode & L2CAP_LM_AUTH) {
		if (!hci_conn_auth(conn->hcon))
1407
			goto done;
1408
	}
Linus Torvalds's avatar
Linus Torvalds committed
1409

1410
	sk->sk_state = BT_CONFIG;
1411
	result = status = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1412

1413
done:
1414
	write_unlock(&list->lock);
Linus Torvalds's avatar
Linus Torvalds committed
1415

1416 1417 1418 1419
response:
	bh_unlock_sock(parent);

sendresp:
Linus Torvalds's avatar
Linus Torvalds committed
1420
	rsp.scid   = __cpu_to_le16(scid);
1421 1422 1423
	rsp.dcid   = __cpu_to_le16(dcid);
	rsp.result = __cpu_to_le16(result);
	rsp.status = __cpu_to_le16(status);
1424
	l2cap_send_rsp(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
Linus Torvalds's avatar
Linus Torvalds committed
1425 1426 1427
	return 0;
}

1428
static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
Linus Torvalds's avatar
Linus Torvalds committed
1429
{
1430 1431
	struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
	u16 scid, dcid, result, status;
Linus Torvalds's avatar
Linus Torvalds committed
1432
	struct sock *sk;
1433
	char req[128];
Linus Torvalds's avatar
Linus Torvalds committed
1434 1435 1436 1437 1438 1439

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

1440
	BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);
Linus Torvalds's avatar
Linus Torvalds committed
1441 1442 1443 1444

	if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
		return -ENOENT;

1445 1446
	switch (result) {
	case L2CAP_CR_SUCCESS:
1447
		sk->sk_state = BT_CONFIG;
Linus Torvalds's avatar
Linus Torvalds committed
1448
		l2cap_pi(sk)->dcid = dcid;
1449
		l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
Linus Torvalds's avatar
Linus Torvalds committed
1450 1451

		l2cap_send_req(conn, L2CAP_CONF_REQ, l2cap_build_conf_req(sk, req), req);
1452 1453 1454 1455 1456 1457
		break;

	case L2CAP_CR_PEND:
		break;

	default:
Linus Torvalds's avatar
Linus Torvalds committed
1458
		l2cap_chan_del(sk, ECONNREFUSED);
1459
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1460 1461 1462 1463 1464 1465
	}

	bh_unlock_sock(sk);
	return 0;
}

1466
static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
Linus Torvalds's avatar
Linus Torvalds committed
1467
{
1468 1469 1470
	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
	u16 dcid, flags;
	u8  rsp[64];
Linus Torvalds's avatar
Linus Torvalds committed
1471 1472 1473 1474 1475 1476
	struct sock *sk;
	int result;

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

1477
	BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1478 1479 1480 1481

	if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid)))
		return -ENOENT;

1482
	l2cap_parse_conf_req(sk, req->data, cmd->len - sizeof(*req));
Linus Torvalds's avatar
Linus Torvalds committed
1483

1484
	if (flags & 0x0001) {
Linus Torvalds's avatar
Linus Torvalds committed
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
		/* Incomplete config. Send empty response. */
		l2cap_send_rsp(conn, cmd->ident, L2CAP_CONF_RSP, l2cap_build_conf_rsp(sk, rsp, NULL), rsp);
		goto unlock;
	}

	/* Complete config. */
	l2cap_send_rsp(conn, cmd->ident, L2CAP_CONF_RSP, l2cap_build_conf_rsp(sk, rsp, &result), rsp);

	if (result)
		goto unlock;

	/* Output config done */
1497
	l2cap_pi(sk)->conf_state |= L2CAP_CONF_OUTPUT_DONE;
Linus Torvalds's avatar
Linus Torvalds committed
1498

1499
	if (l2cap_pi(sk)->conf_state & L2CAP_CONF_INPUT_DONE) {
1500
		sk->sk_state = BT_CONNECTED;
Linus Torvalds's avatar
Linus Torvalds committed
1501
		l2cap_chan_ready(sk);
1502
	} else if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT)) {
1503
		u8 req[64];
Linus Torvalds's avatar
Linus Torvalds committed
1504 1505 1506 1507 1508 1509 1510 1511
		l2cap_send_req(conn, L2CAP_CONF_REQ, l2cap_build_conf_req(sk, req), req);
	}

unlock:
	bh_unlock_sock(sk);
	return 0;
}

1512
static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
Linus Torvalds's avatar
Linus Torvalds committed
1513
{
1514 1515
	struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
	u16 scid, flags, result;
Linus Torvalds's avatar
Linus Torvalds committed
1516 1517 1518 1519 1520 1521 1522
	struct sock *sk;
	int err = 0;

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

1523
	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", scid, flags, result);
Linus Torvalds's avatar
Linus Torvalds committed
1524 1525 1526 1527

	if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
		return -ENOENT;

1528 1529 1530
	switch (result) {
	case L2CAP_CONF_SUCCESS:
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1531

1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
	case L2CAP_CONF_UNACCEPT:
		if (++l2cap_pi(sk)->conf_retry < L2CAP_CONF_MAX_RETRIES) {
			char req[128];
			/* 
			   It does not make sense to adjust L2CAP parameters 
			   that are currently defined in the spec. We simply 
			   resend config request that we sent earlier. It is
			   stupid :) but it helps qualification testing
			   which expects at least some response from us.
			*/
			l2cap_send_req(conn, L2CAP_CONF_REQ,
				l2cap_build_conf_req(sk, req), req);
			goto done;
		}

	default: 
1548
		sk->sk_state = BT_DISCONN;
1549
		sk->sk_err   = ECONNRESET;
1550
		l2cap_sock_set_timer(sk, HZ * 5);
1551 1552 1553 1554 1555 1556
		{
			struct l2cap_disconn_req req;
			req.dcid = __cpu_to_le16(l2cap_pi(sk)->dcid);
			req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
			l2cap_send_req(conn, L2CAP_DISCONN_REQ, sizeof(req), &req);
		}
Linus Torvalds's avatar
Linus Torvalds committed
1557 1558 1559 1560 1561 1562 1563
		goto done;
	}

	if (flags & 0x01)
		goto done;

	/* Input config done */
1564
	l2cap_pi(sk)->conf_state |= L2CAP_CONF_INPUT_DONE;
Linus Torvalds's avatar
Linus Torvalds committed
1565

1566
	if (l2cap_pi(sk)->conf_state & L2CAP_CONF_OUTPUT_DONE) {
1567
		sk->sk_state = BT_CONNECTED;
Linus Torvalds's avatar
Linus Torvalds committed
1568 1569 1570 1571 1572 1573 1574 1575
		l2cap_chan_ready(sk);
	}

done:
	bh_unlock_sock(sk);
	return err;
}

1576
static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
Linus Torvalds's avatar
Linus Torvalds committed
1577
{
1578 1579 1580
	struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
	struct l2cap_disconn_rsp rsp;
	u16 dcid, scid;
Linus Torvalds's avatar
Linus Torvalds committed
1581 1582 1583 1584 1585
	struct sock *sk;

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

1586
	BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
Linus Torvalds's avatar
Linus Torvalds committed
1587 1588 1589 1590 1591 1592

	if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid)))
		return 0;

	rsp.dcid = __cpu_to_le16(l2cap_pi(sk)->scid);
	rsp.scid = __cpu_to_le16(l2cap_pi(sk)->dcid);
1593
	l2cap_send_rsp(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
Linus Torvalds's avatar
Linus Torvalds committed
1594

1595
	sk->sk_shutdown = SHUTDOWN_MASK;
1596
	
Linus Torvalds's avatar
Linus Torvalds committed
1597 1598 1599 1600 1601 1602 1603
	l2cap_chan_del(sk, ECONNRESET);
	bh_unlock_sock(sk);

	l2cap_sock_kill(sk);
	return 0;
}

1604
static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
Linus Torvalds's avatar
Linus Torvalds committed
1605
{
1606 1607
	struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
	u16 dcid, scid;
Linus Torvalds's avatar
Linus Torvalds committed
1608 1609 1610 1611 1612
	struct sock *sk;

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

1613
	BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
Linus Torvalds's avatar
Linus Torvalds committed
1614 1615

	if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
1616
		return 0;
1617
	l2cap_chan_del(sk, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1618 1619 1620 1621 1622 1623
	bh_unlock_sock(sk);

	l2cap_sock_kill(sk);
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1624
static inline void l2cap_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
1625
{
1626
	u8 *data = skb->data;
Linus Torvalds's avatar
Linus Torvalds committed
1627
	int len = skb->len;
1628
	struct l2cap_cmd_hdr cmd;
Linus Torvalds's avatar
Linus Torvalds committed
1629 1630 1631 1632 1633 1634 1635
	int err = 0;

	while (len >= L2CAP_CMD_HDR_SIZE) {
		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
		data += L2CAP_CMD_HDR_SIZE;
		len  -= L2CAP_CMD_HDR_SIZE;

Linus Torvalds's avatar
Linus Torvalds committed
1636
		cmd.len = __le16_to_cpu(cmd.len);
Linus Torvalds's avatar
Linus Torvalds committed
1637

1638
		BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd.len, cmd.ident);
Linus Torvalds's avatar
Linus Torvalds committed
1639 1640

		if (cmd.len > len || !cmd.ident) {
1641
			BT_DBG("corrupted command");
Linus Torvalds's avatar
Linus Torvalds committed
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
			break;
		}

		switch (cmd.code) {
		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, 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_COMMAND_REJ:
			/* FIXME: We should process this */
			l2cap_raw_recv(conn, skb);
			break;

		case L2CAP_ECHO_REQ:
			l2cap_send_rsp(conn, cmd.ident, L2CAP_ECHO_RSP, cmd.len, data);
			break;

		case L2CAP_ECHO_RSP:
		case L2CAP_INFO_REQ:
		case L2CAP_INFO_RSP:
			l2cap_raw_recv(conn, skb);
			break;

		default:
1686
			BT_ERR("Uknown signaling command 0x%2.2x", cmd.code);
Linus Torvalds's avatar
Linus Torvalds committed
1687 1688
			err = -EINVAL;
			break;
1689
		}
Linus Torvalds's avatar
Linus Torvalds committed
1690 1691

		if (err) {
1692
			struct l2cap_cmd_rej rej;
1693
			BT_DBG("error %d", err);
Linus Torvalds's avatar
Linus Torvalds committed
1694 1695

			/* FIXME: Map err to a valid reason. */
Linus Torvalds's avatar
Linus Torvalds committed
1696
			rej.reason = __cpu_to_le16(0);
1697
			l2cap_send_rsp(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
Linus Torvalds's avatar
Linus Torvalds committed
1698 1699 1700 1701 1702 1703
		}

		data += cmd.len;
		len  -= cmd.len;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1704
	kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1705 1706
}

1707
static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
1708 1709 1710
{
	struct sock *sk;

1711 1712 1713
	sk = l2cap_get_chan_by_scid(&conn->chan_list, cid);
	if (!sk) {
		BT_DBG("unknown cid 0x%4.4x", cid);
Linus Torvalds's avatar
Linus Torvalds committed
1714 1715 1716
		goto drop;
	}

1717
	BT_DBG("sk %p, len %d", sk, skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
1718

1719
	if (sk->sk_state != BT_CONNECTED)
Linus Torvalds's avatar
Linus Torvalds committed
1720 1721 1722 1723 1724
		goto drop;

	if (l2cap_pi(sk)->imtu < skb->len)
		goto drop;

1725 1726 1727 1728 1729 1730 1731
	/* 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 */ 
	
	if (!sock_queue_rcv_skb(sk, skb))
		goto done;
Linus Torvalds's avatar
Linus Torvalds committed
1732 1733

drop:
Linus Torvalds's avatar
Linus Torvalds committed
1734
	kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1735

1736 1737
done:
	if (sk) bh_unlock_sock(sk);
Linus Torvalds's avatar
Linus Torvalds committed
1738 1739 1740
	return 0;
}

1741
static inline int l2cap_conless_channel(struct l2cap_conn *conn, u16 psm, struct sk_buff *skb)
1742 1743 1744 1745 1746 1747 1748 1749 1750
{
	struct sock *sk;

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

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

1751
	if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED)
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
		goto drop;

	if (l2cap_pi(sk)->imtu < skb->len)
		goto drop;

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

drop:
	kfree_skb(skb);

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

Linus Torvalds's avatar
Linus Torvalds committed
1768 1769
static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
{
1770 1771
	struct l2cap_hdr *lh = (void *) skb->data;
	u16 cid, psm, len;
Linus Torvalds's avatar
Linus Torvalds committed
1772 1773

	skb_pull(skb, L2CAP_HDR_SIZE);
Linus Torvalds's avatar
Linus Torvalds committed
1774 1775
	cid = __le16_to_cpu(lh->cid);
	len = __le16_to_cpu(lh->len);
Linus Torvalds's avatar
Linus Torvalds committed
1776

1777
	BT_DBG("len %d, cid 0x%4.4x", len, cid);
Linus Torvalds's avatar
Linus Torvalds committed
1778

1779 1780
	switch (cid) {
	case 0x0001:
Linus Torvalds's avatar
Linus Torvalds committed
1781
		l2cap_sig_channel(conn, skb);
1782 1783 1784
		break;

	case 0x0002:
1785
		psm = get_unaligned((u16 *) skb->data);
1786 1787 1788 1789 1790
		skb_pull(skb, 2);
		l2cap_conless_channel(conn, psm, skb);
		break;
		
	default:
Linus Torvalds's avatar
Linus Torvalds committed
1791
		l2cap_data_channel(conn, cid, skb);
1792 1793
		break;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1794 1795
}

1796
/* ---- L2CAP interface with lower layer (HCI) ---- */
Linus Torvalds's avatar
Linus Torvalds committed
1797

1798
static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
1799 1800 1801
{
	int exact = 0, lm1 = 0, lm2 = 0;
	register struct sock *sk;
1802
	struct hlist_node *node;
Linus Torvalds's avatar
Linus Torvalds committed
1803

1804 1805
	if (type != ACL_LINK)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1806

1807
	BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
Linus Torvalds's avatar
Linus Torvalds committed
1808

1809 1810
	/* Find listening sockets and check their link_mode */
	read_lock(&l2cap_sk_list.lock);
1811
	sk_for_each(sk, node, &l2cap_sk_list.head) {
1812
		if (sk->sk_state != BT_LISTEN)
1813
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
1814

1815
		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
1816 1817
			lm1 |= (HCI_LM_ACCEPT | l2cap_pi(sk)->link_mode);
			exact++;
1818
		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
1819 1820 1821
			lm2 |= (HCI_LM_ACCEPT | l2cap_pi(sk)->link_mode);
	}
	read_unlock(&l2cap_sk_list.lock);
Linus Torvalds's avatar
Linus Torvalds committed
1822

1823
	return exact ? lm1 : lm2;
Linus Torvalds's avatar
Linus Torvalds committed
1824 1825
}

1826
static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
Linus Torvalds's avatar
Linus Torvalds committed
1827
{
1828
	BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
Linus Torvalds's avatar
Linus Torvalds committed
1829

1830
	if (hcon->type != ACL_LINK)
Linus Torvalds's avatar
Linus Torvalds committed
1831 1832
		return 0;

1833 1834 1835 1836 1837 1838 1839
	if (!status) {
		struct l2cap_conn *conn;

		conn = l2cap_conn_add(hcon, status);
		if (conn)
			l2cap_conn_ready(conn);
	} else 
1840
		l2cap_conn_del(hcon, bt_err(status));
1841 1842
	
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1843 1844
}

1845
static int l2cap_disconn_ind(struct hci_conn *hcon, u8 reason)
Linus Torvalds's avatar
Linus Torvalds committed
1846
{
1847
	BT_DBG("hcon %p reason %d", hcon, reason);
Linus Torvalds's avatar
Linus Torvalds committed
1848

1849
	if (hcon->type != ACL_LINK)
Linus Torvalds's avatar
Linus Torvalds committed
1850 1851
		return 0;

1852
	l2cap_conn_del(hcon, bt_err(reason));
1853 1854
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
1855

1856
static int l2cap_auth_cfm(struct hci_conn *hcon, u8 status)
1857 1858 1859
{
	struct l2cap_chan_list *l;
	struct l2cap_conn *conn;
1860
	struct l2cap_conn_rsp rsp;
1861 1862 1863 1864 1865 1866
	struct sock *sk;
	int result;
	
	if (!(conn = hcon->l2cap_data))
		return 0;
	l = &conn->chan_list;
Linus Torvalds's avatar
Linus Torvalds committed
1867

1868
	BT_DBG("conn %p", conn);
Linus Torvalds's avatar
Linus Torvalds committed
1869

1870
	read_lock(&l->lock);
Linus Torvalds's avatar
Linus Torvalds committed
1871

1872 1873
	for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
		bh_lock_sock(sk);
Linus Torvalds's avatar
Linus Torvalds committed
1874

1875
		if (sk->sk_state != BT_CONNECT2 ||
1876 1877 1878
				(l2cap_pi(sk)->link_mode & L2CAP_LM_ENCRYPT)) {
			bh_unlock_sock(sk);
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
1879 1880
		}

1881
		if (!status) {
1882
			sk->sk_state = BT_CONFIG;
1883 1884
			result = 0;
		} else {
1885
			sk->sk_state = BT_DISCONN;
1886 1887
			l2cap_sock_set_timer(sk, HZ/10);
			result = L2CAP_CR_SEC_BLOCK;
Linus Torvalds's avatar
Linus Torvalds committed
1888 1889
		}

1890 1891 1892 1893
		rsp.scid   = __cpu_to_le16(l2cap_pi(sk)->dcid);
		rsp.dcid   = __cpu_to_le16(l2cap_pi(sk)->scid);
		rsp.result = __cpu_to_le16(result);
		rsp.status = __cpu_to_le16(0);
1894
		l2cap_send_rsp(conn, l2cap_pi(sk)->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
Linus Torvalds's avatar
Linus Torvalds committed
1895

1896
		bh_unlock_sock(sk);
Linus Torvalds's avatar
Linus Torvalds committed
1897 1898
	}

1899 1900
	read_unlock(&l->lock);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1901 1902
}

1903
static int l2cap_encrypt_cfm(struct hci_conn *hcon, u8 status)
Linus Torvalds's avatar
Linus Torvalds committed
1904
{
1905 1906
	struct l2cap_chan_list *l;
	struct l2cap_conn *conn;
1907
	struct l2cap_conn_rsp rsp;
1908 1909 1910 1911 1912 1913
	struct sock *sk;
	int result;
	
	if (!(conn = hcon->l2cap_data))
		return 0;
	l = &conn->chan_list;
Linus Torvalds's avatar
Linus Torvalds committed
1914

1915
	BT_DBG("conn %p", conn);
Linus Torvalds's avatar
Linus Torvalds committed
1916

1917 1918 1919 1920 1921
	read_lock(&l->lock);

	for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
		bh_lock_sock(sk);

1922
		if (sk->sk_state != BT_CONNECT2) {
1923 1924 1925 1926 1927
			bh_unlock_sock(sk);
			continue;
		}

		if (!status) {
1928
			sk->sk_state = BT_CONFIG;
1929 1930
			result = 0;
		} else {
1931
			sk->sk_state = BT_DISCONN;
1932 1933 1934 1935 1936 1937 1938 1939
			l2cap_sock_set_timer(sk, HZ/10);
			result = L2CAP_CR_SEC_BLOCK;
		}

		rsp.scid   = __cpu_to_le16(l2cap_pi(sk)->dcid);
		rsp.dcid   = __cpu_to_le16(l2cap_pi(sk)->scid);
		rsp.result = __cpu_to_le16(result);
		rsp.status = __cpu_to_le16(0);
1940
		l2cap_send_rsp(conn, l2cap_pi(sk)->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
Linus Torvalds's avatar
Linus Torvalds committed
1941

1942 1943
		bh_unlock_sock(sk);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1944

1945
	read_unlock(&l->lock);
Linus Torvalds's avatar
Linus Torvalds committed
1946 1947 1948
	return 0;
}

1949
static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
Linus Torvalds's avatar
Linus Torvalds committed
1950
{
1951
	struct l2cap_conn *conn = hcon->l2cap_data;
Linus Torvalds's avatar
Linus Torvalds committed
1952

1953
	if (!conn && !(conn = l2cap_conn_add(hcon, 0)))
Linus Torvalds's avatar
Linus Torvalds committed
1954 1955
		goto drop;

1956
	BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1957 1958

	if (flags & ACL_START) {
1959
		struct l2cap_hdr *hdr;
1960
		int len;
Linus Torvalds's avatar
Linus Torvalds committed
1961

Linus Torvalds's avatar
Linus Torvalds committed
1962
		if (conn->rx_len) {
1963 1964 1965
			BT_ERR("Unexpected start frame (len %d)", skb->len);
			kfree_skb(conn->rx_skb);
			conn->rx_skb = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1966 1967 1968
			conn->rx_len = 0;
		}

1969
		if (skb->len < 2) {
1970
			BT_ERR("Frame is too short (len %d)", skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
1971 1972 1973
			goto drop;
		}

1974
		hdr = (struct l2cap_hdr *) skb->data;
1975
		len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
Linus Torvalds's avatar
Linus Torvalds committed
1976

1977
		if (len == skb->len) {
Linus Torvalds's avatar
Linus Torvalds committed
1978 1979 1980 1981 1982
			/* Complete frame received */
			l2cap_recv_frame(conn, skb);
			return 0;
		}

1983 1984 1985 1986 1987 1988 1989 1990
		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);
			goto drop;
		}

Linus Torvalds's avatar
Linus Torvalds committed
1991
		/* Allocate skb for the complete frame (with header) */
1992
		if (!(conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC)))
Linus Torvalds's avatar
Linus Torvalds committed
1993 1994 1995
			goto drop;

		memcpy(skb_put(conn->rx_skb, skb->len), skb->data, skb->len);
1996
		conn->rx_len = len - skb->len;
Linus Torvalds's avatar
Linus Torvalds committed
1997
	} else {
1998
		BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
Linus Torvalds's avatar
Linus Torvalds committed
1999

Linus Torvalds's avatar
Linus Torvalds committed
2000
		if (!conn->rx_len) {
2001
			BT_ERR("Unexpected continuation frame (len %d)", skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
2002 2003 2004
			goto drop;
		}

Linus Torvalds's avatar
Linus Torvalds committed
2005
		if (skb->len > conn->rx_len) {
2006
			BT_ERR("Fragment is too long (len %d, expected %d)",
2007 2008 2009 2010
					skb->len, conn->rx_len);
			kfree_skb(conn->rx_skb);
			conn->rx_skb = NULL;
			conn->rx_len = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
			goto drop;
		}

		memcpy(skb_put(conn->rx_skb, skb->len), skb->data, skb->len);
		conn->rx_len -= skb->len;

		if (!conn->rx_len) {
			/* Complete frame received */
			l2cap_recv_frame(conn, conn->rx_skb);
			conn->rx_skb = NULL;
		}
	}

drop:
Linus Torvalds's avatar
Linus Torvalds committed
2025
	kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
2026 2027 2028
	return 0;
}

2029
/* ---- Proc fs support ---- */
2030 2031
#ifdef CONFIG_PROC_FS
static void *l2cap_seq_start(struct seq_file *seq, loff_t *pos)
2032 2033
{
	struct sock *sk;
2034
	struct hlist_node *node;
2035
	loff_t l = *pos;
2036

2037
	read_lock_bh(&l2cap_sk_list.lock);
2038

2039
	sk_for_each(sk, node, &l2cap_sk_list.head)
2040
		if (!l--)
2041 2042 2043 2044
			goto found;
	sk = NULL;
found:
	return sk;
2045
}
2046

2047 2048 2049
static void *l2cap_seq_next(struct seq_file *seq, void *e, loff_t *pos)
{
	(*pos)++;
2050
	return sk_next(e);
2051
}
2052

2053 2054 2055
static void l2cap_seq_stop(struct seq_file *seq, void *e)
{
	read_unlock_bh(&l2cap_sk_list.lock);
2056 2057
}

2058
static int  l2cap_seq_show(struct seq_file *seq, void *e)
2059
{
2060 2061 2062 2063 2064
	struct sock *sk = e;
	struct l2cap_pinfo *pi = l2cap_pi(sk);

	seq_printf(seq, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d 0x%x\n",
			batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst), 
2065 2066
			sk->sk_state, pi->psm, pi->scid, pi->dcid, pi->imtu,
			pi->omtu, pi->link_mode);
2067 2068 2069 2070 2071 2072 2073 2074 2075
	return 0;
}

static struct seq_operations l2cap_seq_ops = {
	.start  = l2cap_seq_start,
	.next   = l2cap_seq_next,
	.stop   = l2cap_seq_stop,
	.show   = l2cap_seq_show 
};
2076

2077 2078 2079 2080
static int l2cap_seq_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &l2cap_seq_ops);
}
2081

2082
static struct file_operations l2cap_seq_fops = {
2083
	.owner	 = THIS_MODULE,
2084 2085 2086 2087 2088
	.open    = l2cap_seq_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release,
};
2089

Maksim Krasnyanskiy's avatar
Maksim Krasnyanskiy committed
2090
static int  __init l2cap_proc_init(void)
2091 2092 2093 2094
{
        struct proc_dir_entry *p = create_proc_entry("l2cap", S_IRUGO, proc_bt);
        if (!p)
                return -ENOMEM;
Maksim Krasnyanskiy's avatar
Maksim Krasnyanskiy committed
2095
	p->owner     = THIS_MODULE;
2096 2097 2098
        p->proc_fops = &l2cap_seq_fops;
        return 0;
}
2099

Maksim Krasnyanskiy's avatar
Maksim Krasnyanskiy committed
2100
static void __exit l2cap_proc_cleanup(void)
2101 2102 2103
{
        remove_proc_entry("l2cap", proc_bt);
}
2104

2105
#else /* CONFIG_PROC_FS */
2106

Maksim Krasnyanskiy's avatar
Maksim Krasnyanskiy committed
2107
static int  __init l2cap_proc_init(void)
2108 2109 2110 2111
{
        return 0;
}

Maksim Krasnyanskiy's avatar
Maksim Krasnyanskiy committed
2112
static void __exit l2cap_proc_cleanup(void)
2113
{
2114
        return;
2115
}
2116
#endif /* CONFIG_PROC_FS */
2117 2118

static struct proto_ops l2cap_sock_ops = {
2119
	.family  =      PF_BLUETOOTH,
2120
	.owner   =	THIS_MODULE,
2121 2122 2123 2124 2125 2126 2127
	.release =      l2cap_sock_release,
	.bind    =      l2cap_sock_bind,
	.connect =      l2cap_sock_connect,
	.listen  =      l2cap_sock_listen,
	.accept  =      l2cap_sock_accept,
	.getname =      l2cap_sock_getname,
	.sendmsg =      l2cap_sock_sendmsg,
2128 2129
	.recvmsg =      bt_sock_recvmsg,
	.poll    =      bt_sock_poll,
2130 2131 2132 2133 2134 2135
	.mmap    =      sock_no_mmap,
	.socketpair =   sock_no_socketpair,
	.ioctl      =   sock_no_ioctl,
	.shutdown   =   l2cap_sock_shutdown,
	.setsockopt =   l2cap_sock_setsockopt,
	.getsockopt =   l2cap_sock_getsockopt
Linus Torvalds's avatar
Linus Torvalds committed
2136 2137
};

2138
static struct net_proto_family l2cap_sock_family_ops = {
2139
	.family =       PF_BLUETOOTH,
2140
	.create	=       l2cap_sock_create,
2141
	.owner	=	THIS_MODULE,
Linus Torvalds's avatar
Linus Torvalds committed
2142 2143
};

2144
static struct hci_proto l2cap_hci_proto = {
2145 2146 2147 2148 2149 2150 2151 2152
	.name =         "L2CAP",
	.id   =         HCI_PROTO_L2CAP,
	.connect_ind =  l2cap_connect_ind,
	.connect_cfm =  l2cap_connect_cfm,
	.disconn_ind =  l2cap_disconn_ind,
	.auth_cfm    =  l2cap_auth_cfm,
	.encrypt_cfm =  l2cap_encrypt_cfm,
	.recv_acldata =	l2cap_recv_acldata
Linus Torvalds's avatar
Linus Torvalds committed
2153 2154 2155 2156
};

int __init l2cap_init(void)
{
2157
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
2158

2159
	if ((err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops))) {
2160
		BT_ERR("L2CAP socket registration failed");
2161
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
2162 2163
	}

2164
	if ((err = hci_register_proto(&l2cap_hci_proto))) {
2165
		BT_ERR("L2CAP protocol registration failed");
2166
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
2167 2168
	}

2169 2170
	l2cap_proc_init();
	
2171 2172 2173
	BT_INFO("L2CAP ver %s", VERSION);
	BT_INFO("L2CAP socket layer initialized");

Linus Torvalds's avatar
Linus Torvalds committed
2174 2175 2176
	return 0;
}

Maksim Krasnyanskiy's avatar
Maksim Krasnyanskiy committed
2177
void __exit l2cap_cleanup(void)
Linus Torvalds's avatar
Linus Torvalds committed
2178
{
2179
	l2cap_proc_cleanup();
Linus Torvalds's avatar
Linus Torvalds committed
2180

2181
	/* Unregister socket and protocol */
2182
	if (bt_sock_unregister(BTPROTO_L2CAP))
2183
		BT_ERR("L2CAP socket unregistration failed");
Linus Torvalds's avatar
Linus Torvalds committed
2184

2185
	if (hci_unregister_proto(&l2cap_hci_proto))
2186
		BT_ERR("L2CAP protocol unregistration failed");
Linus Torvalds's avatar
Linus Torvalds committed
2187 2188
}

2189 2190 2191 2192 2193 2194 2195 2196 2197
void l2cap_load(void)
{
	/* Dummy function to trigger automatic L2CAP module loading by 
	   other modules that use L2CAP sockets but don not use any other
	   symbols from it. */
	return;
}
EXPORT_SYMBOL(l2cap_load);

Linus Torvalds's avatar
Linus Torvalds committed
2198 2199
module_init(l2cap_init);
module_exit(l2cap_cleanup);
Linus Torvalds's avatar
Linus Torvalds committed
2200 2201

MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
2202
MODULE_DESCRIPTION("Bluetooth L2CAP ver " VERSION);
Linus Torvalds's avatar
Linus Torvalds committed
2203
MODULE_LICENSE("GPL");