2 * Copyright (C) 2007-2010 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"
32 #include "ring_buffer.h"
34 #include "aggregation.h"
35 #include "gateway_common.h"
36 #include "gateway_client.h"
39 void slide_own_bcast_window(struct batman_if *batman_if)
41 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
42 struct hashtable_t *hash = bat_priv->orig_hash;
43 struct hlist_node *walk;
44 struct hlist_head *head;
45 struct element_t *bucket;
46 struct orig_node *orig_node;
51 spin_lock_bh(&bat_priv->orig_hash_lock);
53 for (i = 0; i < hash->size; i++) {
54 head = &hash->table[i];
56 hlist_for_each_entry(bucket, walk, head, hlist) {
57 orig_node = bucket->data;
58 word_index = batman_if->if_num * NUM_WORDS;
59 word = &(orig_node->bcast_own[word_index]);
61 bit_get_packet(bat_priv, word, 1, 0);
62 orig_node->bcast_own_sum[batman_if->if_num] =
63 bit_packet_count(word);
67 spin_unlock_bh(&bat_priv->orig_hash_lock);
70 static void update_HNA(struct bat_priv *bat_priv, struct orig_node *orig_node,
71 unsigned char *hna_buff, int hna_buff_len)
73 if ((hna_buff_len != orig_node->hna_buff_len) ||
74 ((hna_buff_len > 0) &&
75 (orig_node->hna_buff_len > 0) &&
76 (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
78 if (orig_node->hna_buff_len > 0)
79 hna_global_del_orig(bat_priv, orig_node,
80 "originator changed hna");
82 if ((hna_buff_len > 0) && (hna_buff))
83 hna_global_add_orig(bat_priv, orig_node,
84 hna_buff, hna_buff_len);
88 static void update_route(struct bat_priv *bat_priv,
89 struct orig_node *orig_node,
90 struct neigh_node *neigh_node,
91 unsigned char *hna_buff, int hna_buff_len)
94 if ((orig_node->router) && (!neigh_node)) {
96 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
98 hna_global_del_orig(bat_priv, orig_node,
99 "originator timed out");
102 } else if ((!orig_node->router) && (neigh_node)) {
104 bat_dbg(DBG_ROUTES, bat_priv,
105 "Adding route towards: %pM (via %pM)\n",
106 orig_node->orig, neigh_node->addr);
107 hna_global_add_orig(bat_priv, orig_node,
108 hna_buff, hna_buff_len);
112 bat_dbg(DBG_ROUTES, bat_priv,
113 "Changing route towards: %pM "
114 "(now via %pM - was via %pM)\n",
115 orig_node->orig, neigh_node->addr,
116 orig_node->router->addr);
119 orig_node->router = neigh_node;
123 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
124 struct neigh_node *neigh_node, unsigned char *hna_buff,
131 if (orig_node->router != neigh_node)
132 update_route(bat_priv, orig_node, neigh_node,
133 hna_buff, hna_buff_len);
134 /* may be just HNA changed */
136 update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
139 static int is_bidirectional_neigh(struct orig_node *orig_node,
140 struct orig_node *orig_neigh_node,
141 struct batman_packet *batman_packet,
142 struct batman_if *if_incoming)
144 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
145 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
146 unsigned char total_count;
148 if (orig_node == orig_neigh_node) {
149 list_for_each_entry(tmp_neigh_node,
150 &orig_node->neigh_list,
153 if (compare_orig(tmp_neigh_node->addr,
154 orig_neigh_node->orig) &&
155 (tmp_neigh_node->if_incoming == if_incoming))
156 neigh_node = tmp_neigh_node;
160 neigh_node = create_neighbor(orig_node,
162 orig_neigh_node->orig,
164 /* create_neighbor failed, return 0 */
168 neigh_node->last_valid = jiffies;
170 /* find packet count of corresponding one hop neighbor */
171 list_for_each_entry(tmp_neigh_node,
172 &orig_neigh_node->neigh_list, list) {
174 if (compare_orig(tmp_neigh_node->addr,
175 orig_neigh_node->orig) &&
176 (tmp_neigh_node->if_incoming == if_incoming))
177 neigh_node = tmp_neigh_node;
181 neigh_node = create_neighbor(orig_neigh_node,
183 orig_neigh_node->orig,
185 /* create_neighbor failed, return 0 */
190 orig_node->last_valid = jiffies;
192 /* pay attention to not get a value bigger than 100 % */
193 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
194 neigh_node->real_packet_count ?
195 neigh_node->real_packet_count :
196 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
198 /* if we have too few packets (too less data) we set tq_own to zero */
199 /* if we receive too few packets it is not considered bidirectional */
200 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
201 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
202 orig_neigh_node->tq_own = 0;
204 /* neigh_node->real_packet_count is never zero as we
205 * only purge old information when getting new
207 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
208 neigh_node->real_packet_count;
211 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
212 * affect the nearly-symmetric links only a little, but
213 * punishes asymmetric links more. This will give a value
214 * between 0 and TQ_MAX_VALUE
216 orig_neigh_node->tq_asym_penalty =
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 - neigh_node->real_packet_count)) /
222 (TQ_LOCAL_WINDOW_SIZE *
223 TQ_LOCAL_WINDOW_SIZE *
224 TQ_LOCAL_WINDOW_SIZE);
226 batman_packet->tq = ((batman_packet->tq *
227 orig_neigh_node->tq_own *
228 orig_neigh_node->tq_asym_penalty) /
229 (TQ_MAX_VALUE * TQ_MAX_VALUE));
231 bat_dbg(DBG_BATMAN, bat_priv,
233 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
234 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
236 orig_node->orig, orig_neigh_node->orig, total_count,
237 neigh_node->real_packet_count, orig_neigh_node->tq_own,
238 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
240 /* if link has the minimum required transmission quality
241 * consider it bidirectional */
242 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
248 static void update_orig(struct bat_priv *bat_priv,
249 struct orig_node *orig_node,
250 struct ethhdr *ethhdr,
251 struct batman_packet *batman_packet,
252 struct batman_if *if_incoming,
253 unsigned char *hna_buff, int hna_buff_len,
256 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
257 int tmp_hna_buff_len;
259 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
260 "Searching and updating originator entry of received packet\n");
262 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
263 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
264 (tmp_neigh_node->if_incoming == if_incoming)) {
265 neigh_node = tmp_neigh_node;
272 ring_buffer_set(tmp_neigh_node->tq_recv,
273 &tmp_neigh_node->tq_index, 0);
274 tmp_neigh_node->tq_avg =
275 ring_buffer_avg(tmp_neigh_node->tq_recv);
279 struct orig_node *orig_tmp;
281 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
285 neigh_node = create_neighbor(orig_node, orig_tmp,
286 ethhdr->h_source, if_incoming);
290 bat_dbg(DBG_BATMAN, bat_priv,
291 "Updating existing last-hop neighbor of originator\n");
293 orig_node->flags = batman_packet->flags;
294 neigh_node->last_valid = jiffies;
296 ring_buffer_set(neigh_node->tq_recv,
297 &neigh_node->tq_index,
299 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
302 orig_node->last_ttl = batman_packet->ttl;
303 neigh_node->last_ttl = batman_packet->ttl;
306 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
307 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
309 /* if this neighbor already is our next hop there is nothing
311 if (orig_node->router == neigh_node)
314 /* if this neighbor does not offer a better TQ we won't consider it */
315 if ((orig_node->router) &&
316 (orig_node->router->tq_avg > neigh_node->tq_avg))
319 /* if the TQ is the same and the link not more symetric we
320 * won't consider it either */
321 if ((orig_node->router) &&
322 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
323 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
324 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
327 update_routes(bat_priv, orig_node, neigh_node,
328 hna_buff, tmp_hna_buff_len);
332 update_routes(bat_priv, orig_node, orig_node->router,
333 hna_buff, tmp_hna_buff_len);
336 if (orig_node->gw_flags != batman_packet->gw_flags)
337 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
339 orig_node->gw_flags = batman_packet->gw_flags;
341 /* restart gateway selection if fast or late switching was enabled */
342 if ((orig_node->gw_flags) &&
343 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
344 (atomic_read(&bat_priv->gw_sel_class) > 2))
345 gw_check_election(bat_priv, orig_node);
348 /* checks whether the host restarted and is in the protection time.
350 * 0 if the packet is to be accepted
351 * 1 if the packet is to be ignored.
353 static int window_protected(struct bat_priv *bat_priv,
354 int32_t seq_num_diff,
355 unsigned long *last_reset)
357 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
358 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
359 if (time_after(jiffies, *last_reset +
360 msecs_to_jiffies(RESET_PROTECTION_MS))) {
362 *last_reset = jiffies;
363 bat_dbg(DBG_BATMAN, bat_priv,
364 "old packet received, start protection\n");
373 /* processes a batman packet for all interfaces, adjusts the sequence number and
374 * finds out whether it is a duplicate.
376 * 1 the packet is a duplicate
377 * 0 the packet has not yet been received
378 * -1 the packet is old and has been received while the seqno window
379 * was protected. Caller should drop it.
381 static char count_real_packets(struct ethhdr *ethhdr,
382 struct batman_packet *batman_packet,
383 struct batman_if *if_incoming)
385 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
386 struct orig_node *orig_node;
387 struct neigh_node *tmp_neigh_node;
388 char is_duplicate = 0;
393 orig_node = get_orig_node(bat_priv, batman_packet->orig);
397 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
399 /* signalize caller that the packet is to be dropped. */
400 if (window_protected(bat_priv, seq_diff,
401 &orig_node->batman_seqno_reset))
404 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
406 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
407 orig_node->last_real_seqno,
408 batman_packet->seqno);
410 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
411 (tmp_neigh_node->if_incoming == if_incoming))
416 /* if the window moved, set the update flag. */
417 need_update |= bit_get_packet(bat_priv,
418 tmp_neigh_node->real_bits,
421 tmp_neigh_node->real_packet_count =
422 bit_packet_count(tmp_neigh_node->real_bits);
426 bat_dbg(DBG_BATMAN, bat_priv,
427 "updating last_seqno: old %d, new %d\n",
428 orig_node->last_real_seqno, batman_packet->seqno);
429 orig_node->last_real_seqno = batman_packet->seqno;
435 /* copy primary address for bonding */
436 static void mark_bonding_address(struct orig_node *orig_node,
437 struct orig_node *orig_neigh_node,
438 struct batman_packet *batman_packet)
441 if (batman_packet->flags & PRIMARIES_FIRST_HOP)
442 memcpy(orig_neigh_node->primary_addr,
443 orig_node->orig, ETH_ALEN);
448 /* mark possible bond.candidates in the neighbor list */
449 void update_bonding_candidates(struct orig_node *orig_node)
452 int interference_candidate;
454 struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
455 struct neigh_node *first_candidate, *last_candidate;
457 /* update the candidates for this originator */
458 if (!orig_node->router) {
459 orig_node->bond.candidates = 0;
463 best_tq = orig_node->router->tq_avg;
465 /* update bond.candidates */
469 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
470 * as "bonding partner" */
472 /* first, zero the list */
473 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
474 tmp_neigh_node->next_bond_candidate = NULL;
477 first_candidate = NULL;
478 last_candidate = NULL;
479 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
481 /* only consider if it has the same primary address ... */
482 if (memcmp(orig_node->orig,
483 tmp_neigh_node->orig_node->primary_addr,
487 /* ... and is good enough to be considered */
488 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
491 /* check if we have another candidate with the same
492 * mac address or interface. If we do, we won't
493 * select this candidate because of possible interference. */
495 interference_candidate = 0;
496 list_for_each_entry(tmp_neigh_node2,
497 &orig_node->neigh_list, list) {
499 if (tmp_neigh_node2 == tmp_neigh_node)
502 /* we only care if the other candidate is even
503 * considered as candidate. */
504 if (!tmp_neigh_node2->next_bond_candidate)
508 if ((tmp_neigh_node->if_incoming ==
509 tmp_neigh_node2->if_incoming)
510 || (memcmp(tmp_neigh_node->addr,
511 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
513 interference_candidate = 1;
517 /* don't care further if it is an interference candidate */
518 if (interference_candidate)
521 if (!first_candidate) {
522 first_candidate = tmp_neigh_node;
523 tmp_neigh_node->next_bond_candidate = first_candidate;
525 tmp_neigh_node->next_bond_candidate = last_candidate;
527 last_candidate = tmp_neigh_node;
532 if (candidates > 0) {
533 first_candidate->next_bond_candidate = last_candidate;
534 orig_node->bond.selected = first_candidate;
537 orig_node->bond.candidates = candidates;
540 void receive_bat_packet(struct ethhdr *ethhdr,
541 struct batman_packet *batman_packet,
542 unsigned char *hna_buff, int hna_buff_len,
543 struct batman_if *if_incoming)
545 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
546 struct batman_if *batman_if;
547 struct orig_node *orig_neigh_node, *orig_node;
548 char has_directlink_flag;
549 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
550 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
552 uint32_t if_incoming_seqno;
554 /* Silently drop when the batman packet is actually not a
557 * This might happen if a packet is padded (e.g. Ethernet has a
558 * minimum frame length of 64 byte) and the aggregation interprets
559 * it as an additional length.
561 * TODO: A more sane solution would be to have a bit in the
562 * batman_packet to detect whether the packet is the last
563 * packet in an aggregation. Here we expect that the padding
564 * is always zero (or not 0x01)
566 if (batman_packet->packet_type != BAT_PACKET)
569 /* could be changed by schedule_own_packet() */
570 if_incoming_seqno = atomic_read(&if_incoming->seqno);
572 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
574 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
575 batman_packet->orig) ? 1 : 0);
577 bat_dbg(DBG_BATMAN, bat_priv,
578 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
579 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
580 "TTL %d, V %d, IDF %d)\n",
581 ethhdr->h_source, if_incoming->net_dev->name,
582 if_incoming->net_dev->dev_addr, batman_packet->orig,
583 batman_packet->prev_sender, batman_packet->seqno,
584 batman_packet->tq, batman_packet->ttl, batman_packet->version,
585 has_directlink_flag);
588 list_for_each_entry_rcu(batman_if, &if_list, list) {
589 if (batman_if->if_status != IF_ACTIVE)
592 if (batman_if->soft_iface != if_incoming->soft_iface)
595 if (compare_orig(ethhdr->h_source,
596 batman_if->net_dev->dev_addr))
599 if (compare_orig(batman_packet->orig,
600 batman_if->net_dev->dev_addr))
603 if (compare_orig(batman_packet->prev_sender,
604 batman_if->net_dev->dev_addr))
607 if (compare_orig(ethhdr->h_source, broadcast_addr))
612 if (batman_packet->version != COMPAT_VERSION) {
613 bat_dbg(DBG_BATMAN, bat_priv,
614 "Drop packet: incompatible batman version (%i)\n",
615 batman_packet->version);
620 bat_dbg(DBG_BATMAN, bat_priv,
621 "Drop packet: received my own broadcast (sender: %pM"
628 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
629 "ignoring all packets with broadcast source addr (sender: %pM"
630 ")\n", ethhdr->h_source);
638 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
640 if (!orig_neigh_node)
643 /* neighbor has to indicate direct link and it has to
644 * come via the corresponding interface */
645 /* if received seqno equals last send seqno save new
646 * seqno for bidirectional check */
647 if (has_directlink_flag &&
648 compare_orig(if_incoming->net_dev->dev_addr,
649 batman_packet->orig) &&
650 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
651 offset = if_incoming->if_num * NUM_WORDS;
652 word = &(orig_neigh_node->bcast_own[offset]);
654 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
655 bit_packet_count(word);
658 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
659 "originator packet from myself (via neighbor)\n");
664 bat_dbg(DBG_BATMAN, bat_priv,
665 "Drop packet: ignoring all rebroadcast echos (sender: "
666 "%pM)\n", ethhdr->h_source);
670 orig_node = get_orig_node(bat_priv, batman_packet->orig);
674 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
676 if (is_duplicate == -1) {
677 bat_dbg(DBG_BATMAN, bat_priv,
678 "Drop packet: packet within seqno protection time "
679 "(sender: %pM)\n", ethhdr->h_source);
683 if (batman_packet->tq == 0) {
684 bat_dbg(DBG_BATMAN, bat_priv,
685 "Drop packet: originator packet with tq equal 0\n");
689 /* avoid temporary routing loops */
690 if ((orig_node->router) &&
691 (orig_node->router->orig_node->router) &&
692 (compare_orig(orig_node->router->addr,
693 batman_packet->prev_sender)) &&
694 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
695 (compare_orig(orig_node->router->addr,
696 orig_node->router->orig_node->router->addr))) {
697 bat_dbg(DBG_BATMAN, bat_priv,
698 "Drop packet: ignoring all rebroadcast packets that "
699 "may make me loop (sender: %pM)\n", ethhdr->h_source);
703 /* if sender is a direct neighbor the sender mac equals
705 orig_neigh_node = (is_single_hop_neigh ?
707 get_orig_node(bat_priv, ethhdr->h_source));
708 if (!orig_neigh_node)
711 /* drop packet if sender is not a direct neighbor and if we
712 * don't route towards it */
713 if (!is_single_hop_neigh && (!orig_neigh_node->router)) {
714 bat_dbg(DBG_BATMAN, bat_priv,
715 "Drop packet: OGM via unknown neighbor!\n");
719 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
720 batman_packet, if_incoming);
722 /* update ranking if it is not a duplicate or has the same
723 * seqno and similar ttl as the non-duplicate */
724 if (is_bidirectional &&
726 ((orig_node->last_real_seqno == batman_packet->seqno) &&
727 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
728 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
729 if_incoming, hna_buff, hna_buff_len, is_duplicate);
731 mark_bonding_address(orig_node, orig_neigh_node, batman_packet);
732 update_bonding_candidates(orig_node);
734 /* is single hop (direct) neighbor */
735 if (is_single_hop_neigh) {
737 /* mark direct link on incoming interface */
738 schedule_forward_packet(orig_node, ethhdr, batman_packet,
739 1, hna_buff_len, if_incoming);
741 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
742 "rebroadcast neighbor packet with direct link flag\n");
746 /* multihop originator */
747 if (!is_bidirectional) {
748 bat_dbg(DBG_BATMAN, bat_priv,
749 "Drop packet: not received via bidirectional link\n");
754 bat_dbg(DBG_BATMAN, bat_priv,
755 "Drop packet: duplicate packet received\n");
759 bat_dbg(DBG_BATMAN, bat_priv,
760 "Forwarding packet: rebroadcast originator packet\n");
761 schedule_forward_packet(orig_node, ethhdr, batman_packet,
762 0, hna_buff_len, if_incoming);
765 int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
767 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
768 struct ethhdr *ethhdr;
770 /* drop packet if it has not necessary minimum size */
771 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
774 ethhdr = (struct ethhdr *)skb_mac_header(skb);
776 /* packet with broadcast indication but unicast recipient */
777 if (!is_broadcast_ether_addr(ethhdr->h_dest))
780 /* packet with broadcast sender address */
781 if (is_broadcast_ether_addr(ethhdr->h_source))
784 /* create a copy of the skb, if needed, to modify it. */
785 if (skb_cow(skb, 0) < 0)
788 /* keep skb linear */
789 if (skb_linearize(skb) < 0)
792 ethhdr = (struct ethhdr *)skb_mac_header(skb);
794 spin_lock_bh(&bat_priv->orig_hash_lock);
795 receive_aggr_bat_packet(ethhdr,
799 spin_unlock_bh(&bat_priv->orig_hash_lock);
802 return NET_RX_SUCCESS;
805 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
806 struct sk_buff *skb, size_t icmp_len)
808 struct orig_node *orig_node;
809 struct icmp_packet_rr *icmp_packet;
810 struct batman_if *batman_if;
812 uint8_t dstaddr[ETH_ALEN];
814 icmp_packet = (struct icmp_packet_rr *)skb->data;
816 /* add data to device queue */
817 if (icmp_packet->msg_type != ECHO_REQUEST) {
818 bat_socket_receive_packet(icmp_packet, icmp_len);
822 if (!bat_priv->primary_if)
825 /* answer echo request (ping) */
826 /* get routing information */
827 spin_lock_bh(&bat_priv->orig_hash_lock);
828 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
829 compare_orig, choose_orig,
833 if ((orig_node) && (orig_node->router)) {
835 /* don't lock while sending the packets ... we therefore
836 * copy the required data before sending */
837 batman_if = orig_node->router->if_incoming;
838 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
839 spin_unlock_bh(&bat_priv->orig_hash_lock);
841 /* create a copy of the skb, if needed, to modify it. */
842 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
845 icmp_packet = (struct icmp_packet_rr *)skb->data;
847 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
848 memcpy(icmp_packet->orig,
849 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
850 icmp_packet->msg_type = ECHO_REPLY;
851 icmp_packet->ttl = TTL;
853 send_skb_packet(skb, batman_if, dstaddr);
854 ret = NET_RX_SUCCESS;
857 spin_unlock_bh(&bat_priv->orig_hash_lock);
862 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
865 struct orig_node *orig_node;
866 struct icmp_packet *icmp_packet;
867 struct batman_if *batman_if;
869 uint8_t dstaddr[ETH_ALEN];
871 icmp_packet = (struct icmp_packet *)skb->data;
873 /* send TTL exceeded if packet is an echo request (traceroute) */
874 if (icmp_packet->msg_type != ECHO_REQUEST) {
875 pr_debug("Warning - can't forward icmp packet from %pM to "
876 "%pM: ttl exceeded\n", icmp_packet->orig,
881 if (!bat_priv->primary_if)
884 /* get routing information */
885 spin_lock_bh(&bat_priv->orig_hash_lock);
886 orig_node = ((struct orig_node *)
887 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
891 if ((orig_node) && (orig_node->router)) {
893 /* don't lock while sending the packets ... we therefore
894 * copy the required data before sending */
895 batman_if = orig_node->router->if_incoming;
896 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
897 spin_unlock_bh(&bat_priv->orig_hash_lock);
899 /* create a copy of the skb, if needed, to modify it. */
900 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
903 icmp_packet = (struct icmp_packet *) skb->data;
905 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
906 memcpy(icmp_packet->orig,
907 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
908 icmp_packet->msg_type = TTL_EXCEEDED;
909 icmp_packet->ttl = TTL;
911 send_skb_packet(skb, batman_if, dstaddr);
912 ret = NET_RX_SUCCESS;
915 spin_unlock_bh(&bat_priv->orig_hash_lock);
921 int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
923 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
924 struct icmp_packet_rr *icmp_packet;
925 struct ethhdr *ethhdr;
926 struct orig_node *orig_node;
927 struct batman_if *batman_if;
928 int hdr_size = sizeof(struct icmp_packet);
930 uint8_t dstaddr[ETH_ALEN];
933 * we truncate all incoming icmp packets if they don't match our size
935 if (skb->len >= sizeof(struct icmp_packet_rr))
936 hdr_size = sizeof(struct icmp_packet_rr);
938 /* drop packet if it has not necessary minimum size */
939 if (unlikely(!pskb_may_pull(skb, hdr_size)))
942 ethhdr = (struct ethhdr *)skb_mac_header(skb);
944 /* packet with unicast indication but broadcast recipient */
945 if (is_broadcast_ether_addr(ethhdr->h_dest))
948 /* packet with broadcast sender address */
949 if (is_broadcast_ether_addr(ethhdr->h_source))
953 if (!is_my_mac(ethhdr->h_dest))
956 icmp_packet = (struct icmp_packet_rr *)skb->data;
958 /* add record route information if not full */
959 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
960 (icmp_packet->rr_cur < BAT_RR_LEN)) {
961 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
962 ethhdr->h_dest, ETH_ALEN);
963 icmp_packet->rr_cur++;
967 if (is_my_mac(icmp_packet->dst))
968 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
971 if (icmp_packet->ttl < 2)
972 return recv_icmp_ttl_exceeded(bat_priv, skb);
976 /* get routing information */
977 spin_lock_bh(&bat_priv->orig_hash_lock);
978 orig_node = ((struct orig_node *)
979 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
982 if ((orig_node) && (orig_node->router)) {
984 /* don't lock while sending the packets ... we therefore
985 * copy the required data before sending */
986 batman_if = orig_node->router->if_incoming;
987 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
988 spin_unlock_bh(&bat_priv->orig_hash_lock);
990 /* create a copy of the skb, if needed, to modify it. */
991 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
994 icmp_packet = (struct icmp_packet_rr *)skb->data;
1000 send_skb_packet(skb, batman_if, dstaddr);
1001 ret = NET_RX_SUCCESS;
1004 spin_unlock_bh(&bat_priv->orig_hash_lock);
1009 /* find a suitable router for this originator, and use
1010 * bonding if possible. */
1011 struct neigh_node *find_router(struct bat_priv *bat_priv,
1012 struct orig_node *orig_node,
1013 struct batman_if *recv_if)
1015 struct orig_node *primary_orig_node;
1016 struct orig_node *router_orig;
1017 struct neigh_node *router, *first_candidate, *best_router;
1018 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1019 int bonding_enabled;
1024 if (!orig_node->router)
1027 /* without bonding, the first node should
1028 * always choose the default router. */
1030 bonding_enabled = atomic_read(&bat_priv->bonding);
1032 if ((!recv_if) && (!bonding_enabled))
1033 return orig_node->router;
1035 router_orig = orig_node->router->orig_node;
1037 /* if we have something in the primary_addr, we can search
1038 * for a potential bonding candidate. */
1039 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1040 return orig_node->router;
1042 /* find the orig_node which has the primary interface. might
1043 * even be the same as our router_orig in many cases */
1045 if (memcmp(router_orig->primary_addr,
1046 router_orig->orig, ETH_ALEN) == 0) {
1047 primary_orig_node = router_orig;
1049 primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig,
1051 router_orig->primary_addr);
1053 if (!primary_orig_node)
1054 return orig_node->router;
1057 /* with less than 2 candidates, we can't do any
1058 * bonding and prefer the original router. */
1060 if (primary_orig_node->bond.candidates < 2)
1061 return orig_node->router;
1064 /* all nodes between should choose a candidate which
1065 * is is not on the interface where the packet came
1067 first_candidate = primary_orig_node->bond.selected;
1068 router = first_candidate;
1070 if (bonding_enabled) {
1071 /* in the bonding case, send the packets in a round
1072 * robin fashion over the remaining interfaces. */
1074 /* recv_if == NULL on the first node. */
1075 if (router->if_incoming != recv_if)
1078 router = router->next_bond_candidate;
1079 } while (router != first_candidate);
1081 primary_orig_node->bond.selected = router->next_bond_candidate;
1084 /* if bonding is disabled, use the best of the
1085 * remaining candidates which are not using
1086 * this interface. */
1087 best_router = first_candidate;
1090 /* recv_if == NULL on the first node. */
1091 if ((router->if_incoming != recv_if) &&
1092 (router->tq_avg > best_router->tq_avg))
1093 best_router = router;
1095 router = router->next_bond_candidate;
1096 } while (router != first_candidate);
1098 router = best_router;
1104 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1106 struct ethhdr *ethhdr;
1108 /* drop packet if it has not necessary minimum size */
1109 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1112 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1114 /* packet with unicast indication but broadcast recipient */
1115 if (is_broadcast_ether_addr(ethhdr->h_dest))
1118 /* packet with broadcast sender address */
1119 if (is_broadcast_ether_addr(ethhdr->h_source))
1123 if (!is_my_mac(ethhdr->h_dest))
1129 int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1132 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1133 struct orig_node *orig_node;
1134 struct neigh_node *router;
1135 struct batman_if *batman_if;
1136 uint8_t dstaddr[ETH_ALEN];
1137 struct unicast_packet *unicast_packet;
1138 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1140 struct sk_buff *new_skb;
1142 unicast_packet = (struct unicast_packet *)skb->data;
1145 if (unicast_packet->ttl < 2) {
1146 pr_debug("Warning - can't forward unicast packet from %pM to "
1147 "%pM: ttl exceeded\n", ethhdr->h_source,
1148 unicast_packet->dest);
1152 /* get routing information */
1153 spin_lock_bh(&bat_priv->orig_hash_lock);
1154 orig_node = ((struct orig_node *)
1155 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1156 unicast_packet->dest));
1158 router = find_router(bat_priv, orig_node, recv_if);
1161 spin_unlock_bh(&bat_priv->orig_hash_lock);
1165 /* don't lock while sending the packets ... we therefore
1166 * copy the required data before sending */
1168 batman_if = router->if_incoming;
1169 memcpy(dstaddr, router->addr, ETH_ALEN);
1171 spin_unlock_bh(&bat_priv->orig_hash_lock);
1173 /* create a copy of the skb, if needed, to modify it. */
1174 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1177 unicast_packet = (struct unicast_packet *)skb->data;
1179 if (unicast_packet->packet_type == BAT_UNICAST &&
1180 atomic_read(&bat_priv->fragmentation) &&
1181 skb->len > batman_if->net_dev->mtu)
1182 return frag_send_skb(skb, bat_priv, batman_if,
1185 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1186 frag_can_reassemble(skb, batman_if->net_dev->mtu)) {
1188 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1190 if (ret == NET_RX_DROP)
1193 /* packet was buffered for late merge */
1195 return NET_RX_SUCCESS;
1198 unicast_packet = (struct unicast_packet *)skb->data;
1202 unicast_packet->ttl--;
1205 send_skb_packet(skb, batman_if, dstaddr);
1207 return NET_RX_SUCCESS;
1210 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1212 struct unicast_packet *unicast_packet;
1213 int hdr_size = sizeof(struct unicast_packet);
1215 if (check_unicast_packet(skb, hdr_size) < 0)
1218 unicast_packet = (struct unicast_packet *)skb->data;
1221 if (is_my_mac(unicast_packet->dest)) {
1222 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1223 return NET_RX_SUCCESS;
1226 return route_unicast_packet(skb, recv_if, hdr_size);
1229 int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1231 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1232 struct unicast_frag_packet *unicast_packet;
1233 int hdr_size = sizeof(struct unicast_frag_packet);
1234 struct sk_buff *new_skb = NULL;
1237 if (check_unicast_packet(skb, hdr_size) < 0)
1240 unicast_packet = (struct unicast_frag_packet *)skb->data;
1243 if (is_my_mac(unicast_packet->dest)) {
1245 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1247 if (ret == NET_RX_DROP)
1250 /* packet was buffered for late merge */
1252 return NET_RX_SUCCESS;
1254 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1255 sizeof(struct unicast_packet));
1256 return NET_RX_SUCCESS;
1259 return route_unicast_packet(skb, recv_if, hdr_size);
1263 int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1265 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1266 struct orig_node *orig_node;
1267 struct bcast_packet *bcast_packet;
1268 struct ethhdr *ethhdr;
1269 int hdr_size = sizeof(struct bcast_packet);
1272 /* drop packet if it has not necessary minimum size */
1273 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1276 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1278 /* packet with broadcast indication but unicast recipient */
1279 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1282 /* packet with broadcast sender address */
1283 if (is_broadcast_ether_addr(ethhdr->h_source))
1286 /* ignore broadcasts sent by myself */
1287 if (is_my_mac(ethhdr->h_source))
1290 bcast_packet = (struct bcast_packet *)skb->data;
1292 /* ignore broadcasts originated by myself */
1293 if (is_my_mac(bcast_packet->orig))
1296 if (bcast_packet->ttl < 2)
1299 spin_lock_bh(&bat_priv->orig_hash_lock);
1300 orig_node = ((struct orig_node *)
1301 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1302 bcast_packet->orig));
1305 spin_unlock_bh(&bat_priv->orig_hash_lock);
1309 /* check whether the packet is a duplicate */
1310 if (get_bit_status(orig_node->bcast_bits,
1311 orig_node->last_bcast_seqno,
1312 ntohl(bcast_packet->seqno))) {
1313 spin_unlock_bh(&bat_priv->orig_hash_lock);
1317 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1319 /* check whether the packet is old and the host just restarted. */
1320 if (window_protected(bat_priv, seq_diff,
1321 &orig_node->bcast_seqno_reset)) {
1322 spin_unlock_bh(&bat_priv->orig_hash_lock);
1326 /* mark broadcast in flood history, update window position
1328 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1329 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1331 spin_unlock_bh(&bat_priv->orig_hash_lock);
1332 /* rebroadcast packet */
1333 add_bcast_packet_to_list(bat_priv, skb);
1335 /* broadcast for me */
1336 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1338 return NET_RX_SUCCESS;
1341 int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1343 struct vis_packet *vis_packet;
1344 struct ethhdr *ethhdr;
1345 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1346 int hdr_size = sizeof(struct vis_packet);
1348 /* keep skb linear */
1349 if (skb_linearize(skb) < 0)
1352 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1355 vis_packet = (struct vis_packet *)skb->data;
1356 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1359 if (!is_my_mac(ethhdr->h_dest))
1362 /* ignore own packets */
1363 if (is_my_mac(vis_packet->vis_orig))
1366 if (is_my_mac(vis_packet->sender_orig))
1369 switch (vis_packet->vis_type) {
1370 case VIS_TYPE_SERVER_SYNC:
1371 receive_server_sync_packet(bat_priv, vis_packet,
1375 case VIS_TYPE_CLIENT_UPDATE:
1376 receive_client_update_packet(bat_priv, vis_packet,
1380 default: /* ignore unknown packet */
1384 /* We take a copy of the data in the packet, so we should
1385 always free the skbuf. */