DragonFlyBSD Kernel Audit
sys/net/vlan/if_vlan.c
← back
   1
   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
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 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
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 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
 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
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
/*
 * Copyright 1998 Massachusetts Institute of Technology
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that both the above copyright notice and this
 * permission notice appear in all copies, that both the above
 * copyright notice and this permission notice appear in all
 * supporting documentation, and that the name of M.I.T. not be used
 * in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  M.I.T. makes
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 * 
 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD: src/sys/net/if_vlan.c,v 1.15.2.13 2003/02/14 22:25:58 fenner Exp $
 */

/*
 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
 * Might be extended some day to also handle IEEE 802.1p priority
 * tagging.  This is sort of sneaky in the implementation, since
 * we need to pretend to be enough of an Ethernet implementation
 * to make arp work.  The way we do this is by telling everyone
 * that we are an Ethernet, and then catch the packets that
 * ether_output() left on our output queue queue when it calls
 * if_start(), rewrite them for use by the real outgoing interface,
 * and ask it to send them.
 *
 *
 * Note about vlan's MP safe approach:
 *
 * - All configuration operation, e.g. config, unconfig and change flags,
 *   is serialized by netisr0; not by vlan's serializer
 *
 * - Parent interface's trunk and vlans are linked in the following
 *   fashion:
 *                     CPU0     CPU1     CPU2     CPU3
 *   +--------------+--------+--------+--------+--------+
 *   | parent ifnet |trunk[0]|trunk[1]|trunk[2]|trunk[3]|
 *   +--------------+--------+--------+--------+--------+
 *                       |        |        |        |
 *                       V        V        V        V
 *   +--------------+--------+--------+--------+--------+
 *   |   vlan ifnet |entry[0]|entry[1]|entry[2]|entry[3]|
 *   +--------------+--------+--------+--------+--------+
 *                       |        |        |        |
 *                       V        V        V        V
 *   +--------------+--------+--------+--------+--------+
 *   |   vlan ifnet |entry[0]|entry[1]|entry[2]|entry[3]|
 *   +--------------+--------+--------+--------+--------+
 *
 * - Vlan is linked/unlinked onto parent interface's trunk using following
 *   way:
 *
 *       CPU0             CPU1              CPU2              CPU3
 *
 *      netisr0 <----------------------------------------------+
 *  (config/unconfig)                                          |
 *         |                                                   |
 *         | domsg                                             | replymsg
 *         : (link/unlink)                                     |
 *         :                                                   |
 *         :   fwdmsg             fwdmsg            fwdmsg     |
 *         :-----------> netisr1 --------> netisr2 --------> netisr3
 *                    (link/unlink)     (link/unlink)     (link/unlink)
 *
 * - Parent interface's trunk is destroyed in the following lockless way:
 *
 *     old_trunk = ifp->if_vlantrunks;
 *     ifp->if_vlantrunks = NULL;
 *     netmsg_service_sync();
 *     (*)
 *     free(old_trunk);
 *
 *   Since all of the accessing of if_vlantrunks only happens in network
 *   threads (percpu netisr and ifnet threads), after netmsg_service_sync()
 *   the network threads are promised to see only NULL if_vlantrunks; we
 *   are safe to free the "to be destroyed" parent interface's trunk
 *   afterwards.
 */

#ifndef NVLAN
#include "use_vlan.h"
#endif
#include "opt_inet.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/module.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/bus.h>
#include <sys/thread2.h>

#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/ifq_var.h>
#include <net/if_clone.h>
#include <net/netmsg2.h>
#include <net/netisr2.h>

#ifdef INET
#include <netinet/in.h>
#include <netinet/if_ether.h>
#endif

#include <net/vlan/if_vlan_var.h>
#include <net/vlan/if_vlan_ether.h>

struct ifvlan;

struct vlan_mc_entry {
	struct ether_addr		mc_addr;
	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
};

struct vlan_entry {
	struct ifvlan		*ifv;
	LIST_ENTRY(vlan_entry)	ifv_link;
};

