br_vlan.c 15.0 KB
Newer Older
1 2 3 4 5 6 7
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
#include <linux/slab.h>

#include "br_private.h"

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
static void __vlan_add_pvid(struct net_port_vlans *v, u16 vid)
{
	if (v->pvid == vid)
		return;

	smp_wmb();
	v->pvid = vid;
}

static void __vlan_delete_pvid(struct net_port_vlans *v, u16 vid)
{
	if (v->pvid != vid)
		return;

	smp_wmb();
	v->pvid = 0;
}

26 27 28 29
static void __vlan_add_flags(struct net_port_vlans *v, u16 vid, u16 flags)
{
	if (flags & BRIDGE_VLAN_INFO_PVID)
		__vlan_add_pvid(v, vid);
30 31
	else
		__vlan_delete_pvid(v, vid);
32 33 34

	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
		set_bit(vid, v->untagged_bitmap);
35 36
	else
		clear_bit(vid, v->untagged_bitmap);
37 38
}

39
static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
40
{
41 42 43
	struct net_bridge_port *p = NULL;
	struct net_bridge *br;
	struct net_device *dev;
44 45
	int err;

46
	if (test_bit(vid, v->vlan_bitmap)) {
47
		__vlan_add_flags(v, vid, flags);
48 49
		return 0;
	}
50

51 52 53 54 55 56 57 58 59
	if (v->port_idx) {
		p = v->parent.port;
		br = p->br;
		dev = p->dev;
	} else {
		br = v->parent.br;
		dev = br->dev;
	}

60
	if (p) {
61
		/* Add VLAN to the device filter if it is supported.
62 63
		 * This ensures tagged traffic enters the bridge when
		 * promiscuous mode is disabled by br_manage_promisc().
64
		 */
65
		err = vlan_vid_add(dev, br->vlan_proto, vid);
66 67 68
		if (err)
			return err;
	}
69

70 71 72 73 74
	err = br_fdb_insert(br, p, dev->dev_addr, vid);
	if (err) {
		br_err(br, "failed insert local address into bridge "
		       "forwarding table\n");
		goto out_filt;
75 76 77
	}

	set_bit(vid, v->vlan_bitmap);
78
	v->num_vlans++;
79
	__vlan_add_flags(v, vid, flags);
80

81
	return 0;
82 83

out_filt:
84
	if (p)
85
		vlan_vid_del(dev, br->vlan_proto, vid);
86
	return err;
87 88 89 90 91 92 93
}

static int __vlan_del(struct net_port_vlans *v, u16 vid)
{
	if (!test_bit(vid, v->vlan_bitmap))
		return -EINVAL;

94
	__vlan_delete_pvid(v, vid);
95
	clear_bit(vid, v->untagged_bitmap);
96

97 98 99 100
	if (v->port_idx) {
		struct net_bridge_port *p = v->parent.port;
		vlan_vid_del(p->dev, p->br->vlan_proto, vid);
	}
101 102

	clear_bit(vid, v->vlan_bitmap);
103
	v->num_vlans--;
104
	if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
105
		if (v->port_idx)
106
			RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
107
		else
108
			RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
109 110 111 112 113 114 115
		kfree_rcu(v, rcu);
	}
	return 0;
}

static void __vlan_flush(struct net_port_vlans *v)
{
116 117
	smp_wmb();
	v->pvid = 0;
118
	bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
119
	if (v->port_idx)
120
		RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
121
	else
122
		RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
123 124 125
	kfree_rcu(v, rcu);
}

126 127 128
struct sk_buff *br_handle_vlan(struct net_bridge *br,
			       const struct net_port_vlans *pv,
			       struct sk_buff *skb)
129 130 131
{
	u16 vid;

132 133
	/* If this packet was not filtered at input, let it pass */
	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
134 135
		goto out;

136 137 138 139 140 141 142 143 144 145 146 147 148 149
	/* Vlan filter table must be configured at this point.  The
	 * only exception is the bridge is set in promisc mode and the
	 * packet is destined for the bridge device.  In this case
	 * pass the packet as is.
	 */
	if (!pv) {
		if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
			goto out;
		} else {
			kfree_skb(skb);
			return NULL;
		}
	}

