bat_iv_ogm.c 62.4 KB
Newer Older
1
/* Copyright (C) 2007-2016  B.A.T.M.A.N. contributors:
2 3 4 5 6 7 8 9 10 11 12 13 14
 *
 * 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
15
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 17
 */

18
#include "bat_algo.h"
19
#include "main.h"
20 21 22 23 24 25 26 27 28 29 30 31 32 33

#include <linux/atomic.h>
#include <linux/bitmap.h>
#include <linux/bitops.h>
#include <linux/bug.h>
#include <linux/byteorder/generic.h>
#include <linux/cache.h>
#include <linux/errno.h>
#include <linux/etherdevice.h>
#include <linux/fs.h>
#include <linux/if_ether.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/list.h>
34
#include <linux/kref.h>
35
#include <linux/lockdep.h>
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <linux/netdevice.h>
#include <linux/pkt_sched.h>
#include <linux/printk.h>
#include <linux/random.h>
#include <linux/rculist.h>
#include <linux/rcupdate.h>
#include <linux/seq_file.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/stddef.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/workqueue.h>

#include "bitarray.h"
#include "hard-interface.h"
#include "hash.h"
#include "network-coding.h"
55
#include "originator.h"
56
#include "packet.h"
57 58
#include "routing.h"
#include "send.h"
59
#include "translation-table.h"
60

61
/**
62
 * enum batadv_dup_status - duplicate status
63
 * @BATADV_NO_DUP: the packet is no duplicate
64 65 66 67 68 69 70 71 72 73 74 75
 * @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,
};

76 77 78 79 80 81
/**
 * 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
 */
82
static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
83 84 85 86 87 88
{
	lq_recv[*lq_index] = value;
	*lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
}

/**
89
 * batadv_ring_buffer_avg - compute the average of all non-zero values stored
90 91 92
 * in the given ring buffer
 * @lq_recv: pointer to the ring buffer
 *
93
 * Return: computed average value.
94
 */
95
static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
96
{
97 98 99 100
	const u8 *ptr;
	u16 count = 0;
	u16 i = 0;
	u16 sum = 0;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

	ptr = lq_recv;

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

		i++;
		ptr++;
	}

	if (count == 0)
		return 0;

117
	return (u8)(sum / count);
118
}
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/**
 * batadv_iv_ogm_orig_free - free the private resources allocated for this
 *  orig_node
 * @orig_node: the orig_node for which the resources have to be free'd
 */
static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
{
	kfree(orig_node->bat_iv.bcast_own);
	kfree(orig_node->bat_iv.bcast_own_sum);
}

/**
 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
 *  include the new hard-interface
 * @orig_node: the orig_node that has to be changed
 * @max_if_num: the current amount of interfaces
 *
137
 * Return: 0 on success, a negative error code otherwise.
138 139 140 141 142
 */
static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
				     int max_if_num)
{
	void *data_ptr;
143
	size_t old_size;
144 145 146 147 148
	int ret = -ENOMEM;

	spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);

	old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
149 150 151
	data_ptr = kmalloc_array(max_if_num,
				 BATADV_NUM_WORDS * sizeof(unsigned long),
				 GFP_ATOMIC);
152 153 154 155 156 157 158
	if (!data_ptr)
		goto unlock;

	memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
	kfree(orig_node->bat_iv.bcast_own);
	orig_node->bat_iv.bcast_own = data_ptr;

159
	data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
160
	if (!data_ptr)
161 162 163
		goto unlock;

	memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
164
	       (max_if_num - 1) * sizeof(u8));
165 166 167 168 169 170 171 172 173 174 175 176
	kfree(orig_node->bat_iv.bcast_own_sum);
	orig_node->bat_iv.bcast_own_sum = data_ptr;

	ret = 0;

unlock:
	spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);

	return ret;
}

/**
177
 * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own
178 179 180 181
 * @orig_node: the orig_node that has to be changed
 * @max_if_num: the current amount of interfaces
 * @del_if_num: the index of the interface being removed
 */
182 183 184
static void
batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
				   int max_if_num, int del_if_num)
185
{
186 187 188
	size_t chunk_size;
	size_t if_offset;
	void *data_ptr;
189

190
	lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
191 192

	chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
193
	data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
194
	if (!data_ptr)
195 196
		/* use old buffer when new one could not be allocated */
		data_ptr = orig_node->bat_iv.bcast_own;
197 198

	/* copy first part */
199
	memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
200 201

	/* copy second part */
202
	if_offset = (del_if_num + 1) * chunk_size;
203 204 205
	memmove((char *)data_ptr + del_if_num * chunk_size,
		(uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
		(max_if_num - del_if_num) * chunk_size);
206

207 208 209 210 211 212
	/* bcast_own was shrunk down in new buffer; free old one */
	if (orig_node->bat_iv.bcast_own != data_ptr) {
		kfree(orig_node->bat_iv.bcast_own);
		orig_node->bat_iv.bcast_own = data_ptr;
	}
}
213

214 215 216 217 218 219 220 221 222 223 224 225 226 227
/**
 * batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum
 * @orig_node: the orig_node that has to be changed
 * @max_if_num: the current amount of interfaces
 * @del_if_num: the index of the interface being removed
 */
static void
batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
				       int max_if_num, int del_if_num)
{
	size_t if_offset;
	void *data_ptr;

	lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
228

229
	data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
230 231 232
	if (!data_ptr)
		/* use old buffer when new one could not be allocated */
		data_ptr = orig_node->bat_iv.bcast_own_sum;
233

234 235
	memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
		del_if_num * sizeof(u8));
236

237
	if_offset = (del_if_num + 1) * sizeof(u8);
238 239 240 241 242 243 244 245 246 247
	memmove((char *)data_ptr + del_if_num * sizeof(u8),
		orig_node->bat_iv.bcast_own_sum + if_offset,
		(max_if_num - del_if_num) * sizeof(u8));

	/* bcast_own_sum was shrunk down in new buffer; free old one */
	if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
		kfree(orig_node->bat_iv.bcast_own_sum);
		orig_node->bat_iv.bcast_own_sum = data_ptr;
	}
}
248

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
/**
 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
 *  exclude the removed interface
 * @orig_node: the orig_node that has to be changed
 * @max_if_num: the current amount of interfaces
 * @del_if_num: the index of the interface being removed
 *
 * Return: 0 on success, a negative error code otherwise.
 */
static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
				     int max_if_num, int del_if_num)
{
	spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);

	if (max_if_num == 0) {
		kfree(orig_node->bat_iv.bcast_own);
		kfree(orig_node->bat_iv.bcast_own_sum);
		orig_node->bat_iv.bcast_own = NULL;
		orig_node->bat_iv.bcast_own_sum = NULL;
	} else {
		batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
						   del_if_num);
		batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
						       del_if_num);
	}
274 275 276

	spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);

277
	return 0;
278 279
}

280 281 282 283 284
/**
 * 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
 *
285
 * Return: the originator object corresponding to the passed mac address or NULL
286 287 288 289
 * on failure.
 * If the object does not exists it is created an initialised.
 */
static struct batadv_orig_node *
290
batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
{
	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;

310
	size = bat_priv->num_ifaces * sizeof(u8);
311 312
	orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
	if (!orig_node->bat_iv.bcast_own_sum)
313
		goto free_orig_node;
314 315 316 317 318

	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)
319
		goto free_orig_node;
320 321 322 323

	return orig_node;

free_orig_node:
324
	/* free twice, as batadv_orig_node_new sets refcount to 2 */
325 326
	batadv_orig_node_put(orig_node);
	batadv_orig_node_put(orig_node);
327 328 329 330

	return NULL;
}

