routing.c 33.2 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 715

	/* Roaming phase starts: I have new information but the ttvn has not
	 * been incremented yet. This flag will make me check all the incoming
716 717
	 * packets for the correct destination.
	 */
718
	bat_priv->tt.poss_change = true;
719

720
	batadv_orig_node_free_ref(orig_node);
721 722 723 724 725
out:
	/* returning NET_RX_DROP will make the caller function kfree the skb */
	return NET_RX_DROP;
}

726
/* find a suitable router for this originator, and use
727
 * bonding if possible. increases the found neighbors
728 729
 * refcount.
 */
730 731 732 733
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)
734
{
735 736 737
	struct batadv_orig_node *primary_orig_node;
	struct batadv_orig_node *router_orig;
	struct batadv_neigh_node *router;
738 739
	static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
	int bonding_enabled;
740
	uint8_t *primary_addr;
741 742 743 744

	if (!orig_node)
		return NULL;

745
	router = batadv_orig_node_get_router(orig_node);
746
	if (!router)
747
		goto err;
748 749

	/* without bonding, the first node should
750 751
	 * always choose the default router.
	 */
752 753
	bonding_enabled = atomic_read(&bat_priv->bonding);

754 755
	rcu_read_lock();
	/* select default router to output */
756
	router_orig = router->orig_node;
757 758
	if (!router_orig)
		goto err_unlock;
759 760 761

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

763 764
	primary_addr = router_orig->primary_addr;

765
	/* if we have something in the primary_addr, we can search
766 767
	 * for a potential bonding candidate.
	 */
768
	if (batadv_compare_eth(primary_addr, zero_mac))
769
		goto return_router;
770 771

	/* find the orig_node which has the primary interface. might
772 773
	 * even be the same as our router_orig in many cases
	 */
774
	if (batadv_compare_eth(primary_addr, router_orig->orig)) {
775 776
		primary_orig_node = router_orig;
	} else {
777 778
		primary_orig_node = batadv_orig_hash_find(bat_priv,
							  primary_addr);
779
		if (!primary_orig_node)
780
			goto return_router;
781

782
		batadv_orig_node_free_ref(primary_orig_node);
783 784 785
	}

	/* with less than 2 candidates, we can't do any
786 787
	 * bonding and prefer the original router.
	 */
788 789
	if (atomic_read(&primary_orig_node->bond_candidates) < 2)
		goto return_router;
790 791 792

	/* all nodes between should choose a candidate which
	 * is is not on the interface where the packet came
793 794
	 * in.
	 */
795
	batadv_neigh_node_free_ref(router);
796

797
	if (bonding_enabled)
798
		router = batadv_find_bond_router(primary_orig_node, recv_if);
799
	else
800
		router = batadv_find_ifalter_router(primary_orig_node, recv_if);
801

802
return_router:
803
	if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
804 805
		goto err_unlock;

806
	rcu_read_unlock();
807
	return router;
808 809 810 811
err_unlock:
	rcu_read_unlock();
err:
	if (router)
812
		batadv_neigh_node_free_ref(router);
813
	return NULL;
814 815
}

816
static int batadv_route_unicast_packet(struct sk_buff *skb,
817
				       struct batadv_hard_iface *recv_if)
818
{
819 820 821
	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;
822
	struct batadv_unicast_packet *unicast_packet;
823
	struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
824
	int ret = NET_RX_DROP;
825 826
	struct sk_buff *new_skb;

827
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
828 829

	/* TTL exceeded */
830
	if (unicast_packet->header.ttl < 2) {
831 832
		pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
			 ethhdr->h_source, unicast_packet->dest);
833
		goto out;
834 835 836
	}

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

839
	if (!orig_node)
840
		goto out;
841

842
	/* find_router() increases neigh_nodes refcount if found. */
843
	neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
844

845
	if (!neigh_node)
846
		goto out;
847 848

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

852
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
853

