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 /* caller must hold the neigh_list_lock */
275 void bonding_candidate_del(struct orig_node *orig_node,
276 struct neigh_node *neigh_node)
278 /* this neighbor is not part of our candidate list */
279 if (list_empty(&neigh_node->bonding_list))
282 list_del_rcu(&neigh_node->bonding_list);
283 call_rcu(&neigh_node->rcu_bond, neigh_node_free_rcu_bond);
284 INIT_LIST_HEAD(&neigh_node->bonding_list);
285 atomic_dec(&orig_node->bond_candidates);
291 static void bonding_candidate_add(struct orig_node *orig_node,
292 struct neigh_node *neigh_node)
294 struct hlist_node *node;
295 struct neigh_node *tmp_neigh_node;
296 uint8_t best_tq, interference_candidate = 0;
298 spin_lock_bh(&orig_node->neigh_list_lock);
300 /* only consider if it has the same primary address ... */
301 if (!compare_orig(orig_node->orig,
302 neigh_node->orig_node->primary_addr))
305 if (!orig_node->router)
308 best_tq = orig_node->router->tq_avg;
310 /* ... and is good enough to be considered */
311 if (neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
315 * check if we have another candidate with the same mac address or
316 * interface. If we do, we won't select this candidate because of
317 * possible interference.
319 hlist_for_each_entry_rcu(tmp_neigh_node, node,
320 &orig_node->neigh_list, list) {
322 if (tmp_neigh_node == neigh_node)
325 /* we only care if the other candidate is even
326 * considered as candidate. */
327 if (list_empty(&tmp_neigh_node->bonding_list))
330 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
331 (compare_orig(neigh_node->addr, tmp_neigh_node->addr))) {
332 interference_candidate = 1;
337 /* don't care further if it is an interference candidate */
338 if (interference_candidate)
341 /* this neighbor already is part of our candidate list */
342 if (!list_empty(&neigh_node->bonding_list))
345 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
346 kref_get(&neigh_node->refcount);
347 atomic_inc(&orig_node->bond_candidates);
351 bonding_candidate_del(orig_node, neigh_node);
354 spin_unlock_bh(&orig_node->neigh_list_lock);
358 /* copy primary address for bonding */
359 static void bonding_save_primary(struct orig_node *orig_node,
360 struct orig_node *orig_neigh_node,
361 struct batman_packet *batman_packet)
363 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
366 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
369 static void update_orig(struct bat_priv *bat_priv,
370 struct orig_node *orig_node,
371 struct ethhdr *ethhdr,
372 struct batman_packet *batman_packet,
373 struct batman_if *if_incoming,
374 unsigned char *hna_buff, int hna_buff_len,
377 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
378 struct orig_node *orig_node_tmp;
379 struct hlist_node *node;
380 int tmp_hna_buff_len;
381 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
383 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
384 "Searching and updating originator entry of received packet\n");
387 hlist_for_each_entry_rcu(tmp_neigh_node, node,
388 &orig_node->neigh_list, list) {
389 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
390 (tmp_neigh_node->if_incoming == if_incoming)) {
391 neigh_node = tmp_neigh_node;
398 ring_buffer_set(tmp_neigh_node->tq_recv,
399 &tmp_neigh_node->tq_index, 0);
400 tmp_neigh_node->tq_avg =
401 ring_buffer_avg(tmp_neigh_node->tq_recv);
405 struct orig_node *orig_tmp;
407 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
411 neigh_node = create_neighbor(orig_node, orig_tmp,
412 ethhdr->h_source, if_incoming);
414 kref_put(&orig_tmp->refcount, orig_node_free_ref);
418 bat_dbg(DBG_BATMAN, bat_priv,
419 "Updating existing last-hop neighbor of originator\n");
421 kref_get(&neigh_node->refcount);
424 orig_node->flags = batman_packet->flags;
425 neigh_node->last_valid = jiffies;
427 ring_buffer_set(neigh_node->tq_recv,
428 &neigh_node->tq_index,
430 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
433 orig_node->last_ttl = batman_packet->ttl;
434 neigh_node->last_ttl = batman_packet->ttl;
437 bonding_candidate_add(orig_node, neigh_node);
439 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
440 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
442 /* if this neighbor already is our next hop there is nothing
444 if (orig_node->router == neigh_node)
447 /* if this neighbor does not offer a better TQ we won't consider it */
448 if ((orig_node->router) &&
449 (orig_node->router->tq_avg > neigh_node->tq_avg))
452 /* if the TQ is the same and the link not more symetric we
453 * won't consider it either */
454 if ((orig_node->router) &&
455 (neigh_node->tq_avg == orig_node->router->tq_avg)) {
456 orig_node_tmp = orig_node->router->orig_node;
457 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
459 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
460 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
462 orig_node_tmp = neigh_node->orig_node;
463 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
464 bcast_own_sum_neigh =
465 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
466 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
468 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
472 update_routes(bat_priv, orig_node, neigh_node,
473 hna_buff, tmp_hna_buff_len);
477 update_routes(bat_priv, orig_node, orig_node->router,
478 hna_buff, tmp_hna_buff_len);
481 if (orig_node->gw_flags != batman_packet->gw_flags)
482 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
484 orig_node->gw_flags = batman_packet->gw_flags;
486 /* restart gateway selection if fast or late switching was enabled */
487 if ((orig_node->gw_flags) &&
488 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
489 (atomic_read(&bat_priv->gw_sel_class) > 2))
490 gw_check_election(bat_priv, orig_node);
498 kref_put(&neigh_node->refcount, neigh_node_free_ref);
501 /* checks whether the host restarted and is in the protection time.
503 * 0 if the packet is to be accepted
504 * 1 if the packet is to be ignored.
506 static int window_protected(struct bat_priv *bat_priv,
507 int32_t seq_num_diff,
508 unsigned long *last_reset)
510 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
511 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
512 if (time_after(jiffies, *last_reset +
513 msecs_to_jiffies(RESET_PROTECTION_MS))) {
515 *last_reset = jiffies;
516 bat_dbg(DBG_BATMAN, bat_priv,
517 "old packet received, start protection\n");
526 /* processes a batman packet for all interfaces, adjusts the sequence number and
527 * finds out whether it is a duplicate.
529 * 1 the packet is a duplicate
530 * 0 the packet has not yet been received
531 * -1 the packet is old and has been received while the seqno window
532 * was protected. Caller should drop it.
534 static char count_real_packets(struct ethhdr *ethhdr,
535 struct batman_packet *batman_packet,
536 struct batman_if *if_incoming)
538 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
539 struct orig_node *orig_node;
540 struct neigh_node *tmp_neigh_node;
541 struct hlist_node *node;
542 char is_duplicate = 0;
547 orig_node = get_orig_node(bat_priv, batman_packet->orig);
551 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
553 /* signalize caller that the packet is to be dropped. */
554 if (window_protected(bat_priv, seq_diff,
555 &orig_node->batman_seqno_reset))
559 hlist_for_each_entry_rcu(tmp_neigh_node, node,
560 &orig_node->neigh_list, list) {
562 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
563 orig_node->last_real_seqno,
564 batman_packet->seqno);
566 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
567 (tmp_neigh_node->if_incoming == if_incoming))
572 /* if the window moved, set the update flag. */
573 need_update |= bit_get_packet(bat_priv,
574 tmp_neigh_node->real_bits,
577 tmp_neigh_node->real_packet_count =
578 bit_packet_count(tmp_neigh_node->real_bits);
583 bat_dbg(DBG_BATMAN, bat_priv,
584 "updating last_seqno: old %d, new %d\n",
585 orig_node->last_real_seqno, batman_packet->seqno);
586 orig_node->last_real_seqno = batman_packet->seqno;
589 kref_put(&orig_node->refcount, orig_node_free_ref);
593 kref_put(&orig_node->refcount, orig_node_free_ref);
597 void receive_bat_packet(struct ethhdr *ethhdr,
598 struct batman_packet *batman_packet,
599 unsigned char *hna_buff, int hna_buff_len,
600 struct batman_if *if_incoming)
602 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
603 struct batman_if *batman_if;
604 struct orig_node *orig_neigh_node, *orig_node;
605 char has_directlink_flag;
606 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
607 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
609 uint32_t if_incoming_seqno;
611 /* Silently drop when the batman packet is actually not a
614 * This might happen if a packet is padded (e.g. Ethernet has a
615 * minimum frame length of 64 byte) and the aggregation interprets
616 * it as an additional length.
618 * TODO: A more sane solution would be to have a bit in the
619 * batman_packet to detect whether the packet is the last
620 * packet in an aggregation. Here we expect that the padding
621 * is always zero (or not 0x01)
623 if (batman_packet->packet_type != BAT_PACKET)
626 /* could be changed by schedule_own_packet() */
627 if_incoming_seqno = atomic_read(&if_incoming->seqno);
629 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
631 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
632 batman_packet->orig) ? 1 : 0);
634 bat_dbg(DBG_BATMAN, bat_priv,
635 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
636 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
637 "TTL %d, V %d, IDF %d)\n",
638 ethhdr->h_source, if_incoming->net_dev->name,
639 if_incoming->net_dev->dev_addr, batman_packet->orig,
640 batman_packet->prev_sender, batman_packet->seqno,
641 batman_packet->tq, batman_packet->ttl, batman_packet->version,
642 has_directlink_flag);
645 list_for_each_entry_rcu(batman_if, &if_list, list) {
646 if (batman_if->if_status != IF_ACTIVE)
649 if (batman_if->soft_iface != if_incoming->soft_iface)
652 if (compare_orig(ethhdr->h_source,
653 batman_if->net_dev->dev_addr))
656 if (compare_orig(batman_packet->orig,
657 batman_if->net_dev->dev_addr))
660 if (compare_orig(batman_packet->prev_sender,
661 batman_if->net_dev->dev_addr))
664 if (compare_orig(ethhdr->h_source, broadcast_addr))
669 if (batman_packet->version != COMPAT_VERSION) {
670 bat_dbg(DBG_BATMAN, bat_priv,
671 "Drop packet: incompatible batman version (%i)\n",
672 batman_packet->version);
677 bat_dbg(DBG_BATMAN, bat_priv,
678 "Drop packet: received my own broadcast (sender: %pM"
685 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
686 "ignoring all packets with broadcast source addr (sender: %pM"
687 ")\n", ethhdr->h_source);
695 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
696 if (!orig_neigh_node)
699 /* neighbor has to indicate direct link and it has to
700 * come via the corresponding interface */
701 /* if received seqno equals last send seqno save new
702 * seqno for bidirectional check */
703 if (has_directlink_flag &&
704 compare_orig(if_incoming->net_dev->dev_addr,
705 batman_packet->orig) &&
706 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
707 offset = if_incoming->if_num * NUM_WORDS;
709 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
710 word = &(orig_neigh_node->bcast_own[offset]);
712 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
713 bit_packet_count(word);
714 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
717 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
718 "originator packet from myself (via neighbor)\n");
719 kref_put(&orig_neigh_node->refcount, orig_node_free_ref);
724 bat_dbg(DBG_BATMAN, bat_priv,
725 "Drop packet: ignoring all rebroadcast echos (sender: "
726 "%pM)\n", ethhdr->h_source);
730 orig_node = get_orig_node(bat_priv, batman_packet->orig);
734 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
736 if (is_duplicate == -1) {
737 bat_dbg(DBG_BATMAN, bat_priv,
738 "Drop packet: packet within seqno protection time "
739 "(sender: %pM)\n", ethhdr->h_source);
743 if (batman_packet->tq == 0) {
744 bat_dbg(DBG_BATMAN, bat_priv,
745 "Drop packet: originator packet with tq equal 0\n");
749 /* avoid temporary routing loops */
750 if ((orig_node->router) &&
751 (orig_node->router->orig_node->router) &&
752 (compare_orig(orig_node->router->addr,
753 batman_packet->prev_sender)) &&
754 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
755 (compare_orig(orig_node->router->addr,
756 orig_node->router->orig_node->router->addr))) {
757 bat_dbg(DBG_BATMAN, bat_priv,
758 "Drop packet: ignoring all rebroadcast packets that "
759 "may make me loop (sender: %pM)\n", ethhdr->h_source);
763 /* if sender is a direct neighbor the sender mac equals
765 orig_neigh_node = (is_single_hop_neigh ?
767 get_orig_node(bat_priv, ethhdr->h_source));
768 if (!orig_neigh_node)
771 /* drop packet if sender is not a direct neighbor and if we
772 * don't route towards it */
773 if (!is_single_hop_neigh && (!orig_neigh_node->router)) {
774 bat_dbg(DBG_BATMAN, bat_priv,
775 "Drop packet: OGM via unknown neighbor!\n");
779 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
780 batman_packet, if_incoming);
782 bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
784 /* update ranking if it is not a duplicate or has the same
785 * seqno and similar ttl as the non-duplicate */
786 if (is_bidirectional &&
788 ((orig_node->last_real_seqno == batman_packet->seqno) &&
789 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
790 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
791 if_incoming, hna_buff, hna_buff_len, is_duplicate);
793 /* is single hop (direct) neighbor */
794 if (is_single_hop_neigh) {
796 /* mark direct link on incoming interface */
797 schedule_forward_packet(orig_node, ethhdr, batman_packet,
798 1, hna_buff_len, if_incoming);
800 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
801 "rebroadcast neighbor packet with direct link flag\n");
805 /* multihop originator */
806 if (!is_bidirectional) {
807 bat_dbg(DBG_BATMAN, bat_priv,
808 "Drop packet: not received via bidirectional link\n");
813 bat_dbg(DBG_BATMAN, bat_priv,
814 "Drop packet: duplicate packet received\n");
818 bat_dbg(DBG_BATMAN, bat_priv,
819 "Forwarding packet: rebroadcast originator packet\n");
820 schedule_forward_packet(orig_node, ethhdr, batman_packet,
821 0, hna_buff_len, if_incoming);
824 if (!is_single_hop_neigh)
825 kref_put(&orig_neigh_node->refcount, orig_node_free_ref);
827 kref_put(&orig_node->refcount, orig_node_free_ref);
830 int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
832 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
833 struct ethhdr *ethhdr;
835 /* drop packet if it has not necessary minimum size */
836 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
839 ethhdr = (struct ethhdr *)skb_mac_header(skb);
841 /* packet with broadcast indication but unicast recipient */
842 if (!is_broadcast_ether_addr(ethhdr->h_dest))
845 /* packet with broadcast sender address */
846 if (is_broadcast_ether_addr(ethhdr->h_source))
849 /* create a copy of the skb, if needed, to modify it. */
850 if (skb_cow(skb, 0) < 0)
853 /* keep skb linear */
854 if (skb_linearize(skb) < 0)
857 ethhdr = (struct ethhdr *)skb_mac_header(skb);
859 spin_lock_bh(&bat_priv->orig_hash_lock);
860 receive_aggr_bat_packet(ethhdr,
864 spin_unlock_bh(&bat_priv->orig_hash_lock);
867 return NET_RX_SUCCESS;
870 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
871 struct sk_buff *skb, size_t icmp_len)
873 struct orig_node *orig_node;
874 struct icmp_packet_rr *icmp_packet;
875 struct batman_if *batman_if;
877 uint8_t dstaddr[ETH_ALEN];
879 icmp_packet = (struct icmp_packet_rr *)skb->data;
881 /* add data to device queue */
882 if (icmp_packet->msg_type != ECHO_REQUEST) {
883 bat_socket_receive_packet(icmp_packet, icmp_len);
887 if (!bat_priv->primary_if)
890 /* answer echo request (ping) */
891 /* get routing information */
892 spin_lock_bh(&bat_priv->orig_hash_lock);
894 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
895 compare_orig, choose_orig,
900 if ((orig_node) && (orig_node->router)) {
902 /* don't lock while sending the packets ... we therefore
903 * copy the required data before sending */
904 batman_if = orig_node->router->if_incoming;
905 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
906 spin_unlock_bh(&bat_priv->orig_hash_lock);
908 /* create a copy of the skb, if needed, to modify it. */
909 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
912 icmp_packet = (struct icmp_packet_rr *)skb->data;
914 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
915 memcpy(icmp_packet->orig,
916 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
917 icmp_packet->msg_type = ECHO_REPLY;
918 icmp_packet->ttl = TTL;
920 send_skb_packet(skb, batman_if, dstaddr);
921 ret = NET_RX_SUCCESS;
924 spin_unlock_bh(&bat_priv->orig_hash_lock);
929 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
932 struct orig_node *orig_node;
933 struct icmp_packet *icmp_packet;
934 struct batman_if *batman_if;
936 uint8_t dstaddr[ETH_ALEN];
938 icmp_packet = (struct icmp_packet *)skb->data;
940 /* send TTL exceeded if packet is an echo request (traceroute) */
941 if (icmp_packet->msg_type != ECHO_REQUEST) {
942 pr_debug("Warning - can't forward icmp packet from %pM to "
943 "%pM: ttl exceeded\n", icmp_packet->orig,
948 if (!bat_priv->primary_if)
951 /* get routing information */
952 spin_lock_bh(&bat_priv->orig_hash_lock);
954 orig_node = ((struct orig_node *)
955 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
960 if ((orig_node) && (orig_node->router)) {
962 /* don't lock while sending the packets ... we therefore
963 * copy the required data before sending */
964 batman_if = orig_node->router->if_incoming;
965 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
966 spin_unlock_bh(&bat_priv->orig_hash_lock);
968 /* create a copy of the skb, if needed, to modify it. */
969 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
972 icmp_packet = (struct icmp_packet *) skb->data;
974 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
975 memcpy(icmp_packet->orig,
976 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
977 icmp_packet->msg_type = TTL_EXCEEDED;
978 icmp_packet->ttl = TTL;
980 send_skb_packet(skb, batman_if, dstaddr);
981 ret = NET_RX_SUCCESS;
984 spin_unlock_bh(&bat_priv->orig_hash_lock);
990 int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
992 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
993 struct icmp_packet_rr *icmp_packet;
994 struct ethhdr *ethhdr;
995 struct orig_node *orig_node;
996 struct batman_if *batman_if;
997 int hdr_size = sizeof(struct icmp_packet);
999 uint8_t dstaddr[ETH_ALEN];
1002 * we truncate all incoming icmp packets if they don't match our size
1004 if (skb->len >= sizeof(struct icmp_packet_rr))
1005 hdr_size = sizeof(struct icmp_packet_rr);
1007 /* drop packet if it has not necessary minimum size */
1008 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1011 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1013 /* packet with unicast indication but broadcast recipient */
1014 if (is_broadcast_ether_addr(ethhdr->h_dest))
1017 /* packet with broadcast sender address */
1018 if (is_broadcast_ether_addr(ethhdr->h_source))
1022 if (!is_my_mac(ethhdr->h_dest))
1025 icmp_packet = (struct icmp_packet_rr *)skb->data;
1027 /* add record route information if not full */
1028 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
1029 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1030 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1031 ethhdr->h_dest, ETH_ALEN);
1032 icmp_packet->rr_cur++;
1036 if (is_my_mac(icmp_packet->dst))
1037 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1040 if (icmp_packet->ttl < 2)
1041 return recv_icmp_ttl_exceeded(bat_priv, skb);
1045 /* get routing information */
1046 spin_lock_bh(&bat_priv->orig_hash_lock);
1048 orig_node = ((struct orig_node *)
1049 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1053 if ((orig_node) && (orig_node->router)) {
1055 /* don't lock while sending the packets ... we therefore
1056 * copy the required data before sending */
1057 batman_if = orig_node->router->if_incoming;
1058 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
1059 spin_unlock_bh(&bat_priv->orig_hash_lock);
1061 /* create a copy of the skb, if needed, to modify it. */
1062 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1065 icmp_packet = (struct icmp_packet_rr *)skb->data;
1071 send_skb_packet(skb, batman_if, dstaddr);
1072 ret = NET_RX_SUCCESS;
1075 spin_unlock_bh(&bat_priv->orig_hash_lock);
1080 /* find a suitable router for this originator, and use
1081 * bonding if possible. increases the found neighbors
1083 struct neigh_node *find_router(struct bat_priv *bat_priv,
1084 struct orig_node *orig_node,
1085 struct batman_if *recv_if)
1087 struct orig_node *primary_orig_node;
1088 struct orig_node *router_orig;
1089 struct neigh_node *router, *first_candidate, *tmp_neigh_node;
1090 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1091 int bonding_enabled;
1096 if (!orig_node->router)
1099 /* without bonding, the first node should
1100 * always choose the default router. */
1101 bonding_enabled = atomic_read(&bat_priv->bonding);
1104 /* select default router to output */
1105 router = orig_node->router;
1106 router_orig = orig_node->router->orig_node;
1113 if ((!recv_if) && (!bonding_enabled))
1116 /* if we have something in the primary_addr, we can search
1117 * for a potential bonding candidate. */
1118 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1121 /* find the orig_node which has the primary interface. might
1122 * even be the same as our router_orig in many cases */
1124 if (memcmp(router_orig->primary_addr,
1125 router_orig->orig, ETH_ALEN) == 0) {
1126 primary_orig_node = router_orig;
1128 primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig,
1130 router_orig->primary_addr);
1131 if (!primary_orig_node)
1135 /* with less than 2 candidates, we can't do any
1136 * bonding and prefer the original router. */
1137 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
1141 /* all nodes between should choose a candidate which
1142 * is is not on the interface where the packet came
1145 first_candidate = NULL;
1148 if (bonding_enabled) {
1149 /* in the bonding case, send the packets in a round
1150 * robin fashion over the remaining interfaces. */
1152 list_for_each_entry_rcu(tmp_neigh_node,
1153 &primary_orig_node->bond_list, bonding_list) {
1154 if (!first_candidate)
1155 first_candidate = tmp_neigh_node;
1156 /* recv_if == NULL on the first node. */
1157 if (tmp_neigh_node->if_incoming != recv_if) {
1158 router = tmp_neigh_node;
1163 /* use the first candidate if nothing was found. */
1165 router = first_candidate;
1167 /* selected should point to the next element
1168 * after the current router */
1169 spin_lock_bh(&primary_orig_node->neigh_list_lock);
1170 /* this is a list_move(), which unfortunately
1171 * does not exist as rcu version */
1172 list_del_rcu(&primary_orig_node->bond_list);
1173 list_add_rcu(&primary_orig_node->bond_list,
1174 &router->bonding_list);
1175 spin_unlock_bh(&primary_orig_node->neigh_list_lock);
1178 /* if bonding is disabled, use the best of the
1179 * remaining candidates which are not using
1180 * this interface. */
1181 list_for_each_entry_rcu(tmp_neigh_node,
1182 &primary_orig_node->bond_list, bonding_list) {
1183 if (!first_candidate)
1184 first_candidate = tmp_neigh_node;
1186 /* recv_if == NULL on the first node. */
1187 if (tmp_neigh_node->if_incoming != recv_if)
1188 /* if we don't have a router yet
1189 * or this one is better, choose it. */
1191 (tmp_neigh_node->tq_avg > router->tq_avg)) {
1192 router = tmp_neigh_node;
1196 /* use the first candidate if nothing was found. */
1198 router = first_candidate;
1201 kref_get(&router->refcount);
1206 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1208 struct ethhdr *ethhdr;
1210 /* drop packet if it has not necessary minimum size */
1211 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1214 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1216 /* packet with unicast indication but broadcast recipient */
1217 if (is_broadcast_ether_addr(ethhdr->h_dest))
1220 /* packet with broadcast sender address */
1221 if (is_broadcast_ether_addr(ethhdr->h_source))
1225 if (!is_my_mac(ethhdr->h_dest))
1231 int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1234 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1235 struct orig_node *orig_node;
1236 struct neigh_node *router;
1237 struct batman_if *batman_if;
1238 uint8_t dstaddr[ETH_ALEN];
1239 struct unicast_packet *unicast_packet;
1240 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1242 struct sk_buff *new_skb;
1244 unicast_packet = (struct unicast_packet *)skb->data;
1247 if (unicast_packet->ttl < 2) {
1248 pr_debug("Warning - can't forward unicast packet from %pM to "
1249 "%pM: ttl exceeded\n", ethhdr->h_source,
1250 unicast_packet->dest);
1254 /* get routing information */
1255 spin_lock_bh(&bat_priv->orig_hash_lock);
1257 orig_node = ((struct orig_node *)
1258 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1259 unicast_packet->dest));
1262 /* find_router() increases neigh_nodes refcount if found. */
1263 router = find_router(bat_priv, orig_node, recv_if);
1266 spin_unlock_bh(&bat_priv->orig_hash_lock);
1270 /* don't lock while sending the packets ... we therefore
1271 * copy the required data before sending */
1273 batman_if = router->if_incoming;
1274 memcpy(dstaddr, router->addr, ETH_ALEN);
1276 spin_unlock_bh(&bat_priv->orig_hash_lock);
1278 /* create a copy of the skb, if needed, to modify it. */
1279 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1282 unicast_packet = (struct unicast_packet *)skb->data;
1284 if (unicast_packet->packet_type == BAT_UNICAST &&
1285 atomic_read(&bat_priv->fragmentation) &&
1286 skb->len > batman_if->net_dev->mtu)
1287 return frag_send_skb(skb, bat_priv, batman_if,
1290 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1291 frag_can_reassemble(skb, batman_if->net_dev->mtu)) {
1293 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1295 if (ret == NET_RX_DROP)
1298 /* packet was buffered for late merge */
1300 return NET_RX_SUCCESS;
1303 unicast_packet = (struct unicast_packet *)skb->data;
1307 unicast_packet->ttl--;
1310 send_skb_packet(skb, batman_if, dstaddr);
1312 return NET_RX_SUCCESS;
1315 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1317 struct unicast_packet *unicast_packet;
1318 int hdr_size = sizeof(struct unicast_packet);
1320 if (check_unicast_packet(skb, hdr_size) < 0)
1323 unicast_packet = (struct unicast_packet *)skb->data;
1326 if (is_my_mac(unicast_packet->dest)) {
1327 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1328 return NET_RX_SUCCESS;
1331 return route_unicast_packet(skb, recv_if, hdr_size);
1334 int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1336 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1337 struct unicast_frag_packet *unicast_packet;
1338 int hdr_size = sizeof(struct unicast_frag_packet);
1339 struct sk_buff *new_skb = NULL;
1342 if (check_unicast_packet(skb, hdr_size) < 0)
1345 unicast_packet = (struct unicast_frag_packet *)skb->data;
1348 if (is_my_mac(unicast_packet->dest)) {
1350 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1352 if (ret == NET_RX_DROP)
1355 /* packet was buffered for late merge */
1357 return NET_RX_SUCCESS;
1359 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1360 sizeof(struct unicast_packet));
1361 return NET_RX_SUCCESS;
1364 return route_unicast_packet(skb, recv_if, hdr_size);
1368 int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1370 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1371 struct orig_node *orig_node;
1372 struct bcast_packet *bcast_packet;
1373 struct ethhdr *ethhdr;
1374 int hdr_size = sizeof(struct bcast_packet);
1377 /* drop packet if it has not necessary minimum size */
1378 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1381 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1383 /* packet with broadcast indication but unicast recipient */
1384 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1387 /* packet with broadcast sender address */
1388 if (is_broadcast_ether_addr(ethhdr->h_source))
1391 /* ignore broadcasts sent by myself */
1392 if (is_my_mac(ethhdr->h_source))
1395 bcast_packet = (struct bcast_packet *)skb->data;
1397 /* ignore broadcasts originated by myself */
1398 if (is_my_mac(bcast_packet->orig))
1401 if (bcast_packet->ttl < 2)
1404 spin_lock_bh(&bat_priv->orig_hash_lock);
1406 orig_node = ((struct orig_node *)
1407 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1408 bcast_packet->orig));
1412 spin_unlock_bh(&bat_priv->orig_hash_lock);
1416 /* check whether the packet is a duplicate */
1417 if (get_bit_status(orig_node->bcast_bits,
1418 orig_node->last_bcast_seqno,
1419 ntohl(bcast_packet->seqno))) {
1420 spin_unlock_bh(&bat_priv->orig_hash_lock);
1424 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1426 /* check whether the packet is old and the host just restarted. */
1427 if (window_protected(bat_priv, seq_diff,
1428 &orig_node->bcast_seqno_reset)) {
1429 spin_unlock_bh(&bat_priv->orig_hash_lock);
1433 /* mark broadcast in flood history, update window position
1435 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1436 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1438 spin_unlock_bh(&bat_priv->orig_hash_lock);
1439 /* rebroadcast packet */
1440 add_bcast_packet_to_list(bat_priv, skb);
1442 /* broadcast for me */
1443 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1445 return NET_RX_SUCCESS;
1448 int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1450 struct vis_packet *vis_packet;
1451 struct ethhdr *ethhdr;
1452 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1453 int hdr_size = sizeof(struct vis_packet);
1455 /* keep skb linear */
1456 if (skb_linearize(skb) < 0)
1459 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1462 vis_packet = (struct vis_packet *)skb->data;
1463 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1466 if (!is_my_mac(ethhdr->h_dest))
1469 /* ignore own packets */
1470 if (is_my_mac(vis_packet->vis_orig))
1473 if (is_my_mac(vis_packet->sender_orig))
1476 switch (vis_packet->vis_type) {
1477 case VIS_TYPE_SERVER_SYNC:
1478 receive_server_sync_packet(bat_priv, vis_packet,
1482 case VIS_TYPE_CLIENT_UPDATE:
1483 receive_client_update_packet(bat_priv, vis_packet,
1487 default: /* ignore unknown packet */
1491 /* We take a copy of the data in the packet, so we should
1492 always free the skbuf. */