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