hard-interface.c 14.5 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 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
static struct hard_iface *hardif_get_active(const struct net_device *soft_iface)
79
{
80
	struct hard_iface *hard_iface;
81 82

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

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

92
	hard_iface = NULL;
93 94 95

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

99 100
static void primary_if_update_addr(struct bat_priv *bat_priv,
				   struct hard_iface *oldif)
101 102
{
	struct vis_packet *vis_packet;
103 104 105 106 107
	struct hard_iface *primary_if;

	primary_if = primary_if_get_selected(bat_priv);
	if (!primary_if)
		goto out;
108 109 110

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

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

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

126
	ASSERT_RTNL();
127

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

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

134
	if (!new_hard_iface)
135
		goto out;
136

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

out:
	if (curr_hard_iface)
		hardif_free_ref(curr_hard_iface);
143 144
}

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

	return false;
}

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

	rcu_read_lock();
158
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
159 160
		if ((hard_iface->if_status != IF_ACTIVE) &&
		    (hard_iface->if_status != IF_TO_BE_ACTIVATED))
161 162
			continue;

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

166 167
		if (!compare_eth(hard_iface->net_dev->dev_addr,
				 net_dev->dev_addr))
168 169
			continue;

170 171 172
		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");
173 174 175 176
	}
	rcu_read_unlock();
}

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

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

	rcu_read_lock();
190
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
191 192
		if ((hard_iface->if_status != IF_ACTIVE) &&
		    (hard_iface->if_status != IF_TO_BE_ACTIVATED))
193 194
			continue;

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

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

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

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

216
static void hardif_activate_interface(struct hard_iface *hard_iface)
217 218
{
	struct bat_priv *bat_priv;
219
	struct hard_iface *primary_if = NULL;
220

221
	if (hard_iface->if_status != IF_INACTIVE)
222
		goto out;
223

224
	bat_priv = netdev_priv(hard_iface->soft_iface);
225

226
	bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
227
	hard_iface->if_status = IF_TO_BE_ACTIVATED;
228

229
	/* the first active interface becomes our primary interface or
230
	 * the next active interface after the old primary interface was removed
231
	 */
232 233 234
	primary_if = primary_if_get_selected(bat_priv);
	if (!primary_if)
		primary_if_select(bat_priv, hard_iface);
235

236 237
	bat_info(hard_iface->soft_iface, "Interface activated: %s\n",
		 hard_iface->net_dev->name);
238

239
	batadv_update_min_mtu(hard_iface->soft_iface);
240 241 242 243

out:
	if (primary_if)
		hardif_free_ref(primary_if);
244 245
}

246
static void hardif_deactivate_interface(struct hard_iface *hard_iface)
247
{
248 249
	if ((hard_iface->if_status != IF_ACTIVE) &&
	    (hard_iface->if_status != IF_TO_BE_ACTIVATED))
250 251
		return;

252
	hard_iface->if_status = IF_INACTIVE;
253

254 255
	bat_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
		 hard_iface->net_dev->name);
256

257
	batadv_update_min_mtu(hard_iface->soft_iface);
258 259
}

260 261
int batadv_hardif_enable_interface(struct hard_iface *hard_iface,
				   const char *iface_name)
262 263
{
	struct bat_priv *bat_priv;
264 265
	struct net_device *soft_iface;
	int ret;
266

267
	if (hard_iface->if_status != IF_NOT_IN_USE)
268 269
		goto out;

270
	if (!atomic_inc_not_zero(&hard_iface->refcount))
271 272
		goto out;

273 274
	/* hard-interface is part of a bridge */
	if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
275
		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",
276 277
		       hard_iface->net_dev->name);

278
	soft_iface = dev_get_by_name(&init_net, iface_name);
279

280
	if (!soft_iface) {
281
		soft_iface = batadv_softif_create(iface_name);
282

283 284
		if (!soft_iface) {
			ret = -ENOMEM;
285
			goto err;
286
		}
287 288

		/* dev_get_by_name() increases the reference counter for us */
289 290 291
		dev_hold(soft_iface);
	}

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

299
	hard_iface->soft_iface = soft_iface;
300
	bat_priv = netdev_priv(hard_iface->soft_iface);
301

302
	ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
303
	if (ret < 0)
304
		goto err_dev;
305

306
	hard_iface->if_num = bat_priv->num_ifaces;
307
	bat_priv->num_ifaces++;
308
	hard_iface->if_status = IF_INACTIVE;
309
	batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
310

311
	hard_iface->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
312
	hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
313 314
	hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
	dev_add_pack(&hard_iface->batman_adv_ptype);
315

316 317 318
	atomic_set(&hard_iface->frag_seqno, 1);
	bat_info(hard_iface->soft_iface, "Adding interface: %s\n",
		 hard_iface->net_dev->name);
319

320
	if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
321
		ETH_DATA_LEN + BAT_HEADER_LEN)
322
		bat_info(hard_iface->soft_iface,
323
			 "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",
324 325
			 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
			 ETH_DATA_LEN + BAT_HEADER_LEN);
326

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

334 335
	if (hardif_is_iface_up(hard_iface))
		hardif_activate_interface(hard_iface);
336
	else
337 338
		bat_err(hard_iface->soft_iface,
			"Not using interface %s (retrying later): interface not active\n",
339
			hard_iface->net_dev->name);
340 341

	/* begin scheduling originator messages on that interface */
342
	batadv_schedule_bat_ogm(hard_iface);
343 344 345 346

out:
	return 0;

347 348
err_dev:
	dev_put(soft_iface);
349
err:
350
	hardif_free_ref(hard_iface);
351
	return ret;
352 353
}

