Staging: batman-adv: Remove hashdata_compare_cb from hash
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / batman-adv / routing.c
blobbb0bd7871959515382e0f7992bbe03437274604c
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 "unicast.h"
37 void slide_own_bcast_window(struct batman_if *batman_if)
39 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
40 HASHIT(hashit);
41 struct orig_node *orig_node;
42 TYPE_OF_WORD *word;
43 unsigned long flags;
45 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
47 while (hash_iterate(bat_priv->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(bat_priv, word, 1, 0);
52 orig_node->bcast_own_sum[batman_if->if_num] =
53 bit_packet_count(word);
56 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
59 static void update_HNA(struct bat_priv *bat_priv, 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(bat_priv, orig_node,
69 "originator changed hna");
71 if ((hna_buff_len > 0) && (hna_buff != NULL))
72 hna_global_add_orig(bat_priv, orig_node,
73 hna_buff, hna_buff_len);
77 static void update_route(struct bat_priv *bat_priv,
78 struct orig_node *orig_node,
79 struct neigh_node *neigh_node,
80 unsigned char *hna_buff, int hna_buff_len)
82 /* route deleted */
83 if ((orig_node->router != NULL) && (neigh_node == NULL)) {
85 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
86 orig_node->orig);
87 hna_global_del_orig(bat_priv, orig_node,
88 "originator timed out");
90 /* route added */
91 } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
93 bat_dbg(DBG_ROUTES, bat_priv,
94 "Adding route towards: %pM (via %pM)\n",
95 orig_node->orig, neigh_node->addr);
96 hna_global_add_orig(bat_priv, orig_node,
97 hna_buff, hna_buff_len);
99 /* route changed */
100 } else {
101 bat_dbg(DBG_ROUTES, bat_priv,
102 "Changing route towards: %pM "
103 "(now via %pM - was via %pM)\n",
104 orig_node->orig, neigh_node->addr,
105 orig_node->router->addr);
108 orig_node->router = neigh_node;
112 void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
113 struct neigh_node *neigh_node, unsigned char *hna_buff,
114 int hna_buff_len)
117 if (orig_node == NULL)
118 return;
120 if (orig_node->router != neigh_node)
121 update_route(bat_priv, orig_node, neigh_node,
122 hna_buff, hna_buff_len);
123 /* may be just HNA changed */
124 else
125 update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
128 static int is_bidirectional_neigh(struct orig_node *orig_node,
129 struct orig_node *orig_neigh_node,
130 struct batman_packet *batman_packet,
131 struct batman_if *if_incoming)
133 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
134 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
135 unsigned char total_count;
137 if (orig_node == orig_neigh_node) {
138 list_for_each_entry(tmp_neigh_node,
139 &orig_node->neigh_list,
140 list) {
142 if (compare_orig(tmp_neigh_node->addr,
143 orig_neigh_node->orig) &&
144 (tmp_neigh_node->if_incoming == if_incoming))
145 neigh_node = tmp_neigh_node;
148 if (!neigh_node)
149 neigh_node = create_neighbor(orig_node,
150 orig_neigh_node,
151 orig_neigh_node->orig,
152 if_incoming);
153 /* create_neighbor failed, return 0 */
154 if (!neigh_node)
155 return 0;
157 neigh_node->last_valid = jiffies;
158 } else {
159 /* find packet count of corresponding one hop neighbor */
160 list_for_each_entry(tmp_neigh_node,
161 &orig_neigh_node->neigh_list, list) {
163 if (compare_orig(tmp_neigh_node->addr,
164 orig_neigh_node->orig) &&
165 (tmp_neigh_node->if_incoming == if_incoming))
166 neigh_node = tmp_neigh_node;
169 if (!neigh_node)
170 neigh_node = create_neighbor(orig_neigh_node,
171 orig_neigh_node,
172 orig_neigh_node->orig,
173 if_incoming);
174 /* create_neighbor failed, return 0 */
175 if (!neigh_node)
176 return 0;
179 orig_node->last_valid = jiffies;
181 /* pay attention to not get a value bigger than 100 % */
182 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
183 neigh_node->real_packet_count ?
184 neigh_node->real_packet_count :
185 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
187 /* if we have too few packets (too less data) we set tq_own to zero */
188 /* if we receive too few packets it is not considered bidirectional */
189 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
190 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
191 orig_neigh_node->tq_own = 0;
192 else
193 /* neigh_node->real_packet_count is never zero as we
194 * only purge old information when getting new
195 * information */
196 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
197 neigh_node->real_packet_count;
200 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
201 * affect the nearly-symmetric links only a little, but
202 * punishes asymmetric links more. This will give a value
203 * between 0 and TQ_MAX_VALUE
205 orig_neigh_node->tq_asym_penalty =
206 TQ_MAX_VALUE -
207 (TQ_MAX_VALUE *
208 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
209 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
210 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
211 (TQ_LOCAL_WINDOW_SIZE *
212 TQ_LOCAL_WINDOW_SIZE *
213 TQ_LOCAL_WINDOW_SIZE);
215 batman_packet->tq = ((batman_packet->tq *
216 orig_neigh_node->tq_own *
217 orig_neigh_node->tq_asym_penalty) /
218 (TQ_MAX_VALUE * TQ_MAX_VALUE));
220 bat_dbg(DBG_BATMAN, bat_priv,
221 "bidirectional: "
222 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
223 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
224 "total tq: %3i\n",
225 orig_node->orig, orig_neigh_node->orig, total_count,
226 neigh_node->real_packet_count, orig_neigh_node->tq_own,
227 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
229 /* if link has the minimum required transmission quality
230 * consider it bidirectional */
231 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
232 return 1;
234 return 0;
237 static void update_orig(struct bat_priv *bat_priv,
238 struct orig_node *orig_node,
239 struct ethhdr *ethhdr,
240 struct batman_packet *batman_packet,
241 struct batman_if *if_incoming,
242 unsigned char *hna_buff, int hna_buff_len,
243 char is_duplicate)
245 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
246 int tmp_hna_buff_len;
248 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
249 "Searching and updating originator entry of received packet\n");
251 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
252 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
253 (tmp_neigh_node->if_incoming == if_incoming)) {
254 neigh_node = tmp_neigh_node;
255 continue;
258 if (is_duplicate)
259 continue;
261 ring_buffer_set(tmp_neigh_node->tq_recv,
262 &tmp_neigh_node->tq_index, 0);
263 tmp_neigh_node->tq_avg =
264 ring_buffer_avg(tmp_neigh_node->tq_recv);
267 if (!neigh_node) {
268 struct orig_node *orig_tmp;
270 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
271 if (!orig_tmp)
272 return;
274 neigh_node = create_neighbor(orig_node, orig_tmp,
275 ethhdr->h_source, if_incoming);
276 if (!neigh_node)
277 return;
278 } else
279 bat_dbg(DBG_BATMAN, bat_priv,
280 "Updating existing last-hop neighbor of originator\n");
282 orig_node->flags = batman_packet->flags;
283 neigh_node->last_valid = jiffies;
285 ring_buffer_set(neigh_node->tq_recv,
286 &neigh_node->tq_index,
287 batman_packet->tq);
288 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
290 if (!is_duplicate) {
291 orig_node->last_ttl = batman_packet->ttl;
292 neigh_node->last_ttl = batman_packet->ttl;
295 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
296 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
298 /* if this neighbor already is our next hop there is nothing
299 * to change */
300 if (orig_node->router == neigh_node)
301 goto update_hna;
303 /* if this neighbor does not offer a better TQ we won't consider it */
304 if ((orig_node->router) &&
305 (orig_node->router->tq_avg > neigh_node->tq_avg))
306 goto update_hna;
308 /* if the TQ is the same and the link not more symetric we
309 * won't consider it either */
310 if ((orig_node->router) &&
311 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
312 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
313 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
314 goto update_hna;
316 update_routes(bat_priv, orig_node, neigh_node,
317 hna_buff, tmp_hna_buff_len);
318 return;
320 update_hna:
321 update_routes(bat_priv, orig_node, orig_node->router,
322 hna_buff, tmp_hna_buff_len);
325 /* checks whether the host restarted and is in the protection time.
326 * returns:
327 * 0 if the packet is to be accepted
328 * 1 if the packet is to be ignored.
330 static int window_protected(struct bat_priv *bat_priv,
331 int32_t seq_num_diff,
332 unsigned long *last_reset)
334 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
335 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
336 if (time_after(jiffies, *last_reset +
337 msecs_to_jiffies(RESET_PROTECTION_MS))) {
339 *last_reset = jiffies;
340 bat_dbg(DBG_BATMAN, bat_priv,
341 "old packet received, start protection\n");
343 return 0;
344 } else
345 return 1;
347 return 0;
350 /* processes a batman packet for all interfaces, adjusts the sequence number and
351 * finds out whether it is a duplicate.
352 * returns:
353 * 1 the packet is a duplicate
354 * 0 the packet has not yet been received
355 * -1 the packet is old and has been received while the seqno window
356 * was protected. Caller should drop it.
358 static char count_real_packets(struct ethhdr *ethhdr,
359 struct batman_packet *batman_packet,
360 struct batman_if *if_incoming)
362 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
363 struct orig_node *orig_node;
364 struct neigh_node *tmp_neigh_node;
365 char is_duplicate = 0;
366 int32_t seq_diff;
367 int need_update = 0;
368 int set_mark;
370 orig_node = get_orig_node(bat_priv, batman_packet->orig);
371 if (orig_node == NULL)
372 return 0;
374 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
376 /* signalize caller that the packet is to be dropped. */
377 if (window_protected(bat_priv, seq_diff,
378 &orig_node->batman_seqno_reset))
379 return -1;
381 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
383 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
384 orig_node->last_real_seqno,
385 batman_packet->seqno);
387 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
388 (tmp_neigh_node->if_incoming == if_incoming))
389 set_mark = 1;
390 else
391 set_mark = 0;
393 /* if the window moved, set the update flag. */
394 need_update |= bit_get_packet(bat_priv,
395 tmp_neigh_node->real_bits,
396 seq_diff, set_mark);
398 tmp_neigh_node->real_packet_count =
399 bit_packet_count(tmp_neigh_node->real_bits);
402 if (need_update) {
403 bat_dbg(DBG_BATMAN, bat_priv,
404 "updating last_seqno: old %d, new %d\n",
405 orig_node->last_real_seqno, batman_packet->seqno);
406 orig_node->last_real_seqno = batman_packet->seqno;
409 return is_duplicate;
412 /* copy primary address for bonding */
413 static void mark_bonding_address(struct bat_priv *bat_priv,
414 struct orig_node *orig_node,
415 struct orig_node *orig_neigh_node,
416 struct batman_packet *batman_packet)
419 if (batman_packet->flags & PRIMARIES_FIRST_HOP)
420 memcpy(orig_neigh_node->primary_addr,
421 orig_node->orig, ETH_ALEN);
423 return;
426 /* mark possible bond.candidates in the neighbor list */
427 void update_bonding_candidates(struct bat_priv *bat_priv,
428 struct orig_node *orig_node)
430 int candidates;
431 int interference_candidate;
432 int best_tq;
433 struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
434 struct neigh_node *first_candidate, *last_candidate;
436 /* update the candidates for this originator */
437 if (!orig_node->router) {
438 orig_node->bond.candidates = 0;
439 return;
442 best_tq = orig_node->router->tq_avg;
444 /* update bond.candidates */
446 candidates = 0;
448 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
449 * as "bonding partner" */
451 /* first, zero the list */
452 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
453 tmp_neigh_node->next_bond_candidate = NULL;
456 first_candidate = NULL;
457 last_candidate = NULL;
458 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
460 /* only consider if it has the same primary address ... */
461 if (memcmp(orig_node->orig,
462 tmp_neigh_node->orig_node->primary_addr,
463 ETH_ALEN) != 0)
464 continue;
466 /* ... and is good enough to be considered */
467 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
468 continue;
470 /* check if we have another candidate with the same
471 * mac address or interface. If we do, we won't
472 * select this candidate because of possible interference. */
474 interference_candidate = 0;
475 list_for_each_entry(tmp_neigh_node2,
476 &orig_node->neigh_list, list) {
478 if (tmp_neigh_node2 == tmp_neigh_node)
479 continue;
481 /* we only care if the other candidate is even
482 * considered as candidate. */
483 if (tmp_neigh_node2->next_bond_candidate == NULL)
484 continue;
487 if ((tmp_neigh_node->if_incoming ==
488 tmp_neigh_node2->if_incoming)
489 || (memcmp(tmp_neigh_node->addr,
490 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
492 interference_candidate = 1;
493 break;
496 /* don't care further if it is an interference candidate */
497 if (interference_candidate)
498 continue;
500 if (first_candidate == NULL) {
501 first_candidate = tmp_neigh_node;
502 tmp_neigh_node->next_bond_candidate = first_candidate;
503 } else
504 tmp_neigh_node->next_bond_candidate = last_candidate;
506 last_candidate = tmp_neigh_node;
508 candidates++;
511 if (candidates > 0) {
512 first_candidate->next_bond_candidate = last_candidate;
513 orig_node->bond.selected = first_candidate;
516 orig_node->bond.candidates = candidates;
519 void receive_bat_packet(struct ethhdr *ethhdr,
520 struct batman_packet *batman_packet,
521 unsigned char *hna_buff, int hna_buff_len,
522 struct batman_if *if_incoming)
524 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
525 struct batman_if *batman_if;
526 struct orig_node *orig_neigh_node, *orig_node;
527 char has_directlink_flag;
528 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
529 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
530 char is_duplicate;
531 uint32_t if_incoming_seqno;
533 /* Silently drop when the batman packet is actually not a
534 * correct packet.
536 * This might happen if a packet is padded (e.g. Ethernet has a
537 * minimum frame length of 64 byte) and the aggregation interprets
538 * it as an additional length.
540 * TODO: A more sane solution would be to have a bit in the
541 * batman_packet to detect whether the packet is the last
542 * packet in an aggregation. Here we expect that the padding
543 * is always zero (or not 0x01)
545 if (batman_packet->packet_type != BAT_PACKET)
546 return;
548 /* could be changed by schedule_own_packet() */
549 if_incoming_seqno = atomic_read(&if_incoming->seqno);
551 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
553 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
554 batman_packet->orig) ? 1 : 0);
556 bat_dbg(DBG_BATMAN, bat_priv,
557 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
558 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
559 "TTL %d, V %d, IDF %d)\n",
560 ethhdr->h_source, if_incoming->net_dev->name,
561 if_incoming->net_dev->dev_addr, batman_packet->orig,
562 batman_packet->prev_sender, batman_packet->seqno,
563 batman_packet->tq, batman_packet->ttl, batman_packet->version,
564 has_directlink_flag);
566 rcu_read_lock();
567 list_for_each_entry_rcu(batman_if, &if_list, list) {
568 if (batman_if->if_status != IF_ACTIVE)
569 continue;
571 if (batman_if->soft_iface != if_incoming->soft_iface)
572 continue;
574 if (compare_orig(ethhdr->h_source,
575 batman_if->net_dev->dev_addr))
576 is_my_addr = 1;
578 if (compare_orig(batman_packet->orig,
579 batman_if->net_dev->dev_addr))
580 is_my_orig = 1;
582 if (compare_orig(batman_packet->prev_sender,
583 batman_if->net_dev->dev_addr))
584 is_my_oldorig = 1;
586 if (compare_orig(ethhdr->h_source, broadcast_addr))
587 is_broadcast = 1;
589 rcu_read_unlock();
591 if (batman_packet->version != COMPAT_VERSION) {
592 bat_dbg(DBG_BATMAN, bat_priv,
593 "Drop packet: incompatible batman version (%i)\n",
594 batman_packet->version);
595 return;
598 if (is_my_addr) {
599 bat_dbg(DBG_BATMAN, bat_priv,
600 "Drop packet: received my own broadcast (sender: %pM"
601 ")\n",
602 ethhdr->h_source);
603 return;
606 if (is_broadcast) {
607 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
608 "ignoring all packets with broadcast source addr (sender: %pM"
609 ")\n", ethhdr->h_source);
610 return;
613 if (is_my_orig) {
614 TYPE_OF_WORD *word;
615 int offset;
617 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
619 if (!orig_neigh_node)
620 return;
622 /* neighbor has to indicate direct link and it has to
623 * come via the corresponding interface */
624 /* if received seqno equals last send seqno save new
625 * seqno for bidirectional check */
626 if (has_directlink_flag &&
627 compare_orig(if_incoming->net_dev->dev_addr,
628 batman_packet->orig) &&
629 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
630 offset = if_incoming->if_num * NUM_WORDS;
631 word = &(orig_neigh_node->bcast_own[offset]);
632 bit_mark(word, 0);
633 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
634 bit_packet_count(word);
637 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
638 "originator packet from myself (via neighbor)\n");
639 return;
642 if (is_my_oldorig) {
643 bat_dbg(DBG_BATMAN, bat_priv,
644 "Drop packet: ignoring all rebroadcast echos (sender: "
645 "%pM)\n", ethhdr->h_source);
646 return;
649 orig_node = get_orig_node(bat_priv, batman_packet->orig);
650 if (orig_node == NULL)
651 return;
653 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
655 if (is_duplicate == -1) {
656 bat_dbg(DBG_BATMAN, bat_priv,
657 "Drop packet: packet within seqno protection time "
658 "(sender: %pM)\n", ethhdr->h_source);
659 return;
662 if (batman_packet->tq == 0) {
663 bat_dbg(DBG_BATMAN, bat_priv,
664 "Drop packet: originator packet with tq equal 0\n");
665 return;
668 /* avoid temporary routing loops */
669 if ((orig_node->router) &&
670 (orig_node->router->orig_node->router) &&
671 (compare_orig(orig_node->router->addr,
672 batman_packet->prev_sender)) &&
673 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
674 (compare_orig(orig_node->router->addr,
675 orig_node->router->orig_node->router->addr))) {
676 bat_dbg(DBG_BATMAN, bat_priv,
677 "Drop packet: ignoring all rebroadcast packets that "
678 "may make me loop (sender: %pM)\n", ethhdr->h_source);
679 return;
682 /* if sender is a direct neighbor the sender mac equals
683 * originator mac */
684 orig_neigh_node = (is_single_hop_neigh ?
685 orig_node :
686 get_orig_node(bat_priv, ethhdr->h_source));
687 if (orig_neigh_node == NULL)
688 return;
690 /* drop packet if sender is not a direct neighbor and if we
691 * don't route towards it */
692 if (!is_single_hop_neigh &&
693 (orig_neigh_node->router == NULL)) {
694 bat_dbg(DBG_BATMAN, bat_priv,
695 "Drop packet: OGM via unknown neighbor!\n");
696 return;
699 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
700 batman_packet, if_incoming);
702 /* update ranking if it is not a duplicate or has the same
703 * seqno and similar ttl as the non-duplicate */
704 if (is_bidirectional &&
705 (!is_duplicate ||
706 ((orig_node->last_real_seqno == batman_packet->seqno) &&
707 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
708 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
709 if_incoming, hna_buff, hna_buff_len, is_duplicate);
711 mark_bonding_address(bat_priv, orig_node,
712 orig_neigh_node, batman_packet);
713 update_bonding_candidates(bat_priv, orig_node);
715 /* is single hop (direct) neighbor */
716 if (is_single_hop_neigh) {
718 /* mark direct link on incoming interface */
719 schedule_forward_packet(orig_node, ethhdr, batman_packet,
720 1, hna_buff_len, if_incoming);
722 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
723 "rebroadcast neighbor packet with direct link flag\n");
724 return;
727 /* multihop originator */
728 if (!is_bidirectional) {
729 bat_dbg(DBG_BATMAN, bat_priv,
730 "Drop packet: not received via bidirectional link\n");
731 return;
734 if (is_duplicate) {
735 bat_dbg(DBG_BATMAN, bat_priv,
736 "Drop packet: duplicate packet received\n");
737 return;
740 bat_dbg(DBG_BATMAN, bat_priv,
741 "Forwarding packet: rebroadcast originator packet\n");
742 schedule_forward_packet(orig_node, ethhdr, batman_packet,
743 0, hna_buff_len, if_incoming);
746 int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
748 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
749 struct ethhdr *ethhdr;
750 unsigned long flags;
752 /* drop packet if it has not necessary minimum size */
753 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
754 return NET_RX_DROP;
756 ethhdr = (struct ethhdr *)skb_mac_header(skb);
758 /* packet with broadcast indication but unicast recipient */
759 if (!is_bcast(ethhdr->h_dest))
760 return NET_RX_DROP;
762 /* packet with broadcast sender address */
763 if (is_bcast(ethhdr->h_source))
764 return NET_RX_DROP;
766 /* create a copy of the skb, if needed, to modify it. */
767 if (skb_cow(skb, 0) < 0)
768 return NET_RX_DROP;
770 /* keep skb linear */
771 if (skb_linearize(skb) < 0)
772 return NET_RX_DROP;
774 ethhdr = (struct ethhdr *)skb_mac_header(skb);
776 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
777 receive_aggr_bat_packet(ethhdr,
778 skb->data,
779 skb_headlen(skb),
780 batman_if);
781 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
783 kfree_skb(skb);
784 return NET_RX_SUCCESS;
787 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
788 struct sk_buff *skb, size_t icmp_len)
790 struct orig_node *orig_node;
791 struct icmp_packet_rr *icmp_packet;
792 struct ethhdr *ethhdr;
793 struct batman_if *batman_if;
794 int ret;
795 unsigned long flags;
796 uint8_t dstaddr[ETH_ALEN];
798 icmp_packet = (struct icmp_packet_rr *)skb->data;
799 ethhdr = (struct ethhdr *)skb_mac_header(skb);
801 /* add data to device queue */
802 if (icmp_packet->msg_type != ECHO_REQUEST) {
803 bat_socket_receive_packet(icmp_packet, icmp_len);
804 return NET_RX_DROP;
807 if (!bat_priv->primary_if)
808 return NET_RX_DROP;
810 /* answer echo request (ping) */
811 /* get routing information */
812 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
813 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
814 compare_orig,
815 icmp_packet->orig));
816 ret = NET_RX_DROP;
818 if ((orig_node != NULL) &&
819 (orig_node->router != NULL)) {
821 /* don't lock while sending the packets ... we therefore
822 * copy the required data before sending */
823 batman_if = orig_node->router->if_incoming;
824 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
825 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
827 /* create a copy of the skb, if needed, to modify it. */
828 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
829 return NET_RX_DROP;
831 icmp_packet = (struct icmp_packet_rr *)skb->data;
832 ethhdr = (struct ethhdr *)skb_mac_header(skb);
834 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
835 memcpy(icmp_packet->orig,
836 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
837 icmp_packet->msg_type = ECHO_REPLY;
838 icmp_packet->ttl = TTL;
840 send_skb_packet(skb, batman_if, dstaddr);
841 ret = NET_RX_SUCCESS;
843 } else
844 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
846 return ret;
849 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
850 struct sk_buff *skb, size_t icmp_len)
852 struct orig_node *orig_node;
853 struct icmp_packet *icmp_packet;
854 struct ethhdr *ethhdr;
855 struct batman_if *batman_if;
856 int ret;
857 unsigned long flags;
858 uint8_t dstaddr[ETH_ALEN];
860 icmp_packet = (struct icmp_packet *)skb->data;
861 ethhdr = (struct ethhdr *)skb_mac_header(skb);
863 /* send TTL exceeded if packet is an echo request (traceroute) */
864 if (icmp_packet->msg_type != ECHO_REQUEST) {
865 pr_debug("Warning - can't forward icmp packet from %pM to "
866 "%pM: ttl exceeded\n", icmp_packet->orig,
867 icmp_packet->dst);
868 return NET_RX_DROP;
871 if (!bat_priv->primary_if)
872 return NET_RX_DROP;
874 /* get routing information */
875 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
876 orig_node = ((struct orig_node *)
877 hash_find(bat_priv->orig_hash, compare_orig,
878 icmp_packet->orig));
879 ret = NET_RX_DROP;
881 if ((orig_node != NULL) &&
882 (orig_node->router != NULL)) {
884 /* don't lock while sending the packets ... we therefore
885 * copy the required data before sending */
886 batman_if = orig_node->router->if_incoming;
887 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
888 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
890 /* create a copy of the skb, if needed, to modify it. */
891 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
892 return NET_RX_DROP;
894 icmp_packet = (struct icmp_packet *) skb->data;
895 ethhdr = (struct ethhdr *)skb_mac_header(skb);
897 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
898 memcpy(icmp_packet->orig,
899 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
900 icmp_packet->msg_type = TTL_EXCEEDED;
901 icmp_packet->ttl = TTL;
903 send_skb_packet(skb, batman_if, dstaddr);
904 ret = NET_RX_SUCCESS;
906 } else
907 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
909 return ret;
913 int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
915 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
916 struct icmp_packet_rr *icmp_packet;
917 struct ethhdr *ethhdr;
918 struct orig_node *orig_node;
919 struct batman_if *batman_if;
920 int hdr_size = sizeof(struct icmp_packet);
921 int ret;
922 unsigned long flags;
923 uint8_t dstaddr[ETH_ALEN];
926 * we truncate all incoming icmp packets if they don't match our size
928 if (skb->len >= sizeof(struct icmp_packet_rr))
929 hdr_size = sizeof(struct icmp_packet_rr);
931 /* drop packet if it has not necessary minimum size */
932 if (unlikely(!pskb_may_pull(skb, hdr_size)))
933 return NET_RX_DROP;
935 ethhdr = (struct ethhdr *)skb_mac_header(skb);
937 /* packet with unicast indication but broadcast recipient */
938 if (is_bcast(ethhdr->h_dest))
939 return NET_RX_DROP;
941 /* packet with broadcast sender address */
942 if (is_bcast(ethhdr->h_source))
943 return NET_RX_DROP;
945 /* not for me */
946 if (!is_my_mac(ethhdr->h_dest))
947 return NET_RX_DROP;
949 icmp_packet = (struct icmp_packet_rr *)skb->data;
951 /* add record route information if not full */
952 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
953 (icmp_packet->rr_cur < BAT_RR_LEN)) {
954 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
955 ethhdr->h_dest, ETH_ALEN);
956 icmp_packet->rr_cur++;
959 /* packet for me */
960 if (is_my_mac(icmp_packet->dst))
961 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
963 /* TTL exceeded */
964 if (icmp_packet->ttl < 2)
965 return recv_icmp_ttl_exceeded(bat_priv, skb, hdr_size);
967 ret = NET_RX_DROP;
969 /* get routing information */
970 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
971 orig_node = ((struct orig_node *)
972 hash_find(bat_priv->orig_hash, compare_orig,
973 icmp_packet->dst));
975 if ((orig_node != NULL) &&
976 (orig_node->router != NULL)) {
978 /* don't lock while sending the packets ... we therefore
979 * copy the required data before sending */
980 batman_if = orig_node->router->if_incoming;
981 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
982 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
984 /* create a copy of the skb, if needed, to modify it. */
985 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
986 return NET_RX_DROP;
988 icmp_packet = (struct icmp_packet_rr *)skb->data;
989 ethhdr = (struct ethhdr *)skb_mac_header(skb);
991 /* decrement ttl */
992 icmp_packet->ttl--;
994 /* route it */
995 send_skb_packet(skb, batman_if, dstaddr);
996 ret = NET_RX_SUCCESS;
998 } else
999 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1001 return ret;
1004 /* find a suitable router for this originator, and use
1005 * bonding if possible. */
1006 struct neigh_node *find_router(struct bat_priv *bat_priv,
1007 struct orig_node *orig_node,
1008 struct batman_if *recv_if)
1010 struct orig_node *primary_orig_node;
1011 struct orig_node *router_orig;
1012 struct neigh_node *router, *first_candidate, *best_router;
1013 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1014 int bonding_enabled;
1016 if (!orig_node)
1017 return NULL;
1019 if (!orig_node->router)
1020 return NULL;
1022 /* without bonding, the first node should
1023 * always choose the default router. */
1025 bonding_enabled = atomic_read(&bat_priv->bonding);
1027 if ((!recv_if) && (!bonding_enabled))
1028 return orig_node->router;
1030 router_orig = orig_node->router->orig_node;
1032 /* if we have something in the primary_addr, we can search
1033 * for a potential bonding candidate. */
1034 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1035 return orig_node->router;
1037 /* find the orig_node which has the primary interface. might
1038 * even be the same as our router_orig in many cases */
1040 if (memcmp(router_orig->primary_addr,
1041 router_orig->orig, ETH_ALEN) == 0) {
1042 primary_orig_node = router_orig;
1043 } else {
1044 primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig,
1045 router_orig->primary_addr);
1047 if (!primary_orig_node)
1048 return orig_node->router;
1051 /* with less than 2 candidates, we can't do any
1052 * bonding and prefer the original router. */
1054 if (primary_orig_node->bond.candidates < 2)
1055 return orig_node->router;
1058 /* all nodes between should choose a candidate which
1059 * is is not on the interface where the packet came
1060 * in. */
1061 first_candidate = primary_orig_node->bond.selected;
1062 router = first_candidate;
1064 if (bonding_enabled) {
1065 /* in the bonding case, send the packets in a round
1066 * robin fashion over the remaining interfaces. */
1067 do {
1068 /* recv_if == NULL on the first node. */
1069 if (router->if_incoming != recv_if)
1070 break;
1072 router = router->next_bond_candidate;
1073 } while (router != first_candidate);
1075 primary_orig_node->bond.selected = router->next_bond_candidate;
1077 } else {
1078 /* if bonding is disabled, use the best of the
1079 * remaining candidates which are not using
1080 * this interface. */
1081 best_router = first_candidate;
1083 do {
1084 /* recv_if == NULL on the first node. */
1085 if ((router->if_incoming != recv_if) &&
1086 (router->tq_avg > best_router->tq_avg))
1087 best_router = router;
1089 router = router->next_bond_candidate;
1090 } while (router != first_candidate);
1092 router = best_router;
1095 return router;
1098 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1100 struct ethhdr *ethhdr;
1102 /* drop packet if it has not necessary minimum size */
1103 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1104 return -1;
1106 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1108 /* packet with unicast indication but broadcast recipient */
1109 if (is_bcast(ethhdr->h_dest))
1110 return -1;
1112 /* packet with broadcast sender address */
1113 if (is_bcast(ethhdr->h_source))
1114 return -1;
1116 /* not for me */
1117 if (!is_my_mac(ethhdr->h_dest))
1118 return -1;
1120 return 0;
1123 int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1124 int hdr_size)
1126 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1127 struct orig_node *orig_node;
1128 struct neigh_node *router;
1129 struct batman_if *batman_if;
1130 uint8_t dstaddr[ETH_ALEN];
1131 unsigned long flags;
1132 struct unicast_packet *unicast_packet;
1133 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1134 int ret;
1135 struct sk_buff *new_skb;
1137 unicast_packet = (struct unicast_packet *)skb->data;
1139 /* TTL exceeded */
1140 if (unicast_packet->ttl < 2) {
1141 pr_debug("Warning - can't forward unicast packet from %pM to "
1142 "%pM: ttl exceeded\n", ethhdr->h_source,
1143 unicast_packet->dest);
1144 return NET_RX_DROP;
1147 /* get routing information */
1148 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
1149 orig_node = ((struct orig_node *)
1150 hash_find(bat_priv->orig_hash, compare_orig,
1151 unicast_packet->dest));
1153 router = find_router(bat_priv, orig_node, recv_if);
1155 if (!router) {
1156 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1157 return NET_RX_DROP;
1160 /* don't lock while sending the packets ... we therefore
1161 * copy the required data before sending */
1163 batman_if = router->if_incoming;
1164 memcpy(dstaddr, router->addr, ETH_ALEN);
1166 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1168 /* create a copy of the skb, if needed, to modify it. */
1169 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1170 return NET_RX_DROP;
1172 unicast_packet = (struct unicast_packet *)skb->data;
1174 if (unicast_packet->packet_type == BAT_UNICAST &&
1175 atomic_read(&bat_priv->fragmentation) &&
1176 skb->len > batman_if->net_dev->mtu)
1177 return frag_send_skb(skb, bat_priv, batman_if,
1178 dstaddr);
1180 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
1181 2 * skb->len - hdr_size <= batman_if->net_dev->mtu) {
1183 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1185 if (ret == NET_RX_DROP)
1186 return NET_RX_DROP;
1188 /* packet was buffered for late merge */
1189 if (!new_skb)
1190 return NET_RX_SUCCESS;
1192 skb = new_skb;
1193 unicast_packet = (struct unicast_packet *)skb->data;
1196 /* decrement ttl */
1197 unicast_packet->ttl--;
1199 /* route it */
1200 send_skb_packet(skb, batman_if, dstaddr);
1202 return NET_RX_SUCCESS;
1205 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1207 struct unicast_packet *unicast_packet;
1208 int hdr_size = sizeof(struct unicast_packet);
1210 if (check_unicast_packet(skb, hdr_size) < 0)
1211 return NET_RX_DROP;
1213 unicast_packet = (struct unicast_packet *)skb->data;
1215 /* packet for me */
1216 if (is_my_mac(unicast_packet->dest)) {
1217 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1218 return NET_RX_SUCCESS;
1221 return route_unicast_packet(skb, recv_if, hdr_size);
1224 int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1226 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1227 struct unicast_frag_packet *unicast_packet;
1228 int hdr_size = sizeof(struct unicast_frag_packet);
1229 struct sk_buff *new_skb = NULL;
1230 int ret;
1232 if (check_unicast_packet(skb, hdr_size) < 0)
1233 return NET_RX_DROP;
1235 unicast_packet = (struct unicast_frag_packet *)skb->data;
1237 /* packet for me */
1238 if (is_my_mac(unicast_packet->dest)) {
1240 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1242 if (ret == NET_RX_DROP)
1243 return NET_RX_DROP;
1245 /* packet was buffered for late merge */
1246 if (!new_skb)
1247 return NET_RX_SUCCESS;
1249 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1250 sizeof(struct unicast_packet));
1251 return NET_RX_SUCCESS;
1254 return route_unicast_packet(skb, recv_if, hdr_size);
1258 int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1260 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1261 struct orig_node *orig_node;
1262 struct bcast_packet *bcast_packet;
1263 struct ethhdr *ethhdr;
1264 int hdr_size = sizeof(struct bcast_packet);
1265 int32_t seq_diff;
1266 unsigned long flags;
1268 /* drop packet if it has not necessary minimum size */
1269 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1270 return NET_RX_DROP;
1272 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1274 /* packet with broadcast indication but unicast recipient */
1275 if (!is_bcast(ethhdr->h_dest))
1276 return NET_RX_DROP;
1278 /* packet with broadcast sender address */
1279 if (is_bcast(ethhdr->h_source))
1280 return NET_RX_DROP;
1282 /* ignore broadcasts sent by myself */
1283 if (is_my_mac(ethhdr->h_source))
1284 return NET_RX_DROP;
1286 bcast_packet = (struct bcast_packet *)skb->data;
1288 /* ignore broadcasts originated by myself */
1289 if (is_my_mac(bcast_packet->orig))
1290 return NET_RX_DROP;
1292 if (bcast_packet->ttl < 2)
1293 return NET_RX_DROP;
1295 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
1296 orig_node = ((struct orig_node *)
1297 hash_find(bat_priv->orig_hash, compare_orig,
1298 bcast_packet->orig));
1300 if (orig_node == NULL) {
1301 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1302 return NET_RX_DROP;
1305 /* check whether the packet is a duplicate */
1306 if (get_bit_status(orig_node->bcast_bits,
1307 orig_node->last_bcast_seqno,
1308 ntohl(bcast_packet->seqno))) {
1309 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1310 return NET_RX_DROP;
1313 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1315 /* check whether the packet is old and the host just restarted. */
1316 if (window_protected(bat_priv, seq_diff,
1317 &orig_node->bcast_seqno_reset)) {
1318 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1319 return NET_RX_DROP;
1322 /* mark broadcast in flood history, update window position
1323 * if required. */
1324 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1325 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1327 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
1328 /* rebroadcast packet */
1329 add_bcast_packet_to_list(bat_priv, skb);
1331 /* broadcast for me */
1332 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1334 return NET_RX_SUCCESS;
1337 int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1339 struct vis_packet *vis_packet;
1340 struct ethhdr *ethhdr;
1341 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1342 int hdr_size = sizeof(struct vis_packet);
1344 /* keep skb linear */
1345 if (skb_linearize(skb) < 0)
1346 return NET_RX_DROP;
1348 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1349 return NET_RX_DROP;
1351 vis_packet = (struct vis_packet *)skb->data;
1352 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1354 /* not for me */
1355 if (!is_my_mac(ethhdr->h_dest))
1356 return NET_RX_DROP;
1358 /* ignore own packets */
1359 if (is_my_mac(vis_packet->vis_orig))
1360 return NET_RX_DROP;
1362 if (is_my_mac(vis_packet->sender_orig))
1363 return NET_RX_DROP;
1365 switch (vis_packet->vis_type) {
1366 case VIS_TYPE_SERVER_SYNC:
1367 receive_server_sync_packet(bat_priv, vis_packet,
1368 skb_headlen(skb));
1369 break;
1371 case VIS_TYPE_CLIENT_UPDATE:
1372 receive_client_update_packet(bat_priv, vis_packet,
1373 skb_headlen(skb));
1374 break;
1376 default: /* ignore unknown packet */
1377 break;
1380 /* We take a copy of the data in the packet, so we should
1381 always free the skbuf. */
1382 return NET_RX_DROP;