150
	/* At this point, we know that the frame was filtered and contains
151
	 * a valid vlan id.  If the vlan id is set in the untagged bitmap,
T
tanxiaojun 已提交
152
	 * send untagged; otherwise, send tagged.
153 154
	 */
	br_vlan_get_tag(skb, &vid);
155
	if (test_bit(vid, pv->untagged_bitmap))
156
		skb->vlan_tci = 0;
157 158 159 160 161 162 163 164 165

out:
	return skb;
}

/* Called under RCU */
bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
			struct sk_buff *skb, u16 *vid)
{
166 167
	bool tagged;
	__be16 proto;
168

169 170 171
	/* If VLAN filtering is disabled on the bridge, all packets are
	 * permitted.
	 */
172 173
	if (!br->vlan_enabled) {
		BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
174
		return true;
175
	}
176 177 178 179 180

	/* If there are no vlan in the permitted list, all packets are
	 * rejected.
	 */
	if (!v)
181
		goto drop;
182

183
	BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
184 185
	proto = br->vlan_proto;

186 187 188 189
	/* If vlan tx offload is disabled on bridge device and frame was
	 * sent from vlan device on the bridge device, it does not have
	 * HW accelerated vlan tag.
	 */
190
	if (unlikely(!skb_vlan_tag_present(skb) &&
191
		     skb->protocol == proto)) {
192
		skb = skb_vlan_untag(skb);
193 194 195 196
		if (unlikely(!skb))
			return false;
	}

197 198 199 200 201
	if (!br_vlan_get_tag(skb, vid)) {
		/* Tagged frame */
		if (skb->vlan_proto != proto) {
			/* Protocol-mismatch, empty out vlan_tci for new tag */
			skb_push(skb, ETH_HLEN);
202
			skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
203
							skb_vlan_tag_get(skb));
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
			if (unlikely(!skb))
				return false;

			skb_pull(skb, ETH_HLEN);
			skb_reset_mac_len(skb);
			*vid = 0;
			tagged = false;
		} else {
			tagged = true;
		}
	} else {
		/* Untagged frame */
		tagged = false;
	}

219
	if (!*vid) {
220 221
		u16 pvid = br_get_pvid(v);

222 223 224
		/* Frame had a tag with VID 0 or did not have a tag.
		 * See if pvid is set on this port.  That tells us which
		 * vlan untagged or priority-tagged traffic belongs to.
225
		 */
V
Vlad Yasevich 已提交
226
		if (!pvid)
227
			goto drop;
228

229 230
		/* PVID is set on this port.  Any untagged or priority-tagged
		 * ingress frame is considered to belong to this vlan.
231
		 */
232
		*vid = pvid;
233
		if (likely(!tagged))
234
			/* Untagged Frame. */
235
			__vlan_hwaccel_put_tag(skb, proto, pvid);
236 237 238 239 240 241 242 243
		else
			/* Priority-tagged Frame.
			 * At this point, We know that skb->vlan_tci had
			 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
			 * We update only VID field and preserve PCP field.
			 */
			skb->vlan_tci |= pvid;

244 245 246 247 248
		return true;
	}

	/* Frame had a valid vlan tag.  See if vlan is allowed */
	if (test_bit(*vid, v->vlan_bitmap))
249
		return true;
250 251
drop:
	kfree_skb(skb);
252 253 254
	return false;
}

255 256 257 258 259 260 261
/* Called under RCU. */
bool br_allowed_egress(struct net_bridge *br,
		       const struct net_port_vlans *v,
		       const struct sk_buff *skb)
{
	u16 vid;

262 263
	/* If this packet was not filtered at input, let it pass */
	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
264 265 266 267 268 269 270 271 272 273 274 275
		return true;

	if (!v)
		return false;

	br_vlan_get_tag(skb, &vid);
	if (test_bit(vid, v->vlan_bitmap))
		return true;

	return false;
}

