bat_iv_ogm.c 43.5 KB
Newer Older
1
/* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Marek Lindner, Simon Wunderlich
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

#include "main.h"
#include "translation-table.h"
#include "originator.h"
#include "routing.h"
#include "gateway_common.h"
#include "gateway_client.h"
#include "hard-interface.h"
#include "send.h"
28
#include "bat_algo.h"
29
#include "network-coding.h"
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

/**
 * batadv_dup_status - duplicate status
 * @BATADV_NO_DUP: the packet is a duplicate
 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
 *  neighbor)
 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
 * @BATADV_PROTECTED: originator is currently protected (after reboot)
 */
enum batadv_dup_status {
	BATADV_NO_DUP = 0,
	BATADV_ORIG_DUP,
	BATADV_NEIGH_DUP,
	BATADV_PROTECTED,
};

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/**
 * batadv_ring_buffer_set - update the ring buffer with the given value
 * @lq_recv: pointer to the ring buffer
 * @lq_index: index to store the value at
 * @value: value to store in the ring buffer
 */
static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
				   uint8_t value)
{
	lq_recv[*lq_index] = value;
	*lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
}

/**
 * batadv_ring_buffer_set - compute the average of all non-zero values stored
 * in the given ring buffer
 * @lq_recv: pointer to the ring buffer
 *
 * Returns computed average value.
 */
static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
{
	const uint8_t *ptr;
	uint16_t count = 0, i = 0, sum = 0;

	ptr = lq_recv;

	while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
		if (*ptr != 0) {
			count++;
			sum += *ptr;
		}

		i++;
		ptr++;
	}

	if (count == 0)
		return 0;

	return (uint8_t)(sum / count);
}
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/**
 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
 * @bat_priv: the bat priv with all the soft interface information
 * @addr: mac address of the originator
 *
 * Returns the originator object corresponding to the passed mac address or NULL
 * on failure.
 * If the object does not exists it is created an initialised.
 */
static struct batadv_orig_node *
batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr)
{
	struct batadv_orig_node *orig_node;
	int size, hash_added;

	orig_node = batadv_orig_hash_find(bat_priv, addr);
	if (orig_node)
		return orig_node;

	orig_node = batadv_orig_node_new(bat_priv, addr);
	if (!orig_node)
		return NULL;

	spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);

	size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
	orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
	if (!orig_node->bat_iv.bcast_own)
		goto free_orig_node;

	size = bat_priv->num_ifaces * sizeof(uint8_t);
	orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
	if (!orig_node->bat_iv.bcast_own_sum)
		goto free_bcast_own;

	hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
				     batadv_choose_orig, orig_node,
				     &orig_node->hash_entry);
	if (hash_added != 0)
		goto free_bcast_own;

	return orig_node;

free_bcast_own:
	kfree(orig_node->bat_iv.bcast_own);
free_orig_node:
	batadv_orig_node_free_ref(orig_node);

	return NULL;
}

141 142 143 144
static struct batadv_neigh_node *
batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
			const uint8_t *neigh_addr,
			struct batadv_orig_node *orig_node,
145
			struct batadv_orig_node *orig_neigh)
146
{
147
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
148
	struct batadv_neigh_node *neigh_node;
149

150
	neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, orig_node);
151 152 153
	if (!neigh_node)
		goto out;

154
	spin_lock_init(&neigh_node->bat_iv.lq_update_lock);
155

156 157 158
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
		   "Creating new neighbor %pM for orig_node %pM on interface %s\n",
		   neigh_addr, orig_node->orig, hard_iface->net_dev->name);
159 160 161 162 163 164 165 166 167

	spin_lock_bh(&orig_node->neigh_list_lock);
	hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
	spin_unlock_bh(&orig_node->neigh_list_lock);

out:
	return neigh_node;
}

168
static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
169
{
170
	struct batadv_ogm_packet *batadv_ogm_packet;
171
	unsigned char *ogm_buff;
172
	uint32_t random_seqno;
173
	int res = -ENOMEM;
174 175 176

	/* randomize initial seqno to avoid collision */
	get_random_bytes(&random_seqno, sizeof(random_seqno));
177
	atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
178

179 180 181
	hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
	ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
	if (!ogm_buff)
182 183
		goto out;

184 185 186
	hard_iface->bat_iv.ogm_buff = ogm_buff;

	batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
187 188 189 190
	batadv_ogm_packet->header.packet_type = BATADV_IV_OGM;
	batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION;
	batadv_ogm_packet->header.ttl = 2;
	batadv_ogm_packet->flags = BATADV_NO_FLAGS;
191
	batadv_ogm_packet->reserved = 0;
192
	batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
193 194 195 196 197

	res = 0;

out:
	return res;
198 199
}

200
static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
201
{
202 203
	kfree(hard_iface->bat_iv.ogm_buff);
	hard_iface->bat_iv.ogm_buff = NULL;
204 205
}

206
static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
207
{
208
	struct batadv_ogm_packet *batadv_ogm_packet;
209
	unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
210

211
	batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
212
	memcpy(batadv_ogm_packet->orig,
213
	       hard_iface->net_dev->dev_addr, ETH_ALEN);
214
	memcpy(batadv_ogm_packet->prev_sender,
215
	       hard_iface->net_dev->dev_addr, ETH_ALEN);
216 217
}

218 219
static void
batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
220
{
221
	struct batadv_ogm_packet *batadv_ogm_packet;
222
	unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
223

224
	batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
225 226
	batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
	batadv_ogm_packet->header.ttl = BATADV_TTL;
227 228
}

229
/* when do we schedule our own ogm to be sent */
230
static unsigned long
231
batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
232
{
233 234 235
	unsigned int msecs;

	msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
236
	msecs += prandom_u32() % (2 * BATADV_JITTER);
237 238

	return jiffies + msecs_to_jiffies(msecs);
239 240 241
}

