]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/batman-adv/bat_iv_ogm.c
batman-adv: Prefix main non-static functions with batadv_
[mirror_ubuntu-jammy-kernel.git] / net / batman-adv / bat_iv_ogm.c
CommitLineData
fc957275 1/*
567db7b0 2 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
fc957275
ML
3 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
fc957275
ML
23#include "translation-table.h"
24#include "ring_buffer.h"
25#include "originator.h"
26#include "routing.h"
27#include "gateway_common.h"
28#include "gateway_client.h"
29#include "hard-interface.h"
30#include "send.h"
1c280471 31#include "bat_algo.h"
fc957275 32
7ae8b285
ML
33static struct neigh_node *bat_iv_ogm_neigh_new(struct hard_iface *hard_iface,
34 const uint8_t *neigh_addr,
35 struct orig_node *orig_node,
36 struct orig_node *orig_neigh,
e0f5211f 37 __be32 seqno)
7ae8b285
ML
38{
39 struct neigh_node *neigh_node;
40
e0f5211f
AV
41 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr,
42 ntohl(seqno));
7ae8b285
ML
43 if (!neigh_node)
44 goto out;
45
46 INIT_LIST_HEAD(&neigh_node->bonding_list);
7ae8b285
ML
47
48 neigh_node->orig_node = orig_neigh;
49 neigh_node->if_incoming = hard_iface;
50
51 spin_lock_bh(&orig_node->neigh_list_lock);
52 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
53 spin_unlock_bh(&orig_node->neigh_list_lock);
54
55out:
56 return neigh_node;
57}
58
77af7575 59static int bat_iv_ogm_iface_enable(struct hard_iface *hard_iface)
d0b9fd89
ML
60{
61 struct batman_ogm_packet *batman_ogm_packet;
d7d32ec0 62 uint32_t random_seqno;
5346c35e 63 int res = -ENOMEM;
d7d32ec0
ML
64
65 /* randomize initial seqno to avoid collision */
66 get_random_bytes(&random_seqno, sizeof(random_seqno));
67 atomic_set(&hard_iface->seqno, random_seqno);
d0b9fd89 68
76e3d7fc 69 hard_iface->packet_len = BATMAN_OGM_HLEN;
d0b9fd89
ML
70 hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
71
77af7575
ML
72 if (!hard_iface->packet_buff)
73 goto out;
74
d0b9fd89 75 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
1eeb479f 76 batman_ogm_packet->header.packet_type = BAT_IV_OGM;
76543d14
SE
77 batman_ogm_packet->header.version = COMPAT_VERSION;
78 batman_ogm_packet->header.ttl = 2;
d0b9fd89 79 batman_ogm_packet->flags = NO_FLAGS;
d0b9fd89
ML
80 batman_ogm_packet->tq = TQ_MAX_VALUE;
81 batman_ogm_packet->tt_num_changes = 0;
82 batman_ogm_packet->ttvn = 0;
77af7575
ML
83
84 res = 0;
85
86out:
87 return res;
d0b9fd89
ML
88}
89
00a50076
ML
90static void bat_iv_ogm_iface_disable(struct hard_iface *hard_iface)
91{
92 kfree(hard_iface->packet_buff);
93 hard_iface->packet_buff = NULL;
94}
95
c3229398 96static void bat_iv_ogm_iface_update_mac(struct hard_iface *hard_iface)
d0b9fd89
ML
97{
98 struct batman_ogm_packet *batman_ogm_packet;
99
100 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
c3229398
ML
101 memcpy(batman_ogm_packet->orig,
102 hard_iface->net_dev->dev_addr, ETH_ALEN);
103 memcpy(batman_ogm_packet->prev_sender,
104 hard_iface->net_dev->dev_addr, ETH_ALEN);
d0b9fd89
ML
105}
106
c3229398 107static void bat_iv_ogm_primary_iface_set(struct hard_iface *hard_iface)
d0b9fd89
ML
108{
109 struct batman_ogm_packet *batman_ogm_packet;
110
111 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
c3229398
ML
112 batman_ogm_packet->flags = PRIMARIES_FIRST_HOP;
113 batman_ogm_packet->header.ttl = TTL;
d0b9fd89
ML
114}
115
b9dacc52 116/* when do we schedule our own ogm to be sent */
01c4224b 117static unsigned long bat_iv_ogm_emit_send_time(const struct bat_priv *bat_priv)
b9dacc52
ML
118{
119 return jiffies + msecs_to_jiffies(
120 atomic_read(&bat_priv->orig_interval) -
121 JITTER + (random32() % 2*JITTER));
122}
123
124/* when do we schedule a ogm packet to be sent */
01c4224b 125static unsigned long bat_iv_ogm_fwd_send_time(void)
b9dacc52
ML
126{
127 return jiffies + msecs_to_jiffies(random32() % (JITTER/2));
128}
129
130/* apply hop penalty for a normal link */
131static uint8_t hop_penalty(uint8_t tq, const struct bat_priv *bat_priv)
132{
133 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
134 return (tq * (TQ_MAX_VALUE - hop_penalty)) / (TQ_MAX_VALUE);
135}
136
fc957275 137/* is there another aggregated packet here? */
01c4224b
ML
138static int bat_iv_ogm_aggr_packet(int buff_pos, int packet_len,
139 int tt_num_changes)
fc957275 140{
08c36d3e
SE
141 int next_buff_pos = 0;
142
143 next_buff_pos += buff_pos + BATMAN_OGM_HLEN;
144 next_buff_pos += batadv_tt_len(tt_num_changes);
fc957275
ML
145
146 return (next_buff_pos <= packet_len) &&
147 (next_buff_pos <= MAX_AGGREGATION_BYTES);
148}
149
b9dacc52 150/* send a batman ogm to a given interface */
01c4224b
ML
151static void bat_iv_ogm_send_to_if(struct forw_packet *forw_packet,
152 struct hard_iface *hard_iface)
b9dacc52
ML
153{
154 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
155 char *fwd_str;
156 uint8_t packet_num;
157 int16_t buff_pos;
158 struct batman_ogm_packet *batman_ogm_packet;
159 struct sk_buff *skb;
160
161 if (hard_iface->if_status != IF_ACTIVE)
162 return;
163
164 packet_num = 0;
165 buff_pos = 0;
166 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
167
168 /* adjust all flags and log packets */
01c4224b
ML
169 while (bat_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
170 batman_ogm_packet->tt_num_changes)) {
b9dacc52
ML
171
172 /* we might have aggregated direct link packets with an
173 * ordinary base packet */
174 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
175 (forw_packet->if_incoming == hard_iface))
176 batman_ogm_packet->flags |= DIRECTLINK;
177 else
178 batman_ogm_packet->flags &= ~DIRECTLINK;
179
180 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
181 "Sending own" :
182 "Forwarding"));
183 bat_dbg(DBG_BATMAN, bat_priv,
c97c72b4 184 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n",
b9dacc52
ML
185 fwd_str, (packet_num > 0 ? "aggregated " : ""),
186 batman_ogm_packet->orig,
187 ntohl(batman_ogm_packet->seqno),
76543d14 188 batman_ogm_packet->tq, batman_ogm_packet->header.ttl,
b9dacc52
ML
189 (batman_ogm_packet->flags & DIRECTLINK ?
190 "on" : "off"),
191 batman_ogm_packet->ttvn, hard_iface->net_dev->name,
192 hard_iface->net_dev->dev_addr);
193
08c36d3e
SE
194 buff_pos += BATMAN_OGM_HLEN;
195 buff_pos += batadv_tt_len(batman_ogm_packet->tt_num_changes);
b9dacc52
ML
196 packet_num++;
197 batman_ogm_packet = (struct batman_ogm_packet *)
198 (forw_packet->skb->data + buff_pos);
199 }
200
201 /* create clone because function is called more than once */
202 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
f8214865
MH
203 if (skb) {
204 batadv_inc_counter(bat_priv, BAT_CNT_MGMT_TX);
205 batadv_add_counter(bat_priv, BAT_CNT_MGMT_TX_BYTES,
206 skb->len + ETH_HLEN);
3193e8fd 207 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
f8214865 208 }
b9dacc52
ML
209}
210
211/* send a batman ogm packet */
01c4224b 212static void bat_iv_ogm_emit(struct forw_packet *forw_packet)
b9dacc52
ML
213{
214 struct hard_iface *hard_iface;
215 struct net_device *soft_iface;
216 struct bat_priv *bat_priv;
217 struct hard_iface *primary_if = NULL;
218 struct batman_ogm_packet *batman_ogm_packet;
219 unsigned char directlink;
220
221 batman_ogm_packet = (struct batman_ogm_packet *)
222 (forw_packet->skb->data);
223 directlink = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
224
225 if (!forw_packet->if_incoming) {
86ceb360 226 pr_err("Error - can't forward packet: incoming iface not specified\n");
b9dacc52
ML
227 goto out;
228 }
229
230 soft_iface = forw_packet->if_incoming->soft_iface;
231 bat_priv = netdev_priv(soft_iface);
232
233 if (forw_packet->if_incoming->if_status != IF_ACTIVE)
234 goto out;
235
236 primary_if = primary_if_get_selected(bat_priv);
237 if (!primary_if)
238 goto out;
239
240 /* multihomed peer assumed */
241 /* non-primary OGMs are only broadcasted on their interface */
76543d14 242 if ((directlink && (batman_ogm_packet->header.ttl == 1)) ||
b9dacc52
ML
243 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
244
245 /* FIXME: what about aggregated packets ? */
246 bat_dbg(DBG_BATMAN, bat_priv,
c97c72b4 247 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
b9dacc52
ML
248 (forw_packet->own ? "Sending own" : "Forwarding"),
249 batman_ogm_packet->orig,
250 ntohl(batman_ogm_packet->seqno),
76543d14 251 batman_ogm_packet->header.ttl,
b9dacc52
ML
252 forw_packet->if_incoming->net_dev->name,
253 forw_packet->if_incoming->net_dev->dev_addr);
254
255 /* skb is only used once and than forw_packet is free'd */
9455e34c
SE
256 batadv_send_skb_packet(forw_packet->skb,
257 forw_packet->if_incoming,
3193e8fd 258 batadv_broadcast_addr);
b9dacc52
ML
259 forw_packet->skb = NULL;
260
261 goto out;
262 }
263
264 /* broadcast on every interface */
265 rcu_read_lock();
3193e8fd 266 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
b9dacc52
ML
267 if (hard_iface->soft_iface != soft_iface)
268 continue;
269
01c4224b 270 bat_iv_ogm_send_to_if(forw_packet, hard_iface);
b9dacc52
ML
271 }
272 rcu_read_unlock();
273
274out:
275 if (primary_if)
276 hardif_free_ref(primary_if);
277}
278
279/* return true if new_packet can be aggregated with forw_packet */
01c4224b 280static bool bat_iv_ogm_can_aggregate(const struct batman_ogm_packet
b9dacc52 281 *new_batman_ogm_packet,
01c4224b
ML
282 struct bat_priv *bat_priv,
283 int packet_len, unsigned long send_time,
284 bool directlink,
285 const struct hard_iface *if_incoming,
286 const struct forw_packet *forw_packet)
b9dacc52
ML
287{
288 struct batman_ogm_packet *batman_ogm_packet;
289 int aggregated_bytes = forw_packet->packet_len + packet_len;
290 struct hard_iface *primary_if = NULL;
291 bool res = false;
292
293 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
294
295 /**
296 * we can aggregate the current packet to this aggregated packet
297 * if:
298 *
299 * - the send time is within our MAX_AGGREGATION_MS time
300 * - the resulting packet wont be bigger than
301 * MAX_AGGREGATION_BYTES
302 */
303
304 if (time_before(send_time, forw_packet->send_time) &&
305 time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
306 forw_packet->send_time) &&
307 (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
308
309 /**
310 * check aggregation compatibility
311 * -> direct link packets are broadcasted on
312 * their interface only
313 * -> aggregate packet if the current packet is
314 * a "global" packet as well as the base
315 * packet
316 */
317
318 primary_if = primary_if_get_selected(bat_priv);
319 if (!primary_if)
320 goto out;
321
322 /* packets without direct link flag and high TTL
323 * are flooded through the net */
324 if ((!directlink) &&
325 (!(batman_ogm_packet->flags & DIRECTLINK)) &&
76543d14 326 (batman_ogm_packet->header.ttl != 1) &&
b9dacc52
ML
327
328 /* own packets originating non-primary
329 * interfaces leave only that interface */
330 ((!forw_packet->own) ||
331 (forw_packet->if_incoming == primary_if))) {
332 res = true;
333 goto out;
334 }
335
336 /* if the incoming packet is sent via this one
337 * interface only - we still can aggregate */
338 if ((directlink) &&
76543d14 339 (new_batman_ogm_packet->header.ttl == 1) &&
b9dacc52
ML
340 (forw_packet->if_incoming == if_incoming) &&
341
342 /* packets from direct neighbors or
343 * own secondary interface packets
344 * (= secondary interface packets in general) */
345 (batman_ogm_packet->flags & DIRECTLINK ||
346 (forw_packet->own &&
347 forw_packet->if_incoming != primary_if))) {
348 res = true;
349 goto out;
350 }
351 }
352
353out:
354 if (primary_if)
355 hardif_free_ref(primary_if);
356 return res;
357}
358
359/* create a new aggregated packet and add this packet to it */
01c4224b
ML
360static void bat_iv_ogm_aggregate_new(const unsigned char *packet_buff,
361 int packet_len, unsigned long send_time,
362 bool direct_link,
363 struct hard_iface *if_incoming,
364 int own_packet)
b9dacc52
ML
365{
366 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
367 struct forw_packet *forw_packet_aggr;
368 unsigned char *skb_buff;
369
370 if (!atomic_inc_not_zero(&if_incoming->refcount))
371 return;
372
373 /* own packet should always be scheduled */
374 if (!own_packet) {
375 if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
376 bat_dbg(DBG_BATMAN, bat_priv,
377 "batman packet queue full\n");
378 goto out;
379 }
380 }
381
382 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
383 if (!forw_packet_aggr) {
384 if (!own_packet)
385 atomic_inc(&bat_priv->batman_queue_left);
386 goto out;
387 }
388
389 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
390 (packet_len < MAX_AGGREGATION_BYTES))
391 forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
0d125074 392 ETH_HLEN);
b9dacc52 393 else
0d125074 394 forw_packet_aggr->skb = dev_alloc_skb(packet_len + ETH_HLEN);
b9dacc52
ML
395
396 if (!forw_packet_aggr->skb) {
397 if (!own_packet)
398 atomic_inc(&bat_priv->batman_queue_left);
399 kfree(forw_packet_aggr);
400 goto out;
401 }
0d125074 402 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
b9dacc52
ML
403
404 INIT_HLIST_NODE(&forw_packet_aggr->list);
405
406 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
407 forw_packet_aggr->packet_len = packet_len;
408 memcpy(skb_buff, packet_buff, packet_len);
409
410 forw_packet_aggr->own = own_packet;
411 forw_packet_aggr->if_incoming = if_incoming;
412 forw_packet_aggr->num_packets = 0;
413 forw_packet_aggr->direct_link_flags = NO_FLAGS;
414 forw_packet_aggr->send_time = send_time;
415
416 /* save packet direct link flag status */
417 if (direct_link)
418 forw_packet_aggr->direct_link_flags |= 1;
419
420 /* add new packet to packet list */
421 spin_lock_bh(&bat_priv->forw_bat_list_lock);
422 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
423 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
424
425 /* start timer for this packet */
426 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
9455e34c 427 batadv_send_outstanding_bat_ogm_packet);
3193e8fd 428 queue_delayed_work(batadv_event_workqueue,
b9dacc52
ML
429 &forw_packet_aggr->delayed_work,
430 send_time - jiffies);
431
432 return;
433out:
434 hardif_free_ref(if_incoming);
435}
436
437/* aggregate a new packet into the existing ogm packet */
01c4224b
ML
438static void bat_iv_ogm_aggregate(struct forw_packet *forw_packet_aggr,
439 const unsigned char *packet_buff,
440 int packet_len, bool direct_link)
b9dacc52
ML
441{
442 unsigned char *skb_buff;
443
444 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
445 memcpy(skb_buff, packet_buff, packet_len);
446 forw_packet_aggr->packet_len += packet_len;
447 forw_packet_aggr->num_packets++;
448
449 /* save packet direct link flag status */
450 if (direct_link)
451 forw_packet_aggr->direct_link_flags |=
452 (1 << forw_packet_aggr->num_packets);
453}
454
01c4224b
ML
455static void bat_iv_ogm_queue_add(struct bat_priv *bat_priv,
456 unsigned char *packet_buff,
457 int packet_len, struct hard_iface *if_incoming,
458 int own_packet, unsigned long send_time)
b9dacc52
ML
459{
460 /**
461 * _aggr -> pointer to the packet we want to aggregate with
462 * _pos -> pointer to the position in the queue
463 */
464 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
465 struct hlist_node *tmp_node;
466 struct batman_ogm_packet *batman_ogm_packet;
467 bool direct_link;
468
469 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
470 direct_link = batman_ogm_packet->flags & DIRECTLINK ? 1 : 0;
471
472 /* find position for the packet in the forward queue */
473 spin_lock_bh(&bat_priv->forw_bat_list_lock);
474 /* own packets are not to be aggregated */
475 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
476 hlist_for_each_entry(forw_packet_pos, tmp_node,
477 &bat_priv->forw_bat_list, list) {
01c4224b
ML
478 if (bat_iv_ogm_can_aggregate(batman_ogm_packet,
479 bat_priv, packet_len,
480 send_time, direct_link,
481 if_incoming,
482 forw_packet_pos)) {
b9dacc52
ML
483 forw_packet_aggr = forw_packet_pos;
484 break;
485 }
486 }
487 }
488
489 /* nothing to aggregate with - either aggregation disabled or no
490 * suitable aggregation packet found */
491 if (!forw_packet_aggr) {
492 /* the following section can run without the lock */
493 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
494
495 /**
496 * if we could not aggregate this packet with one of the others
497 * we hold it back for a while, so that it might be aggregated
498 * later on
499 */
500 if ((!own_packet) &&
501 (atomic_read(&bat_priv->aggregated_ogms)))
502 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
503
01c4224b
ML
504 bat_iv_ogm_aggregate_new(packet_buff, packet_len,
505 send_time, direct_link,
506 if_incoming, own_packet);
b9dacc52 507 } else {
01c4224b
ML
508 bat_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
509 packet_len, direct_link);
b9dacc52
ML
510 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
511 }
512}
513
01c4224b
ML
514static void bat_iv_ogm_forward(struct orig_node *orig_node,
515 const struct ethhdr *ethhdr,
516 struct batman_ogm_packet *batman_ogm_packet,
75cd33f8 517 bool is_single_hop_neigh,
13b2541b 518 bool is_from_best_next_hop,
75cd33f8 519 struct hard_iface *if_incoming)
b9dacc52
ML
520{
521 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
b9dacc52
ML
522 uint8_t tt_num_changes;
523
76543d14 524 if (batman_ogm_packet->header.ttl <= 1) {
b9dacc52
ML
525 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
526 return;
527 }
528
13b2541b
ML
529 if (!is_from_best_next_hop) {
530 /* Mark the forwarded packet when it is not coming from our
531 * best next hop. We still need to forward the packet for our
532 * neighbor link quality detection to work in case the packet
533 * originated from a single hop neighbor. Otherwise we can
534 * simply drop the ogm.
535 */
536 if (is_single_hop_neigh)
537 batman_ogm_packet->flags |= NOT_BEST_NEXT_HOP;
538 else
539 return;
540 }
b9dacc52 541
b9dacc52
ML
542 tt_num_changes = batman_ogm_packet->tt_num_changes;
543
76543d14 544 batman_ogm_packet->header.ttl--;
b9dacc52
ML
545 memcpy(batman_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
546
b9dacc52
ML
547 /* apply hop penalty */
548 batman_ogm_packet->tq = hop_penalty(batman_ogm_packet->tq, bat_priv);
549
550 bat_dbg(DBG_BATMAN, bat_priv,
13b2541b
ML
551 "Forwarding packet: tq: %i, ttl: %i\n",
552 batman_ogm_packet->tq, batman_ogm_packet->header.ttl);
b9dacc52 553
b9dacc52
ML
554 /* switch of primaries first hop flag when forwarding */
555 batman_ogm_packet->flags &= ~PRIMARIES_FIRST_HOP;
75cd33f8 556 if (is_single_hop_neigh)
b9dacc52
ML
557 batman_ogm_packet->flags |= DIRECTLINK;
558 else
559 batman_ogm_packet->flags &= ~DIRECTLINK;
560
01c4224b 561 bat_iv_ogm_queue_add(bat_priv, (unsigned char *)batman_ogm_packet,
08c36d3e 562 BATMAN_OGM_HLEN + batadv_tt_len(tt_num_changes),
01c4224b 563 if_incoming, 0, bat_iv_ogm_fwd_send_time());
b9dacc52
ML
564}
565
be9aa4c1 566static void bat_iv_ogm_schedule(struct hard_iface *hard_iface)
b9dacc52
ML
567{
568 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
569 struct batman_ogm_packet *batman_ogm_packet;
570 struct hard_iface *primary_if;
be9aa4c1 571 int vis_server, tt_num_changes = 0;
b9dacc52
ML
572
573 vis_server = atomic_read(&bat_priv->vis_mode);
574 primary_if = primary_if_get_selected(bat_priv);
575
be9aa4c1
ML
576 if (hard_iface == primary_if)
577 tt_num_changes = batadv_tt_append_diff(bat_priv,
578 &hard_iface->packet_buff,
579 &hard_iface->packet_len,
580 BATMAN_OGM_HLEN);
581
b9dacc52
ML
582 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
583
584 /* change sequence number to network order */
585 batman_ogm_packet->seqno =
586 htonl((uint32_t)atomic_read(&hard_iface->seqno));
be9aa4c1 587 atomic_inc(&hard_iface->seqno);
b9dacc52
ML
588
589 batman_ogm_packet->ttvn = atomic_read(&bat_priv->ttvn);
66a1b2bc 590 batman_ogm_packet->tt_crc = htons(bat_priv->tt_crc);
b9dacc52
ML
591 if (tt_num_changes >= 0)
592 batman_ogm_packet->tt_num_changes = tt_num_changes;
593
594 if (vis_server == VIS_TYPE_SERVER_SYNC)
595 batman_ogm_packet->flags |= VIS_SERVER;
596 else
597 batman_ogm_packet->flags &= ~VIS_SERVER;
598
599 if ((hard_iface == primary_if) &&
600 (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER))
601 batman_ogm_packet->gw_flags =
602 (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
603 else
604 batman_ogm_packet->gw_flags = NO_FLAGS;
605
30d3c511 606 batadv_slide_own_bcast_window(hard_iface);
01c4224b
ML
607 bat_iv_ogm_queue_add(bat_priv, hard_iface->packet_buff,
608 hard_iface->packet_len, hard_iface, 1,
609 bat_iv_ogm_emit_send_time(bat_priv));
b9dacc52
ML
610
611 if (primary_if)
612 hardif_free_ref(primary_if);
613}
614
01c4224b
ML
615static void bat_iv_ogm_orig_update(struct bat_priv *bat_priv,
616 struct orig_node *orig_node,
617 const struct ethhdr *ethhdr,
618 const struct batman_ogm_packet
fc957275 619 *batman_ogm_packet,
01c4224b
ML
620 struct hard_iface *if_incoming,
621 const unsigned char *tt_buff,
622 int is_duplicate)
fc957275
ML
623{
624 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
625 struct neigh_node *router = NULL;
626 struct orig_node *orig_node_tmp;
627 struct hlist_node *node;
628 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
629
86ceb360
SE
630 bat_dbg(DBG_BATMAN, bat_priv,
631 "update_originator(): Searching and updating originator entry of received packet\n");
fc957275
ML
632
633 rcu_read_lock();
634 hlist_for_each_entry_rcu(tmp_neigh_node, node,
635 &orig_node->neigh_list, list) {
636 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
637 (tmp_neigh_node->if_incoming == if_incoming) &&
638 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
639 if (neigh_node)
7d211efc 640 batadv_neigh_node_free_ref(neigh_node);
fc957275
ML
641 neigh_node = tmp_neigh_node;
642 continue;
643 }
644
645 if (is_duplicate)
646 continue;
647
e3b0d0de 648 spin_lock_bh(&tmp_neigh_node->lq_update_lock);
925a6672
SE
649 batadv_ring_buffer_set(tmp_neigh_node->tq_recv,
650 &tmp_neigh_node->tq_index, 0);
fc957275 651 tmp_neigh_node->tq_avg =
925a6672 652 batadv_ring_buffer_avg(tmp_neigh_node->tq_recv);
e3b0d0de 653 spin_unlock_bh(&tmp_neigh_node->lq_update_lock);
fc957275
ML
654 }
655
656 if (!neigh_node) {
657 struct orig_node *orig_tmp;
658
7d211efc 659 orig_tmp = batadv_get_orig_node(bat_priv, ethhdr->h_source);
fc957275
ML
660 if (!orig_tmp)
661 goto unlock;
662
7ae8b285
ML
663 neigh_node = bat_iv_ogm_neigh_new(if_incoming, ethhdr->h_source,
664 orig_node, orig_tmp,
665 batman_ogm_packet->seqno);
fc957275 666
7d211efc 667 batadv_orig_node_free_ref(orig_tmp);
fc957275
ML
668 if (!neigh_node)
669 goto unlock;
670 } else
671 bat_dbg(DBG_BATMAN, bat_priv,
672 "Updating existing last-hop neighbor of originator\n");
673
674 rcu_read_unlock();
675
676 orig_node->flags = batman_ogm_packet->flags;
d7b2a97e 677 neigh_node->last_seen = jiffies;
fc957275 678
e3b0d0de 679 spin_lock_bh(&neigh_node->lq_update_lock);
925a6672
SE
680 batadv_ring_buffer_set(neigh_node->tq_recv,
681 &neigh_node->tq_index,
682 batman_ogm_packet->tq);
683 neigh_node->tq_avg = batadv_ring_buffer_avg(neigh_node->tq_recv);
e3b0d0de 684 spin_unlock_bh(&neigh_node->lq_update_lock);
fc957275
ML
685
686 if (!is_duplicate) {
76543d14
SE
687 orig_node->last_ttl = batman_ogm_packet->header.ttl;
688 neigh_node->last_ttl = batman_ogm_packet->header.ttl;
fc957275
ML
689 }
690
30d3c511 691 batadv_bonding_candidate_add(orig_node, neigh_node);
fc957275
ML
692
693 /* if this neighbor already is our next hop there is nothing
694 * to change */
7d211efc 695 router = batadv_orig_node_get_router(orig_node);
fc957275
ML
696 if (router == neigh_node)
697 goto update_tt;
698
699 /* if this neighbor does not offer a better TQ we won't consider it */
700 if (router && (router->tq_avg > neigh_node->tq_avg))
701 goto update_tt;
702
703 /* if the TQ is the same and the link not more symmetric we
704 * won't consider it either */
705 if (router && (neigh_node->tq_avg == router->tq_avg)) {
706 orig_node_tmp = router->orig_node;
707 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
708 bcast_own_sum_orig =
709 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
710 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
711
712 orig_node_tmp = neigh_node->orig_node;
713 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
714 bcast_own_sum_neigh =
715 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
716 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
717
718 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
719 goto update_tt;
720 }
721
30d3c511 722 batadv_update_route(bat_priv, orig_node, neigh_node);
fc957275
ML
723
724update_tt:
725 /* I have to check for transtable changes only if the OGM has been
726 * sent through a primary interface */
727 if (((batman_ogm_packet->orig != ethhdr->h_source) &&
76543d14 728 (batman_ogm_packet->header.ttl > 2)) ||
fc957275 729 (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
08c36d3e
SE
730 batadv_tt_update_orig(bat_priv, orig_node, tt_buff,
731 batman_ogm_packet->tt_num_changes,
732 batman_ogm_packet->ttvn,
733 ntohs(batman_ogm_packet->tt_crc));
fc957275
ML
734
735 if (orig_node->gw_flags != batman_ogm_packet->gw_flags)
7cf06bc6
SE
736 batadv_gw_node_update(bat_priv, orig_node,
737 batman_ogm_packet->gw_flags);
fc957275
ML
738
739 orig_node->gw_flags = batman_ogm_packet->gw_flags;
740
741 /* restart gateway selection if fast or late switching was enabled */
742 if ((orig_node->gw_flags) &&
743 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
744 (atomic_read(&bat_priv->gw_sel_class) > 2))
7cf06bc6 745 batadv_gw_check_election(bat_priv, orig_node);
fc957275
ML
746
747 goto out;
748
749unlock:
750 rcu_read_unlock();
751out:
752 if (neigh_node)
7d211efc 753 batadv_neigh_node_free_ref(neigh_node);
fc957275 754 if (router)
7d211efc 755 batadv_neigh_node_free_ref(router);
fc957275
ML
756}
757
01c4224b
ML
758static int bat_iv_ogm_calc_tq(struct orig_node *orig_node,
759 struct orig_node *orig_neigh_node,
760 struct batman_ogm_packet *batman_ogm_packet,
761 struct hard_iface *if_incoming)
fc957275
ML
762{
763 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
764 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
765 struct hlist_node *node;
766 uint8_t total_count;
767 uint8_t orig_eq_count, neigh_rq_count, tq_own;
768 int tq_asym_penalty, ret = 0;
769
770 /* find corresponding one hop neighbor */
771 rcu_read_lock();
772 hlist_for_each_entry_rcu(tmp_neigh_node, node,
773 &orig_neigh_node->neigh_list, list) {
774
775 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
776 continue;
777
778 if (tmp_neigh_node->if_incoming != if_incoming)
779 continue;
780
781 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
782 continue;
783
784 neigh_node = tmp_neigh_node;
785 break;
786 }
787 rcu_read_unlock();
788
789 if (!neigh_node)
7ae8b285
ML
790 neigh_node = bat_iv_ogm_neigh_new(if_incoming,
791 orig_neigh_node->orig,
792 orig_neigh_node,
793 orig_neigh_node,
794 batman_ogm_packet->seqno);
fc957275
ML
795
796 if (!neigh_node)
797 goto out;
798
d7b2a97e 799 /* if orig_node is direct neighbor update neigh_node last_seen */
fc957275 800 if (orig_node == orig_neigh_node)
d7b2a97e 801 neigh_node->last_seen = jiffies;
fc957275 802
d7b2a97e 803 orig_node->last_seen = jiffies;
fc957275
ML
804
805 /* find packet count of corresponding one hop neighbor */
806 spin_lock_bh(&orig_node->ogm_cnt_lock);
807 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
808 neigh_rq_count = neigh_node->real_packet_count;
809 spin_unlock_bh(&orig_node->ogm_cnt_lock);
810
811 /* pay attention to not get a value bigger than 100 % */
812 total_count = (orig_eq_count > neigh_rq_count ?
813 neigh_rq_count : orig_eq_count);
814
815 /* if we have too few packets (too less data) we set tq_own to zero */
816 /* if we receive too few packets it is not considered bidirectional */
817 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
818 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
819 tq_own = 0;
820 else
821 /* neigh_node->real_packet_count is never zero as we
822 * only purge old information when getting new
823 * information */
824 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
825
21a1236b 826 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
fc957275
ML
827 * affect the nearly-symmetric links only a little, but
828 * punishes asymmetric links more. This will give a value
829 * between 0 and TQ_MAX_VALUE
830 */
831 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
832 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
833 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
834 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
835 (TQ_LOCAL_WINDOW_SIZE *
836 TQ_LOCAL_WINDOW_SIZE *
837 TQ_LOCAL_WINDOW_SIZE);
838
839 batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own
840 * tq_asym_penalty) /
841 (TQ_MAX_VALUE * TQ_MAX_VALUE));
842
843 bat_dbg(DBG_BATMAN, bat_priv,
86ceb360 844 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
fc957275
ML
845 orig_node->orig, orig_neigh_node->orig, total_count,
846 neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq);
847
848 /* if link has the minimum required transmission quality
849 * consider it bidirectional */
850 if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
851 ret = 1;
852
853out:
854 if (neigh_node)
7d211efc 855 batadv_neigh_node_free_ref(neigh_node);
fc957275
ML
856 return ret;
857}
858
859/* processes a batman packet for all interfaces, adjusts the sequence number and
860 * finds out whether it is a duplicate.
861 * returns:
862 * 1 the packet is a duplicate
863 * 0 the packet has not yet been received
864 * -1 the packet is old and has been received while the seqno window
865 * was protected. Caller should drop it.
866 */
01c4224b
ML
867static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
868 const struct batman_ogm_packet
fc957275 869 *batman_ogm_packet,
01c4224b 870 const struct hard_iface *if_incoming)
fc957275
ML
871{
872 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
873 struct orig_node *orig_node;
874 struct neigh_node *tmp_neigh_node;
875 struct hlist_node *node;
876 int is_duplicate = 0;
877 int32_t seq_diff;
878 int need_update = 0;
879 int set_mark, ret = -1;
e0f5211f 880 uint32_t seqno = ntohl(batman_ogm_packet->seqno);
fc957275 881
7d211efc 882 orig_node = batadv_get_orig_node(bat_priv, batman_ogm_packet->orig);
fc957275
ML
883 if (!orig_node)
884 return 0;
885
886 spin_lock_bh(&orig_node->ogm_cnt_lock);
e0f5211f 887 seq_diff = seqno - orig_node->last_real_seqno;
fc957275
ML
888
889 /* signalize caller that the packet is to be dropped. */
1e5cc266 890 if (!hlist_empty(&orig_node->neigh_list) &&
30d3c511
SE
891 batadv_window_protected(bat_priv, seq_diff,
892 &orig_node->batman_seqno_reset))
fc957275
ML
893 goto out;
894
895 rcu_read_lock();
896 hlist_for_each_entry_rcu(tmp_neigh_node, node,
897 &orig_node->neigh_list, list) {
898
0079d2ce
SE
899 is_duplicate |= bat_test_bit(tmp_neigh_node->real_bits,
900 orig_node->last_real_seqno,
e0f5211f 901 seqno);
fc957275
ML
902
903 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
904 (tmp_neigh_node->if_incoming == if_incoming))
905 set_mark = 1;
906 else
907 set_mark = 0;
908
909 /* if the window moved, set the update flag. */
0f5f9322
SE
910 need_update |= batadv_bit_get_packet(bat_priv,
911 tmp_neigh_node->real_bits,
912 seq_diff, set_mark);
fc957275
ML
913
914 tmp_neigh_node->real_packet_count =
0079d2ce
SE
915 bitmap_weight(tmp_neigh_node->real_bits,
916 TQ_LOCAL_WINDOW_SIZE);
fc957275
ML
917 }
918 rcu_read_unlock();
919
920 if (need_update) {
921 bat_dbg(DBG_BATMAN, bat_priv,
c97c72b4 922 "updating last_seqno: old %u, new %u\n",
e0f5211f
AV
923 orig_node->last_real_seqno, seqno);
924 orig_node->last_real_seqno = seqno;
fc957275
ML
925 }
926
927 ret = is_duplicate;
928
929out:
930 spin_unlock_bh(&orig_node->ogm_cnt_lock);
7d211efc 931 batadv_orig_node_free_ref(orig_node);
fc957275
ML
932 return ret;
933}
934
01c4224b
ML
935static void bat_iv_ogm_process(const struct ethhdr *ethhdr,
936 struct batman_ogm_packet *batman_ogm_packet,
937 const unsigned char *tt_buff,
938 struct hard_iface *if_incoming)
fc957275
ML
939{
940 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
941 struct hard_iface *hard_iface;
942 struct orig_node *orig_neigh_node, *orig_node;
943 struct neigh_node *router = NULL, *router_router = NULL;
944 struct neigh_node *orig_neigh_router = NULL;
945 int has_directlink_flag;
946 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
75cd33f8
ML
947 int is_broadcast = 0, is_bidirectional;
948 bool is_single_hop_neigh = false;
13b2541b 949 bool is_from_best_next_hop = false;
fc957275
ML
950 int is_duplicate;
951 uint32_t if_incoming_seqno;
952
953 /* Silently drop when the batman packet is actually not a
954 * correct packet.
955 *
956 * This might happen if a packet is padded (e.g. Ethernet has a
957 * minimum frame length of 64 byte) and the aggregation interprets
958 * it as an additional length.
959 *
960 * TODO: A more sane solution would be to have a bit in the
961 * batman_ogm_packet to detect whether the packet is the last
962 * packet in an aggregation. Here we expect that the padding
963 * is always zero (or not 0x01)
964 */
1eeb479f 965 if (batman_ogm_packet->header.packet_type != BAT_IV_OGM)
fc957275
ML
966 return;
967
968 /* could be changed by schedule_own_packet() */
969 if_incoming_seqno = atomic_read(&if_incoming->seqno);
970
971 has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
972
75cd33f8
ML
973 if (compare_eth(ethhdr->h_source, batman_ogm_packet->orig))
974 is_single_hop_neigh = true;
fc957275
ML
975
976 bat_dbg(DBG_BATMAN, bat_priv,
c97c72b4 977 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, ttvn %u, crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
fc957275
ML
978 ethhdr->h_source, if_incoming->net_dev->name,
979 if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
e0f5211f 980 batman_ogm_packet->prev_sender, ntohl(batman_ogm_packet->seqno),
16a70345 981 batman_ogm_packet->ttvn, ntohs(batman_ogm_packet->tt_crc),
fc957275 982 batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
76543d14
SE
983 batman_ogm_packet->header.ttl,
984 batman_ogm_packet->header.version, has_directlink_flag);
fc957275
ML
985
986 rcu_read_lock();
3193e8fd 987 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
fc957275
ML
988 if (hard_iface->if_status != IF_ACTIVE)
989 continue;
990
991 if (hard_iface->soft_iface != if_incoming->soft_iface)
992 continue;
993
994 if (compare_eth(ethhdr->h_source,
995 hard_iface->net_dev->dev_addr))
996 is_my_addr = 1;
997
998 if (compare_eth(batman_ogm_packet->orig,
999 hard_iface->net_dev->dev_addr))
1000 is_my_orig = 1;
1001
1002 if (compare_eth(batman_ogm_packet->prev_sender,
1003 hard_iface->net_dev->dev_addr))
1004 is_my_oldorig = 1;
1005
1006 if (is_broadcast_ether_addr(ethhdr->h_source))
1007 is_broadcast = 1;
1008 }
1009 rcu_read_unlock();
1010
76543d14 1011 if (batman_ogm_packet->header.version != COMPAT_VERSION) {
fc957275
ML
1012 bat_dbg(DBG_BATMAN, bat_priv,
1013 "Drop packet: incompatible batman version (%i)\n",
76543d14 1014 batman_ogm_packet->header.version);
fc957275
ML
1015 return;
1016 }
1017
1018 if (is_my_addr) {
1019 bat_dbg(DBG_BATMAN, bat_priv,
86ceb360 1020 "Drop packet: received my own broadcast (sender: %pM)\n",
fc957275
ML
1021 ethhdr->h_source);
1022 return;
1023 }
1024
1025 if (is_broadcast) {
86ceb360
SE
1026 bat_dbg(DBG_BATMAN, bat_priv,
1027 "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
1028 ethhdr->h_source);
fc957275
ML
1029 return;
1030 }
1031
1032 if (is_my_orig) {
1033 unsigned long *word;
1034 int offset;
1035
7d211efc
SE
1036 orig_neigh_node = batadv_get_orig_node(bat_priv,
1037 ethhdr->h_source);
fc957275
ML
1038 if (!orig_neigh_node)
1039 return;
1040
1041 /* neighbor has to indicate direct link and it has to
1042 * come via the corresponding interface */
1043 /* save packet seqno for bidirectional check */
1044 if (has_directlink_flag &&
1045 compare_eth(if_incoming->net_dev->dev_addr,
1046 batman_ogm_packet->orig)) {
1047 offset = if_incoming->if_num * NUM_WORDS;
1048
1049 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1050 word = &(orig_neigh_node->bcast_own[offset]);
0079d2ce
SE
1051 bat_set_bit(word,
1052 if_incoming_seqno -
e0f5211f 1053 ntohl(batman_ogm_packet->seqno) - 2);
fc957275 1054 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
0079d2ce 1055 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
fc957275
ML
1056 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1057 }
1058
86ceb360
SE
1059 bat_dbg(DBG_BATMAN, bat_priv,
1060 "Drop packet: originator packet from myself (via neighbor)\n");
7d211efc 1061 batadv_orig_node_free_ref(orig_neigh_node);
fc957275
ML
1062 return;
1063 }
1064
1065 if (is_my_oldorig) {
1066 bat_dbg(DBG_BATMAN, bat_priv,
86ceb360
SE
1067 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1068 ethhdr->h_source);
fc957275
ML
1069 return;
1070 }
1071
13b2541b
ML
1072 if (batman_ogm_packet->flags & NOT_BEST_NEXT_HOP) {
1073 bat_dbg(DBG_BATMAN, bat_priv,
fefa5329
ML
1074 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1075 ethhdr->h_source);
13b2541b
ML
1076 return;
1077 }
1078
7d211efc 1079 orig_node = batadv_get_orig_node(bat_priv, batman_ogm_packet->orig);
fc957275
ML
1080 if (!orig_node)
1081 return;
1082
01c4224b
ML
1083 is_duplicate = bat_iv_ogm_update_seqnos(ethhdr, batman_ogm_packet,
1084 if_incoming);
fc957275
ML
1085
1086 if (is_duplicate == -1) {
1087 bat_dbg(DBG_BATMAN, bat_priv,
86ceb360
SE
1088 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1089 ethhdr->h_source);
fc957275
ML
1090 goto out;
1091 }
1092
1093 if (batman_ogm_packet->tq == 0) {
1094 bat_dbg(DBG_BATMAN, bat_priv,
1095 "Drop packet: originator packet with tq equal 0\n");
1096 goto out;
1097 }
1098
7d211efc 1099 router = batadv_orig_node_get_router(orig_node);
fc957275 1100 if (router)
7d211efc 1101 router_router = batadv_orig_node_get_router(router->orig_node);
fc957275 1102
13b2541b
ML
1103 if ((router && router->tq_avg != 0) &&
1104 (compare_eth(router->addr, ethhdr->h_source)))
1105 is_from_best_next_hop = true;
1106
fc957275
ML
1107 /* avoid temporary routing loops */
1108 if (router && router_router &&
1109 (compare_eth(router->addr, batman_ogm_packet->prev_sender)) &&
1110 !(compare_eth(batman_ogm_packet->orig,
1111 batman_ogm_packet->prev_sender)) &&
1112 (compare_eth(router->addr, router_router->addr))) {
1113 bat_dbg(DBG_BATMAN, bat_priv,
86ceb360
SE
1114 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1115 ethhdr->h_source);
fc957275
ML
1116 goto out;
1117 }
1118
1119 /* if sender is a direct neighbor the sender mac equals
1120 * originator mac */
1121 orig_neigh_node = (is_single_hop_neigh ?
1122 orig_node :
7d211efc 1123 batadv_get_orig_node(bat_priv, ethhdr->h_source));
fc957275
ML
1124 if (!orig_neigh_node)
1125 goto out;
1126
7d211efc 1127 orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
fc957275
ML
1128
1129 /* drop packet if sender is not a direct neighbor and if we
1130 * don't route towards it */
1131 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1132 bat_dbg(DBG_BATMAN, bat_priv,
1133 "Drop packet: OGM via unknown neighbor!\n");
1134 goto out_neigh;
1135 }
1136
01c4224b
ML
1137 is_bidirectional = bat_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1138 batman_ogm_packet, if_incoming);
fc957275 1139
30d3c511
SE
1140 batadv_bonding_save_primary(orig_node, orig_neigh_node,
1141 batman_ogm_packet);
fc957275
ML
1142
1143 /* update ranking if it is not a duplicate or has the same
1144 * seqno and similar ttl as the non-duplicate */
1145 if (is_bidirectional &&
1146 (!is_duplicate ||
e0f5211f 1147 ((orig_node->last_real_seqno == ntohl(batman_ogm_packet->seqno)) &&
76543d14 1148 (orig_node->last_ttl - 3 <= batman_ogm_packet->header.ttl))))
01c4224b
ML
1149 bat_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1150 batman_ogm_packet, if_incoming,
1151 tt_buff, is_duplicate);
fc957275
ML
1152
1153 /* is single hop (direct) neighbor */
1154 if (is_single_hop_neigh) {
1155
1156 /* mark direct link on incoming interface */
01c4224b 1157 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
13b2541b
ML
1158 is_single_hop_neigh, is_from_best_next_hop,
1159 if_incoming);
fc957275 1160
86ceb360
SE
1161 bat_dbg(DBG_BATMAN, bat_priv,
1162 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
fc957275
ML
1163 goto out_neigh;
1164 }
1165
1166 /* multihop originator */
1167 if (!is_bidirectional) {
1168 bat_dbg(DBG_BATMAN, bat_priv,
1169 "Drop packet: not received via bidirectional link\n");
1170 goto out_neigh;
1171 }
1172
1173 if (is_duplicate) {
1174 bat_dbg(DBG_BATMAN, bat_priv,
1175 "Drop packet: duplicate packet received\n");
1176 goto out_neigh;
1177 }
1178
1179 bat_dbg(DBG_BATMAN, bat_priv,
1180 "Forwarding packet: rebroadcast originator packet\n");
01c4224b 1181 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
13b2541b
ML
1182 is_single_hop_neigh, is_from_best_next_hop,
1183 if_incoming);
fc957275
ML
1184
1185out_neigh:
1186 if ((orig_neigh_node) && (!is_single_hop_neigh))
7d211efc 1187 batadv_orig_node_free_ref(orig_neigh_node);
fc957275
ML
1188out:
1189 if (router)
7d211efc 1190 batadv_neigh_node_free_ref(router);
fc957275 1191 if (router_router)
7d211efc 1192 batadv_neigh_node_free_ref(router_router);
fc957275 1193 if (orig_neigh_router)
7d211efc 1194 batadv_neigh_node_free_ref(orig_neigh_router);
fc957275 1195
7d211efc 1196 batadv_orig_node_free_ref(orig_node);
fc957275
ML
1197}
1198
c3e29312
ML
1199static int bat_iv_ogm_receive(struct sk_buff *skb,
1200 struct hard_iface *if_incoming)
fc957275 1201{
edbf12ba 1202 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
fc957275 1203 struct batman_ogm_packet *batman_ogm_packet;
8780dad9
ML
1204 struct ethhdr *ethhdr;
1205 int buff_pos = 0, packet_len;
1206 unsigned char *tt_buff, *packet_buff;
c3e29312
ML
1207 bool ret;
1208
30d3c511 1209 ret = batadv_check_management_packet(skb, if_incoming, BATMAN_OGM_HLEN);
c3e29312
ML
1210 if (!ret)
1211 return NET_RX_DROP;
fc957275 1212
edbf12ba
ML
1213 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1214 * that does not have B.A.T.M.A.N. IV enabled ?
1215 */
1216 if (bat_priv->bat_algo_ops->bat_ogm_emit != bat_iv_ogm_emit)
1217 return NET_RX_DROP;
1218
f8214865
MH
1219 batadv_inc_counter(bat_priv, BAT_CNT_MGMT_RX);
1220 batadv_add_counter(bat_priv, BAT_CNT_MGMT_RX_BYTES,
1221 skb->len + ETH_HLEN);
1222
8780dad9
ML
1223 packet_len = skb_headlen(skb);
1224 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1225 packet_buff = skb->data;
fc957275
ML
1226 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
1227
1228 /* unpack the aggregated packets and process them one by one */
1229 do {
76e3d7fc 1230 tt_buff = packet_buff + buff_pos + BATMAN_OGM_HLEN;
fc957275 1231
01c4224b
ML
1232 bat_iv_ogm_process(ethhdr, batman_ogm_packet,
1233 tt_buff, if_incoming);
fc957275 1234
08c36d3e
SE
1235 buff_pos += BATMAN_OGM_HLEN;
1236 buff_pos += batadv_tt_len(batman_ogm_packet->tt_num_changes);
fc957275
ML
1237
1238 batman_ogm_packet = (struct batman_ogm_packet *)
1239 (packet_buff + buff_pos);
01c4224b
ML
1240 } while (bat_iv_ogm_aggr_packet(buff_pos, packet_len,
1241 batman_ogm_packet->tt_num_changes));
c3e29312
ML
1242
1243 kfree_skb(skb);
1244 return NET_RX_SUCCESS;
fc957275 1245}
1c280471
ML
1246
1247static struct bat_algo_ops batman_iv __read_mostly = {
519d3497 1248 .name = "BATMAN_IV",
c2aca022 1249 .bat_iface_enable = bat_iv_ogm_iface_enable,
00a50076 1250 .bat_iface_disable = bat_iv_ogm_iface_disable,
c3229398 1251 .bat_iface_update_mac = bat_iv_ogm_iface_update_mac,
cd8b78e7 1252 .bat_primary_iface_set = bat_iv_ogm_primary_iface_set,
01c4224b
ML
1253 .bat_ogm_schedule = bat_iv_ogm_schedule,
1254 .bat_ogm_emit = bat_iv_ogm_emit,
1c280471
ML
1255};
1256
81c524f7 1257int __init batadv_iv_init(void)
1c280471 1258{
c3e29312
ML
1259 int ret;
1260
1261 /* batman originator packet */
3193e8fd 1262 ret = batadv_recv_handler_register(BAT_IV_OGM, bat_iv_ogm_receive);
c3e29312
ML
1263 if (ret < 0)
1264 goto out;
1265
3193e8fd 1266 ret = batadv_algo_register(&batman_iv);
c3e29312
ML
1267 if (ret < 0)
1268 goto handler_unregister;
1269
1270 goto out;
1271
1272handler_unregister:
3193e8fd 1273 batadv_recv_handler_unregister(BAT_IV_OGM);
c3e29312
ML
1274out:
1275 return ret;
1c280471 1276}