hard-interface.c 15.0 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
 *
 * 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 "hard-interface.h"
#include "soft-interface.h"
#include "send.h"
#include "translation-table.h"
#include "routing.h"
#include "bat_sysfs.h"
#include "originator.h"
#include "hash.h"
29
#include "bridge_loop_avoidance.h"
30 31 32

#include <linux/if_arp.h>

33
void batadv_hardif_free_rcu(struct rcu_head *rcu)
34
{
35
	struct hard_iface *hard_iface;
36

37 38 39
	hard_iface = container_of(rcu, struct hard_iface, rcu);
	dev_put(hard_iface->net_dev);
	kfree(hard_iface);
40 41
}

42
struct hard_iface *batadv_hardif_get_by_netdev(const struct net_device *net_dev)
43
{
44
	struct hard_iface *hard_iface;
45 46

	rcu_read_lock();
47
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
48 49
		if (hard_iface->net_dev == net_dev &&
		    atomic_inc_not_zero(&hard_iface->refcount))
50 51 52
			goto out;
	}

53
	hard_iface = NULL;
54 55 56

out:
	rcu_read_unlock();
57
	return hard_iface;
58 59
}

60
static int batadv_is_valid_iface(const struct net_device *net_dev)
61 62 63 64 65 66 67 68 69 70 71
{
	if (net_dev->flags & IFF_LOOPBACK)
		return 0;

	if (net_dev->type != ARPHRD_ETHER)
		return 0;

	if (net_dev->addr_len != ETH_ALEN)
		return 0;

	/* no batman over batman */
72
	if (batadv_softif_is_valid(net_dev))
73 74 75 76 77
		return 0;

	return 1;
}

78 79
static struct hard_iface *
batadv_hardif_get_active(const struct net_device *soft_iface)
80
{
81
	struct hard_iface *hard_iface;
82 83

	rcu_read_lock();
84
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
85
		if (hard_iface->soft_iface != soft_iface)
86 87
			continue;

88
		if (hard_iface->if_status == BATADV_IF_ACTIVE &&
89
		    atomic_inc_not_zero(&hard_iface->refcount))
90 91 92
			goto out;
	}

93
	hard_iface = NULL;
94 95 96

out:
	rcu_read_unlock();
97
	return hard_iface;
98 99
}

100 101
static void batadv_primary_if_update_addr(struct bat_priv *bat_priv,
					  struct hard_iface *oldif)
102
{
103
	struct batadv_vis_packet *vis_packet;
104 105
	struct hard_iface *primary_if;

106
	primary_if = batadv_primary_if_get_selected(bat_priv);
107 108
	if (!primary_if)
		goto out;
109

110
	vis_packet = (struct batadv_vis_packet *)
111
				bat_priv->my_vis_info->skb_packet->data;
112
	memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
113
	memcpy(vis_packet->sender_orig,
114 115
	       primary_if->net_dev->dev_addr, ETH_ALEN);

116
	batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
117 118
out:
	if (primary_if)
119
		batadv_hardif_free_ref(primary_if);
120 121
}

122 123
static void batadv_primary_if_select(struct bat_priv *bat_priv,
				     struct hard_iface *new_hard_iface)
124
{
125
	struct hard_iface *curr_hard_iface;
126

127
	ASSERT_RTNL();
128

129 130
	if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
		new_hard_iface = NULL;
131

132
	curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
133
	rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
134

135
	if (!new_hard_iface)
136
		goto out;
137

138
	bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
139
	batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
140 141 142

out:
	if (curr_hard_iface)
143
		batadv_hardif_free_ref(curr_hard_iface);
144 145
}

146
static bool batadv_hardif_is_iface_up(const struct hard_iface *hard_iface)
147
{
148
	if (hard_iface->net_dev->flags & IFF_UP)
149 150 151 152 153
		return true;

	return false;
}

154
static void batadv_check_known_mac_addr(const struct net_device *net_dev)
155
{
156
	const struct hard_iface *hard_iface;
157 158

	rcu_read_lock();
159
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
160 161
		if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
		    (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
162 163
			continue;

164
		if (hard_iface->net_dev == net_dev)
165 166
			continue;

167 168
		if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
					net_dev->dev_addr))
169 170
			continue;

171 172 173
		pr_warn("The newly added mac address (%pM) already exists on: %s\n",
			net_dev->dev_addr, hard_iface->net_dev->name);
		pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
174 175 176 177
	}
	rcu_read_unlock();
}

