batman-adv: remove unused parameters
[linux-2.6.git] / net / batman-adv / routing.c
blobe946dc93b9bd4961d1980d3cdb5b245c139670ec
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"
35 #include "gateway_common.h"
36 #include "gateway_client.h"
37 #include "unicast.h"
39 void slide_own_bcast_window(struct batman_if *batman_if)
41 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
42 struct hashtable_t *hash = bat_priv->orig_hash;
43 struct hlist_node *walk;
44 struct hlist_head *head;
45 struct element_t *bucket;
46 struct orig_node *orig_node;
47 unsigned long *word;
48 int i;
49 size_t word_index;
51 spin_lock_bh(&bat_priv->orig_hash_lock);
53 for (i = 0; i < hash->size; i++) {
54 head = &hash->table[i];
56 hlist_for_each_entry(bucket, walk, head, hlist) {
57 orig_node = bucket->data;
58 word_index = batman_if->if_num * NUM_WORDS;
59 word = &(orig_node->bcast_own[word_index]);
61 bit_get_packet(bat_priv, word, 1, 0);
62 orig_node->bcast_own_sum[batman_if->if_num] =
63 bit_packet_count(word);
67 spin_unlock_bh(&bat_priv->orig_hash_lock);
70 static void update_HNA(struct bat_priv *bat_priv, struct orig_node *orig_node,
71 unsigned char *hna_buff, int hna_buff_len)
73 if ((hna_buff_len != orig_node->hna_buff_len) ||
74 ((hna_buff_len > 0) &&
75 (orig_node->hna_buff_len > 0) &&
76 (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
78 if (orig_node->hna_buff_len > 0)
79 hna_global_del_orig(bat_priv, orig_node,
80 "originator changed hna");
82 if ((hna_buff_len > 0) && (hna_buff))
83 hna_global_add_orig(bat_priv, orig_node,
84 hna_buff, hna_buff_len);
88 static void update_route(struct bat_priv *bat_priv,
89 struct orig_node *orig_node,
90 struct neigh_node *neigh_node,
91 unsigned char *hna_buff, int hna_buff_len)
93 /* route deleted */
94 if ((orig_node->router) && (!neigh_node)) {
96 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
97 orig_node->orig);
98 hna_global_del_orig(bat_priv, orig_node,
99 "originator timed out");
101 /* route added */
102 } else if ((!orig_node->router) && (neigh_node)) {
104 bat_dbg(DBG_ROUTES, bat_priv,
105 "Adding route towards: %pM (via %pM)\n",
106 orig_node->orig, neigh_node->addr);
107 hna_global_add_orig(bat_priv, orig_node,
108 hna_buff, hna_buff_len);
110 /* route changed */
111 } else {
112 bat_dbg(DBG_ROUTES, bat_priv,
113 "Changing route towards: %pM "
114 "(now via %pM - was via %pM)\n",
115 orig_node->orig, neigh_node->addr,
116 orig_node->router->addr);
119 orig_node->router = neigh_node;
123 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
124 struct neigh_node *neigh_node, unsigned char *hna_buff,
125 int hna_buff_len)
128 if (!orig_node)
129 return;
131 if (orig_node->router != neigh_node)
132 update_route(bat_priv, orig_node, neigh_node,
133 hna_buff, hna_buff_len);
134 /* may be just HNA changed */
135 else
136 update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
139 static int is_bidirectional_neigh(struct orig_node *orig_node,
140 struct orig_node *orig_neigh_node,
141 struct batman_packet *batman_packet,
142 struct batman_if *if_incoming)
144 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
145 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
146 unsigned char total_count;
148 if (orig_node == orig_neigh_node) {
149 list_for_each_entry(tmp_neigh_node,
150 &orig_node->neigh_list,
151 list) {
153 if (compare_orig(tmp_neigh_node->addr,
154 orig_neigh_node->orig) &&
155 (tmp_neigh_node->if_incoming == if_incoming))
156 neigh_node = tmp_neigh_node;
159 if (!neigh_node)
160 neigh_node = create_neighbor(orig_node,
161 orig_neigh_node,
162 orig_neigh_node->orig,
163 if_incoming);
164 /* create_neighbor failed, return 0 */
165 if (!neigh_node)
166 return 0;
168 neigh_node->last_valid = jiffies;
169 } else {
170 /* find packet count of corresponding one hop neighbor */
171 list_for_each_entry(tmp_neigh_node,
172 &orig_neigh_node->neigh_list, list) {
174 if (compare_orig(tmp_neigh_node->addr,
175 orig_neigh_node->orig) &&
176 (tmp_neigh_node->if_incoming == if_incoming))
177 neigh_node = tmp_neigh_node;
180 if (!neigh_node)
181 neigh_node = create_neighbor(orig_neigh_node,
182 orig_neigh_node,
183 orig_neigh_node->orig,
184 if_incoming);
185 /* create_neighbor failed, return 0 */
186 if (!neigh_node)
187 return 0;
190 orig_node->last_valid = jiffies;
192 /* pay attention to not get a value bigger than 100 % */
193 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
194 neigh_node->real_packet_count ?
195 neigh_node->real_packet_count :
196 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
198 /* if we have too few packets (too less data) we set tq_own to zero */
199 /* if we receive too few packets it is not considered bidirectional */
200 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
201 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
202 orig_neigh_node->tq_own = 0;
203 else
204 /* neigh_node->real_packet_count is never zero as we
205 * only purge old information when getting new
206 * information */
207 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
208 neigh_node->real_packet_count;
211 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
212 * affect the nearly-symmetric links only a little, but
213 * punishes asymmetric links more. This will give a value
214 * between 0 and TQ_MAX_VALUE
216 orig_neigh_node->tq_asym_penalty =
217 TQ_MAX_VALUE -
218 (TQ_MAX_VALUE *
219 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
220 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
221 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
222 (TQ_LOCAL_WINDOW_SIZE *
223 TQ_LOCAL_WINDOW_SIZE *
224 TQ_LOCAL_WINDOW_SIZE);
226 batman_packet->tq = ((batman_packet->tq *
227 orig_neigh_node->tq_own *
228 orig_neigh_node->tq_asym_penalty) /
229 (TQ_MAX_VALUE * TQ_MAX_VALUE));
231 bat_dbg(DBG_BATMAN, bat_priv,
232 "bidirectional: "
233 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
234 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
235 "total tq: %3i\n",
236 orig_node->orig, orig_neigh_node->orig, total_count,
237 neigh_node->real_packet_count, orig_neigh_node->tq_own,
238 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
240 /* if link has the minimum required transmission quality
241 * consider it bidirectional */
242 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
243 return 1;
245 return 0;
248 static void update_orig(struct bat_priv *bat_priv,
249 struct orig_node *orig_node,
250 struct ethhdr *ethhdr,
251 struct batman_packet *batman_packet,
252 struct batman_if *if_incoming,
253 unsigned char *hna_buff, int hna_buff_len,
254 char is_duplicate)
256 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
257 int tmp_hna_buff_len;
259 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
260 "Searching and updating originator entry of received packet\n");
262 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
263 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
264 (tmp_neigh_node->if_incoming == if_incoming)) {
265 neigh_node = tmp_neigh_node;
266 continue;
269 if (is_duplicate)
270 continue;
272 ring_buffer_set(tmp_neigh_node->tq_recv,
273 &tmp_neigh_node->tq_index, 0);
274 tmp_neigh_node->tq_avg =
275 ring_buffer_avg(tmp_neigh_node->tq_recv);
278 if (!neigh_node) {
279 struct orig_node *orig_tmp;
281 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
282 if (!orig_tmp)
283 return;
285 neigh_node = create_neighbor(orig_node, orig_tmp,
286 ethhdr->h_source, if_incoming);
287 if (!neigh_node)
288 return;
289 } else
290 bat_dbg(DBG_BATMAN, bat_priv,
291 "Updating existing last-hop neighbor of originator\n");
293 orig_node->flags = batman_packet->flags;
294 neigh_node->last_valid = jiffies;
296 ring_buffer_set(neigh_node->tq_recv,
297 &neigh_node->tq_index,
298 batman_packet->tq);
299 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
301 if (!is_duplicate) {
302 orig_node->last_ttl = batman_packet->ttl;
303 neigh_node->last_ttl = batman_packet->ttl;
306 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
307 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
309 /* if this neighbor already is our next hop there is nothing
310 * to change */
311 if (orig_node->router == neigh_node)
312 goto update_hna;
314 /* if this neighbor does not offer a better TQ we won't consider it */
315 if ((orig_node->router) &&
316 (orig_node->router->tq_avg > neigh_node->tq_avg))
317 goto update_hna;
319 /* if the TQ is the same and the link not more symetric we
320 * won't consider it either */
321 if ((orig_node->router) &&
322 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
323 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
324 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
325 goto update_hna;
327 update_routes(bat_priv, orig_node, neigh_node,
328 hna_buff, tmp_hna_buff_len);
329 goto update_gw;
331 update_hna:
332 update_routes(bat_priv, orig_node, orig_node->router,
333 hna_buff, tmp_hna_buff_len);
335 update_gw:
336 if (orig_node->gw_flags != batman_packet->gw_flags)
337 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
339 orig_node->gw_flags = batman_packet->gw_flags;
341 /* restart gateway selection if fast or late switching was enabled */
342 if ((orig_node->gw_flags) &&
343 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
344 (atomic_read(&bat_priv->gw_sel_class) > 2))
345 gw_check_election(bat_priv, orig_node);
348 /* checks whether the host restarted and is in the protection time.
349 * returns:
350 * 0 if the packet is to be accepted
351 * 1 if the packet is to be ignored.
353 static int window_protected(struct bat_priv *bat_priv,
354 int32_t seq_num_diff,
355 unsigned long *last_reset)
357 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
358 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
359 if (time_after(jiffies, *last_reset +
360 msecs_to_jiffies(RESET_PROTECTION_MS))) {
362 *last_reset = jiffies;
363 bat_dbg(DBG_BATMAN, bat_priv,
364 "old packet received, start protection\n");
366 return 0;
367 } else
368 return 1;
370 return 0;
373 /* processes a batman packet for all interfaces, adjusts the sequence number and
374 * finds out whether it is a duplicate.
375 * returns:
376 * 1 the packet is a duplicate
377 * 0 the packet has not yet been received
378 * -1 the packet is old and has been received while the seqno window
379 * was protected. Caller should drop it.
381 static char count_real_packets(struct ethhdr *ethhdr,
382 struct batman_packet *batman_packet,
383 struct batman_if *if_incoming)
385 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
386 struct orig_node *orig_node;
387 struct neigh_node *tmp_neigh_node;
388 char is_duplicate = 0;
389 int32_t seq_diff;
390 int need_update = 0;
391 int set_mark;
393 orig_node = get_orig_node(bat_priv, batman_packet->orig);
394 if (!orig_node)
395 return 0;
397 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
399 /* signalize caller that the packet is to be dropped. */
400 if (window_protected(bat_priv, seq_diff,
401 &orig_node->batman_seqno_reset))
402 return -1;
404 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
406 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
407 orig_node->last_real_seqno,
408 batman_packet->seqno);
410 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
411 (tmp_neigh_node->if_incoming == if_incoming))
412 set_mark = 1;
413 else
414 set_mark = 0;
416 /* if the window moved, set the update flag. */
417 need_update |= bit_get_packet(bat_priv,
418 tmp_neigh_node->real_bits,
419 seq_diff, set_mark);
421 tmp_neigh_node->real_packet_count =
422 bit_packet_count(tmp_neigh_node->real_bits);
425 if (need_update) {
426 bat_dbg(DBG_BATMAN, bat_priv,
427 "updating last_seqno: old %d, new %d\n",
428 orig_node->last_real_seqno, batman_packet->seqno);
429 orig_node->last_real_seqno = batman_packet->seqno;
432 return is_duplicate;
435 /* copy primary address for bonding */
436 static void mark_bonding_address(struct orig_node *orig_node,
437 struct orig_node *orig_neigh_node,
438 struct batman_packet *batman_packet)
441 if (batman_packet->flags & PRIMARIES_FIRST_HOP)
442 memcpy(orig_neigh_node->primary_addr,
443 orig_node->orig, ETH_ALEN);
445 return;
448 /* mark possible bond.candidates in the neighbor list */
449 void update_bonding_candidates(struct orig_node *orig_node)
451 int candidates;
452 int interference_candidate;
453 int best_tq;
454 struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
455 struct neigh_node *first_candidate, *last_candidate;
457 /* update the candidates for this originator */
458 if (!orig_node->router) {
459 orig_node->bond.candidates = 0;
460 return;
463 best_tq = orig_node->router->tq_avg;
465 /* update bond.candidates */
467 candidates = 0;
469 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
470 * as "bonding partner" */
472 /* first, zero the list */
473 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
474 tmp_neigh_node->next_bond_candidate = NULL;
477 first_candidate = NULL;
478 last_candidate = NULL;
479 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
481 /* only consider if it has the same primary address ... */
482 if (memcmp(orig_node->orig,
483 tmp_neigh_node->orig_node->primary_addr,
484 ETH_ALEN) != 0)
485 continue;
487 /* ... and is good enough to be considered */
488 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
489 continue;
491 /* check if we have another candidate with the same
492 * mac address or interface. If we do, we won't
493 * select this candidate because of possible interference. */
495 interference_candidate = 0;
496 list_for_each_entry(tmp_neigh_node2,
497 &orig_node->neigh_list, list) {
499 if (tmp_neigh_node2 == tmp_neigh_node)
500 continue;
502 /* we only care if the other candidate is even
503 * considered as candidate. */
504 if (!tmp_neigh_node2->next_bond_candidate)
505 continue;
508 if ((tmp_neigh_node->if_incoming ==
509 tmp_neigh_node2->if_incoming)
510 || (memcmp(tmp_neigh_node->addr,
511 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
513 interference_candidate = 1;
514 break;
517 /* don't care further if it is an interference candidate */
518 if (interference_candidate)
519 continue;
521 if (!first_candidate) {
522 first_candidate = tmp_neigh_node;
523 tmp_neigh_node->next_bond_candidate = first_candidate;
524 } else
525 tmp_neigh_node->next_bond_candidate = last_candidate;
527 last_candidate = tmp_neigh_node;
529 candidates++;
532 if (candidates > 0) {
533 first_candidate->next_bond_candidate = last_candidate;
534 orig_node->bond.selected = first_candidate;
537 orig_node->bond.candidates = candidates;
540 void receive_bat_packet(struct ethhdr *ethhdr,
541 struct batman_packet *batman_packet,
542 unsigned char *hna_buff, int hna_buff_len,
543 struct batman_if *if_incoming)
545 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
546 struct batman_if *batman_if;
547 struct orig_node *orig_neigh_node, *orig_node;
548 char has_directlink_flag;
549 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
550 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
551 char is_duplicate;
552 uint32_t if_incoming_seqno;
554 /* Silently drop when the batman packet is actually not a
555 * correct packet.
557 * This might happen if a packet is padded (e.g. Ethernet has a
558 * minimum frame length of 64 byte) and the aggregation interprets
559 * it as an additional length.
561 * TODO: A more sane solution would be to have a bit in the
562 * batman_packet to detect whether the packet is the last
563 * packet in an aggregation. Here we expect that the padding
564 * is always zero (or not 0x01)
566 if (batman_packet->packet_type != BAT_PACKET)
567 return;
569 /* could be changed by schedule_own_packet() */
570 if_incoming_seqno = atomic_read(&if_incoming->seqno);
572 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
574 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
575 batman_packet->orig) ? 1 : 0);
577 bat_dbg(DBG_BATMAN, bat_priv,
578 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
579 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
580 "TTL %d, V %d, IDF %d)\n",
581 ethhdr->h_source, if_incoming->net_dev->name,
582 if_incoming->net_dev->dev_addr, batman_packet->orig,
583 batman_packet->prev_sender, batman_packet->seqno,
584 batman_packet->tq, batman_packet->ttl, batman_packet->version,
585 has_directlink_flag);
587 rcu_read_lock();
588 list_for_each_entry_rcu(batman_if, &if_list, list) {
589 if (batman_if->if_status != IF_ACTIVE)
590 continue;
592 if (batman_if->soft_iface != if_incoming->soft_iface)
593 continue;
595 if (compare_orig(ethhdr->h_source,
596 batman_if->net_dev->dev_addr))
597 is_my_addr = 1;
599 if (compare_orig(batman_packet->orig,
600 batman_if->net_dev->dev_addr))
601 is_my_orig = 1;
603 if (compare_orig(batman_packet->prev_sender,
604 batman_if->net_dev->dev_addr))
605 is_my_oldorig = 1;
607 if (compare_orig(ethhdr->h_source, broadcast_addr))
608 is_broadcast = 1;
610 rcu_read_unlock();
612 if (batman_packet->version != COMPAT_VERSION) {
613 bat_dbg(DBG_BATMAN, bat_priv,
614 "Drop packet: incompatible batman version (%i)\n",
615 batman_packet->version);
616 return;
619 if (is_my_addr) {
620 bat_dbg(DBG_BATMAN, bat_priv,
621 "Drop packet: received my own broadcast (sender: %pM"
622 ")\n",
623 ethhdr->h_source);
624 return;
627 if (is_broadcast) {
628 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
629 "ignoring all packets with broadcast source addr (sender: %pM"
630 ")\n", ethhdr->h_source);
631 return;
634 if (is_my_orig) {
635 unsigned long *word;
636 int offset;
638 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
640 if (!orig_neigh_node)
641 return;
643 /* neighbor has to indicate direct link and it has to
644 * come via the corresponding interface */
645 /* if received seqno equals last send seqno save new
646 * seqno for bidirectional check */
647 if (has_directlink_flag &&
648 compare_orig(if_incoming->net_dev->dev_addr,
649 batman_packet->orig) &&
650 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
651 offset = if_incoming->if_num * NUM_WORDS;
652 word = &(orig_neigh_node->bcast_own[offset]);
653 bit_mark(word, 0);
654 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
655 bit_packet_count(word);
658 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
659 "originator packet from myself (via neighbor)\n");
660 return;
663 if (is_my_oldorig) {
664 bat_dbg(DBG_BATMAN, bat_priv,
665 "Drop packet: ignoring all rebroadcast echos (sender: "
666 "%pM)\n", ethhdr->h_source);
667 return;
670 orig_node = get_orig_node(bat_priv, batman_packet->orig);
671 if (!orig_node)
672 return;
674 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
676 if (is_duplicate == -1) {
677 bat_dbg(DBG_BATMAN, bat_priv,
678 "Drop packet: packet within seqno protection time "
679 "(sender: %pM)\n", ethhdr->h_source);
680 return;
683 if (batman_packet->tq == 0) {
684 bat_dbg(DBG_BATMAN, bat_priv,
685 "Drop packet: originator packet with tq equal 0\n");
686 return;
689 /* avoid temporary routing loops */
690 if ((orig_node->router) &&
691 (orig_node->router->orig_node->router) &&
692 (compare_orig(orig_node->router->addr,
693 batman_packet->prev_sender)) &&
694 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
695 (compare_orig(orig_node->router->addr,
696 orig_node->router->orig_node->router->addr))) {
697 bat_dbg(DBG_BATMAN, bat_priv,
698 "Drop packet: ignoring all rebroadcast packets that "
699 "may make me loop (sender: %pM)\n", ethhdr->h_source);
700 return;
703 /* if sender is a direct neighbor the sender mac equals
704 * originator mac */
705 orig_neigh_node = (is_single_hop_neigh ?
706 orig_node :
707 get_orig_node(bat_priv, ethhdr->h_source));
708 if (!orig_neigh_node)
709 return;
711 /* drop packet if sender is not a direct neighbor and if we
712 * don't route towards it */
713 if (!is_single_hop_neigh && (!orig_neigh_node->router)) {
714 bat_dbg(DBG_BATMAN, bat_priv,
715 "Drop packet: OGM via unknown neighbor!\n");
716 return;
719 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
720 batman_packet, if_incoming);
722 /* update ranking if it is not a duplicate or has the same
723 * seqno and similar ttl as the non-duplicate */
724 if (is_bidirectional &&
725 (!is_duplicate ||
726 ((orig_node->last_real_seqno == batman_packet->seqno) &&
727 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
728 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
729 if_incoming, hna_buff, hna_buff_len, is_duplicate);
731 mark_bonding_address(orig_node, orig_neigh_node, batman_packet);
732 update_bonding_candidates(orig_node);
734 /* is single hop (direct) neighbor */
735 if (is_single_hop_neigh) {
737 /* mark direct link on incoming interface */
738 schedule_forward_packet(orig_node, ethhdr, batman_packet,
739 1, hna_buff_len, if_incoming);
741 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
742 "rebroadcast neighbor packet with direct link flag\n");
743 return;
746 /* multihop originator */
747 if (!is_bidirectional) {
748 bat_dbg(DBG_BATMAN, bat_priv,
749 "Drop packet: not received via bidirectional link\n");
750 return;
753 if (is_duplicate) {
754 bat_dbg(DBG_BATMAN, bat_priv,
755 "Drop packet: duplicate packet received\n");
756 return;
759 bat_dbg(DBG_BATMAN, bat_priv,
760 "Forwarding packet: rebroadcast originator packet\n");
761 schedule_forward_packet(orig_node, ethhdr, batman_packet,
762 0, hna_buff_len, if_incoming);
765 int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
767 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
768 struct ethhdr *ethhdr;
770 /* drop packet if it has not necessary minimum size */
771 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
772 return NET_RX_DROP;
774 ethhdr = (struct ethhdr *)skb_mac_header(skb);
776 /* packet with broadcast indication but unicast recipient */
777 if (!is_broadcast_ether_addr(ethhdr->h_dest))
778 return NET_RX_DROP;
780 /* packet with broadcast sender address */
781 if (is_broadcast_ether_addr(ethhdr->h_source))
782 return NET_RX_DROP;
784 /* create a copy of the skb, if needed, to modify it. */
785 if (skb_cow(skb, 0) < 0)
786 return NET_RX_DROP;
788 /* keep skb linear */
789 if (skb_linearize(skb) < 0)
790 return NET_RX_DROP;
792 ethhdr = (struct ethhdr *)skb_mac_header(skb);
794 spin_lock_bh(&bat_priv->orig_hash_lock);
795 receive_aggr_bat_packet(ethhdr,
796 skb->data,
797 skb_headlen(skb),
798 batman_if);
799 spin_unlock_bh(&bat_priv->orig_hash_lock);
801 kfree_skb(skb);
802 return NET_RX_SUCCESS;
805 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
806 struct sk_buff *skb, size_t icmp_len)
808 struct orig_node *orig_node;
809 struct icmp_packet_rr *icmp_packet;
810 struct ethhdr *ethhdr;
811 struct batman_if *batman_if;
812 int ret;
813 uint8_t dstaddr[ETH_ALEN];
815 icmp_packet = (struct icmp_packet_rr *)skb->data;
816 ethhdr = (struct ethhdr *)skb_mac_header(skb);
818 /* add data to device queue */
819 if (icmp_packet->msg_type != ECHO_REQUEST) {
820 bat_socket_receive_packet(icmp_packet, icmp_len);
821 return NET_RX_DROP;
824 if (!bat_priv->primary_if)
825 return NET_RX_DROP;
827 /* answer echo request (ping) */
828 /* get routing information */
829 spin_lock_bh(&bat_priv->orig_hash_lock);
830 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
831 compare_orig, choose_orig,
832 icmp_packet->orig));
833 ret = NET_RX_DROP;
835 if ((orig_node) && (orig_node->router)) {
837 /* don't lock while sending the packets ... we therefore
838 * copy the required data before sending */
839 batman_if = orig_node->router->if_incoming;
840 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
841 spin_unlock_bh(&bat_priv->orig_hash_lock);
843 /* create a copy of the skb, if needed, to modify it. */
844 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
845 return NET_RX_DROP;
847 icmp_packet = (struct icmp_packet_rr *)skb->data;
848 ethhdr = (struct ethhdr *)skb_mac_header(skb);
850 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
851 memcpy(icmp_packet->orig,
852 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
853 icmp_packet->msg_type = ECHO_REPLY;
854 icmp_packet->ttl = TTL;
856 send_skb_packet(skb, batman_if, dstaddr);
857 ret = NET_RX_SUCCESS;
859 } else
860 spin_unlock_bh(&bat_priv->orig_hash_lock);
862 return ret;
865 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
866 struct sk_buff *skb)
868 struct orig_node *orig_node;
869 struct icmp_packet *icmp_packet;
870 struct ethhdr *ethhdr;
871 struct batman_if *batman_if;
872 int ret;
873 uint8_t dstaddr[ETH_ALEN];
875 icmp_packet = (struct icmp_packet *)skb->data;
876 ethhdr = (struct ethhdr *)skb_mac_header(skb);
878 /* send TTL exceeded if packet is an echo request (traceroute) */
879 if (icmp_packet->msg_type != ECHO_REQUEST) {
880 pr_debug("Warning - can't forward icmp packet from %pM to "
881 "%pM: ttl exceeded\n", icmp_packet->orig,
882 icmp_packet->dst);
883 return NET_RX_DROP;
886 if (!bat_priv->primary_if)
887 return NET_RX_DROP;
889 /* get routing information */
890 spin_lock_bh(&bat_priv->orig_hash_lock);
891 orig_node = ((struct orig_node *)
892 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
893 icmp_packet->orig));
894 ret = NET_RX_DROP;
896 if ((orig_node) && (orig_node->router)) {
898 /* don't lock while sending the packets ... we therefore
899 * copy the required data before sending */
900 batman_if = orig_node->router->if_incoming;
901 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
902 spin_unlock_bh(&bat_priv->orig_hash_lock);
904 /* create a copy of the skb, if needed, to modify it. */
905 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
906 return NET_RX_DROP;
908 icmp_packet = (struct icmp_packet *) skb->data;
909 ethhdr = (struct ethhdr *)skb_mac_header(skb);
911 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
912 memcpy(icmp_packet->orig,
913 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
914 icmp_packet->msg_type = TTL_EXCEEDED;
915 icmp_packet->ttl = TTL;
917 send_skb_packet(skb, batman_if, dstaddr);
918 ret = NET_RX_SUCCESS;
920 } else
921 spin_unlock_bh(&bat_priv->orig_hash_lock);
923 return ret;
927 int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
929 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
930 struct icmp_packet_rr *icmp_packet;
931 struct ethhdr *ethhdr;
932 struct orig_node *orig_node;
933 struct batman_if *batman_if;
934 int hdr_size = sizeof(struct icmp_packet);
935 int ret;
936 uint8_t dstaddr[ETH_ALEN];
939 * we truncate all incoming icmp packets if they don't match our size
941 if (skb->len >= sizeof(struct icmp_packet_rr))
942 hdr_size = sizeof(struct icmp_packet_rr);
944 /* drop packet if it has not necessary minimum size */
945 if (unlikely(!pskb_may_pull(skb, hdr_size)))
946 return NET_RX_DROP;
948 ethhdr = (struct ethhdr *)skb_mac_header(skb);
950 /* packet with unicast indication but broadcast recipient */
951 if (is_broadcast_ether_addr(ethhdr->h_dest))
952 return NET_RX_DROP;
954 /* packet with broadcast sender address */
955 if (is_broadcast_ether_addr(ethhdr->h_source))
956 return NET_RX_DROP;
958 /* not for me */
959 if (!is_my_mac(ethhdr->h_dest))
960 return NET_RX_DROP;
962 icmp_packet = (struct icmp_packet_rr *)skb->data;
964 /* add record route information if not full */
965 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
966 (icmp_packet->rr_cur < BAT_RR_LEN)) {
967 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
968 ethhdr->h_dest, ETH_ALEN);
969 icmp_packet->rr_cur++;
972 /* packet for me */
973 if (is_my_mac(icmp_packet->dst))
974 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
976 /* TTL exceeded */
977 if (icmp_packet->ttl < 2)
978 return recv_icmp_ttl_exceeded(bat_priv, skb);
980 ret = NET_RX_DROP;
982 /* get routing information */
983 spin_lock_bh(&bat_priv->orig_hash_lock);
984 orig_node = ((struct orig_node *)
985 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
986 icmp_packet->dst));
988 if ((orig_node) && (orig_node->router)) {
990 /* don't lock while sending the packets ... we therefore
991 * copy the required data before sending */
992 batman_if = orig_node->router->if_incoming;
993 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
994 spin_unlock_bh(&bat_priv->orig_hash_lock);
996 /* create a copy of the skb, if needed, to modify it. */
997 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
998 return NET_RX_DROP;
1000 icmp_packet = (struct icmp_packet_rr *)skb->data;
1001 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1003 /* decrement ttl */
1004 icmp_packet->ttl--;
1006 /* route it */
1007 send_skb_packet(skb, batman_if, dstaddr);
1008 ret = NET_RX_SUCCESS;
1010 } else
1011 spin_unlock_bh(&bat_priv->orig_hash_lock);
1013 return ret;
1016 /* find a suitable router for this originator, and use
1017 * bonding if possible. */
1018 struct neigh_node *find_router(struct bat_priv *bat_priv,
1019 struct orig_node *orig_node,
1020 struct batman_if *recv_if)
1022 struct orig_node *primary_orig_node;
1023 struct orig_node *router_orig;
1024 struct neigh_node *router, *first_candidate, *best_router;
1025 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1026 int bonding_enabled;
1028 if (!orig_node)
1029 return NULL;
1031 if (!orig_node->router)
1032 return NULL;
1034 /* without bonding, the first node should
1035 * always choose the default router. */
1037 bonding_enabled = atomic_read(&bat_priv->bonding);
1039 if ((!recv_if) && (!bonding_enabled))
1040 return orig_node->router;
1042 router_orig = orig_node->router->orig_node;
1044 /* if we have something in the primary_addr, we can search
1045 * for a potential bonding candidate. */
1046 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1047 return orig_node->router;
1049 /* find the orig_node which has the primary interface. might
1050 * even be the same as our router_orig in many cases */
1052 if (memcmp(router_orig->primary_addr,
1053 router_orig->orig, ETH_ALEN) == 0) {
1054 primary_orig_node = router_orig;
1055 } else {
1056 primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig,
1057 choose_orig,
1058 router_orig->primary_addr);
1060 if (!primary_orig_node)
1061 return orig_node->router;
1064 /* with less than 2 candidates, we can't do any
1065 * bonding and prefer the original router. */
1067 if (primary_orig_node->bond.candidates < 2)
1068 return orig_node->router;
1071 /* all nodes between should choose a candidate which
1072 * is is not on the interface where the packet came
1073 * in. */
1074 first_candidate = primary_orig_node->bond.selected;
1075 router = first_candidate;
1077 if (bonding_enabled) {
1078 /* in the bonding case, send the packets in a round
1079 * robin fashion over the remaining interfaces. */
1080 do {
1081 /* recv_if == NULL on the first node. */
1082 if (router->if_incoming != recv_if)
1083 break;
1085 router = router->next_bond_candidate;
1086 } while (router != first_candidate);
1088 primary_orig_node->bond.selected = router->next_bond_candidate;
1090 } else {
1091 /* if bonding is disabled, use the best of the
1092 * remaining candidates which are not using
1093 * this interface. */
1094 best_router = first_candidate;
1096 do {
1097 /* recv_if == NULL on the first node. */
1098 if ((router->if_incoming != recv_if) &&
1099 (router->tq_avg > best_router->tq_avg))
1100 best_router = router;
1102 router = router->next_bond_candidate;
1103 } while (router != first_candidate);
1105 router = best_router;
1108 return router;
1111 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1113 struct ethhdr *ethhdr;
1115 /* drop packet if it has not necessary minimum size */
1116 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1117 return -1;
1119 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1121 /* packet with unicast indication but broadcast recipient */
1122 if (is_broadcast_ether_addr(ethhdr->h_dest))
1123 return -1;
1125 /* packet with broadcast sender address */
1126 if (is_broadcast_ether_addr(ethhdr->h_source))
1127 return -1;
1129 /* not for me */
1130 if (!is_my_mac(ethhdr->h_dest))
1131 return -1;
1133 return 0;
1136 int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1137 int hdr_size)
1139 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1140 struct orig_node *orig_node;
1141 struct neigh_node *router;
1142 struct batman_if *batman_if;
1143 uint8_t dstaddr[ETH_ALEN];
1144 struct unicast_packet *unicast_packet;
1145 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1146 int ret;
1147 struct sk_buff *new_skb;
1149 unicast_packet = (struct unicast_packet *)skb->data;
1151 /* TTL exceeded */
1152 if (unicast_packet->ttl < 2) {
1153 pr_debug("Warning - can't forward unicast packet from %pM to "
1154 "%pM: ttl exceeded\n", ethhdr->h_source,
1155 unicast_packet->dest);
1156 return NET_RX_DROP;
1159 /* get routing information */
1160 spin_lock_bh(&bat_priv->orig_hash_lock);
1161 orig_node = ((struct orig_node *)
1162 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1163 unicast_packet->dest));
1165 router = find_router(bat_priv, orig_node, recv_if);
1167 if (!router) {
1168 spin_unlock_bh(&bat_priv->orig_hash_lock);
1169 return NET_RX_DROP;
1172 /* don't lock while sending the packets ... we therefore
1173 * copy the required data before sending */
1175 batman_if = router->if_incoming;
1176 memcpy(dstaddr, router->addr, ETH_ALEN);
1178 spin_unlock_bh(&bat_priv->orig_hash_lock);
1180 /* create a copy of the skb, if needed, to modify it. */
1181 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1182 return NET_RX_DROP;
1184 unicast_packet = (struct unicast_packet *)skb->data;
1186 if (unicast_packet->packet_type == BAT_UNICAST &&
1187 atomic_read(&bat_priv->fragmentation) &&
1188 skb->len > batman_if->net_dev->mtu)
1189 return frag_send_skb(skb, bat_priv, batman_if,
1190 dstaddr);
1192 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1193 frag_can_reassemble(skb, batman_if->net_dev->mtu)) {
1195 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1197 if (ret == NET_RX_DROP)
1198 return NET_RX_DROP;
1200 /* packet was buffered for late merge */
1201 if (!new_skb)
1202 return NET_RX_SUCCESS;
1204 skb = new_skb;
1205 unicast_packet = (struct unicast_packet *)skb->data;
1208 /* decrement ttl */
1209 unicast_packet->ttl--;
1211 /* route it */
1212 send_skb_packet(skb, batman_if, dstaddr);
1214 return NET_RX_SUCCESS;
1217 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1219 struct unicast_packet *unicast_packet;
1220 int hdr_size = sizeof(struct unicast_packet);
1222 if (check_unicast_packet(skb, hdr_size) < 0)
1223 return NET_RX_DROP;
1225 unicast_packet = (struct unicast_packet *)skb->data;
1227 /* packet for me */
1228 if (is_my_mac(unicast_packet->dest)) {
1229 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1230 return NET_RX_SUCCESS;
1233 return route_unicast_packet(skb, recv_if, hdr_size);
1236 int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1238 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1239 struct unicast_frag_packet *unicast_packet;
1240 int hdr_size = sizeof(struct unicast_frag_packet);
1241 struct sk_buff *new_skb = NULL;
1242 int ret;
1244 if (check_unicast_packet(skb, hdr_size) < 0)
1245 return NET_RX_DROP;
1247 unicast_packet = (struct unicast_frag_packet *)skb->data;
1249 /* packet for me */
1250 if (is_my_mac(unicast_packet->dest)) {
1252 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1254 if (ret == NET_RX_DROP)
1255 return NET_RX_DROP;
1257 /* packet was buffered for late merge */
1258 if (!new_skb)
1259 return NET_RX_SUCCESS;
1261 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1262 sizeof(struct unicast_packet));
1263 return NET_RX_SUCCESS;
1266 return route_unicast_packet(skb, recv_if, hdr_size);
1270 int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1272 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1273 struct orig_node *orig_node;
1274 struct bcast_packet *bcast_packet;
1275 struct ethhdr *ethhdr;
1276 int hdr_size = sizeof(struct bcast_packet);
1277 int32_t seq_diff;
1279 /* drop packet if it has not necessary minimum size */
1280 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1281 return NET_RX_DROP;
1283 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1285 /* packet with broadcast indication but unicast recipient */
1286 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1287 return NET_RX_DROP;
1289 /* packet with broadcast sender address */
1290 if (is_broadcast_ether_addr(ethhdr->h_source))
1291 return NET_RX_DROP;
1293 /* ignore broadcasts sent by myself */
1294 if (is_my_mac(ethhdr->h_source))
1295 return NET_RX_DROP;
1297 bcast_packet = (struct bcast_packet *)skb->data;
1299 /* ignore broadcasts originated by myself */
1300 if (is_my_mac(bcast_packet->orig))
1301 return NET_RX_DROP;
1303 if (bcast_packet->ttl < 2)
1304 return NET_RX_DROP;
1306 spin_lock_bh(&bat_priv->orig_hash_lock);
1307 orig_node = ((struct orig_node *)
1308 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1309 bcast_packet->orig));
1311 if (!orig_node) {
1312 spin_unlock_bh(&bat_priv->orig_hash_lock);
1313 return NET_RX_DROP;
1316 /* check whether the packet is a duplicate */
1317 if (get_bit_status(orig_node->bcast_bits,
1318 orig_node->last_bcast_seqno,
1319 ntohl(bcast_packet->seqno))) {
1320 spin_unlock_bh(&bat_priv->orig_hash_lock);
1321 return NET_RX_DROP;
1324 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1326 /* check whether the packet is old and the host just restarted. */
1327 if (window_protected(bat_priv, seq_diff,
1328 &orig_node->bcast_seqno_reset)) {
1329 spin_unlock_bh(&bat_priv->orig_hash_lock);
1330 return NET_RX_DROP;
1333 /* mark broadcast in flood history, update window position
1334 * if required. */
1335 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1336 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1338 spin_unlock_bh(&bat_priv->orig_hash_lock);
1339 /* rebroadcast packet */
1340 add_bcast_packet_to_list(bat_priv, skb);
1342 /* broadcast for me */
1343 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1345 return NET_RX_SUCCESS;
1348 int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1350 struct vis_packet *vis_packet;
1351 struct ethhdr *ethhdr;
1352 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1353 int hdr_size = sizeof(struct vis_packet);
1355 /* keep skb linear */
1356 if (skb_linearize(skb) < 0)
1357 return NET_RX_DROP;
1359 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1360 return NET_RX_DROP;
1362 vis_packet = (struct vis_packet *)skb->data;
1363 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1365 /* not for me */
1366 if (!is_my_mac(ethhdr->h_dest))
1367 return NET_RX_DROP;
1369 /* ignore own packets */
1370 if (is_my_mac(vis_packet->vis_orig))
1371 return NET_RX_DROP;
1373 if (is_my_mac(vis_packet->sender_orig))
1374 return NET_RX_DROP;
1376 switch (vis_packet->vis_type) {
1377 case VIS_TYPE_SERVER_SYNC:
1378 receive_server_sync_packet(bat_priv, vis_packet,
1379 skb_headlen(skb));
1380 break;
1382 case VIS_TYPE_CLIENT_UPDATE:
1383 receive_client_update_packet(bat_priv, vis_packet,
1384 skb_headlen(skb));
1385 break;
1387 default: /* ignore unknown packet */
1388 break;
1391 /* We take a copy of the data in the packet, so we should
1392 always free the skbuf. */
1393 return NET_RX_DROP;