gateway_client.c 21.4 KB
Newer Older
1
/* Copyright (C) 2009-2013 B.A.T.M.A.N. contributors:
2 3 4 5 6 7 8 9 10 11 12 13 14
 *
 * Marek Lindner
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 17 18
 */

#include "main.h"
19
#include "sysfs.h"
20 21 22
#include "gateway_client.h"
#include "gateway_common.h"
#include "hard-interface.h"
23
#include "originator.h"
24
#include "translation-table.h"
25
#include "routing.h"
26 27 28 29 30
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/udp.h>
#include <linux/if_vlan.h>

31
/* This is the offset of the options field in a dhcp packet starting at
32 33
 * the beginning of the dhcp header
 */
34 35
#define BATADV_DHCP_OPTIONS_OFFSET 240
#define BATADV_DHCP_REQUEST 3
36

37
static void batadv_gw_node_free_ref(struct batadv_gw_node *gw_node)
38
{
39
	if (atomic_dec_and_test(&gw_node->refcount))
40
		kfree_rcu(gw_node, rcu);
41 42
}

43 44
static struct batadv_gw_node *
batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
45
{
46
	struct batadv_gw_node *gw_node;
47

48
	rcu_read_lock();
49
	gw_node = rcu_dereference(bat_priv->gw.curr_gw);
50
	if (!gw_node)
51
		goto out;
52

53 54
	if (!atomic_inc_not_zero(&gw_node->refcount))
		gw_node = NULL;
55

56 57
out:
	rcu_read_unlock();
58
	return gw_node;
59 60
}

61 62
struct batadv_orig_node *
batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
63
{
64 65
	struct batadv_gw_node *gw_node;
	struct batadv_orig_node *orig_node = NULL;
66

67
	gw_node = batadv_gw_get_selected_gw_node(bat_priv);
68 69 70 71 72 73 74 75 76 77
	if (!gw_node)
		goto out;

	rcu_read_lock();
	orig_node = gw_node->orig_node;
	if (!orig_node)
		goto unlock;

	if (!atomic_inc_not_zero(&orig_node->refcount))
		orig_node = NULL;
78

79 80 81
unlock:
	rcu_read_unlock();
out:
82
	if (gw_node)
83
		batadv_gw_node_free_ref(gw_node);
84
	return orig_node;
85 86
}

87 88
static void batadv_gw_select(struct batadv_priv *bat_priv,
			     struct batadv_gw_node *new_gw_node)
89
{
90
	struct batadv_gw_node *curr_gw_node;
91

92
	spin_lock_bh(&bat_priv->gw.list_lock);
93

94 95
	if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
		new_gw_node = NULL;
96

97 98
	curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
	rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
99 100

	if (curr_gw_node)
101
		batadv_gw_node_free_ref(curr_gw_node);
102

103
	spin_unlock_bh(&bat_priv->gw.list_lock);
104 105
}

106
void batadv_gw_deselect(struct batadv_priv *bat_priv)
107
{
108
	atomic_set(&bat_priv->gw.reselect, 1);
109 110
}