/* when do we schedule a ogm packet to be sent */
242
static unsigned long batadv_iv_ogm_fwd_send_time(void)
243
{
244
	return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
245 246 247
}

/* apply hop penalty for a normal link */
248 249
static uint8_t batadv_hop_penalty(uint8_t tq,
				  const struct batadv_priv *bat_priv)
250 251
{
	int hop_penalty = atomic_read(&bat_priv->hop_penalty);
252 253 254 255 256 257
	int new_tq;

	new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
	new_tq /= BATADV_TQ_MAX_VALUE;

	return new_tq;
258 259
}

260
/* is there another aggregated packet here? */
261
static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
262
				     __be16 tvlv_len)
263
{
264 265
	int next_buff_pos = 0;

266
	next_buff_pos += buff_pos + BATADV_OGM_HLEN;
267
	next_buff_pos += ntohs(tvlv_len);
268 269

	return (next_buff_pos <= packet_len) &&
270
	       (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
271 272
}

273
/* send a batman ogm to a given interface */
274 275
static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
				     struct batadv_hard_iface *hard_iface)
276
{
277
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
278 279 280
	char *fwd_str;
	uint8_t packet_num;
	int16_t buff_pos;
281
	struct batadv_ogm_packet *batadv_ogm_packet;
282
	struct sk_buff *skb;
283
	uint8_t *packet_pos;
284

285
	if (hard_iface->if_status != BATADV_IF_ACTIVE)
286 287 288 289
		return;

	packet_num = 0;
	buff_pos = 0;
290 291
	packet_pos = forw_packet->skb->data;
	batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
292 293

	/* adjust all flags and log packets */
294
	while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
295
					 batadv_ogm_packet->tvlv_len)) {
296
		/* we might have aggregated direct link packets with an
297 298
		 * ordinary base packet
		 */
299 300
		if (forw_packet->direct_link_flags & BIT(packet_num) &&
		    forw_packet->if_incoming == hard_iface)
301
			batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
302
		else
303
			batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
304

305 306 307 308 309
		if (packet_num > 0 || !forw_packet->own)
			fwd_str = "Forwarding";
		else
			fwd_str = "Sending own";

310
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
311
			   "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
312
			   fwd_str, (packet_num > 0 ? "aggregated " : ""),
313 314 315 316
			   batadv_ogm_packet->orig,
			   ntohl(batadv_ogm_packet->seqno),
			   batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl,
			   (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
317
			    "on" : "off"),
318
			   hard_iface->net_dev->name,
319
			   hard_iface->net_dev->dev_addr);
320

321
		buff_pos += BATADV_OGM_HLEN;
322
		buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
323
		packet_num++;
324 325
		packet_pos = forw_packet->skb->data + buff_pos;
		batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
326 327 328 329
	}

	/* create clone because function is called more than once */
	skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
330
	if (skb) {
331 332
		batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
		batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
333
				   skb->len + ETH_HLEN);
334
		batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
335
	}
336 337 338
}

/* send a batman ogm packet */
339
static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
340
{
341
	struct batadv_hard_iface *hard_iface;
342
	struct net_device *soft_iface;
343 344
	struct batadv_priv *bat_priv;
	struct batadv_hard_iface *primary_if = NULL;
345
	struct batadv_ogm_packet *batadv_ogm_packet;
346
	unsigned char directlink;
347
	uint8_t *packet_pos;
348

349 350
	packet_pos = forw_packet->skb->data;
	batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
351
	directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0);
352 353

	if (!forw_packet->if_incoming) {
354
		pr_err("Error - can't forward packet: incoming iface not specified\n");
355 356 357 358 359 360
		goto out;
	}

	soft_iface = forw_packet->if_incoming->soft_iface;
	bat_priv = netdev_priv(soft_iface);

361
	if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
362 363
		goto out;

364
	primary_if = batadv_primary_if_get_selected(bat_priv);
365 366 367
	if (!primary_if)
		goto out;

368 369 370
	/* multihomed peer assumed
	 * non-primary OGMs are only broadcasted on their interface
	 */
371
	if ((directlink && (batadv_ogm_packet->header.ttl == 1)) ||
372 373
	    (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
		/* FIXME: what about aggregated packets ? */
374
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
375 376
			   "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
			   (forw_packet->own ? "Sending own" : "Forwarding"),
377 378 379
			   batadv_ogm_packet->orig,
			   ntohl(batadv_ogm_packet->seqno),
			   batadv_ogm_packet->header.ttl,
380 381
			   forw_packet->if_incoming->net_dev->name,
			   forw_packet->if_incoming->net_dev->dev_addr);
382 383

		/* skb is only used once and than forw_packet is free'd */
384 385
		batadv_send_skb_packet(forw_packet->skb,
				       forw_packet->if_incoming,
386
				       batadv_broadcast_addr);
387 388 389 390 391 392 393
		forw_packet->skb = NULL;

		goto out;
	}

	/* broadcast on every interface */
	rcu_read_lock();
394
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
395 396 397
		if (hard_iface->soft_iface != soft_iface)
			continue;

398
		batadv_iv_ogm_send_to_if(forw_packet, hard_iface);
399 400 401 402 403
	}
	rcu_read_unlock();

out:
	if (primary_if)
404
		batadv_hardif_free_ref(primary_if);
405 406 407
}

/* return true if new_packet can be aggregated with forw_packet */
408
static bool
409
batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
410
			    struct batadv_priv *bat_priv,
411 412
			    int packet_len, unsigned long send_time,
			    bool directlink,
413 414
			    const struct batadv_hard_iface *if_incoming,
			    const struct batadv_forw_packet *forw_packet)