struct	ifvlan {
	struct	arpcom ifv_ac;	/* make this an interface */
	struct	ifnet *ifv_p;	/* parent inteface of this vlan */
	int ifv_pflags;		/* special flags we have set on parent */
	struct	ifv_linkmib {
		int	ifvm_parent;
		uint16_t ifvm_proto; /* encapsulation ethertype */
		uint16_t ifvm_tag; /* tag to apply on packets leaving if */
	}	ifv_mib;
	SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
	LIST_ENTRY(ifvlan) ifv_list;
	struct vlan_entry ifv_entries[1];
};
#define	ifv_if	ifv_ac.ac_if
#define	ifv_tag	ifv_mib.ifvm_tag

struct vlan_trunk {
	LIST_HEAD(, vlan_entry) vlan_list;
};

struct netmsg_vlan {
	struct netmsg_base base;
	struct ifvlan	*nv_ifv;
	struct ifnet	*nv_ifp_p;
	const char	*nv_parent_name;
	uint16_t	nv_vlantag;
};

#define VLANNAME	"vlan"

SYSCTL_DECL(_net_link);
SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");

static MALLOC_DEFINE(M_VLAN, "vlan", "802.1Q Virtual LAN Interface");
static LIST_HEAD(, ifvlan) ifv_list;

static int	vlan_clone_create(struct if_clone *, int, caddr_t, caddr_t);
static int	vlan_clone_destroy(struct ifnet *);
static void	vlan_ifdetach(void *, struct ifnet *);

static void	vlan_init(void *);
static void	vlan_start(struct ifnet *, struct ifaltq_subque *);
static int	vlan_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
static void	vlan_input(struct mbuf *);

static int	vlan_setflags(struct ifvlan *, struct ifnet *, int);
static int	vlan_setflag(struct ifvlan *, struct ifnet *, int, int,
			     int (*)(struct ifnet *, int));
static int	vlan_config_flags(struct ifvlan *ifv);
static void	vlan_clrmulti(struct ifvlan *, struct ifnet *);
static int	vlan_setmulti(struct ifvlan *, struct ifnet *);
static int	vlan_config_multi(struct ifvlan *);
static int	vlan_config(struct ifvlan *, const char *, uint16_t);
static int	vlan_unconfig(struct ifvlan *);
static void	vlan_link(struct ifvlan *, struct ifnet *);
static void	vlan_unlink(struct ifvlan *, struct ifnet *);

static void	vlan_config_dispatch(netmsg_t);
static void	vlan_unconfig_dispatch(netmsg_t);
static void	vlan_link_dispatch(netmsg_t);
static void	vlan_unlink_dispatch(netmsg_t);
static void	vlan_multi_dispatch(netmsg_t);
static void	vlan_flags_dispatch(netmsg_t);
static void	vlan_ifdetach_dispatch(netmsg_t);

/* Special flags we should propagate to parent */
static struct {
	int flag;
	int (*func)(struct ifnet *, int);
} vlan_pflags[] = {
	{ IFF_PROMISC, ifpromisc },
	{ IFF_ALLMULTI, if_allmulti },
	{ 0, NULL }
};

static eventhandler_tag vlan_ifdetach_cookie;
static struct if_clone vlan_cloner =
	IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy,
			     NVLAN, IF_MAXUNIT);

/*
 * Handle IFF_* flags that require certain changes on the parent:
 * if "set" is true, update parent's flags respective to our if_flags;
 * if "set" is false, forcedly clear the flags set on parent.
 */
static int
vlan_setflags(struct ifvlan *ifv, struct ifnet *ifp_p, int set)
{
	int error, i;

	ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);

	for (i = 0; vlan_pflags[i].func != NULL; i++) {
		error = vlan_setflag(ifv, ifp_p, vlan_pflags[i].flag,
				     set, vlan_pflags[i].func);
		if (error)
			return error;
	}
	return 0;
}

