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