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