111 112
static struct batadv_gw_node *
batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
113
{
114 115
	struct batadv_neigh_node *router;
	struct batadv_gw_node *gw_node, *curr_gw = NULL;
116
	uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
117
	uint32_t gw_divisor;
118
	uint8_t max_tq = 0;
119
	uint8_t tq_avg;
120
	struct batadv_orig_node *orig_node;
121

122 123 124
	gw_divisor = BATADV_TQ_LOCAL_WINDOW_SIZE * BATADV_TQ_LOCAL_WINDOW_SIZE;
	gw_divisor *= 64;

125
	rcu_read_lock();
126
	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
127
		if (gw_node->deleted)
128 129
			continue;

130
		orig_node = gw_node->orig_node;
131
		router = batadv_orig_node_get_router(orig_node);
132
		if (!router)
133 134
			continue;

135 136 137
		if (!atomic_inc_not_zero(&gw_node->refcount))
			goto next;

138
		tq_avg = router->bat_iv.tq_avg;
139

140 141
		switch (atomic_read(&bat_priv->gw_sel_class)) {
		case 1: /* fast connection */
142 143 144
			tmp_gw_factor = tq_avg * tq_avg;
			tmp_gw_factor *= gw_node->bandwidth_down;
			tmp_gw_factor *= 100 * 100;
145
			tmp_gw_factor /= gw_divisor;
146 147 148

			if ((tmp_gw_factor > max_gw_factor) ||
			    ((tmp_gw_factor == max_gw_factor) &&
149
			     (tq_avg > max_tq))) {
150
				if (curr_gw)
151
					batadv_gw_node_free_ref(curr_gw);
152 153 154
				curr_gw = gw_node;
				atomic_inc(&curr_gw->refcount);
			}
155 156
			break;

157
		default: /* 2:  stable connection (use best statistic)
158 159 160 161 162
			  * 3:  fast-switch (use best statistic but change as
			  *     soon as a better gateway appears)
			  * XX: late-switch (use best statistic but change as
			  *     soon as a better gateway appears which has
			  *     $routing_class more tq points)
163
			  */
164
			if (tq_avg > max_tq) {
165
				if (curr_gw)
166
					batadv_gw_node_free_ref(curr_gw);
167 168 169
				curr_gw = gw_node;
				atomic_inc(&curr_gw->refcount);
			}
170 171 172
			break;
		}

173 174
		if (tq_avg > max_tq)
			max_tq = tq_avg;
175 176 177

		if (tmp_gw_factor > max_gw_factor)
			max_gw_factor = tmp_gw_factor;
178

179
		batadv_gw_node_free_ref(gw_node);
180 181

next:
182
		batadv_neigh_node_free_ref(router);
183
	}
184
	rcu_read_unlock();
185

186 187
	return curr_gw;
}
188

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/**
 * batadv_gw_check_client_stop - check if client mode has been switched off
 * @bat_priv: the bat priv with all the soft interface information
 *
 * This function assumes the caller has checked that the gw state *is actually
 * changing*. This function is not supposed to be called when there is no state
 * change.
 */
void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
{
	struct batadv_gw_node *curr_gw;

	if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
		return;

	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
	if (!curr_gw)
		return;

208 209 210 211 212
	/* deselect the current gateway so that next time that client mode is
	 * enabled a proper GW_ADD event can be sent
	 */
	batadv_gw_select(bat_priv, NULL);

213 214 215 216 217 218 219 220
	/* if batman-adv is switching the gw client mode off and a gateway was
	 * already selected, send a DEL uevent
	 */
	batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);

	batadv_gw_node_free_ref(curr_gw);
}

221
void batadv_gw_election(struct batadv_priv *bat_priv)
222
{
223 224
	struct batadv_gw_node *curr_gw = NULL, *next_gw = NULL;
	struct batadv_neigh_node *router = NULL;
225
	char gw_addr[18] = { '\0' };
226

227
	if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
228 229
		goto out;

230
	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
231

232
	if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
233 234
		goto out;

235
	next_gw = batadv_gw_get_best_gw_node(bat_priv);
236 237 238 239 240

	if (curr_gw == next_gw)
		goto out;

	if (next_gw) {
241 242
		sprintf(gw_addr, "%pM", next_gw->orig_node->orig);

243
		router = batadv_orig_node_get_router(next_gw->orig_node);
244
		if (!router) {
245
			batadv_gw_deselect(bat_priv);
246 247
			goto out;
		}
248 249
	}

250
	if ((curr_gw) && (!next_gw)) {
251
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
252
			   "Removing selected gateway - no gateway in range\n");
253 254
		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
				    NULL);
255
	} else if ((!curr_gw) && (next_gw)) {
256
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
257
			   "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
258
			   next_gw->orig_node->orig,
259 260 261
			   next_gw->bandwidth_down / 10,
			   next_gw->bandwidth_down % 10,
			   next_gw->bandwidth_up / 10,
262
			   next_gw->bandwidth_up % 10, router->bat_iv.tq_avg);
263 264
		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
				    gw_addr);
265
	} else {
266
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
267
			   "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
268
			   next_gw->orig_node->orig,
269 270 271
			   next_gw->bandwidth_down / 10,
			   next_gw->bandwidth_down % 10,
			   next_gw->bandwidth_up / 10,
272
			   next_gw->bandwidth_up % 10, router->bat_iv.tq_avg);
273 274
		batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
				    gw_addr);
275 276
	}

277
	batadv_gw_select(bat_priv, next_gw);
278

279 280
out:
	if (curr_gw)
281
		batadv_gw_node_free_ref(curr_gw);
