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