331 332
static struct batadv_neigh_node *
batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
333
			const u8 *neigh_addr,
334
			struct batadv_orig_node *orig_node,
335
			struct batadv_orig_node *orig_neigh)
336
{
337
	struct batadv_neigh_node *neigh_node;
338

339
	neigh_node = batadv_neigh_node_new(orig_node, hard_iface, neigh_addr);
340 341 342
	if (!neigh_node)
		goto out;

343
	neigh_node->orig_node = orig_neigh;
344 345 346 347 348

out:
	return neigh_node;
}

349
static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
350
{
351
	struct batadv_ogm_packet *batadv_ogm_packet;
352
	unsigned char *ogm_buff;
353
	u32 random_seqno;
354 355 356

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

359 360 361
	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)
362
		return -ENOMEM;
363

364 365 366
	hard_iface->bat_iv.ogm_buff = ogm_buff;

	batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
367 368 369
	batadv_ogm_packet->packet_type = BATADV_IV_OGM;
	batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
	batadv_ogm_packet->ttl = 2;
370
	batadv_ogm_packet->flags = BATADV_NO_FLAGS;
371
	batadv_ogm_packet->reserved = 0;
372
	batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
373

374
	return 0;
375 376
}

377
static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
378
{
379 380
	kfree(hard_iface->bat_iv.ogm_buff);
	hard_iface->bat_iv.ogm_buff = NULL;
381 382
}

383
static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
384
{
385
	struct batadv_ogm_packet *batadv_ogm_packet;
386
	unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
387

388
	batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
389 390 391 392
	ether_addr_copy(batadv_ogm_packet->orig,
			hard_iface->net_dev->dev_addr);
	ether_addr_copy(batadv_ogm_packet->prev_sender,
			hard_iface->net_dev->dev_addr);
393 394
}

395 396
static void
batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
397
{
398
	struct batadv_ogm_packet *batadv_ogm_packet;
399
	unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
400

401
	batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
402
	batadv_ogm_packet->ttl = BATADV_TTL;
403 404
}

405
/* when do we schedule our own ogm to be sent */
406
static unsigned long
407
batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
408
{
409 410 411
	unsigned int msecs;

	msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
412
	msecs += prandom_u32() % (2 * BATADV_JITTER);
413 414

	return jiffies + msecs_to_jiffies(msecs);
415 416 417
}

/* when do we schedule a ogm packet to be sent */
418
static unsigned long batadv_iv_ogm_fwd_send_time(void)
419
{
420
	return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
421 422 423
}

/* apply hop penalty for a normal link */
424
static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
425 426
{
	int hop_penalty = atomic_read(&bat_priv->hop_penalty);
427 428 429 430 431 432
	int new_tq;

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

	return new_tq;
433 434
}

435 436 437 438 439 440 441 442
/**
 * batadv_iv_ogm_aggr_packet - checks if there is another OGM attached
 * @buff_pos: current position in the skb
 * @packet_len: total length of the skb
 * @tvlv_len: tvlv length of the previously considered OGM
 *
 * Return: true if there is enough space for another OGM, false otherwise.
 */
443 444
static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
				      __be16 tvlv_len)
445
{
446 447
	int next_buff_pos = 0;

448
	next_buff_pos += buff_pos + BATADV_OGM_HLEN;
449
	next_buff_pos += ntohs(tvlv_len);
450 451

	return (next_buff_pos <= packet_len) &&
452
	       (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
453 454
}

455
/* send a batman ogm to a given interface */
456 457
static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
				     struct batadv_hard_iface *hard_iface)
458
{
459
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
460
	const char *fwd_str;
461 462
	u8 packet_num;
	s16 buff_pos;
463
	struct batadv_ogm_packet *batadv_ogm_packet;
464
	struct sk_buff *skb;
465
	u8 *packet_pos;
466

467
	if (hard_iface->if_status != BATADV_IF_ACTIVE)
468 469 470 471
		return;

	packet_num = 0;
	buff_pos = 0;
472 473
	packet_pos = forw_packet->skb->data;
	batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
474 475

	/* adjust all flags and log packets */
476
	while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
477
					 batadv_ogm_packet->tvlv_len)) {
478
		/* we might have aggregated direct link packets with an
479 480
		 * ordinary base packet
		 */
481 482
		if (forw_packet->direct_link_flags & BIT(packet_num) &&
		    forw_packet->if_incoming == hard_iface)
483
			batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
484
		else
485
			batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
486

487 488 489 490 491
		if (packet_num > 0 || !forw_packet->own)
			fwd_str = "Forwarding";
		else
			fwd_str = "Sending own";

492
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
493
			   "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
494
			   fwd_str, (packet_num > 0 ? "aggregated " : ""),
495 496
			   batadv_ogm_packet->orig,
			   ntohl(batadv_ogm_packet->seqno),
497
			   batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
498
			   ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
499
			    "on" : "off"),
500
			   hard_iface->net_dev->name,
501
			   hard_iface->net_dev->dev_addr);
502

503
		buff_pos += BATADV_OGM_HLEN;
504
		buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
505
		packet_num++;
506 507
		packet_pos = forw_packet->skb->data + buff_pos;
		batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
508 509 510 511
	}

	/* create clone because function is called more than once */
	skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
512
	if (skb) {
513 514
		batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
		batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
515
				   skb->len + ETH_HLEN);
516
		batadv_send_broadcast_skb(skb, hard_iface);
517
	}
518 519 520
}

/* send a batman ogm packet */
521
static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
522 523
{
	struct net_device *soft_iface;
524 525
	struct batadv_priv *bat_priv;
	struct batadv_hard_iface *primary_if = NULL;
526 527

	if (!forw_packet->if_incoming) {
528
		pr_err("Error - can't forward packet: incoming iface not specified\n");
529 530 531 532 533 534
		goto out;
	}

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

535
	if (WARN_ON(!forw_packet->if_outgoing))
536 537
		goto out;

538
	if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
539 540
		goto out;

541
	if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
542 543
		goto out;

544 545 546
	primary_if = batadv_primary_if_get_selected(bat_priv);
	if (!primary_if)
		goto out;
547

548 549
	/* only for one specific outgoing interface */
	batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
550 551 552

out:
	if (primary_if)
553
		batadv_hardif_put(primary_if);
554 555
}

556 557 558 559 560 561 562
/**
 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
 *  existing forward packet
 * @new_bat_ogm_packet: OGM packet to be aggregated
 * @bat_priv: the bat priv with all the soft interface information
 * @packet_len: (total) length of the OGM
 * @send_time: timestamp (jiffies) when the packet is to be sent
563
 * @directlink: true if this is a direct link packet
564 565 566 567
 * @if_incoming: interface where the packet was received
 * @if_outgoing: interface for which the retransmission should be considered
 * @forw_packet: the forwarded packet which should be checked
 *
568
 * Return: true if new_packet can be aggregated with forw_packet
569
 */
570
static bool
571
batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
572
			    struct batadv_priv *bat_priv,
573 574
			    int packet_len, unsigned long send_time,
			    bool directlink,
575
			    const struct batadv_hard_iface *if_incoming,
576
			    const struct batadv_hard_iface *if_outgoing,
577
			    const struct batadv_forw_packet *forw_packet)
