hard-interface.c 14.5 KB
Newer Older
1
/*
2
 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
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 30
 *
 * 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"
31
#include "bridge_loop_avoidance.h"
32 33 34

#include <linux/if_arp.h>

35
void batadv_hardif_free_rcu(struct rcu_head *rcu)
36
{
37
	struct hard_iface *hard_iface;
38

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

44
struct hard_iface *batadv_hardif_get_by_netdev(const struct net_device *net_dev)
45
{
46
	struct hard_iface *hard_iface;
47 48

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

55
	hard_iface = NULL;
56 57 58

out:
	rcu_read_unlock();
59
	return hard_iface;
60 61
}

62
static int is_valid_iface(const struct net_device *net_dev)
63 64 65 66 67 68 69 70 71 72 73
{
	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 */
74
	if (softif_is_valid(net_dev))
75 76 77 78 79 80 81 82 83
		return 0;

	/* Device is being bridged */
	/* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
		return 0; */

	return 1;
}

84
static struct hard_iface *hardif_get_active(const struct net_device *soft_iface)
85
{
86
	struct hard_iface *hard_iface;
87 88

	rcu_read_lock();
89 90
	list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
		if (hard_iface->soft_iface != soft_iface)
91 92
			continue;

93 94
		if (hard_iface->if_status == IF_ACTIVE &&
		    atomic_inc_not_zero(&hard_iface->refcount))
95 96 97
			goto out;
	}

98
	hard_iface = NULL;
99 100 101

out:
	rcu_read_unlock();
102
	return hard_iface;
103 104
}

105 106
static void primary_if_update_addr(struct bat_priv *bat_priv,
				   struct hard_iface *oldif)
107 108
{
	struct vis_packet *vis_packet;
109 110 111 112 113
	struct hard_iface *primary_if;

	primary_if = primary_if_get_selected(bat_priv);
	if (!primary_if)
		goto out;
114 115 116

	vis_packet = (struct vis_packet *)
				bat_priv->my_vis_info->skb_packet->data;
117
	memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
118
	memcpy(vis_packet->sender_orig,
119 120
	       primary_if->net_dev->dev_addr, ETH_ALEN);

121
	batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
122 123 124
out:
	if (primary_if)
		hardif_free_ref(primary_if);
125 126
}

127 128
static void primary_if_select(struct bat_priv *bat_priv,
			      struct hard_iface *new_hard_iface)
129
{
130
	struct hard_iface *curr_hard_iface;
131

132
	ASSERT_RTNL();
133

134 135
	if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
		new_hard_iface = NULL;
136

137
	curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
138
	rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
139

140
	if (!new_hard_iface)
141
		goto out;
142

143
	bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
144 145 146 147 148
	primary_if_update_addr(bat_priv, curr_hard_iface);

out:
	if (curr_hard_iface)
		hardif_free_ref(curr_hard_iface);
149 150
}

151
static bool hardif_is_iface_up(const struct hard_iface *hard_iface)
152
{
153
	if (hard_iface->net_dev->flags & IFF_UP)
154 155 156 157 158
		return true;

	return false;
}

159
static void check_known_mac_addr(const struct net_device *net_dev)
160
{
161
	const struct hard_iface *hard_iface;
162 163

	rcu_read_lock();
164 165 166
	list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
		if ((hard_iface->if_status != IF_ACTIVE) &&
		    (hard_iface->if_status != IF_TO_BE_ACTIVATED))
167 168
			continue;

169
		if (hard_iface->net_dev == net_dev)
170 171
			continue;

172 173
		if (!compare_eth(hard_iface->net_dev->dev_addr,
				 net_dev->dev_addr))
174 175
			continue;

176 177 178
		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");
179 180 181 182
	}
	rcu_read_unlock();
}

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

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

	rcu_read_lock();
195 196 197
	list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
		if ((hard_iface->if_status != IF_ACTIVE) &&
		    (hard_iface->if_status != IF_TO_BE_ACTIVATED))
198 199
			continue;

200
		if (hard_iface->soft_iface != soft_iface)
201 202
			continue;

203
		min_mtu = min_t(int, hard_iface->net_dev->mtu - BAT_HEADER_LEN,
204 205 206 207 208 209 210 211
				min_mtu);
	}
	rcu_read_unlock();
out:
	return min_mtu;
}

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

216
	min_mtu = batadv_hardif_min_mtu(soft_iface);
217 218 219 220
	if (soft_iface->mtu != min_mtu)
		soft_iface->mtu = min_mtu;
}

