netdev.c 5.8 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4
   BNEP implementation for Linux Bluetooth stack (BlueZ).
   Copyright (C) 2001-2002 Inventel Systemes
   Written 2001-2002 by
5
	Clément Moreau <clement.moreau@inventel.fr>
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17
	David Libault  <david.libault@inventel.fr>

   Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 2 as
   published by the Free Software Foundation;

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
18 19 20
   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
L
Linus Torvalds 已提交
21 22
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

23 24
   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
   SOFTWARE IS DISCLAIMED.
*/

#include <linux/etherdevice.h>

#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>
#include <net/bluetooth/l2cap.h>

#include "bnep.h"

#define BNEP_TX_QUEUE_LEN 20

static int bnep_net_open(struct net_device *dev)
{
	netif_start_queue(dev);
	return 0;
}

static int bnep_net_close(struct net_device *dev)
{
	netif_stop_queue(dev);
	return 0;
}

static void bnep_net_set_mc_list(struct net_device *dev)
{
#ifdef CONFIG_BT_BNEP_MC_FILTER
53
	struct bnep_session *s = netdev_priv(dev);
L
Linus Torvalds 已提交
54 55 56 57 58
	struct sock *sk = s->sock->sk;
	struct bnep_set_filter_req *r;
	struct sk_buff *skb;
	int size;

59
	BT_DBG("%s mc_count %d", dev->name, netdev_mc_count(dev));
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73

	size = sizeof(*r) + (BNEP_MAX_MULTICAST_FILTERS + 1) * ETH_ALEN * 2;
	skb  = alloc_skb(size, GFP_ATOMIC);
	if (!skb) {
		BT_ERR("%s Multicast list allocation failed", dev->name);
		return;
	}

	r = (void *) skb->data;
	__skb_put(skb, sizeof(*r));

	r->type = BNEP_CONTROL;
	r->ctrl = BNEP_FILTER_MULTI_ADDR_SET;

74
	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
L
Linus Torvalds 已提交
75 76 77 78 79 80 81
		u8 start[ETH_ALEN] = { 0x01 };

		/* Request all addresses */
		memcpy(__skb_put(skb, ETH_ALEN), start, ETH_ALEN);
		memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
		r->len = htons(ETH_ALEN * 2);
	} else {
82
		struct netdev_hw_addr *ha;
L
Linus Torvalds 已提交
83 84 85 86 87
		int i, len = skb->len;

		if (dev->flags & IFF_BROADCAST) {
			memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
			memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
88 89
		}

L
Linus Torvalds 已提交
90 91
		/* FIXME: We should group addresses here. */

92
		i = 0;
93
		netdev_for_each_mc_addr(ha, dev) {
94 95
			if (i == BNEP_MAX_MULTICAST_FILTERS)
				break;
96 97
			memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN);
			memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN);
98 99

			i++;
L
Linus Torvalds 已提交
100 101 102 103 104
		}
		r->len = htons(skb->len - len);
	}

	skb_queue_tail(&sk->sk_write_queue, skb);
E
Eric Dumazet 已提交
105
	wake_up_interruptible(sk_sleep(sk));
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#endif
}

static int bnep_net_set_mac_addr(struct net_device *dev, void *arg)
{
	BT_DBG("%s", dev->name);
	return 0;
}

static void bnep_net_timeout(struct net_device *dev)
{
	BT_DBG("net_timeout");
	netif_wake_queue(dev);
}

#ifdef CONFIG_BT_BNEP_MC_FILTER
122
static int bnep_net_mc_filter(struct sk_buff *skb, struct bnep_session *s)
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130 131 132 133
{
	struct ethhdr *eh = (void *) skb->data;

	if ((eh->h_dest[0] & 1) && !test_bit(bnep_mc_hash(eh->h_dest), (ulong *) &s->mc_filter))
		return 1;
	return 0;
}
#endif

