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 due to their size or
79 * because they have already been sent TT_OGM_APPEND_MAX times.
80 * In this case send a tt request */
81 if (!tt_num_changes) {
86 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
87 (struct tt_change *)tt_buff);
89 /* Even if we received the precomputed crc with the OGM, we
90 * prefer to recompute it to spot any possible inconsistency
91 * in the global table */
92 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
94 /* The ttvn alone is not enough to guarantee consistency
95 * because a single value could represent different states
96 * (due to the wrap around). Thus a node has to check whether
97 * the resulting table (after applying the changes) is still
98 * consistent or not. E.g. a node could disconnect while its
99 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
100 * checking the CRC value is mandatory to detect the
102 if (orig_node->tt_crc != tt_crc)
105 /* Roaming phase is over: tables are in sync again. I can
107 orig_node->tt_poss_change = false;
109 /* if we missed more than one change or our tables are not
110 * in sync anymore -> request fresh tt data */
111 if (ttvn != orig_ttvn || orig_node->tt_crc != tt_crc) {
113 bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
114 "Need to retrieve the correct information "
115 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
116 "%u num_changes: %u)\n", orig_node->orig, ttvn,
117 orig_ttvn, tt_crc, orig_node->tt_crc,
119 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
126 static void update_route(struct bat_priv *bat_priv,
127 struct orig_node *orig_node,
128 struct neigh_node *neigh_node)
130 struct neigh_node *curr_router;
132 curr_router = orig_node_get_router(orig_node);
135 if ((curr_router) && (!neigh_node)) {
136 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
138 tt_global_del_orig(bat_priv, orig_node,
139 "Deleted route towards originator");
142 } else if ((!curr_router) && (neigh_node)) {
144 bat_dbg(DBG_ROUTES, bat_priv,
145 "Adding route towards: %pM (via %pM)\n",
146 orig_node->orig, neigh_node->addr);
148 } else if (neigh_node && curr_router) {
149 bat_dbg(DBG_ROUTES, bat_priv,
150 "Changing route towards: %pM "
151 "(now via %pM - was via %pM)\n",
152 orig_node->orig, neigh_node->addr,
157 neigh_node_free_ref(curr_router);
159 /* increase refcount of new best neighbor */
160 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
163 spin_lock_bh(&orig_node->neigh_list_lock);
164 rcu_assign_pointer(orig_node->router, neigh_node);
165 spin_unlock_bh(&orig_node->neigh_list_lock);
167 /* decrease refcount of previous best neighbor */
169 neigh_node_free_ref(curr_router);
172 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
173 struct neigh_node *neigh_node)
175 struct neigh_node *router = NULL;
180 router = orig_node_get_router(orig_node);
182 if (router != neigh_node)
183 update_route(bat_priv, orig_node, neigh_node);
187 neigh_node_free_ref(router);
190 static int is_bidirectional_neigh(struct orig_node *orig_node,
191 struct orig_node *orig_neigh_node,
192 struct batman_packet *batman_packet,
193 struct hard_iface *if_incoming)
195 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
196 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
197 struct hlist_node *node;
199 uint8_t orig_eq_count, neigh_rq_count, tq_own;
200 int tq_asym_penalty, ret = 0;
202 /* find corresponding one hop neighbor */
204 hlist_for_each_entry_rcu(tmp_neigh_node, node,
205 &orig_neigh_node->neigh_list, list) {
207 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
210 if (tmp_neigh_node->if_incoming != if_incoming)
213 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
216 neigh_node = tmp_neigh_node;
222 neigh_node = create_neighbor(orig_neigh_node,
224 orig_neigh_node->orig,
230 /* if orig_node is direct neighbor update neigh_node last_valid */
231 if (orig_node == orig_neigh_node)
232 neigh_node->last_valid = jiffies;
234 orig_node->last_valid = jiffies;
236 /* find packet count of corresponding one hop neighbor */
237 spin_lock_bh(&orig_node->ogm_cnt_lock);
238 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
239 neigh_rq_count = neigh_node->real_packet_count;
240 spin_unlock_bh(&orig_node->ogm_cnt_lock);
242 /* pay attention to not get a value bigger than 100 % */
243 total_count = (orig_eq_count > neigh_rq_count ?
244 neigh_rq_count : orig_eq_count);
246 /* if we have too few packets (too less data) we set tq_own to zero */
247 /* if we receive too few packets it is not considered bidirectional */
248 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
249 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
252 /* neigh_node->real_packet_count is never zero as we
253 * only purge old information when getting new
255 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
258 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
259 * affect the nearly-symmetric links only a little, but
260 * punishes asymmetric links more. This will give a value
261 * between 0 and TQ_MAX_VALUE
263 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
264 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
265 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
266 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
267 (TQ_LOCAL_WINDOW_SIZE *
268 TQ_LOCAL_WINDOW_SIZE *
269 TQ_LOCAL_WINDOW_SIZE);
271 batman_packet->tq = ((batman_packet->tq * tq_own * tq_asym_penalty) /
272 (TQ_MAX_VALUE * TQ_MAX_VALUE));
274 bat_dbg(DBG_BATMAN, bat_priv,
276 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
277 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
279 orig_node->orig, orig_neigh_node->orig, total_count,
280 neigh_rq_count, tq_own, tq_asym_penalty, batman_packet->tq);
282 /* if link has the minimum required transmission quality
283 * consider it bidirectional */
284 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
289 neigh_node_free_ref(neigh_node);
293 /* caller must hold the neigh_list_lock */
294 void bonding_candidate_del(struct orig_node *orig_node,
295 struct neigh_node *neigh_node)
297 /* this neighbor is not part of our candidate list */
298 if (list_empty(&neigh_node->bonding_list))
301 list_del_rcu(&neigh_node->bonding_list);
302 INIT_LIST_HEAD(&neigh_node->bonding_list);
303 neigh_node_free_ref(neigh_node);
304 atomic_dec(&orig_node->bond_candidates);
310 static void bonding_candidate_add(struct orig_node *orig_node,
311 struct neigh_node *neigh_node)
313 struct hlist_node *node;
314 struct neigh_node *tmp_neigh_node, *router = NULL;
315 uint8_t interference_candidate = 0;
317 spin_lock_bh(&orig_node->neigh_list_lock);
319 /* only consider if it has the same primary address ... */
320 if (!compare_eth(orig_node->orig,
321 neigh_node->orig_node->primary_addr))
324 router = orig_node_get_router(orig_node);
328 /* ... and is good enough to be considered */
329 if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
333 * check if we have another candidate with the same mac address or
334 * interface. If we do, we won't select this candidate because of
335 * possible interference.
337 hlist_for_each_entry_rcu(tmp_neigh_node, node,
338 &orig_node->neigh_list, list) {
340 if (tmp_neigh_node == neigh_node)
343 /* we only care if the other candidate is even
344 * considered as candidate. */
345 if (list_empty(&tmp_neigh_node->bonding_list))
348 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
349 (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
350 interference_candidate = 1;
355 /* don't care further if it is an interference candidate */
356 if (interference_candidate)
359 /* this neighbor already is part of our candidate list */
360 if (!list_empty(&neigh_node->bonding_list))
363 if (!atomic_inc_not_zero(&neigh_node->refcount))
366 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
367 atomic_inc(&orig_node->bond_candidates);
371 bonding_candidate_del(orig_node, neigh_node);
374 spin_unlock_bh(&orig_node->neigh_list_lock);
377 neigh_node_free_ref(router);
380 /* copy primary address for bonding */
381 static void bonding_save_primary(const struct orig_node *orig_node,
382 struct orig_node *orig_neigh_node,
383 const struct batman_packet *batman_packet)
385 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
388 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
391 static void update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
392 const struct ethhdr *ethhdr,
393 const struct batman_packet *batman_packet,
394 struct hard_iface *if_incoming,
395 const unsigned char *tt_buff, int is_duplicate)
397 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
398 struct neigh_node *router = NULL;
399 struct orig_node *orig_node_tmp;
400 struct hlist_node *node;
401 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
403 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
404 "Searching and updating originator entry of received packet\n");
407 hlist_for_each_entry_rcu(tmp_neigh_node, node,
408 &orig_node->neigh_list, list) {
409 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
410 (tmp_neigh_node->if_incoming == if_incoming) &&
411 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
413 neigh_node_free_ref(neigh_node);
414 neigh_node = tmp_neigh_node;
421 spin_lock_bh(&tmp_neigh_node->tq_lock);
422 ring_buffer_set(tmp_neigh_node->tq_recv,
423 &tmp_neigh_node->tq_index, 0);
424 tmp_neigh_node->tq_avg =
425 ring_buffer_avg(tmp_neigh_node->tq_recv);
426 spin_unlock_bh(&tmp_neigh_node->tq_lock);
430 struct orig_node *orig_tmp;
432 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
436 neigh_node = create_neighbor(orig_node, orig_tmp,
437 ethhdr->h_source, if_incoming);
439 orig_node_free_ref(orig_tmp);
443 bat_dbg(DBG_BATMAN, bat_priv,
444 "Updating existing last-hop neighbor of originator\n");
448 orig_node->flags = batman_packet->flags;
449 neigh_node->last_valid = jiffies;
451 spin_lock_bh(&neigh_node->tq_lock);
452 ring_buffer_set(neigh_node->tq_recv,
453 &neigh_node->tq_index,
455 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
456 spin_unlock_bh(&neigh_node->tq_lock);
459 orig_node->last_ttl = batman_packet->ttl;
460 neigh_node->last_ttl = batman_packet->ttl;
463 bonding_candidate_add(orig_node, neigh_node);
465 /* if this neighbor already is our next hop there is nothing
467 router = orig_node_get_router(orig_node);
468 if (router == neigh_node)
471 /* if this neighbor does not offer a better TQ we won't consider it */
472 if (router && (router->tq_avg > neigh_node->tq_avg))
475 /* if the TQ is the same and the link not more symmetric we
476 * won't consider it either */
477 if (router && (neigh_node->tq_avg == router->tq_avg)) {
478 orig_node_tmp = router->orig_node;
479 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
481 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
482 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
484 orig_node_tmp = neigh_node->orig_node;
485 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
486 bcast_own_sum_neigh =
487 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
488 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
490 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
494 update_routes(bat_priv, orig_node, neigh_node);
497 /* I have to check for transtable changes only if the OGM has been
498 * sent through a primary interface */
499 if (((batman_packet->orig != ethhdr->h_source) &&
500 (batman_packet->ttl > 2)) ||
501 (batman_packet->flags & PRIMARIES_FIRST_HOP))
502 update_transtable(bat_priv, orig_node, tt_buff,
503 batman_packet->tt_num_changes,
505 batman_packet->tt_crc);
507 if (orig_node->gw_flags != batman_packet->gw_flags)
508 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
510 orig_node->gw_flags = batman_packet->gw_flags;
512 /* restart gateway selection if fast or late switching was enabled */
513 if ((orig_node->gw_flags) &&
514 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
515 (atomic_read(&bat_priv->gw_sel_class) > 2))
516 gw_check_election(bat_priv, orig_node);
524 neigh_node_free_ref(neigh_node);
526 neigh_node_free_ref(router);
529 /* checks whether the host restarted and is in the protection time.
531 * 0 if the packet is to be accepted
532 * 1 if the packet is to be ignored.
534 static int window_protected(struct bat_priv *bat_priv,
535 int32_t seq_num_diff,
536 unsigned long *last_reset)
538 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
539 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
540 if (time_after(jiffies, *last_reset +
541 msecs_to_jiffies(RESET_PROTECTION_MS))) {
543 *last_reset = jiffies;
544 bat_dbg(DBG_BATMAN, bat_priv,
545 "old packet received, start protection\n");
554 /* processes a batman packet for all interfaces, adjusts the sequence number and
555 * finds out whether it is a duplicate.
557 * 1 the packet is a duplicate
558 * 0 the packet has not yet been received
559 * -1 the packet is old and has been received while the seqno window
560 * was protected. Caller should drop it.
562 static int count_real_packets(const struct ethhdr *ethhdr,
563 const struct batman_packet *batman_packet,
564 const struct hard_iface *if_incoming)
566 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
567 struct orig_node *orig_node;
568 struct neigh_node *tmp_neigh_node;
569 struct hlist_node *node;
570 int is_duplicate = 0;
573 int set_mark, ret = -1;
575 orig_node = get_orig_node(bat_priv, batman_packet->orig);
579 spin_lock_bh(&orig_node->ogm_cnt_lock);
580 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
582 /* signalize caller that the packet is to be dropped. */
583 if (window_protected(bat_priv, seq_diff,
584 &orig_node->batman_seqno_reset))
588 hlist_for_each_entry_rcu(tmp_neigh_node, node,
589 &orig_node->neigh_list, list) {
591 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
592 orig_node->last_real_seqno,
593 batman_packet->seqno);
595 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
596 (tmp_neigh_node->if_incoming == if_incoming))
601 /* if the window moved, set the update flag. */
602 need_update |= bit_get_packet(bat_priv,
603 tmp_neigh_node->real_bits,
606 tmp_neigh_node->real_packet_count =
607 bit_packet_count(tmp_neigh_node->real_bits);
612 bat_dbg(DBG_BATMAN, bat_priv,
613 "updating last_seqno: old %d, new %d\n",
614 orig_node->last_real_seqno, batman_packet->seqno);
615 orig_node->last_real_seqno = batman_packet->seqno;
621 spin_unlock_bh(&orig_node->ogm_cnt_lock);
622 orig_node_free_ref(orig_node);
626 void receive_bat_packet(const struct ethhdr *ethhdr,
627 struct batman_packet *batman_packet,
628 const unsigned char *tt_buff,
629 struct hard_iface *if_incoming)
631 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
632 struct hard_iface *hard_iface;
633 struct orig_node *orig_neigh_node, *orig_node;
634 struct neigh_node *router = NULL, *router_router = NULL;
635 struct neigh_node *orig_neigh_router = NULL;
636 int has_directlink_flag;
637 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
638 int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
640 uint32_t if_incoming_seqno;
642 /* Silently drop when the batman packet is actually not a
645 * This might happen if a packet is padded (e.g. Ethernet has a
646 * minimum frame length of 64 byte) and the aggregation interprets
647 * it as an additional length.
649 * TODO: A more sane solution would be to have a bit in the
650 * batman_packet to detect whether the packet is the last
651 * packet in an aggregation. Here we expect that the padding
652 * is always zero (or not 0x01)
654 if (batman_packet->packet_type != BAT_PACKET)
657 /* could be changed by schedule_own_packet() */
658 if_incoming_seqno = atomic_read(&if_incoming->seqno);
660 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
662 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
663 batman_packet->orig) ? 1 : 0);
665 bat_dbg(DBG_BATMAN, bat_priv,
666 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
667 "(from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, "
668 "crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
669 ethhdr->h_source, if_incoming->net_dev->name,
670 if_incoming->net_dev->dev_addr, batman_packet->orig,
671 batman_packet->prev_sender, batman_packet->seqno,
672 batman_packet->ttvn, batman_packet->tt_crc,
673 batman_packet->tt_num_changes, batman_packet->tq,
674 batman_packet->ttl, batman_packet->version,
675 has_directlink_flag);
678 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
679 if (hard_iface->if_status != IF_ACTIVE)
682 if (hard_iface->soft_iface != if_incoming->soft_iface)
685 if (compare_eth(ethhdr->h_source,
686 hard_iface->net_dev->dev_addr))
689 if (compare_eth(batman_packet->orig,
690 hard_iface->net_dev->dev_addr))
693 if (compare_eth(batman_packet->prev_sender,
694 hard_iface->net_dev->dev_addr))
697 if (is_broadcast_ether_addr(ethhdr->h_source))
702 if (batman_packet->version != COMPAT_VERSION) {
703 bat_dbg(DBG_BATMAN, bat_priv,
704 "Drop packet: incompatible batman version (%i)\n",
705 batman_packet->version);
710 bat_dbg(DBG_BATMAN, bat_priv,
711 "Drop packet: received my own broadcast (sender: %pM"
718 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
719 "ignoring all packets with broadcast source addr (sender: %pM"
720 ")\n", ethhdr->h_source);
728 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
729 if (!orig_neigh_node)
732 /* neighbor has to indicate direct link and it has to
733 * come via the corresponding interface */
734 /* save packet seqno for bidirectional check */
735 if (has_directlink_flag &&
736 compare_eth(if_incoming->net_dev->dev_addr,
737 batman_packet->orig)) {
738 offset = if_incoming->if_num * NUM_WORDS;
740 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
741 word = &(orig_neigh_node->bcast_own[offset]);
743 if_incoming_seqno - batman_packet->seqno - 2);
744 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
745 bit_packet_count(word);
746 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
749 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
750 "originator packet from myself (via neighbor)\n");
751 orig_node_free_ref(orig_neigh_node);
756 bat_dbg(DBG_BATMAN, bat_priv,
757 "Drop packet: ignoring all rebroadcast echos (sender: "
758 "%pM)\n", ethhdr->h_source);
762 orig_node = get_orig_node(bat_priv, batman_packet->orig);
766 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
768 if (is_duplicate == -1) {
769 bat_dbg(DBG_BATMAN, bat_priv,
770 "Drop packet: packet within seqno protection time "
771 "(sender: %pM)\n", ethhdr->h_source);
775 if (batman_packet->tq == 0) {
776 bat_dbg(DBG_BATMAN, bat_priv,
777 "Drop packet: originator packet with tq equal 0\n");
781 router = orig_node_get_router(orig_node);
783 router_router = orig_node_get_router(router->orig_node);
785 /* avoid temporary routing loops */
786 if (router && router_router &&
787 (compare_eth(router->addr, batman_packet->prev_sender)) &&
788 !(compare_eth(batman_packet->orig, batman_packet->prev_sender)) &&
789 (compare_eth(router->addr, router_router->addr))) {
790 bat_dbg(DBG_BATMAN, bat_priv,
791 "Drop packet: ignoring all rebroadcast packets that "
792 "may make me loop (sender: %pM)\n", ethhdr->h_source);
796 /* if sender is a direct neighbor the sender mac equals
798 orig_neigh_node = (is_single_hop_neigh ?
800 get_orig_node(bat_priv, ethhdr->h_source));
801 if (!orig_neigh_node)
804 orig_neigh_router = orig_node_get_router(orig_neigh_node);
806 /* drop packet if sender is not a direct neighbor and if we
807 * don't route towards it */
808 if (!is_single_hop_neigh && (!orig_neigh_router)) {
809 bat_dbg(DBG_BATMAN, bat_priv,
810 "Drop packet: OGM via unknown neighbor!\n");
814 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
815 batman_packet, if_incoming);
817 bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
819 /* update ranking if it is not a duplicate or has the same
820 * seqno and similar ttl as the non-duplicate */
821 if (is_bidirectional &&
823 ((orig_node->last_real_seqno == batman_packet->seqno) &&
824 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
825 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
826 if_incoming, tt_buff, is_duplicate);
828 /* is single hop (direct) neighbor */
829 if (is_single_hop_neigh) {
831 /* mark direct link on incoming interface */
832 schedule_forward_packet(orig_node, ethhdr, batman_packet,
835 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
836 "rebroadcast neighbor packet with direct link flag\n");
840 /* multihop originator */
841 if (!is_bidirectional) {
842 bat_dbg(DBG_BATMAN, bat_priv,
843 "Drop packet: not received via bidirectional link\n");
848 bat_dbg(DBG_BATMAN, bat_priv,
849 "Drop packet: duplicate packet received\n");
853 bat_dbg(DBG_BATMAN, bat_priv,
854 "Forwarding packet: rebroadcast originator packet\n");
855 schedule_forward_packet(orig_node, ethhdr, batman_packet,
859 if ((orig_neigh_node) && (!is_single_hop_neigh))
860 orig_node_free_ref(orig_neigh_node);
863 neigh_node_free_ref(router);
865 neigh_node_free_ref(router_router);
866 if (orig_neigh_router)
867 neigh_node_free_ref(orig_neigh_router);
869 orig_node_free_ref(orig_node);
872 int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
874 struct ethhdr *ethhdr;
876 /* drop packet if it has not necessary minimum size */
877 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
880 ethhdr = (struct ethhdr *)skb_mac_header(skb);
882 /* packet with broadcast indication but unicast recipient */
883 if (!is_broadcast_ether_addr(ethhdr->h_dest))
886 /* packet with broadcast sender address */
887 if (is_broadcast_ether_addr(ethhdr->h_source))
890 /* create a copy of the skb, if needed, to modify it. */
891 if (skb_cow(skb, 0) < 0)
894 /* keep skb linear */
895 if (skb_linearize(skb) < 0)
898 ethhdr = (struct ethhdr *)skb_mac_header(skb);
900 receive_aggr_bat_packet(ethhdr,
906 return NET_RX_SUCCESS;
909 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
910 struct sk_buff *skb, size_t icmp_len)
912 struct hard_iface *primary_if = NULL;
913 struct orig_node *orig_node = NULL;
914 struct neigh_node *router = NULL;
915 struct icmp_packet_rr *icmp_packet;
916 int ret = NET_RX_DROP;
918 icmp_packet = (struct icmp_packet_rr *)skb->data;
920 /* add data to device queue */
921 if (icmp_packet->msg_type != ECHO_REQUEST) {
922 bat_socket_receive_packet(icmp_packet, icmp_len);
926 primary_if = primary_if_get_selected(bat_priv);
930 /* answer echo request (ping) */
931 /* get routing information */
932 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
936 router = orig_node_get_router(orig_node);
940 /* create a copy of the skb, if needed, to modify it. */
941 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
944 icmp_packet = (struct icmp_packet_rr *)skb->data;
946 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
947 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
948 icmp_packet->msg_type = ECHO_REPLY;
949 icmp_packet->ttl = TTL;
951 send_skb_packet(skb, router->if_incoming, router->addr);
952 ret = NET_RX_SUCCESS;
956 hardif_free_ref(primary_if);
958 neigh_node_free_ref(router);
960 orig_node_free_ref(orig_node);
964 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
967 struct hard_iface *primary_if = NULL;
968 struct orig_node *orig_node = NULL;
969 struct neigh_node *router = NULL;
970 struct icmp_packet *icmp_packet;
971 int ret = NET_RX_DROP;
973 icmp_packet = (struct icmp_packet *)skb->data;
975 /* send TTL exceeded if packet is an echo request (traceroute) */
976 if (icmp_packet->msg_type != ECHO_REQUEST) {
977 pr_debug("Warning - can't forward icmp packet from %pM to "
978 "%pM: ttl exceeded\n", icmp_packet->orig,
983 primary_if = primary_if_get_selected(bat_priv);
987 /* get routing information */
988 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
992 router = orig_node_get_router(orig_node);
996 /* create a copy of the skb, if needed, to modify it. */
997 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1000 icmp_packet = (struct icmp_packet *)skb->data;
1002 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
1003 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
1004 icmp_packet->msg_type = TTL_EXCEEDED;
1005 icmp_packet->ttl = TTL;
1007 send_skb_packet(skb, router->if_incoming, router->addr);
1008 ret = NET_RX_SUCCESS;
1012 hardif_free_ref(primary_if);
1014 neigh_node_free_ref(router);
1016 orig_node_free_ref(orig_node);
1021 int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1023 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1024 struct icmp_packet_rr *icmp_packet;
1025 struct ethhdr *ethhdr;
1026 struct orig_node *orig_node = NULL;
1027 struct neigh_node *router = NULL;
1028 int hdr_size = sizeof(struct icmp_packet);
1029 int ret = NET_RX_DROP;
1032 * we truncate all incoming icmp packets if they don't match our size
1034 if (skb->len >= sizeof(struct icmp_packet_rr))
1035 hdr_size = sizeof(struct icmp_packet_rr);
1037 /* drop packet if it has not necessary minimum size */
1038 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1041 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1043 /* packet with unicast indication but broadcast recipient */
1044 if (is_broadcast_ether_addr(ethhdr->h_dest))
1047 /* packet with broadcast sender address */
1048 if (is_broadcast_ether_addr(ethhdr->h_source))
1052 if (!is_my_mac(ethhdr->h_dest))
1055 icmp_packet = (struct icmp_packet_rr *)skb->data;
1057 /* add record route information if not full */
1058 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
1059 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1060 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1061 ethhdr->h_dest, ETH_ALEN);
1062 icmp_packet->rr_cur++;
1066 if (is_my_mac(icmp_packet->dst))
1067 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1070 if (icmp_packet->ttl < 2)
1071 return recv_icmp_ttl_exceeded(bat_priv, skb);
1073 /* get routing information */
1074 orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
1078 router = orig_node_get_router(orig_node);
1082 /* create a copy of the skb, if needed, to modify it. */
1083 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1086 icmp_packet = (struct icmp_packet_rr *)skb->data;
1092 send_skb_packet(skb, router->if_incoming, router->addr);
1093 ret = NET_RX_SUCCESS;
1097 neigh_node_free_ref(router);
1099 orig_node_free_ref(orig_node);
1103 /* In the bonding case, send the packets in a round
1104 * robin fashion over the remaining interfaces.
1106 * This method rotates the bonding list and increases the
1107 * returned router's refcount. */
1108 static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
1109 const struct hard_iface *recv_if)
1111 struct neigh_node *tmp_neigh_node;
1112 struct neigh_node *router = NULL, *first_candidate = NULL;
1115 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1117 if (!first_candidate)
1118 first_candidate = tmp_neigh_node;
1120 /* recv_if == NULL on the first node. */
1121 if (tmp_neigh_node->if_incoming == recv_if)
1124 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1127 router = tmp_neigh_node;
1131 /* use the first candidate if nothing was found. */
1132 if (!router && first_candidate &&
1133 atomic_inc_not_zero(&first_candidate->refcount))
1134 router = first_candidate;
1139 /* selected should point to the next element
1140 * after the current router */
1141 spin_lock_bh(&primary_orig->neigh_list_lock);
1142 /* this is a list_move(), which unfortunately
1143 * does not exist as rcu version */
1144 list_del_rcu(&primary_orig->bond_list);
1145 list_add_rcu(&primary_orig->bond_list,
1146 &router->bonding_list);
1147 spin_unlock_bh(&primary_orig->neigh_list_lock);
1154 /* Interface Alternating: Use the best of the
1155 * remaining candidates which are not using
1158 * Increases the returned router's refcount */
1159 static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
1160 const struct hard_iface *recv_if)
1162 struct neigh_node *tmp_neigh_node;
1163 struct neigh_node *router = NULL, *first_candidate = NULL;
1166 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1168 if (!first_candidate)
1169 first_candidate = tmp_neigh_node;
1171 /* recv_if == NULL on the first node. */
1172 if (tmp_neigh_node->if_incoming == recv_if)
1175 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1178 /* if we don't have a router yet
1179 * or this one is better, choose it. */
1181 (tmp_neigh_node->tq_avg > router->tq_avg)) {
1182 /* decrement refcount of
1183 * previously selected router */
1185 neigh_node_free_ref(router);
1187 router = tmp_neigh_node;
1188 atomic_inc_not_zero(&router->refcount);
1191 neigh_node_free_ref(tmp_neigh_node);
1194 /* use the first candidate if nothing was found. */
1195 if (!router && first_candidate &&
1196 atomic_inc_not_zero(&first_candidate->refcount))
1197 router = first_candidate;
1203 int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
1205 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1206 struct tt_query_packet *tt_query;
1207 struct ethhdr *ethhdr;
1209 /* drop packet if it has not necessary minimum size */
1210 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
1213 /* I could need to modify it */
1214 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
1217 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1219 /* packet with unicast indication but broadcast recipient */
1220 if (is_broadcast_ether_addr(ethhdr->h_dest))
1223 /* packet with broadcast sender address */
1224 if (is_broadcast_ether_addr(ethhdr->h_source))
1227 tt_query = (struct tt_query_packet *)skb->data;
1229 tt_query->tt_data = ntohs(tt_query->tt_data);
1231 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
1233 /* If we cannot provide an answer the tt_request is
1235 if (!send_tt_response(bat_priv, tt_query)) {
1236 bat_dbg(DBG_TT, bat_priv,
1237 "Routing TT_REQUEST to %pM [%c]\n",
1239 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1240 tt_query->tt_data = htons(tt_query->tt_data);
1241 return route_unicast_packet(skb, recv_if);
1245 /* packet needs to be linearized to access the TT changes */
1246 if (skb_linearize(skb) < 0)
1249 if (is_my_mac(tt_query->dst))
1250 handle_tt_response(bat_priv, tt_query);
1252 bat_dbg(DBG_TT, bat_priv,
1253 "Routing TT_RESPONSE to %pM [%c]\n",
1255 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1256 tt_query->tt_data = htons(tt_query->tt_data);
1257 return route_unicast_packet(skb, recv_if);
1263 /* returning NET_RX_DROP will make the caller function kfree the skb */
1267 int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
1269 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1270 struct roam_adv_packet *roam_adv_packet;
1271 struct orig_node *orig_node;
1272 struct ethhdr *ethhdr;
1274 /* drop packet if it has not necessary minimum size */
1275 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
1278 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1280 /* packet with unicast indication but broadcast recipient */
1281 if (is_broadcast_ether_addr(ethhdr->h_dest))
1284 /* packet with broadcast sender address */
1285 if (is_broadcast_ether_addr(ethhdr->h_source))
1288 roam_adv_packet = (struct roam_adv_packet *)skb->data;
1290 if (!is_my_mac(roam_adv_packet->dst))
1291 return route_unicast_packet(skb, recv_if);
1293 orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
1297 bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
1298 "(client %pM)\n", roam_adv_packet->src,
1299 roam_adv_packet->client);
1301 tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
1302 atomic_read(&orig_node->last_ttvn) + 1, true, false);
1304 /* Roaming phase starts: I have new information but the ttvn has not
1305 * been incremented yet. This flag will make me check all the incoming
1306 * packets for the correct destination. */
1307 bat_priv->tt_poss_change = true;
1309 orig_node_free_ref(orig_node);
1311 /* returning NET_RX_DROP will make the caller function kfree the skb */
1315 /* find a suitable router for this originator, and use
1316 * bonding if possible. increases the found neighbors
1318 struct neigh_node *find_router(struct bat_priv *bat_priv,
1319 struct orig_node *orig_node,
1320 const struct hard_iface *recv_if)
1322 struct orig_node *primary_orig_node;
1323 struct orig_node *router_orig;
1324 struct neigh_node *router;
1325 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1326 int bonding_enabled;
1331 router = orig_node_get_router(orig_node);
1335 /* without bonding, the first node should
1336 * always choose the default router. */
1337 bonding_enabled = atomic_read(&bat_priv->bonding);
1340 /* select default router to output */
1341 router_orig = router->orig_node;
1345 if ((!recv_if) && (!bonding_enabled))
1348 /* if we have something in the primary_addr, we can search
1349 * for a potential bonding candidate. */
1350 if (compare_eth(router_orig->primary_addr, zero_mac))
1353 /* find the orig_node which has the primary interface. might
1354 * even be the same as our router_orig in many cases */
1356 if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
1357 primary_orig_node = router_orig;
1359 primary_orig_node = orig_hash_find(bat_priv,
1360 router_orig->primary_addr);
1361 if (!primary_orig_node)
1364 orig_node_free_ref(primary_orig_node);
1367 /* with less than 2 candidates, we can't do any
1368 * bonding and prefer the original router. */
1369 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
1372 /* all nodes between should choose a candidate which
1373 * is is not on the interface where the packet came
1376 neigh_node_free_ref(router);
1378 if (bonding_enabled)
1379 router = find_bond_router(primary_orig_node, recv_if);
1381 router = find_ifalter_router(primary_orig_node, recv_if);
1384 if (router && router->if_incoming->if_status != IF_ACTIVE)
1393 neigh_node_free_ref(router);
1397 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1399 struct ethhdr *ethhdr;
1401 /* drop packet if it has not necessary minimum size */
1402 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1405 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1407 /* packet with unicast indication but broadcast recipient */
1408 if (is_broadcast_ether_addr(ethhdr->h_dest))
1411 /* packet with broadcast sender address */
1412 if (is_broadcast_ether_addr(ethhdr->h_source))
1416 if (!is_my_mac(ethhdr->h_dest))
1422 int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1424 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1425 struct orig_node *orig_node = NULL;
1426 struct neigh_node *neigh_node = NULL;
1427 struct unicast_packet *unicast_packet;
1428 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1429 int ret = NET_RX_DROP;
1430 struct sk_buff *new_skb;
1432 unicast_packet = (struct unicast_packet *)skb->data;
1435 if (unicast_packet->ttl < 2) {
1436 pr_debug("Warning - can't forward unicast packet from %pM to "
1437 "%pM: ttl exceeded\n", ethhdr->h_source,
1438 unicast_packet->dest);
1442 /* get routing information */
1443 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1448 /* find_router() increases neigh_nodes refcount if found. */
1449 neigh_node = find_router(bat_priv, orig_node, recv_if);
1454 /* create a copy of the skb, if needed, to modify it. */
1455 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1458 unicast_packet = (struct unicast_packet *)skb->data;
1460 if (unicast_packet->packet_type == BAT_UNICAST &&
1461 atomic_read(&bat_priv->fragmentation) &&
1462 skb->len > neigh_node->if_incoming->net_dev->mtu) {
1463 ret = frag_send_skb(skb, bat_priv,
1464 neigh_node->if_incoming, neigh_node->addr);
1468 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1469 frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
1471 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1473 if (ret == NET_RX_DROP)
1476 /* packet was buffered for late merge */
1478 ret = NET_RX_SUCCESS;
1483 unicast_packet = (struct unicast_packet *)skb->data;
1487 unicast_packet->ttl--;
1490 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1491 ret = NET_RX_SUCCESS;
1495 neigh_node_free_ref(neigh_node);
1497 orig_node_free_ref(orig_node);
1501 static int check_unicast_ttvn(struct bat_priv *bat_priv,
1502 struct sk_buff *skb) {
1504 struct orig_node *orig_node;
1505 struct ethhdr *ethhdr;
1506 struct hard_iface *primary_if;
1507 struct unicast_packet *unicast_packet;
1508 bool tt_poss_change;
1510 /* I could need to modify it */
1511 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
1514 unicast_packet = (struct unicast_packet *)skb->data;
1516 if (is_my_mac(unicast_packet->dest)) {
1517 tt_poss_change = bat_priv->tt_poss_change;
1518 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1520 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1525 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
1526 tt_poss_change = orig_node->tt_poss_change;
1527 orig_node_free_ref(orig_node);
1530 /* Check whether I have to reroute the packet */
1531 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
1532 /* Linearize the skb before accessing it */
1533 if (skb_linearize(skb) < 0)
1536 ethhdr = (struct ethhdr *)(skb->data +
1537 sizeof(struct unicast_packet));
1538 orig_node = transtable_search(bat_priv, NULL, ethhdr->h_dest);
1541 if (!is_my_client(bat_priv, ethhdr->h_dest))
1543 primary_if = primary_if_get_selected(bat_priv);
1546 memcpy(unicast_packet->dest,
1547 primary_if->net_dev->dev_addr, ETH_ALEN);
1548 hardif_free_ref(primary_if);
1550 memcpy(unicast_packet->dest, orig_node->orig,
1552 curr_ttvn = (uint8_t)
1553 atomic_read(&orig_node->last_ttvn);
1554 orig_node_free_ref(orig_node);
1557 bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
1558 "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
1559 "%pM\n", unicast_packet->ttvn, curr_ttvn,
1560 ethhdr->h_dest, unicast_packet->dest);
1562 unicast_packet->ttvn = curr_ttvn;
1567 int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1569 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1570 struct unicast_packet *unicast_packet;
1571 int hdr_size = sizeof(*unicast_packet);
1573 if (check_unicast_packet(skb, hdr_size) < 0)
1576 if (!check_unicast_ttvn(bat_priv, skb))
1579 unicast_packet = (struct unicast_packet *)skb->data;
1582 if (is_my_mac(unicast_packet->dest)) {
1583 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1584 return NET_RX_SUCCESS;
1587 return route_unicast_packet(skb, recv_if);
1590 int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1592 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1593 struct unicast_frag_packet *unicast_packet;
1594 int hdr_size = sizeof(*unicast_packet);
1595 struct sk_buff *new_skb = NULL;
1598 if (check_unicast_packet(skb, hdr_size) < 0)
1601 if (!check_unicast_ttvn(bat_priv, skb))
1604 unicast_packet = (struct unicast_frag_packet *)skb->data;
1607 if (is_my_mac(unicast_packet->dest)) {
1609 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1611 if (ret == NET_RX_DROP)
1614 /* packet was buffered for late merge */
1616 return NET_RX_SUCCESS;
1618 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1619 sizeof(struct unicast_packet));
1620 return NET_RX_SUCCESS;
1623 return route_unicast_packet(skb, recv_if);
1627 int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1629 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1630 struct orig_node *orig_node = NULL;
1631 struct bcast_packet *bcast_packet;
1632 struct ethhdr *ethhdr;
1633 int hdr_size = sizeof(*bcast_packet);
1634 int ret = NET_RX_DROP;
1637 /* drop packet if it has not necessary minimum size */
1638 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1641 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1643 /* packet with broadcast indication but unicast recipient */
1644 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1647 /* packet with broadcast sender address */
1648 if (is_broadcast_ether_addr(ethhdr->h_source))
1651 /* ignore broadcasts sent by myself */
1652 if (is_my_mac(ethhdr->h_source))
1655 bcast_packet = (struct bcast_packet *)skb->data;
1657 /* ignore broadcasts originated by myself */
1658 if (is_my_mac(bcast_packet->orig))
1661 if (bcast_packet->ttl < 2)
1664 orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
1669 spin_lock_bh(&orig_node->bcast_seqno_lock);
1671 /* check whether the packet is a duplicate */
1672 if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1673 ntohl(bcast_packet->seqno)))
1676 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1678 /* check whether the packet is old and the host just restarted. */
1679 if (window_protected(bat_priv, seq_diff,
1680 &orig_node->bcast_seqno_reset))
1683 /* mark broadcast in flood history, update window position
1685 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1686 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1688 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1690 /* rebroadcast packet */
1691 add_bcast_packet_to_list(bat_priv, skb, 1);
1693 /* broadcast for me */
1694 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1695 ret = NET_RX_SUCCESS;
1699 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1702 orig_node_free_ref(orig_node);
1706 int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1708 struct vis_packet *vis_packet;
1709 struct ethhdr *ethhdr;
1710 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1711 int hdr_size = sizeof(*vis_packet);
1713 /* keep skb linear */
1714 if (skb_linearize(skb) < 0)
1717 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1720 vis_packet = (struct vis_packet *)skb->data;
1721 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1724 if (!is_my_mac(ethhdr->h_dest))
1727 /* ignore own packets */
1728 if (is_my_mac(vis_packet->vis_orig))
1731 if (is_my_mac(vis_packet->sender_orig))
1734 switch (vis_packet->vis_type) {
1735 case VIS_TYPE_SERVER_SYNC:
1736 receive_server_sync_packet(bat_priv, vis_packet,
1740 case VIS_TYPE_CLIENT_UPDATE:
1741 receive_client_update_packet(bat_priv, vis_packet,
1745 default: /* ignore unknown packet */
1749 /* We take a copy of the data in the packet, so we should
1750 always free the skbuf. */