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)
93 if ((orig_node->router) && (!neigh_node)) {
95 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
97 hna_global_del_orig(bat_priv, orig_node,
98 "originator timed out");
101 } else if ((!orig_node->router) && (neigh_node)) {
103 bat_dbg(DBG_ROUTES, bat_priv,
104 "Adding route towards: %pM (via %pM)\n",
105 orig_node->orig, neigh_node->addr);
106 hna_global_add_orig(bat_priv, orig_node,
107 hna_buff, hna_buff_len);
111 bat_dbg(DBG_ROUTES, bat_priv,
112 "Changing route towards: %pM "
113 "(now via %pM - was via %pM)\n",
114 orig_node->orig, neigh_node->addr,
115 orig_node->router->addr);
118 orig_node->router = neigh_node;
122 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
123 struct neigh_node *neigh_node, unsigned char *hna_buff,
130 if (orig_node->router != neigh_node)
131 update_route(bat_priv, orig_node, neigh_node,
132 hna_buff, hna_buff_len);
133 /* may be just HNA changed */
135 update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
138 static int is_bidirectional_neigh(struct orig_node *orig_node,
139 struct orig_node *orig_neigh_node,
140 struct batman_packet *batman_packet,
141 struct batman_if *if_incoming)
143 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
144 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
145 unsigned char total_count;
147 if (orig_node == orig_neigh_node) {
148 list_for_each_entry(tmp_neigh_node,
149 &orig_node->neigh_list,
152 if (compare_orig(tmp_neigh_node->addr,
153 orig_neigh_node->orig) &&
154 (tmp_neigh_node->if_incoming == if_incoming))
155 neigh_node = tmp_neigh_node;
159 neigh_node = create_neighbor(orig_node,
161 orig_neigh_node->orig,
163 /* create_neighbor failed, return 0 */
167 neigh_node->last_valid = jiffies;
169 /* find packet count of corresponding one hop neighbor */
170 list_for_each_entry(tmp_neigh_node,
171 &orig_neigh_node->neigh_list, list) {
173 if (compare_orig(tmp_neigh_node->addr,
174 orig_neigh_node->orig) &&
175 (tmp_neigh_node->if_incoming == if_incoming))
176 neigh_node = tmp_neigh_node;
180 neigh_node = create_neighbor(orig_neigh_node,
182 orig_neigh_node->orig,
184 /* create_neighbor failed, return 0 */
189 orig_node->last_valid = jiffies;
191 /* pay attention to not get a value bigger than 100 % */
192 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
193 neigh_node->real_packet_count ?
194 neigh_node->real_packet_count :
195 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
197 /* if we have too few packets (too less data) we set tq_own to zero */
198 /* if we receive too few packets it is not considered bidirectional */
199 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
200 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
201 orig_neigh_node->tq_own = 0;
203 /* neigh_node->real_packet_count is never zero as we
204 * only purge old information when getting new
206 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
207 neigh_node->real_packet_count;
210 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
211 * affect the nearly-symmetric links only a little, but
212 * punishes asymmetric links more. This will give a value
213 * between 0 and TQ_MAX_VALUE
215 orig_neigh_node->tq_asym_penalty =
218 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
219 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
220 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
221 (TQ_LOCAL_WINDOW_SIZE *
222 TQ_LOCAL_WINDOW_SIZE *
223 TQ_LOCAL_WINDOW_SIZE);
225 batman_packet->tq = ((batman_packet->tq *
226 orig_neigh_node->tq_own *
227 orig_neigh_node->tq_asym_penalty) /
228 (TQ_MAX_VALUE * TQ_MAX_VALUE));
230 bat_dbg(DBG_BATMAN, bat_priv,
232 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
233 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
235 orig_node->orig, orig_neigh_node->orig, total_count,
236 neigh_node->real_packet_count, orig_neigh_node->tq_own,
237 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
239 /* if link has the minimum required transmission quality
240 * consider it bidirectional */
241 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
247 static void update_orig(struct bat_priv *bat_priv,
248 struct orig_node *orig_node,
249 struct ethhdr *ethhdr,
250 struct batman_packet *batman_packet,
251 struct batman_if *if_incoming,
252 unsigned char *hna_buff, int hna_buff_len,
255 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
256 int tmp_hna_buff_len;
258 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
259 "Searching and updating originator entry of received packet\n");
261 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
262 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
263 (tmp_neigh_node->if_incoming == if_incoming)) {
264 neigh_node = tmp_neigh_node;
271 ring_buffer_set(tmp_neigh_node->tq_recv,
272 &tmp_neigh_node->tq_index, 0);
273 tmp_neigh_node->tq_avg =
274 ring_buffer_avg(tmp_neigh_node->tq_recv);
278 struct orig_node *orig_tmp;
280 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
284 neigh_node = create_neighbor(orig_node, orig_tmp,
285 ethhdr->h_source, if_incoming);
289 bat_dbg(DBG_BATMAN, bat_priv,
290 "Updating existing last-hop neighbor of originator\n");
292 orig_node->flags = batman_packet->flags;
293 neigh_node->last_valid = jiffies;
295 ring_buffer_set(neigh_node->tq_recv,
296 &neigh_node->tq_index,
298 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
301 orig_node->last_ttl = batman_packet->ttl;
302 neigh_node->last_ttl = batman_packet->ttl;
305 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
306 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
308 /* if this neighbor already is our next hop there is nothing
310 if (orig_node->router == neigh_node)
313 /* if this neighbor does not offer a better TQ we won't consider it */
314 if ((orig_node->router) &&
315 (orig_node->router->tq_avg > neigh_node->tq_avg))
318 /* if the TQ is the same and the link not more symetric we
319 * won't consider it either */
320 if ((orig_node->router) &&
321 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
322 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
323 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
326 update_routes(bat_priv, orig_node, neigh_node,
327 hna_buff, tmp_hna_buff_len);
331 update_routes(bat_priv, orig_node, orig_node->router,
332 hna_buff, tmp_hna_buff_len);
335 if (orig_node->gw_flags != batman_packet->gw_flags)
336 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
338 orig_node->gw_flags = batman_packet->gw_flags;
340 /* restart gateway selection if fast or late switching was enabled */
341 if ((orig_node->gw_flags) &&
342 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
343 (atomic_read(&bat_priv->gw_sel_class) > 2))
344 gw_check_election(bat_priv, orig_node);
347 /* checks whether the host restarted and is in the protection time.
349 * 0 if the packet is to be accepted
350 * 1 if the packet is to be ignored.
352 static int window_protected(struct bat_priv *bat_priv,
353 int32_t seq_num_diff,
354 unsigned long *last_reset)
356 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
357 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
358 if (time_after(jiffies, *last_reset +
359 msecs_to_jiffies(RESET_PROTECTION_MS))) {
361 *last_reset = jiffies;
362 bat_dbg(DBG_BATMAN, bat_priv,
363 "old packet received, start protection\n");
372 /* processes a batman packet for all interfaces, adjusts the sequence number and
373 * finds out whether it is a duplicate.
375 * 1 the packet is a duplicate
376 * 0 the packet has not yet been received
377 * -1 the packet is old and has been received while the seqno window
378 * was protected. Caller should drop it.
380 static char count_real_packets(struct ethhdr *ethhdr,
381 struct batman_packet *batman_packet,
382 struct batman_if *if_incoming)
384 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
385 struct orig_node *orig_node;
386 struct neigh_node *tmp_neigh_node;
387 char is_duplicate = 0;
392 orig_node = get_orig_node(bat_priv, batman_packet->orig);
396 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
398 /* signalize caller that the packet is to be dropped. */
399 if (window_protected(bat_priv, seq_diff,
400 &orig_node->batman_seqno_reset))
403 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
405 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
406 orig_node->last_real_seqno,
407 batman_packet->seqno);
409 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
410 (tmp_neigh_node->if_incoming == if_incoming))
415 /* if the window moved, set the update flag. */
416 need_update |= bit_get_packet(bat_priv,
417 tmp_neigh_node->real_bits,
420 tmp_neigh_node->real_packet_count =
421 bit_packet_count(tmp_neigh_node->real_bits);
425 bat_dbg(DBG_BATMAN, bat_priv,
426 "updating last_seqno: old %d, new %d\n",
427 orig_node->last_real_seqno, batman_packet->seqno);
428 orig_node->last_real_seqno = batman_packet->seqno;
434 /* copy primary address for bonding */
435 static void mark_bonding_address(struct orig_node *orig_node,
436 struct orig_node *orig_neigh_node,
437 struct batman_packet *batman_packet)
440 if (batman_packet->flags & PRIMARIES_FIRST_HOP)
441 memcpy(orig_neigh_node->primary_addr,
442 orig_node->orig, ETH_ALEN);
447 /* mark possible bond.candidates in the neighbor list */
448 void update_bonding_candidates(struct orig_node *orig_node)
451 int interference_candidate;
453 struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
454 struct neigh_node *first_candidate, *last_candidate;
456 /* update the candidates for this originator */
457 if (!orig_node->router) {
458 orig_node->bond.candidates = 0;
462 best_tq = orig_node->router->tq_avg;
464 /* update bond.candidates */
468 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
469 * as "bonding partner" */
471 /* first, zero the list */
472 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
473 tmp_neigh_node->next_bond_candidate = NULL;
476 first_candidate = NULL;
477 last_candidate = NULL;
478 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
480 /* only consider if it has the same primary address ... */
481 if (memcmp(orig_node->orig,
482 tmp_neigh_node->orig_node->primary_addr,
486 /* ... and is good enough to be considered */
487 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
490 /* check if we have another candidate with the same
491 * mac address or interface. If we do, we won't
492 * select this candidate because of possible interference. */
494 interference_candidate = 0;
495 list_for_each_entry(tmp_neigh_node2,
496 &orig_node->neigh_list, list) {
498 if (tmp_neigh_node2 == tmp_neigh_node)
501 /* we only care if the other candidate is even
502 * considered as candidate. */
503 if (!tmp_neigh_node2->next_bond_candidate)
507 if ((tmp_neigh_node->if_incoming ==
508 tmp_neigh_node2->if_incoming)
509 || (memcmp(tmp_neigh_node->addr,
510 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
512 interference_candidate = 1;
516 /* don't care further if it is an interference candidate */
517 if (interference_candidate)
520 if (!first_candidate) {
521 first_candidate = tmp_neigh_node;
522 tmp_neigh_node->next_bond_candidate = first_candidate;
524 tmp_neigh_node->next_bond_candidate = last_candidate;
526 last_candidate = tmp_neigh_node;
531 if (candidates > 0) {
532 first_candidate->next_bond_candidate = last_candidate;
533 orig_node->bond.selected = first_candidate;
536 orig_node->bond.candidates = candidates;
539 void receive_bat_packet(struct ethhdr *ethhdr,
540 struct batman_packet *batman_packet,
541 unsigned char *hna_buff, int hna_buff_len,
542 struct batman_if *if_incoming)
544 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
545 struct batman_if *batman_if;
546 struct orig_node *orig_neigh_node, *orig_node;
547 char has_directlink_flag;
548 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
549 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
551 uint32_t if_incoming_seqno;
553 /* Silently drop when the batman packet is actually not a
556 * This might happen if a packet is padded (e.g. Ethernet has a
557 * minimum frame length of 64 byte) and the aggregation interprets
558 * it as an additional length.
560 * TODO: A more sane solution would be to have a bit in the
561 * batman_packet to detect whether the packet is the last
562 * packet in an aggregation. Here we expect that the padding
563 * is always zero (or not 0x01)
565 if (batman_packet->packet_type != BAT_PACKET)
568 /* could be changed by schedule_own_packet() */
569 if_incoming_seqno = atomic_read(&if_incoming->seqno);
571 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
573 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
574 batman_packet->orig) ? 1 : 0);
576 bat_dbg(DBG_BATMAN, bat_priv,
577 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
578 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
579 "TTL %d, V %d, IDF %d)\n",
580 ethhdr->h_source, if_incoming->net_dev->name,
581 if_incoming->net_dev->dev_addr, batman_packet->orig,
582 batman_packet->prev_sender, batman_packet->seqno,
583 batman_packet->tq, batman_packet->ttl, batman_packet->version,
584 has_directlink_flag);
587 list_for_each_entry_rcu(batman_if, &if_list, list) {
588 if (batman_if->if_status != IF_ACTIVE)
591 if (batman_if->soft_iface != if_incoming->soft_iface)
594 if (compare_orig(ethhdr->h_source,
595 batman_if->net_dev->dev_addr))
598 if (compare_orig(batman_packet->orig,
599 batman_if->net_dev->dev_addr))
602 if (compare_orig(batman_packet->prev_sender,
603 batman_if->net_dev->dev_addr))
606 if (compare_orig(ethhdr->h_source, broadcast_addr))
611 if (batman_packet->version != COMPAT_VERSION) {
612 bat_dbg(DBG_BATMAN, bat_priv,
613 "Drop packet: incompatible batman version (%i)\n",
614 batman_packet->version);
619 bat_dbg(DBG_BATMAN, bat_priv,
620 "Drop packet: received my own broadcast (sender: %pM"
627 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
628 "ignoring all packets with broadcast source addr (sender: %pM"
629 ")\n", ethhdr->h_source);
637 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
639 if (!orig_neigh_node)
642 /* neighbor has to indicate direct link and it has to
643 * come via the corresponding interface */
644 /* if received seqno equals last send seqno save new
645 * seqno for bidirectional check */
646 if (has_directlink_flag &&
647 compare_orig(if_incoming->net_dev->dev_addr,
648 batman_packet->orig) &&
649 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
650 offset = if_incoming->if_num * NUM_WORDS;
651 word = &(orig_neigh_node->bcast_own[offset]);
653 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
654 bit_packet_count(word);
657 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
658 "originator packet from myself (via neighbor)\n");
663 bat_dbg(DBG_BATMAN, bat_priv,
664 "Drop packet: ignoring all rebroadcast echos (sender: "
665 "%pM)\n", ethhdr->h_source);
669 orig_node = get_orig_node(bat_priv, batman_packet->orig);
673 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
675 if (is_duplicate == -1) {
676 bat_dbg(DBG_BATMAN, bat_priv,
677 "Drop packet: packet within seqno protection time "
678 "(sender: %pM)\n", ethhdr->h_source);
682 if (batman_packet->tq == 0) {
683 bat_dbg(DBG_BATMAN, bat_priv,
684 "Drop packet: originator packet with tq equal 0\n");
688 /* avoid temporary routing loops */
689 if ((orig_node->router) &&
690 (orig_node->router->orig_node->router) &&
691 (compare_orig(orig_node->router->addr,
692 batman_packet->prev_sender)) &&
693 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
694 (compare_orig(orig_node->router->addr,
695 orig_node->router->orig_node->router->addr))) {
696 bat_dbg(DBG_BATMAN, bat_priv,
697 "Drop packet: ignoring all rebroadcast packets that "
698 "may make me loop (sender: %pM)\n", ethhdr->h_source);
702 /* if sender is a direct neighbor the sender mac equals
704 orig_neigh_node = (is_single_hop_neigh ?
706 get_orig_node(bat_priv, ethhdr->h_source));
707 if (!orig_neigh_node)
710 /* drop packet if sender is not a direct neighbor and if we
711 * don't route towards it */
712 if (!is_single_hop_neigh && (!orig_neigh_node->router)) {
713 bat_dbg(DBG_BATMAN, bat_priv,
714 "Drop packet: OGM via unknown neighbor!\n");
718 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
719 batman_packet, if_incoming);
721 /* update ranking if it is not a duplicate or has the same
722 * seqno and similar ttl as the non-duplicate */
723 if (is_bidirectional &&
725 ((orig_node->last_real_seqno == batman_packet->seqno) &&
726 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
727 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
728 if_incoming, hna_buff, hna_buff_len, is_duplicate);
730 mark_bonding_address(orig_node, orig_neigh_node, batman_packet);
731 update_bonding_candidates(orig_node);
733 /* is single hop (direct) neighbor */
734 if (is_single_hop_neigh) {
736 /* mark direct link on incoming interface */
737 schedule_forward_packet(orig_node, ethhdr, batman_packet,
738 1, hna_buff_len, if_incoming);
740 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
741 "rebroadcast neighbor packet with direct link flag\n");
745 /* multihop originator */
746 if (!is_bidirectional) {
747 bat_dbg(DBG_BATMAN, bat_priv,
748 "Drop packet: not received via bidirectional link\n");
753 bat_dbg(DBG_BATMAN, bat_priv,
754 "Drop packet: duplicate packet received\n");
758 bat_dbg(DBG_BATMAN, bat_priv,
759 "Forwarding packet: rebroadcast originator packet\n");
760 schedule_forward_packet(orig_node, ethhdr, batman_packet,
761 0, hna_buff_len, if_incoming);
764 int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
766 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
767 struct ethhdr *ethhdr;
769 /* drop packet if it has not necessary minimum size */
770 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
773 ethhdr = (struct ethhdr *)skb_mac_header(skb);
775 /* packet with broadcast indication but unicast recipient */
776 if (!is_broadcast_ether_addr(ethhdr->h_dest))
779 /* packet with broadcast sender address */
780 if (is_broadcast_ether_addr(ethhdr->h_source))
783 /* create a copy of the skb, if needed, to modify it. */
784 if (skb_cow(skb, 0) < 0)
787 /* keep skb linear */
788 if (skb_linearize(skb) < 0)
791 ethhdr = (struct ethhdr *)skb_mac_header(skb);
793 spin_lock_bh(&bat_priv->orig_hash_lock);
794 receive_aggr_bat_packet(ethhdr,
798 spin_unlock_bh(&bat_priv->orig_hash_lock);
801 return NET_RX_SUCCESS;
804 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
805 struct sk_buff *skb, size_t icmp_len)
807 struct orig_node *orig_node;
808 struct icmp_packet_rr *icmp_packet;
809 struct batman_if *batman_if;
811 uint8_t dstaddr[ETH_ALEN];
813 icmp_packet = (struct icmp_packet_rr *)skb->data;
815 /* add data to device queue */
816 if (icmp_packet->msg_type != ECHO_REQUEST) {
817 bat_socket_receive_packet(icmp_packet, icmp_len);
821 if (!bat_priv->primary_if)
824 /* answer echo request (ping) */
825 /* get routing information */
826 spin_lock_bh(&bat_priv->orig_hash_lock);
827 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
828 compare_orig, choose_orig,
832 if ((orig_node) && (orig_node->router)) {
834 /* don't lock while sending the packets ... we therefore
835 * copy the required data before sending */
836 batman_if = orig_node->router->if_incoming;
837 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
838 spin_unlock_bh(&bat_priv->orig_hash_lock);
840 /* create a copy of the skb, if needed, to modify it. */
841 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
844 icmp_packet = (struct icmp_packet_rr *)skb->data;
846 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
847 memcpy(icmp_packet->orig,
848 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
849 icmp_packet->msg_type = ECHO_REPLY;
850 icmp_packet->ttl = TTL;
852 send_skb_packet(skb, batman_if, dstaddr);
853 ret = NET_RX_SUCCESS;
856 spin_unlock_bh(&bat_priv->orig_hash_lock);
861 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
864 struct orig_node *orig_node;
865 struct icmp_packet *icmp_packet;
866 struct batman_if *batman_if;
868 uint8_t dstaddr[ETH_ALEN];
870 icmp_packet = (struct icmp_packet *)skb->data;
872 /* send TTL exceeded if packet is an echo request (traceroute) */
873 if (icmp_packet->msg_type != ECHO_REQUEST) {
874 pr_debug("Warning - can't forward icmp packet from %pM to "
875 "%pM: ttl exceeded\n", icmp_packet->orig,
880 if (!bat_priv->primary_if)
883 /* get routing information */
884 spin_lock_bh(&bat_priv->orig_hash_lock);
885 orig_node = ((struct orig_node *)
886 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
890 if ((orig_node) && (orig_node->router)) {
892 /* don't lock while sending the packets ... we therefore
893 * copy the required data before sending */
894 batman_if = orig_node->router->if_incoming;
895 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
896 spin_unlock_bh(&bat_priv->orig_hash_lock);
898 /* create a copy of the skb, if needed, to modify it. */
899 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
902 icmp_packet = (struct icmp_packet *) skb->data;
904 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
905 memcpy(icmp_packet->orig,
906 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
907 icmp_packet->msg_type = TTL_EXCEEDED;
908 icmp_packet->ttl = TTL;
910 send_skb_packet(skb, batman_if, dstaddr);
911 ret = NET_RX_SUCCESS;
914 spin_unlock_bh(&bat_priv->orig_hash_lock);
920 int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
922 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
923 struct icmp_packet_rr *icmp_packet;
924 struct ethhdr *ethhdr;
925 struct orig_node *orig_node;
926 struct batman_if *batman_if;
927 int hdr_size = sizeof(struct icmp_packet);
929 uint8_t dstaddr[ETH_ALEN];
932 * we truncate all incoming icmp packets if they don't match our size
934 if (skb->len >= sizeof(struct icmp_packet_rr))
935 hdr_size = sizeof(struct icmp_packet_rr);
937 /* drop packet if it has not necessary minimum size */
938 if (unlikely(!pskb_may_pull(skb, hdr_size)))
941 ethhdr = (struct ethhdr *)skb_mac_header(skb);
943 /* packet with unicast indication but broadcast recipient */
944 if (is_broadcast_ether_addr(ethhdr->h_dest))
947 /* packet with broadcast sender address */
948 if (is_broadcast_ether_addr(ethhdr->h_source))
952 if (!is_my_mac(ethhdr->h_dest))
955 icmp_packet = (struct icmp_packet_rr *)skb->data;
957 /* add record route information if not full */
958 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
959 (icmp_packet->rr_cur < BAT_RR_LEN)) {
960 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
961 ethhdr->h_dest, ETH_ALEN);
962 icmp_packet->rr_cur++;
966 if (is_my_mac(icmp_packet->dst))
967 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
970 if (icmp_packet->ttl < 2)
971 return recv_icmp_ttl_exceeded(bat_priv, skb);
975 /* get routing information */
976 spin_lock_bh(&bat_priv->orig_hash_lock);
977 orig_node = ((struct orig_node *)
978 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
981 if ((orig_node) && (orig_node->router)) {
983 /* don't lock while sending the packets ... we therefore
984 * copy the required data before sending */
985 batman_if = orig_node->router->if_incoming;
986 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
987 spin_unlock_bh(&bat_priv->orig_hash_lock);
989 /* create a copy of the skb, if needed, to modify it. */
990 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
993 icmp_packet = (struct icmp_packet_rr *)skb->data;
999 send_skb_packet(skb, batman_if, dstaddr);
1000 ret = NET_RX_SUCCESS;
1003 spin_unlock_bh(&bat_priv->orig_hash_lock);
1008 /* find a suitable router for this originator, and use
1009 * bonding if possible. */
1010 struct neigh_node *find_router(struct bat_priv *bat_priv,
1011 struct orig_node *orig_node,
1012 struct batman_if *recv_if)
1014 struct orig_node *primary_orig_node;
1015 struct orig_node *router_orig;
1016 struct neigh_node *router, *first_candidate, *best_router;
1017 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1018 int bonding_enabled;
1023 if (!orig_node->router)
1026 /* without bonding, the first node should
1027 * always choose the default router. */
1029 bonding_enabled = atomic_read(&bat_priv->bonding);
1031 if ((!recv_if) && (!bonding_enabled))
1032 return orig_node->router;
1034 router_orig = orig_node->router->orig_node;
1036 /* if we have something in the primary_addr, we can search
1037 * for a potential bonding candidate. */
1038 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1039 return orig_node->router;
1041 /* find the orig_node which has the primary interface. might
1042 * even be the same as our router_orig in many cases */
1044 if (memcmp(router_orig->primary_addr,
1045 router_orig->orig, ETH_ALEN) == 0) {
1046 primary_orig_node = router_orig;
1048 primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig,
1050 router_orig->primary_addr);
1052 if (!primary_orig_node)
1053 return orig_node->router;
1056 /* with less than 2 candidates, we can't do any
1057 * bonding and prefer the original router. */
1059 if (primary_orig_node->bond.candidates < 2)
1060 return orig_node->router;
1063 /* all nodes between should choose a candidate which
1064 * is is not on the interface where the packet came
1066 first_candidate = primary_orig_node->bond.selected;
1067 router = first_candidate;
1069 if (bonding_enabled) {
1070 /* in the bonding case, send the packets in a round
1071 * robin fashion over the remaining interfaces. */
1073 /* recv_if == NULL on the first node. */
1074 if (router->if_incoming != recv_if)
1077 router = router->next_bond_candidate;
1078 } while (router != first_candidate);
1080 primary_orig_node->bond.selected = router->next_bond_candidate;
1083 /* if bonding is disabled, use the best of the
1084 * remaining candidates which are not using
1085 * this interface. */
1086 best_router = first_candidate;
1089 /* recv_if == NULL on the first node. */
1090 if ((router->if_incoming != recv_if) &&
1091 (router->tq_avg > best_router->tq_avg))
1092 best_router = router;
1094 router = router->next_bond_candidate;
1095 } while (router != first_candidate);
1097 router = best_router;
1103 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1105 struct ethhdr *ethhdr;
1107 /* drop packet if it has not necessary minimum size */
1108 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1111 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1113 /* packet with unicast indication but broadcast recipient */
1114 if (is_broadcast_ether_addr(ethhdr->h_dest))
1117 /* packet with broadcast sender address */
1118 if (is_broadcast_ether_addr(ethhdr->h_source))
1122 if (!is_my_mac(ethhdr->h_dest))
1128 int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1131 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1132 struct orig_node *orig_node;
1133 struct neigh_node *router;
1134 struct batman_if *batman_if;
1135 uint8_t dstaddr[ETH_ALEN];
1136 struct unicast_packet *unicast_packet;
1137 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1139 struct sk_buff *new_skb;
1141 unicast_packet = (struct unicast_packet *)skb->data;
1144 if (unicast_packet->ttl < 2) {
1145 pr_debug("Warning - can't forward unicast packet from %pM to "
1146 "%pM: ttl exceeded\n", ethhdr->h_source,
1147 unicast_packet->dest);
1151 /* get routing information */
1152 spin_lock_bh(&bat_priv->orig_hash_lock);
1153 orig_node = ((struct orig_node *)
1154 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1155 unicast_packet->dest));
1157 router = find_router(bat_priv, orig_node, recv_if);
1160 spin_unlock_bh(&bat_priv->orig_hash_lock);
1164 /* don't lock while sending the packets ... we therefore
1165 * copy the required data before sending */
1167 batman_if = router->if_incoming;
1168 memcpy(dstaddr, router->addr, ETH_ALEN);
1170 spin_unlock_bh(&bat_priv->orig_hash_lock);
1172 /* create a copy of the skb, if needed, to modify it. */
1173 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1176 unicast_packet = (struct unicast_packet *)skb->data;
1178 if (unicast_packet->packet_type == BAT_UNICAST &&
1179 atomic_read(&bat_priv->fragmentation) &&
1180 skb->len > batman_if->net_dev->mtu)
1181 return frag_send_skb(skb, bat_priv, batman_if,
1184 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1185 frag_can_reassemble(skb, batman_if->net_dev->mtu)) {
1187 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1189 if (ret == NET_RX_DROP)
1192 /* packet was buffered for late merge */
1194 return NET_RX_SUCCESS;
1197 unicast_packet = (struct unicast_packet *)skb->data;
1201 unicast_packet->ttl--;
1204 send_skb_packet(skb, batman_if, dstaddr);
1206 return NET_RX_SUCCESS;
1209 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1211 struct unicast_packet *unicast_packet;
1212 int hdr_size = sizeof(struct unicast_packet);
1214 if (check_unicast_packet(skb, hdr_size) < 0)
1217 unicast_packet = (struct unicast_packet *)skb->data;
1220 if (is_my_mac(unicast_packet->dest)) {
1221 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1222 return NET_RX_SUCCESS;
1225 return route_unicast_packet(skb, recv_if, hdr_size);
1228 int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1230 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1231 struct unicast_frag_packet *unicast_packet;
1232 int hdr_size = sizeof(struct unicast_frag_packet);
1233 struct sk_buff *new_skb = NULL;
1236 if (check_unicast_packet(skb, hdr_size) < 0)
1239 unicast_packet = (struct unicast_frag_packet *)skb->data;
1242 if (is_my_mac(unicast_packet->dest)) {
1244 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1246 if (ret == NET_RX_DROP)
1249 /* packet was buffered for late merge */
1251 return NET_RX_SUCCESS;
1253 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1254 sizeof(struct unicast_packet));
1255 return NET_RX_SUCCESS;
1258 return route_unicast_packet(skb, recv_if, hdr_size);
1262 int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1264 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1265 struct orig_node *orig_node;
1266 struct bcast_packet *bcast_packet;
1267 struct ethhdr *ethhdr;
1268 int hdr_size = sizeof(struct bcast_packet);
1271 /* drop packet if it has not necessary minimum size */
1272 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1275 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1277 /* packet with broadcast indication but unicast recipient */
1278 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1281 /* packet with broadcast sender address */
1282 if (is_broadcast_ether_addr(ethhdr->h_source))
1285 /* ignore broadcasts sent by myself */
1286 if (is_my_mac(ethhdr->h_source))
1289 bcast_packet = (struct bcast_packet *)skb->data;
1291 /* ignore broadcasts originated by myself */
1292 if (is_my_mac(bcast_packet->orig))
1295 if (bcast_packet->ttl < 2)
1298 spin_lock_bh(&bat_priv->orig_hash_lock);
1299 orig_node = ((struct orig_node *)
1300 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1301 bcast_packet->orig));
1304 spin_unlock_bh(&bat_priv->orig_hash_lock);
1308 /* check whether the packet is a duplicate */
1309 if (get_bit_status(orig_node->bcast_bits,
1310 orig_node->last_bcast_seqno,
1311 ntohl(bcast_packet->seqno))) {
1312 spin_unlock_bh(&bat_priv->orig_hash_lock);
1316 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1318 /* check whether the packet is old and the host just restarted. */
1319 if (window_protected(bat_priv, seq_diff,
1320 &orig_node->bcast_seqno_reset)) {
1321 spin_unlock_bh(&bat_priv->orig_hash_lock);
1325 /* mark broadcast in flood history, update window position
1327 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1328 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1330 spin_unlock_bh(&bat_priv->orig_hash_lock);
1331 /* rebroadcast packet */
1332 add_bcast_packet_to_list(bat_priv, skb);
1334 /* broadcast for me */
1335 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1337 return NET_RX_SUCCESS;
1340 int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1342 struct vis_packet *vis_packet;
1343 struct ethhdr *ethhdr;
1344 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1345 int hdr_size = sizeof(struct vis_packet);
1347 /* keep skb linear */
1348 if (skb_linearize(skb) < 0)
1351 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1354 vis_packet = (struct vis_packet *)skb->data;
1355 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1358 if (!is_my_mac(ethhdr->h_dest))
1361 /* ignore own packets */
1362 if (is_my_mac(vis_packet->vis_orig))
1365 if (is_my_mac(vis_packet->sender_orig))
1368 switch (vis_packet->vis_type) {
1369 case VIS_TYPE_SERVER_SYNC:
1370 receive_server_sync_packet(bat_priv, vis_packet,
1374 case VIS_TYPE_CLIENT_UPDATE:
1375 receive_client_update_packet(bat_priv, vis_packet,
1379 default: /* ignore unknown packet */
1383 /* We take a copy of the data in the packet, so we should
1384 always free the skbuf. */