Staging: batman-adv: bonding and interface alternating
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / batman-adv / routing.c
blob048795e319a80b83e73b3f06df234ae86d933d71
1 /*
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
18 * 02110-1301, USA
22 #include "main.h"
23 #include "routing.h"
24 #include "send.h"
25 #include "hash.h"
26 #include "soft-interface.h"
27 #include "hard-interface.h"
28 #include "icmp_socket.h"
29 #include "translation-table.h"
30 #include "originator.h"
31 #include "types.h"
32 #include "ring_buffer.h"
33 #include "vis.h"
34 #include "aggregation.h"
36 static DECLARE_WAIT_QUEUE_HEAD(thread_wait);
38 void slide_own_bcast_window(struct batman_if *batman_if)
40 HASHIT(hashit);
41 struct orig_node *orig_node;
42 TYPE_OF_WORD *word;
43 unsigned long flags;
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)
80 /* route deleted */
81 if ((orig_node->router != NULL) && (neigh_node == NULL)) {
83 bat_dbg(DBG_ROUTES, "Deleting route towards: %pM\n",
84 orig_node->orig);
85 hna_global_del_orig(orig_node, "originator timed out");
87 /* route added */
88 } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
90 bat_dbg(DBG_ROUTES,
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);
95 /* route changed */
96 } else {
97 bat_dbg(DBG_ROUTES,
98 "Changing route towards: %pM "
99 "(now via %pM - was via %pM)\n",
100 orig_node->orig, neigh_node->addr,
101 orig_node->router->addr);
104 orig_node->router = neigh_node;
108 void update_routes(struct orig_node *orig_node,
109 struct neigh_node *neigh_node,
110 unsigned char *hna_buff, int hna_buff_len)
113 if (orig_node == NULL)
114 return;
116 if (orig_node->router != neigh_node)
117 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
118 /* may be just HNA changed */
119 else
120 update_HNA(orig_node, hna_buff, hna_buff_len);
123 static int isBidirectionalNeigh(struct orig_node *orig_node,
124 struct orig_node *orig_neigh_node,
125 struct batman_packet *batman_packet,
126 struct batman_if *if_incoming)
128 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
129 unsigned char total_count;
131 if (orig_node == orig_neigh_node) {
132 list_for_each_entry(tmp_neigh_node,
133 &orig_node->neigh_list,
134 list) {
136 if (compare_orig(tmp_neigh_node->addr,
137 orig_neigh_node->orig) &&
138 (tmp_neigh_node->if_incoming == if_incoming))
139 neigh_node = tmp_neigh_node;
142 if (!neigh_node)
143 neigh_node = create_neighbor(orig_node,
144 orig_neigh_node,
145 orig_neigh_node->orig,
146 if_incoming);
147 /* create_neighbor failed, return 0 */
148 if (!neigh_node)
149 return 0;
151 neigh_node->last_valid = jiffies;
152 } else {
153 /* find packet count of corresponding one hop neighbor */
154 list_for_each_entry(tmp_neigh_node,
155 &orig_neigh_node->neigh_list, list) {
157 if (compare_orig(tmp_neigh_node->addr,
158 orig_neigh_node->orig) &&
159 (tmp_neigh_node->if_incoming == if_incoming))
160 neigh_node = tmp_neigh_node;
163 if (!neigh_node)
164 neigh_node = create_neighbor(orig_neigh_node,
165 orig_neigh_node,
166 orig_neigh_node->orig,
167 if_incoming);
168 /* create_neighbor failed, return 0 */
169 if (!neigh_node)
170 return 0;
173 orig_node->last_valid = jiffies;
175 /* pay attention to not get a value bigger than 100 % */
176 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
177 neigh_node->real_packet_count ?
178 neigh_node->real_packet_count :
179 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
181 /* if we have too few packets (too less data) we set tq_own to zero */
182 /* if we receive too few packets it is not considered bidirectional */
183 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
184 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
185 orig_neigh_node->tq_own = 0;
186 else
187 /* neigh_node->real_packet_count is never zero as we
188 * only purge old information when getting new
189 * information */
190 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
191 neigh_node->real_packet_count;
194 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
195 * affect the nearly-symmetric links only a little, but
196 * punishes asymmetric links more. This will give a value
197 * between 0 and TQ_MAX_VALUE
199 orig_neigh_node->tq_asym_penalty =
200 TQ_MAX_VALUE -
201 (TQ_MAX_VALUE *
202 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
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 *
206 TQ_LOCAL_WINDOW_SIZE *
207 TQ_LOCAL_WINDOW_SIZE);
209 batman_packet->tq = ((batman_packet->tq *
210 orig_neigh_node->tq_own *
211 orig_neigh_node->tq_asym_penalty) /
212 (TQ_MAX_VALUE * TQ_MAX_VALUE));
214 bat_dbg(DBG_BATMAN,
215 "bidirectional: "
216 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
217 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
218 "total tq: %3i\n",
219 orig_node->orig, orig_neigh_node->orig, total_count,
220 neigh_node->real_packet_count, orig_neigh_node->tq_own,
221 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
223 /* if link has the minimum required transmission quality
224 * consider it bidirectional */
225 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
226 return 1;
228 return 0;
231 static void update_orig(struct orig_node *orig_node, struct ethhdr *ethhdr,
232 struct batman_packet *batman_packet,
233 struct batman_if *if_incoming,
234 unsigned char *hna_buff, int hna_buff_len,
235 char is_duplicate)
237 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
238 int tmp_hna_buff_len;
240 bat_dbg(DBG_BATMAN, "update_originator(): "
241 "Searching and updating originator entry of received packet\n");
243 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
244 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
245 (tmp_neigh_node->if_incoming == if_incoming)) {
246 neigh_node = tmp_neigh_node;
247 continue;
250 if (is_duplicate)
251 continue;
253 ring_buffer_set(tmp_neigh_node->tq_recv,
254 &tmp_neigh_node->tq_index, 0);
255 tmp_neigh_node->tq_avg =
256 ring_buffer_avg(tmp_neigh_node->tq_recv);
259 if (!neigh_node) {
260 struct orig_node *orig_tmp;
262 orig_tmp = get_orig_node(ethhdr->h_source);
263 if (!orig_tmp)
264 return;
266 neigh_node = create_neighbor(orig_node,
267 orig_tmp,
268 ethhdr->h_source, if_incoming);
269 if (!neigh_node)
270 return;
271 } else
272 bat_dbg(DBG_BATMAN,
273 "Updating existing last-hop neighbor of originator\n");
275 orig_node->flags = batman_packet->flags;
276 neigh_node->last_valid = jiffies;
278 ring_buffer_set(neigh_node->tq_recv,
279 &neigh_node->tq_index,
280 batman_packet->tq);
281 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
283 if (!is_duplicate) {
284 orig_node->last_ttl = batman_packet->ttl;
285 neigh_node->last_ttl = batman_packet->ttl;
288 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
289 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
291 /* if this neighbor already is our next hop there is nothing
292 * to change */
293 if (orig_node->router == neigh_node)
294 goto update_hna;
296 /* if this neighbor does not offer a better TQ we won't consider it */
297 if ((orig_node->router) &&
298 (orig_node->router->tq_avg > neigh_node->tq_avg))
299 goto update_hna;
301 /* if the TQ is the same and the link not more symetric we
302 * won't consider it either */
303 if ((orig_node->router) &&
304 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
305 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
306 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
307 goto update_hna;
309 update_routes(orig_node, neigh_node, hna_buff, tmp_hna_buff_len);
310 return;
312 update_hna:
313 update_routes(orig_node, orig_node->router, hna_buff, tmp_hna_buff_len);
316 /* checks whether the host restarted and is in the protection time.
317 * returns:
318 * 0 if the packet is to be accepted
319 * 1 if the packet is to be ignored.
321 static int window_protected(int32_t seq_num_diff,
322 unsigned long *last_reset)
324 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
325 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
326 if (time_after(jiffies, *last_reset +
327 msecs_to_jiffies(RESET_PROTECTION_MS))) {
329 *last_reset = jiffies;
330 bat_dbg(DBG_BATMAN,
331 "old packet received, start protection\n");
333 return 0;
334 } else
335 return 1;
337 return 0;
340 /* processes a batman packet for all interfaces, adjusts the sequence number and
341 * finds out whether it is a duplicate.
342 * returns:
343 * 1 the packet is a duplicate
344 * 0 the packet has not yet been received
345 * -1 the packet is old and has been received while the seqno window
346 * was protected. Caller should drop it.
348 static char count_real_packets(struct ethhdr *ethhdr,
349 struct batman_packet *batman_packet,
350 struct batman_if *if_incoming)
352 struct orig_node *orig_node;
353 struct neigh_node *tmp_neigh_node;
354 char is_duplicate = 0;
355 int32_t seq_diff;
356 int need_update = 0;
357 int set_mark;
359 orig_node = get_orig_node(batman_packet->orig);
360 if (orig_node == NULL)
361 return 0;
363 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
365 /* signalize caller that the packet is to be dropped. */
366 if (window_protected(seq_diff, &orig_node->batman_seqno_reset))
367 return -1;
369 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
371 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
372 orig_node->last_real_seqno,
373 batman_packet->seqno);
375 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
376 (tmp_neigh_node->if_incoming == if_incoming))
377 set_mark = 1;
378 else
379 set_mark = 0;
381 /* if the window moved, set the update flag. */
382 need_update |= bit_get_packet(tmp_neigh_node->real_bits,
383 seq_diff, set_mark);
385 tmp_neigh_node->real_packet_count =
386 bit_packet_count(tmp_neigh_node->real_bits);
389 if (need_update) {
390 bat_dbg(DBG_BATMAN, "updating last_seqno: old %d, new %d\n",
391 orig_node->last_real_seqno, batman_packet->seqno);
392 orig_node->last_real_seqno = batman_packet->seqno;
395 return is_duplicate;
398 /* copy primary address for bonding */
399 static void mark_bonding_address(struct bat_priv *bat_priv,
400 struct orig_node *orig_node,
401 struct orig_node *orig_neigh_node,
402 struct batman_packet *batman_packet)
405 if (batman_packet->flags & PRIMARIES_FIRST_HOP)
406 memcpy(orig_neigh_node->primary_addr,
407 orig_node->orig, ETH_ALEN);
409 return;
412 /* mark possible bond.candidates in the neighbor list */
413 void update_bonding_candidates(struct bat_priv *bat_priv,
414 struct orig_node *orig_node)
416 int candidates;
417 int interference_candidate;
418 int best_tq;
419 struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
420 struct neigh_node *first_candidate, *last_candidate;
422 /* update the candidates for this originator */
423 if (!orig_node->router) {
424 orig_node->bond.candidates = 0;
425 return;
428 best_tq = orig_node->router->tq_avg;
430 /* update bond.candidates */
432 candidates = 0;
434 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
435 * as "bonding partner" */
437 /* first, zero the list */
438 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
439 tmp_neigh_node->next_bond_candidate = NULL;
442 first_candidate = NULL;
443 last_candidate = NULL;
444 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
446 /* only consider if it has the same primary address ... */
447 if (memcmp(orig_node->orig,
448 tmp_neigh_node->orig_node->primary_addr,
449 ETH_ALEN) != 0)
450 continue;
452 /* ... and is good enough to be considered */
453 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
454 continue;
456 /* check if we have another candidate with the same
457 * mac address or interface. If we do, we won't
458 * select this candidate because of possible interference. */
460 interference_candidate = 0;
461 list_for_each_entry(tmp_neigh_node2,
462 &orig_node->neigh_list, list) {
464 if (tmp_neigh_node2 == tmp_neigh_node)
465 continue;
467 /* we only care if the other candidate is even
468 * considered as candidate. */
469 if (tmp_neigh_node2->next_bond_candidate == NULL)
470 continue;
473 if ((tmp_neigh_node->if_incoming ==
474 tmp_neigh_node2->if_incoming)
475 || (memcmp(tmp_neigh_node->addr,
476 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
478 interference_candidate = 1;
479 break;
482 /* don't care further if it is an interference candidate */
483 if (interference_candidate)
484 continue;
486 if (first_candidate == NULL) {
487 first_candidate = tmp_neigh_node;
488 tmp_neigh_node->next_bond_candidate = first_candidate;
489 } else
490 tmp_neigh_node->next_bond_candidate = last_candidate;
492 last_candidate = tmp_neigh_node;
494 candidates++;
497 if (candidates > 0) {
498 first_candidate->next_bond_candidate = last_candidate;
499 orig_node->bond.selected = first_candidate;
502 orig_node->bond.candidates = candidates;
505 void receive_bat_packet(struct ethhdr *ethhdr,
506 struct batman_packet *batman_packet,
507 unsigned char *hna_buff, int hna_buff_len,
508 struct batman_if *if_incoming)
510 /* FIXME: each orig_node->batman_if will be attached to a softif */
511 struct bat_priv *bat_priv = netdev_priv(soft_device);
512 struct batman_if *batman_if;
513 struct orig_node *orig_neigh_node, *orig_node;
514 char has_directlink_flag;
515 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
516 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
517 char is_duplicate;
518 uint32_t if_incoming_seqno;
520 /* Silently drop when the batman packet is actually not a
521 * correct packet.
523 * This might happen if a packet is padded (e.g. Ethernet has a
524 * minimum frame length of 64 byte) and the aggregation interprets
525 * it as an additional length.
527 * TODO: A more sane solution would be to have a bit in the
528 * batman_packet to detect whether the packet is the last
529 * packet in an aggregation. Here we expect that the padding
530 * is always zero (or not 0x01)
532 if (batman_packet->packet_type != BAT_PACKET)
533 return;
535 /* could be changed by schedule_own_packet() */
536 if_incoming_seqno = atomic_read(&if_incoming->seqno);
538 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
540 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
541 batman_packet->orig) ? 1 : 0);
543 bat_dbg(DBG_BATMAN, "Received BATMAN packet via NB: %pM, IF: %s [%s] "
544 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
545 "TTL %d, V %d, IDF %d)\n",
546 ethhdr->h_source, if_incoming->dev, if_incoming->addr_str,
547 batman_packet->orig, batman_packet->prev_sender,
548 batman_packet->seqno, batman_packet->tq, batman_packet->ttl,
549 batman_packet->version, has_directlink_flag);
551 list_for_each_entry_rcu(batman_if, &if_list, list) {
552 if (batman_if->if_status != IF_ACTIVE)
553 continue;
555 if (compare_orig(ethhdr->h_source,
556 batman_if->net_dev->dev_addr))
557 is_my_addr = 1;
559 if (compare_orig(batman_packet->orig,
560 batman_if->net_dev->dev_addr))
561 is_my_orig = 1;
563 if (compare_orig(batman_packet->prev_sender,
564 batman_if->net_dev->dev_addr))
565 is_my_oldorig = 1;
567 if (compare_orig(ethhdr->h_source, broadcastAddr))
568 is_broadcast = 1;
571 if (batman_packet->version != COMPAT_VERSION) {
572 bat_dbg(DBG_BATMAN,
573 "Drop packet: incompatible batman version (%i)\n",
574 batman_packet->version);
575 return;
578 if (is_my_addr) {
579 bat_dbg(DBG_BATMAN,
580 "Drop packet: received my own broadcast (sender: %pM"
581 ")\n",
582 ethhdr->h_source);
583 return;
586 if (is_broadcast) {
587 bat_dbg(DBG_BATMAN, "Drop packet: "
588 "ignoring all packets with broadcast source addr (sender: %pM"
589 ")\n", ethhdr->h_source);
590 return;
593 if (is_my_orig) {
594 TYPE_OF_WORD *word;
595 int offset;
597 orig_neigh_node = get_orig_node(ethhdr->h_source);
599 if (!orig_neigh_node)
600 return;
602 /* neighbor has to indicate direct link and it has to
603 * come via the corresponding interface */
604 /* if received seqno equals last send seqno save new
605 * seqno for bidirectional check */
606 if (has_directlink_flag &&
607 compare_orig(if_incoming->net_dev->dev_addr,
608 batman_packet->orig) &&
609 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
610 offset = if_incoming->if_num * NUM_WORDS;
611 word = &(orig_neigh_node->bcast_own[offset]);
612 bit_mark(word, 0);
613 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
614 bit_packet_count(word);
617 bat_dbg(DBG_BATMAN, "Drop packet: "
618 "originator packet from myself (via neighbor)\n");
619 return;
622 if (is_my_oldorig) {
623 bat_dbg(DBG_BATMAN,
624 "Drop packet: ignoring all rebroadcast echos (sender: "
625 "%pM)\n", ethhdr->h_source);
626 return;
629 orig_node = get_orig_node(batman_packet->orig);
630 if (orig_node == NULL)
631 return;
633 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
635 if (is_duplicate == -1) {
636 bat_dbg(DBG_BATMAN,
637 "Drop packet: packet within seqno protection time "
638 "(sender: %pM)\n", ethhdr->h_source);
639 return;
642 if (batman_packet->tq == 0) {
643 bat_dbg(DBG_BATMAN,
644 "Drop packet: originator packet with tq equal 0\n");
645 return;
648 /* avoid temporary routing loops */
649 if ((orig_node->router) &&
650 (orig_node->router->orig_node->router) &&
651 (compare_orig(orig_node->router->addr,
652 batman_packet->prev_sender)) &&
653 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
654 (compare_orig(orig_node->router->addr,
655 orig_node->router->orig_node->router->addr))) {
656 bat_dbg(DBG_BATMAN,
657 "Drop packet: ignoring all rebroadcast packets that "
658 "may make me loop (sender: %pM)\n", ethhdr->h_source);
659 return;
662 /* if sender is a direct neighbor the sender mac equals
663 * originator mac */
664 orig_neigh_node = (is_single_hop_neigh ?
665 orig_node : get_orig_node(ethhdr->h_source));
666 if (orig_neigh_node == NULL)
667 return;
669 /* drop packet if sender is not a direct neighbor and if we
670 * don't route towards it */
671 if (!is_single_hop_neigh &&
672 (orig_neigh_node->router == NULL)) {
673 bat_dbg(DBG_BATMAN, "Drop packet: OGM via unknown neighbor!\n");
674 return;
677 is_bidirectional = isBidirectionalNeigh(orig_node, orig_neigh_node,
678 batman_packet, if_incoming);
680 /* update ranking if it is not a duplicate or has the same
681 * seqno and similar ttl as the non-duplicate */
682 if (is_bidirectional &&
683 (!is_duplicate ||
684 ((orig_node->last_real_seqno == batman_packet->seqno) &&
685 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
686 update_orig(orig_node, ethhdr, batman_packet,
687 if_incoming, hna_buff, hna_buff_len, is_duplicate);
689 mark_bonding_address(bat_priv, orig_node,
690 orig_neigh_node, batman_packet);
691 update_bonding_candidates(bat_priv, orig_node);
693 /* is single hop (direct) neighbor */
694 if (is_single_hop_neigh) {
696 /* mark direct link on incoming interface */
697 schedule_forward_packet(orig_node, ethhdr, batman_packet,
698 1, hna_buff_len, if_incoming);
700 bat_dbg(DBG_BATMAN, "Forwarding packet: "
701 "rebroadcast neighbor packet with direct link flag\n");
702 return;
705 /* multihop originator */
706 if (!is_bidirectional) {
707 bat_dbg(DBG_BATMAN,
708 "Drop packet: not received via bidirectional link\n");
709 return;
712 if (is_duplicate) {
713 bat_dbg(DBG_BATMAN, "Drop packet: duplicate packet received\n");
714 return;
717 bat_dbg(DBG_BATMAN,
718 "Forwarding packet: rebroadcast originator packet\n");
719 schedule_forward_packet(orig_node, ethhdr, batman_packet,
720 0, hna_buff_len, if_incoming);
723 int recv_bat_packet(struct sk_buff *skb,
724 struct batman_if *batman_if)
726 struct ethhdr *ethhdr;
727 unsigned long flags;
728 struct sk_buff *skb_old;
730 /* drop packet if it has not necessary minimum size */
731 if (skb_headlen(skb) < sizeof(struct batman_packet))
732 return NET_RX_DROP;
734 ethhdr = (struct ethhdr *)skb_mac_header(skb);
736 /* packet with broadcast indication but unicast recipient */
737 if (!is_bcast(ethhdr->h_dest))
738 return NET_RX_DROP;
740 /* packet with broadcast sender address */
741 if (is_bcast(ethhdr->h_source))
742 return NET_RX_DROP;
744 /* TODO: we use headlen instead of "length", because
745 * only this data is paged in. */
747 /* create a copy of the skb, if needed, to modify it. */
748 if (!skb_clone_writable(skb, skb_headlen(skb))) {
749 skb_old = skb;
750 skb = skb_copy(skb, GFP_ATOMIC);
751 if (!skb)
752 return NET_RX_DROP;
753 ethhdr = (struct ethhdr *)skb_mac_header(skb);
754 kfree_skb(skb_old);
757 spin_lock_irqsave(&orig_hash_lock, flags);
758 receive_aggr_bat_packet(ethhdr,
759 skb->data,
760 skb_headlen(skb),
761 batman_if);
762 spin_unlock_irqrestore(&orig_hash_lock, flags);
764 kfree_skb(skb);
765 return NET_RX_SUCCESS;
768 static int recv_my_icmp_packet(struct sk_buff *skb)
770 struct orig_node *orig_node;
771 struct icmp_packet *icmp_packet;
772 struct ethhdr *ethhdr;
773 struct sk_buff *skb_old;
774 struct batman_if *batman_if;
775 int ret;
776 unsigned long flags;
777 uint8_t dstaddr[ETH_ALEN];
779 icmp_packet = (struct icmp_packet *)skb->data;
780 ethhdr = (struct ethhdr *)skb_mac_header(skb);
782 /* add data to device queue */
783 if (icmp_packet->msg_type != ECHO_REQUEST) {
784 bat_socket_receive_packet(icmp_packet);
785 return NET_RX_DROP;
788 /* answer echo request (ping) */
789 /* get routing information */
790 spin_lock_irqsave(&orig_hash_lock, flags);
791 orig_node = ((struct orig_node *)hash_find(orig_hash,
792 icmp_packet->orig));
793 ret = NET_RX_DROP;
795 if ((orig_node != NULL) &&
796 (orig_node->router != NULL)) {
798 /* don't lock while sending the packets ... we therefore
799 * copy the required data before sending */
800 batman_if = orig_node->router->if_incoming;
801 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
802 spin_unlock_irqrestore(&orig_hash_lock, flags);
804 /* create a copy of the skb, if needed, to modify it. */
805 skb_old = NULL;
806 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
807 skb_old = skb;
808 skb = skb_copy(skb, GFP_ATOMIC);
809 if (!skb)
810 return NET_RX_DROP;
812 icmp_packet = (struct icmp_packet *)skb->data;
813 ethhdr = (struct ethhdr *)skb_mac_header(skb);
814 kfree_skb(skb_old);
817 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
818 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
819 icmp_packet->msg_type = ECHO_REPLY;
820 icmp_packet->ttl = TTL;
822 send_skb_packet(skb, batman_if, dstaddr);
823 ret = NET_RX_SUCCESS;
825 } else
826 spin_unlock_irqrestore(&orig_hash_lock, flags);
828 return ret;
831 static int recv_icmp_ttl_exceeded(struct sk_buff *skb)
833 struct orig_node *orig_node;
834 struct icmp_packet *icmp_packet;
835 struct ethhdr *ethhdr;
836 struct sk_buff *skb_old;
837 struct batman_if *batman_if;
838 int ret;
839 unsigned long flags;
840 uint8_t dstaddr[ETH_ALEN];
842 icmp_packet = (struct icmp_packet *)skb->data;
843 ethhdr = (struct ethhdr *)skb_mac_header(skb);
845 /* send TTL exceeded if packet is an echo request (traceroute) */
846 if (icmp_packet->msg_type != ECHO_REQUEST) {
847 printk(KERN_WARNING "batman-adv:"
848 "Warning - can't forward icmp packet from %pM to %pM: "
849 "ttl exceeded\n",
850 icmp_packet->orig, icmp_packet->dst);
851 return NET_RX_DROP;
854 /* get routing information */
855 spin_lock_irqsave(&orig_hash_lock, flags);
856 orig_node = ((struct orig_node *)
857 hash_find(orig_hash, icmp_packet->orig));
858 ret = NET_RX_DROP;
860 if ((orig_node != NULL) &&
861 (orig_node->router != NULL)) {
863 /* don't lock while sending the packets ... we therefore
864 * copy the required data before sending */
865 batman_if = orig_node->router->if_incoming;
866 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
867 spin_unlock_irqrestore(&orig_hash_lock, flags);
869 /* create a copy of the skb, if needed, to modify it. */
870 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
871 skb_old = skb;
872 skb = skb_copy(skb, GFP_ATOMIC);
873 if (!skb)
874 return NET_RX_DROP;
875 icmp_packet = (struct icmp_packet *) skb->data;
876 ethhdr = (struct ethhdr *)skb_mac_header(skb);
877 kfree_skb(skb_old);
880 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
881 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
882 icmp_packet->msg_type = TTL_EXCEEDED;
883 icmp_packet->ttl = TTL;
885 send_skb_packet(skb, batman_if, dstaddr);
886 ret = NET_RX_SUCCESS;
888 } else
889 spin_unlock_irqrestore(&orig_hash_lock, flags);
891 return ret;
895 int recv_icmp_packet(struct sk_buff *skb)
897 struct icmp_packet *icmp_packet;
898 struct ethhdr *ethhdr;
899 struct orig_node *orig_node;
900 struct sk_buff *skb_old;
901 struct batman_if *batman_if;
902 int hdr_size = sizeof(struct icmp_packet);
903 int ret;
904 unsigned long flags;
905 uint8_t dstaddr[ETH_ALEN];
907 /* drop packet if it has not necessary minimum size */
908 if (skb_headlen(skb) < hdr_size)
909 return NET_RX_DROP;
911 ethhdr = (struct ethhdr *)skb_mac_header(skb);
913 /* packet with unicast indication but broadcast recipient */
914 if (is_bcast(ethhdr->h_dest))
915 return NET_RX_DROP;
917 /* packet with broadcast sender address */
918 if (is_bcast(ethhdr->h_source))
919 return NET_RX_DROP;
921 /* not for me */
922 if (!is_my_mac(ethhdr->h_dest))
923 return NET_RX_DROP;
925 icmp_packet = (struct icmp_packet *)skb->data;
927 /* packet for me */
928 if (is_my_mac(icmp_packet->dst))
929 return recv_my_icmp_packet(skb);
931 /* TTL exceeded */
932 if (icmp_packet->ttl < 2)
933 return recv_icmp_ttl_exceeded(skb);
935 ret = NET_RX_DROP;
937 /* get routing information */
938 spin_lock_irqsave(&orig_hash_lock, flags);
939 orig_node = ((struct orig_node *)
940 hash_find(orig_hash, icmp_packet->dst));
942 if ((orig_node != NULL) &&
943 (orig_node->router != NULL)) {
945 /* don't lock while sending the packets ... we therefore
946 * copy the required data before sending */
947 batman_if = orig_node->router->if_incoming;
948 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
949 spin_unlock_irqrestore(&orig_hash_lock, flags);
951 /* create a copy of the skb, if needed, to modify it. */
952 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
953 skb_old = skb;
954 skb = skb_copy(skb, GFP_ATOMIC);
955 if (!skb)
956 return NET_RX_DROP;
957 icmp_packet = (struct icmp_packet *)skb->data;
958 ethhdr = (struct ethhdr *)skb_mac_header(skb);
959 kfree_skb(skb_old);
962 /* decrement ttl */
963 icmp_packet->ttl--;
965 /* route it */
966 send_skb_packet(skb, batman_if, dstaddr);
967 ret = NET_RX_SUCCESS;
969 } else
970 spin_unlock_irqrestore(&orig_hash_lock, flags);
972 return ret;
975 /* find a suitable router for this originator, and use
976 * bonding if possible. */
977 struct neigh_node *find_router(struct orig_node *orig_node,
978 struct batman_if *recv_if)
980 /* FIXME: each orig_node->batman_if will be attached to a softif */
981 struct bat_priv *bat_priv = netdev_priv(soft_device);
982 struct orig_node *primary_orig_node;
983 struct orig_node *router_orig;
984 struct neigh_node *router, *first_candidate, *best_router;
985 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
986 int bonding_enabled;
988 if (!orig_node)
989 return NULL;
991 if (!orig_node->router)
992 return NULL;
994 /* without bonding, the first node should
995 * always choose the default router. */
997 bonding_enabled = atomic_read(&bat_priv->bonding_enabled);
998 if (!bonding_enabled && (recv_if == NULL))
999 return orig_node->router;
1001 router_orig = orig_node->router->orig_node;
1003 /* if we have something in the primary_addr, we can search
1004 * for a potential bonding candidate. */
1005 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1006 return orig_node->router;
1008 /* find the orig_node which has the primary interface. might
1009 * even be the same as our router_orig in many cases */
1011 if (memcmp(router_orig->primary_addr,
1012 router_orig->orig, ETH_ALEN) == 0) {
1013 primary_orig_node = router_orig;
1014 } else {
1015 primary_orig_node = hash_find(orig_hash,
1016 router_orig->primary_addr);
1017 if (!primary_orig_node)
1018 return orig_node->router;
1021 /* with less than 2 candidates, we can't do any
1022 * bonding and prefer the original router. */
1024 if (primary_orig_node->bond.candidates < 2)
1025 return orig_node->router;
1028 /* all nodes between should choose a candidate which
1029 * is is not on the interface where the packet came
1030 * in. */
1031 first_candidate = primary_orig_node->bond.selected;
1032 router = first_candidate;
1034 if (bonding_enabled) {
1035 /* in the bonding case, send the packets in a round
1036 * robin fashion over the remaining interfaces. */
1037 do {
1038 /* recv_if == NULL on the first node. */
1039 if (router->if_incoming != recv_if)
1040 break;
1042 router = router->next_bond_candidate;
1043 } while (router != first_candidate);
1045 primary_orig_node->bond.selected = router->next_bond_candidate;
1047 } else {
1048 /* if bonding is disabled, use the best of the
1049 * remaining candidates which are not using
1050 * this interface. */
1051 best_router = first_candidate;
1053 do {
1054 /* recv_if == NULL on the first node. */
1055 if ((router->if_incoming != recv_if) &&
1056 (router->tq_avg > best_router->tq_avg))
1057 best_router = router;
1059 router = router->next_bond_candidate;
1060 } while (router != first_candidate);
1062 router = best_router;
1065 return router;
1068 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1070 struct unicast_packet *unicast_packet;
1071 struct orig_node *orig_node;
1072 struct neigh_node *router;
1073 struct ethhdr *ethhdr;
1074 struct batman_if *batman_if;
1075 struct sk_buff *skb_old;
1076 uint8_t dstaddr[ETH_ALEN];
1077 int hdr_size = sizeof(struct unicast_packet);
1078 unsigned long flags;
1080 /* drop packet if it has not necessary minimum size */
1081 if (skb_headlen(skb) < hdr_size)
1082 return NET_RX_DROP;
1084 ethhdr = (struct ethhdr *) skb_mac_header(skb);
1086 /* packet with unicast indication but broadcast recipient */
1087 if (is_bcast(ethhdr->h_dest))
1088 return NET_RX_DROP;
1090 /* packet with broadcast sender address */
1091 if (is_bcast(ethhdr->h_source))
1092 return NET_RX_DROP;
1094 /* not for me */
1095 if (!is_my_mac(ethhdr->h_dest))
1096 return NET_RX_DROP;
1098 unicast_packet = (struct unicast_packet *) skb->data;
1100 /* packet for me */
1101 if (is_my_mac(unicast_packet->dest)) {
1102 interface_rx(skb, hdr_size);
1103 return NET_RX_SUCCESS;
1106 /* TTL exceeded */
1107 if (unicast_packet->ttl < 2) {
1108 printk(KERN_WARNING "batman-adv:Warning - "
1109 "can't forward unicast packet from %pM to %pM: "
1110 "ttl exceeded\n",
1111 ethhdr->h_source, unicast_packet->dest);
1112 return NET_RX_DROP;
1115 /* get routing information */
1116 spin_lock_irqsave(&orig_hash_lock, flags);
1117 orig_node = ((struct orig_node *)
1118 hash_find(orig_hash, unicast_packet->dest));
1120 router = find_router(orig_node, recv_if);
1122 if (!router) {
1123 spin_unlock_irqrestore(&orig_hash_lock, flags);
1124 return NET_RX_DROP;
1127 /* don't lock while sending the packets ... we therefore
1128 * copy the required data before sending */
1130 batman_if = router->if_incoming;
1131 memcpy(dstaddr, router->addr, ETH_ALEN);
1133 spin_unlock_irqrestore(&orig_hash_lock, flags);
1135 /* create a copy of the skb, if needed, to modify it. */
1136 if (!skb_clone_writable(skb, sizeof(struct unicast_packet))) {
1137 skb_old = skb;
1138 skb = skb_copy(skb, GFP_ATOMIC);
1139 if (!skb)
1140 return NET_RX_DROP;
1141 unicast_packet = (struct unicast_packet *) skb->data;
1142 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1143 kfree_skb(skb_old);
1146 /* decrement ttl */
1147 unicast_packet->ttl--;
1149 /* route it */
1150 send_skb_packet(skb, batman_if, dstaddr);
1152 return NET_RX_SUCCESS;
1155 int recv_bcast_packet(struct sk_buff *skb)
1157 struct orig_node *orig_node;
1158 struct bcast_packet *bcast_packet;
1159 struct ethhdr *ethhdr;
1160 int hdr_size = sizeof(struct bcast_packet);
1161 int32_t seq_diff;
1162 unsigned long flags;
1164 /* drop packet if it has not necessary minimum size */
1165 if (skb_headlen(skb) < hdr_size)
1166 return NET_RX_DROP;
1168 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1170 /* packet with broadcast indication but unicast recipient */
1171 if (!is_bcast(ethhdr->h_dest))
1172 return NET_RX_DROP;
1174 /* packet with broadcast sender address */
1175 if (is_bcast(ethhdr->h_source))
1176 return NET_RX_DROP;
1178 /* ignore broadcasts sent by myself */
1179 if (is_my_mac(ethhdr->h_source))
1180 return NET_RX_DROP;
1182 bcast_packet = (struct bcast_packet *)skb->data;
1184 /* ignore broadcasts originated by myself */
1185 if (is_my_mac(bcast_packet->orig))
1186 return NET_RX_DROP;
1188 if (bcast_packet->ttl < 2)
1189 return NET_RX_DROP;
1191 spin_lock_irqsave(&orig_hash_lock, flags);
1192 orig_node = ((struct orig_node *)
1193 hash_find(orig_hash, bcast_packet->orig));
1195 if (orig_node == NULL) {
1196 spin_unlock_irqrestore(&orig_hash_lock, flags);
1197 return NET_RX_DROP;
1200 /* check whether the packet is a duplicate */
1201 if (get_bit_status(orig_node->bcast_bits,
1202 orig_node->last_bcast_seqno,
1203 ntohl(bcast_packet->seqno))) {
1204 spin_unlock_irqrestore(&orig_hash_lock, flags);
1205 return NET_RX_DROP;
1208 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1210 /* check whether the packet is old and the host just restarted. */
1211 if (window_protected(seq_diff, &orig_node->bcast_seqno_reset)) {
1212 spin_unlock_irqrestore(&orig_hash_lock, flags);
1213 return NET_RX_DROP;
1216 /* mark broadcast in flood history, update window position
1217 * if required. */
1218 if (bit_get_packet(orig_node->bcast_bits, seq_diff, 1))
1219 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1221 spin_unlock_irqrestore(&orig_hash_lock, flags);
1222 /* rebroadcast packet */
1223 add_bcast_packet_to_list(skb);
1225 /* broadcast for me */
1226 interface_rx(skb, hdr_size);
1228 return NET_RX_SUCCESS;
1231 int recv_vis_packet(struct sk_buff *skb)
1233 struct vis_packet *vis_packet;
1234 struct ethhdr *ethhdr;
1235 struct bat_priv *bat_priv;
1236 int hdr_size = sizeof(struct vis_packet);
1238 if (skb_headlen(skb) < hdr_size)
1239 return NET_RX_DROP;
1241 vis_packet = (struct vis_packet *) skb->data;
1242 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1244 /* not for me */
1245 if (!is_my_mac(ethhdr->h_dest))
1246 return NET_RX_DROP;
1248 /* ignore own packets */
1249 if (is_my_mac(vis_packet->vis_orig))
1250 return NET_RX_DROP;
1252 if (is_my_mac(vis_packet->sender_orig))
1253 return NET_RX_DROP;
1255 /* FIXME: each batman_if will be attached to a softif */
1256 bat_priv = netdev_priv(soft_device);
1258 switch (vis_packet->vis_type) {
1259 case VIS_TYPE_SERVER_SYNC:
1260 /* TODO: handle fragmented skbs properly */
1261 receive_server_sync_packet(bat_priv, vis_packet,
1262 skb_headlen(skb));
1263 break;
1265 case VIS_TYPE_CLIENT_UPDATE:
1266 /* TODO: handle fragmented skbs properly */
1267 receive_client_update_packet(bat_priv, vis_packet,
1268 skb_headlen(skb));
1269 break;
1271 default: /* ignore unknown packet */
1272 break;
1275 /* We take a copy of the data in the packet, so we should
1276 always free the skbuf. */
1277 return NET_RX_DROP;