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