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>
55 #ifndef CONFIG_BT_BNEP_DEBUG
62 static LIST_HEAD(bnep_session_list);
63 static DECLARE_RWSEM(bnep_session_sem);
65 static struct bnep_session *__bnep_get_session(u8 *dst)
67 struct bnep_session *s;
72 list_for_each(p, &bnep_session_list) {
73 s = list_entry(p, struct bnep_session, list);
74 if (!compare_ether_addr(dst, s->eh.h_source))
80 static void __bnep_link_session(struct bnep_session *s)
82 /* It's safe to call __module_get() here because sessions are added
83 by the socket layer which has to hold the refference to this module.
85 __module_get(THIS_MODULE);
86 list_add(&s->list, &bnep_session_list);
89 static void __bnep_unlink_session(struct bnep_session *s)
92 module_put(THIS_MODULE);
95 static int bnep_send(struct bnep_session *s, void *data, size_t len)
97 struct socket *sock = s->sock;
98 struct kvec iv = { data, len };
100 return kernel_sendmsg(sock, &s->msg, &iv, 1, len);
103 static int bnep_send_rsp(struct bnep_session *s, u8 ctrl, u16 resp)
105 struct bnep_control_rsp rsp;
106 rsp.type = BNEP_CONTROL;
108 rsp.resp = htons(resp);
109 return bnep_send(s, &rsp, sizeof(rsp));
112 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
113 static inline void bnep_set_default_proto_filter(struct bnep_session *s)
116 s->proto_filter[0].start = ETH_P_IP;
117 s->proto_filter[0].end = ETH_P_ARP;
118 /* (RARP, AppleTalk) */
119 s->proto_filter[1].start = ETH_P_RARP;
120 s->proto_filter[1].end = ETH_P_AARP;
122 s->proto_filter[2].start = ETH_P_IPX;
123 s->proto_filter[2].end = ETH_P_IPV6;
127 static int bnep_ctrl_set_netfilter(struct bnep_session *s, __be16 *data, int len)
134 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);
185 BT_DBG("filter len %d", n);
187 #ifdef CONFIG_BT_BNEP_MC_FILTER
193 /* Always send broadcast */
194 set_bit(bnep_mc_hash(s->dev->broadcast), (ulong *) &s->mc_filter);
196 /* Add address ranges to the multicast hash */
200 memcpy(a1, data, ETH_ALEN); data += ETH_ALEN;
201 a2 = data; data += ETH_ALEN;
203 BT_DBG("mc filter %s -> %s",
204 batostr((void *) a1), batostr((void *) a2));
206 #define INCA(a) { int i = 5; while (i >=0 && ++a[i--] == 0); }
208 /* Iterate from a1 to a2 */
209 set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter);
210 while (memcmp(a1, a2, 6) < 0 && s->mc_filter != ~0LL) {
212 set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter);
217 BT_DBG("mc filter hash 0x%llx", s->mc_filter);
219 bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_SUCCESS);
221 bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_FILTER_UNSUPPORTED_REQ);
226 static int bnep_rx_control(struct bnep_session *s, void *data, int len)
228 u8 cmd = *(u8 *)data;
234 case BNEP_CMD_NOT_UNDERSTOOD:
235 case BNEP_SETUP_CONN_REQ:
236 case BNEP_SETUP_CONN_RSP:
237 case BNEP_FILTER_NET_TYPE_RSP:
238 case BNEP_FILTER_MULTI_ADDR_RSP:
239 /* Ignore these for now */
242 case BNEP_FILTER_NET_TYPE_SET:
243 err = bnep_ctrl_set_netfilter(s, data, len);
246 case BNEP_FILTER_MULTI_ADDR_SET:
247 err = bnep_ctrl_set_mcfilter(s, data, len);
252 pkt[0] = BNEP_CONTROL;
253 pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
255 bnep_send(s, pkt, sizeof(pkt));
263 static int bnep_rx_extension(struct bnep_session *s, struct sk_buff *skb)
265 struct bnep_ext_hdr *h;
269 h = (void *) skb->data;
270 if (!skb_pull(skb, sizeof(*h))) {
275 BT_DBG("type 0x%x len %d", h->type, h->len);
277 switch (h->type & BNEP_TYPE_MASK) {
278 case BNEP_EXT_CONTROL:
279 bnep_rx_control(s, skb->data, skb->len);
283 /* Unknown extension, skip it. */
287 if (!skb_pull(skb, h->len)) {
291 } while (!err && (h->type & BNEP_EXT_HEADER));
296 static u8 __bnep_rx_hlen[] = {
297 ETH_HLEN, /* BNEP_GENERAL */
298 0, /* BNEP_CONTROL */
299 2, /* BNEP_COMPRESSED */
300 ETH_ALEN + 2, /* BNEP_COMPRESSED_SRC_ONLY */
301 ETH_ALEN + 2 /* BNEP_COMPRESSED_DST_ONLY */
303 #define BNEP_RX_TYPES (sizeof(__bnep_rx_hlen) - 1)
305 static inline int bnep_rx_frame(struct bnep_session *s, struct sk_buff *skb)
307 struct net_device *dev = s->dev;
308 struct sk_buff *nskb;
311 dev->last_rx = jiffies;
312 s->stats.rx_bytes += skb->len;
314 type = *(u8 *) skb->data; skb_pull(skb, 1);
316 if ((type & BNEP_TYPE_MASK) > BNEP_RX_TYPES)
319 if ((type & BNEP_TYPE_MASK) == BNEP_CONTROL) {
320 bnep_rx_control(s, skb->data, skb->len);
325 skb_reset_mac_header(skb);
327 /* Verify and pull out header */
328 if (!skb_pull(skb, __bnep_rx_hlen[type & BNEP_TYPE_MASK]))
331 s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2));
333 if (type & BNEP_EXT_HEADER) {
334 if (bnep_rx_extension(s, skb) < 0)
338 /* Strip 802.1p header */
339 if (ntohs(s->eh.h_proto) == 0x8100) {
340 if (!skb_pull(skb, 4))
342 s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2));
345 /* We have to alloc new skb and copy data here :(. Because original skb
346 * may not be modified and because of the alignment requirements. */
347 nskb = alloc_skb(2 + ETH_HLEN + skb->len, GFP_KERNEL);
349 s->stats.rx_dropped++;
353 skb_reserve(nskb, 2);
355 /* Decompress header and construct ether frame */
356 switch (type & BNEP_TYPE_MASK) {
357 case BNEP_COMPRESSED:
358 memcpy(__skb_put(nskb, ETH_HLEN), &s->eh, ETH_HLEN);
361 case BNEP_COMPRESSED_SRC_ONLY:
362 memcpy(__skb_put(nskb, ETH_ALEN), s->eh.h_dest, ETH_ALEN);
363 memcpy(__skb_put(nskb, ETH_ALEN), skb_mac_header(skb), ETH_ALEN);
364 put_unaligned(s->eh.h_proto, (__be16 *) __skb_put(nskb, 2));
367 case BNEP_COMPRESSED_DST_ONLY:
368 memcpy(__skb_put(nskb, ETH_ALEN), skb_mac_header(skb),
370 memcpy(__skb_put(nskb, ETH_ALEN + 2), s->eh.h_source,
375 memcpy(__skb_put(nskb, ETH_ALEN * 2), skb_mac_header(skb),
377 put_unaligned(s->eh.h_proto, (__be16 *) __skb_put(nskb, 2));
381 skb_copy_from_linear_data(skb, __skb_put(nskb, skb->len), skb->len);
384 s->stats.rx_packets++;
385 nskb->ip_summed = CHECKSUM_NONE;
386 nskb->protocol = eth_type_trans(nskb, dev);
391 s->stats.rx_errors++;
396 static u8 __bnep_tx_types[] = {
398 BNEP_COMPRESSED_SRC_ONLY,
399 BNEP_COMPRESSED_DST_ONLY,
403 static inline int bnep_tx_frame(struct bnep_session *s, struct sk_buff *skb)
405 struct ethhdr *eh = (void *) skb->data;
406 struct socket *sock = s->sock;
411 BT_DBG("skb %p dev %p type %d", skb, skb->dev, skb->pkt_type);
414 /* Control frame sent by us */
418 iv[il++] = (struct kvec) { &type, 1 };
421 if (!compare_ether_addr(eh->h_dest, s->eh.h_source))
424 if (!compare_ether_addr(eh->h_source, s->eh.h_dest))
428 skb_pull(skb, ETH_ALEN * 2);
430 type = __bnep_tx_types[type];
432 case BNEP_COMPRESSED_SRC_ONLY:
433 iv[il++] = (struct kvec) { eh->h_source, ETH_ALEN };
437 case BNEP_COMPRESSED_DST_ONLY:
438 iv[il++] = (struct kvec) { eh->h_dest, ETH_ALEN };
444 iv[il++] = (struct kvec) { skb->data, skb->len };
447 /* FIXME: linearize skb */
449 len = kernel_sendmsg(sock, &s->msg, iv, il, len);
454 s->stats.tx_bytes += len;
455 s->stats.tx_packets++;
462 static int bnep_session(void *arg)
464 struct bnep_session *s = arg;
465 struct net_device *dev = s->dev;
466 struct sock *sk = s->sock->sk;
472 daemonize("kbnepd %s", dev->name);
473 set_user_nice(current, -15);
475 init_waitqueue_entry(&wait, current);
476 add_wait_queue(sk->sk_sleep, &wait);
477 while (!atomic_read(&s->killed)) {
478 set_current_state(TASK_INTERRUPTIBLE);
481 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
483 bnep_rx_frame(s, skb);
486 if (sk->sk_state != BT_CONNECTED)
490 while ((skb = skb_dequeue(&sk->sk_write_queue)))
491 if (bnep_tx_frame(s, skb))
493 netif_wake_queue(dev);
497 set_current_state(TASK_RUNNING);
498 remove_wait_queue(sk->sk_sleep, &wait);
500 /* Cleanup session */
501 down_write(&bnep_session_sem);
503 /* Delete network device */
504 unregister_netdev(dev);
506 /* Wakeup user-space polling for socket errors */
507 s->sock->sk->sk_err = EUNATCH;
509 wake_up_interruptible(s->sock->sk->sk_sleep);
511 /* Release the socket */
514 __bnep_unlink_session(s);
516 up_write(&bnep_session_sem);
521 static struct device *bnep_get_device(struct bnep_session *session)
523 bdaddr_t *src = &bt_sk(session->sock->sk)->src;
524 bdaddr_t *dst = &bt_sk(session->sock->sk)->dst;
525 struct hci_dev *hdev;
526 struct hci_conn *conn;
528 hdev = hci_get_route(dst, src);
532 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
536 return conn ? &conn->dev : NULL;
539 int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock)
541 struct net_device *dev;
542 struct bnep_session *s, *ss;
543 u8 dst[ETH_ALEN], src[ETH_ALEN];
548 baswap((void *) dst, &bt_sk(sock->sk)->dst);
549 baswap((void *) src, &bt_sk(sock->sk)->src);
551 /* session struct allocated as private part of net_device */
552 dev = alloc_netdev(sizeof(struct bnep_session),
553 (*req->device) ? req->device : "bnep%d",
558 down_write(&bnep_session_sem);
560 ss = __bnep_get_session(dst);
561 if (ss && ss->state == BT_CONNECTED) {
568 /* This is rx header therefore addresses are swapped.
569 * ie eh.h_dest is our local address. */
570 memcpy(s->eh.h_dest, &src, ETH_ALEN);
571 memcpy(s->eh.h_source, &dst, ETH_ALEN);
572 memcpy(dev->dev_addr, s->eh.h_dest, ETH_ALEN);
577 s->state = BT_CONNECTED;
579 s->msg.msg_flags = MSG_NOSIGNAL;
581 #ifdef CONFIG_BT_BNEP_MC_FILTER
582 /* Set default mc filter */
583 set_bit(bnep_mc_hash(dev->broadcast), (ulong *) &s->mc_filter);
586 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
587 /* Set default protocol filter */
588 bnep_set_default_proto_filter(s);
591 SET_NETDEV_DEV(dev, bnep_get_device(s));
593 err = register_netdev(dev);
598 __bnep_link_session(s);
600 err = kernel_thread(bnep_session, s, CLONE_KERNEL);
602 /* Session thread start failed, gotta cleanup. */
603 unregister_netdev(dev);
604 __bnep_unlink_session(s);
608 up_write(&bnep_session_sem);
609 strcpy(req->device, dev->name);
613 up_write(&bnep_session_sem);
618 int bnep_del_connection(struct bnep_conndel_req *req)
620 struct bnep_session *s;
625 down_read(&bnep_session_sem);
627 s = __bnep_get_session(req->dst);
629 /* Wakeup user-space which is polling for socket errors.
630 * This is temporary hack untill we have shutdown in L2CAP */
631 s->sock->sk->sk_err = EUNATCH;
633 /* Kill session thread */
634 atomic_inc(&s->killed);
635 wake_up_interruptible(s->sock->sk->sk_sleep);
639 up_read(&bnep_session_sem);
643 static void __bnep_copy_ci(struct bnep_conninfo *ci, struct bnep_session *s)
645 memcpy(ci->dst, s->eh.h_source, ETH_ALEN);
646 strcpy(ci->device, s->dev->name);
647 ci->flags = s->flags;
648 ci->state = s->state;
652 int bnep_get_connlist(struct bnep_connlist_req *req)
657 down_read(&bnep_session_sem);
659 list_for_each(p, &bnep_session_list) {
660 struct bnep_session *s;
661 struct bnep_conninfo ci;
663 s = list_entry(p, struct bnep_session, list);
665 __bnep_copy_ci(&ci, s);
667 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
672 if (++n >= req->cnum)
679 up_read(&bnep_session_sem);
683 int bnep_get_conninfo(struct bnep_conninfo *ci)
685 struct bnep_session *s;
688 down_read(&bnep_session_sem);
690 s = __bnep_get_session(ci->dst);
692 __bnep_copy_ci(ci, s);
696 up_read(&bnep_session_sem);
700 static int __init bnep_init(void)
706 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
707 strcat(flt, "protocol ");
710 #ifdef CONFIG_BT_BNEP_MC_FILTER
711 strcat(flt, "multicast");
714 BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION);
716 BT_INFO("BNEP filters: %s", flt);
722 static void __exit bnep_exit(void)
727 module_init(bnep_init);
728 module_exit(bnep_exit);
730 MODULE_AUTHOR("David Libault <david.libault@inventel.fr>, Maxim Krasnyansky <maxk@qualcomm.com>");
731 MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION);
732 MODULE_VERSION(VERSION);
733 MODULE_LICENSE("GPL");
734 MODULE_ALIAS("bt-proto-4");