Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / net / batman-adv / bat_iv_ogm.c
CommitLineData
7db7d9f3 1// SPDX-License-Identifier: GPL-2.0
cfa55c6d 2/* Copyright (C) B.A.T.M.A.N. contributors:
fc957275
ML
3 *
4 * Marek Lindner, Simon Wunderlich
fc957275
ML
5 */
6
a2d08166 7#include "bat_iv_ogm.h"
fc957275 8#include "main.h"
1e2c2a4f
SE
9
10#include <linux/atomic.h>
11#include <linux/bitmap.h>
12#include <linux/bitops.h>
13#include <linux/bug.h>
14#include <linux/byteorder/generic.h>
15#include <linux/cache.h>
eb7da4f1 16#include <linux/container_of.h>
1e2c2a4f
SE
17#include <linux/errno.h>
18#include <linux/etherdevice.h>
b92b94ac 19#include <linux/gfp.h>
1e2c2a4f
SE
20#include <linux/if_ether.h>
21#include <linux/init.h>
22#include <linux/jiffies.h>
77ae32e8 23#include <linux/kref.h>
fcafa5e7 24#include <linux/list.h>
40e220b4 25#include <linux/lockdep.h>
e4aa3412 26#include <linux/minmax.h>
40e220b4 27#include <linux/mutex.h>
1e2c2a4f 28#include <linux/netdevice.h>
024f99cb 29#include <linux/netlink.h>
1e2c2a4f
SE
30#include <linux/pkt_sched.h>
31#include <linux/printk.h>
32#include <linux/random.h>
33#include <linux/rculist.h>
34#include <linux/rcupdate.h>
1e2c2a4f
SE
35#include <linux/skbuff.h>
36#include <linux/slab.h>
37#include <linux/spinlock.h>
38#include <linux/stddef.h>
39#include <linux/string.h>
5c956d11 40#include <linux/string_choices.h>
1e2c2a4f
SE
41#include <linux/types.h>
42#include <linux/workqueue.h>
024f99cb
MS
43#include <net/genetlink.h>
44#include <net/netlink.h>
fec149f5 45#include <uapi/linux/batadv_packet.h>
024f99cb 46#include <uapi/linux/batman_adv.h>
1e2c2a4f 47
a2d08166 48#include "bat_algo.h"
1e2c2a4f 49#include "bitarray.h"
34d99cfe 50#include "gateway_client.h"
1e2c2a4f
SE
51#include "hard-interface.h"
52#include "hash.h"
ba412080 53#include "log.h"
024f99cb 54#include "netlink.h"
1e2c2a4f 55#include "network-coding.h"
fc957275
ML
56#include "originator.h"
57#include "routing.h"
fc957275 58#include "send.h"
1e2c2a4f 59#include "translation-table.h"
1f8dce49 60#include "tvlv.h"
fc957275 61
f0d97253
AQ
62static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
63
791c2a2d 64/**
95298a91 65 * enum batadv_dup_status - duplicate status
791c2a2d
AQ
66 */
67enum batadv_dup_status {
8b84cc4f 68 /** @BATADV_NO_DUP: the packet is no duplicate */
791c2a2d 69 BATADV_NO_DUP = 0,
8b84cc4f
SE
70
71 /**
72 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for
73 * the neighbor)
74 */
791c2a2d 75 BATADV_ORIG_DUP,
8b84cc4f
SE
76
77 /** @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor */
791c2a2d 78 BATADV_NEIGH_DUP,
8b84cc4f
SE
79
80 /**
81 * @BATADV_PROTECTED: originator is currently protected (after reboot)
82 */
791c2a2d
AQ
83 BATADV_PROTECTED,
84};
85
24a5deeb 86/**
7e9a8c2c 87 * batadv_ring_buffer_set() - update the ring buffer with the given value
24a5deeb
AQ
88 * @lq_recv: pointer to the ring buffer
89 * @lq_index: index to store the value at
90 * @value: value to store in the ring buffer
91 */
6b5e971a 92static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
24a5deeb
AQ
93{
94 lq_recv[*lq_index] = value;
95 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
96}
97
98/**
7e9a8c2c 99 * batadv_ring_buffer_avg() - compute the average of all non-zero values stored
24a5deeb
AQ
100 * in the given ring buffer
101 * @lq_recv: pointer to the ring buffer
102 *
62fe710f 103 * Return: computed average value.
24a5deeb 104 */
6b5e971a 105static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
24a5deeb 106{
6b5e971a
SE
107 const u8 *ptr;
108 u16 count = 0;
109 u16 i = 0;
110 u16 sum = 0;
24a5deeb
AQ
111
112 ptr = lq_recv;
113
114 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
115 if (*ptr != 0) {
116 count++;
117 sum += *ptr;
118 }
119
120 i++;
121 ptr++;
122 }
123
124 if (count == 0)
125 return 0;
126
6b5e971a 127 return (u8)(sum / count);
24a5deeb 128}
d98cae64 129
bbad0a5e 130/**
7e9a8c2c
SE
131 * batadv_iv_ogm_orig_get() - retrieve or create (if does not exist) an
132 * originator
94433355 133 * @bat_priv: the bat priv with all the mesh interface information
bbad0a5e
AQ
134 * @addr: mac address of the originator
135 *
62fe710f 136 * Return: the originator object corresponding to the passed mac address or NULL
bbad0a5e 137 * on failure.
bccb48c8 138 * If the object does not exist, it is created and initialised.
bbad0a5e
AQ
139 */
140static struct batadv_orig_node *
6b5e971a 141batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
bbad0a5e
AQ
142{
143 struct batadv_orig_node *orig_node;
f22e0893 144 int hash_added;
bbad0a5e
AQ
145
146 orig_node = batadv_orig_hash_find(bat_priv, addr);
147 if (orig_node)
148 return orig_node;
149
150 orig_node = batadv_orig_node_new(bat_priv, addr);
151 if (!orig_node)
152 return NULL;
153
154 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
155
55db2d59 156 kref_get(&orig_node->refcount);
bbad0a5e
AQ
157 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
158 batadv_choose_orig, orig_node,
159 &orig_node->hash_entry);
160 if (hash_added != 0)
55db2d59 161 goto free_orig_node_hash;
bbad0a5e
AQ
162
163 return orig_node;
164
55db2d59 165free_orig_node_hash:
dee222c7 166 /* reference for batadv_hash_add */
5d967310 167 batadv_orig_node_put(orig_node);
dee222c7 168 /* reference from batadv_orig_node_new */
5d967310 169 batadv_orig_node_put(orig_node);
bbad0a5e
AQ
170
171 return NULL;
172}
173
56303d34
SE
174static struct batadv_neigh_node *
175batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
6b5e971a 176 const u8 *neigh_addr,
56303d34 177 struct batadv_orig_node *orig_node,
863dd7a8 178 struct batadv_orig_node *orig_neigh)
7ae8b285 179{
741aa06b 180 struct batadv_neigh_node *neigh_node;
7ae8b285 181
6f0a6b5e
ML
182 neigh_node = batadv_neigh_node_get_or_create(orig_node,
183 hard_iface, neigh_addr);
7ae8b285
ML
184 if (!neigh_node)
185 goto out;
186
89652331 187 neigh_node->orig_node = orig_neigh;
7ae8b285 188
7ae8b285
ML
189out:
190 return neigh_node;
191}
192
56303d34 193static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
d0b9fd89 194{
96412690 195 struct batadv_ogm_packet *batadv_ogm_packet;
14511519 196 unsigned char *ogm_buff;
6b5e971a 197 u32 random_seqno;
d7d32ec0 198
40e220b4
SE
199 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
200
d7d32ec0
ML
201 /* randomize initial seqno to avoid collision */
202 get_random_bytes(&random_seqno, sizeof(random_seqno));
14511519 203 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
d0b9fd89 204
14511519
ML
205 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
206 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
40e220b4
SE
207 if (!ogm_buff) {
208 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
42d9f2cb 209 return -ENOMEM;
40e220b4 210 }
77af7575 211
14511519
ML
212 hard_iface->bat_iv.ogm_buff = ogm_buff;
213
214 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
a40d9b07
SW
215 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
216 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
217 batadv_ogm_packet->ttl = 2;
96412690 218 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
414254e3 219 batadv_ogm_packet->reserved = 0;
96412690 220 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
77af7575 221
40e220b4
SE
222 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
223
42d9f2cb 224 return 0;
d0b9fd89
ML
225}
226
56303d34 227static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
00a50076 228{
40e220b4
SE
229 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
230
14511519
ML
231 kfree(hard_iface->bat_iv.ogm_buff);
232 hard_iface->bat_iv.ogm_buff = NULL;
40e220b4
SE
233
234 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
00a50076
ML
235}
236
56303d34 237static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
d0b9fd89 238{
96412690 239 struct batadv_ogm_packet *batadv_ogm_packet;
40e220b4 240 void *ogm_buff;
d0b9fd89 241
40e220b4
SE
242 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
243
244 ogm_buff = hard_iface->bat_iv.ogm_buff;
245 if (!ogm_buff)
246 goto unlock;
247
248 batadv_ogm_packet = ogm_buff;
8fdd0153
AQ
249 ether_addr_copy(batadv_ogm_packet->orig,
250 hard_iface->net_dev->dev_addr);
251 ether_addr_copy(batadv_ogm_packet->prev_sender,
252 hard_iface->net_dev->dev_addr);
40e220b4
SE
253
254unlock:
255 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
d0b9fd89
ML
256}
257
56303d34
SE
258static void
259batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
d0b9fd89 260{
96412690 261 struct batadv_ogm_packet *batadv_ogm_packet;
40e220b4 262 void *ogm_buff;
d0b9fd89 263
40e220b4
SE
264 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
265
266 ogm_buff = hard_iface->bat_iv.ogm_buff;
267 if (!ogm_buff)
268 goto unlock;
269
270 batadv_ogm_packet = ogm_buff;
a40d9b07 271 batadv_ogm_packet->ttl = BATADV_TTL;
40e220b4
SE
272
273unlock:
274 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
d0b9fd89
ML
275}
276
b9dacc52 277/* when do we schedule our own ogm to be sent */
fe8bc396 278static unsigned long
56303d34 279batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
b9dacc52 280{
42d0b044
SE
281 unsigned int msecs;
282
283 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
8032bf12 284 msecs += get_random_u32_below(2 * BATADV_JITTER);
42d0b044
SE
285
286 return jiffies + msecs_to_jiffies(msecs);
b9dacc52
ML
287}
288
289/* when do we schedule a ogm packet to be sent */
fe8bc396 290static unsigned long batadv_iv_ogm_fwd_send_time(void)
b9dacc52 291{
8032bf12 292 return jiffies + msecs_to_jiffies(get_random_u32_below(BATADV_JITTER / 2));
b9dacc52
ML
293}
294
295/* apply hop penalty for a normal link */
6b5e971a 296static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
b9dacc52
ML
297{
298 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
42d0b044
SE
299 int new_tq;
300
301 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
302 new_tq /= BATADV_TQ_MAX_VALUE;
303
304 return new_tq;
b9dacc52
ML
305}
306
600184b7 307/**
7e9a8c2c 308 * batadv_iv_ogm_aggr_packet() - checks if there is another OGM attached
600184b7
SW
309 * @buff_pos: current position in the skb
310 * @packet_len: total length of the skb
a15d56a6 311 * @ogm_packet: potential OGM in buffer
600184b7
SW
312 *
313 * Return: true if there is enough space for another OGM, false otherwise.
314 */
a15d56a6
SE
315static bool
316batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
317 const struct batadv_ogm_packet *ogm_packet)
fc957275 318{
08c36d3e
SE
319 int next_buff_pos = 0;
320
a15d56a6
SE
321 /* check if there is enough space for the header */
322 next_buff_pos += buff_pos + sizeof(*ogm_packet);
323 if (next_buff_pos > packet_len)
324 return false;
325
326 /* check if there is enough space for the optional TVLV */
327 next_buff_pos += ntohs(ogm_packet->tvlv_len);
fc957275 328
548b0c5d 329 return next_buff_pos <= packet_len;
fc957275
ML
330}
331
b9dacc52 332/* send a batman ogm to a given interface */
56303d34
SE
333static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
334 struct batadv_hard_iface *hard_iface)
b9dacc52 335{
94433355 336 struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
de12baec 337 const char *fwd_str;
6b5e971a
SE
338 u8 packet_num;
339 s16 buff_pos;
96412690 340 struct batadv_ogm_packet *batadv_ogm_packet;
b9dacc52 341 struct sk_buff *skb;
6b5e971a 342 u8 *packet_pos;
b9dacc52 343
e9a4f295 344 if (hard_iface->if_status != BATADV_IF_ACTIVE)
b9dacc52
ML
345 return;
346
347 packet_num = 0;
348 buff_pos = 0;
c67893d1
SE
349 packet_pos = forw_packet->skb->data;
350 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
b9dacc52
ML
351
352 /* adjust all flags and log packets */
fe8bc396 353 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
a15d56a6 354 batadv_ogm_packet)) {
b9dacc52 355 /* we might have aggregated direct link packets with an
9cfc7bd6
SE
356 * ordinary base packet
357 */
77405977 358 if (test_bit(packet_num, forw_packet->direct_link_flags) &&
8de47de5 359 forw_packet->if_incoming == hard_iface)
96412690 360 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
b9dacc52 361 else
96412690 362 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
b9dacc52 363
c67893d1
SE
364 if (packet_num > 0 || !forw_packet->own)
365 fwd_str = "Forwarding";
366 else
367 fwd_str = "Sending own";
368
39c75a51 369 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
e1bf0c14 370 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
1eda58bf 371 fwd_str, (packet_num > 0 ? "aggregated " : ""),
96412690
SE
372 batadv_ogm_packet->orig,
373 ntohl(batadv_ogm_packet->seqno),
a40d9b07 374 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
5c956d11 375 str_on_off(batadv_ogm_packet->flags & BATADV_DIRECTLINK),
e1bf0c14 376 hard_iface->net_dev->name,
1eda58bf 377 hard_iface->net_dev->dev_addr);
b9dacc52 378
7e071c79 379 buff_pos += BATADV_OGM_HLEN;
ef261577 380 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
b9dacc52 381 packet_num++;
c67893d1
SE
382 packet_pos = forw_packet->skb->data + buff_pos;
383 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
b9dacc52
ML
384 }
385
386 /* create clone because function is called more than once */
387 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
f8214865 388 if (skb) {
d69909d2
SE
389 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
390 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
f8214865 391 skb->len + ETH_HLEN);
95d39278 392 batadv_send_broadcast_skb(skb, hard_iface);
f8214865 393 }
b9dacc52
ML
394}
395
396/* send a batman ogm packet */
56303d34 397static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
b9dacc52 398{
94433355 399 struct net_device *mesh_iface;
b9dacc52
ML
400
401 if (!forw_packet->if_incoming) {
86ceb360 402 pr_err("Error - can't forward packet: incoming iface not specified\n");
f55a2e84 403 return;
b9dacc52
ML
404 }
405
94433355 406 mesh_iface = forw_packet->if_incoming->mesh_iface;
b9dacc52 407
ef0a937f 408 if (WARN_ON(!forw_packet->if_outgoing))
f55a2e84 409 return;
b9dacc52 410
94433355
SE
411 if (forw_packet->if_outgoing->mesh_iface != mesh_iface) {
412 pr_warn("%s: mesh interface switch for queued OGM\n", __func__);
f55a2e84 413 return;
9f460ae3 414 }
b9dacc52 415
ef0a937f 416 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
f55a2e84 417 return;
b9dacc52 418
ef0a937f
SW
419 /* only for one specific outgoing interface */
420 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
b9dacc52
ML
421}
422
ef0a937f 423/**
7e9a8c2c 424 * batadv_iv_ogm_can_aggregate() - find out if an OGM can be aggregated on an
ef0a937f
SW
425 * existing forward packet
426 * @new_bat_ogm_packet: OGM packet to be aggregated
94433355 427 * @bat_priv: the bat priv with all the mesh interface information
ef0a937f
SW
428 * @packet_len: (total) length of the OGM
429 * @send_time: timestamp (jiffies) when the packet is to be sent
95298a91 430 * @directlink: true if this is a direct link packet
ef0a937f
SW
431 * @if_incoming: interface where the packet was received
432 * @if_outgoing: interface for which the retransmission should be considered
433 * @forw_packet: the forwarded packet which should be checked
434 *
62fe710f 435 * Return: true if new_packet can be aggregated with forw_packet
ef0a937f 436 */
fe8bc396 437static bool
96412690 438batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
56303d34 439 struct batadv_priv *bat_priv,
fe8bc396
SE
440 int packet_len, unsigned long send_time,
441 bool directlink,
56303d34 442 const struct batadv_hard_iface *if_incoming,
ef0a937f 443 const struct batadv_hard_iface *if_outgoing,
56303d34 444 const struct batadv_forw_packet *forw_packet)
b9dacc52 445{
96412690 446 struct batadv_ogm_packet *batadv_ogm_packet;
e4aa3412 447 unsigned int aggregated_bytes = forw_packet->packet_len + packet_len;
56303d34 448 struct batadv_hard_iface *primary_if = NULL;
0db11005 449 u8 packet_num = forw_packet->num_packets;
b9dacc52 450 bool res = false;
42d0b044 451 unsigned long aggregation_end_time;
e4aa3412 452 unsigned int max_bytes;
b9dacc52 453
96412690 454 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
42d0b044
SE
455 aggregation_end_time = send_time;
456 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
b9dacc52 457
e4aa3412
SE
458 max_bytes = min_t(unsigned int, if_outgoing->net_dev->mtu,
459 BATADV_MAX_AGGREGATION_BYTES);
460
9cfc7bd6 461 /* we can aggregate the current packet to this aggregated packet
b9dacc52
ML
462 * if:
463 *
464 * - the send time is within our MAX_AGGREGATION_MS time
35796c1d 465 * - the resulting packet won't be bigger than
e4aa3412 466 * MAX_AGGREGATION_BYTES and MTU of the outgoing interface
77405977 467 * - the number of packets is lower than MAX_AGGREGATION_PACKETS
8f34b388 468 * otherwise aggregation is not possible
b9dacc52 469 */
8f34b388
MP
470 if (!time_before(send_time, forw_packet->send_time) ||
471 !time_after_eq(aggregation_end_time, forw_packet->send_time))
472 return false;
473
e4aa3412 474 if (aggregated_bytes > max_bytes)
8f34b388
MP
475 return false;
476
77405977 477 if (packet_num >= BATADV_MAX_AGGREGATION_PACKETS)
434becf5
SE
478 return false;
479
8f34b388
MP
480 /* packet is not leaving on the same interface. */
481 if (forw_packet->if_outgoing != if_outgoing)
482 return false;
483
484 /* check aggregation compatibility
485 * -> direct link packets are broadcasted on
486 * their interface only
487 * -> aggregate packet if the current packet is
488 * a "global" packet as well as the base
489 * packet
490 */
491 primary_if = batadv_primary_if_get_selected(bat_priv);
492 if (!primary_if)
493 return false;
ef0a937f 494
8f34b388
MP
495 /* packets without direct link flag and high TTL
496 * are flooded through the net
497 */
498 if (!directlink &&
499 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
500 batadv_ogm_packet->ttl != 1 &&
501
502 /* own packets originating non-primary
503 * interfaces leave only that interface
504 */
505 (!forw_packet->own ||
506 forw_packet->if_incoming == primary_if)) {
507 res = true;
508 goto out;
509 }
b9dacc52 510
8f34b388
MP
511 /* if the incoming packet is sent via this one
512 * interface only - we still can aggregate
513 */
514 if (directlink &&
515 new_bat_ogm_packet->ttl == 1 &&
516 forw_packet->if_incoming == if_incoming &&
517
518 /* packets from direct neighbors or
519 * own secondary interface packets
520 * (= secondary interface packets in general)
521 */
522 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
523 (forw_packet->own &&
524 forw_packet->if_incoming != primary_if))) {
525 res = true;
526 goto out;
b9dacc52
ML
527 }
528
529out:
79a0bffb 530 batadv_hardif_put(primary_if);
b9dacc52
ML
531 return res;
532}
533
1b371d13 534/**
7e9a8c2c 535 * batadv_iv_ogm_aggregate_new() - create a new aggregated packet and add this
ef0a937f
SW
536 * packet to it.
537 * @packet_buff: pointer to the OGM
538 * @packet_len: (total) length of the OGM
539 * @send_time: timestamp (jiffies) when the packet is to be sent
540 * @direct_link: whether this OGM has direct link status
541 * @if_incoming: interface where the packet was received
542 * @if_outgoing: interface for which the retransmission should be considered
543 * @own_packet: true if it is a self-generated ogm
544 */
fe8bc396
SE
545static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
546 int packet_len, unsigned long send_time,
547 bool direct_link,
56303d34 548 struct batadv_hard_iface *if_incoming,
ef0a937f 549 struct batadv_hard_iface *if_outgoing,
fe8bc396 550 int own_packet)
b9dacc52 551{
94433355 552 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
56303d34 553 struct batadv_forw_packet *forw_packet_aggr;
99ba18ef 554 struct sk_buff *skb;
b9dacc52 555 unsigned char *skb_buff;
42d0b044 556 unsigned int skb_size;
a65e5481 557 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
b9dacc52 558
e4aa3412
SE
559 if (atomic_read(&bat_priv->aggregated_ogms))
560 skb_size = max_t(unsigned int, BATADV_MAX_AGGREGATION_BYTES,
561 packet_len);
b9dacc52 562 else
5b246574
SE
563 skb_size = packet_len;
564
41ab6c48 565 skb_size += ETH_HLEN;
b9dacc52 566
99ba18ef
LL
567 skb = netdev_alloc_skb_ip_align(NULL, skb_size);
568 if (!skb)
569 return;
570
571 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
572 queue_left, bat_priv, skb);
573 if (!forw_packet_aggr) {
574 kfree_skb(skb);
a65e5481
LL
575 return;
576 }
577
c54f38c9 578 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
41ab6c48 579 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
b9dacc52 580
b9dacc52
ML
581 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
582 forw_packet_aggr->packet_len = packet_len;
583 memcpy(skb_buff, packet_buff, packet_len);
584
585 forw_packet_aggr->own = own_packet;
77405977
SE
586 bitmap_zero(forw_packet_aggr->direct_link_flags,
587 BATADV_MAX_AGGREGATION_PACKETS);
b9dacc52
ML
588 forw_packet_aggr->send_time = send_time;
589
590 /* save packet direct link flag status */
591 if (direct_link)
77405977 592 set_bit(0, forw_packet_aggr->direct_link_flags);
b9dacc52 593
b9dacc52 594 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
f0d97253 595 batadv_iv_send_outstanding_bat_ogm_packet);
9b4aec64
LL
596
597 batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
b9dacc52
ML
598}
599
600/* aggregate a new packet into the existing ogm packet */
56303d34 601static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
fe8bc396
SE
602 const unsigned char *packet_buff,
603 int packet_len, bool direct_link)
b9dacc52 604{
e04de486 605 skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
b9dacc52 606 forw_packet_aggr->packet_len += packet_len;
b9dacc52
ML
607
608 /* save packet direct link flag status */
77405977
SE
609 if (direct_link)
610 set_bit(forw_packet_aggr->num_packets,
611 forw_packet_aggr->direct_link_flags);
0db11005
SE
612
613 forw_packet_aggr->num_packets++;
b9dacc52
ML
614}
615
ef0a937f 616/**
7e9a8c2c 617 * batadv_iv_ogm_queue_add() - queue up an OGM for transmission
94433355 618 * @bat_priv: the bat priv with all the mesh interface information
ef0a937f
SW
619 * @packet_buff: pointer to the OGM
620 * @packet_len: (total) length of the OGM
621 * @if_incoming: interface where the packet was received
622 * @if_outgoing: interface for which the retransmission should be considered
623 * @own_packet: true if it is a self-generated ogm
624 * @send_time: timestamp (jiffies) when the packet is to be sent
625 */
56303d34 626static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
fe8bc396
SE
627 unsigned char *packet_buff,
628 int packet_len,
56303d34 629 struct batadv_hard_iface *if_incoming,
ef0a937f 630 struct batadv_hard_iface *if_outgoing,
fe8bc396 631 int own_packet, unsigned long send_time)
b9dacc52 632{
9cfc7bd6 633 /* _aggr -> pointer to the packet we want to aggregate with
b9dacc52
ML
634 * _pos -> pointer to the position in the queue
635 */
56303d34
SE
636 struct batadv_forw_packet *forw_packet_aggr = NULL;
637 struct batadv_forw_packet *forw_packet_pos = NULL;
96412690 638 struct batadv_ogm_packet *batadv_ogm_packet;
b9dacc52 639 bool direct_link;
42d0b044 640 unsigned long max_aggregation_jiffies;
b9dacc52 641
96412690 642 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
56489151 643 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
42d0b044 644 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
b9dacc52
ML
645
646 /* find position for the packet in the forward queue */
647 spin_lock_bh(&bat_priv->forw_bat_list_lock);
648 /* own packets are not to be aggregated */
56489151 649 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
b67bfe0d 650 hlist_for_each_entry(forw_packet_pos,
b9dacc52 651 &bat_priv->forw_bat_list, list) {
96412690 652 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
fe8bc396
SE
653 bat_priv, packet_len,
654 send_time, direct_link,
655 if_incoming,
ef0a937f 656 if_outgoing,
fe8bc396 657 forw_packet_pos)) {
b9dacc52
ML
658 forw_packet_aggr = forw_packet_pos;
659 break;
660 }
661 }
662 }
663
664 /* nothing to aggregate with - either aggregation disabled or no
9cfc7bd6
SE
665 * suitable aggregation packet found
666 */
b9dacc52
ML
667 if (!forw_packet_aggr) {
668 /* the following section can run without the lock */
669 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
670
9cfc7bd6 671 /* if we could not aggregate this packet with one of the others
b9dacc52
ML
672 * we hold it back for a while, so that it might be aggregated
673 * later on
674 */
42d0b044
SE
675 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
676 send_time += max_aggregation_jiffies;
b9dacc52 677
fe8bc396
SE
678 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
679 send_time, direct_link,
ef0a937f
SW
680 if_incoming, if_outgoing,
681 own_packet);
b9dacc52 682 } else {
fe8bc396
SE
683 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
684 packet_len, direct_link);
b9dacc52
ML
685 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
686 }
687}
688
56303d34 689static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
fe8bc396 690 const struct ethhdr *ethhdr,
96412690 691 struct batadv_ogm_packet *batadv_ogm_packet,
fe8bc396
SE
692 bool is_single_hop_neigh,
693 bool is_from_best_next_hop,
ef0a937f
SW
694 struct batadv_hard_iface *if_incoming,
695 struct batadv_hard_iface *if_outgoing)
b9dacc52 696{
94433355 697 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
6b5e971a 698 u16 tvlv_len;
b9dacc52 699
a40d9b07 700 if (batadv_ogm_packet->ttl <= 1) {
39c75a51 701 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
b9dacc52
ML
702 return;
703 }
704
13b2541b
ML
705 if (!is_from_best_next_hop) {
706 /* Mark the forwarded packet when it is not coming from our
707 * best next hop. We still need to forward the packet for our
708 * neighbor link quality detection to work in case the packet
709 * originated from a single hop neighbor. Otherwise we can
710 * simply drop the ogm.
711 */
712 if (is_single_hop_neigh)
96412690 713 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
13b2541b
ML
714 else
715 return;
716 }
b9dacc52 717
ef261577 718 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
b9dacc52 719
a40d9b07 720 batadv_ogm_packet->ttl--;
8fdd0153 721 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
b9dacc52 722
b9dacc52 723 /* apply hop penalty */
96412690 724 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
fe8bc396 725 bat_priv);
b9dacc52 726
39c75a51 727 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 728 "Forwarding packet: tq: %i, ttl: %i\n",
a40d9b07 729 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
b9dacc52 730
75cd33f8 731 if (is_single_hop_neigh)
96412690 732 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
b9dacc52 733 else
96412690 734 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
b9dacc52 735
96412690 736 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
ef261577 737 BATADV_OGM_HLEN + tvlv_len,
ef0a937f
SW
738 if_incoming, if_outgoing, 0,
739 batadv_iv_ogm_fwd_send_time());
b9dacc52
ML
740}
741
d9896617 742/**
7e9a8c2c
SE
743 * batadv_iv_ogm_slide_own_bcast_window() - bitshift own OGM broadcast windows
744 * for the given interface
d9896617
AQ
745 * @hard_iface: the interface for which the windows have to be shifted
746 */
747static void
748batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
749{
94433355 750 struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
d9896617
AQ
751 struct batadv_hashtable *hash = bat_priv->orig_hash;
752 struct hlist_head *head;
753 struct batadv_orig_node *orig_node;
dee222c7 754 struct batadv_orig_ifinfo *orig_ifinfo;
d9896617 755 unsigned long *word;
6b5e971a 756 u32 i;
6b5e971a 757 u8 *w;
d9896617
AQ
758
759 for (i = 0; i < hash->size; i++) {
760 head = &hash->table[i];
761
762 rcu_read_lock();
763 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
dee222c7
SE
764 hlist_for_each_entry_rcu(orig_ifinfo,
765 &orig_node->ifinfo_list,
766 list) {
767 if (orig_ifinfo->if_outgoing != hard_iface)
768 continue;
769
770 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
771 word = orig_ifinfo->bat_iv.bcast_own;
772 batadv_bit_get_packet(bat_priv, word, 1, 0);
773 w = &orig_ifinfo->bat_iv.bcast_own_sum;
774 *w = bitmap_weight(word,
775 BATADV_TQ_LOCAL_WINDOW_SIZE);
776 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
777 }
d9896617
AQ
778 }
779 rcu_read_unlock();
780 }
781}
782
40e220b4
SE
783/**
784 * batadv_iv_ogm_schedule_buff() - schedule submission of hardif ogm buffer
785 * @hard_iface: interface whose ogm buffer should be transmitted
786 */
787static void batadv_iv_ogm_schedule_buff(struct batadv_hard_iface *hard_iface)
b9dacc52 788{
94433355 789 struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface);
14511519 790 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
96412690 791 struct batadv_ogm_packet *batadv_ogm_packet;
ef0a937f 792 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
14511519 793 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
6b5e971a
SE
794 u32 seqno;
795 u16 tvlv_len = 0;
ef0a937f 796 unsigned long send_time;
b9dacc52 797
40e220b4 798 lockdep_assert_held(&hard_iface->bat_iv.ogm_buff_mutex);
f0d97253 799
8e8ce081
SE
800 /* interface already disabled by batadv_iv_ogm_iface_disable */
801 if (!*ogm_buff)
802 return;
803
f0d97253
AQ
804 /* the interface gets activated here to avoid race conditions between
805 * the moment of activating the interface in
806 * hardif_activate_interface() where the originator mac is set and
807 * outdated packets (especially uninitialized mac addresses) in the
808 * packet queue
809 */
810 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
811 hard_iface->if_status = BATADV_IF_ACTIVE;
812
e5d89254 813 primary_if = batadv_primary_if_get_selected(bat_priv);
b9dacc52 814
e1bf0c14
ML
815 if (hard_iface == primary_if) {
816 /* tt changes have to be committed before the tvlv data is
817 * appended as it may alter the tt tvlv container
818 */
819 batadv_tt_local_commit_changes(bat_priv);
ef261577
ML
820 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
821 ogm_buff_len,
822 BATADV_OGM_HLEN);
e1bf0c14 823 }
be9aa4c1 824
14511519 825 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
ef261577 826 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
b9dacc52
ML
827
828 /* change sequence number to network order */
6b5e971a 829 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
bbb1f90e 830 batadv_ogm_packet->seqno = htonl(seqno);
14511519 831 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
b9dacc52 832
d9896617 833 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
b9dacc52 834
ef0a937f
SW
835 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
836
837 if (hard_iface != primary_if) {
838 /* OGMs from secondary interfaces are only scheduled on their
839 * respective interfaces.
840 */
841 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
842 hard_iface, hard_iface, 1, send_time);
843 goto out;
844 }
845
846 /* OGMs from primary interfaces are scheduled on all
847 * interfaces.
848 */
849 rcu_read_lock();
850 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
94433355 851 if (tmp_hard_iface->mesh_iface != hard_iface->mesh_iface)
87b40f53 852 continue;
27353446
SE
853
854 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
855 continue;
856
ef0a937f
SW
857 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
858 *ogm_buff_len, hard_iface,
859 tmp_hard_iface, 1, send_time);
27353446
SE
860
861 batadv_hardif_put(tmp_hard_iface);
ef0a937f
SW
862 }
863 rcu_read_unlock();
864
865out:
79a0bffb 866 batadv_hardif_put(primary_if);
b9dacc52
ML
867}
868
40e220b4
SE
869static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
870{
871 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
872 hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
873 return;
874
875 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
876 batadv_iv_ogm_schedule_buff(hard_iface);
877 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
878}
879
dee222c7 880/**
bccb48c8 881 * batadv_iv_orig_ifinfo_sum() - Get bcast_own sum for originator over interface
dee222c7
SE
882 * @orig_node: originator which reproadcasted the OGMs directly
883 * @if_outgoing: interface which transmitted the original OGM and received the
884 * direct rebroadcast
885 *
886 * Return: Number of replied (rebroadcasted) OGMs which were transmitted by
887 * an originator and directly (without intermediate hop) received by a specific
888 * interface
889 */
890static u8 batadv_iv_orig_ifinfo_sum(struct batadv_orig_node *orig_node,
891 struct batadv_hard_iface *if_outgoing)
892{
893 struct batadv_orig_ifinfo *orig_ifinfo;
894 u8 sum;
895
896 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
897 if (!orig_ifinfo)
898 return 0;
899
900 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
901 sum = orig_ifinfo->bat_iv.bcast_own_sum;
902 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
903
904 batadv_orig_ifinfo_put(orig_ifinfo);
905
906 return sum;
907}
908
89652331 909/**
7e9a8c2c 910 * batadv_iv_ogm_orig_update() - use OGM to update corresponding data in an
89652331 911 * originator
94433355 912 * @bat_priv: the bat priv with all the mesh interface information
89652331 913 * @orig_node: the orig node who originally emitted the ogm packet
7351a482 914 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
89652331
SW
915 * @ethhdr: Ethernet header of the OGM
916 * @batadv_ogm_packet: the ogm packet
917 * @if_incoming: interface where the packet was received
918 * @if_outgoing: interface for which the retransmission should be considered
89652331
SW
919 * @dup_status: the duplicate status of this ogm packet.
920 */
fe8bc396 921static void
56303d34
SE
922batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
923 struct batadv_orig_node *orig_node,
7351a482 924 struct batadv_orig_ifinfo *orig_ifinfo,
fe8bc396 925 const struct ethhdr *ethhdr,
96412690 926 const struct batadv_ogm_packet *batadv_ogm_packet,
56303d34 927 struct batadv_hard_iface *if_incoming,
89652331 928 struct batadv_hard_iface *if_outgoing,
7c24bbbe 929 enum batadv_dup_status dup_status)
fc957275 930{
89652331
SW
931 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
932 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
4f248cff
SE
933 struct batadv_neigh_node *neigh_node = NULL;
934 struct batadv_neigh_node *tmp_neigh_node = NULL;
56303d34 935 struct batadv_neigh_node *router = NULL;
6b5e971a
SE
936 u8 sum_orig, sum_neigh;
937 u8 *neigh_addr;
938 u8 tq_avg;
fc957275 939
39c75a51 940 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
22f0502e
SE
941 "%s(): Searching and updating originator entry of received packet\n",
942 __func__);
fc957275
ML
943
944 rcu_read_lock();
b67bfe0d 945 hlist_for_each_entry_rcu(tmp_neigh_node,
fc957275 946 &orig_node->neigh_list, list) {
1eda58bf
SE
947 neigh_addr = tmp_neigh_node->addr;
948 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
949 tmp_neigh_node->if_incoming == if_incoming &&
77ae32e8 950 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
38dc40ef 951 if (WARN(neigh_node, "too many matching neigh_nodes"))
25bb2509 952 batadv_neigh_node_put(neigh_node);
fc957275
ML
953 neigh_node = tmp_neigh_node;
954 continue;
955 }
956
7c24bbbe 957 if (dup_status != BATADV_NO_DUP)
fc957275
ML
958 continue;
959
89652331
SW
960 /* only update the entry for this outgoing interface */
961 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
962 if_outgoing);
963 if (!neigh_ifinfo)
964 continue;
965
966 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
967 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
968 &neigh_ifinfo->bat_iv.tq_index, 0);
969 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
970 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
971 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
972
044fa3ae 973 batadv_neigh_ifinfo_put(neigh_ifinfo);
89652331 974 neigh_ifinfo = NULL;
fc957275
ML
975 }
976
977 if (!neigh_node) {
56303d34 978 struct batadv_orig_node *orig_tmp;
fc957275 979
bbad0a5e 980 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
fc957275
ML
981 if (!orig_tmp)
982 goto unlock;
983
fe8bc396
SE
984 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
985 ethhdr->h_source,
863dd7a8 986 orig_node, orig_tmp);
fc957275 987
5d967310 988 batadv_orig_node_put(orig_tmp);
fc957275
ML
989 if (!neigh_node)
990 goto unlock;
23badd6d 991 } else {
39c75a51 992 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 993 "Updating existing last-hop neighbor of originator\n");
23badd6d 994 }
fc957275
ML
995
996 rcu_read_unlock();
89652331
SW
997 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
998 if (!neigh_ifinfo)
999 goto out;
fc957275 1000
d7b2a97e 1001 neigh_node->last_seen = jiffies;
fc957275 1002
89652331
SW
1003 spin_lock_bh(&neigh_node->ifinfo_lock);
1004 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1005 &neigh_ifinfo->bat_iv.tq_index,
96412690 1006 batadv_ogm_packet->tq);
89652331
SW
1007 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1008 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1009 spin_unlock_bh(&neigh_node->ifinfo_lock);
fc957275 1010
7c24bbbe 1011 if (dup_status == BATADV_NO_DUP) {
7351a482 1012 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
89652331 1013 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
fc957275
ML
1014 }
1015
fc957275 1016 /* if this neighbor already is our next hop there is nothing
9cfc7bd6
SE
1017 * to change
1018 */
7351a482 1019 router = batadv_orig_router_get(orig_node, if_outgoing);
fc957275 1020 if (router == neigh_node)
e1bf0c14 1021 goto out;
fc957275 1022
89652331
SW
1023 if (router) {
1024 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1025 if (!router_ifinfo)
1026 goto out;
1027
1028 /* if this neighbor does not offer a better TQ we won't
1029 * consider it
1030 */
1031 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1032 goto out;
1033 }
fc957275
ML
1034
1035 /* if the TQ is the same and the link not more symmetric we
9cfc7bd6
SE
1036 * won't consider it either
1037 */
89652331 1038 if (router_ifinfo &&
01b97a3e 1039 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
dee222c7
SE
1040 sum_orig = batadv_iv_orig_ifinfo_sum(router->orig_node,
1041 router->if_incoming);
1042 sum_neigh = batadv_iv_orig_ifinfo_sum(neigh_node->orig_node,
1043 neigh_node->if_incoming);
bbb1f90e 1044 if (sum_orig >= sum_neigh)
e1bf0c14 1045 goto out;
fc957275
ML
1046 }
1047
7351a482 1048 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
fc957275
ML
1049 goto out;
1050
1051unlock:
1052 rcu_read_unlock();
1053out:
79a0bffb
SE
1054 batadv_neigh_node_put(neigh_node);
1055 batadv_neigh_node_put(router);
1056 batadv_neigh_ifinfo_put(neigh_ifinfo);
1057 batadv_neigh_ifinfo_put(router_ifinfo);
fc957275
ML
1058}
1059
89652331 1060/**
7e9a8c2c 1061 * batadv_iv_ogm_calc_tq() - calculate tq for current received ogm packet
89652331
SW
1062 * @orig_node: the orig node who originally emitted the ogm packet
1063 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1064 * @batadv_ogm_packet: the ogm packet
1065 * @if_incoming: interface where the packet was received
1066 * @if_outgoing: interface for which the retransmission should be considered
1067 *
4b426b10 1068 * Return: true if the link can be considered bidirectional, false otherwise
89652331 1069 */
4b426b10
SE
1070static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1071 struct batadv_orig_node *orig_neigh_node,
1072 struct batadv_ogm_packet *batadv_ogm_packet,
1073 struct batadv_hard_iface *if_incoming,
1074 struct batadv_hard_iface *if_outgoing)
fc957275 1075{
94433355 1076 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
56303d34 1077 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
89652331 1078 struct batadv_neigh_ifinfo *neigh_ifinfo;
6b5e971a
SE
1079 u8 total_count;
1080 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
3bda14d0 1081 unsigned int tq_iface_hop_penalty = BATADV_TQ_MAX_VALUE;
42d0b044 1082 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
d285f52c 1083 unsigned int tq_asym_penalty, inv_asym_penalty;
42d0b044 1084 unsigned int combined_tq;
4b426b10 1085 bool ret = false;
fc957275
ML
1086
1087 /* find corresponding one hop neighbor */
1088 rcu_read_lock();
b67bfe0d 1089 hlist_for_each_entry_rcu(tmp_neigh_node,
fc957275 1090 &orig_neigh_node->neigh_list, list) {
1eda58bf
SE
1091 if (!batadv_compare_eth(tmp_neigh_node->addr,
1092 orig_neigh_node->orig))
fc957275
ML
1093 continue;
1094
1095 if (tmp_neigh_node->if_incoming != if_incoming)
1096 continue;
1097
77ae32e8 1098 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
fc957275
ML
1099 continue;
1100
1101 neigh_node = tmp_neigh_node;
1102 break;
1103 }
1104 rcu_read_unlock();
1105
1106 if (!neigh_node)
fe8bc396
SE
1107 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1108 orig_neigh_node->orig,
1109 orig_neigh_node,
863dd7a8 1110 orig_neigh_node);
fc957275
ML
1111
1112 if (!neigh_node)
1113 goto out;
1114
d7b2a97e 1115 /* if orig_node is direct neighbor update neigh_node last_seen */
fc957275 1116 if (orig_node == orig_neigh_node)
d7b2a97e 1117 neigh_node->last_seen = jiffies;
fc957275 1118
d7b2a97e 1119 orig_node->last_seen = jiffies;
fc957275
ML
1120
1121 /* find packet count of corresponding one hop neighbor */
dee222c7 1122 orig_eq_count = batadv_iv_orig_ifinfo_sum(orig_neigh_node, if_incoming);
89652331
SW
1123 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1124 if (neigh_ifinfo) {
1125 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
044fa3ae 1126 batadv_neigh_ifinfo_put(neigh_ifinfo);
89652331
SW
1127 } else {
1128 neigh_rq_count = 0;
1129 }
fc957275
ML
1130
1131 /* pay attention to not get a value bigger than 100 % */
c67893d1
SE
1132 if (orig_eq_count > neigh_rq_count)
1133 total_count = neigh_rq_count;
1134 else
1135 total_count = orig_eq_count;
fc957275 1136
9cfc7bd6
SE
1137 /* if we have too few packets (too less data) we set tq_own to zero
1138 * if we receive too few packets it is not considered bidirectional
1139 */
42d0b044
SE
1140 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1141 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
fc957275
ML
1142 tq_own = 0;
1143 else
1144 /* neigh_node->real_packet_count is never zero as we
1145 * only purge old information when getting new
9cfc7bd6
SE
1146 * information
1147 */
42d0b044 1148 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
fc957275 1149
21a1236b 1150 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
fc957275
ML
1151 * affect the nearly-symmetric links only a little, but
1152 * punishes asymmetric links more. This will give a value
1153 * between 0 and TQ_MAX_VALUE
1154 */
42d0b044
SE
1155 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1156 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1157 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1158 BATADV_TQ_LOCAL_WINDOW_SIZE *
1159 BATADV_TQ_LOCAL_WINDOW_SIZE;
1160 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1161 inv_asym_penalty /= neigh_rq_max_cube;
1162 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
3bda14d0 1163 tq_iface_hop_penalty -= atomic_read(&if_incoming->hop_penalty);
42d0b044 1164
c0398768
SW
1165 /* penalize if the OGM is forwarded on the same interface. WiFi
1166 * interfaces and other half duplex devices suffer from throughput
1167 * drops as they can't send and receive at the same time.
1168 */
825ffe1f 1169 if (if_outgoing && if_incoming == if_outgoing &&
10b1bbb4 1170 batadv_is_wifi_hardif(if_outgoing))
3bda14d0
LL
1171 tq_iface_hop_penalty = batadv_hop_penalty(tq_iface_hop_penalty,
1172 bat_priv);
c0398768
SW
1173
1174 combined_tq = batadv_ogm_packet->tq *
1175 tq_own *
1176 tq_asym_penalty *
3bda14d0 1177 tq_iface_hop_penalty;
c0398768
SW
1178 combined_tq /= BATADV_TQ_MAX_VALUE *
1179 BATADV_TQ_MAX_VALUE *
1180 BATADV_TQ_MAX_VALUE;
96412690 1181 batadv_ogm_packet->tq = combined_tq;
fc957275 1182
39c75a51 1183 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
3bda14d0 1184 "bidirectional: orig = %pM neigh = %pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_hop_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
1eda58bf 1185 orig_node->orig, orig_neigh_node->orig, total_count,
3bda14d0
LL
1186 neigh_rq_count, tq_own, tq_asym_penalty,
1187 tq_iface_hop_penalty, batadv_ogm_packet->tq,
1188 if_incoming->net_dev->name,
c0398768 1189 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
fc957275
ML
1190
1191 /* if link has the minimum required transmission quality
9cfc7bd6
SE
1192 * consider it bidirectional
1193 */
96412690 1194 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
4b426b10 1195 ret = true;
fc957275
ML
1196
1197out:
79a0bffb 1198 batadv_neigh_node_put(neigh_node);
fc957275
ML
1199 return ret;
1200}
1201
7c24bbbe 1202/**
7e9a8c2c 1203 * batadv_iv_ogm_update_seqnos() - process a batman packet for all interfaces,
7c24bbbe
SW
1204 * adjust the sequence number and find out whether it is a duplicate
1205 * @ethhdr: ethernet header of the packet
1206 * @batadv_ogm_packet: OGM packet to be considered
1207 * @if_incoming: interface on which the OGM packet was received
89652331 1208 * @if_outgoing: interface for which the retransmission should be considered
7c24bbbe 1209 *
62fe710f 1210 * Return: duplicate status as enum batadv_dup_status
fc957275 1211 */
7c24bbbe 1212static enum batadv_dup_status
fe8bc396 1213batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
96412690 1214 const struct batadv_ogm_packet *batadv_ogm_packet,
89652331
SW
1215 const struct batadv_hard_iface *if_incoming,
1216 struct batadv_hard_iface *if_outgoing)
fc957275 1217{
94433355 1218 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
56303d34 1219 struct batadv_orig_node *orig_node;
7351a482 1220 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
89652331
SW
1221 struct batadv_neigh_node *neigh_node;
1222 struct batadv_neigh_ifinfo *neigh_ifinfo;
4b426b10 1223 bool is_dup;
6b5e971a 1224 s32 seq_diff;
4b426b10 1225 bool need_update = false;
7c24bbbe
SW
1226 int set_mark;
1227 enum batadv_dup_status ret = BATADV_NO_DUP;
6b5e971a
SE
1228 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1229 u8 *neigh_addr;
1230 u8 packet_count;
0538f759 1231 unsigned long *bitmap;
fc957275 1232
bbad0a5e 1233 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
fc957275 1234 if (!orig_node)
7c24bbbe 1235 return BATADV_NO_DUP;
fc957275 1236
7351a482
SW
1237 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1238 if (WARN_ON(!orig_ifinfo)) {
5d967310 1239 batadv_orig_node_put(orig_node);
7351a482
SW
1240 return 0;
1241 }
1242
bbad0a5e 1243 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
7351a482 1244 seq_diff = seqno - orig_ifinfo->last_real_seqno;
fc957275
ML
1245
1246 /* signalize caller that the packet is to be dropped. */
1e5cc266 1247 if (!hlist_empty(&orig_node->neigh_list) &&
30d3c511 1248 batadv_window_protected(bat_priv, seq_diff,
81f02683
SW
1249 BATADV_TQ_LOCAL_WINDOW_SIZE,
1250 &orig_ifinfo->batman_seqno_reset, NULL)) {
7c24bbbe 1251 ret = BATADV_PROTECTED;
fc957275 1252 goto out;
7c24bbbe 1253 }
fc957275
ML
1254
1255 rcu_read_lock();
89652331
SW
1256 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1257 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1258 if_outgoing);
1259 if (!neigh_ifinfo)
1260 continue;
1261
1262 neigh_addr = neigh_node->addr;
1263 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
7351a482 1264 orig_ifinfo->last_real_seqno,
7c24bbbe
SW
1265 seqno);
1266
1eda58bf 1267 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
89652331 1268 neigh_node->if_incoming == if_incoming) {
fc957275 1269 set_mark = 1;
7c24bbbe
SW
1270 if (is_dup)
1271 ret = BATADV_NEIGH_DUP;
1272 } else {
fc957275 1273 set_mark = 0;
825ffe1f 1274 if (is_dup && ret != BATADV_NEIGH_DUP)
7c24bbbe
SW
1275 ret = BATADV_ORIG_DUP;
1276 }
fc957275
ML
1277
1278 /* if the window moved, set the update flag. */
89652331 1279 bitmap = neigh_ifinfo->bat_iv.real_bits;
0538f759 1280 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
0f5f9322 1281 seq_diff, set_mark);
fc957275 1282
89652331 1283 packet_count = bitmap_weight(bitmap,
bbb1f90e 1284 BATADV_TQ_LOCAL_WINDOW_SIZE);
89652331 1285 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
044fa3ae 1286 batadv_neigh_ifinfo_put(neigh_ifinfo);
fc957275
ML
1287 }
1288 rcu_read_unlock();
1289
1290 if (need_update) {
39c75a51 1291 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
7351a482
SW
1292 "%s updating last_seqno: old %u, new %u\n",
1293 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1294 orig_ifinfo->last_real_seqno, seqno);
1295 orig_ifinfo->last_real_seqno = seqno;
fc957275
ML
1296 }
1297
fc957275 1298out:
bbad0a5e 1299 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
5d967310 1300 batadv_orig_node_put(orig_node);
35f94779 1301 batadv_orig_ifinfo_put(orig_ifinfo);
fc957275
ML
1302 return ret;
1303}
1304
7351a482 1305/**
7e9a8c2c
SE
1306 * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing
1307 * interface
7351a482 1308 * @skb: the skb containing the OGM
95298a91 1309 * @ogm_offset: offset from skb->data to start of ogm header
7351a482
SW
1310 * @orig_node: the (cached) orig node for the originator of this OGM
1311 * @if_incoming: the interface where this packet was received
1312 * @if_outgoing: the interface for which the packet should be considered
1313 */
1314static void
1315batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1316 struct batadv_orig_node *orig_node,
1317 struct batadv_hard_iface *if_incoming,
1318 struct batadv_hard_iface *if_outgoing)
fc957275 1319{
94433355 1320 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
4ff1e2a7 1321 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
4f248cff
SE
1322 struct batadv_neigh_node *router = NULL;
1323 struct batadv_neigh_node *router_router = NULL;
7351a482
SW
1324 struct batadv_orig_node *orig_neigh_node;
1325 struct batadv_orig_ifinfo *orig_ifinfo;
56303d34 1326 struct batadv_neigh_node *orig_neigh_router = NULL;
89652331 1327 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
7351a482 1328 struct batadv_ogm_packet *ogm_packet;
7c24bbbe 1329 enum batadv_dup_status dup_status;
7351a482
SW
1330 bool is_from_best_next_hop = false;
1331 bool is_single_hop_neigh = false;
1332 bool sameseq, similar_ttl;
1333 struct sk_buff *skb_priv;
1334 struct ethhdr *ethhdr;
6b5e971a 1335 u8 *prev_sender;
4b426b10 1336 bool is_bidirect;
fc957275 1337
7351a482
SW
1338 /* create a private copy of the skb, as some functions change tq value
1339 * and/or flags.
fc957275 1340 */
7351a482
SW
1341 skb_priv = skb_copy(skb, GFP_ATOMIC);
1342 if (!skb_priv)
fc957275
ML
1343 return;
1344
7351a482
SW
1345 ethhdr = eth_hdr(skb_priv);
1346 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
fc957275 1347
7351a482
SW
1348 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1349 if_incoming, if_outgoing);
1350 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
75cd33f8 1351 is_single_hop_neigh = true;
fc957275 1352
7c24bbbe 1353 if (dup_status == BATADV_PROTECTED) {
39c75a51 1354 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
1355 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1356 ethhdr->h_source);
fc957275
ML
1357 goto out;
1358 }
1359
7351a482 1360 if (ogm_packet->tq == 0) {
39c75a51 1361 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1362 "Drop packet: originator packet with tq equal 0\n");
fc957275
ML
1363 goto out;
1364 }
1365
4ff1e2a7
ML
1366 if (is_single_hop_neigh) {
1367 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1368 ethhdr->h_source);
1369 if (hardif_neigh)
1370 hardif_neigh->last_seen = jiffies;
1371 }
1372
7351a482 1373 router = batadv_orig_router_get(orig_node, if_outgoing);
0538f759 1374 if (router) {
7351a482
SW
1375 router_router = batadv_orig_router_get(router->orig_node,
1376 if_outgoing);
1377 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
0538f759 1378 }
fc957275 1379
7351a482 1380 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1eda58bf 1381 (batadv_compare_eth(router->addr, ethhdr->h_source)))
13b2541b
ML
1382 is_from_best_next_hop = true;
1383
7351a482 1384 prev_sender = ogm_packet->prev_sender;
fc957275
ML
1385 /* avoid temporary routing loops */
1386 if (router && router_router &&
1eda58bf 1387 (batadv_compare_eth(router->addr, prev_sender)) &&
7351a482 1388 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1eda58bf 1389 (batadv_compare_eth(router->addr, router_router->addr))) {
39c75a51 1390 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
1391 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1392 ethhdr->h_source);
fc957275
ML
1393 goto out;
1394 }
1395
7351a482
SW
1396 if (if_outgoing == BATADV_IF_DEFAULT)
1397 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
ef261577 1398
fc957275 1399 /* if sender is a direct neighbor the sender mac equals
9cfc7bd6
SE
1400 * originator mac
1401 */
c67893d1
SE
1402 if (is_single_hop_neigh)
1403 orig_neigh_node = orig_node;
1404 else
bbad0a5e
AQ
1405 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1406 ethhdr->h_source);
c67893d1 1407
fc957275
ML
1408 if (!orig_neigh_node)
1409 goto out;
1410
d56b1705
MH
1411 /* Update nc_nodes of the originator */
1412 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
7351a482 1413 ogm_packet, is_single_hop_neigh);
d56b1705 1414
7351a482
SW
1415 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1416 if_outgoing);
fc957275
ML
1417
1418 /* drop packet if sender is not a direct neighbor and if we
9cfc7bd6
SE
1419 * don't route towards it
1420 */
825ffe1f 1421 if (!is_single_hop_neigh && !orig_neigh_router) {
39c75a51 1422 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1423 "Drop packet: OGM via unknown neighbor!\n");
fc957275
ML
1424 goto out_neigh;
1425 }
1426
fe8bc396 1427 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
7351a482
SW
1428 ogm_packet, if_incoming,
1429 if_outgoing);
fc957275 1430
fc957275 1431 /* update ranking if it is not a duplicate or has the same
9cfc7bd6
SE
1432 * seqno and similar ttl as the non-duplicate
1433 */
7351a482
SW
1434 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1435 if (!orig_ifinfo)
1436 goto out_neigh;
1437
1438 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1439 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1440
825ffe1f 1441 if (is_bidirect && (dup_status == BATADV_NO_DUP ||
7351a482
SW
1442 (sameseq && similar_ttl))) {
1443 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1444 orig_ifinfo, ethhdr,
1445 ogm_packet, if_incoming,
1446 if_outgoing, dup_status);
1447 }
35f94779 1448 batadv_orig_ifinfo_put(orig_ifinfo);
fc957275 1449
ef0a937f
SW
1450 /* only forward for specific interface, not for the default one. */
1451 if (if_outgoing == BATADV_IF_DEFAULT)
1452 goto out_neigh;
1453
fc957275
ML
1454 /* is single hop (direct) neighbor */
1455 if (is_single_hop_neigh) {
7351a482
SW
1456 /* OGMs from secondary interfaces should only scheduled once
1457 * per interface where it has been received, not multiple times
1458 */
825ffe1f
SE
1459 if (ogm_packet->ttl <= 2 &&
1460 if_incoming != if_outgoing) {
7351a482
SW
1461 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1462 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1463 goto out_neigh;
1464 }
fc957275 1465 /* mark direct link on incoming interface */
7351a482 1466 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
fe8bc396 1467 is_single_hop_neigh,
ef0a937f
SW
1468 is_from_best_next_hop, if_incoming,
1469 if_outgoing);
fc957275 1470
39c75a51 1471 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1472 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
fc957275
ML
1473 goto out_neigh;
1474 }
1475
1476 /* multihop originator */
fe8bc396 1477 if (!is_bidirect) {
39c75a51 1478 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1479 "Drop packet: not received via bidirectional link\n");
fc957275
ML
1480 goto out_neigh;
1481 }
1482
7c24bbbe 1483 if (dup_status == BATADV_NEIGH_DUP) {
39c75a51 1484 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1485 "Drop packet: duplicate packet received\n");
fc957275
ML
1486 goto out_neigh;
1487 }
1488
39c75a51 1489 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1490 "Forwarding packet: rebroadcast originator packet\n");
7351a482 1491 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
fe8bc396 1492 is_single_hop_neigh, is_from_best_next_hop,
ef0a937f 1493 if_incoming, if_outgoing);
fc957275
ML
1494
1495out_neigh:
825ffe1f 1496 if (orig_neigh_node && !is_single_hop_neigh)
5d967310 1497 batadv_orig_node_put(orig_neigh_node);
fc957275 1498out:
79a0bffb
SE
1499 batadv_neigh_ifinfo_put(router_ifinfo);
1500 batadv_neigh_node_put(router);
1501 batadv_neigh_node_put(router_router);
1502 batadv_neigh_node_put(orig_neigh_router);
1503 batadv_hardif_neigh_put(hardif_neigh);
fc957275 1504
bd687fe4 1505 consume_skb(skb_priv);
7351a482
SW
1506}
1507
dee222c7
SE
1508/**
1509 * batadv_iv_ogm_process_reply() - Check OGM for direct reply and process it
1510 * @ogm_packet: rebroadcast OGM packet to process
1511 * @if_incoming: the interface where this packet was received
1512 * @orig_node: originator which reproadcasted the OGMs
1513 * @if_incoming_seqno: OGM sequence number when rebroadcast was received
1514 */
1515static void batadv_iv_ogm_process_reply(struct batadv_ogm_packet *ogm_packet,
1516 struct batadv_hard_iface *if_incoming,
1517 struct batadv_orig_node *orig_node,
1518 u32 if_incoming_seqno)
1519{
1520 struct batadv_orig_ifinfo *orig_ifinfo;
1521 s32 bit_pos;
1522 u8 *weight;
1523
1524 /* neighbor has to indicate direct link and it has to
1525 * come via the corresponding interface
1526 */
1527 if (!(ogm_packet->flags & BATADV_DIRECTLINK))
1528 return;
1529
1530 if (!batadv_compare_eth(if_incoming->net_dev->dev_addr,
1531 ogm_packet->orig))
1532 return;
1533
1534 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_incoming);
1535 if (!orig_ifinfo)
1536 return;
1537
1538 /* save packet seqno for bidirectional check */
1539 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1540 bit_pos = if_incoming_seqno - 2;
1541 bit_pos -= ntohl(ogm_packet->seqno);
1542 batadv_set_bit(orig_ifinfo->bat_iv.bcast_own, bit_pos);
1543 weight = &orig_ifinfo->bat_iv.bcast_own_sum;
1544 *weight = bitmap_weight(orig_ifinfo->bat_iv.bcast_own,
1545 BATADV_TQ_LOCAL_WINDOW_SIZE);
1546 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1547
1548 batadv_orig_ifinfo_put(orig_ifinfo);
1549}
1550
7351a482 1551/**
7e9a8c2c 1552 * batadv_iv_ogm_process() - process an incoming batman iv OGM
7351a482
SW
1553 * @skb: the skb containing the OGM
1554 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
bccb48c8 1555 * @if_incoming: the interface where this packet was received
7351a482
SW
1556 */
1557static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1558 struct batadv_hard_iface *if_incoming)
1559{
94433355 1560 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
7351a482
SW
1561 struct batadv_orig_node *orig_neigh_node, *orig_node;
1562 struct batadv_hard_iface *hard_iface;
1563 struct batadv_ogm_packet *ogm_packet;
6b5e971a 1564 u32 if_incoming_seqno;
7351a482
SW
1565 bool has_directlink_flag;
1566 struct ethhdr *ethhdr;
1567 bool is_my_oldorig = false;
1568 bool is_my_addr = false;
1569 bool is_my_orig = false;
1570
1571 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1572 ethhdr = eth_hdr(skb);
1573
1574 /* Silently drop when the batman packet is actually not a
1575 * correct packet.
1576 *
1577 * This might happen if a packet is padded (e.g. Ethernet has a
1578 * minimum frame length of 64 byte) and the aggregation interprets
1579 * it as an additional length.
1580 *
1581 * TODO: A more sane solution would be to have a bit in the
1582 * batadv_ogm_packet to detect whether the packet is the last
1583 * packet in an aggregation. Here we expect that the padding
1584 * is always zero (or not 0x01)
1585 */
1586 if (ogm_packet->packet_type != BATADV_IV_OGM)
1587 return;
1588
1589 /* could be changed by schedule_own_packet() */
1590 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1591
1592 if (ogm_packet->flags & BATADV_DIRECTLINK)
1593 has_directlink_flag = true;
1594 else
1595 has_directlink_flag = false;
1596
1597 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1598 "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",
1599 ethhdr->h_source, if_incoming->net_dev->name,
1600 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1601 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1602 ogm_packet->tq, ogm_packet->ttl,
1603 ogm_packet->version, has_directlink_flag);
1604
1605 rcu_read_lock();
1606 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1607 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1608 continue;
1609
94433355 1610 if (hard_iface->mesh_iface != if_incoming->mesh_iface)
7351a482
SW
1611 continue;
1612
1613 if (batadv_compare_eth(ethhdr->h_source,
1614 hard_iface->net_dev->dev_addr))
1615 is_my_addr = true;
1616
1617 if (batadv_compare_eth(ogm_packet->orig,
1618 hard_iface->net_dev->dev_addr))
1619 is_my_orig = true;
1620
1621 if (batadv_compare_eth(ogm_packet->prev_sender,
1622 hard_iface->net_dev->dev_addr))
1623 is_my_oldorig = true;
1624 }
1625 rcu_read_unlock();
1626
1627 if (is_my_addr) {
1628 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1629 "Drop packet: received my own broadcast (sender: %pM)\n",
1630 ethhdr->h_source);
1631 return;
1632 }
1633
1634 if (is_my_orig) {
7351a482
SW
1635 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1636 ethhdr->h_source);
1637 if (!orig_neigh_node)
1638 return;
1639
dee222c7
SE
1640 batadv_iv_ogm_process_reply(ogm_packet, if_incoming,
1641 orig_neigh_node, if_incoming_seqno);
7351a482
SW
1642
1643 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1644 "Drop packet: originator packet from myself (via neighbor)\n");
5d967310 1645 batadv_orig_node_put(orig_neigh_node);
7351a482
SW
1646 return;
1647 }
1648
1649 if (is_my_oldorig) {
1650 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1651 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1652 ethhdr->h_source);
1653 return;
1654 }
1655
1656 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1657 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1658 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1659 ethhdr->h_source);
1660 return;
1661 }
1662
1663 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1664 if (!orig_node)
1665 return;
1666
1667 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1668 if_incoming, BATADV_IF_DEFAULT);
1669
1670 rcu_read_lock();
1671 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1672 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1673 continue;
1674
94433355 1675 if (hard_iface->mesh_iface != bat_priv->mesh_iface)
7351a482
SW
1676 continue;
1677
27353446
SE
1678 if (!kref_get_unless_zero(&hard_iface->refcount))
1679 continue;
1680
7351a482
SW
1681 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1682 if_incoming, hard_iface);
27353446
SE
1683
1684 batadv_hardif_put(hard_iface);
7351a482
SW
1685 }
1686 rcu_read_unlock();
1687
5d967310 1688 batadv_orig_node_put(orig_node);
fc957275
ML
1689}
1690
f0d97253
AQ
1691static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1692{
1693 struct delayed_work *delayed_work;
1694 struct batadv_forw_packet *forw_packet;
1695 struct batadv_priv *bat_priv;
bd687fe4 1696 bool dropped = false;
f0d97253
AQ
1697
1698 delayed_work = to_delayed_work(work);
1699 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1700 delayed_work);
94433355 1701 bat_priv = netdev_priv(forw_packet->if_incoming->mesh_iface);
f0d97253 1702
bd687fe4
SE
1703 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1704 dropped = true;
f0d97253 1705 goto out;
bd687fe4 1706 }
f0d97253
AQ
1707
1708 batadv_iv_ogm_emit(forw_packet);
1709
1710 /* we have to have at least one packet in the queue to determine the
1711 * queues wake up time unless we are shutting down.
1712 *
1713 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1714 * primary interface should only be rescheduled once per period, but
1715 * this function will be called for the forw_packet instances of the
1716 * other secondary interfaces as well.
1717 */
1718 if (forw_packet->own &&
1719 forw_packet->if_incoming == forw_packet->if_outgoing)
1720 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1721
1722out:
9b4aec64
LL
1723 /* do we get something for free()? */
1724 if (batadv_forw_packet_steal(forw_packet,
1725 &bat_priv->forw_bat_list_lock))
1726 batadv_forw_packet_free(forw_packet, dropped);
f0d97253
AQ
1727}
1728
fe8bc396 1729static int batadv_iv_ogm_receive(struct sk_buff *skb,
56303d34 1730 struct batadv_hard_iface *if_incoming)
fc957275 1731{
94433355 1732 struct batadv_priv *bat_priv = netdev_priv(if_incoming->mesh_iface);
7351a482 1733 struct batadv_ogm_packet *ogm_packet;
6b5e971a 1734 u8 *packet_pos;
7351a482 1735 int ogm_offset;
b91a2543
SE
1736 bool res;
1737 int ret = NET_RX_DROP;
c3e29312 1738
b91a2543
SE
1739 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1740 if (!res)
1741 goto free_skb;
fc957275 1742
edbf12ba
ML
1743 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1744 * that does not have B.A.T.M.A.N. IV enabled ?
1745 */
29824a55 1746 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
b91a2543 1747 goto free_skb;
edbf12ba 1748
d69909d2
SE
1749 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1750 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
f8214865
MH
1751 skb->len + ETH_HLEN);
1752
7351a482
SW
1753 ogm_offset = 0;
1754 ogm_packet = (struct batadv_ogm_packet *)skb->data;
fc957275
ML
1755
1756 /* unpack the aggregated packets and process them one by one */
7351a482 1757 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
a15d56a6 1758 ogm_packet)) {
7351a482 1759 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
fc957275 1760
7351a482
SW
1761 ogm_offset += BATADV_OGM_HLEN;
1762 ogm_offset += ntohs(ogm_packet->tvlv_len);
fc957275 1763
7351a482
SW
1764 packet_pos = skb->data + ogm_offset;
1765 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
b47506d9 1766 }
c3e29312 1767
b91a2543
SE
1768 ret = NET_RX_SUCCESS;
1769
1770free_skb:
1771 if (ret == NET_RX_SUCCESS)
1772 consume_skb(skb);
1773 else
1774 kfree_skb(skb);
1775
1776 return ret;
fc957275 1777}
1c280471 1778
024f99cb 1779/**
7e9a8c2c 1780 * batadv_iv_ogm_neigh_get_tq_avg() - Get the TQ average for a neighbour on a
024f99cb
MS
1781 * given outgoing interface.
1782 * @neigh_node: Neighbour of interest
1783 * @if_outgoing: Outgoing interface of interest
1784 * @tq_avg: Pointer of where to store the TQ average
1785 *
1786 * Return: False if no average TQ available, otherwise true.
1787 */
1788static bool
1789batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1790 struct batadv_hard_iface *if_outgoing,
1791 u8 *tq_avg)
1792{
1793 struct batadv_neigh_ifinfo *n_ifinfo;
1794
1795 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1796 if (!n_ifinfo)
1797 return false;
1798
1799 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1800 batadv_neigh_ifinfo_put(n_ifinfo);
1801
1802 return true;
1803}
1804
1805/**
7e9a8c2c 1806 * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a
024f99cb
MS
1807 * message
1808 * @msg: Netlink message to dump into
1809 * @portid: Port making netlink request
1810 * @seq: Sequence number of netlink message
94433355 1811 * @bat_priv: The bat priv with all the mesh interface information
024f99cb
MS
1812 * @if_outgoing: Limit dump to entries with this outgoing interface
1813 * @orig_node: Originator to dump
1814 * @neigh_node: Single hops neighbour
1815 * @best: Is the best originator
1816 *
1817 * Return: Error code, or 0 on success
1818 */
1819static int
1820batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1821 struct batadv_priv *bat_priv,
1822 struct batadv_hard_iface *if_outgoing,
1823 struct batadv_orig_node *orig_node,
1824 struct batadv_neigh_node *neigh_node,
1825 bool best)
1826{
1827 void *hdr;
1828 u8 tq_avg;
1829 unsigned int last_seen_msecs;
1830
1831 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
1832
1833 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
1834 return 0;
1835
1836 if (if_outgoing != BATADV_IF_DEFAULT &&
1837 if_outgoing != neigh_node->if_incoming)
1838 return 0;
1839
1840 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1841 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
1842 if (!hdr)
1843 return -ENOBUFS;
1844
1845 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1846 orig_node->orig) ||
1847 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
1848 neigh_node->addr) ||
d295345a
SE
1849 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
1850 neigh_node->if_incoming->net_dev->name) ||
024f99cb
MS
1851 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
1852 neigh_node->if_incoming->net_dev->ifindex) ||
1853 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
1854 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
1855 last_seen_msecs))
1856 goto nla_put_failure;
1857
1858 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1859 goto nla_put_failure;
1860
1861 genlmsg_end(msg, hdr);
1862 return 0;
1863
1864 nla_put_failure:
1865 genlmsg_cancel(msg, hdr);
1866 return -EMSGSIZE;
1867}
1868
1869/**
7e9a8c2c 1870 * batadv_iv_ogm_orig_dump_entry() - Dump an originator entry into a message
024f99cb
MS
1871 * @msg: Netlink message to dump into
1872 * @portid: Port making netlink request
1873 * @seq: Sequence number of netlink message
94433355 1874 * @bat_priv: The bat priv with all the mesh interface information
024f99cb
MS
1875 * @if_outgoing: Limit dump to entries with this outgoing interface
1876 * @orig_node: Originator to dump
1877 * @sub_s: Number of sub entries to skip
1878 *
1879 * This function assumes the caller holds rcu_read_lock().
1880 *
1881 * Return: Error code, or 0 on success
1882 */
1883static int
1884batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1885 struct batadv_priv *bat_priv,
1886 struct batadv_hard_iface *if_outgoing,
1887 struct batadv_orig_node *orig_node, int *sub_s)
1888{
1889 struct batadv_neigh_node *neigh_node_best;
1890 struct batadv_neigh_node *neigh_node;
1891 int sub = 0;
1892 bool best;
1893 u8 tq_avg_best;
1894
1895 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
1896 if (!neigh_node_best)
1897 goto out;
1898
1899 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
1900 &tq_avg_best))
1901 goto out;
1902
1903 if (tq_avg_best == 0)
1904 goto out;
1905
1906 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1907 if (sub++ < *sub_s)
1908 continue;
1909
1910 best = (neigh_node == neigh_node_best);
1911
1912 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
1913 bat_priv, if_outgoing,
1914 orig_node, neigh_node,
1915 best)) {
1916 batadv_neigh_node_put(neigh_node_best);
1917
1918 *sub_s = sub - 1;
1919 return -EMSGSIZE;
1920 }
1921 }
1922
1923 out:
79a0bffb 1924 batadv_neigh_node_put(neigh_node_best);
024f99cb
MS
1925
1926 *sub_s = 0;
1927 return 0;
1928}
1929
1930/**
7e9a8c2c 1931 * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a
024f99cb
MS
1932 * message
1933 * @msg: Netlink message to dump into
1934 * @portid: Port making netlink request
1935 * @seq: Sequence number of netlink message
94433355 1936 * @bat_priv: The bat priv with all the mesh interface information
024f99cb
MS
1937 * @if_outgoing: Limit dump to entries with this outgoing interface
1938 * @head: Bucket to be dumped
1939 * @idx_s: Number of entries to be skipped
1940 * @sub: Number of sub entries to be skipped
1941 *
1942 * Return: Error code, or 0 on success
1943 */
1944static int
1945batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1946 struct batadv_priv *bat_priv,
1947 struct batadv_hard_iface *if_outgoing,
1948 struct hlist_head *head, int *idx_s, int *sub)
1949{
1950 struct batadv_orig_node *orig_node;
1951 int idx = 0;
1952
1953 rcu_read_lock();
1954 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1955 if (idx++ < *idx_s)
1956 continue;
1957
1958 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
1959 if_outgoing, orig_node,
1960 sub)) {
1961 rcu_read_unlock();
1962 *idx_s = idx - 1;
1963 return -EMSGSIZE;
1964 }
1965 }
1966 rcu_read_unlock();
1967
1968 *idx_s = 0;
1969 *sub = 0;
1970 return 0;
1971}
1972
1973/**
7e9a8c2c 1974 * batadv_iv_ogm_orig_dump() - Dump the originators into a message
024f99cb
MS
1975 * @msg: Netlink message to dump into
1976 * @cb: Control block containing additional options
94433355 1977 * @bat_priv: The bat priv with all the mesh interface information
024f99cb
MS
1978 * @if_outgoing: Limit dump to entries with this outgoing interface
1979 */
1980static void
1981batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
1982 struct batadv_priv *bat_priv,
1983 struct batadv_hard_iface *if_outgoing)
1984{
1985 struct batadv_hashtable *hash = bat_priv->orig_hash;
1986 struct hlist_head *head;
1987 int bucket = cb->args[0];
1988 int idx = cb->args[1];
1989 int sub = cb->args[2];
1990 int portid = NETLINK_CB(cb->skb).portid;
1991
1992 while (bucket < hash->size) {
1993 head = &hash->table[bucket];
1994
1995 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
1996 cb->nlh->nlmsg_seq,
1997 bat_priv, if_outgoing, head,
1998 &idx, &sub))
1999 break;
2000
2001 bucket++;
2002 }
2003
2004 cb->args[0] = bucket;
2005 cb->args[1] = idx;
2006 cb->args[2] = sub;
2007}
2008
a3285a8f 2009/**
7e9a8c2c 2010 * batadv_iv_ogm_neigh_diff() - calculate tq difference of two neighbors
a3285a8f 2011 * @neigh1: the first neighbor object of the comparison
89652331 2012 * @if_outgoing1: outgoing interface for the first neighbor
a3285a8f 2013 * @neigh2: the second neighbor object of the comparison
89652331 2014 * @if_outgoing2: outgoing interface for the second neighbor
57b12502 2015 * @diff: pointer to integer receiving the calculated difference
a3285a8f 2016 *
57b12502
MP
2017 * The content of *@diff is only valid when this function returns true.
2018 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2019 * the same as or higher than the metric via neigh2
2020 *
2021 * Return: true when the difference could be calculated, false otherwise
a3285a8f 2022 */
57b12502
MP
2023static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2024 struct batadv_hard_iface *if_outgoing1,
2025 struct batadv_neigh_node *neigh2,
2026 struct batadv_hard_iface *if_outgoing2,
2027 int *diff)
a3285a8f 2028{
89652331 2029 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
6b5e971a 2030 u8 tq1, tq2;
57b12502 2031 bool ret = true;
89652331
SW
2032
2033 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2034 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
a3285a8f 2035
89652331 2036 if (!neigh1_ifinfo || !neigh2_ifinfo) {
57b12502 2037 ret = false;
89652331
SW
2038 goto out;
2039 }
a3285a8f 2040
89652331
SW
2041 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2042 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
57b12502 2043 *diff = (int)tq1 - (int)tq2;
89652331
SW
2044
2045out:
79a0bffb
SE
2046 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2047 batadv_neigh_ifinfo_put(neigh2_ifinfo);
89652331 2048
57b12502
MP
2049 return ret;
2050}
2051
024f99cb 2052/**
7e9a8c2c 2053 * batadv_iv_ogm_neigh_dump_neigh() - Dump a neighbour into a netlink message
024f99cb
MS
2054 * @msg: Netlink message to dump into
2055 * @portid: Port making netlink request
2056 * @seq: Sequence number of netlink message
2057 * @hardif_neigh: Neighbour to be dumped
2058 *
2059 * Return: Error code, or 0 on success
2060 */
2061static int
2062batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2063 struct batadv_hardif_neigh_node *hardif_neigh)
2064{
2065 void *hdr;
2066 unsigned int last_seen_msecs;
2067
2068 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2069
2070 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2071 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2072 if (!hdr)
2073 return -ENOBUFS;
2074
2075 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2076 hardif_neigh->addr) ||
d295345a
SE
2077 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2078 hardif_neigh->if_incoming->net_dev->name) ||
024f99cb
MS
2079 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2080 hardif_neigh->if_incoming->net_dev->ifindex) ||
2081 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2082 last_seen_msecs))
2083 goto nla_put_failure;
2084
2085 genlmsg_end(msg, hdr);
2086 return 0;
2087
2088 nla_put_failure:
2089 genlmsg_cancel(msg, hdr);
2090 return -EMSGSIZE;
2091}
2092
2093/**
7e9a8c2c 2094 * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface
024f99cb
MS
2095 * into a message
2096 * @msg: Netlink message to dump into
2097 * @portid: Port making netlink request
2098 * @seq: Sequence number of netlink message
94433355 2099 * @bat_priv: The bat priv with all the mesh interface information
024f99cb
MS
2100 * @hard_iface: Hard interface to dump the neighbours for
2101 * @idx_s: Number of entries to skip
2102 *
2103 * This function assumes the caller holds rcu_read_lock().
2104 *
2105 * Return: Error code, or 0 on success
2106 */
2107static int
2108batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2109 struct batadv_priv *bat_priv,
2110 struct batadv_hard_iface *hard_iface,
2111 int *idx_s)
2112{
2113 struct batadv_hardif_neigh_node *hardif_neigh;
2114 int idx = 0;
2115
2116 hlist_for_each_entry_rcu(hardif_neigh,
2117 &hard_iface->neigh_list, list) {
2118 if (idx++ < *idx_s)
2119 continue;
2120
2121 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2122 hardif_neigh)) {
2123 *idx_s = idx - 1;
2124 return -EMSGSIZE;
2125 }
2126 }
2127
2128 *idx_s = 0;
2129 return 0;
2130}
2131
2132/**
7e9a8c2c 2133 * batadv_iv_ogm_neigh_dump() - Dump the neighbours into a message
024f99cb
MS
2134 * @msg: Netlink message to dump into
2135 * @cb: Control block containing additional options
94433355 2136 * @bat_priv: The bat priv with all the mesh interface information
bccb48c8 2137 * @single_hardif: Limit dump to this hard interface
024f99cb
MS
2138 */
2139static void
2140batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2141 struct batadv_priv *bat_priv,
2142 struct batadv_hard_iface *single_hardif)
2143{
2144 struct batadv_hard_iface *hard_iface;
2145 int i_hardif = 0;
2146 int i_hardif_s = cb->args[0];
2147 int idx = cb->args[1];
2148 int portid = NETLINK_CB(cb->skb).portid;
2149
2150 rcu_read_lock();
2151 if (single_hardif) {
2152 if (i_hardif_s == 0) {
2153 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2154 cb->nlh->nlmsg_seq,
2155 bat_priv,
2156 single_hardif,
2157 &idx) == 0)
2158 i_hardif++;
2159 }
2160 } else {
2161 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2162 list) {
94433355 2163 if (hard_iface->mesh_iface != bat_priv->mesh_iface)
024f99cb
MS
2164 continue;
2165
2166 if (i_hardif++ < i_hardif_s)
2167 continue;
2168
2169 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2170 cb->nlh->nlmsg_seq,
2171 bat_priv,
2172 hard_iface, &idx)) {
2173 i_hardif--;
2174 break;
2175 }
2176 }
2177 }
2178 rcu_read_unlock();
2179
2180 cb->args[0] = i_hardif;
2181 cb->args[1] = idx;
2182}
2183
57b12502 2184/**
7e9a8c2c 2185 * batadv_iv_ogm_neigh_cmp() - compare the metrics of two neighbors
57b12502
MP
2186 * @neigh1: the first neighbor object of the comparison
2187 * @if_outgoing1: outgoing interface for the first neighbor
2188 * @neigh2: the second neighbor object of the comparison
2189 * @if_outgoing2: outgoing interface for the second neighbor
2190 *
2191 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
2192 * lower, the same as or higher than the metric via neigh2
2193 */
2194static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
2195 struct batadv_hard_iface *if_outgoing1,
2196 struct batadv_neigh_node *neigh2,
2197 struct batadv_hard_iface *if_outgoing2)
2198{
2199 bool ret;
2200 int diff;
2201
2202 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2203 if_outgoing2, &diff);
2204 if (!ret)
2205 return 0;
2206
89652331 2207 return diff;
a3285a8f
AQ
2208}
2209
c43c981e 2210/**
7e9a8c2c 2211 * batadv_iv_ogm_neigh_is_sob() - check if neigh1 is similarly good or better
18165f6f 2212 * than neigh2 from the metric prospective
c43c981e 2213 * @neigh1: the first neighbor object of the comparison
95298a91 2214 * @if_outgoing1: outgoing interface for the first neighbor
c43c981e 2215 * @neigh2: the second neighbor object of the comparison
89652331 2216 * @if_outgoing2: outgoing interface for the second neighbor
95298a91 2217 *
62fe710f 2218 * Return: true if the metric via neigh1 is equally good or better than
89652331 2219 * the metric via neigh2, false otherwise.
c43c981e 2220 */
89652331 2221static bool
18165f6f 2222batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
89652331
SW
2223 struct batadv_hard_iface *if_outgoing1,
2224 struct batadv_neigh_node *neigh2,
2225 struct batadv_hard_iface *if_outgoing2)
c43c981e 2226{
89652331 2227 bool ret;
57b12502 2228 int diff;
c43c981e 2229
57b12502
MP
2230 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2231 if_outgoing2, &diff);
2232 if (!ret)
2233 return false;
89652331 2234
57b12502 2235 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
89652331 2236 return ret;
c43c981e
AQ
2237}
2238
9e6b5648 2239static void batadv_iv_iface_enabled(struct batadv_hard_iface *hard_iface)
f0d97253
AQ
2240{
2241 /* begin scheduling originator messages on that interface */
2242 batadv_iv_ogm_schedule(hard_iface);
2243}
2244
1a9070ec 2245/**
7e9a8c2c 2246 * batadv_iv_init_sel_class() - initialize GW selection class
94433355 2247 * @bat_priv: the bat priv with all the mesh interface information
1a9070ec
SE
2248 */
2249static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2250{
2251 /* set default TQ difference threshold to 20 */
2252 atomic_set(&bat_priv->gw.sel_class, 20);
2253}
2254
34d99cfe
AQ
2255static struct batadv_gw_node *
2256batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2257{
2258 struct batadv_neigh_node *router;
2259 struct batadv_neigh_ifinfo *router_ifinfo;
2260 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2261 u64 max_gw_factor = 0;
2262 u64 tmp_gw_factor = 0;
2263 u8 max_tq = 0;
2264 u8 tq_avg;
2265 struct batadv_orig_node *orig_node;
2266
2267 rcu_read_lock();
70ea5cee 2268 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
34d99cfe
AQ
2269 orig_node = gw_node->orig_node;
2270 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2271 if (!router)
2272 continue;
2273
2274 router_ifinfo = batadv_neigh_ifinfo_get(router,
2275 BATADV_IF_DEFAULT);
2276 if (!router_ifinfo)
2277 goto next;
2278
2279 if (!kref_get_unless_zero(&gw_node->refcount))
2280 goto next;
2281
2282 tq_avg = router_ifinfo->bat_iv.tq_avg;
2283
2284 switch (atomic_read(&bat_priv->gw.sel_class)) {
2285 case 1: /* fast connection */
2286 tmp_gw_factor = tq_avg * tq_avg;
2287 tmp_gw_factor *= gw_node->bandwidth_down;
2288 tmp_gw_factor *= 100 * 100;
2289 tmp_gw_factor >>= 18;
2290
825ffe1f
SE
2291 if (tmp_gw_factor > max_gw_factor ||
2292 (tmp_gw_factor == max_gw_factor &&
2293 tq_avg > max_tq)) {
79a0bffb 2294 batadv_gw_node_put(curr_gw);
34d99cfe
AQ
2295 curr_gw = gw_node;
2296 kref_get(&curr_gw->refcount);
2297 }
2298 break;
2299
2300 default: /* 2: stable connection (use best statistic)
2301 * 3: fast-switch (use best statistic but change as
2302 * soon as a better gateway appears)
2303 * XX: late-switch (use best statistic but change as
2304 * soon as a better gateway appears which has
2305 * $routing_class more tq points)
2306 */
2307 if (tq_avg > max_tq) {
79a0bffb 2308 batadv_gw_node_put(curr_gw);
34d99cfe
AQ
2309 curr_gw = gw_node;
2310 kref_get(&curr_gw->refcount);
2311 }
2312 break;
2313 }
2314
2315 if (tq_avg > max_tq)
2316 max_tq = tq_avg;
2317
2318 if (tmp_gw_factor > max_gw_factor)
2319 max_gw_factor = tmp_gw_factor;
2320
2321 batadv_gw_node_put(gw_node);
2322
2323next:
2324 batadv_neigh_node_put(router);
79a0bffb 2325 batadv_neigh_ifinfo_put(router_ifinfo);
34d99cfe
AQ
2326 }
2327 rcu_read_unlock();
2328
2329 return curr_gw;
2330}
2331
2332static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2333 struct batadv_orig_node *curr_gw_orig,
2334 struct batadv_orig_node *orig_node)
2335{
2336 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2337 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2338 struct batadv_neigh_node *router_gw = NULL;
2339 struct batadv_neigh_node *router_orig = NULL;
2340 u8 gw_tq_avg, orig_tq_avg;
2341 bool ret = false;
2342
2343 /* dynamic re-election is performed only on fast or late switch */
2344 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2345 return false;
2346
2347 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2348 if (!router_gw) {
2349 ret = true;
2350 goto out;
2351 }
2352
2353 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2354 BATADV_IF_DEFAULT);
2355 if (!router_gw_ifinfo) {
2356 ret = true;
2357 goto out;
2358 }
2359
2360 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2361 if (!router_orig)
2362 goto out;
2363
2364 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2365 BATADV_IF_DEFAULT);
2366 if (!router_orig_ifinfo)
2367 goto out;
2368
2369 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2370 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2371
2372 /* the TQ value has to be better */
2373 if (orig_tq_avg < gw_tq_avg)
2374 goto out;
2375
2376 /* if the routing class is greater than 3 the value tells us how much
2377 * greater the TQ value of the new gateway must be
2378 */
2379 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2380 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2381 goto out;
2382
2383 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2384 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2385 gw_tq_avg, orig_tq_avg);
2386
2387 ret = true;
2388out:
79a0bffb
SE
2389 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2390 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2391 batadv_neigh_node_put(router_gw);
2392 batadv_neigh_node_put(router_orig);
34d99cfe
AQ
2393
2394 return ret;
2395}
2396
efb766af 2397/**
7e9a8c2c 2398 * batadv_iv_gw_dump_entry() - Dump a gateway into a message
efb766af
AL
2399 * @msg: Netlink message to dump into
2400 * @portid: Port making netlink request
9264c85c 2401 * @cb: Control block containing additional options
94433355 2402 * @bat_priv: The bat priv with all the mesh interface information
efb766af
AL
2403 * @gw_node: Gateway to be dumped
2404 *
2405 * Return: Error code, or 0 on success
2406 */
9264c85c
SE
2407static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid,
2408 struct netlink_callback *cb,
efb766af
AL
2409 struct batadv_priv *bat_priv,
2410 struct batadv_gw_node *gw_node)
2411{
2412 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2413 struct batadv_neigh_node *router;
b5685d26 2414 struct batadv_gw_node *curr_gw = NULL;
10d57028 2415 int ret = 0;
efb766af
AL
2416 void *hdr;
2417
2418 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2419 if (!router)
2420 goto out;
2421
2422 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2423 if (!router_ifinfo)
2424 goto out;
2425
2426 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2427
9264c85c
SE
2428 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2429 &batadv_netlink_family, NLM_F_MULTI,
2430 BATADV_CMD_GET_GATEWAYS);
efb766af
AL
2431 if (!hdr) {
2432 ret = -ENOBUFS;
2433 goto out;
2434 }
2435
9264c85c
SE
2436 genl_dump_check_consistent(cb, hdr);
2437
efb766af
AL
2438 ret = -EMSGSIZE;
2439
2440 if (curr_gw == gw_node)
2441 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2442 genlmsg_cancel(msg, hdr);
2443 goto out;
2444 }
2445
2446 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2447 gw_node->orig_node->orig) ||
2448 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2449 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2450 router->addr) ||
2451 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2452 router->if_incoming->net_dev->name) ||
d295345a
SE
2453 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2454 router->if_incoming->net_dev->ifindex) ||
efb766af
AL
2455 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2456 gw_node->bandwidth_down) ||
2457 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2458 gw_node->bandwidth_up)) {
2459 genlmsg_cancel(msg, hdr);
2460 goto out;
2461 }
2462
2463 genlmsg_end(msg, hdr);
2464 ret = 0;
2465
2466out:
79a0bffb
SE
2467 batadv_gw_node_put(curr_gw);
2468 batadv_neigh_ifinfo_put(router_ifinfo);
2469 batadv_neigh_node_put(router);
efb766af
AL
2470 return ret;
2471}
2472
2473/**
7e9a8c2c 2474 * batadv_iv_gw_dump() - Dump gateways into a message
efb766af
AL
2475 * @msg: Netlink message to dump into
2476 * @cb: Control block containing additional options
94433355 2477 * @bat_priv: The bat priv with all the mesh interface information
efb766af
AL
2478 */
2479static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2480 struct batadv_priv *bat_priv)
2481{
2482 int portid = NETLINK_CB(cb->skb).portid;
2483 struct batadv_gw_node *gw_node;
2484 int idx_skip = cb->args[0];
2485 int idx = 0;
2486
9264c85c
SE
2487 spin_lock_bh(&bat_priv->gw.list_lock);
2488 cb->seq = bat_priv->gw.generation << 1 | 1;
2489
2490 hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
efb766af
AL
2491 if (idx++ < idx_skip)
2492 continue;
2493
9264c85c
SE
2494 if (batadv_iv_gw_dump_entry(msg, portid, cb, bat_priv,
2495 gw_node)) {
efb766af
AL
2496 idx_skip = idx - 1;
2497 goto unlock;
2498 }
2499 }
2500
2501 idx_skip = idx;
2502unlock:
9264c85c 2503 spin_unlock_bh(&bat_priv->gw.list_lock);
efb766af
AL
2504
2505 cb->args[0] = idx_skip;
2506}
2507
56303d34 2508static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
519d3497 2509 .name = "BATMAN_IV",
29824a55 2510 .iface = {
29824a55 2511 .enable = batadv_iv_ogm_iface_enable,
9e6b5648 2512 .enabled = batadv_iv_iface_enabled,
29824a55
AQ
2513 .disable = batadv_iv_ogm_iface_disable,
2514 .update_mac = batadv_iv_ogm_iface_update_mac,
2515 .primary_set = batadv_iv_ogm_primary_iface_set,
2516 },
2517 .neigh = {
2518 .cmp = batadv_iv_ogm_neigh_cmp,
2519 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
024f99cb 2520 .dump = batadv_iv_ogm_neigh_dump,
29824a55
AQ
2521 },
2522 .orig = {
024f99cb 2523 .dump = batadv_iv_ogm_orig_dump,
29824a55 2524 },
34d99cfe 2525 .gw = {
1a9070ec 2526 .init_sel_class = batadv_iv_init_sel_class,
6f96d46f 2527 .sel_class_max = BATADV_TQ_MAX_VALUE,
34d99cfe
AQ
2528 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2529 .is_eligible = batadv_iv_gw_is_eligible,
efb766af 2530 .dump = batadv_iv_gw_dump,
34d99cfe 2531 },
1c280471
ML
2532};
2533
ff15c27c
SE
2534/**
2535 * batadv_iv_init() - B.A.T.M.A.N. IV initialization function
2536 *
2537 * Return: 0 on success or negative error number in case of failure
2538 */
81c524f7 2539int __init batadv_iv_init(void)
1c280471 2540{
c3e29312
ML
2541 int ret;
2542
2543 /* batman originator packet */
acd34afa
SE
2544 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2545 batadv_iv_ogm_receive);
c3e29312
ML
2546 if (ret < 0)
2547 goto out;
2548
fe8bc396 2549 ret = batadv_algo_register(&batadv_batman_iv);
c3e29312
ML
2550 if (ret < 0)
2551 goto handler_unregister;
2552
2553 goto out;
2554
2555handler_unregister:
acd34afa 2556 batadv_recv_handler_unregister(BATADV_IV_OGM);
c3e29312
ML
2557out:
2558 return ret;
1c280471 2559}