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