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