core.c 16.2 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4
   BNEP implementation for Linux Bluetooth stack (BlueZ).
   Copyright (C) 2001-2002 Inventel Systemes
   Written 2001-2002 by
5
	Clément Moreau <clement.moreau@inventel.fr>
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17
	David Libault  <david.libault@inventel.fr>

   Copyright (C) 2002 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
18 19 20
   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 已提交
21 22
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

23 24
   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
L
Linus Torvalds 已提交
25 26 27 28
   SOFTWARE IS DISCLAIMED.
*/

#include <linux/module.h>
S
Szymon Janc 已提交
29
#include <linux/kthread.h>
L
Linus Torvalds 已提交
30 31 32 33 34
#include <linux/file.h>
#include <linux/etherdevice.h>
#include <asm/unaligned.h>

#include <net/bluetooth/bluetooth.h>
35
#include <net/bluetooth/l2cap.h>
36
#include <net/bluetooth/hci_core.h>
L
Linus Torvalds 已提交
37 38 39

#include "bnep.h"

40 41
#define VERSION "1.3"

42 43
static bool compress_src = true;
static bool compress_dst = true;
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53

static LIST_HEAD(bnep_session_list);
static DECLARE_RWSEM(bnep_session_sem);

static struct bnep_session *__bnep_get_session(u8 *dst)
{
	struct bnep_session *s;

	BT_DBG("");

54
	list_for_each_entry(s, &bnep_session_list, list)
55
		if (ether_addr_equal(dst, s->eh.h_source))
L
Linus Torvalds 已提交
56
			return s;
57

L
Linus Torvalds 已提交
58 59 60 61 62
	return NULL;
}

static void __bnep_link_session(struct bnep_session *s)
{
63
	list_add(&s->list, &bnep_session_list);
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
}

static void __bnep_unlink_session(struct bnep_session *s)
{
	list_del(&s->list);
}

static int bnep_send(struct bnep_session *s, void *data, size_t len)
{
	struct socket *sock = s->sock;
	struct kvec iv = { data, len };

	return kernel_sendmsg(sock, &s->msg, &iv, 1, len);
}

static int bnep_send_rsp(struct bnep_session *s, u8 ctrl, u16 resp)
{
	struct bnep_control_rsp rsp;
	rsp.type = BNEP_CONTROL;
	rsp.ctrl = ctrl;
	rsp.resp = htons(resp);
	return bnep_send(s, &rsp, sizeof(rsp));
}

#ifdef CONFIG_BT_BNEP_PROTO_FILTER
static inline void bnep_set_default_proto_filter(struct bnep_session *s)
{
	/* (IPv4, ARP)  */
92 93
	s->proto_filter[0].start = ETH_P_IP;
	s->proto_filter[0].end   = ETH_P_ARP;
L
Linus Torvalds 已提交
94
	/* (RARP, AppleTalk) */
95 96
	s->proto_filter[1].start = ETH_P_RARP;
	s->proto_filter[1].end   = ETH_P_AARP;
L
Linus Torvalds 已提交
97
	/* (IPX, IPv6) */
98 99
	s->proto_filter[2].start = ETH_P_IPX;
	s->proto_filter[2].end   = ETH_P_IPV6;
L
Linus Torvalds 已提交
100 101 102
}
#endif

A
Al Viro 已提交
103
static int bnep_ctrl_set_netfilter(struct bnep_session *s, __be16 *data, int len)
L
Linus Torvalds 已提交
104 105 106 107 108 109
{
	int n;

	if (len < 2)
		return -EILSEQ;

110
	n = get_unaligned_be16(data);
111 112
	data++;
	len -= 2;
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125

	if (len < n)
		return -EILSEQ;

	BT_DBG("filter len %d", n);

#ifdef CONFIG_BT_BNEP_PROTO_FILTER
	n /= 4;
	if (n <= BNEP_MAX_PROTO_FILTERS) {
		struct bnep_proto_filter *f = s->proto_filter;
		int i;

		for (i = 0; i < n; i++) {
126 127
			f[i].start = get_unaligned_be16(data++);
			f[i].end   = get_unaligned_be16(data++);
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

			BT_DBG("proto filter start %d end %d",
				f[i].start, f[i].end);
		}

		if (i < BNEP_MAX_PROTO_FILTERS)
			memset(f + i, 0, sizeof(*f));

		if (n == 0)
			bnep_set_default_proto_filter(s);

		bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_SUCCESS);
	} else {
		bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_LIMIT_REACHED);
	}