282
	if (next_gw)
283
		batadv_gw_node_free_ref(next_gw);
284
	if (router)
285
		batadv_neigh_node_free_ref(router);
286 287
}

288 289
void batadv_gw_check_election(struct batadv_priv *bat_priv,
			      struct batadv_orig_node *orig_node)
290
{
291 292
	struct batadv_orig_node *curr_gw_orig;
	struct batadv_neigh_node *router_gw = NULL, *router_orig = NULL;
293 294
	uint8_t gw_tq_avg, orig_tq_avg;

295
	curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
296 297
	if (!curr_gw_orig)
		goto deselect;
298

299
	router_gw = batadv_orig_node_get_router(curr_gw_orig);
300 301
	if (!router_gw)
		goto deselect;
302 303

	/* this node already is the gateway */
304
	if (curr_gw_orig == orig_node)
305
		goto out;
306

307
	router_orig = batadv_orig_node_get_router(orig_node);
308 309
	if (!router_orig)
		goto out;
310

311 312
	gw_tq_avg = router_gw->bat_iv.tq_avg;
	orig_tq_avg = router_orig->bat_iv.tq_avg;
313 314 315

	/* the TQ value has to be better */
	if (orig_tq_avg < gw_tq_avg)
316
		goto out;
317

318
	/* if the routing class is greater than 3 the value tells us how much
319
	 * greater the TQ value of the new gateway must be
320
	 */
321 322
	if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
	    (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
323
		goto out;
324

325
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
326 327
		   "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
		   gw_tq_avg, orig_tq_avg);
328 329

deselect:
330
	batadv_gw_deselect(bat_priv);
331
out:
332
	if (curr_gw_orig)
333
		batadv_orig_node_free_ref(curr_gw_orig);
334
	if (router_gw)
335
		batadv_neigh_node_free_ref(router_gw);
336
	if (router_orig)
337
		batadv_neigh_node_free_ref(router_orig);
338

339
	return;
340 341
}

342 343 344 345 346 347
/**
 * batadv_gw_node_add - add gateway node to list of available gateways
 * @bat_priv: the bat priv with all the soft interface information
 * @orig_node: originator announcing gateway capabilities
 * @gateway: announced bandwidth information
 */
348 349
static void batadv_gw_node_add(struct batadv_priv *bat_priv,
			       struct batadv_orig_node *orig_node,
350
			       struct batadv_tvlv_gateway_data *gateway)
351
{
352
	struct batadv_gw_node *gw_node;
353 354 355

	if (gateway->bandwidth_down == 0)
		return;
356

357
	gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
358 359 360 361 362
	if (!gw_node)
		return;

	INIT_HLIST_NODE(&gw_node->list);
	gw_node->orig_node = orig_node;
363
	atomic_set(&gw_node->refcount, 1);
364

365 366 367
	spin_lock_bh(&bat_priv->gw.list_lock);
	hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
	spin_unlock_bh(&bat_priv->gw.list_lock);
368

369
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
370 371 372 373 374 375
		   "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
		   orig_node->orig,
		   ntohl(gateway->bandwidth_down) / 10,
		   ntohl(gateway->bandwidth_down) % 10,
		   ntohl(gateway->bandwidth_up) / 10,
		   ntohl(gateway->bandwidth_up) % 10);
376 377
}

378 379 380 381 382 383 384 385 386 387
/**
 * batadv_gw_node_get - retrieve gateway node from list of available gateways
 * @bat_priv: the bat priv with all the soft interface information
 * @orig_node: originator announcing gateway capabilities
 *
 * Returns gateway node if found or NULL otherwise.
 */
static struct batadv_gw_node *
batadv_gw_node_get(struct batadv_priv *bat_priv,
		   struct batadv_orig_node *orig_node)
388
{
389
	struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
390 391

	rcu_read_lock();
392 393
	hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
		if (gw_node_tmp->orig_node != orig_node)
394 395
			continue;

396 397
		if (gw_node_tmp->deleted)
			continue;
398

399 400
		if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
			continue;
401

402 403 404 405
		gw_node = gw_node_tmp;
		break;
	}
	rcu_read_unlock();
406

407 408
	return gw_node;
}
409

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
/**
 * batadv_gw_node_update - update list of available gateways with changed
 *  bandwidth information
 * @bat_priv: the bat priv with all the soft interface information
 * @orig_node: originator announcing gateway capabilities
 * @gateway: announced bandwidth information
 */
