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"
37 DECLARE_WAIT_QUEUE_HEAD(thread_wait);
39 static atomic_t data_ready_cond;
41 void slide_own_bcast_window(struct batman_if *batman_if)
43 struct hash_it_t *hashit = NULL;
44 struct orig_node *orig_node;
47 spin_lock(&orig_hash_lock);
49 while (NULL != (hashit = hash_iterate(orig_hash, hashit))) {
50 orig_node = hashit->bucket->data;
51 word = &(orig_node->bcast_own[batman_if->if_num * NUM_WORDS]);
53 bit_get_packet(word, 1, 0);
54 orig_node->bcast_own_sum[batman_if->if_num] =
55 bit_packet_count(word);
58 spin_unlock(&orig_hash_lock);
61 static void update_HNA(struct orig_node *orig_node,
62 unsigned char *hna_buff, int hna_buff_len)
64 if ((hna_buff_len != orig_node->hna_buff_len) ||
65 ((hna_buff_len > 0) &&
66 (orig_node->hna_buff_len > 0) &&
67 (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
69 if (orig_node->hna_buff_len > 0)
70 hna_global_del_orig(orig_node,
71 "originator changed hna");
73 if ((hna_buff_len > 0) && (hna_buff != NULL))
74 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
78 static void update_route(struct orig_node *orig_node,
79 struct neigh_node *neigh_node,
80 unsigned char *hna_buff, int hna_buff_len)
82 char orig_str[ETH_STR_LEN], neigh_str[ETH_STR_LEN];
83 char router_str[ETH_STR_LEN];
85 addr_to_string(orig_str, orig_node->orig);
88 if ((orig_node->router != NULL) && (neigh_node == NULL)) {
90 bat_dbg(DBG_ROUTES, "Deleting route towards: %s\n",
92 hna_global_del_orig(orig_node, "originator timed out");
95 } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
97 addr_to_string(neigh_str, neigh_node->addr);
99 "Adding route towards: %s (via %s)\n",
100 orig_str, neigh_str);
101 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
105 addr_to_string(neigh_str, neigh_node->addr);
106 addr_to_string(router_str, orig_node->router->addr);
107 bat_dbg(DBG_ROUTES, "Changing route towards: %s (now via %s - was via %s)\n", orig_str, neigh_str, router_str);
110 if (neigh_node != NULL)
111 orig_node->batman_if = neigh_node->if_incoming;
113 orig_node->batman_if = NULL;
115 orig_node->router = neigh_node;
119 void update_routes(struct orig_node *orig_node,
120 struct neigh_node *neigh_node,
121 unsigned char *hna_buff, int hna_buff_len)
124 if (orig_node == NULL)
127 if (orig_node->router != neigh_node)
128 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
129 /* may be just HNA changed */
131 update_HNA(orig_node, hna_buff, hna_buff_len);
134 static int isBidirectionalNeigh(struct orig_node *orig_node,
135 struct orig_node *orig_neigh_node,
136 struct batman_packet *batman_packet,
137 struct batman_if *if_incoming)
139 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
140 char orig_str[ETH_STR_LEN], neigh_str[ETH_STR_LEN];
141 unsigned char total_count;
143 addr_to_string(orig_str, orig_node->orig);
144 addr_to_string(neigh_str, orig_neigh_node->orig);
146 if (orig_node == orig_neigh_node) {
147 list_for_each_entry(tmp_neigh_node,
148 &orig_node->neigh_list,
151 if (compare_orig(tmp_neigh_node->addr,
152 orig_neigh_node->orig) &&
153 (tmp_neigh_node->if_incoming == if_incoming))
154 neigh_node = tmp_neigh_node;
157 if (neigh_node == NULL)
158 neigh_node = create_neighbor(orig_node,
160 orig_neigh_node->orig,
163 neigh_node->last_valid = jiffies;
165 /* find packet count of corresponding one hop neighbor */
166 list_for_each_entry(tmp_neigh_node,
167 &orig_neigh_node->neigh_list, list) {
169 if (compare_orig(tmp_neigh_node->addr,
170 orig_neigh_node->orig) &&
171 (tmp_neigh_node->if_incoming == if_incoming))
172 neigh_node = tmp_neigh_node;
175 if (neigh_node == NULL)
176 neigh_node = create_neighbor(orig_neigh_node,
178 orig_neigh_node->orig,
182 orig_node->last_valid = jiffies;
184 /* pay attention to not get a value bigger than 100 % */
185 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
186 neigh_node->real_packet_count ?
187 neigh_node->real_packet_count :
188 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
190 /* if we have too few packets (too less data) we set tq_own to zero */
191 /* if we receive too few packets it is not considered bidirectional */
192 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
193 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
194 orig_neigh_node->tq_own = 0;
196 /* neigh_node->real_packet_count is never zero as we
197 * only purge old information when getting new
199 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
200 neigh_node->real_packet_count;
203 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
204 * affect the nearly-symmetric links only a little, but
205 * punishes asymmetric links more. This will give a value
206 * between 0 and TQ_MAX_VALUE
208 orig_neigh_node->tq_asym_penalty =
211 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
212 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
213 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
214 (TQ_LOCAL_WINDOW_SIZE *
215 TQ_LOCAL_WINDOW_SIZE *
216 TQ_LOCAL_WINDOW_SIZE);
218 batman_packet->tq = ((batman_packet->tq *
219 orig_neigh_node->tq_own *
220 orig_neigh_node->tq_asym_penalty) /
221 (TQ_MAX_VALUE * TQ_MAX_VALUE));
223 bat_dbg(DBG_BATMAN, "bidirectional: orig = %-15s neigh = %-15s => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i \n",
224 orig_str, neigh_str, 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 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
243 int tmp_hna_buff_len;
245 bat_dbg(DBG_BATMAN, "update_originator(): Searching and updating originator entry of received packet \n");
247 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
248 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
249 (tmp_neigh_node->if_incoming == if_incoming)) {
250 neigh_node = tmp_neigh_node;
257 ring_buffer_set(tmp_neigh_node->tq_recv,
258 &tmp_neigh_node->tq_index, 0);
259 tmp_neigh_node->tq_avg =
260 ring_buffer_avg(tmp_neigh_node->tq_recv);
263 if (neigh_node == NULL)
264 neigh_node = create_neighbor(orig_node,
265 get_orig_node(ethhdr->h_source),
266 ethhdr->h_source, if_incoming);
269 "Updating existing last-hop neighbour 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);
313 static char count_real_packets(struct ethhdr *ethhdr,
314 struct batman_packet *batman_packet,
315 struct batman_if *if_incoming)
317 struct orig_node *orig_node;
318 struct neigh_node *tmp_neigh_node;
319 char is_duplicate = 0;
322 orig_node = get_orig_node(batman_packet->orig);
323 if (orig_node == NULL)
326 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
330 get_bit_status(tmp_neigh_node->real_bits,
331 orig_node->last_real_seqno,
332 batman_packet->seqno);
333 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
334 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
335 (tmp_neigh_node->if_incoming == if_incoming))
336 bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 1);
338 bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 0);
340 tmp_neigh_node->real_packet_count =
341 bit_packet_count(tmp_neigh_node->real_bits);
345 bat_dbg(DBG_BATMAN, "updating last_seqno: old %d, new %d \n",
346 orig_node->last_real_seqno, batman_packet->seqno);
347 orig_node->last_real_seqno = batman_packet->seqno;
353 void receive_bat_packet(struct ethhdr *ethhdr,
354 struct batman_packet *batman_packet,
355 unsigned char *hna_buff,
357 struct batman_if *if_incoming)
359 struct batman_if *batman_if;
360 struct orig_node *orig_neigh_node, *orig_node;
361 char orig_str[ETH_STR_LEN], prev_sender_str[ETH_STR_LEN];
362 char neigh_str[ETH_STR_LEN];
363 char has_directlink_flag;
364 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
365 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
367 unsigned short if_incoming_seqno;
369 /* Silently drop when the batman packet is actually not a
372 * This might happen if a packet is padded (e.g. Ethernet has a
373 * minimum frame length of 64 byte) and the aggregation interprets
374 * it as an additional length.
376 * TODO: A more sane solution would be to have a bit in the
377 * batman_packet to detect whether the packet is the last
378 * packet in an aggregation. Here we expect that the padding
379 * is always zero (or not 0x01)
381 if (batman_packet->packet_type != BAT_PACKET)
384 /* could be changed by schedule_own_packet() */
385 if_incoming_seqno = atomic_read(&if_incoming->seqno);
387 addr_to_string(orig_str, batman_packet->orig);
388 addr_to_string(prev_sender_str, batman_packet->prev_sender);
389 addr_to_string(neigh_str, ethhdr->h_source);
391 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
393 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
394 batman_packet->orig) ? 1 : 0);
396 bat_dbg(DBG_BATMAN, "Received BATMAN packet via NB: %s, IF: %s [%s] (from OG: %s, via prev OG: %s, seqno %d, tq %d, TTL %d, V %d, IDF %d) \n",
397 neigh_str, if_incoming->dev, if_incoming->addr_str,
398 orig_str, prev_sender_str, batman_packet->seqno,
399 batman_packet->tq, batman_packet->ttl, batman_packet->version,
400 has_directlink_flag);
402 list_for_each_entry_rcu(batman_if, &if_list, list) {
403 if (batman_if->if_active != IF_ACTIVE)
406 if (compare_orig(ethhdr->h_source,
407 batman_if->net_dev->dev_addr))
410 if (compare_orig(batman_packet->orig,
411 batman_if->net_dev->dev_addr))
414 if (compare_orig(batman_packet->prev_sender,
415 batman_if->net_dev->dev_addr))
418 if (compare_orig(ethhdr->h_source, broadcastAddr))
422 if (batman_packet->version != COMPAT_VERSION) {
424 "Drop packet: incompatible batman version (%i)\n",
425 batman_packet->version);
431 "Drop packet: received my own broadcast (sender: %s)\n",
437 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all packets with broadcast source addr (sender: %s) \n", neigh_str);
445 orig_neigh_node = get_orig_node(ethhdr->h_source);
447 /* neighbour has to indicate direct link and it has to
448 * come via the corresponding interface */
449 /* if received seqno equals last send seqno save new
450 * seqno for bidirectional check */
451 if (has_directlink_flag &&
452 compare_orig(if_incoming->net_dev->dev_addr,
453 batman_packet->orig) &&
454 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
455 offset = if_incoming->if_num * NUM_WORDS;
456 word = &(orig_neigh_node->bcast_own[offset]);
458 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
459 bit_packet_count(word);
462 bat_dbg(DBG_BATMAN, "Drop packet: originator packet from myself (via neighbour) \n");
466 if (batman_packet->tq == 0) {
467 count_real_packets(ethhdr, batman_packet, if_incoming);
469 bat_dbg(DBG_BATMAN, "Drop packet: originator packet with tq equal 0 \n");
474 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast echos (sender: %s) \n", neigh_str);
478 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
480 orig_node = get_orig_node(batman_packet->orig);
481 if (orig_node == NULL)
484 /* avoid temporary routing loops */
485 if ((orig_node->router) &&
486 (orig_node->router->orig_node->router) &&
487 (compare_orig(orig_node->router->addr,
488 batman_packet->prev_sender)) &&
489 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
490 (compare_orig(orig_node->router->addr,
491 orig_node->router->orig_node->router->addr))) {
492 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %s) \n", neigh_str);
496 /* if sender is a direct neighbor the sender mac equals
498 orig_neigh_node = (is_single_hop_neigh ?
499 orig_node : get_orig_node(ethhdr->h_source));
500 if (orig_neigh_node == NULL)
503 /* drop packet if sender is not a direct neighbor and if we
504 * don't route towards it */
505 if (!is_single_hop_neigh &&
506 (orig_neigh_node->router == NULL)) {
507 bat_dbg(DBG_BATMAN, "Drop packet: OGM via unknown neighbor!\n");
511 is_bidirectional = isBidirectionalNeigh(orig_node, orig_neigh_node,
512 batman_packet, if_incoming);
514 /* update ranking if it is not a duplicate or has the same
515 * seqno and similar ttl as the non-duplicate */
516 if (is_bidirectional &&
518 ((orig_node->last_real_seqno == batman_packet->seqno) &&
519 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
520 update_orig(orig_node, ethhdr, batman_packet,
521 if_incoming, hna_buff, hna_buff_len, is_duplicate);
523 /* is single hop (direct) neighbour */
524 if (is_single_hop_neigh) {
526 /* mark direct link on incoming interface */
527 schedule_forward_packet(orig_node, ethhdr, batman_packet,
528 1, hna_buff_len, if_incoming);
530 bat_dbg(DBG_BATMAN, "Forwarding packet: rebroadcast neighbour packet with direct link flag\n");
534 /* multihop originator */
535 if (!is_bidirectional) {
537 "Drop packet: not received via bidirectional link\n");
542 bat_dbg(DBG_BATMAN, "Drop packet: duplicate packet received\n");
547 "Forwarding packet: rebroadcast originator packet\n");
548 schedule_forward_packet(orig_node, ethhdr, batman_packet,
549 0, hna_buff_len, if_incoming);
553 static int receive_raw_packet(struct socket *raw_sock,
554 unsigned char *packet_buff, int packet_buff_len)
559 iov.iov_base = packet_buff;
560 iov.iov_len = packet_buff_len;
562 msg.msg_flags = MSG_DONTWAIT; /* non-blocking */
565 msg.msg_control = NULL;
567 return kernel_recvmsg(raw_sock, &msg, &iov, 1, packet_buff_len,
571 static void recv_bat_packet(struct ethhdr *ethhdr,
572 unsigned char *packet_buff,
574 struct batman_if *batman_if)
576 /* packet with broadcast indication but unicast recipient */
577 if (!is_bcast(ethhdr->h_dest))
580 /* packet with broadcast sender address */
581 if (is_bcast(ethhdr->h_source))
584 /* drop packet if it has not at least one batman packet as payload */
585 if (result < sizeof(struct ethhdr) + sizeof(struct batman_packet))
588 spin_lock(&orig_hash_lock);
589 receive_aggr_bat_packet(ethhdr,
590 packet_buff + sizeof(struct ethhdr),
591 result - sizeof(struct ethhdr),
593 spin_unlock(&orig_hash_lock);
596 static void recv_my_icmp_packet(struct ethhdr *ethhdr,
597 struct icmp_packet *icmp_packet,
598 unsigned char *packet_buff,
601 struct orig_node *orig_node;
603 /* add data to device queue */
604 if (icmp_packet->msg_type != ECHO_REQUEST) {
605 bat_device_receive_packet(icmp_packet);
609 /* answer echo request (ping) */
610 /* get routing information */
611 spin_lock(&orig_hash_lock);
612 orig_node = ((struct orig_node *)hash_find(orig_hash,
615 if ((orig_node != NULL) &&
616 (orig_node->batman_if != NULL) &&
617 (orig_node->router != NULL)) {
618 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
619 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
620 icmp_packet->msg_type = ECHO_REPLY;
621 icmp_packet->ttl = TTL;
623 send_raw_packet(packet_buff + sizeof(struct ethhdr),
624 result - sizeof(struct ethhdr),
625 orig_node->batman_if,
626 orig_node->router->addr);
629 spin_unlock(&orig_hash_lock);
633 static void recv_icmp_ttl_exceeded(struct icmp_packet *icmp_packet,
634 struct ethhdr *ethhdr,
635 unsigned char *packet_buff,
637 struct batman_if *batman_if)
639 unsigned char src_str[ETH_STR_LEN], dst_str[ETH_STR_LEN];
640 struct orig_node *orig_node;
642 addr_to_string(src_str, icmp_packet->orig);
643 addr_to_string(dst_str, icmp_packet->dst);
645 printk(KERN_WARNING "batman-adv:Warning - can't send packet from %s to %s: ttl exceeded\n", src_str, dst_str);
647 /* send TTL exceeded if packet is an echo request (traceroute) */
648 if (icmp_packet->msg_type != ECHO_REQUEST)
651 /* get routing information */
652 spin_lock(&orig_hash_lock);
653 orig_node = ((struct orig_node *)
654 hash_find(orig_hash, icmp_packet->orig));
656 if ((orig_node != NULL) &&
657 (orig_node->batman_if != NULL) &&
658 (orig_node->router != NULL)) {
659 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
660 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
661 icmp_packet->msg_type = TTL_EXCEEDED;
662 icmp_packet->ttl = TTL;
664 send_raw_packet(packet_buff + sizeof(struct ethhdr),
665 result - sizeof(struct ethhdr),
666 orig_node->batman_if,
667 orig_node->router->addr);
671 spin_unlock(&orig_hash_lock);
676 static void recv_icmp_packet(struct ethhdr *ethhdr,
677 unsigned char *packet_buff,
679 struct batman_if *batman_if)
681 struct icmp_packet *icmp_packet;
682 struct orig_node *orig_node;
684 /* packet with unicast indication but broadcast recipient */
685 if (is_bcast(ethhdr->h_dest))
688 /* packet with broadcast sender address */
689 if (is_bcast(ethhdr->h_source))
693 if (!is_my_mac(ethhdr->h_dest))
696 /* drop packet if it has not necessary minimum size */
697 if (result < sizeof(struct ethhdr) + sizeof(struct icmp_packet))
700 icmp_packet = (struct icmp_packet *)
701 (packet_buff + sizeof(struct ethhdr));
704 if (is_my_mac(icmp_packet->dst))
705 recv_my_icmp_packet(ethhdr, icmp_packet, packet_buff, result);
708 if (icmp_packet->ttl < 2) {
709 recv_icmp_ttl_exceeded(icmp_packet, ethhdr, packet_buff, result,
715 /* get routing information */
716 spin_lock(&orig_hash_lock);
717 orig_node = ((struct orig_node *)
718 hash_find(orig_hash, icmp_packet->dst));
720 if ((orig_node != NULL) &&
721 (orig_node->batman_if != NULL) &&
722 (orig_node->router != NULL)) {
728 send_raw_packet(packet_buff + sizeof(struct ethhdr),
729 result - sizeof(struct ethhdr),
730 orig_node->batman_if,
731 orig_node->router->addr);
733 spin_unlock(&orig_hash_lock);
736 static void recv_unicast_packet(struct ethhdr *ethhdr,
737 unsigned char *packet_buff,
739 struct batman_if *batman_if)
741 struct unicast_packet *unicast_packet;
742 unsigned char src_str[ETH_STR_LEN], dst_str[ETH_STR_LEN];
743 struct orig_node *orig_node;
744 int hdr_size = sizeof(struct ethhdr) + sizeof(struct unicast_packet);
746 /* packet with unicast indication but broadcast recipient */
747 if (is_bcast(ethhdr->h_dest))
750 /* packet with broadcast sender address */
751 if (is_bcast(ethhdr->h_source))
755 if (!is_my_mac(ethhdr->h_dest))
758 /* drop packet if it has not necessary minimum size */
759 if (result < hdr_size)
762 unicast_packet = (struct unicast_packet *)
763 (packet_buff + sizeof(struct ethhdr));
766 if (is_my_mac(unicast_packet->dest)) {
767 interface_rx(soft_device, packet_buff + hdr_size,
774 if (unicast_packet->ttl < 2) {
775 addr_to_string(src_str, ((struct ethhdr *)
776 (unicast_packet + 1))->h_source);
777 addr_to_string(dst_str, unicast_packet->dest);
779 printk(KERN_WARNING "batman-adv:Warning - can't send packet from %s to %s: ttl exceeded\n", src_str, dst_str);
783 /* get routing information */
784 spin_lock(&orig_hash_lock);
785 orig_node = ((struct orig_node *)
786 hash_find(orig_hash, unicast_packet->dest));
788 if ((orig_node != NULL) &&
789 (orig_node->batman_if != NULL) &&
790 (orig_node->router != NULL)) {
792 unicast_packet->ttl--;
795 send_raw_packet(packet_buff + sizeof(struct ethhdr),
796 result - sizeof(struct ethhdr),
797 orig_node->batman_if,
798 orig_node->router->addr);
800 spin_unlock(&orig_hash_lock);
804 static void recv_bcast_packet(struct ethhdr *ethhdr,
805 unsigned char *packet_buff,
807 struct batman_if *batman_if)
809 struct orig_node *orig_node;
810 struct bcast_packet *bcast_packet;
811 int hdr_size = sizeof(struct ethhdr) + sizeof(struct bcast_packet);
813 /* packet with broadcast indication but unicast recipient */
814 if (!is_bcast(ethhdr->h_dest))
817 /* packet with broadcast sender address */
818 if (is_bcast(ethhdr->h_source))
821 /* drop packet if it has not necessary minimum size */
822 if (result < hdr_size)
825 /* ignore broadcasts sent by myself */
826 if (is_my_mac(ethhdr->h_source))
829 bcast_packet = (struct bcast_packet *)
830 (packet_buff + sizeof(struct ethhdr));
832 /* ignore broadcasts originated by myself */
833 if (is_my_mac(bcast_packet->orig))
836 spin_lock(&orig_hash_lock);
837 orig_node = ((struct orig_node *)
838 hash_find(orig_hash, bcast_packet->orig));
840 if (orig_node == NULL) {
841 spin_unlock(&orig_hash_lock);
845 /* check flood history */
846 if (get_bit_status(orig_node->bcast_bits,
847 orig_node->last_bcast_seqno,
848 ntohs(bcast_packet->seqno))) {
849 spin_unlock(&orig_hash_lock);
853 /* mark broadcast in flood history */
854 if (bit_get_packet(orig_node->bcast_bits,
855 ntohs(bcast_packet->seqno) -
856 orig_node->last_bcast_seqno, 1))
857 orig_node->last_bcast_seqno = ntohs(bcast_packet->seqno);
859 spin_unlock(&orig_hash_lock);
861 /* broadcast for me */
862 interface_rx(soft_device, packet_buff + hdr_size, result - hdr_size);
864 /* rebroadcast packet */
865 add_bcast_packet_to_list(packet_buff + sizeof(struct ethhdr),
866 result - sizeof(struct ethhdr));
869 static void recv_vis_packet(struct ethhdr *ethhdr,
870 unsigned char *packet_buff,
873 struct vis_packet *vis_packet;
874 int hdr_size = sizeof(struct ethhdr) + sizeof(struct vis_packet);
877 /* drop if too short. */
878 if (result < hdr_size)
882 if (!is_my_mac(ethhdr->h_dest))
885 vis_packet = (struct vis_packet *)(packet_buff + sizeof(struct ethhdr));
886 vis_info_len = result - hdr_size;
888 /* ignore own packets */
889 if (is_my_mac(vis_packet->vis_orig))
892 if (is_my_mac(vis_packet->sender_orig))
895 switch (vis_packet->vis_type) {
896 case VIS_TYPE_SERVER_SYNC:
897 receive_server_sync_packet(vis_packet, vis_info_len);
900 case VIS_TYPE_CLIENT_UPDATE:
901 receive_client_update_packet(vis_packet, vis_info_len);
904 default: /* ignore unknown packet */
909 static int recv_one_packet(struct batman_if *batman_if,
910 unsigned char *packet_buff)
913 struct ethhdr *ethhdr;
914 struct batman_packet *batman_packet;
916 result = receive_raw_packet(batman_if->raw_sock, packet_buff,
921 if (result < sizeof(struct ethhdr) + 2)
924 ethhdr = (struct ethhdr *)packet_buff;
925 batman_packet = (struct batman_packet *)
926 (packet_buff + sizeof(struct ethhdr));
928 if (batman_packet->version != COMPAT_VERSION) {
930 "Drop packet: incompatible batman version (%i)\n",
931 batman_packet->version);
935 switch (batman_packet->packet_type) {
936 /* batman originator packet */
938 recv_bat_packet(ethhdr, packet_buff, result, batman_if);
941 /* batman icmp packet */
943 recv_icmp_packet(ethhdr, packet_buff, result, batman_if);
948 recv_unicast_packet(ethhdr, packet_buff, result, batman_if);
951 /* broadcast packet */
953 recv_bcast_packet(ethhdr,
954 packet_buff, result, batman_if);
959 recv_vis_packet(ethhdr, packet_buff, result);
966 static int discard_one_packet(struct batman_if *batman_if,
967 unsigned char *packet_buff)
969 int result = -EAGAIN;
971 if (batman_if->raw_sock) {
972 result = receive_raw_packet(batman_if->raw_sock,
980 static bool is_interface_active(struct batman_if *batman_if)
982 if (batman_if->if_active != IF_ACTIVE)
988 static void service_interface(struct batman_if *batman_if,
989 unsigned char *packet_buff)
995 if (is_interface_active(batman_if))
996 result = recv_one_packet(batman_if, packet_buff);
998 result = discard_one_packet(batman_if, packet_buff);
999 } while (result >= 0);
1001 /* we perform none blocking reads, so EAGAIN indicates there
1002 are no more packets to read. Anything else is a real
1005 if ((result < 0) && (result != -EAGAIN))
1006 printk(KERN_ERR "batman-adv:Could not receive packet from interface %s: %i\n", batman_if->dev, result);
1009 static void service_interfaces(unsigned char *packet_buffer)
1011 struct batman_if *batman_if;
1013 list_for_each_entry_rcu(batman_if, &if_list, list) {
1015 service_interface(batman_if, packet_buffer);
1022 int packet_recv_thread(void *data)
1024 unsigned char *packet_buff;
1026 atomic_set(&data_ready_cond, 0);
1027 atomic_set(&exit_cond, 0);
1028 packet_buff = kmalloc(PACKBUFF_SIZE, GFP_KERNEL);
1030 printk(KERN_ERR"batman-adv:Could allocate memory for the packet buffer. :(\n");
1034 while ((!kthread_should_stop()) && (!atomic_read(&exit_cond))) {
1036 wait_event_interruptible(thread_wait,
1037 (atomic_read(&data_ready_cond) ||
1038 atomic_read(&exit_cond)));
1040 atomic_set(&data_ready_cond, 0);
1042 if (kthread_should_stop() || atomic_read(&exit_cond))
1045 service_interfaces(packet_buff);
1049 /* do not exit until kthread_stop() is actually called,
1050 * otherwise it will wait for us forever. */
1051 while (!kthread_should_stop())
1057 void batman_data_ready(struct sock *sk, int len)
1059 void (*data_ready)(struct sock *, int) = sk->sk_user_data;
1061 data_ready(sk, len);
1063 atomic_set(&data_ready_cond, 1);
1064 wake_up_interruptible(&thread_wait);