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/l2cap.h>
36 #include <net/bluetooth/hci_core.h>
42 static bool compress_src
= true;
43 static bool compress_dst
= true;
45 static LIST_HEAD(bnep_session_list
);
46 static DECLARE_RWSEM(bnep_session_sem
);
48 static struct bnep_session
*__bnep_get_session(u8
*dst
)
50 struct bnep_session
*s
;
54 list_for_each_entry(s
, &bnep_session_list
, list
)
55 if (ether_addr_equal(dst
, s
->eh
.h_source
))
61 static void __bnep_link_session(struct bnep_session
*s
)
63 list_add(&s
->list
, &bnep_session_list
);
66 static void __bnep_unlink_session(struct bnep_session
*s
)
71 static int bnep_send(struct bnep_session
*s
, void *data
, size_t len
)
73 struct socket
*sock
= s
->sock
;
74 struct kvec iv
= { data
, len
};
76 return kernel_sendmsg(sock
, &s
->msg
, &iv
, 1, len
);
79 static int bnep_send_rsp(struct bnep_session
*s
, u8 ctrl
, u16 resp
)
81 struct bnep_control_rsp rsp
;
82 rsp
.type
= BNEP_CONTROL
;
84 rsp
.resp
= htons(resp
);
85 return bnep_send(s
, &rsp
, sizeof(rsp
));
88 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
89 static inline void bnep_set_default_proto_filter(struct bnep_session
*s
)
92 s
->proto_filter
[0].start
= ETH_P_IP
;
93 s
->proto_filter
[0].end
= ETH_P_ARP
;
94 /* (RARP, AppleTalk) */
95 s
->proto_filter
[1].start
= ETH_P_RARP
;
96 s
->proto_filter
[1].end
= ETH_P_AARP
;
98 s
->proto_filter
[2].start
= ETH_P_IPX
;
99 s
->proto_filter
[2].end
= ETH_P_IPV6
;
103 static int bnep_ctrl_set_netfilter(struct bnep_session
*s
, __be16
*data
, int len
)
110 n
= get_unaligned_be16(data
);
117 BT_DBG("filter len %d", n
);
119 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
121 if (n
<= BNEP_MAX_PROTO_FILTERS
) {
122 struct bnep_proto_filter
*f
= s
->proto_filter
;
125 for (i
= 0; i
< n
; i
++) {
126 f
[i
].start
= get_unaligned_be16(data
++);
127 f
[i
].end
= get_unaligned_be16(data
++);
129 BT_DBG("proto filter start %d end %d",
130 f
[i
].start
, f
[i
].end
);
133 if (i
< BNEP_MAX_PROTO_FILTERS
)
134 memset(f
+ i
, 0, sizeof(*f
));
137 bnep_set_default_proto_filter(s
);
139 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_SUCCESS
);
141 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_FILTER_LIMIT_REACHED
);
144 bnep_send_rsp(s
, BNEP_FILTER_NET_TYPE_RSP
, BNEP_FILTER_UNSUPPORTED_REQ
);
149 static int bnep_ctrl_set_mcfilter(struct bnep_session
*s
, u8
*data
, int len
)
156 n
= get_unaligned_be16(data
);
163 BT_DBG("filter len %d", n
);
165 #ifdef CONFIG_BT_BNEP_MC_FILTER
173 /* Always send broadcast */
174 set_bit(bnep_mc_hash(s
->dev
->broadcast
), (ulong
*) &s
->mc_filter
);
176 /* Add address ranges to the multicast hash */
180 memcpy(a1
, data
, ETH_ALEN
);
185 BT_DBG("mc filter %pMR -> %pMR", a1
, a2
);
187 /* Iterate from a1 to a2 */
188 set_bit(bnep_mc_hash(a1
), (ulong
*) &s
->mc_filter
);
189 while (memcmp(a1
, a2
, 6) < 0 && s
->mc_filter
!= ~0LL) {
192 while (i
>= 0 && ++a1
[i
--] == 0)
195 set_bit(bnep_mc_hash(a1
), (ulong
*) &s
->mc_filter
);
200 BT_DBG("mc filter hash 0x%llx", s
->mc_filter
);
202 bnep_send_rsp(s
, BNEP_FILTER_MULTI_ADDR_RSP
, BNEP_SUCCESS
);
204 bnep_send_rsp(s
, BNEP_FILTER_MULTI_ADDR_RSP
, BNEP_FILTER_UNSUPPORTED_REQ
);
209 static int bnep_rx_control(struct bnep_session
*s
, void *data
, int len
)
211 u8 cmd
= *(u8
*)data
;
218 case BNEP_CMD_NOT_UNDERSTOOD
:
219 case BNEP_SETUP_CONN_RSP
:
220 case BNEP_FILTER_NET_TYPE_RSP
:
221 case BNEP_FILTER_MULTI_ADDR_RSP
:
222 /* Ignore these for now */
225 case BNEP_FILTER_NET_TYPE_SET
:
226 err
= bnep_ctrl_set_netfilter(s
, data
, len
);
229 case BNEP_FILTER_MULTI_ADDR_SET
:
230 err
= bnep_ctrl_set_mcfilter(s
, data
, len
);
233 case BNEP_SETUP_CONN_REQ
:
234 /* Successful response should be sent only once */
235 if (test_bit(BNEP_SETUP_RESPONSE
, &s
->flags
) &&
236 !test_and_set_bit(BNEP_SETUP_RSP_SENT
, &s
->flags
))
237 err
= bnep_send_rsp(s
, BNEP_SETUP_CONN_RSP
,
240 err
= bnep_send_rsp(s
, BNEP_SETUP_CONN_RSP
,
241 BNEP_CONN_NOT_ALLOWED
);
246 pkt
[0] = BNEP_CONTROL
;
247 pkt
[1] = BNEP_CMD_NOT_UNDERSTOOD
;
249 err
= bnep_send(s
, pkt
, sizeof(pkt
));
257 static int bnep_rx_extension(struct bnep_session
*s
, struct sk_buff
*skb
)
259 struct bnep_ext_hdr
*h
;
263 h
= (void *) skb
->data
;
264 if (!skb_pull(skb
, sizeof(*h
))) {
269 BT_DBG("type 0x%x len %d", h
->type
, h
->len
);
271 switch (h
->type
& BNEP_TYPE_MASK
) {
272 case BNEP_EXT_CONTROL
:
273 bnep_rx_control(s
, skb
->data
, skb
->len
);
277 /* Unknown extension, skip it. */
281 if (!skb_pull(skb
, h
->len
)) {
285 } while (!err
&& (h
->type
& BNEP_EXT_HEADER
));
290 static u8 __bnep_rx_hlen
[] = {
291 ETH_HLEN
, /* BNEP_GENERAL */
292 0, /* BNEP_CONTROL */
293 2, /* BNEP_COMPRESSED */
294 ETH_ALEN
+ 2, /* BNEP_COMPRESSED_SRC_ONLY */
295 ETH_ALEN
+ 2 /* BNEP_COMPRESSED_DST_ONLY */
298 static int bnep_rx_frame(struct bnep_session
*s
, struct sk_buff
*skb
)
300 struct net_device
*dev
= s
->dev
;
301 struct sk_buff
*nskb
;
304 dev
->stats
.rx_bytes
+= skb
->len
;
306 type
= *(u8
*) skb
->data
;
308 ctrl_type
= *(u8
*)skb
->data
;
310 if ((type
& BNEP_TYPE_MASK
) >= sizeof(__bnep_rx_hlen
))
313 if ((type
& BNEP_TYPE_MASK
) == BNEP_CONTROL
) {
314 if (bnep_rx_control(s
, skb
->data
, skb
->len
) < 0) {
315 dev
->stats
.tx_errors
++;
320 if (!(type
& BNEP_EXT_HEADER
)) {
325 /* Verify and pull ctrl message since it's already processed */
327 case BNEP_SETUP_CONN_REQ
:
328 /* Pull: ctrl type (1 b), len (1 b), data (len bytes) */
329 if (!skb_pull(skb
, 2 + *(u8
*)(skb
->data
+ 1) * 2))
332 case BNEP_FILTER_MULTI_ADDR_SET
:
333 case BNEP_FILTER_NET_TYPE_SET
:
334 /* Pull: ctrl type (1 b), len (2 b), data (len bytes) */
335 if (!skb_pull(skb
, 3 + *(u16
*)(skb
->data
+ 1) * 2))
343 skb_reset_mac_header(skb
);
345 /* Verify and pull out header */
346 if (!skb_pull(skb
, __bnep_rx_hlen
[type
& BNEP_TYPE_MASK
]))
349 s
->eh
.h_proto
= get_unaligned((__be16
*) (skb
->data
- 2));
352 if (type
& BNEP_EXT_HEADER
) {
353 if (bnep_rx_extension(s
, skb
) < 0)
357 /* Strip 802.1p header */
358 if (ntohs(s
->eh
.h_proto
) == ETH_P_8021Q
) {
359 if (!skb_pull(skb
, 4))
361 s
->eh
.h_proto
= get_unaligned((__be16
*) (skb
->data
- 2));
364 /* We have to alloc new skb and copy data here :(. Because original skb
365 * may not be modified and because of the alignment requirements. */
366 nskb
= alloc_skb(2 + ETH_HLEN
+ skb
->len
, GFP_KERNEL
);
368 dev
->stats
.rx_dropped
++;
372 skb_reserve(nskb
, 2);
374 /* Decompress header and construct ether frame */
375 switch (type
& BNEP_TYPE_MASK
) {
376 case BNEP_COMPRESSED
:
377 memcpy(__skb_put(nskb
, ETH_HLEN
), &s
->eh
, ETH_HLEN
);
380 case BNEP_COMPRESSED_SRC_ONLY
:
381 memcpy(__skb_put(nskb
, ETH_ALEN
), s
->eh
.h_dest
, ETH_ALEN
);
382 memcpy(__skb_put(nskb
, ETH_ALEN
), skb_mac_header(skb
), ETH_ALEN
);
383 put_unaligned(s
->eh
.h_proto
, (__be16
*) __skb_put(nskb
, 2));
386 case BNEP_COMPRESSED_DST_ONLY
:
387 memcpy(__skb_put(nskb
, ETH_ALEN
), skb_mac_header(skb
),
389 memcpy(__skb_put(nskb
, ETH_ALEN
+ 2), s
->eh
.h_source
,
394 memcpy(__skb_put(nskb
, ETH_ALEN
* 2), skb_mac_header(skb
),
396 put_unaligned(s
->eh
.h_proto
, (__be16
*) __skb_put(nskb
, 2));
400 skb_copy_from_linear_data(skb
, __skb_put(nskb
, skb
->len
), skb
->len
);
403 dev
->stats
.rx_packets
++;
404 nskb
->ip_summed
= CHECKSUM_NONE
;
405 nskb
->protocol
= eth_type_trans(nskb
, dev
);
410 dev
->stats
.rx_errors
++;
415 static u8 __bnep_tx_types
[] = {
417 BNEP_COMPRESSED_SRC_ONLY
,
418 BNEP_COMPRESSED_DST_ONLY
,
422 static int bnep_tx_frame(struct bnep_session
*s
, struct sk_buff
*skb
)
424 struct ethhdr
*eh
= (void *) skb
->data
;
425 struct socket
*sock
= s
->sock
;
430 BT_DBG("skb %p dev %p type %d", skb
, skb
->dev
, skb
->pkt_type
);
433 /* Control frame sent by us */
437 iv
[il
++] = (struct kvec
) { &type
, 1 };
440 if (compress_src
&& ether_addr_equal(eh
->h_dest
, s
->eh
.h_source
))
443 if (compress_dst
&& ether_addr_equal(eh
->h_source
, s
->eh
.h_dest
))
447 skb_pull(skb
, ETH_ALEN
* 2);
449 type
= __bnep_tx_types
[type
];
451 case BNEP_COMPRESSED_SRC_ONLY
:
452 iv
[il
++] = (struct kvec
) { eh
->h_source
, ETH_ALEN
};
456 case BNEP_COMPRESSED_DST_ONLY
:
457 iv
[il
++] = (struct kvec
) { eh
->h_dest
, ETH_ALEN
};
463 iv
[il
++] = (struct kvec
) { skb
->data
, skb
->len
};
466 /* FIXME: linearize skb */
468 len
= kernel_sendmsg(sock
, &s
->msg
, iv
, il
, len
);
473 s
->dev
->stats
.tx_bytes
+= len
;
474 s
->dev
->stats
.tx_packets
++;
481 static int bnep_session(void *arg
)
483 struct bnep_session
*s
= arg
;
484 struct net_device
*dev
= s
->dev
;
485 struct sock
*sk
= s
->sock
->sk
;
491 set_user_nice(current
, -15);
493 init_waitqueue_entry(&wait
, current
);
494 add_wait_queue(sk_sleep(sk
), &wait
);
496 set_current_state(TASK_INTERRUPTIBLE
);
498 if (atomic_read(&s
->terminate
))
501 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
))) {
503 if (!skb_linearize(skb
))
504 bnep_rx_frame(s
, skb
);
509 if (sk
->sk_state
!= BT_CONNECTED
)
513 while ((skb
= skb_dequeue(&sk
->sk_write_queue
)))
514 if (bnep_tx_frame(s
, skb
))
516 netif_wake_queue(dev
);
520 __set_current_state(TASK_RUNNING
);
521 remove_wait_queue(sk_sleep(sk
), &wait
);
523 /* Cleanup session */
524 down_write(&bnep_session_sem
);
526 /* Delete network device */
527 unregister_netdev(dev
);
529 /* Wakeup user-space polling for socket errors */
530 s
->sock
->sk
->sk_err
= EUNATCH
;
532 wake_up_interruptible(sk_sleep(s
->sock
->sk
));
534 /* Release the socket */
537 __bnep_unlink_session(s
);
539 up_write(&bnep_session_sem
);
541 module_put_and_exit(0);
545 static struct device
*bnep_get_device(struct bnep_session
*session
)
547 struct l2cap_conn
*conn
= l2cap_pi(session
->sock
->sk
)->chan
->conn
;
549 if (!conn
|| !conn
->hcon
)
552 return &conn
->hcon
->dev
;
555 static struct device_type bnep_type
= {
559 int bnep_add_connection(struct bnep_connadd_req
*req
, struct socket
*sock
)
561 u32 valid_flags
= BIT(BNEP_SETUP_RESPONSE
);
562 struct net_device
*dev
;
563 struct bnep_session
*s
, *ss
;
564 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
569 if (!l2cap_is_socket(sock
))
572 if (req
->flags
& ~valid_flags
)
575 baswap((void *) dst
, &l2cap_pi(sock
->sk
)->chan
->dst
);
576 baswap((void *) src
, &l2cap_pi(sock
->sk
)->chan
->src
);
578 /* session struct allocated as private part of net_device */
579 dev
= alloc_netdev(sizeof(struct bnep_session
),
580 (*req
->device
) ? req
->device
: "bnep%d",
586 down_write(&bnep_session_sem
);
588 ss
= __bnep_get_session(dst
);
589 if (ss
&& ss
->state
== BT_CONNECTED
) {
594 s
= netdev_priv(dev
);
596 /* This is rx header therefore addresses are swapped.
597 * ie. eh.h_dest is our local address. */
598 memcpy(s
->eh
.h_dest
, &src
, ETH_ALEN
);
599 memcpy(s
->eh
.h_source
, &dst
, ETH_ALEN
);
600 memcpy(dev
->dev_addr
, s
->eh
.h_dest
, ETH_ALEN
);
605 s
->state
= BT_CONNECTED
;
606 s
->flags
= req
->flags
;
608 s
->msg
.msg_flags
= MSG_NOSIGNAL
;
610 #ifdef CONFIG_BT_BNEP_MC_FILTER
611 /* Set default mc filter to not filter out any mc addresses
612 * as defined in the BNEP specification (revision 0.95a)
613 * http://grouper.ieee.org/groups/802/15/Bluetooth/BNEP.pdf
618 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
619 /* Set default protocol filter */
620 bnep_set_default_proto_filter(s
);
623 SET_NETDEV_DEV(dev
, bnep_get_device(s
));
624 SET_NETDEV_DEVTYPE(dev
, &bnep_type
);
626 err
= register_netdev(dev
);
630 __bnep_link_session(s
);
632 __module_get(THIS_MODULE
);
633 s
->task
= kthread_run(bnep_session
, s
, "kbnepd %s", dev
->name
);
634 if (IS_ERR(s
->task
)) {
635 /* Session thread start failed, gotta cleanup. */
636 module_put(THIS_MODULE
);
637 unregister_netdev(dev
);
638 __bnep_unlink_session(s
);
639 err
= PTR_ERR(s
->task
);
643 up_write(&bnep_session_sem
);
644 strcpy(req
->device
, dev
->name
);
648 up_write(&bnep_session_sem
);
653 int bnep_del_connection(struct bnep_conndel_req
*req
)
656 struct bnep_session
*s
;
661 if (req
->flags
& ~valid_flags
)
664 down_read(&bnep_session_sem
);
666 s
= __bnep_get_session(req
->dst
);
668 atomic_inc(&s
->terminate
);
669 wake_up_process(s
->task
);
673 up_read(&bnep_session_sem
);
677 static void __bnep_copy_ci(struct bnep_conninfo
*ci
, struct bnep_session
*s
)
679 u32 valid_flags
= BIT(BNEP_SETUP_RESPONSE
);
681 memset(ci
, 0, sizeof(*ci
));
682 memcpy(ci
->dst
, s
->eh
.h_source
, ETH_ALEN
);
683 strcpy(ci
->device
, s
->dev
->name
);
684 ci
->flags
= s
->flags
& valid_flags
;
685 ci
->state
= s
->state
;
689 int bnep_get_connlist(struct bnep_connlist_req
*req
)
691 struct bnep_session
*s
;
694 down_read(&bnep_session_sem
);
696 list_for_each_entry(s
, &bnep_session_list
, list
) {
697 struct bnep_conninfo ci
;
699 __bnep_copy_ci(&ci
, s
);
701 if (copy_to_user(req
->ci
, &ci
, sizeof(ci
))) {
706 if (++n
>= req
->cnum
)
713 up_read(&bnep_session_sem
);
717 int bnep_get_conninfo(struct bnep_conninfo
*ci
)
719 struct bnep_session
*s
;
722 down_read(&bnep_session_sem
);
724 s
= __bnep_get_session(ci
->dst
);
726 __bnep_copy_ci(ci
, s
);
730 up_read(&bnep_session_sem
);
734 static int __init
bnep_init(void)
738 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
739 strcat(flt
, "protocol ");
742 #ifdef CONFIG_BT_BNEP_MC_FILTER
743 strcat(flt
, "multicast");
746 BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION
);
748 BT_INFO("BNEP filters: %s", flt
);
754 static void __exit
bnep_exit(void)
759 module_init(bnep_init
);
760 module_exit(bnep_exit
);
762 module_param(compress_src
, bool, 0644);
763 MODULE_PARM_DESC(compress_src
, "Compress sources headers");
765 module_param(compress_dst
, bool, 0644);
766 MODULE_PARM_DESC(compress_dst
, "Compress destination headers");
768 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
769 MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION
);
770 MODULE_VERSION(VERSION
);
771 MODULE_LICENSE("GPL");
772 MODULE_ALIAS("bt-proto-4");