void batadv_gw_node_update(struct batadv_priv *bat_priv,
			   struct batadv_orig_node *orig_node,
			   struct batadv_tvlv_gateway_data *gateway)
{
	struct batadv_gw_node *gw_node, *curr_gw = NULL;

	gw_node = batadv_gw_node_get(bat_priv, orig_node);
	if (!gw_node) {
		batadv_gw_node_add(bat_priv, orig_node, gateway);
		goto out;
427 428
	}

429 430 431
	if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
	    (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
		goto out;
432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
		   "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
		   orig_node->orig,
		   gw_node->bandwidth_down / 10,
		   gw_node->bandwidth_down % 10,
		   gw_node->bandwidth_up / 10,
		   gw_node->bandwidth_up % 10,
		   ntohl(gateway->bandwidth_down) / 10,
		   ntohl(gateway->bandwidth_down) % 10,
		   ntohl(gateway->bandwidth_up) / 10,
		   ntohl(gateway->bandwidth_up) % 10);

	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);

	gw_node->deleted = 0;
	if (ntohl(gateway->bandwidth_down) == 0) {
		gw_node->deleted = jiffies;
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
			   "Gateway %pM removed from gateway list\n",
			   orig_node->orig);
454

455 456 457 458 459 460 461
		/* Note: We don't need a NULL check here, since curr_gw never
		 * gets dereferenced.
		 */
		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
		if (gw_node == curr_gw)
			batadv_gw_deselect(bat_priv);
	}
462

463
out:
464
	if (curr_gw)
465
		batadv_gw_node_free_ref(curr_gw);
466 467
	if (gw_node)
		batadv_gw_node_free_ref(gw_node);
468 469
}

470 471
void batadv_gw_node_delete(struct batadv_priv *bat_priv,
			   struct batadv_orig_node *orig_node)
472
{
473 474 475 476 477 478
	struct batadv_tvlv_gateway_data gateway;

	gateway.bandwidth_down = 0;
	gateway.bandwidth_up = 0;

	batadv_gw_node_update(bat_priv, orig_node, &gateway);
479 480
}

481
void batadv_gw_node_purge(struct batadv_priv *bat_priv)
482
{
483
	struct batadv_gw_node *gw_node, *curr_gw;
484
	struct hlist_node *node_tmp;
485
	unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT);
486
	int do_deselect = 0;
487

488
	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
489

490
	spin_lock_bh(&bat_priv->gw.list_lock);
491

492
	hlist_for_each_entry_safe(gw_node, node_tmp,
493
				  &bat_priv->gw.list, list) {
494 495
		if (((!gw_node->deleted) ||
		     (time_before(jiffies, gw_node->deleted + timeout))) &&
496
		    atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE)
497 498
			continue;

499 500
		if (curr_gw == gw_node)
			do_deselect = 1;
501 502

		hlist_del_rcu(&gw_node->list);
503
		batadv_gw_node_free_ref(gw_node);
504 505
	}

506
	spin_unlock_bh(&bat_priv->gw.list_lock);
507 508 509

	/* gw_deselect() needs to acquire the gw_list_lock */
	if (do_deselect)
510
		batadv_gw_deselect(bat_priv);
511 512

	if (curr_gw)
513
		batadv_gw_node_free_ref(curr_gw);
514 515
}

516
/* fails if orig_node has no router */
517 518 519
static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
				    struct seq_file *seq,
				    const struct batadv_gw_node *gw_node)
520
{
521 522
	struct batadv_gw_node *curr_gw;
	struct batadv_neigh_node *router;
523
	int ret = -1;
524

525
	router = batadv_orig_node_get_router(gw_node->orig_node);
526 527
	if (!router)
		goto out;
528

529
	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
530

531
	ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
532 533
			 (curr_gw == gw_node ? "=>" : "  "),
			 gw_node->orig_node->orig,
534
			 router->bat_iv.tq_avg, router->addr,
535
			 router->if_incoming->net_dev->name,
536 537 538 539
			 gw_node->bandwidth_down / 10,
			 gw_node->bandwidth_down % 10,
			 gw_node->bandwidth_up / 10,
			 gw_node->bandwidth_up % 10);
540

541
	batadv_neigh_node_free_ref(router);
542
	if (curr_gw)