415
{
416
	struct batadv_ogm_packet *batadv_ogm_packet;
417
	int aggregated_bytes = forw_packet->packet_len + packet_len;
418
	struct batadv_hard_iface *primary_if = NULL;
419
	bool res = false;
420
	unsigned long aggregation_end_time;
421

422
	batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
423 424
	aggregation_end_time = send_time;
	aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
425

426
	/* we can aggregate the current packet to this aggregated packet
427 428 429 430 431 432 433
	 * if:
	 *
	 * - the send time is within our MAX_AGGREGATION_MS time
	 * - the resulting packet wont be bigger than
	 *   MAX_AGGREGATION_BYTES
	 */
	if (time_before(send_time, forw_packet->send_time) &&
434 435
	    time_after_eq(aggregation_end_time, forw_packet->send_time) &&
	    (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
436
		/* check aggregation compatibility
437 438 439 440 441 442
		 * -> direct link packets are broadcasted on
		 *    their interface only
		 * -> aggregate packet if the current packet is
		 *    a "global" packet as well as the base
		 *    packet
		 */
443
		primary_if = batadv_primary_if_get_selected(bat_priv);
444 445 446 447
		if (!primary_if)
			goto out;

		/* packets without direct link flag and high TTL
448 449
		 * are flooded through the net
		 */
450
		if ((!directlink) &&
451 452
		    (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
		    (batadv_ogm_packet->header.ttl != 1) &&
453 454

		    /* own packets originating non-primary
455 456
		     * interfaces leave only that interface
		     */
457 458 459 460 461 462 463
		    ((!forw_packet->own) ||
		     (forw_packet->if_incoming == primary_if))) {
			res = true;
			goto out;
		}

		/* if the incoming packet is sent via this one
464 465
		 * interface only - we still can aggregate
		 */
466
		if ((directlink) &&
467
		    (new_bat_ogm_packet->header.ttl == 1) &&
468 469 470 471
		    (forw_packet->if_incoming == if_incoming) &&

		    /* packets from direct neighbors or
		     * own secondary interface packets
472 473
		     * (= secondary interface packets in general)
		     */
474
		    (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
475 476 477 478 479 480 481 482 483
		     (forw_packet->own &&
		      forw_packet->if_incoming != primary_if))) {
			res = true;
			goto out;
		}
	}

out:
	if (primary_if)
484
		batadv_hardif_free_ref(primary_if);
485 486 487 488
	return res;
}

/* create a new aggregated packet and add this packet to it */
489 490 491
static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
					int packet_len, unsigned long send_time,
					bool direct_link,
492
					struct batadv_hard_iface *if_incoming,
493
					int own_packet)
494
{
495 496
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
	struct batadv_forw_packet *forw_packet_aggr;
497
	unsigned char *skb_buff;
498
	unsigned int skb_size;
499 500 501 502 503 504

	if (!atomic_inc_not_zero(&if_incoming->refcount))
		return;

	/* own packet should always be scheduled */
	if (!own_packet) {
505
		if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
506
			batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
507
				   "batman packet queue full\n");
508 509 510 511 512 513 514 515 516 517 518 519
			goto out;
		}
	}

	forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
	if (!forw_packet_aggr) {
		if (!own_packet)
			atomic_inc(&bat_priv->batman_queue_left);
		goto out;
	}

	if ((atomic_read(&bat_priv->aggregated_ogms)) &&
520
	    (packet_len < BATADV_MAX_AGGREGATION_BYTES))
521
		skb_size = BATADV_MAX_AGGREGATION_BYTES;
522
	else
523 524
		skb_size = packet_len;

525
	skb_size += ETH_HLEN;
526

527
	forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
528 529 530 531 532 533
	if (!forw_packet_aggr->skb) {
		if (!own_packet)
			atomic_inc(&bat_priv->batman_queue_left);
		kfree(forw_packet_aggr);
		goto out;
	}
534
	forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
535
	skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
536 537 538 539 540 541 542 543

	skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
	forw_packet_aggr->packet_len = packet_len;
	memcpy(skb_buff, packet_buff, packet_len);

	forw_packet_aggr->own = own_packet;
	forw_packet_aggr->if_incoming = if_incoming;
	forw_packet_aggr->num_packets = 0;
544
	forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
545 546 547 548 549 550 551 552 553 554 555 556 557
	forw_packet_aggr->send_time = send_time;

	/* save packet direct link flag status */
	if (direct_link)
		forw_packet_aggr->direct_link_flags |= 1;

	/* add new packet to packet list */
	spin_lock_bh(&bat_priv->forw_bat_list_lock);
	hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
	spin_unlock_bh(&bat_priv->forw_bat_list_lock);

	/* start timer for this packet */
	INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
558
			  batadv_send_outstanding_bat_ogm_packet);
559
	queue_delayed_work(batadv_event_workqueue,
560 561 562 563 564
			   &forw_packet_aggr->delayed_work,
			   send_time - jiffies);

	return;
out:
565
	batadv_hardif_free_ref(if_incoming);
566 567 568
}

/* aggregate a new packet into the existing ogm packet */
569
static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
570 571
				    const unsigned char *packet_buff,
				    int packet_len, bool direct_link)
572 573
{
	unsigned char *skb_buff;
574
	unsigned long new_direct_link_flag;
575 576 577 578 579 580 581

	skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
	memcpy(skb_buff, packet_buff, packet_len);
	forw_packet_aggr->packet_len += packet_len;
	forw_packet_aggr->num_packets++;

	/* save packet direct link flag status */
582 583 584 585
	if (direct_link) {
		new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
		forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
	}
586 587
}

588
static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
589 590
				    unsigned char *packet_buff,
				    int packet_len,
591
				    struct batadv_hard_iface *if_incoming,
592
				    int own_packet, unsigned long send_time)
