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];
55 hlist_for_each_entry(bucket, walk, head, hlist) {
56 orig_node = bucket->data;
57 word_index = batman_if->if_num * NUM_WORDS;
58 word = &(orig_node->bcast_own[word_index]);
60 bit_get_packet(bat_priv, word, 1, 0);
61 orig_node->bcast_own_sum[batman_if->if_num] =
62 bit_packet_count(word);
66 spin_unlock_bh(&bat_priv->orig_hash_lock);
69 static void update_HNA(struct bat_priv *bat_priv, struct orig_node *orig_node,
70 unsigned char *hna_buff, int hna_buff_len)
72 if ((hna_buff_len != orig_node->hna_buff_len) ||
73 ((hna_buff_len > 0) &&
74 (orig_node->hna_buff_len > 0) &&
75 (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
77 if (orig_node->hna_buff_len > 0)
78 hna_global_del_orig(bat_priv, orig_node,
79 "originator changed hna");
81 if ((hna_buff_len > 0) && (hna_buff))
82 hna_global_add_orig(bat_priv, orig_node,
83 hna_buff, hna_buff_len);
87 static void update_route(struct bat_priv *bat_priv,
88 struct orig_node *orig_node,
89 struct neigh_node *neigh_node,
90 unsigned char *hna_buff, int hna_buff_len)
92 struct neigh_node *neigh_node_tmp;
95 if ((orig_node->router) && (!neigh_node)) {
97 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
99 hna_global_del_orig(bat_priv, orig_node,
100 "originator timed out");
103 } else if ((!orig_node->router) && (neigh_node)) {
105 bat_dbg(DBG_ROUTES, bat_priv,
106 "Adding route towards: %pM (via %pM)\n",
107 orig_node->orig, neigh_node->addr);
108 hna_global_add_orig(bat_priv, orig_node,
109 hna_buff, hna_buff_len);
113 bat_dbg(DBG_ROUTES, bat_priv,
114 "Changing route towards: %pM "
115 "(now via %pM - was via %pM)\n",
116 orig_node->orig, neigh_node->addr,
117 orig_node->router->addr);
121 kref_get(&neigh_node->refcount);
122 neigh_node_tmp = orig_node->router;
123 orig_node->router = neigh_node;
125 kref_put(&neigh_node_tmp->refcount, neigh_node_free_ref);
129 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
130 struct neigh_node *neigh_node, unsigned char *hna_buff,
137 if (orig_node->router != neigh_node)
138 update_route(bat_priv, orig_node, neigh_node,
139 hna_buff, hna_buff_len);
140 /* may be just HNA changed */
142 update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
145 static int is_bidirectional_neigh(struct orig_node *orig_node,
146 struct orig_node *orig_neigh_node,
147 struct batman_packet *batman_packet,
148 struct batman_if *if_incoming)
150 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
151 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
152 unsigned char total_count;
154 if (orig_node == orig_neigh_node) {
155 list_for_each_entry(tmp_neigh_node,
156 &orig_node->neigh_list,
159 if (compare_orig(tmp_neigh_node->addr,
160 orig_neigh_node->orig) &&
161 (tmp_neigh_node->if_incoming == if_incoming))
162 neigh_node = tmp_neigh_node;
166 neigh_node = create_neighbor(orig_node,
168 orig_neigh_node->orig,
170 /* create_neighbor failed, return 0 */
174 neigh_node->last_valid = jiffies;
176 /* find packet count of corresponding one hop neighbor */
177 list_for_each_entry(tmp_neigh_node,
178 &orig_neigh_node->neigh_list, list) {
180 if (compare_orig(tmp_neigh_node->addr,
181 orig_neigh_node->orig) &&
182 (tmp_neigh_node->if_incoming == if_incoming))
183 neigh_node = tmp_neigh_node;
187 neigh_node = create_neighbor(orig_neigh_node,
189 orig_neigh_node->orig,
191 /* create_neighbor failed, return 0 */
196 orig_node->last_valid = jiffies;
198 /* pay attention to not get a value bigger than 100 % */
199 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
200 neigh_node->real_packet_count ?
201 neigh_node->real_packet_count :
202 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
204 /* if we have too few packets (too less data) we set tq_own to zero */
205 /* if we receive too few packets it is not considered bidirectional */
206 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
207 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
208 orig_neigh_node->tq_own = 0;
210 /* neigh_node->real_packet_count is never zero as we
211 * only purge old information when getting new
213 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
214 neigh_node->real_packet_count;
217 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
218 * affect the nearly-symmetric links only a little, but
219 * punishes asymmetric links more. This will give a value
220 * between 0 and TQ_MAX_VALUE
222 orig_neigh_node->tq_asym_penalty =
225 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
226 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
227 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
228 (TQ_LOCAL_WINDOW_SIZE *
229 TQ_LOCAL_WINDOW_SIZE *
230 TQ_LOCAL_WINDOW_SIZE);
232 batman_packet->tq = ((batman_packet->tq *
233 orig_neigh_node->tq_own *
234 orig_neigh_node->tq_asym_penalty) /
235 (TQ_MAX_VALUE * TQ_MAX_VALUE));
237 bat_dbg(DBG_BATMAN, bat_priv,
239 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
240 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
242 orig_node->orig, orig_neigh_node->orig, total_count,
243 neigh_node->real_packet_count, orig_neigh_node->tq_own,
244 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
246 /* if link has the minimum required transmission quality
247 * consider it bidirectional */
248 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
254 static void update_orig(struct bat_priv *bat_priv,
255 struct orig_node *orig_node,
256 struct ethhdr *ethhdr,
257 struct batman_packet *batman_packet,
258 struct batman_if *if_incoming,
259 unsigned char *hna_buff, int hna_buff_len,
262 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
263 int tmp_hna_buff_len;
265 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
266 "Searching and updating originator entry of received packet\n");
268 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
269 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
270 (tmp_neigh_node->if_incoming == if_incoming)) {
271 neigh_node = tmp_neigh_node;
278 ring_buffer_set(tmp_neigh_node->tq_recv,
279 &tmp_neigh_node->tq_index, 0);
280 tmp_neigh_node->tq_avg =
281 ring_buffer_avg(tmp_neigh_node->tq_recv);
285 struct orig_node *orig_tmp;
287 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
291 neigh_node = create_neighbor(orig_node, orig_tmp,
292 ethhdr->h_source, if_incoming);
296 bat_dbg(DBG_BATMAN, bat_priv,
297 "Updating existing last-hop neighbor of originator\n");
299 orig_node->flags = batman_packet->flags;
300 neigh_node->last_valid = jiffies;
302 ring_buffer_set(neigh_node->tq_recv,
303 &neigh_node->tq_index,
305 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
308 orig_node->last_ttl = batman_packet->ttl;
309 neigh_node->last_ttl = batman_packet->ttl;
312 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
313 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
315 /* if this neighbor already is our next hop there is nothing
317 if (orig_node->router == neigh_node)
320 /* if this neighbor does not offer a better TQ we won't consider it */
321 if ((orig_node->router) &&
322 (orig_node->router->tq_avg > neigh_node->tq_avg))
325 /* if the TQ is the same and the link not more symetric we
326 * won't consider it either */
327 if ((orig_node->router) &&
328 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
329 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
330 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
333 update_routes(bat_priv, orig_node, neigh_node,
334 hna_buff, tmp_hna_buff_len);
338 update_routes(bat_priv, orig_node, orig_node->router,
339 hna_buff, tmp_hna_buff_len);
342 if (orig_node->gw_flags != batman_packet->gw_flags)
343 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
345 orig_node->gw_flags = batman_packet->gw_flags;
347 /* restart gateway selection if fast or late switching was enabled */
348 if ((orig_node->gw_flags) &&
349 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
350 (atomic_read(&bat_priv->gw_sel_class) > 2))
351 gw_check_election(bat_priv, orig_node);
354 /* checks whether the host restarted and is in the protection time.
356 * 0 if the packet is to be accepted
357 * 1 if the packet is to be ignored.
359 static int window_protected(struct bat_priv *bat_priv,
360 int32_t seq_num_diff,
361 unsigned long *last_reset)
363 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
364 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
365 if (time_after(jiffies, *last_reset +
366 msecs_to_jiffies(RESET_PROTECTION_MS))) {
368 *last_reset = jiffies;
369 bat_dbg(DBG_BATMAN, bat_priv,
370 "old packet received, start protection\n");
379 /* processes a batman packet for all interfaces, adjusts the sequence number and
380 * finds out whether it is a duplicate.
382 * 1 the packet is a duplicate
383 * 0 the packet has not yet been received
384 * -1 the packet is old and has been received while the seqno window
385 * was protected. Caller should drop it.
387 static char count_real_packets(struct ethhdr *ethhdr,
388 struct batman_packet *batman_packet,
389 struct batman_if *if_incoming)
391 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
392 struct orig_node *orig_node;
393 struct neigh_node *tmp_neigh_node;
394 char is_duplicate = 0;
399 orig_node = get_orig_node(bat_priv, batman_packet->orig);
403 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
405 /* signalize caller that the packet is to be dropped. */
406 if (window_protected(bat_priv, seq_diff,
407 &orig_node->batman_seqno_reset))
410 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
412 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
413 orig_node->last_real_seqno,
414 batman_packet->seqno);
416 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
417 (tmp_neigh_node->if_incoming == if_incoming))
422 /* if the window moved, set the update flag. */
423 need_update |= bit_get_packet(bat_priv,
424 tmp_neigh_node->real_bits,
427 tmp_neigh_node->real_packet_count =
428 bit_packet_count(tmp_neigh_node->real_bits);
432 bat_dbg(DBG_BATMAN, bat_priv,
433 "updating last_seqno: old %d, new %d\n",
434 orig_node->last_real_seqno, batman_packet->seqno);
435 orig_node->last_real_seqno = batman_packet->seqno;
441 /* copy primary address for bonding */
442 static void mark_bonding_address(struct orig_node *orig_node,
443 struct orig_node *orig_neigh_node,
444 struct batman_packet *batman_packet)
447 if (batman_packet->flags & PRIMARIES_FIRST_HOP)
448 memcpy(orig_neigh_node->primary_addr,
449 orig_node->orig, ETH_ALEN);
454 /* mark possible bond.candidates in the neighbor list */
455 void update_bonding_candidates(struct orig_node *orig_node)
458 int interference_candidate;
460 struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
461 struct neigh_node *first_candidate, *last_candidate;
463 /* update the candidates for this originator */
464 if (!orig_node->router) {
465 orig_node->bond.candidates = 0;
469 best_tq = orig_node->router->tq_avg;
471 /* update bond.candidates */
475 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
476 * as "bonding partner" */
478 /* first, zero the list */
479 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
480 tmp_neigh_node->next_bond_candidate = NULL;
483 first_candidate = NULL;
484 last_candidate = NULL;
485 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
487 /* only consider if it has the same primary address ... */
488 if (memcmp(orig_node->orig,
489 tmp_neigh_node->orig_node->primary_addr,
493 /* ... and is good enough to be considered */
494 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
497 /* check if we have another candidate with the same
498 * mac address or interface. If we do, we won't
499 * select this candidate because of possible interference. */
501 interference_candidate = 0;
502 list_for_each_entry(tmp_neigh_node2,
503 &orig_node->neigh_list, list) {
505 if (tmp_neigh_node2 == tmp_neigh_node)
508 /* we only care if the other candidate is even
509 * considered as candidate. */
510 if (!tmp_neigh_node2->next_bond_candidate)
514 if ((tmp_neigh_node->if_incoming ==
515 tmp_neigh_node2->if_incoming)
516 || (memcmp(tmp_neigh_node->addr,
517 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
519 interference_candidate = 1;
523 /* don't care further if it is an interference candidate */
524 if (interference_candidate)
527 if (!first_candidate) {
528 first_candidate = tmp_neigh_node;
529 tmp_neigh_node->next_bond_candidate = first_candidate;
531 tmp_neigh_node->next_bond_candidate = last_candidate;
533 last_candidate = tmp_neigh_node;
538 if (candidates > 0) {
539 first_candidate->next_bond_candidate = last_candidate;
540 orig_node->bond.selected = first_candidate;
543 orig_node->bond.candidates = candidates;
546 void receive_bat_packet(struct ethhdr *ethhdr,
547 struct batman_packet *batman_packet,
548 unsigned char *hna_buff, int hna_buff_len,
549 struct batman_if *if_incoming)
551 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
552 struct batman_if *batman_if;
553 struct orig_node *orig_neigh_node, *orig_node;
554 char has_directlink_flag;
555 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
556 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
558 uint32_t if_incoming_seqno;
560 /* Silently drop when the batman packet is actually not a
563 * This might happen if a packet is padded (e.g. Ethernet has a
564 * minimum frame length of 64 byte) and the aggregation interprets
565 * it as an additional length.
567 * TODO: A more sane solution would be to have a bit in the
568 * batman_packet to detect whether the packet is the last
569 * packet in an aggregation. Here we expect that the padding
570 * is always zero (or not 0x01)
572 if (batman_packet->packet_type != BAT_PACKET)
575 /* could be changed by schedule_own_packet() */
576 if_incoming_seqno = atomic_read(&if_incoming->seqno);
578 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
580 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
581 batman_packet->orig) ? 1 : 0);
583 bat_dbg(DBG_BATMAN, bat_priv,
584 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
585 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
586 "TTL %d, V %d, IDF %d)\n",
587 ethhdr->h_source, if_incoming->net_dev->name,
588 if_incoming->net_dev->dev_addr, batman_packet->orig,
589 batman_packet->prev_sender, batman_packet->seqno,
590 batman_packet->tq, batman_packet->ttl, batman_packet->version,
591 has_directlink_flag);
594 list_for_each_entry_rcu(batman_if, &if_list, list) {
595 if (batman_if->if_status != IF_ACTIVE)
598 if (batman_if->soft_iface != if_incoming->soft_iface)
601 if (compare_orig(ethhdr->h_source,
602 batman_if->net_dev->dev_addr))
605 if (compare_orig(batman_packet->orig,
606 batman_if->net_dev->dev_addr))
609 if (compare_orig(batman_packet->prev_sender,
610 batman_if->net_dev->dev_addr))
613 if (compare_orig(ethhdr->h_source, broadcast_addr))
618 if (batman_packet->version != COMPAT_VERSION) {
619 bat_dbg(DBG_BATMAN, bat_priv,
620 "Drop packet: incompatible batman version (%i)\n",
621 batman_packet->version);
626 bat_dbg(DBG_BATMAN, bat_priv,
627 "Drop packet: received my own broadcast (sender: %pM"
634 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
635 "ignoring all packets with broadcast source addr (sender: %pM"
636 ")\n", ethhdr->h_source);
644 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
646 if (!orig_neigh_node)
649 /* neighbor has to indicate direct link and it has to
650 * come via the corresponding interface */
651 /* if received seqno equals last send seqno save new
652 * seqno for bidirectional check */
653 if (has_directlink_flag &&
654 compare_orig(if_incoming->net_dev->dev_addr,
655 batman_packet->orig) &&
656 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
657 offset = if_incoming->if_num * NUM_WORDS;
658 word = &(orig_neigh_node->bcast_own[offset]);
660 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
661 bit_packet_count(word);
664 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
665 "originator packet from myself (via neighbor)\n");
670 bat_dbg(DBG_BATMAN, bat_priv,
671 "Drop packet: ignoring all rebroadcast echos (sender: "
672 "%pM)\n", ethhdr->h_source);
676 orig_node = get_orig_node(bat_priv, batman_packet->orig);
680 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
682 if (is_duplicate == -1) {
683 bat_dbg(DBG_BATMAN, bat_priv,
684 "Drop packet: packet within seqno protection time "
685 "(sender: %pM)\n", ethhdr->h_source);
689 if (batman_packet->tq == 0) {
690 bat_dbg(DBG_BATMAN, bat_priv,
691 "Drop packet: originator packet with tq equal 0\n");
695 /* avoid temporary routing loops */
696 if ((orig_node->router) &&
697 (orig_node->router->orig_node->router) &&
698 (compare_orig(orig_node->router->addr,
699 batman_packet->prev_sender)) &&
700 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
701 (compare_orig(orig_node->router->addr,
702 orig_node->router->orig_node->router->addr))) {
703 bat_dbg(DBG_BATMAN, bat_priv,
704 "Drop packet: ignoring all rebroadcast packets that "
705 "may make me loop (sender: %pM)\n", ethhdr->h_source);
709 /* if sender is a direct neighbor the sender mac equals
711 orig_neigh_node = (is_single_hop_neigh ?
713 get_orig_node(bat_priv, ethhdr->h_source));
714 if (!orig_neigh_node)
717 /* drop packet if sender is not a direct neighbor and if we
718 * don't route towards it */
719 if (!is_single_hop_neigh && (!orig_neigh_node->router)) {
720 bat_dbg(DBG_BATMAN, bat_priv,
721 "Drop packet: OGM via unknown neighbor!\n");
725 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
726 batman_packet, if_incoming);
728 /* update ranking if it is not a duplicate or has the same
729 * seqno and similar ttl as the non-duplicate */
730 if (is_bidirectional &&
732 ((orig_node->last_real_seqno == batman_packet->seqno) &&
733 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
734 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
735 if_incoming, hna_buff, hna_buff_len, is_duplicate);
737 mark_bonding_address(orig_node, orig_neigh_node, batman_packet);
738 update_bonding_candidates(orig_node);
740 /* is single hop (direct) neighbor */
741 if (is_single_hop_neigh) {
743 /* mark direct link on incoming interface */
744 schedule_forward_packet(orig_node, ethhdr, batman_packet,
745 1, hna_buff_len, if_incoming);
747 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
748 "rebroadcast neighbor packet with direct link flag\n");
752 /* multihop originator */
753 if (!is_bidirectional) {
754 bat_dbg(DBG_BATMAN, bat_priv,
755 "Drop packet: not received via bidirectional link\n");
760 bat_dbg(DBG_BATMAN, bat_priv,
761 "Drop packet: duplicate packet received\n");
765 bat_dbg(DBG_BATMAN, bat_priv,
766 "Forwarding packet: rebroadcast originator packet\n");
767 schedule_forward_packet(orig_node, ethhdr, batman_packet,
768 0, hna_buff_len, if_incoming);
771 int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
773 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
774 struct ethhdr *ethhdr;
776 /* drop packet if it has not necessary minimum size */
777 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
780 ethhdr = (struct ethhdr *)skb_mac_header(skb);
782 /* packet with broadcast indication but unicast recipient */
783 if (!is_broadcast_ether_addr(ethhdr->h_dest))
786 /* packet with broadcast sender address */
787 if (is_broadcast_ether_addr(ethhdr->h_source))
790 /* create a copy of the skb, if needed, to modify it. */
791 if (skb_cow(skb, 0) < 0)
794 /* keep skb linear */
795 if (skb_linearize(skb) < 0)
798 ethhdr = (struct ethhdr *)skb_mac_header(skb);
800 spin_lock_bh(&bat_priv->orig_hash_lock);
801 receive_aggr_bat_packet(ethhdr,
805 spin_unlock_bh(&bat_priv->orig_hash_lock);
808 return NET_RX_SUCCESS;
811 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
812 struct sk_buff *skb, size_t icmp_len)
814 struct orig_node *orig_node;
815 struct icmp_packet_rr *icmp_packet;
816 struct batman_if *batman_if;
818 uint8_t dstaddr[ETH_ALEN];
820 icmp_packet = (struct icmp_packet_rr *)skb->data;
822 /* add data to device queue */
823 if (icmp_packet->msg_type != ECHO_REQUEST) {
824 bat_socket_receive_packet(icmp_packet, icmp_len);
828 if (!bat_priv->primary_if)
831 /* answer echo request (ping) */
832 /* get routing information */
833 spin_lock_bh(&bat_priv->orig_hash_lock);
834 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
835 compare_orig, choose_orig,
839 if ((orig_node) && (orig_node->router)) {
841 /* don't lock while sending the packets ... we therefore
842 * copy the required data before sending */
843 batman_if = orig_node->router->if_incoming;
844 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
845 spin_unlock_bh(&bat_priv->orig_hash_lock);
847 /* create a copy of the skb, if needed, to modify it. */
848 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
851 icmp_packet = (struct icmp_packet_rr *)skb->data;
853 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
854 memcpy(icmp_packet->orig,
855 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
856 icmp_packet->msg_type = ECHO_REPLY;
857 icmp_packet->ttl = TTL;
859 send_skb_packet(skb, batman_if, dstaddr);
860 ret = NET_RX_SUCCESS;
863 spin_unlock_bh(&bat_priv->orig_hash_lock);
868 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
871 struct orig_node *orig_node;
872 struct icmp_packet *icmp_packet;
873 struct batman_if *batman_if;
875 uint8_t dstaddr[ETH_ALEN];
877 icmp_packet = (struct icmp_packet *)skb->data;
879 /* send TTL exceeded if packet is an echo request (traceroute) */
880 if (icmp_packet->msg_type != ECHO_REQUEST) {
881 pr_debug("Warning - can't forward icmp packet from %pM to "
882 "%pM: ttl exceeded\n", icmp_packet->orig,
887 if (!bat_priv->primary_if)
890 /* get routing information */
891 spin_lock_bh(&bat_priv->orig_hash_lock);
892 orig_node = ((struct orig_node *)
893 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
897 if ((orig_node) && (orig_node->router)) {
899 /* don't lock while sending the packets ... we therefore
900 * copy the required data before sending */
901 batman_if = orig_node->router->if_incoming;
902 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
903 spin_unlock_bh(&bat_priv->orig_hash_lock);
905 /* create a copy of the skb, if needed, to modify it. */
906 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
909 icmp_packet = (struct icmp_packet *) skb->data;
911 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
912 memcpy(icmp_packet->orig,
913 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
914 icmp_packet->msg_type = TTL_EXCEEDED;
915 icmp_packet->ttl = TTL;
917 send_skb_packet(skb, batman_if, dstaddr);
918 ret = NET_RX_SUCCESS;
921 spin_unlock_bh(&bat_priv->orig_hash_lock);
927 int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
929 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
930 struct icmp_packet_rr *icmp_packet;
931 struct ethhdr *ethhdr;
932 struct orig_node *orig_node;
933 struct batman_if *batman_if;
934 int hdr_size = sizeof(struct icmp_packet);
936 uint8_t dstaddr[ETH_ALEN];
939 * we truncate all incoming icmp packets if they don't match our size
941 if (skb->len >= sizeof(struct icmp_packet_rr))
942 hdr_size = sizeof(struct icmp_packet_rr);
944 /* drop packet if it has not necessary minimum size */
945 if (unlikely(!pskb_may_pull(skb, hdr_size)))
948 ethhdr = (struct ethhdr *)skb_mac_header(skb);
950 /* packet with unicast indication but broadcast recipient */
951 if (is_broadcast_ether_addr(ethhdr->h_dest))
954 /* packet with broadcast sender address */
955 if (is_broadcast_ether_addr(ethhdr->h_source))
959 if (!is_my_mac(ethhdr->h_dest))
962 icmp_packet = (struct icmp_packet_rr *)skb->data;
964 /* add record route information if not full */
965 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
966 (icmp_packet->rr_cur < BAT_RR_LEN)) {
967 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
968 ethhdr->h_dest, ETH_ALEN);
969 icmp_packet->rr_cur++;
973 if (is_my_mac(icmp_packet->dst))
974 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
977 if (icmp_packet->ttl < 2)
978 return recv_icmp_ttl_exceeded(bat_priv, skb);
982 /* get routing information */
983 spin_lock_bh(&bat_priv->orig_hash_lock);
984 orig_node = ((struct orig_node *)
985 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
988 if ((orig_node) && (orig_node->router)) {
990 /* don't lock while sending the packets ... we therefore
991 * copy the required data before sending */
992 batman_if = orig_node->router->if_incoming;
993 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
994 spin_unlock_bh(&bat_priv->orig_hash_lock);
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_rr *)skb->data;
1006 send_skb_packet(skb, batman_if, dstaddr);
1007 ret = NET_RX_SUCCESS;
1010 spin_unlock_bh(&bat_priv->orig_hash_lock);
1015 /* find a suitable router for this originator, and use
1016 * bonding if possible. */
1017 struct neigh_node *find_router(struct bat_priv *bat_priv,
1018 struct orig_node *orig_node,
1019 struct batman_if *recv_if)
1021 struct orig_node *primary_orig_node;
1022 struct orig_node *router_orig;
1023 struct neigh_node *router, *first_candidate, *best_router;
1024 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1025 int bonding_enabled;
1030 if (!orig_node->router)
1033 /* without bonding, the first node should
1034 * always choose the default router. */
1036 bonding_enabled = atomic_read(&bat_priv->bonding);
1038 if ((!recv_if) && (!bonding_enabled))
1039 return orig_node->router;
1041 router_orig = orig_node->router->orig_node;
1043 /* if we have something in the primary_addr, we can search
1044 * for a potential bonding candidate. */
1045 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1046 return orig_node->router;
1048 /* find the orig_node which has the primary interface. might
1049 * even be the same as our router_orig in many cases */
1051 if (memcmp(router_orig->primary_addr,
1052 router_orig->orig, ETH_ALEN) == 0) {
1053 primary_orig_node = router_orig;
1055 primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig,
1057 router_orig->primary_addr);
1059 if (!primary_orig_node)
1060 return orig_node->router;
1063 /* with less than 2 candidates, we can't do any
1064 * bonding and prefer the original router. */
1066 if (primary_orig_node->bond.candidates < 2)
1067 return orig_node->router;
1070 /* all nodes between should choose a candidate which
1071 * is is not on the interface where the packet came
1073 first_candidate = primary_orig_node->bond.selected;
1074 router = first_candidate;
1076 if (bonding_enabled) {
1077 /* in the bonding case, send the packets in a round
1078 * robin fashion over the remaining interfaces. */
1080 /* recv_if == NULL on the first node. */
1081 if (router->if_incoming != recv_if)
1084 router = router->next_bond_candidate;
1085 } while (router != first_candidate);
1087 primary_orig_node->bond.selected = router->next_bond_candidate;
1090 /* if bonding is disabled, use the best of the
1091 * remaining candidates which are not using
1092 * this interface. */
1093 best_router = first_candidate;
1096 /* recv_if == NULL on the first node. */
1097 if ((router->if_incoming != recv_if) &&
1098 (router->tq_avg > best_router->tq_avg))
1099 best_router = router;
1101 router = router->next_bond_candidate;
1102 } while (router != first_candidate);
1104 router = best_router;
1110 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1112 struct ethhdr *ethhdr;
1114 /* drop packet if it has not necessary minimum size */
1115 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1118 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1120 /* packet with unicast indication but broadcast recipient */
1121 if (is_broadcast_ether_addr(ethhdr->h_dest))
1124 /* packet with broadcast sender address */
1125 if (is_broadcast_ether_addr(ethhdr->h_source))
1129 if (!is_my_mac(ethhdr->h_dest))
1135 int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1138 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1139 struct orig_node *orig_node;
1140 struct neigh_node *router;
1141 struct batman_if *batman_if;
1142 uint8_t dstaddr[ETH_ALEN];
1143 struct unicast_packet *unicast_packet;
1144 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1146 struct sk_buff *new_skb;
1148 unicast_packet = (struct unicast_packet *)skb->data;
1151 if (unicast_packet->ttl < 2) {
1152 pr_debug("Warning - can't forward unicast packet from %pM to "
1153 "%pM: ttl exceeded\n", ethhdr->h_source,
1154 unicast_packet->dest);
1158 /* get routing information */
1159 spin_lock_bh(&bat_priv->orig_hash_lock);
1160 orig_node = ((struct orig_node *)
1161 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1162 unicast_packet->dest));
1164 router = find_router(bat_priv, orig_node, recv_if);
1167 spin_unlock_bh(&bat_priv->orig_hash_lock);
1171 /* don't lock while sending the packets ... we therefore
1172 * copy the required data before sending */
1174 batman_if = router->if_incoming;
1175 memcpy(dstaddr, router->addr, ETH_ALEN);
1177 spin_unlock_bh(&bat_priv->orig_hash_lock);
1179 /* create a copy of the skb, if needed, to modify it. */
1180 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1183 unicast_packet = (struct unicast_packet *)skb->data;
1185 if (unicast_packet->packet_type == BAT_UNICAST &&
1186 atomic_read(&bat_priv->fragmentation) &&
1187 skb->len > batman_if->net_dev->mtu)
1188 return frag_send_skb(skb, bat_priv, batman_if,
1191 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1192 frag_can_reassemble(skb, batman_if->net_dev->mtu)) {
1194 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1196 if (ret == NET_RX_DROP)
1199 /* packet was buffered for late merge */
1201 return NET_RX_SUCCESS;
1204 unicast_packet = (struct unicast_packet *)skb->data;
1208 unicast_packet->ttl--;
1211 send_skb_packet(skb, batman_if, dstaddr);
1213 return NET_RX_SUCCESS;
1216 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1218 struct unicast_packet *unicast_packet;
1219 int hdr_size = sizeof(struct unicast_packet);
1221 if (check_unicast_packet(skb, hdr_size) < 0)
1224 unicast_packet = (struct unicast_packet *)skb->data;
1227 if (is_my_mac(unicast_packet->dest)) {
1228 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1229 return NET_RX_SUCCESS;
1232 return route_unicast_packet(skb, recv_if, hdr_size);
1235 int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1237 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1238 struct unicast_frag_packet *unicast_packet;
1239 int hdr_size = sizeof(struct unicast_frag_packet);
1240 struct sk_buff *new_skb = NULL;
1243 if (check_unicast_packet(skb, hdr_size) < 0)
1246 unicast_packet = (struct unicast_frag_packet *)skb->data;
1249 if (is_my_mac(unicast_packet->dest)) {
1251 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1253 if (ret == NET_RX_DROP)
1256 /* packet was buffered for late merge */
1258 return NET_RX_SUCCESS;
1260 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1261 sizeof(struct unicast_packet));
1262 return NET_RX_SUCCESS;
1265 return route_unicast_packet(skb, recv_if, hdr_size);
1269 int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1271 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1272 struct orig_node *orig_node;
1273 struct bcast_packet *bcast_packet;
1274 struct ethhdr *ethhdr;
1275 int hdr_size = sizeof(struct bcast_packet);
1278 /* drop packet if it has not necessary minimum size */
1279 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1282 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1284 /* packet with broadcast indication but unicast recipient */
1285 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1288 /* packet with broadcast sender address */
1289 if (is_broadcast_ether_addr(ethhdr->h_source))
1292 /* ignore broadcasts sent by myself */
1293 if (is_my_mac(ethhdr->h_source))
1296 bcast_packet = (struct bcast_packet *)skb->data;
1298 /* ignore broadcasts originated by myself */
1299 if (is_my_mac(bcast_packet->orig))
1302 if (bcast_packet->ttl < 2)
1305 spin_lock_bh(&bat_priv->orig_hash_lock);
1306 orig_node = ((struct orig_node *)
1307 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1308 bcast_packet->orig));
1311 spin_unlock_bh(&bat_priv->orig_hash_lock);
1315 /* check whether the packet is a duplicate */
1316 if (get_bit_status(orig_node->bcast_bits,
1317 orig_node->last_bcast_seqno,
1318 ntohl(bcast_packet->seqno))) {
1319 spin_unlock_bh(&bat_priv->orig_hash_lock);
1323 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1325 /* check whether the packet is old and the host just restarted. */
1326 if (window_protected(bat_priv, seq_diff,
1327 &orig_node->bcast_seqno_reset)) {
1328 spin_unlock_bh(&bat_priv->orig_hash_lock);
1332 /* mark broadcast in flood history, update window position
1334 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1335 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1337 spin_unlock_bh(&bat_priv->orig_hash_lock);
1338 /* rebroadcast packet */
1339 add_bcast_packet_to_list(bat_priv, skb);
1341 /* broadcast for me */
1342 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1344 return NET_RX_SUCCESS;
1347 int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1349 struct vis_packet *vis_packet;
1350 struct ethhdr *ethhdr;
1351 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1352 int hdr_size = sizeof(struct vis_packet);
1354 /* keep skb linear */
1355 if (skb_linearize(skb) < 0)
1358 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1361 vis_packet = (struct vis_packet *)skb->data;
1362 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1365 if (!is_my_mac(ethhdr->h_dest))
1368 /* ignore own packets */
1369 if (is_my_mac(vis_packet->vis_orig))
1372 if (is_my_mac(vis_packet->sender_orig))
1375 switch (vis_packet->vis_type) {
1376 case VIS_TYPE_SERVER_SYNC:
1377 receive_server_sync_packet(bat_priv, vis_packet,
1381 case VIS_TYPE_CLIENT_UPDATE:
1382 receive_client_update_packet(bat_priv, vis_packet,
1386 default: /* ignore unknown packet */
1390 /* We take a copy of the data in the packet, so we should
1391 always free the skbuf. */