routing.c 35.7 KB
Newer Older
1
/* Copyright (C) 2007-2012 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 28 29
 *
 * 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 "routing.h"
#include "send.h"
#include "soft-interface.h"
#include "hard-interface.h"
#include "icmp_socket.h"
#include "translation-table.h"
#include "originator.h"
#include "vis.h"
#include "unicast.h"
30
#include "bridge_loop_avoidance.h"
31
#include "distributed-arp-table.h"
32

33
static int batadv_route_unicast_packet(struct sk_buff *skb,
34
				       struct batadv_hard_iface *recv_if);
35

36
void batadv_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
37
{
38
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
39
	struct batadv_hashtable *hash = bat_priv->orig_hash;
40
	struct hlist_node *node;
41
	struct hlist_head *head;
42
	struct batadv_orig_node *orig_node;
43
	unsigned long *word;
44
	uint32_t i;
45
	size_t word_index;
46
	uint8_t *w;
47 48 49 50

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

51
		rcu_read_lock();
52
		hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
53
			spin_lock_bh(&orig_node->ogm_cnt_lock);
54
			word_index = hard_iface->if_num * BATADV_NUM_WORDS;
55 56
			word = &(orig_node->bcast_own[word_index]);

57
			batadv_bit_get_packet(bat_priv, word, 1, 0);
58 59
			w = &orig_node->bcast_own_sum[hard_iface->if_num];
			*w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
60
			spin_unlock_bh(&orig_node->ogm_cnt_lock);
61
		}
62
		rcu_read_unlock();
63 64 65
	}
}

66 67 68
static void _batadv_update_route(struct batadv_priv *bat_priv,
				 struct batadv_orig_node *orig_node,
				 struct batadv_neigh_node *neigh_node)
69
{
70
	struct batadv_neigh_node *curr_router;
71

72
	curr_router = batadv_orig_node_get_router(orig_node);
73

74
	/* route deleted */
75
	if ((curr_router) && (!neigh_node)) {
76 77
		batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
			   "Deleting route towards: %pM\n", orig_node->orig);
78 79
		batadv_tt_global_del_orig(bat_priv, orig_node,
					  "Deleted route towards originator");
80

81 82
	/* route added */
	} else if ((!curr_router) && (neigh_node)) {
83

84
		batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
85 86
			   "Adding route towards: %pM (via %pM)\n",
			   orig_node->orig, neigh_node->addr);
87
	/* route changed */
88
	} else if (neigh_node && curr_router) {
89
		batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
90 91 92
			   "Changing route towards: %pM (now via %pM - was via %pM)\n",
			   orig_node->orig, neigh_node->addr,
			   curr_router->addr);
93 94
	}

95
	if (curr_router)
96
		batadv_neigh_node_free_ref(curr_router);
97 98

	/* increase refcount of new best neighbor */
99 100
	if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
		neigh_node = NULL;
101 102 103 104 105 106 107

	spin_lock_bh(&orig_node->neigh_list_lock);
	rcu_assign_pointer(orig_node->router, neigh_node);
	spin_unlock_bh(&orig_node->neigh_list_lock);

	/* decrease refcount of previous best neighbor */
	if (curr_router)
108
		batadv_neigh_node_free_ref(curr_router);
109 110
}

111 112 113
void batadv_update_route(struct batadv_priv *bat_priv,
			 struct batadv_orig_node *orig_node,
			 struct batadv_neigh_node *neigh_node)
114
{
115
	struct batadv_neigh_node *router = NULL;
116 117

	if (!orig_node)
118 119
		goto out;

120
	router = batadv_orig_node_get_router(orig_node);
121

122
	if (router != neigh_node)
123
		_batadv_update_route(bat_priv, orig_node, neigh_node);
124 125 126

out:
	if (router)
127
		batadv_neigh_node_free_ref(router);
128 129
}

130
/* caller must hold the neigh_list_lock */
131 132
void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
				  struct batadv_neigh_node *neigh_node)
133 134 135 136 137 138 139
{
	/* this neighbor is not part of our candidate list */
	if (list_empty(&neigh_node->bonding_list))
		goto out;

	list_del_rcu(&neigh_node->bonding_list);
	INIT_LIST_HEAD(&neigh_node->bonding_list);
140
	batadv_neigh_node_free_ref(neigh_node);
141 142 143 144 145 146
	atomic_dec(&orig_node->bond_candidates);

out:
	return;
}

147 148
void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
				  struct batadv_neigh_node *neigh_node)