854
	if (unicast_packet->header.packet_type == BATADV_UNICAST &&
855
	    atomic_read(&bat_priv->fragmentation) &&
856
	    skb->len > neigh_node->if_incoming->net_dev->mtu) {
857 858 859
		ret = batadv_frag_send_skb(skb, bat_priv,
					   neigh_node->if_incoming,
					   neigh_node->addr);
860 861
		goto out;
	}
862

863
	if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
864 865
	    batadv_frag_can_reassemble(skb,
				       neigh_node->if_incoming->net_dev->mtu)) {
866

867
		ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
868 869

		if (ret == NET_RX_DROP)
870
			goto out;
871 872

		/* packet was buffered for late merge */
873 874 875 876
		if (!new_skb) {
			ret = NET_RX_SUCCESS;
			goto out;
		}
877 878

		skb = new_skb;
879
		unicast_packet = (struct batadv_unicast_packet *)skb->data;
880 881 882
	}

	/* decrement ttl */
883
	unicast_packet->header.ttl--;
884

885
	/* Update stats counter */
886 887
	batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
	batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
888 889
			   skb->len + ETH_HLEN);

890
	/* route it */
891
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
892
	ret = NET_RX_SUCCESS;
893

894 895
out:
	if (neigh_node)
896
		batadv_neigh_node_free_ref(neigh_node);
897
	if (orig_node)
898
		batadv_orig_node_free_ref(orig_node);
899
	return ret;
900 901
}

902
static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
903
				     struct sk_buff *skb) {
904
	uint8_t curr_ttvn;
905
	struct batadv_orig_node *orig_node;
906
	struct ethhdr *ethhdr;
907
	struct batadv_hard_iface *primary_if;
908
	struct batadv_unicast_packet *unicast_packet;
909
	bool tt_poss_change;
910
	int is_old_ttvn;
911

912 913 914 915 916 917
	/* 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)
918 919
		return 0;

920
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
921

922
	if (batadv_is_my_mac(unicast_packet->dest)) {
923 924
		tt_poss_change = bat_priv->tt.poss_change;
		curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
925
	} else {
926 927
		orig_node = batadv_orig_hash_find(bat_priv,
						  unicast_packet->dest);
928 929 930 931 932

		if (!orig_node)
			return 0;

		curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
933
		tt_poss_change = orig_node->tt_poss_change;
934
		batadv_orig_node_free_ref(orig_node);
935 936 937
	}

	/* Check whether I have to reroute the packet */
938 939
	is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
	if (is_old_ttvn || tt_poss_change) {
940
		/* check if there is enough data before accessing it */
941
		if (pskb_may_pull(skb, sizeof(struct batadv_unicast_packet) +
942
				  ETH_HLEN) < 0)
943 944
			return 0;

945
		ethhdr = (struct ethhdr *)(skb->data + sizeof(*unicast_packet));
946 947 948 949

		/* we don't have an updated route for this client, so we should
		 * not try to reroute the packet!!
		 */
950 951
		if (batadv_tt_global_client_is_roaming(bat_priv,
						       ethhdr->h_dest))
952 953
			return 1;

954 955
		orig_node = batadv_transtable_search(bat_priv, NULL,
						     ethhdr->h_dest);
956 957

		if (!orig_node) {
958
			if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
959
				return 0;
960
			primary_if = batadv_primary_if_get_selected(bat_priv);
961 962 963 964
			if (!primary_if)
				return 0;
			memcpy(unicast_packet->dest,
			       primary_if->net_dev->dev_addr, ETH_ALEN);
965
			batadv_hardif_free_ref(primary_if);
966 967 968
		} else {
			memcpy(unicast_packet->dest, orig_node->orig,
			       ETH_ALEN);
969
			curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
970
			batadv_orig_node_free_ref(orig_node);
971 972
		}

973 974 975 976
		net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
					 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
					 unicast_packet->ttvn, curr_ttvn,
					 ethhdr->h_dest, unicast_packet->dest);
977 978 979 980 981 982

		unicast_packet->ttvn = curr_ttvn;
	}
	return 1;
}

