]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/batman-adv/soft-interface.c
c9b35d9f799121774e3abe64a6c6789c60d0d39c
[net-next-2.6.git] / drivers / staging / batman-adv / soft-interface.c
1 /*
2  * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  *
20  */
21
22 #include "main.h"
23 #include "soft-interface.h"
24 #include "hard-interface.h"
25 #include "send.h"
26 #include "translation-table.h"
27 #include "types.h"
28 #include "hash.h"
29 #include <linux/ethtool.h>
30 #include <linux/etherdevice.h>
31
32 static uint16_t bcast_seqno = 1; /* give own bcast messages seq numbers to avoid
33                                   * broadcast storms */
34 static int32_t skb_packets;
35 static int32_t skb_bad_packets;
36
37 unsigned char mainIfAddr[ETH_ALEN];
38 static unsigned char mainIfAddr_default[ETH_ALEN];
39 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
40 static void bat_get_drvinfo(struct net_device *dev,
41                             struct ethtool_drvinfo *info);
42 static u32 bat_get_msglevel(struct net_device *dev);
43 static void bat_set_msglevel(struct net_device *dev, u32 value);
44 static u32 bat_get_link(struct net_device *dev);
45 static u32 bat_get_rx_csum(struct net_device *dev);
46 static int bat_set_rx_csum(struct net_device *dev, u32 data);
47
48 static const struct ethtool_ops bat_ethtool_ops = {
49         .get_settings = bat_get_settings,
50         .get_drvinfo = bat_get_drvinfo,
51         .get_msglevel = bat_get_msglevel,
52         .set_msglevel = bat_set_msglevel,
53         .get_link = bat_get_link,
54         .get_rx_csum = bat_get_rx_csum,
55         .set_rx_csum = bat_set_rx_csum
56 };
57
58 void set_main_if_addr(uint8_t *addr)
59 {
60         memcpy(mainIfAddr, addr, ETH_ALEN);
61 }
62
63 int main_if_was_up(void)
64 {
65         return (memcmp(mainIfAddr, mainIfAddr_default, ETH_ALEN) != 0 ? 1 : 0);
66 }
67
68 int my_skb_push(struct sk_buff *skb, unsigned int len)
69 {
70         int result = 0;
71
72         skb_packets++;
73         if (skb_headroom(skb) < len) {
74                 skb_bad_packets++;
75                 result = pskb_expand_head(skb, len, 0, GFP_ATOMIC);
76
77                 if (result < 0)
78                         return result;
79         }
80
81         skb_push(skb, len);
82         return 0;
83 }
84
85 #ifdef HAVE_NET_DEVICE_OPS
86 static const struct net_device_ops bat_netdev_ops = {
87         .ndo_open = interface_open,
88         .ndo_stop = interface_release,
89         .ndo_get_stats = interface_stats,
90         .ndo_set_mac_address = interface_set_mac_addr,
91         .ndo_change_mtu = interface_change_mtu,
92         .ndo_start_xmit = interface_tx,
93         .ndo_validate_addr = eth_validate_addr
94 };
95 #endif
96
97 void interface_setup(struct net_device *dev)
98 {
99         struct bat_priv *priv = netdev_priv(dev);
100         char dev_addr[ETH_ALEN];
101
102         ether_setup(dev);
103
104 #ifdef HAVE_NET_DEVICE_OPS
105         dev->netdev_ops = &bat_netdev_ops;
106 #else
107         dev->open = interface_open;
108         dev->stop = interface_release;
109         dev->get_stats = interface_stats;
110         dev->set_mac_address = interface_set_mac_addr;
111         dev->change_mtu = interface_change_mtu;
112         dev->hard_start_xmit = interface_tx;
113 #endif
114         dev->destructor = free_netdev;
115
116         dev->mtu = hardif_min_mtu();
117         dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
118                                                 * skbuff for our header */
119
120         /* generate random address */
121         random_ether_addr(dev_addr);
122         memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
123
124         SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
125
126         memset(priv, 0, sizeof(struct bat_priv));
127 }
128
129 int interface_open(struct net_device *dev)
130 {
131         netif_start_queue(dev);
132         return 0;
133 }
134
135 int interface_release(struct net_device *dev)
136 {
137         netif_stop_queue(dev);
138         return 0;
139 }
140
141 struct net_device_stats *interface_stats(struct net_device *dev)
142 {
143         struct bat_priv *priv = netdev_priv(dev);
144         return &priv->stats;
145 }
146
147 int interface_set_mac_addr(struct net_device *dev, void *p)
148 {
149         struct sockaddr *addr = p;
150
151         if (!is_valid_ether_addr(addr->sa_data))
152                 return -EADDRNOTAVAIL;
153
154         hna_local_remove(dev->dev_addr, "mac address changed");
155         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
156         hna_local_add(dev->dev_addr);
157
158         return 0;
159 }
160
161 int interface_change_mtu(struct net_device *dev, int new_mtu)
162 {
163         /* check ranges */
164         if ((new_mtu < 68) || (new_mtu > hardif_min_mtu()))
165                 return -EINVAL;
166
167         dev->mtu = new_mtu;
168
169         return 0;
170 }
171
172 int interface_tx(struct sk_buff *skb, struct net_device *dev)
173 {
174         struct unicast_packet *unicast_packet;
175         struct bcast_packet *bcast_packet;
176         struct orig_node *orig_node;
177         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
178         struct bat_priv *priv = netdev_priv(dev);
179         struct batman_if *batman_if;
180         uint8_t dstaddr[6];
181         int data_len = skb->len;
182         unsigned long flags;
183
184         if (atomic_read(&module_state) != MODULE_ACTIVE)
185                 goto dropped;
186
187         dev->trans_start = jiffies;
188         /* TODO: check this for locks */
189         hna_local_add(ethhdr->h_source);
190
191         /* ethernet packet should be broadcasted */
192         if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
193
194                 if (my_skb_push(skb, sizeof(struct bcast_packet)) < 0)
195                         goto dropped;
196
197                 bcast_packet = (struct bcast_packet *)skb->data;
198                 bcast_packet->version = COMPAT_VERSION;
199
200                 /* batman packet type: broadcast */
201                 bcast_packet->packet_type = BAT_BCAST;
202
203                 /* hw address of first interface is the orig mac because only
204                  * this mac is known throughout the mesh */
205                 memcpy(bcast_packet->orig, mainIfAddr, ETH_ALEN);
206
207                 /* set broadcast sequence number */
208                 bcast_packet->seqno = htons(bcast_seqno);
209
210                 bcast_seqno++;
211
212                 /* broadcast packet */
213                 add_bcast_packet_to_list(skb);
214                 /* a copy is stored in the bcast list, therefore removing
215                  * the original skb. */
216                 kfree_skb(skb);
217
218         /* unicast packet */
219         } else {
220                 spin_lock_irqsave(&orig_hash_lock, flags);
221                 /* get routing information */
222                 orig_node = ((struct orig_node *)hash_find(orig_hash,
223                                                            ethhdr->h_dest));
224
225                 /* check for hna host */
226                 if (!orig_node)
227                         orig_node = transtable_search(ethhdr->h_dest);
228
229                 if ((orig_node) &&
230                     (orig_node->batman_if) &&
231                     (orig_node->router)) {
232                         if (my_skb_push(skb, sizeof(struct unicast_packet)) < 0)
233                                 goto unlock;
234
235                         unicast_packet = (struct unicast_packet *)skb->data;
236
237                         unicast_packet->version = COMPAT_VERSION;
238                         /* batman packet type: unicast */
239                         unicast_packet->packet_type = BAT_UNICAST;
240                         /* set unicast ttl */
241                         unicast_packet->ttl = TTL;
242                         /* copy the destination for faster routing */
243                         memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
244
245                         /* net_dev won't be available when not active */
246                         if (orig_node->batman_if->if_active != IF_ACTIVE)
247                                 goto unlock;
248
249                         /* don't lock while sending the packets ... we therefore
250                          * copy the required data before sending */
251
252                         batman_if = orig_node->batman_if;
253                         memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
254                         spin_unlock_irqrestore(&orig_hash_lock, flags);
255
256                         send_skb_packet(skb, batman_if, dstaddr);
257                 } else {
258                         goto unlock;
259                 }
260         }
261
262         priv->stats.tx_packets++;
263         priv->stats.tx_bytes += data_len;
264         goto end;
265
266 unlock:
267         spin_unlock_irqrestore(&orig_hash_lock, flags);
268 dropped:
269         priv->stats.tx_dropped++;
270 end:
271         return NETDEV_TX_OK;
272 }
273
274 void interface_rx(struct sk_buff *skb, int hdr_size)
275 {
276         struct net_device *dev = soft_device;
277         struct bat_priv *priv = netdev_priv(dev);
278
279         /* check if enough space is available for pulling, and pull */
280         if (!pskb_may_pull(skb, hdr_size)) {
281                 kfree_skb(skb);
282                 return;
283         }
284         skb_pull_rcsum(skb, hdr_size);
285 /*      skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
286
287         skb->dev = dev;
288         skb->protocol = eth_type_trans(skb, dev);
289
290         /* should not be neccesary anymore as we use skb_pull_rcsum()
291          * TODO: please verify this and remove this TODO
292          * -- Dec 21st 2009, Simon Wunderlich */
293
294 /*      skb->ip_summed = CHECKSUM_UNNECESSARY;*/
295
296         /* TODO: set skb->pkt_type to PACKET_BROADCAST, PACKET_MULTICAST,
297          * PACKET_OTHERHOST or PACKET_HOST */
298
299         priv->stats.rx_packets++;
300         priv->stats.rx_bytes += skb->len;
301
302         dev->last_rx = jiffies;
303
304         netif_rx(skb);
305 }
306
307 /* ethtool */
308 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
309 {
310         cmd->supported = 0;
311         cmd->advertising = 0;
312         cmd->speed = SPEED_10;
313         cmd->duplex = DUPLEX_FULL;
314         cmd->port = PORT_TP;
315         cmd->phy_address = 0;
316         cmd->transceiver = XCVR_INTERNAL;
317         cmd->autoneg = AUTONEG_DISABLE;
318         cmd->maxtxpkt = 0;
319         cmd->maxrxpkt = 0;
320
321         return 0;
322 }
323
324 static void bat_get_drvinfo(struct net_device *dev,
325                             struct ethtool_drvinfo *info)
326 {
327         strcpy(info->driver, "B.A.T.M.A.N. advanced");
328         strcpy(info->version, SOURCE_VERSION);
329         strcpy(info->fw_version, "N/A");
330         strcpy(info->bus_info, "batman");
331 }
332
333 static u32 bat_get_msglevel(struct net_device *dev)
334 {
335         return -EOPNOTSUPP;
336 }
337
338 static void bat_set_msglevel(struct net_device *dev, u32 value)
339 {
340 }
341
342 static u32 bat_get_link(struct net_device *dev)
343 {
344         return 1;
345 }
346
347 static u32 bat_get_rx_csum(struct net_device *dev)
348 {
349         return 0;
350 }
351
352 static int bat_set_rx_csum(struct net_device *dev, u32 data)
353 {
354         return -EOPNOTSUPP;
355 }