593
{
594
	/* _aggr -> pointer to the packet we want to aggregate with
595 596
	 * _pos -> pointer to the position in the queue
	 */
597 598
	struct batadv_forw_packet *forw_packet_aggr = NULL;
	struct batadv_forw_packet *forw_packet_pos = NULL;
599
	struct batadv_ogm_packet *batadv_ogm_packet;
600
	bool direct_link;
601
	unsigned long max_aggregation_jiffies;
602

603 604
	batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
	direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
605
	max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
606 607 608 609 610

	/* find position for the packet in the forward queue */
	spin_lock_bh(&bat_priv->forw_bat_list_lock);
	/* own packets are not to be aggregated */
	if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
611
		hlist_for_each_entry(forw_packet_pos,
612
				     &bat_priv->forw_bat_list, list) {
613
			if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
614 615 616 617
							bat_priv, packet_len,
							send_time, direct_link,
							if_incoming,
							forw_packet_pos)) {
618 619 620 621 622 623 624
				forw_packet_aggr = forw_packet_pos;
				break;
			}
		}
	}

	/* nothing to aggregate with - either aggregation disabled or no
625 626
	 * suitable aggregation packet found
	 */
627 628 629 630
	if (!forw_packet_aggr) {
		/* the following section can run without the lock */
		spin_unlock_bh(&bat_priv->forw_bat_list_lock);

631
		/* if we could not aggregate this packet with one of the others
632 633 634
		 * we hold it back for a while, so that it might be aggregated
		 * later on
		 */
635 636
		if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
			send_time += max_aggregation_jiffies;
637

638 639 640
		batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
					    send_time, direct_link,
					    if_incoming, own_packet);
641
	} else {
642 643
		batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
					packet_len, direct_link);
644 645 646 647
		spin_unlock_bh(&bat_priv->forw_bat_list_lock);
	}
}

648
static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
649
				  const struct ethhdr *ethhdr,
650
				  struct batadv_ogm_packet *batadv_ogm_packet,
651 652
				  bool is_single_hop_neigh,
				  bool is_from_best_next_hop,
653
				  struct batadv_hard_iface *if_incoming)
654
{
655
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
656
	uint16_t tvlv_len;
657

658
	if (batadv_ogm_packet->header.ttl <= 1) {
659
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
660 661 662
		return;
	}

663 664 665 666 667 668 669 670
	if (!is_from_best_next_hop) {
		/* Mark the forwarded packet when it is not coming from our
		 * best next hop. We still need to forward the packet for our
		 * neighbor link quality detection to work in case the packet
		 * originated from a single hop neighbor. Otherwise we can
		 * simply drop the ogm.
		 */
		if (is_single_hop_neigh)
671
			batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
672 673 674
		else
			return;
	}
675

676
	tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
677

678 679
	batadv_ogm_packet->header.ttl--;
	memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
680 681

	/* apply hop penalty */
682
	batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
683
						   bat_priv);
684

685
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
686
		   "Forwarding packet: tq: %i, ttl: %i\n",
687
		   batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl);
688 689

	/* switch of primaries first hop flag when forwarding */
690
	batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
691
	if (is_single_hop_neigh)
692
		batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
693
	else
694
		batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
695

696
	batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
697
				BATADV_OGM_HLEN + tvlv_len,
698
				if_incoming, 0, batadv_iv_ogm_fwd_send_time());
699 700
}

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
/**
 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
 * the given interface
 * @hard_iface: the interface for which the windows have to be shifted
 */
static void
batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
{
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
	struct batadv_hashtable *hash = bat_priv->orig_hash;
	struct hlist_head *head;
	struct batadv_orig_node *orig_node;
	unsigned long *word;
	uint32_t i;
	size_t word_index;
	uint8_t *w;
717
	int if_num;
718 719 720 721 722 723

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

		rcu_read_lock();
		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
724
			spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
725
			word_index = hard_iface->if_num * BATADV_NUM_WORDS;
726
			word = &(orig_node->bat_iv.bcast_own[word_index]);
727 728

			batadv_bit_get_packet(bat_priv, word, 1, 0);
729 730
			if_num = hard_iface->if_num;
			w = &orig_node->bat_iv.bcast_own_sum[if_num];
731
			*w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
732
			spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
733 734 735 736 737
		}
		rcu_read_unlock();
	}
}

738
static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
739
{
740
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
741
	unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
742
	struct batadv_ogm_packet *batadv_ogm_packet;
743
	struct batadv_hard_iface *primary_if;
744
	int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
745
	uint32_t seqno;
746
	uint16_t tvlv_len = 0;
747

748
	primary_if = batadv_primary_if_get_selected(bat_priv);
749

750 751 752 753 754
	if (hard_iface == primary_if) {
		/* tt changes have to be committed before the tvlv data is
		 * appended as it may alter the tt tvlv container
		 */
		batadv_tt_local_commit_changes(bat_priv);
755 756 757
		tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
							    ogm_buff_len,
							    BATADV_OGM_HLEN);
758
	}
759

760
	batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
761
	batadv_ogm_packet->tvlv_len = htons(tvlv_len);
762 763

	/* change sequence number to network order */
764
	seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
765
	batadv_ogm_packet->seqno = htonl(seqno);
766
	atomic_inc(&hard_iface->bat_iv.ogm_seqno);
767

768
	batadv_iv_ogm_slide_own_bcast_window(hard_iface);
769 770
	batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff,
				hard_iface->bat_iv.ogm_buff_len, hard_iface, 1,
771
				batadv_iv_ogm_emit_send_time(bat_priv));
772 773

	if (primary_if)
774
		batadv_hardif_free_ref(primary_if);
775 776
}

777
static void
778 779
batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
			  struct batadv_orig_node *orig_node,
780
			  const struct ethhdr *ethhdr,
781
			  const struct batadv_ogm_packet *batadv_ogm_packet,
782
			  struct batadv_hard_iface *if_incoming,
783
			  const unsigned char *tt_buff,
784
			  enum batadv_dup_status dup_status)
