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
26 #include "soft-interface.h"
27 #include "hard-interface.h"
28 #include "icmp_socket.h"
29 #include "translation-table.h"
30 #include "originator.h"
32 #include "ring_buffer.h"
34 #include "aggregation.h"
35 #include "gateway_common.h"
36 #include "gateway_client.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
;
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
)
94 if ((orig_node
->router
) && (!neigh_node
)) {
96 bat_dbg(DBG_ROUTES
, bat_priv
, "Deleting route towards: %pM\n",
98 hna_global_del_orig(bat_priv
, orig_node
,
99 "originator timed out");
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
);
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
,
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 */
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
,
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
;
160 neigh_node
= create_neighbor(orig_node
,
162 orig_neigh_node
->orig
,
164 /* create_neighbor failed, return 0 */
168 neigh_node
->last_valid
= jiffies
;
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
;
181 neigh_node
= create_neighbor(orig_neigh_node
,
183 orig_neigh_node
->orig
,
185 /* create_neighbor failed, 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;
204 /* neigh_node->real_packet_count is never zero as we
205 * only purge old information when getting new
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
=
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
,
233 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
234 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
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
)
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
,
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
;
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
);
279 struct orig_node
*orig_tmp
;
281 orig_tmp
= get_orig_node(bat_priv
, ethhdr
->h_source
);
285 neigh_node
= create_neighbor(orig_node
, orig_tmp
,
286 ethhdr
->h_source
, if_incoming
);
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
,
299 neigh_node
->tq_avg
= ring_buffer_avg(neigh_node
->tq_recv
);
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
311 if (orig_node
->router
== neigh_node
)
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
))
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
])))
327 update_routes(bat_priv
, orig_node
, neigh_node
,
328 hna_buff
, tmp_hna_buff_len
);
332 update_routes(bat_priv
, orig_node
, orig_node
->router
,
333 hna_buff
, tmp_hna_buff_len
);
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.
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");
373 /* processes a batman packet for all interfaces, adjusts the sequence number and
374 * finds out whether it is a duplicate.
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;
393 orig_node
= get_orig_node(bat_priv
, batman_packet
->orig
);
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
))
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
))
416 /* if the window moved, set the update flag. */
417 need_update
|= bit_get_packet(bat_priv
,
418 tmp_neigh_node
->real_bits
,
421 tmp_neigh_node
->real_packet_count
=
422 bit_packet_count(tmp_neigh_node
->real_bits
);
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
;
435 /* copy primary address for bonding */
436 static void mark_bonding_address(struct bat_priv
*bat_priv
,
437 struct orig_node
*orig_node
,
438 struct orig_node
*orig_neigh_node
,
439 struct batman_packet
*batman_packet
)
442 if (batman_packet
->flags
& PRIMARIES_FIRST_HOP
)
443 memcpy(orig_neigh_node
->primary_addr
,
444 orig_node
->orig
, ETH_ALEN
);
449 /* mark possible bond.candidates in the neighbor list */
450 void update_bonding_candidates(struct bat_priv
*bat_priv
,
451 struct orig_node
*orig_node
)
454 int interference_candidate
;
456 struct neigh_node
*tmp_neigh_node
, *tmp_neigh_node2
;
457 struct neigh_node
*first_candidate
, *last_candidate
;
459 /* update the candidates for this originator */
460 if (!orig_node
->router
) {
461 orig_node
->bond
.candidates
= 0;
465 best_tq
= orig_node
->router
->tq_avg
;
467 /* update bond.candidates */
471 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
472 * as "bonding partner" */
474 /* first, zero the list */
475 list_for_each_entry(tmp_neigh_node
, &orig_node
->neigh_list
, list
) {
476 tmp_neigh_node
->next_bond_candidate
= NULL
;
479 first_candidate
= NULL
;
480 last_candidate
= NULL
;
481 list_for_each_entry(tmp_neigh_node
, &orig_node
->neigh_list
, list
) {
483 /* only consider if it has the same primary address ... */
484 if (memcmp(orig_node
->orig
,
485 tmp_neigh_node
->orig_node
->primary_addr
,
489 /* ... and is good enough to be considered */
490 if (tmp_neigh_node
->tq_avg
< best_tq
- BONDING_TQ_THRESHOLD
)
493 /* check if we have another candidate with the same
494 * mac address or interface. If we do, we won't
495 * select this candidate because of possible interference. */
497 interference_candidate
= 0;
498 list_for_each_entry(tmp_neigh_node2
,
499 &orig_node
->neigh_list
, list
) {
501 if (tmp_neigh_node2
== tmp_neigh_node
)
504 /* we only care if the other candidate is even
505 * considered as candidate. */
506 if (!tmp_neigh_node2
->next_bond_candidate
)
510 if ((tmp_neigh_node
->if_incoming
==
511 tmp_neigh_node2
->if_incoming
)
512 || (memcmp(tmp_neigh_node
->addr
,
513 tmp_neigh_node2
->addr
, ETH_ALEN
) == 0)) {
515 interference_candidate
= 1;
519 /* don't care further if it is an interference candidate */
520 if (interference_candidate
)
523 if (!first_candidate
) {
524 first_candidate
= tmp_neigh_node
;
525 tmp_neigh_node
->next_bond_candidate
= first_candidate
;
527 tmp_neigh_node
->next_bond_candidate
= last_candidate
;
529 last_candidate
= tmp_neigh_node
;
534 if (candidates
> 0) {
535 first_candidate
->next_bond_candidate
= last_candidate
;
536 orig_node
->bond
.selected
= first_candidate
;
539 orig_node
->bond
.candidates
= candidates
;
542 void receive_bat_packet(struct ethhdr
*ethhdr
,
543 struct batman_packet
*batman_packet
,
544 unsigned char *hna_buff
, int hna_buff_len
,
545 struct batman_if
*if_incoming
)
547 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
548 struct batman_if
*batman_if
;
549 struct orig_node
*orig_neigh_node
, *orig_node
;
550 char has_directlink_flag
;
551 char is_my_addr
= 0, is_my_orig
= 0, is_my_oldorig
= 0;
552 char is_broadcast
= 0, is_bidirectional
, is_single_hop_neigh
;
554 uint32_t if_incoming_seqno
;
556 /* Silently drop when the batman packet is actually not a
559 * This might happen if a packet is padded (e.g. Ethernet has a
560 * minimum frame length of 64 byte) and the aggregation interprets
561 * it as an additional length.
563 * TODO: A more sane solution would be to have a bit in the
564 * batman_packet to detect whether the packet is the last
565 * packet in an aggregation. Here we expect that the padding
566 * is always zero (or not 0x01)
568 if (batman_packet
->packet_type
!= BAT_PACKET
)
571 /* could be changed by schedule_own_packet() */
572 if_incoming_seqno
= atomic_read(&if_incoming
->seqno
);
574 has_directlink_flag
= (batman_packet
->flags
& DIRECTLINK
? 1 : 0);
576 is_single_hop_neigh
= (compare_orig(ethhdr
->h_source
,
577 batman_packet
->orig
) ? 1 : 0);
579 bat_dbg(DBG_BATMAN
, bat_priv
,
580 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
581 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
582 "TTL %d, V %d, IDF %d)\n",
583 ethhdr
->h_source
, if_incoming
->net_dev
->name
,
584 if_incoming
->net_dev
->dev_addr
, batman_packet
->orig
,
585 batman_packet
->prev_sender
, batman_packet
->seqno
,
586 batman_packet
->tq
, batman_packet
->ttl
, batman_packet
->version
,
587 has_directlink_flag
);
590 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
591 if (batman_if
->if_status
!= IF_ACTIVE
)
594 if (batman_if
->soft_iface
!= if_incoming
->soft_iface
)
597 if (compare_orig(ethhdr
->h_source
,
598 batman_if
->net_dev
->dev_addr
))
601 if (compare_orig(batman_packet
->orig
,
602 batman_if
->net_dev
->dev_addr
))
605 if (compare_orig(batman_packet
->prev_sender
,
606 batman_if
->net_dev
->dev_addr
))
609 if (compare_orig(ethhdr
->h_source
, broadcast_addr
))
614 if (batman_packet
->version
!= COMPAT_VERSION
) {
615 bat_dbg(DBG_BATMAN
, bat_priv
,
616 "Drop packet: incompatible batman version (%i)\n",
617 batman_packet
->version
);
622 bat_dbg(DBG_BATMAN
, bat_priv
,
623 "Drop packet: received my own broadcast (sender: %pM"
630 bat_dbg(DBG_BATMAN
, bat_priv
, "Drop packet: "
631 "ignoring all packets with broadcast source addr (sender: %pM"
632 ")\n", ethhdr
->h_source
);
640 orig_neigh_node
= get_orig_node(bat_priv
, ethhdr
->h_source
);
642 if (!orig_neigh_node
)
645 /* neighbor has to indicate direct link and it has to
646 * come via the corresponding interface */
647 /* if received seqno equals last send seqno save new
648 * seqno for bidirectional check */
649 if (has_directlink_flag
&&
650 compare_orig(if_incoming
->net_dev
->dev_addr
,
651 batman_packet
->orig
) &&
652 (batman_packet
->seqno
- if_incoming_seqno
+ 2 == 0)) {
653 offset
= if_incoming
->if_num
* NUM_WORDS
;
654 word
= &(orig_neigh_node
->bcast_own
[offset
]);
656 orig_neigh_node
->bcast_own_sum
[if_incoming
->if_num
] =
657 bit_packet_count(word
);
660 bat_dbg(DBG_BATMAN
, bat_priv
, "Drop packet: "
661 "originator packet from myself (via neighbor)\n");
666 bat_dbg(DBG_BATMAN
, bat_priv
,
667 "Drop packet: ignoring all rebroadcast echos (sender: "
668 "%pM)\n", ethhdr
->h_source
);
672 orig_node
= get_orig_node(bat_priv
, batman_packet
->orig
);
676 is_duplicate
= count_real_packets(ethhdr
, batman_packet
, if_incoming
);
678 if (is_duplicate
== -1) {
679 bat_dbg(DBG_BATMAN
, bat_priv
,
680 "Drop packet: packet within seqno protection time "
681 "(sender: %pM)\n", ethhdr
->h_source
);
685 if (batman_packet
->tq
== 0) {
686 bat_dbg(DBG_BATMAN
, bat_priv
,
687 "Drop packet: originator packet with tq equal 0\n");
691 /* avoid temporary routing loops */
692 if ((orig_node
->router
) &&
693 (orig_node
->router
->orig_node
->router
) &&
694 (compare_orig(orig_node
->router
->addr
,
695 batman_packet
->prev_sender
)) &&
696 !(compare_orig(batman_packet
->orig
, batman_packet
->prev_sender
)) &&
697 (compare_orig(orig_node
->router
->addr
,
698 orig_node
->router
->orig_node
->router
->addr
))) {
699 bat_dbg(DBG_BATMAN
, bat_priv
,
700 "Drop packet: ignoring all rebroadcast packets that "
701 "may make me loop (sender: %pM)\n", ethhdr
->h_source
);
705 /* if sender is a direct neighbor the sender mac equals
707 orig_neigh_node
= (is_single_hop_neigh
?
709 get_orig_node(bat_priv
, ethhdr
->h_source
));
710 if (!orig_neigh_node
)
713 /* drop packet if sender is not a direct neighbor and if we
714 * don't route towards it */
715 if (!is_single_hop_neigh
&& (!orig_neigh_node
->router
)) {
716 bat_dbg(DBG_BATMAN
, bat_priv
,
717 "Drop packet: OGM via unknown neighbor!\n");
721 is_bidirectional
= is_bidirectional_neigh(orig_node
, orig_neigh_node
,
722 batman_packet
, if_incoming
);
724 /* update ranking if it is not a duplicate or has the same
725 * seqno and similar ttl as the non-duplicate */
726 if (is_bidirectional
&&
728 ((orig_node
->last_real_seqno
== batman_packet
->seqno
) &&
729 (orig_node
->last_ttl
- 3 <= batman_packet
->ttl
))))
730 update_orig(bat_priv
, orig_node
, ethhdr
, batman_packet
,
731 if_incoming
, hna_buff
, hna_buff_len
, is_duplicate
);
733 mark_bonding_address(bat_priv
, orig_node
,
734 orig_neigh_node
, batman_packet
);
735 update_bonding_candidates(bat_priv
, orig_node
);
737 /* is single hop (direct) neighbor */
738 if (is_single_hop_neigh
) {
740 /* mark direct link on incoming interface */
741 schedule_forward_packet(orig_node
, ethhdr
, batman_packet
,
742 1, hna_buff_len
, if_incoming
);
744 bat_dbg(DBG_BATMAN
, bat_priv
, "Forwarding packet: "
745 "rebroadcast neighbor packet with direct link flag\n");
749 /* multihop originator */
750 if (!is_bidirectional
) {
751 bat_dbg(DBG_BATMAN
, bat_priv
,
752 "Drop packet: not received via bidirectional link\n");
757 bat_dbg(DBG_BATMAN
, bat_priv
,
758 "Drop packet: duplicate packet received\n");
762 bat_dbg(DBG_BATMAN
, bat_priv
,
763 "Forwarding packet: rebroadcast originator packet\n");
764 schedule_forward_packet(orig_node
, ethhdr
, batman_packet
,
765 0, hna_buff_len
, if_incoming
);
768 int recv_bat_packet(struct sk_buff
*skb
, struct batman_if
*batman_if
)
770 struct bat_priv
*bat_priv
= netdev_priv(batman_if
->soft_iface
);
771 struct ethhdr
*ethhdr
;
773 /* drop packet if it has not necessary minimum size */
774 if (unlikely(!pskb_may_pull(skb
, sizeof(struct batman_packet
))))
777 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
779 /* packet with broadcast indication but unicast recipient */
780 if (!is_broadcast_ether_addr(ethhdr
->h_dest
))
783 /* packet with broadcast sender address */
784 if (is_broadcast_ether_addr(ethhdr
->h_source
))
787 /* create a copy of the skb, if needed, to modify it. */
788 if (skb_cow(skb
, 0) < 0)
791 /* keep skb linear */
792 if (skb_linearize(skb
) < 0)
795 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
797 spin_lock_bh(&bat_priv
->orig_hash_lock
);
798 receive_aggr_bat_packet(ethhdr
,
802 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
805 return NET_RX_SUCCESS
;
808 static int recv_my_icmp_packet(struct bat_priv
*bat_priv
,
809 struct sk_buff
*skb
, size_t icmp_len
)
811 struct orig_node
*orig_node
;
812 struct icmp_packet_rr
*icmp_packet
;
813 struct ethhdr
*ethhdr
;
814 struct batman_if
*batman_if
;
816 uint8_t dstaddr
[ETH_ALEN
];
818 icmp_packet
= (struct icmp_packet_rr
*)skb
->data
;
819 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
821 /* add data to device queue */
822 if (icmp_packet
->msg_type
!= ECHO_REQUEST
) {
823 bat_socket_receive_packet(icmp_packet
, icmp_len
);
827 if (!bat_priv
->primary_if
)
830 /* answer echo request (ping) */
831 /* get routing information */
832 spin_lock_bh(&bat_priv
->orig_hash_lock
);
833 orig_node
= ((struct orig_node
*)hash_find(bat_priv
->orig_hash
,
834 compare_orig
, choose_orig
,
838 if ((orig_node
) && (orig_node
->router
)) {
840 /* don't lock while sending the packets ... we therefore
841 * copy the required data before sending */
842 batman_if
= orig_node
->router
->if_incoming
;
843 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
844 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
846 /* create a copy of the skb, if needed, to modify it. */
847 if (skb_cow(skb
, sizeof(struct ethhdr
)) < 0)
850 icmp_packet
= (struct icmp_packet_rr
*)skb
->data
;
851 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
853 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
854 memcpy(icmp_packet
->orig
,
855 bat_priv
->primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
856 icmp_packet
->msg_type
= ECHO_REPLY
;
857 icmp_packet
->ttl
= TTL
;
859 send_skb_packet(skb
, batman_if
, dstaddr
);
860 ret
= NET_RX_SUCCESS
;
863 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
868 static int recv_icmp_ttl_exceeded(struct bat_priv
*bat_priv
,
869 struct sk_buff
*skb
, size_t icmp_len
)
871 struct orig_node
*orig_node
;
872 struct icmp_packet
*icmp_packet
;
873 struct ethhdr
*ethhdr
;
874 struct batman_if
*batman_if
;
876 uint8_t dstaddr
[ETH_ALEN
];
878 icmp_packet
= (struct icmp_packet
*)skb
->data
;
879 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
881 /* send TTL exceeded if packet is an echo request (traceroute) */
882 if (icmp_packet
->msg_type
!= ECHO_REQUEST
) {
883 pr_debug("Warning - can't forward icmp packet from %pM to "
884 "%pM: ttl exceeded\n", icmp_packet
->orig
,
889 if (!bat_priv
->primary_if
)
892 /* get routing information */
893 spin_lock_bh(&bat_priv
->orig_hash_lock
);
894 orig_node
= ((struct orig_node
*)
895 hash_find(bat_priv
->orig_hash
, compare_orig
, choose_orig
,
899 if ((orig_node
) && (orig_node
->router
)) {
901 /* don't lock while sending the packets ... we therefore
902 * copy the required data before sending */
903 batman_if
= orig_node
->router
->if_incoming
;
904 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
905 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
907 /* create a copy of the skb, if needed, to modify it. */
908 if (skb_cow(skb
, sizeof(struct ethhdr
)) < 0)
911 icmp_packet
= (struct icmp_packet
*) skb
->data
;
912 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
914 memcpy(icmp_packet
->dst
, icmp_packet
->orig
, ETH_ALEN
);
915 memcpy(icmp_packet
->orig
,
916 bat_priv
->primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
917 icmp_packet
->msg_type
= TTL_EXCEEDED
;
918 icmp_packet
->ttl
= TTL
;
920 send_skb_packet(skb
, batman_if
, dstaddr
);
921 ret
= NET_RX_SUCCESS
;
924 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
930 int recv_icmp_packet(struct sk_buff
*skb
, struct batman_if
*recv_if
)
932 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
933 struct icmp_packet_rr
*icmp_packet
;
934 struct ethhdr
*ethhdr
;
935 struct orig_node
*orig_node
;
936 struct batman_if
*batman_if
;
937 int hdr_size
= sizeof(struct icmp_packet
);
939 uint8_t dstaddr
[ETH_ALEN
];
942 * we truncate all incoming icmp packets if they don't match our size
944 if (skb
->len
>= sizeof(struct icmp_packet_rr
))
945 hdr_size
= sizeof(struct icmp_packet_rr
);
947 /* drop packet if it has not necessary minimum size */
948 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
951 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
953 /* packet with unicast indication but broadcast recipient */
954 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
957 /* packet with broadcast sender address */
958 if (is_broadcast_ether_addr(ethhdr
->h_source
))
962 if (!is_my_mac(ethhdr
->h_dest
))
965 icmp_packet
= (struct icmp_packet_rr
*)skb
->data
;
967 /* add record route information if not full */
968 if ((hdr_size
== sizeof(struct icmp_packet_rr
)) &&
969 (icmp_packet
->rr_cur
< BAT_RR_LEN
)) {
970 memcpy(&(icmp_packet
->rr
[icmp_packet
->rr_cur
]),
971 ethhdr
->h_dest
, ETH_ALEN
);
972 icmp_packet
->rr_cur
++;
976 if (is_my_mac(icmp_packet
->dst
))
977 return recv_my_icmp_packet(bat_priv
, skb
, hdr_size
);
980 if (icmp_packet
->ttl
< 2)
981 return recv_icmp_ttl_exceeded(bat_priv
, skb
, hdr_size
);
985 /* get routing information */
986 spin_lock_bh(&bat_priv
->orig_hash_lock
);
987 orig_node
= ((struct orig_node
*)
988 hash_find(bat_priv
->orig_hash
, compare_orig
, choose_orig
,
991 if ((orig_node
) && (orig_node
->router
)) {
993 /* don't lock while sending the packets ... we therefore
994 * copy the required data before sending */
995 batman_if
= orig_node
->router
->if_incoming
;
996 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
997 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
999 /* create a copy of the skb, if needed, to modify it. */
1000 if (skb_cow(skb
, sizeof(struct ethhdr
)) < 0)
1003 icmp_packet
= (struct icmp_packet_rr
*)skb
->data
;
1004 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1010 send_skb_packet(skb
, batman_if
, dstaddr
);
1011 ret
= NET_RX_SUCCESS
;
1014 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
1019 /* find a suitable router for this originator, and use
1020 * bonding if possible. */
1021 struct neigh_node
*find_router(struct bat_priv
*bat_priv
,
1022 struct orig_node
*orig_node
,
1023 struct batman_if
*recv_if
)
1025 struct orig_node
*primary_orig_node
;
1026 struct orig_node
*router_orig
;
1027 struct neigh_node
*router
, *first_candidate
, *best_router
;
1028 static uint8_t zero_mac
[ETH_ALEN
] = {0, 0, 0, 0, 0, 0};
1029 int bonding_enabled
;
1034 if (!orig_node
->router
)
1037 /* without bonding, the first node should
1038 * always choose the default router. */
1040 bonding_enabled
= atomic_read(&bat_priv
->bonding
);
1042 if ((!recv_if
) && (!bonding_enabled
))
1043 return orig_node
->router
;
1045 router_orig
= orig_node
->router
->orig_node
;
1047 /* if we have something in the primary_addr, we can search
1048 * for a potential bonding candidate. */
1049 if (memcmp(router_orig
->primary_addr
, zero_mac
, ETH_ALEN
) == 0)
1050 return orig_node
->router
;
1052 /* find the orig_node which has the primary interface. might
1053 * even be the same as our router_orig in many cases */
1055 if (memcmp(router_orig
->primary_addr
,
1056 router_orig
->orig
, ETH_ALEN
) == 0) {
1057 primary_orig_node
= router_orig
;
1059 primary_orig_node
= hash_find(bat_priv
->orig_hash
, compare_orig
,
1061 router_orig
->primary_addr
);
1063 if (!primary_orig_node
)
1064 return orig_node
->router
;
1067 /* with less than 2 candidates, we can't do any
1068 * bonding and prefer the original router. */
1070 if (primary_orig_node
->bond
.candidates
< 2)
1071 return orig_node
->router
;
1074 /* all nodes between should choose a candidate which
1075 * is is not on the interface where the packet came
1077 first_candidate
= primary_orig_node
->bond
.selected
;
1078 router
= first_candidate
;
1080 if (bonding_enabled
) {
1081 /* in the bonding case, send the packets in a round
1082 * robin fashion over the remaining interfaces. */
1084 /* recv_if == NULL on the first node. */
1085 if (router
->if_incoming
!= recv_if
)
1088 router
= router
->next_bond_candidate
;
1089 } while (router
!= first_candidate
);
1091 primary_orig_node
->bond
.selected
= router
->next_bond_candidate
;
1094 /* if bonding is disabled, use the best of the
1095 * remaining candidates which are not using
1096 * this interface. */
1097 best_router
= first_candidate
;
1100 /* recv_if == NULL on the first node. */
1101 if ((router
->if_incoming
!= recv_if
) &&
1102 (router
->tq_avg
> best_router
->tq_avg
))
1103 best_router
= router
;
1105 router
= router
->next_bond_candidate
;
1106 } while (router
!= first_candidate
);
1108 router
= best_router
;
1114 static int check_unicast_packet(struct sk_buff
*skb
, int hdr_size
)
1116 struct ethhdr
*ethhdr
;
1118 /* drop packet if it has not necessary minimum size */
1119 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1122 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1124 /* packet with unicast indication but broadcast recipient */
1125 if (is_broadcast_ether_addr(ethhdr
->h_dest
))
1128 /* packet with broadcast sender address */
1129 if (is_broadcast_ether_addr(ethhdr
->h_source
))
1133 if (!is_my_mac(ethhdr
->h_dest
))
1139 int route_unicast_packet(struct sk_buff
*skb
, struct batman_if
*recv_if
,
1142 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1143 struct orig_node
*orig_node
;
1144 struct neigh_node
*router
;
1145 struct batman_if
*batman_if
;
1146 uint8_t dstaddr
[ETH_ALEN
];
1147 struct unicast_packet
*unicast_packet
;
1148 struct ethhdr
*ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1150 struct sk_buff
*new_skb
;
1152 unicast_packet
= (struct unicast_packet
*)skb
->data
;
1155 if (unicast_packet
->ttl
< 2) {
1156 pr_debug("Warning - can't forward unicast packet from %pM to "
1157 "%pM: ttl exceeded\n", ethhdr
->h_source
,
1158 unicast_packet
->dest
);
1162 /* get routing information */
1163 spin_lock_bh(&bat_priv
->orig_hash_lock
);
1164 orig_node
= ((struct orig_node
*)
1165 hash_find(bat_priv
->orig_hash
, compare_orig
, choose_orig
,
1166 unicast_packet
->dest
));
1168 router
= find_router(bat_priv
, orig_node
, recv_if
);
1171 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
1175 /* don't lock while sending the packets ... we therefore
1176 * copy the required data before sending */
1178 batman_if
= router
->if_incoming
;
1179 memcpy(dstaddr
, router
->addr
, ETH_ALEN
);
1181 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
1183 /* create a copy of the skb, if needed, to modify it. */
1184 if (skb_cow(skb
, sizeof(struct ethhdr
)) < 0)
1187 unicast_packet
= (struct unicast_packet
*)skb
->data
;
1189 if (unicast_packet
->packet_type
== BAT_UNICAST
&&
1190 atomic_read(&bat_priv
->fragmentation
) &&
1191 skb
->len
> batman_if
->net_dev
->mtu
)
1192 return frag_send_skb(skb
, bat_priv
, batman_if
,
1195 if (unicast_packet
->packet_type
== BAT_UNICAST_FRAG
&&
1196 2 * skb
->len
- hdr_size
<= batman_if
->net_dev
->mtu
) {
1198 ret
= frag_reassemble_skb(skb
, bat_priv
, &new_skb
);
1200 if (ret
== NET_RX_DROP
)
1203 /* packet was buffered for late merge */
1205 return NET_RX_SUCCESS
;
1208 unicast_packet
= (struct unicast_packet
*)skb
->data
;
1212 unicast_packet
->ttl
--;
1215 send_skb_packet(skb
, batman_if
, dstaddr
);
1217 return NET_RX_SUCCESS
;
1220 int recv_unicast_packet(struct sk_buff
*skb
, struct batman_if
*recv_if
)
1222 struct unicast_packet
*unicast_packet
;
1223 int hdr_size
= sizeof(struct unicast_packet
);
1225 if (check_unicast_packet(skb
, hdr_size
) < 0)
1228 unicast_packet
= (struct unicast_packet
*)skb
->data
;
1231 if (is_my_mac(unicast_packet
->dest
)) {
1232 interface_rx(recv_if
->soft_iface
, skb
, recv_if
, hdr_size
);
1233 return NET_RX_SUCCESS
;
1236 return route_unicast_packet(skb
, recv_if
, hdr_size
);
1239 int recv_ucast_frag_packet(struct sk_buff
*skb
, struct batman_if
*recv_if
)
1241 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1242 struct unicast_frag_packet
*unicast_packet
;
1243 int hdr_size
= sizeof(struct unicast_frag_packet
);
1244 struct sk_buff
*new_skb
= NULL
;
1247 if (check_unicast_packet(skb
, hdr_size
) < 0)
1250 unicast_packet
= (struct unicast_frag_packet
*)skb
->data
;
1253 if (is_my_mac(unicast_packet
->dest
)) {
1255 ret
= frag_reassemble_skb(skb
, bat_priv
, &new_skb
);
1257 if (ret
== NET_RX_DROP
)
1260 /* packet was buffered for late merge */
1262 return NET_RX_SUCCESS
;
1264 interface_rx(recv_if
->soft_iface
, new_skb
, recv_if
,
1265 sizeof(struct unicast_packet
));
1266 return NET_RX_SUCCESS
;
1269 return route_unicast_packet(skb
, recv_if
, hdr_size
);
1273 int recv_bcast_packet(struct sk_buff
*skb
, struct batman_if
*recv_if
)
1275 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1276 struct orig_node
*orig_node
;
1277 struct bcast_packet
*bcast_packet
;
1278 struct ethhdr
*ethhdr
;
1279 int hdr_size
= sizeof(struct bcast_packet
);
1282 /* drop packet if it has not necessary minimum size */
1283 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1286 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1288 /* packet with broadcast indication but unicast recipient */
1289 if (!is_broadcast_ether_addr(ethhdr
->h_dest
))
1292 /* packet with broadcast sender address */
1293 if (is_broadcast_ether_addr(ethhdr
->h_source
))
1296 /* ignore broadcasts sent by myself */
1297 if (is_my_mac(ethhdr
->h_source
))
1300 bcast_packet
= (struct bcast_packet
*)skb
->data
;
1302 /* ignore broadcasts originated by myself */
1303 if (is_my_mac(bcast_packet
->orig
))
1306 if (bcast_packet
->ttl
< 2)
1309 spin_lock_bh(&bat_priv
->orig_hash_lock
);
1310 orig_node
= ((struct orig_node
*)
1311 hash_find(bat_priv
->orig_hash
, compare_orig
, choose_orig
,
1312 bcast_packet
->orig
));
1315 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
1319 /* check whether the packet is a duplicate */
1320 if (get_bit_status(orig_node
->bcast_bits
,
1321 orig_node
->last_bcast_seqno
,
1322 ntohl(bcast_packet
->seqno
))) {
1323 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
1327 seq_diff
= ntohl(bcast_packet
->seqno
) - orig_node
->last_bcast_seqno
;
1329 /* check whether the packet is old and the host just restarted. */
1330 if (window_protected(bat_priv
, seq_diff
,
1331 &orig_node
->bcast_seqno_reset
)) {
1332 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
1336 /* mark broadcast in flood history, update window position
1338 if (bit_get_packet(bat_priv
, orig_node
->bcast_bits
, seq_diff
, 1))
1339 orig_node
->last_bcast_seqno
= ntohl(bcast_packet
->seqno
);
1341 spin_unlock_bh(&bat_priv
->orig_hash_lock
);
1342 /* rebroadcast packet */
1343 add_bcast_packet_to_list(bat_priv
, skb
);
1345 /* broadcast for me */
1346 interface_rx(recv_if
->soft_iface
, skb
, recv_if
, hdr_size
);
1348 return NET_RX_SUCCESS
;
1351 int recv_vis_packet(struct sk_buff
*skb
, struct batman_if
*recv_if
)
1353 struct vis_packet
*vis_packet
;
1354 struct ethhdr
*ethhdr
;
1355 struct bat_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1356 int hdr_size
= sizeof(struct vis_packet
);
1358 /* keep skb linear */
1359 if (skb_linearize(skb
) < 0)
1362 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1365 vis_packet
= (struct vis_packet
*)skb
->data
;
1366 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1369 if (!is_my_mac(ethhdr
->h_dest
))
1372 /* ignore own packets */
1373 if (is_my_mac(vis_packet
->vis_orig
))
1376 if (is_my_mac(vis_packet
->sender_orig
))
1379 switch (vis_packet
->vis_type
) {
1380 case VIS_TYPE_SERVER_SYNC
:
1381 receive_server_sync_packet(bat_priv
, vis_packet
,
1385 case VIS_TYPE_CLIENT_UPDATE
:
1386 receive_client_update_packet(bat_priv
, vis_packet
,
1390 default: /* ignore unknown packet */
1394 /* We take a copy of the data in the packet, so we should
1395 always free the skbuf. */