221
static void hardif_activate_interface(struct hard_iface *hard_iface)
222 223
{
	struct bat_priv *bat_priv;
224
	struct hard_iface *primary_if = NULL;
225

226
	if (hard_iface->if_status != IF_INACTIVE)
227
		goto out;
228

229
	bat_priv = netdev_priv(hard_iface->soft_iface);
230

231
	bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
232
	hard_iface->if_status = IF_TO_BE_ACTIVATED;
233 234 235

	/**
	 * the first active interface becomes our primary interface or
236
	 * the next active interface after the old primary interface was removed
237
	 */
238 239 240
	primary_if = primary_if_get_selected(bat_priv);
	if (!primary_if)
		primary_if_select(bat_priv, hard_iface);
241

242 243
	bat_info(hard_iface->soft_iface, "Interface activated: %s\n",
		 hard_iface->net_dev->name);
244

245
	batadv_update_min_mtu(hard_iface->soft_iface);
246 247 248 249

out:
	if (primary_if)
		hardif_free_ref(primary_if);
250 251
}

252
static void hardif_deactivate_interface(struct hard_iface *hard_iface)
253
{
254 255
	if ((hard_iface->if_status != IF_ACTIVE) &&
	    (hard_iface->if_status != IF_TO_BE_ACTIVATED))
256 257
		return;

258
	hard_iface->if_status = IF_INACTIVE;
259

260 261
	bat_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
		 hard_iface->net_dev->name);
262

263
	batadv_update_min_mtu(hard_iface->soft_iface);
264 265
}

266 267
int batadv_hardif_enable_interface(struct hard_iface *hard_iface,
				   const char *iface_name)
268 269
{
	struct bat_priv *bat_priv;
270 271
	struct net_device *soft_iface;
	int ret;
272

273
	if (hard_iface->if_status != IF_NOT_IN_USE)
274 275
		goto out;

276
	if (!atomic_inc_not_zero(&hard_iface->refcount))
277 278
		goto out;

279 280
	/* hard-interface is part of a bridge */
	if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
281
		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",
282 283
		       hard_iface->net_dev->name);

284
	soft_iface = dev_get_by_name(&init_net, iface_name);
285

286 287
	if (!soft_iface) {
		soft_iface = softif_create(iface_name);
288

289 290
		if (!soft_iface) {
			ret = -ENOMEM;
291
			goto err;
292
		}
293 294

		/* dev_get_by_name() increases the reference counter for us */
295 296 297 298
		dev_hold(soft_iface);
	}

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

305
	hard_iface->soft_iface = soft_iface;
306
	bat_priv = netdev_priv(hard_iface->soft_iface);
307

308
	ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
309
	if (ret < 0)
310
		goto err_dev;
311

312
	hard_iface->if_num = bat_priv->num_ifaces;
313
	bat_priv->num_ifaces++;
314
	hard_iface->if_status = IF_INACTIVE;
315
	batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
316

317 318 319 320
	hard_iface->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
	hard_iface->batman_adv_ptype.func = batman_skb_recv;
	hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
	dev_add_pack(&hard_iface->batman_adv_ptype);
321

322 323 324
	atomic_set(&hard_iface->frag_seqno, 1);
	bat_info(hard_iface->soft_iface, "Adding interface: %s\n",
		 hard_iface->net_dev->name);
325

326
	if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
327
		ETH_DATA_LEN + BAT_HEADER_LEN)
328
		bat_info(hard_iface->soft_iface,
329
			 "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",
330 331
			 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
			 ETH_DATA_LEN + BAT_HEADER_LEN);
332

333
	if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
334
		ETH_DATA_LEN + BAT_HEADER_LEN)
335
		bat_info(hard_iface->soft_iface,
336
			 "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",
337 338
			 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
			 ETH_DATA_LEN + BAT_HEADER_LEN);
339

340 341
	if (hardif_is_iface_up(hard_iface))
		hardif_activate_interface(hard_iface);
342
	else
343 344
		bat_err(hard_iface->soft_iface,
			"Not using interface %s (retrying later): interface not active\n",
345
			hard_iface->net_dev->name);
346 347

	/* begin scheduling originator messages on that interface */
348
	batadv_schedule_bat_ogm(hard_iface);
349 350 351 352

out:
	return 0;

353 354
err_dev:
	dev_put(soft_iface);
355
err:
356
	hardif_free_ref(hard_iface);
357
	return ret;
358 359
}

360
void batadv_hardif_disable_interface(struct hard_iface *hard_iface)
361
{
362
	struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
363
	struct hard_iface *primary_if = NULL;
364

365 366
	if (hard_iface->if_status == IF_ACTIVE)
		hardif_deactivate_interface(hard_iface);
367

368
	if (hard_iface->if_status != IF_INACTIVE)
369
		goto out;
370

371 372 373
	bat_info(hard_iface->soft_iface, "Removing interface: %s\n",
		 hard_iface->net_dev->name);
	dev_remove_pack(&hard_iface->batman_adv_ptype);
374 375

	bat_priv->num_ifaces--;
376
	batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
377

378 379
	primary_if = primary_if_get_selected(bat_priv);
	if (hard_iface == primary_if) {
380
		struct hard_iface *new_if;
381

382
		new_if = hardif_get_active(hard_iface->soft_iface);
383
		primary_if_select(bat_priv, new_if);
384 385

		if (new_if)
386
			hardif_free_ref(new_if);
387 388
	}

389
	bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
390
	hard_iface->if_status = IF_NOT_IN_USE;
391

392
	/* delete all references to this hard_iface */
393
	batadv_purge_orig_ref(bat_priv);
394
	batadv_purge_outstanding_packets(bat_priv, hard_iface);
395
	dev_put(hard_iface->soft_iface);
396 397 398

	/* nobody uses this interface anymore */
	if (!bat_priv->num_ifaces)
399
		softif_destroy(hard_iface->soft_iface);
400

401 402
	hard_iface->soft_iface = NULL;
	hardif_free_ref(hard_iface);
403 404 405 406

out:
	if (primary_if)
		hardif_free_ref(primary_if);
407 408
}

