projects/linux·blob master · refs

233 lines · 5.7 KiB

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 <[email protected]>
7 David Libault <[email protected]>
8
9 Copyright (C) 2002 Maxim Krasnyansky <[email protected]>
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
35static int bnep_net_open(struct net_device *dev)
36{
37 netif_start_queue(dev);
38 return 0;
39}
40
41static int bnep_net_close(struct net_device *dev)
42{
43 netif_stop_queue(dev);
44 return 0;
45}
46
47static 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
106static int bnep_net_set_mac_addr(struct net_device *dev, void *arg)
107{
108 BT_DBG("%s", dev->name);
109 return 0;
110}
111
112static 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
119static 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. */
131static 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
145static 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
161static 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 if (!pskb_may_pull(skb, ETH_HLEN)) {
170 dev->stats.tx_dropped++;
171 kfree_skb(skb);
172 return NETDEV_TX_OK;
173 }
174
175#ifdef CONFIG_BT_BNEP_MC_FILTER
176 if (bnep_net_mc_filter(skb, s)) {
177 kfree_skb(skb);
178 return NETDEV_TX_OK;
179 }
180#endif
181
182#ifdef CONFIG_BT_BNEP_PROTO_FILTER
183 if (bnep_net_proto_filter(skb, s)) {
184 kfree_skb(skb);
185 return NETDEV_TX_OK;
186 }
187#endif
188
189 /*
190 * We cannot send L2CAP packets from here as we are potentially in a bh.
191 * So we have to queue them and wake up session thread which is sleeping
192 * on the sk_sleep(sk).
193 */
194 netif_trans_update(dev);
195 skb_queue_tail(&sk->sk_write_queue, skb);
196 wake_up_interruptible(sk_sleep(sk));
197
198 if (skb_queue_len(&sk->sk_write_queue) >= BNEP_TX_QUEUE_LEN) {
199 BT_DBG("tx queue is full");
200
201 /* Stop queuing.
202 * Session thread will do netif_wake_queue() */
203 netif_stop_queue(dev);
204 }
205
206 return NETDEV_TX_OK;
207}
208
209static const struct net_device_ops bnep_netdev_ops = {
210 .ndo_open = bnep_net_open,
211 .ndo_stop = bnep_net_close,
212 .ndo_start_xmit = bnep_net_xmit,
213 .ndo_validate_addr = eth_validate_addr,
214 .ndo_set_rx_mode = bnep_net_set_mc_list,
215 .ndo_set_mac_address = bnep_net_set_mac_addr,
216 .ndo_tx_timeout = bnep_net_timeout,
217
218};
219
220void bnep_net_setup(struct net_device *dev)
221{
222
223 eth_broadcast_addr(dev->broadcast);
224 dev->addr_len = ETH_ALEN;
225
226 ether_setup(dev);
227 dev->min_mtu = ETH_MIN_MTU;
228 dev->max_mtu = ETH_MAX_MTU;
229 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
230 dev->netdev_ops = &bnep_netdev_ops;
231
232 dev->watchdog_timeo = HZ * 2;
233}