543
		batadv_gw_node_free_ref(curr_gw);
544
out:
545
	return ret;
546 547
}

548
int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
549 550
{
	struct net_device *net_dev = (struct net_device *)seq->private;
551 552 553
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
	struct batadv_hard_iface *primary_if;
	struct batadv_gw_node *gw_node;
554
	int gw_count = 0;
555

556 557
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
558
		goto out;
559

560
	seq_printf(seq,
561
		   "      %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
562 563
		   "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
		   BATADV_SOURCE_VERSION, primary_if->net_dev->name,
564
		   primary_if->net_dev->dev_addr, net_dev->name);
565 566

	rcu_read_lock();
567
	hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
568 569 570
		if (gw_node->deleted)
			continue;

571
		/* fails if orig_node has no router */
572
		if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
573 574 575 576 577 578 579
			continue;

		gw_count++;
	}
	rcu_read_unlock();

	if (gw_count == 0)
580
		seq_puts(seq, "No gateways in range ...\n");
581

582 583
out:
	if (primary_if)
584
		batadv_hardif_free_ref(primary_if);
585
	return 0;
586 587
}

588
/* this call might reallocate skb data */
589
static bool batadv_is_type_dhcprequest(struct sk_buff *skb, int header_len)
590 591 592 593 594 595 596 597 598 599
{
	int ret = false;
	unsigned char *p;
	int pkt_len;

	if (skb_linearize(skb) < 0)
		goto out;

	pkt_len = skb_headlen(skb);

600
	if (pkt_len < header_len + BATADV_DHCP_OPTIONS_OFFSET + 1)
601 602
		goto out;

603 604
	p = skb->data + header_len + BATADV_DHCP_OPTIONS_OFFSET;
	pkt_len -= header_len + BATADV_DHCP_OPTIONS_OFFSET + 1;
605 606

	/* Access the dhcp option lists. Each entry is made up by:
607 608
	 * - octet 1: option type
	 * - octet 2: option data len (only if type != 255 and 0)
609 610
	 * - octet 3: option data
	 */
611
	while (*p != 255 && !ret) {
612
		/* p now points to the first octet: option type */
613 614
		if (*p == 53) {
			/* type 53 is the message type option.
615 616
			 * Jump the len octet and go to the data octet
			 */
617 618 619 620 621
			if (pkt_len < 2)
				goto out;
			p += 2;

			/* check if the message type is what we need */
622
			if (*p == BATADV_DHCP_REQUEST)
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
				ret = true;
			break;
		} else if (*p == 0) {
			/* option type 0 (padding), just go forward */
			if (pkt_len < 1)
				goto out;
			pkt_len--;
			p++;
		} else {
			/* This is any other option. So we get the length... */
			if (pkt_len < 1)
				goto out;
			pkt_len--;
			p++;

			/* ...and then we jump over the data */
639
			if (pkt_len < 1 + (*p))
640
				goto out;
641 642
			pkt_len -= 1 + (*p);
			p += 1 + (*p);
643 644 645 646 647 648
		}
	}
out:
	return ret;
}

649
/* this call might reallocate skb data */
650
bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
651 652 653 654 655
{
	struct ethhdr *ethhdr;
	struct iphdr *iphdr;
	struct ipv6hdr *ipv6hdr;
	struct udphdr *udphdr;
656 657
	struct vlan_ethhdr *vhdr;
	__be16 proto;
658 659

	/* check for ethernet header */
660 661
	if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
		return false;
662
	ethhdr = (struct ethhdr *)skb->data;
663
	proto = ethhdr->h_proto;
664
	*header_len += ETH_HLEN;
665 666

	/* check for initial vlan header */
667
	if (proto == htons(ETH_P_8021Q)) {
668 669
		if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
			return false;
670 671 672

		vhdr = (struct vlan_ethhdr *)skb->data;
		proto = vhdr->h_vlan_encapsulated_proto;
673
		*header_len += VLAN_HLEN;
674 675 676
	}

	/* check for ip header */
677 678
	switch (proto) {
	case htons(ETH_P_IP):
679 680 681 682
		if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
			return false;
		iphdr = (struct iphdr *)(skb->data + *header_len);
		*header_len += iphdr->ihl * 4;
683 684 685

		/* check for udp header */
		if (iphdr->protocol != IPPROTO_UDP)
686
			return false;
687 688

		break;
689
	case htons(ETH_P_IPV6):
690 691 692 693
		if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
			return false;
		ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
		*header_len += sizeof(*ipv6hdr);
694 695 696

		/* check for udp header */
		if (ipv6hdr->nexthdr != IPPROTO_UDP)
697
			return false;
698 699 700

		break;
	default:
701
		return false;
702 703
	}

704 705
	if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
		return false;
706 707 708 709 710 711

	/* skb->data might have been reallocated by pskb_may_pull() */
	ethhdr = (struct ethhdr *)skb->data;
	if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
		ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);

