blob: ea5e58c252f00d5509e0fdbc1971d8aa2548f176 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
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 "routing.h"
26#include "send.h"
27#include "bat_debugfs.h"
28#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029#include "hash.h"
30#include "gateway_common.h"
31#include "gateway_client.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032#include "bat_sysfs.h"
33#include <linux/slab.h>
34#include <linux/ethtool.h>
35#include <linux/etherdevice.h>
36#include <linux/if_vlan.h>
37#include "unicast.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038
39
40static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
41static void bat_get_drvinfo(struct net_device *dev,
42 struct ethtool_drvinfo *info);
43static u32 bat_get_msglevel(struct net_device *dev);
44static void bat_set_msglevel(struct net_device *dev, u32 value);
45static u32 bat_get_link(struct net_device *dev);
46static u32 bat_get_rx_csum(struct net_device *dev);
47static int bat_set_rx_csum(struct net_device *dev, u32 data);
48
49static const struct ethtool_ops bat_ethtool_ops = {
50 .get_settings = bat_get_settings,
51 .get_drvinfo = bat_get_drvinfo,
52 .get_msglevel = bat_get_msglevel,
53 .set_msglevel = bat_set_msglevel,
54 .get_link = bat_get_link,
55 .get_rx_csum = bat_get_rx_csum,
56 .set_rx_csum = bat_set_rx_csum
57};
58
59int my_skb_head_push(struct sk_buff *skb, unsigned int len)
60{
61 int result;
62
63 /**
64 * TODO: We must check if we can release all references to non-payload
65 * data using skb_header_release in our skbs to allow skb_cow_header to
66 * work optimally. This means that those skbs are not allowed to read
67 * or write any data which is before the current position of skb->data
68 * after that call and thus allow other skbs with the same data buffer
69 * to write freely in that area.
70 */
71 result = skb_cow_head(skb, len);
72 if (result < 0)
73 return result;
74
75 skb_push(skb, len);
76 return 0;
77}
78
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000079static void softif_neigh_free_rcu(struct rcu_head *rcu)
80{
81 struct softif_neigh *softif_neigh;
82
83 softif_neigh = container_of(rcu, struct softif_neigh, rcu);
Marek Lindner7d2b5542011-02-10 14:33:50 +000084 kfree(softif_neigh);
85}
86
87static void softif_neigh_free_ref(struct softif_neigh *softif_neigh)
88{
89 if (atomic_dec_and_test(&softif_neigh->refcount))
90 call_rcu(&softif_neigh->rcu, softif_neigh_free_rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000091}
92
Simon Wunderlichba85fac2011-04-17 20:34:27 +020093static struct softif_neigh *softif_neigh_get_selected(struct bat_priv *bat_priv)
94{
95 struct softif_neigh *neigh;
96
97 rcu_read_lock();
98 neigh = rcu_dereference(bat_priv->softif_neigh);
99
100 if (neigh && !atomic_inc_not_zero(&neigh->refcount))
101 neigh = NULL;
102
103 rcu_read_unlock();
104 return neigh;
105}
106
107static void softif_neigh_select(struct bat_priv *bat_priv,
108 struct softif_neigh *new_neigh)
109{
110 struct softif_neigh *curr_neigh;
111
112 spin_lock_bh(&bat_priv->softif_neigh_lock);
113
114 if (new_neigh && !atomic_inc_not_zero(&new_neigh->refcount))
115 new_neigh = NULL;
116
117 curr_neigh = bat_priv->softif_neigh;
118 rcu_assign_pointer(bat_priv->softif_neigh, new_neigh);
119
120 if (curr_neigh)
121 softif_neigh_free_ref(curr_neigh);
122
123 spin_unlock_bh(&bat_priv->softif_neigh_lock);
124}
125
126static void softif_neigh_deselect(struct bat_priv *bat_priv)
127{
128 softif_neigh_select(bat_priv, NULL);
129}
130
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131void softif_neigh_purge(struct bat_priv *bat_priv)
132{
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200133 struct softif_neigh *softif_neigh, *curr_softif_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000134 struct hlist_node *node, *node_tmp;
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200135 char do_deselect = 0;
136
137 curr_softif_neigh = softif_neigh_get_selected(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000138
139 spin_lock_bh(&bat_priv->softif_neigh_lock);
140
141 hlist_for_each_entry_safe(softif_neigh, node, node_tmp,
142 &bat_priv->softif_neigh_list, list) {
143
144 if ((!time_after(jiffies, softif_neigh->last_seen +
145 msecs_to_jiffies(SOFTIF_NEIGH_TIMEOUT))) &&
146 (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE))
147 continue;
148
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200149 if (curr_softif_neigh == softif_neigh) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000150 bat_dbg(DBG_ROUTES, bat_priv,
151 "Current mesh exit point '%pM' vanished "
152 "(vid: %d).\n",
153 softif_neigh->addr, softif_neigh->vid);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200154 do_deselect = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155 }
156
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200157 hlist_del_rcu(&softif_neigh->list);
Marek Lindner7d2b5542011-02-10 14:33:50 +0000158 softif_neigh_free_ref(softif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000159 }
160
161 spin_unlock_bh(&bat_priv->softif_neigh_lock);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200162
163 /* soft_neigh_deselect() needs to acquire the softif_neigh_lock */
164 if (do_deselect)
165 softif_neigh_deselect(bat_priv);
166
167 if (curr_softif_neigh)
168 softif_neigh_free_ref(curr_softif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169}
170
171static struct softif_neigh *softif_neigh_get(struct bat_priv *bat_priv,
172 uint8_t *addr, short vid)
173{
174 struct softif_neigh *softif_neigh;
175 struct hlist_node *node;
176
177 rcu_read_lock();
178 hlist_for_each_entry_rcu(softif_neigh, node,
179 &bat_priv->softif_neigh_list, list) {
Marek Lindner39901e72011-02-18 12:28:08 +0000180 if (!compare_eth(softif_neigh->addr, addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181 continue;
182
183 if (softif_neigh->vid != vid)
184 continue;
185
Marek Lindner7d2b5542011-02-10 14:33:50 +0000186 if (!atomic_inc_not_zero(&softif_neigh->refcount))
187 continue;
188
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000189 softif_neigh->last_seen = jiffies;
Marek Lindner7d2b5542011-02-10 14:33:50 +0000190 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191 }
192
193 softif_neigh = kzalloc(sizeof(struct softif_neigh), GFP_ATOMIC);
194 if (!softif_neigh)
195 goto out;
196
197 memcpy(softif_neigh->addr, addr, ETH_ALEN);
198 softif_neigh->vid = vid;
199 softif_neigh->last_seen = jiffies;
Marek Lindner7d2b5542011-02-10 14:33:50 +0000200 /* initialize with 2 - caller decrements counter by one */
201 atomic_set(&softif_neigh->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202
203 INIT_HLIST_NODE(&softif_neigh->list);
204 spin_lock_bh(&bat_priv->softif_neigh_lock);
205 hlist_add_head_rcu(&softif_neigh->list, &bat_priv->softif_neigh_list);
206 spin_unlock_bh(&bat_priv->softif_neigh_lock);
207
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208out:
209 rcu_read_unlock();
210 return softif_neigh;
211}
212
213int softif_neigh_seq_print_text(struct seq_file *seq, void *offset)
214{
215 struct net_device *net_dev = (struct net_device *)seq->private;
216 struct bat_priv *bat_priv = netdev_priv(net_dev);
217 struct softif_neigh *softif_neigh;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200218 struct hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219 struct hlist_node *node;
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200220 struct softif_neigh *curr_softif_neigh;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200221 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222
Marek Lindner32ae9b22011-04-20 15:40:58 +0200223 primary_if = primary_if_get_selected(bat_priv);
224 if (!primary_if) {
225 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
226 "please specify interfaces to enable it\n",
227 net_dev->name);
228 goto out;
229 }
230
231 if (primary_if->if_status != IF_ACTIVE) {
232 ret = seq_printf(seq, "BATMAN mesh %s "
233 "disabled - primary interface not active\n",
234 net_dev->name);
235 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236 }
237
238 seq_printf(seq, "Softif neighbor list (%s)\n", net_dev->name);
239
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200240 curr_softif_neigh = softif_neigh_get_selected(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241 rcu_read_lock();
242 hlist_for_each_entry_rcu(softif_neigh, node,
243 &bat_priv->softif_neigh_list, list)
Linus Lüssing9e0b33c2011-02-18 12:20:13 +0000244 seq_printf(seq, "%s %pM (vid: %d)\n",
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200245 curr_softif_neigh == softif_neigh
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246 ? "=>" : " ", softif_neigh->addr,
247 softif_neigh->vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248 rcu_read_unlock();
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200249 if (curr_softif_neigh)
250 softif_neigh_free_ref(curr_softif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251
Marek Lindner32ae9b22011-04-20 15:40:58 +0200252out:
253 if (primary_if)
254 hardif_free_ref(primary_if);
255 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256}
257
258static void softif_batman_recv(struct sk_buff *skb, struct net_device *dev,
259 short vid)
260{
261 struct bat_priv *bat_priv = netdev_priv(dev);
262 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
263 struct batman_packet *batman_packet;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200264 struct softif_neigh *softif_neigh = NULL;
265 struct hard_iface *primary_if = NULL;
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200266 struct softif_neigh *curr_softif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267
268 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
269 batman_packet = (struct batman_packet *)
270 (skb->data + ETH_HLEN + VLAN_HLEN);
271 else
272 batman_packet = (struct batman_packet *)(skb->data + ETH_HLEN);
273
274 if (batman_packet->version != COMPAT_VERSION)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200275 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276
277 if (batman_packet->packet_type != BAT_PACKET)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200278 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279
280 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
Marek Lindner32ae9b22011-04-20 15:40:58 +0200281 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282
283 if (is_my_mac(batman_packet->orig))
Marek Lindner32ae9b22011-04-20 15:40:58 +0200284 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285
286 softif_neigh = softif_neigh_get(bat_priv, batman_packet->orig, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287 if (!softif_neigh)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200288 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200290 curr_softif_neigh = softif_neigh_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200291 if (!curr_softif_neigh)
292 goto out;
293
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200294 if (curr_softif_neigh == softif_neigh)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295 goto out;
296
Marek Lindner32ae9b22011-04-20 15:40:58 +0200297 primary_if = primary_if_get_selected(bat_priv);
298 if (!primary_if)
299 goto out;
300
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301 /* we got a neighbor but its mac is 'bigger' than ours */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200302 if (memcmp(primary_if->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000303 softif_neigh->addr, ETH_ALEN) < 0)
304 goto out;
305
306 /* switch to new 'smallest neighbor' */
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200307 if ((curr_softif_neigh) &&
308 (memcmp(softif_neigh->addr, curr_softif_neigh->addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309 ETH_ALEN) < 0)) {
310 bat_dbg(DBG_ROUTES, bat_priv,
311 "Changing mesh exit point from %pM (vid: %d) "
312 "to %pM (vid: %d).\n",
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200313 curr_softif_neigh->addr,
314 curr_softif_neigh->vid,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315 softif_neigh->addr, softif_neigh->vid);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200316
317 softif_neigh_select(bat_priv, softif_neigh);
318 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319 }
320
321 /* close own batX device and use softif_neigh as exit node */
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200322 if ((!curr_softif_neigh) &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323 (memcmp(softif_neigh->addr,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200324 primary_if->net_dev->dev_addr, ETH_ALEN) < 0)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325 bat_dbg(DBG_ROUTES, bat_priv,
326 "Setting mesh exit point to %pM (vid: %d).\n",
327 softif_neigh->addr, softif_neigh->vid);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200328
329 softif_neigh_select(bat_priv, softif_neigh);
330 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331 }
332
333out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334 kfree_skb(skb);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200335 if (softif_neigh)
336 softif_neigh_free_ref(softif_neigh);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200337 if (curr_softif_neigh)
338 softif_neigh_free_ref(curr_softif_neigh);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200339 if (primary_if)
340 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341 return;
342}
343
344static int interface_open(struct net_device *dev)
345{
346 netif_start_queue(dev);
347 return 0;
348}
349
350static int interface_release(struct net_device *dev)
351{
352 netif_stop_queue(dev);
353 return 0;
354}
355
356static struct net_device_stats *interface_stats(struct net_device *dev)
357{
358 struct bat_priv *bat_priv = netdev_priv(dev);
359 return &bat_priv->stats;
360}
361
362static int interface_set_mac_addr(struct net_device *dev, void *p)
363{
364 struct bat_priv *bat_priv = netdev_priv(dev);
365 struct sockaddr *addr = p;
366
367 if (!is_valid_ether_addr(addr->sa_data))
368 return -EADDRNOTAVAIL;
369
370 /* only modify hna-table if it has been initialised before */
371 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
372 hna_local_remove(bat_priv, dev->dev_addr,
373 "mac address changed");
374 hna_local_add(dev, addr->sa_data);
375 }
376
377 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
378 return 0;
379}
380
381static int interface_change_mtu(struct net_device *dev, int new_mtu)
382{
383 /* check ranges */
384 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
385 return -EINVAL;
386
387 dev->mtu = new_mtu;
388
389 return 0;
390}
391
392int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
393{
394 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
395 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200396 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397 struct bcast_packet *bcast_packet;
398 struct vlan_ethhdr *vhdr;
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200399 struct softif_neigh *curr_softif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400 int data_len = skb->len, ret;
401 short vid = -1;
402 bool do_bcast = false;
403
404 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
405 goto dropped;
406
407 soft_iface->trans_start = jiffies;
408
409 switch (ntohs(ethhdr->h_proto)) {
410 case ETH_P_8021Q:
411 vhdr = (struct vlan_ethhdr *)skb->data;
412 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
413
414 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
415 break;
416
417 /* fall through */
418 case ETH_P_BATMAN:
419 softif_batman_recv(skb, soft_iface, vid);
420 goto end;
421 }
422
423 /**
424 * if we have a another chosen mesh exit node in range
425 * it will transport the packets to the mesh
426 */
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200427 curr_softif_neigh = softif_neigh_get_selected(bat_priv);
428 if ((curr_softif_neigh) && (curr_softif_neigh->vid == vid))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429 goto dropped;
430
431 /* TODO: check this for locks */
432 hna_local_add(soft_iface, ethhdr->h_source);
433
434 if (is_multicast_ether_addr(ethhdr->h_dest)) {
435 ret = gw_is_target(bat_priv, skb);
436
437 if (ret < 0)
438 goto dropped;
439
440 if (ret == 0)
441 do_bcast = true;
442 }
443
444 /* ethernet packet should be broadcasted */
445 if (do_bcast) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200446 primary_if = primary_if_get_selected(bat_priv);
447 if (!primary_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448 goto dropped;
449
450 if (my_skb_head_push(skb, sizeof(struct bcast_packet)) < 0)
451 goto dropped;
452
453 bcast_packet = (struct bcast_packet *)skb->data;
454 bcast_packet->version = COMPAT_VERSION;
455 bcast_packet->ttl = TTL;
456
457 /* batman packet type: broadcast */
458 bcast_packet->packet_type = BAT_BCAST;
459
460 /* hw address of first interface is the orig mac because only
461 * this mac is known throughout the mesh */
462 memcpy(bcast_packet->orig,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200463 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464
465 /* set broadcast sequence number */
466 bcast_packet->seqno =
467 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
468
469 add_bcast_packet_to_list(bat_priv, skb);
470
471 /* a copy is stored in the bcast list, therefore removing
472 * the original skb. */
473 kfree_skb(skb);
474
475 /* unicast packet */
476 } else {
477 ret = unicast_send_skb(skb, bat_priv);
478 if (ret != 0)
479 goto dropped_freed;
480 }
481
482 bat_priv->stats.tx_packets++;
483 bat_priv->stats.tx_bytes += data_len;
484 goto end;
485
486dropped:
487 kfree_skb(skb);
488dropped_freed:
489 bat_priv->stats.tx_dropped++;
490end:
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200491 if (curr_softif_neigh)
492 softif_neigh_free_ref(curr_softif_neigh);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200493 if (primary_if)
494 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495 return NETDEV_TX_OK;
496}
497
498void interface_rx(struct net_device *soft_iface,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000499 struct sk_buff *skb, struct hard_iface *recv_if,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500 int hdr_size)
501{
502 struct bat_priv *bat_priv = netdev_priv(soft_iface);
503 struct unicast_packet *unicast_packet;
504 struct ethhdr *ethhdr;
505 struct vlan_ethhdr *vhdr;
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200506 struct softif_neigh *curr_softif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507 short vid = -1;
508 int ret;
509
510 /* check if enough space is available for pulling, and pull */
511 if (!pskb_may_pull(skb, hdr_size))
512 goto dropped;
513
514 skb_pull_rcsum(skb, hdr_size);
515 skb_reset_mac_header(skb);
516
517 ethhdr = (struct ethhdr *)skb_mac_header(skb);
518
519 switch (ntohs(ethhdr->h_proto)) {
520 case ETH_P_8021Q:
521 vhdr = (struct vlan_ethhdr *)skb->data;
522 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
523
524 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
525 break;
526
527 /* fall through */
528 case ETH_P_BATMAN:
529 goto dropped;
530 }
531
532 /**
533 * if we have a another chosen mesh exit node in range
534 * it will transport the packets to the non-mesh network
535 */
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200536 curr_softif_neigh = softif_neigh_get_selected(bat_priv);
537 if (curr_softif_neigh && (curr_softif_neigh->vid == vid)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000538 skb_push(skb, hdr_size);
539 unicast_packet = (struct unicast_packet *)skb->data;
540
541 if ((unicast_packet->packet_type != BAT_UNICAST) &&
542 (unicast_packet->packet_type != BAT_UNICAST_FRAG))
543 goto dropped;
544
545 skb_reset_mac_header(skb);
546
547 memcpy(unicast_packet->dest,
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200548 curr_softif_neigh->addr, ETH_ALEN);
Linus Lüssing7cefb142011-03-02 17:39:31 +0000549 ret = route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550 if (ret == NET_RX_DROP)
551 goto dropped;
552
553 goto out;
554 }
555
556 /* skb->dev & skb->pkt_type are set here */
557 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
558 goto dropped;
559 skb->protocol = eth_type_trans(skb, soft_iface);
560
561 /* should not be neccesary anymore as we use skb_pull_rcsum()
562 * TODO: please verify this and remove this TODO
563 * -- Dec 21st 2009, Simon Wunderlich */
564
565/* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
566
567 bat_priv->stats.rx_packets++;
568 bat_priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
569
570 soft_iface->last_rx = jiffies;
571
572 netif_rx(skb);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200573 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574
575dropped:
576 kfree_skb(skb);
577out:
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200578 if (curr_softif_neigh)
579 softif_neigh_free_ref(curr_softif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580 return;
581}
582
583#ifdef HAVE_NET_DEVICE_OPS
584static const struct net_device_ops bat_netdev_ops = {
585 .ndo_open = interface_open,
586 .ndo_stop = interface_release,
587 .ndo_get_stats = interface_stats,
588 .ndo_set_mac_address = interface_set_mac_addr,
589 .ndo_change_mtu = interface_change_mtu,
590 .ndo_start_xmit = interface_tx,
591 .ndo_validate_addr = eth_validate_addr
592};
593#endif
594
595static void interface_setup(struct net_device *dev)
596{
597 struct bat_priv *priv = netdev_priv(dev);
598 char dev_addr[ETH_ALEN];
599
600 ether_setup(dev);
601
602#ifdef HAVE_NET_DEVICE_OPS
603 dev->netdev_ops = &bat_netdev_ops;
604#else
605 dev->open = interface_open;
606 dev->stop = interface_release;
607 dev->get_stats = interface_stats;
608 dev->set_mac_address = interface_set_mac_addr;
609 dev->change_mtu = interface_change_mtu;
610 dev->hard_start_xmit = interface_tx;
611#endif
612 dev->destructor = free_netdev;
Andrew Lunnaf20b712011-04-17 20:39:07 +0200613 dev->tx_queue_len = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614
615 /**
616 * can't call min_mtu, because the needed variables
617 * have not been initialized yet
618 */
619 dev->mtu = ETH_DATA_LEN;
620 dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
621 * skbuff for our header */
622
623 /* generate random address */
624 random_ether_addr(dev_addr);
625 memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
626
627 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
628
629 memset(priv, 0, sizeof(struct bat_priv));
630}
631
632struct net_device *softif_create(char *name)
633{
634 struct net_device *soft_iface;
635 struct bat_priv *bat_priv;
636 int ret;
637
638 soft_iface = alloc_netdev(sizeof(struct bat_priv) , name,
639 interface_setup);
640
641 if (!soft_iface) {
642 pr_err("Unable to allocate the batman interface: %s\n", name);
643 goto out;
644 }
645
646 ret = register_netdev(soft_iface);
647 if (ret < 0) {
648 pr_err("Unable to register the batman interface '%s': %i\n",
649 name, ret);
650 goto free_soft_iface;
651 }
652
653 bat_priv = netdev_priv(soft_iface);
654
655 atomic_set(&bat_priv->aggregated_ogms, 1);
656 atomic_set(&bat_priv->bonding, 0);
657 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
658 atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
659 atomic_set(&bat_priv->gw_sel_class, 20);
660 atomic_set(&bat_priv->gw_bandwidth, 41);
661 atomic_set(&bat_priv->orig_interval, 1000);
662 atomic_set(&bat_priv->hop_penalty, 10);
663 atomic_set(&bat_priv->log_level, 0);
664 atomic_set(&bat_priv->fragmentation, 1);
665 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
666 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
667
668 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
669 atomic_set(&bat_priv->bcast_seqno, 1);
670 atomic_set(&bat_priv->hna_local_changed, 0);
671
672 bat_priv->primary_if = NULL;
673 bat_priv->num_ifaces = 0;
674 bat_priv->softif_neigh = NULL;
675
676 ret = sysfs_add_meshif(soft_iface);
677 if (ret < 0)
678 goto unreg_soft_iface;
679
680 ret = debugfs_add_meshif(soft_iface);
681 if (ret < 0)
682 goto unreg_sysfs;
683
684 ret = mesh_init(soft_iface);
685 if (ret < 0)
686 goto unreg_debugfs;
687
688 return soft_iface;
689
690unreg_debugfs:
691 debugfs_del_meshif(soft_iface);
692unreg_sysfs:
693 sysfs_del_meshif(soft_iface);
694unreg_soft_iface:
695 unregister_netdev(soft_iface);
696 return NULL;
697
698free_soft_iface:
699 free_netdev(soft_iface);
700out:
701 return NULL;
702}
703
704void softif_destroy(struct net_device *soft_iface)
705{
706 debugfs_del_meshif(soft_iface);
707 sysfs_del_meshif(soft_iface);
708 mesh_free(soft_iface);
709 unregister_netdevice(soft_iface);
710}
711
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000712int softif_is_valid(struct net_device *net_dev)
713{
714#ifdef HAVE_NET_DEVICE_OPS
715 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
716 return 1;
717#else
718 if (net_dev->hard_start_xmit == interface_tx)
719 return 1;
720#endif
721
722 return 0;
723}
724
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725/* ethtool */
726static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
727{
728 cmd->supported = 0;
729 cmd->advertising = 0;
730 cmd->speed = SPEED_10;
731 cmd->duplex = DUPLEX_FULL;
732 cmd->port = PORT_TP;
733 cmd->phy_address = 0;
734 cmd->transceiver = XCVR_INTERNAL;
735 cmd->autoneg = AUTONEG_DISABLE;
736 cmd->maxtxpkt = 0;
737 cmd->maxrxpkt = 0;
738
739 return 0;
740}
741
742static void bat_get_drvinfo(struct net_device *dev,
743 struct ethtool_drvinfo *info)
744{
745 strcpy(info->driver, "B.A.T.M.A.N. advanced");
746 strcpy(info->version, SOURCE_VERSION);
747 strcpy(info->fw_version, "N/A");
748 strcpy(info->bus_info, "batman");
749}
750
751static u32 bat_get_msglevel(struct net_device *dev)
752{
753 return -EOPNOTSUPP;
754}
755
756static void bat_set_msglevel(struct net_device *dev, u32 value)
757{
758}
759
760static u32 bat_get_link(struct net_device *dev)
761{
762 return 1;
763}
764
765static u32 bat_get_rx_csum(struct net_device *dev)
766{
767 return 0;
768}
769
770static int bat_set_rx_csum(struct net_device *dev, u32 data)
771{
772 return -EOPNOTSUPP;
773}