#ifdef CONFIG_BT_BNEP_PROTO_FILTER
/* Determine ether protocol. Based on eth_type_trans. */
134
static u16 bnep_net_eth_proto(struct sk_buff *skb)
L
Linus Torvalds 已提交
135 136
{
	struct ethhdr *eh = (void *) skb->data;
137
	u16 proto = ntohs(eh->h_proto);
138

S
Simon Horman 已提交
139
	if (proto >= ETH_P_802_3_MIN)
140
		return proto;
141

142 143
	if (get_unaligned((__be16 *) skb->data) == htons(0xFFFF))
		return ETH_P_802_3;
144

145
	return ETH_P_802_2;
L
Linus Torvalds 已提交
146 147
}

148
static int bnep_net_proto_filter(struct sk_buff *skb, struct bnep_session *s)
L
Linus Torvalds 已提交
149 150 151 152
{
	u16 proto = bnep_net_eth_proto(skb);
	struct bnep_proto_filter *f = s->proto_filter;
	int i;
153

L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161 162 163
	for (i = 0; i < BNEP_MAX_PROTO_FILTERS && f[i].end; i++) {
		if (proto >= f[i].start && proto <= f[i].end)
			return 0;
	}

	BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb, proto);
	return 1;
}
#endif

164 165
static netdev_tx_t bnep_net_xmit(struct sk_buff *skb,
				 struct net_device *dev)
L
Linus Torvalds 已提交
166
{
167
	struct bnep_session *s = netdev_priv(dev);
L
Linus Torvalds 已提交
168 169 170 171 172 173 174
	struct sock *sk = s->sock->sk;

	BT_DBG("skb %p, dev %p", skb, dev);

#ifdef CONFIG_BT_BNEP_MC_FILTER
	if (bnep_net_mc_filter(skb, s)) {
		kfree_skb(skb);
175
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
176 177
	}
#endif
178

L
Linus Torvalds 已提交
179 180 181
#ifdef CONFIG_BT_BNEP_PROTO_FILTER
	if (bnep_net_proto_filter(skb, s)) {
		kfree_skb(skb);
182
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
183 184
	}
#endif
185

L
Linus Torvalds 已提交
186 187 188
	/*
	 * We cannot send L2CAP packets from here as we are potentially in a bh.
	 * So we have to queue them and wake up session thread which is sleeping
E
Eric Dumazet 已提交
189
	 * on the sk_sleep(sk).
L
Linus Torvalds 已提交
190 191 192
	 */
	dev->trans_start = jiffies;
	skb_queue_tail(&sk->sk_write_queue, skb);
E
Eric Dumazet 已提交
193
	wake_up_interruptible(sk_sleep(sk));
L
Linus Torvalds 已提交
194 195 196 197 198 199 200 201 202

	if (skb_queue_len(&sk->sk_write_queue) >= BNEP_TX_QUEUE_LEN) {
		BT_DBG("tx queue is full");

		/* Stop queuing.
		 * Session thread will do netif_wake_queue() */
		netif_stop_queue(dev);
	}

203
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
204 205
}

S
Stephen Hemminger 已提交
206 207 208 209 210
static const struct net_device_ops bnep_netdev_ops = {
	.ndo_open            = bnep_net_open,
	.ndo_stop            = bnep_net_close,
	.ndo_start_xmit	     = bnep_net_xmit,
	.ndo_validate_addr   = eth_validate_addr,
211
	.ndo_set_rx_mode     = bnep_net_set_mc_list,
S
Stephen Hemminger 已提交
212 213 214 215 216 217
	.ndo_set_mac_address = bnep_net_set_mac_addr,
	.ndo_tx_timeout      = bnep_net_timeout,
	.ndo_change_mtu	     = eth_change_mtu,

};

L
Linus Torvalds 已提交
218 219 220
void bnep_net_setup(struct net_device *dev)
{

221
	eth_broadcast_addr(dev->broadcast);
L
Linus Torvalds 已提交
222 223 224
	dev->addr_len = ETH_ALEN;

	ether_setup(dev);
225
	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
S
Stephen Hemminger 已提交
226
	dev->netdev_ops = &bnep_netdev_ops;
L
Linus Torvalds 已提交
227 228 229

	dev->watchdog_timeo  = HZ * 2;
}