/* Handle a reference counted flag that should be set on the parent as well */
static int
vlan_setflag(struct ifvlan *ifv, struct ifnet *ifp_p, int flag, int set,
	     int (*func)(struct ifnet *, int))
{
	struct ifnet *ifp = &ifv->ifv_if;
	int error, ifv_flag;

	ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);

	ifv_flag = set ? (ifp->if_flags & flag) : 0;

	/*
	 * See if recorded parent's status is different from what
	 * we want it to be.  If it is, flip it.  We record parent's
	 * status in ifv_pflags so that we won't clear parent's flag
	 * we haven't set.  In fact, we don't clear or set parent's
	 * flags directly, but get or release references to them.
	 * That's why we can be sure that recorded flags still are
	 * in accord with actual parent's flags.
	 */
	if (ifv_flag != (ifv->ifv_pflags & flag)) {
		error = func(ifp_p, ifv_flag);
		if (error)
			return error;
		ifv->ifv_pflags &= ~flag;
		ifv->ifv_pflags |= ifv_flag;
	}
	return 0;
}

/*
 * Program our multicast filter. What we're actually doing is
 * programming the multicast filter of the parent. This has the
 * side effect of causing the parent interface to receive multicast
 * traffic that it doesn't really want, which ends up being discarded
 * later by the upper protocol layers. Unfortunately, there's no way
 * to avoid this: there really is only one physical interface.
 */
static int
vlan_setmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
{
	struct ifmultiaddr *ifma;
	struct vlan_mc_entry *mc = NULL;
	struct sockaddr_dl sdl;
	struct ifnet *ifp = &ifv->ifv_if;

	ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);

	/*
	 * First, remove any existing filter entries.
	 */
	vlan_clrmulti(ifv, ifp_p);

	/*
	 * Save the filter entries to be added to parent.
	 *
	 * TODO: need ifnet_serialize_main
	 */
	ifnet_serialize_all(ifp);
	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
		if (ifma->ifma_addr->sa_family != AF_LINK)
			continue;

		/* Save a copy */
		mc = kmalloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
		      &mc->mc_addr, ETHER_ADDR_LEN);
		SLIST_INSERT_HEAD(&ifv->vlan_mc_listhead, mc, mc_entries);
	}
	ifnet_deserialize_all(ifp);

	/*
	 * Now program new ones.
	 */
	bzero(&sdl, sizeof(sdl));
	sdl.sdl_len = sizeof(sdl);
	sdl.sdl_family = AF_LINK;
	sdl.sdl_index = ifp_p->if_index;
	sdl.sdl_type = IFT_ETHER;
	sdl.sdl_alen = ETHER_ADDR_LEN;

	/*
	 * Program the parent multicast filter
	 */
	SLIST_FOREACH(mc, &ifv->vlan_mc_listhead, mc_entries) {
		int error;

		bcopy(&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
		error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, NULL);
		if (error) {
			/* XXX probably should keep going */
			return error;
		}
	}
	return 0;
}

static void
vlan_clrmulti(struct ifvlan *ifv, struct ifnet *ifp_p)
{
	struct vlan_mc_entry *mc;
	struct sockaddr_dl sdl;

	ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);

	bzero(&sdl, sizeof(sdl));
	sdl.sdl_len = sizeof(sdl);
	sdl.sdl_family = AF_LINK;
	sdl.sdl_index = ifp_p->if_index;
	sdl.sdl_type = IFT_ETHER;
	sdl.sdl_alen = ETHER_ADDR_LEN;

	while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
		bcopy(&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
		if_delmulti(ifp_p, (struct sockaddr *)&sdl); /* ignore error */

		SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
		kfree(mc, M_VLAN);
	}
}

static int
vlan_modevent(module_t mod, int type, void *data)
{
	switch (type) {
	case MOD_LOAD:
		LIST_INIT(&ifv_list);
		vlan_input_p = vlan_input;
		vlan_ifdetach_cookie =
		EVENTHANDLER_REGISTER(ifnet_detach_event,
				      vlan_ifdetach, NULL,
				      EVENTHANDLER_PRI_ANY);
		if_clone_attach(&vlan_cloner);
		break;

	case MOD_UNLOAD:
		if_clone_detach(&vlan_cloner);

		vlan_input_p = NULL;
		/*
		 * Make sure that all protocol threads see vlan_input_p change.
		 */
		netmsg_service_sync();

		EVENTHANDLER_DEREGISTER(ifnet_detach_event,
					vlan_ifdetach_cookie);
		while (!LIST_EMPTY(&ifv_list))
			vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
		break;
	}
	return 0;
}