578
{
579
	struct batadv_ogm_packet *batadv_ogm_packet;
580
	int aggregated_bytes = forw_packet->packet_len + packet_len;
581
	struct batadv_hard_iface *primary_if = NULL;
582
	bool res = false;
583
	unsigned long aggregation_end_time;
584

585
	batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
586 587
	aggregation_end_time = send_time;
	aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
588

589
	/* we can aggregate the current packet to this aggregated packet
590 591 592 593 594
	 * if:
	 *
	 * - the send time is within our MAX_AGGREGATION_MS time
	 * - the resulting packet wont be bigger than
	 *   MAX_AGGREGATION_BYTES
595
	 * otherwise aggregation is not possible
596
	 */
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
	if (!time_before(send_time, forw_packet->send_time) ||
	    !time_after_eq(aggregation_end_time, forw_packet->send_time))
		return false;

	if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
		return false;

	/* packet is not leaving on the same interface. */
	if (forw_packet->if_outgoing != if_outgoing)
		return false;

	/* check aggregation compatibility
	 * -> 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
	 */
	primary_if = batadv_primary_if_get_selected(bat_priv);
	if (!primary_if)
		return false;
618

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
	/* packets without direct link flag and high TTL
	 * are flooded through the net
	 */
	if (!directlink &&
	    !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
	    batadv_ogm_packet->ttl != 1 &&

	    /* own packets originating non-primary
	     * interfaces leave only that interface
	     */
	    (!forw_packet->own ||
	     forw_packet->if_incoming == primary_if)) {
		res = true;
		goto out;
	}
634

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
	/* if the incoming packet is sent via this one
	 * interface only - we still can aggregate
	 */
	if (directlink &&
	    new_bat_ogm_packet->ttl == 1 &&
	    forw_packet->if_incoming == if_incoming &&

	    /* packets from direct neighbors or
	     * own secondary interface packets
	     * (= secondary interface packets in general)
	     */
	    (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
	     (forw_packet->own &&
	      forw_packet->if_incoming != primary_if))) {
		res = true;
		goto out;
651 652 653 654
	}

out:
	if (primary_if)
655
		batadv_hardif_put(primary_if);
656 657 658
	return res;
}

659 660
/**
 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
661 662 663 664 665 666 667 668 669
 *  packet to it.
 * @packet_buff: pointer to the OGM
 * @packet_len: (total) length of the OGM
 * @send_time: timestamp (jiffies) when the packet is to be sent
 * @direct_link: whether this OGM has direct link status
 * @if_incoming: interface where the packet was received
 * @if_outgoing: interface for which the retransmission should be considered
 * @own_packet: true if it is a self-generated ogm
 */
670 671 672
static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
					int packet_len, unsigned long send_time,
					bool direct_link,
673
					struct batadv_hard_iface *if_incoming,
674
					struct batadv_hard_iface *if_outgoing,
675
					int own_packet)
676
{
677 678
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
	struct batadv_forw_packet *forw_packet_aggr;
679
	unsigned char *skb_buff;
680
	unsigned int skb_size;
681 682 683

	/* own packet should always be scheduled */
	if (!own_packet) {
684
		if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
685
			batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
686
				   "batman packet queue full\n");
687
			return;
688 689 690 691
		}
	}

	forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
692 693
	if (!forw_packet_aggr)
		goto out_nomem;
694

695 696
	if (atomic_read(&bat_priv->aggregated_ogms) &&
	    packet_len < BATADV_MAX_AGGREGATION_BYTES)
697
		skb_size = BATADV_MAX_AGGREGATION_BYTES;
698
	else
699 700
		skb_size = packet_len;

701
	skb_size += ETH_HLEN;
702

703
	forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
704 705
	if (!forw_packet_aggr->skb)
		goto out_free_forw_packet;
706
	forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
707
	skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
708 709 710 711 712

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

713 714
	kref_get(&if_incoming->refcount);
	kref_get(&if_outgoing->refcount);
715 716
	forw_packet_aggr->own = own_packet;
	forw_packet_aggr->if_incoming = if_incoming;
717
	forw_packet_aggr->if_outgoing = if_outgoing;
718
	forw_packet_aggr->num_packets = 0;
719
	forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
720 721 722 723 724 725 726 727 728 729 730 731 732
	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,
733
			  batadv_send_outstanding_bat_ogm_packet);
734
	queue_delayed_work(batadv_event_workqueue,
735 736 737 738
			   &forw_packet_aggr->delayed_work,
			   send_time - jiffies);

	return;
739 740 741 742 743
out_free_forw_packet:
	kfree(forw_packet_aggr);
out_nomem:
	if (!own_packet)
		atomic_inc(&bat_priv->batman_queue_left);
744 745 746
}

/* aggregate a new packet into the existing ogm packet */
747
static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
748 749
				    const unsigned char *packet_buff,
				    int packet_len, bool direct_link)
750 751
{
	unsigned char *skb_buff;
752
	unsigned long new_direct_link_flag;
753 754 755 756 757 758 759

	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 */
760 761 762 763
	if (direct_link) {
		new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
		forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
	}
764 765
}

766 767 768 769 770 771 772 773 774 775
/**
 * batadv_iv_ogm_queue_add - queue up an OGM for transmission
 * @bat_priv: the bat priv with all the soft interface information
 * @packet_buff: pointer to the OGM
 * @packet_len: (total) length of the OGM
 * @if_incoming: interface where the packet was received
 * @if_outgoing: interface for which the retransmission should be considered
 * @own_packet: true if it is a self-generated ogm
 * @send_time: timestamp (jiffies) when the packet is to be sent
 */
776
static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
777 778
				    unsigned char *packet_buff,
				    int packet_len,
779
				    struct batadv_hard_iface *if_incoming,
780
				    struct batadv_hard_iface *if_outgoing,
781
				    int own_packet, unsigned long send_time)
782
{
783
	/* _aggr -> pointer to the packet we want to aggregate with
784 785
	 * _pos -> pointer to the position in the queue
	 */
786 787
	struct batadv_forw_packet *forw_packet_aggr = NULL;
	struct batadv_forw_packet *forw_packet_pos = NULL;
788
	struct batadv_ogm_packet *batadv_ogm_packet;
789
	bool direct_link;
790
	unsigned long max_aggregation_jiffies;
791

792
	batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
793
	direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
794
	max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
795 796 797 798

	/* 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 */
799
	if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
800
		hlist_for_each_entry(forw_packet_pos,
801
				     &bat_priv->forw_bat_list, list) {
802
			if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
803 804 805
							bat_priv, packet_len,
							send_time, direct_link,
							if_incoming,
806
							if_outgoing,
807
							forw_packet_pos)) {
808 809 810 811 812 813 814
				forw_packet_aggr = forw_packet_pos;
				break;
			}
		}
	}

	/* nothing to aggregate with - either aggregation disabled or no
815 816
	 * suitable aggregation packet found
	 */
817 818 819 820
	if (!forw_packet_aggr) {
		/* the following section can run without the lock */
		spin_unlock_bh(&bat_priv->forw_bat_list_lock);

821
		/* if we could not aggregate this packet with one of the others
822 823 824
		 * we hold it back for a while, so that it might be aggregated
		 * later on
		 */
825 826
		if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
			send_time += max_aggregation_jiffies;
827

828 829
		batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
					    send_time, direct_link,
830 831
					    if_incoming, if_outgoing,
					    own_packet);
832
	} else {
833 834
		batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
					packet_len, direct_link);
835 836 837 838
		spin_unlock_bh(&bat_priv->forw_bat_list_lock);
	}
}

839
static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
840
				  const struct ethhdr *ethhdr,
841
				  struct batadv_ogm_packet *batadv_ogm_packet,
842 843
				  bool is_single_hop_neigh,
				  bool is_from_best_next_hop,
844 845
				  struct batadv_hard_iface *if_incoming,
				  struct batadv_hard_iface *if_outgoing)
846
{
847
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
848
	u16 tvlv_len;
849

850
	if (batadv_ogm_packet->ttl <= 1) {
851
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
852 853 854
		return;
	}

855 856 857 858 859 860 861 862
	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)
863
			batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
864 865 866
		else
			return;
	}
867

868
	tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
