blob: 24e91eb7f649f17d7136b59825c08503b62978eb [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BNEP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2001-2002 Inventel Systemes
4 Written 2001-2002 by
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 Clément Moreau <clement.moreau@inventel.fr>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 David Libault <david.libault@inventel.fr>
7
8 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License version 2 as
12 published by the Free Software Foundation;
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
17 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090018 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090023 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
24 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 SOFTWARE IS DISCLAIMED.
26*/
27
28/*
29 * $Id: core.c,v 1.20 2002/08/04 21:23:58 maxk Exp $
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/module.h>
33
34#include <linux/kernel.h>
35#include <linux/sched.h>
36#include <linux/signal.h>
37#include <linux/init.h>
38#include <linux/wait.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070039#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/net.h>
42#include <net/sock.h>
43
44#include <linux/socket.h>
45#include <linux/file.h>
46
47#include <linux/netdevice.h>
48#include <linux/etherdevice.h>
49#include <linux/skbuff.h>
50
51#include <asm/unaligned.h>
52
53#include <net/bluetooth/bluetooth.h>
Marcel Holtmann0a85b962006-07-06 13:09:02 +020054#include <net/bluetooth/hci_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/bluetooth/l2cap.h>
56
57#include "bnep.h"
58
59#ifndef CONFIG_BT_BNEP_DEBUG
60#undef BT_DBG
61#define BT_DBG(D...)
62#endif
63
64#define VERSION "1.2"
65
66static LIST_HEAD(bnep_session_list);
67static DECLARE_RWSEM(bnep_session_sem);
68
69static struct bnep_session *__bnep_get_session(u8 *dst)
70{
71 struct bnep_session *s;
72 struct list_head *p;
73
74 BT_DBG("");
75
76 list_for_each(p, &bnep_session_list) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090077 s = list_entry(p, struct bnep_session, list);
Kris Katterjohnd3f4a682006-01-09 16:01:43 -080078 if (!compare_ether_addr(dst, s->eh.h_source))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return s;
80 }
81 return NULL;
82}
83
84static void __bnep_link_session(struct bnep_session *s)
85{
86 /* It's safe to call __module_get() here because sessions are added
87 by the socket layer which has to hold the refference to this module.
88 */
89 __module_get(THIS_MODULE);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090090 list_add(&s->list, &bnep_session_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93static void __bnep_unlink_session(struct bnep_session *s)
94{
95 list_del(&s->list);
96 module_put(THIS_MODULE);
97}
98
99static int bnep_send(struct bnep_session *s, void *data, size_t len)
100{
101 struct socket *sock = s->sock;
102 struct kvec iv = { data, len };
103
104 return kernel_sendmsg(sock, &s->msg, &iv, 1, len);
105}
106
107static int bnep_send_rsp(struct bnep_session *s, u8 ctrl, u16 resp)
108{
109 struct bnep_control_rsp rsp;
110 rsp.type = BNEP_CONTROL;
111 rsp.ctrl = ctrl;
112 rsp.resp = htons(resp);
113 return bnep_send(s, &rsp, sizeof(rsp));
114}
115
116#ifdef CONFIG_BT_BNEP_PROTO_FILTER
117static inline void bnep_set_default_proto_filter(struct bnep_session *s)
118{
119 /* (IPv4, ARP) */
Al Viroe41d2162006-11-08 00:27:36 -0800120 s->proto_filter[0].start = ETH_P_IP;
121 s->proto_filter[0].end = ETH_P_ARP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* (RARP, AppleTalk) */
Al Viroe41d2162006-11-08 00:27:36 -0800123 s->proto_filter[1].start = ETH_P_RARP;
124 s->proto_filter[1].end = ETH_P_AARP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /* (IPX, IPv6) */
Al Viroe41d2162006-11-08 00:27:36 -0800126 s->proto_filter[2].start = ETH_P_IPX;
127 s->proto_filter[2].end = ETH_P_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129#endif
130
Al Viro1bc5d442006-11-08 00:27:57 -0800131static int bnep_ctrl_set_netfilter(struct bnep_session *s, __be16 *data, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 int n;
134
135 if (len < 2)
136 return -EILSEQ;
137
Harvey Harrison83985312008-05-02 16:25:46 -0700138 n = get_unaligned_be16(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 data++; len -= 2;
140
141 if (len < n)
142 return -EILSEQ;
143
144 BT_DBG("filter len %d", n);
145
146#ifdef CONFIG_BT_BNEP_PROTO_FILTER
147 n /= 4;
148 if (n <= BNEP_MAX_PROTO_FILTERS) {
149 struct bnep_proto_filter *f = s->proto_filter;
150 int i;
151
152 for (i = 0; i < n; i++) {
Harvey Harrison83985312008-05-02 16:25:46 -0700153 f[i].start = get_unaligned_be16(data++);
154 f[i].end = get_unaligned_be16(data++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 BT_DBG("proto filter start %d end %d",
157 f[i].start, f[i].end);
158 }
159
160 if (i < BNEP_MAX_PROTO_FILTERS)
161 memset(f + i, 0, sizeof(*f));
162
163 if (n == 0)
164 bnep_set_default_proto_filter(s);
165
166 bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_SUCCESS);
167 } else {
168 bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_LIMIT_REACHED);
169 }
170#else
171 bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_UNSUPPORTED_REQ);
172#endif
173 return 0;
174}
175
176static int bnep_ctrl_set_mcfilter(struct bnep_session *s, u8 *data, int len)
177{
178 int n;
179
180 if (len < 2)
181 return -EILSEQ;
182
Harvey Harrison83985312008-05-02 16:25:46 -0700183 n = get_unaligned_be16(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 data += 2; len -= 2;
185
186 if (len < n)
187 return -EILSEQ;
188
189 BT_DBG("filter len %d", n);
190
191#ifdef CONFIG_BT_BNEP_MC_FILTER
192 n /= (ETH_ALEN * 2);
193
194 if (n > 0) {
195 s->mc_filter = 0;
196
197 /* Always send broadcast */
198 set_bit(bnep_mc_hash(s->dev->broadcast), (ulong *) &s->mc_filter);
199
200 /* Add address ranges to the multicast hash */
201 for (; n > 0; n--) {
202 u8 a1[6], *a2;
203
204 memcpy(a1, data, ETH_ALEN); data += ETH_ALEN;
205 a2 = data; data += ETH_ALEN;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 BT_DBG("mc filter %s -> %s",
208 batostr((void *) a1), batostr((void *) a2));
209
210 #define INCA(a) { int i = 5; while (i >=0 && ++a[i--] == 0); }
211
212 /* Iterate from a1 to a2 */
213 set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter);
214 while (memcmp(a1, a2, 6) < 0 && s->mc_filter != ~0LL) {
215 INCA(a1);
216 set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter);
217 }
218 }
219 }
220
221 BT_DBG("mc filter hash 0x%llx", s->mc_filter);
222
223 bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_SUCCESS);
224#else
225 bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_FILTER_UNSUPPORTED_REQ);
226#endif
227 return 0;
228}
229
230static int bnep_rx_control(struct bnep_session *s, void *data, int len)
231{
232 u8 cmd = *(u8 *)data;
233 int err = 0;
234
235 data++; len--;
236
237 switch (cmd) {
238 case BNEP_CMD_NOT_UNDERSTOOD:
239 case BNEP_SETUP_CONN_REQ:
240 case BNEP_SETUP_CONN_RSP:
241 case BNEP_FILTER_NET_TYPE_RSP:
242 case BNEP_FILTER_MULTI_ADDR_RSP:
243 /* Ignore these for now */
244 break;
245
246 case BNEP_FILTER_NET_TYPE_SET:
247 err = bnep_ctrl_set_netfilter(s, data, len);
248 break;
249
250 case BNEP_FILTER_MULTI_ADDR_SET:
251 err = bnep_ctrl_set_mcfilter(s, data, len);
252 break;
253
254 default: {
255 u8 pkt[3];
256 pkt[0] = BNEP_CONTROL;
257 pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
258 pkt[2] = cmd;
259 bnep_send(s, pkt, sizeof(pkt));
260 }
261 break;
262 }
263
264 return err;
265}
266
267static int bnep_rx_extension(struct bnep_session *s, struct sk_buff *skb)
268{
269 struct bnep_ext_hdr *h;
270 int err = 0;
271
272 do {
273 h = (void *) skb->data;
274 if (!skb_pull(skb, sizeof(*h))) {
275 err = -EILSEQ;
276 break;
277 }
278
279 BT_DBG("type 0x%x len %d", h->type, h->len);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 switch (h->type & BNEP_TYPE_MASK) {
282 case BNEP_EXT_CONTROL:
283 bnep_rx_control(s, skb->data, skb->len);
284 break;
285
286 default:
287 /* Unknown extension, skip it. */
288 break;
289 }
290
291 if (!skb_pull(skb, h->len)) {
292 err = -EILSEQ;
293 break;
294 }
295 } while (!err && (h->type & BNEP_EXT_HEADER));
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return err;
298}
299
300static u8 __bnep_rx_hlen[] = {
301 ETH_HLEN, /* BNEP_GENERAL */
302 0, /* BNEP_CONTROL */
303 2, /* BNEP_COMPRESSED */
304 ETH_ALEN + 2, /* BNEP_COMPRESSED_SRC_ONLY */
305 ETH_ALEN + 2 /* BNEP_COMPRESSED_DST_ONLY */
306};
307#define BNEP_RX_TYPES (sizeof(__bnep_rx_hlen) - 1)
308
309static inline int bnep_rx_frame(struct bnep_session *s, struct sk_buff *skb)
310{
311 struct net_device *dev = s->dev;
312 struct sk_buff *nskb;
313 u8 type;
314
315 dev->last_rx = jiffies;
316 s->stats.rx_bytes += skb->len;
317
318 type = *(u8 *) skb->data; skb_pull(skb, 1);
319
320 if ((type & BNEP_TYPE_MASK) > BNEP_RX_TYPES)
321 goto badframe;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if ((type & BNEP_TYPE_MASK) == BNEP_CONTROL) {
324 bnep_rx_control(s, skb->data, skb->len);
325 kfree_skb(skb);
326 return 0;
327 }
328
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700329 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* Verify and pull out header */
332 if (!skb_pull(skb, __bnep_rx_hlen[type & BNEP_TYPE_MASK]))
333 goto badframe;
334
Al Viro1bc5d442006-11-08 00:27:57 -0800335 s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (type & BNEP_EXT_HEADER) {
338 if (bnep_rx_extension(s, skb) < 0)
339 goto badframe;
340 }
341
342 /* Strip 802.1p header */
343 if (ntohs(s->eh.h_proto) == 0x8100) {
344 if (!skb_pull(skb, 4))
345 goto badframe;
Al Viro1bc5d442006-11-08 00:27:57 -0800346 s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* We have to alloc new skb and copy data here :(. Because original skb
350 * may not be modified and because of the alignment requirements. */
351 nskb = alloc_skb(2 + ETH_HLEN + skb->len, GFP_KERNEL);
352 if (!nskb) {
353 s->stats.rx_dropped++;
354 kfree_skb(skb);
355 return -ENOMEM;
356 }
357 skb_reserve(nskb, 2);
358
359 /* Decompress header and construct ether frame */
360 switch (type & BNEP_TYPE_MASK) {
361 case BNEP_COMPRESSED:
362 memcpy(__skb_put(nskb, ETH_HLEN), &s->eh, ETH_HLEN);
363 break;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 case BNEP_COMPRESSED_SRC_ONLY:
366 memcpy(__skb_put(nskb, ETH_ALEN), s->eh.h_dest, ETH_ALEN);
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700367 memcpy(__skb_put(nskb, ETH_ALEN), skb_mac_header(skb), ETH_ALEN);
Al Viro1bc5d442006-11-08 00:27:57 -0800368 put_unaligned(s->eh.h_proto, (__be16 *) __skb_put(nskb, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 break;
370
371 case BNEP_COMPRESSED_DST_ONLY:
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700372 memcpy(__skb_put(nskb, ETH_ALEN), skb_mac_header(skb),
373 ETH_ALEN);
374 memcpy(__skb_put(nskb, ETH_ALEN + 2), s->eh.h_source,
375 ETH_ALEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
377
378 case BNEP_GENERAL:
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700379 memcpy(__skb_put(nskb, ETH_ALEN * 2), skb_mac_header(skb),
380 ETH_ALEN * 2);
Al Viro1bc5d442006-11-08 00:27:57 -0800381 put_unaligned(s->eh.h_proto, (__be16 *) __skb_put(nskb, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 break;
383 }
384
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300385 skb_copy_from_linear_data(skb, __skb_put(nskb, skb->len), skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 kfree_skb(skb);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 s->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 nskb->ip_summed = CHECKSUM_NONE;
390 nskb->protocol = eth_type_trans(nskb, dev);
391 netif_rx_ni(nskb);
392 return 0;
393
394badframe:
395 s->stats.rx_errors++;
396 kfree_skb(skb);
397 return 0;
398}
399
400static u8 __bnep_tx_types[] = {
401 BNEP_GENERAL,
402 BNEP_COMPRESSED_SRC_ONLY,
403 BNEP_COMPRESSED_DST_ONLY,
404 BNEP_COMPRESSED
405};
406
407static inline int bnep_tx_frame(struct bnep_session *s, struct sk_buff *skb)
408{
409 struct ethhdr *eh = (void *) skb->data;
410 struct socket *sock = s->sock;
411 struct kvec iv[3];
412 int len = 0, il = 0;
413 u8 type = 0;
414
415 BT_DBG("skb %p dev %p type %d", skb, skb->dev, skb->pkt_type);
416
417 if (!skb->dev) {
418 /* Control frame sent by us */
419 goto send;
420 }
421
422 iv[il++] = (struct kvec) { &type, 1 };
423 len++;
424
Kris Katterjohnd3f4a682006-01-09 16:01:43 -0800425 if (!compare_ether_addr(eh->h_dest, s->eh.h_source))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 type |= 0x01;
427
Kris Katterjohnd3f4a682006-01-09 16:01:43 -0800428 if (!compare_ether_addr(eh->h_source, s->eh.h_dest))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 type |= 0x02;
430
431 if (type)
432 skb_pull(skb, ETH_ALEN * 2);
433
434 type = __bnep_tx_types[type];
435 switch (type) {
436 case BNEP_COMPRESSED_SRC_ONLY:
437 iv[il++] = (struct kvec) { eh->h_source, ETH_ALEN };
438 len += ETH_ALEN;
439 break;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 case BNEP_COMPRESSED_DST_ONLY:
442 iv[il++] = (struct kvec) { eh->h_dest, ETH_ALEN };
443 len += ETH_ALEN;
444 break;
445 }
446
447send:
448 iv[il++] = (struct kvec) { skb->data, skb->len };
449 len += skb->len;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* FIXME: linearize skb */
452 {
453 len = kernel_sendmsg(sock, &s->msg, iv, il, len);
454 }
455 kfree_skb(skb);
456
457 if (len > 0) {
458 s->stats.tx_bytes += len;
459 s->stats.tx_packets++;
460 return 0;
461 }
462
463 return len;
464}
465
466static int bnep_session(void *arg)
467{
468 struct bnep_session *s = arg;
469 struct net_device *dev = s->dev;
470 struct sock *sk = s->sock->sk;
471 struct sk_buff *skb;
472 wait_queue_t wait;
473
474 BT_DBG("");
475
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900476 daemonize("kbnepd %s", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 set_user_nice(current, -15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 init_waitqueue_entry(&wait, current);
480 add_wait_queue(sk->sk_sleep, &wait);
481 while (!atomic_read(&s->killed)) {
482 set_current_state(TASK_INTERRUPTIBLE);
483
484 // RX
485 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
486 skb_orphan(skb);
487 bnep_rx_frame(s, skb);
488 }
489
490 if (sk->sk_state != BT_CONNECTED)
491 break;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 // TX
494 while ((skb = skb_dequeue(&sk->sk_write_queue)))
495 if (bnep_tx_frame(s, skb))
496 break;
497 netif_wake_queue(dev);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 schedule();
500 }
501 set_current_state(TASK_RUNNING);
502 remove_wait_queue(sk->sk_sleep, &wait);
503
504 /* Cleanup session */
505 down_write(&bnep_session_sem);
506
507 /* Delete network device */
508 unregister_netdev(dev);
509
Marcel Holtmannec8dab32008-07-14 20:13:53 +0200510 /* Wakeup user-space polling for socket errors */
511 s->sock->sk->sk_err = EUNATCH;
512
513 wake_up_interruptible(s->sock->sk->sk_sleep);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* Release the socket */
516 fput(s->sock->file);
517
518 __bnep_unlink_session(s);
519
520 up_write(&bnep_session_sem);
521 free_netdev(dev);
522 return 0;
523}
524
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200525static struct device *bnep_get_device(struct bnep_session *session)
526{
527 bdaddr_t *src = &bt_sk(session->sock->sk)->src;
528 bdaddr_t *dst = &bt_sk(session->sock->sk)->dst;
529 struct hci_dev *hdev;
530 struct hci_conn *conn;
531
532 hdev = hci_get_route(dst, src);
533 if (!hdev)
534 return NULL;
535
536 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200537
538 hci_dev_put(hdev);
539
Marcel Holtmannb2cfcd72006-10-15 17:31:05 +0200540 return conn ? &conn->dev : NULL;
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock)
544{
545 struct net_device *dev;
546 struct bnep_session *s, *ss;
547 u8 dst[ETH_ALEN], src[ETH_ALEN];
548 int err;
549
550 BT_DBG("");
551
552 baswap((void *) dst, &bt_sk(sock->sk)->dst);
553 baswap((void *) src, &bt_sk(sock->sk)->src);
554
555 /* session struct allocated as private part of net_device */
556 dev = alloc_netdev(sizeof(struct bnep_session),
557 (*req->device) ? req->device : "bnep%d",
558 bnep_net_setup);
Tobias Klauser67b52e52006-03-21 23:53:16 -0800559 if (!dev)
560 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 down_write(&bnep_session_sem);
563
564 ss = __bnep_get_session(dst);
565 if (ss && ss->state == BT_CONNECTED) {
566 err = -EEXIST;
567 goto failed;
568 }
569
570 s = dev->priv;
571
572 /* This is rx header therefore addresses are swapped.
573 * ie eh.h_dest is our local address. */
574 memcpy(s->eh.h_dest, &src, ETH_ALEN);
575 memcpy(s->eh.h_source, &dst, ETH_ALEN);
576 memcpy(dev->dev_addr, s->eh.h_dest, ETH_ALEN);
577
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200578 s->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 s->sock = sock;
580 s->role = req->role;
581 s->state = BT_CONNECTED;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 s->msg.msg_flags = MSG_NOSIGNAL;
584
585#ifdef CONFIG_BT_BNEP_MC_FILTER
586 /* Set default mc filter */
587 set_bit(bnep_mc_hash(dev->broadcast), (ulong *) &s->mc_filter);
588#endif
589
590#ifdef CONFIG_BT_BNEP_PROTO_FILTER
591 /* Set default protocol filter */
592 bnep_set_default_proto_filter(s);
593#endif
594
Marcel Holtmann0a85b962006-07-06 13:09:02 +0200595 SET_NETDEV_DEV(dev, bnep_get_device(s));
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 err = register_netdev(dev);
598 if (err) {
599 goto failed;
600 }
601
602 __bnep_link_session(s);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 err = kernel_thread(bnep_session, s, CLONE_KERNEL);
605 if (err < 0) {
606 /* Session thread start failed, gotta cleanup. */
607 unregister_netdev(dev);
608 __bnep_unlink_session(s);
609 goto failed;
610 }
611
612 up_write(&bnep_session_sem);
613 strcpy(req->device, dev->name);
614 return 0;
615
616failed:
617 up_write(&bnep_session_sem);
618 free_netdev(dev);
619 return err;
620}
621
622int bnep_del_connection(struct bnep_conndel_req *req)
623{
624 struct bnep_session *s;
625 int err = 0;
626
627 BT_DBG("");
628
629 down_read(&bnep_session_sem);
630
631 s = __bnep_get_session(req->dst);
632 if (s) {
633 /* Wakeup user-space which is polling for socket errors.
634 * This is temporary hack untill we have shutdown in L2CAP */
635 s->sock->sk->sk_err = EUNATCH;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* Kill session thread */
638 atomic_inc(&s->killed);
639 wake_up_interruptible(s->sock->sk->sk_sleep);
640 } else
641 err = -ENOENT;
642
643 up_read(&bnep_session_sem);
644 return err;
645}
646
647static void __bnep_copy_ci(struct bnep_conninfo *ci, struct bnep_session *s)
648{
649 memcpy(ci->dst, s->eh.h_source, ETH_ALEN);
650 strcpy(ci->device, s->dev->name);
651 ci->flags = s->flags;
652 ci->state = s->state;
653 ci->role = s->role;
654}
655
656int bnep_get_connlist(struct bnep_connlist_req *req)
657{
658 struct list_head *p;
659 int err = 0, n = 0;
660
661 down_read(&bnep_session_sem);
662
663 list_for_each(p, &bnep_session_list) {
664 struct bnep_session *s;
665 struct bnep_conninfo ci;
666
667 s = list_entry(p, struct bnep_session, list);
668
669 __bnep_copy_ci(&ci, s);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
672 err = -EFAULT;
673 break;
674 }
675
676 if (++n >= req->cnum)
677 break;
678
679 req->ci++;
680 }
681 req->cnum = n;
682
683 up_read(&bnep_session_sem);
684 return err;
685}
686
687int bnep_get_conninfo(struct bnep_conninfo *ci)
688{
689 struct bnep_session *s;
690 int err = 0;
691
692 down_read(&bnep_session_sem);
693
694 s = __bnep_get_session(ci->dst);
695 if (s)
696 __bnep_copy_ci(ci, s);
697 else
698 err = -ENOENT;
699
700 up_read(&bnep_session_sem);
701 return err;
702}
703
704static int __init bnep_init(void)
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900705{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 char flt[50] = "";
707
708 l2cap_load();
709
710#ifdef CONFIG_BT_BNEP_PROTO_FILTER
711 strcat(flt, "protocol ");
712#endif
713
714#ifdef CONFIG_BT_BNEP_MC_FILTER
715 strcat(flt, "multicast");
716#endif
717
718 BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION);
719 if (flt[0])
720 BT_INFO("BNEP filters: %s", flt);
721
722 bnep_sock_init();
723 return 0;
724}
725
726static void __exit bnep_exit(void)
727{
728 bnep_sock_cleanup();
729}
730
731module_init(bnep_init);
732module_exit(bnep_exit);
733
734MODULE_AUTHOR("David Libault <david.libault@inventel.fr>, Maxim Krasnyansky <maxk@qualcomm.com>");
735MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION);
736MODULE_VERSION(VERSION);
737MODULE_LICENSE("GPL");
738MODULE_ALIAS("bt-proto-4");