1 /* 2 BNEP implementation for Linux Bluetooth stack (BlueZ). 3 Copyright (C) 2001-2002 Inventel Systemes 4 Written 2001-2002 by 5 Clément Moreau <clement.moreau@inventel.fr> 6 David Libault <david.libault@inventel.fr> 7 8 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com> 9 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; 13 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. 22 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. 26 */ 27 28 #include <linux/module.h> 29 30 #include <linux/socket.h> 31 #include <linux/netdevice.h> 32 #include <linux/etherdevice.h> 33 #include <linux/skbuff.h> 34 #include <linux/wait.h> 35 36 #include <asm/unaligned.h> 37 38 #include <net/bluetooth/bluetooth.h> 39 #include <net/bluetooth/hci_core.h> 40 #include <net/bluetooth/l2cap.h> 41 42 #include "bnep.h" 43 44 #ifndef CONFIG_BT_BNEP_DEBUG 45 #undef BT_DBG 46 #define BT_DBG( A... ) 47 #endif 48 49 #define BNEP_TX_QUEUE_LEN 20 50 51 static int bnep_net_open(struct net_device *dev) 52 { 53 netif_start_queue(dev); 54 return 0; 55 } 56 57 static int bnep_net_close(struct net_device *dev) 58 { 59 netif_stop_queue(dev); 60 return 0; 61 } 62 63 static struct net_device_stats *bnep_net_get_stats(struct net_device *dev) 64 { 65 struct bnep_session *s = dev->priv; 66 return &s->stats; 67 } 68 69 static void bnep_net_set_mc_list(struct net_device *dev) 70 { 71 #ifdef CONFIG_BT_BNEP_MC_FILTER 72 struct bnep_session *s = dev->priv; 73 struct sock *sk = s->sock->sk; 74 struct bnep_set_filter_req *r; 75 struct sk_buff *skb; 76 int size; 77 78 BT_DBG("%s mc_count %d", dev->name, dev->mc_count); 79 80 size = sizeof(*r) + (BNEP_MAX_MULTICAST_FILTERS + 1) * ETH_ALEN * 2; 81 skb = alloc_skb(size, GFP_ATOMIC); 82 if (!skb) { 83 BT_ERR("%s Multicast list allocation failed", dev->name); 84 return; 85 } 86 87 r = (void *) skb->data; 88 __skb_put(skb, sizeof(*r)); 89 90 r->type = BNEP_CONTROL; 91 r->ctrl = BNEP_FILTER_MULTI_ADDR_SET; 92 93 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) { 94 u8 start[ETH_ALEN] = { 0x01 }; 95 96 /* Request all addresses */ 97 memcpy(__skb_put(skb, ETH_ALEN), start, ETH_ALEN); 98 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN); 99 r->len = htons(ETH_ALEN * 2); 100 } else { 101 struct dev_mc_list *dmi = dev->mc_list; 102 int i, len = skb->len; 103 104 if (dev->flags & IFF_BROADCAST) { 105 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN); 106 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN); 107 } 108 109 /* FIXME: We should group addresses here. */ 110 111 for (i = 0; i < dev->mc_count && i < BNEP_MAX_MULTICAST_FILTERS; i++) { 112 memcpy(__skb_put(skb, ETH_ALEN), dmi->dmi_addr, ETH_ALEN); 113 memcpy(__skb_put(skb, ETH_ALEN), dmi->dmi_addr, ETH_ALEN); 114 dmi = dmi->next; 115 } 116 r->len = htons(skb->len - len); 117 } 118 119 skb_queue_tail(&sk->sk_write_queue, skb); 120 wake_up_interruptible(sk->sk_sleep); 121 #endif 122 } 123 124 static int bnep_net_set_mac_addr(struct net_device *dev, void *arg) 125 { 126 BT_DBG("%s", dev->name); 127 return 0; 128 } 129 130 static void bnep_net_timeout(struct net_device *dev) 131 { 132 BT_DBG("net_timeout"); 133 netif_wake_queue(dev); 134 } 135 136 static int bnep_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 137 { 138 return -EINVAL; 139 } 140 141 #ifdef CONFIG_BT_BNEP_MC_FILTER 142 static inline int bnep_net_mc_filter(struct sk_buff *skb, struct bnep_session *s) 143 { 144 struct ethhdr *eh = (void *) skb->data; 145 146 if ((eh->h_dest[0] & 1) && !test_bit(bnep_mc_hash(eh->h_dest), (ulong *) &s->mc_filter)) 147 return 1; 148 return 0; 149 } 150 #endif 151 152 #ifdef CONFIG_BT_BNEP_PROTO_FILTER 153 /* Determine ether protocol. Based on eth_type_trans. */ 154 static inline u16 bnep_net_eth_proto(struct sk_buff *skb) 155 { 156 struct ethhdr *eh = (void *) skb->data; 157 u16 proto = ntohs(eh->h_proto); 158 159 if (proto >= 1536) 160 return proto; 161 162 if (get_unaligned((__be16 *) skb->data) == htons(0xFFFF)) 163 return ETH_P_802_3; 164 165 return ETH_P_802_2; 166 } 167 168 static inline int bnep_net_proto_filter(struct sk_buff *skb, struct bnep_session *s) 169 { 170 u16 proto = bnep_net_eth_proto(skb); 171 struct bnep_proto_filter *f = s->proto_filter; 172 int i; 173 174 for (i = 0; i < BNEP_MAX_PROTO_FILTERS && f[i].end; i++) { 175 if (proto >= f[i].start && proto <= f[i].end) 176 return 0; 177 } 178 179 BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb, proto); 180 return 1; 181 } 182 #endif 183 184 static int bnep_net_xmit(struct sk_buff *skb, struct net_device *dev) 185 { 186 struct bnep_session *s = dev->priv; 187 struct sock *sk = s->sock->sk; 188 189 BT_DBG("skb %p, dev %p", skb, dev); 190 191 #ifdef CONFIG_BT_BNEP_MC_FILTER 192 if (bnep_net_mc_filter(skb, s)) { 193 kfree_skb(skb); 194 return 0; 195 } 196 #endif 197 198 #ifdef CONFIG_BT_BNEP_PROTO_FILTER 199 if (bnep_net_proto_filter(skb, s)) { 200 kfree_skb(skb); 201 return 0; 202 } 203 #endif 204 205 /* 206 * We cannot send L2CAP packets from here as we are potentially in a bh. 207 * So we have to queue them and wake up session thread which is sleeping 208 * on the sk->sk_sleep. 209 */ 210 dev->trans_start = jiffies; 211 skb_queue_tail(&sk->sk_write_queue, skb); 212 wake_up_interruptible(sk->sk_sleep); 213 214 if (skb_queue_len(&sk->sk_write_queue) >= BNEP_TX_QUEUE_LEN) { 215 BT_DBG("tx queue is full"); 216 217 /* Stop queuing. 218 * Session thread will do netif_wake_queue() */ 219 netif_stop_queue(dev); 220 } 221 222 return 0; 223 } 224 225 void bnep_net_setup(struct net_device *dev) 226 { 227 228 memset(dev->broadcast, 0xff, ETH_ALEN); 229 dev->addr_len = ETH_ALEN; 230 231 ether_setup(dev); 232 233 dev->open = bnep_net_open; 234 dev->stop = bnep_net_close; 235 dev->hard_start_xmit = bnep_net_xmit; 236 dev->get_stats = bnep_net_get_stats; 237 dev->do_ioctl = bnep_net_ioctl; 238 dev->set_mac_address = bnep_net_set_mac_addr; 239 dev->set_multicast_list = bnep_net_set_mc_list; 240 241 dev->watchdog_timeo = HZ * 2; 242 dev->tx_timeout = bnep_net_timeout; 243 } 244