149 150
{
	struct hlist_node *node;
151
	struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
152
	uint8_t interference_candidate = 0;
153 154 155 156

	spin_lock_bh(&orig_node->neigh_list_lock);

	/* only consider if it has the same primary address ...  */
157 158
	if (!batadv_compare_eth(orig_node->orig,
				neigh_node->orig_node->primary_addr))
159 160
		goto candidate_del;

161
	router = batadv_orig_node_get_router(orig_node);
162
	if (!router)
163 164 165
		goto candidate_del;

	/* ... and is good enough to be considered */
166
	if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
167 168
		goto candidate_del;

169
	/* check if we have another candidate with the same mac address or
170 171 172 173 174 175 176 177 178 179
	 * interface. If we do, we won't select this candidate because of
	 * possible interference.
	 */
	hlist_for_each_entry_rcu(tmp_neigh_node, node,
				 &orig_node->neigh_list, list) {

		if (tmp_neigh_node == neigh_node)
			continue;

		/* we only care if the other candidate is even
180 181
		 * considered as candidate.
		 */
182 183 184 185
		if (list_empty(&tmp_neigh_node->bonding_list))
			continue;

		if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
186 187
		    (batadv_compare_eth(neigh_node->addr,
					tmp_neigh_node->addr))) {
188 189 190 191 192 193 194 195 196 197 198 199 200
			interference_candidate = 1;
			break;
		}
	}

	/* don't care further if it is an interference candidate */
	if (interference_candidate)
		goto candidate_del;

	/* this neighbor already is part of our candidate list */
	if (!list_empty(&neigh_node->bonding_list))
		goto out;

201 202 203
	if (!atomic_inc_not_zero(&neigh_node->refcount))
		goto out;

204 205 206 207 208
	list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
	atomic_inc(&orig_node->bond_candidates);
	goto out;

candidate_del:
209
	batadv_bonding_candidate_del(orig_node, neigh_node);
210 211 212

out:
	spin_unlock_bh(&orig_node->neigh_list_lock);
213 214

	if (router)
215
		batadv_neigh_node_free_ref(router);
216 217 218
}

/* copy primary address for bonding */
219
void
220 221
batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
			    struct batadv_orig_node *orig_neigh_node,
222
			    const struct batadv_ogm_packet *batman_ogm_packet)
223
{
224
	if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
225 226 227 228 229
		return;

	memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
}

230 231 232 233 234
/* checks whether the host restarted and is in the protection time.
 * returns:
 *  0 if the packet is to be accepted
 *  1 if the packet is to be ignored.
 */
235
int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
236
			    unsigned long *last_reset)
237
{
238 239 240 241
	if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
	    seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
		if (!batadv_has_timed_out(*last_reset,
					  BATADV_RESET_PROTECTION_MS))
242
			return 1;
243 244

		*last_reset = jiffies;
245
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
246
			   "old packet received, start protection\n");
247
	}
248

249 250 251
	return 0;
}

252
bool batadv_check_management_packet(struct sk_buff *skb,
253
				    struct batadv_hard_iface *hard_iface,
254
				    int header_len)
255 256 257 258
{
	struct ethhdr *ethhdr;

	/* drop packet if it has not necessary minimum size */
259 260
	if (unlikely(!pskb_may_pull(skb, header_len)))
		return false;
261 262 263 264 265

	ethhdr = (struct ethhdr *)skb_mac_header(skb);

	/* packet with broadcast indication but unicast recipient */
	if (!is_broadcast_ether_addr(ethhdr->h_dest))
266
		return false;
267 268 269

	/* packet with broadcast sender address */
	if (is_broadcast_ether_addr(ethhdr->h_source))
270
		return false;
271 272 273

	/* create a copy of the skb, if needed, to modify it. */
	if (skb_cow(skb, 0) < 0)
274
		return false;
275 276 277

	/* keep skb linear */
	if (skb_linearize(skb) < 0)
278
		return false;
279

280
	return true;
281 282
}

283
static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
284
				      struct sk_buff *skb, size_t icmp_len)
285
{
286 287 288
	struct batadv_hard_iface *primary_if = NULL;
	struct batadv_orig_node *orig_node = NULL;
	struct batadv_neigh_node *router = NULL;
289
	struct batadv_icmp_packet_rr *icmp_packet;
290
	int ret = NET_RX_DROP;
291

292
	icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
293 294

	/* add data to device queue */
295
	if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
296
		batadv_socket_receive_packet(icmp_packet, icmp_len);
297
		goto out;
298 299
	}

300
	primary_if = batadv_primary_if_get_selected(bat_priv);
301
	if (!primary_if)
302
		goto out;
303 304 305

	/* answer echo request (ping) */
	/* get routing information */
306
	orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
307
	if (!orig_node)
308
		goto out;
309

310
	router = batadv_orig_node_get_router(orig_node);
311 312
	if (!router)
		goto out;
313

314
	/* create a copy of the skb, if needed, to modify it. */
315
	if (skb_cow(skb, ETH_HLEN) < 0)
316 317
		goto out;

318
	icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
319 320

	memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
321
	memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
322
	icmp_packet->msg_type = BATADV_ECHO_REPLY;
323
	icmp_packet->header.ttl = BATADV_TTL;
324

325
	batadv_send_skb_packet(skb, router->if_incoming, router->addr);
326
	ret = NET_RX_SUCCESS;
327

328
out:
329
	if (primary_if)
330
		batadv_hardif_free_ref(primary_if);
331
	if (router)
332
		batadv_neigh_node_free_ref(router);
333
	if (orig_node)
334
		batadv_orig_node_free_ref(orig_node);
335 336 337
	return ret;
}

338
static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
339
					 struct sk_buff *skb)
340
{
341 342 343
	struct batadv_hard_iface *primary_if = NULL;
	struct batadv_orig_node *orig_node = NULL;
	struct batadv_neigh_node *router = NULL;
344
	struct batadv_icmp_packet *icmp_packet;
345
	int ret = NET_RX_DROP;
346

347
	icmp_packet = (struct batadv_icmp_packet *)skb->data;
348 349

	/* send TTL exceeded if packet is an echo request (traceroute) */
350
	if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
351 352
		pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
			 icmp_packet->orig, icmp_packet->dst);
353
		goto out;
354 355
	}

356
	primary_if = batadv_primary_if_get_selected(bat_priv);
357
	if (!primary_if)
358
		goto out;
359 360

	/* get routing information */
361
	orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
362
	if (!orig_node)
363
		goto out;
364

365
	router = batadv_orig_node_get_router(orig_node);
366 367
	if (!router)
		goto out;
368

369
	/* create a copy of the skb, if needed, to modify it. */
370
	if (skb_cow(skb, ETH_HLEN) < 0)
371
		goto out;
372

373
	icmp_packet = (struct batadv_icmp_packet *)skb->data;
374

375
	memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
376
	memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
377
	icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
378
	icmp_packet->header.ttl = BATADV_TTL;
379

380
	batadv_send_skb_packet(skb, router->if_incoming, router->addr);
381
	ret = NET_RX_SUCCESS;
382

383
out:
384
	if (primary_if)
385
		batadv_hardif_free_ref(primary_if);
386
	if (router)
387
		batadv_neigh_node_free_ref(router);
388
	if (orig_node)
389
		batadv_orig_node_free_ref(orig_node);
390 391 392 393
	return ret;
}


394 395
int batadv_recv_icmp_packet(struct sk_buff *skb,
			    struct batadv_hard_iface *recv_if)
396
{
397
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
398
	struct batadv_icmp_packet_rr *icmp_packet;
399
	struct ethhdr *ethhdr;
400 401
	struct batadv_orig_node *orig_node = NULL;
	struct batadv_neigh_node *router = NULL;
402
	int hdr_size = sizeof(struct batadv_icmp_packet);
403
	int ret = NET_RX_DROP;
404

405
	/* we truncate all incoming icmp packets if they don't match our size */
406 407
	if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
		hdr_size = sizeof(struct batadv_icmp_packet_rr);
408 409 410

	/* drop packet if it has not necessary minimum size */
	if (unlikely(!pskb_may_pull(skb, hdr_size)))
411
		goto out;
412 413 414 415 416

	ethhdr = (struct ethhdr *)skb_mac_header(skb);

	/* packet with unicast indication but broadcast recipient */
	if (is_broadcast_ether_addr(ethhdr->h_dest))
417
		goto out;
418 419 420

	/* packet with broadcast sender address */
	if (is_broadcast_ether_addr(ethhdr->h_source))
421
		goto out;
422 423

	/* not for me */
424
	if (!batadv_is_my_mac(ethhdr->h_dest))
425
		goto out;
426

427
	icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
428 429

	/* add record route information if not full */
430
	if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
431
	    (icmp_packet->rr_cur < BATADV_RR_LEN)) {
432
		memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
433
		       ethhdr->h_dest, ETH_ALEN);
434 435 436 437
		icmp_packet->rr_cur++;
	}

	/* packet for me */
438
	if (batadv_is_my_mac(icmp_packet->dst))
439
		return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
440 441

	/* TTL exceeded */
442
	if (icmp_packet->header.ttl < 2)
443
		return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
444 445

	/* get routing information */
446
	orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
447
	if (!orig_node)
448
		goto out;
449

450
	router = batadv_orig_node_get_router(orig_node);
451 452
	if (!router)
		goto out;
453

454
	/* create a copy of the skb, if needed, to modify it. */
455
	if (skb_cow(skb, ETH_HLEN) < 0)
456
		goto out;
457

458
	icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
459

460
	/* decrement ttl */
461
	icmp_packet->header.ttl--;
462 463

	/* route it */
464
	batadv_send_skb_packet(skb, router->if_incoming, router->addr);
465
	ret = NET_RX_SUCCESS;
466

467
out:
468
	if (router)
469
		batadv_neigh_node_free_ref(router);
470
	if (orig_node)
471
		batadv_orig_node_free_ref(orig_node);
472 473 474
	return ret;
}

475 476 477 478
/* In the bonding case, send the packets in a round
 * robin fashion over the remaining interfaces.
 *
 * This method rotates the bonding list and increases the
479 480
 * returned router's refcount.
 */
481 482 483
static struct batadv_neigh_node *
batadv_find_bond_router(struct batadv_orig_node *primary_orig,
			const struct batadv_hard_iface *recv_if)
484
{
485 486
	struct batadv_neigh_node *tmp_neigh_node;
	struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513

	rcu_read_lock();
	list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
				bonding_list) {
		if (!first_candidate)
			first_candidate = tmp_neigh_node;

		/* recv_if == NULL on the first node. */
		if (tmp_neigh_node->if_incoming == recv_if)
			continue;

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

		router = tmp_neigh_node;
		break;
	}

	/* use the first candidate if nothing was found. */
	if (!router && first_candidate &&
	    atomic_inc_not_zero(&first_candidate->refcount))
		router = first_candidate;

	if (!router)
		goto out;

	/* selected should point to the next element
514 515
	 * after the current router
	 */
516 517
	spin_lock_bh(&primary_orig->neigh_list_lock);
	/* this is a list_move(), which unfortunately
518 519
	 * does not exist as rcu version
	 */
520 521 522 523 524 525 526 527 528 529 530 531 532 533
	list_del_rcu(&primary_orig->bond_list);
	list_add_rcu(&primary_orig->bond_list,
		     &router->bonding_list);
	spin_unlock_bh(&primary_orig->neigh_list_lock);

out:
	rcu_read_unlock();
	return router;
}

/* Interface Alternating: Use the best of the
 * remaining candidates which are not using
 * this interface.
 *
534 535
 * Increases the returned router's refcount
 */
536 537 538
static struct batadv_neigh_node *
batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
			   const struct batadv_hard_iface *recv_if)
539
{
540 541
	struct batadv_neigh_node *tmp_neigh_node;
	struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
542 543 544 545 546 547 548 549 550 551 552

	rcu_read_lock();
	list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
				bonding_list) {
		if (!first_candidate)
			first_candidate = tmp_neigh_node;

		/* recv_if == NULL on the first node. */
		if (tmp_neigh_node->if_incoming == recv_if)
			continue;

553 554 555
		if (router && tmp_neigh_node->tq_avg <= router->tq_avg)
			continue;

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

559 560 561
		/* decrement refcount of previously selected router */
		if (router)
			batadv_neigh_node_free_ref(router);
562

563 564
		/* we found a better router (or at least one valid router) */
		router = tmp_neigh_node;
565 566 567 568 569 570 571 572 573 574 575
	}

	/* use the first candidate if nothing was found. */
	if (!router && first_candidate &&
	    atomic_inc_not_zero(&first_candidate->refcount))
		router = first_candidate;

	rcu_read_unlock();
	return router;
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
{
	struct ethhdr *ethhdr;

	/* drop packet if it has not necessary minimum size */
	if (unlikely(!pskb_may_pull(skb, hdr_size)))
		return -1;

	ethhdr = (struct ethhdr *)skb_mac_header(skb);

	/* packet with unicast indication but broadcast recipient */
	if (is_broadcast_ether_addr(ethhdr->h_dest))
		return -1;

	/* packet with broadcast sender address */
	if (is_broadcast_ether_addr(ethhdr->h_source))
		return -1;

	/* not for me */
	if (!batadv_is_my_mac(ethhdr->h_dest))
		return -1;

	return 0;
}

601
int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
602
{
603
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
604
	struct batadv_tt_query_packet *tt_query;
605
	uint16_t tt_size;
606
	int hdr_size = sizeof(*tt_query);
607
	char tt_flag;
608
	size_t packet_size;
609

610 611
	if (batadv_check_unicast_packet(skb, hdr_size) < 0)
		return NET_RX_DROP;
612 613

	/* I could need to modify it */
614
	if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
615 616
		goto out;

617
	tt_query = (struct batadv_tt_query_packet *)skb->data;
618

619
	switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
620
	case BATADV_TT_REQUEST:
621
		batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
622

623
		/* If we cannot provide an answer the tt_request is
624 625
		 * forwarded
		 */
626
		if (!batadv_send_tt_response(bat_priv, tt_query)) {
627 628 629 630 631
			if (tt_query->flags & BATADV_TT_FULL_TABLE)
				tt_flag = 'F';
			else
				tt_flag = '.';

632
			batadv_dbg(BATADV_DBG_TT, bat_priv,
633 634 635
				   "Routing TT_REQUEST to %pM [%c]\n",
				   tt_query->dst,
				   tt_flag);
636
			return batadv_route_unicast_packet(skb, recv_if);
637 638
		}
		break;
639
	case BATADV_TT_RESPONSE:
640
		batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
641

642
		if (batadv_is_my_mac(tt_query->dst)) {
643
			/* packet needs to be linearized to access the TT
644 645
			 * changes
			 */
646 647
			if (skb_linearize(skb) < 0)
				goto out;
648
			/* skb_linearize() possibly changed skb->data */
649
			tt_query = (struct batadv_tt_query_packet *)skb->data;
650

651
			tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
652 653

			/* Ensure we have all the claimed data */
654 655 656
			packet_size = sizeof(struct batadv_tt_query_packet);
			packet_size += tt_size;
			if (unlikely(skb_headlen(skb) < packet_size))
657 658
				goto out;

659
			batadv_handle_tt_response(bat_priv, tt_query);
660
		} else {
661 662 663 664
			if (tt_query->flags & BATADV_TT_FULL_TABLE)
				tt_flag =  'F';
			else
				tt_flag = '.';
665
			batadv_dbg(BATADV_DBG_TT, bat_priv,
666 667 668
				   "Routing TT_RESPONSE to %pM [%c]\n",
				   tt_query->dst,
				   tt_flag);
669
			return batadv_route_unicast_packet(skb, recv_if);
670 671 672 673 674 675 676 677 678
		}
		break;
	}

out:
	/* returning NET_RX_DROP will make the caller function kfree the skb */
	return NET_RX_DROP;
}

679
int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
680
{
681
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
682
	struct batadv_roam_adv_packet *roam_adv_packet;
683
	struct batadv_orig_node *orig_node;
684

685
	if (batadv_check_unicast_packet(skb, sizeof(*roam_adv_packet)) < 0)
686 687
		goto out;

688
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
689

690
	roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
691

692
	if (!batadv_is_my_mac(roam_adv_packet->dst))
693
		return batadv_route_unicast_packet(skb, recv_if);
694

695 696 697 698
	/* check if it is a backbone gateway. we don't accept
	 * roaming advertisement from it, as it has the same
	 * entries as we have.
	 */
699
	if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
700 701
		goto out;

702
	orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
703 704 705
	if (!orig_node)
		goto out;

706
	batadv_dbg(BATADV_DBG_TT, bat_priv,
707 708
		   "Received ROAMING_ADV from %pM (client %pM)\n",
		   roam_adv_packet->src, roam_adv_packet->client);
709

710
	batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
711
			     BATADV_TT_CLIENT_ROAM,
712
			     atomic_read(&orig_node->last_ttvn) + 1);
713

714
	batadv_orig_node_free_ref(orig_node);
715 716 717 718 719
out:
	/* returning NET_RX_DROP will make the caller function kfree the skb */
	return NET_RX_DROP;
}

720
/* find a suitable router for this originator, and use
721
 * bonding if possible. increases the found neighbors
722 723
 * refcount.
 */
724 725 726 727
struct batadv_neigh_node *
batadv_find_router(struct batadv_priv *bat_priv,
		   struct batadv_orig_node *orig_node,
		   const struct batadv_hard_iface *recv_if)
728
{
729 730 731
	struct batadv_orig_node *primary_orig_node;
	struct batadv_orig_node *router_orig;
	struct batadv_neigh_node *router;
732 733
	static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
	int bonding_enabled;
734
	uint8_t *primary_addr;
735 736 737 738

	if (!orig_node)
		return NULL;

739
	router = batadv_orig_node_get_router(orig_node);
740
	if (!router)
741
		goto err;
742 743

	/* without bonding, the first node should
744 745
	 * always choose the default router.
	 */
746 747
	bonding_enabled = atomic_read(&bat_priv->bonding);

748 749
	rcu_read_lock();
	/* select default router to output */
750
	router_orig = router->orig_node;
751 752
	if (!router_orig)
		goto err_unlock;
753 754 755

	if ((!recv_if) && (!bonding_enabled))
		goto return_router;
756

757 758
	primary_addr = router_orig->primary_addr;

759
	/* if we have something in the primary_addr, we can search
760 761
	 * for a potential bonding candidate.
	 */
762
	if (batadv_compare_eth(primary_addr, zero_mac))
763
		goto return_router;
764 765

	/* find the orig_node which has the primary interface. might
766 767
	 * even be the same as our router_orig in many cases
	 */
768
	if (batadv_compare_eth(primary_addr, router_orig->orig)) {
769 770
		primary_orig_node = router_orig;
	} else {
771 772
		primary_orig_node = batadv_orig_hash_find(bat_priv,
							  primary_addr);
773
		if (!primary_orig_node)
774
			goto return_router;
775

776
		batadv_orig_node_free_ref(primary_orig_node);
777 778 779
	}

	/* with less than 2 candidates, we can't do any
780 781
	 * bonding and prefer the original router.
	 */
782 783
	if (atomic_read(&primary_orig_node->bond_candidates) < 2)
		goto return_router;
784 785 786

	/* all nodes between should choose a candidate which
	 * is is not on the interface where the packet came
787 788
	 * in.
	 */
789
	batadv_neigh_node_free_ref(router);
790

791
	if (bonding_enabled)
792
		router = batadv_find_bond_router(primary_orig_node, recv_if);
793
	else
794
		router = batadv_find_ifalter_router(primary_orig_node, recv_if);
795

796
return_router:
797
	if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
798 799
		goto err_unlock;

800
	rcu_read_unlock();
801
	return router;
802 803 804 805
err_unlock:
	rcu_read_unlock();
err:
	if (router)
806
		batadv_neigh_node_free_ref(router);
807
	return NULL;
808 809
}

810
static int batadv_route_unicast_packet(struct sk_buff *skb,
811
				       struct batadv_hard_iface *recv_if)
812
{
813 814 815
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
	struct batadv_orig_node *orig_node = NULL;
	struct batadv_neigh_node *neigh_node = NULL;
816
	struct batadv_unicast_packet *unicast_packet;
817
	struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
818
	int ret = NET_RX_DROP;
819 820
	struct sk_buff *new_skb;

821
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
822 823

	/* TTL exceeded */
824
	if (unicast_packet->header.ttl < 2) {
825 826
		pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
			 ethhdr->h_source, unicast_packet->dest);
827
		goto out;
828 829 830
	}

	/* get routing information */
831
	orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
832

833
	if (!orig_node)
834
		goto out;
835

836
	/* find_router() increases neigh_nodes refcount if found. */
837
	neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
838

839
	if (!neigh_node)
840
		goto out;
841 842

	/* create a copy of the skb, if needed, to modify it. */
843
	if (skb_cow(skb, ETH_HLEN) < 0)
844
		goto out;
845

846
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
847

848
	if (unicast_packet->header.packet_type == BATADV_UNICAST &&
849
	    atomic_read(&bat_priv->fragmentation) &&
850
	    skb->len > neigh_node->if_incoming->net_dev->mtu) {
851 852 853
		ret = batadv_frag_send_skb(skb, bat_priv,
					   neigh_node->if_incoming,
					   neigh_node->addr);
854 855
		goto out;
	}
856

857
	if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
858 859
	    batadv_frag_can_reassemble(skb,
				       neigh_node->if_incoming->net_dev->mtu)) {
860

861
		ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
862 863

		if (ret == NET_RX_DROP)
864
			goto out;
865 866

		/* packet was buffered for late merge */
867 868 869 870
		if (!new_skb) {
			ret = NET_RX_SUCCESS;
			goto out;
		}
871 872

		skb = new_skb;
873
		unicast_packet = (struct batadv_unicast_packet *)skb->data;
874 875 876
	}

	/* decrement ttl */
877
	unicast_packet->header.ttl--;
878

879
	/* Update stats counter */
880 881
	batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
	batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
882 883
			   skb->len + ETH_HLEN);

884
	/* route it */
885
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
886
	ret = NET_RX_SUCCESS;
887

888 889
out:
	if (neigh_node)
890
		batadv_neigh_node_free_ref(neigh_node);
891
	if (orig_node)
892
		batadv_orig_node_free_ref(orig_node);
893
	return ret;
894 895
}

896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
/**
 * batadv_reroute_unicast_packet - update the unicast header for re-routing
 * @bat_priv: the bat priv with all the soft interface information
 * @unicast_packet: the unicast header to be updated
 * @dst_addr: the payload destination
 *
 * Search the translation table for dst_addr and update the unicast header with
 * the new corresponding information (originator address where the destination
 * client currently is and its known TTVN)
 *
 * Returns true if the packet header has been updated, false otherwise
 */
static bool
batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
			      struct batadv_unicast_packet *unicast_packet,
			      uint8_t *dst_addr)
{
	struct batadv_orig_node *orig_node = NULL;
	struct batadv_hard_iface *primary_if = NULL;
	bool ret = false;
	uint8_t *orig_addr, orig_ttvn;

	if (batadv_is_my_client(bat_priv, dst_addr)) {
		primary_if = batadv_primary_if_get_selected(bat_priv);
		if (!primary_if)
			goto out;
		orig_addr = primary_if->net_dev->dev_addr;
		orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
	} else {
		orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr);
		if (!orig_node)
			goto out;

		if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
			goto out;

		orig_addr = orig_node->orig;
		orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
	}

	/* update the packet header */
	memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
	unicast_packet->ttvn = orig_ttvn;

	ret = true;
out:
	if (primary_if)
		batadv_hardif_free_ref(primary_if);
	if (orig_node)
		batadv_orig_node_free_ref(orig_node);

	return ret;
}

950
static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
951
				     struct sk_buff *skb) {
952
	uint8_t curr_ttvn, old_ttvn;
953
	struct batadv_orig_node *orig_node;
954
	struct ethhdr *ethhdr;
955
	struct batadv_hard_iface *primary_if;
956
	struct batadv_unicast_packet *unicast_packet;
957
	int is_old_ttvn;
958

959 960 961 962 963 964
	/* check if there is enough data before accessing it */
	if (pskb_may_pull(skb, sizeof(*unicast_packet) + ETH_HLEN) < 0)
		return 0;

	/* create a copy of the skb (in case of for re-routing) to modify it. */
	if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
965 966
		return 0;

967
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
968
	ethhdr = (struct ethhdr *)(skb->data + sizeof(*unicast_packet));
969

970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
	/* check if the destination client was served by this node and it is now
	 * roaming. In this case, it means that the node has got a ROAM_ADV
	 * message and that it knows the new destination in the mesh to re-route
	 * the packet to
	 */
	if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest)) {
		if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
						  ethhdr->h_dest))
			net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
						 bat_priv,
						 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
						 unicast_packet->dest,
						 ethhdr->h_dest);
		/* at this point the mesh destination should have been
		 * substituted with the originator address found in the global
		 * table. If not, let the packet go untouched anyway because
		 * there is nothing the node can do
		 */
		return 1;
	}

	/* retrieve the TTVN known by this node for the packet destination. This
	 * value is used later to check if the node which sent (or re-routed
	 * last time) the packet had an updated information or not
	 */
	curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
	if (!batadv_is_my_mac(unicast_packet->dest)) {
997 998
		orig_node = batadv_orig_hash_find(bat_priv,
						  unicast_packet->dest);
999 1000 1001 1002
		/* if it is not possible to find the orig_node representing the
		 * destination, the packet can immediately be dropped as it will
		 * not be possible to deliver it
		 */
1003 1004 1005 1006
		if (!orig_node)
			return 0;

		curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
1007
		batadv_orig_node_free_ref(orig_node);
1008 1009
	}

1010 1011 1012
	/* check if the TTVN contained in the packet is fresher than what the
	 * node knows
	 */
1013
	is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
1014 1015
	if (!is_old_ttvn)
		return 1;
1016

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
	old_ttvn = unicast_packet->ttvn;
	/* the packet was forged based on outdated network information. Its
	 * destination can possibly be updated and forwarded towards the new
	 * target host
	 */
	if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
					  ethhdr->h_dest)) {
		net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
					 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
					 unicast_packet->dest, ethhdr->h_dest,
					 old_ttvn, curr_ttvn);
		return 1;
	}
1030

1031 1032 1033 1034 1035 1036
	/* the packet has not been re-routed: either the destination is
	 * currently served by this node or there is no destination at all and
	 * it is possible to drop the packet
	 */
	if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
		return 0;
1037

1038 1039 1040 1041 1042 1043
	/* update the header in order to let the packet be delivered to this
	 * node's soft interface
	 */
	primary_if = batadv_primary_if_get_selected(bat_priv);
	if (!primary_if)
		return 0;
1044

1045 1046 1047 1048 1049
	memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);

	batadv_hardif_free_ref(primary_if);

	unicast_packet->ttvn = curr_ttvn;
1050 1051 1052 1053

	return 1;
}

1054 1055
int batadv_recv_unicast_packet(struct sk_buff *skb,
			       struct batadv_hard_iface *recv_if)
1056
{
1057
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1058
	struct batadv_unicast_packet *unicast_packet;
1059
	struct batadv_unicast_4addr_packet *unicast_4addr_packet;
1060 1061
	uint8_t *orig_addr;
	struct batadv_orig_node *orig_node = NULL;
1062
	int hdr_size = sizeof(*unicast_packet);
1063
	bool is4addr;
1064

1065
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
1066
	unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
1067

1068
	is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
1069
	/* the caller function should have already pulled 2 bytes */
1070
	if (is4addr)
1071
		hdr_size = sizeof(*unicast_4addr_packet);
1072

1073
	if (batadv_check_unicast_packet(skb, hdr_size) < 0)
1074 1075
		return NET_RX_DROP;

1076
	if (!batadv_check_unicast_ttvn(bat_priv, skb))
1077 1078
		return NET_RX_DROP;

1079
	/* packet for me */
1080
	if (batadv_is_my_mac(unicast_packet->dest)) {
1081
		if (is4addr) {
1082 1083
			batadv_dat_inc_counter(bat_priv,
					       unicast_4addr_packet->subtype);
1084 1085 1086
			orig_addr = unicast_4addr_packet->src;
			orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
		}
1087

1088 1089 1090 1091 1092 1093 1094
		if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
							  hdr_size))
			goto rx_success;
		if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
							hdr_size))
			goto rx_success;

1095
		batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1096
				    orig_node);
1097

1098
rx_success:
1099 1100 1101
		if (orig_node)
			batadv_orig_node_free_ref(orig_node);

1102 1103 1104
		return NET_RX_SUCCESS;
	}

1105
	return batadv_route_unicast_packet(skb, recv_if);
1106 1107
}

1108
int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1109
				  struct batadv_hard_iface *recv_if)
1110
{
1111
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1112
	struct batadv_unicast_frag_packet *unicast_packet;
1113
	int hdr_size = sizeof(*unicast_packet);
1114 1115 1116
	struct sk_buff *new_skb = NULL;
	int ret;

1117
	if (batadv_check_unicast_packet(skb, hdr_size) < 0)
1118 1119
		return NET_RX_DROP;

1120
	if (!batadv_check_unicast_ttvn(bat_priv, skb))
1121 1122
		return NET_RX_DROP;

1123
	unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
1124 1125

	/* packet for me */
1126
	if (batadv_is_my_mac(unicast_packet->dest)) {
1127

1128
		ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
1129 1130 1131 1132 1133 1134 1135 1136

		if (ret == NET_RX_DROP)
			return NET_RX_DROP;

		/* packet was buffered for late merge */
		if (!new_skb)
			return NET_RX_SUCCESS;

1137 1138 1139 1140 1141 1142 1143
		if (batadv_dat_snoop_incoming_arp_request(bat_priv, new_skb,
							  hdr_size))
			goto rx_success;
		if (batadv_dat_snoop_incoming_arp_reply(bat_priv, new_skb,
							hdr_size))
			goto rx_success;

1144
		batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1145
				    sizeof(struct batadv_unicast_packet), NULL);
1146 1147

rx_success:
1148 1149 1150
		return NET_RX_SUCCESS;
	}

1151
	return batadv_route_unicast_packet(skb, recv_if);
1152 1153 1154
}


1155 1156
int batadv_recv_bcast_packet(struct sk_buff *skb,
			     struct batadv_hard_iface *recv_if)
1157
{
1158 1159
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
	struct batadv_orig_node *orig_node = NULL;
1160
	struct batadv_bcast_packet *bcast_packet;
1161
	struct ethhdr *ethhdr;
1162
	int hdr_size = sizeof(*bcast_packet);
1163
	int ret = NET_RX_DROP;
1164 1165 1166 1167
	int32_t seq_diff;

	/* drop packet if it has not necessary minimum size */
	if (unlikely(!pskb_may_pull(skb, hdr_size)))
1168
		goto out;
1169 1170 1171 1172 1173

	ethhdr = (struct ethhdr *)skb_mac_header(skb);

	/* packet with broadcast indication but unicast recipient */
	if (!is_broadcast_ether_addr(ethhdr->h_dest))
1174
		goto out;
1175 1176 1177

	/* packet with broadcast sender address */
	if (is_broadcast_ether_addr(ethhdr->h_source))
1178
		goto out;
1179 1180

	/* ignore broadcasts sent by myself */
1181
	if (batadv_is_my_mac(ethhdr->h_source))
1182
		goto out;
1183

1184
	bcast_packet = (struct batadv_bcast_packet *)skb->data;
1185 1186

	/* ignore broadcasts originated by myself */
1187
	if (batadv_is_my_mac(bcast_packet->orig))
1188
		goto out;
1189

1190
	if (bcast_packet->header.ttl < 2)
1191
		goto out;
1192

1193
	orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
1194 1195

	if (!orig_node)
1196
		goto out;
1197

1198
	spin_lock_bh(&orig_node->bcast_seqno_lock);
1199 1200

	/* check whether the packet is a duplicate */
1201 1202
	if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
			    ntohl(bcast_packet->seqno)))
1203
		goto spin_unlock;
1204 1205 1206 1207

	seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;

	/* check whether the packet is old and the host just restarted. */
1208 1209
	if (batadv_window_protected(bat_priv, seq_diff,
				    &orig_node->bcast_seqno_reset))
1210
		goto spin_unlock;
1211 1212

	/* mark broadcast in flood history, update window position
1213 1214
	 * if required.
	 */
1215
	if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1216 1217
		orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);

1218 1219
	spin_unlock_bh(&orig_node->bcast_seqno_lock);

1220 1221 1222 1223 1224 1225
	/* keep skb linear for crc calculation */
	if (skb_linearize(skb) < 0)
		goto out;

	bcast_packet = (struct batadv_bcast_packet *)skb->data;

1226
	/* check whether this has been sent by another originator before */
1227
	if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, skb->len))
1228 1229
		goto out;

1230
	/* rebroadcast packet */
1231
	batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
1232

1233 1234 1235
	/* don't hand the broadcast up if it is from an originator
	 * from the same backbone.
	 */
1236
	if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
1237 1238
		goto out;

1239 1240 1241 1242 1243
	if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
		goto rx_success;
	if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
		goto rx_success;

1244
	/* broadcast for me */
1245 1246
	batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
			    orig_node);
1247 1248

rx_success:
1249 1250
	ret = NET_RX_SUCCESS;
	goto out;
1251

1252 1253 1254 1255
spin_unlock:
	spin_unlock_bh(&orig_node->bcast_seqno_lock);
out:
	if (orig_node)
1256
		batadv_orig_node_free_ref(orig_node);
1257
	return ret;
1258 1259
}

1260 1261
int batadv_recv_vis_packet(struct sk_buff *skb,
			   struct batadv_hard_iface *recv_if)
1262
{
1263
	struct batadv_vis_packet *vis_packet;
1264
	struct ethhdr *ethhdr;
1265
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1266
	int hdr_size = sizeof(*vis_packet);
1267 1268 1269 1270 1271 1272 1273 1274

	/* keep skb linear */
	if (skb_linearize(skb) < 0)
		return NET_RX_DROP;

	if (unlikely(!pskb_may_pull(skb, hdr_size)))
		return NET_RX_DROP;

1275
	vis_packet = (struct batadv_vis_packet *)skb->data;
1276 1277 1278
	ethhdr = (struct ethhdr *)skb_mac_header(skb);

	/* not for me */
1279
	if (!batadv_is_my_mac(ethhdr->h_dest))
1280 1281 1282
		return NET_RX_DROP;

	/* ignore own packets */
1283
	if (batadv_is_my_mac(vis_packet->vis_orig))
1284 1285
		return NET_RX_DROP;

1286
	if (batadv_is_my_mac(vis_packet->sender_orig))
1287 1288 1289
		return NET_RX_DROP;

	switch (vis_packet->vis_type) {
1290
	case BATADV_VIS_TYPE_SERVER_SYNC:
1291 1292
		batadv_receive_server_sync_packet(bat_priv, vis_packet,
						  skb_headlen(skb));
1293 1294
		break;

1295
	case BATADV_VIS_TYPE_CLIENT_UPDATE:
1296 1297
		batadv_receive_client_update_packet(bat_priv, vis_packet,
						    skb_headlen(skb));
1298 1299 1300 1301 1302 1303 1304
		break;

	default:	/* ignore unknown packet */
		break;
	}

	/* We take a copy of the data in the packet, so we should
1305 1306
	 * always free the skbuf.
	 */
1307 1308
	return NET_RX_DROP;
}