869

870
	batadv_ogm_packet->ttl--;
871
	ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
872 873

	/* apply hop penalty */
874
	batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
875
						   bat_priv);
876

877
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
878
		   "Forwarding packet: tq: %i, ttl: %i\n",
879
		   batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
880

881
	if (is_single_hop_neigh)
882
		batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
883
	else
884
		batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
885

886
	batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
887
				BATADV_OGM_HLEN + tvlv_len,
888 889
				if_incoming, if_outgoing, 0,
				batadv_iv_ogm_fwd_send_time());
890 891
}

892 893 894 895 896 897 898 899 900 901 902 903 904
/**
 * 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;
905
	u32 i;
906
	size_t word_index;
907
	u8 *w;
908
	int if_num;
909 910 911 912 913 914

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

		rcu_read_lock();
		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
915
			spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
916
			word_index = hard_iface->if_num * BATADV_NUM_WORDS;
917
			word = &orig_node->bat_iv.bcast_own[word_index];
918 919

			batadv_bit_get_packet(bat_priv, word, 1, 0);
920 921
			if_num = hard_iface->if_num;
			w = &orig_node->bat_iv.bcast_own_sum[if_num];
922
			*w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
923
			spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
924 925 926 927 928
		}
		rcu_read_unlock();
	}
}

929
static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
930
{
931
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
932
	unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
933
	struct batadv_ogm_packet *batadv_ogm_packet;
934
	struct batadv_hard_iface *primary_if, *tmp_hard_iface;
935
	int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
936 937
	u32 seqno;
	u16 tvlv_len = 0;
938
	unsigned long send_time;
939

940
	primary_if = batadv_primary_if_get_selected(bat_priv);
941

942 943 944 945 946
	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);
947 948 949
		tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
							    ogm_buff_len,
							    BATADV_OGM_HLEN);
950
	}
951

952
	batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
953
	batadv_ogm_packet->tvlv_len = htons(tvlv_len);
954 955

	/* change sequence number to network order */
956
	seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
957
	batadv_ogm_packet->seqno = htonl(seqno);
958
	atomic_inc(&hard_iface->bat_iv.ogm_seqno);
959

960
	batadv_iv_ogm_slide_own_bcast_window(hard_iface);
961

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
	send_time = batadv_iv_ogm_emit_send_time(bat_priv);

	if (hard_iface != primary_if) {
		/* OGMs from secondary interfaces are only scheduled on their
		 * respective interfaces.
		 */
		batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
					hard_iface, hard_iface, 1, send_time);
		goto out;
	}

	/* OGMs from primary interfaces are scheduled on all
	 * interfaces.
	 */
	rcu_read_lock();
	list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
		if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
979
			continue;
980 981 982 983

		if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
			continue;

984 985 986
		batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
					*ogm_buff_len, hard_iface,
					tmp_hard_iface, 1, send_time);
987 988

		batadv_hardif_put(tmp_hard_iface);
989 990 991 992
	}
	rcu_read_unlock();

out:
993
	if (primary_if)
994
		batadv_hardif_put(primary_if);
995 996
}

997 998 999 1000 1001
/**
 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
 *  originator
 * @bat_priv: the bat priv with all the soft interface information
 * @orig_node: the orig node who originally emitted the ogm packet
1002
 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
1003 1004 1005 1006 1007 1008
 * @ethhdr: Ethernet header of the OGM
 * @batadv_ogm_packet: the ogm packet
 * @if_incoming: interface where the packet was received
 * @if_outgoing: interface for which the retransmission should be considered
 * @dup_status: the duplicate status of this ogm packet.
 */
1009
static void
1010 1011
batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
			  struct batadv_orig_node *orig_node,
1012
			  struct batadv_orig_ifinfo *orig_ifinfo,
1013
			  const struct ethhdr *ethhdr,
1014
			  const struct batadv_ogm_packet *batadv_ogm_packet,
1015
			  struct batadv_hard_iface *if_incoming,
1016
			  struct batadv_hard_iface *if_outgoing,
1017
			  enum batadv_dup_status dup_status)
1018
{
1019 1020
	struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1021 1022
	struct batadv_neigh_node *neigh_node = NULL;
	struct batadv_neigh_node *tmp_neigh_node = NULL;
1023 1024
	struct batadv_neigh_node *router = NULL;
	struct batadv_orig_node *orig_node_tmp;
1025
	int if_num;
1026 1027 1028
	u8 sum_orig, sum_neigh;
	u8 *neigh_addr;
	u8 tq_avg;
1029

1030
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1031
		   "update_originator(): Searching and updating originator entry of received packet\n");
1032 1033

	rcu_read_lock();
1034
	hlist_for_each_entry_rcu(tmp_neigh_node,
1035
				 &orig_node->neigh_list, list) {
1036 1037 1038
		neigh_addr = tmp_neigh_node->addr;
		if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
		    tmp_neigh_node->if_incoming == if_incoming &&
1039
		    kref_get_unless_zero(&tmp_neigh_node->refcount)) {
1040
			if (WARN(neigh_node, "too many matching neigh_nodes"))
1041
				batadv_neigh_node_put(neigh_node);
1042 1043 1044 1045
			neigh_node = tmp_neigh_node;
			continue;
		}

1046
		if (dup_status != BATADV_NO_DUP)
1047 1048
			continue;

1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
		/* only update the entry for this outgoing interface */
		neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
						       if_outgoing);
		if (!neigh_ifinfo)
			continue;

		spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
		batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
				       &neigh_ifinfo->bat_iv.tq_index, 0);
		tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
		neigh_ifinfo->bat_iv.tq_avg = tq_avg;
		spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);

1062
		batadv_neigh_ifinfo_put(neigh_ifinfo);
1063
		neigh_ifinfo = NULL;
1064 1065 1066
	}

	if (!neigh_node) {
1067
		struct batadv_orig_node *orig_tmp;
1068

1069
		orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
1070 1071 1072
		if (!orig_tmp)
			goto unlock;

1073 1074
		neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
						     ethhdr->h_source,
1075
						     orig_node, orig_tmp);
1076

1077
		batadv_orig_node_put(orig_tmp);
1078 1079
		if (!neigh_node)
			goto unlock;
1080
	} else {
1081
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1082
			   "Updating existing last-hop neighbor of originator\n");
1083
	}
1084 1085

	rcu_read_unlock();
1086 1087 1088
	neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
	if (!neigh_ifinfo)
		goto out;
1089

1090
	neigh_node->last_seen = jiffies;
1091

1092 1093 1094
	spin_lock_bh(&neigh_node->ifinfo_lock);
	batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
			       &neigh_ifinfo->bat_iv.tq_index,
1095
			       batadv_ogm_packet->tq);
1096 1097 1098
	tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
	neigh_ifinfo->bat_iv.tq_avg = tq_avg;
	spin_unlock_bh(&neigh_node->ifinfo_lock);
1099

1100
	if (dup_status == BATADV_NO_DUP) {
1101
		orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1102
		neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1103 1104 1105
	}

	/* if this neighbor already is our next hop there is nothing
1106 1107
	 * to change
	 */
1108
	router = batadv_orig_router_get(orig_node, if_outgoing);
1109
	if (router == neigh_node)
1110
		goto out;
1111

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
	if (router) {
		router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
		if (!router_ifinfo)
			goto out;

		/* if this neighbor does not offer a better TQ we won't
		 * consider it
		 */
		if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
			goto out;
	}
1123 1124

	/* if the TQ is the same and the link not more symmetric we
1125 1126
	 * won't consider it either
	 */