983 984
int batadv_recv_unicast_packet(struct sk_buff *skb,
			       struct batadv_hard_iface *recv_if)
985
{
986
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
987
	struct batadv_unicast_packet *unicast_packet;
988
	struct batadv_unicast_4addr_packet *unicast_4addr_packet;
989
	int hdr_size = sizeof(*unicast_packet);
990
	bool is4addr;
991

992
	unicast_packet = (struct batadv_unicast_packet *)skb->data;
993
	unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
994

995
	is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
996
	/* the caller function should have already pulled 2 bytes */
997
	if (is4addr)
998
		hdr_size = sizeof(*unicast_4addr_packet);
999

1000
	if (batadv_check_unicast_packet(skb, hdr_size) < 0)
1001 1002
		return NET_RX_DROP;

1003
	if (!batadv_check_unicast_ttvn(bat_priv, skb))
1004 1005
		return NET_RX_DROP;

1006
	/* packet for me */
1007
	if (batadv_is_my_mac(unicast_packet->dest)) {
1008 1009 1010 1011
		if (is4addr)
			batadv_dat_inc_counter(bat_priv,
					       unicast_4addr_packet->subtype);

1012 1013 1014 1015 1016 1017 1018
		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;

1019 1020 1021
		batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
				    NULL);

1022
rx_success:
1023 1024 1025
		return NET_RX_SUCCESS;
	}

1026
	return batadv_route_unicast_packet(skb, recv_if);
1027 1028
}

1029
int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1030
				  struct batadv_hard_iface *recv_if)
1031
{
1032
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1033
	struct batadv_unicast_frag_packet *unicast_packet;
1034
	int hdr_size = sizeof(*unicast_packet);
1035 1036 1037
	struct sk_buff *new_skb = NULL;
	int ret;

1038
	if (batadv_check_unicast_packet(skb, hdr_size) < 0)
1039 1040
		return NET_RX_DROP;

1041
	if (!batadv_check_unicast_ttvn(bat_priv, skb))
1042 1043
		return NET_RX_DROP;

1044
	unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
1045 1046

	/* packet for me */
1047
	if (batadv_is_my_mac(unicast_packet->dest)) {
1048

1049
		ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
1050 1051 1052 1053 1054 1055 1056 1057

		if (ret == NET_RX_DROP)
			return NET_RX_DROP;

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

1058 1059 1060 1061 1062 1063 1064
		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;

1065
		batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1066
				    sizeof(struct batadv_unicast_packet), NULL);
1067 1068

rx_success:
1069 1070 1071
		return NET_RX_SUCCESS;
	}

1072
	return batadv_route_unicast_packet(skb, recv_if);
1073 1074 1075
}


1076 1077
int batadv_recv_bcast_packet(struct sk_buff *skb,
			     struct batadv_hard_iface *recv_if)
1078
{
1079 1080
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
	struct batadv_orig_node *orig_node = NULL;
1081
	struct batadv_bcast_packet *bcast_packet;
1082
	struct ethhdr *ethhdr;
1083
	int hdr_size = sizeof(*bcast_packet);
1084
	int ret = NET_RX_DROP;
1085 1086 1087 1088
	int32_t seq_diff;

	/* drop packet if it has not necessary minimum size */
	if (unlikely(!pskb_may_pull(skb, hdr_size)))
1089
		goto out;
1090 1091 1092 1093 1094

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

	/* packet with broadcast indication but unicast recipient */
	if (!is_broadcast_ether_addr(ethhdr->h_dest))
1095
		goto out;
1096 1097 1098

	/* packet with broadcast sender address */
	if (is_broadcast_ether_addr(ethhdr->h_source))
1099
		goto out;
1100 1101

	/* ignore broadcasts sent by myself */
1102
	if (batadv_is_my_mac(ethhdr->h_source))
1103
		goto out;
1104

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

	/* ignore broadcasts originated by myself */
1108
	if (batadv_is_my_mac(bcast_packet->orig))
1109
		goto out;
1110

1111
	if (bcast_packet->header.ttl < 2)
1112
		goto out;
1113

1114
	orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
1115 1116

	if (!orig_node)
1117
		goto out;
1118

1119
	spin_lock_bh(&orig_node->bcast_seqno_lock);
1120 1121

	/* check whether the packet is a duplicate */
1122 1123
	if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
			    ntohl(bcast_packet->seqno)))