static moduledata_t vlan_mod = {
	"if_vlan",
	vlan_modevent,
	0
};

DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);

static void
vlan_ifdetach_dispatch(netmsg_t msg)
{
	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
	struct ifnet *ifp_p = vmsg->nv_ifp_p;
	struct vlan_trunk *vlantrunks, *trunk;
	struct vlan_entry *ifve;

	vlantrunks = ifp_p->if_vlantrunks;
	if (vlantrunks == NULL)
		goto reply;
	trunk = &vlantrunks[mycpuid];

	while (ifp_p->if_vlantrunks &&
	       (ifve = LIST_FIRST(&trunk->vlan_list)) != NULL)
	{
		vlan_unconfig(ifve->ifv);
	}
reply:
	lwkt_replymsg(&vmsg->base.lmsg, 0);
}

static void
vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
{
	struct netmsg_vlan vmsg;

	ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);

	bzero(&vmsg, sizeof(vmsg));

	netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
		    0, vlan_ifdetach_dispatch);
	vmsg.nv_ifp_p = ifp;

	lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
}

static int
vlan_clone_create(struct if_clone *ifc, int unit,
		  caddr_t params __unused, caddr_t data __unused)
{
	struct ifvlan *ifv;
	struct ifnet *ifp;
	int vlan_size, i;

	vlan_size = sizeof(struct ifvlan)
		  + ((netisr_ncpus - 1) * sizeof(struct vlan_entry));
	ifv = kmalloc(vlan_size, M_VLAN, M_WAITOK | M_ZERO);
	SLIST_INIT(&ifv->vlan_mc_listhead);
	for (i = 0; i < netisr_ncpus; ++i)
		ifv->ifv_entries[i].ifv = ifv;

	crit_enter();	/* XXX not MP safe */
	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
	crit_exit();

	ifp = &ifv->ifv_if;
	ifp->if_softc = ifv;
	if_initname(ifp, "vlan", unit);
	/* NB: flags are not set here */
	ifp->if_linkmib = &ifv->ifv_mib;
	ifp->if_linkmiblen = sizeof ifv->ifv_mib;
	/* NB: mtu is not set here */

	ifp->if_init = vlan_init;
	ifp->if_start = vlan_start;
	ifp->if_ioctl = vlan_ioctl;
	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
	ifq_set_ready(&ifp->if_snd);
	ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr, NULL);
	/* Now undo some of the damage... */
	ifp->if_data.ifi_type = IFT_L2VLAN;
	ifp->if_data.ifi_hdrlen = EVL_ENCAPLEN;

	return (0);
}

static int
vlan_clone_destroy(struct ifnet *ifp)
{
	struct ifvlan *ifv = ifp->if_softc;

	crit_enter();	/* XXX not MP safe */
	LIST_REMOVE(ifv, ifv_list);
	crit_exit();

	vlan_unconfig(ifv);
	ether_ifdetach(ifp);

	kfree(ifv, M_VLAN);

	return 0;
}

static void
vlan_init(void *xsc)
{
	struct ifvlan *ifv = xsc;
	struct ifnet *ifp = &ifv->ifv_if;

	ASSERT_IFNET_SERIALIZED_ALL(ifp);

	if (ifv->ifv_p != NULL)
		ifp->if_flags |= IFF_RUNNING;
}

