2 BNEP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2001-2002 Inventel Systemes
5 Clément Moreau <clement.moreau@inventel.fr>
6 David Libault <david.libault@inventel.fr>
8 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
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;
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
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
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
24 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
25 SOFTWARE IS DISCLAIMED.
29 * $Id: netdev.c,v 1.8 2002/08/04 21:23:58 maxk Exp $
32 #include <linux/config.h>
33 #include <linux/module.h>
35 #include <linux/socket.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/wait.h>
41 #include <asm/unaligned.h>
43 #include <net/bluetooth/bluetooth.h>
44 #include <net/bluetooth/hci_core.h>
45 #include <net/bluetooth/l2cap.h>
49 #ifndef CONFIG_BT_BNEP_DEBUG
51 #define BT_DBG( A... )
54 #define BNEP_TX_QUEUE_LEN 20
56 static int bnep_net_open(struct net_device
*dev
)
58 netif_start_queue(dev
);
62 static int bnep_net_close(struct net_device
*dev
)
64 netif_stop_queue(dev
);
68 static struct net_device_stats
*bnep_net_get_stats(struct net_device
*dev
)
70 struct bnep_session
*s
= dev
->priv
;
74 static void bnep_net_set_mc_list(struct net_device
*dev
)
76 #ifdef CONFIG_BT_BNEP_MC_FILTER
77 struct bnep_session
*s
= dev
->priv
;
78 struct sock
*sk
= s
->sock
->sk
;
79 struct bnep_set_filter_req
*r
;
83 BT_DBG("%s mc_count %d", dev
->name
, dev
->mc_count
);
85 size
= sizeof(*r
) + (BNEP_MAX_MULTICAST_FILTERS
+ 1) * ETH_ALEN
* 2;
86 skb
= alloc_skb(size
, GFP_ATOMIC
);
88 BT_ERR("%s Multicast list allocation failed", dev
->name
);
92 r
= (void *) skb
->data
;
93 __skb_put(skb
, sizeof(*r
));
95 r
->type
= BNEP_CONTROL
;
96 r
->ctrl
= BNEP_FILTER_MULTI_ADDR_SET
;
98 if (dev
->flags
& (IFF_PROMISC
| IFF_ALLMULTI
)) {
99 u8 start
[ETH_ALEN
] = { 0x01 };
101 /* Request all addresses */
102 memcpy(__skb_put(skb
, ETH_ALEN
), start
, ETH_ALEN
);
103 memcpy(__skb_put(skb
, ETH_ALEN
), dev
->broadcast
, ETH_ALEN
);
104 r
->len
= htons(ETH_ALEN
* 2);
106 struct dev_mc_list
*dmi
= dev
->mc_list
;
107 int i
, len
= skb
->len
;
109 if (dev
->flags
& IFF_BROADCAST
) {
110 memcpy(__skb_put(skb
, ETH_ALEN
), dev
->broadcast
, ETH_ALEN
);
111 memcpy(__skb_put(skb
, ETH_ALEN
), dev
->broadcast
, ETH_ALEN
);
114 /* FIXME: We should group addresses here. */
116 for (i
= 0; i
< dev
->mc_count
&& i
< BNEP_MAX_MULTICAST_FILTERS
; i
++) {
117 memcpy(__skb_put(skb
, ETH_ALEN
), dmi
->dmi_addr
, ETH_ALEN
);
118 memcpy(__skb_put(skb
, ETH_ALEN
), dmi
->dmi_addr
, ETH_ALEN
);
121 r
->len
= htons(skb
->len
- len
);
124 skb_queue_tail(&sk
->sk_write_queue
, skb
);
125 wake_up_interruptible(sk
->sk_sleep
);
129 static int bnep_net_set_mac_addr(struct net_device
*dev
, void *arg
)
131 BT_DBG("%s", dev
->name
);
135 static void bnep_net_timeout(struct net_device
*dev
)
137 BT_DBG("net_timeout");
138 netif_wake_queue(dev
);
141 static int bnep_net_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
146 #ifdef CONFIG_BT_BNEP_MC_FILTER
147 static inline int bnep_net_mc_filter(struct sk_buff
*skb
, struct bnep_session
*s
)
149 struct ethhdr
*eh
= (void *) skb
->data
;
151 if ((eh
->h_dest
[0] & 1) && !test_bit(bnep_mc_hash(eh
->h_dest
), (ulong
*) &s
->mc_filter
))
157 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
158 /* Determine ether protocol. Based on eth_type_trans. */
159 static inline u16
bnep_net_eth_proto(struct sk_buff
*skb
)
161 struct ethhdr
*eh
= (void *) skb
->data
;
163 if (ntohs(eh
->h_proto
) >= 1536)
166 if (get_unaligned((u16
*) skb
->data
) == 0xFFFF)
167 return htons(ETH_P_802_3
);
169 return htons(ETH_P_802_2
);
172 static inline int bnep_net_proto_filter(struct sk_buff
*skb
, struct bnep_session
*s
)
174 u16 proto
= bnep_net_eth_proto(skb
);
175 struct bnep_proto_filter
*f
= s
->proto_filter
;
178 for (i
= 0; i
< BNEP_MAX_PROTO_FILTERS
&& f
[i
].end
; i
++) {
179 if (proto
>= f
[i
].start
&& proto
<= f
[i
].end
)
183 BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb
, proto
);
188 static int bnep_net_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
190 struct bnep_session
*s
= dev
->priv
;
191 struct sock
*sk
= s
->sock
->sk
;
193 BT_DBG("skb %p, dev %p", skb
, dev
);
195 #ifdef CONFIG_BT_BNEP_MC_FILTER
196 if (bnep_net_mc_filter(skb
, s
)) {
202 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
203 if (bnep_net_proto_filter(skb
, s
)) {
210 * We cannot send L2CAP packets from here as we are potentially in a bh.
211 * So we have to queue them and wake up session thread which is sleeping
212 * on the sk->sk_sleep.
214 dev
->trans_start
= jiffies
;
215 skb_queue_tail(&sk
->sk_write_queue
, skb
);
216 wake_up_interruptible(sk
->sk_sleep
);
218 if (skb_queue_len(&sk
->sk_write_queue
) >= BNEP_TX_QUEUE_LEN
) {
219 BT_DBG("tx queue is full");
222 * Session thread will do netif_wake_queue() */
223 netif_stop_queue(dev
);
229 void bnep_net_setup(struct net_device
*dev
)
232 memset(dev
->broadcast
, 0xff, ETH_ALEN
);
233 dev
->addr_len
= ETH_ALEN
;
237 dev
->open
= bnep_net_open
;
238 dev
->stop
= bnep_net_close
;
239 dev
->hard_start_xmit
= bnep_net_xmit
;
240 dev
->get_stats
= bnep_net_get_stats
;
241 dev
->do_ioctl
= bnep_net_ioctl
;
242 dev
->set_mac_address
= bnep_net_set_mac_addr
;
243 dev
->set_multicast_list
= bnep_net_set_mc_list
;
245 dev
->watchdog_timeo
= HZ
* 2;
246 dev
->tx_timeout
= bnep_net_timeout
;