1124
		goto spin_unlock;
1125 1126 1127 1128

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

	/* check whether the packet is old and the host just restarted. */
1129 1130
	if (batadv_window_protected(bat_priv, seq_diff,
				    &orig_node->bcast_seqno_reset))
1131
		goto spin_unlock;
1132 1133

	/* mark broadcast in flood history, update window position
1134 1135
	 * if required.
	 */
1136
	if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1137 1138
		orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);

1139 1140
	spin_unlock_bh(&orig_node->bcast_seqno_lock);

1141 1142 1143 1144 1145 1146
	/* keep skb linear for crc calculation */
	if (skb_linearize(skb) < 0)
		goto out;

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

1147
	/* check whether this has been sent by another originator before */
1148
	if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, skb->len))
1149 1150
		goto out;

1151
	/* rebroadcast packet */
1152
	batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
1153

1154 1155 1156
	/* don't hand the broadcast up if it is from an originator
	 * from the same backbone.
	 */
1157
	if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
1158 1159
		goto out;

1160 1161 1162 1163 1164
	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;

1165
	/* broadcast for me */
1166 1167
	batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
			    orig_node);
1168 1169

rx_success:
1170 1171
	ret = NET_RX_SUCCESS;
	goto out;
1172

1173 1174 1175 1176
spin_unlock:
	spin_unlock_bh(&orig_node->bcast_seqno_lock);
out:
	if (orig_node)
1177
		batadv_orig_node_free_ref(orig_node);
1178
	return ret;
1179 1180
}

1181 1182
int batadv_recv_vis_packet(struct sk_buff *skb,
			   struct batadv_hard_iface *recv_if)
1183
{
1184
	struct batadv_vis_packet *vis_packet;
1185
	struct ethhdr *ethhdr;
1186
	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1187
	int hdr_size = sizeof(*vis_packet);
1188 1189 1190 1191 1192 1193 1194 1195

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

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

1196
	vis_packet = (struct batadv_vis_packet *)skb->data;
1197 1198 1199
	ethhdr = (struct ethhdr *)skb_mac_header(skb);

	/* not for me */
1200
	if (!batadv_is_my_mac(ethhdr->h_dest))
1201 1202 1203
		return NET_RX_DROP;

	/* ignore own packets */
1204
	if (batadv_is_my_mac(vis_packet->vis_orig))
1205 1206
		return NET_RX_DROP;

1207
	if (batadv_is_my_mac(vis_packet->sender_orig))
1208 1209 1210
		return NET_RX_DROP;

	switch (vis_packet->vis_type) {
1211
	case BATADV_VIS_TYPE_SERVER_SYNC:
1212 1213
		batadv_receive_server_sync_packet(bat_priv, vis_packet,
						  skb_headlen(skb));
1214 1215
		break;

1216
	case BATADV_VIS_TYPE_CLIENT_UPDATE:
1217 1218
		batadv_receive_client_update_packet(bat_priv, vis_packet,
						    skb_headlen(skb));
1219 1220 1221 1222 1223 1224 1225
		break;

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

	/* We take a copy of the data in the packet, so we should
1226 1227
	 * always free the skbuf.
	 */
1228 1229
	return NET_RX_DROP;
}