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