1127
	if (router_ifinfo &&
1128
	    neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
1129
		orig_node_tmp = router->orig_node;
1130
		spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1131
		if_num = router->if_incoming->if_num;
1132 1133
		sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
		spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1134 1135

		orig_node_tmp = neigh_node->orig_node;
1136
		spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1137
		if_num = neigh_node->if_incoming->if_num;
1138 1139
		sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
		spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1140

1141
		if (sum_orig >= sum_neigh)
1142
			goto out;
1143 1144
	}

1145
	batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
1146 1147 1148 1149 1150 1151
	goto out;

unlock:
	rcu_read_unlock();
out:
	if (neigh_node)
1152
		batadv_neigh_node_put(neigh_node);
1153
	if (router)
1154
		batadv_neigh_node_put(router);
1155
	if (neigh_ifinfo)
1156
		batadv_neigh_ifinfo_put(neigh_ifinfo);
1157
	if (router_ifinfo)
1158
		batadv_neigh_ifinfo_put(router_ifinfo);
1159 1160
}

1161 1162 1163 1164 1165 1166 1167 1168
/**
 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
 * @orig_node: the orig node who originally emitted the ogm packet
 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
 * @batadv_ogm_packet: the ogm packet
 * @if_incoming: interface where the packet was received
 * @if_outgoing: interface for which the retransmission should be considered
 *
1169
 * Return: true if the link can be considered bidirectional, false otherwise
1170
 */
1171 1172 1173 1174 1175
static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
				  struct batadv_orig_node *orig_neigh_node,
				  struct batadv_ogm_packet *batadv_ogm_packet,
				  struct batadv_hard_iface *if_incoming,
				  struct batadv_hard_iface *if_outgoing)
1176
{
1177 1178
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
	struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1179
	struct batadv_neigh_ifinfo *neigh_ifinfo;
1180 1181
	u8 total_count;
	u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1182
	unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1183 1184
	int if_num;
	unsigned int tq_asym_penalty, inv_asym_penalty;
1185
	unsigned int combined_tq;
1186
	unsigned int tq_iface_penalty;
1187
	bool ret = false;
1188 1189 1190

	/* find corresponding one hop neighbor */
	rcu_read_lock();
1191
	hlist_for_each_entry_rcu(tmp_neigh_node,
1192
				 &orig_neigh_node->neigh_list, list) {
1193 1194
		if (!batadv_compare_eth(tmp_neigh_node->addr,
					orig_neigh_node->orig))
1195 1196 1197 1198 1199
			continue;

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

1200
		if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
1201 1202 1203 1204 1205 1206 1207 1208
			continue;

		neigh_node = tmp_neigh_node;
		break;
	}
	rcu_read_unlock();

	if (!neigh_node)
1209 1210 1211
		neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
						     orig_neigh_node->orig,
						     orig_neigh_node,
1212
						     orig_neigh_node);
1213 1214 1215 1216

	if (!neigh_node)
		goto out;

1217
	/* if orig_node is direct neighbor update neigh_node last_seen */
1218
	if (orig_node == orig_neigh_node)
1219
		neigh_node->last_seen = jiffies;
1220

1221
	orig_node->last_seen = jiffies;
1222 1223

	/* find packet count of corresponding one hop neighbor */
1224 1225 1226
	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];
1227 1228 1229
	neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
	if (neigh_ifinfo) {
		neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1230
		batadv_neigh_ifinfo_put(neigh_ifinfo);
1231 1232 1233
	} else {
		neigh_rq_count = 0;
	}
1234
	spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1235 1236

	/* pay attention to not get a value bigger than 100 % */
1237 1238 1239 1240
	if (orig_eq_count > neigh_rq_count)
		total_count = neigh_rq_count;
	else
		total_count = orig_eq_count;
1241

1242 1243 1244
	/* 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
	 */
1245 1246
	if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
	    neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1247 1248 1249 1250
		tq_own = 0;
	else
		/* neigh_node->real_packet_count is never zero as we
		 * only purge old information when getting new
1251 1252
		 * information
		 */
1253
		tq_own = (BATADV_TQ_MAX_VALUE * total_count) /	neigh_rq_count;
1254

1255
	/* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1256 1257 1258 1259
	 * affect the nearly-symmetric links only a little, but
	 * punishes asymmetric links more.  This will give a value
	 * between 0 and TQ_MAX_VALUE
	 */
1260 1261 1262 1263 1264 1265 1266 1267 1268
	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;

S
Simon Wunderlich 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	/* penalize if the OGM is forwarded on the same interface. WiFi
	 * interfaces and other half duplex devices suffer from throughput
	 * drops as they can't send and receive at the same time.
	 */
	tq_iface_penalty = BATADV_TQ_MAX_VALUE;
	if (if_outgoing && (if_incoming == if_outgoing) &&
	    batadv_is_wifi_netdev(if_outgoing->net_dev))
		tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
						      bat_priv);

	combined_tq = batadv_ogm_packet->tq *
		      tq_own *
		      tq_asym_penalty *
		      tq_iface_penalty;
	combined_tq /= BATADV_TQ_MAX_VALUE *
		       BATADV_TQ_MAX_VALUE *
		       BATADV_TQ_MAX_VALUE;
1286
	batadv_ogm_packet->tq = combined_tq;
1287

1288
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
S
Simon Wunderlich 已提交
1289
		   "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
1290
		   orig_node->orig, orig_neigh_node->orig, total_count,
S
Simon Wunderlich 已提交
1291 1292 1293
		   neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
		   batadv_ogm_packet->tq, if_incoming->net_dev->name,
		   if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1294 1295

	/* if link has the minimum required transmission quality
1296 1297
	 * consider it bidirectional
	 */
1298
	if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1299
		ret = true;
1300 1301 1302

out:
	if (neigh_node)
1303
		batadv_neigh_node_put(neigh_node);
1304 1305 1306
	return ret;
}

1307 1308 1309 1310 1311 1312
/**
 * 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
1313
 * @if_outgoing: interface for which the retransmission should be considered
1314
 *
1315
 * Return: duplicate status as enum batadv_dup_status
1316
 */
1317
static enum batadv_dup_status
1318
batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1319
			    const struct batadv_ogm_packet *batadv_ogm_packet,
1320 1321
			    const struct batadv_hard_iface *if_incoming,
			    struct batadv_hard_iface *if_outgoing)
1322
{
1323 1324
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
	struct batadv_orig_node *orig_node;
1325
	struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1326 1327
	struct batadv_neigh_node *neigh_node;
	struct batadv_neigh_ifinfo *neigh_ifinfo;
1328
	bool is_dup;
1329
	s32 seq_diff;
1330
	bool need_update = false;
1331 1332
	int set_mark;
	enum batadv_dup_status ret = BATADV_NO_DUP;
1333 1334 1335
	u32 seqno = ntohl(batadv_ogm_packet->seqno);
	u8 *neigh_addr;
	u8 packet_count;
1336
	unsigned long *bitmap;
1337

1338
	orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1339
	if (!orig_node)
1340
		return BATADV_NO_DUP;
1341

1342 1343
	orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
	if (WARN_ON(!orig_ifinfo)) {
1344
		batadv_orig_node_put(orig_node);
1345 1346 1347
		return 0;
	}

1348
	spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1349
	seq_diff = seqno - orig_ifinfo->last_real_seqno;
1350 1351

	/* signalize caller that the packet is to be dropped. */
1352
	if (!hlist_empty(&orig_node->neigh_list) &&
1353
	    batadv_window_protected(bat_priv, seq_diff,
1354 1355
				    BATADV_TQ_LOCAL_WINDOW_SIZE,
				    &orig_ifinfo->batman_seqno_reset, NULL)) {
1356
		ret = BATADV_PROTECTED;
1357
		goto out;
1358
	}
1359 1360

	rcu_read_lock();
1361 1362 1363 1364 1365 1366 1367 1368
	hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
		neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
						       if_outgoing);
		if (!neigh_ifinfo)
			continue;

		neigh_addr = neigh_node->addr;
		is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1369
					 orig_ifinfo->last_real_seqno,