276 277 278 279 280 281
/* Called under RCU */
bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
{
	struct net_bridge *br = p->br;
	struct net_port_vlans *v;

282
	/* If filtering was disabled at input, let it pass. */
283
	if (!br->vlan_enabled)
284 285 286 287 288 289
		return true;

	v = rcu_dereference(p->vlan_info);
	if (!v)
		return false;

290 291 292
	if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
		*vid = 0;

293 294
	if (!*vid) {
		*vid = br_get_pvid(v);
V
Vlad Yasevich 已提交
295
		if (!*vid)
296 297 298 299 300 301 302 303 304 305 306
			return false;

		return true;
	}

	if (test_bit(*vid, v->vlan_bitmap))
		return true;

	return false;
}

307 308 309
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
310
int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
311 312 313 314 315 316 317 318
{
	struct net_port_vlans *pv = NULL;
	int err;

	ASSERT_RTNL();

	pv = rtnl_dereference(br->vlan_info);
	if (pv)
319
		return __vlan_add(pv, vid, flags);
320 321 322 323 324 325 326 327

	/* Create port vlan infomration
	 */
	pv = kzalloc(sizeof(*pv), GFP_KERNEL);
	if (!pv)
		return -ENOMEM;

	pv->parent.br = br;
328
	err = __vlan_add(pv, vid, flags);
329 330 331 332 333 334 335 336 337 338
	if (err)
		goto out;

	rcu_assign_pointer(br->vlan_info, pv);
	return 0;
out:
	kfree(pv);
	return err;
}

339 340 341
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
342 343 344 345 346 347 348 349 350 351
int br_vlan_delete(struct net_bridge *br, u16 vid)
{
	struct net_port_vlans *pv;

	ASSERT_RTNL();

	pv = rtnl_dereference(br->vlan_info);
	if (!pv)
		return -EINVAL;

352
	br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
353

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	__vlan_del(pv, vid);
	return 0;
}

void br_vlan_flush(struct net_bridge *br)
{
	struct net_port_vlans *pv;

	ASSERT_RTNL();
	pv = rtnl_dereference(br->vlan_info);
	if (!pv)
		return;

	__vlan_flush(pv);
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
bool br_vlan_find(struct net_bridge *br, u16 vid)
{
	struct net_port_vlans *pv;
	bool found = false;

	rcu_read_lock();
	pv = rcu_dereference(br->vlan_info);

	if (!pv)
		goto out;

	if (test_bit(vid, pv->vlan_bitmap))
		found = true;

out:
	rcu_read_unlock();
	return found;
}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
/* Must be protected by RTNL. */
static void recalculate_group_addr(struct net_bridge *br)
{
	if (br->group_addr_set)
		return;

	spin_lock_bh(&br->lock);
	if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
		/* Bridge Group Address */
		br->group_addr[5] = 0x00;
	} else { /* vlan_enabled && ETH_P_8021AD */
		/* Provider Bridge Group Address */
		br->group_addr[5] = 0x08;
	}
	spin_unlock_bh(&br->lock);
}

/* Must be protected by RTNL. */
void br_recalculate_fwd_mask(struct net_bridge *br)
{
	if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
		br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
	else /* vlan_enabled && ETH_P_8021AD */
		br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
					      ~(1u << br->group_addr[5]);
}

