macvlan: resolve ENOENT errors on creation
[linux-block.git] / net / batman-adv / bat_iv_ogm.c
CommitLineData
0b873931 1/* Copyright (C) 2007-2013 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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
fc957275
ML
18 */
19
20#include "main.h"
fc957275 21#include "translation-table.h"
fc957275
ML
22#include "originator.h"
23#include "routing.h"
24#include "gateway_common.h"
25#include "gateway_client.h"
26#include "hard-interface.h"
27#include "send.h"
1c280471 28#include "bat_algo.h"
d56b1705 29#include "network-coding.h"
fc957275 30
791c2a2d
AQ
31
32/**
33 * batadv_dup_status - duplicate status
34 * @BATADV_NO_DUP: the packet is a duplicate
35 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
36 * neighbor)
37 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
38 * @BATADV_PROTECTED: originator is currently protected (after reboot)
39 */
40enum batadv_dup_status {
41 BATADV_NO_DUP = 0,
42 BATADV_ORIG_DUP,
43 BATADV_NEIGH_DUP,
44 BATADV_PROTECTED,
45};
46
24a5deeb
AQ
47/**
48 * batadv_ring_buffer_set - update the ring buffer with the given value
49 * @lq_recv: pointer to the ring buffer
50 * @lq_index: index to store the value at
51 * @value: value to store in the ring buffer
52 */
53static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
54 uint8_t value)
55{
56 lq_recv[*lq_index] = value;
57 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
58}
59
60/**
61 * batadv_ring_buffer_set - compute the average of all non-zero values stored
62 * in the given ring buffer
63 * @lq_recv: pointer to the ring buffer
64 *
65 * Returns computed average value.
66 */
67static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
68{
69 const uint8_t *ptr;
70 uint16_t count = 0, i = 0, sum = 0;
71
72 ptr = lq_recv;
73
74 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
75 if (*ptr != 0) {
76 count++;
77 sum += *ptr;
78 }
79
80 i++;
81 ptr++;
82 }
83
84 if (count == 0)
85 return 0;
86
87 return (uint8_t)(sum / count);
88}
d98cae64 89
56303d34
SE
90static struct batadv_neigh_node *
91batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
92 const uint8_t *neigh_addr,
93 struct batadv_orig_node *orig_node,
863dd7a8 94 struct batadv_orig_node *orig_neigh)
7ae8b285 95{
56303d34 96 struct batadv_neigh_node *neigh_node;
7ae8b285 97
863dd7a8 98 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr);
7ae8b285
ML
99 if (!neigh_node)
100 goto out;
101
102 INIT_LIST_HEAD(&neigh_node->bonding_list);
7ae8b285
ML
103
104 neigh_node->orig_node = orig_neigh;
105 neigh_node->if_incoming = hard_iface;
106
107 spin_lock_bh(&orig_node->neigh_list_lock);
108 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
109 spin_unlock_bh(&orig_node->neigh_list_lock);
110
111out:
112 return neigh_node;
113}
114
56303d34 115static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
d0b9fd89 116{
96412690 117 struct batadv_ogm_packet *batadv_ogm_packet;
14511519 118 unsigned char *ogm_buff;
d7d32ec0 119 uint32_t random_seqno;
5346c35e 120 int res = -ENOMEM;
d7d32ec0
ML
121
122 /* randomize initial seqno to avoid collision */
123 get_random_bytes(&random_seqno, sizeof(random_seqno));
14511519 124 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
d0b9fd89 125
14511519
ML
126 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
127 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
128 if (!ogm_buff)
77af7575
ML
129 goto out;
130
14511519
ML
131 hard_iface->bat_iv.ogm_buff = ogm_buff;
132
133 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
96412690
SE
134 batadv_ogm_packet->header.packet_type = BATADV_IV_OGM;
135 batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION;
136 batadv_ogm_packet->header.ttl = 2;
137 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
414254e3 138 batadv_ogm_packet->reserved = 0;
96412690 139 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
77af7575
ML
140
141 res = 0;
142
143out:
144 return res;
d0b9fd89
ML
145}
146
56303d34 147static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
00a50076 148{
14511519
ML
149 kfree(hard_iface->bat_iv.ogm_buff);
150 hard_iface->bat_iv.ogm_buff = NULL;
00a50076
ML
151}
152
56303d34 153static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
d0b9fd89 154{
96412690 155 struct batadv_ogm_packet *batadv_ogm_packet;
14511519 156 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
d0b9fd89 157
14511519 158 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
96412690 159 memcpy(batadv_ogm_packet->orig,
c3229398 160 hard_iface->net_dev->dev_addr, ETH_ALEN);
96412690 161 memcpy(batadv_ogm_packet->prev_sender,
c3229398 162 hard_iface->net_dev->dev_addr, ETH_ALEN);
d0b9fd89
ML
163}
164
56303d34
SE
165static void
166batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
d0b9fd89 167{
96412690 168 struct batadv_ogm_packet *batadv_ogm_packet;
14511519 169 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
d0b9fd89 170
14511519 171 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
96412690
SE
172 batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
173 batadv_ogm_packet->header.ttl = BATADV_TTL;
d0b9fd89
ML
174}
175
b9dacc52 176/* when do we schedule our own ogm to be sent */
fe8bc396 177static unsigned long
56303d34 178batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
b9dacc52 179{
42d0b044
SE
180 unsigned int msecs;
181
182 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
e76e4320 183 msecs += prandom_u32() % (2 * BATADV_JITTER);
42d0b044
SE
184
185 return jiffies + msecs_to_jiffies(msecs);
b9dacc52
ML
186}
187
188/* when do we schedule a ogm packet to be sent */
fe8bc396 189static unsigned long batadv_iv_ogm_fwd_send_time(void)
b9dacc52 190{
e76e4320 191 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
b9dacc52
ML
192}
193
194/* apply hop penalty for a normal link */
56303d34
SE
195static uint8_t batadv_hop_penalty(uint8_t tq,
196 const struct batadv_priv *bat_priv)
b9dacc52
ML
197{
198 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
42d0b044
SE
199 int new_tq;
200
201 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
202 new_tq /= BATADV_TQ_MAX_VALUE;
203
204 return new_tq;
b9dacc52
ML
205}
206
fc957275 207/* is there another aggregated packet here? */
fe8bc396 208static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
ef261577 209 __be16 tvlv_len)
fc957275 210{
08c36d3e
SE
211 int next_buff_pos = 0;
212
7e071c79 213 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
ef261577 214 next_buff_pos += ntohs(tvlv_len);
fc957275
ML
215
216 return (next_buff_pos <= packet_len) &&
42d0b044 217 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
fc957275
ML
218}
219
b9dacc52 220/* send a batman ogm to a given interface */
56303d34
SE
221static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
222 struct batadv_hard_iface *hard_iface)
b9dacc52 223{
56303d34 224 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
b9dacc52
ML
225 char *fwd_str;
226 uint8_t packet_num;
227 int16_t buff_pos;
96412690 228 struct batadv_ogm_packet *batadv_ogm_packet;
b9dacc52 229 struct sk_buff *skb;
c67893d1 230 uint8_t *packet_pos;
b9dacc52 231
e9a4f295 232 if (hard_iface->if_status != BATADV_IF_ACTIVE)
b9dacc52
ML
233 return;
234
235 packet_num = 0;
236 buff_pos = 0;
c67893d1
SE
237 packet_pos = forw_packet->skb->data;
238 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
b9dacc52
ML
239
240 /* adjust all flags and log packets */
fe8bc396 241 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
ef261577 242 batadv_ogm_packet->tvlv_len)) {
b9dacc52 243 /* we might have aggregated direct link packets with an
9cfc7bd6
SE
244 * ordinary base packet
245 */
8de47de5
SE
246 if (forw_packet->direct_link_flags & BIT(packet_num) &&
247 forw_packet->if_incoming == hard_iface)
96412690 248 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
b9dacc52 249 else
96412690 250 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
b9dacc52 251
c67893d1
SE
252 if (packet_num > 0 || !forw_packet->own)
253 fwd_str = "Forwarding";
254 else
255 fwd_str = "Sending own";
256
39c75a51 257 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
e1bf0c14 258 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
1eda58bf 259 fwd_str, (packet_num > 0 ? "aggregated " : ""),
96412690
SE
260 batadv_ogm_packet->orig,
261 ntohl(batadv_ogm_packet->seqno),
262 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl,
263 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
1eda58bf 264 "on" : "off"),
e1bf0c14 265 hard_iface->net_dev->name,
1eda58bf 266 hard_iface->net_dev->dev_addr);
b9dacc52 267
7e071c79 268 buff_pos += BATADV_OGM_HLEN;
ef261577 269 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
b9dacc52 270 packet_num++;
c67893d1
SE
271 packet_pos = forw_packet->skb->data + buff_pos;
272 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
b9dacc52
ML
273 }
274
275 /* create clone because function is called more than once */
276 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
f8214865 277 if (skb) {
d69909d2
SE
278 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
279 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
f8214865 280 skb->len + ETH_HLEN);
3193e8fd 281 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
f8214865 282 }
b9dacc52
ML
283}
284
285/* send a batman ogm packet */
56303d34 286static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
b9dacc52 287{
56303d34 288 struct batadv_hard_iface *hard_iface;
b9dacc52 289 struct net_device *soft_iface;
56303d34
SE
290 struct batadv_priv *bat_priv;
291 struct batadv_hard_iface *primary_if = NULL;
96412690 292 struct batadv_ogm_packet *batadv_ogm_packet;
b9dacc52 293 unsigned char directlink;
c67893d1 294 uint8_t *packet_pos;
b9dacc52 295
c67893d1
SE
296 packet_pos = forw_packet->skb->data;
297 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
96412690 298 directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0);
b9dacc52
ML
299
300 if (!forw_packet->if_incoming) {
86ceb360 301 pr_err("Error - can't forward packet: incoming iface not specified\n");
b9dacc52
ML
302 goto out;
303 }
304
305 soft_iface = forw_packet->if_incoming->soft_iface;
306 bat_priv = netdev_priv(soft_iface);
307
e9a4f295 308 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
b9dacc52
ML
309 goto out;
310
e5d89254 311 primary_if = batadv_primary_if_get_selected(bat_priv);
b9dacc52
ML
312 if (!primary_if)
313 goto out;
314
9cfc7bd6
SE
315 /* multihomed peer assumed
316 * non-primary OGMs are only broadcasted on their interface
317 */
96412690 318 if ((directlink && (batadv_ogm_packet->header.ttl == 1)) ||
b9dacc52 319 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
b9dacc52 320 /* FIXME: what about aggregated packets ? */
39c75a51 321 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
322 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
323 (forw_packet->own ? "Sending own" : "Forwarding"),
96412690
SE
324 batadv_ogm_packet->orig,
325 ntohl(batadv_ogm_packet->seqno),
326 batadv_ogm_packet->header.ttl,
1eda58bf
SE
327 forw_packet->if_incoming->net_dev->name,
328 forw_packet->if_incoming->net_dev->dev_addr);
b9dacc52
ML
329
330 /* skb is only used once and than forw_packet is free'd */
9455e34c
SE
331 batadv_send_skb_packet(forw_packet->skb,
332 forw_packet->if_incoming,
3193e8fd 333 batadv_broadcast_addr);
b9dacc52
ML
334 forw_packet->skb = NULL;
335
336 goto out;
337 }
338
339 /* broadcast on every interface */
340 rcu_read_lock();
3193e8fd 341 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
b9dacc52
ML
342 if (hard_iface->soft_iface != soft_iface)
343 continue;
344
fe8bc396 345 batadv_iv_ogm_send_to_if(forw_packet, hard_iface);
b9dacc52
ML
346 }
347 rcu_read_unlock();
348
349out:
350 if (primary_if)
e5d89254 351 batadv_hardif_free_ref(primary_if);
b9dacc52
ML
352}
353
354/* return true if new_packet can be aggregated with forw_packet */
fe8bc396 355static bool
96412690 356batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
56303d34 357 struct batadv_priv *bat_priv,
fe8bc396
SE
358 int packet_len, unsigned long send_time,
359 bool directlink,
56303d34
SE
360 const struct batadv_hard_iface *if_incoming,
361 const struct batadv_forw_packet *forw_packet)
b9dacc52 362{
96412690 363 struct batadv_ogm_packet *batadv_ogm_packet;
b9dacc52 364 int aggregated_bytes = forw_packet->packet_len + packet_len;
56303d34 365 struct batadv_hard_iface *primary_if = NULL;
b9dacc52 366 bool res = false;
42d0b044 367 unsigned long aggregation_end_time;
b9dacc52 368
96412690 369 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
42d0b044
SE
370 aggregation_end_time = send_time;
371 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
b9dacc52 372
9cfc7bd6 373 /* we can aggregate the current packet to this aggregated packet
b9dacc52
ML
374 * if:
375 *
376 * - the send time is within our MAX_AGGREGATION_MS time
377 * - the resulting packet wont be bigger than
378 * MAX_AGGREGATION_BYTES
379 */
b9dacc52 380 if (time_before(send_time, forw_packet->send_time) &&
42d0b044
SE
381 time_after_eq(aggregation_end_time, forw_packet->send_time) &&
382 (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
9cfc7bd6 383 /* check aggregation compatibility
b9dacc52
ML
384 * -> direct link packets are broadcasted on
385 * their interface only
386 * -> aggregate packet if the current packet is
387 * a "global" packet as well as the base
388 * packet
389 */
e5d89254 390 primary_if = batadv_primary_if_get_selected(bat_priv);
b9dacc52
ML
391 if (!primary_if)
392 goto out;
393
394 /* packets without direct link flag and high TTL
9cfc7bd6
SE
395 * are flooded through the net
396 */
b9dacc52 397 if ((!directlink) &&
96412690
SE
398 (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
399 (batadv_ogm_packet->header.ttl != 1) &&
b9dacc52
ML
400
401 /* own packets originating non-primary
9cfc7bd6
SE
402 * interfaces leave only that interface
403 */
b9dacc52
ML
404 ((!forw_packet->own) ||
405 (forw_packet->if_incoming == primary_if))) {
406 res = true;
407 goto out;
408 }
409
410 /* if the incoming packet is sent via this one
9cfc7bd6
SE
411 * interface only - we still can aggregate
412 */
b9dacc52 413 if ((directlink) &&
fe8bc396 414 (new_bat_ogm_packet->header.ttl == 1) &&
b9dacc52
ML
415 (forw_packet->if_incoming == if_incoming) &&
416
417 /* packets from direct neighbors or
418 * own secondary interface packets
9cfc7bd6
SE
419 * (= secondary interface packets in general)
420 */
96412690 421 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
b9dacc52
ML
422 (forw_packet->own &&
423 forw_packet->if_incoming != primary_if))) {
424 res = true;
425 goto out;
426 }
427 }
428
429out:
430 if (primary_if)
e5d89254 431 batadv_hardif_free_ref(primary_if);
b9dacc52
ML
432 return res;
433}
434
435/* create a new aggregated packet and add this packet to it */
fe8bc396
SE
436static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
437 int packet_len, unsigned long send_time,
438 bool direct_link,
56303d34 439 struct batadv_hard_iface *if_incoming,
fe8bc396 440 int own_packet)
b9dacc52 441{
56303d34
SE
442 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
443 struct batadv_forw_packet *forw_packet_aggr;
b9dacc52 444 unsigned char *skb_buff;
42d0b044 445 unsigned int skb_size;
b9dacc52
ML
446
447 if (!atomic_inc_not_zero(&if_incoming->refcount))
448 return;
449
450 /* own packet should always be scheduled */
451 if (!own_packet) {
3e34819e 452 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
39c75a51 453 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 454 "batman packet queue full\n");
b9dacc52
ML
455 goto out;
456 }
457 }
458
459 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
460 if (!forw_packet_aggr) {
461 if (!own_packet)
462 atomic_inc(&bat_priv->batman_queue_left);
463 goto out;
464 }
465
466 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
42d0b044 467 (packet_len < BATADV_MAX_AGGREGATION_BYTES))
5b246574 468 skb_size = BATADV_MAX_AGGREGATION_BYTES;
b9dacc52 469 else
5b246574
SE
470 skb_size = packet_len;
471
41ab6c48 472 skb_size += ETH_HLEN;
b9dacc52 473
41ab6c48 474 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
b9dacc52
ML
475 if (!forw_packet_aggr->skb) {
476 if (!own_packet)
477 atomic_inc(&bat_priv->batman_queue_left);
478 kfree(forw_packet_aggr);
479 goto out;
480 }
c54f38c9 481 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
41ab6c48 482 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
b9dacc52 483
b9dacc52
ML
484 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
485 forw_packet_aggr->packet_len = packet_len;
486 memcpy(skb_buff, packet_buff, packet_len);
487
488 forw_packet_aggr->own = own_packet;
489 forw_packet_aggr->if_incoming = if_incoming;
490 forw_packet_aggr->num_packets = 0;
42d0b044 491 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
b9dacc52
ML
492 forw_packet_aggr->send_time = send_time;
493
494 /* save packet direct link flag status */
495 if (direct_link)
496 forw_packet_aggr->direct_link_flags |= 1;
497
498 /* add new packet to packet list */
499 spin_lock_bh(&bat_priv->forw_bat_list_lock);
500 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
501 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
502
503 /* start timer for this packet */
504 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
9455e34c 505 batadv_send_outstanding_bat_ogm_packet);
3193e8fd 506 queue_delayed_work(batadv_event_workqueue,
b9dacc52
ML
507 &forw_packet_aggr->delayed_work,
508 send_time - jiffies);
509
510 return;
511out:
e5d89254 512 batadv_hardif_free_ref(if_incoming);
b9dacc52
ML
513}
514
515/* aggregate a new packet into the existing ogm packet */
56303d34 516static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
fe8bc396
SE
517 const unsigned char *packet_buff,
518 int packet_len, bool direct_link)
b9dacc52
ML
519{
520 unsigned char *skb_buff;
8de47de5 521 unsigned long new_direct_link_flag;
b9dacc52
ML
522
523 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
524 memcpy(skb_buff, packet_buff, packet_len);
525 forw_packet_aggr->packet_len += packet_len;
526 forw_packet_aggr->num_packets++;
527
528 /* save packet direct link flag status */
8de47de5
SE
529 if (direct_link) {
530 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
531 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
532 }
b9dacc52
ML
533}
534
56303d34 535static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
fe8bc396
SE
536 unsigned char *packet_buff,
537 int packet_len,
56303d34 538 struct batadv_hard_iface *if_incoming,
fe8bc396 539 int own_packet, unsigned long send_time)
b9dacc52 540{
9cfc7bd6 541 /* _aggr -> pointer to the packet we want to aggregate with
b9dacc52
ML
542 * _pos -> pointer to the position in the queue
543 */
56303d34
SE
544 struct batadv_forw_packet *forw_packet_aggr = NULL;
545 struct batadv_forw_packet *forw_packet_pos = NULL;
96412690 546 struct batadv_ogm_packet *batadv_ogm_packet;
b9dacc52 547 bool direct_link;
42d0b044 548 unsigned long max_aggregation_jiffies;
b9dacc52 549
96412690
SE
550 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
551 direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
42d0b044 552 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
b9dacc52
ML
553
554 /* find position for the packet in the forward queue */
555 spin_lock_bh(&bat_priv->forw_bat_list_lock);
556 /* own packets are not to be aggregated */
557 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
b67bfe0d 558 hlist_for_each_entry(forw_packet_pos,
b9dacc52 559 &bat_priv->forw_bat_list, list) {
96412690 560 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
fe8bc396
SE
561 bat_priv, packet_len,
562 send_time, direct_link,
563 if_incoming,
564 forw_packet_pos)) {
b9dacc52
ML
565 forw_packet_aggr = forw_packet_pos;
566 break;
567 }
568 }
569 }
570
571 /* nothing to aggregate with - either aggregation disabled or no
9cfc7bd6
SE
572 * suitable aggregation packet found
573 */
b9dacc52
ML
574 if (!forw_packet_aggr) {
575 /* the following section can run without the lock */
576 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
577
9cfc7bd6 578 /* if we could not aggregate this packet with one of the others
b9dacc52
ML
579 * we hold it back for a while, so that it might be aggregated
580 * later on
581 */
42d0b044
SE
582 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
583 send_time += max_aggregation_jiffies;
b9dacc52 584
fe8bc396
SE
585 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
586 send_time, direct_link,
587 if_incoming, own_packet);
b9dacc52 588 } else {
fe8bc396
SE
589 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
590 packet_len, direct_link);
b9dacc52
ML
591 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
592 }
593}
594
56303d34 595static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
fe8bc396 596 const struct ethhdr *ethhdr,
96412690 597 struct batadv_ogm_packet *batadv_ogm_packet,
fe8bc396
SE
598 bool is_single_hop_neigh,
599 bool is_from_best_next_hop,
56303d34 600 struct batadv_hard_iface *if_incoming)
b9dacc52 601{
56303d34 602 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
ef261577 603 uint16_t tvlv_len;
b9dacc52 604
96412690 605 if (batadv_ogm_packet->header.ttl <= 1) {
39c75a51 606 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
b9dacc52
ML
607 return;
608 }
609
13b2541b
ML
610 if (!is_from_best_next_hop) {
611 /* Mark the forwarded packet when it is not coming from our
612 * best next hop. We still need to forward the packet for our
613 * neighbor link quality detection to work in case the packet
614 * originated from a single hop neighbor. Otherwise we can
615 * simply drop the ogm.
616 */
617 if (is_single_hop_neigh)
96412690 618 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
13b2541b
ML
619 else
620 return;
621 }
b9dacc52 622
ef261577 623 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
b9dacc52 624
96412690
SE
625 batadv_ogm_packet->header.ttl--;
626 memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
b9dacc52 627
b9dacc52 628 /* apply hop penalty */
96412690 629 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
fe8bc396 630 bat_priv);
b9dacc52 631
39c75a51 632 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 633 "Forwarding packet: tq: %i, ttl: %i\n",
96412690 634 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl);
b9dacc52 635
b9dacc52 636 /* switch of primaries first hop flag when forwarding */
96412690 637 batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
75cd33f8 638 if (is_single_hop_neigh)
96412690 639 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
b9dacc52 640 else
96412690 641 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
b9dacc52 642
96412690 643 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
ef261577 644 BATADV_OGM_HLEN + tvlv_len,
fe8bc396 645 if_incoming, 0, batadv_iv_ogm_fwd_send_time());
b9dacc52
ML
646}
647
d9896617
AQ
648/**
649 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
650 * the given interface
651 * @hard_iface: the interface for which the windows have to be shifted
652 */
653static void
654batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
655{
656 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
657 struct batadv_hashtable *hash = bat_priv->orig_hash;
658 struct hlist_head *head;
659 struct batadv_orig_node *orig_node;
660 unsigned long *word;
661 uint32_t i;
662 size_t word_index;
663 uint8_t *w;
664
665 for (i = 0; i < hash->size; i++) {
666 head = &hash->table[i];
667
668 rcu_read_lock();
669 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
670 spin_lock_bh(&orig_node->ogm_cnt_lock);
671 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
672 word = &(orig_node->bcast_own[word_index]);
673
674 batadv_bit_get_packet(bat_priv, word, 1, 0);
675 w = &orig_node->bcast_own_sum[hard_iface->if_num];
676 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
677 spin_unlock_bh(&orig_node->ogm_cnt_lock);
678 }
679 rcu_read_unlock();
680 }
681}
682
56303d34 683static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
b9dacc52 684{
56303d34 685 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
14511519 686 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
96412690 687 struct batadv_ogm_packet *batadv_ogm_packet;
56303d34 688 struct batadv_hard_iface *primary_if;
14511519 689 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
bbb1f90e 690 uint32_t seqno;
ef261577 691 uint16_t tvlv_len = 0;
b9dacc52 692
e5d89254 693 primary_if = batadv_primary_if_get_selected(bat_priv);
b9dacc52 694
e1bf0c14
ML
695 if (hard_iface == primary_if) {
696 /* tt changes have to be committed before the tvlv data is
697 * appended as it may alter the tt tvlv container
698 */
699 batadv_tt_local_commit_changes(bat_priv);
ef261577
ML
700 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
701 ogm_buff_len,
702 BATADV_OGM_HLEN);
e1bf0c14 703 }
be9aa4c1 704
14511519 705 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
ef261577 706 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
b9dacc52
ML
707
708 /* change sequence number to network order */
14511519 709 seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
bbb1f90e 710 batadv_ogm_packet->seqno = htonl(seqno);
14511519 711 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
b9dacc52 712
d9896617 713 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
14511519
ML
714 batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff,
715 hard_iface->bat_iv.ogm_buff_len, hard_iface, 1,
fe8bc396 716 batadv_iv_ogm_emit_send_time(bat_priv));
b9dacc52
ML
717
718 if (primary_if)
e5d89254 719 batadv_hardif_free_ref(primary_if);
b9dacc52
ML
720}
721
fe8bc396 722static void
56303d34
SE
723batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
724 struct batadv_orig_node *orig_node,
fe8bc396 725 const struct ethhdr *ethhdr,
96412690 726 const struct batadv_ogm_packet *batadv_ogm_packet,
56303d34 727 struct batadv_hard_iface *if_incoming,
fe8bc396 728 const unsigned char *tt_buff,
7c24bbbe 729 enum batadv_dup_status dup_status)
fc957275 730{
56303d34
SE
731 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
732 struct batadv_neigh_node *router = NULL;
733 struct batadv_orig_node *orig_node_tmp;
7caf69fb 734 int if_num;
bbb1f90e 735 uint8_t sum_orig, sum_neigh;
1eda58bf 736 uint8_t *neigh_addr;
bbb1f90e 737 uint8_t tq_avg;
fc957275 738
39c75a51 739 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 740 "update_originator(): Searching and updating originator entry of received packet\n");
fc957275
ML
741
742 rcu_read_lock();
b67bfe0d 743 hlist_for_each_entry_rcu(tmp_neigh_node,
fc957275 744 &orig_node->neigh_list, list) {
1eda58bf
SE
745 neigh_addr = tmp_neigh_node->addr;
746 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
747 tmp_neigh_node->if_incoming == if_incoming &&
748 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
38dc40ef 749 if (WARN(neigh_node, "too many matching neigh_nodes"))
7d211efc 750 batadv_neigh_node_free_ref(neigh_node);
fc957275
ML
751 neigh_node = tmp_neigh_node;
752 continue;
753 }
754
7c24bbbe 755 if (dup_status != BATADV_NO_DUP)
fc957275
ML
756 continue;
757
e3b0d0de 758 spin_lock_bh(&tmp_neigh_node->lq_update_lock);
925a6672
SE
759 batadv_ring_buffer_set(tmp_neigh_node->tq_recv,
760 &tmp_neigh_node->tq_index, 0);
bbb1f90e
SE
761 tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->tq_recv);
762 tmp_neigh_node->tq_avg = tq_avg;
e3b0d0de 763 spin_unlock_bh(&tmp_neigh_node->lq_update_lock);
fc957275
ML
764 }
765
766 if (!neigh_node) {
56303d34 767 struct batadv_orig_node *orig_tmp;
fc957275 768
7d211efc 769 orig_tmp = batadv_get_orig_node(bat_priv, ethhdr->h_source);
fc957275
ML
770 if (!orig_tmp)
771 goto unlock;
772
fe8bc396
SE
773 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
774 ethhdr->h_source,
863dd7a8 775 orig_node, orig_tmp);
fc957275 776
7d211efc 777 batadv_orig_node_free_ref(orig_tmp);
fc957275
ML
778 if (!neigh_node)
779 goto unlock;
780 } else
39c75a51 781 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 782 "Updating existing last-hop neighbor of originator\n");
fc957275
ML
783
784 rcu_read_unlock();
785
d7b2a97e 786 neigh_node->last_seen = jiffies;
fc957275 787
e3b0d0de 788 spin_lock_bh(&neigh_node->lq_update_lock);
925a6672
SE
789 batadv_ring_buffer_set(neigh_node->tq_recv,
790 &neigh_node->tq_index,
96412690 791 batadv_ogm_packet->tq);
925a6672 792 neigh_node->tq_avg = batadv_ring_buffer_avg(neigh_node->tq_recv);
e3b0d0de 793 spin_unlock_bh(&neigh_node->lq_update_lock);
fc957275 794
7c24bbbe 795 if (dup_status == BATADV_NO_DUP) {
96412690
SE
796 orig_node->last_ttl = batadv_ogm_packet->header.ttl;
797 neigh_node->last_ttl = batadv_ogm_packet->header.ttl;
fc957275
ML
798 }
799
30d3c511 800 batadv_bonding_candidate_add(orig_node, neigh_node);
fc957275
ML
801
802 /* if this neighbor already is our next hop there is nothing
9cfc7bd6
SE
803 * to change
804 */
7d211efc 805 router = batadv_orig_node_get_router(orig_node);
fc957275 806 if (router == neigh_node)
e1bf0c14 807 goto out;
fc957275
ML
808
809 /* if this neighbor does not offer a better TQ we won't consider it */
810 if (router && (router->tq_avg > neigh_node->tq_avg))
e1bf0c14 811 goto out;
fc957275
ML
812
813 /* if the TQ is the same and the link not more symmetric we
9cfc7bd6
SE
814 * won't consider it either
815 */
fc957275
ML
816 if (router && (neigh_node->tq_avg == router->tq_avg)) {
817 orig_node_tmp = router->orig_node;
818 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
7caf69fb
LL
819 if_num = router->if_incoming->if_num;
820 sum_orig = orig_node_tmp->bcast_own_sum[if_num];
fc957275
ML
821 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
822
823 orig_node_tmp = neigh_node->orig_node;
824 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
7caf69fb
LL
825 if_num = neigh_node->if_incoming->if_num;
826 sum_neigh = orig_node_tmp->bcast_own_sum[if_num];
fc957275
ML
827 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
828
bbb1f90e 829 if (sum_orig >= sum_neigh)
e1bf0c14 830 goto out;
fc957275
ML
831 }
832
30d3c511 833 batadv_update_route(bat_priv, orig_node, neigh_node);
fc957275
ML
834 goto out;
835
836unlock:
837 rcu_read_unlock();
838out:
839 if (neigh_node)
7d211efc 840 batadv_neigh_node_free_ref(neigh_node);
fc957275 841 if (router)
7d211efc 842 batadv_neigh_node_free_ref(router);
fc957275
ML
843}
844
56303d34
SE
845static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
846 struct batadv_orig_node *orig_neigh_node,
96412690 847 struct batadv_ogm_packet *batadv_ogm_packet,
56303d34 848 struct batadv_hard_iface *if_incoming)
fc957275 849{
56303d34
SE
850 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
851 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
fc957275 852 uint8_t total_count;
42d0b044
SE
853 uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
854 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
855 int tq_asym_penalty, inv_asym_penalty, ret = 0;
856 unsigned int combined_tq;
fc957275
ML
857
858 /* find corresponding one hop neighbor */
859 rcu_read_lock();
b67bfe0d 860 hlist_for_each_entry_rcu(tmp_neigh_node,
fc957275 861 &orig_neigh_node->neigh_list, list) {
1eda58bf
SE
862 if (!batadv_compare_eth(tmp_neigh_node->addr,
863 orig_neigh_node->orig))
fc957275
ML
864 continue;
865
866 if (tmp_neigh_node->if_incoming != if_incoming)
867 continue;
868
869 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
870 continue;
871
872 neigh_node = tmp_neigh_node;
873 break;
874 }
875 rcu_read_unlock();
876
877 if (!neigh_node)
fe8bc396
SE
878 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
879 orig_neigh_node->orig,
880 orig_neigh_node,
863dd7a8 881 orig_neigh_node);
fc957275
ML
882
883 if (!neigh_node)
884 goto out;
885
d7b2a97e 886 /* if orig_node is direct neighbor update neigh_node last_seen */
fc957275 887 if (orig_node == orig_neigh_node)
d7b2a97e 888 neigh_node->last_seen = jiffies;
fc957275 889
d7b2a97e 890 orig_node->last_seen = jiffies;
fc957275
ML
891
892 /* find packet count of corresponding one hop neighbor */
893 spin_lock_bh(&orig_node->ogm_cnt_lock);
894 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
895 neigh_rq_count = neigh_node->real_packet_count;
896 spin_unlock_bh(&orig_node->ogm_cnt_lock);
897
898 /* pay attention to not get a value bigger than 100 % */
c67893d1
SE
899 if (orig_eq_count > neigh_rq_count)
900 total_count = neigh_rq_count;
901 else
902 total_count = orig_eq_count;
fc957275 903
9cfc7bd6
SE
904 /* if we have too few packets (too less data) we set tq_own to zero
905 * if we receive too few packets it is not considered bidirectional
906 */
42d0b044
SE
907 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
908 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
fc957275
ML
909 tq_own = 0;
910 else
911 /* neigh_node->real_packet_count is never zero as we
912 * only purge old information when getting new
9cfc7bd6
SE
913 * information
914 */
42d0b044 915 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
fc957275 916
21a1236b 917 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
fc957275
ML
918 * affect the nearly-symmetric links only a little, but
919 * punishes asymmetric links more. This will give a value
920 * between 0 and TQ_MAX_VALUE
921 */
42d0b044
SE
922 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
923 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
924 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
925 BATADV_TQ_LOCAL_WINDOW_SIZE *
926 BATADV_TQ_LOCAL_WINDOW_SIZE;
927 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
928 inv_asym_penalty /= neigh_rq_max_cube;
929 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
930
96412690 931 combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
42d0b044 932 combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
96412690 933 batadv_ogm_packet->tq = combined_tq;
fc957275 934
39c75a51 935 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
936 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
937 orig_node->orig, orig_neigh_node->orig, total_count,
938 neigh_rq_count, tq_own,
96412690 939 tq_asym_penalty, batadv_ogm_packet->tq);
fc957275
ML
940
941 /* if link has the minimum required transmission quality
9cfc7bd6
SE
942 * consider it bidirectional
943 */
96412690 944 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
fc957275
ML
945 ret = 1;
946
947out:
948 if (neigh_node)
7d211efc 949 batadv_neigh_node_free_ref(neigh_node);
fc957275
ML
950 return ret;
951}
952
7c24bbbe
SW
953/**
954 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
955 * adjust the sequence number and find out whether it is a duplicate
956 * @ethhdr: ethernet header of the packet
957 * @batadv_ogm_packet: OGM packet to be considered
958 * @if_incoming: interface on which the OGM packet was received
959 *
960 * Returns duplicate status as enum batadv_dup_status
fc957275 961 */
7c24bbbe 962static enum batadv_dup_status
fe8bc396 963batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
96412690 964 const struct batadv_ogm_packet *batadv_ogm_packet,
56303d34 965 const struct batadv_hard_iface *if_incoming)
fc957275 966{
56303d34
SE
967 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
968 struct batadv_orig_node *orig_node;
969 struct batadv_neigh_node *tmp_neigh_node;
7c24bbbe 970 int is_dup;
fc957275
ML
971 int32_t seq_diff;
972 int need_update = 0;
7c24bbbe
SW
973 int set_mark;
974 enum batadv_dup_status ret = BATADV_NO_DUP;
96412690 975 uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
1eda58bf 976 uint8_t *neigh_addr;
bbb1f90e 977 uint8_t packet_count;
fc957275 978
96412690 979 orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
fc957275 980 if (!orig_node)
7c24bbbe 981 return BATADV_NO_DUP;
fc957275
ML
982
983 spin_lock_bh(&orig_node->ogm_cnt_lock);
e0f5211f 984 seq_diff = seqno - orig_node->last_real_seqno;
fc957275
ML
985
986 /* signalize caller that the packet is to be dropped. */
1e5cc266 987 if (!hlist_empty(&orig_node->neigh_list) &&
30d3c511 988 batadv_window_protected(bat_priv, seq_diff,
7c24bbbe
SW
989 &orig_node->batman_seqno_reset)) {
990 ret = BATADV_PROTECTED;
fc957275 991 goto out;
7c24bbbe 992 }
fc957275
ML
993
994 rcu_read_lock();
b67bfe0d 995 hlist_for_each_entry_rcu(tmp_neigh_node,
fc957275 996 &orig_node->neigh_list, list) {
1eda58bf 997 neigh_addr = tmp_neigh_node->addr;
7c24bbbe
SW
998 is_dup = batadv_test_bit(tmp_neigh_node->real_bits,
999 orig_node->last_real_seqno,
1000 seqno);
1001
1eda58bf 1002 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
7c24bbbe 1003 tmp_neigh_node->if_incoming == if_incoming) {
fc957275 1004 set_mark = 1;
7c24bbbe
SW
1005 if (is_dup)
1006 ret = BATADV_NEIGH_DUP;
1007 } else {
fc957275 1008 set_mark = 0;
7c24bbbe
SW
1009 if (is_dup && (ret != BATADV_NEIGH_DUP))
1010 ret = BATADV_ORIG_DUP;
1011 }
fc957275
ML
1012
1013 /* if the window moved, set the update flag. */
0f5f9322
SE
1014 need_update |= batadv_bit_get_packet(bat_priv,
1015 tmp_neigh_node->real_bits,
1016 seq_diff, set_mark);
fc957275 1017
bbb1f90e
SE
1018 packet_count = bitmap_weight(tmp_neigh_node->real_bits,
1019 BATADV_TQ_LOCAL_WINDOW_SIZE);
1020 tmp_neigh_node->real_packet_count = packet_count;
fc957275
ML
1021 }
1022 rcu_read_unlock();
1023
1024 if (need_update) {
39c75a51 1025 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
1026 "updating last_seqno: old %u, new %u\n",
1027 orig_node->last_real_seqno, seqno);
e0f5211f 1028 orig_node->last_real_seqno = seqno;
fc957275
ML
1029 }
1030
fc957275
ML
1031out:
1032 spin_unlock_bh(&orig_node->ogm_cnt_lock);
7d211efc 1033 batadv_orig_node_free_ref(orig_node);
fc957275
ML
1034 return ret;
1035}
1036
fe8bc396 1037static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
96412690 1038 struct batadv_ogm_packet *batadv_ogm_packet,
fe8bc396 1039 const unsigned char *tt_buff,
56303d34 1040 struct batadv_hard_iface *if_incoming)
fc957275 1041{
56303d34
SE
1042 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1043 struct batadv_hard_iface *hard_iface;
1044 struct batadv_orig_node *orig_neigh_node, *orig_node;
1045 struct batadv_neigh_node *router = NULL, *router_router = NULL;
1046 struct batadv_neigh_node *orig_neigh_router = NULL;
fc957275
ML
1047 int has_directlink_flag;
1048 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
281581d3 1049 int is_bidirect;
75cd33f8 1050 bool is_single_hop_neigh = false;
13b2541b 1051 bool is_from_best_next_hop = false;
7c24bbbe
SW
1052 int sameseq, similar_ttl;
1053 enum batadv_dup_status dup_status;
fc957275 1054 uint32_t if_incoming_seqno;
1eda58bf 1055 uint8_t *prev_sender;
fc957275
ML
1056
1057 /* Silently drop when the batman packet is actually not a
1058 * correct packet.
1059 *
1060 * This might happen if a packet is padded (e.g. Ethernet has a
1061 * minimum frame length of 64 byte) and the aggregation interprets
1062 * it as an additional length.
1063 *
1064 * TODO: A more sane solution would be to have a bit in the
96412690 1065 * batadv_ogm_packet to detect whether the packet is the last
fc957275
ML
1066 * packet in an aggregation. Here we expect that the padding
1067 * is always zero (or not 0x01)
1068 */
96412690 1069 if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM)
fc957275
ML
1070 return;
1071
1072 /* could be changed by schedule_own_packet() */
14511519 1073 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
fc957275 1074
96412690 1075 if (batadv_ogm_packet->flags & BATADV_DIRECTLINK)
acd34afa
SE
1076 has_directlink_flag = 1;
1077 else
1078 has_directlink_flag = 0;
fc957275 1079
96412690 1080 if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig))
75cd33f8 1081 is_single_hop_neigh = true;
fc957275 1082
39c75a51 1083 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
e1bf0c14 1084 "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",
1eda58bf 1085 ethhdr->h_source, if_incoming->net_dev->name,
96412690
SE
1086 if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
1087 batadv_ogm_packet->prev_sender,
e1bf0c14 1088 ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->tq,
96412690
SE
1089 batadv_ogm_packet->header.ttl,
1090 batadv_ogm_packet->header.version, has_directlink_flag);
fc957275
ML
1091
1092 rcu_read_lock();
3193e8fd 1093 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
e9a4f295 1094 if (hard_iface->if_status != BATADV_IF_ACTIVE)
fc957275
ML
1095 continue;
1096
1097 if (hard_iface->soft_iface != if_incoming->soft_iface)
1098 continue;
1099
1eda58bf
SE
1100 if (batadv_compare_eth(ethhdr->h_source,
1101 hard_iface->net_dev->dev_addr))
fc957275
ML
1102 is_my_addr = 1;
1103
96412690 1104 if (batadv_compare_eth(batadv_ogm_packet->orig,
1eda58bf 1105 hard_iface->net_dev->dev_addr))
fc957275
ML
1106 is_my_orig = 1;
1107
96412690 1108 if (batadv_compare_eth(batadv_ogm_packet->prev_sender,
1eda58bf 1109 hard_iface->net_dev->dev_addr))
fc957275 1110 is_my_oldorig = 1;
fc957275
ML
1111 }
1112 rcu_read_unlock();
1113
fc957275 1114 if (is_my_addr) {
39c75a51 1115 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
1116 "Drop packet: received my own broadcast (sender: %pM)\n",
1117 ethhdr->h_source);
fc957275
ML
1118 return;
1119 }
1120
fc957275
ML
1121 if (is_my_orig) {
1122 unsigned long *word;
1123 int offset;
9b4a1159 1124 int32_t bit_pos;
42d0b044
SE
1125 int16_t if_num;
1126 uint8_t *weight;
fc957275 1127
7d211efc
SE
1128 orig_neigh_node = batadv_get_orig_node(bat_priv,
1129 ethhdr->h_source);
fc957275
ML
1130 if (!orig_neigh_node)
1131 return;
1132
1133 /* neighbor has to indicate direct link and it has to
9cfc7bd6
SE
1134 * come via the corresponding interface
1135 * save packet seqno for bidirectional check
1136 */
fc957275 1137 if (has_directlink_flag &&
1eda58bf 1138 batadv_compare_eth(if_incoming->net_dev->dev_addr,
96412690 1139 batadv_ogm_packet->orig)) {
42d0b044
SE
1140 if_num = if_incoming->if_num;
1141 offset = if_num * BATADV_NUM_WORDS;
fc957275
ML
1142
1143 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1144 word = &(orig_neigh_node->bcast_own[offset]);
9b4a1159 1145 bit_pos = if_incoming_seqno - 2;
96412690 1146 bit_pos -= ntohl(batadv_ogm_packet->seqno);
9b4a1159 1147 batadv_set_bit(word, bit_pos);
42d0b044
SE
1148 weight = &orig_neigh_node->bcast_own_sum[if_num];
1149 *weight = bitmap_weight(word,
1150 BATADV_TQ_LOCAL_WINDOW_SIZE);
fc957275
ML
1151 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1152 }
1153
39c75a51 1154 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1155 "Drop packet: originator packet from myself (via neighbor)\n");
7d211efc 1156 batadv_orig_node_free_ref(orig_neigh_node);
fc957275
ML
1157 return;
1158 }
1159
1160 if (is_my_oldorig) {
39c75a51 1161 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
1162 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1163 ethhdr->h_source);
fc957275
ML
1164 return;
1165 }
1166
96412690 1167 if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
39c75a51 1168 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
1169 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1170 ethhdr->h_source);
13b2541b
ML
1171 return;
1172 }
1173
96412690 1174 orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
fc957275
ML
1175 if (!orig_node)
1176 return;
1177
7c24bbbe
SW
1178 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet,
1179 if_incoming);
fc957275 1180
7c24bbbe 1181 if (dup_status == BATADV_PROTECTED) {
39c75a51 1182 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
1183 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1184 ethhdr->h_source);
fc957275
ML
1185 goto out;
1186 }
1187
96412690 1188 if (batadv_ogm_packet->tq == 0) {
39c75a51 1189 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1190 "Drop packet: originator packet with tq equal 0\n");
fc957275
ML
1191 goto out;
1192 }
1193
7d211efc 1194 router = batadv_orig_node_get_router(orig_node);
fc957275 1195 if (router)
7d211efc 1196 router_router = batadv_orig_node_get_router(router->orig_node);
fc957275 1197
13b2541b 1198 if ((router && router->tq_avg != 0) &&
1eda58bf 1199 (batadv_compare_eth(router->addr, ethhdr->h_source)))
13b2541b
ML
1200 is_from_best_next_hop = true;
1201
96412690 1202 prev_sender = batadv_ogm_packet->prev_sender;
fc957275
ML
1203 /* avoid temporary routing loops */
1204 if (router && router_router &&
1eda58bf 1205 (batadv_compare_eth(router->addr, prev_sender)) &&
96412690 1206 !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) &&
1eda58bf 1207 (batadv_compare_eth(router->addr, router_router->addr))) {
39c75a51 1208 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
1209 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1210 ethhdr->h_source);
fc957275
ML
1211 goto out;
1212 }
1213
ef261577
ML
1214 batadv_tvlv_ogm_receive(bat_priv, batadv_ogm_packet, orig_node);
1215
fc957275 1216 /* if sender is a direct neighbor the sender mac equals
9cfc7bd6
SE
1217 * originator mac
1218 */
c67893d1
SE
1219 if (is_single_hop_neigh)
1220 orig_neigh_node = orig_node;
1221 else
1222 orig_neigh_node = batadv_get_orig_node(bat_priv,
1223 ethhdr->h_source);
1224
fc957275
ML
1225 if (!orig_neigh_node)
1226 goto out;
1227
d56b1705
MH
1228 /* Update nc_nodes of the originator */
1229 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1230 batadv_ogm_packet, is_single_hop_neigh);
1231
7d211efc 1232 orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
fc957275
ML
1233
1234 /* drop packet if sender is not a direct neighbor and if we
9cfc7bd6
SE
1235 * don't route towards it
1236 */
fc957275 1237 if (!is_single_hop_neigh && (!orig_neigh_router)) {
39c75a51 1238 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1239 "Drop packet: OGM via unknown neighbor!\n");
fc957275
ML
1240 goto out_neigh;
1241 }
1242
fe8bc396 1243 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
96412690 1244 batadv_ogm_packet, if_incoming);
fc957275 1245
30d3c511 1246 batadv_bonding_save_primary(orig_node, orig_neigh_node,
96412690 1247 batadv_ogm_packet);
fc957275
ML
1248
1249 /* update ranking if it is not a duplicate or has the same
9cfc7bd6
SE
1250 * seqno and similar ttl as the non-duplicate
1251 */
96412690 1252 sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
7c24bbbe
SW
1253 similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl;
1254 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1255 (sameseq && similar_ttl)))
fe8bc396 1256 batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
96412690 1257 batadv_ogm_packet, if_incoming,
7c24bbbe 1258 tt_buff, dup_status);
fc957275
ML
1259
1260 /* is single hop (direct) neighbor */
1261 if (is_single_hop_neigh) {
fc957275 1262 /* mark direct link on incoming interface */
96412690 1263 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
fe8bc396
SE
1264 is_single_hop_neigh,
1265 is_from_best_next_hop, if_incoming);
fc957275 1266
39c75a51 1267 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1268 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
fc957275
ML
1269 goto out_neigh;
1270 }
1271
1272 /* multihop originator */
fe8bc396 1273 if (!is_bidirect) {
39c75a51 1274 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1275 "Drop packet: not received via bidirectional link\n");
fc957275
ML
1276 goto out_neigh;
1277 }
1278
7c24bbbe 1279 if (dup_status == BATADV_NEIGH_DUP) {
39c75a51 1280 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1281 "Drop packet: duplicate packet received\n");
fc957275
ML
1282 goto out_neigh;
1283 }
1284
39c75a51 1285 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 1286 "Forwarding packet: rebroadcast originator packet\n");
96412690 1287 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
fe8bc396
SE
1288 is_single_hop_neigh, is_from_best_next_hop,
1289 if_incoming);
fc957275
ML
1290
1291out_neigh:
1292 if ((orig_neigh_node) && (!is_single_hop_neigh))
7d211efc 1293 batadv_orig_node_free_ref(orig_neigh_node);
fc957275
ML
1294out:
1295 if (router)
7d211efc 1296 batadv_neigh_node_free_ref(router);
fc957275 1297 if (router_router)
7d211efc 1298 batadv_neigh_node_free_ref(router_router);
fc957275 1299 if (orig_neigh_router)
7d211efc 1300 batadv_neigh_node_free_ref(orig_neigh_router);
fc957275 1301
7d211efc 1302 batadv_orig_node_free_ref(orig_node);
fc957275
ML
1303}
1304
fe8bc396 1305static int batadv_iv_ogm_receive(struct sk_buff *skb,
56303d34 1306 struct batadv_hard_iface *if_incoming)
fc957275 1307{
56303d34 1308 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
96412690 1309 struct batadv_ogm_packet *batadv_ogm_packet;
8780dad9
ML
1310 struct ethhdr *ethhdr;
1311 int buff_pos = 0, packet_len;
ef261577 1312 unsigned char *tvlv_buff, *packet_buff;
c67893d1 1313 uint8_t *packet_pos;
ef261577 1314 bool ret;
c3e29312 1315
7e071c79 1316 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
c3e29312
ML
1317 if (!ret)
1318 return NET_RX_DROP;
fc957275 1319
edbf12ba
ML
1320 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1321 * that does not have B.A.T.M.A.N. IV enabled ?
1322 */
fe8bc396 1323 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
edbf12ba
ML
1324 return NET_RX_DROP;
1325
d69909d2
SE
1326 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1327 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
f8214865
MH
1328 skb->len + ETH_HLEN);
1329
8780dad9 1330 packet_len = skb_headlen(skb);
7ed4be95 1331 ethhdr = eth_hdr(skb);
8780dad9 1332 packet_buff = skb->data;
96412690 1333 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
fc957275
ML
1334
1335 /* unpack the aggregated packets and process them one by one */
b47506d9 1336 while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
ef261577
ML
1337 batadv_ogm_packet->tvlv_len)) {
1338 tvlv_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
fc957275 1339
ef261577
ML
1340 batadv_iv_ogm_process(ethhdr, batadv_ogm_packet,
1341 tvlv_buff, if_incoming);
fc957275 1342
7e071c79 1343 buff_pos += BATADV_OGM_HLEN;
ef261577 1344 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
fc957275 1345
c67893d1
SE
1346 packet_pos = packet_buff + buff_pos;
1347 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
b47506d9 1348 }
c3e29312
ML
1349
1350 kfree_skb(skb);
1351 return NET_RX_SUCCESS;
fc957275 1352}
1c280471 1353
56303d34 1354static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
519d3497 1355 .name = "BATMAN_IV",
fe8bc396
SE
1356 .bat_iface_enable = batadv_iv_ogm_iface_enable,
1357 .bat_iface_disable = batadv_iv_ogm_iface_disable,
1358 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
1359 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
1360 .bat_ogm_schedule = batadv_iv_ogm_schedule,
1361 .bat_ogm_emit = batadv_iv_ogm_emit,
1c280471
ML
1362};
1363
81c524f7 1364int __init batadv_iv_init(void)
1c280471 1365{
c3e29312
ML
1366 int ret;
1367
1368 /* batman originator packet */
acd34afa
SE
1369 ret = batadv_recv_handler_register(BATADV_IV_OGM,
1370 batadv_iv_ogm_receive);
c3e29312
ML
1371 if (ret < 0)
1372 goto out;
1373
fe8bc396 1374 ret = batadv_algo_register(&batadv_batman_iv);
c3e29312
ML
1375 if (ret < 0)
1376 goto handler_unregister;
1377
1378 goto out;
1379
1380handler_unregister:
acd34afa 1381 batadv_recv_handler_unregister(BATADV_IV_OGM);
c3e29312
ML
1382out:
1383 return ret;
1c280471 1384}