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 /* FIXME: each batman_if will be attached to a softif */
787 struct bat_priv *bat_priv = netdev_priv(soft_device);
788 struct orig_node *orig_node;
789 struct icmp_packet_rr *icmp_packet;
790 struct ethhdr *ethhdr;
791 struct sk_buff *skb_old;
792 struct batman_if *batman_if;
795 uint8_t dstaddr[ETH_ALEN];
797 icmp_packet = (struct icmp_packet_rr *)skb->data;
798 ethhdr = (struct ethhdr *)skb_mac_header(skb);
800 /* add data to device queue */
801 if (icmp_packet->msg_type != ECHO_REQUEST) {
802 bat_socket_receive_packet(icmp_packet, icmp_len);
806 if (!bat_priv->primary_if)
809 /* answer echo request (ping) */
810 /* get routing information */
811 spin_lock_irqsave(&orig_hash_lock, flags);
812 orig_node = ((struct orig_node *)hash_find(orig_hash,
816 if ((orig_node != NULL) &&
817 (orig_node->router != NULL)) {
819 /* don't lock while sending the packets ... we therefore
820 * copy the required data before sending */
821 batman_if = orig_node->router->if_incoming;
822 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
823 spin_unlock_irqrestore(&orig_hash_lock, flags);
825 /* create a copy of the skb, if needed, to modify it. */
827 if (!skb_clone_writable(skb, icmp_len)) {
829 skb = skb_copy(skb, GFP_ATOMIC);
832 icmp_packet = (struct icmp_packet_rr *)skb->data;
833 ethhdr = (struct ethhdr *)skb_mac_header(skb);
837 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
838 memcpy(icmp_packet->orig,
839 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
840 icmp_packet->msg_type = ECHO_REPLY;
841 icmp_packet->ttl = TTL;
843 send_skb_packet(skb, batman_if, dstaddr);
844 ret = NET_RX_SUCCESS;
847 spin_unlock_irqrestore(&orig_hash_lock, flags);
852 static int recv_icmp_ttl_exceeded(struct sk_buff *skb, size_t icmp_len)
854 /* FIXME: each batman_if will be attached to a softif */
855 struct bat_priv *bat_priv = netdev_priv(soft_device);
856 struct orig_node *orig_node;
857 struct icmp_packet *icmp_packet;
858 struct ethhdr *ethhdr;
859 struct sk_buff *skb_old;
860 struct batman_if *batman_if;
863 uint8_t dstaddr[ETH_ALEN];
865 icmp_packet = (struct icmp_packet *)skb->data;
866 ethhdr = (struct ethhdr *)skb_mac_header(skb);
868 /* send TTL exceeded if packet is an echo request (traceroute) */
869 if (icmp_packet->msg_type != ECHO_REQUEST) {
870 pr_warning("Warning - can't forward icmp packet from %pM to "
871 "%pM: ttl exceeded\n", icmp_packet->orig,
876 if (!bat_priv->primary_if)
879 /* get routing information */
880 spin_lock_irqsave(&orig_hash_lock, flags);
881 orig_node = ((struct orig_node *)
882 hash_find(orig_hash, icmp_packet->orig));
885 if ((orig_node != NULL) &&
886 (orig_node->router != NULL)) {
888 /* don't lock while sending the packets ... we therefore
889 * copy the required data before sending */
890 batman_if = orig_node->router->if_incoming;
891 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
892 spin_unlock_irqrestore(&orig_hash_lock, flags);
894 /* create a copy of the skb, if needed, to modify it. */
895 if (!skb_clone_writable(skb, icmp_len)) {
897 skb = skb_copy(skb, GFP_ATOMIC);
900 icmp_packet = (struct icmp_packet *) skb->data;
901 ethhdr = (struct ethhdr *)skb_mac_header(skb);
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_irqrestore(&orig_hash_lock, flags);
921 int recv_icmp_packet(struct sk_buff *skb)
923 struct icmp_packet_rr *icmp_packet;
924 struct ethhdr *ethhdr;
925 struct orig_node *orig_node;
926 struct sk_buff *skb_old;
927 struct batman_if *batman_if;
928 int hdr_size = sizeof(struct icmp_packet);
931 uint8_t dstaddr[ETH_ALEN];
934 * we truncate all incoming icmp packets if they don't match our size
936 if (skb_headlen(skb) >= sizeof(struct icmp_packet_rr))
937 hdr_size = sizeof(struct icmp_packet_rr);
939 /* drop packet if it has not necessary minimum size */
940 if (skb_headlen(skb) < hdr_size)
943 ethhdr = (struct ethhdr *)skb_mac_header(skb);
945 /* packet with unicast indication but broadcast recipient */
946 if (is_bcast(ethhdr->h_dest))
949 /* packet with broadcast sender address */
950 if (is_bcast(ethhdr->h_source))
954 if (!is_my_mac(ethhdr->h_dest))
957 icmp_packet = (struct icmp_packet_rr *)skb->data;
959 /* add record route information if not full */
960 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
961 (icmp_packet->rr_cur < BAT_RR_LEN)) {
962 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
963 ethhdr->h_dest, ETH_ALEN);
964 icmp_packet->rr_cur++;
968 if (is_my_mac(icmp_packet->dst))
969 return recv_my_icmp_packet(skb, hdr_size);
972 if (icmp_packet->ttl < 2)
973 return recv_icmp_ttl_exceeded(skb, hdr_size);
977 /* get routing information */
978 spin_lock_irqsave(&orig_hash_lock, flags);
979 orig_node = ((struct orig_node *)
980 hash_find(orig_hash, icmp_packet->dst));
982 if ((orig_node != NULL) &&
983 (orig_node->router != NULL)) {
985 /* don't lock while sending the packets ... we therefore
986 * copy the required data before sending */
987 batman_if = orig_node->router->if_incoming;
988 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
989 spin_unlock_irqrestore(&orig_hash_lock, flags);
991 /* create a copy of the skb, if needed, to modify it. */
992 if (!skb_clone_writable(skb, hdr_size)) {
994 skb = skb_copy(skb, GFP_ATOMIC);
997 icmp_packet = (struct icmp_packet_rr *)skb->data;
998 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1006 send_skb_packet(skb, batman_if, dstaddr);
1007 ret = NET_RX_SUCCESS;
1010 spin_unlock_irqrestore(&orig_hash_lock, flags);
1015 /* find a suitable router for this originator, and use
1016 * bonding if possible. */
1017 struct neigh_node *find_router(struct orig_node *orig_node,
1018 struct batman_if *recv_if)
1020 /* FIXME: each orig_node->batman_if will be attached to a softif */
1021 struct bat_priv *bat_priv = netdev_priv(soft_device);
1022 struct orig_node *primary_orig_node;
1023 struct orig_node *router_orig;
1024 struct neigh_node *router, *first_candidate, *best_router;
1025 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1026 int bonding_enabled;
1031 if (!orig_node->router)
1034 /* without bonding, the first node should
1035 * always choose the default router. */
1037 bonding_enabled = atomic_read(&bat_priv->bonding_enabled);
1038 if (!bonding_enabled && (recv_if == NULL))
1039 return orig_node->router;
1041 router_orig = orig_node->router->orig_node;
1043 /* if we have something in the primary_addr, we can search
1044 * for a potential bonding candidate. */
1045 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1046 return orig_node->router;
1048 /* find the orig_node which has the primary interface. might
1049 * even be the same as our router_orig in many cases */
1051 if (memcmp(router_orig->primary_addr,
1052 router_orig->orig, ETH_ALEN) == 0) {
1053 primary_orig_node = router_orig;
1055 primary_orig_node = hash_find(orig_hash,
1056 router_orig->primary_addr);
1057 if (!primary_orig_node)
1058 return orig_node->router;
1061 /* with less than 2 candidates, we can't do any
1062 * bonding and prefer the original router. */
1064 if (primary_orig_node->bond.candidates < 2)
1065 return orig_node->router;
1068 /* all nodes between should choose a candidate which
1069 * is is not on the interface where the packet came
1071 first_candidate = primary_orig_node->bond.selected;
1072 router = first_candidate;
1074 if (bonding_enabled) {
1075 /* in the bonding case, send the packets in a round
1076 * robin fashion over the remaining interfaces. */
1078 /* recv_if == NULL on the first node. */
1079 if (router->if_incoming != recv_if)
1082 router = router->next_bond_candidate;
1083 } while (router != first_candidate);
1085 primary_orig_node->bond.selected = router->next_bond_candidate;
1088 /* if bonding is disabled, use the best of the
1089 * remaining candidates which are not using
1090 * this interface. */
1091 best_router = first_candidate;
1094 /* recv_if == NULL on the first node. */
1095 if ((router->if_incoming != recv_if) &&
1096 (router->tq_avg > best_router->tq_avg))
1097 best_router = router;
1099 router = router->next_bond_candidate;
1100 } while (router != first_candidate);
1102 router = best_router;
1108 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1110 struct unicast_packet *unicast_packet;
1111 struct orig_node *orig_node;
1112 struct neigh_node *router;
1113 struct ethhdr *ethhdr;
1114 struct batman_if *batman_if;
1115 struct sk_buff *skb_old;
1116 uint8_t dstaddr[ETH_ALEN];
1117 int hdr_size = sizeof(struct unicast_packet);
1118 unsigned long flags;
1120 /* drop packet if it has not necessary minimum size */
1121 if (skb_headlen(skb) < hdr_size)
1124 ethhdr = (struct ethhdr *) skb_mac_header(skb);
1126 /* packet with unicast indication but broadcast recipient */
1127 if (is_bcast(ethhdr->h_dest))
1130 /* packet with broadcast sender address */
1131 if (is_bcast(ethhdr->h_source))
1135 if (!is_my_mac(ethhdr->h_dest))
1138 unicast_packet = (struct unicast_packet *) skb->data;
1141 if (is_my_mac(unicast_packet->dest)) {
1142 interface_rx(skb, hdr_size);
1143 return NET_RX_SUCCESS;
1147 if (unicast_packet->ttl < 2) {
1148 pr_warning("Warning - can't forward unicast packet from %pM to "
1149 "%pM: ttl exceeded\n", ethhdr->h_source,
1150 unicast_packet->dest);
1154 /* get routing information */
1155 spin_lock_irqsave(&orig_hash_lock, flags);
1156 orig_node = ((struct orig_node *)
1157 hash_find(orig_hash, unicast_packet->dest));
1159 router = find_router(orig_node, recv_if);
1162 spin_unlock_irqrestore(&orig_hash_lock, flags);
1166 /* don't lock while sending the packets ... we therefore
1167 * copy the required data before sending */
1169 batman_if = router->if_incoming;
1170 memcpy(dstaddr, router->addr, ETH_ALEN);
1172 spin_unlock_irqrestore(&orig_hash_lock, flags);
1174 /* create a copy of the skb, if needed, to modify it. */
1175 if (!skb_clone_writable(skb, sizeof(struct unicast_packet))) {
1177 skb = skb_copy(skb, GFP_ATOMIC);
1180 unicast_packet = (struct unicast_packet *) skb->data;
1181 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1186 unicast_packet->ttl--;
1189 send_skb_packet(skb, batman_if, dstaddr);
1191 return NET_RX_SUCCESS;
1194 int recv_bcast_packet(struct sk_buff *skb)
1196 struct orig_node *orig_node;
1197 struct bcast_packet *bcast_packet;
1198 struct ethhdr *ethhdr;
1199 int hdr_size = sizeof(struct bcast_packet);
1201 unsigned long flags;
1203 /* drop packet if it has not necessary minimum size */
1204 if (skb_headlen(skb) < hdr_size)
1207 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1209 /* packet with broadcast indication but unicast recipient */
1210 if (!is_bcast(ethhdr->h_dest))
1213 /* packet with broadcast sender address */
1214 if (is_bcast(ethhdr->h_source))
1217 /* ignore broadcasts sent by myself */
1218 if (is_my_mac(ethhdr->h_source))
1221 bcast_packet = (struct bcast_packet *)skb->data;
1223 /* ignore broadcasts originated by myself */
1224 if (is_my_mac(bcast_packet->orig))
1227 if (bcast_packet->ttl < 2)
1230 spin_lock_irqsave(&orig_hash_lock, flags);
1231 orig_node = ((struct orig_node *)
1232 hash_find(orig_hash, bcast_packet->orig));
1234 if (orig_node == NULL) {
1235 spin_unlock_irqrestore(&orig_hash_lock, flags);
1239 /* check whether the packet is a duplicate */
1240 if (get_bit_status(orig_node->bcast_bits,
1241 orig_node->last_bcast_seqno,
1242 ntohl(bcast_packet->seqno))) {
1243 spin_unlock_irqrestore(&orig_hash_lock, flags);
1247 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1249 /* check whether the packet is old and the host just restarted. */
1250 if (window_protected(seq_diff, &orig_node->bcast_seqno_reset)) {
1251 spin_unlock_irqrestore(&orig_hash_lock, flags);
1255 /* mark broadcast in flood history, update window position
1257 if (bit_get_packet(orig_node->bcast_bits, seq_diff, 1))
1258 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1260 spin_unlock_irqrestore(&orig_hash_lock, flags);
1261 /* rebroadcast packet */
1262 add_bcast_packet_to_list(skb);
1264 /* broadcast for me */
1265 interface_rx(skb, hdr_size);
1267 return NET_RX_SUCCESS;
1270 int recv_vis_packet(struct sk_buff *skb)
1272 struct vis_packet *vis_packet;
1273 struct ethhdr *ethhdr;
1274 struct bat_priv *bat_priv;
1275 int hdr_size = sizeof(struct vis_packet);
1277 if (skb_headlen(skb) < hdr_size)
1280 vis_packet = (struct vis_packet *) skb->data;
1281 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1284 if (!is_my_mac(ethhdr->h_dest))
1287 /* ignore own packets */
1288 if (is_my_mac(vis_packet->vis_orig))
1291 if (is_my_mac(vis_packet->sender_orig))
1294 /* FIXME: each batman_if will be attached to a softif */
1295 bat_priv = netdev_priv(soft_device);
1297 switch (vis_packet->vis_type) {
1298 case VIS_TYPE_SERVER_SYNC:
1299 /* TODO: handle fragmented skbs properly */
1300 receive_server_sync_packet(bat_priv, vis_packet,
1304 case VIS_TYPE_CLIENT_UPDATE:
1305 /* TODO: handle fragmented skbs properly */
1306 receive_client_update_packet(bat_priv, vis_packet,
1310 default: /* ignore unknown packet */
1314 /* We take a copy of the data in the packet, so we should
1315 always free the skbuf. */