354
void batadv_hardif_disable_interface(struct hard_iface *hard_iface)
355
{
356
	struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
357
	struct hard_iface *primary_if = NULL;
358

359 360
	if (hard_iface->if_status == IF_ACTIVE)
		hardif_deactivate_interface(hard_iface);
361

362
	if (hard_iface->if_status != IF_INACTIVE)
363
		goto out;
364

365 366 367
	bat_info(hard_iface->soft_iface, "Removing interface: %s\n",
		 hard_iface->net_dev->name);
	dev_remove_pack(&hard_iface->batman_adv_ptype);
368 369

	bat_priv->num_ifaces--;
370
	batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
371

372 373
	primary_if = primary_if_get_selected(bat_priv);
	if (hard_iface == primary_if) {
374
		struct hard_iface *new_if;
375

376
		new_if = hardif_get_active(hard_iface->soft_iface);
377
		primary_if_select(bat_priv, new_if);
378 379

		if (new_if)
380
			hardif_free_ref(new_if);
381 382
	}

383
	bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
384
	hard_iface->if_status = IF_NOT_IN_USE;
385

386
	/* delete all references to this hard_iface */
387
	batadv_purge_orig_ref(bat_priv);
388
	batadv_purge_outstanding_packets(bat_priv, hard_iface);
389
	dev_put(hard_iface->soft_iface);
390 391 392

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

395 396
	hard_iface->soft_iface = NULL;
	hardif_free_ref(hard_iface);
397 398 399 400

out:
	if (primary_if)
		hardif_free_ref(primary_if);
401 402
}

403
static struct hard_iface *hardif_add_interface(struct net_device *net_dev)
404
{
405
	struct hard_iface *hard_iface;
406 407
	int ret;

408 409
	ASSERT_RTNL();

410 411 412 413 414 415
	ret = is_valid_iface(net_dev);
	if (ret != 1)
		goto out;

	dev_hold(net_dev);

416
	hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
417
	if (!hard_iface)
418 419
		goto release_dev;

420
	ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
421 422 423
	if (ret)
		goto free_if;

424 425 426 427 428
	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);
429
	/* extra reference for return */
430
	atomic_set(&hard_iface->refcount, 2);
431

432
	check_known_mac_addr(hard_iface->net_dev);
433
	list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
434

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

441
	return hard_iface;
442 443

free_if:
444
	kfree(hard_iface);
445 446 447 448 449 450
release_dev:
	dev_put(net_dev);
out:
	return NULL;
}

451
static void hardif_remove_interface(struct hard_iface *hard_iface)
452
{
453 454
	ASSERT_RTNL();

455
	/* first deactivate interface */
456
	if (hard_iface->if_status != IF_NOT_IN_USE)
457
		batadv_hardif_disable_interface(hard_iface);
458

459
	if (hard_iface->if_status != IF_NOT_IN_USE)
460 461
		return;

462
	hard_iface->if_status = IF_TO_BE_REMOVED;
463
	batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
464
	hardif_free_ref(hard_iface);
465 466
}

467
void batadv_hardif_remove_interfaces(void)
468
{
469
	struct hard_iface *hard_iface, *hard_iface_tmp;
470

471
	rtnl_lock();
472
	list_for_each_entry_safe(hard_iface, hard_iface_tmp,
473
				 &batadv_hardif_list, list) {
474 475
		list_del_rcu(&hard_iface->list);
		hardif_remove_interface(hard_iface);
476 477 478 479 480 481 482
	}
	rtnl_unlock();
}

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

488 489
	if (!hard_iface && event == NETDEV_REGISTER)
		hard_iface = hardif_add_interface(net_dev);
490

491
	if (!hard_iface)
492 493 494 495
		goto out;

	switch (event) {
	case NETDEV_UP:
496
		hardif_activate_interface(hard_iface);
497 498 499
		break;
	case NETDEV_GOING_DOWN:
	case NETDEV_DOWN:
500
		hardif_deactivate_interface(hard_iface);
501 502
		break;
	case NETDEV_UNREGISTER:
503
		list_del_rcu(&hard_iface->list);
504

505
		hardif_remove_interface(hard_iface);
506 507
		break;
	case NETDEV_CHANGEMTU:
508
		if (hard_iface->soft_iface)
509
			batadv_update_min_mtu(hard_iface->soft_iface);
510 511
		break;
	case NETDEV_CHANGEADDR:
512
		if (hard_iface->if_status == IF_NOT_IN_USE)
513 514
			goto hardif_put;

515
		check_known_mac_addr(hard_iface->net_dev);
516

517
		bat_priv = netdev_priv(hard_iface->soft_iface);
518
		bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
519

520 521 522 523 524
		primary_if = primary_if_get_selected(bat_priv);
		if (!primary_if)
			goto hardif_put;

		if (hard_iface == primary_if)
525
			primary_if_update_addr(bat_priv, NULL);
526 527 528
		break;
	default:
		break;
J
Joe Perches 已提交
529
	}
530 531

hardif_put:
532
	hardif_free_ref(hard_iface);
533
out:
534 535
	if (primary_if)
		hardif_free_ref(primary_if);
536 537 538
	return NOTIFY_DONE;
}

539
/* This function returns true if the interface represented by ifindex is a
540 541
 * 802.11 wireless device
 */
542
bool batadv_is_wifi_iface(int ifindex)
543 544 545 546 547 548 549 550 551 552 553 554 555
{
	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
556 557
	 * check for wireless_handlers != NULL
	 */
558 559 560 561 562 563 564 565 566 567 568 569 570
	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;
}

571
struct notifier_block batadv_hard_if_notifier = {
572 573
	.notifier_call = hard_if_event,
};