785
{
786 787 788
	struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
	struct batadv_neigh_node *router = NULL;
	struct batadv_orig_node *orig_node_tmp;
789
	int if_num;
790
	uint8_t sum_orig, sum_neigh;
791
	uint8_t *neigh_addr;
792
	uint8_t tq_avg;
793

794
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
795
		   "update_originator(): Searching and updating originator entry of received packet\n");
796 797

	rcu_read_lock();
798
	hlist_for_each_entry_rcu(tmp_neigh_node,
799
				 &orig_node->neigh_list, list) {
800 801 802 803
		neigh_addr = tmp_neigh_node->addr;
		if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
		    tmp_neigh_node->if_incoming == if_incoming &&
		    atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
804
			if (WARN(neigh_node, "too many matching neigh_nodes"))
805
				batadv_neigh_node_free_ref(neigh_node);
806 807 808 809
			neigh_node = tmp_neigh_node;
			continue;
		}

810
		if (dup_status != BATADV_NO_DUP)
811 812
			continue;

813 814 815 816 817 818
		spin_lock_bh(&tmp_neigh_node->bat_iv.lq_update_lock);
		batadv_ring_buffer_set(tmp_neigh_node->bat_iv.tq_recv,
				       &tmp_neigh_node->bat_iv.tq_index, 0);
		tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->bat_iv.tq_recv);
		tmp_neigh_node->bat_iv.tq_avg = tq_avg;
		spin_unlock_bh(&tmp_neigh_node->bat_iv.lq_update_lock);
819 820 821
	}

	if (!neigh_node) {
822
		struct batadv_orig_node *orig_tmp;
823

824
		orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
825 826 827
		if (!orig_tmp)
			goto unlock;

828 829
		neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
						     ethhdr->h_source,
830
						     orig_node, orig_tmp);
831

832
		batadv_orig_node_free_ref(orig_tmp);
833 834 835
		if (!neigh_node)
			goto unlock;
	} else
836
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
837
			   "Updating existing last-hop neighbor of originator\n");
838 839 840

	rcu_read_unlock();

841
	neigh_node->last_seen = jiffies;
842

843 844 845
	spin_lock_bh(&neigh_node->bat_iv.lq_update_lock);
	batadv_ring_buffer_set(neigh_node->bat_iv.tq_recv,
			       &neigh_node->bat_iv.tq_index,
846
			       batadv_ogm_packet->tq);
847 848 849
	tq_avg = batadv_ring_buffer_avg(neigh_node->bat_iv.tq_recv);
	neigh_node->bat_iv.tq_avg = tq_avg;
	spin_unlock_bh(&neigh_node->bat_iv.lq_update_lock);
850

851
	if (dup_status == BATADV_NO_DUP) {
852 853
		orig_node->last_ttl = batadv_ogm_packet->header.ttl;
		neigh_node->last_ttl = batadv_ogm_packet->header.ttl;
854 855
	}

856
	batadv_bonding_candidate_add(orig_node, neigh_node);
857 858

	/* if this neighbor already is our next hop there is nothing
859 860
	 * to change
	 */
861
	router = batadv_orig_node_get_router(orig_node);
862
	if (router == neigh_node)
863
		goto out;
864 865

	/* if this neighbor does not offer a better TQ we won't consider it */
866
	if (router && (router->bat_iv.tq_avg > neigh_node->bat_iv.tq_avg))
867
		goto out;
868 869

	/* if the TQ is the same and the link not more symmetric we
870 871
	 * won't consider it either
	 */
872
	if (router && (neigh_node->bat_iv.tq_avg == router->bat_iv.tq_avg)) {
873
		orig_node_tmp = router->orig_node;
874
		spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
875
		if_num = router->if_incoming->if_num;
876 877
		sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
		spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
878 879

		orig_node_tmp = neigh_node->orig_node;
880
		spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
881
		if_num = neigh_node->if_incoming->if_num;
882 883
		sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
		spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
884

885
		if (sum_orig >= sum_neigh)
886
			goto out;
887 888
	}

889
	batadv_update_route(bat_priv, orig_node, neigh_node);
890 891 892 893 894 895
	goto out;

unlock:
	rcu_read_unlock();
out:
	if (neigh_node)
896
		batadv_neigh_node_free_ref(neigh_node);
897
	if (router)
898
		batadv_neigh_node_free_ref(router);
899 900
}

901 902
static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
				 struct batadv_orig_node *orig_neigh_node,
903
				 struct batadv_ogm_packet *batadv_ogm_packet,
904
				 struct batadv_hard_iface *if_incoming)
905
{
906 907
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
	struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
908
	uint8_t total_count;
909 910
	uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
	unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
911
	int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0;
912
	unsigned int combined_tq;
913 914 915

	/* find corresponding one hop neighbor */
	rcu_read_lock();
916
	hlist_for_each_entry_rcu(tmp_neigh_node,
917
				 &orig_neigh_node->neigh_list, list) {
918 919
		if (!batadv_compare_eth(tmp_neigh_node->addr,
					orig_neigh_node->orig))
920 921 922 923 924 925 926 927 928 929 930 931 932 933
			continue;

		if (tmp_neigh_node->if_incoming != if_incoming)
			continue;

		if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
			continue;

		neigh_node = tmp_neigh_node;
		break;
	}
	rcu_read_unlock();

	if (!neigh_node)
934 935 936
		neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
						     orig_neigh_node->orig,
						     orig_neigh_node,
937
						     orig_neigh_node);
938 939 940 941

	if (!neigh_node)
		goto out;

942
	/* if orig_node is direct neighbor update neigh_node last_seen */
943
	if (orig_node == orig_neigh_node)
944
		neigh_node->last_seen = jiffies;
945

946
	orig_node->last_seen = jiffies;
947 948

	/* find packet count of corresponding one hop neighbor */
949 950 951
	spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
	if_num = if_incoming->if_num;
	orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