static void
vlan_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
{
	struct ifvlan *ifv = ifp->if_softc;
	struct ifnet *ifp_p = ifv->ifv_p;
	struct mbuf *m;
	lwkt_port_t p_port;

	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
	ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);

	if (ifp_p == NULL) {
		ifsq_purge(ifsq);
		return;
	}

	if ((ifp->if_flags & IFF_RUNNING) == 0)
		return;

	p_port = netisr_cpuport(
	    ifsq_get_cpuid(ifq_get_subq_default(&ifp_p->if_snd)));
	for (;;) {
		struct netmsg_packet *nmp;

		m = ifsq_dequeue(ifsq);
		if (m == NULL)
			break;
		BPF_MTAP(ifp, m);

		/*
		 * Do not run parent's if_start() if the parent is not up,
		 * or parent's driver will cause a system crash.
		 */
		if ((ifp_p->if_flags & (IFF_UP | IFF_RUNNING)) !=
		    (IFF_UP | IFF_RUNNING)) {
			m_freem(m);
			IFNET_STAT_INC(ifp, collisions, 1);
			continue;
		}

		/*
		 * We need some way to tell the interface where the packet
		 * came from so that it knows how to find the VLAN tag to
		 * use, so we set the ether_vlantag in the mbuf packet header
		 * to our vlan tag.  We also set the M_VLANTAG flag in the
		 * mbuf to let the parent driver know that the ether_vlantag
		 * is really valid.
		 */
		m->m_pkthdr.ether_vlantag = ifv->ifv_tag;
		m->m_flags |= M_VLANTAG;

		nmp = &m->m_hdr.mh_netmsg;

		netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
			    0, vlan_start_dispatch);
		nmp->nm_packet = m;
		nmp->base.lmsg.u.ms_resultp = ifp_p;

		lwkt_sendmsg(p_port, &nmp->base.lmsg);
		IFNET_STAT_INC(ifp, opackets, 1);
	}
}

static void
vlan_input(struct mbuf *m)
{
	struct ifvlan *ifv = NULL;
	struct ifnet *rcvif;
	struct vlan_trunk *vlantrunks;
	struct vlan_entry *entry;
	int cpuid = mycpuid;

	ASSERT_NETISR_NCPUS(cpuid);

	rcvif = m->m_pkthdr.rcvif;
	KKASSERT(m->m_flags & M_VLANTAG);

	/* Make sure 'vlantrunks' is really used. */
	vlantrunks = rcvif->if_vlantrunks;
	cpu_ccfence();
	if (vlantrunks == NULL) {
		IFNET_STAT_INC(rcvif, noproto, 1);
		m_freem(m);
		return;
	}

	/*
	 * Locate the associated vlan
	 */
	LIST_FOREACH(entry, &vlantrunks[cpuid].vlan_list, ifv_link) {
		if (entry->ifv->ifv_tag ==
		    EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag))
		{
			ifv = entry->ifv;
			break;
		}
	}

	/*
	 * Discard packets to unknown vlans, if the vlan interface is
	 * not completely initialized yet, or it is being destroyed.
	 */
	if (ifv == NULL || ifv->ifv_p != rcvif) {
		IFNET_STAT_INC(rcvif, noproto, 1);
		m_freem(m);
		return;
	}

	/*
	 * Clear M_VLANTAG, then hand the vlan-stripped packet to the
	 * vlan(4) interface.
	 */
	m->m_flags &= ~M_VLANTAG;

	ether_reinput_oncpu(&ifv->ifv_if, m, REINPUT_RUNBPF);
}

static void
vlan_link_dispatch(netmsg_t msg)
{
	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
	struct ifvlan *ifv = vmsg->nv_ifv;
	struct ifnet *ifp_p = vmsg->nv_ifp_p;
	struct vlan_entry *entry;
	struct vlan_trunk *vlantrunks, *trunk;
	int cpu = mycpuid;

	vlantrunks = ifp_p->if_vlantrunks;
	KASSERT(vlantrunks != NULL,
		("vlan trunk has not been initialized yet"));

	entry = &ifv->ifv_entries[cpu];
	trunk = &vlantrunks[cpu];

	/*
	 * Critical section protects per-cpu list
	 */
	crit_enter();
	LIST_INSERT_HEAD(&trunk->vlan_list, entry, ifv_link);
	crit_exit();

	netisr_forwardmsg(&vmsg->base, cpu + 1);
}