416 417 418 419 420 421 422 423 424
int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
{
	if (!rtnl_trylock())
		return restart_syscall();

	if (br->vlan_enabled == val)
		goto unlock;

	br->vlan_enabled = val;
425
	br_manage_promisc(br);
426 427
	recalculate_group_addr(br);
	br_recalculate_fwd_mask(br);
428 429 430 431 432 433

unlock:
	rtnl_unlock();
	return 0;
}

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
{
	int err = 0;
	struct net_bridge_port *p;
	struct net_port_vlans *pv;
	__be16 proto, oldproto;
	u16 vid, errvid;

	if (val != ETH_P_8021Q && val != ETH_P_8021AD)
		return -EPROTONOSUPPORT;

	if (!rtnl_trylock())
		return restart_syscall();

	proto = htons(val);
	if (br->vlan_proto == proto)
		goto unlock;

	/* Add VLANs for the new proto to the device filter. */
	list_for_each_entry(p, &br->port_list, list) {
		pv = rtnl_dereference(p->vlan_info);
		if (!pv)
			continue;

		for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
			err = vlan_vid_add(p->dev, proto, vid);
			if (err)
				goto err_filt;
		}
	}

	oldproto = br->vlan_proto;
	br->vlan_proto = proto;

	recalculate_group_addr(br);
	br_recalculate_fwd_mask(br);

	/* Delete VLANs for the old proto from the device filter. */
	list_for_each_entry(p, &br->port_list, list) {
		pv = rtnl_dereference(p->vlan_info);
		if (!pv)
			continue;

		for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
			vlan_vid_del(p->dev, oldproto, vid);
	}

unlock:
	rtnl_unlock();
	return err;

err_filt:
	errvid = vid;
	for_each_set_bit(vid, pv->vlan_bitmap, errvid)
		vlan_vid_del(p->dev, proto, vid);

	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
		pv = rtnl_dereference(p->vlan_info);
		if (!pv)
			continue;

		for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
			vlan_vid_del(p->dev, proto, vid);
	}

	goto unlock;
}

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 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 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
static bool vlan_default_pvid(struct net_port_vlans *pv, u16 vid)
{
	return pv && vid == pv->pvid && test_bit(vid, pv->untagged_bitmap);
}

static void br_vlan_disable_default_pvid(struct net_bridge *br)
{
	struct net_bridge_port *p;
	u16 pvid = br->default_pvid;

	/* Disable default_pvid on all ports where it is still
	 * configured.
	 */
	if (vlan_default_pvid(br_get_vlan_info(br), pvid))
		br_vlan_delete(br, pvid);

	list_for_each_entry(p, &br->port_list, list) {
		if (vlan_default_pvid(nbp_get_vlan_info(p), pvid))
			nbp_vlan_delete(p, pvid);
	}

	br->default_pvid = 0;
}

static int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
{
	struct net_bridge_port *p;
	u16 old_pvid;
	int err = 0;
	unsigned long *changed;

	changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
			  GFP_KERNEL);
	if (!changed)
		return -ENOMEM;

	old_pvid = br->default_pvid;

	/* Update default_pvid config only if we do not conflict with
	 * user configuration.
	 */
	if ((!old_pvid || vlan_default_pvid(br_get_vlan_info(br), old_pvid)) &&
	    !br_vlan_find(br, pvid)) {
		err = br_vlan_add(br, pvid,
				  BRIDGE_VLAN_INFO_PVID |
				  BRIDGE_VLAN_INFO_UNTAGGED);
		if (err)
			goto out;
		br_vlan_delete(br, old_pvid);
		set_bit(0, changed);
	}

	list_for_each_entry(p, &br->port_list, list) {
		/* Update default_pvid config only if we do not conflict with
		 * user configuration.
		 */
		if ((old_pvid &&
		     !vlan_default_pvid(nbp_get_vlan_info(p), old_pvid)) ||
		    nbp_vlan_find(p, pvid))
			continue;

		err = nbp_vlan_add(p, pvid,
				   BRIDGE_VLAN_INFO_PVID |
				   BRIDGE_VLAN_INFO_UNTAGGED);
		if (err)
			goto err_port;
		nbp_vlan_delete(p, old_pvid);
		set_bit(p->port_no, changed);
	}

	br->default_pvid = pvid;

out:
	kfree(changed);
	return err;