952
	neigh_rq_count = neigh_node->bat_iv.real_packet_count;
953
	spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
954 955

	/* pay attention to not get a value bigger than 100 % */
956 957 958 959
	if (orig_eq_count > neigh_rq_count)
		total_count = neigh_rq_count;
	else
		total_count = orig_eq_count;
960

961 962 963
	/* if we have too few packets (too less data) we set tq_own to zero
	 * if we receive too few packets it is not considered bidirectional
	 */
964 965
	if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
	    neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
966 967 968 969
		tq_own = 0;
	else
		/* neigh_node->real_packet_count is never zero as we
		 * only purge old information when getting new
970 971
		 * information
		 */
972
		tq_own = (BATADV_TQ_MAX_VALUE * total_count) /	neigh_rq_count;
973

974
	/* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
975 976 977 978
	 * affect the nearly-symmetric links only a little, but
	 * punishes asymmetric links more.  This will give a value
	 * between 0 and TQ_MAX_VALUE
	 */
979 980 981 982 983 984 985 986 987
	neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
	neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
	neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
			    BATADV_TQ_LOCAL_WINDOW_SIZE *
			    BATADV_TQ_LOCAL_WINDOW_SIZE;
	inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
	inv_asym_penalty /= neigh_rq_max_cube;
	tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;

988
	combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
989
	combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
990
	batadv_ogm_packet->tq = combined_tq;
991

992
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
993 994 995
		   "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
		   orig_node->orig, orig_neigh_node->orig, total_count,
		   neigh_rq_count, tq_own,
996
		   tq_asym_penalty, batadv_ogm_packet->tq);
997 998

	/* if link has the minimum required transmission quality
999 1000
	 * consider it bidirectional
	 */
1001
	if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1002 1003 1004 1005
		ret = 1;

out:
	if (neigh_node)
1006
		batadv_neigh_node_free_ref(neigh_node);
1007 1008 1009
	return ret;
}

1010 1011 1012 1013 1014 1015 1016 1017
/**
 * batadv_iv_ogm_update_seqnos -  process a batman packet for all interfaces,
 *  adjust the sequence number and find out whether it is a duplicate
 * @ethhdr: ethernet header of the packet
 * @batadv_ogm_packet: OGM packet to be considered
 * @if_incoming: interface on which the OGM packet was received
 *
 * Returns duplicate status as enum batadv_dup_status
1018
 */
1019
static enum batadv_dup_status
1020
batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1021
			    const struct batadv_ogm_packet *batadv_ogm_packet,
1022
			    const struct batadv_hard_iface *if_incoming)
1023
{
1024 1025 1026
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
	struct batadv_orig_node *orig_node;
	struct batadv_neigh_node *tmp_neigh_node;
1027
	int is_dup;
1028 1029
	int32_t seq_diff;
	int need_update = 0;
1030 1031
	int set_mark;
	enum batadv_dup_status ret = BATADV_NO_DUP;
1032
	uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
1033
	uint8_t *neigh_addr;
1034
	uint8_t packet_count;
1035
	unsigned long *bitmap;
1036

1037
	orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1038
	if (!orig_node)
1039
		return BATADV_NO_DUP;
1040

1041
	spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1042
	seq_diff = seqno - orig_node->last_real_seqno;
1043 1044

	/* signalize caller that the packet is to be dropped. */
1045
	if (!hlist_empty(&orig_node->neigh_list) &&
1046
	    batadv_window_protected(bat_priv, seq_diff,
1047 1048
				    &orig_node->batman_seqno_reset)) {
		ret = BATADV_PROTECTED;
1049
		goto out;
1050
	}
1051 1052

	rcu_read_lock();
1053
	hlist_for_each_entry_rcu(tmp_neigh_node,
1054
				 &orig_node->neigh_list, list) {
1055
		neigh_addr = tmp_neigh_node->addr;
1056
		is_dup = batadv_test_bit(tmp_neigh_node->bat_iv.real_bits,
1057 1058 1059
					 orig_node->last_real_seqno,
					 seqno);

1060
		if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1061
		    tmp_neigh_node->if_incoming == if_incoming) {
1062
			set_mark = 1;
1063 1064 1065
			if (is_dup)
				ret = BATADV_NEIGH_DUP;
		} else {
1066
			set_mark = 0;
1067 1068 1069
			if (is_dup && (ret != BATADV_NEIGH_DUP))
				ret = BATADV_ORIG_DUP;
		}
1070 1071

		/* if the window moved, set the update flag. */
1072 1073
		bitmap = tmp_neigh_node->bat_iv.real_bits;
		need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1074
						     seq_diff, set_mark);
1075

1076
		packet_count = bitmap_weight(tmp_neigh_node->bat_iv.real_bits,
1077
					     BATADV_TQ_LOCAL_WINDOW_SIZE);
1078
		tmp_neigh_node->bat_iv.real_packet_count = packet_count;
1079 1080 1081 1082
	}
	rcu_read_unlock();

	if (need_update) {
1083
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1084 1085
			   "updating last_seqno: old %u, new %u\n",
			   orig_node->last_real_seqno, seqno);
1086
		orig_node->last_real_seqno = seqno;
1087 1088 1089
	}

out:
1090
	spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1091
	batadv_orig_node_free_ref(orig_node);
1092 1093 1094
	return ret;
}

1095
static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1096
				  struct batadv_ogm_packet *batadv_ogm_packet,
1097
				  const unsigned char *tt_buff,
1098
				  struct batadv_hard_iface *if_incoming)