static void
vlan_link(struct ifvlan *ifv, struct ifnet *ifp_p)
{
	struct netmsg_vlan vmsg;

	/* Assert in netisr0 */
	ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);

	if (ifp_p->if_vlantrunks == NULL) {
		struct vlan_trunk *vlantrunks;
		int i;

		vlantrunks = kmalloc(sizeof(*vlantrunks) * netisr_ncpus,
				     M_VLAN,
				     M_WAITOK | M_ZERO);
		for (i = 0; i < netisr_ncpus; ++i)
			LIST_INIT(&vlantrunks[i].vlan_list);

		ifp_p->if_vlantrunks = vlantrunks;
	}

	bzero(&vmsg, sizeof(vmsg));

	netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
		    0, vlan_link_dispatch);
	vmsg.nv_ifv = ifv;
	vmsg.nv_ifp_p = ifp_p;

	netisr_domsg(&vmsg.base, 0);
}

static void
vlan_config_dispatch(netmsg_t msg)
{
	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
	struct ifvlan *ifv;
	struct ifnet *ifp_p, *ifp;
	struct sockaddr_dl *sdl1, *sdl2;
	int error;

	/* Assert in netisr0 */

	ifp_p = ifunit_netisr(vmsg->nv_parent_name);
	if (ifp_p == NULL) {
		error = ENOENT;
		goto reply;
	}

	if (ifp_p->if_data.ifi_type != IFT_ETHER) {
		error = EPROTONOSUPPORT;
		goto reply;
	}

	ifv = vmsg->nv_ifv;
	ifp = &ifv->ifv_if;

	if (ifv->ifv_p) {
		error = EBUSY;
		goto reply;
	}

	/* Link vlan into parent's vlantrunk */
	vlan_link(ifv, ifp_p);

	ifnet_serialize_all(ifp);

	ifv->ifv_tag = vmsg->nv_vlantag;
	if (ifp_p->if_capenable & IFCAP_VLAN_MTU)
		ifp->if_mtu = ifp_p->if_mtu;
	else
		ifp->if_mtu = ifp_p->if_data.ifi_mtu - EVL_ENCAPLEN;

	/*
	 * Copy only a selected subset of flags from the parent.
	 * Other flags are none of our business.
	 */
#define VLAN_INHERIT_FLAGS	(IFF_BROADCAST | IFF_MULTICAST | \
				 IFF_SIMPLEX | IFF_POINTOPOINT)

	ifp->if_flags &= ~VLAN_INHERIT_FLAGS;
	ifp->if_flags |= (ifp_p->if_flags & VLAN_INHERIT_FLAGS);

#undef VLAN_INHERIT_FLAGS

	/*
	 * Set up our ``Ethernet address'' to reflect the underlying
	 * physical interface's.
	 */
	sdl1 = IF_LLSOCKADDR(ifp);
	sdl2 = IF_LLSOCKADDR(ifp_p);
	sdl1->sdl_type = IFT_ETHER;
	sdl1->sdl_alen = ETHER_ADDR_LEN;
	bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
	bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);

	/*
	 * Release vlan's serializer before reprogramming parent's
	 * multicast filter to avoid possible dead lock.
	 */
	ifnet_deserialize_all(ifp);

	/*
	 * Configure multicast addresses that may already be
	 * joined on the vlan device.
	 */
	vlan_setmulti(ifv, ifp_p);

	/*
	 * Set flags on the parent, if necessary.
	 */
	vlan_setflags(ifv, ifp_p, 1);

	/*
	 * Connect to parent after everything have been set up,
	 * so input/output could know that vlan is ready to go
	 */
	ifv->ifv_p = ifp_p;
	error = 0;
reply:
	lwkt_replymsg(&vmsg->base.lmsg, error);
}

static int
vlan_config(struct ifvlan *ifv, const char *parent_name, uint16_t vlantag)
{
	struct netmsg_vlan vmsg;

	ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);

	bzero(&vmsg, sizeof(vmsg));

	netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
		    0, vlan_config_dispatch);
	vmsg.nv_ifv = ifv;
	vmsg.nv_parent_name = parent_name;
	vmsg.nv_vlantag = vlantag;

	return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
}

static void
vlan_unlink_dispatch(netmsg_t msg)
{
	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
	struct ifvlan *ifv = vmsg->nv_ifv;
	struct vlan_entry *entry;
	int cpu = mycpuid;

	KASSERT(vmsg->nv_ifp_p->if_vlantrunks != NULL,
		("vlan trunk has not been initialized yet"));
	entry = &ifv->ifv_entries[cpu];

	crit_enter();
	LIST_REMOVE(entry, ifv_link);
	crit_exit();

	netisr_forwardmsg(&vmsg->base, cpu + 1);
}

