blob: 0faad5ce6dc480efe1928c1c60728e14b4093b92 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <linux/socket.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/wait.h>
36
37#include <asm/unaligned.h>
38
39#include <net/bluetooth/bluetooth.h>
40#include <net/bluetooth/hci_core.h>
41#include <net/bluetooth/l2cap.h>
42
43#include "bnep.h"
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define BNEP_TX_QUEUE_LEN 20
46
47static int bnep_net_open(struct net_device *dev)
48{
49 netif_start_queue(dev);
50 return 0;
51}
52
53static int bnep_net_close(struct net_device *dev)
54{
55 netif_stop_queue(dev);
56 return 0;
57}
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void bnep_net_set_mc_list(struct net_device *dev)
60{
61#ifdef CONFIG_BT_BNEP_MC_FILTER
Wang Chen524ad0a2008-11-12 23:39:10 -080062 struct bnep_session *s = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct sock *sk = s->sock->sk;
64 struct bnep_set_filter_req *r;
65 struct sk_buff *skb;
66 int size;
67
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000068 BT_DBG("%s mc_count %d", dev->name, netdev_mc_count(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 size = sizeof(*r) + (BNEP_MAX_MULTICAST_FILTERS + 1) * ETH_ALEN * 2;
71 skb = alloc_skb(size, GFP_ATOMIC);
72 if (!skb) {
73 BT_ERR("%s Multicast list allocation failed", dev->name);
74 return;
75 }
76
77 r = (void *) skb->data;
78 __skb_put(skb, sizeof(*r));
79
80 r->type = BNEP_CONTROL;
81 r->ctrl = BNEP_FILTER_MULTI_ADDR_SET;
82
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090083 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 u8 start[ETH_ALEN] = { 0x01 };
85
86 /* Request all addresses */
87 memcpy(__skb_put(skb, ETH_ALEN), start, ETH_ALEN);
88 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
89 r->len = htons(ETH_ALEN * 2);
90 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +000091 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int i, len = skb->len;
93
94 if (dev->flags & IFF_BROADCAST) {
95 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
96 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090097 }
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 /* FIXME: We should group addresses here. */
100
Jiri Pirkoff6e2162010-03-01 05:09:14 +0000101 i = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000102 netdev_for_each_mc_addr(ha, dev) {
Jiri Pirkoff6e2162010-03-01 05:09:14 +0000103 if (i == BNEP_MAX_MULTICAST_FILTERS)
104 break;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000105 memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN);
106 memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108 r->len = htons(skb->len - len);
109 }
110
111 skb_queue_tail(&sk->sk_write_queue, skb);
Eric Dumazetaa395142010-04-20 13:03:51 +0000112 wake_up_interruptible(sk_sleep(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#endif
114}
115
116static int bnep_net_set_mac_addr(struct net_device *dev, void *arg)
117{
118 BT_DBG("%s", dev->name);
119 return 0;
120}
121
122static void bnep_net_timeout(struct net_device *dev)
123{
124 BT_DBG("net_timeout");
125 netif_wake_queue(dev);
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#ifdef CONFIG_BT_BNEP_MC_FILTER
129static inline int bnep_net_mc_filter(struct sk_buff *skb, struct bnep_session *s)
130{
131 struct ethhdr *eh = (void *) skb->data;
132
133 if ((eh->h_dest[0] & 1) && !test_bit(bnep_mc_hash(eh->h_dest), (ulong *) &s->mc_filter))
134 return 1;
135 return 0;
136}
137#endif
138
139#ifdef CONFIG_BT_BNEP_PROTO_FILTER
140/* Determine ether protocol. Based on eth_type_trans. */
141static inline u16 bnep_net_eth_proto(struct sk_buff *skb)
142{
143 struct ethhdr *eh = (void *) skb->data;
Al Viroe41d2162006-11-08 00:27:36 -0800144 u16 proto = ntohs(eh->h_proto);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900145
Al Viroe41d2162006-11-08 00:27:36 -0800146 if (proto >= 1536)
147 return proto;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900148
Al Viroe41d2162006-11-08 00:27:36 -0800149 if (get_unaligned((__be16 *) skb->data) == htons(0xFFFF))
150 return ETH_P_802_3;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900151
Al Viroe41d2162006-11-08 00:27:36 -0800152 return ETH_P_802_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155static inline int bnep_net_proto_filter(struct sk_buff *skb, struct bnep_session *s)
156{
157 u16 proto = bnep_net_eth_proto(skb);
158 struct bnep_proto_filter *f = s->proto_filter;
159 int i;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 for (i = 0; i < BNEP_MAX_PROTO_FILTERS && f[i].end; i++) {
162 if (proto >= f[i].start && proto <= f[i].end)
163 return 0;
164 }
165
166 BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb, proto);
167 return 1;
168}
169#endif
170
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000171static netdev_tx_t bnep_net_xmit(struct sk_buff *skb,
172 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Wang Chen524ad0a2008-11-12 23:39:10 -0800174 struct bnep_session *s = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct sock *sk = s->sock->sk;
176
177 BT_DBG("skb %p, dev %p", skb, dev);
178
179#ifdef CONFIG_BT_BNEP_MC_FILTER
180 if (bnep_net_mc_filter(skb, s)) {
181 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000182 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184#endif
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#ifdef CONFIG_BT_BNEP_PROTO_FILTER
187 if (bnep_net_proto_filter(skb, s)) {
188 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000189 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191#endif
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /*
194 * We cannot send L2CAP packets from here as we are potentially in a bh.
195 * So we have to queue them and wake up session thread which is sleeping
Eric Dumazetaa395142010-04-20 13:03:51 +0000196 * on the sk_sleep(sk).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
198 dev->trans_start = jiffies;
199 skb_queue_tail(&sk->sk_write_queue, skb);
Eric Dumazetaa395142010-04-20 13:03:51 +0000200 wake_up_interruptible(sk_sleep(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (skb_queue_len(&sk->sk_write_queue) >= BNEP_TX_QUEUE_LEN) {
203 BT_DBG("tx queue is full");
204
205 /* Stop queuing.
206 * Session thread will do netif_wake_queue() */
207 netif_stop_queue(dev);
208 }
209
Patrick McHardy6ed10652009-06-23 06:03:08 +0000210 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Stephen Hemmingerb4d7f0a2009-01-07 17:23:17 -0800213static const struct net_device_ops bnep_netdev_ops = {
214 .ndo_open = bnep_net_open,
215 .ndo_stop = bnep_net_close,
216 .ndo_start_xmit = bnep_net_xmit,
217 .ndo_validate_addr = eth_validate_addr,
218 .ndo_set_multicast_list = bnep_net_set_mc_list,
219 .ndo_set_mac_address = bnep_net_set_mac_addr,
220 .ndo_tx_timeout = bnep_net_timeout,
221 .ndo_change_mtu = eth_change_mtu,
222
223};
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225void bnep_net_setup(struct net_device *dev)
226{
227
228 memset(dev->broadcast, 0xff, ETH_ALEN);
229 dev->addr_len = ETH_ALEN;
230
231 ether_setup(dev);
Stephen Hemmingerb4d7f0a2009-01-07 17:23:17 -0800232 dev->netdev_ops = &bnep_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 dev->watchdog_timeo = HZ * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}