#else
	bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_UNSUPPORTED_REQ);
#endif
	return 0;
}

static int bnep_ctrl_set_mcfilter(struct bnep_session *s, u8 *data, int len)
{
	int n;

	if (len < 2)
		return -EILSEQ;

156
	n = get_unaligned_be16(data);
157 158
	data += 2;
	len -= 2;
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168

	if (len < n)
		return -EILSEQ;

	BT_DBG("filter len %d", n);

#ifdef CONFIG_BT_BNEP_MC_FILTER
	n /= (ETH_ALEN * 2);

	if (n > 0) {
169 170
		int i;

L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179
		s->mc_filter = 0;

		/* Always send broadcast */
		set_bit(bnep_mc_hash(s->dev->broadcast), (ulong *) &s->mc_filter);

		/* Add address ranges to the multicast hash */
		for (; n > 0; n--) {
			u8 a1[6], *a2;

180 181 182 183
			memcpy(a1, data, ETH_ALEN);
			data += ETH_ALEN;
			a2 = data;
			data += ETH_ALEN;
184

185
			BT_DBG("mc filter %pMR -> %pMR", a1, a2);
L
Linus Torvalds 已提交
186 187 188 189

			/* Iterate from a1 to a2 */
			set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter);
			while (memcmp(a1, a2, 6) < 0 && s->mc_filter != ~0LL) {
190 191 192 193 194
				/* Increment a1 */
				i = 5;
				while (i >= 0 && ++a1[i--] == 0)
					;

L
Linus Torvalds 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
				set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter);
			}
		}
	}

	BT_DBG("mc filter hash 0x%llx", s->mc_filter);

	bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_SUCCESS);
#else
	bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_FILTER_UNSUPPORTED_REQ);
#endif
	return 0;
}

static int bnep_rx_control(struct bnep_session *s, void *data, int len)
{
	u8  cmd = *(u8 *)data;
	int err = 0;

214 215
	data++;
	len--;
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

	switch (cmd) {
	case BNEP_CMD_NOT_UNDERSTOOD:
	case BNEP_SETUP_CONN_RSP:
	case BNEP_FILTER_NET_TYPE_RSP:
	case BNEP_FILTER_MULTI_ADDR_RSP:
		/* Ignore these for now */
		break;

	case BNEP_FILTER_NET_TYPE_SET:
		err = bnep_ctrl_set_netfilter(s, data, len);
		break;

	case BNEP_FILTER_MULTI_ADDR_SET:
		err = bnep_ctrl_set_mcfilter(s, data, len);
		break;

233 234 235 236
	case BNEP_SETUP_CONN_REQ:
		err = bnep_send_rsp(s, BNEP_SETUP_CONN_RSP, BNEP_CONN_NOT_ALLOWED);
		break;

L
Linus Torvalds 已提交
237 238 239 240 241
	default: {
			u8 pkt[3];
			pkt[0] = BNEP_CONTROL;
			pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
			pkt[2] = cmd;
242
			err = bnep_send(s, pkt, sizeof(pkt));
L
Linus Torvalds 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		}
		break;
	}

	return err;
}

static int bnep_rx_extension(struct bnep_session *s, struct sk_buff *skb)
{
	struct bnep_ext_hdr *h;
	int err = 0;

	do {
		h = (void *) skb->data;
		if (!skb_pull(skb, sizeof(*h))) {
			err = -EILSEQ;
			break;
		}

		BT_DBG("type 0x%x len %d", h->type, h->len);
263

L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
		switch (h->type & BNEP_TYPE_MASK) {
		case BNEP_EXT_CONTROL:
			bnep_rx_control(s, skb->data, skb->len);
			break;

		default:
			/* Unknown extension, skip it. */
			break;
		}

		if (!skb_pull(skb, h->len)) {
			err = -EILSEQ;
			break;
		}
	} while (!err && (h->type & BNEP_EXT_HEADER));
279

L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287 288 289 290
	return err;
}

static u8 __bnep_rx_hlen[] = {
	ETH_HLEN,     /* BNEP_GENERAL */
	0,            /* BNEP_CONTROL */
	2,            /* BNEP_COMPRESSED */
	ETH_ALEN + 2, /* BNEP_COMPRESSED_SRC_ONLY */
	ETH_ALEN + 2  /* BNEP_COMPRESSED_DST_ONLY */
};