static void
vlan_unlink(struct ifvlan *ifv, struct ifnet *ifp_p)
{
	struct vlan_trunk *vlantrunks = ifp_p->if_vlantrunks;
	struct netmsg_vlan vmsg;

	/* Assert in netisr0 */
	ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);

	KASSERT(ifp_p->if_vlantrunks != NULL,
		("vlan trunk has not been initialized yet"));

	bzero(&vmsg, sizeof(vmsg));

	netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
		    0, vlan_unlink_dispatch);
	vmsg.nv_ifv = ifv;
	vmsg.nv_ifp_p = ifp_p;

	netisr_domsg(&vmsg.base, 0);

	crit_enter();
	if (LIST_EMPTY(&vlantrunks[mycpuid].vlan_list)) {
		ifp_p->if_vlantrunks = NULL;

		/*
		 * Make sure that all protocol threads see if_vlantrunks change.
		 */
		netmsg_service_sync();
		kfree(vlantrunks, M_VLAN);
	}
	crit_exit();
}

static void
vlan_unconfig_dispatch(netmsg_t msg)
{
	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
	struct sockaddr_dl *sdl;
	struct ifvlan *ifv;
	struct ifnet *ifp_p, *ifp;
	int error;

	/* Assert in netisr0 */

	ifv = vmsg->nv_ifv;
	ifp = &ifv->ifv_if;

	if (ifp->if_flags & IFF_UP)
		if_down(ifp);

	ifnet_serialize_all(ifp);

	ifp->if_flags &= ~IFF_RUNNING;

	/*
	 * Save parent ifnet pointer and disconnect from parent.
	 *
	 * This is done early in this function, so input/output could
	 * know that we are disconnecting.
	 */
	ifp_p = ifv->ifv_p;
	ifv->ifv_p = NULL;

	/*
	 * Release vlan's serializer before reprogramming parent's
	 * multicast filter to avoid possible dead lock.
	 */
	ifnet_deserialize_all(ifp);

	if (ifp_p) {
		/*
		 * Since the interface is being unconfigured, we need to
		 * empty the list of multicast groups that we may have joined
		 * while we were alive from the parent's list.
		 */
		vlan_clrmulti(ifv, ifp_p);

		/* Clear parent's flags which was set by us. */
		vlan_setflags(ifv, ifp_p, 0);
	}

	ifnet_serialize_all(ifp);

	ifp->if_mtu = ETHERMTU;

	/* Clear our MAC address. */
	sdl = IF_LLSOCKADDR(ifp);
	sdl->sdl_type = IFT_ETHER;
	sdl->sdl_alen = ETHER_ADDR_LEN;
	bzero(LLADDR(sdl), ETHER_ADDR_LEN);
	bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);

	ifnet_deserialize_all(ifp);

	/* Unlink vlan from parent's vlantrunk */
	if (ifp_p != NULL && ifp_p->if_vlantrunks != NULL)
		vlan_unlink(ifv, ifp_p);

	error = 0;
	lwkt_replymsg(&vmsg->base.lmsg, error);
}

static int
vlan_unconfig(struct ifvlan *ifv)
{
	struct netmsg_vlan vmsg;

	ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);

	bzero(&vmsg, sizeof(vmsg));

	netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
		    0, vlan_unconfig_dispatch);
	vmsg.nv_ifv = ifv;

	return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
}

