xref: /linux/net/bluetooth/bnep/netdev.c (revision b693b51e0829b96a5c43f45c3fba3d11f6f09d2f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3    BNEP implementation for Linux Bluetooth stack (BlueZ).
4    Copyright (C) 2001-2002 Inventel Systemes
5    Written 2001-2002 by
6 	Clément Moreau <clement.moreau@inventel.fr>
7 	David Libault  <david.libault@inventel.fr>
8 
9    Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
10 
11    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22    SOFTWARE IS DISCLAIMED.
23 */
24 
25 #include <linux/etherdevice.h>
26 
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
29 #include <net/bluetooth/l2cap.h>
30 
31 #include "bnep.h"
32 
33 #define BNEP_TX_QUEUE_LEN 20
34 
35 static int bnep_net_open(struct net_device *dev)
36 {
37 	netif_start_queue(dev);
38 	return 0;
39 }
40 
41 static int bnep_net_close(struct net_device *dev)
42 {
43 	netif_stop_queue(dev);
44 	return 0;
45 }
46 
47 static void bnep_net_set_mc_list(struct net_device *dev)
48 {
49 #ifdef CONFIG_BT_BNEP_MC_FILTER
50 	struct bnep_session *s = netdev_priv(dev);
51 	struct sock *sk = s->sock->sk;
52 	struct bnep_set_filter_req *r;
53 	struct sk_buff *skb;
54 	int size;
55 
56 	BT_DBG("%s mc_count %d", dev->name, netdev_mc_count(dev));
57 
58 	size = sizeof(*r) + (BNEP_MAX_MULTICAST_FILTERS + 1) * ETH_ALEN * 2;
59 	skb  = alloc_skb(size, GFP_ATOMIC);
60 	if (!skb) {
61 		BT_ERR("%s Multicast list allocation failed", dev->name);
62 		return;
63 	}
64 
65 	r = (void *) skb->data;
66 	__skb_put(skb, sizeof(*r));
67 
68 	r->type = BNEP_CONTROL;
69 	r->ctrl = BNEP_FILTER_MULTI_ADDR_SET;
70 
71 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
72 		u8 start[ETH_ALEN] = { 0x01 };
73 
74 		/* Request all addresses */
75 		__skb_put_data(skb, start, ETH_ALEN);
76 		__skb_put_data(skb, dev->broadcast, ETH_ALEN);
77 		r->len = htons(ETH_ALEN * 2);
78 	} else {
79 		struct netdev_hw_addr *ha;
80 		int i, len = skb->len;
81 
82 		if (dev->flags & IFF_BROADCAST) {
83 			__skb_put_data(skb, dev->broadcast, ETH_ALEN);
84 			__skb_put_data(skb, dev->broadcast, ETH_ALEN);
85 		}
86 
87 		/* FIXME: We should group addresses here. */
88 
89 		i = 0;
90 		netdev_for_each_mc_addr(ha, dev) {
91 			if (i == BNEP_MAX_MULTICAST_FILTERS)
92 				break;
93 			__skb_put_data(skb, ha->addr, ETH_ALEN);
94 			__skb_put_data(skb, ha->addr, ETH_ALEN);
95 
96 			i++;
97 		}
98 		r->len = htons(skb->len - len);
99 	}
100 
101 	skb_queue_tail(&sk->sk_write_queue, skb);
102 	wake_up_interruptible(sk_sleep(sk));
103 #endif
104 }
105 
106 static int bnep_net_set_mac_addr(struct net_device *dev, void *arg)
107 {
108 	BT_DBG("%s", dev->name);
109 	return 0;
110 }
111 
112 static void bnep_net_timeout(struct net_device *dev, unsigned int txqueue)
113 {
114 	BT_DBG("net_timeout");
115 	netif_wake_queue(dev);
116 }
117 
118 #ifdef CONFIG_BT_BNEP_MC_FILTER
119 static int bnep_net_mc_filter(struct sk_buff *skb, struct bnep_session *s)
120 {
121 	struct ethhdr *eh = (void *) skb->data;
122 
123 	if ((eh->h_dest[0] & 1) && !test_bit(bnep_mc_hash(eh->h_dest), (ulong *) &s->mc_filter))
124 		return 1;
125 	return 0;
126 }
127 #endif
128 
129 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
130 /* Determine ether protocol. Based on eth_type_trans. */
131 static u16 bnep_net_eth_proto(struct sk_buff *skb)
132 {
133 	struct ethhdr *eh = (void *) skb->data;
134 	u16 proto = ntohs(eh->h_proto);
135 
136 	if (proto >= ETH_P_802_3_MIN)
137 		return proto;
138 
139 	if (get_unaligned((__be16 *) skb->data) == htons(0xFFFF))
140 		return ETH_P_802_3;
141 
142 	return ETH_P_802_2;
143 }
144 
145 static int bnep_net_proto_filter(struct sk_buff *skb, struct bnep_session *s)
146 {
147 	u16 proto = bnep_net_eth_proto(skb);
148 	struct bnep_proto_filter *f = s->proto_filter;
149 	int i;
150 
151 	for (i = 0; i < BNEP_MAX_PROTO_FILTERS && f[i].end; i++) {
152 		if (proto >= f[i].start && proto <= f[i].end)
153 			return 0;
154 	}
155 
156 	BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb, proto);
157 	return 1;
158 }
159 #endif
160 
161 static netdev_tx_t bnep_net_xmit(struct sk_buff *skb,
162 				 struct net_device *dev)
163 {
164 	struct bnep_session *s = netdev_priv(dev);
165 	struct sock *sk = s->sock->sk;
166 
167 	BT_DBG("skb %p, dev %p", skb, dev);
168 
169 #ifdef CONFIG_BT_BNEP_MC_FILTER
170 	if (bnep_net_mc_filter(skb, s)) {
171 		kfree_skb(skb);
172 		return NETDEV_TX_OK;
173 	}
174 #endif
175 
176 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
177 	if (bnep_net_proto_filter(skb, s)) {
178 		kfree_skb(skb);
179 		return NETDEV_TX_OK;
180 	}
181 #endif
182 
183 	/*
184 	 * We cannot send L2CAP packets from here as we are potentially in a bh.
185 	 * So we have to queue them and wake up session thread which is sleeping
186 	 * on the sk_sleep(sk).
187 	 */
188 	netif_trans_update(dev);
189 	skb_queue_tail(&sk->sk_write_queue, skb);
190 	wake_up_interruptible(sk_sleep(sk));
191 
192 	if (skb_queue_len(&sk->sk_write_queue) >= BNEP_TX_QUEUE_LEN) {
193 		BT_DBG("tx queue is full");
194 
195 		/* Stop queuing.
196 		 * Session thread will do netif_wake_queue() */
197 		netif_stop_queue(dev);
198 	}
199 
200 	return NETDEV_TX_OK;
201 }
202 
203 static const struct net_device_ops bnep_netdev_ops = {
204 	.ndo_open            = bnep_net_open,
205 	.ndo_stop            = bnep_net_close,
206 	.ndo_start_xmit	     = bnep_net_xmit,
207 	.ndo_validate_addr   = eth_validate_addr,
208 	.ndo_set_rx_mode     = bnep_net_set_mc_list,
209 	.ndo_set_mac_address = bnep_net_set_mac_addr,
210 	.ndo_tx_timeout      = bnep_net_timeout,
211 
212 };
213 
214 void bnep_net_setup(struct net_device *dev)
215 {
216 
217 	eth_broadcast_addr(dev->broadcast);
218 	dev->addr_len = ETH_ALEN;
219 
220 	ether_setup(dev);
221 	dev->min_mtu = 0;
222 	dev->max_mtu = ETH_MAX_MTU;
223 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
224 	dev->netdev_ops = &bnep_netdev_ops;
225 
226 	dev->watchdog_timeo  = HZ * 2;
227 }
228