178
int batadv_hardif_min_mtu(struct net_device *soft_iface)
179
{
180 181
	const struct bat_priv *bat_priv = netdev_priv(soft_iface);
	const struct hard_iface *hard_iface;
182
	/* allow big frames if all devices are capable to do so
183 184
	 * (have MTU > 1500 + BAT_HEADER_LEN)
	 */
185 186 187 188 189 190
	int min_mtu = ETH_DATA_LEN;

	if (atomic_read(&bat_priv->fragmentation))
		goto out;

	rcu_read_lock();
191
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
192 193
		if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
		    (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
194 195
			continue;

196
		if (hard_iface->soft_iface != soft_iface)
197 198
			continue;

199 200
		min_mtu = min_t(int,
				hard_iface->net_dev->mtu - BATADV_HEADER_LEN,
201 202 203 204 205 206 207 208
				min_mtu);
	}
	rcu_read_unlock();
out:
	return min_mtu;
}

/* adjusts the MTU if a new interface with a smaller MTU appeared. */
209
void batadv_update_min_mtu(struct net_device *soft_iface)
210 211 212
{
	int min_mtu;

213
	min_mtu = batadv_hardif_min_mtu(soft_iface);
214 215 216 217
	if (soft_iface->mtu != min_mtu)
		soft_iface->mtu = min_mtu;
}

218
static void batadv_hardif_activate_interface(struct hard_iface *hard_iface)
219 220
{
	struct bat_priv *bat_priv;
221
	struct hard_iface *primary_if = NULL;
222

223
	if (hard_iface->if_status != BATADV_IF_INACTIVE)
224
		goto out;
225

226
	bat_priv = netdev_priv(hard_iface->soft_iface);
227

228
	bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
229
	hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
230

231
	/* the first active interface becomes our primary interface or
232
	 * the next active interface after the old primary interface was removed
233
	 */
234
	primary_if = batadv_primary_if_get_selected(bat_priv);
235
	if (!primary_if)
236
		batadv_primary_if_select(bat_priv, hard_iface);
237

238 239
	batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
		    hard_iface->net_dev->name);
240

241
	batadv_update_min_mtu(hard_iface->soft_iface);
242 243 244

out:
	if (primary_if)
245
		batadv_hardif_free_ref(primary_if);
246 247
}

248
static void batadv_hardif_deactivate_interface(struct hard_iface *hard_iface)
249
{
250 251
	if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
	    (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
252 253
		return;

254
	hard_iface->if_status = BATADV_IF_INACTIVE;
255

256 257
	batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
		    hard_iface->net_dev->name);
258

259
	batadv_update_min_mtu(hard_iface->soft_iface);
260 261
}

262 263
int batadv_hardif_enable_interface(struct hard_iface *hard_iface,
				   const char *iface_name)
264 265
{
	struct bat_priv *bat_priv;
266
	struct net_device *soft_iface;
267
	__be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
268
	int ret;
269

270
	if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
271 272
		goto out;

273
	if (!atomic_inc_not_zero(&hard_iface->refcount))
274 275
		goto out;

276 277
	/* hard-interface is part of a bridge */
	if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
278
		pr_err("You are about to enable batman-adv on '%s' which already is part of a bridge. Unless you know exactly what you are doing this is probably wrong and won't work the way you think it would.\n",
279 280
		       hard_iface->net_dev->name);

281
	soft_iface = dev_get_by_name(&init_net, iface_name);
282

283
	if (!soft_iface) {
284
		soft_iface = batadv_softif_create(iface_name);
285

286 287
		if (!soft_iface) {
			ret = -ENOMEM;
288
			goto err;
289
		}
290 291

		/* dev_get_by_name() increases the reference counter for us */
292 293 294
		dev_hold(soft_iface);
	}

295
	if (!batadv_softif_is_valid(soft_iface)) {
296
		pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
297 298
		       soft_iface->name);
		ret = -EINVAL;
299
		goto err_dev;
300 301
	}

302
	hard_iface->soft_iface = soft_iface;
303
	bat_priv = netdev_priv(hard_iface->soft_iface);
304

305
	ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
306
	if (ret < 0)
307
		goto err_dev;
308

309
	hard_iface->if_num = bat_priv->num_ifaces;
310
	bat_priv->num_ifaces++;
311
	hard_iface->if_status = BATADV_IF_INACTIVE;
312
	batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
313

314
	hard_iface->batman_adv_ptype.type = ethertype;
315
	hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
