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"
29 #include "translation-table.h"
30 #include "originator.h"
32 #include "ring_buffer.h"
34 #include "aggregation.h"
36 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)
81 if ((orig_node->router != NULL) && (neigh_node == NULL)) {
83 bat_dbg(DBG_ROUTES, "Deleting route towards: %pM\n",
85 hna_global_del_orig(orig_node, "originator timed out");
88 } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
91 "Adding route towards: %pM (via %pM)\n",
92 orig_node->orig, neigh_node->addr);
93 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
97 bat_dbg(DBG_ROUTES, "Changing route towards: %pM (now via %pM - was via %pM)\n", orig_node->orig, neigh_node->addr, orig_node->router->addr);
100 orig_node->router = neigh_node;
104 void update_routes(struct orig_node *orig_node,
105 struct neigh_node *neigh_node,
106 unsigned char *hna_buff, int hna_buff_len)
109 if (orig_node == NULL)
112 if (orig_node->router != neigh_node)
113 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
114 /* may be just HNA changed */
116 update_HNA(orig_node, hna_buff, hna_buff_len);
119 static int isBidirectionalNeigh(struct orig_node *orig_node,
120 struct orig_node *orig_neigh_node,
121 struct batman_packet *batman_packet,
122 struct batman_if *if_incoming)
124 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
125 unsigned char total_count;
127 if (orig_node == orig_neigh_node) {
128 list_for_each_entry(tmp_neigh_node,
129 &orig_node->neigh_list,
132 if (compare_orig(tmp_neigh_node->addr,
133 orig_neigh_node->orig) &&
134 (tmp_neigh_node->if_incoming == if_incoming))
135 neigh_node = tmp_neigh_node;
139 neigh_node = create_neighbor(orig_node,
141 orig_neigh_node->orig,
143 /* create_neighbor failed, return 0 */
147 neigh_node->last_valid = jiffies;
149 /* find packet count of corresponding one hop neighbor */
150 list_for_each_entry(tmp_neigh_node,
151 &orig_neigh_node->neigh_list, 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_neigh_node,
162 orig_neigh_node->orig,
164 /* create_neighbor failed, return 0 */
169 orig_node->last_valid = jiffies;
171 /* pay attention to not get a value bigger than 100 % */
172 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
173 neigh_node->real_packet_count ?
174 neigh_node->real_packet_count :
175 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
177 /* if we have too few packets (too less data) we set tq_own to zero */
178 /* if we receive too few packets it is not considered bidirectional */
179 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
180 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
181 orig_neigh_node->tq_own = 0;
183 /* neigh_node->real_packet_count is never zero as we
184 * only purge old information when getting new
186 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
187 neigh_node->real_packet_count;
190 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
191 * affect the nearly-symmetric links only a little, but
192 * punishes asymmetric links more. This will give a value
193 * between 0 and TQ_MAX_VALUE
195 orig_neigh_node->tq_asym_penalty =
198 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
199 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
200 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
201 (TQ_LOCAL_WINDOW_SIZE *
202 TQ_LOCAL_WINDOW_SIZE *
203 TQ_LOCAL_WINDOW_SIZE);
205 batman_packet->tq = ((batman_packet->tq *
206 orig_neigh_node->tq_own *
207 orig_neigh_node->tq_asym_penalty) /
208 (TQ_MAX_VALUE * TQ_MAX_VALUE));
210 bat_dbg(DBG_BATMAN, "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
211 orig_node->orig, orig_neigh_node->orig, total_count,
212 neigh_node->real_packet_count, orig_neigh_node->tq_own,
213 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
215 /* if link has the minimum required transmission quality
216 * consider it bidirectional */
217 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
223 static void update_orig(struct orig_node *orig_node, struct ethhdr *ethhdr,
224 struct batman_packet *batman_packet,
225 struct batman_if *if_incoming,
226 unsigned char *hna_buff, int hna_buff_len,
229 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
230 int tmp_hna_buff_len;
232 bat_dbg(DBG_BATMAN, "update_originator(): Searching and updating originator entry of received packet\n");
234 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
235 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
236 (tmp_neigh_node->if_incoming == if_incoming)) {
237 neigh_node = tmp_neigh_node;
244 ring_buffer_set(tmp_neigh_node->tq_recv,
245 &tmp_neigh_node->tq_index, 0);
246 tmp_neigh_node->tq_avg =
247 ring_buffer_avg(tmp_neigh_node->tq_recv);
251 struct orig_node *orig_tmp;
253 orig_tmp = get_orig_node(ethhdr->h_source);
257 neigh_node = create_neighbor(orig_node,
259 ethhdr->h_source, if_incoming);
264 "Updating existing last-hop neighbor of originator\n");
266 orig_node->flags = batman_packet->flags;
267 neigh_node->last_valid = jiffies;
269 ring_buffer_set(neigh_node->tq_recv,
270 &neigh_node->tq_index,
272 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
275 orig_node->last_ttl = batman_packet->ttl;
276 neigh_node->last_ttl = batman_packet->ttl;
279 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
280 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
282 /* if this neighbor already is our next hop there is nothing
284 if (orig_node->router == neigh_node)
287 /* if this neighbor does not offer a better TQ we won't consider it */
288 if ((orig_node->router) &&
289 (orig_node->router->tq_avg > neigh_node->tq_avg))
292 /* if the TQ is the same and the link not more symetric we
293 * won't consider it either */
294 if ((orig_node->router) &&
295 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
296 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
297 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
300 update_routes(orig_node, neigh_node, hna_buff, tmp_hna_buff_len);
304 update_routes(orig_node, orig_node->router, hna_buff, tmp_hna_buff_len);
307 /* checks whether the host restarted and is in the protection time.
309 * 0 if the packet is to be accepted
310 * 1 if the packet is to be ignored.
312 static int window_protected(int16_t seq_num_diff,
313 unsigned long *last_reset)
315 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
316 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
317 if (time_after(jiffies, *last_reset +
318 msecs_to_jiffies(RESET_PROTECTION_MS))) {
320 *last_reset = jiffies;
322 "old packet received, start protection\n");
331 /* processes a batman packet for all interfaces, adjusts the sequence number and
332 * finds out whether it is a duplicate.
334 * 1 the packet is a duplicate
335 * 0 the packet has not yet been received
336 * -1 the packet is old and has been received while the seqno window
337 * was protected. Caller should drop it.
339 static char count_real_packets(struct ethhdr *ethhdr,
340 struct batman_packet *batman_packet,
341 struct batman_if *if_incoming)
343 struct orig_node *orig_node;
344 struct neigh_node *tmp_neigh_node;
345 char is_duplicate = 0;
350 orig_node = get_orig_node(batman_packet->orig);
351 if (orig_node == NULL)
354 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
356 /* signalize caller that the packet is to be dropped. */
357 if (window_protected(seq_diff, &orig_node->batman_seqno_reset))
360 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
362 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
363 orig_node->last_real_seqno,
364 batman_packet->seqno);
366 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
367 (tmp_neigh_node->if_incoming == if_incoming))
372 /* if the window moved, set the update flag. */
373 need_update |= bit_get_packet(tmp_neigh_node->real_bits,
376 tmp_neigh_node->real_packet_count =
377 bit_packet_count(tmp_neigh_node->real_bits);
381 bat_dbg(DBG_BATMAN, "updating last_seqno: old %d, new %d\n",
382 orig_node->last_real_seqno, batman_packet->seqno);
383 orig_node->last_real_seqno = batman_packet->seqno;
389 void receive_bat_packet(struct ethhdr *ethhdr,
390 struct batman_packet *batman_packet,
391 unsigned char *hna_buff, int hna_buff_len,
392 struct batman_if *if_incoming)
394 struct batman_if *batman_if;
395 struct orig_node *orig_neigh_node, *orig_node;
396 char has_directlink_flag;
397 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
398 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
400 unsigned short if_incoming_seqno;
402 /* Silently drop when the batman packet is actually not a
405 * This might happen if a packet is padded (e.g. Ethernet has a
406 * minimum frame length of 64 byte) and the aggregation interprets
407 * it as an additional length.
409 * TODO: A more sane solution would be to have a bit in the
410 * batman_packet to detect whether the packet is the last
411 * packet in an aggregation. Here we expect that the padding
412 * is always zero (or not 0x01)
414 if (batman_packet->packet_type != BAT_PACKET)
417 /* could be changed by schedule_own_packet() */
418 if_incoming_seqno = atomic_read(&if_incoming->seqno);
420 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
422 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
423 batman_packet->orig) ? 1 : 0);
425 bat_dbg(DBG_BATMAN, "Received BATMAN packet via NB: %pM, IF: %s [%s] (from OG: %pM, via prev OG: %pM, seqno %d, tq %d, TTL %d, V %d, IDF %d)\n",
426 ethhdr->h_source, if_incoming->dev, if_incoming->addr_str,
427 batman_packet->orig, batman_packet->prev_sender,
428 batman_packet->seqno, batman_packet->tq, batman_packet->ttl,
429 batman_packet->version, has_directlink_flag);
431 list_for_each_entry_rcu(batman_if, &if_list, list) {
432 if (batman_if->if_status != IF_ACTIVE)
435 if (compare_orig(ethhdr->h_source,
436 batman_if->net_dev->dev_addr))
439 if (compare_orig(batman_packet->orig,
440 batman_if->net_dev->dev_addr))
443 if (compare_orig(batman_packet->prev_sender,
444 batman_if->net_dev->dev_addr))
447 if (compare_orig(ethhdr->h_source, broadcastAddr))
451 if (batman_packet->version != COMPAT_VERSION) {
453 "Drop packet: incompatible batman version (%i)\n",
454 batman_packet->version);
460 "Drop packet: received my own broadcast (sender: %pM)\n",
466 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n", ethhdr->h_source);
474 orig_neigh_node = get_orig_node(ethhdr->h_source);
476 if (!orig_neigh_node)
479 /* neighbor has to indicate direct link and it has to
480 * come via the corresponding interface */
481 /* if received seqno equals last send seqno save new
482 * seqno for bidirectional check */
483 if (has_directlink_flag &&
484 compare_orig(if_incoming->net_dev->dev_addr,
485 batman_packet->orig) &&
486 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
487 offset = if_incoming->if_num * NUM_WORDS;
488 word = &(orig_neigh_node->bcast_own[offset]);
490 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
491 bit_packet_count(word);
494 bat_dbg(DBG_BATMAN, "Drop packet: originator packet from myself (via neighbor)\n");
499 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n", ethhdr->h_source);
503 orig_node = get_orig_node(batman_packet->orig);
504 if (orig_node == NULL)
507 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
509 if (is_duplicate == -1) {
510 bat_dbg(DBG_BATMAN, "Drop packet: packet within seqno protection time (sender: %pM)\n", ethhdr->h_source);
514 if (batman_packet->tq == 0) {
515 bat_dbg(DBG_BATMAN, "Drop packet: originator packet with tq equal 0\n");
519 /* avoid temporary routing loops */
520 if ((orig_node->router) &&
521 (orig_node->router->orig_node->router) &&
522 (compare_orig(orig_node->router->addr,
523 batman_packet->prev_sender)) &&
524 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
525 (compare_orig(orig_node->router->addr,
526 orig_node->router->orig_node->router->addr))) {
527 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n", ethhdr->h_source);
531 /* if sender is a direct neighbor the sender mac equals
533 orig_neigh_node = (is_single_hop_neigh ?
534 orig_node : get_orig_node(ethhdr->h_source));
535 if (orig_neigh_node == NULL)
538 /* drop packet if sender is not a direct neighbor and if we
539 * don't route towards it */
540 if (!is_single_hop_neigh &&
541 (orig_neigh_node->router == NULL)) {
542 bat_dbg(DBG_BATMAN, "Drop packet: OGM via unknown neighbor!\n");
546 is_bidirectional = isBidirectionalNeigh(orig_node, orig_neigh_node,
547 batman_packet, if_incoming);
549 /* update ranking if it is not a duplicate or has the same
550 * seqno and similar ttl as the non-duplicate */
551 if (is_bidirectional &&
553 ((orig_node->last_real_seqno == batman_packet->seqno) &&
554 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
555 update_orig(orig_node, ethhdr, batman_packet,
556 if_incoming, hna_buff, hna_buff_len, is_duplicate);
558 /* is single hop (direct) neighbor */
559 if (is_single_hop_neigh) {
561 /* mark direct link on incoming interface */
562 schedule_forward_packet(orig_node, ethhdr, batman_packet,
563 1, hna_buff_len, if_incoming);
565 bat_dbg(DBG_BATMAN, "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
569 /* multihop originator */
570 if (!is_bidirectional) {
572 "Drop packet: not received via bidirectional link\n");
577 bat_dbg(DBG_BATMAN, "Drop packet: duplicate packet received\n");
582 "Forwarding packet: rebroadcast originator packet\n");
583 schedule_forward_packet(orig_node, ethhdr, batman_packet,
584 0, hna_buff_len, if_incoming);
587 int recv_bat_packet(struct sk_buff *skb,
588 struct batman_if *batman_if)
590 struct ethhdr *ethhdr;
592 struct sk_buff *skb_old;
594 /* drop packet if it has not necessary minimum size */
595 if (skb_headlen(skb) < sizeof(struct batman_packet))
598 ethhdr = (struct ethhdr *)skb_mac_header(skb);
600 /* packet with broadcast indication but unicast recipient */
601 if (!is_bcast(ethhdr->h_dest))
604 /* packet with broadcast sender address */
605 if (is_bcast(ethhdr->h_source))
608 /* TODO: we use headlen instead of "length", because
609 * only this data is paged in. */
611 /* create a copy of the skb, if needed, to modify it. */
612 if (!skb_clone_writable(skb, skb_headlen(skb))) {
614 skb = skb_copy(skb, GFP_ATOMIC);
617 ethhdr = (struct ethhdr *)skb_mac_header(skb);
621 spin_lock_irqsave(&orig_hash_lock, flags);
622 receive_aggr_bat_packet(ethhdr,
626 spin_unlock_irqrestore(&orig_hash_lock, flags);
629 return NET_RX_SUCCESS;
632 static int recv_my_icmp_packet(struct sk_buff *skb)
634 struct orig_node *orig_node;
635 struct icmp_packet *icmp_packet;
636 struct ethhdr *ethhdr;
637 struct sk_buff *skb_old;
638 struct batman_if *batman_if;
641 uint8_t dstaddr[ETH_ALEN];
643 icmp_packet = (struct icmp_packet *)skb->data;
644 ethhdr = (struct ethhdr *)skb_mac_header(skb);
646 /* add data to device queue */
647 if (icmp_packet->msg_type != ECHO_REQUEST) {
648 bat_device_receive_packet(icmp_packet);
652 /* answer echo request (ping) */
653 /* get routing information */
654 spin_lock_irqsave(&orig_hash_lock, flags);
655 orig_node = ((struct orig_node *)hash_find(orig_hash,
659 if ((orig_node != NULL) &&
660 (orig_node->router != NULL)) {
662 /* don't lock while sending the packets ... we therefore
663 * copy the required data before sending */
664 batman_if = orig_node->router->if_incoming;
665 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
666 spin_unlock_irqrestore(&orig_hash_lock, flags);
668 /* create a copy of the skb, if needed, to modify it. */
670 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
672 skb = skb_copy(skb, GFP_ATOMIC);
676 icmp_packet = (struct icmp_packet *)skb->data;
677 ethhdr = (struct ethhdr *)skb_mac_header(skb);
681 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
682 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
683 icmp_packet->msg_type = ECHO_REPLY;
684 icmp_packet->ttl = TTL;
686 send_skb_packet(skb, batman_if, dstaddr);
687 ret = NET_RX_SUCCESS;
690 spin_unlock_irqrestore(&orig_hash_lock, flags);
695 static int recv_icmp_ttl_exceeded(struct sk_buff *skb)
697 struct orig_node *orig_node;
698 struct icmp_packet *icmp_packet;
699 struct ethhdr *ethhdr;
700 struct sk_buff *skb_old;
701 struct batman_if *batman_if;
704 uint8_t dstaddr[ETH_ALEN];
706 icmp_packet = (struct icmp_packet *)skb->data;
707 ethhdr = (struct ethhdr *)skb_mac_header(skb);
709 /* send TTL exceeded if packet is an echo request (traceroute) */
710 if (icmp_packet->msg_type != ECHO_REQUEST) {
711 printk(KERN_WARNING "batman-adv:Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
712 icmp_packet->orig, icmp_packet->dst);
716 /* get routing information */
717 spin_lock_irqsave(&orig_hash_lock, flags);
718 orig_node = ((struct orig_node *)
719 hash_find(orig_hash, icmp_packet->orig));
722 if ((orig_node != NULL) &&
723 (orig_node->router != NULL)) {
725 /* don't lock while sending the packets ... we therefore
726 * copy the required data before sending */
727 batman_if = orig_node->router->if_incoming;
728 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
729 spin_unlock_irqrestore(&orig_hash_lock, flags);
731 /* create a copy of the skb, if needed, to modify it. */
732 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
734 skb = skb_copy(skb, GFP_ATOMIC);
737 icmp_packet = (struct icmp_packet *) skb->data;
738 ethhdr = (struct ethhdr *)skb_mac_header(skb);
742 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
743 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
744 icmp_packet->msg_type = TTL_EXCEEDED;
745 icmp_packet->ttl = TTL;
747 send_skb_packet(skb, batman_if, dstaddr);
748 ret = NET_RX_SUCCESS;
751 spin_unlock_irqrestore(&orig_hash_lock, flags);
757 int recv_icmp_packet(struct sk_buff *skb)
759 struct icmp_packet *icmp_packet;
760 struct ethhdr *ethhdr;
761 struct orig_node *orig_node;
762 struct sk_buff *skb_old;
763 struct batman_if *batman_if;
764 int hdr_size = sizeof(struct icmp_packet);
767 uint8_t dstaddr[ETH_ALEN];
769 /* drop packet if it has not necessary minimum size */
770 if (skb_headlen(skb) < hdr_size)
773 ethhdr = (struct ethhdr *)skb_mac_header(skb);
775 /* packet with unicast indication but broadcast recipient */
776 if (is_bcast(ethhdr->h_dest))
779 /* packet with broadcast sender address */
780 if (is_bcast(ethhdr->h_source))
784 if (!is_my_mac(ethhdr->h_dest))
787 icmp_packet = (struct icmp_packet *)skb->data;
790 if (is_my_mac(icmp_packet->dst))
791 return recv_my_icmp_packet(skb);
794 if (icmp_packet->ttl < 2)
795 return recv_icmp_ttl_exceeded(skb);
799 /* get routing information */
800 spin_lock_irqsave(&orig_hash_lock, flags);
801 orig_node = ((struct orig_node *)
802 hash_find(orig_hash, icmp_packet->dst));
804 if ((orig_node != NULL) &&
805 (orig_node->router != NULL)) {
807 /* don't lock while sending the packets ... we therefore
808 * copy the required data before sending */
809 batman_if = orig_node->router->if_incoming;
810 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
811 spin_unlock_irqrestore(&orig_hash_lock, flags);
813 /* create a copy of the skb, if needed, to modify it. */
814 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
816 skb = skb_copy(skb, GFP_ATOMIC);
819 icmp_packet = (struct icmp_packet *)skb->data;
820 ethhdr = (struct ethhdr *)skb_mac_header(skb);
828 send_skb_packet(skb, batman_if, dstaddr);
829 ret = NET_RX_SUCCESS;
832 spin_unlock_irqrestore(&orig_hash_lock, flags);
837 int recv_unicast_packet(struct sk_buff *skb)
839 struct unicast_packet *unicast_packet;
840 struct orig_node *orig_node;
841 struct ethhdr *ethhdr;
842 struct batman_if *batman_if;
843 struct sk_buff *skb_old;
844 uint8_t dstaddr[ETH_ALEN];
845 int hdr_size = sizeof(struct unicast_packet);
849 /* drop packet if it has not necessary minimum size */
850 if (skb_headlen(skb) < hdr_size)
853 ethhdr = (struct ethhdr *) skb_mac_header(skb);
855 /* packet with unicast indication but broadcast recipient */
856 if (is_bcast(ethhdr->h_dest))
859 /* packet with broadcast sender address */
860 if (is_bcast(ethhdr->h_source))
864 if (!is_my_mac(ethhdr->h_dest))
867 unicast_packet = (struct unicast_packet *) skb->data;
870 if (is_my_mac(unicast_packet->dest)) {
871 interface_rx(skb, hdr_size);
872 return NET_RX_SUCCESS;
876 if (unicast_packet->ttl < 2) {
877 printk(KERN_WARNING "batman-adv:Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
878 ethhdr->h_source, unicast_packet->dest);
883 /* get routing information */
884 spin_lock_irqsave(&orig_hash_lock, flags);
885 orig_node = ((struct orig_node *)
886 hash_find(orig_hash, unicast_packet->dest));
888 if ((orig_node != NULL) &&
889 (orig_node->router != NULL)) {
891 /* don't lock while sending the packets ... we therefore
892 * copy the required data before sending */
893 batman_if = orig_node->router->if_incoming;
894 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
895 spin_unlock_irqrestore(&orig_hash_lock, flags);
897 /* create a copy of the skb, if needed, to modify it. */
898 if (!skb_clone_writable(skb, sizeof(struct unicast_packet))) {
900 skb = skb_copy(skb, GFP_ATOMIC);
903 unicast_packet = (struct unicast_packet *)skb->data;
904 ethhdr = (struct ethhdr *)skb_mac_header(skb);
908 unicast_packet->ttl--;
911 send_skb_packet(skb, batman_if, dstaddr);
912 ret = NET_RX_SUCCESS;
915 spin_unlock_irqrestore(&orig_hash_lock, flags);
920 int recv_bcast_packet(struct sk_buff *skb)
922 struct orig_node *orig_node;
923 struct bcast_packet *bcast_packet;
924 struct ethhdr *ethhdr;
925 int hdr_size = sizeof(struct bcast_packet);
929 /* drop packet if it has not necessary minimum size */
930 if (skb_headlen(skb) < hdr_size)
933 ethhdr = (struct ethhdr *)skb_mac_header(skb);
935 /* packet with broadcast indication but unicast recipient */
936 if (!is_bcast(ethhdr->h_dest))
939 /* packet with broadcast sender address */
940 if (is_bcast(ethhdr->h_source))
943 /* ignore broadcasts sent by myself */
944 if (is_my_mac(ethhdr->h_source))
947 bcast_packet = (struct bcast_packet *)skb->data;
949 /* ignore broadcasts originated by myself */
950 if (is_my_mac(bcast_packet->orig))
953 spin_lock_irqsave(&orig_hash_lock, flags);
954 orig_node = ((struct orig_node *)
955 hash_find(orig_hash, bcast_packet->orig));
957 if (orig_node == NULL) {
958 spin_unlock_irqrestore(&orig_hash_lock, flags);
962 /* check whether the packet is a duplicate */
963 if (get_bit_status(orig_node->bcast_bits,
964 orig_node->last_bcast_seqno,
965 ntohs(bcast_packet->seqno))) {
966 spin_unlock_irqrestore(&orig_hash_lock, flags);
970 seq_diff = ntohs(bcast_packet->seqno) - orig_node->last_bcast_seqno;
972 /* check whether the packet is old and the host just restarted. */
973 if (window_protected(seq_diff, &orig_node->bcast_seqno_reset)) {
974 spin_unlock_irqrestore(&orig_hash_lock, flags);
978 /* mark broadcast in flood history, update window position
980 if (bit_get_packet(orig_node->bcast_bits, seq_diff, 1))
981 orig_node->last_bcast_seqno = ntohs(bcast_packet->seqno);
983 spin_unlock_irqrestore(&orig_hash_lock, flags);
984 /* rebroadcast packet */
985 add_bcast_packet_to_list(skb);
987 /* broadcast for me */
988 interface_rx(skb, hdr_size);
990 return NET_RX_SUCCESS;
993 int recv_vis_packet(struct sk_buff *skb)
995 struct vis_packet *vis_packet;
996 struct ethhdr *ethhdr;
997 struct bat_priv *bat_priv;
998 int hdr_size = sizeof(struct vis_packet);
1000 if (skb_headlen(skb) < hdr_size)
1003 vis_packet = (struct vis_packet *) skb->data;
1004 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1007 if (!is_my_mac(ethhdr->h_dest))
1010 /* ignore own packets */
1011 if (is_my_mac(vis_packet->vis_orig))
1014 if (is_my_mac(vis_packet->sender_orig))
1017 /* FIXME: each batman_if will be attached to a softif */
1018 bat_priv = netdev_priv(soft_device);
1020 switch (vis_packet->vis_type) {
1021 case VIS_TYPE_SERVER_SYNC:
1022 /* TODO: handle fragmented skbs properly */
1023 receive_server_sync_packet(bat_priv, vis_packet,
1027 case VIS_TYPE_CLIENT_UPDATE:
1028 /* TODO: handle fragmented skbs properly */
1029 receive_client_update_packet(bat_priv, vis_packet,
1033 default: /* ignore unknown packet */
1037 /* We take a copy of the data in the packet, so we should
1038 always free the skbuf. */