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 batman_if *batman_if)
40 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
41 struct hashtable_t *hash = bat_priv->orig_hash;
42 struct hlist_node *walk;
43 struct hlist_head *head;
44 struct element_t *bucket;
45 struct orig_node *orig_node;
50 spin_lock_bh(&bat_priv->orig_hash_lock);
52 for (i = 0; i < hash->size; i++) {
53 head = &hash->table[i];
56 hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
57 orig_node = bucket->data;
58 spin_lock_bh(&orig_node->ogm_cnt_lock);
59 word_index = batman_if->if_num * NUM_WORDS;
60 word = &(orig_node->bcast_own[word_index]);
62 bit_get_packet(bat_priv, word, 1, 0);
63 orig_node->bcast_own_sum[batman_if->if_num] =
64 bit_packet_count(word);
65 spin_unlock_bh(&orig_node->ogm_cnt_lock);
70 spin_unlock_bh(&bat_priv->orig_hash_lock);
73 static void update_HNA(struct bat_priv *bat_priv, struct orig_node *orig_node,
74 unsigned char *hna_buff, int hna_buff_len)
76 if ((hna_buff_len != orig_node->hna_buff_len) ||
77 ((hna_buff_len > 0) &&
78 (orig_node->hna_buff_len > 0) &&
79 (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
81 if (orig_node->hna_buff_len > 0)
82 hna_global_del_orig(bat_priv, orig_node,
83 "originator changed hna");
85 if ((hna_buff_len > 0) && (hna_buff))
86 hna_global_add_orig(bat_priv, orig_node,
87 hna_buff, hna_buff_len);
91 static void update_route(struct bat_priv *bat_priv,
92 struct orig_node *orig_node,
93 struct neigh_node *neigh_node,
94 unsigned char *hna_buff, int hna_buff_len)
96 struct neigh_node *neigh_node_tmp;
99 if ((orig_node->router) && (!neigh_node)) {
101 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
103 hna_global_del_orig(bat_priv, orig_node,
104 "originator timed out");
107 } else if ((!orig_node->router) && (neigh_node)) {
109 bat_dbg(DBG_ROUTES, bat_priv,
110 "Adding route towards: %pM (via %pM)\n",
111 orig_node->orig, neigh_node->addr);
112 hna_global_add_orig(bat_priv, orig_node,
113 hna_buff, hna_buff_len);
117 bat_dbg(DBG_ROUTES, bat_priv,
118 "Changing route towards: %pM "
119 "(now via %pM - was via %pM)\n",
120 orig_node->orig, neigh_node->addr,
121 orig_node->router->addr);
125 kref_get(&neigh_node->refcount);
126 neigh_node_tmp = orig_node->router;
127 orig_node->router = neigh_node;
129 kref_put(&neigh_node_tmp->refcount, neigh_node_free_ref);
133 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
134 struct neigh_node *neigh_node, unsigned char *hna_buff,
141 if (orig_node->router != neigh_node)
142 update_route(bat_priv, orig_node, neigh_node,
143 hna_buff, hna_buff_len);
144 /* may be just HNA changed */
146 update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
149 static int is_bidirectional_neigh(struct orig_node *orig_node,
150 struct orig_node *orig_neigh_node,
151 struct batman_packet *batman_packet,
152 struct batman_if *if_incoming)
154 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
155 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
156 struct hlist_node *node;
157 unsigned char total_count;
160 if (orig_node == orig_neigh_node) {
162 hlist_for_each_entry_rcu(tmp_neigh_node, node,
163 &orig_node->neigh_list, list) {
165 if (compare_orig(tmp_neigh_node->addr,
166 orig_neigh_node->orig) &&
167 (tmp_neigh_node->if_incoming == if_incoming))
168 neigh_node = tmp_neigh_node;
172 neigh_node = create_neighbor(orig_node,
174 orig_neigh_node->orig,
176 /* create_neighbor failed, return 0 */
180 kref_get(&neigh_node->refcount);
183 neigh_node->last_valid = jiffies;
185 /* find packet count of corresponding one hop neighbor */
187 hlist_for_each_entry_rcu(tmp_neigh_node, node,
188 &orig_neigh_node->neigh_list, list) {
190 if (compare_orig(tmp_neigh_node->addr,
191 orig_neigh_node->orig) &&
192 (tmp_neigh_node->if_incoming == if_incoming))
193 neigh_node = tmp_neigh_node;
197 neigh_node = create_neighbor(orig_neigh_node,
199 orig_neigh_node->orig,
201 /* create_neighbor failed, return 0 */
205 kref_get(&neigh_node->refcount);
209 orig_node->last_valid = jiffies;
211 /* pay attention to not get a value bigger than 100 % */
212 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
213 neigh_node->real_packet_count ?
214 neigh_node->real_packet_count :
215 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
217 /* if we have too few packets (too less data) we set tq_own to zero */
218 /* if we receive too few packets it is not considered bidirectional */
219 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
220 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
221 orig_neigh_node->tq_own = 0;
223 /* neigh_node->real_packet_count is never zero as we
224 * only purge old information when getting new
226 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
227 neigh_node->real_packet_count;
230 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
231 * affect the nearly-symmetric links only a little, but
232 * punishes asymmetric links more. This will give a value
233 * between 0 and TQ_MAX_VALUE
235 orig_neigh_node->tq_asym_penalty =
238 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
239 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
240 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
241 (TQ_LOCAL_WINDOW_SIZE *
242 TQ_LOCAL_WINDOW_SIZE *
243 TQ_LOCAL_WINDOW_SIZE);
245 batman_packet->tq = ((batman_packet->tq *
246 orig_neigh_node->tq_own *
247 orig_neigh_node->tq_asym_penalty) /
248 (TQ_MAX_VALUE * TQ_MAX_VALUE));
250 bat_dbg(DBG_BATMAN, bat_priv,
252 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
253 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
255 orig_node->orig, orig_neigh_node->orig, total_count,
256 neigh_node->real_packet_count, orig_neigh_node->tq_own,
257 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
259 /* if link has the minimum required transmission quality
260 * consider it bidirectional */
261 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
270 kref_put(&neigh_node->refcount, neigh_node_free_ref);
274 static void update_orig(struct bat_priv *bat_priv,
275 struct orig_node *orig_node,
276 struct ethhdr *ethhdr,
277 struct batman_packet *batman_packet,
278 struct batman_if *if_incoming,
279 unsigned char *hna_buff, int hna_buff_len,
282 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
283 struct orig_node *orig_node_tmp;
284 struct hlist_node *node;
285 int tmp_hna_buff_len;
286 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
288 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
289 "Searching and updating originator entry of received packet\n");
292 hlist_for_each_entry_rcu(tmp_neigh_node, node,
293 &orig_node->neigh_list, list) {
294 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
295 (tmp_neigh_node->if_incoming == if_incoming)) {
296 neigh_node = tmp_neigh_node;
303 ring_buffer_set(tmp_neigh_node->tq_recv,
304 &tmp_neigh_node->tq_index, 0);
305 tmp_neigh_node->tq_avg =
306 ring_buffer_avg(tmp_neigh_node->tq_recv);
310 struct orig_node *orig_tmp;
312 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
316 neigh_node = create_neighbor(orig_node, orig_tmp,
317 ethhdr->h_source, if_incoming);
319 kref_put(&orig_tmp->refcount, orig_node_free_ref);
323 bat_dbg(DBG_BATMAN, bat_priv,
324 "Updating existing last-hop neighbor of originator\n");
326 kref_get(&neigh_node->refcount);
329 orig_node->flags = batman_packet->flags;
330 neigh_node->last_valid = jiffies;
332 ring_buffer_set(neigh_node->tq_recv,
333 &neigh_node->tq_index,
335 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
338 orig_node->last_ttl = batman_packet->ttl;
339 neigh_node->last_ttl = batman_packet->ttl;
342 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
343 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
345 /* if this neighbor already is our next hop there is nothing
347 if (orig_node->router == neigh_node)
350 /* if this neighbor does not offer a better TQ we won't consider it */
351 if ((orig_node->router) &&
352 (orig_node->router->tq_avg > neigh_node->tq_avg))
355 /* if the TQ is the same and the link not more symetric we
356 * won't consider it either */
357 if ((orig_node->router) &&
358 (neigh_node->tq_avg == orig_node->router->tq_avg)) {
359 orig_node_tmp = orig_node->router->orig_node;
360 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
362 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
363 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
365 orig_node_tmp = neigh_node->orig_node;
366 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
367 bcast_own_sum_neigh =
368 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
369 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
371 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
375 update_routes(bat_priv, orig_node, neigh_node,
376 hna_buff, tmp_hna_buff_len);
380 update_routes(bat_priv, orig_node, orig_node->router,
381 hna_buff, tmp_hna_buff_len);
384 if (orig_node->gw_flags != batman_packet->gw_flags)
385 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
387 orig_node->gw_flags = batman_packet->gw_flags;
389 /* restart gateway selection if fast or late switching was enabled */
390 if ((orig_node->gw_flags) &&
391 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
392 (atomic_read(&bat_priv->gw_sel_class) > 2))
393 gw_check_election(bat_priv, orig_node);
401 kref_put(&neigh_node->refcount, neigh_node_free_ref);
404 /* checks whether the host restarted and is in the protection time.
406 * 0 if the packet is to be accepted
407 * 1 if the packet is to be ignored.
409 static int window_protected(struct bat_priv *bat_priv,
410 int32_t seq_num_diff,
411 unsigned long *last_reset)
413 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
414 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
415 if (time_after(jiffies, *last_reset +
416 msecs_to_jiffies(RESET_PROTECTION_MS))) {
418 *last_reset = jiffies;
419 bat_dbg(DBG_BATMAN, bat_priv,
420 "old packet received, start protection\n");
429 /* processes a batman packet for all interfaces, adjusts the sequence number and
430 * finds out whether it is a duplicate.
432 * 1 the packet is a duplicate
433 * 0 the packet has not yet been received
434 * -1 the packet is old and has been received while the seqno window
435 * was protected. Caller should drop it.
437 static char count_real_packets(struct ethhdr *ethhdr,
438 struct batman_packet *batman_packet,
439 struct batman_if *if_incoming)
441 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
442 struct orig_node *orig_node;
443 struct neigh_node *tmp_neigh_node;
444 struct hlist_node *node;
445 char is_duplicate = 0;
450 orig_node = get_orig_node(bat_priv, batman_packet->orig);
454 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
456 /* signalize caller that the packet is to be dropped. */
457 if (window_protected(bat_priv, seq_diff,
458 &orig_node->batman_seqno_reset))
462 hlist_for_each_entry_rcu(tmp_neigh_node, node,
463 &orig_node->neigh_list, list) {
465 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
466 orig_node->last_real_seqno,
467 batman_packet->seqno);
469 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
470 (tmp_neigh_node->if_incoming == if_incoming))
475 /* if the window moved, set the update flag. */
476 need_update |= bit_get_packet(bat_priv,
477 tmp_neigh_node->real_bits,
480 tmp_neigh_node->real_packet_count =
481 bit_packet_count(tmp_neigh_node->real_bits);
486 bat_dbg(DBG_BATMAN, bat_priv,
487 "updating last_seqno: old %d, new %d\n",
488 orig_node->last_real_seqno, batman_packet->seqno);
489 orig_node->last_real_seqno = batman_packet->seqno;
492 kref_put(&orig_node->refcount, orig_node_free_ref);
496 kref_put(&orig_node->refcount, orig_node_free_ref);
500 /* copy primary address for bonding */
501 static void mark_bonding_address(struct orig_node *orig_node,
502 struct orig_node *orig_neigh_node,
503 struct batman_packet *batman_packet)
506 if (batman_packet->flags & PRIMARIES_FIRST_HOP)
507 memcpy(orig_neigh_node->primary_addr,
508 orig_node->orig, ETH_ALEN);
513 /* mark possible bond.candidates in the neighbor list */
514 void update_bonding_candidates(struct orig_node *orig_node)
517 int interference_candidate;
519 struct hlist_node *node, *node2;
520 struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
521 struct neigh_node *first_candidate, *last_candidate;
523 /* update the candidates for this originator */
524 if (!orig_node->router) {
525 orig_node->bond.candidates = 0;
529 best_tq = orig_node->router->tq_avg;
531 /* update bond.candidates */
535 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
536 * as "bonding partner" */
538 /* first, zero the list */
540 hlist_for_each_entry_rcu(tmp_neigh_node, node,
541 &orig_node->neigh_list, list) {
542 tmp_neigh_node->next_bond_candidate = NULL;
546 first_candidate = NULL;
547 last_candidate = NULL;
550 hlist_for_each_entry_rcu(tmp_neigh_node, node,
551 &orig_node->neigh_list, list) {
553 /* only consider if it has the same primary address ... */
554 if (memcmp(orig_node->orig,
555 tmp_neigh_node->orig_node->primary_addr,
559 /* ... and is good enough to be considered */
560 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
563 /* check if we have another candidate with the same
564 * mac address or interface. If we do, we won't
565 * select this candidate because of possible interference. */
567 interference_candidate = 0;
568 hlist_for_each_entry_rcu(tmp_neigh_node2, node2,
569 &orig_node->neigh_list, list) {
571 if (tmp_neigh_node2 == tmp_neigh_node)
574 /* we only care if the other candidate is even
575 * considered as candidate. */
576 if (!tmp_neigh_node2->next_bond_candidate)
580 if ((tmp_neigh_node->if_incoming ==
581 tmp_neigh_node2->if_incoming)
582 || (memcmp(tmp_neigh_node->addr,
583 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
585 interference_candidate = 1;
589 /* don't care further if it is an interference candidate */
590 if (interference_candidate)
593 if (!first_candidate) {
594 first_candidate = tmp_neigh_node;
595 tmp_neigh_node->next_bond_candidate = first_candidate;
597 tmp_neigh_node->next_bond_candidate = last_candidate;
599 last_candidate = tmp_neigh_node;
605 if (candidates > 0) {
606 first_candidate->next_bond_candidate = last_candidate;
607 orig_node->bond.selected = first_candidate;
610 orig_node->bond.candidates = candidates;
613 void receive_bat_packet(struct ethhdr *ethhdr,
614 struct batman_packet *batman_packet,
615 unsigned char *hna_buff, int hna_buff_len,
616 struct batman_if *if_incoming)
618 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
619 struct batman_if *batman_if;
620 struct orig_node *orig_neigh_node, *orig_node;
621 char has_directlink_flag;
622 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
623 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
625 uint32_t if_incoming_seqno;
627 /* Silently drop when the batman packet is actually not a
630 * This might happen if a packet is padded (e.g. Ethernet has a
631 * minimum frame length of 64 byte) and the aggregation interprets
632 * it as an additional length.
634 * TODO: A more sane solution would be to have a bit in the
635 * batman_packet to detect whether the packet is the last
636 * packet in an aggregation. Here we expect that the padding
637 * is always zero (or not 0x01)
639 if (batman_packet->packet_type != BAT_PACKET)
642 /* could be changed by schedule_own_packet() */
643 if_incoming_seqno = atomic_read(&if_incoming->seqno);
645 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
647 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
648 batman_packet->orig) ? 1 : 0);
650 bat_dbg(DBG_BATMAN, bat_priv,
651 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
652 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
653 "TTL %d, V %d, IDF %d)\n",
654 ethhdr->h_source, if_incoming->net_dev->name,
655 if_incoming->net_dev->dev_addr, batman_packet->orig,
656 batman_packet->prev_sender, batman_packet->seqno,
657 batman_packet->tq, batman_packet->ttl, batman_packet->version,
658 has_directlink_flag);
661 list_for_each_entry_rcu(batman_if, &if_list, list) {
662 if (batman_if->if_status != IF_ACTIVE)
665 if (batman_if->soft_iface != if_incoming->soft_iface)
668 if (compare_orig(ethhdr->h_source,
669 batman_if->net_dev->dev_addr))
672 if (compare_orig(batman_packet->orig,
673 batman_if->net_dev->dev_addr))
676 if (compare_orig(batman_packet->prev_sender,
677 batman_if->net_dev->dev_addr))
680 if (compare_orig(ethhdr->h_source, broadcast_addr))
685 if (batman_packet->version != COMPAT_VERSION) {
686 bat_dbg(DBG_BATMAN, bat_priv,
687 "Drop packet: incompatible batman version (%i)\n",
688 batman_packet->version);
693 bat_dbg(DBG_BATMAN, bat_priv,
694 "Drop packet: received my own broadcast (sender: %pM"
701 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
702 "ignoring all packets with broadcast source addr (sender: %pM"
703 ")\n", ethhdr->h_source);
711 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
712 if (!orig_neigh_node)
715 /* neighbor has to indicate direct link and it has to
716 * come via the corresponding interface */
717 /* if received seqno equals last send seqno save new
718 * seqno for bidirectional check */
719 if (has_directlink_flag &&
720 compare_orig(if_incoming->net_dev->dev_addr,
721 batman_packet->orig) &&
722 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
723 offset = if_incoming->if_num * NUM_WORDS;
725 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
726 word = &(orig_neigh_node->bcast_own[offset]);
728 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
729 bit_packet_count(word);
730 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
733 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
734 "originator packet from myself (via neighbor)\n");
735 kref_put(&orig_neigh_node->refcount, orig_node_free_ref);
740 bat_dbg(DBG_BATMAN, bat_priv,
741 "Drop packet: ignoring all rebroadcast echos (sender: "
742 "%pM)\n", ethhdr->h_source);
746 orig_node = get_orig_node(bat_priv, batman_packet->orig);
750 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
752 if (is_duplicate == -1) {
753 bat_dbg(DBG_BATMAN, bat_priv,
754 "Drop packet: packet within seqno protection time "
755 "(sender: %pM)\n", ethhdr->h_source);
759 if (batman_packet->tq == 0) {
760 bat_dbg(DBG_BATMAN, bat_priv,
761 "Drop packet: originator packet with tq equal 0\n");
765 /* avoid temporary routing loops */
766 if ((orig_node->router) &&
767 (orig_node->router->orig_node->router) &&
768 (compare_orig(orig_node->router->addr,
769 batman_packet->prev_sender)) &&
770 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
771 (compare_orig(orig_node->router->addr,
772 orig_node->router->orig_node->router->addr))) {
773 bat_dbg(DBG_BATMAN, bat_priv,
774 "Drop packet: ignoring all rebroadcast packets that "
775 "may make me loop (sender: %pM)\n", ethhdr->h_source);
779 /* if sender is a direct neighbor the sender mac equals
781 orig_neigh_node = (is_single_hop_neigh ?
783 get_orig_node(bat_priv, ethhdr->h_source));
784 if (!orig_neigh_node)
787 /* drop packet if sender is not a direct neighbor and if we
788 * don't route towards it */
789 if (!is_single_hop_neigh && (!orig_neigh_node->router)) {
790 bat_dbg(DBG_BATMAN, bat_priv,
791 "Drop packet: OGM via unknown neighbor!\n");
795 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
796 batman_packet, if_incoming);
798 /* update ranking if it is not a duplicate or has the same
799 * seqno and similar ttl as the non-duplicate */
800 if (is_bidirectional &&
802 ((orig_node->last_real_seqno == batman_packet->seqno) &&
803 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
804 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
805 if_incoming, hna_buff, hna_buff_len, is_duplicate);
807 mark_bonding_address(orig_node, orig_neigh_node, batman_packet);
808 update_bonding_candidates(orig_node);
810 /* is single hop (direct) neighbor */
811 if (is_single_hop_neigh) {
813 /* mark direct link on incoming interface */
814 schedule_forward_packet(orig_node, ethhdr, batman_packet,
815 1, hna_buff_len, if_incoming);
817 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
818 "rebroadcast neighbor packet with direct link flag\n");
822 /* multihop originator */
823 if (!is_bidirectional) {
824 bat_dbg(DBG_BATMAN, bat_priv,
825 "Drop packet: not received via bidirectional link\n");
830 bat_dbg(DBG_BATMAN, bat_priv,
831 "Drop packet: duplicate packet received\n");
835 bat_dbg(DBG_BATMAN, bat_priv,
836 "Forwarding packet: rebroadcast originator packet\n");
837 schedule_forward_packet(orig_node, ethhdr, batman_packet,
838 0, hna_buff_len, if_incoming);
841 if (!is_single_hop_neigh)
842 kref_put(&orig_neigh_node->refcount, orig_node_free_ref);
844 kref_put(&orig_node->refcount, orig_node_free_ref);
847 int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
849 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
850 struct ethhdr *ethhdr;
852 /* drop packet if it has not necessary minimum size */
853 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
856 ethhdr = (struct ethhdr *)skb_mac_header(skb);
858 /* packet with broadcast indication but unicast recipient */
859 if (!is_broadcast_ether_addr(ethhdr->h_dest))
862 /* packet with broadcast sender address */
863 if (is_broadcast_ether_addr(ethhdr->h_source))
866 /* create a copy of the skb, if needed, to modify it. */
867 if (skb_cow(skb, 0) < 0)
870 /* keep skb linear */
871 if (skb_linearize(skb) < 0)
874 ethhdr = (struct ethhdr *)skb_mac_header(skb);
876 spin_lock_bh(&bat_priv->orig_hash_lock);
877 receive_aggr_bat_packet(ethhdr,
881 spin_unlock_bh(&bat_priv->orig_hash_lock);
884 return NET_RX_SUCCESS;
887 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
888 struct sk_buff *skb, size_t icmp_len)
890 struct orig_node *orig_node;
891 struct icmp_packet_rr *icmp_packet;
892 struct batman_if *batman_if;
894 uint8_t dstaddr[ETH_ALEN];
896 icmp_packet = (struct icmp_packet_rr *)skb->data;
898 /* add data to device queue */
899 if (icmp_packet->msg_type != ECHO_REQUEST) {
900 bat_socket_receive_packet(icmp_packet, icmp_len);
904 if (!bat_priv->primary_if)
907 /* answer echo request (ping) */
908 /* get routing information */
909 spin_lock_bh(&bat_priv->orig_hash_lock);
911 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
912 compare_orig, choose_orig,
917 if ((orig_node) && (orig_node->router)) {
919 /* don't lock while sending the packets ... we therefore
920 * copy the required data before sending */
921 batman_if = orig_node->router->if_incoming;
922 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
923 spin_unlock_bh(&bat_priv->orig_hash_lock);
925 /* create a copy of the skb, if needed, to modify it. */
926 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
929 icmp_packet = (struct icmp_packet_rr *)skb->data;
931 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
932 memcpy(icmp_packet->orig,
933 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
934 icmp_packet->msg_type = ECHO_REPLY;
935 icmp_packet->ttl = TTL;
937 send_skb_packet(skb, batman_if, dstaddr);
938 ret = NET_RX_SUCCESS;
941 spin_unlock_bh(&bat_priv->orig_hash_lock);
946 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
949 struct orig_node *orig_node;
950 struct icmp_packet *icmp_packet;
951 struct batman_if *batman_if;
953 uint8_t dstaddr[ETH_ALEN];
955 icmp_packet = (struct icmp_packet *)skb->data;
957 /* send TTL exceeded if packet is an echo request (traceroute) */
958 if (icmp_packet->msg_type != ECHO_REQUEST) {
959 pr_debug("Warning - can't forward icmp packet from %pM to "
960 "%pM: ttl exceeded\n", icmp_packet->orig,
965 if (!bat_priv->primary_if)
968 /* get routing information */
969 spin_lock_bh(&bat_priv->orig_hash_lock);
971 orig_node = ((struct orig_node *)
972 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
977 if ((orig_node) && (orig_node->router)) {
979 /* don't lock while sending the packets ... we therefore
980 * copy the required data before sending */
981 batman_if = orig_node->router->if_incoming;
982 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
983 spin_unlock_bh(&bat_priv->orig_hash_lock);
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,
993 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
994 icmp_packet->msg_type = TTL_EXCEEDED;
995 icmp_packet->ttl = TTL;
997 send_skb_packet(skb, batman_if, dstaddr);
998 ret = NET_RX_SUCCESS;
1001 spin_unlock_bh(&bat_priv->orig_hash_lock);
1007 int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
1009 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1010 struct icmp_packet_rr *icmp_packet;
1011 struct ethhdr *ethhdr;
1012 struct orig_node *orig_node;
1013 struct batman_if *batman_if;
1014 int hdr_size = sizeof(struct icmp_packet);
1016 uint8_t dstaddr[ETH_ALEN];
1019 * we truncate all incoming icmp packets if they don't match our size
1021 if (skb->len >= sizeof(struct icmp_packet_rr))
1022 hdr_size = sizeof(struct icmp_packet_rr);
1024 /* drop packet if it has not necessary minimum size */
1025 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1028 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1030 /* packet with unicast indication but broadcast recipient */
1031 if (is_broadcast_ether_addr(ethhdr->h_dest))
1034 /* packet with broadcast sender address */
1035 if (is_broadcast_ether_addr(ethhdr->h_source))
1039 if (!is_my_mac(ethhdr->h_dest))
1042 icmp_packet = (struct icmp_packet_rr *)skb->data;
1044 /* add record route information if not full */
1045 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
1046 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1047 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1048 ethhdr->h_dest, ETH_ALEN);
1049 icmp_packet->rr_cur++;
1053 if (is_my_mac(icmp_packet->dst))
1054 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1057 if (icmp_packet->ttl < 2)
1058 return recv_icmp_ttl_exceeded(bat_priv, skb);
1062 /* get routing information */
1063 spin_lock_bh(&bat_priv->orig_hash_lock);
1065 orig_node = ((struct orig_node *)
1066 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1070 if ((orig_node) && (orig_node->router)) {
1072 /* don't lock while sending the packets ... we therefore
1073 * copy the required data before sending */
1074 batman_if = orig_node->router->if_incoming;
1075 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
1076 spin_unlock_bh(&bat_priv->orig_hash_lock);
1078 /* create a copy of the skb, if needed, to modify it. */
1079 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1082 icmp_packet = (struct icmp_packet_rr *)skb->data;
1088 send_skb_packet(skb, batman_if, dstaddr);
1089 ret = NET_RX_SUCCESS;
1092 spin_unlock_bh(&bat_priv->orig_hash_lock);
1097 /* find a suitable router for this originator, and use
1098 * bonding if possible. */
1099 struct neigh_node *find_router(struct bat_priv *bat_priv,
1100 struct orig_node *orig_node,
1101 struct batman_if *recv_if)
1103 struct orig_node *primary_orig_node;
1104 struct orig_node *router_orig;
1105 struct neigh_node *router, *first_candidate, *best_router;
1106 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1107 int bonding_enabled;
1112 if (!orig_node->router)
1115 /* without bonding, the first node should
1116 * always choose the default router. */
1118 bonding_enabled = atomic_read(&bat_priv->bonding);
1120 if ((!recv_if) && (!bonding_enabled))
1121 return orig_node->router;
1123 router_orig = orig_node->router->orig_node;
1125 /* if we have something in the primary_addr, we can search
1126 * for a potential bonding candidate. */
1127 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1128 return orig_node->router;
1130 /* find the orig_node which has the primary interface. might
1131 * even be the same as our router_orig in many cases */
1133 if (memcmp(router_orig->primary_addr,
1134 router_orig->orig, ETH_ALEN) == 0) {
1135 primary_orig_node = router_orig;
1138 primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig,
1140 router_orig->primary_addr);
1143 if (!primary_orig_node)
1144 return orig_node->router;
1147 /* with less than 2 candidates, we can't do any
1148 * bonding and prefer the original router. */
1150 if (primary_orig_node->bond.candidates < 2)
1151 return orig_node->router;
1154 /* all nodes between should choose a candidate which
1155 * is is not on the interface where the packet came
1157 first_candidate = primary_orig_node->bond.selected;
1158 router = first_candidate;
1160 if (bonding_enabled) {
1161 /* in the bonding case, send the packets in a round
1162 * robin fashion over the remaining interfaces. */
1164 /* recv_if == NULL on the first node. */
1165 if (router->if_incoming != recv_if)
1168 router = router->next_bond_candidate;
1169 } while (router != first_candidate);
1171 primary_orig_node->bond.selected = router->next_bond_candidate;
1174 /* if bonding is disabled, use the best of the
1175 * remaining candidates which are not using
1176 * this interface. */
1177 best_router = first_candidate;
1180 /* recv_if == NULL on the first node. */
1181 if ((router->if_incoming != recv_if) &&
1182 (router->tq_avg > best_router->tq_avg))
1183 best_router = router;
1185 router = router->next_bond_candidate;
1186 } while (router != first_candidate);
1188 router = best_router;
1194 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1196 struct ethhdr *ethhdr;
1198 /* drop packet if it has not necessary minimum size */
1199 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1202 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1204 /* packet with unicast indication but broadcast recipient */
1205 if (is_broadcast_ether_addr(ethhdr->h_dest))
1208 /* packet with broadcast sender address */
1209 if (is_broadcast_ether_addr(ethhdr->h_source))
1213 if (!is_my_mac(ethhdr->h_dest))
1219 int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1222 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1223 struct orig_node *orig_node;
1224 struct neigh_node *router;
1225 struct batman_if *batman_if;
1226 uint8_t dstaddr[ETH_ALEN];
1227 struct unicast_packet *unicast_packet;
1228 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1230 struct sk_buff *new_skb;
1232 unicast_packet = (struct unicast_packet *)skb->data;
1235 if (unicast_packet->ttl < 2) {
1236 pr_debug("Warning - can't forward unicast packet from %pM to "
1237 "%pM: ttl exceeded\n", ethhdr->h_source,
1238 unicast_packet->dest);
1242 /* get routing information */
1243 spin_lock_bh(&bat_priv->orig_hash_lock);
1245 orig_node = ((struct orig_node *)
1246 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1247 unicast_packet->dest));
1250 router = find_router(bat_priv, orig_node, recv_if);
1253 spin_unlock_bh(&bat_priv->orig_hash_lock);
1257 /* don't lock while sending the packets ... we therefore
1258 * copy the required data before sending */
1260 batman_if = router->if_incoming;
1261 memcpy(dstaddr, router->addr, ETH_ALEN);
1263 spin_unlock_bh(&bat_priv->orig_hash_lock);
1265 /* create a copy of the skb, if needed, to modify it. */
1266 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1269 unicast_packet = (struct unicast_packet *)skb->data;
1271 if (unicast_packet->packet_type == BAT_UNICAST &&
1272 atomic_read(&bat_priv->fragmentation) &&
1273 skb->len > batman_if->net_dev->mtu)
1274 return frag_send_skb(skb, bat_priv, batman_if,
1277 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1278 frag_can_reassemble(skb, batman_if->net_dev->mtu)) {
1280 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1282 if (ret == NET_RX_DROP)
1285 /* packet was buffered for late merge */
1287 return NET_RX_SUCCESS;
1290 unicast_packet = (struct unicast_packet *)skb->data;
1294 unicast_packet->ttl--;
1297 send_skb_packet(skb, batman_if, dstaddr);
1299 return NET_RX_SUCCESS;
1302 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1304 struct unicast_packet *unicast_packet;
1305 int hdr_size = sizeof(struct unicast_packet);
1307 if (check_unicast_packet(skb, hdr_size) < 0)
1310 unicast_packet = (struct unicast_packet *)skb->data;
1313 if (is_my_mac(unicast_packet->dest)) {
1314 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1315 return NET_RX_SUCCESS;
1318 return route_unicast_packet(skb, recv_if, hdr_size);
1321 int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1323 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1324 struct unicast_frag_packet *unicast_packet;
1325 int hdr_size = sizeof(struct unicast_frag_packet);
1326 struct sk_buff *new_skb = NULL;
1329 if (check_unicast_packet(skb, hdr_size) < 0)
1332 unicast_packet = (struct unicast_frag_packet *)skb->data;
1335 if (is_my_mac(unicast_packet->dest)) {
1337 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1339 if (ret == NET_RX_DROP)
1342 /* packet was buffered for late merge */
1344 return NET_RX_SUCCESS;
1346 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1347 sizeof(struct unicast_packet));
1348 return NET_RX_SUCCESS;
1351 return route_unicast_packet(skb, recv_if, hdr_size);
1355 int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1357 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1358 struct orig_node *orig_node;
1359 struct bcast_packet *bcast_packet;
1360 struct ethhdr *ethhdr;
1361 int hdr_size = sizeof(struct bcast_packet);
1364 /* drop packet if it has not necessary minimum size */
1365 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1368 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1370 /* packet with broadcast indication but unicast recipient */
1371 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1374 /* packet with broadcast sender address */
1375 if (is_broadcast_ether_addr(ethhdr->h_source))
1378 /* ignore broadcasts sent by myself */
1379 if (is_my_mac(ethhdr->h_source))
1382 bcast_packet = (struct bcast_packet *)skb->data;
1384 /* ignore broadcasts originated by myself */
1385 if (is_my_mac(bcast_packet->orig))
1388 if (bcast_packet->ttl < 2)
1391 spin_lock_bh(&bat_priv->orig_hash_lock);
1393 orig_node = ((struct orig_node *)
1394 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1395 bcast_packet->orig));
1399 spin_unlock_bh(&bat_priv->orig_hash_lock);
1403 /* check whether the packet is a duplicate */
1404 if (get_bit_status(orig_node->bcast_bits,
1405 orig_node->last_bcast_seqno,
1406 ntohl(bcast_packet->seqno))) {
1407 spin_unlock_bh(&bat_priv->orig_hash_lock);
1411 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1413 /* check whether the packet is old and the host just restarted. */
1414 if (window_protected(bat_priv, seq_diff,
1415 &orig_node->bcast_seqno_reset)) {
1416 spin_unlock_bh(&bat_priv->orig_hash_lock);
1420 /* mark broadcast in flood history, update window position
1422 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1423 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1425 spin_unlock_bh(&bat_priv->orig_hash_lock);
1426 /* rebroadcast packet */
1427 add_bcast_packet_to_list(bat_priv, skb);
1429 /* broadcast for me */
1430 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1432 return NET_RX_SUCCESS;
1435 int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1437 struct vis_packet *vis_packet;
1438 struct ethhdr *ethhdr;
1439 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1440 int hdr_size = sizeof(struct vis_packet);
1442 /* keep skb linear */
1443 if (skb_linearize(skb) < 0)
1446 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1449 vis_packet = (struct vis_packet *)skb->data;
1450 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1453 if (!is_my_mac(ethhdr->h_dest))
1456 /* ignore own packets */
1457 if (is_my_mac(vis_packet->vis_orig))
1460 if (is_my_mac(vis_packet->sender_orig))
1463 switch (vis_packet->vis_type) {
1464 case VIS_TYPE_SERVER_SYNC:
1465 receive_server_sync_packet(bat_priv, vis_packet,
1469 case VIS_TYPE_CLIENT_UPDATE:
1470 receive_client_update_packet(bat_priv, vis_packet,
1474 default: /* ignore unknown packet */
1478 /* We take a copy of the data in the packet, so we should
1479 always free the skbuf. */