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