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>
40 #include <linux/socket.h>
41 #include <linux/file.h>
43 #include <linux/netdevice.h>
44 #include <linux/etherdevice.h>
45 #include <linux/skbuff.h>
47 #include <asm/unaligned.h>
49 #include <net/bluetooth/bluetooth.h>
50 #include <net/bluetooth/hci_core.h>
51 #include <net/bluetooth/l2cap.h>
57 static int compress_src
= 1;
58 static int compress_dst
= 1;
60 static LIST_HEAD(bnep_session_list
);
61 static DECLARE_RWSEM(bnep_session_sem
);
63 static struct bnep_session
*__bnep_get_session(u8
*dst
)
65 struct bnep_session
*s
;
70 list_for_each(p
, &bnep_session_list
) {
71 s
= list_entry(p
, struct bnep_session
, list
);
72 if (!compare_ether_addr(dst
, s
->eh
.h_source
))
78 static void __bnep_link_session(struct bnep_session
*s
)
80 /* It's safe to call __module_get() here because sessions are added
81 by the socket layer which has to hold the refference to this module.
83 __module_get(THIS_MODULE
);
84 list_add(&s
->list
, &bnep_session_list
);
87 static void __bnep_unlink_session(struct bnep_session
*s
)
90 module_put(THIS_MODULE
);
93 static int bnep_send(struct bnep_session
*s
, void *data
, size_t len
)
95 struct socket
*sock
= s
->sock
;
96 struct kvec iv
= { data
, len
};
98 return kernel_sendmsg(sock
, &s
->msg
, &iv
, 1, len
);
101 static int bnep_send_rsp(struct bnep_session
*s
, u8 ctrl
, u16 resp
)
103 struct bnep_control_rsp rsp
;
104 rsp
.type
= BNEP_CONTROL
;
106 rsp
.resp
= htons(resp
);
107 return bnep_send(s
, &rsp
, sizeof(rsp
));
110 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
111 static inline void bnep_set_default_proto_filter(struct bnep_session
*s
)
114 s
->proto_filter
[0].start
= ETH_P_IP
;
115 s
->proto_filter
[0].end
= ETH_P_ARP
;
116 /* (RARP, AppleTalk) */
117 s
->proto_filter
[1].start
= ETH_P_RARP
;
118 s
->proto_filter
[1].end
= ETH_P_AARP
;
120 s
->proto_filter
[2].start
= ETH_P_IPX
;
121 s
->proto_filter
[2].end
= ETH_P_IPV6
;
125 static int bnep_ctrl_set_netfilter(struct bnep_session
*s
, __be16
*data
, int len
)
132 n
= get_unaligned_be16(data
);
138 BT_DBG("filter len %d", n
);
140 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
142 if (n
<= BNEP_MAX_PROTO_FILTERS
) {
143 struct bnep_proto_filter
*f
= s
->proto_filter
;
146 for (i
= 0; i
< n
; i
++) {
147 f
[i
].start
= get_unaligned_be16(data
++);
148 f
[i
].end
= get_unaligned_be16(data
++);
150 BT_DBG("proto filter start %d end %d",
151 f
[i
].start
, f
[i
].end
);
154 if (i
< BNEP_MAX_PROTO_FILTERS
)
155 memset(f
+ i
, 0, sizeof(*f
));
158 bnep_set_default_proto_filter(s
);
160 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_SUCCESS
);
162 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_FILTER_LIMIT_REACHED
);
165 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_FILTER_UNSUPPORTED_REQ
);
170 static int bnep_ctrl_set_mcfilter(struct bnep_session
*s
, u8
*data
, int len
)
177 n
= get_unaligned_be16(data
);
183 BT_DBG("filter len %d", n
);
185 #ifdef CONFIG_BT_BNEP_MC_FILTER
191 /* Always send broadcast */
192 set_bit(bnep_mc_hash(s
->dev
->broadcast
), (ulong
*) &s
->mc_filter
);
194 /* Add address ranges to the multicast hash */
198 memcpy(a1
, data
, ETH_ALEN
); data
+= ETH_ALEN
;
199 a2
= data
; data
+= ETH_ALEN
;
201 BT_DBG("mc filter %s -> %s",
202 batostr((void *) a1
), batostr((void *) a2
));
204 #define INCA(a) { int i = 5; while (i >=0 && ++a[i--] == 0); }
206 /* Iterate from a1 to a2 */
207 set_bit(bnep_mc_hash(a1
), (ulong
*) &s
->mc_filter
);
208 while (memcmp(a1
, a2
, 6) < 0 && s
->mc_filter
!= ~0LL) {
210 set_bit(bnep_mc_hash(a1
), (ulong
*) &s
->mc_filter
);
215 BT_DBG("mc filter hash 0x%llx", s
->mc_filter
);
217 bnep_send_rsp(s
, BNEP_FILTER_MULTI_ADDR_RSP
, BNEP_SUCCESS
);
219 bnep_send_rsp(s
, BNEP_FILTER_MULTI_ADDR_RSP
, BNEP_FILTER_UNSUPPORTED_REQ
);
224 static int bnep_rx_control(struct bnep_session
*s
, void *data
, int len
)
226 u8 cmd
= *(u8
*)data
;
232 case BNEP_CMD_NOT_UNDERSTOOD
:
233 case BNEP_SETUP_CONN_REQ
:
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
);
250 pkt
[0] = BNEP_CONTROL
;
251 pkt
[1] = BNEP_CMD_NOT_UNDERSTOOD
;
253 bnep_send(s
, pkt
, sizeof(pkt
));
261 static int bnep_rx_extension(struct bnep_session
*s
, struct sk_buff
*skb
)
263 struct bnep_ext_hdr
*h
;
267 h
= (void *) skb
->data
;
268 if (!skb_pull(skb
, sizeof(*h
))) {
273 BT_DBG("type 0x%x len %d", h
->type
, h
->len
);
275 switch (h
->type
& BNEP_TYPE_MASK
) {
276 case BNEP_EXT_CONTROL
:
277 bnep_rx_control(s
, skb
->data
, skb
->len
);
281 /* Unknown extension, skip it. */
285 if (!skb_pull(skb
, h
->len
)) {
289 } while (!err
&& (h
->type
& BNEP_EXT_HEADER
));
294 static u8 __bnep_rx_hlen
[] = {
295 ETH_HLEN
, /* BNEP_GENERAL */
296 0, /* BNEP_CONTROL */
297 2, /* BNEP_COMPRESSED */
298 ETH_ALEN
+ 2, /* BNEP_COMPRESSED_SRC_ONLY */
299 ETH_ALEN
+ 2 /* BNEP_COMPRESSED_DST_ONLY */
301 #define BNEP_RX_TYPES (sizeof(__bnep_rx_hlen) - 1)
303 static inline int bnep_rx_frame(struct bnep_session
*s
, struct sk_buff
*skb
)
305 struct net_device
*dev
= s
->dev
;
306 struct sk_buff
*nskb
;
309 dev
->stats
.rx_bytes
+= skb
->len
;
311 type
= *(u8
*) skb
->data
; skb_pull(skb
, 1);
313 if ((type
& BNEP_TYPE_MASK
) > BNEP_RX_TYPES
)
316 if ((type
& BNEP_TYPE_MASK
) == BNEP_CONTROL
) {
317 bnep_rx_control(s
, skb
->data
, skb
->len
);
322 skb_reset_mac_header(skb
);
324 /* Verify and pull out header */
325 if (!skb_pull(skb
, __bnep_rx_hlen
[type
& BNEP_TYPE_MASK
]))
328 s
->eh
.h_proto
= get_unaligned((__be16
*) (skb
->data
- 2));
330 if (type
& BNEP_EXT_HEADER
) {
331 if (bnep_rx_extension(s
, skb
) < 0)
335 /* Strip 802.1p header */
336 if (ntohs(s
->eh
.h_proto
) == 0x8100) {
337 if (!skb_pull(skb
, 4))
339 s
->eh
.h_proto
= get_unaligned((__be16
*) (skb
->data
- 2));
342 /* We have to alloc new skb and copy data here :(. Because original skb
343 * may not be modified and because of the alignment requirements. */
344 nskb
= alloc_skb(2 + ETH_HLEN
+ skb
->len
, GFP_KERNEL
);
346 dev
->stats
.rx_dropped
++;
350 skb_reserve(nskb
, 2);
352 /* Decompress header and construct ether frame */
353 switch (type
& BNEP_TYPE_MASK
) {
354 case BNEP_COMPRESSED
:
355 memcpy(__skb_put(nskb
, ETH_HLEN
), &s
->eh
, ETH_HLEN
);
358 case BNEP_COMPRESSED_SRC_ONLY
:
359 memcpy(__skb_put(nskb
, ETH_ALEN
), s
->eh
.h_dest
, ETH_ALEN
);
360 memcpy(__skb_put(nskb
, ETH_ALEN
), skb_mac_header(skb
), ETH_ALEN
);
361 put_unaligned(s
->eh
.h_proto
, (__be16
*) __skb_put(nskb
, 2));
364 case BNEP_COMPRESSED_DST_ONLY
:
365 memcpy(__skb_put(nskb
, ETH_ALEN
), skb_mac_header(skb
),
367 memcpy(__skb_put(nskb
, ETH_ALEN
+ 2), s
->eh
.h_source
,
372 memcpy(__skb_put(nskb
, ETH_ALEN
* 2), skb_mac_header(skb
),
374 put_unaligned(s
->eh
.h_proto
, (__be16
*) __skb_put(nskb
, 2));
378 skb_copy_from_linear_data(skb
, __skb_put(nskb
, skb
->len
), skb
->len
);
381 dev
->stats
.rx_packets
++;
382 nskb
->ip_summed
= CHECKSUM_NONE
;
383 nskb
->protocol
= eth_type_trans(nskb
, dev
);
388 dev
->stats
.rx_errors
++;
393 static u8 __bnep_tx_types
[] = {
395 BNEP_COMPRESSED_SRC_ONLY
,
396 BNEP_COMPRESSED_DST_ONLY
,
400 static inline int bnep_tx_frame(struct bnep_session
*s
, struct sk_buff
*skb
)
402 struct ethhdr
*eh
= (void *) skb
->data
;
403 struct socket
*sock
= s
->sock
;
408 BT_DBG("skb %p dev %p type %d", skb
, skb
->dev
, skb
->pkt_type
);
411 /* Control frame sent by us */
415 iv
[il
++] = (struct kvec
) { &type
, 1 };
418 if (compress_src
&& !compare_ether_addr(eh
->h_dest
, s
->eh
.h_source
))
421 if (compress_dst
&& !compare_ether_addr(eh
->h_source
, s
->eh
.h_dest
))
425 skb_pull(skb
, ETH_ALEN
* 2);
427 type
= __bnep_tx_types
[type
];
429 case BNEP_COMPRESSED_SRC_ONLY
:
430 iv
[il
++] = (struct kvec
) { eh
->h_source
, ETH_ALEN
};
434 case BNEP_COMPRESSED_DST_ONLY
:
435 iv
[il
++] = (struct kvec
) { eh
->h_dest
, ETH_ALEN
};
441 iv
[il
++] = (struct kvec
) { skb
->data
, skb
->len
};
444 /* FIXME: linearize skb */
446 len
= kernel_sendmsg(sock
, &s
->msg
, iv
, il
, len
);
451 s
->dev
->stats
.tx_bytes
+= len
;
452 s
->dev
->stats
.tx_packets
++;
459 static int bnep_session(void *arg
)
461 struct bnep_session
*s
= arg
;
462 struct net_device
*dev
= s
->dev
;
463 struct sock
*sk
= s
->sock
->sk
;
469 daemonize("kbnepd %s", dev
->name
);
470 set_user_nice(current
, -15);
472 init_waitqueue_entry(&wait
, current
);
473 add_wait_queue(sk
->sk_sleep
, &wait
);
474 while (!atomic_read(&s
->killed
)) {
475 set_current_state(TASK_INTERRUPTIBLE
);
478 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
))) {
480 bnep_rx_frame(s
, skb
);
483 if (sk
->sk_state
!= BT_CONNECTED
)
487 while ((skb
= skb_dequeue(&sk
->sk_write_queue
)))
488 if (bnep_tx_frame(s
, skb
))
490 netif_wake_queue(dev
);
494 set_current_state(TASK_RUNNING
);
495 remove_wait_queue(sk
->sk_sleep
, &wait
);
497 /* Cleanup session */
498 down_write(&bnep_session_sem
);
500 /* Delete network device */
501 unregister_netdev(dev
);
503 /* Wakeup user-space polling for socket errors */
504 s
->sock
->sk
->sk_err
= EUNATCH
;
506 wake_up_interruptible(s
->sock
->sk
->sk_sleep
);
508 /* Release the socket */
511 __bnep_unlink_session(s
);
513 up_write(&bnep_session_sem
);
518 static struct device
*bnep_get_device(struct bnep_session
*session
)
520 bdaddr_t
*src
= &bt_sk(session
->sock
->sk
)->src
;
521 bdaddr_t
*dst
= &bt_sk(session
->sock
->sk
)->dst
;
522 struct hci_dev
*hdev
;
523 struct hci_conn
*conn
;
525 hdev
= hci_get_route(dst
, src
);
529 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, dst
);
533 return conn
? &conn
->dev
: NULL
;
536 int bnep_add_connection(struct bnep_connadd_req
*req
, struct socket
*sock
)
538 struct net_device
*dev
;
539 struct bnep_session
*s
, *ss
;
540 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
545 baswap((void *) dst
, &bt_sk(sock
->sk
)->dst
);
546 baswap((void *) src
, &bt_sk(sock
->sk
)->src
);
548 /* session struct allocated as private part of net_device */
549 dev
= alloc_netdev(sizeof(struct bnep_session
),
550 (*req
->device
) ? req
->device
: "bnep%d",
555 down_write(&bnep_session_sem
);
557 ss
= __bnep_get_session(dst
);
558 if (ss
&& ss
->state
== BT_CONNECTED
) {
563 s
= netdev_priv(dev
);
565 /* This is rx header therefore addresses are swapped.
566 * ie eh.h_dest is our local address. */
567 memcpy(s
->eh
.h_dest
, &src
, ETH_ALEN
);
568 memcpy(s
->eh
.h_source
, &dst
, ETH_ALEN
);
569 memcpy(dev
->dev_addr
, s
->eh
.h_dest
, ETH_ALEN
);
574 s
->state
= BT_CONNECTED
;
576 s
->msg
.msg_flags
= MSG_NOSIGNAL
;
578 #ifdef CONFIG_BT_BNEP_MC_FILTER
579 /* Set default mc filter */
580 set_bit(bnep_mc_hash(dev
->broadcast
), (ulong
*) &s
->mc_filter
);
583 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
584 /* Set default protocol filter */
585 bnep_set_default_proto_filter(s
);
588 SET_NETDEV_DEV(dev
, bnep_get_device(s
));
590 err
= register_netdev(dev
);
595 __bnep_link_session(s
);
597 err
= kernel_thread(bnep_session
, s
, CLONE_KERNEL
);
599 /* Session thread start failed, gotta cleanup. */
600 unregister_netdev(dev
);
601 __bnep_unlink_session(s
);
605 up_write(&bnep_session_sem
);
606 strcpy(req
->device
, dev
->name
);
610 up_write(&bnep_session_sem
);
615 int bnep_del_connection(struct bnep_conndel_req
*req
)
617 struct bnep_session
*s
;
622 down_read(&bnep_session_sem
);
624 s
= __bnep_get_session(req
->dst
);
626 /* Wakeup user-space which is polling for socket errors.
627 * This is temporary hack untill we have shutdown in L2CAP */
628 s
->sock
->sk
->sk_err
= EUNATCH
;
630 /* Kill session thread */
631 atomic_inc(&s
->killed
);
632 wake_up_interruptible(s
->sock
->sk
->sk_sleep
);
636 up_read(&bnep_session_sem
);
640 static void __bnep_copy_ci(struct bnep_conninfo
*ci
, struct bnep_session
*s
)
642 memcpy(ci
->dst
, s
->eh
.h_source
, ETH_ALEN
);
643 strcpy(ci
->device
, s
->dev
->name
);
644 ci
->flags
= s
->flags
;
645 ci
->state
= s
->state
;
649 int bnep_get_connlist(struct bnep_connlist_req
*req
)
654 down_read(&bnep_session_sem
);
656 list_for_each(p
, &bnep_session_list
) {
657 struct bnep_session
*s
;
658 struct bnep_conninfo ci
;
660 s
= list_entry(p
, struct bnep_session
, list
);
662 __bnep_copy_ci(&ci
, s
);
664 if (copy_to_user(req
->ci
, &ci
, sizeof(ci
))) {
669 if (++n
>= req
->cnum
)
676 up_read(&bnep_session_sem
);
680 int bnep_get_conninfo(struct bnep_conninfo
*ci
)
682 struct bnep_session
*s
;
685 down_read(&bnep_session_sem
);
687 s
= __bnep_get_session(ci
->dst
);
689 __bnep_copy_ci(ci
, s
);
693 up_read(&bnep_session_sem
);
697 static int __init
bnep_init(void)
703 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
704 strcat(flt
, "protocol ");
707 #ifdef CONFIG_BT_BNEP_MC_FILTER
708 strcat(flt
, "multicast");
711 BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION
);
713 BT_INFO("BNEP filters: %s", flt
);
719 static void __exit
bnep_exit(void)
724 module_init(bnep_init
);
725 module_exit(bnep_exit
);
727 module_param(compress_src
, bool, 0644);
728 MODULE_PARM_DESC(compress_src
, "Compress sources headers");
730 module_param(compress_dst
, bool, 0644);
731 MODULE_PARM_DESC(compress_dst
, "Compress destination headers");
733 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
734 MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION
);
735 MODULE_VERSION(VERSION
);
736 MODULE_LICENSE("GPL");
737 MODULE_ALIAS("bt-proto-4");