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