static int
vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
{
	struct ifvlan *ifv = ifp->if_softc;
	struct ifreq *ifr = (struct ifreq *)data;
	struct ifnet *ifp_p;
	struct vlanreq vlr;
	int error = 0;

	ASSERT_IFNET_SERIALIZED_ALL(ifp);

	switch (cmd) {
	case SIOCGIFMEDIA:
		ifp_p = ifv->ifv_p;
		if (ifp_p != NULL) {
			/*
			 * Release vlan interface's serializer to void
			 * possible dead lock.
			 */
			ifnet_deserialize_all(ifp);

			ifnet_serialize_all(ifp_p);
			error = ifp_p->if_ioctl(ifp_p, SIOCGIFMEDIA, data, cr);
			ifnet_deserialize_all(ifp_p);

			ifnet_serialize_all(ifp);

			if (ifv->ifv_p == NULL || ifv->ifv_p != ifp_p) {
				/*
				 * We are disconnected from the original
				 * parent interface or the parent interface
				 * is changed, after vlan interface's
				 * serializer is released.
				 */
				error = EINVAL;
			}

			/* Limit the result to the parent's current config. */
			if (error == 0) {
				struct ifmediareq *ifmr;

				ifmr = (struct ifmediareq *) data;
				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
					ifmr->ifm_count = 1;
					error = copyout(&ifmr->ifm_current,
						ifmr->ifm_ulist, 
						sizeof(int));
				}
			}
		} else {
			error = EINVAL;
		}
		break;

	case SIOCSIFMEDIA:
		error = EINVAL;
		break;

	case SIOCSETVLAN:
		error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
		if (error)
			break;

		ifnet_deserialize_all(ifp);
		if (vlr.vlr_parent[0] == '\0')
			error = vlan_unconfig(ifv);
		else
			error = vlan_config(ifv, vlr.vlr_parent, vlr.vlr_tag);
		ifnet_serialize_all(ifp);
		break;

	case SIOCGETVLAN:
		bzero(&vlr, sizeof(vlr));
		if (ifv->ifv_p) {
			strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
			    sizeof(vlr.vlr_parent));
			vlr.vlr_tag = ifv->ifv_tag;
		}
		error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
		break;

	case SIOCSIFFLAGS:
		if (ifp->if_flags & IFF_UP)
			ifp->if_init(ifp);
		else
			ifp->if_flags &= ~IFF_RUNNING;

		/*
		 * We should propagate selected flags to the parent,
		 * e.g., promiscuous mode.
		 */
		ifnet_deserialize_all(ifp);
		error = vlan_config_flags(ifv);
		ifnet_serialize_all(ifp);
		break;

	case SIOCADDMULTI:
	case SIOCDELMULTI:
		ifnet_deserialize_all(ifp);
		error = vlan_config_multi(ifv);
		ifnet_serialize_all(ifp);
		break;

	default:
		error = ether_ioctl(ifp, cmd, data);
		break;
	}
	return error;
}

static void
vlan_multi_dispatch(netmsg_t msg)
{
	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
	struct ifvlan *ifv = vmsg->nv_ifv;
	int error = 0;

	/*
	 * If we don't have a parent, just remember the membership for
	 * when we do.
	 */
	if (ifv->ifv_p != NULL)
		error = vlan_setmulti(ifv, ifv->ifv_p);
	lwkt_replymsg(&vmsg->base.lmsg, error);
}

static int
vlan_config_multi(struct ifvlan *ifv)
{
	struct netmsg_vlan vmsg;

	ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);

	bzero(&vmsg, sizeof(vmsg));

	netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
		    0, vlan_multi_dispatch);
	vmsg.nv_ifv = ifv;

	return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
}

static void
vlan_flags_dispatch(netmsg_t msg)
{
	struct netmsg_vlan *vmsg = (struct netmsg_vlan *)msg;
	struct ifvlan *ifv = vmsg->nv_ifv;
	int error = 0;

	/*
	 * If we don't have a parent, just remember the flags for
	 * when we do.
	 */
	if (ifv->ifv_p != NULL)
		error = vlan_setflags(ifv, ifv->ifv_p, 1);
	lwkt_replymsg(&vmsg->base.lmsg, error);
}

static int
vlan_config_flags(struct ifvlan *ifv)
{
	struct netmsg_vlan vmsg;

	ASSERT_IFNET_NOT_SERIALIZED_ALL(&ifv->ifv_if);

	bzero(&vmsg, sizeof(vmsg));

	netmsg_init(&vmsg.base, NULL, &curthread->td_msgport,
		    0, vlan_flags_dispatch);
	vmsg.nv_ifv = ifv;

	return lwkt_domsg(netisr_cpuport(0), &vmsg.base.lmsg, 0);
}