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>
29 #include <linux/kthread.h>
30 #include <linux/file.h>
31 #include <linux/etherdevice.h>
32 #include <asm/unaligned.h>
34 #include <net/bluetooth/bluetooth.h>
35 #include <net/bluetooth/hci_core.h>
41 static bool compress_src
= true;
42 static bool compress_dst
= true;
44 static LIST_HEAD(bnep_session_list
);
45 static DECLARE_RWSEM(bnep_session_sem
);
47 static struct bnep_session
*__bnep_get_session(u8
*dst
)
49 struct bnep_session
*s
;
53 list_for_each_entry(s
, &bnep_session_list
, list
)
54 if (ether_addr_equal(dst
, s
->eh
.h_source
))
60 static void __bnep_link_session(struct bnep_session
*s
)
62 list_add(&s
->list
, &bnep_session_list
);
65 static void __bnep_unlink_session(struct bnep_session
*s
)
70 static int bnep_send(struct bnep_session
*s
, void *data
, size_t len
)
72 struct socket
*sock
= s
->sock
;
73 struct kvec iv
= { data
, len
};
75 return kernel_sendmsg(sock
, &s
->msg
, &iv
, 1, len
);
78 static int bnep_send_rsp(struct bnep_session
*s
, u8 ctrl
, u16 resp
)
80 struct bnep_control_rsp rsp
;
81 rsp
.type
= BNEP_CONTROL
;
83 rsp
.resp
= htons(resp
);
84 return bnep_send(s
, &rsp
, sizeof(rsp
));
87 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
88 static inline void bnep_set_default_proto_filter(struct bnep_session
*s
)
91 s
->proto_filter
[0].start
= ETH_P_IP
;
92 s
->proto_filter
[0].end
= ETH_P_ARP
;
93 /* (RARP, AppleTalk) */
94 s
->proto_filter
[1].start
= ETH_P_RARP
;
95 s
->proto_filter
[1].end
= ETH_P_AARP
;
97 s
->proto_filter
[2].start
= ETH_P_IPX
;
98 s
->proto_filter
[2].end
= ETH_P_IPV6
;
102 static int bnep_ctrl_set_netfilter(struct bnep_session
*s
, __be16
*data
, int len
)
109 n
= get_unaligned_be16(data
);
116 BT_DBG("filter len %d", n
);
118 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
120 if (n
<= BNEP_MAX_PROTO_FILTERS
) {
121 struct bnep_proto_filter
*f
= s
->proto_filter
;
124 for (i
= 0; i
< n
; i
++) {
125 f
[i
].start
= get_unaligned_be16(data
++);
126 f
[i
].end
= get_unaligned_be16(data
++);
128 BT_DBG("proto filter start %d end %d",
129 f
[i
].start
, f
[i
].end
);
132 if (i
< BNEP_MAX_PROTO_FILTERS
)
133 memset(f
+ i
, 0, sizeof(*f
));
136 bnep_set_default_proto_filter(s
);
138 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_SUCCESS
);
140 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_FILTER_LIMIT_REACHED
);
143 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_FILTER_UNSUPPORTED_REQ
);
148 static int bnep_ctrl_set_mcfilter(struct bnep_session
*s
, u8
*data
, int len
)
155 n
= get_unaligned_be16(data
);
162 BT_DBG("filter len %d", n
);
164 #ifdef CONFIG_BT_BNEP_MC_FILTER
172 /* Always send broadcast */
173 set_bit(bnep_mc_hash(s
->dev
->broadcast
), (ulong
*) &s
->mc_filter
);
175 /* Add address ranges to the multicast hash */
179 memcpy(a1
, data
, ETH_ALEN
);
184 BT_DBG("mc filter %pMR -> %pMR", a1
, a2
);
186 /* Iterate from a1 to a2 */
187 set_bit(bnep_mc_hash(a1
), (ulong
*) &s
->mc_filter
);
188 while (memcmp(a1
, a2
, 6) < 0 && s
->mc_filter
!= ~0LL) {
191 while (i
>= 0 && ++a1
[i
--] == 0)
194 set_bit(bnep_mc_hash(a1
), (ulong
*) &s
->mc_filter
);
199 BT_DBG("mc filter hash 0x%llx", s
->mc_filter
);
201 bnep_send_rsp(s
, BNEP_FILTER_MULTI_ADDR_RSP
, BNEP_SUCCESS
);
203 bnep_send_rsp(s
, BNEP_FILTER_MULTI_ADDR_RSP
, BNEP_FILTER_UNSUPPORTED_REQ
);
208 static int bnep_rx_control(struct bnep_session
*s
, void *data
, int len
)
210 u8 cmd
= *(u8
*)data
;
217 case BNEP_CMD_NOT_UNDERSTOOD
:
218 case BNEP_SETUP_CONN_RSP
:
219 case BNEP_FILTER_NET_TYPE_RSP
:
220 case BNEP_FILTER_MULTI_ADDR_RSP
:
221 /* Ignore these for now */
224 case BNEP_FILTER_NET_TYPE_SET
:
225 err
= bnep_ctrl_set_netfilter(s
, data
, len
);
228 case BNEP_FILTER_MULTI_ADDR_SET
:
229 err
= bnep_ctrl_set_mcfilter(s
, data
, len
);
232 case BNEP_SETUP_CONN_REQ
:
233 err
= bnep_send_rsp(s
, BNEP_SETUP_CONN_RSP
, BNEP_CONN_NOT_ALLOWED
);
238 pkt
[0] = BNEP_CONTROL
;
239 pkt
[1] = BNEP_CMD_NOT_UNDERSTOOD
;
241 bnep_send(s
, pkt
, sizeof(pkt
));
249 static int bnep_rx_extension(struct bnep_session
*s
, struct sk_buff
*skb
)
251 struct bnep_ext_hdr
*h
;
255 h
= (void *) skb
->data
;
256 if (!skb_pull(skb
, sizeof(*h
))) {
261 BT_DBG("type 0x%x len %d", h
->type
, h
->len
);
263 switch (h
->type
& BNEP_TYPE_MASK
) {
264 case BNEP_EXT_CONTROL
:
265 bnep_rx_control(s
, skb
->data
, skb
->len
);
269 /* Unknown extension, skip it. */
273 if (!skb_pull(skb
, h
->len
)) {
277 } while (!err
&& (h
->type
& BNEP_EXT_HEADER
));
282 static u8 __bnep_rx_hlen
[] = {
283 ETH_HLEN
, /* BNEP_GENERAL */
284 0, /* BNEP_CONTROL */
285 2, /* BNEP_COMPRESSED */
286 ETH_ALEN
+ 2, /* BNEP_COMPRESSED_SRC_ONLY */
287 ETH_ALEN
+ 2 /* BNEP_COMPRESSED_DST_ONLY */
290 static int bnep_rx_frame(struct bnep_session
*s
, struct sk_buff
*skb
)
292 struct net_device
*dev
= s
->dev
;
293 struct sk_buff
*nskb
;
296 dev
->stats
.rx_bytes
+= skb
->len
;
298 type
= *(u8
*) skb
->data
;
301 if ((type
& BNEP_TYPE_MASK
) >= sizeof(__bnep_rx_hlen
))
304 if ((type
& BNEP_TYPE_MASK
) == BNEP_CONTROL
) {
305 bnep_rx_control(s
, skb
->data
, skb
->len
);
310 skb_reset_mac_header(skb
);
312 /* Verify and pull out header */
313 if (!skb_pull(skb
, __bnep_rx_hlen
[type
& BNEP_TYPE_MASK
]))
316 s
->eh
.h_proto
= get_unaligned((__be16
*) (skb
->data
- 2));
318 if (type
& BNEP_EXT_HEADER
) {
319 if (bnep_rx_extension(s
, skb
) < 0)
323 /* Strip 802.1p header */
324 if (ntohs(s
->eh
.h_proto
) == ETH_P_8021Q
) {
325 if (!skb_pull(skb
, 4))
327 s
->eh
.h_proto
= get_unaligned((__be16
*) (skb
->data
- 2));
330 /* We have to alloc new skb and copy data here :(. Because original skb
331 * may not be modified and because of the alignment requirements. */
332 nskb
= alloc_skb(2 + ETH_HLEN
+ skb
->len
, GFP_KERNEL
);
334 dev
->stats
.rx_dropped
++;
338 skb_reserve(nskb
, 2);
340 /* Decompress header and construct ether frame */
341 switch (type
& BNEP_TYPE_MASK
) {
342 case BNEP_COMPRESSED
:
343 memcpy(__skb_put(nskb
, ETH_HLEN
), &s
->eh
, ETH_HLEN
);
346 case BNEP_COMPRESSED_SRC_ONLY
:
347 memcpy(__skb_put(nskb
, ETH_ALEN
), s
->eh
.h_dest
, ETH_ALEN
);
348 memcpy(__skb_put(nskb
, ETH_ALEN
), skb_mac_header(skb
), ETH_ALEN
);
349 put_unaligned(s
->eh
.h_proto
, (__be16
*) __skb_put(nskb
, 2));
352 case BNEP_COMPRESSED_DST_ONLY
:
353 memcpy(__skb_put(nskb
, ETH_ALEN
), skb_mac_header(skb
),
355 memcpy(__skb_put(nskb
, ETH_ALEN
+ 2), s
->eh
.h_source
,
360 memcpy(__skb_put(nskb
, ETH_ALEN
* 2), skb_mac_header(skb
),
362 put_unaligned(s
->eh
.h_proto
, (__be16
*) __skb_put(nskb
, 2));
366 skb_copy_from_linear_data(skb
, __skb_put(nskb
, skb
->len
), skb
->len
);
369 dev
->stats
.rx_packets
++;
370 nskb
->ip_summed
= CHECKSUM_NONE
;
371 nskb
->protocol
= eth_type_trans(nskb
, dev
);
376 dev
->stats
.rx_errors
++;
381 static u8 __bnep_tx_types
[] = {
383 BNEP_COMPRESSED_SRC_ONLY
,
384 BNEP_COMPRESSED_DST_ONLY
,
388 static int bnep_tx_frame(struct bnep_session
*s
, struct sk_buff
*skb
)
390 struct ethhdr
*eh
= (void *) skb
->data
;
391 struct socket
*sock
= s
->sock
;
396 BT_DBG("skb %p dev %p type %d", skb
, skb
->dev
, skb
->pkt_type
);
399 /* Control frame sent by us */
403 iv
[il
++] = (struct kvec
) { &type
, 1 };
406 if (compress_src
&& ether_addr_equal(eh
->h_dest
, s
->eh
.h_source
))
409 if (compress_dst
&& ether_addr_equal(eh
->h_source
, s
->eh
.h_dest
))
413 skb_pull(skb
, ETH_ALEN
* 2);
415 type
= __bnep_tx_types
[type
];
417 case BNEP_COMPRESSED_SRC_ONLY
:
418 iv
[il
++] = (struct kvec
) { eh
->h_source
, ETH_ALEN
};
422 case BNEP_COMPRESSED_DST_ONLY
:
423 iv
[il
++] = (struct kvec
) { eh
->h_dest
, ETH_ALEN
};
429 iv
[il
++] = (struct kvec
) { skb
->data
, skb
->len
};
432 /* FIXME: linearize skb */
434 len
= kernel_sendmsg(sock
, &s
->msg
, iv
, il
, len
);
439 s
->dev
->stats
.tx_bytes
+= len
;
440 s
->dev
->stats
.tx_packets
++;
447 static int bnep_session(void *arg
)
449 struct bnep_session
*s
= arg
;
450 struct net_device
*dev
= s
->dev
;
451 struct sock
*sk
= s
->sock
->sk
;
457 set_user_nice(current
, -15);
459 init_waitqueue_entry(&wait
, current
);
460 add_wait_queue(sk_sleep(sk
), &wait
);
462 set_current_state(TASK_INTERRUPTIBLE
);
464 if (atomic_read(&s
->terminate
))
467 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
))) {
469 if (!skb_linearize(skb
))
470 bnep_rx_frame(s
, skb
);
475 if (sk
->sk_state
!= BT_CONNECTED
)
479 while ((skb
= skb_dequeue(&sk
->sk_write_queue
)))
480 if (bnep_tx_frame(s
, skb
))
482 netif_wake_queue(dev
);
486 __set_current_state(TASK_RUNNING
);
487 remove_wait_queue(sk_sleep(sk
), &wait
);
489 /* Cleanup session */
490 down_write(&bnep_session_sem
);
492 /* Delete network device */
493 unregister_netdev(dev
);
495 /* Wakeup user-space polling for socket errors */
496 s
->sock
->sk
->sk_err
= EUNATCH
;
498 wake_up_interruptible(sk_sleep(s
->sock
->sk
));
500 /* Release the socket */
503 __bnep_unlink_session(s
);
505 up_write(&bnep_session_sem
);
507 module_put_and_exit(0);
511 static struct device
*bnep_get_device(struct bnep_session
*session
)
513 bdaddr_t
*src
= &bt_sk(session
->sock
->sk
)->src
;
514 bdaddr_t
*dst
= &bt_sk(session
->sock
->sk
)->dst
;
515 struct hci_dev
*hdev
;
516 struct hci_conn
*conn
;
518 hdev
= hci_get_route(dst
, src
);
522 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, dst
);
526 return conn
? &conn
->dev
: NULL
;
529 static struct device_type bnep_type
= {
533 int bnep_add_connection(struct bnep_connadd_req
*req
, struct socket
*sock
)
535 struct net_device
*dev
;
536 struct bnep_session
*s
, *ss
;
537 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
542 baswap((void *) dst
, &bt_sk(sock
->sk
)->dst
);
543 baswap((void *) src
, &bt_sk(sock
->sk
)->src
);
545 /* session struct allocated as private part of net_device */
546 dev
= alloc_netdev(sizeof(struct bnep_session
),
547 (*req
->device
) ? req
->device
: "bnep%d",
552 down_write(&bnep_session_sem
);
554 ss
= __bnep_get_session(dst
);
555 if (ss
&& ss
->state
== BT_CONNECTED
) {
560 s
= netdev_priv(dev
);
562 /* This is rx header therefore addresses are swapped.
563 * ie. eh.h_dest is our local address. */
564 memcpy(s
->eh
.h_dest
, &src
, ETH_ALEN
);
565 memcpy(s
->eh
.h_source
, &dst
, ETH_ALEN
);
566 memcpy(dev
->dev_addr
, s
->eh
.h_dest
, ETH_ALEN
);
571 s
->state
= BT_CONNECTED
;
573 s
->msg
.msg_flags
= MSG_NOSIGNAL
;
575 #ifdef CONFIG_BT_BNEP_MC_FILTER
576 /* Set default mc filter */
577 set_bit(bnep_mc_hash(dev
->broadcast
), (ulong
*) &s
->mc_filter
);
580 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
581 /* Set default protocol filter */
582 bnep_set_default_proto_filter(s
);
585 SET_NETDEV_DEV(dev
, bnep_get_device(s
));
586 SET_NETDEV_DEVTYPE(dev
, &bnep_type
);
588 err
= register_netdev(dev
);
592 __bnep_link_session(s
);
594 __module_get(THIS_MODULE
);
595 s
->task
= kthread_run(bnep_session
, s
, "kbnepd %s", dev
->name
);
596 if (IS_ERR(s
->task
)) {
597 /* Session thread start failed, gotta cleanup. */
598 module_put(THIS_MODULE
);
599 unregister_netdev(dev
);
600 __bnep_unlink_session(s
);
601 err
= PTR_ERR(s
->task
);
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 atomic_inc(&s
->terminate
);
627 wake_up_process(s
->task
);
631 up_read(&bnep_session_sem
);
635 static void __bnep_copy_ci(struct bnep_conninfo
*ci
, struct bnep_session
*s
)
637 memset(ci
, 0, sizeof(*ci
));
638 memcpy(ci
->dst
, s
->eh
.h_source
, ETH_ALEN
);
639 strcpy(ci
->device
, s
->dev
->name
);
640 ci
->flags
= s
->flags
;
641 ci
->state
= s
->state
;
645 int bnep_get_connlist(struct bnep_connlist_req
*req
)
647 struct bnep_session
*s
;
650 down_read(&bnep_session_sem
);
652 list_for_each_entry(s
, &bnep_session_list
, list
) {
653 struct bnep_conninfo ci
;
655 __bnep_copy_ci(&ci
, s
);
657 if (copy_to_user(req
->ci
, &ci
, sizeof(ci
))) {
662 if (++n
>= req
->cnum
)
669 up_read(&bnep_session_sem
);
673 int bnep_get_conninfo(struct bnep_conninfo
*ci
)
675 struct bnep_session
*s
;
678 down_read(&bnep_session_sem
);
680 s
= __bnep_get_session(ci
->dst
);
682 __bnep_copy_ci(ci
, s
);
686 up_read(&bnep_session_sem
);
690 static int __init
bnep_init(void)
694 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
695 strcat(flt
, "protocol ");
698 #ifdef CONFIG_BT_BNEP_MC_FILTER
699 strcat(flt
, "multicast");
702 BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION
);
704 BT_INFO("BNEP filters: %s", flt
);
710 static void __exit
bnep_exit(void)
715 module_init(bnep_init
);
716 module_exit(bnep_exit
);
718 module_param(compress_src
, bool, 0644);
719 MODULE_PARM_DESC(compress_src
, "Compress sources headers");
721 module_param(compress_dst
, bool, 0644);
722 MODULE_PARM_DESC(compress_dst
, "Compress destination headers");
724 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
725 MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION
);
726 MODULE_VERSION(VERSION
);
727 MODULE_LICENSE("GPL");
728 MODULE_ALIAS("bt-proto-4");