712 713
	udphdr = (struct udphdr *)(skb->data + *header_len);
	*header_len += sizeof(*udphdr);
714 715

	/* check for bootp port */
716
	if ((proto == htons(ETH_P_IP)) &&
717
	    (udphdr->dest != htons(67)))
718
		return false;
719

720
	if ((proto == htons(ETH_P_IPV6)) &&
721
	    (udphdr->dest != htons(547)))
722
		return false;
723

724 725
	return true;
}
726

727 728 729 730 731 732 733 734 735 736 737 738 739 740
/**
 * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
 * @bat_priv: the bat priv with all the soft interface information
 * @skb: the outgoing packet
 *
 * Check if the skb is a DHCP request and if it is sent to the current best GW
 * server. Due to topology changes it may be the case that the GW server
 * previously selected is not the best one anymore.
 *
 * Returns true if the packet destination is unicast and it is not the best gw,
 * false otherwise.
 *
 * This call might reallocate skb data.
 */
741
bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
742
			    struct sk_buff *skb)
743
{
744 745
	struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL;
	struct batadv_orig_node *orig_dst_node = NULL;
746
	struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL;
747
	struct ethhdr *ethhdr;
748 749 750
	bool ret, out_of_range = false;
	unsigned int header_len = 0;
	uint8_t curr_tq_avg;
751 752 753
	unsigned short vid;

	vid = batadv_get_vid(skb, 0);
754

755
	ret = batadv_gw_is_dhcp_target(skb, &header_len);
756 757 758
	if (!ret)
		goto out;

759
	ethhdr = (struct ethhdr *)skb->data;
760
	orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
761
						 ethhdr->h_dest, vid);
762 763 764
	if (!orig_dst_node)
		goto out;

765 766
	gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
	if (!gw_node->bandwidth_down == 0)
767 768
		goto out;

769
	ret = batadv_is_type_dhcprequest(skb, header_len);
770 771 772 773
	if (!ret)
		goto out;

	switch (atomic_read(&bat_priv->gw_mode)) {
774
	case BATADV_GW_MODE_SERVER:
775
		/* If we are a GW then we are our best GW. We can artificially
776 777
		 * set the tq towards ourself as the maximum value
		 */
778
		curr_tq_avg = BATADV_TQ_MAX_VALUE;
779
		break;
780
	case BATADV_GW_MODE_CLIENT:
781
		curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
782 783 784 785 786 787 788 789 790
		if (!curr_gw)
			goto out;

		/* packet is going to our gateway */
		if (curr_gw->orig_node == orig_dst_node)
			goto out;

		/* If the dhcp packet has been sent to a different gw,
		 * we have to evaluate whether the old gw is still
791 792
		 * reliable enough
		 */
793 794
		neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
						NULL);
795 796 797
		if (!neigh_curr)
			goto out;

798
		curr_tq_avg = neigh_curr->bat_iv.tq_avg;
799
		break;
800
	case BATADV_GW_MODE_OFF:
801 802
	default:
		goto out;
803
	}
804

805
	neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
806
	if (!neigh_old)
807 808
		goto out;

809
	if (curr_tq_avg - neigh_old->bat_iv.tq_avg > BATADV_GW_THRESHOLD)
810 811 812 813
		out_of_range = true;

out:
	if (orig_dst_node)
814
		batadv_orig_node_free_ref(orig_dst_node);
815
	if (curr_gw)
816
		batadv_gw_node_free_ref(curr_gw);
817 818
	if (gw_node)
		batadv_gw_node_free_ref(gw_node);
819
	if (neigh_old)
820
		batadv_neigh_node_free_ref(neigh_old);
821
	if (neigh_curr)
822
		batadv_neigh_node_free_ref(neigh_curr);
823
	return out_of_range;
824
}