316 317
	hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
	dev_add_pack(&hard_iface->batman_adv_ptype);
318

319
	atomic_set(&hard_iface->frag_seqno, 1);
320 321
	batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
		    hard_iface->net_dev->name);
322

323
	if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
324
		ETH_DATA_LEN + BATADV_HEADER_LEN)
325 326 327
		batadv_info(hard_iface->soft_iface,
			    "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. Packets going over this interface will be fragmented on layer2 which could impact the performance. Setting the MTU to %zi would solve the problem.\n",
			    hard_iface->net_dev->name, hard_iface->net_dev->mtu,
328
			    ETH_DATA_LEN + BATADV_HEADER_LEN);
329

330
	if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
331
		ETH_DATA_LEN + BATADV_HEADER_LEN)
332 333 334
		batadv_info(hard_iface->soft_iface,
			    "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. If you experience problems getting traffic through try increasing the MTU to %zi.\n",
			    hard_iface->net_dev->name, hard_iface->net_dev->mtu,
335
			    ETH_DATA_LEN + BATADV_HEADER_LEN);
336

337 338
	if (batadv_hardif_is_iface_up(hard_iface))
		batadv_hardif_activate_interface(hard_iface);
339
	else
340 341 342
		batadv_err(hard_iface->soft_iface,
			   "Not using interface %s (retrying later): interface not active\n",
			   hard_iface->net_dev->name);
343 344

	/* begin scheduling originator messages on that interface */
345
	batadv_schedule_bat_ogm(hard_iface);
346 347 348 349

out:
	return 0;

350 351
err_dev:
	dev_put(soft_iface);
352
err:
353
	batadv_hardif_free_ref(hard_iface);
354
	return ret;
355 356
}

357
void batadv_hardif_disable_interface(struct hard_iface *hard_iface)
358
{
359
	struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
360
	struct hard_iface *primary_if = NULL;
361

362
	if (hard_iface->if_status == BATADV_IF_ACTIVE)
363
		batadv_hardif_deactivate_interface(hard_iface);
364

365
	if (hard_iface->if_status != BATADV_IF_INACTIVE)
366
		goto out;
367

368 369
	batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
		    hard_iface->net_dev->name);
370
	dev_remove_pack(&hard_iface->batman_adv_ptype);
371 372

	bat_priv->num_ifaces--;
373
	batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
374

375
	primary_if = batadv_primary_if_get_selected(bat_priv);
376
	if (hard_iface == primary_if) {
377
		struct hard_iface *new_if;
378

379 380
		new_if = batadv_hardif_get_active(hard_iface->soft_iface);
		batadv_primary_if_select(bat_priv, new_if);
381 382

		if (new_if)
383
			batadv_hardif_free_ref(new_if);
384 385
	}

386
	bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
387
	hard_iface->if_status = BATADV_IF_NOT_IN_USE;
388

389
	/* delete all references to this hard_iface */
390
	batadv_purge_orig_ref(bat_priv);
391
	batadv_purge_outstanding_packets(bat_priv, hard_iface);
392
	dev_put(hard_iface->soft_iface);
393 394 395

	/* nobody uses this interface anymore */
	if (!bat_priv->num_ifaces)
396
		batadv_softif_destroy(hard_iface->soft_iface);
397

398
	hard_iface->soft_iface = NULL;
399
	batadv_hardif_free_ref(hard_iface);
400 401 402

out:
	if (primary_if)
403
		batadv_hardif_free_ref(primary_if);
404 405
}

406 407
static struct hard_iface *
batadv_hardif_add_interface(struct net_device *net_dev)
408
{
409
	struct hard_iface *hard_iface;
410 411
	int ret;

412 413
	ASSERT_RTNL();

414
	ret = batadv_is_valid_iface(net_dev);
415 416 417 418 419
	if (ret != 1)
		goto out;

	dev_hold(net_dev);

420
	hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
421
	if (!hard_iface)
422 423
		goto release_dev;

424
	ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
425 426 427
	if (ret)
		goto free_if;

428 429 430
	hard_iface->if_num = -1;
	hard_iface->net_dev = net_dev;
	hard_iface->soft_iface = NULL;
431
	hard_iface->if_status = BATADV_IF_NOT_IN_USE;
432
	INIT_LIST_HEAD(&hard_iface->list);
433
	/* extra reference for return */
434
	atomic_set(&hard_iface->refcount, 2);
435

436
	batadv_check_known_mac_addr(hard_iface->net_dev);
437
	list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
438

439
	/* This can't be called via a bat_priv callback because
440 441 442 443 444
	 * we have no bat_priv yet.
	 */
	atomic_set(&hard_iface->seqno, 1);
	hard_iface->packet_buff = NULL;

445
	return hard_iface;
446 447

free_if:
448
	kfree(hard_iface);
449 450 451 452 453 454
release_dev:
	dev_put(net_dev);
out:
	return NULL;
}