291
static int bnep_rx_frame(struct bnep_session *s, struct sk_buff *skb)
L
Linus Torvalds 已提交
292 293 294
{
	struct net_device *dev = s->dev;
	struct sk_buff *nskb;
295
	u8 type, ctrl_type;
L
Linus Torvalds 已提交
296

S
Stephen Hemminger 已提交
297
	dev->stats.rx_bytes += skb->len;
L
Linus Torvalds 已提交
298

299 300
	type = *(u8 *) skb->data;
	skb_pull(skb, 1);
301
	ctrl_type = *(u8 *)skb->data;
L
Linus Torvalds 已提交
302

303
	if ((type & BNEP_TYPE_MASK) >= sizeof(__bnep_rx_hlen))
L
Linus Torvalds 已提交
304
		goto badframe;
305

L
Linus Torvalds 已提交
306
	if ((type & BNEP_TYPE_MASK) == BNEP_CONTROL) {
307 308 309 310 311
		if (bnep_rx_control(s, skb->data, skb->len) < 0) {
			dev->stats.tx_errors++;
			kfree_skb(skb);
			return 0;
		}
L
Linus Torvalds 已提交
312

313 314 315 316
		if (!(type & BNEP_EXT_HEADER)) {
			kfree_skb(skb);
			return 0;
		}
L
Linus Torvalds 已提交
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
		/* Verify and pull ctrl message since it's already processed */
		switch (ctrl_type) {
		case BNEP_SETUP_CONN_REQ:
			/* Pull: ctrl type (1 b), len (1 b), data (len bytes) */
			if (!skb_pull(skb, 2 + *(u8 *)(skb->data + 1) * 2))
				goto badframe;
			break;
		case BNEP_FILTER_MULTI_ADDR_SET:
		case BNEP_FILTER_NET_TYPE_SET:
			/* Pull: ctrl type (1 b), len (2 b), data (len bytes) */
			if (!skb_pull(skb, 3 + *(u16 *)(skb->data + 1) * 2))
				goto badframe;
			break;
		default:
			kfree_skb(skb);
			return 0;
		}
	} else {
		skb_reset_mac_header(skb);

		/* Verify and pull out header */
		if (!skb_pull(skb, __bnep_rx_hlen[type & BNEP_TYPE_MASK]))
			goto badframe;
L
Linus Torvalds 已提交
341

342 343
		s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2));
	}
L
Linus Torvalds 已提交
344 345 346 347 348 349 350

	if (type & BNEP_EXT_HEADER) {
		if (bnep_rx_extension(s, skb) < 0)
			goto badframe;
	}

	/* Strip 802.1p header */
351
	if (ntohs(s->eh.h_proto) == ETH_P_8021Q) {
L
Linus Torvalds 已提交
352 353
		if (!skb_pull(skb, 4))
			goto badframe;
A
Al Viro 已提交
354
		s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2));
L
Linus Torvalds 已提交
355
	}
356

L
Linus Torvalds 已提交
357 358 359 360
	/* We have to alloc new skb and copy data here :(. Because original skb
	 * may not be modified and because of the alignment requirements. */
	nskb = alloc_skb(2 + ETH_HLEN + skb->len, GFP_KERNEL);
	if (!nskb) {
S
Stephen Hemminger 已提交
361
		dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
362 363 364 365 366 367 368 369 370 371
		kfree_skb(skb);
		return -ENOMEM;
	}
	skb_reserve(nskb, 2);

	/* Decompress header and construct ether frame */
	switch (type & BNEP_TYPE_MASK) {
	case BNEP_COMPRESSED:
		memcpy(__skb_put(nskb, ETH_HLEN), &s->eh, ETH_HLEN);
		break;
372

L
Linus Torvalds 已提交
373 374
	case BNEP_COMPRESSED_SRC_ONLY:
		memcpy(__skb_put(nskb, ETH_ALEN), s->eh.h_dest, ETH_ALEN);
375
		memcpy(__skb_put(nskb, ETH_ALEN), skb_mac_header(skb), ETH_ALEN);
A
Al Viro 已提交
376
		put_unaligned(s->eh.h_proto, (__be16 *) __skb_put(nskb, 2));
L
Linus Torvalds 已提交
377 378 379
		break;

	case BNEP_COMPRESSED_DST_ONLY:
380
		memcpy(__skb_put(nskb, ETH_ALEN), skb_mac_header(skb),
381
								ETH_ALEN);
382
		memcpy(__skb_put(nskb, ETH_ALEN + 2), s->eh.h_source,
383
								ETH_ALEN + 2);
L
Linus Torvalds 已提交
384 385 386
		break;

	case BNEP_GENERAL:
387
		memcpy(__skb_put(nskb, ETH_ALEN * 2), skb_mac_header(skb),
388
								ETH_ALEN * 2);
A
Al Viro 已提交
389
		put_unaligned(s->eh.h_proto, (__be16 *) __skb_put(nskb, 2));
L
Linus Torvalds 已提交
390 391 392
		break;
	}

