1 /* Copyright (C) 2007-2016 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, see <http://www.gnu.org/licenses/>.
18 #include "bat_iv_ogm.h"
21 #include <linux/atomic.h>
22 #include <linux/bitmap.h>
23 #include <linux/bitops.h>
24 #include <linux/bug.h>
25 #include <linux/byteorder/generic.h>
26 #include <linux/cache.h>
27 #include <linux/errno.h>
28 #include <linux/etherdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/init.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/kref.h>
35 #include <linux/list.h>
36 #include <linux/lockdep.h>
37 #include <linux/netdevice.h>
38 #include <linux/pkt_sched.h>
39 #include <linux/printk.h>
40 #include <linux/random.h>
41 #include <linux/rculist.h>
42 #include <linux/rcupdate.h>
43 #include <linux/seq_file.h>
44 #include <linux/skbuff.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/stddef.h>
48 #include <linux/string.h>
49 #include <linux/types.h>
50 #include <linux/workqueue.h>
54 #include "gateway_client.h"
55 #include "hard-interface.h"
58 #include "network-coding.h"
59 #include "originator.h"
63 #include "translation-table.h"
66 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct
*work
);
69 * enum batadv_dup_status - duplicate status
70 * @BATADV_NO_DUP: the packet is no duplicate
71 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
73 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
74 * @BATADV_PROTECTED: originator is currently protected (after reboot)
76 enum batadv_dup_status
{
84 * batadv_ring_buffer_set - update the ring buffer with the given value
85 * @lq_recv: pointer to the ring buffer
86 * @lq_index: index to store the value at
87 * @value: value to store in the ring buffer
89 static void batadv_ring_buffer_set(u8 lq_recv
[], u8
*lq_index
, u8 value
)
91 lq_recv
[*lq_index
] = value
;
92 *lq_index
= (*lq_index
+ 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE
;
96 * batadv_ring_buffer_avg - compute the average of all non-zero values stored
97 * in the given ring buffer
98 * @lq_recv: pointer to the ring buffer
100 * Return: computed average value.
102 static u8
batadv_ring_buffer_avg(const u8 lq_recv
[])
111 while (i
< BATADV_TQ_GLOBAL_WINDOW_SIZE
) {
124 return (u8
)(sum
/ count
);
128 * batadv_iv_ogm_orig_free - free the private resources allocated for this
130 * @orig_node: the orig_node for which the resources have to be free'd
132 static void batadv_iv_ogm_orig_free(struct batadv_orig_node
*orig_node
)
134 kfree(orig_node
->bat_iv
.bcast_own
);
135 kfree(orig_node
->bat_iv
.bcast_own_sum
);
139 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
140 * include the new hard-interface
141 * @orig_node: the orig_node that has to be changed
142 * @max_if_num: the current amount of interfaces
144 * Return: 0 on success, a negative error code otherwise.
146 static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node
*orig_node
,
153 spin_lock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
155 old_size
= (max_if_num
- 1) * sizeof(unsigned long) * BATADV_NUM_WORDS
;
156 data_ptr
= kmalloc_array(max_if_num
,
157 BATADV_NUM_WORDS
* sizeof(unsigned long),
162 memcpy(data_ptr
, orig_node
->bat_iv
.bcast_own
, old_size
);
163 kfree(orig_node
->bat_iv
.bcast_own
);
164 orig_node
->bat_iv
.bcast_own
= data_ptr
;
166 data_ptr
= kmalloc_array(max_if_num
, sizeof(u8
), GFP_ATOMIC
);
170 memcpy(data_ptr
, orig_node
->bat_iv
.bcast_own_sum
,
171 (max_if_num
- 1) * sizeof(u8
));
172 kfree(orig_node
->bat_iv
.bcast_own_sum
);
173 orig_node
->bat_iv
.bcast_own_sum
= data_ptr
;
178 spin_unlock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
184 * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own
185 * @orig_node: the orig_node that has to be changed
186 * @max_if_num: the current amount of interfaces
187 * @del_if_num: the index of the interface being removed
190 batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node
*orig_node
,
191 int max_if_num
, int del_if_num
)
197 lockdep_assert_held(&orig_node
->bat_iv
.ogm_cnt_lock
);
199 chunk_size
= sizeof(unsigned long) * BATADV_NUM_WORDS
;
200 data_ptr
= kmalloc_array(max_if_num
, chunk_size
, GFP_ATOMIC
);
202 /* use old buffer when new one could not be allocated */
203 data_ptr
= orig_node
->bat_iv
.bcast_own
;
205 /* copy first part */
206 memmove(data_ptr
, orig_node
->bat_iv
.bcast_own
, del_if_num
* chunk_size
);
208 /* copy second part */
209 if_offset
= (del_if_num
+ 1) * chunk_size
;
210 memmove((char *)data_ptr
+ del_if_num
* chunk_size
,
211 (uint8_t *)orig_node
->bat_iv
.bcast_own
+ if_offset
,
212 (max_if_num
- del_if_num
) * chunk_size
);
214 /* bcast_own was shrunk down in new buffer; free old one */
215 if (orig_node
->bat_iv
.bcast_own
!= data_ptr
) {
216 kfree(orig_node
->bat_iv
.bcast_own
);
217 orig_node
->bat_iv
.bcast_own
= data_ptr
;
222 * batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum
223 * @orig_node: the orig_node that has to be changed
224 * @max_if_num: the current amount of interfaces
225 * @del_if_num: the index of the interface being removed
228 batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node
*orig_node
,
229 int max_if_num
, int del_if_num
)
234 lockdep_assert_held(&orig_node
->bat_iv
.ogm_cnt_lock
);
236 data_ptr
= kmalloc_array(max_if_num
, sizeof(u8
), GFP_ATOMIC
);
238 /* use old buffer when new one could not be allocated */
239 data_ptr
= orig_node
->bat_iv
.bcast_own_sum
;
241 memmove(data_ptr
, orig_node
->bat_iv
.bcast_own_sum
,
242 del_if_num
* sizeof(u8
));
244 if_offset
= (del_if_num
+ 1) * sizeof(u8
);
245 memmove((char *)data_ptr
+ del_if_num
* sizeof(u8
),
246 orig_node
->bat_iv
.bcast_own_sum
+ if_offset
,
247 (max_if_num
- del_if_num
) * sizeof(u8
));
249 /* bcast_own_sum was shrunk down in new buffer; free old one */
250 if (orig_node
->bat_iv
.bcast_own_sum
!= data_ptr
) {
251 kfree(orig_node
->bat_iv
.bcast_own_sum
);
252 orig_node
->bat_iv
.bcast_own_sum
= data_ptr
;
257 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
258 * exclude the removed interface
259 * @orig_node: the orig_node that has to be changed
260 * @max_if_num: the current amount of interfaces
261 * @del_if_num: the index of the interface being removed
263 * Return: 0 on success, a negative error code otherwise.
265 static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node
*orig_node
,
266 int max_if_num
, int del_if_num
)
268 spin_lock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
270 if (max_if_num
== 0) {
271 kfree(orig_node
->bat_iv
.bcast_own
);
272 kfree(orig_node
->bat_iv
.bcast_own_sum
);
273 orig_node
->bat_iv
.bcast_own
= NULL
;
274 orig_node
->bat_iv
.bcast_own_sum
= NULL
;
276 batadv_iv_ogm_drop_bcast_own_entry(orig_node
, max_if_num
,
278 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node
, max_if_num
,
282 spin_unlock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
288 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
289 * @bat_priv: the bat priv with all the soft interface information
290 * @addr: mac address of the originator
292 * Return: the originator object corresponding to the passed mac address or NULL
294 * If the object does not exists it is created an initialised.
296 static struct batadv_orig_node
*
297 batadv_iv_ogm_orig_get(struct batadv_priv
*bat_priv
, const u8
*addr
)
299 struct batadv_orig_node
*orig_node
;
300 int size
, hash_added
;
302 orig_node
= batadv_orig_hash_find(bat_priv
, addr
);
306 orig_node
= batadv_orig_node_new(bat_priv
, addr
);
310 spin_lock_init(&orig_node
->bat_iv
.ogm_cnt_lock
);
312 size
= bat_priv
->num_ifaces
* sizeof(unsigned long) * BATADV_NUM_WORDS
;
313 orig_node
->bat_iv
.bcast_own
= kzalloc(size
, GFP_ATOMIC
);
314 if (!orig_node
->bat_iv
.bcast_own
)
317 size
= bat_priv
->num_ifaces
* sizeof(u8
);
318 orig_node
->bat_iv
.bcast_own_sum
= kzalloc(size
, GFP_ATOMIC
);
319 if (!orig_node
->bat_iv
.bcast_own_sum
)
322 hash_added
= batadv_hash_add(bat_priv
->orig_hash
, batadv_compare_orig
,
323 batadv_choose_orig
, orig_node
,
324 &orig_node
->hash_entry
);
331 /* free twice, as batadv_orig_node_new sets refcount to 2 */
332 batadv_orig_node_put(orig_node
);
333 batadv_orig_node_put(orig_node
);
338 static struct batadv_neigh_node
*
339 batadv_iv_ogm_neigh_new(struct batadv_hard_iface
*hard_iface
,
340 const u8
*neigh_addr
,
341 struct batadv_orig_node
*orig_node
,
342 struct batadv_orig_node
*orig_neigh
)
344 struct batadv_neigh_node
*neigh_node
;
346 neigh_node
= batadv_neigh_node_get_or_create(orig_node
,
347 hard_iface
, neigh_addr
);
351 neigh_node
->orig_node
= orig_neigh
;
357 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface
*hard_iface
)
359 struct batadv_ogm_packet
*batadv_ogm_packet
;
360 unsigned char *ogm_buff
;
363 /* randomize initial seqno to avoid collision */
364 get_random_bytes(&random_seqno
, sizeof(random_seqno
));
365 atomic_set(&hard_iface
->bat_iv
.ogm_seqno
, random_seqno
);
367 hard_iface
->bat_iv
.ogm_buff_len
= BATADV_OGM_HLEN
;
368 ogm_buff
= kmalloc(hard_iface
->bat_iv
.ogm_buff_len
, GFP_ATOMIC
);
372 hard_iface
->bat_iv
.ogm_buff
= ogm_buff
;
374 batadv_ogm_packet
= (struct batadv_ogm_packet
*)ogm_buff
;
375 batadv_ogm_packet
->packet_type
= BATADV_IV_OGM
;
376 batadv_ogm_packet
->version
= BATADV_COMPAT_VERSION
;
377 batadv_ogm_packet
->ttl
= 2;
378 batadv_ogm_packet
->flags
= BATADV_NO_FLAGS
;
379 batadv_ogm_packet
->reserved
= 0;
380 batadv_ogm_packet
->tq
= BATADV_TQ_MAX_VALUE
;
385 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface
*hard_iface
)
387 kfree(hard_iface
->bat_iv
.ogm_buff
);
388 hard_iface
->bat_iv
.ogm_buff
= NULL
;
391 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface
*hard_iface
)
393 struct batadv_ogm_packet
*batadv_ogm_packet
;
394 unsigned char *ogm_buff
= hard_iface
->bat_iv
.ogm_buff
;
396 batadv_ogm_packet
= (struct batadv_ogm_packet
*)ogm_buff
;
397 ether_addr_copy(batadv_ogm_packet
->orig
,
398 hard_iface
->net_dev
->dev_addr
);
399 ether_addr_copy(batadv_ogm_packet
->prev_sender
,
400 hard_iface
->net_dev
->dev_addr
);
404 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface
*hard_iface
)
406 struct batadv_ogm_packet
*batadv_ogm_packet
;
407 unsigned char *ogm_buff
= hard_iface
->bat_iv
.ogm_buff
;
409 batadv_ogm_packet
= (struct batadv_ogm_packet
*)ogm_buff
;
410 batadv_ogm_packet
->ttl
= BATADV_TTL
;
413 /* when do we schedule our own ogm to be sent */
415 batadv_iv_ogm_emit_send_time(const struct batadv_priv
*bat_priv
)
419 msecs
= atomic_read(&bat_priv
->orig_interval
) - BATADV_JITTER
;
420 msecs
+= prandom_u32() % (2 * BATADV_JITTER
);
422 return jiffies
+ msecs_to_jiffies(msecs
);
425 /* when do we schedule a ogm packet to be sent */
426 static unsigned long batadv_iv_ogm_fwd_send_time(void)
428 return jiffies
+ msecs_to_jiffies(prandom_u32() % (BATADV_JITTER
/ 2));
431 /* apply hop penalty for a normal link */
432 static u8
batadv_hop_penalty(u8 tq
, const struct batadv_priv
*bat_priv
)
434 int hop_penalty
= atomic_read(&bat_priv
->hop_penalty
);
437 new_tq
= tq
* (BATADV_TQ_MAX_VALUE
- hop_penalty
);
438 new_tq
/= BATADV_TQ_MAX_VALUE
;
444 * batadv_iv_ogm_aggr_packet - checks if there is another OGM attached
445 * @buff_pos: current position in the skb
446 * @packet_len: total length of the skb
447 * @tvlv_len: tvlv length of the previously considered OGM
449 * Return: true if there is enough space for another OGM, false otherwise.
451 static bool batadv_iv_ogm_aggr_packet(int buff_pos
, int packet_len
,
454 int next_buff_pos
= 0;
456 next_buff_pos
+= buff_pos
+ BATADV_OGM_HLEN
;
457 next_buff_pos
+= ntohs(tvlv_len
);
459 return (next_buff_pos
<= packet_len
) &&
460 (next_buff_pos
<= BATADV_MAX_AGGREGATION_BYTES
);
463 /* send a batman ogm to a given interface */
464 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet
*forw_packet
,
465 struct batadv_hard_iface
*hard_iface
)
467 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
471 struct batadv_ogm_packet
*batadv_ogm_packet
;
475 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
480 packet_pos
= forw_packet
->skb
->data
;
481 batadv_ogm_packet
= (struct batadv_ogm_packet
*)packet_pos
;
483 /* adjust all flags and log packets */
484 while (batadv_iv_ogm_aggr_packet(buff_pos
, forw_packet
->packet_len
,
485 batadv_ogm_packet
->tvlv_len
)) {
486 /* we might have aggregated direct link packets with an
487 * ordinary base packet
489 if (forw_packet
->direct_link_flags
& BIT(packet_num
) &&
490 forw_packet
->if_incoming
== hard_iface
)
491 batadv_ogm_packet
->flags
|= BATADV_DIRECTLINK
;
493 batadv_ogm_packet
->flags
&= ~BATADV_DIRECTLINK
;
495 if (packet_num
> 0 || !forw_packet
->own
)
496 fwd_str
= "Forwarding";
498 fwd_str
= "Sending own";
500 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
501 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
502 fwd_str
, (packet_num
> 0 ? "aggregated " : ""),
503 batadv_ogm_packet
->orig
,
504 ntohl(batadv_ogm_packet
->seqno
),
505 batadv_ogm_packet
->tq
, batadv_ogm_packet
->ttl
,
506 ((batadv_ogm_packet
->flags
& BATADV_DIRECTLINK
) ?
508 hard_iface
->net_dev
->name
,
509 hard_iface
->net_dev
->dev_addr
);
511 buff_pos
+= BATADV_OGM_HLEN
;
512 buff_pos
+= ntohs(batadv_ogm_packet
->tvlv_len
);
514 packet_pos
= forw_packet
->skb
->data
+ buff_pos
;
515 batadv_ogm_packet
= (struct batadv_ogm_packet
*)packet_pos
;
518 /* create clone because function is called more than once */
519 skb
= skb_clone(forw_packet
->skb
, GFP_ATOMIC
);
521 batadv_inc_counter(bat_priv
, BATADV_CNT_MGMT_TX
);
522 batadv_add_counter(bat_priv
, BATADV_CNT_MGMT_TX_BYTES
,
523 skb
->len
+ ETH_HLEN
);
524 batadv_send_broadcast_skb(skb
, hard_iface
);
528 /* send a batman ogm packet */
529 static void batadv_iv_ogm_emit(struct batadv_forw_packet
*forw_packet
)
531 struct net_device
*soft_iface
;
533 if (!forw_packet
->if_incoming
) {
534 pr_err("Error - can't forward packet: incoming iface not specified\n");
538 soft_iface
= forw_packet
->if_incoming
->soft_iface
;
540 if (WARN_ON(!forw_packet
->if_outgoing
))
543 if (WARN_ON(forw_packet
->if_outgoing
->soft_iface
!= soft_iface
))
546 if (forw_packet
->if_incoming
->if_status
!= BATADV_IF_ACTIVE
)
549 /* only for one specific outgoing interface */
550 batadv_iv_ogm_send_to_if(forw_packet
, forw_packet
->if_outgoing
);
554 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
555 * existing forward packet
556 * @new_bat_ogm_packet: OGM packet to be aggregated
557 * @bat_priv: the bat priv with all the soft interface information
558 * @packet_len: (total) length of the OGM
559 * @send_time: timestamp (jiffies) when the packet is to be sent
560 * @directlink: true if this is a direct link packet
561 * @if_incoming: interface where the packet was received
562 * @if_outgoing: interface for which the retransmission should be considered
563 * @forw_packet: the forwarded packet which should be checked
565 * Return: true if new_packet can be aggregated with forw_packet
568 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet
*new_bat_ogm_packet
,
569 struct batadv_priv
*bat_priv
,
570 int packet_len
, unsigned long send_time
,
572 const struct batadv_hard_iface
*if_incoming
,
573 const struct batadv_hard_iface
*if_outgoing
,
574 const struct batadv_forw_packet
*forw_packet
)
576 struct batadv_ogm_packet
*batadv_ogm_packet
;
577 int aggregated_bytes
= forw_packet
->packet_len
+ packet_len
;
578 struct batadv_hard_iface
*primary_if
= NULL
;
580 unsigned long aggregation_end_time
;
582 batadv_ogm_packet
= (struct batadv_ogm_packet
*)forw_packet
->skb
->data
;
583 aggregation_end_time
= send_time
;
584 aggregation_end_time
+= msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS
);
586 /* we can aggregate the current packet to this aggregated packet
589 * - the send time is within our MAX_AGGREGATION_MS time
590 * - the resulting packet wont be bigger than
591 * MAX_AGGREGATION_BYTES
592 * otherwise aggregation is not possible
594 if (!time_before(send_time
, forw_packet
->send_time
) ||
595 !time_after_eq(aggregation_end_time
, forw_packet
->send_time
))
598 if (aggregated_bytes
> BATADV_MAX_AGGREGATION_BYTES
)
601 /* packet is not leaving on the same interface. */
602 if (forw_packet
->if_outgoing
!= if_outgoing
)
605 /* check aggregation compatibility
606 * -> direct link packets are broadcasted on
607 * their interface only
608 * -> aggregate packet if the current packet is
609 * a "global" packet as well as the base
612 primary_if
= batadv_primary_if_get_selected(bat_priv
);
616 /* packets without direct link flag and high TTL
617 * are flooded through the net
620 !(batadv_ogm_packet
->flags
& BATADV_DIRECTLINK
) &&
621 batadv_ogm_packet
->ttl
!= 1 &&
623 /* own packets originating non-primary
624 * interfaces leave only that interface
626 (!forw_packet
->own
||
627 forw_packet
->if_incoming
== primary_if
)) {
632 /* if the incoming packet is sent via this one
633 * interface only - we still can aggregate
636 new_bat_ogm_packet
->ttl
== 1 &&
637 forw_packet
->if_incoming
== if_incoming
&&
639 /* packets from direct neighbors or
640 * own secondary interface packets
641 * (= secondary interface packets in general)
643 (batadv_ogm_packet
->flags
& BATADV_DIRECTLINK
||
645 forw_packet
->if_incoming
!= primary_if
))) {
652 batadv_hardif_put(primary_if
);
657 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
659 * @packet_buff: pointer to the OGM
660 * @packet_len: (total) length of the OGM
661 * @send_time: timestamp (jiffies) when the packet is to be sent
662 * @direct_link: whether this OGM has direct link status
663 * @if_incoming: interface where the packet was received
664 * @if_outgoing: interface for which the retransmission should be considered
665 * @own_packet: true if it is a self-generated ogm
667 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff
,
668 int packet_len
, unsigned long send_time
,
670 struct batadv_hard_iface
*if_incoming
,
671 struct batadv_hard_iface
*if_outgoing
,
674 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
675 struct batadv_forw_packet
*forw_packet_aggr
;
676 unsigned char *skb_buff
;
677 unsigned int skb_size
;
679 /* own packet should always be scheduled */
681 if (!batadv_atomic_dec_not_zero(&bat_priv
->batman_queue_left
)) {
682 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
683 "batman packet queue full\n");
688 forw_packet_aggr
= kmalloc(sizeof(*forw_packet_aggr
), GFP_ATOMIC
);
689 if (!forw_packet_aggr
)
692 if (atomic_read(&bat_priv
->aggregated_ogms
) &&
693 packet_len
< BATADV_MAX_AGGREGATION_BYTES
)
694 skb_size
= BATADV_MAX_AGGREGATION_BYTES
;
696 skb_size
= packet_len
;
698 skb_size
+= ETH_HLEN
;
700 forw_packet_aggr
->skb
= netdev_alloc_skb_ip_align(NULL
, skb_size
);
701 if (!forw_packet_aggr
->skb
)
702 goto out_free_forw_packet
;
703 forw_packet_aggr
->skb
->priority
= TC_PRIO_CONTROL
;
704 skb_reserve(forw_packet_aggr
->skb
, ETH_HLEN
);
706 skb_buff
= skb_put(forw_packet_aggr
->skb
, packet_len
);
707 forw_packet_aggr
->packet_len
= packet_len
;
708 memcpy(skb_buff
, packet_buff
, packet_len
);
710 kref_get(&if_incoming
->refcount
);
711 kref_get(&if_outgoing
->refcount
);
712 forw_packet_aggr
->own
= own_packet
;
713 forw_packet_aggr
->if_incoming
= if_incoming
;
714 forw_packet_aggr
->if_outgoing
= if_outgoing
;
715 forw_packet_aggr
->num_packets
= 0;
716 forw_packet_aggr
->direct_link_flags
= BATADV_NO_FLAGS
;
717 forw_packet_aggr
->send_time
= send_time
;
719 /* save packet direct link flag status */
721 forw_packet_aggr
->direct_link_flags
|= 1;
723 /* add new packet to packet list */
724 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
725 hlist_add_head(&forw_packet_aggr
->list
, &bat_priv
->forw_bat_list
);
726 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
728 /* start timer for this packet */
729 INIT_DELAYED_WORK(&forw_packet_aggr
->delayed_work
,
730 batadv_iv_send_outstanding_bat_ogm_packet
);
731 queue_delayed_work(batadv_event_workqueue
,
732 &forw_packet_aggr
->delayed_work
,
733 send_time
- jiffies
);
736 out_free_forw_packet
:
737 kfree(forw_packet_aggr
);
740 atomic_inc(&bat_priv
->batman_queue_left
);
743 /* aggregate a new packet into the existing ogm packet */
744 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet
*forw_packet_aggr
,
745 const unsigned char *packet_buff
,
746 int packet_len
, bool direct_link
)
748 unsigned char *skb_buff
;
749 unsigned long new_direct_link_flag
;
751 skb_buff
= skb_put(forw_packet_aggr
->skb
, packet_len
);
752 memcpy(skb_buff
, packet_buff
, packet_len
);
753 forw_packet_aggr
->packet_len
+= packet_len
;
754 forw_packet_aggr
->num_packets
++;
756 /* save packet direct link flag status */
758 new_direct_link_flag
= BIT(forw_packet_aggr
->num_packets
);
759 forw_packet_aggr
->direct_link_flags
|= new_direct_link_flag
;
764 * batadv_iv_ogm_queue_add - queue up an OGM for transmission
765 * @bat_priv: the bat priv with all the soft interface information
766 * @packet_buff: pointer to the OGM
767 * @packet_len: (total) length of the OGM
768 * @if_incoming: interface where the packet was received
769 * @if_outgoing: interface for which the retransmission should be considered
770 * @own_packet: true if it is a self-generated ogm
771 * @send_time: timestamp (jiffies) when the packet is to be sent
773 static void batadv_iv_ogm_queue_add(struct batadv_priv
*bat_priv
,
774 unsigned char *packet_buff
,
776 struct batadv_hard_iface
*if_incoming
,
777 struct batadv_hard_iface
*if_outgoing
,
778 int own_packet
, unsigned long send_time
)
780 /* _aggr -> pointer to the packet we want to aggregate with
781 * _pos -> pointer to the position in the queue
783 struct batadv_forw_packet
*forw_packet_aggr
= NULL
;
784 struct batadv_forw_packet
*forw_packet_pos
= NULL
;
785 struct batadv_ogm_packet
*batadv_ogm_packet
;
787 unsigned long max_aggregation_jiffies
;
789 batadv_ogm_packet
= (struct batadv_ogm_packet
*)packet_buff
;
790 direct_link
= !!(batadv_ogm_packet
->flags
& BATADV_DIRECTLINK
);
791 max_aggregation_jiffies
= msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS
);
793 /* find position for the packet in the forward queue */
794 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
795 /* own packets are not to be aggregated */
796 if (atomic_read(&bat_priv
->aggregated_ogms
) && !own_packet
) {
797 hlist_for_each_entry(forw_packet_pos
,
798 &bat_priv
->forw_bat_list
, list
) {
799 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet
,
800 bat_priv
, packet_len
,
801 send_time
, direct_link
,
805 forw_packet_aggr
= forw_packet_pos
;
811 /* nothing to aggregate with - either aggregation disabled or no
812 * suitable aggregation packet found
814 if (!forw_packet_aggr
) {
815 /* the following section can run without the lock */
816 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
818 /* if we could not aggregate this packet with one of the others
819 * we hold it back for a while, so that it might be aggregated
822 if (!own_packet
&& atomic_read(&bat_priv
->aggregated_ogms
))
823 send_time
+= max_aggregation_jiffies
;
825 batadv_iv_ogm_aggregate_new(packet_buff
, packet_len
,
826 send_time
, direct_link
,
827 if_incoming
, if_outgoing
,
830 batadv_iv_ogm_aggregate(forw_packet_aggr
, packet_buff
,
831 packet_len
, direct_link
);
832 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
836 static void batadv_iv_ogm_forward(struct batadv_orig_node
*orig_node
,
837 const struct ethhdr
*ethhdr
,
838 struct batadv_ogm_packet
*batadv_ogm_packet
,
839 bool is_single_hop_neigh
,
840 bool is_from_best_next_hop
,
841 struct batadv_hard_iface
*if_incoming
,
842 struct batadv_hard_iface
*if_outgoing
)
844 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
847 if (batadv_ogm_packet
->ttl
<= 1) {
848 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
, "ttl exceeded\n");
852 if (!is_from_best_next_hop
) {
853 /* Mark the forwarded packet when it is not coming from our
854 * best next hop. We still need to forward the packet for our
855 * neighbor link quality detection to work in case the packet
856 * originated from a single hop neighbor. Otherwise we can
857 * simply drop the ogm.
859 if (is_single_hop_neigh
)
860 batadv_ogm_packet
->flags
|= BATADV_NOT_BEST_NEXT_HOP
;
865 tvlv_len
= ntohs(batadv_ogm_packet
->tvlv_len
);
867 batadv_ogm_packet
->ttl
--;
868 ether_addr_copy(batadv_ogm_packet
->prev_sender
, ethhdr
->h_source
);
870 /* apply hop penalty */
871 batadv_ogm_packet
->tq
= batadv_hop_penalty(batadv_ogm_packet
->tq
,
874 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
875 "Forwarding packet: tq: %i, ttl: %i\n",
876 batadv_ogm_packet
->tq
, batadv_ogm_packet
->ttl
);
878 if (is_single_hop_neigh
)
879 batadv_ogm_packet
->flags
|= BATADV_DIRECTLINK
;
881 batadv_ogm_packet
->flags
&= ~BATADV_DIRECTLINK
;
883 batadv_iv_ogm_queue_add(bat_priv
, (unsigned char *)batadv_ogm_packet
,
884 BATADV_OGM_HLEN
+ tvlv_len
,
885 if_incoming
, if_outgoing
, 0,
886 batadv_iv_ogm_fwd_send_time());
890 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
891 * the given interface
892 * @hard_iface: the interface for which the windows have to be shifted
895 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface
*hard_iface
)
897 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
898 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
899 struct hlist_head
*head
;
900 struct batadv_orig_node
*orig_node
;
907 for (i
= 0; i
< hash
->size
; i
++) {
908 head
= &hash
->table
[i
];
911 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
912 spin_lock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
913 word_index
= hard_iface
->if_num
* BATADV_NUM_WORDS
;
914 word
= &orig_node
->bat_iv
.bcast_own
[word_index
];
916 batadv_bit_get_packet(bat_priv
, word
, 1, 0);
917 if_num
= hard_iface
->if_num
;
918 w
= &orig_node
->bat_iv
.bcast_own_sum
[if_num
];
919 *w
= bitmap_weight(word
, BATADV_TQ_LOCAL_WINDOW_SIZE
);
920 spin_unlock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
926 static void batadv_iv_ogm_schedule(struct batadv_hard_iface
*hard_iface
)
928 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
929 unsigned char **ogm_buff
= &hard_iface
->bat_iv
.ogm_buff
;
930 struct batadv_ogm_packet
*batadv_ogm_packet
;
931 struct batadv_hard_iface
*primary_if
, *tmp_hard_iface
;
932 int *ogm_buff_len
= &hard_iface
->bat_iv
.ogm_buff_len
;
935 unsigned long send_time
;
937 if ((hard_iface
->if_status
== BATADV_IF_NOT_IN_USE
) ||
938 (hard_iface
->if_status
== BATADV_IF_TO_BE_REMOVED
))
941 /* the interface gets activated here to avoid race conditions between
942 * the moment of activating the interface in
943 * hardif_activate_interface() where the originator mac is set and
944 * outdated packets (especially uninitialized mac addresses) in the
947 if (hard_iface
->if_status
== BATADV_IF_TO_BE_ACTIVATED
)
948 hard_iface
->if_status
= BATADV_IF_ACTIVE
;
950 primary_if
= batadv_primary_if_get_selected(bat_priv
);
952 if (hard_iface
== primary_if
) {
953 /* tt changes have to be committed before the tvlv data is
954 * appended as it may alter the tt tvlv container
956 batadv_tt_local_commit_changes(bat_priv
);
957 tvlv_len
= batadv_tvlv_container_ogm_append(bat_priv
, ogm_buff
,
962 batadv_ogm_packet
= (struct batadv_ogm_packet
*)(*ogm_buff
);
963 batadv_ogm_packet
->tvlv_len
= htons(tvlv_len
);
965 /* change sequence number to network order */
966 seqno
= (u32
)atomic_read(&hard_iface
->bat_iv
.ogm_seqno
);
967 batadv_ogm_packet
->seqno
= htonl(seqno
);
968 atomic_inc(&hard_iface
->bat_iv
.ogm_seqno
);
970 batadv_iv_ogm_slide_own_bcast_window(hard_iface
);
972 send_time
= batadv_iv_ogm_emit_send_time(bat_priv
);
974 if (hard_iface
!= primary_if
) {
975 /* OGMs from secondary interfaces are only scheduled on their
976 * respective interfaces.
978 batadv_iv_ogm_queue_add(bat_priv
, *ogm_buff
, *ogm_buff_len
,
979 hard_iface
, hard_iface
, 1, send_time
);
983 /* OGMs from primary interfaces are scheduled on all
987 list_for_each_entry_rcu(tmp_hard_iface
, &batadv_hardif_list
, list
) {
988 if (tmp_hard_iface
->soft_iface
!= hard_iface
->soft_iface
)
991 if (!kref_get_unless_zero(&tmp_hard_iface
->refcount
))
994 batadv_iv_ogm_queue_add(bat_priv
, *ogm_buff
,
995 *ogm_buff_len
, hard_iface
,
996 tmp_hard_iface
, 1, send_time
);
998 batadv_hardif_put(tmp_hard_iface
);
1004 batadv_hardif_put(primary_if
);
1008 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
1010 * @bat_priv: the bat priv with all the soft interface information
1011 * @orig_node: the orig node who originally emitted the ogm packet
1012 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
1013 * @ethhdr: Ethernet header of the OGM
1014 * @batadv_ogm_packet: the ogm packet
1015 * @if_incoming: interface where the packet was received
1016 * @if_outgoing: interface for which the retransmission should be considered
1017 * @dup_status: the duplicate status of this ogm packet.
1020 batadv_iv_ogm_orig_update(struct batadv_priv
*bat_priv
,
1021 struct batadv_orig_node
*orig_node
,
1022 struct batadv_orig_ifinfo
*orig_ifinfo
,
1023 const struct ethhdr
*ethhdr
,
1024 const struct batadv_ogm_packet
*batadv_ogm_packet
,
1025 struct batadv_hard_iface
*if_incoming
,
1026 struct batadv_hard_iface
*if_outgoing
,
1027 enum batadv_dup_status dup_status
)
1029 struct batadv_neigh_ifinfo
*neigh_ifinfo
= NULL
;
1030 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
1031 struct batadv_neigh_node
*neigh_node
= NULL
;
1032 struct batadv_neigh_node
*tmp_neigh_node
= NULL
;
1033 struct batadv_neigh_node
*router
= NULL
;
1034 struct batadv_orig_node
*orig_node_tmp
;
1036 u8 sum_orig
, sum_neigh
;
1040 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1041 "update_originator(): Searching and updating originator entry of received packet\n");
1044 hlist_for_each_entry_rcu(tmp_neigh_node
,
1045 &orig_node
->neigh_list
, list
) {
1046 neigh_addr
= tmp_neigh_node
->addr
;
1047 if (batadv_compare_eth(neigh_addr
, ethhdr
->h_source
) &&
1048 tmp_neigh_node
->if_incoming
== if_incoming
&&
1049 kref_get_unless_zero(&tmp_neigh_node
->refcount
)) {
1050 if (WARN(neigh_node
, "too many matching neigh_nodes"))
1051 batadv_neigh_node_put(neigh_node
);
1052 neigh_node
= tmp_neigh_node
;
1056 if (dup_status
!= BATADV_NO_DUP
)
1059 /* only update the entry for this outgoing interface */
1060 neigh_ifinfo
= batadv_neigh_ifinfo_get(tmp_neigh_node
,
1065 spin_lock_bh(&tmp_neigh_node
->ifinfo_lock
);
1066 batadv_ring_buffer_set(neigh_ifinfo
->bat_iv
.tq_recv
,
1067 &neigh_ifinfo
->bat_iv
.tq_index
, 0);
1068 tq_avg
= batadv_ring_buffer_avg(neigh_ifinfo
->bat_iv
.tq_recv
);
1069 neigh_ifinfo
->bat_iv
.tq_avg
= tq_avg
;
1070 spin_unlock_bh(&tmp_neigh_node
->ifinfo_lock
);
1072 batadv_neigh_ifinfo_put(neigh_ifinfo
);
1073 neigh_ifinfo
= NULL
;
1077 struct batadv_orig_node
*orig_tmp
;
1079 orig_tmp
= batadv_iv_ogm_orig_get(bat_priv
, ethhdr
->h_source
);
1083 neigh_node
= batadv_iv_ogm_neigh_new(if_incoming
,
1085 orig_node
, orig_tmp
);
1087 batadv_orig_node_put(orig_tmp
);
1091 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1092 "Updating existing last-hop neighbor of originator\n");
1096 neigh_ifinfo
= batadv_neigh_ifinfo_new(neigh_node
, if_outgoing
);
1100 neigh_node
->last_seen
= jiffies
;
1102 spin_lock_bh(&neigh_node
->ifinfo_lock
);
1103 batadv_ring_buffer_set(neigh_ifinfo
->bat_iv
.tq_recv
,
1104 &neigh_ifinfo
->bat_iv
.tq_index
,
1105 batadv_ogm_packet
->tq
);
1106 tq_avg
= batadv_ring_buffer_avg(neigh_ifinfo
->bat_iv
.tq_recv
);
1107 neigh_ifinfo
->bat_iv
.tq_avg
= tq_avg
;
1108 spin_unlock_bh(&neigh_node
->ifinfo_lock
);
1110 if (dup_status
== BATADV_NO_DUP
) {
1111 orig_ifinfo
->last_ttl
= batadv_ogm_packet
->ttl
;
1112 neigh_ifinfo
->last_ttl
= batadv_ogm_packet
->ttl
;
1115 /* if this neighbor already is our next hop there is nothing
1118 router
= batadv_orig_router_get(orig_node
, if_outgoing
);
1119 if (router
== neigh_node
)
1123 router_ifinfo
= batadv_neigh_ifinfo_get(router
, if_outgoing
);
1127 /* if this neighbor does not offer a better TQ we won't
1130 if (router_ifinfo
->bat_iv
.tq_avg
> neigh_ifinfo
->bat_iv
.tq_avg
)
1134 /* if the TQ is the same and the link not more symmetric we
1135 * won't consider it either
1137 if (router_ifinfo
&&
1138 neigh_ifinfo
->bat_iv
.tq_avg
== router_ifinfo
->bat_iv
.tq_avg
) {
1139 orig_node_tmp
= router
->orig_node
;
1140 spin_lock_bh(&orig_node_tmp
->bat_iv
.ogm_cnt_lock
);
1141 if_num
= router
->if_incoming
->if_num
;
1142 sum_orig
= orig_node_tmp
->bat_iv
.bcast_own_sum
[if_num
];
1143 spin_unlock_bh(&orig_node_tmp
->bat_iv
.ogm_cnt_lock
);
1145 orig_node_tmp
= neigh_node
->orig_node
;
1146 spin_lock_bh(&orig_node_tmp
->bat_iv
.ogm_cnt_lock
);
1147 if_num
= neigh_node
->if_incoming
->if_num
;
1148 sum_neigh
= orig_node_tmp
->bat_iv
.bcast_own_sum
[if_num
];
1149 spin_unlock_bh(&orig_node_tmp
->bat_iv
.ogm_cnt_lock
);
1151 if (sum_orig
>= sum_neigh
)
1155 batadv_update_route(bat_priv
, orig_node
, if_outgoing
, neigh_node
);
1162 batadv_neigh_node_put(neigh_node
);
1164 batadv_neigh_node_put(router
);
1166 batadv_neigh_ifinfo_put(neigh_ifinfo
);
1168 batadv_neigh_ifinfo_put(router_ifinfo
);
1172 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
1173 * @orig_node: the orig node who originally emitted the ogm packet
1174 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1175 * @batadv_ogm_packet: the ogm packet
1176 * @if_incoming: interface where the packet was received
1177 * @if_outgoing: interface for which the retransmission should be considered
1179 * Return: true if the link can be considered bidirectional, false otherwise
1181 static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node
*orig_node
,
1182 struct batadv_orig_node
*orig_neigh_node
,
1183 struct batadv_ogm_packet
*batadv_ogm_packet
,
1184 struct batadv_hard_iface
*if_incoming
,
1185 struct batadv_hard_iface
*if_outgoing
)
1187 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
1188 struct batadv_neigh_node
*neigh_node
= NULL
, *tmp_neigh_node
;
1189 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
1191 u8 orig_eq_count
, neigh_rq_count
, neigh_rq_inv
, tq_own
;
1192 unsigned int neigh_rq_inv_cube
, neigh_rq_max_cube
;
1194 unsigned int tq_asym_penalty
, inv_asym_penalty
;
1195 unsigned int combined_tq
;
1196 unsigned int tq_iface_penalty
;
1199 /* find corresponding one hop neighbor */
1201 hlist_for_each_entry_rcu(tmp_neigh_node
,
1202 &orig_neigh_node
->neigh_list
, list
) {
1203 if (!batadv_compare_eth(tmp_neigh_node
->addr
,
1204 orig_neigh_node
->orig
))
1207 if (tmp_neigh_node
->if_incoming
!= if_incoming
)
1210 if (!kref_get_unless_zero(&tmp_neigh_node
->refcount
))
1213 neigh_node
= tmp_neigh_node
;
1219 neigh_node
= batadv_iv_ogm_neigh_new(if_incoming
,
1220 orig_neigh_node
->orig
,
1227 /* if orig_node is direct neighbor update neigh_node last_seen */
1228 if (orig_node
== orig_neigh_node
)
1229 neigh_node
->last_seen
= jiffies
;
1231 orig_node
->last_seen
= jiffies
;
1233 /* find packet count of corresponding one hop neighbor */
1234 spin_lock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
1235 if_num
= if_incoming
->if_num
;
1236 orig_eq_count
= orig_neigh_node
->bat_iv
.bcast_own_sum
[if_num
];
1237 neigh_ifinfo
= batadv_neigh_ifinfo_new(neigh_node
, if_outgoing
);
1239 neigh_rq_count
= neigh_ifinfo
->bat_iv
.real_packet_count
;
1240 batadv_neigh_ifinfo_put(neigh_ifinfo
);
1244 spin_unlock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
1246 /* pay attention to not get a value bigger than 100 % */
1247 if (orig_eq_count
> neigh_rq_count
)
1248 total_count
= neigh_rq_count
;
1250 total_count
= orig_eq_count
;
1252 /* if we have too few packets (too less data) we set tq_own to zero
1253 * if we receive too few packets it is not considered bidirectional
1255 if (total_count
< BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM
||
1256 neigh_rq_count
< BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM
)
1259 /* neigh_node->real_packet_count is never zero as we
1260 * only purge old information when getting new
1263 tq_own
= (BATADV_TQ_MAX_VALUE
* total_count
) / neigh_rq_count
;
1265 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1266 * affect the nearly-symmetric links only a little, but
1267 * punishes asymmetric links more. This will give a value
1268 * between 0 and TQ_MAX_VALUE
1270 neigh_rq_inv
= BATADV_TQ_LOCAL_WINDOW_SIZE
- neigh_rq_count
;
1271 neigh_rq_inv_cube
= neigh_rq_inv
* neigh_rq_inv
* neigh_rq_inv
;
1272 neigh_rq_max_cube
= BATADV_TQ_LOCAL_WINDOW_SIZE
*
1273 BATADV_TQ_LOCAL_WINDOW_SIZE
*
1274 BATADV_TQ_LOCAL_WINDOW_SIZE
;
1275 inv_asym_penalty
= BATADV_TQ_MAX_VALUE
* neigh_rq_inv_cube
;
1276 inv_asym_penalty
/= neigh_rq_max_cube
;
1277 tq_asym_penalty
= BATADV_TQ_MAX_VALUE
- inv_asym_penalty
;
1279 /* penalize if the OGM is forwarded on the same interface. WiFi
1280 * interfaces and other half duplex devices suffer from throughput
1281 * drops as they can't send and receive at the same time.
1283 tq_iface_penalty
= BATADV_TQ_MAX_VALUE
;
1284 if (if_outgoing
&& (if_incoming
== if_outgoing
) &&
1285 batadv_is_wifi_netdev(if_outgoing
->net_dev
))
1286 tq_iface_penalty
= batadv_hop_penalty(BATADV_TQ_MAX_VALUE
,
1289 combined_tq
= batadv_ogm_packet
->tq
*
1293 combined_tq
/= BATADV_TQ_MAX_VALUE
*
1294 BATADV_TQ_MAX_VALUE
*
1295 BATADV_TQ_MAX_VALUE
;
1296 batadv_ogm_packet
->tq
= combined_tq
;
1298 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1299 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
1300 orig_node
->orig
, orig_neigh_node
->orig
, total_count
,
1301 neigh_rq_count
, tq_own
, tq_asym_penalty
, tq_iface_penalty
,
1302 batadv_ogm_packet
->tq
, if_incoming
->net_dev
->name
,
1303 if_outgoing
? if_outgoing
->net_dev
->name
: "DEFAULT");
1305 /* if link has the minimum required transmission quality
1306 * consider it bidirectional
1308 if (batadv_ogm_packet
->tq
>= BATADV_TQ_TOTAL_BIDRECT_LIMIT
)
1313 batadv_neigh_node_put(neigh_node
);
1318 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1319 * adjust the sequence number and find out whether it is a duplicate
1320 * @ethhdr: ethernet header of the packet
1321 * @batadv_ogm_packet: OGM packet to be considered
1322 * @if_incoming: interface on which the OGM packet was received
1323 * @if_outgoing: interface for which the retransmission should be considered
1325 * Return: duplicate status as enum batadv_dup_status
1327 static enum batadv_dup_status
1328 batadv_iv_ogm_update_seqnos(const struct ethhdr
*ethhdr
,
1329 const struct batadv_ogm_packet
*batadv_ogm_packet
,
1330 const struct batadv_hard_iface
*if_incoming
,
1331 struct batadv_hard_iface
*if_outgoing
)
1333 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
1334 struct batadv_orig_node
*orig_node
;
1335 struct batadv_orig_ifinfo
*orig_ifinfo
= NULL
;
1336 struct batadv_neigh_node
*neigh_node
;
1337 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
1340 bool need_update
= false;
1342 enum batadv_dup_status ret
= BATADV_NO_DUP
;
1343 u32 seqno
= ntohl(batadv_ogm_packet
->seqno
);
1346 unsigned long *bitmap
;
1348 orig_node
= batadv_iv_ogm_orig_get(bat_priv
, batadv_ogm_packet
->orig
);
1350 return BATADV_NO_DUP
;
1352 orig_ifinfo
= batadv_orig_ifinfo_new(orig_node
, if_outgoing
);
1353 if (WARN_ON(!orig_ifinfo
)) {
1354 batadv_orig_node_put(orig_node
);
1358 spin_lock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
1359 seq_diff
= seqno
- orig_ifinfo
->last_real_seqno
;
1361 /* signalize caller that the packet is to be dropped. */
1362 if (!hlist_empty(&orig_node
->neigh_list
) &&
1363 batadv_window_protected(bat_priv
, seq_diff
,
1364 BATADV_TQ_LOCAL_WINDOW_SIZE
,
1365 &orig_ifinfo
->batman_seqno_reset
, NULL
)) {
1366 ret
= BATADV_PROTECTED
;
1371 hlist_for_each_entry_rcu(neigh_node
, &orig_node
->neigh_list
, list
) {
1372 neigh_ifinfo
= batadv_neigh_ifinfo_new(neigh_node
,
1377 neigh_addr
= neigh_node
->addr
;
1378 is_dup
= batadv_test_bit(neigh_ifinfo
->bat_iv
.real_bits
,
1379 orig_ifinfo
->last_real_seqno
,
1382 if (batadv_compare_eth(neigh_addr
, ethhdr
->h_source
) &&
1383 neigh_node
->if_incoming
== if_incoming
) {
1386 ret
= BATADV_NEIGH_DUP
;
1389 if (is_dup
&& (ret
!= BATADV_NEIGH_DUP
))
1390 ret
= BATADV_ORIG_DUP
;
1393 /* if the window moved, set the update flag. */
1394 bitmap
= neigh_ifinfo
->bat_iv
.real_bits
;
1395 need_update
|= batadv_bit_get_packet(bat_priv
, bitmap
,
1396 seq_diff
, set_mark
);
1398 packet_count
= bitmap_weight(bitmap
,
1399 BATADV_TQ_LOCAL_WINDOW_SIZE
);
1400 neigh_ifinfo
->bat_iv
.real_packet_count
= packet_count
;
1401 batadv_neigh_ifinfo_put(neigh_ifinfo
);
1406 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1407 "%s updating last_seqno: old %u, new %u\n",
1408 if_outgoing
? if_outgoing
->net_dev
->name
: "DEFAULT",
1409 orig_ifinfo
->last_real_seqno
, seqno
);
1410 orig_ifinfo
->last_real_seqno
= seqno
;
1414 spin_unlock_bh(&orig_node
->bat_iv
.ogm_cnt_lock
);
1415 batadv_orig_node_put(orig_node
);
1416 batadv_orig_ifinfo_put(orig_ifinfo
);
1421 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
1422 * @skb: the skb containing the OGM
1423 * @ogm_offset: offset from skb->data to start of ogm header
1424 * @orig_node: the (cached) orig node for the originator of this OGM
1425 * @if_incoming: the interface where this packet was received
1426 * @if_outgoing: the interface for which the packet should be considered
1429 batadv_iv_ogm_process_per_outif(const struct sk_buff
*skb
, int ogm_offset
,
1430 struct batadv_orig_node
*orig_node
,
1431 struct batadv_hard_iface
*if_incoming
,
1432 struct batadv_hard_iface
*if_outgoing
)
1434 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
1435 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
1436 struct batadv_neigh_node
*router
= NULL
;
1437 struct batadv_neigh_node
*router_router
= NULL
;
1438 struct batadv_orig_node
*orig_neigh_node
;
1439 struct batadv_orig_ifinfo
*orig_ifinfo
;
1440 struct batadv_neigh_node
*orig_neigh_router
= NULL
;
1441 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
1442 struct batadv_ogm_packet
*ogm_packet
;
1443 enum batadv_dup_status dup_status
;
1444 bool is_from_best_next_hop
= false;
1445 bool is_single_hop_neigh
= false;
1446 bool sameseq
, similar_ttl
;
1447 struct sk_buff
*skb_priv
;
1448 struct ethhdr
*ethhdr
;
1452 /* create a private copy of the skb, as some functions change tq value
1455 skb_priv
= skb_copy(skb
, GFP_ATOMIC
);
1459 ethhdr
= eth_hdr(skb_priv
);
1460 ogm_packet
= (struct batadv_ogm_packet
*)(skb_priv
->data
+ ogm_offset
);
1462 dup_status
= batadv_iv_ogm_update_seqnos(ethhdr
, ogm_packet
,
1463 if_incoming
, if_outgoing
);
1464 if (batadv_compare_eth(ethhdr
->h_source
, ogm_packet
->orig
))
1465 is_single_hop_neigh
= true;
1467 if (dup_status
== BATADV_PROTECTED
) {
1468 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1469 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1474 if (ogm_packet
->tq
== 0) {
1475 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1476 "Drop packet: originator packet with tq equal 0\n");
1480 if (is_single_hop_neigh
) {
1481 hardif_neigh
= batadv_hardif_neigh_get(if_incoming
,
1484 hardif_neigh
->last_seen
= jiffies
;
1487 router
= batadv_orig_router_get(orig_node
, if_outgoing
);
1489 router_router
= batadv_orig_router_get(router
->orig_node
,
1491 router_ifinfo
= batadv_neigh_ifinfo_get(router
, if_outgoing
);
1494 if ((router_ifinfo
&& router_ifinfo
->bat_iv
.tq_avg
!= 0) &&
1495 (batadv_compare_eth(router
->addr
, ethhdr
->h_source
)))
1496 is_from_best_next_hop
= true;
1498 prev_sender
= ogm_packet
->prev_sender
;
1499 /* avoid temporary routing loops */
1500 if (router
&& router_router
&&
1501 (batadv_compare_eth(router
->addr
, prev_sender
)) &&
1502 !(batadv_compare_eth(ogm_packet
->orig
, prev_sender
)) &&
1503 (batadv_compare_eth(router
->addr
, router_router
->addr
))) {
1504 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1505 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1510 if (if_outgoing
== BATADV_IF_DEFAULT
)
1511 batadv_tvlv_ogm_receive(bat_priv
, ogm_packet
, orig_node
);
1513 /* if sender is a direct neighbor the sender mac equals
1516 if (is_single_hop_neigh
)
1517 orig_neigh_node
= orig_node
;
1519 orig_neigh_node
= batadv_iv_ogm_orig_get(bat_priv
,
1522 if (!orig_neigh_node
)
1525 /* Update nc_nodes of the originator */
1526 batadv_nc_update_nc_node(bat_priv
, orig_node
, orig_neigh_node
,
1527 ogm_packet
, is_single_hop_neigh
);
1529 orig_neigh_router
= batadv_orig_router_get(orig_neigh_node
,
1532 /* drop packet if sender is not a direct neighbor and if we
1533 * don't route towards it
1535 if (!is_single_hop_neigh
&& (!orig_neigh_router
)) {
1536 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1537 "Drop packet: OGM via unknown neighbor!\n");
1541 is_bidirect
= batadv_iv_ogm_calc_tq(orig_node
, orig_neigh_node
,
1542 ogm_packet
, if_incoming
,
1545 /* update ranking if it is not a duplicate or has the same
1546 * seqno and similar ttl as the non-duplicate
1548 orig_ifinfo
= batadv_orig_ifinfo_new(orig_node
, if_outgoing
);
1552 sameseq
= orig_ifinfo
->last_real_seqno
== ntohl(ogm_packet
->seqno
);
1553 similar_ttl
= (orig_ifinfo
->last_ttl
- 3) <= ogm_packet
->ttl
;
1555 if (is_bidirect
&& ((dup_status
== BATADV_NO_DUP
) ||
1556 (sameseq
&& similar_ttl
))) {
1557 batadv_iv_ogm_orig_update(bat_priv
, orig_node
,
1558 orig_ifinfo
, ethhdr
,
1559 ogm_packet
, if_incoming
,
1560 if_outgoing
, dup_status
);
1562 batadv_orig_ifinfo_put(orig_ifinfo
);
1564 /* only forward for specific interface, not for the default one. */
1565 if (if_outgoing
== BATADV_IF_DEFAULT
)
1568 /* is single hop (direct) neighbor */
1569 if (is_single_hop_neigh
) {
1570 /* OGMs from secondary interfaces should only scheduled once
1571 * per interface where it has been received, not multiple times
1573 if ((ogm_packet
->ttl
<= 2) &&
1574 (if_incoming
!= if_outgoing
)) {
1575 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1576 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1579 /* mark direct link on incoming interface */
1580 batadv_iv_ogm_forward(orig_node
, ethhdr
, ogm_packet
,
1581 is_single_hop_neigh
,
1582 is_from_best_next_hop
, if_incoming
,
1585 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1586 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1590 /* multihop originator */
1592 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1593 "Drop packet: not received via bidirectional link\n");
1597 if (dup_status
== BATADV_NEIGH_DUP
) {
1598 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1599 "Drop packet: duplicate packet received\n");
1603 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1604 "Forwarding packet: rebroadcast originator packet\n");
1605 batadv_iv_ogm_forward(orig_node
, ethhdr
, ogm_packet
,
1606 is_single_hop_neigh
, is_from_best_next_hop
,
1607 if_incoming
, if_outgoing
);
1610 if ((orig_neigh_node
) && (!is_single_hop_neigh
))
1611 batadv_orig_node_put(orig_neigh_node
);
1614 batadv_neigh_ifinfo_put(router_ifinfo
);
1616 batadv_neigh_node_put(router
);
1618 batadv_neigh_node_put(router_router
);
1619 if (orig_neigh_router
)
1620 batadv_neigh_node_put(orig_neigh_router
);
1622 batadv_hardif_neigh_put(hardif_neigh
);
1624 kfree_skb(skb_priv
);
1628 * batadv_iv_ogm_process - process an incoming batman iv OGM
1629 * @skb: the skb containing the OGM
1630 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1631 * @if_incoming: the interface where this packet was receved
1633 static void batadv_iv_ogm_process(const struct sk_buff
*skb
, int ogm_offset
,
1634 struct batadv_hard_iface
*if_incoming
)
1636 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
1637 struct batadv_orig_node
*orig_neigh_node
, *orig_node
;
1638 struct batadv_hard_iface
*hard_iface
;
1639 struct batadv_ogm_packet
*ogm_packet
;
1640 u32 if_incoming_seqno
;
1641 bool has_directlink_flag
;
1642 struct ethhdr
*ethhdr
;
1643 bool is_my_oldorig
= false;
1644 bool is_my_addr
= false;
1645 bool is_my_orig
= false;
1647 ogm_packet
= (struct batadv_ogm_packet
*)(skb
->data
+ ogm_offset
);
1648 ethhdr
= eth_hdr(skb
);
1650 /* Silently drop when the batman packet is actually not a
1653 * This might happen if a packet is padded (e.g. Ethernet has a
1654 * minimum frame length of 64 byte) and the aggregation interprets
1655 * it as an additional length.
1657 * TODO: A more sane solution would be to have a bit in the
1658 * batadv_ogm_packet to detect whether the packet is the last
1659 * packet in an aggregation. Here we expect that the padding
1660 * is always zero (or not 0x01)
1662 if (ogm_packet
->packet_type
!= BATADV_IV_OGM
)
1665 /* could be changed by schedule_own_packet() */
1666 if_incoming_seqno
= atomic_read(&if_incoming
->bat_iv
.ogm_seqno
);
1668 if (ogm_packet
->flags
& BATADV_DIRECTLINK
)
1669 has_directlink_flag
= true;
1671 has_directlink_flag
= false;
1673 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1674 "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",
1675 ethhdr
->h_source
, if_incoming
->net_dev
->name
,
1676 if_incoming
->net_dev
->dev_addr
, ogm_packet
->orig
,
1677 ogm_packet
->prev_sender
, ntohl(ogm_packet
->seqno
),
1678 ogm_packet
->tq
, ogm_packet
->ttl
,
1679 ogm_packet
->version
, has_directlink_flag
);
1682 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
1683 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
1686 if (hard_iface
->soft_iface
!= if_incoming
->soft_iface
)
1689 if (batadv_compare_eth(ethhdr
->h_source
,
1690 hard_iface
->net_dev
->dev_addr
))
1693 if (batadv_compare_eth(ogm_packet
->orig
,
1694 hard_iface
->net_dev
->dev_addr
))
1697 if (batadv_compare_eth(ogm_packet
->prev_sender
,
1698 hard_iface
->net_dev
->dev_addr
))
1699 is_my_oldorig
= true;
1704 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1705 "Drop packet: received my own broadcast (sender: %pM)\n",
1711 unsigned long *word
;
1717 orig_neigh_node
= batadv_iv_ogm_orig_get(bat_priv
,
1719 if (!orig_neigh_node
)
1722 /* neighbor has to indicate direct link and it has to
1723 * come via the corresponding interface
1724 * save packet seqno for bidirectional check
1726 if (has_directlink_flag
&&
1727 batadv_compare_eth(if_incoming
->net_dev
->dev_addr
,
1728 ogm_packet
->orig
)) {
1729 if_num
= if_incoming
->if_num
;
1730 offset
= if_num
* BATADV_NUM_WORDS
;
1732 spin_lock_bh(&orig_neigh_node
->bat_iv
.ogm_cnt_lock
);
1733 word
= &orig_neigh_node
->bat_iv
.bcast_own
[offset
];
1734 bit_pos
= if_incoming_seqno
- 2;
1735 bit_pos
-= ntohl(ogm_packet
->seqno
);
1736 batadv_set_bit(word
, bit_pos
);
1737 weight
= &orig_neigh_node
->bat_iv
.bcast_own_sum
[if_num
];
1738 *weight
= bitmap_weight(word
,
1739 BATADV_TQ_LOCAL_WINDOW_SIZE
);
1740 spin_unlock_bh(&orig_neigh_node
->bat_iv
.ogm_cnt_lock
);
1743 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1744 "Drop packet: originator packet from myself (via neighbor)\n");
1745 batadv_orig_node_put(orig_neigh_node
);
1749 if (is_my_oldorig
) {
1750 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1751 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1756 if (ogm_packet
->flags
& BATADV_NOT_BEST_NEXT_HOP
) {
1757 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1758 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1763 orig_node
= batadv_iv_ogm_orig_get(bat_priv
, ogm_packet
->orig
);
1767 batadv_iv_ogm_process_per_outif(skb
, ogm_offset
, orig_node
,
1768 if_incoming
, BATADV_IF_DEFAULT
);
1771 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
1772 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
1775 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
1778 if (!kref_get_unless_zero(&hard_iface
->refcount
))
1781 batadv_iv_ogm_process_per_outif(skb
, ogm_offset
, orig_node
,
1782 if_incoming
, hard_iface
);
1784 batadv_hardif_put(hard_iface
);
1788 batadv_orig_node_put(orig_node
);
1791 static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct
*work
)
1793 struct delayed_work
*delayed_work
;
1794 struct batadv_forw_packet
*forw_packet
;
1795 struct batadv_priv
*bat_priv
;
1797 delayed_work
= to_delayed_work(work
);
1798 forw_packet
= container_of(delayed_work
, struct batadv_forw_packet
,
1800 bat_priv
= netdev_priv(forw_packet
->if_incoming
->soft_iface
);
1801 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
1802 hlist_del(&forw_packet
->list
);
1803 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
1805 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_DEACTIVATING
)
1808 batadv_iv_ogm_emit(forw_packet
);
1810 /* we have to have at least one packet in the queue to determine the
1811 * queues wake up time unless we are shutting down.
1813 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1814 * primary interface should only be rescheduled once per period, but
1815 * this function will be called for the forw_packet instances of the
1816 * other secondary interfaces as well.
1818 if (forw_packet
->own
&&
1819 forw_packet
->if_incoming
== forw_packet
->if_outgoing
)
1820 batadv_iv_ogm_schedule(forw_packet
->if_incoming
);
1823 /* don't count own packet */
1824 if (!forw_packet
->own
)
1825 atomic_inc(&bat_priv
->batman_queue_left
);
1827 batadv_forw_packet_free(forw_packet
);
1830 static int batadv_iv_ogm_receive(struct sk_buff
*skb
,
1831 struct batadv_hard_iface
*if_incoming
)
1833 struct batadv_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
1834 struct batadv_ogm_packet
*ogm_packet
;
1839 ret
= batadv_check_management_packet(skb
, if_incoming
, BATADV_OGM_HLEN
);
1843 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1844 * that does not have B.A.T.M.A.N. IV enabled ?
1846 if (bat_priv
->algo_ops
->iface
.enable
!= batadv_iv_ogm_iface_enable
)
1849 batadv_inc_counter(bat_priv
, BATADV_CNT_MGMT_RX
);
1850 batadv_add_counter(bat_priv
, BATADV_CNT_MGMT_RX_BYTES
,
1851 skb
->len
+ ETH_HLEN
);
1854 ogm_packet
= (struct batadv_ogm_packet
*)skb
->data
;
1856 /* unpack the aggregated packets and process them one by one */
1857 while (batadv_iv_ogm_aggr_packet(ogm_offset
, skb_headlen(skb
),
1858 ogm_packet
->tvlv_len
)) {
1859 batadv_iv_ogm_process(skb
, ogm_offset
, if_incoming
);
1861 ogm_offset
+= BATADV_OGM_HLEN
;
1862 ogm_offset
+= ntohs(ogm_packet
->tvlv_len
);
1864 packet_pos
= skb
->data
+ ogm_offset
;
1865 ogm_packet
= (struct batadv_ogm_packet
*)packet_pos
;
1869 return NET_RX_SUCCESS
;
1873 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
1874 * @orig_node: the orig_node for which the neighbors are printed
1875 * @if_outgoing: outgoing interface for these entries
1876 * @seq: debugfs table seq_file struct
1878 * Must be called while holding an rcu lock.
1881 batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node
*orig_node
,
1882 struct batadv_hard_iface
*if_outgoing
,
1883 struct seq_file
*seq
)
1885 struct batadv_neigh_node
*neigh_node
;
1886 struct batadv_neigh_ifinfo
*n_ifinfo
;
1888 hlist_for_each_entry_rcu(neigh_node
, &orig_node
->neigh_list
, list
) {
1889 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
, if_outgoing
);
1893 seq_printf(seq
, " %pM (%3i)",
1895 n_ifinfo
->bat_iv
.tq_avg
);
1897 batadv_neigh_ifinfo_put(n_ifinfo
);
1902 * batadv_iv_ogm_orig_print - print the originator table
1903 * @bat_priv: the bat priv with all the soft interface information
1904 * @seq: debugfs table seq_file struct
1905 * @if_outgoing: the outgoing interface for which this should be printed
1907 static void batadv_iv_ogm_orig_print(struct batadv_priv
*bat_priv
,
1908 struct seq_file
*seq
,
1909 struct batadv_hard_iface
*if_outgoing
)
1911 struct batadv_neigh_node
*neigh_node
;
1912 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1913 int last_seen_msecs
, last_seen_secs
;
1914 struct batadv_orig_node
*orig_node
;
1915 struct batadv_neigh_ifinfo
*n_ifinfo
;
1916 unsigned long last_seen_jiffies
;
1917 struct hlist_head
*head
;
1918 int batman_count
= 0;
1922 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
1924 for (i
= 0; i
< hash
->size
; i
++) {
1925 head
= &hash
->table
[i
];
1928 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1929 neigh_node
= batadv_orig_router_get(orig_node
,
1934 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
,
1939 if (n_ifinfo
->bat_iv
.tq_avg
== 0)
1942 last_seen_jiffies
= jiffies
- orig_node
->last_seen
;
1943 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
1944 last_seen_secs
= last_seen_msecs
/ 1000;
1945 last_seen_msecs
= last_seen_msecs
% 1000;
1947 seq_printf(seq
, "%pM %4i.%03is (%3i) %pM [%10s]:",
1948 orig_node
->orig
, last_seen_secs
,
1949 last_seen_msecs
, n_ifinfo
->bat_iv
.tq_avg
,
1951 neigh_node
->if_incoming
->net_dev
->name
);
1953 batadv_iv_ogm_orig_print_neigh(orig_node
, if_outgoing
,
1955 seq_puts(seq
, "\n");
1959 batadv_neigh_node_put(neigh_node
);
1961 batadv_neigh_ifinfo_put(n_ifinfo
);
1966 if (batman_count
== 0)
1967 seq_puts(seq
, "No batman nodes in range ...\n");
1971 * batadv_iv_hardif_neigh_print - print a single hop neighbour node
1972 * @seq: neighbour table seq_file struct
1973 * @hardif_neigh: hardif neighbour information
1976 batadv_iv_hardif_neigh_print(struct seq_file
*seq
,
1977 struct batadv_hardif_neigh_node
*hardif_neigh
)
1979 int last_secs
, last_msecs
;
1981 last_secs
= jiffies_to_msecs(jiffies
- hardif_neigh
->last_seen
) / 1000;
1982 last_msecs
= jiffies_to_msecs(jiffies
- hardif_neigh
->last_seen
) % 1000;
1984 seq_printf(seq
, " %10s %pM %4i.%03is\n",
1985 hardif_neigh
->if_incoming
->net_dev
->name
,
1986 hardif_neigh
->addr
, last_secs
, last_msecs
);
1990 * batadv_iv_ogm_neigh_print - print the single hop neighbour list
1991 * @bat_priv: the bat priv with all the soft interface information
1992 * @seq: neighbour table seq_file struct
1994 static void batadv_iv_neigh_print(struct batadv_priv
*bat_priv
,
1995 struct seq_file
*seq
)
1997 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1998 struct batadv_hardif_neigh_node
*hardif_neigh
;
1999 struct batadv_hard_iface
*hard_iface
;
2000 int batman_count
= 0;
2002 seq_puts(seq
, " IF Neighbor last-seen\n");
2005 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
2006 if (hard_iface
->soft_iface
!= net_dev
)
2009 hlist_for_each_entry_rcu(hardif_neigh
,
2010 &hard_iface
->neigh_list
, list
) {
2011 batadv_iv_hardif_neigh_print(seq
, hardif_neigh
);
2017 if (batman_count
== 0)
2018 seq_puts(seq
, "No batman nodes in range ...\n");
2022 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
2023 * @neigh1: the first neighbor object of the comparison
2024 * @if_outgoing1: outgoing interface for the first neighbor
2025 * @neigh2: the second neighbor object of the comparison
2026 * @if_outgoing2: outgoing interface for the second neighbor
2028 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
2029 * lower, the same as or higher than the metric via neigh2
2031 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node
*neigh1
,
2032 struct batadv_hard_iface
*if_outgoing1
,
2033 struct batadv_neigh_node
*neigh2
,
2034 struct batadv_hard_iface
*if_outgoing2
)
2036 struct batadv_neigh_ifinfo
*neigh1_ifinfo
, *neigh2_ifinfo
;
2040 neigh1_ifinfo
= batadv_neigh_ifinfo_get(neigh1
, if_outgoing1
);
2041 neigh2_ifinfo
= batadv_neigh_ifinfo_get(neigh2
, if_outgoing2
);
2043 if (!neigh1_ifinfo
|| !neigh2_ifinfo
) {
2048 tq1
= neigh1_ifinfo
->bat_iv
.tq_avg
;
2049 tq2
= neigh2_ifinfo
->bat_iv
.tq_avg
;
2054 batadv_neigh_ifinfo_put(neigh1_ifinfo
);
2056 batadv_neigh_ifinfo_put(neigh2_ifinfo
);
2062 * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
2063 * than neigh2 from the metric prospective
2064 * @neigh1: the first neighbor object of the comparison
2065 * @if_outgoing1: outgoing interface for the first neighbor
2066 * @neigh2: the second neighbor object of the comparison
2067 * @if_outgoing2: outgoing interface for the second neighbor
2069 * Return: true if the metric via neigh1 is equally good or better than
2070 * the metric via neigh2, false otherwise.
2073 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node
*neigh1
,
2074 struct batadv_hard_iface
*if_outgoing1
,
2075 struct batadv_neigh_node
*neigh2
,
2076 struct batadv_hard_iface
*if_outgoing2
)
2078 struct batadv_neigh_ifinfo
*neigh1_ifinfo
, *neigh2_ifinfo
;
2082 neigh1_ifinfo
= batadv_neigh_ifinfo_get(neigh1
, if_outgoing1
);
2083 neigh2_ifinfo
= batadv_neigh_ifinfo_get(neigh2
, if_outgoing2
);
2085 /* we can't say that the metric is better */
2086 if (!neigh1_ifinfo
|| !neigh2_ifinfo
) {
2091 tq1
= neigh1_ifinfo
->bat_iv
.tq_avg
;
2092 tq2
= neigh2_ifinfo
->bat_iv
.tq_avg
;
2093 ret
= (tq1
- tq2
) > -BATADV_TQ_SIMILARITY_THRESHOLD
;
2097 batadv_neigh_ifinfo_put(neigh1_ifinfo
);
2099 batadv_neigh_ifinfo_put(neigh2_ifinfo
);
2104 static void batadv_iv_iface_activate(struct batadv_hard_iface
*hard_iface
)
2106 /* begin scheduling originator messages on that interface */
2107 batadv_iv_ogm_schedule(hard_iface
);
2110 static struct batadv_gw_node
*
2111 batadv_iv_gw_get_best_gw_node(struct batadv_priv
*bat_priv
)
2113 struct batadv_neigh_node
*router
;
2114 struct batadv_neigh_ifinfo
*router_ifinfo
;
2115 struct batadv_gw_node
*gw_node
, *curr_gw
= NULL
;
2116 u64 max_gw_factor
= 0;
2117 u64 tmp_gw_factor
= 0;
2120 struct batadv_orig_node
*orig_node
;
2123 hlist_for_each_entry_rcu(gw_node
, &bat_priv
->gw
.list
, list
) {
2124 orig_node
= gw_node
->orig_node
;
2125 router
= batadv_orig_router_get(orig_node
, BATADV_IF_DEFAULT
);
2129 router_ifinfo
= batadv_neigh_ifinfo_get(router
,
2134 if (!kref_get_unless_zero(&gw_node
->refcount
))
2137 tq_avg
= router_ifinfo
->bat_iv
.tq_avg
;
2139 switch (atomic_read(&bat_priv
->gw
.sel_class
)) {
2140 case 1: /* fast connection */
2141 tmp_gw_factor
= tq_avg
* tq_avg
;
2142 tmp_gw_factor
*= gw_node
->bandwidth_down
;
2143 tmp_gw_factor
*= 100 * 100;
2144 tmp_gw_factor
>>= 18;
2146 if ((tmp_gw_factor
> max_gw_factor
) ||
2147 ((tmp_gw_factor
== max_gw_factor
) &&
2148 (tq_avg
> max_tq
))) {
2150 batadv_gw_node_put(curr_gw
);
2152 kref_get(&curr_gw
->refcount
);
2156 default: /* 2: stable connection (use best statistic)
2157 * 3: fast-switch (use best statistic but change as
2158 * soon as a better gateway appears)
2159 * XX: late-switch (use best statistic but change as
2160 * soon as a better gateway appears which has
2161 * $routing_class more tq points)
2163 if (tq_avg
> max_tq
) {
2165 batadv_gw_node_put(curr_gw
);
2167 kref_get(&curr_gw
->refcount
);
2172 if (tq_avg
> max_tq
)
2175 if (tmp_gw_factor
> max_gw_factor
)
2176 max_gw_factor
= tmp_gw_factor
;
2178 batadv_gw_node_put(gw_node
);
2181 batadv_neigh_node_put(router
);
2183 batadv_neigh_ifinfo_put(router_ifinfo
);
2190 static bool batadv_iv_gw_is_eligible(struct batadv_priv
*bat_priv
,
2191 struct batadv_orig_node
*curr_gw_orig
,
2192 struct batadv_orig_node
*orig_node
)
2194 struct batadv_neigh_ifinfo
*router_orig_ifinfo
= NULL
;
2195 struct batadv_neigh_ifinfo
*router_gw_ifinfo
= NULL
;
2196 struct batadv_neigh_node
*router_gw
= NULL
;
2197 struct batadv_neigh_node
*router_orig
= NULL
;
2198 u8 gw_tq_avg
, orig_tq_avg
;
2201 /* dynamic re-election is performed only on fast or late switch */
2202 if (atomic_read(&bat_priv
->gw
.sel_class
) <= 2)
2205 router_gw
= batadv_orig_router_get(curr_gw_orig
, BATADV_IF_DEFAULT
);
2211 router_gw_ifinfo
= batadv_neigh_ifinfo_get(router_gw
,
2213 if (!router_gw_ifinfo
) {
2218 router_orig
= batadv_orig_router_get(orig_node
, BATADV_IF_DEFAULT
);
2222 router_orig_ifinfo
= batadv_neigh_ifinfo_get(router_orig
,
2224 if (!router_orig_ifinfo
)
2227 gw_tq_avg
= router_gw_ifinfo
->bat_iv
.tq_avg
;
2228 orig_tq_avg
= router_orig_ifinfo
->bat_iv
.tq_avg
;
2230 /* the TQ value has to be better */
2231 if (orig_tq_avg
< gw_tq_avg
)
2234 /* if the routing class is greater than 3 the value tells us how much
2235 * greater the TQ value of the new gateway must be
2237 if ((atomic_read(&bat_priv
->gw
.sel_class
) > 3) &&
2238 (orig_tq_avg
- gw_tq_avg
< atomic_read(&bat_priv
->gw
.sel_class
)))
2241 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
2242 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2243 gw_tq_avg
, orig_tq_avg
);
2247 if (router_gw_ifinfo
)
2248 batadv_neigh_ifinfo_put(router_gw_ifinfo
);
2249 if (router_orig_ifinfo
)
2250 batadv_neigh_ifinfo_put(router_orig_ifinfo
);
2252 batadv_neigh_node_put(router_gw
);
2254 batadv_neigh_node_put(router_orig
);
2259 /* fails if orig_node has no router */
2260 static int batadv_iv_gw_write_buffer_text(struct batadv_priv
*bat_priv
,
2261 struct seq_file
*seq
,
2262 const struct batadv_gw_node
*gw_node
)
2264 struct batadv_gw_node
*curr_gw
;
2265 struct batadv_neigh_node
*router
;
2266 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
2269 router
= batadv_orig_router_get(gw_node
->orig_node
, BATADV_IF_DEFAULT
);
2273 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
2277 curr_gw
= batadv_gw_get_selected_gw_node(bat_priv
);
2279 seq_printf(seq
, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2280 (curr_gw
== gw_node
? "=>" : " "),
2281 gw_node
->orig_node
->orig
,
2282 router_ifinfo
->bat_iv
.tq_avg
, router
->addr
,
2283 router
->if_incoming
->net_dev
->name
,
2284 gw_node
->bandwidth_down
/ 10,
2285 gw_node
->bandwidth_down
% 10,
2286 gw_node
->bandwidth_up
/ 10,
2287 gw_node
->bandwidth_up
% 10);
2288 ret
= seq_has_overflowed(seq
) ? -1 : 0;
2291 batadv_gw_node_put(curr_gw
);
2294 batadv_neigh_ifinfo_put(router_ifinfo
);
2296 batadv_neigh_node_put(router
);
2300 static void batadv_iv_gw_print(struct batadv_priv
*bat_priv
,
2301 struct seq_file
*seq
)
2303 struct batadv_gw_node
*gw_node
;
2307 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2310 hlist_for_each_entry_rcu(gw_node
, &bat_priv
->gw
.list
, list
) {
2311 /* fails if orig_node has no router */
2312 if (batadv_iv_gw_write_buffer_text(bat_priv
, seq
, gw_node
) < 0)
2320 seq_puts(seq
, "No gateways in range ...\n");
2323 static struct batadv_algo_ops batadv_batman_iv __read_mostly
= {
2324 .name
= "BATMAN_IV",
2326 .activate
= batadv_iv_iface_activate
,
2327 .enable
= batadv_iv_ogm_iface_enable
,
2328 .disable
= batadv_iv_ogm_iface_disable
,
2329 .update_mac
= batadv_iv_ogm_iface_update_mac
,
2330 .primary_set
= batadv_iv_ogm_primary_iface_set
,
2333 .cmp
= batadv_iv_ogm_neigh_cmp
,
2334 .is_similar_or_better
= batadv_iv_ogm_neigh_is_sob
,
2335 .print
= batadv_iv_neigh_print
,
2338 .print
= batadv_iv_ogm_orig_print
,
2339 .free
= batadv_iv_ogm_orig_free
,
2340 .add_if
= batadv_iv_ogm_orig_add_if
,
2341 .del_if
= batadv_iv_ogm_orig_del_if
,
2344 .get_best_gw_node
= batadv_iv_gw_get_best_gw_node
,
2345 .is_eligible
= batadv_iv_gw_is_eligible
,
2346 .print
= batadv_iv_gw_print
,
2350 int __init
batadv_iv_init(void)
2354 /* batman originator packet */
2355 ret
= batadv_recv_handler_register(BATADV_IV_OGM
,
2356 batadv_iv_ogm_receive
);
2360 ret
= batadv_algo_register(&batadv_batman_iv
);
2362 goto handler_unregister
;
2367 batadv_recv_handler_unregister(BATADV_IV_OGM
);