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