393
	skb_copy_from_linear_data(skb, __skb_put(nskb, skb->len), skb->len);
L
Linus Torvalds 已提交
394
	kfree_skb(skb);
395

S
Stephen Hemminger 已提交
396
	dev->stats.rx_packets++;
L
Linus Torvalds 已提交
397 398 399 400 401 402
	nskb->ip_summed = CHECKSUM_NONE;
	nskb->protocol  = eth_type_trans(nskb, dev);
	netif_rx_ni(nskb);
	return 0;

badframe:
S
Stephen Hemminger 已提交
403
	dev->stats.rx_errors++;
L
Linus Torvalds 已提交
404 405 406 407 408 409 410 411 412 413 414
	kfree_skb(skb);
	return 0;
}

static u8 __bnep_tx_types[] = {
	BNEP_GENERAL,
	BNEP_COMPRESSED_SRC_ONLY,
	BNEP_COMPRESSED_DST_ONLY,
	BNEP_COMPRESSED
};

415
static int bnep_tx_frame(struct bnep_session *s, struct sk_buff *skb)
L
Linus Torvalds 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
{
	struct ethhdr *eh = (void *) skb->data;
	struct socket *sock = s->sock;
	struct kvec iv[3];
	int len = 0, il = 0;
	u8 type = 0;

	BT_DBG("skb %p dev %p type %d", skb, skb->dev, skb->pkt_type);

	if (!skb->dev) {
		/* Control frame sent by us */
		goto send;
	}

	iv[il++] = (struct kvec) { &type, 1 };
	len++;

433
	if (compress_src && ether_addr_equal(eh->h_dest, s->eh.h_source))
L
Linus Torvalds 已提交
434 435
		type |= 0x01;

436
	if (compress_dst && ether_addr_equal(eh->h_source, s->eh.h_dest))
L
Linus Torvalds 已提交
437 438 439 440 441 442 443 444 445 446 447
		type |= 0x02;

	if (type)
		skb_pull(skb, ETH_ALEN * 2);

	type = __bnep_tx_types[type];
	switch (type) {
	case BNEP_COMPRESSED_SRC_ONLY:
		iv[il++] = (struct kvec) { eh->h_source, ETH_ALEN };
		len += ETH_ALEN;
		break;
448

L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457
	case BNEP_COMPRESSED_DST_ONLY:
		iv[il++] = (struct kvec) { eh->h_dest, ETH_ALEN };
		len += ETH_ALEN;
		break;
	}

send:
	iv[il++] = (struct kvec) { skb->data, skb->len };
	len += skb->len;
458

L
Linus Torvalds 已提交
459 460 461 462 463 464 465
	/* FIXME: linearize skb */
	{
		len = kernel_sendmsg(sock, &s->msg, iv, il, len);
	}
	kfree_skb(skb);

	if (len > 0) {
S
Stephen Hemminger 已提交
466 467
		s->dev->stats.tx_bytes += len;
		s->dev->stats.tx_packets++;
L
Linus Torvalds 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
		return 0;
	}

	return len;
}

static int bnep_session(void *arg)
{
	struct bnep_session *s = arg;
	struct net_device *dev = s->dev;
	struct sock *sk = s->sock->sk;
	struct sk_buff *skb;
	wait_queue_t wait;

	BT_DBG("");

	set_user_nice(current, -15);

	init_waitqueue_entry(&wait, current);
E
Eric Dumazet 已提交
487
	add_wait_queue(sk_sleep(sk), &wait);
488
	while (1) {
L
Linus Torvalds 已提交
489 490
		set_current_state(TASK_INTERRUPTIBLE);

491
		if (atomic_read(&s->terminate))
492
			break;
493
		/* RX */
L
Linus Torvalds 已提交
494 495
		while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
			skb_orphan(skb);
496 497 498 499
			if (!skb_linearize(skb))
				bnep_rx_frame(s, skb);
			else
				kfree_skb(skb);
L
Linus Torvalds 已提交
500 501 502 503
		}

		if (sk->sk_state != BT_CONNECTED)
			break;
504

505
		/* TX */
L
Linus Torvalds 已提交
506 507 508 509
		while ((skb = skb_dequeue(&sk->sk_write_queue)))
			if (bnep_tx_frame(s, skb))
				break;
		netif_wake_queue(dev);
510

L
Linus Torvalds 已提交
511 512
		schedule();
	}