1370 1371
					 seqno);

1372
		if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1373
		    neigh_node->if_incoming == if_incoming) {
1374
			set_mark = 1;
1375 1376 1377
			if (is_dup)
				ret = BATADV_NEIGH_DUP;
		} else {
1378
			set_mark = 0;
1379 1380 1381
			if (is_dup && (ret != BATADV_NEIGH_DUP))
				ret = BATADV_ORIG_DUP;
		}
1382 1383

		/* if the window moved, set the update flag. */
1384
		bitmap = neigh_ifinfo->bat_iv.real_bits;
1385
		need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1386
						     seq_diff, set_mark);
1387

1388
		packet_count = bitmap_weight(bitmap,
1389
					     BATADV_TQ_LOCAL_WINDOW_SIZE);
1390
		neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1391
		batadv_neigh_ifinfo_put(neigh_ifinfo);
1392 1393 1394 1395
	}
	rcu_read_unlock();

	if (need_update) {
1396
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1397 1398 1399 1400
			   "%s updating last_seqno: old %u, new %u\n",
			   if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
			   orig_ifinfo->last_real_seqno, seqno);
		orig_ifinfo->last_real_seqno = seqno;
1401 1402 1403
	}

out:
1404
	spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1405
	batadv_orig_node_put(orig_node);
1406
	batadv_orig_ifinfo_put(orig_ifinfo);
1407 1408 1409
	return ret;
}

1410 1411 1412
/**
 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
 * @skb: the skb containing the OGM
1413
 * @ogm_offset: offset from skb->data to start of ogm header
1414 1415 1416 1417 1418 1419 1420 1421 1422
 * @orig_node: the (cached) orig node for the originator of this OGM
 * @if_incoming: the interface where this packet was received
 * @if_outgoing: the interface for which the packet should be considered
 */
static void
batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
				struct batadv_orig_node *orig_node,
				struct batadv_hard_iface *if_incoming,
				struct batadv_hard_iface *if_outgoing)
1423
{
1424
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1425
	struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1426 1427
	struct batadv_neigh_node *router = NULL;
	struct batadv_neigh_node *router_router = NULL;
1428 1429
	struct batadv_orig_node *orig_neigh_node;
	struct batadv_orig_ifinfo *orig_ifinfo;
1430
	struct batadv_neigh_node *orig_neigh_router = NULL;
1431
	struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1432
	struct batadv_ogm_packet *ogm_packet;
1433
	enum batadv_dup_status dup_status;
1434 1435 1436 1437 1438
	bool is_from_best_next_hop = false;
	bool is_single_hop_neigh = false;
	bool sameseq, similar_ttl;
	struct sk_buff *skb_priv;
	struct ethhdr *ethhdr;
1439
	u8 *prev_sender;
1440
	bool is_bidirect;
1441

1442 1443
	/* create a private copy of the skb, as some functions change tq value
	 * and/or flags.
1444
	 */
1445 1446
	skb_priv = skb_copy(skb, GFP_ATOMIC);
	if (!skb_priv)
1447 1448
		return;

1449 1450
	ethhdr = eth_hdr(skb_priv);
	ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1451

1452 1453 1454
	dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
						 if_incoming, if_outgoing);
	if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1455
		is_single_hop_neigh = true;
1456

1457
	if (dup_status == BATADV_PROTECTED) {
1458
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1459 1460
			   "Drop packet: packet within seqno protection time (sender: %pM)\n",
			   ethhdr->h_source);
1461 1462 1463
		goto out;
	}

1464
	if (ogm_packet->tq == 0) {
1465
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1466
			   "Drop packet: originator packet with tq equal 0\n");
1467 1468 1469
		goto out;
	}

1470 1471 1472 1473 1474 1475 1476
	if (is_single_hop_neigh) {
		hardif_neigh = batadv_hardif_neigh_get(if_incoming,
						       ethhdr->h_source);
		if (hardif_neigh)
			hardif_neigh->last_seen = jiffies;
	}

1477
	router = batadv_orig_router_get(orig_node, if_outgoing);
1478
	if (router) {
1479 1480 1481
		router_router = batadv_orig_router_get(router->orig_node,
						       if_outgoing);
		router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1482
	}
1483

1484
	if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1485
	    (batadv_compare_eth(router->addr, ethhdr->h_source)))
1486 1487
		is_from_best_next_hop = true;

1488
	prev_sender = ogm_packet->prev_sender;
1489 1490
	/* avoid temporary routing loops */
	if (router && router_router &&
1491
	    (batadv_compare_eth(router->addr, prev_sender)) &&
1492
	    !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1493
	    (batadv_compare_eth(router->addr, router_router->addr))) {
1494
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1495 1496
			   "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
			   ethhdr->h_source);
1497 1498 1499
		goto out;
	}

1500 1501
	if (if_outgoing == BATADV_IF_DEFAULT)
		batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1502

1503
	/* if sender is a direct neighbor the sender mac equals
1504 1505
	 * originator mac
	 */
1506 1507 1508
	if (is_single_hop_neigh)
		orig_neigh_node = orig_node;
	else
1509 1510
		orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
							 ethhdr->h_source);
1511

1512 1513 1514
	if (!orig_neigh_node)
		goto out;

1515 1516
	/* Update nc_nodes of the originator */
	batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1517
				 ogm_packet, is_single_hop_neigh);
1518

1519 1520
	orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
						   if_outgoing);
1521 1522

	/* drop packet if sender is not a direct neighbor and if we
1523 1524
	 * don't route towards it
	 */
1525
	if (!is_single_hop_neigh && (!orig_neigh_router)) {
1526
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1527
			   "Drop packet: OGM via unknown neighbor!\n");
1528 1529 1530
		goto out_neigh;
	}

1531
	is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1532 1533
					    ogm_packet, if_incoming,
					    if_outgoing);
1534 1535

	/* update ranking if it is not a duplicate or has the same
1536 1537
	 * seqno and similar ttl as the non-duplicate
	 */
1538 1539 1540 1541 1542 1543 1544
	orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
	if (!orig_ifinfo)
		goto out_neigh;

	sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
	similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;

1545
	if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1546 1547 1548 1549 1550 1551
			    (sameseq && similar_ttl))) {
		batadv_iv_ogm_orig_update(bat_priv, orig_node,
					  orig_ifinfo, ethhdr,
					  ogm_packet, if_incoming,
					  if_outgoing, dup_status);
	}
1552
	batadv_orig_ifinfo_put(orig_ifinfo);
1553

1554 1555 1556 1557
	/* only forward for specific interface, not for the default one. */
	if (if_outgoing == BATADV_IF_DEFAULT)
		goto out_neigh;

1558 1559
	/* is single hop (direct) neighbor */
	if (is_single_hop_neigh) {
1560 1561 1562 1563 1564 1565 1566 1567 1568
		/* OGMs from secondary interfaces should only scheduled once
		 * per interface where it has been received, not multiple times
		 */
		if ((ogm_packet->ttl <= 2) &&
		    (if_incoming != if_outgoing)) {
			batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
				   "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
			goto out_neigh;
		}
1569
		/* mark direct link on incoming interface */
1570
		batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1571
				      is_single_hop_neigh,
1572 1573
				      is_from_best_next_hop, if_incoming,
				      if_outgoing);
1574

1575
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1576
			   "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1577 1578 1579 1580
		goto out_neigh;
	}

	/* multihop originator */
