macvlan: fix panic if lowerdev in a bond
[pandora-kernel.git] / net / batman-adv / soft-interface.c
1 /*
2  * Copyright (C) 2007-2011 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 "routing.h"
26 #include "send.h"
27 #include "bat_debugfs.h"
28 #include "translation-table.h"
29 #include "hash.h"
30 #include "gateway_common.h"
31 #include "gateway_client.h"
32 #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"
38
39
40 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
41 static void bat_get_drvinfo(struct net_device *dev,
42                             struct ethtool_drvinfo *info);
43 static u32 bat_get_msglevel(struct net_device *dev);
44 static void bat_set_msglevel(struct net_device *dev, u32 value);
45 static u32 bat_get_link(struct net_device *dev);
46
47 static const struct ethtool_ops bat_ethtool_ops = {
48         .get_settings = bat_get_settings,
49         .get_drvinfo = bat_get_drvinfo,
50         .get_msglevel = bat_get_msglevel,
51         .set_msglevel = bat_set_msglevel,
52         .get_link = bat_get_link,
53 };
54
55 int my_skb_head_push(struct sk_buff *skb, unsigned int len)
56 {
57         int result;
58
59         /**
60          * TODO: We must check if we can release all references to non-payload
61          * data using skb_header_release in our skbs to allow skb_cow_header to
62          * work optimally. This means that those skbs are not allowed to read
63          * or write any data which is before the current position of skb->data
64          * after that call and thus allow other skbs with the same data buffer
65          * to write freely in that area.
66          */
67         result = skb_cow_head(skb, len);
68         if (result < 0)
69                 return result;
70
71         skb_push(skb, len);
72         return 0;
73 }
74
75 static void softif_neigh_free_rcu(struct rcu_head *rcu)
76 {
77         struct softif_neigh *softif_neigh;
78
79         softif_neigh = container_of(rcu, struct softif_neigh, rcu);
80         kfree(softif_neigh);
81 }
82
83 static void softif_neigh_free_ref(struct softif_neigh *softif_neigh)
84 {
85         if (atomic_dec_and_test(&softif_neigh->refcount))
86                 call_rcu(&softif_neigh->rcu, softif_neigh_free_rcu);
87 }
88
89 static void softif_neigh_vid_free_rcu(struct rcu_head *rcu)
90 {
91         struct softif_neigh_vid *softif_neigh_vid;
92         struct softif_neigh *softif_neigh;
93         struct hlist_node *node, *node_tmp;
94         struct bat_priv *bat_priv;
95
96         softif_neigh_vid = container_of(rcu, struct softif_neigh_vid, rcu);
97         bat_priv = softif_neigh_vid->bat_priv;
98
99         spin_lock_bh(&bat_priv->softif_neigh_lock);
100         hlist_for_each_entry_safe(softif_neigh, node, node_tmp,
101                                   &softif_neigh_vid->softif_neigh_list, list) {
102                 hlist_del_rcu(&softif_neigh->list);
103                 softif_neigh_free_ref(softif_neigh);
104         }
105         spin_unlock_bh(&bat_priv->softif_neigh_lock);
106
107         kfree(softif_neigh_vid);
108 }
109
110 static void softif_neigh_vid_free_ref(struct softif_neigh_vid *softif_neigh_vid)
111 {
112         if (atomic_dec_and_test(&softif_neigh_vid->refcount))
113                 call_rcu(&softif_neigh_vid->rcu, softif_neigh_vid_free_rcu);
114 }
115
116 static struct softif_neigh_vid *softif_neigh_vid_get(struct bat_priv *bat_priv,
117                                                      short vid)
118 {
119         struct softif_neigh_vid *softif_neigh_vid;
120         struct hlist_node *node;
121
122         rcu_read_lock();
123         hlist_for_each_entry_rcu(softif_neigh_vid, node,
124                                  &bat_priv->softif_neigh_vids, list) {
125                 if (softif_neigh_vid->vid != vid)
126                         continue;
127
128                 if (!atomic_inc_not_zero(&softif_neigh_vid->refcount))
129                         continue;
130
131                 goto out;
132         }
133
134         softif_neigh_vid = kzalloc(sizeof(struct softif_neigh_vid),
135                                    GFP_ATOMIC);
136         if (!softif_neigh_vid)
137                 goto out;
138
139         softif_neigh_vid->vid = vid;
140         softif_neigh_vid->bat_priv = bat_priv;
141
142         /* initialize with 2 - caller decrements counter by one */
143         atomic_set(&softif_neigh_vid->refcount, 2);
144         INIT_HLIST_HEAD(&softif_neigh_vid->softif_neigh_list);
145         INIT_HLIST_NODE(&softif_neigh_vid->list);
146         spin_lock_bh(&bat_priv->softif_neigh_vid_lock);
147         hlist_add_head_rcu(&softif_neigh_vid->list,
148                            &bat_priv->softif_neigh_vids);
149         spin_unlock_bh(&bat_priv->softif_neigh_vid_lock);
150
151 out:
152         rcu_read_unlock();
153         return softif_neigh_vid;
154 }
155
156 static struct softif_neigh *softif_neigh_get(struct bat_priv *bat_priv,
157                                              uint8_t *addr, short vid)
158 {
159         struct softif_neigh_vid *softif_neigh_vid;
160         struct softif_neigh *softif_neigh = NULL;
161         struct hlist_node *node;
162
163         softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid);
164         if (!softif_neigh_vid)
165                 goto out;
166
167         rcu_read_lock();
168         hlist_for_each_entry_rcu(softif_neigh, node,
169                                  &softif_neigh_vid->softif_neigh_list,
170                                  list) {
171                 if (!compare_eth(softif_neigh->addr, addr))
172                         continue;
173
174                 if (!atomic_inc_not_zero(&softif_neigh->refcount))
175                         continue;
176
177                 softif_neigh->last_seen = jiffies;
178                 goto unlock;
179         }
180
181         softif_neigh = kzalloc(sizeof(struct softif_neigh), GFP_ATOMIC);
182         if (!softif_neigh)
183                 goto unlock;
184
185         memcpy(softif_neigh->addr, addr, ETH_ALEN);
186         softif_neigh->last_seen = jiffies;
187         /* initialize with 2 - caller decrements counter by one */
188         atomic_set(&softif_neigh->refcount, 2);
189
190         INIT_HLIST_NODE(&softif_neigh->list);
191         spin_lock_bh(&bat_priv->softif_neigh_lock);
192         hlist_add_head_rcu(&softif_neigh->list,
193                            &softif_neigh_vid->softif_neigh_list);
194         spin_unlock_bh(&bat_priv->softif_neigh_lock);
195
196 unlock:
197         rcu_read_unlock();
198 out:
199         if (softif_neigh_vid)
200                 softif_neigh_vid_free_ref(softif_neigh_vid);
201         return softif_neigh;
202 }
203
204 static struct softif_neigh *softif_neigh_get_selected(
205                                 struct softif_neigh_vid *softif_neigh_vid)
206 {
207         struct softif_neigh *softif_neigh;
208
209         rcu_read_lock();
210         softif_neigh = rcu_dereference(softif_neigh_vid->softif_neigh);
211
212         if (softif_neigh && !atomic_inc_not_zero(&softif_neigh->refcount))
213                 softif_neigh = NULL;
214
215         rcu_read_unlock();
216         return softif_neigh;
217 }
218
219 static struct softif_neigh *softif_neigh_vid_get_selected(
220                                                 struct bat_priv *bat_priv,
221                                                 short vid)
222 {
223         struct softif_neigh_vid *softif_neigh_vid;
224         struct softif_neigh *softif_neigh = NULL;
225
226         softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid);
227         if (!softif_neigh_vid)
228                 goto out;
229
230         softif_neigh = softif_neigh_get_selected(softif_neigh_vid);
231 out:
232         if (softif_neigh_vid)
233                 softif_neigh_vid_free_ref(softif_neigh_vid);
234         return softif_neigh;
235 }
236
237 static void softif_neigh_vid_select(struct bat_priv *bat_priv,
238                                     struct softif_neigh *new_neigh,
239                                     short vid)
240 {
241         struct softif_neigh_vid *softif_neigh_vid;
242         struct softif_neigh *curr_neigh;
243
244         softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid);
245         if (!softif_neigh_vid)
246                 goto out;
247
248         spin_lock_bh(&bat_priv->softif_neigh_lock);
249
250         if (new_neigh && !atomic_inc_not_zero(&new_neigh->refcount))
251                 new_neigh = NULL;
252
253         curr_neigh = softif_neigh_vid->softif_neigh;
254         rcu_assign_pointer(softif_neigh_vid->softif_neigh, new_neigh);
255
256         if ((curr_neigh) && (!new_neigh))
257                 bat_dbg(DBG_ROUTES, bat_priv,
258                         "Removing mesh exit point on vid: %d (prev: %pM).\n",
259                         vid, curr_neigh->addr);
260         else if ((curr_neigh) && (new_neigh))
261                 bat_dbg(DBG_ROUTES, bat_priv,
262                         "Changing mesh exit point on vid: %d from %pM "
263                         "to %pM.\n", vid, curr_neigh->addr, new_neigh->addr);
264         else if ((!curr_neigh) && (new_neigh))
265                 bat_dbg(DBG_ROUTES, bat_priv,
266                         "Setting mesh exit point on vid: %d to %pM.\n",
267                         vid, new_neigh->addr);
268
269         if (curr_neigh)
270                 softif_neigh_free_ref(curr_neigh);
271
272         spin_unlock_bh(&bat_priv->softif_neigh_lock);
273
274 out:
275         if (softif_neigh_vid)
276                 softif_neigh_vid_free_ref(softif_neigh_vid);
277 }
278
279 static void softif_neigh_vid_deselect(struct bat_priv *bat_priv,
280                                       struct softif_neigh_vid *softif_neigh_vid)
281 {
282         struct softif_neigh *curr_neigh;
283         struct softif_neigh *softif_neigh = NULL, *softif_neigh_tmp;
284         struct hard_iface *primary_if = NULL;
285         struct hlist_node *node;
286
287         primary_if = primary_if_get_selected(bat_priv);
288         if (!primary_if)
289                 goto out;
290
291         /* find new softif_neigh immediately to avoid temporary loops */
292         rcu_read_lock();
293         curr_neigh = rcu_dereference(softif_neigh_vid->softif_neigh);
294
295         hlist_for_each_entry_rcu(softif_neigh_tmp, node,
296                                  &softif_neigh_vid->softif_neigh_list,
297                                  list) {
298                 if (softif_neigh_tmp == curr_neigh)
299                         continue;
300
301                 /* we got a neighbor but its mac is 'bigger' than ours  */
302                 if (memcmp(primary_if->net_dev->dev_addr,
303                            softif_neigh_tmp->addr, ETH_ALEN) < 0)
304                         continue;
305
306                 if (!atomic_inc_not_zero(&softif_neigh_tmp->refcount))
307                         continue;
308
309                 softif_neigh = softif_neigh_tmp;
310                 goto unlock;
311         }
312
313 unlock:
314         rcu_read_unlock();
315 out:
316         softif_neigh_vid_select(bat_priv, softif_neigh, softif_neigh_vid->vid);
317
318         if (primary_if)
319                 hardif_free_ref(primary_if);
320         if (softif_neigh)
321                 softif_neigh_free_ref(softif_neigh);
322 }
323
324 int softif_neigh_seq_print_text(struct seq_file *seq, void *offset)
325 {
326         struct net_device *net_dev = (struct net_device *)seq->private;
327         struct bat_priv *bat_priv = netdev_priv(net_dev);
328         struct softif_neigh_vid *softif_neigh_vid;
329         struct softif_neigh *softif_neigh;
330         struct hard_iface *primary_if;
331         struct hlist_node *node, *node_tmp;
332         struct softif_neigh *curr_softif_neigh;
333         int ret = 0, last_seen_secs, last_seen_msecs;
334
335         primary_if = primary_if_get_selected(bat_priv);
336         if (!primary_if) {
337                 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
338                                  "please specify interfaces to enable it\n",
339                                  net_dev->name);
340                 goto out;
341         }
342
343         if (primary_if->if_status != IF_ACTIVE) {
344                 ret = seq_printf(seq, "BATMAN mesh %s "
345                                  "disabled - primary interface not active\n",
346                                  net_dev->name);
347                 goto out;
348         }
349
350         seq_printf(seq, "Softif neighbor list (%s)\n", net_dev->name);
351
352         rcu_read_lock();
353         hlist_for_each_entry_rcu(softif_neigh_vid, node,
354                                  &bat_priv->softif_neigh_vids, list) {
355                 seq_printf(seq, "     %-15s %s on vid: %d\n",
356                            "Originator", "last-seen", softif_neigh_vid->vid);
357
358                 curr_softif_neigh = softif_neigh_get_selected(softif_neigh_vid);
359
360                 hlist_for_each_entry_rcu(softif_neigh, node_tmp,
361                                          &softif_neigh_vid->softif_neigh_list,
362                                          list) {
363                         last_seen_secs = jiffies_to_msecs(jiffies -
364                                                 softif_neigh->last_seen) / 1000;
365                         last_seen_msecs = jiffies_to_msecs(jiffies -
366                                                 softif_neigh->last_seen) % 1000;
367                         seq_printf(seq, "%s %pM  %3i.%03is\n",
368                                    curr_softif_neigh == softif_neigh
369                                    ? "=>" : "  ", softif_neigh->addr,
370                                    last_seen_secs, last_seen_msecs);
371                 }
372
373                 if (curr_softif_neigh)
374                         softif_neigh_free_ref(curr_softif_neigh);
375
376                 seq_printf(seq, "\n");
377         }
378         rcu_read_unlock();
379
380 out:
381         if (primary_if)
382                 hardif_free_ref(primary_if);
383         return ret;
384 }
385
386 void softif_neigh_purge(struct bat_priv *bat_priv)
387 {
388         struct softif_neigh *softif_neigh, *curr_softif_neigh;
389         struct softif_neigh_vid *softif_neigh_vid;
390         struct hlist_node *node, *node_tmp, *node_tmp2;
391         char do_deselect;
392
393         rcu_read_lock();
394         hlist_for_each_entry_rcu(softif_neigh_vid, node,
395                                  &bat_priv->softif_neigh_vids, list) {
396                 if (!atomic_inc_not_zero(&softif_neigh_vid->refcount))
397                         continue;
398
399                 curr_softif_neigh = softif_neigh_get_selected(softif_neigh_vid);
400                 do_deselect = 0;
401
402                 spin_lock_bh(&bat_priv->softif_neigh_lock);
403                 hlist_for_each_entry_safe(softif_neigh, node_tmp, node_tmp2,
404                                           &softif_neigh_vid->softif_neigh_list,
405                                           list) {
406                         if ((!time_after(jiffies, softif_neigh->last_seen +
407                                 msecs_to_jiffies(SOFTIF_NEIGH_TIMEOUT))) &&
408                             (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE))
409                                 continue;
410
411                         if (curr_softif_neigh == softif_neigh) {
412                                 bat_dbg(DBG_ROUTES, bat_priv,
413                                         "Current mesh exit point on vid: %d "
414                                         "'%pM' vanished.\n",
415                                         softif_neigh_vid->vid,
416                                         softif_neigh->addr);
417                                 do_deselect = 1;
418                         }
419
420                         hlist_del_rcu(&softif_neigh->list);
421                         softif_neigh_free_ref(softif_neigh);
422                 }
423                 spin_unlock_bh(&bat_priv->softif_neigh_lock);
424
425                 /* soft_neigh_vid_deselect() needs to acquire the
426                  * softif_neigh_lock */
427                 if (do_deselect)
428                         softif_neigh_vid_deselect(bat_priv, softif_neigh_vid);
429
430                 if (curr_softif_neigh)
431                         softif_neigh_free_ref(curr_softif_neigh);
432
433                 softif_neigh_vid_free_ref(softif_neigh_vid);
434         }
435         rcu_read_unlock();
436
437         spin_lock_bh(&bat_priv->softif_neigh_vid_lock);
438         hlist_for_each_entry_safe(softif_neigh_vid, node, node_tmp,
439                                   &bat_priv->softif_neigh_vids, list) {
440                 if (!hlist_empty(&softif_neigh_vid->softif_neigh_list))
441                         continue;
442
443                 hlist_del_rcu(&softif_neigh_vid->list);
444                 softif_neigh_vid_free_ref(softif_neigh_vid);
445         }
446         spin_unlock_bh(&bat_priv->softif_neigh_vid_lock);
447
448 }
449
450 static void softif_batman_recv(struct sk_buff *skb, struct net_device *dev,
451                                short vid)
452 {
453         struct bat_priv *bat_priv = netdev_priv(dev);
454         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
455         struct batman_packet *batman_packet;
456         struct softif_neigh *softif_neigh = NULL;
457         struct hard_iface *primary_if = NULL;
458         struct softif_neigh *curr_softif_neigh = NULL;
459
460         if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
461                 batman_packet = (struct batman_packet *)
462                                         (skb->data + ETH_HLEN + VLAN_HLEN);
463         else
464                 batman_packet = (struct batman_packet *)(skb->data + ETH_HLEN);
465
466         if (batman_packet->version != COMPAT_VERSION)
467                 goto out;
468
469         if (batman_packet->packet_type != BAT_PACKET)
470                 goto out;
471
472         if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
473                 goto out;
474
475         if (is_my_mac(batman_packet->orig))
476                 goto out;
477
478         softif_neigh = softif_neigh_get(bat_priv, batman_packet->orig, vid);
479         if (!softif_neigh)
480                 goto out;
481
482         curr_softif_neigh = softif_neigh_vid_get_selected(bat_priv, vid);
483         if (curr_softif_neigh == softif_neigh)
484                 goto out;
485
486         primary_if = primary_if_get_selected(bat_priv);
487         if (!primary_if)
488                 goto out;
489
490         /* we got a neighbor but its mac is 'bigger' than ours  */
491         if (memcmp(primary_if->net_dev->dev_addr,
492                    softif_neigh->addr, ETH_ALEN) < 0)
493                 goto out;
494
495         /* close own batX device and use softif_neigh as exit node */
496         if (!curr_softif_neigh) {
497                 softif_neigh_vid_select(bat_priv, softif_neigh, vid);
498                 goto out;
499         }
500
501         /* switch to new 'smallest neighbor' */
502         if (memcmp(softif_neigh->addr, curr_softif_neigh->addr, ETH_ALEN) < 0)
503                 softif_neigh_vid_select(bat_priv, softif_neigh, vid);
504
505 out:
506         kfree_skb(skb);
507         if (softif_neigh)
508                 softif_neigh_free_ref(softif_neigh);
509         if (curr_softif_neigh)
510                 softif_neigh_free_ref(curr_softif_neigh);
511         if (primary_if)
512                 hardif_free_ref(primary_if);
513         return;
514 }
515
516 static int interface_open(struct net_device *dev)
517 {
518         netif_start_queue(dev);
519         return 0;
520 }
521
522 static int interface_release(struct net_device *dev)
523 {
524         netif_stop_queue(dev);
525         return 0;
526 }
527
528 static struct net_device_stats *interface_stats(struct net_device *dev)
529 {
530         struct bat_priv *bat_priv = netdev_priv(dev);
531         return &bat_priv->stats;
532 }
533
534 static int interface_set_mac_addr(struct net_device *dev, void *p)
535 {
536         struct bat_priv *bat_priv = netdev_priv(dev);
537         struct sockaddr *addr = p;
538
539         if (!is_valid_ether_addr(addr->sa_data))
540                 return -EADDRNOTAVAIL;
541
542         /* only modify transtable if it has been initialised before */
543         if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
544                 tt_local_remove(bat_priv, dev->dev_addr,
545                                  "mac address changed");
546                 tt_local_add(dev, addr->sa_data);
547         }
548
549         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
550         return 0;
551 }
552
553 static int interface_change_mtu(struct net_device *dev, int new_mtu)
554 {
555         /* check ranges */
556         if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
557                 return -EINVAL;
558
559         dev->mtu = new_mtu;
560
561         return 0;
562 }
563
564 int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
565 {
566         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
567         struct bat_priv *bat_priv = netdev_priv(soft_iface);
568         struct hard_iface *primary_if = NULL;
569         struct bcast_packet *bcast_packet;
570         struct vlan_ethhdr *vhdr;
571         struct softif_neigh *curr_softif_neigh = NULL;
572         int data_len = skb->len, ret;
573         short vid = -1;
574         bool do_bcast = false;
575
576         if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
577                 goto dropped;
578
579         soft_iface->trans_start = jiffies;
580
581         switch (ntohs(ethhdr->h_proto)) {
582         case ETH_P_8021Q:
583                 vhdr = (struct vlan_ethhdr *)skb->data;
584                 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
585
586                 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
587                         break;
588
589                 /* fall through */
590         case ETH_P_BATMAN:
591                 softif_batman_recv(skb, soft_iface, vid);
592                 goto end;
593         }
594
595         /**
596          * if we have a another chosen mesh exit node in range
597          * it will transport the packets to the mesh
598          */
599         curr_softif_neigh = softif_neigh_vid_get_selected(bat_priv, vid);
600         if (curr_softif_neigh)
601                 goto dropped;
602
603         /* TODO: check this for locks */
604         tt_local_add(soft_iface, ethhdr->h_source);
605
606         if (is_multicast_ether_addr(ethhdr->h_dest)) {
607                 ret = gw_is_target(bat_priv, skb);
608
609                 if (ret < 0)
610                         goto dropped;
611
612                 if (ret == 0)
613                         do_bcast = true;
614         }
615
616         /* ethernet packet should be broadcasted */
617         if (do_bcast) {
618                 primary_if = primary_if_get_selected(bat_priv);
619                 if (!primary_if)
620                         goto dropped;
621
622                 if (my_skb_head_push(skb, sizeof(struct bcast_packet)) < 0)
623                         goto dropped;
624
625                 bcast_packet = (struct bcast_packet *)skb->data;
626                 bcast_packet->version = COMPAT_VERSION;
627                 bcast_packet->ttl = TTL;
628
629                 /* batman packet type: broadcast */
630                 bcast_packet->packet_type = BAT_BCAST;
631
632                 /* hw address of first interface is the orig mac because only
633                  * this mac is known throughout the mesh */
634                 memcpy(bcast_packet->orig,
635                        primary_if->net_dev->dev_addr, ETH_ALEN);
636
637                 /* set broadcast sequence number */
638                 bcast_packet->seqno =
639                         htonl(atomic_inc_return(&bat_priv->bcast_seqno));
640
641                 add_bcast_packet_to_list(bat_priv, skb);
642
643                 /* a copy is stored in the bcast list, therefore removing
644                  * the original skb. */
645                 kfree_skb(skb);
646
647         /* unicast packet */
648         } else {
649                 ret = unicast_send_skb(skb, bat_priv);
650                 if (ret != 0)
651                         goto dropped_freed;
652         }
653
654         bat_priv->stats.tx_packets++;
655         bat_priv->stats.tx_bytes += data_len;
656         goto end;
657
658 dropped:
659         kfree_skb(skb);
660 dropped_freed:
661         bat_priv->stats.tx_dropped++;
662 end:
663         if (curr_softif_neigh)
664                 softif_neigh_free_ref(curr_softif_neigh);
665         if (primary_if)
666                 hardif_free_ref(primary_if);
667         return NETDEV_TX_OK;
668 }
669
670 void interface_rx(struct net_device *soft_iface,
671                   struct sk_buff *skb, struct hard_iface *recv_if,
672                   int hdr_size)
673 {
674         struct bat_priv *bat_priv = netdev_priv(soft_iface);
675         struct unicast_packet *unicast_packet;
676         struct ethhdr *ethhdr;
677         struct vlan_ethhdr *vhdr;
678         struct softif_neigh *curr_softif_neigh = NULL;
679         short vid = -1;
680         int ret;
681
682         /* check if enough space is available for pulling, and pull */
683         if (!pskb_may_pull(skb, hdr_size))
684                 goto dropped;
685
686         skb_pull_rcsum(skb, hdr_size);
687         skb_reset_mac_header(skb);
688
689         ethhdr = (struct ethhdr *)skb_mac_header(skb);
690
691         switch (ntohs(ethhdr->h_proto)) {
692         case ETH_P_8021Q:
693                 vhdr = (struct vlan_ethhdr *)skb->data;
694                 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
695
696                 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
697                         break;
698
699                 /* fall through */
700         case ETH_P_BATMAN:
701                 goto dropped;
702         }
703
704         /**
705          * if we have a another chosen mesh exit node in range
706          * it will transport the packets to the non-mesh network
707          */
708         curr_softif_neigh = softif_neigh_vid_get_selected(bat_priv, vid);
709         if (curr_softif_neigh) {
710                 skb_push(skb, hdr_size);
711                 unicast_packet = (struct unicast_packet *)skb->data;
712
713                 if ((unicast_packet->packet_type != BAT_UNICAST) &&
714                     (unicast_packet->packet_type != BAT_UNICAST_FRAG))
715                         goto dropped;
716
717                 skb_reset_mac_header(skb);
718
719                 memcpy(unicast_packet->dest,
720                        curr_softif_neigh->addr, ETH_ALEN);
721                 ret = route_unicast_packet(skb, recv_if);
722                 if (ret == NET_RX_DROP)
723                         goto dropped;
724
725                 goto out;
726         }
727
728         /* skb->dev & skb->pkt_type are set here */
729         if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
730                 goto dropped;
731         skb->protocol = eth_type_trans(skb, soft_iface);
732
733         /* should not be necessary anymore as we use skb_pull_rcsum()
734          * TODO: please verify this and remove this TODO
735          * -- Dec 21st 2009, Simon Wunderlich */
736
737 /*      skb->ip_summed = CHECKSUM_UNNECESSARY;*/
738
739         bat_priv->stats.rx_packets++;
740         bat_priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
741
742         soft_iface->last_rx = jiffies;
743
744         netif_rx(skb);
745         goto out;
746
747 dropped:
748         kfree_skb(skb);
749 out:
750         if (curr_softif_neigh)
751                 softif_neigh_free_ref(curr_softif_neigh);
752         return;
753 }
754
755 #ifdef HAVE_NET_DEVICE_OPS
756 static const struct net_device_ops bat_netdev_ops = {
757         .ndo_open = interface_open,
758         .ndo_stop = interface_release,
759         .ndo_get_stats = interface_stats,
760         .ndo_set_mac_address = interface_set_mac_addr,
761         .ndo_change_mtu = interface_change_mtu,
762         .ndo_start_xmit = interface_tx,
763         .ndo_validate_addr = eth_validate_addr
764 };
765 #endif
766
767 static void interface_setup(struct net_device *dev)
768 {
769         struct bat_priv *priv = netdev_priv(dev);
770         char dev_addr[ETH_ALEN];
771
772         ether_setup(dev);
773
774 #ifdef HAVE_NET_DEVICE_OPS
775         dev->netdev_ops = &bat_netdev_ops;
776 #else
777         dev->open = interface_open;
778         dev->stop = interface_release;
779         dev->get_stats = interface_stats;
780         dev->set_mac_address = interface_set_mac_addr;
781         dev->change_mtu = interface_change_mtu;
782         dev->hard_start_xmit = interface_tx;
783 #endif
784         dev->destructor = free_netdev;
785         dev->tx_queue_len = 0;
786
787         /**
788          * can't call min_mtu, because the needed variables
789          * have not been initialized yet
790          */
791         dev->mtu = ETH_DATA_LEN;
792         /* reserve more space in the skbuff for our header */
793         dev->hard_header_len = BAT_HEADER_LEN;
794
795         /* generate random address */
796         random_ether_addr(dev_addr);
797         memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
798
799         SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
800
801         memset(priv, 0, sizeof(struct bat_priv));
802 }
803
804 struct net_device *softif_create(char *name)
805 {
806         struct net_device *soft_iface;
807         struct bat_priv *bat_priv;
808         int ret;
809
810         soft_iface = alloc_netdev(sizeof(struct bat_priv) , name,
811                                    interface_setup);
812
813         if (!soft_iface) {
814                 pr_err("Unable to allocate the batman interface: %s\n", name);
815                 goto out;
816         }
817
818         ret = register_netdevice(soft_iface);
819         if (ret < 0) {
820                 pr_err("Unable to register the batman interface '%s': %i\n",
821                        name, ret);
822                 goto free_soft_iface;
823         }
824
825         bat_priv = netdev_priv(soft_iface);
826
827         atomic_set(&bat_priv->aggregated_ogms, 1);
828         atomic_set(&bat_priv->bonding, 0);
829         atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
830         atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
831         atomic_set(&bat_priv->gw_sel_class, 20);
832         atomic_set(&bat_priv->gw_bandwidth, 41);
833         atomic_set(&bat_priv->orig_interval, 1000);
834         atomic_set(&bat_priv->hop_penalty, 10);
835         atomic_set(&bat_priv->log_level, 0);
836         atomic_set(&bat_priv->fragmentation, 1);
837         atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
838         atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
839
840         atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
841         atomic_set(&bat_priv->bcast_seqno, 1);
842         atomic_set(&bat_priv->tt_local_changed, 0);
843
844         bat_priv->primary_if = NULL;
845         bat_priv->num_ifaces = 0;
846
847         ret = sysfs_add_meshif(soft_iface);
848         if (ret < 0)
849                 goto unreg_soft_iface;
850
851         ret = debugfs_add_meshif(soft_iface);
852         if (ret < 0)
853                 goto unreg_sysfs;
854
855         ret = mesh_init(soft_iface);
856         if (ret < 0)
857                 goto unreg_debugfs;
858
859         return soft_iface;
860
861 unreg_debugfs:
862         debugfs_del_meshif(soft_iface);
863 unreg_sysfs:
864         sysfs_del_meshif(soft_iface);
865 unreg_soft_iface:
866         unregister_netdev(soft_iface);
867         return NULL;
868
869 free_soft_iface:
870         free_netdev(soft_iface);
871 out:
872         return NULL;
873 }
874
875 void softif_destroy(struct net_device *soft_iface)
876 {
877         debugfs_del_meshif(soft_iface);
878         sysfs_del_meshif(soft_iface);
879         mesh_free(soft_iface);
880         unregister_netdevice(soft_iface);
881 }
882
883 int softif_is_valid(struct net_device *net_dev)
884 {
885 #ifdef HAVE_NET_DEVICE_OPS
886         if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
887                 return 1;
888 #else
889         if (net_dev->hard_start_xmit == interface_tx)
890                 return 1;
891 #endif
892
893         return 0;
894 }
895
896 /* ethtool */
897 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
898 {
899         cmd->supported = 0;
900         cmd->advertising = 0;
901         ethtool_cmd_speed_set(cmd, SPEED_10);
902         cmd->duplex = DUPLEX_FULL;
903         cmd->port = PORT_TP;
904         cmd->phy_address = 0;
905         cmd->transceiver = XCVR_INTERNAL;
906         cmd->autoneg = AUTONEG_DISABLE;
907         cmd->maxtxpkt = 0;
908         cmd->maxrxpkt = 0;
909
910         return 0;
911 }
912
913 static void bat_get_drvinfo(struct net_device *dev,
914                             struct ethtool_drvinfo *info)
915 {
916         strcpy(info->driver, "B.A.T.M.A.N. advanced");
917         strcpy(info->version, SOURCE_VERSION);
918         strcpy(info->fw_version, "N/A");
919         strcpy(info->bus_info, "batman");
920 }
921
922 static u32 bat_get_msglevel(struct net_device *dev)
923 {
924         return -EOPNOTSUPP;
925 }
926
927 static void bat_set_msglevel(struct net_device *dev, u32 value)
928 {
929 }
930
931 static u32 bat_get_link(struct net_device *dev)
932 {
933         return 1;
934 }
935