513
	__set_current_state(TASK_RUNNING);
E
Eric Dumazet 已提交
514
	remove_wait_queue(sk_sleep(sk), &wait);
L
Linus Torvalds 已提交
515 516 517 518 519 520 521

	/* Cleanup session */
	down_write(&bnep_session_sem);

	/* Delete network device */
	unregister_netdev(dev);

522 523 524
	/* Wakeup user-space polling for socket errors */
	s->sock->sk->sk_err = EUNATCH;

E
Eric Dumazet 已提交
525
	wake_up_interruptible(sk_sleep(s->sock->sk));
526

L
Linus Torvalds 已提交
527 528 529 530 531 532 533
	/* Release the socket */
	fput(s->sock->file);

	__bnep_unlink_session(s);

	up_write(&bnep_session_sem);
	free_netdev(dev);
534
	module_put_and_exit(0);
L
Linus Torvalds 已提交
535 536 537
	return 0;
}

538 539
static struct device *bnep_get_device(struct bnep_session *session)
{
540
	struct l2cap_conn *conn = l2cap_pi(session->sock->sk)->chan->conn;
541

542
	if (!conn || !conn->hcon)
543 544
		return NULL;

545
	return &conn->hcon->dev;
546 547
}

548 549 550 551
static struct device_type bnep_type = {
	.name	= "bluetooth",
};

L
Linus Torvalds 已提交
552 553
int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock)
{
554
	u32 valid_flags = 0;
L
Linus Torvalds 已提交
555 556 557 558 559 560 561
	struct net_device *dev;
	struct bnep_session *s, *ss;
	u8 dst[ETH_ALEN], src[ETH_ALEN];
	int err;

	BT_DBG("");

562 563 564
	if (!l2cap_is_socket(sock))
		return -EBADFD;

565 566 567
	if (req->flags & ~valid_flags)
		return -EINVAL;

568 569
	baswap((void *) dst, &l2cap_pi(sock->sk)->chan->dst);
	baswap((void *) src, &l2cap_pi(sock->sk)->chan->src);
L
Linus Torvalds 已提交
570 571 572

	/* session struct allocated as private part of net_device */
	dev = alloc_netdev(sizeof(struct bnep_session),
573 574 575
			   (*req->device) ? req->device : "bnep%d",
			   NET_NAME_UNKNOWN,
			   bnep_net_setup);
576 577
	if (!dev)
		return -ENOMEM;
L
Linus Torvalds 已提交
578 579 580 581 582 583 584 585 586

	down_write(&bnep_session_sem);

	ss = __bnep_get_session(dst);
	if (ss && ss->state == BT_CONNECTED) {
		err = -EEXIST;
		goto failed;
	}

587
	s = netdev_priv(dev);
L
Linus Torvalds 已提交
588 589

	/* This is rx header therefore addresses are swapped.
590
	 * ie. eh.h_dest is our local address. */
L
Linus Torvalds 已提交
591 592 593 594
	memcpy(s->eh.h_dest,   &src, ETH_ALEN);
	memcpy(s->eh.h_source, &dst, ETH_ALEN);
	memcpy(dev->dev_addr, s->eh.h_dest, ETH_ALEN);

595
	s->dev   = dev;
L
Linus Torvalds 已提交
596 597 598
	s->sock  = sock;
	s->role  = req->role;
	s->state = BT_CONNECTED;
599

L
Linus Torvalds 已提交
600 601 602 603 604 605 606 607 608 609 610 611
	s->msg.msg_flags = MSG_NOSIGNAL;

#ifdef CONFIG_BT_BNEP_MC_FILTER
	/* Set default mc filter */
	set_bit(bnep_mc_hash(dev->broadcast), (ulong *) &s->mc_filter);
#endif

#ifdef CONFIG_BT_BNEP_PROTO_FILTER
	/* Set default protocol filter */
	bnep_set_default_proto_filter(s);
#endif

612
	SET_NETDEV_DEV(dev, bnep_get_device(s));
613
	SET_NETDEV_DEVTYPE(dev, &bnep_type);
614

L
Linus Torvalds 已提交
615
	err = register_netdev(dev);
616
	if (err)
L
Linus Torvalds 已提交
617 618 619
		goto failed;

	__bnep_link_session(s);
620

621
	__module_get(THIS_MODULE);
S
Szymon Janc 已提交
622 623
	s->task = kthread_run(bnep_session, s, "kbnepd %s", dev->name);
	if (IS_ERR(s->task)) {
L
Linus Torvalds 已提交
624
		/* Session thread start failed, gotta cleanup. */
625
		module_put(THIS_MODULE);
L
Linus Torvalds 已提交
626 627
		unregister_netdev(dev);
		__bnep_unlink_session(s);
S
Szymon Janc 已提交
628
		err = PTR_ERR(s->task);
L
Linus Torvalds 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
		goto failed;
	}

	up_write(&bnep_session_sem);
	strcpy(req->device, dev->name);
	return 0;

failed:
	up_write(&bnep_session_sem);
	free_netdev(dev);
	return err;
}

