1 /* Copyright (C) 2007-2013 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 "originator.h"
24 #include "gateway_common.h"
25 #include "gateway_client.h"
26 #include "hard-interface.h"
29 #include "network-coding.h"
33 * batadv_dup_status - duplicate status
34 * @BATADV_NO_DUP: the packet is a duplicate
35 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
37 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
38 * @BATADV_PROTECTED: originator is currently protected (after reboot)
40 enum batadv_dup_status
{
48 * batadv_ring_buffer_set - update the ring buffer with the given value
49 * @lq_recv: pointer to the ring buffer
50 * @lq_index: index to store the value at
51 * @value: value to store in the ring buffer
53 static void batadv_ring_buffer_set(uint8_t lq_recv
[], uint8_t *lq_index
,
56 lq_recv
[*lq_index
] = value
;
57 *lq_index
= (*lq_index
+ 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE
;
61 * batadv_ring_buffer_set - compute the average of all non-zero values stored
62 * in the given ring buffer
63 * @lq_recv: pointer to the ring buffer
65 * Returns computed average value.
67 static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv
[])
70 uint16_t count
= 0, i
= 0, sum
= 0;
74 while (i
< BATADV_TQ_GLOBAL_WINDOW_SIZE
) {
87 return (uint8_t)(sum
/ count
);
91 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
92 * @bat_priv: the bat priv with all the soft interface information
93 * @addr: mac address of the originator
95 * Returns the originator object corresponding to the passed mac address or NULL
97 * If the object does not exists it is created an initialised.
99 static struct batadv_orig_node
*
100 batadv_iv_ogm_orig_get(struct batadv_priv
*bat_priv
, const uint8_t *addr
)
102 struct batadv_orig_node
*orig_node
;
103 int size
, hash_added
;
105 orig_node
= batadv_orig_hash_find(bat_priv
, addr
);
109 orig_node
= batadv_orig_node_new(bat_priv
, addr
);
113 spin_lock_init(&orig_node
->bat_iv
.ogm_cnt_lock
);
115 size
= bat_priv
->num_ifaces
* sizeof(unsigned long) * BATADV_NUM_WORDS
;
116 orig_node
->bat_iv
.bcast_own
= kzalloc(size
, GFP_ATOMIC
);
117 if (!orig_node
->bat_iv
.bcast_own
)
120 size
= bat_priv
->num_ifaces
* sizeof(uint8_t);
121 orig_node
->bat_iv
.bcast_own_sum
= kzalloc(size
, GFP_ATOMIC
);
122 if (!orig_node
->bat_iv
.bcast_own_sum
)
125 hash_added
= batadv_hash_add(bat_priv
->orig_hash
, batadv_compare_orig
,
126 batadv_choose_orig
, orig_node
,
127 &orig_node
->hash_entry
);
134 kfree(orig_node
->bat_iv
.bcast_own
);
136 batadv_orig_node_free_ref(orig_node
);
141 static struct batadv_neigh_node
*
142 batadv_iv_ogm_neigh_new(struct batadv_hard_iface
*hard_iface
,
143 const uint8_t *neigh_addr
,
144 struct batadv_orig_node
*orig_node
,
145 struct batadv_orig_node
*orig_neigh
)
147 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
148 struct batadv_neigh_node
*neigh_node
;
150 neigh_node
= batadv_neigh_node_new(hard_iface
, neigh_addr
, orig_node
);
154 spin_lock_init(&neigh_node
->bat_iv
.lq_update_lock
);
156 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
157 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
158 neigh_addr
, orig_node
->orig
, hard_iface
->net_dev
->name
);
160 spin_lock_bh(&orig_node
->neigh_list_lock
);
161 hlist_add_head_rcu(&neigh_node
->list
, &orig_node
->neigh_list
);
162 spin_unlock_bh(&orig_node
->neigh_list_lock
);
168 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface
*hard_iface
)
170 struct batadv_ogm_packet
*batadv_ogm_packet
;
171 unsigned char *ogm_buff
;
172 uint32_t random_seqno
;
175 /* randomize initial seqno to avoid collision */
176 get_random_bytes(&random_seqno
, sizeof(random_seqno
));
177 atomic_set(&hard_iface
->bat_iv
.ogm_seqno
, random_seqno
);
179 hard_iface
->bat_iv
.ogm_buff_len
= BATADV_OGM_HLEN
;
180 ogm_buff
= kmalloc(hard_iface
->bat_iv
.ogm_buff_len
, GFP_ATOMIC
);
184 hard_iface
->bat_iv
.ogm_buff
= ogm_buff
;
186 batadv_ogm_packet
= (struct batadv_ogm_packet
*)ogm_buff
;
187 batadv_ogm_packet
->header
.packet_type
= BATADV_IV_OGM
;
188 batadv_ogm_packet
->header
.version
= BATADV_COMPAT_VERSION
;
189 batadv_ogm_packet
->header
.ttl
= 2;
190 batadv_ogm_packet
->flags
= BATADV_NO_FLAGS
;
191 batadv_ogm_packet
->reserved
= 0;
192 batadv_ogm_packet
->tq
= BATADV_TQ_MAX_VALUE
;
200 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface
*hard_iface
)
202 kfree(hard_iface
->bat_iv
.ogm_buff
);
203 hard_iface
->bat_iv
.ogm_buff
= NULL
;
206 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface
*hard_iface
)
208 struct batadv_ogm_packet
*batadv_ogm_packet
;
209 unsigned char *ogm_buff
= hard_iface
->bat_iv
.ogm_buff
;
211 batadv_ogm_packet
= (struct batadv_ogm_packet
*)ogm_buff
;
212 memcpy(batadv_ogm_packet
->orig
,
213 hard_iface
->net_dev
->dev_addr
, ETH_ALEN
);
214 memcpy(batadv_ogm_packet
->prev_sender
,
215 hard_iface
->net_dev
->dev_addr
, ETH_ALEN
);
219 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface
*hard_iface
)
221 struct batadv_ogm_packet
*batadv_ogm_packet
;
222 unsigned char *ogm_buff
= hard_iface
->bat_iv
.ogm_buff
;
224 batadv_ogm_packet
= (struct batadv_ogm_packet
*)ogm_buff
;
225 batadv_ogm_packet
->flags
= BATADV_PRIMARIES_FIRST_HOP
;
226 batadv_ogm_packet
->header
.ttl
= BATADV_TTL
;
229 /* when do we schedule our own ogm to be sent */
231 batadv_iv_ogm_emit_send_time(const struct batadv_priv
*bat_priv
)
235 msecs
= atomic_read(&bat_priv
->orig_interval
) - BATADV_JITTER
;
236 msecs
+= prandom_u32() % (2 * BATADV_JITTER
);
238 return jiffies
+ msecs_to_jiffies(msecs
);
241 /* when do we schedule a ogm packet to be sent */
242 static unsigned long batadv_iv_ogm_fwd_send_time(void)
244 return jiffies
+ msecs_to_jiffies(prandom_u32() % (BATADV_JITTER
/ 2));
247 /* apply hop penalty for a normal link */
248 static uint8_t batadv_hop_penalty(uint8_t tq
,
249 const struct batadv_priv
*bat_priv
)
251 int hop_penalty
= atomic_read(&bat_priv
->hop_penalty
);
254 new_tq
= tq
* (BATADV_TQ_MAX_VALUE
- hop_penalty
);
255 new_tq
/= BATADV_TQ_MAX_VALUE
;
260 /* is there another aggregated packet here? */
261 static int batadv_iv_ogm_aggr_packet(int buff_pos
, int packet_len
,
264 int next_buff_pos
= 0;
266 next_buff_pos
+= buff_pos
+ BATADV_OGM_HLEN
;
267 next_buff_pos
+= ntohs(tvlv_len
);
269 return (next_buff_pos
<= packet_len
) &&
270 (next_buff_pos
<= BATADV_MAX_AGGREGATION_BYTES
);
273 /* send a batman ogm to a given interface */
274 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet
*forw_packet
,
275 struct batadv_hard_iface
*hard_iface
)
277 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
281 struct batadv_ogm_packet
*batadv_ogm_packet
;
285 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
290 packet_pos
= forw_packet
->skb
->data
;
291 batadv_ogm_packet
= (struct batadv_ogm_packet
*)packet_pos
;
293 /* adjust all flags and log packets */
294 while (batadv_iv_ogm_aggr_packet(buff_pos
, forw_packet
->packet_len
,
295 batadv_ogm_packet
->tvlv_len
)) {
296 /* we might have aggregated direct link packets with an
297 * ordinary base packet
299 if (forw_packet
->direct_link_flags
& BIT(packet_num
) &&
300 forw_packet
->if_incoming
== hard_iface
)
301 batadv_ogm_packet
->flags
|= BATADV_DIRECTLINK
;
303 batadv_ogm_packet
->flags
&= ~BATADV_DIRECTLINK
;
305 if (packet_num
> 0 || !forw_packet
->own
)
306 fwd_str
= "Forwarding";
308 fwd_str
= "Sending own";
310 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
311 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
312 fwd_str
, (packet_num
> 0 ? "aggregated " : ""),
313 batadv_ogm_packet
->orig
,
314 ntohl(batadv_ogm_packet
->seqno
),
315 batadv_ogm_packet
->tq
, batadv_ogm_packet
->header
.ttl
,
316 (batadv_ogm_packet
->flags
& BATADV_DIRECTLINK
?
318 hard_iface
->net_dev
->name
,
319 hard_iface
->net_dev
->dev_addr
);
321 buff_pos
+= BATADV_OGM_HLEN
;
322 buff_pos
+= ntohs(batadv_ogm_packet
->tvlv_len
);
324 packet_pos
= forw_packet
->skb
->data
+ buff_pos
;
325 batadv_ogm_packet
= (struct batadv_ogm_packet
*)packet_pos
;
328 /* create clone because function is called more than once */
329 skb
= skb_clone(forw_packet
->skb
, GFP_ATOMIC
);
331 batadv_inc_counter(bat_priv
, BATADV_CNT_MGMT_TX
);
332 batadv_add_counter(bat_priv
, BATADV_CNT_MGMT_TX_BYTES
,
333 skb
->len
+ ETH_HLEN
);
334 batadv_send_skb_packet(skb
, hard_iface
, batadv_broadcast_addr
);
338 /* send a batman ogm packet */
339 static void batadv_iv_ogm_emit(struct batadv_forw_packet
*forw_packet
)
341 struct batadv_hard_iface
*hard_iface
;
342 struct net_device
*soft_iface
;
343 struct batadv_priv
*bat_priv
;
344 struct batadv_hard_iface
*primary_if
= NULL
;
345 struct batadv_ogm_packet
*batadv_ogm_packet
;
346 unsigned char directlink
;
349 packet_pos
= forw_packet
->skb
->data
;
350 batadv_ogm_packet
= (struct batadv_ogm_packet
*)packet_pos
;
351 directlink
= (batadv_ogm_packet
->flags
& BATADV_DIRECTLINK
? 1 : 0);
353 if (!forw_packet
->if_incoming
) {
354 pr_err("Error - can't forward packet: incoming iface not specified\n");
358 soft_iface
= forw_packet
->if_incoming
->soft_iface
;
359 bat_priv
= netdev_priv(soft_iface
);
361 if (forw_packet
->if_incoming
->if_status
!= BATADV_IF_ACTIVE
)
364 primary_if
= batadv_primary_if_get_selected(bat_priv
);
368 /* multihomed peer assumed
369 * non-primary OGMs are only broadcasted on their interface
371 if ((directlink
&& (batadv_ogm_packet
->header
.ttl
== 1)) ||
372 (forw_packet
->own
&& (forw_packet
->if_incoming
!= primary_if
))) {
373 /* FIXME: what about aggregated packets ? */
374 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
375 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
376 (forw_packet
->own
? "Sending own" : "Forwarding"),
377 batadv_ogm_packet
->orig
,
378 ntohl(batadv_ogm_packet
->seqno
),
379 batadv_ogm_packet
->header
.ttl
,
380 forw_packet
->if_incoming
->net_dev
->name
,
381 forw_packet
->if_incoming
->net_dev
->dev_addr
);
383 /* skb is only used once and than forw_packet is free'd */
384 batadv_send_skb_packet(forw_packet
->skb
,
385 forw_packet
->if_incoming
,
386 batadv_broadcast_addr
);
387 forw_packet
->skb
= NULL
;
392 /* broadcast on every interface */
394 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
395 if (hard_iface
->soft_iface
!= soft_iface
)
398 batadv_iv_ogm_send_to_if(forw_packet
, hard_iface
);
404 batadv_hardif_free_ref(primary_if
);
407 /* return true if new_packet can be aggregated with forw_packet */
409 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet
*new_bat_ogm_packet
,
410 struct batadv_priv
*bat_priv
,
411 int packet_len
, unsigned long send_time
,
413 const struct batadv_hard_iface
*if_incoming
,
414 const struct batadv_forw_packet
*forw_packet
)
416 struct batadv_ogm_packet
*batadv_ogm_packet
;
417 int aggregated_bytes
= forw_packet
->packet_len
+ packet_len
;
418 struct batadv_hard_iface
*primary_if
= NULL
;
420 unsigned long aggregation_end_time
;
422 batadv_ogm_packet
= (struct batadv_ogm_packet
*)forw_packet
->skb
->data
;
423 aggregation_end_time
= send_time
;
424 aggregation_end_time
+= msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS
);
426 /* we can aggregate the current packet to this aggregated packet
429 * - the send time is within our MAX_AGGREGATION_MS time
430 * - the resulting packet wont be bigger than
431 * MAX_AGGREGATION_BYTES
433 if (time_before(send_time
, forw_packet
->send_time
) &&
434 time_after_eq(aggregation_end_time
, forw_packet
->send_time
) &&
435 (aggregated_bytes
<= BATADV_MAX_AGGREGATION_BYTES
)) {
436 /* check aggregation compatibility
437 * -> direct link packets are broadcasted on
438 * their interface only
439 * -> aggregate packet if the current packet is
440 * a "global" packet as well as the base
443 primary_if
= batadv_primary_if_get_selected(bat_priv
);
447 /* packets without direct link flag and high TTL
448 * are flooded through the net
451 (!(batadv_ogm_packet
->flags
& BATADV_DIRECTLINK
)) &&
452 (batadv_ogm_packet
->header
.ttl
!= 1) &&
454 /* own packets originating non-primary
455 * interfaces leave only that interface
457 ((!forw_packet
->own
) ||
458 (forw_packet
->if_incoming
== primary_if
))) {
463 /* if the incoming packet is sent via this one
464 * interface only - we still can aggregate
467 (new_bat_ogm_packet
->header
.ttl
== 1) &&
468 (forw_packet
->if_incoming
== if_incoming
) &&
470 /* packets from direct neighbors or
471 * own secondary interface packets
472 * (= secondary interface packets in general)
474 (batadv_ogm_packet
->flags
& BATADV_DIRECTLINK
||
476 forw_packet
->if_incoming
!= primary_if
))) {
484 batadv_hardif_free_ref(primary_if
);
488 /* create a new aggregated packet and add this packet to it */
489 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff
,
490 int packet_len
, unsigned long send_time
,
492 struct batadv_hard_iface
*if_incoming
,
495 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
496 struct batadv_forw_packet
*forw_packet_aggr
;
497 unsigned char *skb_buff
;
498 unsigned int skb_size
;
500 if (!atomic_inc_not_zero(&if_incoming
->refcount
))
503 /* own packet should always be scheduled */
505 if (!batadv_atomic_dec_not_zero(&bat_priv
->batman_queue_left
)) {
506 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
507 "batman packet queue full\n");
512 forw_packet_aggr
= kmalloc(sizeof(*forw_packet_aggr
), GFP_ATOMIC
);
513 if (!forw_packet_aggr
) {
515 atomic_inc(&bat_priv
->batman_queue_left
);
519 if ((atomic_read(&bat_priv
->aggregated_ogms
)) &&
520 (packet_len
< BATADV_MAX_AGGREGATION_BYTES
))
521 skb_size
= BATADV_MAX_AGGREGATION_BYTES
;
523 skb_size
= packet_len
;
525 skb_size
+= ETH_HLEN
;
527 forw_packet_aggr
->skb
= netdev_alloc_skb_ip_align(NULL
, skb_size
);
528 if (!forw_packet_aggr
->skb
) {
530 atomic_inc(&bat_priv
->batman_queue_left
);
531 kfree(forw_packet_aggr
);
534 forw_packet_aggr
->skb
->priority
= TC_PRIO_CONTROL
;
535 skb_reserve(forw_packet_aggr
->skb
, ETH_HLEN
);
537 skb_buff
= skb_put(forw_packet_aggr
->skb
, packet_len
);
538 forw_packet_aggr
->packet_len
= packet_len
;
539 memcpy(skb_buff
, packet_buff
, packet_len
);
541 forw_packet_aggr
->own
= own_packet
;
542 forw_packet_aggr
->if_incoming
= if_incoming
;
543 forw_packet_aggr
->num_packets
= 0;
544 forw_packet_aggr
->direct_link_flags
= BATADV_NO_FLAGS
;
545 forw_packet_aggr
->send_time
= send_time
;
547 /* save packet direct link flag status */
549 forw_packet_aggr
->direct_link_flags
|= 1;
551 /* add new packet to packet list */
552 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
553 hlist_add_head(&forw_packet_aggr
->list
, &bat_priv
->forw_bat_list
);
554 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
556 /* start timer for this packet */
557 INIT_DELAYED_WORK(&forw_packet_aggr
->delayed_work
,
558 batadv_send_outstanding_bat_ogm_packet
);
559 queue_delayed_work(batadv_event_workqueue
,
560 &forw_packet_aggr
->delayed_work
,
561 send_time
- jiffies
);
565 batadv_hardif_free_ref(if_incoming
);
568 /* aggregate a new packet into the existing ogm packet */
569 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet
*forw_packet_aggr
,
570 const unsigned char *packet_buff
,
571 int packet_len
, bool direct_link
)
573 unsigned char *skb_buff
;
574 unsigned long new_direct_link_flag
;
576 skb_buff
= skb_put(forw_packet_aggr
->skb
, packet_len
);
577 memcpy(skb_buff
, packet_buff
, packet_len
);
578 forw_packet_aggr
->packet_len
+= packet_len
;
579 forw_packet_aggr
->num_packets
++;
581 /* save packet direct link flag status */
583 new_direct_link_flag
= BIT(forw_packet_aggr
->num_packets
);
584 forw_packet_aggr
->direct_link_flags
|= new_direct_link_flag
;
588 static void batadv_iv_ogm_queue_add(struct batadv_priv
*bat_priv
,
589 unsigned char *packet_buff
,
591 struct batadv_hard_iface
*if_incoming
,
592 int own_packet
, unsigned long send_time
)
594 /* _aggr -> pointer to the packet we want to aggregate with
595 * _pos -> pointer to the position in the queue
597 struct batadv_forw_packet
*forw_packet_aggr
= NULL
;
598 struct batadv_forw_packet
*forw_packet_pos
= NULL
;
599 struct batadv_ogm_packet
*batadv_ogm_packet
;
601 unsigned long max_aggregation_jiffies
;
603 batadv_ogm_packet
= (struct batadv_ogm_packet
*)packet_buff
;
604 direct_link
= batadv_ogm_packet
->flags
& BATADV_DIRECTLINK
? 1 : 0;
605 max_aggregation_jiffies
= msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS
);
607 /* find position for the packet in the forward queue */
608 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
609 /* own packets are not to be aggregated */
610 if ((atomic_read(&bat_priv
->aggregated_ogms
)) && (!own_packet
)) {
611 hlist_for_each_entry(forw_packet_pos
,
612 &bat_priv
->forw_bat_list
, list
) {
613 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet
,
614 bat_priv
, packet_len
,
615 send_time
, direct_link
,
618 forw_packet_aggr
= forw_packet_pos
;
624 /* nothing to aggregate with - either aggregation disabled or no
625 * suitable aggregation packet found
627 if (!forw_packet_aggr
) {
628 /* the following section can run without the lock */
629 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
631 /* if we could not aggregate this packet with one of the others
632 * we hold it back for a while, so that it might be aggregated
635 if (!own_packet
&& atomic_read(&bat_priv
->aggregated_ogms
))
636 send_time
+= max_aggregation_jiffies
;
638 batadv_iv_ogm_aggregate_new(packet_buff
, packet_len
,
639 send_time
, direct_link
,
640 if_incoming
, own_packet
);
642 batadv_iv_ogm_aggregate(forw_packet_aggr
, packet_buff
,
643 packet_len
, direct_link
);
644 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
648 static void batadv_iv_ogm_forward(struct batadv_orig_node
*orig_node
,
649 const struct ethhdr
*ethhdr
,
650 struct batadv_ogm_packet
*batadv_ogm_packet
,
651 bool is_single_hop_neigh
,
652 bool is_from_best_next_hop
,
653 struct batadv_hard_iface
*if_incoming
)
655 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
658 if (batadv_ogm_packet
->header
.ttl
<= 1) {
659 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
, "ttl exceeded\n");
663 if (!is_from_best_next_hop
) {
664 /* Mark the forwarded packet when it is not coming from our
665 * best next hop. We still need to forward the packet for our
666 * neighbor link quality detection to work in case the packet
667 * originated from a single hop neighbor. Otherwise we can
668 * simply drop the ogm.
670 if (is_single_hop_neigh
)
671 batadv_ogm_packet
->flags
|= BATADV_NOT_BEST_NEXT_HOP
;
676 tvlv_len
= ntohs(batadv_ogm_packet
->tvlv_len
);
678 batadv_ogm_packet
->header
.ttl
--;
679 memcpy(batadv_ogm_packet
->prev_sender
, ethhdr
->h_source
, ETH_ALEN
);
681 /* apply hop penalty */
682 batadv_ogm_packet
->tq
= batadv_hop_penalty(batadv_ogm_packet
->tq
,
685 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
686 "Forwarding packet: tq: %i, ttl: %i\n",
687 batadv_ogm_packet
->tq
, batadv_ogm_packet
->header
.ttl
);
689 /* switch of primaries first hop flag when forwarding */
690 batadv_ogm_packet
->flags
&= ~BATADV_PRIMARIES_FIRST_HOP
;
691 if (is_single_hop_neigh
)
692 batadv_ogm_packet
->flags
|= BATADV_DIRECTLINK
;
694 batadv_ogm_packet
->flags
&= ~BATADV_DIRECTLINK
;
696 batadv_iv_ogm_queue_add(bat_priv
, (unsigned char *)batadv_ogm_packet
,
697 BATADV_OGM_HLEN
+ tvlv_len
,
698 if_incoming
, 0, batadv_iv_ogm_fwd_send_time());
702 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
703 * the given interface
704 * @hard_iface: the interface for which the windows have to be shifted
707 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface
*hard_iface
)
709 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
710 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
711 struct hlist_head
*head
;
712 struct batadv_orig_node
*orig_node
;
719 for (i
= 0; i
< hash
->size
; i
++) {
720 head
= &hash
->table
[i
];
723 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
724 spin_lock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
725 word_index
= hard_iface
->if_num
* BATADV_NUM_WORDS
;
726 word
= &(orig_node
->bat_iv
.bcast_own
[word_index
]);
728 batadv_bit_get_packet(bat_priv
, word
, 1, 0);
729 if_num
= hard_iface
->if_num
;
730 w
= &orig_node
->bat_iv
.bcast_own_sum
[if_num
];
731 *w
= bitmap_weight(word
, BATADV_TQ_LOCAL_WINDOW_SIZE
);
732 spin_unlock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
738 static void batadv_iv_ogm_schedule(struct batadv_hard_iface
*hard_iface
)
740 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
741 unsigned char **ogm_buff
= &hard_iface
->bat_iv
.ogm_buff
;
742 struct batadv_ogm_packet
*batadv_ogm_packet
;
743 struct batadv_hard_iface
*primary_if
;
744 int *ogm_buff_len
= &hard_iface
->bat_iv
.ogm_buff_len
;
746 uint16_t tvlv_len
= 0;
748 primary_if
= batadv_primary_if_get_selected(bat_priv
);
750 if (hard_iface
== primary_if
) {
751 /* tt changes have to be committed before the tvlv data is
752 * appended as it may alter the tt tvlv container
754 batadv_tt_local_commit_changes(bat_priv
);
755 tvlv_len
= batadv_tvlv_container_ogm_append(bat_priv
, ogm_buff
,
760 batadv_ogm_packet
= (struct batadv_ogm_packet
*)(*ogm_buff
);
761 batadv_ogm_packet
->tvlv_len
= htons(tvlv_len
);
763 /* change sequence number to network order */
764 seqno
= (uint32_t)atomic_read(&hard_iface
->bat_iv
.ogm_seqno
);
765 batadv_ogm_packet
->seqno
= htonl(seqno
);
766 atomic_inc(&hard_iface
->bat_iv
.ogm_seqno
);
768 batadv_iv_ogm_slide_own_bcast_window(hard_iface
);
769 batadv_iv_ogm_queue_add(bat_priv
, hard_iface
->bat_iv
.ogm_buff
,
770 hard_iface
->bat_iv
.ogm_buff_len
, hard_iface
, 1,
771 batadv_iv_ogm_emit_send_time(bat_priv
));
774 batadv_hardif_free_ref(primary_if
);
778 batadv_iv_ogm_orig_update(struct batadv_priv
*bat_priv
,
779 struct batadv_orig_node
*orig_node
,
780 const struct ethhdr
*ethhdr
,
781 const struct batadv_ogm_packet
*batadv_ogm_packet
,
782 struct batadv_hard_iface
*if_incoming
,
783 const unsigned char *tt_buff
,
784 enum batadv_dup_status dup_status
)
786 struct batadv_neigh_node
*neigh_node
= NULL
, *tmp_neigh_node
= NULL
;
787 struct batadv_neigh_node
*router
= NULL
;
788 struct batadv_orig_node
*orig_node_tmp
;
790 uint8_t sum_orig
, sum_neigh
;
794 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
795 "update_originator(): Searching and updating originator entry of received packet\n");
798 hlist_for_each_entry_rcu(tmp_neigh_node
,
799 &orig_node
->neigh_list
, list
) {
800 neigh_addr
= tmp_neigh_node
->addr
;
801 if (batadv_compare_eth(neigh_addr
, ethhdr
->h_source
) &&
802 tmp_neigh_node
->if_incoming
== if_incoming
&&
803 atomic_inc_not_zero(&tmp_neigh_node
->refcount
)) {
804 if (WARN(neigh_node
, "too many matching neigh_nodes"))
805 batadv_neigh_node_free_ref(neigh_node
);
806 neigh_node
= tmp_neigh_node
;
810 if (dup_status
!= BATADV_NO_DUP
)
813 spin_lock_bh(&tmp_neigh_node
->bat_iv
.lq_update_lock
);
814 batadv_ring_buffer_set(tmp_neigh_node
->bat_iv
.tq_recv
,
815 &tmp_neigh_node
->bat_iv
.tq_index
, 0);
816 tq_avg
= batadv_ring_buffer_avg(tmp_neigh_node
->bat_iv
.tq_recv
);
817 tmp_neigh_node
->bat_iv
.tq_avg
= tq_avg
;
818 spin_unlock_bh(&tmp_neigh_node
->bat_iv
.lq_update_lock
);
822 struct batadv_orig_node
*orig_tmp
;
824 orig_tmp
= batadv_iv_ogm_orig_get(bat_priv
, ethhdr
->h_source
);
828 neigh_node
= batadv_iv_ogm_neigh_new(if_incoming
,
830 orig_node
, orig_tmp
);
832 batadv_orig_node_free_ref(orig_tmp
);
836 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
837 "Updating existing last-hop neighbor of originator\n");
841 neigh_node
->last_seen
= jiffies
;
843 spin_lock_bh(&neigh_node
->bat_iv
.lq_update_lock
);
844 batadv_ring_buffer_set(neigh_node
->bat_iv
.tq_recv
,
845 &neigh_node
->bat_iv
.tq_index
,
846 batadv_ogm_packet
->tq
);
847 tq_avg
= batadv_ring_buffer_avg(neigh_node
->bat_iv
.tq_recv
);
848 neigh_node
->bat_iv
.tq_avg
= tq_avg
;
849 spin_unlock_bh(&neigh_node
->bat_iv
.lq_update_lock
);
851 if (dup_status
== BATADV_NO_DUP
) {
852 orig_node
->last_ttl
= batadv_ogm_packet
->header
.ttl
;
853 neigh_node
->last_ttl
= batadv_ogm_packet
->header
.ttl
;
856 batadv_bonding_candidate_add(orig_node
, neigh_node
);
858 /* if this neighbor already is our next hop there is nothing
861 router
= batadv_orig_node_get_router(orig_node
);
862 if (router
== neigh_node
)
865 /* if this neighbor does not offer a better TQ we won't consider it */
866 if (router
&& (router
->bat_iv
.tq_avg
> neigh_node
->bat_iv
.tq_avg
))
869 /* if the TQ is the same and the link not more symmetric we
870 * won't consider it either
872 if (router
&& (neigh_node
->bat_iv
.tq_avg
== router
->bat_iv
.tq_avg
)) {
873 orig_node_tmp
= router
->orig_node
;
874 spin_lock_bh(&orig_node_tmp
->bat_iv
.ogm_cnt_lock
);
875 if_num
= router
->if_incoming
->if_num
;
876 sum_orig
= orig_node_tmp
->bat_iv
.bcast_own_sum
[if_num
];
877 spin_unlock_bh(&orig_node_tmp
->bat_iv
.ogm_cnt_lock
);
879 orig_node_tmp
= neigh_node
->orig_node
;
880 spin_lock_bh(&orig_node_tmp
->bat_iv
.ogm_cnt_lock
);
881 if_num
= neigh_node
->if_incoming
->if_num
;
882 sum_neigh
= orig_node_tmp
->bat_iv
.bcast_own_sum
[if_num
];
883 spin_unlock_bh(&orig_node_tmp
->bat_iv
.ogm_cnt_lock
);
885 if (sum_orig
>= sum_neigh
)
889 batadv_update_route(bat_priv
, orig_node
, neigh_node
);
896 batadv_neigh_node_free_ref(neigh_node
);
898 batadv_neigh_node_free_ref(router
);
901 static int batadv_iv_ogm_calc_tq(struct batadv_orig_node
*orig_node
,
902 struct batadv_orig_node
*orig_neigh_node
,
903 struct batadv_ogm_packet
*batadv_ogm_packet
,
904 struct batadv_hard_iface
*if_incoming
)
906 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
907 struct batadv_neigh_node
*neigh_node
= NULL
, *tmp_neigh_node
;
909 uint8_t orig_eq_count
, neigh_rq_count
, neigh_rq_inv
, tq_own
;
910 unsigned int neigh_rq_inv_cube
, neigh_rq_max_cube
;
911 int tq_asym_penalty
, inv_asym_penalty
, if_num
, ret
= 0;
912 unsigned int combined_tq
;
914 /* find corresponding one hop neighbor */
916 hlist_for_each_entry_rcu(tmp_neigh_node
,
917 &orig_neigh_node
->neigh_list
, list
) {
918 if (!batadv_compare_eth(tmp_neigh_node
->addr
,
919 orig_neigh_node
->orig
))
922 if (tmp_neigh_node
->if_incoming
!= if_incoming
)
925 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
928 neigh_node
= tmp_neigh_node
;
934 neigh_node
= batadv_iv_ogm_neigh_new(if_incoming
,
935 orig_neigh_node
->orig
,
942 /* if orig_node is direct neighbor update neigh_node last_seen */
943 if (orig_node
== orig_neigh_node
)
944 neigh_node
->last_seen
= jiffies
;
946 orig_node
->last_seen
= jiffies
;
948 /* find packet count of corresponding one hop neighbor */
949 spin_lock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
950 if_num
= if_incoming
->if_num
;
951 orig_eq_count
= orig_neigh_node
->bat_iv
.bcast_own_sum
[if_num
];
952 neigh_rq_count
= neigh_node
->bat_iv
.real_packet_count
;
953 spin_unlock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
955 /* pay attention to not get a value bigger than 100 % */
956 if (orig_eq_count
> neigh_rq_count
)
957 total_count
= neigh_rq_count
;
959 total_count
= orig_eq_count
;
961 /* if we have too few packets (too less data) we set tq_own to zero
962 * if we receive too few packets it is not considered bidirectional
964 if (total_count
< BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM
||
965 neigh_rq_count
< BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM
)
968 /* neigh_node->real_packet_count is never zero as we
969 * only purge old information when getting new
972 tq_own
= (BATADV_TQ_MAX_VALUE
* total_count
) / neigh_rq_count
;
974 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
975 * affect the nearly-symmetric links only a little, but
976 * punishes asymmetric links more. This will give a value
977 * between 0 and TQ_MAX_VALUE
979 neigh_rq_inv
= BATADV_TQ_LOCAL_WINDOW_SIZE
- neigh_rq_count
;
980 neigh_rq_inv_cube
= neigh_rq_inv
* neigh_rq_inv
* neigh_rq_inv
;
981 neigh_rq_max_cube
= BATADV_TQ_LOCAL_WINDOW_SIZE
*
982 BATADV_TQ_LOCAL_WINDOW_SIZE
*
983 BATADV_TQ_LOCAL_WINDOW_SIZE
;
984 inv_asym_penalty
= BATADV_TQ_MAX_VALUE
* neigh_rq_inv_cube
;
985 inv_asym_penalty
/= neigh_rq_max_cube
;
986 tq_asym_penalty
= BATADV_TQ_MAX_VALUE
- inv_asym_penalty
;
988 combined_tq
= batadv_ogm_packet
->tq
* tq_own
* tq_asym_penalty
;
989 combined_tq
/= BATADV_TQ_MAX_VALUE
* BATADV_TQ_MAX_VALUE
;
990 batadv_ogm_packet
->tq
= combined_tq
;
992 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
993 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
994 orig_node
->orig
, orig_neigh_node
->orig
, total_count
,
995 neigh_rq_count
, tq_own
,
996 tq_asym_penalty
, batadv_ogm_packet
->tq
);
998 /* if link has the minimum required transmission quality
999 * consider it bidirectional
1001 if (batadv_ogm_packet
->tq
>= BATADV_TQ_TOTAL_BIDRECT_LIMIT
)
1006 batadv_neigh_node_free_ref(neigh_node
);
1011 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1012 * adjust the sequence number and find out whether it is a duplicate
1013 * @ethhdr: ethernet header of the packet
1014 * @batadv_ogm_packet: OGM packet to be considered
1015 * @if_incoming: interface on which the OGM packet was received
1017 * Returns duplicate status as enum batadv_dup_status
1019 static enum batadv_dup_status
1020 batadv_iv_ogm_update_seqnos(const struct ethhdr
*ethhdr
,
1021 const struct batadv_ogm_packet
*batadv_ogm_packet
,
1022 const struct batadv_hard_iface
*if_incoming
)
1024 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
1025 struct batadv_orig_node
*orig_node
;
1026 struct batadv_neigh_node
*tmp_neigh_node
;
1029 int need_update
= 0;
1031 enum batadv_dup_status ret
= BATADV_NO_DUP
;
1032 uint32_t seqno
= ntohl(batadv_ogm_packet
->seqno
);
1033 uint8_t *neigh_addr
;
1034 uint8_t packet_count
;
1035 unsigned long *bitmap
;
1037 orig_node
= batadv_iv_ogm_orig_get(bat_priv
, batadv_ogm_packet
->orig
);
1039 return BATADV_NO_DUP
;
1041 spin_lock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
1042 seq_diff
= seqno
- orig_node
->last_real_seqno
;
1044 /* signalize caller that the packet is to be dropped. */
1045 if (!hlist_empty(&orig_node
->neigh_list
) &&
1046 batadv_window_protected(bat_priv
, seq_diff
,
1047 &orig_node
->batman_seqno_reset
)) {
1048 ret
= BATADV_PROTECTED
;
1053 hlist_for_each_entry_rcu(tmp_neigh_node
,
1054 &orig_node
->neigh_list
, list
) {
1055 neigh_addr
= tmp_neigh_node
->addr
;
1056 is_dup
= batadv_test_bit(tmp_neigh_node
->bat_iv
.real_bits
,
1057 orig_node
->last_real_seqno
,
1060 if (batadv_compare_eth(neigh_addr
, ethhdr
->h_source
) &&
1061 tmp_neigh_node
->if_incoming
== if_incoming
) {
1064 ret
= BATADV_NEIGH_DUP
;
1067 if (is_dup
&& (ret
!= BATADV_NEIGH_DUP
))
1068 ret
= BATADV_ORIG_DUP
;
1071 /* if the window moved, set the update flag. */
1072 bitmap
= tmp_neigh_node
->bat_iv
.real_bits
;
1073 need_update
|= batadv_bit_get_packet(bat_priv
, bitmap
,
1074 seq_diff
, set_mark
);
1076 packet_count
= bitmap_weight(tmp_neigh_node
->bat_iv
.real_bits
,
1077 BATADV_TQ_LOCAL_WINDOW_SIZE
);
1078 tmp_neigh_node
->bat_iv
.real_packet_count
= packet_count
;
1083 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1084 "updating last_seqno: old %u, new %u\n",
1085 orig_node
->last_real_seqno
, seqno
);
1086 orig_node
->last_real_seqno
= seqno
;
1090 spin_unlock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
1091 batadv_orig_node_free_ref(orig_node
);
1095 static void batadv_iv_ogm_process(const struct ethhdr
*ethhdr
,
1096 struct batadv_ogm_packet
*batadv_ogm_packet
,
1097 const unsigned char *tt_buff
,
1098 struct batadv_hard_iface
*if_incoming
)
1100 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
1101 struct batadv_hard_iface
*hard_iface
;
1102 struct batadv_orig_node
*orig_neigh_node
, *orig_node
, *orig_node_tmp
;
1103 struct batadv_neigh_node
*router
= NULL
, *router_router
= NULL
;
1104 struct batadv_neigh_node
*orig_neigh_router
= NULL
;
1105 int has_directlink_flag
;
1106 int is_my_addr
= 0, is_my_orig
= 0, is_my_oldorig
= 0;
1108 bool is_single_hop_neigh
= false;
1109 bool is_from_best_next_hop
= false;
1110 int sameseq
, similar_ttl
;
1111 enum batadv_dup_status dup_status
;
1112 uint32_t if_incoming_seqno
;
1113 uint8_t *prev_sender
;
1115 /* Silently drop when the batman packet is actually not a
1118 * This might happen if a packet is padded (e.g. Ethernet has a
1119 * minimum frame length of 64 byte) and the aggregation interprets
1120 * it as an additional length.
1122 * TODO: A more sane solution would be to have a bit in the
1123 * batadv_ogm_packet to detect whether the packet is the last
1124 * packet in an aggregation. Here we expect that the padding
1125 * is always zero (or not 0x01)
1127 if (batadv_ogm_packet
->header
.packet_type
!= BATADV_IV_OGM
)
1130 /* could be changed by schedule_own_packet() */
1131 if_incoming_seqno
= atomic_read(&if_incoming
->bat_iv
.ogm_seqno
);
1133 if (batadv_ogm_packet
->flags
& BATADV_DIRECTLINK
)
1134 has_directlink_flag
= 1;
1136 has_directlink_flag
= 0;
1138 if (batadv_compare_eth(ethhdr
->h_source
, batadv_ogm_packet
->orig
))
1139 is_single_hop_neigh
= true;
1141 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1142 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
1143 ethhdr
->h_source
, if_incoming
->net_dev
->name
,
1144 if_incoming
->net_dev
->dev_addr
, batadv_ogm_packet
->orig
,
1145 batadv_ogm_packet
->prev_sender
,
1146 ntohl(batadv_ogm_packet
->seqno
), batadv_ogm_packet
->tq
,
1147 batadv_ogm_packet
->header
.ttl
,
1148 batadv_ogm_packet
->header
.version
, has_directlink_flag
);
1151 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
1152 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
1155 if (hard_iface
->soft_iface
!= if_incoming
->soft_iface
)
1158 if (batadv_compare_eth(ethhdr
->h_source
,
1159 hard_iface
->net_dev
->dev_addr
))
1162 if (batadv_compare_eth(batadv_ogm_packet
->orig
,
1163 hard_iface
->net_dev
->dev_addr
))
1166 if (batadv_compare_eth(batadv_ogm_packet
->prev_sender
,
1167 hard_iface
->net_dev
->dev_addr
))
1173 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1174 "Drop packet: received my own broadcast (sender: %pM)\n",
1180 unsigned long *word
;
1186 orig_neigh_node
= batadv_iv_ogm_orig_get(bat_priv
,
1188 if (!orig_neigh_node
)
1191 /* neighbor has to indicate direct link and it has to
1192 * come via the corresponding interface
1193 * save packet seqno for bidirectional check
1195 if (has_directlink_flag
&&
1196 batadv_compare_eth(if_incoming
->net_dev
->dev_addr
,
1197 batadv_ogm_packet
->orig
)) {
1198 if_num
= if_incoming
->if_num
;
1199 offset
= if_num
* BATADV_NUM_WORDS
;
1201 spin_lock_bh(&orig_neigh_node
->bat_iv
.ogm_cnt_lock
);
1202 word
= &(orig_neigh_node
->bat_iv
.bcast_own
[offset
]);
1203 bit_pos
= if_incoming_seqno
- 2;
1204 bit_pos
-= ntohl(batadv_ogm_packet
->seqno
);
1205 batadv_set_bit(word
, bit_pos
);
1206 weight
= &orig_neigh_node
->bat_iv
.bcast_own_sum
[if_num
];
1207 *weight
= bitmap_weight(word
,
1208 BATADV_TQ_LOCAL_WINDOW_SIZE
);
1209 spin_unlock_bh(&orig_neigh_node
->bat_iv
.ogm_cnt_lock
);
1212 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1213 "Drop packet: originator packet from myself (via neighbor)\n");
1214 batadv_orig_node_free_ref(orig_neigh_node
);
1218 if (is_my_oldorig
) {
1219 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1220 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1225 if (batadv_ogm_packet
->flags
& BATADV_NOT_BEST_NEXT_HOP
) {
1226 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1227 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1232 orig_node
= batadv_iv_ogm_orig_get(bat_priv
, batadv_ogm_packet
->orig
);
1236 dup_status
= batadv_iv_ogm_update_seqnos(ethhdr
, batadv_ogm_packet
,
1239 if (dup_status
== BATADV_PROTECTED
) {
1240 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1241 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1246 if (batadv_ogm_packet
->tq
== 0) {
1247 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1248 "Drop packet: originator packet with tq equal 0\n");
1252 router
= batadv_orig_node_get_router(orig_node
);
1254 orig_node_tmp
= router
->orig_node
;
1255 router_router
= batadv_orig_node_get_router(orig_node_tmp
);
1258 if ((router
&& router
->bat_iv
.tq_avg
!= 0) &&
1259 (batadv_compare_eth(router
->addr
, ethhdr
->h_source
)))
1260 is_from_best_next_hop
= true;
1262 prev_sender
= batadv_ogm_packet
->prev_sender
;
1263 /* avoid temporary routing loops */
1264 if (router
&& router_router
&&
1265 (batadv_compare_eth(router
->addr
, prev_sender
)) &&
1266 !(batadv_compare_eth(batadv_ogm_packet
->orig
, prev_sender
)) &&
1267 (batadv_compare_eth(router
->addr
, router_router
->addr
))) {
1268 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1269 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1274 batadv_tvlv_ogm_receive(bat_priv
, batadv_ogm_packet
, orig_node
);
1276 /* if sender is a direct neighbor the sender mac equals
1279 if (is_single_hop_neigh
)
1280 orig_neigh_node
= orig_node
;
1282 orig_neigh_node
= batadv_iv_ogm_orig_get(bat_priv
,
1285 if (!orig_neigh_node
)
1288 /* Update nc_nodes of the originator */
1289 batadv_nc_update_nc_node(bat_priv
, orig_node
, orig_neigh_node
,
1290 batadv_ogm_packet
, is_single_hop_neigh
);
1292 orig_neigh_router
= batadv_orig_node_get_router(orig_neigh_node
);
1294 /* drop packet if sender is not a direct neighbor and if we
1295 * don't route towards it
1297 if (!is_single_hop_neigh
&& (!orig_neigh_router
)) {
1298 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1299 "Drop packet: OGM via unknown neighbor!\n");
1303 is_bidirect
= batadv_iv_ogm_calc_tq(orig_node
, orig_neigh_node
,
1304 batadv_ogm_packet
, if_incoming
);
1306 batadv_bonding_save_primary(orig_node
, orig_neigh_node
,
1309 /* update ranking if it is not a duplicate or has the same
1310 * seqno and similar ttl as the non-duplicate
1312 sameseq
= orig_node
->last_real_seqno
== ntohl(batadv_ogm_packet
->seqno
);
1313 similar_ttl
= orig_node
->last_ttl
- 3 <= batadv_ogm_packet
->header
.ttl
;
1314 if (is_bidirect
&& ((dup_status
== BATADV_NO_DUP
) ||
1315 (sameseq
&& similar_ttl
)))
1316 batadv_iv_ogm_orig_update(bat_priv
, orig_node
, ethhdr
,
1317 batadv_ogm_packet
, if_incoming
,
1318 tt_buff
, dup_status
);
1320 /* is single hop (direct) neighbor */
1321 if (is_single_hop_neigh
) {
1322 /* mark direct link on incoming interface */
1323 batadv_iv_ogm_forward(orig_node
, ethhdr
, batadv_ogm_packet
,
1324 is_single_hop_neigh
,
1325 is_from_best_next_hop
, if_incoming
);
1327 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1328 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1332 /* multihop originator */
1334 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1335 "Drop packet: not received via bidirectional link\n");
1339 if (dup_status
== BATADV_NEIGH_DUP
) {
1340 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1341 "Drop packet: duplicate packet received\n");
1345 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1346 "Forwarding packet: rebroadcast originator packet\n");
1347 batadv_iv_ogm_forward(orig_node
, ethhdr
, batadv_ogm_packet
,
1348 is_single_hop_neigh
, is_from_best_next_hop
,
1352 if ((orig_neigh_node
) && (!is_single_hop_neigh
))
1353 batadv_orig_node_free_ref(orig_neigh_node
);
1356 batadv_neigh_node_free_ref(router
);
1358 batadv_neigh_node_free_ref(router_router
);
1359 if (orig_neigh_router
)
1360 batadv_neigh_node_free_ref(orig_neigh_router
);
1362 batadv_orig_node_free_ref(orig_node
);
1365 static int batadv_iv_ogm_receive(struct sk_buff
*skb
,
1366 struct batadv_hard_iface
*if_incoming
)
1368 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
1369 struct batadv_ogm_packet
*batadv_ogm_packet
;
1370 struct ethhdr
*ethhdr
;
1371 int buff_pos
= 0, packet_len
;
1372 unsigned char *tvlv_buff
, *packet_buff
;
1373 uint8_t *packet_pos
;
1376 ret
= batadv_check_management_packet(skb
, if_incoming
, BATADV_OGM_HLEN
);
1380 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1381 * that does not have B.A.T.M.A.N. IV enabled ?
1383 if (bat_priv
->bat_algo_ops
->bat_ogm_emit
!= batadv_iv_ogm_emit
)
1386 batadv_inc_counter(bat_priv
, BATADV_CNT_MGMT_RX
);
1387 batadv_add_counter(bat_priv
, BATADV_CNT_MGMT_RX_BYTES
,
1388 skb
->len
+ ETH_HLEN
);
1390 packet_len
= skb_headlen(skb
);
1391 ethhdr
= eth_hdr(skb
);
1392 packet_buff
= skb
->data
;
1393 batadv_ogm_packet
= (struct batadv_ogm_packet
*)packet_buff
;
1395 /* unpack the aggregated packets and process them one by one */
1396 while (batadv_iv_ogm_aggr_packet(buff_pos
, packet_len
,
1397 batadv_ogm_packet
->tvlv_len
)) {
1398 tvlv_buff
= packet_buff
+ buff_pos
+ BATADV_OGM_HLEN
;
1400 batadv_iv_ogm_process(ethhdr
, batadv_ogm_packet
,
1401 tvlv_buff
, if_incoming
);
1403 buff_pos
+= BATADV_OGM_HLEN
;
1404 buff_pos
+= ntohs(batadv_ogm_packet
->tvlv_len
);
1406 packet_pos
= packet_buff
+ buff_pos
;
1407 batadv_ogm_packet
= (struct batadv_ogm_packet
*)packet_pos
;
1411 return NET_RX_SUCCESS
;
1414 static struct batadv_algo_ops batadv_batman_iv __read_mostly
= {
1415 .name
= "BATMAN_IV",
1416 .bat_iface_enable
= batadv_iv_ogm_iface_enable
,
1417 .bat_iface_disable
= batadv_iv_ogm_iface_disable
,
1418 .bat_iface_update_mac
= batadv_iv_ogm_iface_update_mac
,
1419 .bat_primary_iface_set
= batadv_iv_ogm_primary_iface_set
,
1420 .bat_ogm_schedule
= batadv_iv_ogm_schedule
,
1421 .bat_ogm_emit
= batadv_iv_ogm_emit
,
1424 int __init
batadv_iv_init(void)
1428 /* batman originator packet */
1429 ret
= batadv_recv_handler_register(BATADV_IV_OGM
,
1430 batadv_iv_ogm_receive
);
1434 ret
= batadv_algo_register(&batadv_batman_iv
);
1436 goto handler_unregister
;
1441 batadv_recv_handler_unregister(BATADV_IV_OGM
);