blob: 98f86f91d47c691b9e6604b73e2e8248f29921cf [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BNEP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2001-2002 Inventel Systemes
4 Written 2001-2002 by
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 Clément Moreau <clement.moreau@inventel.fr>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 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 Hideaki8e87d142007-02-09 23:24:33 +090018 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 Torvalds1da177e2005-04-16 15:20:36 -070021 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090023 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
24 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 SOFTWARE IS DISCLAIMED.
26*/
27
Gustavo Padovan8c520a52012-05-23 04:04:22 -030028#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <net/bluetooth/bluetooth.h>
32#include <net/bluetooth/hci_core.h>
33#include <net/bluetooth/l2cap.h>
34
35#include "bnep.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define BNEP_TX_QUEUE_LEN 20
38
39static int bnep_net_open(struct net_device *dev)
40{
41 netif_start_queue(dev);
42 return 0;
43}
44
45static int bnep_net_close(struct net_device *dev)
46{
47 netif_stop_queue(dev);
48 return 0;
49}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static void bnep_net_set_mc_list(struct net_device *dev)
52{
53#ifdef CONFIG_BT_BNEP_MC_FILTER
Wang Chen524ad0a2008-11-12 23:39:10 -080054 struct bnep_session *s = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 struct sock *sk = s->sock->sk;
56 struct bnep_set_filter_req *r;
57 struct sk_buff *skb;
58 int size;
59
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000060 BT_DBG("%s mc_count %d", dev->name, netdev_mc_count(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 size = sizeof(*r) + (BNEP_MAX_MULTICAST_FILTERS + 1) * ETH_ALEN * 2;
63 skb = alloc_skb(size, GFP_ATOMIC);
64 if (!skb) {
65 BT_ERR("%s Multicast list allocation failed", dev->name);
66 return;
67 }
68
69 r = (void *) skb->data;
70 __skb_put(skb, sizeof(*r));
71
72 r->type = BNEP_CONTROL;
73 r->ctrl = BNEP_FILTER_MULTI_ADDR_SET;
74
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090075 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 u8 start[ETH_ALEN] = { 0x01 };
77
78 /* Request all addresses */
79 memcpy(__skb_put(skb, ETH_ALEN), start, ETH_ALEN);
80 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
81 r->len = htons(ETH_ALEN * 2);
82 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +000083 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int i, len = skb->len;
85
86 if (dev->flags & IFF_BROADCAST) {
87 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
88 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090089 }
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* FIXME: We should group addresses here. */
92
Jiri Pirkoff6e2162010-03-01 05:09:14 +000093 i = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +000094 netdev_for_each_mc_addr(ha, dev) {
Jiri Pirkoff6e2162010-03-01 05:09:14 +000095 if (i == BNEP_MAX_MULTICAST_FILTERS)
96 break;
Jiri Pirko22bedad2010-04-01 21:22:57 +000097 memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN);
98 memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN);
Gustavo F. Padovan1a61a832010-06-18 14:24:00 +000099
100 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102 r->len = htons(skb->len - len);
103 }
104
105 skb_queue_tail(&sk->sk_write_queue, skb);
Eric Dumazetaa395142010-04-20 13:03:51 +0000106 wake_up_interruptible(sk_sleep(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#endif
108}
109
110static int bnep_net_set_mac_addr(struct net_device *dev, void *arg)
111{
112 BT_DBG("%s", dev->name);
113 return 0;
114}
115
116static void bnep_net_timeout(struct net_device *dev)
117{
118 BT_DBG("net_timeout");
119 netif_wake_queue(dev);
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#ifdef CONFIG_BT_BNEP_MC_FILTER
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300123static int bnep_net_mc_filter(struct sk_buff *skb, struct bnep_session *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct ethhdr *eh = (void *) skb->data;
126
127 if ((eh->h_dest[0] & 1) && !test_bit(bnep_mc_hash(eh->h_dest), (ulong *) &s->mc_filter))
128 return 1;
129 return 0;
130}
131#endif
132
133#ifdef CONFIG_BT_BNEP_PROTO_FILTER
134/* Determine ether protocol. Based on eth_type_trans. */
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300135static u16 bnep_net_eth_proto(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct ethhdr *eh = (void *) skb->data;
Al Viroe41d2162006-11-08 00:27:36 -0800138 u16 proto = ntohs(eh->h_proto);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900139
Al Viroe41d2162006-11-08 00:27:36 -0800140 if (proto >= 1536)
141 return proto;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900142
Al Viroe41d2162006-11-08 00:27:36 -0800143 if (get_unaligned((__be16 *) skb->data) == htons(0xFFFF))
144 return ETH_P_802_3;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900145
Al Viroe41d2162006-11-08 00:27:36 -0800146 return ETH_P_802_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300149static int bnep_net_proto_filter(struct sk_buff *skb, struct bnep_session *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 u16 proto = bnep_net_eth_proto(skb);
152 struct bnep_proto_filter *f = s->proto_filter;
153 int i;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 for (i = 0; i < BNEP_MAX_PROTO_FILTERS && f[i].end; i++) {
156 if (proto >= f[i].start && proto <= f[i].end)
157 return 0;
158 }
159
160 BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb, proto);
161 return 1;
162}
163#endif
164
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000165static netdev_tx_t bnep_net_xmit(struct sk_buff *skb,
166 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Wang Chen524ad0a2008-11-12 23:39:10 -0800168 struct bnep_session *s = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct sock *sk = s->sock->sk;
170
171 BT_DBG("skb %p, dev %p", skb, dev);
172
173#ifdef CONFIG_BT_BNEP_MC_FILTER
174 if (bnep_net_mc_filter(skb, s)) {
175 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000176 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178#endif
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#ifdef CONFIG_BT_BNEP_PROTO_FILTER
181 if (bnep_net_proto_filter(skb, s)) {
182 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000183 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185#endif
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /*
188 * We cannot send L2CAP packets from here as we are potentially in a bh.
189 * So we have to queue them and wake up session thread which is sleeping
Eric Dumazetaa395142010-04-20 13:03:51 +0000190 * on the sk_sleep(sk).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
192 dev->trans_start = jiffies;
193 skb_queue_tail(&sk->sk_write_queue, skb);
Eric Dumazetaa395142010-04-20 13:03:51 +0000194 wake_up_interruptible(sk_sleep(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (skb_queue_len(&sk->sk_write_queue) >= BNEP_TX_QUEUE_LEN) {
197 BT_DBG("tx queue is full");
198
199 /* Stop queuing.
200 * Session thread will do netif_wake_queue() */
201 netif_stop_queue(dev);
202 }
203
Patrick McHardy6ed10652009-06-23 06:03:08 +0000204 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Stephen Hemmingerb4d7f0a2009-01-07 17:23:17 -0800207static const struct net_device_ops bnep_netdev_ops = {
208 .ndo_open = bnep_net_open,
209 .ndo_stop = bnep_net_close,
210 .ndo_start_xmit = bnep_net_xmit,
211 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000212 .ndo_set_rx_mode = bnep_net_set_mc_list,
Stephen Hemmingerb4d7f0a2009-01-07 17:23:17 -0800213 .ndo_set_mac_address = bnep_net_set_mac_addr,
214 .ndo_tx_timeout = bnep_net_timeout,
215 .ndo_change_mtu = eth_change_mtu,
216
217};
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219void bnep_net_setup(struct net_device *dev)
220{
221
222 memset(dev->broadcast, 0xff, ETH_ALEN);
223 dev->addr_len = ETH_ALEN;
224
225 ether_setup(dev);
Neil Horman550fd082011-07-26 06:05:38 +0000226 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Stephen Hemmingerb4d7f0a2009-01-07 17:23:17 -0800227 dev->netdev_ops = &bnep_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 dev->watchdog_timeo = HZ * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}