int bnep_del_connection(struct bnep_conndel_req *req)
{
644
	u32 valid_flags = 0;
L
Linus Torvalds 已提交
645 646 647 648 649
	struct bnep_session *s;
	int  err = 0;

	BT_DBG("");

650 651 652
	if (req->flags & ~valid_flags)
		return -EINVAL;

L
Linus Torvalds 已提交
653 654 655
	down_read(&bnep_session_sem);

	s = __bnep_get_session(req->dst);
656 657 658 659
	if (s) {
		atomic_inc(&s->terminate);
		wake_up_process(s->task);
	} else
L
Linus Torvalds 已提交
660 661 662 663 664 665 666 667
		err = -ENOENT;

	up_read(&bnep_session_sem);
	return err;
}

static void __bnep_copy_ci(struct bnep_conninfo *ci, struct bnep_session *s)
{
668 669
	u32 valid_flags = 0;

670
	memset(ci, 0, sizeof(*ci));
L
Linus Torvalds 已提交
671 672
	memcpy(ci->dst, s->eh.h_source, ETH_ALEN);
	strcpy(ci->device, s->dev->name);
673
	ci->flags = s->flags & valid_flags;
L
Linus Torvalds 已提交
674 675 676 677 678 679
	ci->state = s->state;
	ci->role  = s->role;
}

int bnep_get_connlist(struct bnep_connlist_req *req)
{
680
	struct bnep_session *s;
L
Linus Torvalds 已提交
681 682 683 684
	int err = 0, n = 0;

	down_read(&bnep_session_sem);

685
	list_for_each_entry(s, &bnep_session_list, list) {
L
Linus Torvalds 已提交
686 687 688
		struct bnep_conninfo ci;

		__bnep_copy_ci(&ci, s);
689

L
Linus Torvalds 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
		if (copy_to_user(req->ci, &ci, sizeof(ci))) {
			err = -EFAULT;
			break;
		}

		if (++n >= req->cnum)
			break;

		req->ci++;
	}
	req->cnum = n;

	up_read(&bnep_session_sem);
	return err;
}

int bnep_get_conninfo(struct bnep_conninfo *ci)
{
	struct bnep_session *s;
	int err = 0;

	down_read(&bnep_session_sem);

	s = __bnep_get_session(ci->dst);
	if (s)
		__bnep_copy_ci(ci, s);
	else
		err = -ENOENT;

	up_read(&bnep_session_sem);
	return err;
}

static int __init bnep_init(void)
724
{
L
Linus Torvalds 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
	char flt[50] = "";

#ifdef CONFIG_BT_BNEP_PROTO_FILTER
	strcat(flt, "protocol ");
#endif

#ifdef CONFIG_BT_BNEP_MC_FILTER
	strcat(flt, "multicast");
#endif

	BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION);
	if (flt[0])
		BT_INFO("BNEP filters: %s", flt);

	bnep_sock_init();
	return 0;
}

static void __exit bnep_exit(void)
{
	bnep_sock_cleanup();
}

module_init(bnep_init);
module_exit(bnep_exit);

751 752 753 754 755 756
module_param(compress_src, bool, 0644);
MODULE_PARM_DESC(compress_src, "Compress sources headers");

module_param(compress_dst, bool, 0644);
MODULE_PARM_DESC(compress_dst, "Compress destination headers");

757
MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
L
Linus Torvalds 已提交
758 759 760 761
MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");
MODULE_ALIAS("bt-proto-4");