2 * Copyright (C) 2007-2009 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 if (neigh_node != NULL)
101 orig_node->batman_if = neigh_node->if_incoming;
103 orig_node->batman_if = NULL;
105 orig_node->router = neigh_node;
109 void update_routes(struct orig_node *orig_node,
110 struct neigh_node *neigh_node,
111 unsigned char *hna_buff, int hna_buff_len)
114 if (orig_node == NULL)
117 if (orig_node->router != neigh_node)
118 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
119 /* may be just HNA changed */
121 update_HNA(orig_node, hna_buff, hna_buff_len);
124 static int isBidirectionalNeigh(struct orig_node *orig_node,
125 struct orig_node *orig_neigh_node,
126 struct batman_packet *batman_packet,
127 struct batman_if *if_incoming)
129 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
130 unsigned char total_count;
132 if (orig_node == orig_neigh_node) {
133 list_for_each_entry(tmp_neigh_node,
134 &orig_node->neigh_list,
137 if (compare_orig(tmp_neigh_node->addr,
138 orig_neigh_node->orig) &&
139 (tmp_neigh_node->if_incoming == if_incoming))
140 neigh_node = tmp_neigh_node;
144 neigh_node = create_neighbor(orig_node,
146 orig_neigh_node->orig,
148 /* create_neighbor failed, return 0 */
152 neigh_node->last_valid = jiffies;
154 /* find packet count of corresponding one hop neighbor */
155 list_for_each_entry(tmp_neigh_node,
156 &orig_neigh_node->neigh_list, list) {
158 if (compare_orig(tmp_neigh_node->addr,
159 orig_neigh_node->orig) &&
160 (tmp_neigh_node->if_incoming == if_incoming))
161 neigh_node = tmp_neigh_node;
165 neigh_node = create_neighbor(orig_neigh_node,
167 orig_neigh_node->orig,
169 /* create_neighbor failed, return 0 */
174 orig_node->last_valid = jiffies;
176 /* pay attention to not get a value bigger than 100 % */
177 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
178 neigh_node->real_packet_count ?
179 neigh_node->real_packet_count :
180 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
182 /* if we have too few packets (too less data) we set tq_own to zero */
183 /* if we receive too few packets it is not considered bidirectional */
184 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
185 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
186 orig_neigh_node->tq_own = 0;
188 /* neigh_node->real_packet_count is never zero as we
189 * only purge old information when getting new
191 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
192 neigh_node->real_packet_count;
195 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
196 * affect the nearly-symmetric links only a little, but
197 * punishes asymmetric links more. This will give a value
198 * between 0 and TQ_MAX_VALUE
200 orig_neigh_node->tq_asym_penalty =
203 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
204 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
205 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
206 (TQ_LOCAL_WINDOW_SIZE *
207 TQ_LOCAL_WINDOW_SIZE *
208 TQ_LOCAL_WINDOW_SIZE);
210 batman_packet->tq = ((batman_packet->tq *
211 orig_neigh_node->tq_own *
212 orig_neigh_node->tq_asym_penalty) /
213 (TQ_MAX_VALUE * TQ_MAX_VALUE));
215 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",
216 orig_node->orig, orig_neigh_node->orig, total_count,
217 neigh_node->real_packet_count, orig_neigh_node->tq_own,
218 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
220 /* if link has the minimum required transmission quality
221 * consider it bidirectional */
222 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
228 static void update_orig(struct orig_node *orig_node, struct ethhdr *ethhdr,
229 struct batman_packet *batman_packet,
230 struct batman_if *if_incoming,
231 unsigned char *hna_buff, int hna_buff_len,
234 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
235 int tmp_hna_buff_len;
237 bat_dbg(DBG_BATMAN, "update_originator(): Searching and updating originator entry of received packet \n");
239 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
240 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
241 (tmp_neigh_node->if_incoming == if_incoming)) {
242 neigh_node = tmp_neigh_node;
249 ring_buffer_set(tmp_neigh_node->tq_recv,
250 &tmp_neigh_node->tq_index, 0);
251 tmp_neigh_node->tq_avg =
252 ring_buffer_avg(tmp_neigh_node->tq_recv);
256 struct orig_node *orig_tmp;
258 orig_tmp = get_orig_node(ethhdr->h_source);
262 neigh_node = create_neighbor(orig_node,
264 ethhdr->h_source, if_incoming);
269 "Updating existing last-hop neighbor of originator\n");
271 orig_node->flags = batman_packet->flags;
272 neigh_node->last_valid = jiffies;
274 ring_buffer_set(neigh_node->tq_recv,
275 &neigh_node->tq_index,
277 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
280 orig_node->last_ttl = batman_packet->ttl;
281 neigh_node->last_ttl = batman_packet->ttl;
284 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
285 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
287 /* if this neighbor already is our next hop there is nothing
289 if (orig_node->router == neigh_node)
292 /* if this neighbor does not offer a better TQ we won't consider it */
293 if ((orig_node->router) &&
294 (orig_node->router->tq_avg > neigh_node->tq_avg))
297 /* if the TQ is the same and the link not more symetric we
298 * won't consider it either */
299 if ((orig_node->router) &&
300 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
301 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
302 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
305 update_routes(orig_node, neigh_node, hna_buff, tmp_hna_buff_len);
309 update_routes(orig_node, orig_node->router, hna_buff, tmp_hna_buff_len);
312 static char count_real_packets(struct ethhdr *ethhdr,
313 struct batman_packet *batman_packet,
314 struct batman_if *if_incoming)
316 struct orig_node *orig_node;
317 struct neigh_node *tmp_neigh_node;
318 char is_duplicate = 0;
321 orig_node = get_orig_node(batman_packet->orig);
322 if (orig_node == NULL)
325 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
329 get_bit_status(tmp_neigh_node->real_bits,
330 orig_node->last_real_seqno,
331 batman_packet->seqno);
332 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
333 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
334 (tmp_neigh_node->if_incoming == if_incoming))
335 bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 1);
337 bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 0);
339 tmp_neigh_node->real_packet_count =
340 bit_packet_count(tmp_neigh_node->real_bits);
344 bat_dbg(DBG_BATMAN, "updating last_seqno: old %d, new %d \n",
345 orig_node->last_real_seqno, batman_packet->seqno);
346 orig_node->last_real_seqno = batman_packet->seqno;
352 void receive_bat_packet(struct ethhdr *ethhdr,
353 struct batman_packet *batman_packet,
354 unsigned char *hna_buff, int hna_buff_len,
355 struct batman_if *if_incoming)
357 struct batman_if *batman_if;
358 struct orig_node *orig_neigh_node, *orig_node;
359 char has_directlink_flag;
360 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
361 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
363 unsigned short if_incoming_seqno;
365 /* Silently drop when the batman packet is actually not a
368 * This might happen if a packet is padded (e.g. Ethernet has a
369 * minimum frame length of 64 byte) and the aggregation interprets
370 * it as an additional length.
372 * TODO: A more sane solution would be to have a bit in the
373 * batman_packet to detect whether the packet is the last
374 * packet in an aggregation. Here we expect that the padding
375 * is always zero (or not 0x01)
377 if (batman_packet->packet_type != BAT_PACKET)
380 /* could be changed by schedule_own_packet() */
381 if_incoming_seqno = atomic_read(&if_incoming->seqno);
383 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
385 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
386 batman_packet->orig) ? 1 : 0);
388 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",
389 ethhdr->h_source, if_incoming->dev, if_incoming->addr_str,
390 batman_packet->orig, batman_packet->prev_sender,
391 batman_packet->seqno, batman_packet->tq, batman_packet->ttl,
392 batman_packet->version, has_directlink_flag);
394 list_for_each_entry_rcu(batman_if, &if_list, list) {
395 if (batman_if->if_active != IF_ACTIVE)
398 if (compare_orig(ethhdr->h_source,
399 batman_if->net_dev->dev_addr))
402 if (compare_orig(batman_packet->orig,
403 batman_if->net_dev->dev_addr))
406 if (compare_orig(batman_packet->prev_sender,
407 batman_if->net_dev->dev_addr))
410 if (compare_orig(ethhdr->h_source, broadcastAddr))
414 if (batman_packet->version != COMPAT_VERSION) {
416 "Drop packet: incompatible batman version (%i)\n",
417 batman_packet->version);
423 "Drop packet: received my own broadcast (sender: %pM)\n",
429 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all packets with broadcast source addr (sender: %pM) \n", ethhdr->h_source);
437 orig_neigh_node = get_orig_node(ethhdr->h_source);
439 if (!orig_neigh_node)
442 /* neighbor has to indicate direct link and it has to
443 * come via the corresponding interface */
444 /* if received seqno equals last send seqno save new
445 * seqno for bidirectional check */
446 if (has_directlink_flag &&
447 compare_orig(if_incoming->net_dev->dev_addr,
448 batman_packet->orig) &&
449 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
450 offset = if_incoming->if_num * NUM_WORDS;
451 word = &(orig_neigh_node->bcast_own[offset]);
453 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
454 bit_packet_count(word);
457 bat_dbg(DBG_BATMAN, "Drop packet: originator packet from myself (via neighbor) \n");
461 if (batman_packet->tq == 0) {
462 count_real_packets(ethhdr, batman_packet, if_incoming);
464 bat_dbg(DBG_BATMAN, "Drop packet: originator packet with tq equal 0 \n");
469 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast echos (sender: %pM) \n", ethhdr->h_source);
473 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
475 orig_node = get_orig_node(batman_packet->orig);
476 if (orig_node == NULL)
479 /* avoid temporary routing loops */
480 if ((orig_node->router) &&
481 (orig_node->router->orig_node->router) &&
482 (compare_orig(orig_node->router->addr,
483 batman_packet->prev_sender)) &&
484 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
485 (compare_orig(orig_node->router->addr,
486 orig_node->router->orig_node->router->addr))) {
487 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM) \n", ethhdr->h_source);
491 /* if sender is a direct neighbor the sender mac equals
493 orig_neigh_node = (is_single_hop_neigh ?
494 orig_node : get_orig_node(ethhdr->h_source));
495 if (orig_neigh_node == NULL)
498 /* drop packet if sender is not a direct neighbor and if we
499 * don't route towards it */
500 if (!is_single_hop_neigh &&
501 (orig_neigh_node->router == NULL)) {
502 bat_dbg(DBG_BATMAN, "Drop packet: OGM via unknown neighbor!\n");
506 is_bidirectional = isBidirectionalNeigh(orig_node, orig_neigh_node,
507 batman_packet, if_incoming);
509 /* update ranking if it is not a duplicate or has the same
510 * seqno and similar ttl as the non-duplicate */
511 if (is_bidirectional &&
513 ((orig_node->last_real_seqno == batman_packet->seqno) &&
514 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
515 update_orig(orig_node, ethhdr, batman_packet,
516 if_incoming, hna_buff, hna_buff_len, is_duplicate);
518 /* is single hop (direct) neighbor */
519 if (is_single_hop_neigh) {
521 /* mark direct link on incoming interface */
522 schedule_forward_packet(orig_node, ethhdr, batman_packet,
523 1, hna_buff_len, if_incoming);
525 bat_dbg(DBG_BATMAN, "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
529 /* multihop originator */
530 if (!is_bidirectional) {
532 "Drop packet: not received via bidirectional link\n");
537 bat_dbg(DBG_BATMAN, "Drop packet: duplicate packet received\n");
542 "Forwarding packet: rebroadcast originator packet\n");
543 schedule_forward_packet(orig_node, ethhdr, batman_packet,
544 0, hna_buff_len, if_incoming);
547 int recv_bat_packet(struct sk_buff *skb,
548 struct batman_if *batman_if)
550 struct ethhdr *ethhdr;
553 /* drop packet if it has not necessary minimum size */
554 if (skb_headlen(skb) < sizeof(struct batman_packet))
557 ethhdr = (struct ethhdr *)skb_mac_header(skb);
559 /* packet with broadcast indication but unicast recipient */
560 if (!is_bcast(ethhdr->h_dest))
563 /* packet with broadcast sender address */
564 if (is_bcast(ethhdr->h_source))
567 spin_lock_irqsave(&orig_hash_lock, flags);
568 /* TODO: we use headlen instead of "length", because
569 * only this data is paged in. */
570 /* TODO: is another skb_copy needed here? there will be
571 * written on the data, but nobody (?) should further use
573 receive_aggr_bat_packet(ethhdr,
577 spin_unlock_irqrestore(&orig_hash_lock, flags);
580 return NET_RX_SUCCESS;
583 static int recv_my_icmp_packet(struct sk_buff *skb)
585 struct orig_node *orig_node;
586 struct icmp_packet *icmp_packet;
587 struct ethhdr *ethhdr;
588 struct sk_buff *skb_old;
589 struct batman_if *batman_if;
592 uint8_t dstaddr[ETH_ALEN];
594 icmp_packet = (struct icmp_packet *) skb->data;
595 ethhdr = (struct ethhdr *) skb_mac_header(skb);
597 /* add data to device queue */
598 if (icmp_packet->msg_type != ECHO_REQUEST) {
599 bat_device_receive_packet(icmp_packet);
603 /* answer echo request (ping) */
604 /* get routing information */
605 spin_lock_irqsave(&orig_hash_lock, flags);
606 orig_node = ((struct orig_node *)hash_find(orig_hash,
610 if ((orig_node != NULL) &&
611 (orig_node->batman_if != NULL) &&
612 (orig_node->router != NULL)) {
614 /* don't lock while sending the packets ... we therefore
615 * copy the required data before sending */
616 batman_if = orig_node->batman_if;
617 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
618 spin_unlock_irqrestore(&orig_hash_lock, flags);
620 /* create a copy of the skb, if needed, to modify it. */
622 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
624 skb = skb_copy(skb, GFP_ATOMIC);
627 icmp_packet = (struct icmp_packet *) skb->data;
631 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
632 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
633 icmp_packet->msg_type = ECHO_REPLY;
634 icmp_packet->ttl = TTL;
636 send_skb_packet(skb, batman_if, dstaddr);
637 ret = NET_RX_SUCCESS;
640 spin_unlock_irqrestore(&orig_hash_lock, flags);
645 static int recv_icmp_ttl_exceeded(struct sk_buff *skb)
647 struct orig_node *orig_node;
648 struct icmp_packet *icmp_packet;
649 struct ethhdr *ethhdr;
650 struct sk_buff *skb_old;
651 struct batman_if *batman_if;
654 uint8_t dstaddr[ETH_ALEN];
656 icmp_packet = (struct icmp_packet *)skb->data;
657 ethhdr = (struct ethhdr *)skb_mac_header(skb);
659 /* send TTL exceeded if packet is an echo request (traceroute) */
660 if (icmp_packet->msg_type != ECHO_REQUEST) {
661 printk(KERN_WARNING "batman-adv:Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
662 icmp_packet->orig, icmp_packet->dst);
666 /* get routing information */
667 spin_lock_irqsave(&orig_hash_lock, flags);
668 orig_node = ((struct orig_node *)
669 hash_find(orig_hash, icmp_packet->orig));
672 if ((orig_node != NULL) &&
673 (orig_node->batman_if != NULL) &&
674 (orig_node->router != NULL)) {
676 /* don't lock while sending the packets ... we therefore
677 * copy the required data before sending */
678 batman_if = orig_node->batman_if;
679 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
680 spin_unlock_irqrestore(&orig_hash_lock, flags);
682 /* create a copy of the skb, if needed, to modify it. */
683 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
685 skb = skb_copy(skb, GFP_ATOMIC);
688 icmp_packet = (struct icmp_packet *) skb->data;
692 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
693 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
694 icmp_packet->msg_type = TTL_EXCEEDED;
695 icmp_packet->ttl = TTL;
697 send_skb_packet(skb, batman_if, dstaddr);
698 ret = NET_RX_SUCCESS;
701 spin_unlock_irqrestore(&orig_hash_lock, flags);
707 int recv_icmp_packet(struct sk_buff *skb)
709 struct icmp_packet *icmp_packet;
710 struct ethhdr *ethhdr;
711 struct orig_node *orig_node;
712 struct sk_buff *skb_old;
713 struct batman_if *batman_if;
714 int hdr_size = sizeof(struct icmp_packet);
717 uint8_t dstaddr[ETH_ALEN];
719 /* drop packet if it has not necessary minimum size */
720 if (skb_headlen(skb) < hdr_size)
723 ethhdr = (struct ethhdr *)skb_mac_header(skb);
725 /* packet with unicast indication but broadcast recipient */
726 if (is_bcast(ethhdr->h_dest))
729 /* packet with broadcast sender address */
730 if (is_bcast(ethhdr->h_source))
734 if (!is_my_mac(ethhdr->h_dest))
737 icmp_packet = (struct icmp_packet *) skb->data;
740 if (is_my_mac(icmp_packet->dst))
741 return recv_my_icmp_packet(skb);
744 if (icmp_packet->ttl < 2)
745 return recv_icmp_ttl_exceeded(skb);
749 /* get routing information */
750 spin_lock_irqsave(&orig_hash_lock, flags);
751 orig_node = ((struct orig_node *)
752 hash_find(orig_hash, icmp_packet->dst));
754 if ((orig_node != NULL) &&
755 (orig_node->batman_if != NULL) &&
756 (orig_node->router != NULL)) {
758 /* don't lock while sending the packets ... we therefore
759 * copy the required data before sending */
760 batman_if = orig_node->batman_if;
761 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
762 spin_unlock_irqrestore(&orig_hash_lock, flags);
764 /* create a copy of the skb, if needed, to modify it. */
765 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
767 skb = skb_copy(skb, GFP_ATOMIC);
770 icmp_packet = (struct icmp_packet *) skb->data;
778 send_skb_packet(skb, batman_if, dstaddr);
779 ret = NET_RX_SUCCESS;
782 spin_unlock_irqrestore(&orig_hash_lock, flags);
787 int recv_unicast_packet(struct sk_buff *skb)
789 struct unicast_packet *unicast_packet;
790 struct orig_node *orig_node;
791 struct ethhdr *ethhdr;
792 struct batman_if *batman_if;
793 struct sk_buff *skb_old;
794 uint8_t dstaddr[ETH_ALEN];
795 int hdr_size = sizeof(struct unicast_packet);
799 /* drop packet if it has not necessary minimum size */
800 if (skb_headlen(skb) < hdr_size)
803 ethhdr = (struct ethhdr *) skb_mac_header(skb);
805 /* packet with unicast indication but broadcast recipient */
806 if (is_bcast(ethhdr->h_dest))
809 /* packet with broadcast sender address */
810 if (is_bcast(ethhdr->h_source))
814 if (!is_my_mac(ethhdr->h_dest))
817 unicast_packet = (struct unicast_packet *) skb->data;
820 if (is_my_mac(unicast_packet->dest)) {
821 interface_rx(skb, hdr_size);
822 return NET_RX_SUCCESS;
826 if (unicast_packet->ttl < 2) {
827 printk(KERN_WARNING "batman-adv:Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
828 ethhdr->h_source, unicast_packet->dest);
833 /* get routing information */
834 spin_lock_irqsave(&orig_hash_lock, flags);
835 orig_node = ((struct orig_node *)
836 hash_find(orig_hash, unicast_packet->dest));
838 if ((orig_node != NULL) &&
839 (orig_node->batman_if != NULL) &&
840 (orig_node->router != NULL)) {
842 /* don't lock while sending the packets ... we therefore
843 * copy the required data before sending */
844 batman_if = orig_node->batman_if;
845 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
846 spin_unlock_irqrestore(&orig_hash_lock, flags);
848 /* create a copy of the skb, if needed, to modify it. */
849 if (!skb_clone_writable(skb, sizeof(struct unicast_packet))) {
851 skb = skb_copy(skb, GFP_ATOMIC);
854 unicast_packet = (struct unicast_packet *) skb->data;
858 unicast_packet->ttl--;
861 send_skb_packet(skb, batman_if, dstaddr);
862 ret = NET_RX_SUCCESS;
865 spin_unlock_irqrestore(&orig_hash_lock, flags);
871 int recv_bcast_packet(struct sk_buff *skb)
873 struct orig_node *orig_node;
874 struct bcast_packet *bcast_packet;
875 struct ethhdr *ethhdr;
876 int hdr_size = sizeof(struct bcast_packet);
879 /* drop packet if it has not necessary minimum size */
880 if (skb_headlen(skb) < hdr_size)
883 ethhdr = (struct ethhdr *)skb_mac_header(skb);
885 /* packet with broadcast indication but unicast recipient */
886 if (!is_bcast(ethhdr->h_dest))
889 /* packet with broadcast sender address */
890 if (is_bcast(ethhdr->h_source))
893 /* ignore broadcasts sent by myself */
894 if (is_my_mac(ethhdr->h_source))
897 bcast_packet = (struct bcast_packet *) skb->data;
899 /* ignore broadcasts originated by myself */
900 if (is_my_mac(bcast_packet->orig))
903 spin_lock_irqsave(&orig_hash_lock, flags);
904 orig_node = ((struct orig_node *)
905 hash_find(orig_hash, bcast_packet->orig));
907 if (orig_node == NULL) {
908 spin_unlock_irqrestore(&orig_hash_lock, flags);
912 /* check flood history */
913 if (get_bit_status(orig_node->bcast_bits,
914 orig_node->last_bcast_seqno,
915 ntohs(bcast_packet->seqno))) {
916 spin_unlock_irqrestore(&orig_hash_lock, flags);
920 /* mark broadcast in flood history */
921 if (bit_get_packet(orig_node->bcast_bits,
922 ntohs(bcast_packet->seqno) -
923 orig_node->last_bcast_seqno, 1))
924 orig_node->last_bcast_seqno = ntohs(bcast_packet->seqno);
926 spin_unlock_irqrestore(&orig_hash_lock, flags);
928 /* rebroadcast packet */
929 add_bcast_packet_to_list(skb);
931 /* broadcast for me */
932 interface_rx(skb, hdr_size);
934 return NET_RX_SUCCESS;
937 int recv_vis_packet(struct sk_buff *skb)
939 struct vis_packet *vis_packet;
940 struct ethhdr *ethhdr;
941 int hdr_size = sizeof(struct vis_packet);
943 if (skb_headlen(skb) < hdr_size)
946 vis_packet = (struct vis_packet *) skb->data;
947 ethhdr = (struct ethhdr *)skb_mac_header(skb);
950 if (!is_my_mac(ethhdr->h_dest))
953 /* ignore own packets */
954 if (is_my_mac(vis_packet->vis_orig))
957 if (is_my_mac(vis_packet->sender_orig))
960 switch (vis_packet->vis_type) {
961 case VIS_TYPE_SERVER_SYNC:
962 /* TODO: handle fragmented skbs properly */
963 receive_server_sync_packet(vis_packet, skb_headlen(skb));
966 case VIS_TYPE_CLIENT_UPDATE:
967 /* TODO: handle fragmented skbs properly */
968 receive_client_update_packet(vis_packet, skb_headlen(skb));
971 default: /* ignore unknown packet */
975 /* We take a copy of the data in the packet, so we should
976 always free the skbuf. */