409
static struct hard_iface *hardif_add_interface(struct net_device *net_dev)
410
{
411
	struct hard_iface *hard_iface;
412 413
	int ret;

414 415
	ASSERT_RTNL();

416 417 418 419 420 421
	ret = is_valid_iface(net_dev);
	if (ret != 1)
		goto out;

	dev_hold(net_dev);

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

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

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

438 439
	check_known_mac_addr(hard_iface->net_dev);
	list_add_tail_rcu(&hard_iface->list, &hardif_list);
440

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

448
	return hard_iface;
449 450

free_if:
451
	kfree(hard_iface);
452 453 454 455 456 457
release_dev:
	dev_put(net_dev);
out:
	return NULL;
}

458
static void hardif_remove_interface(struct hard_iface *hard_iface)
459
{
460 461
	ASSERT_RTNL();

462
	/* first deactivate interface */
463
	if (hard_iface->if_status != IF_NOT_IN_USE)
464
		batadv_hardif_disable_interface(hard_iface);
465

466
	if (hard_iface->if_status != IF_NOT_IN_USE)
467 468
		return;

469
	hard_iface->if_status = IF_TO_BE_REMOVED;
470
	batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
471
	hardif_free_ref(hard_iface);
472 473
}

474
void batadv_hardif_remove_interfaces(void)
475
{
476
	struct hard_iface *hard_iface, *hard_iface_tmp;
477

478
	rtnl_lock();
479 480 481 482
	list_for_each_entry_safe(hard_iface, hard_iface_tmp,
				 &hardif_list, list) {
		list_del_rcu(&hard_iface->list);
		hardif_remove_interface(hard_iface);
483 484 485 486 487 488 489
	}
	rtnl_unlock();
}

static int hard_if_event(struct notifier_block *this,
			 unsigned long event, void *ptr)
{
490
	struct net_device *net_dev = ptr;
491
	struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
492
	struct hard_iface *primary_if = NULL;
493 494
	struct bat_priv *bat_priv;

495 496
	if (!hard_iface && event == NETDEV_REGISTER)
		hard_iface = hardif_add_interface(net_dev);
497

498
	if (!hard_iface)
499 500 501 502
		goto out;

	switch (event) {
	case NETDEV_UP:
503
		hardif_activate_interface(hard_iface);
504 505 506
		break;
	case NETDEV_GOING_DOWN:
	case NETDEV_DOWN:
507
		hardif_deactivate_interface(hard_iface);
508 509
		break;
	case NETDEV_UNREGISTER:
510
		list_del_rcu(&hard_iface->list);
511

512
		hardif_remove_interface(hard_iface);
513 514
		break;
	case NETDEV_CHANGEMTU:
515
		if (hard_iface->soft_iface)
516
			batadv_update_min_mtu(hard_iface->soft_iface);
517 518
		break;
	case NETDEV_CHANGEADDR:
519
		if (hard_iface->if_status == IF_NOT_IN_USE)
520 521
			goto hardif_put;

522
		check_known_mac_addr(hard_iface->net_dev);
523

524
		bat_priv = netdev_priv(hard_iface->soft_iface);
525
		bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
526

527 528 529 530 531
		primary_if = primary_if_get_selected(bat_priv);
		if (!primary_if)
			goto hardif_put;

		if (hard_iface == primary_if)
532
			primary_if_update_addr(bat_priv, NULL);
533 534 535
		break;
	default:
		break;
J
Joe Perches 已提交
536
	}
537 538

hardif_put:
539
	hardif_free_ref(hard_iface);
540
out:
541 542
	if (primary_if)
		hardif_free_ref(primary_if);
543 544 545
	return NOTIFY_DONE;
}

546 547
/* This function returns true if the interface represented by ifindex is a
 * 802.11 wireless device */
548
bool batadv_is_wifi_iface(int ifindex)
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
{
	struct net_device *net_device = NULL;
	bool ret = false;

	if (ifindex == NULL_IFINDEX)
		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
	 * check for wireless_handlers != NULL */
	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;
}

576
struct notifier_block batadv_hard_if_notifier = {
577 578
	.notifier_call = hard_if_event,
};