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"
36 static DECLARE_WAIT_QUEUE_HEAD(thread_wait);
38 void slide_own_bcast_window(struct batman_if *batman_if)
41 struct orig_node *orig_node;
45 spin_lock_irqsave(&orig_hash_lock, flags);
47 while (hash_iterate(orig_hash, &hashit)) {
48 orig_node = hashit.bucket->data;
49 word = &(orig_node->bcast_own[batman_if->if_num * NUM_WORDS]);
51 bit_get_packet(word, 1, 0);
52 orig_node->bcast_own_sum[batman_if->if_num] =
53 bit_packet_count(word);
56 spin_unlock_irqrestore(&orig_hash_lock, flags);
59 static void update_HNA(struct orig_node *orig_node,
60 unsigned char *hna_buff, int hna_buff_len)
62 if ((hna_buff_len != orig_node->hna_buff_len) ||
63 ((hna_buff_len > 0) &&
64 (orig_node->hna_buff_len > 0) &&
65 (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
67 if (orig_node->hna_buff_len > 0)
68 hna_global_del_orig(orig_node,
69 "originator changed hna");
71 if ((hna_buff_len > 0) && (hna_buff != NULL))
72 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
76 static void update_route(struct orig_node *orig_node,
77 struct neigh_node *neigh_node,
78 unsigned char *hna_buff, int hna_buff_len)
80 /* FIXME: each orig_node->batman_if will be attached to a softif */
81 struct bat_priv *bat_priv = netdev_priv(soft_device);
84 if ((orig_node->router != NULL) && (neigh_node == NULL)) {
86 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
88 hna_global_del_orig(orig_node, "originator timed out");
91 } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
93 bat_dbg(DBG_ROUTES, bat_priv,
94 "Adding route towards: %pM (via %pM)\n",
95 orig_node->orig, neigh_node->addr);
96 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
100 bat_dbg(DBG_ROUTES, bat_priv,
101 "Changing route towards: %pM "
102 "(now via %pM - was via %pM)\n",
103 orig_node->orig, neigh_node->addr,
104 orig_node->router->addr);
107 orig_node->router = neigh_node;
111 void update_routes(struct orig_node *orig_node,
112 struct neigh_node *neigh_node,
113 unsigned char *hna_buff, int hna_buff_len)
116 if (orig_node == NULL)
119 if (orig_node->router != neigh_node)
120 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
121 /* may be just HNA changed */
123 update_HNA(orig_node, hna_buff, hna_buff_len);
126 static int is_bidirectional_neigh(struct orig_node *orig_node,
127 struct orig_node *orig_neigh_node,
128 struct batman_packet *batman_packet,
129 struct batman_if *if_incoming)
131 /* FIXME: each orig_node->batman_if will be attached to a softif */
132 struct bat_priv *bat_priv = netdev_priv(soft_device);
133 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
134 unsigned char total_count;
136 if (orig_node == orig_neigh_node) {
137 list_for_each_entry(tmp_neigh_node,
138 &orig_node->neigh_list,
141 if (compare_orig(tmp_neigh_node->addr,
142 orig_neigh_node->orig) &&
143 (tmp_neigh_node->if_incoming == if_incoming))
144 neigh_node = tmp_neigh_node;
148 neigh_node = create_neighbor(orig_node,
150 orig_neigh_node->orig,
152 /* create_neighbor failed, return 0 */
156 neigh_node->last_valid = jiffies;
158 /* find packet count of corresponding one hop neighbor */
159 list_for_each_entry(tmp_neigh_node,
160 &orig_neigh_node->neigh_list, list) {
162 if (compare_orig(tmp_neigh_node->addr,
163 orig_neigh_node->orig) &&
164 (tmp_neigh_node->if_incoming == if_incoming))
165 neigh_node = tmp_neigh_node;
169 neigh_node = create_neighbor(orig_neigh_node,
171 orig_neigh_node->orig,
173 /* create_neighbor failed, return 0 */
178 orig_node->last_valid = jiffies;
180 /* pay attention to not get a value bigger than 100 % */
181 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
182 neigh_node->real_packet_count ?
183 neigh_node->real_packet_count :
184 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
186 /* if we have too few packets (too less data) we set tq_own to zero */
187 /* if we receive too few packets it is not considered bidirectional */
188 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
189 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
190 orig_neigh_node->tq_own = 0;
192 /* neigh_node->real_packet_count is never zero as we
193 * only purge old information when getting new
195 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
196 neigh_node->real_packet_count;
199 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
200 * affect the nearly-symmetric links only a little, but
201 * punishes asymmetric links more. This will give a value
202 * between 0 and TQ_MAX_VALUE
204 orig_neigh_node->tq_asym_penalty =
207 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
208 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
209 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
210 (TQ_LOCAL_WINDOW_SIZE *
211 TQ_LOCAL_WINDOW_SIZE *
212 TQ_LOCAL_WINDOW_SIZE);
214 batman_packet->tq = ((batman_packet->tq *
215 orig_neigh_node->tq_own *
216 orig_neigh_node->tq_asym_penalty) /
217 (TQ_MAX_VALUE * TQ_MAX_VALUE));
219 bat_dbg(DBG_BATMAN, bat_priv,
221 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
222 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
224 orig_node->orig, orig_neigh_node->orig, total_count,
225 neigh_node->real_packet_count, orig_neigh_node->tq_own,
226 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
228 /* if link has the minimum required transmission quality
229 * consider it bidirectional */
230 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
236 static void update_orig(struct orig_node *orig_node, struct ethhdr *ethhdr,
237 struct batman_packet *batman_packet,
238 struct batman_if *if_incoming,
239 unsigned char *hna_buff, int hna_buff_len,
242 /* FIXME: get bat_priv */
243 struct bat_priv *bat_priv = netdev_priv(soft_device);
244 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
245 int tmp_hna_buff_len;
247 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
248 "Searching and updating originator entry of received packet\n");
250 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
251 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
252 (tmp_neigh_node->if_incoming == if_incoming)) {
253 neigh_node = tmp_neigh_node;
260 ring_buffer_set(tmp_neigh_node->tq_recv,
261 &tmp_neigh_node->tq_index, 0);
262 tmp_neigh_node->tq_avg =
263 ring_buffer_avg(tmp_neigh_node->tq_recv);
267 struct orig_node *orig_tmp;
269 orig_tmp = get_orig_node(ethhdr->h_source);
273 neigh_node = create_neighbor(orig_node,
275 ethhdr->h_source, if_incoming);
279 bat_dbg(DBG_BATMAN, bat_priv,
280 "Updating existing last-hop neighbor of originator\n");
282 orig_node->flags = batman_packet->flags;
283 neigh_node->last_valid = jiffies;
285 ring_buffer_set(neigh_node->tq_recv,
286 &neigh_node->tq_index,
288 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
291 orig_node->last_ttl = batman_packet->ttl;
292 neigh_node->last_ttl = batman_packet->ttl;
295 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
296 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
298 /* if this neighbor already is our next hop there is nothing
300 if (orig_node->router == neigh_node)
303 /* if this neighbor does not offer a better TQ we won't consider it */
304 if ((orig_node->router) &&
305 (orig_node->router->tq_avg > neigh_node->tq_avg))
308 /* if the TQ is the same and the link not more symetric we
309 * won't consider it either */
310 if ((orig_node->router) &&
311 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
312 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
313 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
316 update_routes(orig_node, neigh_node, hna_buff, tmp_hna_buff_len);
320 update_routes(orig_node, orig_node->router, hna_buff, tmp_hna_buff_len);
323 /* checks whether the host restarted and is in the protection time.
325 * 0 if the packet is to be accepted
326 * 1 if the packet is to be ignored.
328 static int window_protected(int32_t seq_num_diff,
329 unsigned long *last_reset)
331 /* FIXME: each orig_node->batman_if will be attached to a softif */
332 struct bat_priv *bat_priv = netdev_priv(soft_device);
334 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
335 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
336 if (time_after(jiffies, *last_reset +
337 msecs_to_jiffies(RESET_PROTECTION_MS))) {
339 *last_reset = jiffies;
340 bat_dbg(DBG_BATMAN, bat_priv,
341 "old packet received, start protection\n");
350 /* processes a batman packet for all interfaces, adjusts the sequence number and
351 * finds out whether it is a duplicate.
353 * 1 the packet is a duplicate
354 * 0 the packet has not yet been received
355 * -1 the packet is old and has been received while the seqno window
356 * was protected. Caller should drop it.
358 static char count_real_packets(struct ethhdr *ethhdr,
359 struct batman_packet *batman_packet,
360 struct batman_if *if_incoming)
362 /* FIXME: each orig_node->batman_if will be attached to a softif */
363 struct bat_priv *bat_priv = netdev_priv(soft_device);
364 struct orig_node *orig_node;
365 struct neigh_node *tmp_neigh_node;
366 char is_duplicate = 0;
371 orig_node = get_orig_node(batman_packet->orig);
372 if (orig_node == NULL)
375 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
377 /* signalize caller that the packet is to be dropped. */
378 if (window_protected(seq_diff, &orig_node->batman_seqno_reset))
381 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
383 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
384 orig_node->last_real_seqno,
385 batman_packet->seqno);
387 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
388 (tmp_neigh_node->if_incoming == if_incoming))
393 /* if the window moved, set the update flag. */
394 need_update |= bit_get_packet(tmp_neigh_node->real_bits,
397 tmp_neigh_node->real_packet_count =
398 bit_packet_count(tmp_neigh_node->real_bits);
402 bat_dbg(DBG_BATMAN, bat_priv,
403 "updating last_seqno: old %d, new %d\n",
404 orig_node->last_real_seqno, batman_packet->seqno);
405 orig_node->last_real_seqno = batman_packet->seqno;
411 /* copy primary address for bonding */
412 static void mark_bonding_address(struct bat_priv *bat_priv,
413 struct orig_node *orig_node,
414 struct orig_node *orig_neigh_node,
415 struct batman_packet *batman_packet)
418 if (batman_packet->flags & PRIMARIES_FIRST_HOP)
419 memcpy(orig_neigh_node->primary_addr,
420 orig_node->orig, ETH_ALEN);
425 /* mark possible bond.candidates in the neighbor list */
426 void update_bonding_candidates(struct bat_priv *bat_priv,
427 struct orig_node *orig_node)
430 int interference_candidate;
432 struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
433 struct neigh_node *first_candidate, *last_candidate;
435 /* update the candidates for this originator */
436 if (!orig_node->router) {
437 orig_node->bond.candidates = 0;
441 best_tq = orig_node->router->tq_avg;
443 /* update bond.candidates */
447 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
448 * as "bonding partner" */
450 /* first, zero the list */
451 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
452 tmp_neigh_node->next_bond_candidate = NULL;
455 first_candidate = NULL;
456 last_candidate = NULL;
457 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
459 /* only consider if it has the same primary address ... */
460 if (memcmp(orig_node->orig,
461 tmp_neigh_node->orig_node->primary_addr,
465 /* ... and is good enough to be considered */
466 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
469 /* check if we have another candidate with the same
470 * mac address or interface. If we do, we won't
471 * select this candidate because of possible interference. */
473 interference_candidate = 0;
474 list_for_each_entry(tmp_neigh_node2,
475 &orig_node->neigh_list, list) {
477 if (tmp_neigh_node2 == tmp_neigh_node)
480 /* we only care if the other candidate is even
481 * considered as candidate. */
482 if (tmp_neigh_node2->next_bond_candidate == NULL)
486 if ((tmp_neigh_node->if_incoming ==
487 tmp_neigh_node2->if_incoming)
488 || (memcmp(tmp_neigh_node->addr,
489 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
491 interference_candidate = 1;
495 /* don't care further if it is an interference candidate */
496 if (interference_candidate)
499 if (first_candidate == NULL) {
500 first_candidate = tmp_neigh_node;
501 tmp_neigh_node->next_bond_candidate = first_candidate;
503 tmp_neigh_node->next_bond_candidate = last_candidate;
505 last_candidate = tmp_neigh_node;
510 if (candidates > 0) {
511 first_candidate->next_bond_candidate = last_candidate;
512 orig_node->bond.selected = first_candidate;
515 orig_node->bond.candidates = candidates;
518 void receive_bat_packet(struct ethhdr *ethhdr,
519 struct batman_packet *batman_packet,
520 unsigned char *hna_buff, int hna_buff_len,
521 struct batman_if *if_incoming)
523 /* FIXME: each orig_node->batman_if will be attached to a softif */
524 struct bat_priv *bat_priv = netdev_priv(soft_device);
525 struct batman_if *batman_if;
526 struct orig_node *orig_neigh_node, *orig_node;
527 char has_directlink_flag;
528 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
529 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
531 uint32_t if_incoming_seqno;
533 /* Silently drop when the batman packet is actually not a
536 * This might happen if a packet is padded (e.g. Ethernet has a
537 * minimum frame length of 64 byte) and the aggregation interprets
538 * it as an additional length.
540 * TODO: A more sane solution would be to have a bit in the
541 * batman_packet to detect whether the packet is the last
542 * packet in an aggregation. Here we expect that the padding
543 * is always zero (or not 0x01)
545 if (batman_packet->packet_type != BAT_PACKET)
548 /* could be changed by schedule_own_packet() */
549 if_incoming_seqno = atomic_read(&if_incoming->seqno);
551 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
553 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
554 batman_packet->orig) ? 1 : 0);
556 bat_dbg(DBG_BATMAN, bat_priv,
557 "Received BATMAN packet via NB: %pM, IF: %s [%s] "
558 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
559 "TTL %d, V %d, IDF %d)\n",
560 ethhdr->h_source, if_incoming->dev, if_incoming->addr_str,
561 batman_packet->orig, batman_packet->prev_sender,
562 batman_packet->seqno, batman_packet->tq, batman_packet->ttl,
563 batman_packet->version, has_directlink_flag);
565 list_for_each_entry_rcu(batman_if, &if_list, list) {
566 if (batman_if->if_status != IF_ACTIVE)
569 if (compare_orig(ethhdr->h_source,
570 batman_if->net_dev->dev_addr))
573 if (compare_orig(batman_packet->orig,
574 batman_if->net_dev->dev_addr))
577 if (compare_orig(batman_packet->prev_sender,
578 batman_if->net_dev->dev_addr))
581 if (compare_orig(ethhdr->h_source, broadcast_addr))
585 if (batman_packet->version != COMPAT_VERSION) {
586 bat_dbg(DBG_BATMAN, bat_priv,
587 "Drop packet: incompatible batman version (%i)\n",
588 batman_packet->version);
593 bat_dbg(DBG_BATMAN, bat_priv,
594 "Drop packet: received my own broadcast (sender: %pM"
601 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
602 "ignoring all packets with broadcast source addr (sender: %pM"
603 ")\n", ethhdr->h_source);
611 orig_neigh_node = get_orig_node(ethhdr->h_source);
613 if (!orig_neigh_node)
616 /* neighbor has to indicate direct link and it has to
617 * come via the corresponding interface */
618 /* if received seqno equals last send seqno save new
619 * seqno for bidirectional check */
620 if (has_directlink_flag &&
621 compare_orig(if_incoming->net_dev->dev_addr,
622 batman_packet->orig) &&
623 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
624 offset = if_incoming->if_num * NUM_WORDS;
625 word = &(orig_neigh_node->bcast_own[offset]);
627 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
628 bit_packet_count(word);
631 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
632 "originator packet from myself (via neighbor)\n");
637 bat_dbg(DBG_BATMAN, bat_priv,
638 "Drop packet: ignoring all rebroadcast echos (sender: "
639 "%pM)\n", ethhdr->h_source);
643 orig_node = get_orig_node(batman_packet->orig);
644 if (orig_node == NULL)
647 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
649 if (is_duplicate == -1) {
650 bat_dbg(DBG_BATMAN, bat_priv,
651 "Drop packet: packet within seqno protection time "
652 "(sender: %pM)\n", ethhdr->h_source);
656 if (batman_packet->tq == 0) {
657 bat_dbg(DBG_BATMAN, bat_priv,
658 "Drop packet: originator packet with tq equal 0\n");
662 /* avoid temporary routing loops */
663 if ((orig_node->router) &&
664 (orig_node->router->orig_node->router) &&
665 (compare_orig(orig_node->router->addr,
666 batman_packet->prev_sender)) &&
667 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
668 (compare_orig(orig_node->router->addr,
669 orig_node->router->orig_node->router->addr))) {
670 bat_dbg(DBG_BATMAN, bat_priv,
671 "Drop packet: ignoring all rebroadcast packets that "
672 "may make me loop (sender: %pM)\n", ethhdr->h_source);
676 /* if sender is a direct neighbor the sender mac equals
678 orig_neigh_node = (is_single_hop_neigh ?
679 orig_node : get_orig_node(ethhdr->h_source));
680 if (orig_neigh_node == NULL)
683 /* drop packet if sender is not a direct neighbor and if we
684 * don't route towards it */
685 if (!is_single_hop_neigh &&
686 (orig_neigh_node->router == NULL)) {
687 bat_dbg(DBG_BATMAN, bat_priv,
688 "Drop packet: OGM via unknown neighbor!\n");
692 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
693 batman_packet, if_incoming);
695 /* update ranking if it is not a duplicate or has the same
696 * seqno and similar ttl as the non-duplicate */
697 if (is_bidirectional &&
699 ((orig_node->last_real_seqno == batman_packet->seqno) &&
700 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
701 update_orig(orig_node, ethhdr, batman_packet,
702 if_incoming, hna_buff, hna_buff_len, is_duplicate);
704 mark_bonding_address(bat_priv, orig_node,
705 orig_neigh_node, batman_packet);
706 update_bonding_candidates(bat_priv, orig_node);
708 /* is single hop (direct) neighbor */
709 if (is_single_hop_neigh) {
711 /* mark direct link on incoming interface */
712 schedule_forward_packet(orig_node, ethhdr, batman_packet,
713 1, hna_buff_len, if_incoming);
715 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
716 "rebroadcast neighbor packet with direct link flag\n");
720 /* multihop originator */
721 if (!is_bidirectional) {
722 bat_dbg(DBG_BATMAN, bat_priv,
723 "Drop packet: not received via bidirectional link\n");
728 bat_dbg(DBG_BATMAN, bat_priv,
729 "Drop packet: duplicate packet received\n");
733 bat_dbg(DBG_BATMAN, bat_priv,
734 "Forwarding packet: rebroadcast originator packet\n");
735 schedule_forward_packet(orig_node, ethhdr, batman_packet,
736 0, hna_buff_len, if_incoming);
739 int recv_bat_packet(struct sk_buff *skb,
740 struct batman_if *batman_if)
742 struct ethhdr *ethhdr;
744 struct sk_buff *skb_old;
746 /* drop packet if it has not necessary minimum size */
747 if (skb_headlen(skb) < sizeof(struct batman_packet))
750 ethhdr = (struct ethhdr *)skb_mac_header(skb);
752 /* packet with broadcast indication but unicast recipient */
753 if (!is_bcast(ethhdr->h_dest))
756 /* packet with broadcast sender address */
757 if (is_bcast(ethhdr->h_source))
760 /* TODO: we use headlen instead of "length", because
761 * only this data is paged in. */
763 /* create a copy of the skb, if needed, to modify it. */
764 if (!skb_clone_writable(skb, skb_headlen(skb))) {
766 skb = skb_copy(skb, GFP_ATOMIC);
769 ethhdr = (struct ethhdr *)skb_mac_header(skb);
773 spin_lock_irqsave(&orig_hash_lock, flags);
774 receive_aggr_bat_packet(ethhdr,
778 spin_unlock_irqrestore(&orig_hash_lock, flags);
781 return NET_RX_SUCCESS;
784 static int recv_my_icmp_packet(struct sk_buff *skb, size_t icmp_len)
786 struct orig_node *orig_node;
787 struct icmp_packet_rr *icmp_packet;
788 struct ethhdr *ethhdr;
789 struct sk_buff *skb_old;
790 struct batman_if *batman_if;
793 uint8_t dstaddr[ETH_ALEN];
795 icmp_packet = (struct icmp_packet_rr *)skb->data;
796 ethhdr = (struct ethhdr *)skb_mac_header(skb);
798 /* add data to device queue */
799 if (icmp_packet->msg_type != ECHO_REQUEST) {
800 bat_socket_receive_packet(icmp_packet, icmp_len);
804 /* answer echo request (ping) */
805 /* get routing information */
806 spin_lock_irqsave(&orig_hash_lock, flags);
807 orig_node = ((struct orig_node *)hash_find(orig_hash,
811 if ((orig_node != NULL) &&
812 (orig_node->router != NULL)) {
814 /* don't lock while sending the packets ... we therefore
815 * copy the required data before sending */
816 batman_if = orig_node->router->if_incoming;
817 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
818 spin_unlock_irqrestore(&orig_hash_lock, flags);
820 /* create a copy of the skb, if needed, to modify it. */
822 if (!skb_clone_writable(skb, icmp_len)) {
824 skb = skb_copy(skb, GFP_ATOMIC);
827 icmp_packet = (struct icmp_packet_rr *)skb->data;
828 ethhdr = (struct ethhdr *)skb_mac_header(skb);
832 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
833 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
834 icmp_packet->msg_type = ECHO_REPLY;
835 icmp_packet->ttl = TTL;
837 send_skb_packet(skb, batman_if, dstaddr);
838 ret = NET_RX_SUCCESS;
841 spin_unlock_irqrestore(&orig_hash_lock, flags);
846 static int recv_icmp_ttl_exceeded(struct sk_buff *skb, size_t icmp_len)
848 struct orig_node *orig_node;
849 struct icmp_packet *icmp_packet;
850 struct ethhdr *ethhdr;
851 struct sk_buff *skb_old;
852 struct batman_if *batman_if;
855 uint8_t dstaddr[ETH_ALEN];
857 icmp_packet = (struct icmp_packet *)skb->data;
858 ethhdr = (struct ethhdr *)skb_mac_header(skb);
860 /* send TTL exceeded if packet is an echo request (traceroute) */
861 if (icmp_packet->msg_type != ECHO_REQUEST) {
862 pr_warning("Warning - can't forward icmp packet from %pM to "
863 "%pM: ttl exceeded\n", icmp_packet->orig,
868 /* get routing information */
869 spin_lock_irqsave(&orig_hash_lock, flags);
870 orig_node = ((struct orig_node *)
871 hash_find(orig_hash, icmp_packet->orig));
874 if ((orig_node != NULL) &&
875 (orig_node->router != NULL)) {
877 /* don't lock while sending the packets ... we therefore
878 * copy the required data before sending */
879 batman_if = orig_node->router->if_incoming;
880 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
881 spin_unlock_irqrestore(&orig_hash_lock, flags);
883 /* create a copy of the skb, if needed, to modify it. */
884 if (!skb_clone_writable(skb, icmp_len)) {
886 skb = skb_copy(skb, GFP_ATOMIC);
889 icmp_packet = (struct icmp_packet *) skb->data;
890 ethhdr = (struct ethhdr *)skb_mac_header(skb);
894 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
895 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
896 icmp_packet->msg_type = TTL_EXCEEDED;
897 icmp_packet->ttl = TTL;
899 send_skb_packet(skb, batman_if, dstaddr);
900 ret = NET_RX_SUCCESS;
903 spin_unlock_irqrestore(&orig_hash_lock, flags);
909 int recv_icmp_packet(struct sk_buff *skb)
911 struct icmp_packet_rr *icmp_packet;
912 struct ethhdr *ethhdr;
913 struct orig_node *orig_node;
914 struct sk_buff *skb_old;
915 struct batman_if *batman_if;
916 int hdr_size = sizeof(struct icmp_packet);
919 uint8_t dstaddr[ETH_ALEN];
922 * we truncate all incoming icmp packets if they don't match our size
924 if (skb_headlen(skb) >= sizeof(struct icmp_packet_rr))
925 hdr_size = sizeof(struct icmp_packet_rr);
927 /* drop packet if it has not necessary minimum size */
928 if (skb_headlen(skb) < hdr_size)
931 ethhdr = (struct ethhdr *)skb_mac_header(skb);
933 /* packet with unicast indication but broadcast recipient */
934 if (is_bcast(ethhdr->h_dest))
937 /* packet with broadcast sender address */
938 if (is_bcast(ethhdr->h_source))
942 if (!is_my_mac(ethhdr->h_dest))
945 icmp_packet = (struct icmp_packet_rr *)skb->data;
947 /* add record route information if not full */
948 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
949 (icmp_packet->rr_cur < BAT_RR_LEN)) {
950 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
951 ethhdr->h_dest, ETH_ALEN);
952 icmp_packet->rr_cur++;
956 if (is_my_mac(icmp_packet->dst))
957 return recv_my_icmp_packet(skb, hdr_size);
960 if (icmp_packet->ttl < 2)
961 return recv_icmp_ttl_exceeded(skb, hdr_size);
965 /* get routing information */
966 spin_lock_irqsave(&orig_hash_lock, flags);
967 orig_node = ((struct orig_node *)
968 hash_find(orig_hash, icmp_packet->dst));
970 if ((orig_node != NULL) &&
971 (orig_node->router != NULL)) {
973 /* don't lock while sending the packets ... we therefore
974 * copy the required data before sending */
975 batman_if = orig_node->router->if_incoming;
976 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
977 spin_unlock_irqrestore(&orig_hash_lock, flags);
979 /* create a copy of the skb, if needed, to modify it. */
980 if (!skb_clone_writable(skb, hdr_size)) {
982 skb = skb_copy(skb, GFP_ATOMIC);
985 icmp_packet = (struct icmp_packet_rr *)skb->data;
986 ethhdr = (struct ethhdr *)skb_mac_header(skb);
994 send_skb_packet(skb, batman_if, dstaddr);
995 ret = NET_RX_SUCCESS;
998 spin_unlock_irqrestore(&orig_hash_lock, flags);
1003 /* find a suitable router for this originator, and use
1004 * bonding if possible. */
1005 struct neigh_node *find_router(struct orig_node *orig_node,
1006 struct batman_if *recv_if)
1008 /* FIXME: each orig_node->batman_if will be attached to a softif */
1009 struct bat_priv *bat_priv = netdev_priv(soft_device);
1010 struct orig_node *primary_orig_node;
1011 struct orig_node *router_orig;
1012 struct neigh_node *router, *first_candidate, *best_router;
1013 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1014 int bonding_enabled;
1019 if (!orig_node->router)
1022 /* without bonding, the first node should
1023 * always choose the default router. */
1025 bonding_enabled = atomic_read(&bat_priv->bonding_enabled);
1026 if (!bonding_enabled && (recv_if == NULL))
1027 return orig_node->router;
1029 router_orig = orig_node->router->orig_node;
1031 /* if we have something in the primary_addr, we can search
1032 * for a potential bonding candidate. */
1033 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1034 return orig_node->router;
1036 /* find the orig_node which has the primary interface. might
1037 * even be the same as our router_orig in many cases */
1039 if (memcmp(router_orig->primary_addr,
1040 router_orig->orig, ETH_ALEN) == 0) {
1041 primary_orig_node = router_orig;
1043 primary_orig_node = hash_find(orig_hash,
1044 router_orig->primary_addr);
1045 if (!primary_orig_node)
1046 return orig_node->router;
1049 /* with less than 2 candidates, we can't do any
1050 * bonding and prefer the original router. */
1052 if (primary_orig_node->bond.candidates < 2)
1053 return orig_node->router;
1056 /* all nodes between should choose a candidate which
1057 * is is not on the interface where the packet came
1059 first_candidate = primary_orig_node->bond.selected;
1060 router = first_candidate;
1062 if (bonding_enabled) {
1063 /* in the bonding case, send the packets in a round
1064 * robin fashion over the remaining interfaces. */
1066 /* recv_if == NULL on the first node. */
1067 if (router->if_incoming != recv_if)
1070 router = router->next_bond_candidate;
1071 } while (router != first_candidate);
1073 primary_orig_node->bond.selected = router->next_bond_candidate;
1076 /* if bonding is disabled, use the best of the
1077 * remaining candidates which are not using
1078 * this interface. */
1079 best_router = first_candidate;
1082 /* recv_if == NULL on the first node. */
1083 if ((router->if_incoming != recv_if) &&
1084 (router->tq_avg > best_router->tq_avg))
1085 best_router = router;
1087 router = router->next_bond_candidate;
1088 } while (router != first_candidate);
1090 router = best_router;
1096 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1098 struct unicast_packet *unicast_packet;
1099 struct orig_node *orig_node;
1100 struct neigh_node *router;
1101 struct ethhdr *ethhdr;
1102 struct batman_if *batman_if;
1103 struct sk_buff *skb_old;
1104 uint8_t dstaddr[ETH_ALEN];
1105 int hdr_size = sizeof(struct unicast_packet);
1106 unsigned long flags;
1108 /* drop packet if it has not necessary minimum size */
1109 if (skb_headlen(skb) < hdr_size)
1112 ethhdr = (struct ethhdr *) skb_mac_header(skb);
1114 /* packet with unicast indication but broadcast recipient */
1115 if (is_bcast(ethhdr->h_dest))
1118 /* packet with broadcast sender address */
1119 if (is_bcast(ethhdr->h_source))
1123 if (!is_my_mac(ethhdr->h_dest))
1126 unicast_packet = (struct unicast_packet *) skb->data;
1129 if (is_my_mac(unicast_packet->dest)) {
1130 interface_rx(skb, hdr_size);
1131 return NET_RX_SUCCESS;
1135 if (unicast_packet->ttl < 2) {
1136 pr_warning("Warning - can't forward unicast packet from %pM to "
1137 "%pM: ttl exceeded\n", ethhdr->h_source,
1138 unicast_packet->dest);
1142 /* get routing information */
1143 spin_lock_irqsave(&orig_hash_lock, flags);
1144 orig_node = ((struct orig_node *)
1145 hash_find(orig_hash, unicast_packet->dest));
1147 router = find_router(orig_node, recv_if);
1150 spin_unlock_irqrestore(&orig_hash_lock, flags);
1154 /* don't lock while sending the packets ... we therefore
1155 * copy the required data before sending */
1157 batman_if = router->if_incoming;
1158 memcpy(dstaddr, router->addr, ETH_ALEN);
1160 spin_unlock_irqrestore(&orig_hash_lock, flags);
1162 /* create a copy of the skb, if needed, to modify it. */
1163 if (!skb_clone_writable(skb, sizeof(struct unicast_packet))) {
1165 skb = skb_copy(skb, GFP_ATOMIC);
1168 unicast_packet = (struct unicast_packet *) skb->data;
1169 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1174 unicast_packet->ttl--;
1177 send_skb_packet(skb, batman_if, dstaddr);
1179 return NET_RX_SUCCESS;
1182 int recv_bcast_packet(struct sk_buff *skb)
1184 struct orig_node *orig_node;
1185 struct bcast_packet *bcast_packet;
1186 struct ethhdr *ethhdr;
1187 int hdr_size = sizeof(struct bcast_packet);
1189 unsigned long flags;
1191 /* drop packet if it has not necessary minimum size */
1192 if (skb_headlen(skb) < hdr_size)
1195 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1197 /* packet with broadcast indication but unicast recipient */
1198 if (!is_bcast(ethhdr->h_dest))
1201 /* packet with broadcast sender address */
1202 if (is_bcast(ethhdr->h_source))
1205 /* ignore broadcasts sent by myself */
1206 if (is_my_mac(ethhdr->h_source))
1209 bcast_packet = (struct bcast_packet *)skb->data;
1211 /* ignore broadcasts originated by myself */
1212 if (is_my_mac(bcast_packet->orig))
1215 if (bcast_packet->ttl < 2)
1218 spin_lock_irqsave(&orig_hash_lock, flags);
1219 orig_node = ((struct orig_node *)
1220 hash_find(orig_hash, bcast_packet->orig));
1222 if (orig_node == NULL) {
1223 spin_unlock_irqrestore(&orig_hash_lock, flags);
1227 /* check whether the packet is a duplicate */
1228 if (get_bit_status(orig_node->bcast_bits,
1229 orig_node->last_bcast_seqno,
1230 ntohl(bcast_packet->seqno))) {
1231 spin_unlock_irqrestore(&orig_hash_lock, flags);
1235 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1237 /* check whether the packet is old and the host just restarted. */
1238 if (window_protected(seq_diff, &orig_node->bcast_seqno_reset)) {
1239 spin_unlock_irqrestore(&orig_hash_lock, flags);
1243 /* mark broadcast in flood history, update window position
1245 if (bit_get_packet(orig_node->bcast_bits, seq_diff, 1))
1246 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1248 spin_unlock_irqrestore(&orig_hash_lock, flags);
1249 /* rebroadcast packet */
1250 add_bcast_packet_to_list(skb);
1252 /* broadcast for me */
1253 interface_rx(skb, hdr_size);
1255 return NET_RX_SUCCESS;
1258 int recv_vis_packet(struct sk_buff *skb)
1260 struct vis_packet *vis_packet;
1261 struct ethhdr *ethhdr;
1262 struct bat_priv *bat_priv;
1263 int hdr_size = sizeof(struct vis_packet);
1265 if (skb_headlen(skb) < hdr_size)
1268 vis_packet = (struct vis_packet *) skb->data;
1269 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1272 if (!is_my_mac(ethhdr->h_dest))
1275 /* ignore own packets */
1276 if (is_my_mac(vis_packet->vis_orig))
1279 if (is_my_mac(vis_packet->sender_orig))
1282 /* FIXME: each batman_if will be attached to a softif */
1283 bat_priv = netdev_priv(soft_device);
1285 switch (vis_packet->vis_type) {
1286 case VIS_TYPE_SERVER_SYNC:
1287 /* TODO: handle fragmented skbs properly */
1288 receive_server_sync_packet(bat_priv, vis_packet,
1292 case VIS_TYPE_CLIENT_UPDATE:
1293 /* TODO: handle fragmented skbs properly */
1294 receive_client_update_packet(bat_priv, vis_packet,
1298 default: /* ignore unknown packet */
1302 /* We take a copy of the data in the packet, so we should
1303 always free the skbuf. */