1099
{
1100 1101
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
	struct batadv_hard_iface *hard_iface;
1102
	struct batadv_orig_node *orig_neigh_node, *orig_node, *orig_node_tmp;
1103 1104
	struct batadv_neigh_node *router = NULL, *router_router = NULL;
	struct batadv_neigh_node *orig_neigh_router = NULL;
1105 1106
	int has_directlink_flag;
	int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
1107
	int is_bidirect;
1108
	bool is_single_hop_neigh = false;
1109
	bool is_from_best_next_hop = false;
1110 1111
	int sameseq, similar_ttl;
	enum batadv_dup_status dup_status;
1112
	uint32_t if_incoming_seqno;
1113
	uint8_t *prev_sender;
1114 1115 1116 1117 1118 1119 1120 1121 1122

	/* Silently drop when the batman packet is actually not a
	 * correct packet.
	 *
	 * This might happen if a packet is padded (e.g. Ethernet has a
	 * minimum frame length of 64 byte) and the aggregation interprets
	 * it as an additional length.
	 *
	 * TODO: A more sane solution would be to have a bit in the
1123
	 * batadv_ogm_packet to detect whether the packet is the last
1124 1125 1126
	 * packet in an aggregation.  Here we expect that the padding
	 * is always zero (or not 0x01)
	 */
1127
	if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM)
1128 1129 1130
		return;

	/* could be changed by schedule_own_packet() */
1131
	if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1132

1133
	if (batadv_ogm_packet->flags & BATADV_DIRECTLINK)
1134 1135 1136
		has_directlink_flag = 1;
	else
		has_directlink_flag = 0;
1137

1138
	if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig))
1139
		is_single_hop_neigh = true;
1140

1141
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1142
		   "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
1143
		   ethhdr->h_source, if_incoming->net_dev->name,
1144 1145
		   if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
		   batadv_ogm_packet->prev_sender,
1146
		   ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->tq,
1147 1148
		   batadv_ogm_packet->header.ttl,
		   batadv_ogm_packet->header.version, has_directlink_flag);
1149 1150

	rcu_read_lock();
1151
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1152
		if (hard_iface->if_status != BATADV_IF_ACTIVE)
1153 1154 1155 1156 1157
			continue;

		if (hard_iface->soft_iface != if_incoming->soft_iface)
			continue;

1158 1159
		if (batadv_compare_eth(ethhdr->h_source,
				       hard_iface->net_dev->dev_addr))
1160 1161
			is_my_addr = 1;

1162
		if (batadv_compare_eth(batadv_ogm_packet->orig,
1163
				       hard_iface->net_dev->dev_addr))
1164 1165
			is_my_orig = 1;

1166
		if (batadv_compare_eth(batadv_ogm_packet->prev_sender,
1167
				       hard_iface->net_dev->dev_addr))
1168 1169 1170 1171 1172
			is_my_oldorig = 1;
	}
	rcu_read_unlock();

	if (is_my_addr) {
1173
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1174 1175
			   "Drop packet: received my own broadcast (sender: %pM)\n",
			   ethhdr->h_source);
1176 1177 1178 1179 1180 1181
		return;
	}

	if (is_my_orig) {
		unsigned long *word;
		int offset;
1182
		int32_t bit_pos;
1183 1184
		int16_t if_num;
		uint8_t *weight;
1185

1186 1187
		orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
							 ethhdr->h_source);
1188 1189 1190 1191
		if (!orig_neigh_node)
			return;

		/* neighbor has to indicate direct link and it has to
1192 1193 1194
		 * come via the corresponding interface
		 * save packet seqno for bidirectional check
		 */
1195
		if (has_directlink_flag &&
1196
		    batadv_compare_eth(if_incoming->net_dev->dev_addr,
1197
				       batadv_ogm_packet->orig)) {
1198 1199
			if_num = if_incoming->if_num;
			offset = if_num * BATADV_NUM_WORDS;
1200

1201 1202
			spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
			word = &(orig_neigh_node->bat_iv.bcast_own[offset]);
1203
			bit_pos = if_incoming_seqno - 2;
1204
			bit_pos -= ntohl(batadv_ogm_packet->seqno);
1205
			batadv_set_bit(word, bit_pos);
1206
			weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1207 1208
			*weight = bitmap_weight(word,
						BATADV_TQ_LOCAL_WINDOW_SIZE);
1209
			spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1210 1211
		}

1212
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1213
			   "Drop packet: originator packet from myself (via neighbor)\n");
1214
		batadv_orig_node_free_ref(orig_neigh_node);
1215 1216 1217 1218
		return;
	}

	if (is_my_oldorig) {
1219
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1220 1221
			   "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
			   ethhdr->h_source);
1222 1223 1224
		return;
	}

1225
	if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1226
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1227 1228
			   "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
			   ethhdr->h_source);
1229 1230 1231
		return;
	}

1232
	orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1233 1234 1235
	if (!orig_node)
		return;

1236 1237
	dup_status = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet,
						 if_incoming);
1238

1239
	if (dup_status == BATADV_PROTECTED) {
1240
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1241 1242
			   "Drop packet: packet within seqno protection time (sender: %pM)\n",
			   ethhdr->h_source);
1243 1244 1245
		goto out;
	}

1246
	if (batadv_ogm_packet->tq == 0) {
1247
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1248
			   "Drop packet: originator packet with tq equal 0\n");
1249 1250 1251
		goto out;
	}

1252
	router = batadv_orig_node_get_router(orig_node);
1253 1254 1255 1256
	if (router) {
		orig_node_tmp = router->orig_node;
		router_router = batadv_orig_node_get_router(orig_node_tmp);
	}
1257

1258
	if ((router && router->bat_iv.tq_avg != 0) &&
1259
	    (batadv_compare_eth(router->addr, ethhdr->h_source)))
1260 1261
		is_from_best_next_hop = true;

1262
	prev_sender = batadv_ogm_packet->prev_sender;
1263 1264
	/* avoid temporary routing loops */
	if (router && router_router &&
1265
	    (batadv_compare_eth(router->addr, prev_sender)) &&
1266
	    !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) &&
1267
	    (batadv_compare_eth(router->addr, router_router->addr))) {
1268
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1269 1270
			   "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
			   ethhdr->h_source);