1581
	if (!is_bidirect) {
1582
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1583
			   "Drop packet: not received via bidirectional link\n");
1584 1585 1586
		goto out_neigh;
	}

1587
	if (dup_status == BATADV_NEIGH_DUP) {
1588
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1589
			   "Drop packet: duplicate packet received\n");
1590 1591 1592
		goto out_neigh;
	}

1593
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1594
		   "Forwarding packet: rebroadcast originator packet\n");
1595
	batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1596
			      is_single_hop_neigh, is_from_best_next_hop,
1597
			      if_incoming, if_outgoing);
1598 1599 1600

out_neigh:
	if ((orig_neigh_node) && (!is_single_hop_neigh))
1601
		batadv_orig_node_put(orig_neigh_node);
1602
out:
1603
	if (router_ifinfo)
1604
		batadv_neigh_ifinfo_put(router_ifinfo);
1605
	if (router)
1606
		batadv_neigh_node_put(router);
1607
	if (router_router)
1608
		batadv_neigh_node_put(router_router);
1609
	if (orig_neigh_router)
1610
		batadv_neigh_node_put(orig_neigh_router);
1611
	if (hardif_neigh)
1612
		batadv_hardif_neigh_put(hardif_neigh);
1613

1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
	kfree_skb(skb_priv);
}

/**
 * batadv_iv_ogm_process - process an incoming batman iv OGM
 * @skb: the skb containing the OGM
 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
 * @if_incoming: the interface where this packet was receved
 */
static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
				  struct batadv_hard_iface *if_incoming)
{
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
	struct batadv_orig_node *orig_neigh_node, *orig_node;
	struct batadv_hard_iface *hard_iface;
	struct batadv_ogm_packet *ogm_packet;
1630
	u32 if_incoming_seqno;
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
	bool has_directlink_flag;
	struct ethhdr *ethhdr;
	bool is_my_oldorig = false;
	bool is_my_addr = false;
	bool is_my_orig = false;

	ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
	ethhdr = eth_hdr(skb);

	/* 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
	 * batadv_ogm_packet to detect whether the packet is the last
	 * packet in an aggregation.  Here we expect that the padding
	 * is always zero (or not 0x01)
	 */
	if (ogm_packet->packet_type != BATADV_IV_OGM)
		return;

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

	if (ogm_packet->flags & BATADV_DIRECTLINK)
		has_directlink_flag = true;
	else
		has_directlink_flag = false;

	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
		   "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",
		   ethhdr->h_source, if_incoming->net_dev->name,
		   if_incoming->net_dev->dev_addr, ogm_packet->orig,
		   ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
		   ogm_packet->tq, ogm_packet->ttl,
		   ogm_packet->version, has_directlink_flag);

	rcu_read_lock();
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
		if (hard_iface->if_status != BATADV_IF_ACTIVE)
			continue;

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

		if (batadv_compare_eth(ethhdr->h_source,
				       hard_iface->net_dev->dev_addr))
			is_my_addr = true;

		if (batadv_compare_eth(ogm_packet->orig,
				       hard_iface->net_dev->dev_addr))
			is_my_orig = true;

		if (batadv_compare_eth(ogm_packet->prev_sender,
				       hard_iface->net_dev->dev_addr))
			is_my_oldorig = true;
	}
	rcu_read_unlock();

	if (is_my_addr) {
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
			   "Drop packet: received my own broadcast (sender: %pM)\n",
			   ethhdr->h_source);
		return;
	}

	if (is_my_orig) {
		unsigned long *word;
		int offset;
1703 1704 1705
		s32 bit_pos;
		s16 if_num;
		u8 *weight;
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722

		orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
							 ethhdr->h_source);
		if (!orig_neigh_node)
			return;

		/* neighbor has to indicate direct link and it has to
		 * come via the corresponding interface
		 * save packet seqno for bidirectional check
		 */
		if (has_directlink_flag &&
		    batadv_compare_eth(if_incoming->net_dev->dev_addr,
				       ogm_packet->orig)) {
			if_num = if_incoming->if_num;
			offset = if_num * BATADV_NUM_WORDS;

			spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1723
			word = &orig_neigh_node->bat_iv.bcast_own[offset];
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
			bit_pos = if_incoming_seqno - 2;
			bit_pos -= ntohl(ogm_packet->seqno);
			batadv_set_bit(word, bit_pos);
			weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
			*weight = bitmap_weight(word,
						BATADV_TQ_LOCAL_WINDOW_SIZE);
			spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
		}

		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
			   "Drop packet: originator packet from myself (via neighbor)\n");
1735
		batadv_orig_node_put(orig_neigh_node);
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
		return;
	}

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

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

	orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
	if (!orig_node)
		return;

	batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
					if_incoming, BATADV_IF_DEFAULT);

	rcu_read_lock();
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
		if (hard_iface->if_status != BATADV_IF_ACTIVE)
			continue;

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

1768 1769 1770
		if (!kref_get_unless_zero(&hard_iface->refcount))
			continue;

1771 1772
		batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
						if_incoming, hard_iface);
1773 1774

		batadv_hardif_put(hard_iface);
1775 1776 1777
	}
	rcu_read_unlock();

1778
	batadv_orig_node_put(orig_node);
1779 1780
}

1781
static int batadv_iv_ogm_receive(struct sk_buff *skb,
1782
				 struct batadv_hard_iface *if_incoming)
1783
{
1784
	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1785
	struct batadv_ogm_packet *ogm_packet;
1786
	u8 *packet_pos;
1787
	int ogm_offset;
1788
	bool ret;
1789

1790
	ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1791 1792
	if (!ret)
		return NET_RX_DROP;
1793

1794 1795 1796
	/* 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 ?
	 */
1797
	if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
1798 1799
		return NET_RX_DROP;

1800 1801
	batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
	batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1802 1803
			   skb->len + ETH_HLEN);

1804 1805
	ogm_offset = 0;
	ogm_packet = (struct batadv_ogm_packet *)skb->data;
1806 1807

	/* unpack the aggregated packets and process them one by one */
1808 1809 1810
	while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
					 ogm_packet->tvlv_len)) {
		batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1811

1812 1813
		ogm_offset += BATADV_OGM_HLEN;
		ogm_offset += ntohs(ogm_packet->tvlv_len);
1814

1815 1816
		packet_pos = skb->data + ogm_offset;
		ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1817
	}
1818 1819 1820

	kfree_skb(skb);
	return NET_RX_SUCCESS;
1821
}
1822

1823 1824
/**
 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
 * @orig_node: the orig_node for which the neighbors are printed
 * @if_outgoing: outgoing interface for these entries
 * @seq: debugfs table seq_file struct
 *
 * Must be called while holding an rcu lock.
 */
static void
batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
			       struct batadv_hard_iface *if_outgoing,
			       struct seq_file *seq)
{
	struct batadv_neigh_node *neigh_node;
	struct batadv_neigh_ifinfo *n_ifinfo;

	hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
		n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
		if (!n_ifinfo)
			continue;

		seq_printf(seq, " %pM (%3i)",
			   neigh_node->addr,
			   n_ifinfo->bat_iv.tq_avg);

1848
		batadv_neigh_ifinfo_put(n_ifinfo);
1849 1850 1851
	}
}

1852 1853 1854 1855
/**
 * batadv_iv_ogm_orig_print - print the originator table
 * @bat_priv: the bat priv with all the soft interface information
 * @seq: debugfs table seq_file struct
1856
 * @if_outgoing: the outgoing interface for which this should be printed
1857 1858
 */
static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1859 1860
				     struct seq_file *seq,
				     struct batadv_hard_iface *if_outgoing)
