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
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"
33 #include "aggregation.h"
34 #include "gateway_common.h"
35 #include "gateway_client.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;
49 for (i = 0; i < hash->size; i++) {
50 head = &hash->table[i];
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);
67 static void update_transtable(struct bat_priv *bat_priv,
68 struct orig_node *orig_node,
69 const unsigned char *tt_buff,
70 uint8_t tt_num_changes, uint8_t ttvn,
73 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
74 bool full_table = true;
76 /* the ttvn increased by one -> we can apply the attached changes */
77 if (ttvn - orig_ttvn == 1) {
78 /* the OGM could not contain the changes because they were too
79 * many to fit in one frame or because they have already been
80 * sent TT_OGM_APPEND_MAX times. In this case send a tt
82 if (!tt_num_changes) {
87 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
88 (struct tt_change *)tt_buff);
90 /* Even if we received the crc into the OGM, we prefer
91 * to recompute it to spot any possible inconsistency
92 * in the global table */
93 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
94 /* Roaming phase is over: tables are in sync again. I can
96 orig_node->tt_poss_change = false;
98 /* if we missed more than one change or our tables are not
99 * in sync anymore -> request fresh tt data */
100 if (ttvn != orig_ttvn || orig_node->tt_crc != tt_crc) {
102 bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
103 "Need to retrieve the correct information "
104 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
105 "%u num_changes: %u)\n", orig_node->orig, ttvn,
106 orig_ttvn, tt_crc, orig_node->tt_crc,
108 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
115 static void update_route(struct bat_priv *bat_priv,
116 struct orig_node *orig_node,
117 struct neigh_node *neigh_node)
119 struct neigh_node *curr_router;
121 curr_router = orig_node_get_router(orig_node);
124 if ((curr_router) && (!neigh_node)) {
125 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
127 tt_global_del_orig(bat_priv, orig_node,
128 "Deleted route towards originator");
131 } else if ((!curr_router) && (neigh_node)) {
133 bat_dbg(DBG_ROUTES, bat_priv,
134 "Adding route towards: %pM (via %pM)\n",
135 orig_node->orig, neigh_node->addr);
137 } else if (neigh_node && curr_router) {
138 bat_dbg(DBG_ROUTES, bat_priv,
139 "Changing route towards: %pM "
140 "(now via %pM - was via %pM)\n",
141 orig_node->orig, neigh_node->addr,
146 neigh_node_free_ref(curr_router);
148 /* increase refcount of new best neighbor */
149 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
152 spin_lock_bh(&orig_node->neigh_list_lock);
153 rcu_assign_pointer(orig_node->router, neigh_node);
154 spin_unlock_bh(&orig_node->neigh_list_lock);
156 /* decrease refcount of previous best neighbor */
158 neigh_node_free_ref(curr_router);
161 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
162 struct neigh_node *neigh_node)
164 struct neigh_node *router = NULL;
169 router = orig_node_get_router(orig_node);
171 if (router != neigh_node)
172 update_route(bat_priv, orig_node, neigh_node);
176 neigh_node_free_ref(router);
179 static int is_bidirectional_neigh(struct orig_node *orig_node,
180 struct orig_node *orig_neigh_node,
181 struct batman_packet *batman_packet,
182 struct hard_iface *if_incoming)
184 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
185 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
186 struct hlist_node *node;
188 uint8_t orig_eq_count, neigh_rq_count, tq_own;
189 int tq_asym_penalty, ret = 0;
191 /* find corresponding one hop neighbor */
193 hlist_for_each_entry_rcu(tmp_neigh_node, node,
194 &orig_neigh_node->neigh_list, list) {
196 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
199 if (tmp_neigh_node->if_incoming != if_incoming)
202 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
205 neigh_node = tmp_neigh_node;
211 neigh_node = create_neighbor(orig_neigh_node,
213 orig_neigh_node->orig,
219 /* if orig_node is direct neighbour update neigh_node last_valid */
220 if (orig_node == orig_neigh_node)
221 neigh_node->last_valid = jiffies;
223 orig_node->last_valid = jiffies;
225 /* find packet count of corresponding one hop neighbor */
226 spin_lock_bh(&orig_node->ogm_cnt_lock);
227 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
228 neigh_rq_count = neigh_node->real_packet_count;
229 spin_unlock_bh(&orig_node->ogm_cnt_lock);
231 /* pay attention to not get a value bigger than 100 % */
232 total_count = (orig_eq_count > neigh_rq_count ?
233 neigh_rq_count : orig_eq_count);
235 /* if we have too few packets (too less data) we set tq_own to zero */
236 /* if we receive too few packets it is not considered bidirectional */
237 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
238 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
241 /* neigh_node->real_packet_count is never zero as we
242 * only purge old information when getting new
244 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
247 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
248 * affect the nearly-symmetric links only a little, but
249 * punishes asymmetric links more. This will give a value
250 * between 0 and TQ_MAX_VALUE
252 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
253 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
254 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
255 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
256 (TQ_LOCAL_WINDOW_SIZE *
257 TQ_LOCAL_WINDOW_SIZE *
258 TQ_LOCAL_WINDOW_SIZE);
260 batman_packet->tq = ((batman_packet->tq * tq_own * tq_asym_penalty) /
261 (TQ_MAX_VALUE * TQ_MAX_VALUE));
263 bat_dbg(DBG_BATMAN, bat_priv,
265 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
266 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
268 orig_node->orig, orig_neigh_node->orig, total_count,
269 neigh_rq_count, tq_own, tq_asym_penalty, batman_packet->tq);
271 /* if link has the minimum required transmission quality
272 * consider it bidirectional */
273 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
278 neigh_node_free_ref(neigh_node);
282 /* caller must hold the neigh_list_lock */
283 void bonding_candidate_del(struct orig_node *orig_node,
284 struct neigh_node *neigh_node)
286 /* this neighbor is not part of our candidate list */
287 if (list_empty(&neigh_node->bonding_list))
290 list_del_rcu(&neigh_node->bonding_list);
291 INIT_LIST_HEAD(&neigh_node->bonding_list);
292 neigh_node_free_ref(neigh_node);
293 atomic_dec(&orig_node->bond_candidates);
299 static void bonding_candidate_add(struct orig_node *orig_node,
300 struct neigh_node *neigh_node)
302 struct hlist_node *node;
303 struct neigh_node *tmp_neigh_node, *router = NULL;
304 uint8_t interference_candidate = 0;
306 spin_lock_bh(&orig_node->neigh_list_lock);
308 /* only consider if it has the same primary address ... */
309 if (!compare_eth(orig_node->orig,
310 neigh_node->orig_node->primary_addr))
313 router = orig_node_get_router(orig_node);
317 /* ... and is good enough to be considered */
318 if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
322 * check if we have another candidate with the same mac address or
323 * interface. If we do, we won't select this candidate because of
324 * possible interference.
326 hlist_for_each_entry_rcu(tmp_neigh_node, node,
327 &orig_node->neigh_list, list) {
329 if (tmp_neigh_node == neigh_node)
332 /* we only care if the other candidate is even
333 * considered as candidate. */
334 if (list_empty(&tmp_neigh_node->bonding_list))
337 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
338 (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
339 interference_candidate = 1;
344 /* don't care further if it is an interference candidate */
345 if (interference_candidate)
348 /* this neighbor already is part of our candidate list */
349 if (!list_empty(&neigh_node->bonding_list))
352 if (!atomic_inc_not_zero(&neigh_node->refcount))
355 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
356 atomic_inc(&orig_node->bond_candidates);
360 bonding_candidate_del(orig_node, neigh_node);
363 spin_unlock_bh(&orig_node->neigh_list_lock);
366 neigh_node_free_ref(router);
369 /* copy primary address for bonding */
370 static void bonding_save_primary(const struct orig_node *orig_node,
371 struct orig_node *orig_neigh_node,
372 const struct batman_packet *batman_packet)
374 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
377 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
380 static void update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
381 const struct ethhdr *ethhdr,
382 const struct batman_packet *batman_packet,
383 struct hard_iface *if_incoming,
384 const unsigned char *tt_buff, int is_duplicate)
386 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
387 struct neigh_node *router = NULL;
388 struct orig_node *orig_node_tmp;
389 struct hlist_node *node;
390 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
392 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
393 "Searching and updating originator entry of received packet\n");
396 hlist_for_each_entry_rcu(tmp_neigh_node, node,
397 &orig_node->neigh_list, list) {
398 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
399 (tmp_neigh_node->if_incoming == if_incoming) &&
400 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
402 neigh_node_free_ref(neigh_node);
403 neigh_node = tmp_neigh_node;
410 spin_lock_bh(&tmp_neigh_node->tq_lock);
411 ring_buffer_set(tmp_neigh_node->tq_recv,
412 &tmp_neigh_node->tq_index, 0);
413 tmp_neigh_node->tq_avg =
414 ring_buffer_avg(tmp_neigh_node->tq_recv);
415 spin_unlock_bh(&tmp_neigh_node->tq_lock);
419 struct orig_node *orig_tmp;
421 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
425 neigh_node = create_neighbor(orig_node, orig_tmp,
426 ethhdr->h_source, if_incoming);
428 orig_node_free_ref(orig_tmp);
432 bat_dbg(DBG_BATMAN, bat_priv,
433 "Updating existing last-hop neighbor of originator\n");
437 orig_node->flags = batman_packet->flags;
438 neigh_node->last_valid = jiffies;
440 spin_lock_bh(&neigh_node->tq_lock);
441 ring_buffer_set(neigh_node->tq_recv,
442 &neigh_node->tq_index,
444 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
445 spin_unlock_bh(&neigh_node->tq_lock);
448 orig_node->last_ttl = batman_packet->ttl;
449 neigh_node->last_ttl = batman_packet->ttl;
452 bonding_candidate_add(orig_node, neigh_node);
454 /* if this neighbor already is our next hop there is nothing
456 router = orig_node_get_router(orig_node);
457 if (router == neigh_node)
460 /* if this neighbor does not offer a better TQ we won't consider it */
461 if (router && (router->tq_avg > neigh_node->tq_avg))
464 /* if the TQ is the same and the link not more symetric we
465 * won't consider it either */
466 if (router && (neigh_node->tq_avg == router->tq_avg)) {
467 orig_node_tmp = router->orig_node;
468 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
470 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
471 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
473 orig_node_tmp = neigh_node->orig_node;
474 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
475 bcast_own_sum_neigh =
476 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
477 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
479 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
483 update_routes(bat_priv, orig_node, neigh_node);
486 /* I have to check for transtable changes only if the OGM has been
487 * sent through a primary interface */
488 if (((batman_packet->orig != ethhdr->h_source) &&
489 (batman_packet->ttl > 2)) ||
490 (batman_packet->flags & PRIMARIES_FIRST_HOP))
491 update_transtable(bat_priv, orig_node, tt_buff,
492 batman_packet->tt_num_changes,
494 batman_packet->tt_crc);
496 if (orig_node->gw_flags != batman_packet->gw_flags)
497 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
499 orig_node->gw_flags = batman_packet->gw_flags;
501 /* restart gateway selection if fast or late switching was enabled */
502 if ((orig_node->gw_flags) &&
503 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
504 (atomic_read(&bat_priv->gw_sel_class) > 2))
505 gw_check_election(bat_priv, orig_node);
513 neigh_node_free_ref(neigh_node);
515 neigh_node_free_ref(router);
518 /* checks whether the host restarted and is in the protection time.
520 * 0 if the packet is to be accepted
521 * 1 if the packet is to be ignored.
523 static int window_protected(struct bat_priv *bat_priv,
524 int32_t seq_num_diff,
525 unsigned long *last_reset)
527 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
528 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
529 if (time_after(jiffies, *last_reset +
530 msecs_to_jiffies(RESET_PROTECTION_MS))) {
532 *last_reset = jiffies;
533 bat_dbg(DBG_BATMAN, bat_priv,
534 "old packet received, start protection\n");
543 /* processes a batman packet for all interfaces, adjusts the sequence number and
544 * finds out whether it is a duplicate.
546 * 1 the packet is a duplicate
547 * 0 the packet has not yet been received
548 * -1 the packet is old and has been received while the seqno window
549 * was protected. Caller should drop it.
551 static int count_real_packets(const struct ethhdr *ethhdr,
552 const struct batman_packet *batman_packet,
553 const struct hard_iface *if_incoming)
555 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
556 struct orig_node *orig_node;
557 struct neigh_node *tmp_neigh_node;
558 struct hlist_node *node;
559 int is_duplicate = 0;
562 int set_mark, ret = -1;
564 orig_node = get_orig_node(bat_priv, batman_packet->orig);
568 spin_lock_bh(&orig_node->ogm_cnt_lock);
569 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
571 /* signalize caller that the packet is to be dropped. */
572 if (window_protected(bat_priv, seq_diff,
573 &orig_node->batman_seqno_reset))
577 hlist_for_each_entry_rcu(tmp_neigh_node, node,
578 &orig_node->neigh_list, list) {
580 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
581 orig_node->last_real_seqno,
582 batman_packet->seqno);
584 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
585 (tmp_neigh_node->if_incoming == if_incoming))
590 /* if the window moved, set the update flag. */
591 need_update |= bit_get_packet(bat_priv,
592 tmp_neigh_node->real_bits,
595 tmp_neigh_node->real_packet_count =
596 bit_packet_count(tmp_neigh_node->real_bits);
601 bat_dbg(DBG_BATMAN, bat_priv,
602 "updating last_seqno: old %d, new %d\n",
603 orig_node->last_real_seqno, batman_packet->seqno);
604 orig_node->last_real_seqno = batman_packet->seqno;
610 spin_unlock_bh(&orig_node->ogm_cnt_lock);
611 orig_node_free_ref(orig_node);
615 void receive_bat_packet(const struct ethhdr *ethhdr,
616 struct batman_packet *batman_packet,
617 const unsigned char *tt_buff,
618 struct hard_iface *if_incoming)
620 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
621 struct hard_iface *hard_iface;
622 struct orig_node *orig_neigh_node, *orig_node;
623 struct neigh_node *router = NULL, *router_router = NULL;
624 struct neigh_node *orig_neigh_router = NULL;
625 int has_directlink_flag;
626 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
627 int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
629 uint32_t if_incoming_seqno;
631 /* Silently drop when the batman packet is actually not a
634 * This might happen if a packet is padded (e.g. Ethernet has a
635 * minimum frame length of 64 byte) and the aggregation interprets
636 * it as an additional length.
638 * TODO: A more sane solution would be to have a bit in the
639 * batman_packet to detect whether the packet is the last
640 * packet in an aggregation. Here we expect that the padding
641 * is always zero (or not 0x01)
643 if (batman_packet->packet_type != BAT_PACKET)
646 /* could be changed by schedule_own_packet() */
647 if_incoming_seqno = atomic_read(&if_incoming->seqno);
649 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
651 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
652 batman_packet->orig) ? 1 : 0);
654 bat_dbg(DBG_BATMAN, bat_priv,
655 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
656 "(from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, "
657 "crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
658 ethhdr->h_source, if_incoming->net_dev->name,
659 if_incoming->net_dev->dev_addr, batman_packet->orig,
660 batman_packet->prev_sender, batman_packet->seqno,
661 batman_packet->ttvn, batman_packet->tt_crc,
662 batman_packet->tt_num_changes, batman_packet->tq,
663 batman_packet->ttl, batman_packet->version,
664 has_directlink_flag);
667 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
668 if (hard_iface->if_status != IF_ACTIVE)
671 if (hard_iface->soft_iface != if_incoming->soft_iface)
674 if (compare_eth(ethhdr->h_source,
675 hard_iface->net_dev->dev_addr))
678 if (compare_eth(batman_packet->orig,
679 hard_iface->net_dev->dev_addr))
682 if (compare_eth(batman_packet->prev_sender,
683 hard_iface->net_dev->dev_addr))
686 if (is_broadcast_ether_addr(ethhdr->h_source))
691 if (batman_packet->version != COMPAT_VERSION) {
692 bat_dbg(DBG_BATMAN, bat_priv,
693 "Drop packet: incompatible batman version (%i)\n",
694 batman_packet->version);
699 bat_dbg(DBG_BATMAN, bat_priv,
700 "Drop packet: received my own broadcast (sender: %pM"
707 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
708 "ignoring all packets with broadcast source addr (sender: %pM"
709 ")\n", ethhdr->h_source);
717 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
718 if (!orig_neigh_node)
721 /* neighbor has to indicate direct link and it has to
722 * come via the corresponding interface */
723 /* save packet seqno for bidirectional check */
724 if (has_directlink_flag &&
725 compare_eth(if_incoming->net_dev->dev_addr,
726 batman_packet->orig)) {
727 offset = if_incoming->if_num * NUM_WORDS;
729 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
730 word = &(orig_neigh_node->bcast_own[offset]);
732 if_incoming_seqno - batman_packet->seqno - 2);
733 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
734 bit_packet_count(word);
735 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
738 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
739 "originator packet from myself (via neighbor)\n");
740 orig_node_free_ref(orig_neigh_node);
745 bat_dbg(DBG_BATMAN, bat_priv,
746 "Drop packet: ignoring all rebroadcast echos (sender: "
747 "%pM)\n", ethhdr->h_source);
751 orig_node = get_orig_node(bat_priv, batman_packet->orig);
755 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
757 if (is_duplicate == -1) {
758 bat_dbg(DBG_BATMAN, bat_priv,
759 "Drop packet: packet within seqno protection time "
760 "(sender: %pM)\n", ethhdr->h_source);
764 if (batman_packet->tq == 0) {
765 bat_dbg(DBG_BATMAN, bat_priv,
766 "Drop packet: originator packet with tq equal 0\n");
770 router = orig_node_get_router(orig_node);
772 router_router = orig_node_get_router(router->orig_node);
774 /* avoid temporary routing loops */
775 if (router && router_router &&
776 (compare_eth(router->addr, batman_packet->prev_sender)) &&
777 !(compare_eth(batman_packet->orig, batman_packet->prev_sender)) &&
778 (compare_eth(router->addr, router_router->addr))) {
779 bat_dbg(DBG_BATMAN, bat_priv,
780 "Drop packet: ignoring all rebroadcast packets that "
781 "may make me loop (sender: %pM)\n", ethhdr->h_source);
785 /* if sender is a direct neighbor the sender mac equals
787 orig_neigh_node = (is_single_hop_neigh ?
789 get_orig_node(bat_priv, ethhdr->h_source));
790 if (!orig_neigh_node)
793 orig_neigh_router = orig_node_get_router(orig_neigh_node);
795 /* drop packet if sender is not a direct neighbor and if we
796 * don't route towards it */
797 if (!is_single_hop_neigh && (!orig_neigh_router)) {
798 bat_dbg(DBG_BATMAN, bat_priv,
799 "Drop packet: OGM via unknown neighbor!\n");
803 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
804 batman_packet, if_incoming);
806 bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
808 /* update ranking if it is not a duplicate or has the same
809 * seqno and similar ttl as the non-duplicate */
810 if (is_bidirectional &&
812 ((orig_node->last_real_seqno == batman_packet->seqno) &&
813 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
814 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
815 if_incoming, tt_buff, is_duplicate);
817 /* is single hop (direct) neighbor */
818 if (is_single_hop_neigh) {
820 /* mark direct link on incoming interface */
821 schedule_forward_packet(orig_node, ethhdr, batman_packet,
824 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
825 "rebroadcast neighbor packet with direct link flag\n");
829 /* multihop originator */
830 if (!is_bidirectional) {
831 bat_dbg(DBG_BATMAN, bat_priv,
832 "Drop packet: not received via bidirectional link\n");
837 bat_dbg(DBG_BATMAN, bat_priv,
838 "Drop packet: duplicate packet received\n");
842 bat_dbg(DBG_BATMAN, bat_priv,
843 "Forwarding packet: rebroadcast originator packet\n");
844 schedule_forward_packet(orig_node, ethhdr, batman_packet,
848 if ((orig_neigh_node) && (!is_single_hop_neigh))
849 orig_node_free_ref(orig_neigh_node);
852 neigh_node_free_ref(router);
854 neigh_node_free_ref(router_router);
855 if (orig_neigh_router)
856 neigh_node_free_ref(orig_neigh_router);
858 orig_node_free_ref(orig_node);
861 int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
863 struct ethhdr *ethhdr;
865 /* drop packet if it has not necessary minimum size */
866 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
869 ethhdr = (struct ethhdr *)skb_mac_header(skb);
871 /* packet with broadcast indication but unicast recipient */
872 if (!is_broadcast_ether_addr(ethhdr->h_dest))
875 /* packet with broadcast sender address */
876 if (is_broadcast_ether_addr(ethhdr->h_source))
879 /* create a copy of the skb, if needed, to modify it. */
880 if (skb_cow(skb, 0) < 0)
883 /* keep skb linear */
884 if (skb_linearize(skb) < 0)
887 ethhdr = (struct ethhdr *)skb_mac_header(skb);
889 receive_aggr_bat_packet(ethhdr,
895 return NET_RX_SUCCESS;
898 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
899 struct sk_buff *skb, size_t icmp_len)
901 struct hard_iface *primary_if = NULL;
902 struct orig_node *orig_node = NULL;
903 struct neigh_node *router = NULL;
904 struct icmp_packet_rr *icmp_packet;
905 int ret = NET_RX_DROP;
907 icmp_packet = (struct icmp_packet_rr *)skb->data;
909 /* add data to device queue */
910 if (icmp_packet->msg_type != ECHO_REQUEST) {
911 bat_socket_receive_packet(icmp_packet, icmp_len);
915 primary_if = primary_if_get_selected(bat_priv);
919 /* answer echo request (ping) */
920 /* get routing information */
921 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
925 router = orig_node_get_router(orig_node);
929 /* create a copy of the skb, if needed, to modify it. */
930 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
933 icmp_packet = (struct icmp_packet_rr *)skb->data;
935 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
936 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
937 icmp_packet->msg_type = ECHO_REPLY;
938 icmp_packet->ttl = TTL;
940 send_skb_packet(skb, router->if_incoming, router->addr);
941 ret = NET_RX_SUCCESS;
945 hardif_free_ref(primary_if);
947 neigh_node_free_ref(router);
949 orig_node_free_ref(orig_node);
953 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
956 struct hard_iface *primary_if = NULL;
957 struct orig_node *orig_node = NULL;
958 struct neigh_node *router = NULL;
959 struct icmp_packet *icmp_packet;
960 int ret = NET_RX_DROP;
962 icmp_packet = (struct icmp_packet *)skb->data;
964 /* send TTL exceeded if packet is an echo request (traceroute) */
965 if (icmp_packet->msg_type != ECHO_REQUEST) {
966 pr_debug("Warning - can't forward icmp packet from %pM to "
967 "%pM: ttl exceeded\n", icmp_packet->orig,
972 primary_if = primary_if_get_selected(bat_priv);
976 /* get routing information */
977 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
981 router = orig_node_get_router(orig_node);
985 /* create a copy of the skb, if needed, to modify it. */
986 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
989 icmp_packet = (struct icmp_packet *)skb->data;
991 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
992 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
993 icmp_packet->msg_type = TTL_EXCEEDED;
994 icmp_packet->ttl = TTL;
996 send_skb_packet(skb, router->if_incoming, router->addr);
997 ret = NET_RX_SUCCESS;
1001 hardif_free_ref(primary_if);
1003 neigh_node_free_ref(router);
1005 orig_node_free_ref(orig_node);
1010 int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1012 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1013 struct icmp_packet_rr *icmp_packet;
1014 struct ethhdr *ethhdr;
1015 struct orig_node *orig_node = NULL;
1016 struct neigh_node *router = NULL;
1017 int hdr_size = sizeof(struct icmp_packet);
1018 int ret = NET_RX_DROP;
1021 * we truncate all incoming icmp packets if they don't match our size
1023 if (skb->len >= sizeof(struct icmp_packet_rr))
1024 hdr_size = sizeof(struct icmp_packet_rr);
1026 /* drop packet if it has not necessary minimum size */
1027 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1030 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1032 /* packet with unicast indication but broadcast recipient */
1033 if (is_broadcast_ether_addr(ethhdr->h_dest))
1036 /* packet with broadcast sender address */
1037 if (is_broadcast_ether_addr(ethhdr->h_source))
1041 if (!is_my_mac(ethhdr->h_dest))
1044 icmp_packet = (struct icmp_packet_rr *)skb->data;
1046 /* add record route information if not full */
1047 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
1048 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1049 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1050 ethhdr->h_dest, ETH_ALEN);
1051 icmp_packet->rr_cur++;
1055 if (is_my_mac(icmp_packet->dst))
1056 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1059 if (icmp_packet->ttl < 2)
1060 return recv_icmp_ttl_exceeded(bat_priv, skb);
1062 /* get routing information */
1063 orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
1067 router = orig_node_get_router(orig_node);
1071 /* create a copy of the skb, if needed, to modify it. */
1072 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1075 icmp_packet = (struct icmp_packet_rr *)skb->data;
1081 send_skb_packet(skb, router->if_incoming, router->addr);
1082 ret = NET_RX_SUCCESS;
1086 neigh_node_free_ref(router);
1088 orig_node_free_ref(orig_node);
1092 /* In the bonding case, send the packets in a round
1093 * robin fashion over the remaining interfaces.
1095 * This method rotates the bonding list and increases the
1096 * returned router's refcount. */
1097 static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
1098 const struct hard_iface *recv_if)
1100 struct neigh_node *tmp_neigh_node;
1101 struct neigh_node *router = NULL, *first_candidate = NULL;
1104 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1106 if (!first_candidate)
1107 first_candidate = tmp_neigh_node;
1109 /* recv_if == NULL on the first node. */
1110 if (tmp_neigh_node->if_incoming == recv_if)
1113 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1116 router = tmp_neigh_node;
1120 /* use the first candidate if nothing was found. */
1121 if (!router && first_candidate &&
1122 atomic_inc_not_zero(&first_candidate->refcount))
1123 router = first_candidate;
1128 /* selected should point to the next element
1129 * after the current router */
1130 spin_lock_bh(&primary_orig->neigh_list_lock);
1131 /* this is a list_move(), which unfortunately
1132 * does not exist as rcu version */
1133 list_del_rcu(&primary_orig->bond_list);
1134 list_add_rcu(&primary_orig->bond_list,
1135 &router->bonding_list);
1136 spin_unlock_bh(&primary_orig->neigh_list_lock);
1143 /* Interface Alternating: Use the best of the
1144 * remaining candidates which are not using
1147 * Increases the returned router's refcount */
1148 static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
1149 const struct hard_iface *recv_if)
1151 struct neigh_node *tmp_neigh_node;
1152 struct neigh_node *router = NULL, *first_candidate = NULL;
1155 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1157 if (!first_candidate)
1158 first_candidate = tmp_neigh_node;
1160 /* recv_if == NULL on the first node. */
1161 if (tmp_neigh_node->if_incoming == recv_if)
1164 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1167 /* if we don't have a router yet
1168 * or this one is better, choose it. */
1170 (tmp_neigh_node->tq_avg > router->tq_avg)) {
1171 /* decrement refcount of
1172 * previously selected router */
1174 neigh_node_free_ref(router);
1176 router = tmp_neigh_node;
1177 atomic_inc_not_zero(&router->refcount);
1180 neigh_node_free_ref(tmp_neigh_node);
1183 /* use the first candidate if nothing was found. */
1184 if (!router && first_candidate &&
1185 atomic_inc_not_zero(&first_candidate->refcount))
1186 router = first_candidate;
1192 int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
1194 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1195 struct tt_query_packet *tt_query;
1196 struct ethhdr *ethhdr;
1198 /* drop packet if it has not necessary minimum size */
1199 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
1202 /* I could need to modify it */
1203 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
1206 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1208 /* packet with unicast indication but broadcast recipient */
1209 if (is_broadcast_ether_addr(ethhdr->h_dest))
1212 /* packet with broadcast sender address */
1213 if (is_broadcast_ether_addr(ethhdr->h_source))
1216 tt_query = (struct tt_query_packet *)skb->data;
1218 tt_query->tt_data = ntohs(tt_query->tt_data);
1220 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
1222 /* If we cannot provide an answer the tt_request is
1224 if (!send_tt_response(bat_priv, tt_query)) {
1225 bat_dbg(DBG_TT, bat_priv,
1226 "Routing TT_REQUEST to %pM [%c]\n",
1228 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1229 tt_query->tt_data = htons(tt_query->tt_data);
1230 return route_unicast_packet(skb, recv_if);
1234 /* packet needs to be linearised to access the TT changes */
1235 if (skb_linearize(skb) < 0)
1238 if (is_my_mac(tt_query->dst))
1239 handle_tt_response(bat_priv, tt_query);
1241 bat_dbg(DBG_TT, bat_priv,
1242 "Routing TT_RESPONSE to %pM [%c]\n",
1244 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1245 tt_query->tt_data = htons(tt_query->tt_data);
1246 return route_unicast_packet(skb, recv_if);
1252 /* returning NET_RX_DROP will make the caller function kfree the skb */
1256 int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
1258 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1259 struct roam_adv_packet *roam_adv_packet;
1260 struct orig_node *orig_node;
1261 struct ethhdr *ethhdr;
1263 /* drop packet if it has not necessary minimum size */
1264 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
1267 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1269 /* packet with unicast indication but broadcast recipient */
1270 if (is_broadcast_ether_addr(ethhdr->h_dest))
1273 /* packet with broadcast sender address */
1274 if (is_broadcast_ether_addr(ethhdr->h_source))
1277 roam_adv_packet = (struct roam_adv_packet *)skb->data;
1279 if (!is_my_mac(roam_adv_packet->dst))
1280 return route_unicast_packet(skb, recv_if);
1282 orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
1286 bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
1287 "(client %pM)\n", roam_adv_packet->src,
1288 roam_adv_packet->client);
1290 tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
1291 atomic_read(&orig_node->last_ttvn) + 1, true);
1293 /* Roaming phase starts: I have new information but the ttvn has not
1294 * been incremented yet. This flag will make me check all the incoming
1295 * packets for the correct destination. */
1296 bat_priv->tt_poss_change = true;
1298 orig_node_free_ref(orig_node);
1300 /* returning NET_RX_DROP will make the caller function kfree the skb */
1304 /* find a suitable router for this originator, and use
1305 * bonding if possible. increases the found neighbors
1307 struct neigh_node *find_router(struct bat_priv *bat_priv,
1308 struct orig_node *orig_node,
1309 const struct hard_iface *recv_if)
1311 struct orig_node *primary_orig_node;
1312 struct orig_node *router_orig;
1313 struct neigh_node *router;
1314 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1315 int bonding_enabled;
1320 router = orig_node_get_router(orig_node);
1324 /* without bonding, the first node should
1325 * always choose the default router. */
1326 bonding_enabled = atomic_read(&bat_priv->bonding);
1329 /* select default router to output */
1330 router_orig = router->orig_node;
1334 if ((!recv_if) && (!bonding_enabled))
1337 /* if we have something in the primary_addr, we can search
1338 * for a potential bonding candidate. */
1339 if (compare_eth(router_orig->primary_addr, zero_mac))
1342 /* find the orig_node which has the primary interface. might
1343 * even be the same as our router_orig in many cases */
1345 if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
1346 primary_orig_node = router_orig;
1348 primary_orig_node = orig_hash_find(bat_priv,
1349 router_orig->primary_addr);
1350 if (!primary_orig_node)
1353 orig_node_free_ref(primary_orig_node);
1356 /* with less than 2 candidates, we can't do any
1357 * bonding and prefer the original router. */
1358 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
1361 /* all nodes between should choose a candidate which
1362 * is is not on the interface where the packet came
1365 neigh_node_free_ref(router);
1367 if (bonding_enabled)
1368 router = find_bond_router(primary_orig_node, recv_if);
1370 router = find_ifalter_router(primary_orig_node, recv_if);
1373 if (router && router->if_incoming->if_status != IF_ACTIVE)
1382 neigh_node_free_ref(router);
1386 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1388 struct ethhdr *ethhdr;
1390 /* drop packet if it has not necessary minimum size */
1391 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1394 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1396 /* packet with unicast indication but broadcast recipient */
1397 if (is_broadcast_ether_addr(ethhdr->h_dest))
1400 /* packet with broadcast sender address */
1401 if (is_broadcast_ether_addr(ethhdr->h_source))
1405 if (!is_my_mac(ethhdr->h_dest))
1411 int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1413 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1414 struct orig_node *orig_node = NULL;
1415 struct neigh_node *neigh_node = NULL;
1416 struct unicast_packet *unicast_packet;
1417 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1418 int ret = NET_RX_DROP;
1419 struct sk_buff *new_skb;
1421 unicast_packet = (struct unicast_packet *)skb->data;
1424 if (unicast_packet->ttl < 2) {
1425 pr_debug("Warning - can't forward unicast packet from %pM to "
1426 "%pM: ttl exceeded\n", ethhdr->h_source,
1427 unicast_packet->dest);
1431 /* get routing information */
1432 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1437 /* find_router() increases neigh_nodes refcount if found. */
1438 neigh_node = find_router(bat_priv, orig_node, recv_if);
1443 /* create a copy of the skb, if needed, to modify it. */
1444 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1447 unicast_packet = (struct unicast_packet *)skb->data;
1449 if (unicast_packet->packet_type == BAT_UNICAST &&
1450 atomic_read(&bat_priv->fragmentation) &&
1451 skb->len > neigh_node->if_incoming->net_dev->mtu) {
1452 ret = frag_send_skb(skb, bat_priv,
1453 neigh_node->if_incoming, neigh_node->addr);
1457 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1458 frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
1460 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1462 if (ret == NET_RX_DROP)
1465 /* packet was buffered for late merge */
1467 ret = NET_RX_SUCCESS;
1472 unicast_packet = (struct unicast_packet *)skb->data;
1476 unicast_packet->ttl--;
1479 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1480 ret = NET_RX_SUCCESS;
1484 neigh_node_free_ref(neigh_node);
1486 orig_node_free_ref(orig_node);
1490 static int check_unicast_ttvn(struct bat_priv *bat_priv,
1491 struct sk_buff *skb) {
1493 struct orig_node *orig_node;
1494 struct ethhdr *ethhdr;
1495 struct hard_iface *primary_if;
1496 struct unicast_packet *unicast_packet;
1497 bool tt_poss_change;
1499 /* I could need to modify it */
1500 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
1503 unicast_packet = (struct unicast_packet *)skb->data;
1505 if (is_my_mac(unicast_packet->dest)) {
1506 tt_poss_change = bat_priv->tt_poss_change;
1507 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1509 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1514 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
1515 tt_poss_change = orig_node->tt_poss_change;
1516 orig_node_free_ref(orig_node);
1519 /* Check whether I have to reroute the packet */
1520 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
1521 /* Linearize the skb before accessing it */
1522 if (skb_linearize(skb) < 0)
1525 ethhdr = (struct ethhdr *)(skb->data +
1526 sizeof(struct unicast_packet));
1527 orig_node = transtable_search(bat_priv, ethhdr->h_dest);
1530 if (!is_my_client(bat_priv, ethhdr->h_dest))
1532 primary_if = primary_if_get_selected(bat_priv);
1535 memcpy(unicast_packet->dest,
1536 primary_if->net_dev->dev_addr, ETH_ALEN);
1537 hardif_free_ref(primary_if);
1539 memcpy(unicast_packet->dest, orig_node->orig,
1541 curr_ttvn = (uint8_t)
1542 atomic_read(&orig_node->last_ttvn);
1543 orig_node_free_ref(orig_node);
1546 bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
1547 "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
1548 "%pM\n", unicast_packet->ttvn, curr_ttvn,
1549 ethhdr->h_dest, unicast_packet->dest);
1551 unicast_packet->ttvn = curr_ttvn;
1556 int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1558 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1559 struct unicast_packet *unicast_packet;
1560 int hdr_size = sizeof(*unicast_packet);
1562 if (check_unicast_packet(skb, hdr_size) < 0)
1565 if (!check_unicast_ttvn(bat_priv, skb))
1568 unicast_packet = (struct unicast_packet *)skb->data;
1571 if (is_my_mac(unicast_packet->dest)) {
1572 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1573 return NET_RX_SUCCESS;
1576 return route_unicast_packet(skb, recv_if);
1579 int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1581 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1582 struct unicast_frag_packet *unicast_packet;
1583 int hdr_size = sizeof(*unicast_packet);
1584 struct sk_buff *new_skb = NULL;
1587 if (check_unicast_packet(skb, hdr_size) < 0)
1590 if (!check_unicast_ttvn(bat_priv, skb))
1593 unicast_packet = (struct unicast_frag_packet *)skb->data;
1596 if (is_my_mac(unicast_packet->dest)) {
1598 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1600 if (ret == NET_RX_DROP)
1603 /* packet was buffered for late merge */
1605 return NET_RX_SUCCESS;
1607 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1608 sizeof(struct unicast_packet));
1609 return NET_RX_SUCCESS;
1612 return route_unicast_packet(skb, recv_if);
1616 int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1618 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1619 struct orig_node *orig_node = NULL;
1620 struct bcast_packet *bcast_packet;
1621 struct ethhdr *ethhdr;
1622 int hdr_size = sizeof(*bcast_packet);
1623 int ret = NET_RX_DROP;
1626 /* drop packet if it has not necessary minimum size */
1627 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1630 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1632 /* packet with broadcast indication but unicast recipient */
1633 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1636 /* packet with broadcast sender address */
1637 if (is_broadcast_ether_addr(ethhdr->h_source))
1640 /* ignore broadcasts sent by myself */
1641 if (is_my_mac(ethhdr->h_source))
1644 bcast_packet = (struct bcast_packet *)skb->data;
1646 /* ignore broadcasts originated by myself */
1647 if (is_my_mac(bcast_packet->orig))
1650 if (bcast_packet->ttl < 2)
1653 orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
1658 spin_lock_bh(&orig_node->bcast_seqno_lock);
1660 /* check whether the packet is a duplicate */
1661 if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1662 ntohl(bcast_packet->seqno)))
1665 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1667 /* check whether the packet is old and the host just restarted. */
1668 if (window_protected(bat_priv, seq_diff,
1669 &orig_node->bcast_seqno_reset))
1672 /* mark broadcast in flood history, update window position
1674 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1675 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1677 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1679 /* rebroadcast packet */
1680 add_bcast_packet_to_list(bat_priv, skb, 1);
1682 /* broadcast for me */
1683 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1684 ret = NET_RX_SUCCESS;
1688 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1691 orig_node_free_ref(orig_node);
1695 int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1697 struct vis_packet *vis_packet;
1698 struct ethhdr *ethhdr;
1699 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1700 int hdr_size = sizeof(*vis_packet);
1702 /* keep skb linear */
1703 if (skb_linearize(skb) < 0)
1706 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1709 vis_packet = (struct vis_packet *)skb->data;
1710 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1713 if (!is_my_mac(ethhdr->h_dest))
1716 /* ignore own packets */
1717 if (is_my_mac(vis_packet->vis_orig))
1720 if (is_my_mac(vis_packet->sender_orig))
1723 switch (vis_packet->vis_type) {
1724 case VIS_TYPE_SERVER_SYNC:
1725 receive_server_sync_packet(bat_priv, vis_packet,
1729 case VIS_TYPE_CLIENT_UPDATE:
1730 receive_client_update_packet(bat_priv, vis_packet,
1734 default: /* ignore unknown packet */
1738 /* We take a copy of the data in the packet, so we should
1739 always free the skbuf. */