Add linux-next specific files for 20110831
[linux-2.6/next.git] / net / batman-adv / routing.c
blob19499281b69507c71480b1942429289543b6be6a
1 /*
2 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
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
22 #include "main.h"
23 #include "routing.h"
24 #include "send.h"
25 #include "hash.h"
26 #include "soft-interface.h"
27 #include "hard-interface.h"
28 #include "icmp_socket.h"
29 #include "translation-table.h"
30 #include "originator.h"
31 #include "ring_buffer.h"
32 #include "vis.h"
33 #include "aggregation.h"
34 #include "gateway_common.h"
35 #include "gateway_client.h"
36 #include "unicast.h"
38 void slide_own_bcast_window(struct hard_iface *hard_iface)
40 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
41 struct hashtable_t *hash = bat_priv->orig_hash;
42 struct hlist_node *node;
43 struct hlist_head *head;
44 struct orig_node *orig_node;
45 unsigned long *word;
46 int i;
47 size_t word_index;
49 for (i = 0; i < hash->size; i++) {
50 head = &hash->table[i];
52 rcu_read_lock();
53 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
54 spin_lock_bh(&orig_node->ogm_cnt_lock);
55 word_index = hard_iface->if_num * NUM_WORDS;
56 word = &(orig_node->bcast_own[word_index]);
58 bit_get_packet(bat_priv, word, 1, 0);
59 orig_node->bcast_own_sum[hard_iface->if_num] =
60 bit_packet_count(word);
61 spin_unlock_bh(&orig_node->ogm_cnt_lock);
63 rcu_read_unlock();
67 static void update_route(struct bat_priv *bat_priv,
68 struct orig_node *orig_node,
69 struct neigh_node *neigh_node)
71 struct neigh_node *curr_router;
73 curr_router = orig_node_get_router(orig_node);
75 /* route deleted */
76 if ((curr_router) && (!neigh_node)) {
77 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
78 orig_node->orig);
79 tt_global_del_orig(bat_priv, orig_node,
80 "Deleted route towards originator");
82 /* route added */
83 } else if ((!curr_router) && (neigh_node)) {
85 bat_dbg(DBG_ROUTES, bat_priv,
86 "Adding route towards: %pM (via %pM)\n",
87 orig_node->orig, neigh_node->addr);
88 /* route changed */
89 } else if (neigh_node && curr_router) {
90 bat_dbg(DBG_ROUTES, bat_priv,
91 "Changing route towards: %pM "
92 "(now via %pM - was via %pM)\n",
93 orig_node->orig, neigh_node->addr,
94 curr_router->addr);
97 if (curr_router)
98 neigh_node_free_ref(curr_router);
100 /* increase refcount of new best neighbor */
101 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
102 neigh_node = NULL;
104 spin_lock_bh(&orig_node->neigh_list_lock);
105 rcu_assign_pointer(orig_node->router, neigh_node);
106 spin_unlock_bh(&orig_node->neigh_list_lock);
108 /* decrease refcount of previous best neighbor */
109 if (curr_router)
110 neigh_node_free_ref(curr_router);
113 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
114 struct neigh_node *neigh_node)
116 struct neigh_node *router = NULL;
118 if (!orig_node)
119 goto out;
121 router = orig_node_get_router(orig_node);
123 if (router != neigh_node)
124 update_route(bat_priv, orig_node, neigh_node);
126 out:
127 if (router)
128 neigh_node_free_ref(router);
131 static int is_bidirectional_neigh(struct orig_node *orig_node,
132 struct orig_node *orig_neigh_node,
133 struct batman_packet *batman_packet,
134 struct hard_iface *if_incoming)
136 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
137 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
138 struct hlist_node *node;
139 uint8_t total_count;
140 uint8_t orig_eq_count, neigh_rq_count, tq_own;
141 int tq_asym_penalty, ret = 0;
143 /* find corresponding one hop neighbor */
144 rcu_read_lock();
145 hlist_for_each_entry_rcu(tmp_neigh_node, node,
146 &orig_neigh_node->neigh_list, list) {
148 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
149 continue;
151 if (tmp_neigh_node->if_incoming != if_incoming)
152 continue;
154 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
155 continue;
157 neigh_node = tmp_neigh_node;
158 break;
160 rcu_read_unlock();
162 if (!neigh_node)
163 neigh_node = create_neighbor(orig_neigh_node,
164 orig_neigh_node,
165 orig_neigh_node->orig,
166 if_incoming);
168 if (!neigh_node)
169 goto out;
171 /* if orig_node is direct neighbor update neigh_node last_valid */
172 if (orig_node == orig_neigh_node)
173 neigh_node->last_valid = jiffies;
175 orig_node->last_valid = jiffies;
177 /* find packet count of corresponding one hop neighbor */
178 spin_lock_bh(&orig_node->ogm_cnt_lock);
179 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
180 neigh_rq_count = neigh_node->real_packet_count;
181 spin_unlock_bh(&orig_node->ogm_cnt_lock);
183 /* pay attention to not get a value bigger than 100 % */
184 total_count = (orig_eq_count > neigh_rq_count ?
185 neigh_rq_count : orig_eq_count);
187 /* if we have too few packets (too less data) we set tq_own to zero */
188 /* if we receive too few packets it is not considered bidirectional */
189 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
190 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
191 tq_own = 0;
192 else
193 /* neigh_node->real_packet_count is never zero as we
194 * only purge old information when getting new
195 * information */
196 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
199 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
200 * affect the nearly-symmetric links only a little, but
201 * punishes asymmetric links more. This will give a value
202 * between 0 and TQ_MAX_VALUE
204 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
205 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
206 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
207 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
208 (TQ_LOCAL_WINDOW_SIZE *
209 TQ_LOCAL_WINDOW_SIZE *
210 TQ_LOCAL_WINDOW_SIZE);
212 batman_packet->tq = ((batman_packet->tq * tq_own * tq_asym_penalty) /
213 (TQ_MAX_VALUE * TQ_MAX_VALUE));
215 bat_dbg(DBG_BATMAN, bat_priv,
216 "bidirectional: "
217 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
218 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
219 "total tq: %3i\n",
220 orig_node->orig, orig_neigh_node->orig, total_count,
221 neigh_rq_count, tq_own, tq_asym_penalty, batman_packet->tq);
223 /* if link has the minimum required transmission quality
224 * consider it bidirectional */
225 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
226 ret = 1;
228 out:
229 if (neigh_node)
230 neigh_node_free_ref(neigh_node);
231 return ret;
234 /* caller must hold the neigh_list_lock */
235 void bonding_candidate_del(struct orig_node *orig_node,
236 struct neigh_node *neigh_node)
238 /* this neighbor is not part of our candidate list */
239 if (list_empty(&neigh_node->bonding_list))
240 goto out;
242 list_del_rcu(&neigh_node->bonding_list);
243 INIT_LIST_HEAD(&neigh_node->bonding_list);
244 neigh_node_free_ref(neigh_node);
245 atomic_dec(&orig_node->bond_candidates);
247 out:
248 return;
251 static void bonding_candidate_add(struct orig_node *orig_node,
252 struct neigh_node *neigh_node)
254 struct hlist_node *node;
255 struct neigh_node *tmp_neigh_node, *router = NULL;
256 uint8_t interference_candidate = 0;
258 spin_lock_bh(&orig_node->neigh_list_lock);
260 /* only consider if it has the same primary address ... */
261 if (!compare_eth(orig_node->orig,
262 neigh_node->orig_node->primary_addr))
263 goto candidate_del;
265 router = orig_node_get_router(orig_node);
266 if (!router)
267 goto candidate_del;
269 /* ... and is good enough to be considered */
270 if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
271 goto candidate_del;
274 * check if we have another candidate with the same mac address or
275 * interface. If we do, we won't select this candidate because of
276 * possible interference.
278 hlist_for_each_entry_rcu(tmp_neigh_node, node,
279 &orig_node->neigh_list, list) {
281 if (tmp_neigh_node == neigh_node)
282 continue;
284 /* we only care if the other candidate is even
285 * considered as candidate. */
286 if (list_empty(&tmp_neigh_node->bonding_list))
287 continue;
289 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
290 (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
291 interference_candidate = 1;
292 break;
296 /* don't care further if it is an interference candidate */
297 if (interference_candidate)
298 goto candidate_del;
300 /* this neighbor already is part of our candidate list */
301 if (!list_empty(&neigh_node->bonding_list))
302 goto out;
304 if (!atomic_inc_not_zero(&neigh_node->refcount))
305 goto out;
307 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
308 atomic_inc(&orig_node->bond_candidates);
309 goto out;
311 candidate_del:
312 bonding_candidate_del(orig_node, neigh_node);
314 out:
315 spin_unlock_bh(&orig_node->neigh_list_lock);
317 if (router)
318 neigh_node_free_ref(router);
321 /* copy primary address for bonding */
322 static void bonding_save_primary(const struct orig_node *orig_node,
323 struct orig_node *orig_neigh_node,
324 const struct batman_packet *batman_packet)
326 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
327 return;
329 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
332 static void update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
333 const struct ethhdr *ethhdr,
334 const struct batman_packet *batman_packet,
335 struct hard_iface *if_incoming,
336 const unsigned char *tt_buff, int is_duplicate)
338 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
339 struct neigh_node *router = NULL;
340 struct orig_node *orig_node_tmp;
341 struct hlist_node *node;
342 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
344 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
345 "Searching and updating originator entry of received packet\n");
347 rcu_read_lock();
348 hlist_for_each_entry_rcu(tmp_neigh_node, node,
349 &orig_node->neigh_list, list) {
350 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
351 (tmp_neigh_node->if_incoming == if_incoming) &&
352 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
353 if (neigh_node)
354 neigh_node_free_ref(neigh_node);
355 neigh_node = tmp_neigh_node;
356 continue;
359 if (is_duplicate)
360 continue;
362 spin_lock_bh(&tmp_neigh_node->tq_lock);
363 ring_buffer_set(tmp_neigh_node->tq_recv,
364 &tmp_neigh_node->tq_index, 0);
365 tmp_neigh_node->tq_avg =
366 ring_buffer_avg(tmp_neigh_node->tq_recv);
367 spin_unlock_bh(&tmp_neigh_node->tq_lock);
370 if (!neigh_node) {
371 struct orig_node *orig_tmp;
373 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
374 if (!orig_tmp)
375 goto unlock;
377 neigh_node = create_neighbor(orig_node, orig_tmp,
378 ethhdr->h_source, if_incoming);
380 orig_node_free_ref(orig_tmp);
381 if (!neigh_node)
382 goto unlock;
383 } else
384 bat_dbg(DBG_BATMAN, bat_priv,
385 "Updating existing last-hop neighbor of originator\n");
387 rcu_read_unlock();
389 orig_node->flags = batman_packet->flags;
390 neigh_node->last_valid = jiffies;
392 spin_lock_bh(&neigh_node->tq_lock);
393 ring_buffer_set(neigh_node->tq_recv,
394 &neigh_node->tq_index,
395 batman_packet->tq);
396 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
397 spin_unlock_bh(&neigh_node->tq_lock);
399 if (!is_duplicate) {
400 orig_node->last_ttl = batman_packet->ttl;
401 neigh_node->last_ttl = batman_packet->ttl;
404 bonding_candidate_add(orig_node, neigh_node);
406 /* if this neighbor already is our next hop there is nothing
407 * to change */
408 router = orig_node_get_router(orig_node);
409 if (router == neigh_node)
410 goto update_tt;
412 /* if this neighbor does not offer a better TQ we won't consider it */
413 if (router && (router->tq_avg > neigh_node->tq_avg))
414 goto update_tt;
416 /* if the TQ is the same and the link not more symmetric we
417 * won't consider it either */
418 if (router && (neigh_node->tq_avg == router->tq_avg)) {
419 orig_node_tmp = router->orig_node;
420 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
421 bcast_own_sum_orig =
422 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
423 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
425 orig_node_tmp = neigh_node->orig_node;
426 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
427 bcast_own_sum_neigh =
428 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
429 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
431 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
432 goto update_tt;
435 update_routes(bat_priv, orig_node, neigh_node);
437 update_tt:
438 /* I have to check for transtable changes only if the OGM has been
439 * sent through a primary interface */
440 if (((batman_packet->orig != ethhdr->h_source) &&
441 (batman_packet->ttl > 2)) ||
442 (batman_packet->flags & PRIMARIES_FIRST_HOP))
443 tt_update_orig(bat_priv, orig_node, tt_buff,
444 batman_packet->tt_num_changes,
445 batman_packet->ttvn, batman_packet->tt_crc);
447 if (orig_node->gw_flags != batman_packet->gw_flags)
448 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
450 orig_node->gw_flags = batman_packet->gw_flags;
452 /* restart gateway selection if fast or late switching was enabled */
453 if ((orig_node->gw_flags) &&
454 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
455 (atomic_read(&bat_priv->gw_sel_class) > 2))
456 gw_check_election(bat_priv, orig_node);
458 goto out;
460 unlock:
461 rcu_read_unlock();
462 out:
463 if (neigh_node)
464 neigh_node_free_ref(neigh_node);
465 if (router)
466 neigh_node_free_ref(router);
469 /* checks whether the host restarted and is in the protection time.
470 * returns:
471 * 0 if the packet is to be accepted
472 * 1 if the packet is to be ignored.
474 static int window_protected(struct bat_priv *bat_priv,
475 int32_t seq_num_diff,
476 unsigned long *last_reset)
478 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
479 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
480 if (time_after(jiffies, *last_reset +
481 msecs_to_jiffies(RESET_PROTECTION_MS))) {
483 *last_reset = jiffies;
484 bat_dbg(DBG_BATMAN, bat_priv,
485 "old packet received, start protection\n");
487 return 0;
488 } else
489 return 1;
491 return 0;
494 /* processes a batman packet for all interfaces, adjusts the sequence number and
495 * finds out whether it is a duplicate.
496 * returns:
497 * 1 the packet is a duplicate
498 * 0 the packet has not yet been received
499 * -1 the packet is old and has been received while the seqno window
500 * was protected. Caller should drop it.
502 static int count_real_packets(const struct ethhdr *ethhdr,
503 const struct batman_packet *batman_packet,
504 const struct hard_iface *if_incoming)
506 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
507 struct orig_node *orig_node;
508 struct neigh_node *tmp_neigh_node;
509 struct hlist_node *node;
510 int is_duplicate = 0;
511 int32_t seq_diff;
512 int need_update = 0;
513 int set_mark, ret = -1;
515 orig_node = get_orig_node(bat_priv, batman_packet->orig);
516 if (!orig_node)
517 return 0;
519 spin_lock_bh(&orig_node->ogm_cnt_lock);
520 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
522 /* signalize caller that the packet is to be dropped. */
523 if (window_protected(bat_priv, seq_diff,
524 &orig_node->batman_seqno_reset))
525 goto out;
527 rcu_read_lock();
528 hlist_for_each_entry_rcu(tmp_neigh_node, node,
529 &orig_node->neigh_list, list) {
531 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
532 orig_node->last_real_seqno,
533 batman_packet->seqno);
535 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
536 (tmp_neigh_node->if_incoming == if_incoming))
537 set_mark = 1;
538 else
539 set_mark = 0;
541 /* if the window moved, set the update flag. */
542 need_update |= bit_get_packet(bat_priv,
543 tmp_neigh_node->real_bits,
544 seq_diff, set_mark);
546 tmp_neigh_node->real_packet_count =
547 bit_packet_count(tmp_neigh_node->real_bits);
549 rcu_read_unlock();
551 if (need_update) {
552 bat_dbg(DBG_BATMAN, bat_priv,
553 "updating last_seqno: old %d, new %d\n",
554 orig_node->last_real_seqno, batman_packet->seqno);
555 orig_node->last_real_seqno = batman_packet->seqno;
558 ret = is_duplicate;
560 out:
561 spin_unlock_bh(&orig_node->ogm_cnt_lock);
562 orig_node_free_ref(orig_node);
563 return ret;
566 void receive_bat_packet(const struct ethhdr *ethhdr,
567 struct batman_packet *batman_packet,
568 const unsigned char *tt_buff,
569 struct hard_iface *if_incoming)
571 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
572 struct hard_iface *hard_iface;
573 struct orig_node *orig_neigh_node, *orig_node;
574 struct neigh_node *router = NULL, *router_router = NULL;
575 struct neigh_node *orig_neigh_router = NULL;
576 int has_directlink_flag;
577 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
578 int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
579 int is_duplicate;
580 uint32_t if_incoming_seqno;
582 /* Silently drop when the batman packet is actually not a
583 * correct packet.
585 * This might happen if a packet is padded (e.g. Ethernet has a
586 * minimum frame length of 64 byte) and the aggregation interprets
587 * it as an additional length.
589 * TODO: A more sane solution would be to have a bit in the
590 * batman_packet to detect whether the packet is the last
591 * packet in an aggregation. Here we expect that the padding
592 * is always zero (or not 0x01)
594 if (batman_packet->packet_type != BAT_PACKET)
595 return;
597 /* could be changed by schedule_own_packet() */
598 if_incoming_seqno = atomic_read(&if_incoming->seqno);
600 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
602 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
603 batman_packet->orig) ? 1 : 0);
605 bat_dbg(DBG_BATMAN, bat_priv,
606 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
607 "(from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, "
608 "crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
609 ethhdr->h_source, if_incoming->net_dev->name,
610 if_incoming->net_dev->dev_addr, batman_packet->orig,
611 batman_packet->prev_sender, batman_packet->seqno,
612 batman_packet->ttvn, batman_packet->tt_crc,
613 batman_packet->tt_num_changes, batman_packet->tq,
614 batman_packet->ttl, batman_packet->version,
615 has_directlink_flag);
617 rcu_read_lock();
618 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
619 if (hard_iface->if_status != IF_ACTIVE)
620 continue;
622 if (hard_iface->soft_iface != if_incoming->soft_iface)
623 continue;
625 if (compare_eth(ethhdr->h_source,
626 hard_iface->net_dev->dev_addr))
627 is_my_addr = 1;
629 if (compare_eth(batman_packet->orig,
630 hard_iface->net_dev->dev_addr))
631 is_my_orig = 1;
633 if (compare_eth(batman_packet->prev_sender,
634 hard_iface->net_dev->dev_addr))
635 is_my_oldorig = 1;
637 if (is_broadcast_ether_addr(ethhdr->h_source))
638 is_broadcast = 1;
640 rcu_read_unlock();
642 if (batman_packet->version != COMPAT_VERSION) {
643 bat_dbg(DBG_BATMAN, bat_priv,
644 "Drop packet: incompatible batman version (%i)\n",
645 batman_packet->version);
646 return;
649 if (is_my_addr) {
650 bat_dbg(DBG_BATMAN, bat_priv,
651 "Drop packet: received my own broadcast (sender: %pM"
652 ")\n",
653 ethhdr->h_source);
654 return;
657 if (is_broadcast) {
658 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
659 "ignoring all packets with broadcast source addr (sender: %pM"
660 ")\n", ethhdr->h_source);
661 return;
664 if (is_my_orig) {
665 unsigned long *word;
666 int offset;
668 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
669 if (!orig_neigh_node)
670 return;
672 /* neighbor has to indicate direct link and it has to
673 * come via the corresponding interface */
674 /* save packet seqno for bidirectional check */
675 if (has_directlink_flag &&
676 compare_eth(if_incoming->net_dev->dev_addr,
677 batman_packet->orig)) {
678 offset = if_incoming->if_num * NUM_WORDS;
680 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
681 word = &(orig_neigh_node->bcast_own[offset]);
682 bit_mark(word,
683 if_incoming_seqno - batman_packet->seqno - 2);
684 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
685 bit_packet_count(word);
686 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
689 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
690 "originator packet from myself (via neighbor)\n");
691 orig_node_free_ref(orig_neigh_node);
692 return;
695 if (is_my_oldorig) {
696 bat_dbg(DBG_BATMAN, bat_priv,
697 "Drop packet: ignoring all rebroadcast echos (sender: "
698 "%pM)\n", ethhdr->h_source);
699 return;
702 orig_node = get_orig_node(bat_priv, batman_packet->orig);
703 if (!orig_node)
704 return;
706 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
708 if (is_duplicate == -1) {
709 bat_dbg(DBG_BATMAN, bat_priv,
710 "Drop packet: packet within seqno protection time "
711 "(sender: %pM)\n", ethhdr->h_source);
712 goto out;
715 if (batman_packet->tq == 0) {
716 bat_dbg(DBG_BATMAN, bat_priv,
717 "Drop packet: originator packet with tq equal 0\n");
718 goto out;
721 router = orig_node_get_router(orig_node);
722 if (router)
723 router_router = orig_node_get_router(router->orig_node);
725 /* avoid temporary routing loops */
726 if (router && router_router &&
727 (compare_eth(router->addr, batman_packet->prev_sender)) &&
728 !(compare_eth(batman_packet->orig, batman_packet->prev_sender)) &&
729 (compare_eth(router->addr, router_router->addr))) {
730 bat_dbg(DBG_BATMAN, bat_priv,
731 "Drop packet: ignoring all rebroadcast packets that "
732 "may make me loop (sender: %pM)\n", ethhdr->h_source);
733 goto out;
736 /* if sender is a direct neighbor the sender mac equals
737 * originator mac */
738 orig_neigh_node = (is_single_hop_neigh ?
739 orig_node :
740 get_orig_node(bat_priv, ethhdr->h_source));
741 if (!orig_neigh_node)
742 goto out;
744 orig_neigh_router = orig_node_get_router(orig_neigh_node);
746 /* drop packet if sender is not a direct neighbor and if we
747 * don't route towards it */
748 if (!is_single_hop_neigh && (!orig_neigh_router)) {
749 bat_dbg(DBG_BATMAN, bat_priv,
750 "Drop packet: OGM via unknown neighbor!\n");
751 goto out_neigh;
754 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
755 batman_packet, if_incoming);
757 bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
759 /* update ranking if it is not a duplicate or has the same
760 * seqno and similar ttl as the non-duplicate */
761 if (is_bidirectional &&
762 (!is_duplicate ||
763 ((orig_node->last_real_seqno == batman_packet->seqno) &&
764 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
765 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
766 if_incoming, tt_buff, is_duplicate);
768 /* is single hop (direct) neighbor */
769 if (is_single_hop_neigh) {
771 /* mark direct link on incoming interface */
772 schedule_forward_packet(orig_node, ethhdr, batman_packet,
773 1, if_incoming);
775 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
776 "rebroadcast neighbor packet with direct link flag\n");
777 goto out_neigh;
780 /* multihop originator */
781 if (!is_bidirectional) {
782 bat_dbg(DBG_BATMAN, bat_priv,
783 "Drop packet: not received via bidirectional link\n");
784 goto out_neigh;
787 if (is_duplicate) {
788 bat_dbg(DBG_BATMAN, bat_priv,
789 "Drop packet: duplicate packet received\n");
790 goto out_neigh;
793 bat_dbg(DBG_BATMAN, bat_priv,
794 "Forwarding packet: rebroadcast originator packet\n");
795 schedule_forward_packet(orig_node, ethhdr, batman_packet,
796 0, if_incoming);
798 out_neigh:
799 if ((orig_neigh_node) && (!is_single_hop_neigh))
800 orig_node_free_ref(orig_neigh_node);
801 out:
802 if (router)
803 neigh_node_free_ref(router);
804 if (router_router)
805 neigh_node_free_ref(router_router);
806 if (orig_neigh_router)
807 neigh_node_free_ref(orig_neigh_router);
809 orig_node_free_ref(orig_node);
812 int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
814 struct ethhdr *ethhdr;
816 /* drop packet if it has not necessary minimum size */
817 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
818 return NET_RX_DROP;
820 ethhdr = (struct ethhdr *)skb_mac_header(skb);
822 /* packet with broadcast indication but unicast recipient */
823 if (!is_broadcast_ether_addr(ethhdr->h_dest))
824 return NET_RX_DROP;
826 /* packet with broadcast sender address */
827 if (is_broadcast_ether_addr(ethhdr->h_source))
828 return NET_RX_DROP;
830 /* create a copy of the skb, if needed, to modify it. */
831 if (skb_cow(skb, 0) < 0)
832 return NET_RX_DROP;
834 /* keep skb linear */
835 if (skb_linearize(skb) < 0)
836 return NET_RX_DROP;
838 ethhdr = (struct ethhdr *)skb_mac_header(skb);
840 receive_aggr_bat_packet(ethhdr,
841 skb->data,
842 skb_headlen(skb),
843 hard_iface);
845 kfree_skb(skb);
846 return NET_RX_SUCCESS;
849 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
850 struct sk_buff *skb, size_t icmp_len)
852 struct hard_iface *primary_if = NULL;
853 struct orig_node *orig_node = NULL;
854 struct neigh_node *router = NULL;
855 struct icmp_packet_rr *icmp_packet;
856 int ret = NET_RX_DROP;
858 icmp_packet = (struct icmp_packet_rr *)skb->data;
860 /* add data to device queue */
861 if (icmp_packet->msg_type != ECHO_REQUEST) {
862 bat_socket_receive_packet(icmp_packet, icmp_len);
863 goto out;
866 primary_if = primary_if_get_selected(bat_priv);
867 if (!primary_if)
868 goto out;
870 /* answer echo request (ping) */
871 /* get routing information */
872 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
873 if (!orig_node)
874 goto out;
876 router = orig_node_get_router(orig_node);
877 if (!router)
878 goto out;
880 /* create a copy of the skb, if needed, to modify it. */
881 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
882 goto out;
884 icmp_packet = (struct icmp_packet_rr *)skb->data;
886 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
887 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
888 icmp_packet->msg_type = ECHO_REPLY;
889 icmp_packet->ttl = TTL;
891 send_skb_packet(skb, router->if_incoming, router->addr);
892 ret = NET_RX_SUCCESS;
894 out:
895 if (primary_if)
896 hardif_free_ref(primary_if);
897 if (router)
898 neigh_node_free_ref(router);
899 if (orig_node)
900 orig_node_free_ref(orig_node);
901 return ret;
904 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
905 struct sk_buff *skb)
907 struct hard_iface *primary_if = NULL;
908 struct orig_node *orig_node = NULL;
909 struct neigh_node *router = NULL;
910 struct icmp_packet *icmp_packet;
911 int ret = NET_RX_DROP;
913 icmp_packet = (struct icmp_packet *)skb->data;
915 /* send TTL exceeded if packet is an echo request (traceroute) */
916 if (icmp_packet->msg_type != ECHO_REQUEST) {
917 pr_debug("Warning - can't forward icmp packet from %pM to "
918 "%pM: ttl exceeded\n", icmp_packet->orig,
919 icmp_packet->dst);
920 goto out;
923 primary_if = primary_if_get_selected(bat_priv);
924 if (!primary_if)
925 goto out;
927 /* get routing information */
928 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
929 if (!orig_node)
930 goto out;
932 router = orig_node_get_router(orig_node);
933 if (!router)
934 goto out;
936 /* create a copy of the skb, if needed, to modify it. */
937 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
938 goto out;
940 icmp_packet = (struct icmp_packet *)skb->data;
942 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
943 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
944 icmp_packet->msg_type = TTL_EXCEEDED;
945 icmp_packet->ttl = TTL;
947 send_skb_packet(skb, router->if_incoming, router->addr);
948 ret = NET_RX_SUCCESS;
950 out:
951 if (primary_if)
952 hardif_free_ref(primary_if);
953 if (router)
954 neigh_node_free_ref(router);
955 if (orig_node)
956 orig_node_free_ref(orig_node);
957 return ret;
961 int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
963 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
964 struct icmp_packet_rr *icmp_packet;
965 struct ethhdr *ethhdr;
966 struct orig_node *orig_node = NULL;
967 struct neigh_node *router = NULL;
968 int hdr_size = sizeof(struct icmp_packet);
969 int ret = NET_RX_DROP;
972 * we truncate all incoming icmp packets if they don't match our size
974 if (skb->len >= sizeof(struct icmp_packet_rr))
975 hdr_size = sizeof(struct icmp_packet_rr);
977 /* drop packet if it has not necessary minimum size */
978 if (unlikely(!pskb_may_pull(skb, hdr_size)))
979 goto out;
981 ethhdr = (struct ethhdr *)skb_mac_header(skb);
983 /* packet with unicast indication but broadcast recipient */
984 if (is_broadcast_ether_addr(ethhdr->h_dest))
985 goto out;
987 /* packet with broadcast sender address */
988 if (is_broadcast_ether_addr(ethhdr->h_source))
989 goto out;
991 /* not for me */
992 if (!is_my_mac(ethhdr->h_dest))
993 goto out;
995 icmp_packet = (struct icmp_packet_rr *)skb->data;
997 /* add record route information if not full */
998 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
999 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1000 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1001 ethhdr->h_dest, ETH_ALEN);
1002 icmp_packet->rr_cur++;
1005 /* packet for me */
1006 if (is_my_mac(icmp_packet->dst))
1007 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1009 /* TTL exceeded */
1010 if (icmp_packet->ttl < 2)
1011 return recv_icmp_ttl_exceeded(bat_priv, skb);
1013 /* get routing information */
1014 orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
1015 if (!orig_node)
1016 goto out;
1018 router = orig_node_get_router(orig_node);
1019 if (!router)
1020 goto out;
1022 /* create a copy of the skb, if needed, to modify it. */
1023 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1024 goto out;
1026 icmp_packet = (struct icmp_packet_rr *)skb->data;
1028 /* decrement ttl */
1029 icmp_packet->ttl--;
1031 /* route it */
1032 send_skb_packet(skb, router->if_incoming, router->addr);
1033 ret = NET_RX_SUCCESS;
1035 out:
1036 if (router)
1037 neigh_node_free_ref(router);
1038 if (orig_node)
1039 orig_node_free_ref(orig_node);
1040 return ret;
1043 /* In the bonding case, send the packets in a round
1044 * robin fashion over the remaining interfaces.
1046 * This method rotates the bonding list and increases the
1047 * returned router's refcount. */
1048 static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
1049 const struct hard_iface *recv_if)
1051 struct neigh_node *tmp_neigh_node;
1052 struct neigh_node *router = NULL, *first_candidate = NULL;
1054 rcu_read_lock();
1055 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1056 bonding_list) {
1057 if (!first_candidate)
1058 first_candidate = tmp_neigh_node;
1060 /* recv_if == NULL on the first node. */
1061 if (tmp_neigh_node->if_incoming == recv_if)
1062 continue;
1064 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1065 continue;
1067 router = tmp_neigh_node;
1068 break;
1071 /* use the first candidate if nothing was found. */
1072 if (!router && first_candidate &&
1073 atomic_inc_not_zero(&first_candidate->refcount))
1074 router = first_candidate;
1076 if (!router)
1077 goto out;
1079 /* selected should point to the next element
1080 * after the current router */
1081 spin_lock_bh(&primary_orig->neigh_list_lock);
1082 /* this is a list_move(), which unfortunately
1083 * does not exist as rcu version */
1084 list_del_rcu(&primary_orig->bond_list);
1085 list_add_rcu(&primary_orig->bond_list,
1086 &router->bonding_list);
1087 spin_unlock_bh(&primary_orig->neigh_list_lock);
1089 out:
1090 rcu_read_unlock();
1091 return router;
1094 /* Interface Alternating: Use the best of the
1095 * remaining candidates which are not using
1096 * this interface.
1098 * Increases the returned router's refcount */
1099 static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
1100 const struct hard_iface *recv_if)
1102 struct neigh_node *tmp_neigh_node;
1103 struct neigh_node *router = NULL, *first_candidate = NULL;
1105 rcu_read_lock();
1106 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1107 bonding_list) {
1108 if (!first_candidate)
1109 first_candidate = tmp_neigh_node;
1111 /* recv_if == NULL on the first node. */
1112 if (tmp_neigh_node->if_incoming == recv_if)
1113 continue;
1115 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1116 continue;
1118 /* if we don't have a router yet
1119 * or this one is better, choose it. */
1120 if ((!router) ||
1121 (tmp_neigh_node->tq_avg > router->tq_avg)) {
1122 /* decrement refcount of
1123 * previously selected router */
1124 if (router)
1125 neigh_node_free_ref(router);
1127 router = tmp_neigh_node;
1128 atomic_inc_not_zero(&router->refcount);
1131 neigh_node_free_ref(tmp_neigh_node);
1134 /* use the first candidate if nothing was found. */
1135 if (!router && first_candidate &&
1136 atomic_inc_not_zero(&first_candidate->refcount))
1137 router = first_candidate;
1139 rcu_read_unlock();
1140 return router;
1143 int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
1145 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1146 struct tt_query_packet *tt_query;
1147 struct ethhdr *ethhdr;
1149 /* drop packet if it has not necessary minimum size */
1150 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
1151 goto out;
1153 /* I could need to modify it */
1154 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
1155 goto out;
1157 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1159 /* packet with unicast indication but broadcast recipient */
1160 if (is_broadcast_ether_addr(ethhdr->h_dest))
1161 goto out;
1163 /* packet with broadcast sender address */
1164 if (is_broadcast_ether_addr(ethhdr->h_source))
1165 goto out;
1167 tt_query = (struct tt_query_packet *)skb->data;
1169 tt_query->tt_data = ntohs(tt_query->tt_data);
1171 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
1172 case TT_REQUEST:
1173 /* If we cannot provide an answer the tt_request is
1174 * forwarded */
1175 if (!send_tt_response(bat_priv, tt_query)) {
1176 bat_dbg(DBG_TT, bat_priv,
1177 "Routing TT_REQUEST to %pM [%c]\n",
1178 tt_query->dst,
1179 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1180 tt_query->tt_data = htons(tt_query->tt_data);
1181 return route_unicast_packet(skb, recv_if);
1183 break;
1184 case TT_RESPONSE:
1185 /* packet needs to be linearized to access the TT changes */
1186 if (skb_linearize(skb) < 0)
1187 goto out;
1189 if (is_my_mac(tt_query->dst))
1190 handle_tt_response(bat_priv, tt_query);
1191 else {
1192 bat_dbg(DBG_TT, bat_priv,
1193 "Routing TT_RESPONSE to %pM [%c]\n",
1194 tt_query->dst,
1195 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1196 tt_query->tt_data = htons(tt_query->tt_data);
1197 return route_unicast_packet(skb, recv_if);
1199 break;
1202 out:
1203 /* returning NET_RX_DROP will make the caller function kfree the skb */
1204 return NET_RX_DROP;
1207 int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
1209 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1210 struct roam_adv_packet *roam_adv_packet;
1211 struct orig_node *orig_node;
1212 struct ethhdr *ethhdr;
1214 /* drop packet if it has not necessary minimum size */
1215 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
1216 goto out;
1218 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1220 /* packet with unicast indication but broadcast recipient */
1221 if (is_broadcast_ether_addr(ethhdr->h_dest))
1222 goto out;
1224 /* packet with broadcast sender address */
1225 if (is_broadcast_ether_addr(ethhdr->h_source))
1226 goto out;
1228 roam_adv_packet = (struct roam_adv_packet *)skb->data;
1230 if (!is_my_mac(roam_adv_packet->dst))
1231 return route_unicast_packet(skb, recv_if);
1233 orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
1234 if (!orig_node)
1235 goto out;
1237 bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
1238 "(client %pM)\n", roam_adv_packet->src,
1239 roam_adv_packet->client);
1241 tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
1242 atomic_read(&orig_node->last_ttvn) + 1, true, false);
1244 /* Roaming phase starts: I have new information but the ttvn has not
1245 * been incremented yet. This flag will make me check all the incoming
1246 * packets for the correct destination. */
1247 bat_priv->tt_poss_change = true;
1249 orig_node_free_ref(orig_node);
1250 out:
1251 /* returning NET_RX_DROP will make the caller function kfree the skb */
1252 return NET_RX_DROP;
1255 /* find a suitable router for this originator, and use
1256 * bonding if possible. increases the found neighbors
1257 * refcount.*/
1258 struct neigh_node *find_router(struct bat_priv *bat_priv,
1259 struct orig_node *orig_node,
1260 const struct hard_iface *recv_if)
1262 struct orig_node *primary_orig_node;
1263 struct orig_node *router_orig;
1264 struct neigh_node *router;
1265 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1266 int bonding_enabled;
1268 if (!orig_node)
1269 return NULL;
1271 router = orig_node_get_router(orig_node);
1272 if (!router)
1273 goto err;
1275 /* without bonding, the first node should
1276 * always choose the default router. */
1277 bonding_enabled = atomic_read(&bat_priv->bonding);
1279 rcu_read_lock();
1280 /* select default router to output */
1281 router_orig = router->orig_node;
1282 if (!router_orig)
1283 goto err_unlock;
1285 if ((!recv_if) && (!bonding_enabled))
1286 goto return_router;
1288 /* if we have something in the primary_addr, we can search
1289 * for a potential bonding candidate. */
1290 if (compare_eth(router_orig->primary_addr, zero_mac))
1291 goto return_router;
1293 /* find the orig_node which has the primary interface. might
1294 * even be the same as our router_orig in many cases */
1296 if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
1297 primary_orig_node = router_orig;
1298 } else {
1299 primary_orig_node = orig_hash_find(bat_priv,
1300 router_orig->primary_addr);
1301 if (!primary_orig_node)
1302 goto return_router;
1304 orig_node_free_ref(primary_orig_node);
1307 /* with less than 2 candidates, we can't do any
1308 * bonding and prefer the original router. */
1309 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
1310 goto return_router;
1312 /* all nodes between should choose a candidate which
1313 * is is not on the interface where the packet came
1314 * in. */
1316 neigh_node_free_ref(router);
1318 if (bonding_enabled)
1319 router = find_bond_router(primary_orig_node, recv_if);
1320 else
1321 router = find_ifalter_router(primary_orig_node, recv_if);
1323 return_router:
1324 if (router && router->if_incoming->if_status != IF_ACTIVE)
1325 goto err_unlock;
1327 rcu_read_unlock();
1328 return router;
1329 err_unlock:
1330 rcu_read_unlock();
1331 err:
1332 if (router)
1333 neigh_node_free_ref(router);
1334 return NULL;
1337 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1339 struct ethhdr *ethhdr;
1341 /* drop packet if it has not necessary minimum size */
1342 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1343 return -1;
1345 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1347 /* packet with unicast indication but broadcast recipient */
1348 if (is_broadcast_ether_addr(ethhdr->h_dest))
1349 return -1;
1351 /* packet with broadcast sender address */
1352 if (is_broadcast_ether_addr(ethhdr->h_source))
1353 return -1;
1355 /* not for me */
1356 if (!is_my_mac(ethhdr->h_dest))
1357 return -1;
1359 return 0;
1362 int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1364 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1365 struct orig_node *orig_node = NULL;
1366 struct neigh_node *neigh_node = NULL;
1367 struct unicast_packet *unicast_packet;
1368 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1369 int ret = NET_RX_DROP;
1370 struct sk_buff *new_skb;
1372 unicast_packet = (struct unicast_packet *)skb->data;
1374 /* TTL exceeded */
1375 if (unicast_packet->ttl < 2) {
1376 pr_debug("Warning - can't forward unicast packet from %pM to "
1377 "%pM: ttl exceeded\n", ethhdr->h_source,
1378 unicast_packet->dest);
1379 goto out;
1382 /* get routing information */
1383 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1385 if (!orig_node)
1386 goto out;
1388 /* find_router() increases neigh_nodes refcount if found. */
1389 neigh_node = find_router(bat_priv, orig_node, recv_if);
1391 if (!neigh_node)
1392 goto out;
1394 /* create a copy of the skb, if needed, to modify it. */
1395 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1396 goto out;
1398 unicast_packet = (struct unicast_packet *)skb->data;
1400 if (unicast_packet->packet_type == BAT_UNICAST &&
1401 atomic_read(&bat_priv->fragmentation) &&
1402 skb->len > neigh_node->if_incoming->net_dev->mtu) {
1403 ret = frag_send_skb(skb, bat_priv,
1404 neigh_node->if_incoming, neigh_node->addr);
1405 goto out;
1408 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1409 frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
1411 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1413 if (ret == NET_RX_DROP)
1414 goto out;
1416 /* packet was buffered for late merge */
1417 if (!new_skb) {
1418 ret = NET_RX_SUCCESS;
1419 goto out;
1422 skb = new_skb;
1423 unicast_packet = (struct unicast_packet *)skb->data;
1426 /* decrement ttl */
1427 unicast_packet->ttl--;
1429 /* route it */
1430 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1431 ret = NET_RX_SUCCESS;
1433 out:
1434 if (neigh_node)
1435 neigh_node_free_ref(neigh_node);
1436 if (orig_node)
1437 orig_node_free_ref(orig_node);
1438 return ret;
1441 static int check_unicast_ttvn(struct bat_priv *bat_priv,
1442 struct sk_buff *skb) {
1443 uint8_t curr_ttvn;
1444 struct orig_node *orig_node;
1445 struct ethhdr *ethhdr;
1446 struct hard_iface *primary_if;
1447 struct unicast_packet *unicast_packet;
1448 bool tt_poss_change;
1450 /* I could need to modify it */
1451 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
1452 return 0;
1454 unicast_packet = (struct unicast_packet *)skb->data;
1456 if (is_my_mac(unicast_packet->dest)) {
1457 tt_poss_change = bat_priv->tt_poss_change;
1458 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1459 } else {
1460 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1462 if (!orig_node)
1463 return 0;
1465 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
1466 tt_poss_change = orig_node->tt_poss_change;
1467 orig_node_free_ref(orig_node);
1470 /* Check whether I have to reroute the packet */
1471 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
1472 /* Linearize the skb before accessing it */
1473 if (skb_linearize(skb) < 0)
1474 return 0;
1476 ethhdr = (struct ethhdr *)(skb->data +
1477 sizeof(struct unicast_packet));
1478 orig_node = transtable_search(bat_priv, NULL, ethhdr->h_dest);
1480 if (!orig_node) {
1481 if (!is_my_client(bat_priv, ethhdr->h_dest))
1482 return 0;
1483 primary_if = primary_if_get_selected(bat_priv);
1484 if (!primary_if)
1485 return 0;
1486 memcpy(unicast_packet->dest,
1487 primary_if->net_dev->dev_addr, ETH_ALEN);
1488 hardif_free_ref(primary_if);
1489 } else {
1490 memcpy(unicast_packet->dest, orig_node->orig,
1491 ETH_ALEN);
1492 curr_ttvn = (uint8_t)
1493 atomic_read(&orig_node->last_ttvn);
1494 orig_node_free_ref(orig_node);
1497 bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
1498 "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
1499 "%pM\n", unicast_packet->ttvn, curr_ttvn,
1500 ethhdr->h_dest, unicast_packet->dest);
1502 unicast_packet->ttvn = curr_ttvn;
1504 return 1;
1507 int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1509 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1510 struct unicast_packet *unicast_packet;
1511 int hdr_size = sizeof(*unicast_packet);
1513 if (check_unicast_packet(skb, hdr_size) < 0)
1514 return NET_RX_DROP;
1516 if (!check_unicast_ttvn(bat_priv, skb))
1517 return NET_RX_DROP;
1519 unicast_packet = (struct unicast_packet *)skb->data;
1521 /* packet for me */
1522 if (is_my_mac(unicast_packet->dest)) {
1523 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1524 return NET_RX_SUCCESS;
1527 return route_unicast_packet(skb, recv_if);
1530 int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1532 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1533 struct unicast_frag_packet *unicast_packet;
1534 int hdr_size = sizeof(*unicast_packet);
1535 struct sk_buff *new_skb = NULL;
1536 int ret;
1538 if (check_unicast_packet(skb, hdr_size) < 0)
1539 return NET_RX_DROP;
1541 if (!check_unicast_ttvn(bat_priv, skb))
1542 return NET_RX_DROP;
1544 unicast_packet = (struct unicast_frag_packet *)skb->data;
1546 /* packet for me */
1547 if (is_my_mac(unicast_packet->dest)) {
1549 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1551 if (ret == NET_RX_DROP)
1552 return NET_RX_DROP;
1554 /* packet was buffered for late merge */
1555 if (!new_skb)
1556 return NET_RX_SUCCESS;
1558 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1559 sizeof(struct unicast_packet));
1560 return NET_RX_SUCCESS;
1563 return route_unicast_packet(skb, recv_if);
1567 int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1569 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1570 struct orig_node *orig_node = NULL;
1571 struct bcast_packet *bcast_packet;
1572 struct ethhdr *ethhdr;
1573 int hdr_size = sizeof(*bcast_packet);
1574 int ret = NET_RX_DROP;
1575 int32_t seq_diff;
1577 /* drop packet if it has not necessary minimum size */
1578 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1579 goto out;
1581 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1583 /* packet with broadcast indication but unicast recipient */
1584 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1585 goto out;
1587 /* packet with broadcast sender address */
1588 if (is_broadcast_ether_addr(ethhdr->h_source))
1589 goto out;
1591 /* ignore broadcasts sent by myself */
1592 if (is_my_mac(ethhdr->h_source))
1593 goto out;
1595 bcast_packet = (struct bcast_packet *)skb->data;
1597 /* ignore broadcasts originated by myself */
1598 if (is_my_mac(bcast_packet->orig))
1599 goto out;
1601 if (bcast_packet->ttl < 2)
1602 goto out;
1604 orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
1606 if (!orig_node)
1607 goto out;
1609 spin_lock_bh(&orig_node->bcast_seqno_lock);
1611 /* check whether the packet is a duplicate */
1612 if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1613 ntohl(bcast_packet->seqno)))
1614 goto spin_unlock;
1616 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1618 /* check whether the packet is old and the host just restarted. */
1619 if (window_protected(bat_priv, seq_diff,
1620 &orig_node->bcast_seqno_reset))
1621 goto spin_unlock;
1623 /* mark broadcast in flood history, update window position
1624 * if required. */
1625 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1626 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1628 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1630 /* rebroadcast packet */
1631 add_bcast_packet_to_list(bat_priv, skb, 1);
1633 /* broadcast for me */
1634 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1635 ret = NET_RX_SUCCESS;
1636 goto out;
1638 spin_unlock:
1639 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1640 out:
1641 if (orig_node)
1642 orig_node_free_ref(orig_node);
1643 return ret;
1646 int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1648 struct vis_packet *vis_packet;
1649 struct ethhdr *ethhdr;
1650 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1651 int hdr_size = sizeof(*vis_packet);
1653 /* keep skb linear */
1654 if (skb_linearize(skb) < 0)
1655 return NET_RX_DROP;
1657 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1658 return NET_RX_DROP;
1660 vis_packet = (struct vis_packet *)skb->data;
1661 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1663 /* not for me */
1664 if (!is_my_mac(ethhdr->h_dest))
1665 return NET_RX_DROP;
1667 /* ignore own packets */
1668 if (is_my_mac(vis_packet->vis_orig))
1669 return NET_RX_DROP;
1671 if (is_my_mac(vis_packet->sender_orig))
1672 return NET_RX_DROP;
1674 switch (vis_packet->vis_type) {
1675 case VIS_TYPE_SERVER_SYNC:
1676 receive_server_sync_packet(bat_priv, vis_packet,
1677 skb_headlen(skb));
1678 break;
1680 case VIS_TYPE_CLIENT_UPDATE:
1681 receive_client_update_packet(bat_priv, vis_packet,
1682 skb_headlen(skb));
1683 break;
1685 default: /* ignore unknown packet */
1686 break;
1689 /* We take a copy of the data in the packet, so we should
1690 always free the skbuf. */
1691 return NET_RX_DROP;