1861
{
1862
	struct batadv_neigh_node *neigh_node;
1863 1864 1865
	struct batadv_hashtable *hash = bat_priv->orig_hash;
	int last_seen_msecs, last_seen_secs;
	struct batadv_orig_node *orig_node;
1866
	struct batadv_neigh_ifinfo *n_ifinfo;
1867 1868 1869
	unsigned long last_seen_jiffies;
	struct hlist_head *head;
	int batman_count = 0;
1870
	u32 i;
1871

1872 1873
	seq_puts(seq,
		 "  Originator      last-seen (#/255)           Nexthop [outgoingIF]:   Potential nexthops ...\n");
1874 1875 1876 1877 1878 1879

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

		rcu_read_lock();
		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1880
			neigh_node = batadv_orig_router_get(orig_node,
1881
							    if_outgoing);
1882 1883 1884
			if (!neigh_node)
				continue;

1885
			n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
1886
							   if_outgoing);
1887 1888 1889 1890
			if (!n_ifinfo)
				goto next;

			if (n_ifinfo->bat_iv.tq_avg == 0)
1891 1892 1893 1894 1895 1896 1897 1898 1899
				goto next;

			last_seen_jiffies = jiffies - orig_node->last_seen;
			last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
			last_seen_secs = last_seen_msecs / 1000;
			last_seen_msecs = last_seen_msecs % 1000;

			seq_printf(seq, "%pM %4i.%03is   (%3i) %pM [%10s]:",
				   orig_node->orig, last_seen_secs,
1900
				   last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
1901 1902 1903
				   neigh_node->addr,
				   neigh_node->if_incoming->net_dev->name);

1904 1905
			batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
						       seq);
1906 1907 1908 1909
			seq_puts(seq, "\n");
			batman_count++;

next:
1910
			batadv_neigh_node_put(neigh_node);
1911
			if (n_ifinfo)
1912
				batadv_neigh_ifinfo_put(n_ifinfo);
1913 1914 1915 1916 1917 1918 1919 1920
		}
		rcu_read_unlock();
	}

	if (batman_count == 0)
		seq_puts(seq, "No batman nodes in range ...\n");
}

1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
/**
 * batadv_iv_hardif_neigh_print - print a single hop neighbour node
 * @seq: neighbour table seq_file struct
 * @hardif_neigh: hardif neighbour information
 */
static void
batadv_iv_hardif_neigh_print(struct seq_file *seq,
			     struct batadv_hardif_neigh_node *hardif_neigh)
{
	int last_secs, last_msecs;

	last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
	last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;

	seq_printf(seq, "   %10s   %pM %4i.%03is\n",
		   hardif_neigh->if_incoming->net_dev->name,
		   hardif_neigh->addr, last_secs, last_msecs);
}

/**
 * batadv_iv_ogm_neigh_print - print the single hop neighbour list
 * @bat_priv: the bat priv with all the soft interface information
 * @seq: neighbour table seq_file struct
 */
static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
				  struct seq_file *seq)
{
	struct net_device *net_dev = (struct net_device *)seq->private;
	struct batadv_hardif_neigh_node *hardif_neigh;
	struct batadv_hard_iface *hard_iface;
	int batman_count = 0;

1953
	seq_puts(seq, "           IF        Neighbor      last-seen\n");
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971

	rcu_read_lock();
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
		if (hard_iface->soft_iface != net_dev)
			continue;

		hlist_for_each_entry_rcu(hardif_neigh,
					 &hard_iface->neigh_list, list) {
			batadv_iv_hardif_neigh_print(seq, hardif_neigh);
			batman_count++;
		}
	}
	rcu_read_unlock();

	if (batman_count == 0)
		seq_puts(seq, "No batman nodes in range ...\n");
}

1972 1973 1974
/**
 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
 * @neigh1: the first neighbor object of the comparison
1975
 * @if_outgoing1: outgoing interface for the first neighbor
1976
 * @neigh2: the second neighbor object of the comparison
1977
 * @if_outgoing2: outgoing interface for the second neighbor
1978
 *
1979
 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
1980 1981 1982
 * lower, the same as or higher than the metric via neigh2
 */
static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
1983 1984 1985
				   struct batadv_hard_iface *if_outgoing1,
				   struct batadv_neigh_node *neigh2,
				   struct batadv_hard_iface *if_outgoing2)
1986
{
1987
	struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
1988
	u8 tq1, tq2;
1989 1990 1991 1992
	int diff;

	neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
	neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
1993

1994 1995 1996 1997
	if (!neigh1_ifinfo || !neigh2_ifinfo) {
		diff = 0;
		goto out;
	}
1998

1999 2000 2001 2002 2003 2004
	tq1 = neigh1_ifinfo->bat_iv.tq_avg;
	tq2 = neigh2_ifinfo->bat_iv.tq_avg;
	diff = tq1 - tq2;

out:
	if (neigh1_ifinfo)
2005
		batadv_neigh_ifinfo_put(neigh1_ifinfo);
2006
	if (neigh2_ifinfo)
2007
		batadv_neigh_ifinfo_put(neigh2_ifinfo);
2008 2009

	return diff;
2010 2011
}

2012
/**
2013 2014
 * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
 *  than neigh2 from the metric prospective
2015
 * @neigh1: the first neighbor object of the comparison
2016
 * @if_outgoing1: outgoing interface for the first neighbor
2017
 * @neigh2: the second neighbor object of the comparison
2018
 * @if_outgoing2: outgoing interface for the second neighbor
2019
 *
2020
 * Return: true if the metric via neigh1 is equally good or better than
2021
 * the metric via neigh2, false otherwise.
2022
 */
2023
static bool
2024
batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
2025 2026 2027
			   struct batadv_hard_iface *if_outgoing1,
			   struct batadv_neigh_node *neigh2,
			   struct batadv_hard_iface *if_outgoing2)
2028
{
2029
	struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2030
	u8 tq1, tq2;
2031
	bool ret;
2032

2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
	neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
	neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);

	/* we can't say that the metric is better */
	if (!neigh1_ifinfo || !neigh2_ifinfo) {
		ret = false;
		goto out;
	}

	tq1 = neigh1_ifinfo->bat_iv.tq_avg;
	tq2 = neigh2_ifinfo->bat_iv.tq_avg;
	ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD;

out:
	if (neigh1_ifinfo)
2048
		batadv_neigh_ifinfo_put(neigh1_ifinfo);
2049
	if (neigh2_ifinfo)
2050
		batadv_neigh_ifinfo_put(neigh2_ifinfo);
2051 2052

	return ret;
2053 2054
}

2055
static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2056
	.name = "BATMAN_IV",
2057 2058 2059 2060 2061 2062
	.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,
2063
	.bat_neigh_cmp = batadv_iv_ogm_neigh_cmp,
2064
	.bat_neigh_is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2065
	.bat_neigh_print = batadv_iv_neigh_print,
2066
	.bat_orig_print = batadv_iv_ogm_orig_print,
2067 2068 2069
	.bat_orig_free = batadv_iv_ogm_orig_free,
	.bat_orig_add_if = batadv_iv_ogm_orig_add_if,
	.bat_orig_del_if = batadv_iv_ogm_orig_del_if,
2070 2071
};

2072
int __init batadv_iv_init(void)
2073
{
2074 2075 2076
	int ret;

	/* batman originator packet */
2077 2078
	ret = batadv_recv_handler_register(BATADV_IV_OGM,
					   batadv_iv_ogm_receive);
2079 2080 2081
	if (ret < 0)
		goto out;

2082
	ret = batadv_algo_register(&batadv_batman_iv);
2083 2084 2085 2086 2087 2088
	if (ret < 0)
		goto handler_unregister;

	goto out;

handler_unregister:
2089
	batadv_recv_handler_unregister(BATADV_IV_OGM);
2090 2091
out:
	return ret;
2092
}