455
static void batadv_hardif_remove_interface(struct hard_iface *hard_iface)
456
{
457 458
	ASSERT_RTNL();

459
	/* first deactivate interface */
460
	if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
461
		batadv_hardif_disable_interface(hard_iface);
462

463
	if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
464 465
		return;

466
	hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
467
	batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
468
	batadv_hardif_free_ref(hard_iface);
469 470
}

471
void batadv_hardif_remove_interfaces(void)
472
{
473
	struct hard_iface *hard_iface, *hard_iface_tmp;
474

475
	rtnl_lock();
476
	list_for_each_entry_safe(hard_iface, hard_iface_tmp,
477
				 &batadv_hardif_list, list) {
478
		list_del_rcu(&hard_iface->list);
479
		batadv_hardif_remove_interface(hard_iface);
480 481 482 483
	}
	rtnl_unlock();
}

484 485
static int batadv_hard_if_event(struct notifier_block *this,
				unsigned long event, void *ptr)
486
{
487
	struct net_device *net_dev = ptr;
488
	struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
489
	struct hard_iface *primary_if = NULL;
490 491
	struct bat_priv *bat_priv;

492
	if (!hard_iface && event == NETDEV_REGISTER)
493
		hard_iface = batadv_hardif_add_interface(net_dev);
494

495
	if (!hard_iface)
496 497 498 499
		goto out;

	switch (event) {
	case NETDEV_UP:
500
		batadv_hardif_activate_interface(hard_iface);
501 502 503
		break;
	case NETDEV_GOING_DOWN:
	case NETDEV_DOWN:
504
		batadv_hardif_deactivate_interface(hard_iface);
505 506
		break;
	case NETDEV_UNREGISTER:
507
		list_del_rcu(&hard_iface->list);
508

509
		batadv_hardif_remove_interface(hard_iface);
510 511
		break;
	case NETDEV_CHANGEMTU:
512
		if (hard_iface->soft_iface)
513
			batadv_update_min_mtu(hard_iface->soft_iface);
514 515
		break;
	case NETDEV_CHANGEADDR:
516
		if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
517 518
			goto hardif_put;

519
		batadv_check_known_mac_addr(hard_iface->net_dev);
520

521
		bat_priv = netdev_priv(hard_iface->soft_iface);
522
		bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
523

524
		primary_if = batadv_primary_if_get_selected(bat_priv);
525 526 527 528
		if (!primary_if)
			goto hardif_put;

		if (hard_iface == primary_if)
529
			batadv_primary_if_update_addr(bat_priv, NULL);
530 531 532
		break;
	default:
		break;
J
Joe Perches 已提交
533
	}
534 535

hardif_put:
536
	batadv_hardif_free_ref(hard_iface);
537
out:
538
	if (primary_if)
539
		batadv_hardif_free_ref(primary_if);
540 541 542
	return NOTIFY_DONE;
}

543
/* This function returns true if the interface represented by ifindex is a
544 545
 * 802.11 wireless device
 */
546
bool batadv_is_wifi_iface(int ifindex)
547 548 549 550
{
	struct net_device *net_device = NULL;
	bool ret = false;

551
	if (ifindex == BATADV_NULL_IFINDEX)
552 553 554 555 556 557 558 559
		goto out;

	net_device = dev_get_by_index(&init_net, ifindex);
	if (!net_device)
		goto out;

#ifdef CONFIG_WIRELESS_EXT
	/* pre-cfg80211 drivers have to implement WEXT, so it is possible to
560 561
	 * check for wireless_handlers != NULL
	 */
562 563 564 565 566 567 568 569 570 571 572 573 574
	if (net_device->wireless_handlers)
		ret = true;
	else
#endif
		/* cfg80211 drivers have to set ieee80211_ptr */
		if (net_device->ieee80211_ptr)
			ret = true;
out:
	if (net_device)
		dev_put(net_device);
	return ret;
}

575
struct notifier_block batadv_hard_if_notifier = {
576
	.notifier_call = batadv_hard_if_event,
577
};