1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "translation-table.h"
22 #include "ring_buffer.h"
23 #include "originator.h"
25 #include "gateway_common.h"
26 #include "gateway_client.h"
27 #include "hard-interface.h"
31 static struct neigh_node
*bat_iv_ogm_neigh_new(struct hard_iface
*hard_iface
,
32 const uint8_t *neigh_addr
,
33 struct orig_node
*orig_node
,
34 struct orig_node
*orig_neigh
,
37 struct neigh_node
*neigh_node
;
39 neigh_node
= batadv_neigh_node_new(hard_iface
, neigh_addr
,
44 INIT_LIST_HEAD(&neigh_node
->bonding_list
);
46 neigh_node
->orig_node
= orig_neigh
;
47 neigh_node
->if_incoming
= hard_iface
;
49 spin_lock_bh(&orig_node
->neigh_list_lock
);
50 hlist_add_head_rcu(&neigh_node
->list
, &orig_node
->neigh_list
);
51 spin_unlock_bh(&orig_node
->neigh_list_lock
);
57 static int bat_iv_ogm_iface_enable(struct hard_iface
*hard_iface
)
59 struct batman_ogm_packet
*batman_ogm_packet
;
60 uint32_t random_seqno
;
63 /* randomize initial seqno to avoid collision */
64 get_random_bytes(&random_seqno
, sizeof(random_seqno
));
65 atomic_set(&hard_iface
->seqno
, random_seqno
);
67 hard_iface
->packet_len
= BATMAN_OGM_HLEN
;
68 hard_iface
->packet_buff
= kmalloc(hard_iface
->packet_len
, GFP_ATOMIC
);
70 if (!hard_iface
->packet_buff
)
73 batman_ogm_packet
= (struct batman_ogm_packet
*)hard_iface
->packet_buff
;
74 batman_ogm_packet
->header
.packet_type
= BAT_IV_OGM
;
75 batman_ogm_packet
->header
.version
= COMPAT_VERSION
;
76 batman_ogm_packet
->header
.ttl
= 2;
77 batman_ogm_packet
->flags
= NO_FLAGS
;
78 batman_ogm_packet
->tq
= TQ_MAX_VALUE
;
79 batman_ogm_packet
->tt_num_changes
= 0;
80 batman_ogm_packet
->ttvn
= 0;
88 static void bat_iv_ogm_iface_disable(struct hard_iface
*hard_iface
)
90 kfree(hard_iface
->packet_buff
);
91 hard_iface
->packet_buff
= NULL
;
94 static void bat_iv_ogm_iface_update_mac(struct hard_iface
*hard_iface
)
96 struct batman_ogm_packet
*batman_ogm_packet
;
98 batman_ogm_packet
= (struct batman_ogm_packet
*)hard_iface
->packet_buff
;
99 memcpy(batman_ogm_packet
->orig
,
100 hard_iface
->net_dev
->dev_addr
, ETH_ALEN
);
101 memcpy(batman_ogm_packet
->prev_sender
,
102 hard_iface
->net_dev
->dev_addr
, ETH_ALEN
);
105 static void bat_iv_ogm_primary_iface_set(struct hard_iface
*hard_iface
)
107 struct batman_ogm_packet
*batman_ogm_packet
;
109 batman_ogm_packet
= (struct batman_ogm_packet
*)hard_iface
->packet_buff
;
110 batman_ogm_packet
->flags
= PRIMARIES_FIRST_HOP
;
111 batman_ogm_packet
->header
.ttl
= TTL
;
114 /* when do we schedule our own ogm to be sent */
115 static unsigned long bat_iv_ogm_emit_send_time(const struct bat_priv
*bat_priv
)
117 return jiffies
+ msecs_to_jiffies(
118 atomic_read(&bat_priv
->orig_interval
) -
119 JITTER
+ (random32() % 2*JITTER
));
122 /* when do we schedule a ogm packet to be sent */
123 static unsigned long bat_iv_ogm_fwd_send_time(void)
125 return jiffies
+ msecs_to_jiffies(random32() % (JITTER
/2));
128 /* apply hop penalty for a normal link */
129 static uint8_t hop_penalty(uint8_t tq
, const struct bat_priv
*bat_priv
)
131 int hop_penalty
= atomic_read(&bat_priv
->hop_penalty
);
132 return (tq
* (TQ_MAX_VALUE
- hop_penalty
)) / (TQ_MAX_VALUE
);
135 /* is there another aggregated packet here? */
136 static int bat_iv_ogm_aggr_packet(int buff_pos
, int packet_len
,
139 int next_buff_pos
= 0;
141 next_buff_pos
+= buff_pos
+ BATMAN_OGM_HLEN
;
142 next_buff_pos
+= batadv_tt_len(tt_num_changes
);
144 return (next_buff_pos
<= packet_len
) &&
145 (next_buff_pos
<= MAX_AGGREGATION_BYTES
);
148 /* send a batman ogm to a given interface */
149 static void bat_iv_ogm_send_to_if(struct forw_packet
*forw_packet
,
150 struct hard_iface
*hard_iface
)
152 struct bat_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
156 struct batman_ogm_packet
*batman_ogm_packet
;
159 if (hard_iface
->if_status
!= IF_ACTIVE
)
164 batman_ogm_packet
= (struct batman_ogm_packet
*)forw_packet
->skb
->data
;
166 /* adjust all flags and log packets */
167 while (bat_iv_ogm_aggr_packet(buff_pos
, forw_packet
->packet_len
,
168 batman_ogm_packet
->tt_num_changes
)) {
170 /* we might have aggregated direct link packets with an
171 * ordinary base packet
173 if ((forw_packet
->direct_link_flags
& (1 << packet_num
)) &&
174 (forw_packet
->if_incoming
== hard_iface
))
175 batman_ogm_packet
->flags
|= DIRECTLINK
;
177 batman_ogm_packet
->flags
&= ~DIRECTLINK
;
179 fwd_str
= (packet_num
> 0 ? "Forwarding" : (forw_packet
->own
?
182 bat_dbg(DBG_BATMAN
, bat_priv
,
183 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n",
184 fwd_str
, (packet_num
> 0 ? "aggregated " : ""),
185 batman_ogm_packet
->orig
,
186 ntohl(batman_ogm_packet
->seqno
),
187 batman_ogm_packet
->tq
, batman_ogm_packet
->header
.ttl
,
188 (batman_ogm_packet
->flags
& DIRECTLINK
?
190 batman_ogm_packet
->ttvn
, hard_iface
->net_dev
->name
,
191 hard_iface
->net_dev
->dev_addr
);
193 buff_pos
+= BATMAN_OGM_HLEN
;
194 buff_pos
+= batadv_tt_len(batman_ogm_packet
->tt_num_changes
);
196 batman_ogm_packet
= (struct batman_ogm_packet
*)
197 (forw_packet
->skb
->data
+ buff_pos
);
200 /* create clone because function is called more than once */
201 skb
= skb_clone(forw_packet
->skb
, GFP_ATOMIC
);
203 batadv_inc_counter(bat_priv
, BAT_CNT_MGMT_TX
);
204 batadv_add_counter(bat_priv
, BAT_CNT_MGMT_TX_BYTES
,
205 skb
->len
+ ETH_HLEN
);
206 batadv_send_skb_packet(skb
, hard_iface
, batadv_broadcast_addr
);
210 /* send a batman ogm packet */
211 static void bat_iv_ogm_emit(struct forw_packet
*forw_packet
)
213 struct hard_iface
*hard_iface
;
214 struct net_device
*soft_iface
;
215 struct bat_priv
*bat_priv
;
216 struct hard_iface
*primary_if
= NULL
;
217 struct batman_ogm_packet
*batman_ogm_packet
;
218 unsigned char directlink
;
220 batman_ogm_packet
= (struct batman_ogm_packet
*)
221 (forw_packet
->skb
->data
);
222 directlink
= (batman_ogm_packet
->flags
& DIRECTLINK
? 1 : 0);
224 if (!forw_packet
->if_incoming
) {
225 pr_err("Error - can't forward packet: incoming iface not specified\n");
229 soft_iface
= forw_packet
->if_incoming
->soft_iface
;
230 bat_priv
= netdev_priv(soft_iface
);
232 if (forw_packet
->if_incoming
->if_status
!= IF_ACTIVE
)
235 primary_if
= primary_if_get_selected(bat_priv
);
239 /* multihomed peer assumed
240 * non-primary OGMs are only broadcasted on their interface
242 if ((directlink
&& (batman_ogm_packet
->header
.ttl
== 1)) ||
243 (forw_packet
->own
&& (forw_packet
->if_incoming
!= primary_if
))) {
245 /* FIXME: what about aggregated packets ? */
246 bat_dbg(DBG_BATMAN
, bat_priv
,
247 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
248 (forw_packet
->own
? "Sending own" : "Forwarding"),
249 batman_ogm_packet
->orig
,
250 ntohl(batman_ogm_packet
->seqno
),
251 batman_ogm_packet
->header
.ttl
,
252 forw_packet
->if_incoming
->net_dev
->name
,
253 forw_packet
->if_incoming
->net_dev
->dev_addr
);
255 /* skb is only used once and than forw_packet is free'd */
256 batadv_send_skb_packet(forw_packet
->skb
,
257 forw_packet
->if_incoming
,
258 batadv_broadcast_addr
);
259 forw_packet
->skb
= NULL
;
264 /* broadcast on every interface */
266 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
267 if (hard_iface
->soft_iface
!= soft_iface
)
270 bat_iv_ogm_send_to_if(forw_packet
, hard_iface
);
276 hardif_free_ref(primary_if
);
279 /* return true if new_packet can be aggregated with forw_packet */
280 static bool bat_iv_ogm_can_aggregate(const struct batman_ogm_packet
281 *new_batman_ogm_packet
,
282 struct bat_priv
*bat_priv
,
283 int packet_len
, unsigned long send_time
,
285 const struct hard_iface
*if_incoming
,
286 const struct forw_packet
*forw_packet
)
288 struct batman_ogm_packet
*batman_ogm_packet
;
289 int aggregated_bytes
= forw_packet
->packet_len
+ packet_len
;
290 struct hard_iface
*primary_if
= NULL
;
293 batman_ogm_packet
= (struct batman_ogm_packet
*)forw_packet
->skb
->data
;
295 /* we can aggregate the current packet to this aggregated packet
298 * - the send time is within our MAX_AGGREGATION_MS time
299 * - the resulting packet wont be bigger than
300 * MAX_AGGREGATION_BYTES
302 if (time_before(send_time
, forw_packet
->send_time
) &&
303 time_after_eq(send_time
+ msecs_to_jiffies(MAX_AGGREGATION_MS
),
304 forw_packet
->send_time
) &&
305 (aggregated_bytes
<= MAX_AGGREGATION_BYTES
)) {
307 /* check aggregation compatibility
308 * -> direct link packets are broadcasted on
309 * their interface only
310 * -> aggregate packet if the current packet is
311 * a "global" packet as well as the base
314 primary_if
= primary_if_get_selected(bat_priv
);
318 /* packets without direct link flag and high TTL
319 * are flooded through the net
322 (!(batman_ogm_packet
->flags
& DIRECTLINK
)) &&
323 (batman_ogm_packet
->header
.ttl
!= 1) &&
325 /* own packets originating non-primary
326 * interfaces leave only that interface
328 ((!forw_packet
->own
) ||
329 (forw_packet
->if_incoming
== primary_if
))) {
334 /* if the incoming packet is sent via this one
335 * interface only - we still can aggregate
338 (new_batman_ogm_packet
->header
.ttl
== 1) &&
339 (forw_packet
->if_incoming
== if_incoming
) &&
341 /* packets from direct neighbors or
342 * own secondary interface packets
343 * (= secondary interface packets in general)
345 (batman_ogm_packet
->flags
& DIRECTLINK
||
347 forw_packet
->if_incoming
!= primary_if
))) {
355 hardif_free_ref(primary_if
);
359 /* create a new aggregated packet and add this packet to it */
360 static void bat_iv_ogm_aggregate_new(const unsigned char *packet_buff
,
361 int packet_len
, unsigned long send_time
,
363 struct hard_iface
*if_incoming
,
366 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
367 struct forw_packet
*forw_packet_aggr
;
368 unsigned char *skb_buff
;
370 if (!atomic_inc_not_zero(&if_incoming
->refcount
))
373 /* own packet should always be scheduled */
375 if (!atomic_dec_not_zero(&bat_priv
->batman_queue_left
)) {
376 bat_dbg(DBG_BATMAN
, bat_priv
,
377 "batman packet queue full\n");
382 forw_packet_aggr
= kmalloc(sizeof(*forw_packet_aggr
), GFP_ATOMIC
);
383 if (!forw_packet_aggr
) {
385 atomic_inc(&bat_priv
->batman_queue_left
);
389 if ((atomic_read(&bat_priv
->aggregated_ogms
)) &&
390 (packet_len
< MAX_AGGREGATION_BYTES
))
391 forw_packet_aggr
->skb
= dev_alloc_skb(MAX_AGGREGATION_BYTES
+
394 forw_packet_aggr
->skb
= dev_alloc_skb(packet_len
+ ETH_HLEN
);
396 if (!forw_packet_aggr
->skb
) {
398 atomic_inc(&bat_priv
->batman_queue_left
);
399 kfree(forw_packet_aggr
);
402 skb_reserve(forw_packet_aggr
->skb
, ETH_HLEN
);
404 INIT_HLIST_NODE(&forw_packet_aggr
->list
);
406 skb_buff
= skb_put(forw_packet_aggr
->skb
, packet_len
);
407 forw_packet_aggr
->packet_len
= packet_len
;
408 memcpy(skb_buff
, packet_buff
, packet_len
);
410 forw_packet_aggr
->own
= own_packet
;
411 forw_packet_aggr
->if_incoming
= if_incoming
;
412 forw_packet_aggr
->num_packets
= 0;
413 forw_packet_aggr
->direct_link_flags
= NO_FLAGS
;
414 forw_packet_aggr
->send_time
= send_time
;
416 /* save packet direct link flag status */
418 forw_packet_aggr
->direct_link_flags
|= 1;
420 /* add new packet to packet list */
421 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
422 hlist_add_head(&forw_packet_aggr
->list
, &bat_priv
->forw_bat_list
);
423 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
425 /* start timer for this packet */
426 INIT_DELAYED_WORK(&forw_packet_aggr
->delayed_work
,
427 batadv_send_outstanding_bat_ogm_packet
);
428 queue_delayed_work(batadv_event_workqueue
,
429 &forw_packet_aggr
->delayed_work
,
430 send_time
- jiffies
);
434 hardif_free_ref(if_incoming
);
437 /* aggregate a new packet into the existing ogm packet */
438 static void bat_iv_ogm_aggregate(struct forw_packet
*forw_packet_aggr
,
439 const unsigned char *packet_buff
,
440 int packet_len
, bool direct_link
)
442 unsigned char *skb_buff
;
444 skb_buff
= skb_put(forw_packet_aggr
->skb
, packet_len
);
445 memcpy(skb_buff
, packet_buff
, packet_len
);
446 forw_packet_aggr
->packet_len
+= packet_len
;
447 forw_packet_aggr
->num_packets
++;
449 /* save packet direct link flag status */
451 forw_packet_aggr
->direct_link_flags
|=
452 (1 << forw_packet_aggr
->num_packets
);
455 static void bat_iv_ogm_queue_add(struct bat_priv
*bat_priv
,
456 unsigned char *packet_buff
,
457 int packet_len
, struct hard_iface
*if_incoming
,
458 int own_packet
, unsigned long send_time
)
460 /* _aggr -> pointer to the packet we want to aggregate with
461 * _pos -> pointer to the position in the queue
463 struct forw_packet
*forw_packet_aggr
= NULL
, *forw_packet_pos
= NULL
;
464 struct hlist_node
*tmp_node
;
465 struct batman_ogm_packet
*batman_ogm_packet
;
468 batman_ogm_packet
= (struct batman_ogm_packet
*)packet_buff
;
469 direct_link
= batman_ogm_packet
->flags
& DIRECTLINK
? 1 : 0;
471 /* find position for the packet in the forward queue */
472 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
473 /* own packets are not to be aggregated */
474 if ((atomic_read(&bat_priv
->aggregated_ogms
)) && (!own_packet
)) {
475 hlist_for_each_entry(forw_packet_pos
, tmp_node
,
476 &bat_priv
->forw_bat_list
, list
) {
477 if (bat_iv_ogm_can_aggregate(batman_ogm_packet
,
478 bat_priv
, packet_len
,
479 send_time
, direct_link
,
482 forw_packet_aggr
= forw_packet_pos
;
488 /* nothing to aggregate with - either aggregation disabled or no
489 * suitable aggregation packet found
491 if (!forw_packet_aggr
) {
492 /* the following section can run without the lock */
493 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
495 /* if we could not aggregate this packet with one of the others
496 * we hold it back for a while, so that it might be aggregated
500 (atomic_read(&bat_priv
->aggregated_ogms
)))
501 send_time
+= msecs_to_jiffies(MAX_AGGREGATION_MS
);
503 bat_iv_ogm_aggregate_new(packet_buff
, packet_len
,
504 send_time
, direct_link
,
505 if_incoming
, own_packet
);
507 bat_iv_ogm_aggregate(forw_packet_aggr
, packet_buff
,
508 packet_len
, direct_link
);
509 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
513 static void bat_iv_ogm_forward(struct orig_node
*orig_node
,
514 const struct ethhdr
*ethhdr
,
515 struct batman_ogm_packet
*batman_ogm_packet
,
516 bool is_single_hop_neigh
,
517 bool is_from_best_next_hop
,
518 struct hard_iface
*if_incoming
)
520 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
521 uint8_t tt_num_changes
;
523 if (batman_ogm_packet
->header
.ttl
<= 1) {
524 bat_dbg(DBG_BATMAN
, bat_priv
, "ttl exceeded\n");
528 if (!is_from_best_next_hop
) {
529 /* Mark the forwarded packet when it is not coming from our
530 * best next hop. We still need to forward the packet for our
531 * neighbor link quality detection to work in case the packet
532 * originated from a single hop neighbor. Otherwise we can
533 * simply drop the ogm.
535 if (is_single_hop_neigh
)
536 batman_ogm_packet
->flags
|= NOT_BEST_NEXT_HOP
;
541 tt_num_changes
= batman_ogm_packet
->tt_num_changes
;
543 batman_ogm_packet
->header
.ttl
--;
544 memcpy(batman_ogm_packet
->prev_sender
, ethhdr
->h_source
, ETH_ALEN
);
546 /* apply hop penalty */
547 batman_ogm_packet
->tq
= hop_penalty(batman_ogm_packet
->tq
, bat_priv
);
549 bat_dbg(DBG_BATMAN
, bat_priv
,
550 "Forwarding packet: tq: %i, ttl: %i\n",
551 batman_ogm_packet
->tq
, batman_ogm_packet
->header
.ttl
);
553 /* switch of primaries first hop flag when forwarding */
554 batman_ogm_packet
->flags
&= ~PRIMARIES_FIRST_HOP
;
555 if (is_single_hop_neigh
)
556 batman_ogm_packet
->flags
|= DIRECTLINK
;
558 batman_ogm_packet
->flags
&= ~DIRECTLINK
;
560 bat_iv_ogm_queue_add(bat_priv
, (unsigned char *)batman_ogm_packet
,
561 BATMAN_OGM_HLEN
+ batadv_tt_len(tt_num_changes
),
562 if_incoming
, 0, bat_iv_ogm_fwd_send_time());
565 static void bat_iv_ogm_schedule(struct hard_iface
*hard_iface
)
567 struct bat_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
568 struct batman_ogm_packet
*batman_ogm_packet
;
569 struct hard_iface
*primary_if
;
570 int vis_server
, tt_num_changes
= 0;
572 vis_server
= atomic_read(&bat_priv
->vis_mode
);
573 primary_if
= primary_if_get_selected(bat_priv
);
575 if (hard_iface
== primary_if
)
576 tt_num_changes
= batadv_tt_append_diff(bat_priv
,
577 &hard_iface
->packet_buff
,
578 &hard_iface
->packet_len
,
581 batman_ogm_packet
= (struct batman_ogm_packet
*)hard_iface
->packet_buff
;
583 /* change sequence number to network order */
584 batman_ogm_packet
->seqno
=
585 htonl((uint32_t)atomic_read(&hard_iface
->seqno
));
586 atomic_inc(&hard_iface
->seqno
);
588 batman_ogm_packet
->ttvn
= atomic_read(&bat_priv
->ttvn
);
589 batman_ogm_packet
->tt_crc
= htons(bat_priv
->tt_crc
);
590 if (tt_num_changes
>= 0)
591 batman_ogm_packet
->tt_num_changes
= tt_num_changes
;
593 if (vis_server
== VIS_TYPE_SERVER_SYNC
)
594 batman_ogm_packet
->flags
|= VIS_SERVER
;
596 batman_ogm_packet
->flags
&= ~VIS_SERVER
;
598 if ((hard_iface
== primary_if
) &&
599 (atomic_read(&bat_priv
->gw_mode
) == GW_MODE_SERVER
))
600 batman_ogm_packet
->gw_flags
=
601 (uint8_t)atomic_read(&bat_priv
->gw_bandwidth
);
603 batman_ogm_packet
->gw_flags
= NO_FLAGS
;
605 batadv_slide_own_bcast_window(hard_iface
);
606 bat_iv_ogm_queue_add(bat_priv
, hard_iface
->packet_buff
,
607 hard_iface
->packet_len
, hard_iface
, 1,
608 bat_iv_ogm_emit_send_time(bat_priv
));
611 hardif_free_ref(primary_if
);
614 static void bat_iv_ogm_orig_update(struct bat_priv
*bat_priv
,
615 struct orig_node
*orig_node
,
616 const struct ethhdr
*ethhdr
,
617 const struct batman_ogm_packet
619 struct hard_iface
*if_incoming
,
620 const unsigned char *tt_buff
,
623 struct neigh_node
*neigh_node
= NULL
, *tmp_neigh_node
= NULL
;
624 struct neigh_node
*router
= NULL
;
625 struct orig_node
*orig_node_tmp
;
626 struct hlist_node
*node
;
627 uint8_t bcast_own_sum_orig
, bcast_own_sum_neigh
;
629 bat_dbg(DBG_BATMAN
, bat_priv
,
630 "update_originator(): Searching and updating originator entry of received packet\n");
633 hlist_for_each_entry_rcu(tmp_neigh_node
, node
,
634 &orig_node
->neigh_list
, list
) {
635 if (compare_eth(tmp_neigh_node
->addr
, ethhdr
->h_source
) &&
636 (tmp_neigh_node
->if_incoming
== if_incoming
) &&
637 atomic_inc_not_zero(&tmp_neigh_node
->refcount
)) {
639 batadv_neigh_node_free_ref(neigh_node
);
640 neigh_node
= tmp_neigh_node
;
647 spin_lock_bh(&tmp_neigh_node
->lq_update_lock
);
648 batadv_ring_buffer_set(tmp_neigh_node
->tq_recv
,
649 &tmp_neigh_node
->tq_index
, 0);
650 tmp_neigh_node
->tq_avg
=
651 batadv_ring_buffer_avg(tmp_neigh_node
->tq_recv
);
652 spin_unlock_bh(&tmp_neigh_node
->lq_update_lock
);
656 struct orig_node
*orig_tmp
;
658 orig_tmp
= batadv_get_orig_node(bat_priv
, ethhdr
->h_source
);
662 neigh_node
= bat_iv_ogm_neigh_new(if_incoming
, ethhdr
->h_source
,
664 batman_ogm_packet
->seqno
);
666 batadv_orig_node_free_ref(orig_tmp
);
670 bat_dbg(DBG_BATMAN
, bat_priv
,
671 "Updating existing last-hop neighbor of originator\n");
675 orig_node
->flags
= batman_ogm_packet
->flags
;
676 neigh_node
->last_seen
= jiffies
;
678 spin_lock_bh(&neigh_node
->lq_update_lock
);
679 batadv_ring_buffer_set(neigh_node
->tq_recv
,
680 &neigh_node
->tq_index
,
681 batman_ogm_packet
->tq
);
682 neigh_node
->tq_avg
= batadv_ring_buffer_avg(neigh_node
->tq_recv
);
683 spin_unlock_bh(&neigh_node
->lq_update_lock
);
686 orig_node
->last_ttl
= batman_ogm_packet
->header
.ttl
;
687 neigh_node
->last_ttl
= batman_ogm_packet
->header
.ttl
;
690 batadv_bonding_candidate_add(orig_node
, neigh_node
);
692 /* if this neighbor already is our next hop there is nothing
695 router
= batadv_orig_node_get_router(orig_node
);
696 if (router
== neigh_node
)
699 /* if this neighbor does not offer a better TQ we won't consider it */
700 if (router
&& (router
->tq_avg
> neigh_node
->tq_avg
))
703 /* if the TQ is the same and the link not more symmetric we
704 * won't consider it either
706 if (router
&& (neigh_node
->tq_avg
== router
->tq_avg
)) {
707 orig_node_tmp
= router
->orig_node
;
708 spin_lock_bh(&orig_node_tmp
->ogm_cnt_lock
);
710 orig_node_tmp
->bcast_own_sum
[if_incoming
->if_num
];
711 spin_unlock_bh(&orig_node_tmp
->ogm_cnt_lock
);
713 orig_node_tmp
= neigh_node
->orig_node
;
714 spin_lock_bh(&orig_node_tmp
->ogm_cnt_lock
);
715 bcast_own_sum_neigh
=
716 orig_node_tmp
->bcast_own_sum
[if_incoming
->if_num
];
717 spin_unlock_bh(&orig_node_tmp
->ogm_cnt_lock
);
719 if (bcast_own_sum_orig
>= bcast_own_sum_neigh
)
723 batadv_update_route(bat_priv
, orig_node
, neigh_node
);
726 /* I have to check for transtable changes only if the OGM has been
727 * sent through a primary interface
729 if (((batman_ogm_packet
->orig
!= ethhdr
->h_source
) &&
730 (batman_ogm_packet
->header
.ttl
> 2)) ||
731 (batman_ogm_packet
->flags
& PRIMARIES_FIRST_HOP
))
732 batadv_tt_update_orig(bat_priv
, orig_node
, tt_buff
,
733 batman_ogm_packet
->tt_num_changes
,
734 batman_ogm_packet
->ttvn
,
735 ntohs(batman_ogm_packet
->tt_crc
));
737 if (orig_node
->gw_flags
!= batman_ogm_packet
->gw_flags
)
738 batadv_gw_node_update(bat_priv
, orig_node
,
739 batman_ogm_packet
->gw_flags
);
741 orig_node
->gw_flags
= batman_ogm_packet
->gw_flags
;
743 /* restart gateway selection if fast or late switching was enabled */
744 if ((orig_node
->gw_flags
) &&
745 (atomic_read(&bat_priv
->gw_mode
) == GW_MODE_CLIENT
) &&
746 (atomic_read(&bat_priv
->gw_sel_class
) > 2))
747 batadv_gw_check_election(bat_priv
, orig_node
);
755 batadv_neigh_node_free_ref(neigh_node
);
757 batadv_neigh_node_free_ref(router
);
760 static int bat_iv_ogm_calc_tq(struct orig_node
*orig_node
,
761 struct orig_node
*orig_neigh_node
,
762 struct batman_ogm_packet
*batman_ogm_packet
,
763 struct hard_iface
*if_incoming
)
765 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
766 struct neigh_node
*neigh_node
= NULL
, *tmp_neigh_node
;
767 struct hlist_node
*node
;
769 uint8_t orig_eq_count
, neigh_rq_count
, tq_own
;
770 int tq_asym_penalty
, ret
= 0;
772 /* find corresponding one hop neighbor */
774 hlist_for_each_entry_rcu(tmp_neigh_node
, node
,
775 &orig_neigh_node
->neigh_list
, list
) {
777 if (!compare_eth(tmp_neigh_node
->addr
, orig_neigh_node
->orig
))
780 if (tmp_neigh_node
->if_incoming
!= if_incoming
)
783 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
786 neigh_node
= tmp_neigh_node
;
792 neigh_node
= bat_iv_ogm_neigh_new(if_incoming
,
793 orig_neigh_node
->orig
,
796 batman_ogm_packet
->seqno
);
801 /* if orig_node is direct neighbor update neigh_node last_seen */
802 if (orig_node
== orig_neigh_node
)
803 neigh_node
->last_seen
= jiffies
;
805 orig_node
->last_seen
= jiffies
;
807 /* find packet count of corresponding one hop neighbor */
808 spin_lock_bh(&orig_node
->ogm_cnt_lock
);
809 orig_eq_count
= orig_neigh_node
->bcast_own_sum
[if_incoming
->if_num
];
810 neigh_rq_count
= neigh_node
->real_packet_count
;
811 spin_unlock_bh(&orig_node
->ogm_cnt_lock
);
813 /* pay attention to not get a value bigger than 100 % */
814 total_count
= (orig_eq_count
> neigh_rq_count
?
815 neigh_rq_count
: orig_eq_count
);
817 /* if we have too few packets (too less data) we set tq_own to zero
818 * if we receive too few packets it is not considered bidirectional
820 if ((total_count
< TQ_LOCAL_BIDRECT_SEND_MINIMUM
) ||
821 (neigh_rq_count
< TQ_LOCAL_BIDRECT_RECV_MINIMUM
))
824 /* neigh_node->real_packet_count is never zero as we
825 * only purge old information when getting new
828 tq_own
= (TQ_MAX_VALUE
* total_count
) / neigh_rq_count
;
830 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
831 * affect the nearly-symmetric links only a little, but
832 * punishes asymmetric links more. This will give a value
833 * between 0 and TQ_MAX_VALUE
835 tq_asym_penalty
= TQ_MAX_VALUE
- (TQ_MAX_VALUE
*
836 (TQ_LOCAL_WINDOW_SIZE
- neigh_rq_count
) *
837 (TQ_LOCAL_WINDOW_SIZE
- neigh_rq_count
) *
838 (TQ_LOCAL_WINDOW_SIZE
- neigh_rq_count
)) /
839 (TQ_LOCAL_WINDOW_SIZE
*
840 TQ_LOCAL_WINDOW_SIZE
*
841 TQ_LOCAL_WINDOW_SIZE
);
843 batman_ogm_packet
->tq
= ((batman_ogm_packet
->tq
* tq_own
845 (TQ_MAX_VALUE
* TQ_MAX_VALUE
));
847 bat_dbg(DBG_BATMAN
, bat_priv
,
848 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
849 orig_node
->orig
, orig_neigh_node
->orig
, total_count
,
850 neigh_rq_count
, tq_own
, tq_asym_penalty
, batman_ogm_packet
->tq
);
852 /* if link has the minimum required transmission quality
853 * consider it bidirectional
855 if (batman_ogm_packet
->tq
>= TQ_TOTAL_BIDRECT_LIMIT
)
860 batadv_neigh_node_free_ref(neigh_node
);
864 /* processes a batman packet for all interfaces, adjusts the sequence number and
865 * finds out whether it is a duplicate.
867 * 1 the packet is a duplicate
868 * 0 the packet has not yet been received
869 * -1 the packet is old and has been received while the seqno window
870 * was protected. Caller should drop it.
872 static int bat_iv_ogm_update_seqnos(const struct ethhdr
*ethhdr
,
873 const struct batman_ogm_packet
875 const struct hard_iface
*if_incoming
)
877 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
878 struct orig_node
*orig_node
;
879 struct neigh_node
*tmp_neigh_node
;
880 struct hlist_node
*node
;
881 int is_duplicate
= 0;
884 int set_mark
, ret
= -1;
885 uint32_t seqno
= ntohl(batman_ogm_packet
->seqno
);
887 orig_node
= batadv_get_orig_node(bat_priv
, batman_ogm_packet
->orig
);
891 spin_lock_bh(&orig_node
->ogm_cnt_lock
);
892 seq_diff
= seqno
- orig_node
->last_real_seqno
;
894 /* signalize caller that the packet is to be dropped. */
895 if (!hlist_empty(&orig_node
->neigh_list
) &&
896 batadv_window_protected(bat_priv
, seq_diff
,
897 &orig_node
->batman_seqno_reset
))
901 hlist_for_each_entry_rcu(tmp_neigh_node
, node
,
902 &orig_node
->neigh_list
, list
) {
904 is_duplicate
|= bat_test_bit(tmp_neigh_node
->real_bits
,
905 orig_node
->last_real_seqno
,
908 if (compare_eth(tmp_neigh_node
->addr
, ethhdr
->h_source
) &&
909 (tmp_neigh_node
->if_incoming
== if_incoming
))
914 /* if the window moved, set the update flag. */
915 need_update
|= batadv_bit_get_packet(bat_priv
,
916 tmp_neigh_node
->real_bits
,
919 tmp_neigh_node
->real_packet_count
=
920 bitmap_weight(tmp_neigh_node
->real_bits
,
921 TQ_LOCAL_WINDOW_SIZE
);
926 bat_dbg(DBG_BATMAN
, bat_priv
,
927 "updating last_seqno: old %u, new %u\n",
928 orig_node
->last_real_seqno
, seqno
);
929 orig_node
->last_real_seqno
= seqno
;
935 spin_unlock_bh(&orig_node
->ogm_cnt_lock
);
936 batadv_orig_node_free_ref(orig_node
);
940 static void bat_iv_ogm_process(const struct ethhdr
*ethhdr
,
941 struct batman_ogm_packet
*batman_ogm_packet
,
942 const unsigned char *tt_buff
,
943 struct hard_iface
*if_incoming
)
945 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
946 struct hard_iface
*hard_iface
;
947 struct orig_node
*orig_neigh_node
, *orig_node
;
948 struct neigh_node
*router
= NULL
, *router_router
= NULL
;
949 struct neigh_node
*orig_neigh_router
= NULL
;
950 int has_directlink_flag
;
951 int is_my_addr
= 0, is_my_orig
= 0, is_my_oldorig
= 0;
952 int is_broadcast
= 0, is_bidirectional
;
953 bool is_single_hop_neigh
= false;
954 bool is_from_best_next_hop
= false;
956 uint32_t if_incoming_seqno
;
958 /* Silently drop when the batman packet is actually not a
961 * This might happen if a packet is padded (e.g. Ethernet has a
962 * minimum frame length of 64 byte) and the aggregation interprets
963 * it as an additional length.
965 * TODO: A more sane solution would be to have a bit in the
966 * batman_ogm_packet to detect whether the packet is the last
967 * packet in an aggregation. Here we expect that the padding
968 * is always zero (or not 0x01)
970 if (batman_ogm_packet
->header
.packet_type
!= BAT_IV_OGM
)
973 /* could be changed by schedule_own_packet() */
974 if_incoming_seqno
= atomic_read(&if_incoming
->seqno
);
976 has_directlink_flag
= (batman_ogm_packet
->flags
& DIRECTLINK
? 1 : 0);
978 if (compare_eth(ethhdr
->h_source
, batman_ogm_packet
->orig
))
979 is_single_hop_neigh
= true;
981 bat_dbg(DBG_BATMAN
, bat_priv
,
982 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, ttvn %u, crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
983 ethhdr
->h_source
, if_incoming
->net_dev
->name
,
984 if_incoming
->net_dev
->dev_addr
, batman_ogm_packet
->orig
,
985 batman_ogm_packet
->prev_sender
, ntohl(batman_ogm_packet
->seqno
),
986 batman_ogm_packet
->ttvn
, ntohs(batman_ogm_packet
->tt_crc
),
987 batman_ogm_packet
->tt_num_changes
, batman_ogm_packet
->tq
,
988 batman_ogm_packet
->header
.ttl
,
989 batman_ogm_packet
->header
.version
, has_directlink_flag
);
992 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
993 if (hard_iface
->if_status
!= IF_ACTIVE
)
996 if (hard_iface
->soft_iface
!= if_incoming
->soft_iface
)
999 if (compare_eth(ethhdr
->h_source
,
1000 hard_iface
->net_dev
->dev_addr
))
1003 if (compare_eth(batman_ogm_packet
->orig
,
1004 hard_iface
->net_dev
->dev_addr
))
1007 if (compare_eth(batman_ogm_packet
->prev_sender
,
1008 hard_iface
->net_dev
->dev_addr
))
1011 if (is_broadcast_ether_addr(ethhdr
->h_source
))
1016 if (batman_ogm_packet
->header
.version
!= COMPAT_VERSION
) {
1017 bat_dbg(DBG_BATMAN
, bat_priv
,
1018 "Drop packet: incompatible batman version (%i)\n",
1019 batman_ogm_packet
->header
.version
);
1024 bat_dbg(DBG_BATMAN
, bat_priv
,
1025 "Drop packet: received my own broadcast (sender: %pM)\n",
1031 bat_dbg(DBG_BATMAN
, bat_priv
,
1032 "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
1038 unsigned long *word
;
1041 orig_neigh_node
= batadv_get_orig_node(bat_priv
,
1043 if (!orig_neigh_node
)
1046 /* neighbor has to indicate direct link and it has to
1047 * come via the corresponding interface
1048 * save packet seqno for bidirectional check
1050 if (has_directlink_flag
&&
1051 compare_eth(if_incoming
->net_dev
->dev_addr
,
1052 batman_ogm_packet
->orig
)) {
1053 offset
= if_incoming
->if_num
* NUM_WORDS
;
1055 spin_lock_bh(&orig_neigh_node
->ogm_cnt_lock
);
1056 word
= &(orig_neigh_node
->bcast_own
[offset
]);
1059 ntohl(batman_ogm_packet
->seqno
) - 2);
1060 orig_neigh_node
->bcast_own_sum
[if_incoming
->if_num
] =
1061 bitmap_weight(word
, TQ_LOCAL_WINDOW_SIZE
);
1062 spin_unlock_bh(&orig_neigh_node
->ogm_cnt_lock
);
1065 bat_dbg(DBG_BATMAN
, bat_priv
,
1066 "Drop packet: originator packet from myself (via neighbor)\n");
1067 batadv_orig_node_free_ref(orig_neigh_node
);
1071 if (is_my_oldorig
) {
1072 bat_dbg(DBG_BATMAN
, bat_priv
,
1073 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1078 if (batman_ogm_packet
->flags
& NOT_BEST_NEXT_HOP
) {
1079 bat_dbg(DBG_BATMAN
, bat_priv
,
1080 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1085 orig_node
= batadv_get_orig_node(bat_priv
, batman_ogm_packet
->orig
);
1089 is_duplicate
= bat_iv_ogm_update_seqnos(ethhdr
, batman_ogm_packet
,
1092 if (is_duplicate
== -1) {
1093 bat_dbg(DBG_BATMAN
, bat_priv
,
1094 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1099 if (batman_ogm_packet
->tq
== 0) {
1100 bat_dbg(DBG_BATMAN
, bat_priv
,
1101 "Drop packet: originator packet with tq equal 0\n");
1105 router
= batadv_orig_node_get_router(orig_node
);
1107 router_router
= batadv_orig_node_get_router(router
->orig_node
);
1109 if ((router
&& router
->tq_avg
!= 0) &&
1110 (compare_eth(router
->addr
, ethhdr
->h_source
)))
1111 is_from_best_next_hop
= true;
1113 /* avoid temporary routing loops */
1114 if (router
&& router_router
&&
1115 (compare_eth(router
->addr
, batman_ogm_packet
->prev_sender
)) &&
1116 !(compare_eth(batman_ogm_packet
->orig
,
1117 batman_ogm_packet
->prev_sender
)) &&
1118 (compare_eth(router
->addr
, router_router
->addr
))) {
1119 bat_dbg(DBG_BATMAN
, bat_priv
,
1120 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1125 /* if sender is a direct neighbor the sender mac equals
1128 orig_neigh_node
= (is_single_hop_neigh
?
1130 batadv_get_orig_node(bat_priv
, ethhdr
->h_source
));
1131 if (!orig_neigh_node
)
1134 orig_neigh_router
= batadv_orig_node_get_router(orig_neigh_node
);
1136 /* drop packet if sender is not a direct neighbor and if we
1137 * don't route towards it
1139 if (!is_single_hop_neigh
&& (!orig_neigh_router
)) {
1140 bat_dbg(DBG_BATMAN
, bat_priv
,
1141 "Drop packet: OGM via unknown neighbor!\n");
1145 is_bidirectional
= bat_iv_ogm_calc_tq(orig_node
, orig_neigh_node
,
1146 batman_ogm_packet
, if_incoming
);
1148 batadv_bonding_save_primary(orig_node
, orig_neigh_node
,
1151 /* update ranking if it is not a duplicate or has the same
1152 * seqno and similar ttl as the non-duplicate
1154 if (is_bidirectional
&&
1156 ((orig_node
->last_real_seqno
== ntohl(batman_ogm_packet
->seqno
)) &&
1157 (orig_node
->last_ttl
- 3 <= batman_ogm_packet
->header
.ttl
))))
1158 bat_iv_ogm_orig_update(bat_priv
, orig_node
, ethhdr
,
1159 batman_ogm_packet
, if_incoming
,
1160 tt_buff
, is_duplicate
);
1162 /* is single hop (direct) neighbor */
1163 if (is_single_hop_neigh
) {
1165 /* mark direct link on incoming interface */
1166 bat_iv_ogm_forward(orig_node
, ethhdr
, batman_ogm_packet
,
1167 is_single_hop_neigh
, is_from_best_next_hop
,
1170 bat_dbg(DBG_BATMAN
, bat_priv
,
1171 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1175 /* multihop originator */
1176 if (!is_bidirectional
) {
1177 bat_dbg(DBG_BATMAN
, bat_priv
,
1178 "Drop packet: not received via bidirectional link\n");
1183 bat_dbg(DBG_BATMAN
, bat_priv
,
1184 "Drop packet: duplicate packet received\n");
1188 bat_dbg(DBG_BATMAN
, bat_priv
,
1189 "Forwarding packet: rebroadcast originator packet\n");
1190 bat_iv_ogm_forward(orig_node
, ethhdr
, batman_ogm_packet
,
1191 is_single_hop_neigh
, is_from_best_next_hop
,
1195 if ((orig_neigh_node
) && (!is_single_hop_neigh
))
1196 batadv_orig_node_free_ref(orig_neigh_node
);
1199 batadv_neigh_node_free_ref(router
);
1201 batadv_neigh_node_free_ref(router_router
);
1202 if (orig_neigh_router
)
1203 batadv_neigh_node_free_ref(orig_neigh_router
);
1205 batadv_orig_node_free_ref(orig_node
);
1208 static int bat_iv_ogm_receive(struct sk_buff
*skb
,
1209 struct hard_iface
*if_incoming
)
1211 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
1212 struct batman_ogm_packet
*batman_ogm_packet
;
1213 struct ethhdr
*ethhdr
;
1214 int buff_pos
= 0, packet_len
;
1215 unsigned char *tt_buff
, *packet_buff
;
1218 ret
= batadv_check_management_packet(skb
, if_incoming
, BATMAN_OGM_HLEN
);
1222 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1223 * that does not have B.A.T.M.A.N. IV enabled ?
1225 if (bat_priv
->bat_algo_ops
->bat_ogm_emit
!= bat_iv_ogm_emit
)
1228 batadv_inc_counter(bat_priv
, BAT_CNT_MGMT_RX
);
1229 batadv_add_counter(bat_priv
, BAT_CNT_MGMT_RX_BYTES
,
1230 skb
->len
+ ETH_HLEN
);
1232 packet_len
= skb_headlen(skb
);
1233 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
1234 packet_buff
= skb
->data
;
1235 batman_ogm_packet
= (struct batman_ogm_packet
*)packet_buff
;
1237 /* unpack the aggregated packets and process them one by one */
1239 tt_buff
= packet_buff
+ buff_pos
+ BATMAN_OGM_HLEN
;
1241 bat_iv_ogm_process(ethhdr
, batman_ogm_packet
,
1242 tt_buff
, if_incoming
);
1244 buff_pos
+= BATMAN_OGM_HLEN
;
1245 buff_pos
+= batadv_tt_len(batman_ogm_packet
->tt_num_changes
);
1247 batman_ogm_packet
= (struct batman_ogm_packet
*)
1248 (packet_buff
+ buff_pos
);
1249 } while (bat_iv_ogm_aggr_packet(buff_pos
, packet_len
,
1250 batman_ogm_packet
->tt_num_changes
));
1253 return NET_RX_SUCCESS
;
1256 static struct bat_algo_ops batman_iv __read_mostly
= {
1257 .name
= "BATMAN_IV",
1258 .bat_iface_enable
= bat_iv_ogm_iface_enable
,
1259 .bat_iface_disable
= bat_iv_ogm_iface_disable
,
1260 .bat_iface_update_mac
= bat_iv_ogm_iface_update_mac
,
1261 .bat_primary_iface_set
= bat_iv_ogm_primary_iface_set
,
1262 .bat_ogm_schedule
= bat_iv_ogm_schedule
,
1263 .bat_ogm_emit
= bat_iv_ogm_emit
,
1266 int __init
batadv_iv_init(void)
1270 /* batman originator packet */
1271 ret
= batadv_recv_handler_register(BAT_IV_OGM
, bat_iv_ogm_receive
);
1275 ret
= batadv_algo_register(&batman_iv
);
1277 goto handler_unregister
;
1282 batadv_recv_handler_unregister(BAT_IV_OGM
);