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