net: convert multicast list to list_head
[cascardo/linux.git] / net / bluetooth / bnep / netdev.c
1 /*
2    BNEP implementation for Linux Bluetooth stack (BlueZ).
3    Copyright (C) 2001-2002 Inventel Systemes
4    Written 2001-2002 by
5         ClĂ©ment Moreau <clement.moreau@inventel.fr>
6         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
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.
22
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.
26 */
27
28 #include <linux/module.h>
29
30 #include <linux/socket.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/wait.h>
35
36 #include <asm/unaligned.h>
37
38 #include <net/bluetooth/bluetooth.h>
39 #include <net/bluetooth/hci_core.h>
40 #include <net/bluetooth/l2cap.h>
41
42 #include "bnep.h"
43
44 #define BNEP_TX_QUEUE_LEN 20
45
46 static int bnep_net_open(struct net_device *dev)
47 {
48         netif_start_queue(dev);
49         return 0;
50 }
51
52 static int bnep_net_close(struct net_device *dev)
53 {
54         netif_stop_queue(dev);
55         return 0;
56 }
57
58 static void bnep_net_set_mc_list(struct net_device *dev)
59 {
60 #ifdef CONFIG_BT_BNEP_MC_FILTER
61         struct bnep_session *s = netdev_priv(dev);
62         struct sock *sk = s->sock->sk;
63         struct bnep_set_filter_req *r;
64         struct sk_buff *skb;
65         int size;
66
67         BT_DBG("%s mc_count %d", dev->name, netdev_mc_count(dev));
68
69         size = sizeof(*r) + (BNEP_MAX_MULTICAST_FILTERS + 1) * ETH_ALEN * 2;
70         skb  = alloc_skb(size, GFP_ATOMIC);
71         if (!skb) {
72                 BT_ERR("%s Multicast list allocation failed", dev->name);
73                 return;
74         }
75
76         r = (void *) skb->data;
77         __skb_put(skb, sizeof(*r));
78
79         r->type = BNEP_CONTROL;
80         r->ctrl = BNEP_FILTER_MULTI_ADDR_SET;
81
82         if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
83                 u8 start[ETH_ALEN] = { 0x01 };
84
85                 /* Request all addresses */
86                 memcpy(__skb_put(skb, ETH_ALEN), start, ETH_ALEN);
87                 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
88                 r->len = htons(ETH_ALEN * 2);
89         } else {
90                 struct netdev_hw_addr *ha;
91                 int i, len = skb->len;
92
93                 if (dev->flags & IFF_BROADCAST) {
94                         memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
95                         memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
96                 }
97
98                 /* FIXME: We should group addresses here. */
99
100                 i = 0;
101                 netdev_for_each_mc_addr(ha, dev) {
102                         if (i == BNEP_MAX_MULTICAST_FILTERS)
103                                 break;
104                         memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN);
105                         memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN);
106                 }
107                 r->len = htons(skb->len - len);
108         }
109
110         skb_queue_tail(&sk->sk_write_queue, skb);
111         wake_up_interruptible(sk->sk_sleep);
112 #endif
113 }
114
115 static int bnep_net_set_mac_addr(struct net_device *dev, void *arg)
116 {
117         BT_DBG("%s", dev->name);
118         return 0;
119 }
120
121 static void bnep_net_timeout(struct net_device *dev)
122 {
123         BT_DBG("net_timeout");
124         netif_wake_queue(dev);
125 }
126
127 #ifdef CONFIG_BT_BNEP_MC_FILTER
128 static inline int bnep_net_mc_filter(struct sk_buff *skb, struct bnep_session *s)
129 {
130         struct ethhdr *eh = (void *) skb->data;
131
132         if ((eh->h_dest[0] & 1) && !test_bit(bnep_mc_hash(eh->h_dest), (ulong *) &s->mc_filter))
133                 return 1;
134         return 0;
135 }
136 #endif
137
138 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
139 /* Determine ether protocol. Based on eth_type_trans. */
140 static inline u16 bnep_net_eth_proto(struct sk_buff *skb)
141 {
142         struct ethhdr *eh = (void *) skb->data;
143         u16 proto = ntohs(eh->h_proto);
144
145         if (proto >= 1536)
146                 return proto;
147
148         if (get_unaligned((__be16 *) skb->data) == htons(0xFFFF))
149                 return ETH_P_802_3;
150
151         return ETH_P_802_2;
152 }
153
154 static inline int bnep_net_proto_filter(struct sk_buff *skb, struct bnep_session *s)
155 {
156         u16 proto = bnep_net_eth_proto(skb);
157         struct bnep_proto_filter *f = s->proto_filter;
158         int i;
159
160         for (i = 0; i < BNEP_MAX_PROTO_FILTERS && f[i].end; i++) {
161                 if (proto >= f[i].start && proto <= f[i].end)
162                         return 0;
163         }
164
165         BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb, proto);
166         return 1;
167 }
168 #endif
169
170 static netdev_tx_t bnep_net_xmit(struct sk_buff *skb,
171                                  struct net_device *dev)
172 {
173         struct bnep_session *s = netdev_priv(dev);
174         struct sock *sk = s->sock->sk;
175
176         BT_DBG("skb %p, dev %p", skb, dev);
177
178 #ifdef CONFIG_BT_BNEP_MC_FILTER
179         if (bnep_net_mc_filter(skb, s)) {
180                 kfree_skb(skb);
181                 return NETDEV_TX_OK;
182         }
183 #endif
184
185 #ifdef CONFIG_BT_BNEP_PROTO_FILTER
186         if (bnep_net_proto_filter(skb, s)) {
187                 kfree_skb(skb);
188                 return NETDEV_TX_OK;
189         }
190 #endif
191
192         /*
193          * We cannot send L2CAP packets from here as we are potentially in a bh.
194          * So we have to queue them and wake up session thread which is sleeping
195          * on the sk->sk_sleep.
196          */
197         dev->trans_start = jiffies;
198         skb_queue_tail(&sk->sk_write_queue, skb);
199         wake_up_interruptible(sk->sk_sleep);
200
201         if (skb_queue_len(&sk->sk_write_queue) >= BNEP_TX_QUEUE_LEN) {
202                 BT_DBG("tx queue is full");
203
204                 /* Stop queuing.
205                  * Session thread will do netif_wake_queue() */
206                 netif_stop_queue(dev);
207         }
208
209         return NETDEV_TX_OK;
210 }
211
212 static const struct net_device_ops bnep_netdev_ops = {
213         .ndo_open            = bnep_net_open,
214         .ndo_stop            = bnep_net_close,
215         .ndo_start_xmit      = bnep_net_xmit,
216         .ndo_validate_addr   = eth_validate_addr,
217         .ndo_set_multicast_list = bnep_net_set_mc_list,
218         .ndo_set_mac_address = bnep_net_set_mac_addr,
219         .ndo_tx_timeout      = bnep_net_timeout,
220         .ndo_change_mtu      = eth_change_mtu,
221
222 };
223
224 void bnep_net_setup(struct net_device *dev)
225 {
226
227         memset(dev->broadcast, 0xff, ETH_ALEN);
228         dev->addr_len = ETH_ALEN;
229
230         ether_setup(dev);
231         dev->netdev_ops = &bnep_netdev_ops;
232
233         dev->watchdog_timeo  = HZ * 2;
234 }