YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BNEP implementation for Linux Bluetooth stack (BlueZ). |
| 3 | Copyright (C) 2001-2002 Inventel Systemes |
| 4 | Written 2001-2002 by |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 5 | Clément Moreau <clement.moreau@inventel.fr> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 23 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 24 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | SOFTWARE IS DISCLAIMED. |
| 26 | */ |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/etherdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
| 32 | #include <net/bluetooth/l2cap.h> |
| 33 | |
| 34 | #include "bnep.h" |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #define BNEP_TX_QUEUE_LEN 20 |
| 37 | |
| 38 | static int bnep_net_open(struct net_device *dev) |
| 39 | { |
| 40 | netif_start_queue(dev); |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int bnep_net_close(struct net_device *dev) |
| 45 | { |
| 46 | netif_stop_queue(dev); |
| 47 | return 0; |
| 48 | } |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static void bnep_net_set_mc_list(struct net_device *dev) |
| 51 | { |
| 52 | #ifdef CONFIG_BT_BNEP_MC_FILTER |
Wang Chen | 524ad0a | 2008-11-12 23:39:10 -0800 | [diff] [blame] | 53 | struct bnep_session *s = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | struct sock *sk = s->sock->sk; |
| 55 | struct bnep_set_filter_req *r; |
| 56 | struct sk_buff *skb; |
| 57 | int size; |
| 58 | |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 59 | BT_DBG("%s mc_count %d", dev->name, netdev_mc_count(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | size = sizeof(*r) + (BNEP_MAX_MULTICAST_FILTERS + 1) * ETH_ALEN * 2; |
| 62 | skb = alloc_skb(size, GFP_ATOMIC); |
| 63 | if (!skb) { |
| 64 | BT_ERR("%s Multicast list allocation failed", dev->name); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | r = (void *) skb->data; |
| 69 | __skb_put(skb, sizeof(*r)); |
| 70 | |
| 71 | r->type = BNEP_CONTROL; |
| 72 | r->ctrl = BNEP_FILTER_MULTI_ADDR_SET; |
| 73 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 74 | if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | u8 start[ETH_ALEN] = { 0x01 }; |
| 76 | |
| 77 | /* Request all addresses */ |
| 78 | memcpy(__skb_put(skb, ETH_ALEN), start, ETH_ALEN); |
| 79 | memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN); |
| 80 | r->len = htons(ETH_ALEN * 2); |
| 81 | } else { |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 82 | struct netdev_hw_addr *ha; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | int i, len = skb->len; |
| 84 | |
| 85 | if (dev->flags & IFF_BROADCAST) { |
| 86 | memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN); |
| 87 | memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 88 | } |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* FIXME: We should group addresses here. */ |
| 91 | |
Jiri Pirko | ff6e216 | 2010-03-01 05:09:14 +0000 | [diff] [blame] | 92 | i = 0; |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 93 | netdev_for_each_mc_addr(ha, dev) { |
Jiri Pirko | ff6e216 | 2010-03-01 05:09:14 +0000 | [diff] [blame] | 94 | if (i == BNEP_MAX_MULTICAST_FILTERS) |
| 95 | break; |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 96 | memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN); |
| 97 | memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN); |
Gustavo F. Padovan | 1a61a83 | 2010-06-18 14:24:00 +0000 | [diff] [blame] | 98 | |
| 99 | i++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | r->len = htons(skb->len - len); |
| 102 | } |
| 103 | |
| 104 | skb_queue_tail(&sk->sk_write_queue, skb); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 105 | wake_up_interruptible(sk_sleep(sk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | #endif |
| 107 | } |
| 108 | |
| 109 | static int bnep_net_set_mac_addr(struct net_device *dev, void *arg) |
| 110 | { |
| 111 | BT_DBG("%s", dev->name); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static void bnep_net_timeout(struct net_device *dev) |
| 116 | { |
| 117 | BT_DBG("net_timeout"); |
| 118 | netif_wake_queue(dev); |
| 119 | } |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | #ifdef CONFIG_BT_BNEP_MC_FILTER |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 122 | static int bnep_net_mc_filter(struct sk_buff *skb, struct bnep_session *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | struct ethhdr *eh = (void *) skb->data; |
| 125 | |
| 126 | if ((eh->h_dest[0] & 1) && !test_bit(bnep_mc_hash(eh->h_dest), (ulong *) &s->mc_filter)) |
| 127 | return 1; |
| 128 | return 0; |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | #ifdef CONFIG_BT_BNEP_PROTO_FILTER |
| 133 | /* Determine ether protocol. Based on eth_type_trans. */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 134 | static u16 bnep_net_eth_proto(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
| 136 | struct ethhdr *eh = (void *) skb->data; |
Al Viro | e41d216 | 2006-11-08 00:27:36 -0800 | [diff] [blame] | 137 | u16 proto = ntohs(eh->h_proto); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 138 | |
Simon Horman | e5c5d22 | 2013-03-28 13:38:25 +0900 | [diff] [blame] | 139 | if (proto >= ETH_P_802_3_MIN) |
Al Viro | e41d216 | 2006-11-08 00:27:36 -0800 | [diff] [blame] | 140 | return proto; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 141 | |
Al Viro | e41d216 | 2006-11-08 00:27:36 -0800 | [diff] [blame] | 142 | if (get_unaligned((__be16 *) skb->data) == htons(0xFFFF)) |
| 143 | return ETH_P_802_3; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 144 | |
Al Viro | e41d216 | 2006-11-08 00:27:36 -0800 | [diff] [blame] | 145 | return ETH_P_802_2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 148 | static int bnep_net_proto_filter(struct sk_buff *skb, struct bnep_session *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
| 150 | u16 proto = bnep_net_eth_proto(skb); |
| 151 | struct bnep_proto_filter *f = s->proto_filter; |
| 152 | int i; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | for (i = 0; i < BNEP_MAX_PROTO_FILTERS && f[i].end; i++) { |
| 155 | if (proto >= f[i].start && proto <= f[i].end) |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb, proto); |
| 160 | return 1; |
| 161 | } |
| 162 | #endif |
| 163 | |
Stephen Hemminger | 6fef4c0 | 2009-08-31 19:50:41 +0000 | [diff] [blame] | 164 | static netdev_tx_t bnep_net_xmit(struct sk_buff *skb, |
| 165 | struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
Wang Chen | 524ad0a | 2008-11-12 23:39:10 -0800 | [diff] [blame] | 167 | struct bnep_session *s = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | struct sock *sk = s->sock->sk; |
| 169 | |
| 170 | BT_DBG("skb %p, dev %p", skb, dev); |
| 171 | |
| 172 | #ifdef CONFIG_BT_BNEP_MC_FILTER |
| 173 | if (bnep_net_mc_filter(skb, s)) { |
| 174 | kfree_skb(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 175 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | #endif |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | #ifdef CONFIG_BT_BNEP_PROTO_FILTER |
| 180 | if (bnep_net_proto_filter(skb, s)) { |
| 181 | kfree_skb(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 182 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | #endif |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | /* |
| 187 | * We cannot send L2CAP packets from here as we are potentially in a bh. |
| 188 | * So we have to queue them and wake up session thread which is sleeping |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 189 | * on the sk_sleep(sk). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | */ |
| 191 | dev->trans_start = jiffies; |
| 192 | skb_queue_tail(&sk->sk_write_queue, skb); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 193 | wake_up_interruptible(sk_sleep(sk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
| 195 | if (skb_queue_len(&sk->sk_write_queue) >= BNEP_TX_QUEUE_LEN) { |
| 196 | BT_DBG("tx queue is full"); |
| 197 | |
| 198 | /* Stop queuing. |
| 199 | * Session thread will do netif_wake_queue() */ |
| 200 | netif_stop_queue(dev); |
| 201 | } |
| 202 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 203 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Stephen Hemminger | b4d7f0a | 2009-01-07 17:23:17 -0800 | [diff] [blame] | 206 | static const struct net_device_ops bnep_netdev_ops = { |
| 207 | .ndo_open = bnep_net_open, |
| 208 | .ndo_stop = bnep_net_close, |
| 209 | .ndo_start_xmit = bnep_net_xmit, |
| 210 | .ndo_validate_addr = eth_validate_addr, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 211 | .ndo_set_rx_mode = bnep_net_set_mc_list, |
Stephen Hemminger | b4d7f0a | 2009-01-07 17:23:17 -0800 | [diff] [blame] | 212 | .ndo_set_mac_address = bnep_net_set_mac_addr, |
| 213 | .ndo_tx_timeout = bnep_net_timeout, |
| 214 | .ndo_change_mtu = eth_change_mtu, |
| 215 | |
| 216 | }; |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | void bnep_net_setup(struct net_device *dev) |
| 219 | { |
| 220 | |
| 221 | memset(dev->broadcast, 0xff, ETH_ALEN); |
| 222 | dev->addr_len = ETH_ALEN; |
| 223 | |
| 224 | ether_setup(dev); |
Neil Horman | 550fd08 | 2011-07-26 06:05:38 +0000 | [diff] [blame] | 225 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
Stephen Hemminger | b4d7f0a | 2009-01-07 17:23:17 -0800 | [diff] [blame] | 226 | dev->netdev_ops = &bnep_netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | dev->watchdog_timeo = HZ * 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |