routing.c 35.8 KB
Newer Older
1
/* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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
#include "network-coding.h"
33

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

37
void batadv_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
38
{
39
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
40
	struct batadv_hashtable *hash = bat_priv->orig_hash;
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, 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
		batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
84 85
			   "Adding route towards: %pM (via %pM)\n",
			   orig_node->orig, neigh_node->addr);
86
	/* route changed */
87
	} else if (neigh_node && curr_router) {
88
		batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
89 90 91
			   "Changing route towards: %pM (now via %pM - was via %pM)\n",
			   orig_node->orig, neigh_node->addr,
			   curr_router->addr);
92 93
	}

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

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

	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)
107
		batadv_neigh_node_free_ref(curr_router);
108 109
}

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

	if (!orig_node)
117 118
		goto out;

119
	router = batadv_orig_node_get_router(orig_node);
120

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

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

129
/* caller must hold the neigh_list_lock */
130 131
void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
				  struct batadv_neigh_node *neigh_node)
132 133 134 135 136 137 138
{
	/* 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);
139
	batadv_neigh_node_free_ref(neigh_node);
140 141 142 143 144 145
	atomic_dec(&orig_node->bond_candidates);

out:
	return;
}

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

	spin_lock_bh(&orig_node->neigh_list_lock);

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

159
	router = batadv_orig_node_get_router(orig_node);
160
	if (!router)
161 162 163
		goto candidate_del;

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

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

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

		if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
183 184
		    (batadv_compare_eth(neigh_node->addr,
					tmp_neigh_node->addr))) {
185 186 187 188 189 190 191 192 193 194 195 196 197
			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;

198 199 200
	if (!atomic_inc_not_zero(&neigh_node->refcount))
		goto out;

201 202 203 204 205
	list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
	atomic_inc(&orig_node->bond_candidates);
	goto out;

candidate_del:
206
	batadv_bonding_candidate_del(orig_node, neigh_node);
207 208 209

out:
	spin_unlock_bh(&orig_node->neigh_list_lock);
210 211

	if (router)
212
		batadv_neigh_node_free_ref(router);
213 214 215
}

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

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

227 228 229 230 231
/* 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.
 */
232
int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
233
			    unsigned long *last_reset)
234
{
235 236 237 238
	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))
239
			return 1;
240 241

		*last_reset = jiffies;
242
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
243
			   "old packet received, start protection\n");
244
	}
245

246 247 248
	return 0;
}

249
bool batadv_check_management_packet(struct sk_buff *skb,
250
				    struct batadv_hard_iface *hard_iface,
251
				    int header_len)
252 253 254 255
{
	struct ethhdr *ethhdr;

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

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

	/* packet with broadcast indication but unicast recipient */
	if (!is_broadcast_ether_addr(ethhdr->h_dest))
263
		return false;
264 265 266

	/* packet with broadcast sender address */
	if (is_broadcast_ether_addr(ethhdr->h_source))
267
		return false;
268 269 270

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

	/* keep skb linear */
	if (skb_linearize(skb) < 0)
275
		return false;
276

277
	return true;
278 279
}

280
static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
281
				      struct sk_buff *skb, size_t icmp_len)
282
{
283 284
	struct batadv_hard_iface *primary_if = NULL;
	struct batadv_orig_node *orig_node = NULL;
285
	struct batadv_icmp_packet_rr *icmp_packet;
286
	int ret = NET_RX_DROP;
287

288
	icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
289 290

	/* add data to device queue */
291
	if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
292
		batadv_socket_receive_packet(icmp_packet, icmp_len);
293
		goto out;
294 295
	}

296
	primary_if = batadv_primary_if_get_selected(bat_priv);
297
	if (!primary_if)
298
		goto out;
299 300 301

	/* answer echo request (ping) */
	/* get routing information */
302
	orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
303
	if (!orig_node)
304
		goto out;
305

306
	/* create a copy of the skb, if needed, to modify it. */
307
	if (skb_cow(skb, ETH_HLEN) < 0)
308 309
		goto out;

310
	icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
311 312

	memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
313
	memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
314
	icmp_packet->msg_type = BATADV_ECHO_REPLY;
315
	icmp_packet->header.ttl = BATADV_TTL;
316

317 318
	if (batadv_send_skb_to_orig(skb, orig_node, NULL))
		ret = NET_RX_SUCCESS;
319

320
out:
321
	if (primary_if)
322
		batadv_hardif_free_ref(primary_if);
323
	if (orig_node)
324
		batadv_orig_node_free_ref(orig_node);
325 326 327
	return ret;
}

328
static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
329
					 struct sk_buff *skb)
330
{
331 332
	struct batadv_hard_iface *primary_if = NULL;
	struct batadv_orig_node *orig_node = NULL;
333
	struct batadv_icmp_packet *icmp_packet;
334
	int ret = NET_RX_DROP;
335

336
	icmp_packet = (struct batadv_icmp_packet *)skb->data;
337 338

	/* send TTL exceeded if packet is an echo request (traceroute) */
339
	if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
340 341
		pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
			 icmp_packet->orig, icmp_packet->dst);
342
		goto out;
343 344
	}

345
	primary_if = batadv_primary_if_get_selected(bat_priv);
346
	if (!primary_if)
347
		goto out;
348 349

	/* get routing information */
350
	orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
351
	if (!orig_node)
352
		goto out;
353

354
	/* create a copy of the skb, if needed, to modify it. */
355
	if (skb_cow(skb, ETH_HLEN) < 0)
356
		goto out;
357

358
	icmp_packet = (struct batadv_icmp_packet *)skb->data;
359

360
	memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
361
	memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
362
	icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
363
	icmp_packet->header.ttl = BATADV_TTL;
364

365 366
	if (batadv_send_skb_to_orig(skb, orig_node, NULL))
		ret = NET_RX_SUCCESS;
367

368
out:
369
	if (primary_if)
370
		batadv_hardif_free_ref(primary_if);
371
	if (orig_node)
372
		batadv_orig_node_free_ref(orig_node);
373 374 375 376
	return ret;
}


377 378
int batadv_recv_icmp_packet(struct sk_buff *skb,
			    struct batadv_hard_iface *recv_if)
379
{
380
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
381
	struct batadv_icmp_packet_rr *icmp_packet;
382
	struct ethhdr *ethhdr;
383
	struct batadv_orig_node *orig_node = NULL;
384
	int hdr_size = sizeof(struct batadv_icmp_packet);
385
	int ret = NET_RX_DROP;
386

387
	/* we truncate all incoming icmp packets if they don't match our size */
388 389
	if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
		hdr_size = sizeof(struct batadv_icmp_packet_rr);
390 391 392

	/* drop packet if it has not necessary minimum size */
	if (unlikely(!pskb_may_pull(skb, hdr_size)))
393
		goto out;
394 395 396 397 398

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

	/* packet with unicast indication but broadcast recipient */
	if (is_broadcast_ether_addr(ethhdr->h_dest))
399
		goto out;
400 401 402

	/* packet with broadcast sender address */
	if (is_broadcast_ether_addr(ethhdr->h_source))
403
		goto out;
404 405

	/* not for me */
406
	if (!batadv_is_my_mac(ethhdr->h_dest))
407
		goto out;
408

409
	icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
410 411

	/* add record route information if not full */
412
	if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
413
	    (icmp_packet->rr_cur < BATADV_RR_LEN)) {
414
		memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
415
		       ethhdr->h_dest, ETH_ALEN);
416 417 418 419
		icmp_packet->rr_cur++;
	}

	/* packet for me */
420
	if (batadv_is_my_mac(icmp_packet->dst))
421
		return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
422 423

	/* TTL exceeded */
424
	if (icmp_packet->header.ttl < 2)
425
		return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
426 427

	/* get routing information */
428
	orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
429
	if (!orig_node)
430
		goto out;
431

432
	/* create a copy of the skb, if needed, to modify it. */
433
	if (skb_cow(skb, ETH_HLEN) < 0)
434
		goto out;
435

436
	icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
437

438
	/* decrement ttl */
439
	icmp_packet->header.ttl--;
440 441

	/* route it */
442 443
	if (batadv_send_skb_to_orig(skb, orig_node, recv_if))
		ret = NET_RX_SUCCESS;
444

445 446
out:
	if (orig_node)
447
		batadv_orig_node_free_ref(orig_node);
448 449 450
	return ret;
}

451 452 453 454
/* 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
455 456
 * returned router's refcount.
 */
457 458 459
static struct batadv_neigh_node *
batadv_find_bond_router(struct batadv_orig_node *primary_orig,
			const struct batadv_hard_iface *recv_if)
460
{
461 462
	struct batadv_neigh_node *tmp_neigh_node;
	struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

	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
490 491
	 * after the current router
	 */
492 493
	spin_lock_bh(&primary_orig->neigh_list_lock);
	/* this is a list_move(), which unfortunately
494 495
	 * does not exist as rcu version
	 */
496 497 498 499 500 501 502 503 504 505 506 507 508 509
	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.
 *
510 511
 * Increases the returned router's refcount
 */
512 513 514
static struct batadv_neigh_node *
batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
			   const struct batadv_hard_iface *recv_if)
515
{
516 517
	struct batadv_neigh_node *tmp_neigh_node;
	struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
518 519 520 521 522 523 524 525 526 527 528

	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;

529 530 531
		if (router && tmp_neigh_node->tq_avg <= router->tq_avg)
			continue;

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

535 536 537
		/* decrement refcount of previously selected router */
		if (router)
			batadv_neigh_node_free_ref(router);
538

539 540
		/* we found a better router (or at least one valid router) */
		router = tmp_neigh_node;
541 542 543 544 545 546 547 548 549 550 551
	}

	/* 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;
}

552 553 554 555 556 557 558 559 560 561
/**
 * batadv_check_unicast_packet - Check for malformed unicast packets
 * @skb: packet to check
 * @hdr_size: size of header to pull
 *
 * Check for short header and bad addresses in given packet. Returns negative
 * value when check fails and 0 otherwise. The negative value depends on the
 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
 * and -EREMOTE for non-local (other host) destination.
 */
562 563 564 565 566 567
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)))
568
		return -ENODATA;
569 570 571 572 573

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

	/* packet with unicast indication but broadcast recipient */
	if (is_broadcast_ether_addr(ethhdr->h_dest))
574
		return -EBADR;
575 576 577

	/* packet with broadcast sender address */
	if (is_broadcast_ether_addr(ethhdr->h_source))
578
		return -EBADR;
579 580 581

	/* not for me */
	if (!batadv_is_my_mac(ethhdr->h_dest))
582
		return -EREMOTE;
583 584 585 586

	return 0;
}

587
int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
588
{
589
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
590
	struct batadv_tt_query_packet *tt_query;
591
	uint16_t tt_size;
592
	int hdr_size = sizeof(*tt_query);
593
	char tt_flag;
594
	size_t packet_size;
595

596 597
	if (batadv_check_unicast_packet(skb, hdr_size) < 0)
		return NET_RX_DROP;
598 599

	/* I could need to modify it */
600
	if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
601 602
		goto out;

603
	tt_query = (struct batadv_tt_query_packet *)skb->data;
604

605
	switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
606
	case BATADV_TT_REQUEST:
607
		batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
608

609
		/* If we cannot provide an answer the tt_request is
610 611
		 * forwarded
		 */
612
		if (!batadv_send_tt_response(bat_priv, tt_query)) {
613 614 615 616 617
			if (tt_query->flags & BATADV_TT_FULL_TABLE)
				tt_flag = 'F';
			else
				tt_flag = '.';

618
			batadv_dbg(BATADV_DBG_TT, bat_priv,
619 620 621
				   "Routing TT_REQUEST to %pM [%c]\n",
				   tt_query->dst,
				   tt_flag);
622
			return batadv_route_unicast_packet(skb, recv_if);
623 624
		}
		break;
625
	case BATADV_TT_RESPONSE:
626
		batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
627

628
		if (batadv_is_my_mac(tt_query->dst)) {
629
			/* packet needs to be linearized to access the TT
630 631
			 * changes
			 */
632 633
			if (skb_linearize(skb) < 0)
				goto out;
634
			/* skb_linearize() possibly changed skb->data */
635
			tt_query = (struct batadv_tt_query_packet *)skb->data;
636

637
			tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
638 639

			/* Ensure we have all the claimed data */
640 641 642
			packet_size = sizeof(struct batadv_tt_query_packet);
			packet_size += tt_size;
			if (unlikely(skb_headlen(skb) < packet_size))
643 644
				goto out;

645
			batadv_handle_tt_response(bat_priv, tt_query);
646
		} else {
647 648 649 650
			if (tt_query->flags & BATADV_TT_FULL_TABLE)
				tt_flag =  'F';
			else
				tt_flag = '.';
651
			batadv_dbg(BATADV_DBG_TT, bat_priv,
652 653 654
				   "Routing TT_RESPONSE to %pM [%c]\n",
				   tt_query->dst,
				   tt_flag);
655
			return batadv_route_unicast_packet(skb, recv_if);
656 657 658 659 660 661 662 663 664
		}
		break;
	}

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

665
int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
666
{
667
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
668
	struct batadv_roam_adv_packet *roam_adv_packet;
669
	struct batadv_orig_node *orig_node;
670

671
	if (batadv_check_unicast_packet(skb, sizeof(*roam_adv_packet)) < 0)
672 673
		goto out;

674
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
675

676
	roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
677

678
	if (!batadv_is_my_mac(roam_adv_packet->dst))
679
		return batadv_route_unicast_packet(skb, recv_if);
680

681 682 683 684
	/* check if it is a backbone gateway. we don't accept
	 * roaming advertisement from it, as it has the same
	 * entries as we have.
	 */
685
	if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
686 687
		goto out;

688
	orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
689 690 691
	if (!orig_node)
		goto out;

692
	batadv_dbg(BATADV_DBG_TT, bat_priv,
693 694
		   "Received ROAMING_ADV from %pM (client %pM)\n",
		   roam_adv_packet->src, roam_adv_packet->client);
695

696
	batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
697
			     BATADV_TT_CLIENT_ROAM,
698
			     atomic_read(&orig_node->last_ttvn) + 1);
699

700
	batadv_orig_node_free_ref(orig_node);
701 702 703 704 705
out:
	/* returning NET_RX_DROP will make the caller function kfree the skb */
	return NET_RX_DROP;
}

706
/* find a suitable router for this originator, and use
707
 * bonding if possible. increases the found neighbors
708 709
 * refcount.
 */
710 711 712 713
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)
714
{
715 716 717
	struct batadv_orig_node *primary_orig_node;
	struct batadv_orig_node *router_orig;
	struct batadv_neigh_node *router;
718 719
	static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
	int bonding_enabled;
720
	uint8_t *primary_addr;
721 722 723 724

	if (!orig_node)
		return NULL;

725
	router = batadv_orig_node_get_router(orig_node);
726
	if (!router)
727
		goto err;
728 729

	/* without bonding, the first node should
730 731
	 * always choose the default router.
	 */
732 733
	bonding_enabled = atomic_read(&bat_priv->bonding);

734 735
	rcu_read_lock();
	/* select default router to output */
736
	router_orig = router->orig_node;
737 738
	if (!router_orig)
		goto err_unlock;
739 740 741

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

743 744
	primary_addr = router_orig->primary_addr;

745
	/* if we have something in the primary_addr, we can search
746 747
	 * for a potential bonding candidate.
	 */
748
	if (batadv_compare_eth(primary_addr, zero_mac))
749
		goto return_router;
750 751

	/* find the orig_node which has the primary interface. might
752 753
	 * even be the same as our router_orig in many cases
	 */
754
	if (batadv_compare_eth(primary_addr, router_orig->orig)) {
755 756
		primary_orig_node = router_orig;
	} else {
757 758
		primary_orig_node = batadv_orig_hash_find(bat_priv,
							  primary_addr);
759
		if (!primary_orig_node)
760
			goto return_router;
761

762
		batadv_orig_node_free_ref(primary_orig_node);
763 764 765
	}

	/* with less than 2 candidates, we can't do any
766 767
	 * bonding and prefer the original router.
	 */
768 769
	if (atomic_read(&primary_orig_node->bond_candidates) < 2)
		goto return_router;
770 771 772

	/* all nodes between should choose a candidate which
	 * is is not on the interface where the packet came
773 774
	 * in.
	 */
775
	batadv_neigh_node_free_ref(router);
776

777
	if (bonding_enabled)
778
		router = batadv_find_bond_router(primary_orig_node, recv_if);
779
	else
780
		router = batadv_find_ifalter_router(primary_orig_node, recv_if);
781

782
return_router:
783
	if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
784 785
		goto err_unlock;

786
	rcu_read_unlock();
787
	return router;
788 789 790 791
err_unlock:
	rcu_read_unlock();
err:
	if (router)
792
		batadv_neigh_node_free_ref(router);
793
	return NULL;
794 795
}

796
static int batadv_route_unicast_packet(struct sk_buff *skb,
797
				       struct batadv_hard_iface *recv_if)
798
{
799 800 801
	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;
802
	struct batadv_unicast_packet *unicast_packet;
803
	struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
804
	int ret = NET_RX_DROP;
805 806
	struct sk_buff *new_skb;

807
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
808 809

	/* TTL exceeded */
810
	if (unicast_packet->header.ttl < 2) {
811 812
		pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
			 ethhdr->h_source, unicast_packet->dest);
813
		goto out;
814 815 816
	}

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

819
	if (!orig_node)
820
		goto out;
821

822
	/* find_router() increases neigh_nodes refcount if found. */
823
	neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
824

825
	if (!neigh_node)
826
		goto out;
827 828

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

832
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
833

834
	if (unicast_packet->header.packet_type == BATADV_UNICAST &&
835
	    atomic_read(&bat_priv->fragmentation) &&
836
	    skb->len > neigh_node->if_incoming->net_dev->mtu) {
837 838 839
		ret = batadv_frag_send_skb(skb, bat_priv,
					   neigh_node->if_incoming,
					   neigh_node->addr);
840 841
		goto out;
	}
842

843
	if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
844 845
	    batadv_frag_can_reassemble(skb,
				       neigh_node->if_incoming->net_dev->mtu)) {
846
		ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
847 848

		if (ret == NET_RX_DROP)
849
			goto out;
850 851

		/* packet was buffered for late merge */
852 853 854 855
		if (!new_skb) {
			ret = NET_RX_SUCCESS;
			goto out;
		}
856 857

		skb = new_skb;
858
		unicast_packet = (struct batadv_unicast_packet *)skb->data;
859 860 861
	}

	/* decrement ttl */
862
	unicast_packet->header.ttl--;
863

864 865
	/* network code packet if possible */
	if (batadv_nc_skb_forward(skb, neigh_node, ethhdr)) {
866
		ret = NET_RX_SUCCESS;
867 868 869 870 871 872 873 874
	} else if (batadv_send_skb_to_orig(skb, orig_node, recv_if)) {
		ret = NET_RX_SUCCESS;

		/* Update stats counter */
		batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
		batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
				   skb->len + ETH_HLEN);
	}
875

876 877
out:
	if (neigh_node)
878
		batadv_neigh_node_free_ref(neigh_node);
879
	if (orig_node)
880
		batadv_orig_node_free_ref(orig_node);
881
	return ret;
882 883
}

884 885 886 887 888 889 890 891 892 893 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
/**
 * 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;
}

938
static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
939
				     struct sk_buff *skb) {
940
	uint8_t curr_ttvn, old_ttvn;
941
	struct batadv_orig_node *orig_node;
942
	struct ethhdr *ethhdr;
943
	struct batadv_hard_iface *primary_if;
944
	struct batadv_unicast_packet *unicast_packet;
945
	int is_old_ttvn;
946

947 948 949 950 951 952
	/* 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)
953 954
		return 0;

955
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
956
	ethhdr = (struct ethhdr *)(skb->data + sizeof(*unicast_packet));
957

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
	/* 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)) {
985 986
		orig_node = batadv_orig_hash_find(bat_priv,
						  unicast_packet->dest);
987 988 989 990
		/* 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
		 */
991 992 993 994
		if (!orig_node)
			return 0;

		curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
995
		batadv_orig_node_free_ref(orig_node);
996 997
	}

998 999 1000
	/* check if the TTVN contained in the packet is fresher than what the
	 * node knows
	 */
1001
	is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
1002 1003
	if (!is_old_ttvn)
		return 1;
1004

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	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;
	}
1018

1019 1020 1021 1022 1023 1024
	/* 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;
1025

1026 1027 1028 1029 1030 1031
	/* 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;
1032

1033 1034 1035 1036 1037
	memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);

	batadv_hardif_free_ref(primary_if);

	unicast_packet->ttvn = curr_ttvn;
1038 1039 1040 1041

	return 1;
}

1042 1043
int batadv_recv_unicast_packet(struct sk_buff *skb,
			       struct batadv_hard_iface *recv_if)
1044
{
1045
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1046
	struct batadv_unicast_packet *unicast_packet;
1047
	struct batadv_unicast_4addr_packet *unicast_4addr_packet;
1048 1049
	uint8_t *orig_addr;
	struct batadv_orig_node *orig_node = NULL;
1050
	int check, hdr_size = sizeof(*unicast_packet);
1051
	bool is4addr;
1052

1053
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
1054
	unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
1055

1056
	is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
1057
	/* the caller function should have already pulled 2 bytes */
1058
	if (is4addr)
1059
		hdr_size = sizeof(*unicast_4addr_packet);
1060

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	/* function returns -EREMOTE for promiscuous packets */
	check = batadv_check_unicast_packet(skb, hdr_size);

	/* Even though the packet is not for us, we might save it to use for
	 * decoding a later received coded packet
	 */
	if (check == -EREMOTE)
		batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);

	if (check < 0)
1071 1072
		return NET_RX_DROP;

1073
	if (!batadv_check_unicast_ttvn(bat_priv, skb))
1074 1075
		return NET_RX_DROP;

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

1085 1086 1087 1088 1089 1090 1091
		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;

1092
		batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1093
				    orig_node);
1094

1095
rx_success:
1096 1097 1098
		if (orig_node)
			batadv_orig_node_free_ref(orig_node);

1099 1100 1101
		return NET_RX_SUCCESS;
	}

1102
	return batadv_route_unicast_packet(skb, recv_if);
1103 1104
}

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

1114
	if (batadv_check_unicast_packet(skb, hdr_size) < 0)
1115 1116
		return NET_RX_DROP;

1117
	if (!batadv_check_unicast_ttvn(bat_priv, skb))
1118 1119
		return NET_RX_DROP;

1120
	unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
1121 1122

	/* packet for me */
1123
	if (batadv_is_my_mac(unicast_packet->dest)) {
1124
		ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
1125 1126 1127 1128 1129 1130 1131 1132

		if (ret == NET_RX_DROP)
			return NET_RX_DROP;

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

1133 1134 1135 1136 1137 1138 1139
		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;

1140
		batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1141
				    sizeof(struct batadv_unicast_packet), NULL);
1142 1143

rx_success:
1144 1145 1146
		return NET_RX_SUCCESS;
	}

1147
	return batadv_route_unicast_packet(skb, recv_if);
1148 1149 1150
}


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

	/* drop packet if it has not necessary minimum size */
	if (unlikely(!pskb_may_pull(skb, hdr_size)))
1164
		goto out;
1165 1166 1167 1168 1169

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

	/* packet with broadcast indication but unicast recipient */
	if (!is_broadcast_ether_addr(ethhdr->h_dest))
1170
		goto out;
1171 1172 1173

	/* packet with broadcast sender address */
	if (is_broadcast_ether_addr(ethhdr->h_source))
1174
		goto out;
1175 1176

	/* ignore broadcasts sent by myself */
1177
	if (batadv_is_my_mac(ethhdr->h_source))
1178
		goto out;
1179

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

	/* ignore broadcasts originated by myself */
1183
	if (batadv_is_my_mac(bcast_packet->orig))
1184
		goto out;
1185

1186
	if (bcast_packet->header.ttl < 2)
1187
		goto out;
1188

1189
	orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
1190 1191

	if (!orig_node)
1192
		goto out;
1193

1194
	spin_lock_bh(&orig_node->bcast_seqno_lock);
1195 1196

	/* check whether the packet is a duplicate */
1197 1198
	if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
			    ntohl(bcast_packet->seqno)))
1199
		goto spin_unlock;
1200 1201 1202 1203

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

	/* check whether the packet is old and the host just restarted. */
1204 1205
	if (batadv_window_protected(bat_priv, seq_diff,
				    &orig_node->bcast_seqno_reset))
1206
		goto spin_unlock;
1207 1208

	/* mark broadcast in flood history, update window position
1209 1210
	 * if required.
	 */
1211
	if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1212 1213
		orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);

1214 1215
	spin_unlock_bh(&orig_node->bcast_seqno_lock);

1216
	/* check whether this has been sent by another originator before */
1217
	if (batadv_bla_check_bcast_duplist(bat_priv, skb))
1218 1219
		goto out;

1220
	/* rebroadcast packet */
1221
	batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
1222

1223 1224 1225
	/* don't hand the broadcast up if it is from an originator
	 * from the same backbone.
	 */
1226
	if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
1227 1228
		goto out;

1229 1230 1231 1232 1233
	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;

1234
	/* broadcast for me */
1235 1236
	batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
			    orig_node);
1237 1238

rx_success:
1239 1240
	ret = NET_RX_SUCCESS;
	goto out;
1241

1242 1243 1244 1245
spin_unlock:
	spin_unlock_bh(&orig_node->bcast_seqno_lock);
out:
	if (orig_node)
1246
		batadv_orig_node_free_ref(orig_node);
1247
	return ret;
1248 1249
}

1250 1251
int batadv_recv_vis_packet(struct sk_buff *skb,
			   struct batadv_hard_iface *recv_if)
1252
{
1253
	struct batadv_vis_packet *vis_packet;
1254
	struct ethhdr *ethhdr;
1255
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1256
	int hdr_size = sizeof(*vis_packet);
1257 1258 1259 1260 1261 1262 1263 1264

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

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

1265
	vis_packet = (struct batadv_vis_packet *)skb->data;
1266 1267 1268
	ethhdr = (struct ethhdr *)skb_mac_header(skb);

	/* not for me */
1269
	if (!batadv_is_my_mac(ethhdr->h_dest))
1270 1271 1272
		return NET_RX_DROP;

	/* ignore own packets */
1273
	if (batadv_is_my_mac(vis_packet->vis_orig))
1274 1275
		return NET_RX_DROP;

1276
	if (batadv_is_my_mac(vis_packet->sender_orig))
1277 1278 1279
		return NET_RX_DROP;

	switch (vis_packet->vis_type) {
1280
	case BATADV_VIS_TYPE_SERVER_SYNC:
1281 1282
		batadv_receive_server_sync_packet(bat_priv, vis_packet,
						  skb_headlen(skb));
1283 1284
		break;

1285
	case BATADV_VIS_TYPE_CLIENT_UPDATE:
1286 1287
		batadv_receive_client_update_packet(bat_priv, vis_packet,
						    skb_headlen(skb));
1288 1289 1290 1291 1292 1293 1294
		break;

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

	/* We take a copy of the data in the packet, so we should
1295 1296
	 * always free the skbuf.
	 */
1297 1298
	return NET_RX_DROP;
}