1271 1272 1273
		goto out;
	}

1274 1275
	batadv_tvlv_ogm_receive(bat_priv, batadv_ogm_packet, orig_node);

1276
	/* if sender is a direct neighbor the sender mac equals
1277 1278
	 * originator mac
	 */
1279 1280 1281
	if (is_single_hop_neigh)
		orig_neigh_node = orig_node;
	else
1282 1283
		orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
							 ethhdr->h_source);
1284

1285 1286 1287
	if (!orig_neigh_node)
		goto out;

1288 1289 1290 1291
	/* Update nc_nodes of the originator */
	batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
				 batadv_ogm_packet, is_single_hop_neigh);

1292
	orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
1293 1294

	/* drop packet if sender is not a direct neighbor and if we
1295 1296
	 * don't route towards it
	 */
1297
	if (!is_single_hop_neigh && (!orig_neigh_router)) {
1298
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1299
			   "Drop packet: OGM via unknown neighbor!\n");
1300 1301 1302
		goto out_neigh;
	}

1303
	is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1304
					    batadv_ogm_packet, if_incoming);
1305

1306
	batadv_bonding_save_primary(orig_node, orig_neigh_node,
1307
				    batadv_ogm_packet);
1308 1309

	/* update ranking if it is not a duplicate or has the same
1310 1311
	 * seqno and similar ttl as the non-duplicate
	 */
1312
	sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
1313 1314 1315
	similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl;
	if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
			    (sameseq && similar_ttl)))
1316
		batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1317
					  batadv_ogm_packet, if_incoming,
1318
					  tt_buff, dup_status);
1319 1320 1321 1322

	/* is single hop (direct) neighbor */
	if (is_single_hop_neigh) {
		/* mark direct link on incoming interface */
1323
		batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
1324 1325
				      is_single_hop_neigh,
				      is_from_best_next_hop, if_incoming);
1326

1327
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1328
			   "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1329 1330 1331 1332
		goto out_neigh;
	}

	/* multihop originator */
1333
	if (!is_bidirect) {
1334
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1335
			   "Drop packet: not received via bidirectional link\n");
1336 1337 1338
		goto out_neigh;
	}

1339
	if (dup_status == BATADV_NEIGH_DUP) {
1340
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1341
			   "Drop packet: duplicate packet received\n");
1342 1343 1344
		goto out_neigh;
	}

1345
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1346
		   "Forwarding packet: rebroadcast originator packet\n");
1347
	batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
1348 1349
			      is_single_hop_neigh, is_from_best_next_hop,
			      if_incoming);
1350 1351 1352

out_neigh:
	if ((orig_neigh_node) && (!is_single_hop_neigh))
1353
		batadv_orig_node_free_ref(orig_neigh_node);
1354 1355
out:
	if (router)
1356
		batadv_neigh_node_free_ref(router);
1357
	if (router_router)
1358
		batadv_neigh_node_free_ref(router_router);
1359
	if (orig_neigh_router)
1360
		batadv_neigh_node_free_ref(orig_neigh_router);
1361

1362
	batadv_orig_node_free_ref(orig_node);
1363 1364
}

1365
static int batadv_iv_ogm_receive(struct sk_buff *skb,
1366
				 struct batadv_hard_iface *if_incoming)
1367
{
1368
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1369
	struct batadv_ogm_packet *batadv_ogm_packet;
1370 1371
	struct ethhdr *ethhdr;
	int buff_pos = 0, packet_len;
1372
	unsigned char *tvlv_buff, *packet_buff;
1373
	uint8_t *packet_pos;
1374
	bool ret;
1375

1376
	ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1377 1378
	if (!ret)
		return NET_RX_DROP;
1379

1380 1381 1382
	/* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
	 * that does not have B.A.T.M.A.N. IV enabled ?
	 */
1383
	if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
1384 1385
		return NET_RX_DROP;

1386 1387
	batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
	batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1388 1389
			   skb->len + ETH_HLEN);

1390
	packet_len = skb_headlen(skb);
1391
	ethhdr = eth_hdr(skb);
1392
	packet_buff = skb->data;
1393
	batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
1394 1395

	/* unpack the aggregated packets and process them one by one */
1396
	while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
1397 1398
					 batadv_ogm_packet->tvlv_len)) {
		tvlv_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
1399

1400 1401
		batadv_iv_ogm_process(ethhdr, batadv_ogm_packet,
				      tvlv_buff, if_incoming);
1402

1403
		buff_pos += BATADV_OGM_HLEN;
1404
		buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
1405

1406 1407
		packet_pos = packet_buff + buff_pos;
		batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1408
	}
1409 1410 1411

	kfree_skb(skb);
	return NET_RX_SUCCESS;
1412
}
1413

1414
static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
1415
	.name = "BATMAN_IV",
1416 1417 1418 1419 1420 1421
	.bat_iface_enable = batadv_iv_ogm_iface_enable,
	.bat_iface_disable = batadv_iv_ogm_iface_disable,
	.bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
	.bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
	.bat_ogm_schedule = batadv_iv_ogm_schedule,
	.bat_ogm_emit = batadv_iv_ogm_emit,
1422 1423
};

1424
int __init batadv_iv_init(void)
1425
{
1426 1427 1428
	int ret;

	/* batman originator packet */
1429 1430
	ret = batadv_recv_handler_register(BATADV_IV_OGM,
					   batadv_iv_ogm_receive);
1431 1432 1433
	if (ret < 0)
		goto out;

1434
	ret = batadv_algo_register(&batadv_batman_iv);
1435 1436 1437 1438 1439 1440
	if (ret < 0)
		goto handler_unregister;

	goto out;

handler_unregister:
1441
	batadv_recv_handler_unregister(BATADV_IV_OGM);
1442 1443
out:
	return ret;
1444
}