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
);
140 BT_DBG("filter len %d", n
);
142 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
144 if (n
<= BNEP_MAX_PROTO_FILTERS
) {
145 struct bnep_proto_filter
*f
= s
->proto_filter
;
148 for (i
= 0; i
< n
; i
++) {
149 f
[i
].start
= get_unaligned_be16(data
++);
150 f
[i
].end
= get_unaligned_be16(data
++);
152 BT_DBG("proto filter start %d end %d",
153 f
[i
].start
, f
[i
].end
);
156 if (i
< BNEP_MAX_PROTO_FILTERS
)
157 memset(f
+ i
, 0, sizeof(*f
));
160 bnep_set_default_proto_filter(s
);
162 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_SUCCESS
);
164 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_FILTER_LIMIT_REACHED
);
167 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_FILTER_UNSUPPORTED_REQ
);
172 static int bnep_ctrl_set_mcfilter(struct bnep_session
*s
, u8
*data
, int len
)
179 n
= get_unaligned_be16(data
);
186 BT_DBG("filter len %d", n
);
188 #ifdef CONFIG_BT_BNEP_MC_FILTER
196 /* Always send broadcast */
197 set_bit(bnep_mc_hash(s
->dev
->broadcast
), (ulong
*) &s
->mc_filter
);
199 /* Add address ranges to the multicast hash */
203 memcpy(a1
, data
, ETH_ALEN
);
208 BT_DBG("mc filter %s -> %s",
209 batostr((void *) a1
), batostr((void *) a2
));
211 /* Iterate from a1 to a2 */
212 set_bit(bnep_mc_hash(a1
), (ulong
*) &s
->mc_filter
);
213 while (memcmp(a1
, a2
, 6) < 0 && s
->mc_filter
!= ~0LL) {
216 while (i
>= 0 && ++a1
[i
--] == 0)
219 set_bit(bnep_mc_hash(a1
), (ulong
*) &s
->mc_filter
);
224 BT_DBG("mc filter hash 0x%llx", s
->mc_filter
);
226 bnep_send_rsp(s
, BNEP_FILTER_MULTI_ADDR_RSP
, BNEP_SUCCESS
);
228 bnep_send_rsp(s
, BNEP_FILTER_MULTI_ADDR_RSP
, BNEP_FILTER_UNSUPPORTED_REQ
);
233 static int bnep_rx_control(struct bnep_session
*s
, void *data
, int len
)
235 u8 cmd
= *(u8
*)data
;
242 case BNEP_CMD_NOT_UNDERSTOOD
:
243 case BNEP_SETUP_CONN_RSP
:
244 case BNEP_FILTER_NET_TYPE_RSP
:
245 case BNEP_FILTER_MULTI_ADDR_RSP
:
246 /* Ignore these for now */
249 case BNEP_FILTER_NET_TYPE_SET
:
250 err
= bnep_ctrl_set_netfilter(s
, data
, len
);
253 case BNEP_FILTER_MULTI_ADDR_SET
:
254 err
= bnep_ctrl_set_mcfilter(s
, data
, len
);
257 case BNEP_SETUP_CONN_REQ
:
258 err
= bnep_send_rsp(s
, BNEP_SETUP_CONN_RSP
, BNEP_CONN_NOT_ALLOWED
);
263 pkt
[0] = BNEP_CONTROL
;
264 pkt
[1] = BNEP_CMD_NOT_UNDERSTOOD
;
266 bnep_send(s
, pkt
, sizeof(pkt
));
274 static int bnep_rx_extension(struct bnep_session
*s
, struct sk_buff
*skb
)
276 struct bnep_ext_hdr
*h
;
280 h
= (void *) skb
->data
;
281 if (!skb_pull(skb
, sizeof(*h
))) {
286 BT_DBG("type 0x%x len %d", h
->type
, h
->len
);
288 switch (h
->type
& BNEP_TYPE_MASK
) {
289 case BNEP_EXT_CONTROL
:
290 bnep_rx_control(s
, skb
->data
, skb
->len
);
294 /* Unknown extension, skip it. */
298 if (!skb_pull(skb
, h
->len
)) {
302 } while (!err
&& (h
->type
& BNEP_EXT_HEADER
));
307 static u8 __bnep_rx_hlen
[] = {
308 ETH_HLEN
, /* BNEP_GENERAL */
309 0, /* BNEP_CONTROL */
310 2, /* BNEP_COMPRESSED */
311 ETH_ALEN
+ 2, /* BNEP_COMPRESSED_SRC_ONLY */
312 ETH_ALEN
+ 2 /* BNEP_COMPRESSED_DST_ONLY */
315 static inline int bnep_rx_frame(struct bnep_session
*s
, struct sk_buff
*skb
)
317 struct net_device
*dev
= s
->dev
;
318 struct sk_buff
*nskb
;
321 dev
->stats
.rx_bytes
+= skb
->len
;
323 type
= *(u8
*) skb
->data
;
326 if ((type
& BNEP_TYPE_MASK
) >= sizeof(__bnep_rx_hlen
))
329 if ((type
& BNEP_TYPE_MASK
) == BNEP_CONTROL
) {
330 bnep_rx_control(s
, skb
->data
, skb
->len
);
335 skb_reset_mac_header(skb
);
337 /* Verify and pull out header */
338 if (!skb_pull(skb
, __bnep_rx_hlen
[type
& BNEP_TYPE_MASK
]))
341 s
->eh
.h_proto
= get_unaligned((__be16
*) (skb
->data
- 2));
343 if (type
& BNEP_EXT_HEADER
) {
344 if (bnep_rx_extension(s
, skb
) < 0)
348 /* Strip 802.1p header */
349 if (ntohs(s
->eh
.h_proto
) == 0x8100) {
350 if (!skb_pull(skb
, 4))
352 s
->eh
.h_proto
= get_unaligned((__be16
*) (skb
->data
- 2));
355 /* We have to alloc new skb and copy data here :(. Because original skb
356 * may not be modified and because of the alignment requirements. */
357 nskb
= alloc_skb(2 + ETH_HLEN
+ skb
->len
, GFP_KERNEL
);
359 dev
->stats
.rx_dropped
++;
363 skb_reserve(nskb
, 2);
365 /* Decompress header and construct ether frame */
366 switch (type
& BNEP_TYPE_MASK
) {
367 case BNEP_COMPRESSED
:
368 memcpy(__skb_put(nskb
, ETH_HLEN
), &s
->eh
, ETH_HLEN
);
371 case BNEP_COMPRESSED_SRC_ONLY
:
372 memcpy(__skb_put(nskb
, ETH_ALEN
), s
->eh
.h_dest
, ETH_ALEN
);
373 memcpy(__skb_put(nskb
, ETH_ALEN
), skb_mac_header(skb
), ETH_ALEN
);
374 put_unaligned(s
->eh
.h_proto
, (__be16
*) __skb_put(nskb
, 2));
377 case BNEP_COMPRESSED_DST_ONLY
:
378 memcpy(__skb_put(nskb
, ETH_ALEN
), skb_mac_header(skb
),
380 memcpy(__skb_put(nskb
, ETH_ALEN
+ 2), s
->eh
.h_source
,
385 memcpy(__skb_put(nskb
, ETH_ALEN
* 2), skb_mac_header(skb
),
387 put_unaligned(s
->eh
.h_proto
, (__be16
*) __skb_put(nskb
, 2));
391 skb_copy_from_linear_data(skb
, __skb_put(nskb
, skb
->len
), skb
->len
);
394 dev
->stats
.rx_packets
++;
395 nskb
->ip_summed
= CHECKSUM_NONE
;
396 nskb
->protocol
= eth_type_trans(nskb
, dev
);
401 dev
->stats
.rx_errors
++;
406 static u8 __bnep_tx_types
[] = {
408 BNEP_COMPRESSED_SRC_ONLY
,
409 BNEP_COMPRESSED_DST_ONLY
,
413 static inline int bnep_tx_frame(struct bnep_session
*s
, struct sk_buff
*skb
)
415 struct ethhdr
*eh
= (void *) skb
->data
;
416 struct socket
*sock
= s
->sock
;
421 BT_DBG("skb %p dev %p type %d", skb
, skb
->dev
, skb
->pkt_type
);
424 /* Control frame sent by us */
428 iv
[il
++] = (struct kvec
) { &type
, 1 };
431 if (compress_src
&& !compare_ether_addr(eh
->h_dest
, s
->eh
.h_source
))
434 if (compress_dst
&& !compare_ether_addr(eh
->h_source
, s
->eh
.h_dest
))
438 skb_pull(skb
, ETH_ALEN
* 2);
440 type
= __bnep_tx_types
[type
];
442 case BNEP_COMPRESSED_SRC_ONLY
:
443 iv
[il
++] = (struct kvec
) { eh
->h_source
, ETH_ALEN
};
447 case BNEP_COMPRESSED_DST_ONLY
:
448 iv
[il
++] = (struct kvec
) { eh
->h_dest
, ETH_ALEN
};
454 iv
[il
++] = (struct kvec
) { skb
->data
, skb
->len
};
457 /* FIXME: linearize skb */
459 len
= kernel_sendmsg(sock
, &s
->msg
, iv
, il
, len
);
464 s
->dev
->stats
.tx_bytes
+= len
;
465 s
->dev
->stats
.tx_packets
++;
472 static int bnep_session(void *arg
)
474 struct bnep_session
*s
= arg
;
475 struct net_device
*dev
= s
->dev
;
476 struct sock
*sk
= s
->sock
->sk
;
482 daemonize("kbnepd %s", dev
->name
);
483 set_user_nice(current
, -15);
485 init_waitqueue_entry(&wait
, current
);
486 add_wait_queue(sk_sleep(sk
), &wait
);
487 while (!atomic_read(&s
->killed
)) {
488 set_current_state(TASK_INTERRUPTIBLE
);
491 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
))) {
493 bnep_rx_frame(s
, skb
);
496 if (sk
->sk_state
!= BT_CONNECTED
)
500 while ((skb
= skb_dequeue(&sk
->sk_write_queue
)))
501 if (bnep_tx_frame(s
, skb
))
503 netif_wake_queue(dev
);
507 set_current_state(TASK_RUNNING
);
508 remove_wait_queue(sk_sleep(sk
), &wait
);
510 /* Cleanup session */
511 down_write(&bnep_session_sem
);
513 /* Delete network device */
514 unregister_netdev(dev
);
516 /* Wakeup user-space polling for socket errors */
517 s
->sock
->sk
->sk_err
= EUNATCH
;
519 wake_up_interruptible(sk_sleep(s
->sock
->sk
));
521 /* Release the socket */
524 __bnep_unlink_session(s
);
526 up_write(&bnep_session_sem
);
531 static struct device
*bnep_get_device(struct bnep_session
*session
)
533 bdaddr_t
*src
= &bt_sk(session
->sock
->sk
)->src
;
534 bdaddr_t
*dst
= &bt_sk(session
->sock
->sk
)->dst
;
535 struct hci_dev
*hdev
;
536 struct hci_conn
*conn
;
538 hdev
= hci_get_route(dst
, src
);
542 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, dst
);
546 return conn
? &conn
->dev
: NULL
;
549 static struct device_type bnep_type
= {
553 int bnep_add_connection(struct bnep_connadd_req
*req
, struct socket
*sock
)
555 struct net_device
*dev
;
556 struct bnep_session
*s
, *ss
;
557 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
562 baswap((void *) dst
, &bt_sk(sock
->sk
)->dst
);
563 baswap((void *) src
, &bt_sk(sock
->sk
)->src
);
565 /* session struct allocated as private part of net_device */
566 dev
= alloc_netdev(sizeof(struct bnep_session
),
567 (*req
->device
) ? req
->device
: "bnep%d",
572 down_write(&bnep_session_sem
);
574 ss
= __bnep_get_session(dst
);
575 if (ss
&& ss
->state
== BT_CONNECTED
) {
580 s
= netdev_priv(dev
);
582 /* This is rx header therefore addresses are swapped.
583 * ie. eh.h_dest is our local address. */
584 memcpy(s
->eh
.h_dest
, &src
, ETH_ALEN
);
585 memcpy(s
->eh
.h_source
, &dst
, ETH_ALEN
);
586 memcpy(dev
->dev_addr
, s
->eh
.h_dest
, ETH_ALEN
);
591 s
->state
= BT_CONNECTED
;
593 s
->msg
.msg_flags
= MSG_NOSIGNAL
;
595 #ifdef CONFIG_BT_BNEP_MC_FILTER
596 /* Set default mc filter */
597 set_bit(bnep_mc_hash(dev
->broadcast
), (ulong
*) &s
->mc_filter
);
600 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
601 /* Set default protocol filter */
602 bnep_set_default_proto_filter(s
);
605 SET_NETDEV_DEV(dev
, bnep_get_device(s
));
606 SET_NETDEV_DEVTYPE(dev
, &bnep_type
);
608 err
= register_netdev(dev
);
612 __bnep_link_session(s
);
614 err
= kernel_thread(bnep_session
, s
, CLONE_KERNEL
);
616 /* Session thread start failed, gotta cleanup. */
617 unregister_netdev(dev
);
618 __bnep_unlink_session(s
);
622 up_write(&bnep_session_sem
);
623 strcpy(req
->device
, dev
->name
);
627 up_write(&bnep_session_sem
);
632 int bnep_del_connection(struct bnep_conndel_req
*req
)
634 struct bnep_session
*s
;
639 down_read(&bnep_session_sem
);
641 s
= __bnep_get_session(req
->dst
);
643 /* Wakeup user-space which is polling for socket errors.
644 * This is temporary hack until we have shutdown in L2CAP */
645 s
->sock
->sk
->sk_err
= EUNATCH
;
647 /* Kill session thread */
648 atomic_inc(&s
->killed
);
649 wake_up_interruptible(sk_sleep(s
->sock
->sk
));
653 up_read(&bnep_session_sem
);
657 static void __bnep_copy_ci(struct bnep_conninfo
*ci
, struct bnep_session
*s
)
659 memset(ci
, 0, sizeof(*ci
));
660 memcpy(ci
->dst
, s
->eh
.h_source
, ETH_ALEN
);
661 strcpy(ci
->device
, s
->dev
->name
);
662 ci
->flags
= s
->flags
;
663 ci
->state
= s
->state
;
667 int bnep_get_connlist(struct bnep_connlist_req
*req
)
672 down_read(&bnep_session_sem
);
674 list_for_each(p
, &bnep_session_list
) {
675 struct bnep_session
*s
;
676 struct bnep_conninfo ci
;
678 s
= list_entry(p
, struct bnep_session
, list
);
680 __bnep_copy_ci(&ci
, s
);
682 if (copy_to_user(req
->ci
, &ci
, sizeof(ci
))) {
687 if (++n
>= req
->cnum
)
694 up_read(&bnep_session_sem
);
698 int bnep_get_conninfo(struct bnep_conninfo
*ci
)
700 struct bnep_session
*s
;
703 down_read(&bnep_session_sem
);
705 s
= __bnep_get_session(ci
->dst
);
707 __bnep_copy_ci(ci
, s
);
711 up_read(&bnep_session_sem
);
715 static int __init
bnep_init(void)
719 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
720 strcat(flt
, "protocol ");
723 #ifdef CONFIG_BT_BNEP_MC_FILTER
724 strcat(flt
, "multicast");
727 BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION
);
729 BT_INFO("BNEP filters: %s", flt
);
735 static void __exit
bnep_exit(void)
740 module_init(bnep_init
);
741 module_exit(bnep_exit
);
743 module_param(compress_src
, bool, 0644);
744 MODULE_PARM_DESC(compress_src
, "Compress sources headers");
746 module_param(compress_dst
, bool, 0644);
747 MODULE_PARM_DESC(compress_dst
, "Compress destination headers");
749 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
750 MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION
);
751 MODULE_VERSION(VERSION
);
752 MODULE_LICENSE("GPL");
753 MODULE_ALIAS("bt-proto-4");