err_port:
	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
		if (!test_bit(p->port_no, changed))
			continue;

		if (old_pvid)
			nbp_vlan_add(p, old_pvid,
				     BRIDGE_VLAN_INFO_PVID |
				     BRIDGE_VLAN_INFO_UNTAGGED);
		nbp_vlan_delete(p, pvid);
	}

	if (test_bit(0, changed)) {
		if (old_pvid)
			br_vlan_add(br, old_pvid,
				    BRIDGE_VLAN_INFO_PVID |
				    BRIDGE_VLAN_INFO_UNTAGGED);
		br_vlan_delete(br, pvid);
	}
	goto out;
}

600 601 602 603 604
int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
{
	u16 pvid = val;
	int err = 0;

605
	if (val >= VLAN_VID_MASK)
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
		return -EINVAL;

	if (!rtnl_trylock())
		return restart_syscall();

	if (pvid == br->default_pvid)
		goto unlock;

	/* Only allow default pvid change when filtering is disabled */
	if (br->vlan_enabled) {
		pr_info_once("Please disable vlan filtering to change default_pvid\n");
		err = -EPERM;
		goto unlock;
	}

621 622 623 624
	if (!pvid)
		br_vlan_disable_default_pvid(br);
	else
		err = __br_vlan_set_default_pvid(br, pvid);
625 626 627 628 629 630

unlock:
	rtnl_unlock();
	return err;
}

631
int br_vlan_init(struct net_bridge *br)
632 633
{
	br->vlan_proto = htons(ETH_P_8021Q);
634
	br->default_pvid = 1;
635 636
	return br_vlan_add(br, 1,
			   BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED);
637 638
}

639 640 641
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
642
int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
643 644 645 646 647 648 649 650
{
	struct net_port_vlans *pv = NULL;
	int err;

	ASSERT_RTNL();

	pv = rtnl_dereference(port->vlan_info);
	if (pv)
651
		return __vlan_add(pv, vid, flags);
652 653 654 655 656 657 658 659 660 661 662

	/* Create port vlan infomration
	 */
	pv = kzalloc(sizeof(*pv), GFP_KERNEL);
	if (!pv) {
		err = -ENOMEM;
		goto clean_up;
	}

	pv->port_idx = port->port_no;
	pv->parent.port = port;
663
	err = __vlan_add(pv, vid, flags);
664 665 666 667 668 669 670 671 672 673 674
	if (err)
		goto clean_up;

	rcu_assign_pointer(port->vlan_info, pv);
	return 0;

clean_up:
	kfree(pv);
	return err;
}

675 676 677
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
678 679 680 681 682 683 684 685 686 687
int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
{
	struct net_port_vlans *pv;

	ASSERT_RTNL();

	pv = rtnl_dereference(port->vlan_info);
	if (!pv)
		return -EINVAL;

688
	br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
689

690 691 692 693 694 695
	return __vlan_del(pv, vid);
}

void nbp_vlan_flush(struct net_bridge_port *port)
{
	struct net_port_vlans *pv;
696
	u16 vid;
697 698 699 700 701 702 703

	ASSERT_RTNL();

	pv = rtnl_dereference(port->vlan_info);
	if (!pv)
		return;

704
	for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
705
		vlan_vid_del(port->dev, port->br->vlan_proto, vid);
706

707 708
	__vlan_flush(pv);
}
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727

bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
{
	struct net_port_vlans *pv;
	bool found = false;

	rcu_read_lock();
	pv = rcu_dereference(port->vlan_info);

	if (!pv)
		goto out;

	if (test_bit(vid, pv->vlan_bitmap))
		found = true;

out:
	rcu_read_unlock();
	return found;
}
728 729 730 731 732 733 734 735 736

int nbp_vlan_init(struct net_bridge_port *p)
{
	return p->br->default_pvid ?
			nbp_vlan_add(p, p->br->default_pvid,
				     BRIDGE_VLAN_INFO_PVID |
				     BRIDGE_VLAN_INFO_UNTAGGED) :
			0;
}