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 static char count_real_packets(struct ethhdr *ethhdr,
308 struct batman_packet *batman_packet,
309 struct batman_if *if_incoming)
311 struct orig_node *orig_node;
312 struct neigh_node *tmp_neigh_node;
313 char is_duplicate = 0;
316 orig_node = get_orig_node(batman_packet->orig);
317 if (orig_node == NULL)
320 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
324 get_bit_status(tmp_neigh_node->real_bits,
325 orig_node->last_real_seqno,
326 batman_packet->seqno);
327 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
328 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
329 (tmp_neigh_node->if_incoming == if_incoming))
330 bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 1);
332 bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 0);
334 tmp_neigh_node->real_packet_count =
335 bit_packet_count(tmp_neigh_node->real_bits);
339 bat_dbg(DBG_BATMAN, "updating last_seqno: old %d, new %d\n",
340 orig_node->last_real_seqno, batman_packet->seqno);
341 orig_node->last_real_seqno = batman_packet->seqno;
347 void receive_bat_packet(struct ethhdr *ethhdr,
348 struct batman_packet *batman_packet,
349 unsigned char *hna_buff, int hna_buff_len,
350 struct batman_if *if_incoming)
352 struct batman_if *batman_if;
353 struct orig_node *orig_neigh_node, *orig_node;
354 char has_directlink_flag;
355 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
356 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
358 unsigned short if_incoming_seqno;
360 /* Silently drop when the batman packet is actually not a
363 * This might happen if a packet is padded (e.g. Ethernet has a
364 * minimum frame length of 64 byte) and the aggregation interprets
365 * it as an additional length.
367 * TODO: A more sane solution would be to have a bit in the
368 * batman_packet to detect whether the packet is the last
369 * packet in an aggregation. Here we expect that the padding
370 * is always zero (or not 0x01)
372 if (batman_packet->packet_type != BAT_PACKET)
375 /* could be changed by schedule_own_packet() */
376 if_incoming_seqno = atomic_read(&if_incoming->seqno);
378 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
380 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
381 batman_packet->orig) ? 1 : 0);
383 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",
384 ethhdr->h_source, if_incoming->dev, if_incoming->addr_str,
385 batman_packet->orig, batman_packet->prev_sender,
386 batman_packet->seqno, batman_packet->tq, batman_packet->ttl,
387 batman_packet->version, has_directlink_flag);
389 list_for_each_entry_rcu(batman_if, &if_list, list) {
390 if (batman_if->if_status != IF_ACTIVE)
393 if (compare_orig(ethhdr->h_source,
394 batman_if->net_dev->dev_addr))
397 if (compare_orig(batman_packet->orig,
398 batman_if->net_dev->dev_addr))
401 if (compare_orig(batman_packet->prev_sender,
402 batman_if->net_dev->dev_addr))
405 if (compare_orig(ethhdr->h_source, broadcastAddr))
409 if (batman_packet->version != COMPAT_VERSION) {
411 "Drop packet: incompatible batman version (%i)\n",
412 batman_packet->version);
418 "Drop packet: received my own broadcast (sender: %pM)\n",
424 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n", ethhdr->h_source);
432 orig_neigh_node = get_orig_node(ethhdr->h_source);
434 if (!orig_neigh_node)
437 /* neighbor has to indicate direct link and it has to
438 * come via the corresponding interface */
439 /* if received seqno equals last send seqno save new
440 * seqno for bidirectional check */
441 if (has_directlink_flag &&
442 compare_orig(if_incoming->net_dev->dev_addr,
443 batman_packet->orig) &&
444 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
445 offset = if_incoming->if_num * NUM_WORDS;
446 word = &(orig_neigh_node->bcast_own[offset]);
448 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
449 bit_packet_count(word);
452 bat_dbg(DBG_BATMAN, "Drop packet: originator packet from myself (via neighbor)\n");
456 if (batman_packet->tq == 0) {
457 count_real_packets(ethhdr, batman_packet, if_incoming);
459 bat_dbg(DBG_BATMAN, "Drop packet: originator packet with tq equal 0\n");
464 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n", ethhdr->h_source);
468 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
470 orig_node = get_orig_node(batman_packet->orig);
471 if (orig_node == NULL)
474 /* avoid temporary routing loops */
475 if ((orig_node->router) &&
476 (orig_node->router->orig_node->router) &&
477 (compare_orig(orig_node->router->addr,
478 batman_packet->prev_sender)) &&
479 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
480 (compare_orig(orig_node->router->addr,
481 orig_node->router->orig_node->router->addr))) {
482 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n", ethhdr->h_source);
486 /* if sender is a direct neighbor the sender mac equals
488 orig_neigh_node = (is_single_hop_neigh ?
489 orig_node : get_orig_node(ethhdr->h_source));
490 if (orig_neigh_node == NULL)
493 /* drop packet if sender is not a direct neighbor and if we
494 * don't route towards it */
495 if (!is_single_hop_neigh &&
496 (orig_neigh_node->router == NULL)) {
497 bat_dbg(DBG_BATMAN, "Drop packet: OGM via unknown neighbor!\n");
501 is_bidirectional = isBidirectionalNeigh(orig_node, orig_neigh_node,
502 batman_packet, if_incoming);
504 /* update ranking if it is not a duplicate or has the same
505 * seqno and similar ttl as the non-duplicate */
506 if (is_bidirectional &&
508 ((orig_node->last_real_seqno == batman_packet->seqno) &&
509 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
510 update_orig(orig_node, ethhdr, batman_packet,
511 if_incoming, hna_buff, hna_buff_len, is_duplicate);
513 /* is single hop (direct) neighbor */
514 if (is_single_hop_neigh) {
516 /* mark direct link on incoming interface */
517 schedule_forward_packet(orig_node, ethhdr, batman_packet,
518 1, hna_buff_len, if_incoming);
520 bat_dbg(DBG_BATMAN, "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
524 /* multihop originator */
525 if (!is_bidirectional) {
527 "Drop packet: not received via bidirectional link\n");
532 bat_dbg(DBG_BATMAN, "Drop packet: duplicate packet received\n");
537 "Forwarding packet: rebroadcast originator packet\n");
538 schedule_forward_packet(orig_node, ethhdr, batman_packet,
539 0, hna_buff_len, if_incoming);
542 int recv_bat_packet(struct sk_buff *skb,
543 struct batman_if *batman_if)
545 struct ethhdr *ethhdr;
547 struct sk_buff *skb_old;
549 /* drop packet if it has not necessary minimum size */
550 if (skb_headlen(skb) < sizeof(struct batman_packet))
553 ethhdr = (struct ethhdr *)skb_mac_header(skb);
555 /* packet with broadcast indication but unicast recipient */
556 if (!is_bcast(ethhdr->h_dest))
559 /* packet with broadcast sender address */
560 if (is_bcast(ethhdr->h_source))
563 /* TODO: we use headlen instead of "length", because
564 * only this data is paged in. */
566 /* create a copy of the skb, if needed, to modify it. */
567 if (!skb_clone_writable(skb, skb_headlen(skb))) {
569 skb = skb_copy(skb, GFP_ATOMIC);
575 spin_lock_irqsave(&orig_hash_lock, flags);
576 receive_aggr_bat_packet(ethhdr,
580 spin_unlock_irqrestore(&orig_hash_lock, flags);
583 return NET_RX_SUCCESS;
586 static int recv_my_icmp_packet(struct sk_buff *skb)
588 struct orig_node *orig_node;
589 struct icmp_packet *icmp_packet;
590 struct ethhdr *ethhdr;
591 struct sk_buff *skb_old;
592 struct batman_if *batman_if;
595 uint8_t dstaddr[ETH_ALEN];
597 icmp_packet = (struct icmp_packet *) skb->data;
598 ethhdr = (struct ethhdr *) skb_mac_header(skb);
600 /* add data to device queue */
601 if (icmp_packet->msg_type != ECHO_REQUEST) {
602 bat_device_receive_packet(icmp_packet);
606 /* answer echo request (ping) */
607 /* get routing information */
608 spin_lock_irqsave(&orig_hash_lock, flags);
609 orig_node = ((struct orig_node *)hash_find(orig_hash,
613 if ((orig_node != NULL) &&
614 (orig_node->router != NULL)) {
616 /* don't lock while sending the packets ... we therefore
617 * copy the required data before sending */
618 batman_if = orig_node->router->if_incoming;
619 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
620 spin_unlock_irqrestore(&orig_hash_lock, flags);
622 /* create a copy of the skb, if needed, to modify it. */
624 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
626 skb = skb_copy(skb, GFP_ATOMIC);
629 icmp_packet = (struct icmp_packet *) skb->data;
633 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
634 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
635 icmp_packet->msg_type = ECHO_REPLY;
636 icmp_packet->ttl = TTL;
638 send_skb_packet(skb, batman_if, dstaddr);
639 ret = NET_RX_SUCCESS;
642 spin_unlock_irqrestore(&orig_hash_lock, flags);
647 static int recv_icmp_ttl_exceeded(struct sk_buff *skb)
649 struct orig_node *orig_node;
650 struct icmp_packet *icmp_packet;
651 struct ethhdr *ethhdr;
652 struct sk_buff *skb_old;
653 struct batman_if *batman_if;
656 uint8_t dstaddr[ETH_ALEN];
658 icmp_packet = (struct icmp_packet *)skb->data;
659 ethhdr = (struct ethhdr *)skb_mac_header(skb);
661 /* send TTL exceeded if packet is an echo request (traceroute) */
662 if (icmp_packet->msg_type != ECHO_REQUEST) {
663 printk(KERN_WARNING "batman-adv:Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
664 icmp_packet->orig, icmp_packet->dst);
668 /* get routing information */
669 spin_lock_irqsave(&orig_hash_lock, flags);
670 orig_node = ((struct orig_node *)
671 hash_find(orig_hash, icmp_packet->orig));
674 if ((orig_node != NULL) &&
675 (orig_node->router != NULL)) {
677 /* don't lock while sending the packets ... we therefore
678 * copy the required data before sending */
679 batman_if = orig_node->router->if_incoming;
680 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
681 spin_unlock_irqrestore(&orig_hash_lock, flags);
683 /* create a copy of the skb, if needed, to modify it. */
684 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
686 skb = skb_copy(skb, GFP_ATOMIC);
689 icmp_packet = (struct icmp_packet *) skb->data;
693 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
694 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
695 icmp_packet->msg_type = TTL_EXCEEDED;
696 icmp_packet->ttl = TTL;
698 send_skb_packet(skb, batman_if, dstaddr);
699 ret = NET_RX_SUCCESS;
702 spin_unlock_irqrestore(&orig_hash_lock, flags);
708 int recv_icmp_packet(struct sk_buff *skb)
710 struct icmp_packet *icmp_packet;
711 struct ethhdr *ethhdr;
712 struct orig_node *orig_node;
713 struct sk_buff *skb_old;
714 struct batman_if *batman_if;
715 int hdr_size = sizeof(struct icmp_packet);
718 uint8_t dstaddr[ETH_ALEN];
720 /* drop packet if it has not necessary minimum size */
721 if (skb_headlen(skb) < hdr_size)
724 ethhdr = (struct ethhdr *)skb_mac_header(skb);
726 /* packet with unicast indication but broadcast recipient */
727 if (is_bcast(ethhdr->h_dest))
730 /* packet with broadcast sender address */
731 if (is_bcast(ethhdr->h_source))
735 if (!is_my_mac(ethhdr->h_dest))
738 icmp_packet = (struct icmp_packet *) skb->data;
741 if (is_my_mac(icmp_packet->dst))
742 return recv_my_icmp_packet(skb);
745 if (icmp_packet->ttl < 2)
746 return recv_icmp_ttl_exceeded(skb);
750 /* get routing information */
751 spin_lock_irqsave(&orig_hash_lock, flags);
752 orig_node = ((struct orig_node *)
753 hash_find(orig_hash, icmp_packet->dst));
755 if ((orig_node != 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->router->if_incoming;
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->router != NULL)) {
841 /* don't lock while sending the packets ... we therefore
842 * copy the required data before sending */
843 batman_if = orig_node->router->if_incoming;
844 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
845 spin_unlock_irqrestore(&orig_hash_lock, flags);
847 /* create a copy of the skb, if needed, to modify it. */
848 if (!skb_clone_writable(skb, sizeof(struct unicast_packet))) {
850 skb = skb_copy(skb, GFP_ATOMIC);
853 unicast_packet = (struct unicast_packet *) skb->data;
857 unicast_packet->ttl--;
860 send_skb_packet(skb, batman_if, dstaddr);
861 ret = NET_RX_SUCCESS;
864 spin_unlock_irqrestore(&orig_hash_lock, flags);
870 int recv_bcast_packet(struct sk_buff *skb)
872 struct orig_node *orig_node;
873 struct bcast_packet *bcast_packet;
874 struct ethhdr *ethhdr;
875 int hdr_size = sizeof(struct bcast_packet);
878 /* drop packet if it has not necessary minimum size */
879 if (skb_headlen(skb) < hdr_size)
882 ethhdr = (struct ethhdr *)skb_mac_header(skb);
884 /* packet with broadcast indication but unicast recipient */
885 if (!is_bcast(ethhdr->h_dest))
888 /* packet with broadcast sender address */
889 if (is_bcast(ethhdr->h_source))
892 /* ignore broadcasts sent by myself */
893 if (is_my_mac(ethhdr->h_source))
896 bcast_packet = (struct bcast_packet *)skb->data;
898 /* ignore broadcasts originated by myself */
899 if (is_my_mac(bcast_packet->orig))
902 spin_lock_irqsave(&orig_hash_lock, flags);
903 orig_node = ((struct orig_node *)
904 hash_find(orig_hash, bcast_packet->orig));
906 if (orig_node == NULL) {
907 spin_unlock_irqrestore(&orig_hash_lock, flags);
911 /* check flood history */
912 if (get_bit_status(orig_node->bcast_bits,
913 orig_node->last_bcast_seqno,
914 ntohs(bcast_packet->seqno))) {
915 spin_unlock_irqrestore(&orig_hash_lock, flags);
919 /* mark broadcast in flood history */
920 if (bit_get_packet(orig_node->bcast_bits,
921 ntohs(bcast_packet->seqno) -
922 orig_node->last_bcast_seqno, 1))
923 orig_node->last_bcast_seqno = ntohs(bcast_packet->seqno);
925 spin_unlock_irqrestore(&orig_hash_lock, flags);
927 /* rebroadcast packet */
928 add_bcast_packet_to_list(skb);
930 /* broadcast for me */
931 interface_rx(skb, hdr_size);
933 return NET_RX_SUCCESS;
936 int recv_vis_packet(struct sk_buff *skb)
938 struct vis_packet *vis_packet;
939 struct ethhdr *ethhdr;
940 struct bat_priv *bat_priv;
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 /* FIXME: each batman_if will be attached to a softif */
961 bat_priv = netdev_priv(soft_device);
963 switch (vis_packet->vis_type) {
964 case VIS_TYPE_SERVER_SYNC:
965 /* TODO: handle fragmented skbs properly */
966 receive_server_sync_packet(bat_priv, vis_packet,
970 case VIS_TYPE_CLIENT_UPDATE:
971 /* TODO: handle fragmented skbs properly */
972 receive_client_update_packet(bat_priv, vis_packet,
976 default: /* ignore unknown packet */
980 /* We take a copy of the data in the packet, so we should
981 always free the skbuf. */