2 BNEP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2001-2002 Inventel Systemes
5 Clément Moreau <clement.moreau@inventel.fr>
6 David Libault <david.libault@inventel.fr>
8 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
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;
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
18 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
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
24 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
25 SOFTWARE IS DISCLAIMED.
28 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/signal.h>
33 #include <linux/init.h>
34 #include <linux/wait.h>
35 #include <linux/freezer.h>
36 #include <linux/errno.h>
37 #include <linux/net.h>
38 #include <linux/slab.h>
41 #include <linux/socket.h>
42 #include <linux/file.h>
44 #include <linux/netdevice.h>
45 #include <linux/etherdevice.h>
46 #include <linux/skbuff.h>
48 #include <asm/unaligned.h>
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
52 #include <net/bluetooth/l2cap.h>
58 static int compress_src
= 1;
59 static int compress_dst
= 1;
61 static LIST_HEAD(bnep_session_list
);
62 static DECLARE_RWSEM(bnep_session_sem
);
64 static struct bnep_session
*__bnep_get_session(u8
*dst
)
66 struct bnep_session
*s
;
71 list_for_each(p
, &bnep_session_list
) {
72 s
= list_entry(p
, struct bnep_session
, list
);
73 if (!compare_ether_addr(dst
, s
->eh
.h_source
))
79 static void __bnep_link_session(struct bnep_session
*s
)
81 /* It's safe to call __module_get() here because sessions are added
82 by the socket layer which has to hold the reference to this module.
84 __module_get(THIS_MODULE
);
85 list_add(&s
->list
, &bnep_session_list
);
88 static void __bnep_unlink_session(struct bnep_session
*s
)
91 module_put(THIS_MODULE
);
94 static int bnep_send(struct bnep_session
*s
, void *data
, size_t len
)
96 struct socket
*sock
= s
->sock
;
97 struct kvec iv
= { data
, len
};
99 return kernel_sendmsg(sock
, &s
->msg
, &iv
, 1, len
);
102 static int bnep_send_rsp(struct bnep_session
*s
, u8 ctrl
, u16 resp
)
104 struct bnep_control_rsp rsp
;
105 rsp
.type
= BNEP_CONTROL
;
107 rsp
.resp
= htons(resp
);
108 return bnep_send(s
, &rsp
, sizeof(rsp
));
111 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
112 static inline void bnep_set_default_proto_filter(struct bnep_session
*s
)
115 s
->proto_filter
[0].start
= ETH_P_IP
;
116 s
->proto_filter
[0].end
= ETH_P_ARP
;
117 /* (RARP, AppleTalk) */
118 s
->proto_filter
[1].start
= ETH_P_RARP
;
119 s
->proto_filter
[1].end
= ETH_P_AARP
;
121 s
->proto_filter
[2].start
= ETH_P_IPX
;
122 s
->proto_filter
[2].end
= ETH_P_IPV6
;
126 static int bnep_ctrl_set_netfilter(struct bnep_session
*s
, __be16
*data
, int len
)
133 n
= get_unaligned_be16(data
);
139 BT_DBG("filter len %d", n
);
141 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
143 if (n
<= BNEP_MAX_PROTO_FILTERS
) {
144 struct bnep_proto_filter
*f
= s
->proto_filter
;
147 for (i
= 0; i
< n
; i
++) {
148 f
[i
].start
= get_unaligned_be16(data
++);
149 f
[i
].end
= get_unaligned_be16(data
++);
151 BT_DBG("proto filter start %d end %d",
152 f
[i
].start
, f
[i
].end
);
155 if (i
< BNEP_MAX_PROTO_FILTERS
)
156 memset(f
+ i
, 0, sizeof(*f
));
159 bnep_set_default_proto_filter(s
);
161 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_SUCCESS
);
163 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_FILTER_LIMIT_REACHED
);
166 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_FILTER_UNSUPPORTED_REQ
);
171 static int bnep_ctrl_set_mcfilter(struct bnep_session
*s
, u8
*data
, int len
)
178 n
= get_unaligned_be16(data
);
184 BT_DBG("filter len %d", n
);
186 #ifdef CONFIG_BT_BNEP_MC_FILTER
192 /* Always send broadcast */
193 set_bit(bnep_mc_hash(s
->dev
->broadcast
), (ulong
*) &s
->mc_filter
);
195 /* Add address ranges to the multicast hash */
199 memcpy(a1
, data
, ETH_ALEN
); data
+= ETH_ALEN
;
200 a2
= data
; data
+= ETH_ALEN
;
202 BT_DBG("mc filter %s -> %s",
203 batostr((void *) a1
), batostr((void *) a2
));
205 #define INCA(a) { int i = 5; while (i >=0 && ++a[i--] == 0); }
207 /* Iterate from a1 to a2 */
208 set_bit(bnep_mc_hash(a1
), (ulong
*) &s
->mc_filter
);
209 while (memcmp(a1
, a2
, 6) < 0 && s
->mc_filter
!= ~0LL) {
211 set_bit(bnep_mc_hash(a1
), (ulong
*) &s
->mc_filter
);
216 BT_DBG("mc filter hash 0x%llx", s
->mc_filter
);
218 bnep_send_rsp(s
, BNEP_FILTER_MULTI_ADDR_RSP
, BNEP_SUCCESS
);
220 bnep_send_rsp(s
, BNEP_FILTER_MULTI_ADDR_RSP
, BNEP_FILTER_UNSUPPORTED_REQ
);
225 static int bnep_rx_control(struct bnep_session
*s
, void *data
, int len
)
227 u8 cmd
= *(u8
*)data
;
233 case BNEP_CMD_NOT_UNDERSTOOD
:
234 case BNEP_SETUP_CONN_RSP
:
235 case BNEP_FILTER_NET_TYPE_RSP
:
236 case BNEP_FILTER_MULTI_ADDR_RSP
:
237 /* Ignore these for now */
240 case BNEP_FILTER_NET_TYPE_SET
:
241 err
= bnep_ctrl_set_netfilter(s
, data
, len
);
244 case BNEP_FILTER_MULTI_ADDR_SET
:
245 err
= bnep_ctrl_set_mcfilter(s
, data
, len
);
248 case BNEP_SETUP_CONN_REQ
:
249 err
= bnep_send_rsp(s
, BNEP_SETUP_CONN_RSP
, BNEP_CONN_NOT_ALLOWED
);
254 pkt
[0] = BNEP_CONTROL
;
255 pkt
[1] = BNEP_CMD_NOT_UNDERSTOOD
;
257 bnep_send(s
, pkt
, sizeof(pkt
));
265 static int bnep_rx_extension(struct bnep_session
*s
, struct sk_buff
*skb
)
267 struct bnep_ext_hdr
*h
;
271 h
= (void *) skb
->data
;
272 if (!skb_pull(skb
, sizeof(*h
))) {
277 BT_DBG("type 0x%x len %d", h
->type
, h
->len
);
279 switch (h
->type
& BNEP_TYPE_MASK
) {
280 case BNEP_EXT_CONTROL
:
281 bnep_rx_control(s
, skb
->data
, skb
->len
);
285 /* Unknown extension, skip it. */
289 if (!skb_pull(skb
, h
->len
)) {
293 } while (!err
&& (h
->type
& BNEP_EXT_HEADER
));
298 static u8 __bnep_rx_hlen
[] = {
299 ETH_HLEN
, /* BNEP_GENERAL */
300 0, /* BNEP_CONTROL */
301 2, /* BNEP_COMPRESSED */
302 ETH_ALEN
+ 2, /* BNEP_COMPRESSED_SRC_ONLY */
303 ETH_ALEN
+ 2 /* BNEP_COMPRESSED_DST_ONLY */
305 #define BNEP_RX_TYPES (sizeof(__bnep_rx_hlen) - 1)
307 static inline int bnep_rx_frame(struct bnep_session
*s
, struct sk_buff
*skb
)
309 struct net_device
*dev
= s
->dev
;
310 struct sk_buff
*nskb
;
313 dev
->stats
.rx_bytes
+= skb
->len
;
315 type
= *(u8
*) skb
->data
; skb_pull(skb
, 1);
317 if ((type
& BNEP_TYPE_MASK
) > BNEP_RX_TYPES
)
320 if ((type
& BNEP_TYPE_MASK
) == BNEP_CONTROL
) {
321 bnep_rx_control(s
, skb
->data
, skb
->len
);
326 skb_reset_mac_header(skb
);
328 /* Verify and pull out header */
329 if (!skb_pull(skb
, __bnep_rx_hlen
[type
& BNEP_TYPE_MASK
]))
332 s
->eh
.h_proto
= get_unaligned((__be16
*) (skb
->data
- 2));
334 if (type
& BNEP_EXT_HEADER
) {
335 if (bnep_rx_extension(s
, skb
) < 0)
339 /* Strip 802.1p header */
340 if (ntohs(s
->eh
.h_proto
) == 0x8100) {
341 if (!skb_pull(skb
, 4))
343 s
->eh
.h_proto
= get_unaligned((__be16
*) (skb
->data
- 2));
346 /* We have to alloc new skb and copy data here :(. Because original skb
347 * may not be modified and because of the alignment requirements. */
348 nskb
= alloc_skb(2 + ETH_HLEN
+ skb
->len
, GFP_KERNEL
);
350 dev
->stats
.rx_dropped
++;
354 skb_reserve(nskb
, 2);
356 /* Decompress header and construct ether frame */
357 switch (type
& BNEP_TYPE_MASK
) {
358 case BNEP_COMPRESSED
:
359 memcpy(__skb_put(nskb
, ETH_HLEN
), &s
->eh
, ETH_HLEN
);
362 case BNEP_COMPRESSED_SRC_ONLY
:
363 memcpy(__skb_put(nskb
, ETH_ALEN
), s
->eh
.h_dest
, ETH_ALEN
);
364 memcpy(__skb_put(nskb
, ETH_ALEN
), skb_mac_header(skb
), ETH_ALEN
);
365 put_unaligned(s
->eh
.h_proto
, (__be16
*) __skb_put(nskb
, 2));
368 case BNEP_COMPRESSED_DST_ONLY
:
369 memcpy(__skb_put(nskb
, ETH_ALEN
), skb_mac_header(skb
),
371 memcpy(__skb_put(nskb
, ETH_ALEN
+ 2), s
->eh
.h_source
,
376 memcpy(__skb_put(nskb
, ETH_ALEN
* 2), skb_mac_header(skb
),
378 put_unaligned(s
->eh
.h_proto
, (__be16
*) __skb_put(nskb
, 2));
382 skb_copy_from_linear_data(skb
, __skb_put(nskb
, skb
->len
), skb
->len
);
385 dev
->stats
.rx_packets
++;
386 nskb
->ip_summed
= CHECKSUM_NONE
;
387 nskb
->protocol
= eth_type_trans(nskb
, dev
);
392 dev
->stats
.rx_errors
++;
397 static u8 __bnep_tx_types
[] = {
399 BNEP_COMPRESSED_SRC_ONLY
,
400 BNEP_COMPRESSED_DST_ONLY
,
404 static inline int bnep_tx_frame(struct bnep_session
*s
, struct sk_buff
*skb
)
406 struct ethhdr
*eh
= (void *) skb
->data
;
407 struct socket
*sock
= s
->sock
;
412 BT_DBG("skb %p dev %p type %d", skb
, skb
->dev
, skb
->pkt_type
);
415 /* Control frame sent by us */
419 iv
[il
++] = (struct kvec
) { &type
, 1 };
422 if (compress_src
&& !compare_ether_addr(eh
->h_dest
, s
->eh
.h_source
))
425 if (compress_dst
&& !compare_ether_addr(eh
->h_source
, s
->eh
.h_dest
))
429 skb_pull(skb
, ETH_ALEN
* 2);
431 type
= __bnep_tx_types
[type
];
433 case BNEP_COMPRESSED_SRC_ONLY
:
434 iv
[il
++] = (struct kvec
) { eh
->h_source
, ETH_ALEN
};
438 case BNEP_COMPRESSED_DST_ONLY
:
439 iv
[il
++] = (struct kvec
) { eh
->h_dest
, ETH_ALEN
};
445 iv
[il
++] = (struct kvec
) { skb
->data
, skb
->len
};
448 /* FIXME: linearize skb */
450 len
= kernel_sendmsg(sock
, &s
->msg
, iv
, il
, len
);
455 s
->dev
->stats
.tx_bytes
+= len
;
456 s
->dev
->stats
.tx_packets
++;
463 static int bnep_session(void *arg
)
465 struct bnep_session
*s
= arg
;
466 struct net_device
*dev
= s
->dev
;
467 struct sock
*sk
= s
->sock
->sk
;
473 daemonize("kbnepd %s", dev
->name
);
474 set_user_nice(current
, -15);
476 init_waitqueue_entry(&wait
, current
);
477 add_wait_queue(sk_sleep(sk
), &wait
);
478 while (!atomic_read(&s
->killed
)) {
479 set_current_state(TASK_INTERRUPTIBLE
);
482 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
))) {
484 bnep_rx_frame(s
, skb
);
487 if (sk
->sk_state
!= BT_CONNECTED
)
491 while ((skb
= skb_dequeue(&sk
->sk_write_queue
)))
492 if (bnep_tx_frame(s
, skb
))
494 netif_wake_queue(dev
);
498 set_current_state(TASK_RUNNING
);
499 remove_wait_queue(sk_sleep(sk
), &wait
);
501 /* Cleanup session */
502 down_write(&bnep_session_sem
);
504 /* Delete network device */
505 unregister_netdev(dev
);
507 /* Wakeup user-space polling for socket errors */
508 s
->sock
->sk
->sk_err
= EUNATCH
;
510 wake_up_interruptible(sk_sleep(s
->sock
->sk
));
512 /* Release the socket */
515 __bnep_unlink_session(s
);
517 up_write(&bnep_session_sem
);
522 static struct device
*bnep_get_device(struct bnep_session
*session
)
524 bdaddr_t
*src
= &bt_sk(session
->sock
->sk
)->src
;
525 bdaddr_t
*dst
= &bt_sk(session
->sock
->sk
)->dst
;
526 struct hci_dev
*hdev
;
527 struct hci_conn
*conn
;
529 hdev
= hci_get_route(dst
, src
);
533 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, dst
);
537 return conn
? &conn
->dev
: NULL
;
540 static struct device_type bnep_type
= {
544 int bnep_add_connection(struct bnep_connadd_req
*req
, struct socket
*sock
)
546 struct net_device
*dev
;
547 struct bnep_session
*s
, *ss
;
548 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
553 baswap((void *) dst
, &bt_sk(sock
->sk
)->dst
);
554 baswap((void *) src
, &bt_sk(sock
->sk
)->src
);
556 /* session struct allocated as private part of net_device */
557 dev
= alloc_netdev(sizeof(struct bnep_session
),
558 (*req
->device
) ? req
->device
: "bnep%d",
563 down_write(&bnep_session_sem
);
565 ss
= __bnep_get_session(dst
);
566 if (ss
&& ss
->state
== BT_CONNECTED
) {
571 s
= netdev_priv(dev
);
573 /* This is rx header therefore addresses are swapped.
574 * ie eh.h_dest is our local address. */
575 memcpy(s
->eh
.h_dest
, &src
, ETH_ALEN
);
576 memcpy(s
->eh
.h_source
, &dst
, ETH_ALEN
);
577 memcpy(dev
->dev_addr
, s
->eh
.h_dest
, ETH_ALEN
);
582 s
->state
= BT_CONNECTED
;
584 s
->msg
.msg_flags
= MSG_NOSIGNAL
;
586 #ifdef CONFIG_BT_BNEP_MC_FILTER
587 /* Set default mc filter */
588 set_bit(bnep_mc_hash(dev
->broadcast
), (ulong
*) &s
->mc_filter
);
591 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
592 /* Set default protocol filter */
593 bnep_set_default_proto_filter(s
);
596 SET_NETDEV_DEV(dev
, bnep_get_device(s
));
597 SET_NETDEV_DEVTYPE(dev
, &bnep_type
);
599 err
= register_netdev(dev
);
604 __bnep_link_session(s
);
606 err
= kernel_thread(bnep_session
, s
, CLONE_KERNEL
);
608 /* Session thread start failed, gotta cleanup. */
609 unregister_netdev(dev
);
610 __bnep_unlink_session(s
);
614 up_write(&bnep_session_sem
);
615 strcpy(req
->device
, dev
->name
);
619 up_write(&bnep_session_sem
);
624 int bnep_del_connection(struct bnep_conndel_req
*req
)
626 struct bnep_session
*s
;
631 down_read(&bnep_session_sem
);
633 s
= __bnep_get_session(req
->dst
);
635 /* Wakeup user-space which is polling for socket errors.
636 * This is temporary hack until we have shutdown in L2CAP */
637 s
->sock
->sk
->sk_err
= EUNATCH
;
639 /* Kill session thread */
640 atomic_inc(&s
->killed
);
641 wake_up_interruptible(sk_sleep(s
->sock
->sk
));
645 up_read(&bnep_session_sem
);
649 static void __bnep_copy_ci(struct bnep_conninfo
*ci
, struct bnep_session
*s
)
651 memset(ci
, 0, sizeof(*ci
));
652 memcpy(ci
->dst
, s
->eh
.h_source
, ETH_ALEN
);
653 strcpy(ci
->device
, s
->dev
->name
);
654 ci
->flags
= s
->flags
;
655 ci
->state
= s
->state
;
659 int bnep_get_connlist(struct bnep_connlist_req
*req
)
664 down_read(&bnep_session_sem
);
666 list_for_each(p
, &bnep_session_list
) {
667 struct bnep_session
*s
;
668 struct bnep_conninfo ci
;
670 s
= list_entry(p
, struct bnep_session
, list
);
672 __bnep_copy_ci(&ci
, s
);
674 if (copy_to_user(req
->ci
, &ci
, sizeof(ci
))) {
679 if (++n
>= req
->cnum
)
686 up_read(&bnep_session_sem
);
690 int bnep_get_conninfo(struct bnep_conninfo
*ci
)
692 struct bnep_session
*s
;
695 down_read(&bnep_session_sem
);
697 s
= __bnep_get_session(ci
->dst
);
699 __bnep_copy_ci(ci
, s
);
703 up_read(&bnep_session_sem
);
707 static int __init
bnep_init(void)
711 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
712 strcat(flt
, "protocol ");
715 #ifdef CONFIG_BT_BNEP_MC_FILTER
716 strcat(flt
, "multicast");
719 BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION
);
721 BT_INFO("BNEP filters: %s", flt
);
727 static void __exit
bnep_exit(void)
732 module_init(bnep_init
);
733 module_exit(bnep_exit
);
735 module_param(compress_src
, bool, 0644);
736 MODULE_PARM_DESC(compress_src
, "Compress sources headers");
738 module_param(compress_dst
, bool, 0644);
739 MODULE_PARM_DESC(compress_dst
, "Compress destination headers");
741 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
742 MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION
);
743 MODULE_VERSION(VERSION
);
744 MODULE_LICENSE("GPL");
745 MODULE_ALIAS("bt-proto-4");