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
25 #include "soft-interface.h"
26 #include "hard-interface.h"
28 #include "translation-table.h"
31 #include "ring_buffer.h"
33 #include "aggregation.h"
36 DECLARE_WAIT_QUEUE_HEAD(thread_wait);
37 static DECLARE_DELAYED_WORK(purge_orig_wq, purge_orig);
39 static atomic_t data_ready_cond;
42 static void start_purge_timer(void)
44 queue_delayed_work(bat_event_workqueue, &purge_orig_wq, 1 * HZ);
47 int originator_init(void)
52 spin_lock(&orig_hash_lock);
53 orig_hash = hash_new(128, compare_orig, choose_orig);
58 spin_unlock(&orig_hash_lock);
63 spin_unlock(&orig_hash_lock);
67 void originator_free(void)
72 cancel_delayed_work_sync(&purge_orig_wq);
74 spin_lock(&orig_hash_lock);
75 hash_delete(orig_hash, free_orig_node);
77 spin_unlock(&orig_hash_lock);
80 static struct neigh_node *
81 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
82 uint8_t *neigh, struct batman_if *if_incoming)
84 struct neigh_node *neigh_node;
86 bat_dbg(DBG_BATMAN, "Creating new last-hop neighbour of originator\n");
88 neigh_node = kmalloc(sizeof(struct neigh_node), GFP_ATOMIC);
89 memset(neigh_node, 0, sizeof(struct neigh_node));
90 INIT_LIST_HEAD(&neigh_node->list);
92 memcpy(neigh_node->addr, neigh, ETH_ALEN);
93 neigh_node->orig_node = orig_neigh_node;
94 neigh_node->if_incoming = if_incoming;
96 list_add_tail(&neigh_node->list, &orig_node->neigh_list);
100 void free_orig_node(void *data)
102 struct list_head *list_pos, *list_pos_tmp;
103 struct neigh_node *neigh_node;
104 struct orig_node *orig_node = (struct orig_node *)data;
106 /* for all neighbours towards this originator ... */
107 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
108 neigh_node = list_entry(list_pos, struct neigh_node, list);
114 hna_global_del_orig(orig_node, "originator timed out");
116 kfree(orig_node->bcast_own);
117 kfree(orig_node->bcast_own_sum);
121 /* this function finds or creates an originator entry for the given
122 * address if it does not exits */
123 static struct orig_node *get_orig_node(uint8_t *addr)
125 struct orig_node *orig_node;
126 struct hashtable_t *swaphash;
127 char orig_str[ETH_STR_LEN];
130 orig_node = ((struct orig_node *)hash_find(orig_hash, addr));
132 if (orig_node != NULL)
135 addr_to_string(orig_str, addr);
136 bat_dbg(DBG_BATMAN, "Creating new originator: %s \n", orig_str);
138 orig_node = kmalloc(sizeof(struct orig_node), GFP_ATOMIC);
139 memset(orig_node, 0, sizeof(struct orig_node));
140 INIT_LIST_HEAD(&orig_node->neigh_list);
142 memcpy(orig_node->orig, addr, ETH_ALEN);
143 orig_node->router = NULL;
144 orig_node->batman_if = NULL;
145 orig_node->hna_buff = NULL;
147 size = num_ifs * sizeof(TYPE_OF_WORD) * NUM_WORDS;
149 orig_node->bcast_own = kmalloc(size, GFP_ATOMIC);
150 memset(orig_node->bcast_own, 0, size);
152 size = num_ifs * sizeof(uint8_t);
153 orig_node->bcast_own_sum = kmalloc(size, GFP_ATOMIC);
154 memset(orig_node->bcast_own_sum, 0, size);
156 hash_add(orig_hash, orig_node);
158 if (orig_hash->elements * 4 > orig_hash->size) {
159 swaphash = hash_resize(orig_hash, orig_hash->size * 2);
161 if (swaphash == NULL)
163 "batman-adv:Couldn't resize orig hash table \n");
165 orig_hash = swaphash;
171 void slide_own_bcast_window(struct batman_if *batman_if)
173 struct hash_it_t *hashit = NULL;
174 struct orig_node *orig_node;
177 spin_lock(&orig_hash_lock);
179 while (NULL != (hashit = hash_iterate(orig_hash, hashit))) {
180 orig_node = hashit->bucket->data;
181 word = &(orig_node->bcast_own[batman_if->if_num * NUM_WORDS]);
183 bit_get_packet(word, 1, 0);
184 orig_node->bcast_own_sum[batman_if->if_num] =
185 bit_packet_count(word);
188 spin_unlock(&orig_hash_lock);
191 static void update_HNA(struct orig_node *orig_node,
192 unsigned char *hna_buff, int hna_buff_len)
194 if ((hna_buff_len != orig_node->hna_buff_len) ||
195 ((hna_buff_len > 0) &&
196 (orig_node->hna_buff_len > 0) &&
197 (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
199 if (orig_node->hna_buff_len > 0)
200 hna_global_del_orig(orig_node,
201 "originator changed hna");
203 if ((hna_buff_len > 0) && (hna_buff != NULL))
204 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
208 static void update_route(struct orig_node *orig_node,
209 struct neigh_node *neigh_node,
210 unsigned char *hna_buff, int hna_buff_len)
212 char orig_str[ETH_STR_LEN], neigh_str[ETH_STR_LEN];
213 char router_str[ETH_STR_LEN];
215 addr_to_string(orig_str, orig_node->orig);
218 if ((orig_node->router != NULL) && (neigh_node == NULL)) {
220 bat_dbg(DBG_ROUTES, "Deleting route towards: %s\n",
222 hna_global_del_orig(orig_node, "originator timed out");
225 } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
227 addr_to_string(neigh_str, neigh_node->addr);
229 "Adding route towards: %s (via %s)\n",
230 orig_str, neigh_str);
231 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
235 addr_to_string(neigh_str, neigh_node->addr);
236 addr_to_string(router_str, orig_node->router->addr);
237 bat_dbg(DBG_ROUTES, "Changing route towards: %s (now via %s - was via %s)\n", orig_str, neigh_str, router_str);
240 if (neigh_node != NULL)
241 orig_node->batman_if = neigh_node->if_incoming;
243 orig_node->batman_if = NULL;
245 orig_node->router = neigh_node;
249 static void update_routes(struct orig_node *orig_node,
250 struct neigh_node *neigh_node,
251 unsigned char *hna_buff, int hna_buff_len)
254 if (orig_node == NULL)
257 if (orig_node->router != neigh_node)
258 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
259 /* may be just HNA changed */
261 update_HNA(orig_node, hna_buff, hna_buff_len);
264 static int isBidirectionalNeigh(struct orig_node *orig_node,
265 struct orig_node *orig_neigh_node,
266 struct batman_packet *batman_packet,
267 struct batman_if *if_incoming)
269 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
270 char orig_str[ETH_STR_LEN], neigh_str[ETH_STR_LEN];
271 unsigned char total_count;
273 addr_to_string(orig_str, orig_node->orig);
274 addr_to_string(neigh_str, orig_neigh_node->orig);
276 if (orig_node == orig_neigh_node) {
277 list_for_each_entry(tmp_neigh_node,
278 &orig_node->neigh_list,
281 if (compare_orig(tmp_neigh_node->addr,
282 orig_neigh_node->orig) &&
283 (tmp_neigh_node->if_incoming == if_incoming))
284 neigh_node = tmp_neigh_node;
287 if (neigh_node == NULL)
288 neigh_node = create_neighbor(orig_node,
290 orig_neigh_node->orig,
293 neigh_node->last_valid = jiffies;
295 /* find packet count of corresponding one hop neighbor */
296 list_for_each_entry(tmp_neigh_node,
297 &orig_neigh_node->neigh_list, list) {
299 if (compare_orig(tmp_neigh_node->addr,
300 orig_neigh_node->orig) &&
301 (tmp_neigh_node->if_incoming == if_incoming))
302 neigh_node = tmp_neigh_node;
305 if (neigh_node == NULL)
306 neigh_node = create_neighbor(orig_neigh_node,
308 orig_neigh_node->orig,
312 orig_node->last_valid = jiffies;
314 /* pay attention to not get a value bigger than 100 % */
315 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
316 neigh_node->real_packet_count ?
317 neigh_node->real_packet_count :
318 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
320 /* if we have too few packets (too less data) we set tq_own to zero */
321 /* if we receive too few packets it is not considered bidirectional */
322 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
323 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
324 orig_neigh_node->tq_own = 0;
326 /* neigh_node->real_packet_count is never zero as we
327 * only purge old information when getting new
329 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
330 neigh_node->real_packet_count;
333 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
334 * affect the nearly-symmetric links only a little, but
335 * punishes asymmetric links more. This will give a value
336 * between 0 and TQ_MAX_VALUE
338 orig_neigh_node->tq_asym_penalty =
341 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
342 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
343 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
344 (TQ_LOCAL_WINDOW_SIZE *
345 TQ_LOCAL_WINDOW_SIZE *
346 TQ_LOCAL_WINDOW_SIZE);
348 batman_packet->tq = ((batman_packet->tq *
349 orig_neigh_node->tq_own *
350 orig_neigh_node->tq_asym_penalty) /
351 (TQ_MAX_VALUE * TQ_MAX_VALUE));
353 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",
354 orig_str, neigh_str, total_count,
355 neigh_node->real_packet_count, orig_neigh_node->tq_own,
356 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
358 /* if link has the minimum required transmission quality
359 * consider it bidirectional */
360 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
366 static void update_orig(struct orig_node *orig_node, struct ethhdr *ethhdr,
367 struct batman_packet *batman_packet,
368 struct batman_if *if_incoming,
369 unsigned char *hna_buff, int hna_buff_len,
372 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
373 int tmp_hna_buff_len;
375 bat_dbg(DBG_BATMAN, "update_originator(): Searching and updating originator entry of received packet \n");
377 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
378 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
379 (tmp_neigh_node->if_incoming == if_incoming)) {
380 neigh_node = tmp_neigh_node;
387 ring_buffer_set(tmp_neigh_node->tq_recv,
388 &tmp_neigh_node->tq_index, 0);
389 tmp_neigh_node->tq_avg =
390 ring_buffer_avg(tmp_neigh_node->tq_recv);
393 if (neigh_node == NULL)
394 neigh_node = create_neighbor(orig_node,
395 get_orig_node(ethhdr->h_source),
396 ethhdr->h_source, if_incoming);
399 "Updating existing last-hop neighbour of originator\n");
401 orig_node->flags = batman_packet->flags;
402 neigh_node->last_valid = jiffies;
404 ring_buffer_set(neigh_node->tq_recv,
405 &neigh_node->tq_index,
407 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
410 orig_node->last_ttl = batman_packet->ttl;
411 neigh_node->last_ttl = batman_packet->ttl;
414 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
415 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
417 /* if this neighbor already is our next hop there is nothing
419 if (orig_node->router == neigh_node)
422 /* if this neighbor does not offer a better TQ we won't consider it */
423 if ((orig_node->router) &&
424 (orig_node->router->tq_avg > neigh_node->tq_avg))
427 /* if the TQ is the same and the link not more symetric we
428 * won't consider it either */
429 if ((orig_node->router) &&
430 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
431 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
432 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
435 update_routes(orig_node, neigh_node, hna_buff, tmp_hna_buff_len);
439 update_routes(orig_node, orig_node->router, hna_buff, tmp_hna_buff_len);
443 static char count_real_packets(struct ethhdr *ethhdr,
444 struct batman_packet *batman_packet,
445 struct batman_if *if_incoming)
447 struct orig_node *orig_node;
448 struct neigh_node *tmp_neigh_node;
449 char is_duplicate = 0;
452 orig_node = get_orig_node(batman_packet->orig);
453 if (orig_node == NULL)
456 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
460 get_bit_status(tmp_neigh_node->real_bits,
461 orig_node->last_real_seqno,
462 batman_packet->seqno);
463 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
464 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
465 (tmp_neigh_node->if_incoming == if_incoming))
466 bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 1);
468 bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 0);
470 tmp_neigh_node->real_packet_count =
471 bit_packet_count(tmp_neigh_node->real_bits);
475 bat_dbg(DBG_BATMAN, "updating last_seqno: old %d, new %d \n",
476 orig_node->last_real_seqno, batman_packet->seqno);
477 orig_node->last_real_seqno = batman_packet->seqno;
483 void receive_bat_packet(struct ethhdr *ethhdr,
484 struct batman_packet *batman_packet,
485 unsigned char *hna_buff,
487 struct batman_if *if_incoming)
489 struct batman_if *batman_if;
490 struct orig_node *orig_neigh_node, *orig_node;
491 char orig_str[ETH_STR_LEN], prev_sender_str[ETH_STR_LEN];
492 char neigh_str[ETH_STR_LEN];
493 char has_directlink_flag;
494 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
495 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
497 unsigned short if_incoming_seqno;
499 /* Silently drop when the batman packet is actually not a
502 * This might happen if a packet is padded (e.g. Ethernet has a
503 * minimum frame length of 64 byte) and the aggregation interprets
504 * it as an additional length.
506 * TODO: A more sane solution would be to have a bit in the
507 * batman_packet to detect whether the packet is the last
508 * packet in an aggregation. Here we expect that the padding
509 * is always zero (or not 0x01)
511 if (batman_packet->packet_type != BAT_PACKET)
514 /* could be changed by schedule_own_packet() */
515 if_incoming_seqno = atomic_read(&if_incoming->seqno);
517 addr_to_string(orig_str, batman_packet->orig);
518 addr_to_string(prev_sender_str, batman_packet->prev_sender);
519 addr_to_string(neigh_str, ethhdr->h_source);
521 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
523 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
524 batman_packet->orig) ? 1 : 0);
526 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",
527 neigh_str, if_incoming->dev, if_incoming->addr_str,
528 orig_str, prev_sender_str, batman_packet->seqno,
529 batman_packet->tq, batman_packet->ttl, batman_packet->version,
530 has_directlink_flag);
532 list_for_each_entry_rcu(batman_if, &if_list, list) {
533 if (batman_if->if_active != IF_ACTIVE)
536 if (compare_orig(ethhdr->h_source,
537 batman_if->net_dev->dev_addr))
540 if (compare_orig(batman_packet->orig,
541 batman_if->net_dev->dev_addr))
544 if (compare_orig(batman_packet->prev_sender,
545 batman_if->net_dev->dev_addr))
548 if (compare_orig(ethhdr->h_source, broadcastAddr))
552 if (batman_packet->version != COMPAT_VERSION) {
554 "Drop packet: incompatible batman version (%i)\n",
555 batman_packet->version);
561 "Drop packet: received my own broadcast (sender: %s)\n",
567 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all packets with broadcast source addr (sender: %s) \n", neigh_str);
575 orig_neigh_node = get_orig_node(ethhdr->h_source);
577 /* neighbour has to indicate direct link and it has to
578 * come via the corresponding interface */
579 /* if received seqno equals last send seqno save new
580 * seqno for bidirectional check */
581 if (has_directlink_flag &&
582 compare_orig(if_incoming->net_dev->dev_addr,
583 batman_packet->orig) &&
584 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
585 offset = if_incoming->if_num * NUM_WORDS;
586 word = &(orig_neigh_node->bcast_own[offset]);
588 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
589 bit_packet_count(word);
592 bat_dbg(DBG_BATMAN, "Drop packet: originator packet from myself (via neighbour) \n");
596 if (batman_packet->tq == 0) {
597 count_real_packets(ethhdr, batman_packet, if_incoming);
599 bat_dbg(DBG_BATMAN, "Drop packet: originator packet with tq equal 0 \n");
604 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast echos (sender: %s) \n", neigh_str);
608 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
610 orig_node = get_orig_node(batman_packet->orig);
611 if (orig_node == NULL)
614 /* avoid temporary routing loops */
615 if ((orig_node->router) &&
616 (orig_node->router->orig_node->router) &&
617 (compare_orig(orig_node->router->addr,
618 batman_packet->prev_sender)) &&
619 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
620 (compare_orig(orig_node->router->addr,
621 orig_node->router->orig_node->router->addr))) {
622 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %s) \n", neigh_str);
626 /* if sender is a direct neighbor the sender mac equals
628 orig_neigh_node = (is_single_hop_neigh ?
629 orig_node : get_orig_node(ethhdr->h_source));
630 if (orig_neigh_node == NULL)
633 /* drop packet if sender is not a direct neighbor and if we
634 * don't route towards it */
635 if (!is_single_hop_neigh &&
636 (orig_neigh_node->router == NULL)) {
637 bat_dbg(DBG_BATMAN, "Drop packet: OGM via unknown neighbor!\n");
641 is_bidirectional = isBidirectionalNeigh(orig_node, orig_neigh_node,
642 batman_packet, if_incoming);
644 /* update ranking if it is not a duplicate or has the same
645 * seqno and similar ttl as the non-duplicate */
646 if (is_bidirectional &&
648 ((orig_node->last_real_seqno == batman_packet->seqno) &&
649 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
650 update_orig(orig_node, ethhdr, batman_packet,
651 if_incoming, hna_buff, hna_buff_len, is_duplicate);
653 /* is single hop (direct) neighbour */
654 if (is_single_hop_neigh) {
656 /* mark direct link on incoming interface */
657 schedule_forward_packet(orig_node, ethhdr, batman_packet,
658 1, hna_buff_len, if_incoming);
660 bat_dbg(DBG_BATMAN, "Forwarding packet: rebroadcast neighbour packet with direct link flag\n");
664 /* multihop originator */
665 if (!is_bidirectional) {
667 "Drop packet: not received via bidirectional link\n");
672 bat_dbg(DBG_BATMAN, "Drop packet: duplicate packet received\n");
677 "Forwarding packet: rebroadcast originator packet\n");
678 schedule_forward_packet(orig_node, ethhdr, batman_packet,
679 0, hna_buff_len, if_incoming);
682 static bool purge_orig_neigbours(struct orig_node *orig_node,
683 struct neigh_node **best_neigh_node)
685 struct list_head *list_pos, *list_pos_tmp;
686 char neigh_str[ETH_STR_LEN], orig_str[ETH_STR_LEN];
687 struct neigh_node *neigh_node;
688 bool neigh_purged = false;
690 *best_neigh_node = NULL;
693 /* for all neighbours towards this originator ... */
694 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
695 neigh_node = list_entry(list_pos, struct neigh_node, list);
697 if (time_after(jiffies,
698 (neigh_node->last_valid +
699 ((PURGE_TIMEOUT * HZ) / 1000)))) {
701 addr_to_string(neigh_str, neigh_node->addr);
702 bat_dbg(DBG_BATMAN, "Neighbour timeout: originator %s, neighbour: %s, last_valid %lu\n", orig_str, neigh_str, (neigh_node->last_valid / HZ));
708 if ((*best_neigh_node == NULL) ||
709 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
710 *best_neigh_node = neigh_node;
716 static bool purge_orig_node(struct orig_node *orig_node)
718 struct neigh_node *best_neigh_node;
719 char orig_str[ETH_STR_LEN];
721 addr_to_string(orig_str, orig_node->orig);
723 if (time_after(jiffies,
724 (orig_node->last_valid +
725 ((2 * PURGE_TIMEOUT * HZ) / 1000)))) {
728 "Originator timeout: originator %s, last_valid %lu\n",
729 orig_str, (orig_node->last_valid / HZ));
732 if (purge_orig_neigbours(orig_node, &best_neigh_node))
733 update_routes(orig_node, best_neigh_node,
735 orig_node->hna_buff_len);
741 void purge_orig(struct work_struct *work)
743 struct hash_it_t *hashit = NULL;
744 struct orig_node *orig_node;
746 spin_lock(&orig_hash_lock);
748 /* for all origins... */
749 while (NULL != (hashit = hash_iterate(orig_hash, hashit))) {
750 orig_node = hashit->bucket->data;
751 if (purge_orig_node(orig_node)) {
752 hash_remove_bucket(orig_hash, hashit);
753 free_orig_node(orig_node);
757 spin_unlock(&orig_hash_lock);
762 static int receive_raw_packet(struct socket *raw_sock,
763 unsigned char *packet_buff, int packet_buff_len)
768 iov.iov_base = packet_buff;
769 iov.iov_len = packet_buff_len;
771 msg.msg_flags = MSG_DONTWAIT; /* non-blocking */
774 msg.msg_control = NULL;
776 return kernel_recvmsg(raw_sock, &msg, &iov, 1, packet_buff_len,
780 static void recv_bat_packet(struct ethhdr *ethhdr,
781 unsigned char *packet_buff,
783 struct batman_if *batman_if)
785 /* packet with broadcast indication but unicast recipient */
786 if (!is_bcast(ethhdr->h_dest))
789 /* packet with broadcast sender address */
790 if (is_bcast(ethhdr->h_source))
793 /* drop packet if it has not at least one batman packet as payload */
794 if (result < sizeof(struct ethhdr) + sizeof(struct batman_packet))
797 spin_lock(&orig_hash_lock);
798 receive_aggr_bat_packet(ethhdr,
799 packet_buff + sizeof(struct ethhdr),
800 result - sizeof(struct ethhdr),
802 spin_unlock(&orig_hash_lock);
805 static void recv_my_icmp_packet(struct ethhdr *ethhdr,
806 struct icmp_packet *icmp_packet,
807 unsigned char *packet_buff,
810 struct orig_node *orig_node;
812 /* add data to device queue */
813 if (icmp_packet->msg_type != ECHO_REQUEST) {
814 bat_device_receive_packet(icmp_packet);
818 /* answer echo request (ping) */
819 /* get routing information */
820 spin_lock(&orig_hash_lock);
821 orig_node = ((struct orig_node *)hash_find(orig_hash,
824 if ((orig_node != NULL) &&
825 (orig_node->batman_if != NULL) &&
826 (orig_node->router != NULL)) {
827 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
828 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
829 icmp_packet->msg_type = ECHO_REPLY;
830 icmp_packet->ttl = TTL;
832 send_raw_packet(packet_buff + sizeof(struct ethhdr),
833 result - sizeof(struct ethhdr),
834 orig_node->batman_if,
835 orig_node->router->addr);
838 spin_unlock(&orig_hash_lock);
842 static void recv_icmp_ttl_exceeded(struct icmp_packet *icmp_packet,
843 struct ethhdr *ethhdr,
844 unsigned char *packet_buff,
846 struct batman_if *batman_if)
848 unsigned char src_str[ETH_STR_LEN], dst_str[ETH_STR_LEN];
849 struct orig_node *orig_node;
851 addr_to_string(src_str, icmp_packet->orig);
852 addr_to_string(dst_str, icmp_packet->dst);
854 printk(KERN_WARNING "batman-adv:Warning - can't send packet from %s to %s: ttl exceeded\n", src_str, dst_str);
856 /* send TTL exceeded if packet is an echo request (traceroute) */
857 if (icmp_packet->msg_type != ECHO_REQUEST)
860 /* get routing information */
861 spin_lock(&orig_hash_lock);
862 orig_node = ((struct orig_node *)
863 hash_find(orig_hash, icmp_packet->orig));
865 if ((orig_node != NULL) &&
866 (orig_node->batman_if != NULL) &&
867 (orig_node->router != NULL)) {
868 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
869 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
870 icmp_packet->msg_type = TTL_EXCEEDED;
871 icmp_packet->ttl = TTL;
873 send_raw_packet(packet_buff + sizeof(struct ethhdr),
874 result - sizeof(struct ethhdr),
875 orig_node->batman_if,
876 orig_node->router->addr);
880 spin_unlock(&orig_hash_lock);
885 static void recv_icmp_packet(struct ethhdr *ethhdr,
886 unsigned char *packet_buff,
888 struct batman_if *batman_if)
890 struct icmp_packet *icmp_packet;
891 struct orig_node *orig_node;
893 /* packet with unicast indication but broadcast recipient */
894 if (is_bcast(ethhdr->h_dest))
897 /* packet with broadcast sender address */
898 if (is_bcast(ethhdr->h_source))
902 if (!is_my_mac(ethhdr->h_dest))
905 /* drop packet if it has not necessary minimum size */
906 if (result < sizeof(struct ethhdr) + sizeof(struct icmp_packet))
909 icmp_packet = (struct icmp_packet *)
910 (packet_buff + sizeof(struct ethhdr));
913 if (is_my_mac(icmp_packet->dst))
914 recv_my_icmp_packet(ethhdr, icmp_packet, packet_buff, result);
917 if (icmp_packet->ttl < 2) {
918 recv_icmp_ttl_exceeded(icmp_packet, ethhdr, packet_buff, result,
924 /* get routing information */
925 spin_lock(&orig_hash_lock);
926 orig_node = ((struct orig_node *)
927 hash_find(orig_hash, icmp_packet->dst));
929 if ((orig_node != NULL) &&
930 (orig_node->batman_if != NULL) &&
931 (orig_node->router != NULL)) {
937 send_raw_packet(packet_buff + sizeof(struct ethhdr),
938 result - sizeof(struct ethhdr),
939 orig_node->batman_if,
940 orig_node->router->addr);
942 spin_unlock(&orig_hash_lock);
945 static void recv_unicast_packet(struct ethhdr *ethhdr,
946 unsigned char *packet_buff,
948 struct batman_if *batman_if)
950 struct unicast_packet *unicast_packet;
951 unsigned char src_str[ETH_STR_LEN], dst_str[ETH_STR_LEN];
952 struct orig_node *orig_node;
953 int hdr_size = sizeof(struct ethhdr) + sizeof(struct unicast_packet);
955 /* packet with unicast indication but broadcast recipient */
956 if (is_bcast(ethhdr->h_dest))
959 /* packet with broadcast sender address */
960 if (is_bcast(ethhdr->h_source))
964 if (!is_my_mac(ethhdr->h_dest))
967 /* drop packet if it has not necessary minimum size */
968 if (result < hdr_size)
971 unicast_packet = (struct unicast_packet *)
972 (packet_buff + sizeof(struct ethhdr));
975 if (is_my_mac(unicast_packet->dest)) {
976 interface_rx(soft_device, packet_buff + hdr_size,
983 if (unicast_packet->ttl < 2) {
984 addr_to_string(src_str, ((struct ethhdr *)
985 (unicast_packet + 1))->h_source);
986 addr_to_string(dst_str, unicast_packet->dest);
988 printk(KERN_WARNING "batman-adv:Warning - can't send packet from %s to %s: ttl exceeded\n", src_str, dst_str);
992 /* get routing information */
993 spin_lock(&orig_hash_lock);
994 orig_node = ((struct orig_node *)
995 hash_find(orig_hash, unicast_packet->dest));
997 if ((orig_node != NULL) &&
998 (orig_node->batman_if != NULL) &&
999 (orig_node->router != NULL)) {
1001 unicast_packet->ttl--;
1004 send_raw_packet(packet_buff + sizeof(struct ethhdr),
1005 result - sizeof(struct ethhdr),
1006 orig_node->batman_if,
1007 orig_node->router->addr);
1009 spin_unlock(&orig_hash_lock);
1013 static void recv_bcast_packet(struct ethhdr *ethhdr,
1014 unsigned char *packet_buff,
1016 struct batman_if *batman_if)
1018 struct orig_node *orig_node;
1019 struct bcast_packet *bcast_packet;
1020 int hdr_size = sizeof(struct ethhdr) + sizeof(struct bcast_packet);
1022 /* packet with broadcast indication but unicast recipient */
1023 if (!is_bcast(ethhdr->h_dest))
1026 /* packet with broadcast sender address */
1027 if (is_bcast(ethhdr->h_source))
1030 /* drop packet if it has not necessary minimum size */
1031 if (result < hdr_size)
1034 /* ignore broadcasts sent by myself */
1035 if (is_my_mac(ethhdr->h_source))
1038 bcast_packet = (struct bcast_packet *)
1039 (packet_buff + sizeof(struct ethhdr));
1041 /* ignore broadcasts originated by myself */
1042 if (is_my_mac(bcast_packet->orig))
1045 spin_lock(&orig_hash_lock);
1046 orig_node = ((struct orig_node *)
1047 hash_find(orig_hash, bcast_packet->orig));
1049 if (orig_node == NULL) {
1050 spin_unlock(&orig_hash_lock);
1054 /* check flood history */
1055 if (get_bit_status(orig_node->bcast_bits,
1056 orig_node->last_bcast_seqno,
1057 ntohs(bcast_packet->seqno))) {
1058 spin_unlock(&orig_hash_lock);
1062 /* mark broadcast in flood history */
1063 if (bit_get_packet(orig_node->bcast_bits,
1064 ntohs(bcast_packet->seqno) -
1065 orig_node->last_bcast_seqno, 1))
1066 orig_node->last_bcast_seqno = ntohs(bcast_packet->seqno);
1068 spin_unlock(&orig_hash_lock);
1070 /* broadcast for me */
1071 interface_rx(soft_device, packet_buff + hdr_size, result - hdr_size);
1073 /* rebroadcast packet */
1074 add_bcast_packet_to_list(packet_buff + sizeof(struct ethhdr),
1075 result - sizeof(struct ethhdr));
1078 static void recv_vis_packet(struct ethhdr *ethhdr,
1079 unsigned char *packet_buff,
1082 struct vis_packet *vis_packet;
1083 int hdr_size = sizeof(struct ethhdr) + sizeof(struct vis_packet);
1086 /* drop if too short. */
1087 if (result < hdr_size)
1091 if (!is_my_mac(ethhdr->h_dest))
1094 vis_packet = (struct vis_packet *)(packet_buff + sizeof(struct ethhdr));
1095 vis_info_len = result - hdr_size;
1097 /* ignore own packets */
1098 if (is_my_mac(vis_packet->vis_orig))
1101 if (is_my_mac(vis_packet->sender_orig))
1104 switch (vis_packet->vis_type) {
1105 case VIS_TYPE_SERVER_SYNC:
1106 receive_server_sync_packet(vis_packet, vis_info_len);
1109 case VIS_TYPE_CLIENT_UPDATE:
1110 receive_client_update_packet(vis_packet, vis_info_len);
1113 default: /* ignore unknown packet */
1118 static int recv_one_packet(struct batman_if *batman_if,
1119 unsigned char *packet_buff)
1122 struct ethhdr *ethhdr;
1123 struct batman_packet *batman_packet;
1125 result = receive_raw_packet(batman_if->raw_sock, packet_buff,
1130 if (result < sizeof(struct ethhdr) + 2)
1133 ethhdr = (struct ethhdr *)packet_buff;
1134 batman_packet = (struct batman_packet *)
1135 (packet_buff + sizeof(struct ethhdr));
1137 if (batman_packet->version != COMPAT_VERSION) {
1139 "Drop packet: incompatible batman version (%i)\n",
1140 batman_packet->version);
1144 switch (batman_packet->packet_type) {
1145 /* batman originator packet */
1147 recv_bat_packet(ethhdr, packet_buff, result, batman_if);
1150 /* batman icmp packet */
1152 recv_icmp_packet(ethhdr, packet_buff, result, batman_if);
1155 /* unicast packet */
1157 recv_unicast_packet(ethhdr, packet_buff, result, batman_if);
1160 /* broadcast packet */
1162 recv_bcast_packet(ethhdr,
1163 packet_buff, result, batman_if);
1168 recv_vis_packet(ethhdr, packet_buff, result);
1175 static int discard_one_packet(struct batman_if *batman_if,
1176 unsigned char *packet_buff)
1178 int result = -EAGAIN;
1180 if (batman_if->raw_sock) {
1181 result = receive_raw_packet(batman_if->raw_sock,
1189 static bool is_interface_active(struct batman_if *batman_if)
1191 if (batman_if->if_active != IF_ACTIVE)
1197 static void service_interface(struct batman_if *batman_if,
1198 unsigned char *packet_buff)
1204 if (is_interface_active(batman_if))
1205 result = recv_one_packet(batman_if, packet_buff);
1207 result = discard_one_packet(batman_if, packet_buff);
1208 } while (result >= 0);
1210 /* we perform none blocking reads, so EAGAIN indicates there
1211 are no more packets to read. Anything else is a real
1214 if ((result < 0) && (result != -EAGAIN))
1215 printk(KERN_ERR "batman-adv:Could not receive packet from interface %s: %i\n", batman_if->dev, result);
1218 static void service_interfaces(unsigned char *packet_buffer)
1220 struct batman_if *batman_if;
1222 list_for_each_entry_rcu(batman_if, &if_list, list) {
1224 service_interface(batman_if, packet_buffer);
1231 int packet_recv_thread(void *data)
1233 unsigned char *packet_buff;
1235 atomic_set(&data_ready_cond, 0);
1236 atomic_set(&exit_cond, 0);
1237 packet_buff = kmalloc(PACKBUFF_SIZE, GFP_KERNEL);
1239 printk(KERN_ERR"batman-adv:Could allocate memory for the packet buffer. :(\n");
1243 while ((!kthread_should_stop()) && (!atomic_read(&exit_cond))) {
1245 wait_event_interruptible(thread_wait,
1246 (atomic_read(&data_ready_cond) ||
1247 atomic_read(&exit_cond)));
1249 atomic_set(&data_ready_cond, 0);
1251 if (kthread_should_stop() || atomic_read(&exit_cond))
1254 service_interfaces(packet_buff);
1258 /* do not exit until kthread_stop() is actually called,
1259 * otherwise it will wait for us forever. */
1260 while (!kthread_should_stop())
1266 void batman_data_ready(struct sock *sk, int len)
1268 void (*data_ready)(struct sock *, int) = sk->sk_user_data;
1270 data_ready(sk, len);
1272 atomic_set(&data_ready_cond, 1);
1273 wake_up_interruptible(&thread_wait);