xref: /freebsd/sys/net/if_ethersubr.c (revision 595e514d0df2bac5b813d35f83e32875dbf16a83)
1 /*-
2  * Copyright (c) 1982, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
30  * $FreeBSD$
31  */
32 
33 #include "opt_atalk.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_ipx.h"
37 #include "opt_netgraph.h"
38 #include "opt_mbuf_profiling.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mbuf.h>
47 #include <sys/random.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/sysctl.h>
51 
52 #include <net/if.h>
53 #include <net/if_arp.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <net/if_llc.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/bpf.h>
60 #include <net/ethernet.h>
61 #include <net/if_bridgevar.h>
62 #include <net/if_vlan_var.h>
63 #include <net/if_llatbl.h>
64 #include <net/pf_mtag.h>
65 #include <net/pfil.h>
66 #include <net/vnet.h>
67 
68 #if defined(INET) || defined(INET6)
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/if_ether.h>
72 #include <netinet/ip_carp.h>
73 #include <netinet/ip_var.h>
74 #endif
75 #ifdef INET6
76 #include <netinet6/nd6.h>
77 #endif
78 
79 #ifdef IPX
80 #include <netipx/ipx.h>
81 #include <netipx/ipx_if.h>
82 #endif
83 
84 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
85 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
86 		const struct sockaddr *dst, short *tp, int *hlen);
87 
88 #ifdef NETATALK
89 #include <netatalk/at.h>
90 #include <netatalk/at_var.h>
91 #include <netatalk/at_extern.h>
92 
93 #define llc_snap_org_code llc_un.type_snap.org_code
94 #define llc_snap_ether_type llc_un.type_snap.ether_type
95 
96 extern u_char	at_org_code[3];
97 extern u_char	aarp_org_code[3];
98 #endif /* NETATALK */
99 
100 #include <security/mac/mac_framework.h>
101 
102 #ifdef CTASSERT
103 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
104 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
105 #endif
106 
107 VNET_DEFINE(struct pfil_head, link_pfil_hook);	/* Packet filter hooks */
108 
109 /* netgraph node hooks for ng_ether(4) */
110 void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
111 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
112 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
113 void	(*ng_ether_attach_p)(struct ifnet *ifp);
114 void	(*ng_ether_detach_p)(struct ifnet *ifp);
115 
116 void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
117 
118 /* if_bridge(4) support */
119 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
120 int	(*bridge_output_p)(struct ifnet *, struct mbuf *,
121 		struct sockaddr *, struct rtentry *);
122 void	(*bridge_dn_p)(struct mbuf *, struct ifnet *);
123 
124 /* if_lagg(4) support */
125 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
126 
127 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
128 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
129 
130 static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
131 		struct sockaddr *);
132 #ifdef VIMAGE
133 static	void ether_reassign(struct ifnet *, struct vnet *, char *);
134 #endif
135 
136 /* XXX: should be in an arp support file, not here */
137 static MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
138 
139 #define	ETHER_IS_BROADCAST(addr) \
140 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
141 
142 #define senderr(e) do { error = (e); goto bad;} while (0)
143 
144 static void
145 update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
146 {
147 	int csum_flags = 0;
148 
149 	if (src->m_pkthdr.csum_flags & CSUM_IP)
150 		csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
151 	if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
152 		csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
153 	if (src->m_pkthdr.csum_flags & CSUM_SCTP)
154 		csum_flags |= CSUM_SCTP_VALID;
155 	dst->m_pkthdr.csum_flags |= csum_flags;
156 	if (csum_flags & CSUM_DATA_VALID)
157 		dst->m_pkthdr.csum_data = 0xffff;
158 }
159 
160 /*
161  * Ethernet output routine.
162  * Encapsulate a packet of type family for the local net.
163  * Use trailer local net encapsulation if enough data in first
164  * packet leaves a multiple of 512 bytes of data in remainder.
165  */
166 int
167 ether_output(struct ifnet *ifp, struct mbuf *m,
168 	const struct sockaddr *dst, struct route *ro)
169 {
170 	short type;
171 	int error = 0, hdrcmplt = 0;
172 	u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
173 	struct llentry *lle = NULL;
174 	struct rtentry *rt0 = NULL;
175 	struct ether_header *eh;
176 	struct pf_mtag *t;
177 	int loop_copy = 1;
178 	int hlen;	/* link layer header length */
179 
180 	if (ro != NULL) {
181 		if (!(m->m_flags & (M_BCAST | M_MCAST)))
182 			lle = ro->ro_lle;
183 		rt0 = ro->ro_rt;
184 	}
185 #ifdef MAC
186 	error = mac_ifnet_check_transmit(ifp, m);
187 	if (error)
188 		senderr(error);
189 #endif
190 
191 	M_PROFILE(m);
192 	if (ifp->if_flags & IFF_MONITOR)
193 		senderr(ENETDOWN);
194 	if (!((ifp->if_flags & IFF_UP) &&
195 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
196 		senderr(ENETDOWN);
197 
198 	hlen = ETHER_HDR_LEN;
199 	switch (dst->sa_family) {
200 #ifdef INET
201 	case AF_INET:
202 		if (lle != NULL && (lle->la_flags & LLE_VALID))
203 			memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
204 		else
205 			error = arpresolve(ifp, rt0, m, dst, edst, &lle);
206 		if (error)
207 			return (error == EWOULDBLOCK ? 0 : error);
208 		type = htons(ETHERTYPE_IP);
209 		break;
210 	case AF_ARP:
211 	{
212 		struct arphdr *ah;
213 		ah = mtod(m, struct arphdr *);
214 		ah->ar_hrd = htons(ARPHRD_ETHER);
215 
216 		loop_copy = 0; /* if this is for us, don't do it */
217 
218 		switch(ntohs(ah->ar_op)) {
219 		case ARPOP_REVREQUEST:
220 		case ARPOP_REVREPLY:
221 			type = htons(ETHERTYPE_REVARP);
222 			break;
223 		case ARPOP_REQUEST:
224 		case ARPOP_REPLY:
225 		default:
226 			type = htons(ETHERTYPE_ARP);
227 			break;
228 		}
229 
230 		if (m->m_flags & M_BCAST)
231 			bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
232 		else
233 			bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
234 
235 	}
236 	break;
237 #endif
238 #ifdef INET6
239 	case AF_INET6:
240 		if (lle != NULL && (lle->la_flags & LLE_VALID))
241 			memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
242 		else
243 			error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
244 		if (error)
245 			return error;
246 		type = htons(ETHERTYPE_IPV6);
247 		break;
248 #endif
249 #ifdef IPX
250 	case AF_IPX:
251 		if (ef_outputp) {
252 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
253 		    if (error)
254 			goto bad;
255 		} else
256 		    type = htons(ETHERTYPE_IPX);
257 		bcopy(&((const struct sockaddr_ipx *)dst)->sipx_addr.x_host,
258 		    edst, sizeof (edst));
259 		break;
260 #endif
261 #ifdef NETATALK
262 	case AF_APPLETALK:
263 	  {
264 	    struct at_ifaddr *aa;
265 
266 	    if ((aa = at_ifawithnet((const struct sockaddr_at *)dst)) == NULL)
267 		    senderr(EHOSTUNREACH); /* XXX */
268 	    if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
269 		    ifa_free(&aa->aa_ifa);
270 		    return (0);
271 	    }
272 	    /*
273 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
274 	     */
275 	    if ( aa->aa_flags & AFA_PHASE2 ) {
276 		struct llc llc;
277 
278 		ifa_free(&aa->aa_ifa);
279 		M_PREPEND(m, LLC_SNAPFRAMELEN, M_NOWAIT);
280 		if (m == NULL)
281 			senderr(ENOBUFS);
282 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
283 		llc.llc_control = LLC_UI;
284 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
285 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
286 		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
287 		type = htons(m->m_pkthdr.len);
288 		hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
289 	    } else {
290 		ifa_free(&aa->aa_ifa);
291 		type = htons(ETHERTYPE_AT);
292 	    }
293 	    break;
294 	  }
295 #endif /* NETATALK */
296 
297 	case pseudo_AF_HDRCMPLT:
298 	    {
299 		const struct ether_header *eh;
300 
301 		hdrcmplt = 1;
302 		eh = (const struct ether_header *)dst->sa_data;
303 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
304 		/* FALLTHROUGH */
305 
306 	case AF_UNSPEC:
307 		loop_copy = 0; /* if this is for us, don't do it */
308 		eh = (const struct ether_header *)dst->sa_data;
309 		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
310 		type = eh->ether_type;
311 		break;
312             }
313 	default:
314 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
315 		senderr(EAFNOSUPPORT);
316 	}
317 
318 	if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
319 		update_mbuf_csumflags(m, m);
320 		return (if_simloop(ifp, m, dst->sa_family, 0));
321 	}
322 
323 	/*
324 	 * Add local net header.  If no space in first mbuf,
325 	 * allocate another.
326 	 */
327 	M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
328 	if (m == NULL)
329 		senderr(ENOBUFS);
330 	eh = mtod(m, struct ether_header *);
331 	(void)memcpy(&eh->ether_type, &type,
332 		sizeof(eh->ether_type));
333 	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
334 	if (hdrcmplt)
335 		(void)memcpy(eh->ether_shost, esrc,
336 			sizeof(eh->ether_shost));
337 	else
338 		(void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
339 			sizeof(eh->ether_shost));
340 
341 	/*
342 	 * If a simplex interface, and the packet is being sent to our
343 	 * Ethernet address or a broadcast address, loopback a copy.
344 	 * XXX To make a simplex device behave exactly like a duplex
345 	 * device, we should copy in the case of sending to our own
346 	 * ethernet address (thus letting the original actually appear
347 	 * on the wire). However, we don't do that here for security
348 	 * reasons and compatibility with the original behavior.
349 	 */
350 	if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
351 	    ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
352 		if (m->m_flags & M_BCAST) {
353 			struct mbuf *n;
354 
355 			/*
356 			 * Because if_simloop() modifies the packet, we need a
357 			 * writable copy through m_dup() instead of a readonly
358 			 * one as m_copy[m] would give us. The alternative would
359 			 * be to modify if_simloop() to handle the readonly mbuf,
360 			 * but performancewise it is mostly equivalent (trading
361 			 * extra data copying vs. extra locking).
362 			 *
363 			 * XXX This is a local workaround.  A number of less
364 			 * often used kernel parts suffer from the same bug.
365 			 * See PR kern/105943 for a proposed general solution.
366 			 */
367 			if ((n = m_dup(m, M_NOWAIT)) != NULL) {
368 				update_mbuf_csumflags(m, n);
369 				(void)if_simloop(ifp, n, dst->sa_family, hlen);
370 			} else
371 				ifp->if_iqdrops++;
372 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
373 				ETHER_ADDR_LEN) == 0) {
374 			update_mbuf_csumflags(m, m);
375 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
376 			return (0);	/* XXX */
377 		}
378 	}
379 
380        /*
381 	* Bridges require special output handling.
382 	*/
383 	if (ifp->if_bridge) {
384 		BRIDGE_OUTPUT(ifp, m, error);
385 		return (error);
386 	}
387 
388 #if defined(INET) || defined(INET6)
389 	if (ifp->if_carp &&
390 	    (error = (*carp_output_p)(ifp, m, dst)))
391 		goto bad;
392 #endif
393 
394 	/* Handle ng_ether(4) processing, if any */
395 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
396 		KASSERT(ng_ether_output_p != NULL,
397 		    ("ng_ether_output_p is NULL"));
398 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
399 bad:			if (m != NULL)
400 				m_freem(m);
401 			return (error);
402 		}
403 		if (m == NULL)
404 			return (0);
405 	}
406 
407 	/* Continue with link-layer output */
408 	return ether_output_frame(ifp, m);
409 }
410 
411 /*
412  * Ethernet link layer output routine to send a raw frame to the device.
413  *
414  * This assumes that the 14 byte Ethernet header is present and contiguous
415  * in the first mbuf (if BRIDGE'ing).
416  */
417 int
418 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
419 {
420 	int i;
421 
422 	if (PFIL_HOOKED(&V_link_pfil_hook)) {
423 		i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_OUT, NULL);
424 
425 		if (i != 0)
426 			return (EACCES);
427 
428 		if (m == NULL)
429 			return (0);
430 	}
431 
432 	/*
433 	 * Queue message on interface, update output statistics if
434 	 * successful, and start output if interface not yet active.
435 	 */
436 	return ((ifp->if_transmit)(ifp, m));
437 }
438 
439 #if defined(INET) || defined(INET6)
440 #endif
441 
442 /*
443  * Process a received Ethernet packet; the packet is in the
444  * mbuf chain m with the ethernet header at the front.
445  */
446 static void
447 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
448 {
449 	struct ether_header *eh;
450 	u_short etype;
451 
452 	if ((ifp->if_flags & IFF_UP) == 0) {
453 		m_freem(m);
454 		return;
455 	}
456 #ifdef DIAGNOSTIC
457 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
458 		if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
459 		m_freem(m);
460 		return;
461 	}
462 #endif
463 	/*
464 	 * Do consistency checks to verify assumptions
465 	 * made by code past this point.
466 	 */
467 	if ((m->m_flags & M_PKTHDR) == 0) {
468 		if_printf(ifp, "discard frame w/o packet header\n");
469 		ifp->if_ierrors++;
470 		m_freem(m);
471 		return;
472 	}
473 	if (m->m_len < ETHER_HDR_LEN) {
474 		/* XXX maybe should pullup? */
475 		if_printf(ifp, "discard frame w/o leading ethernet "
476 				"header (len %u pkt len %u)\n",
477 				m->m_len, m->m_pkthdr.len);
478 		ifp->if_ierrors++;
479 		m_freem(m);
480 		return;
481 	}
482 	eh = mtod(m, struct ether_header *);
483 	etype = ntohs(eh->ether_type);
484 	if (m->m_pkthdr.rcvif == NULL) {
485 		if_printf(ifp, "discard frame w/o interface pointer\n");
486 		ifp->if_ierrors++;
487 		m_freem(m);
488 		return;
489 	}
490 #ifdef DIAGNOSTIC
491 	if (m->m_pkthdr.rcvif != ifp) {
492 		if_printf(ifp, "Warning, frame marked as received on %s\n",
493 			m->m_pkthdr.rcvif->if_xname);
494 	}
495 #endif
496 
497 	CURVNET_SET_QUIET(ifp->if_vnet);
498 
499 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
500 		if (ETHER_IS_BROADCAST(eh->ether_dhost))
501 			m->m_flags |= M_BCAST;
502 		else
503 			m->m_flags |= M_MCAST;
504 		ifp->if_imcasts++;
505 	}
506 
507 #ifdef MAC
508 	/*
509 	 * Tag the mbuf with an appropriate MAC label before any other
510 	 * consumers can get to it.
511 	 */
512 	mac_ifnet_create_mbuf(ifp, m);
513 #endif
514 
515 	/*
516 	 * Give bpf a chance at the packet.
517 	 */
518 	ETHER_BPF_MTAP(ifp, m);
519 
520 	/*
521 	 * If the CRC is still on the packet, trim it off. We do this once
522 	 * and once only in case we are re-entered. Nothing else on the
523 	 * Ethernet receive path expects to see the FCS.
524 	 */
525 	if (m->m_flags & M_HASFCS) {
526 		m_adj(m, -ETHER_CRC_LEN);
527 		m->m_flags &= ~M_HASFCS;
528 	}
529 
530 	ifp->if_ibytes += m->m_pkthdr.len;
531 
532 	/* Allow monitor mode to claim this frame, after stats are updated. */
533 	if (ifp->if_flags & IFF_MONITOR) {
534 		m_freem(m);
535 		CURVNET_RESTORE();
536 		return;
537 	}
538 
539 	/* Handle input from a lagg(4) port */
540 	if (ifp->if_type == IFT_IEEE8023ADLAG) {
541 		KASSERT(lagg_input_p != NULL,
542 		    ("%s: if_lagg not loaded!", __func__));
543 		m = (*lagg_input_p)(ifp, m);
544 		if (m != NULL)
545 			ifp = m->m_pkthdr.rcvif;
546 		else {
547 			CURVNET_RESTORE();
548 			return;
549 		}
550 	}
551 
552 	/*
553 	 * If the hardware did not process an 802.1Q tag, do this now,
554 	 * to allow 802.1P priority frames to be passed to the main input
555 	 * path correctly.
556 	 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
557 	 */
558 	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
559 		struct ether_vlan_header *evl;
560 
561 		if (m->m_len < sizeof(*evl) &&
562 		    (m = m_pullup(m, sizeof(*evl))) == NULL) {
563 #ifdef DIAGNOSTIC
564 			if_printf(ifp, "cannot pullup VLAN header\n");
565 #endif
566 			ifp->if_ierrors++;
567 			m_freem(m);
568 			CURVNET_RESTORE();
569 			return;
570 		}
571 
572 		evl = mtod(m, struct ether_vlan_header *);
573 		m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
574 		m->m_flags |= M_VLANTAG;
575 
576 		bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
577 		    ETHER_HDR_LEN - ETHER_TYPE_LEN);
578 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
579 		eh = mtod(m, struct ether_header *);
580 	}
581 
582 	M_SETFIB(m, ifp->if_fib);
583 
584 	/* Allow ng_ether(4) to claim this frame. */
585 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
586 		KASSERT(ng_ether_input_p != NULL,
587 		    ("%s: ng_ether_input_p is NULL", __func__));
588 		m->m_flags &= ~M_PROMISC;
589 		(*ng_ether_input_p)(ifp, &m);
590 		if (m == NULL) {
591 			CURVNET_RESTORE();
592 			return;
593 		}
594 		eh = mtod(m, struct ether_header *);
595 	}
596 
597 	/*
598 	 * Allow if_bridge(4) to claim this frame.
599 	 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
600 	 * and the frame should be delivered locally.
601 	 */
602 	if (ifp->if_bridge != NULL) {
603 		m->m_flags &= ~M_PROMISC;
604 		BRIDGE_INPUT(ifp, m);
605 		if (m == NULL) {
606 			CURVNET_RESTORE();
607 			return;
608 		}
609 		eh = mtod(m, struct ether_header *);
610 	}
611 
612 #if defined(INET) || defined(INET6)
613 	/*
614 	 * Clear M_PROMISC on frame so that carp(4) will see it when the
615 	 * mbuf flows up to Layer 3.
616 	 * FreeBSD's implementation of carp(4) uses the inprotosw
617 	 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
618 	 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
619 	 * is outside the scope of the M_PROMISC test below.
620 	 * TODO: Maintain a hash table of ethernet addresses other than
621 	 * ether_dhost which may be active on this ifp.
622 	 */
623 	if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
624 		m->m_flags &= ~M_PROMISC;
625 	} else
626 #endif
627 	{
628 		/*
629 		 * If the frame received was not for our MAC address, set the
630 		 * M_PROMISC flag on the mbuf chain. The frame may need to
631 		 * be seen by the rest of the Ethernet input path in case of
632 		 * re-entry (e.g. bridge, vlan, netgraph) but should not be
633 		 * seen by upper protocol layers.
634 		 */
635 		if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
636 		    bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
637 			m->m_flags |= M_PROMISC;
638 	}
639 
640 	/* First chunk of an mbuf contains good entropy */
641 	if (harvest.ethernet)
642 		random_harvest(m, 16, 3, 0, RANDOM_NET);
643 
644 	ether_demux(ifp, m);
645 	CURVNET_RESTORE();
646 }
647 
648 /*
649  * Ethernet input dispatch; by default, direct dispatch here regardless of
650  * global configuration.
651  */
652 static void
653 ether_nh_input(struct mbuf *m)
654 {
655 
656 	ether_input_internal(m->m_pkthdr.rcvif, m);
657 }
658 
659 static struct netisr_handler	ether_nh = {
660 	.nh_name = "ether",
661 	.nh_handler = ether_nh_input,
662 	.nh_proto = NETISR_ETHER,
663 	.nh_policy = NETISR_POLICY_SOURCE,
664 	.nh_dispatch = NETISR_DISPATCH_DIRECT,
665 };
666 
667 static void
668 ether_init(__unused void *arg)
669 {
670 
671 	netisr_register(&ether_nh);
672 }
673 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
674 
675 static void
676 vnet_ether_init(__unused void *arg)
677 {
678 	int i;
679 
680 	/* Initialize packet filter hooks. */
681 	V_link_pfil_hook.ph_type = PFIL_TYPE_AF;
682 	V_link_pfil_hook.ph_af = AF_LINK;
683 	if ((i = pfil_head_register(&V_link_pfil_hook)) != 0)
684 		printf("%s: WARNING: unable to register pfil link hook, "
685 			"error %d\n", __func__, i);
686 }
687 VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
688     vnet_ether_init, NULL);
689 
690 static void
691 vnet_ether_destroy(__unused void *arg)
692 {
693 	int i;
694 
695 	if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0)
696 		printf("%s: WARNING: unable to unregister pfil link hook, "
697 			"error %d\n", __func__, i);
698 }
699 VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
700     vnet_ether_destroy, NULL);
701 
702 
703 
704 static void
705 ether_input(struct ifnet *ifp, struct mbuf *m)
706 {
707 
708 	/*
709 	 * We will rely on rcvif being set properly in the deferred context,
710 	 * so assert it is correct here.
711 	 */
712 	KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
713 
714 	netisr_dispatch(NETISR_ETHER, m);
715 }
716 
717 /*
718  * Upper layer processing for a received Ethernet packet.
719  */
720 void
721 ether_demux(struct ifnet *ifp, struct mbuf *m)
722 {
723 	struct ether_header *eh;
724 	int i, isr;
725 	u_short ether_type;
726 #if defined(NETATALK)
727 	struct llc *l;
728 #endif
729 
730 	KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
731 
732 	/* Do not grab PROMISC frames in case we are re-entered. */
733 	if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
734 		i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL);
735 
736 		if (i != 0 || m == NULL)
737 			return;
738 	}
739 
740 	eh = mtod(m, struct ether_header *);
741 	ether_type = ntohs(eh->ether_type);
742 
743 	/*
744 	 * If this frame has a VLAN tag other than 0, call vlan_input()
745 	 * if its module is loaded. Otherwise, drop.
746 	 */
747 	if ((m->m_flags & M_VLANTAG) &&
748 	    EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
749 		if (ifp->if_vlantrunk == NULL) {
750 			ifp->if_noproto++;
751 			m_freem(m);
752 			return;
753 		}
754 		KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
755 		    __func__));
756 		/* Clear before possibly re-entering ether_input(). */
757 		m->m_flags &= ~M_PROMISC;
758 		(*vlan_input_p)(ifp, m);
759 		return;
760 	}
761 
762 	/*
763 	 * Pass promiscuously received frames to the upper layer if the user
764 	 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
765 	 */
766 	if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
767 		m_freem(m);
768 		return;
769 	}
770 
771 	/*
772 	 * Reset layer specific mbuf flags to avoid confusing upper layers.
773 	 * Strip off Ethernet header.
774 	 */
775 	m->m_flags &= ~M_VLANTAG;
776 	m->m_flags &= ~(M_PROTOFLAGS);
777 	m_adj(m, ETHER_HDR_LEN);
778 
779 	/*
780 	 * Dispatch frame to upper layer.
781 	 */
782 	switch (ether_type) {
783 #ifdef INET
784 	case ETHERTYPE_IP:
785 		if ((m = ip_fastforward(m)) == NULL)
786 			return;
787 		isr = NETISR_IP;
788 		break;
789 
790 	case ETHERTYPE_ARP:
791 		if (ifp->if_flags & IFF_NOARP) {
792 			/* Discard packet if ARP is disabled on interface */
793 			m_freem(m);
794 			return;
795 		}
796 		isr = NETISR_ARP;
797 		break;
798 #endif
799 #ifdef IPX
800 	case ETHERTYPE_IPX:
801 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
802 			return;
803 		isr = NETISR_IPX;
804 		break;
805 #endif
806 #ifdef INET6
807 	case ETHERTYPE_IPV6:
808 		isr = NETISR_IPV6;
809 		break;
810 #endif
811 #ifdef NETATALK
812 	case ETHERTYPE_AT:
813 		isr = NETISR_ATALK1;
814 		break;
815 	case ETHERTYPE_AARP:
816 		isr = NETISR_AARP;
817 		break;
818 #endif /* NETATALK */
819 	default:
820 #ifdef IPX
821 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
822 			return;
823 #endif /* IPX */
824 #if defined(NETATALK)
825 		if (ether_type > ETHERMTU)
826 			goto discard;
827 		l = mtod(m, struct llc *);
828 		if (l->llc_dsap == LLC_SNAP_LSAP &&
829 		    l->llc_ssap == LLC_SNAP_LSAP &&
830 		    l->llc_control == LLC_UI) {
831 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
832 			    sizeof(at_org_code)) == 0 &&
833 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
834 				m_adj(m, LLC_SNAPFRAMELEN);
835 				isr = NETISR_ATALK2;
836 				break;
837 			}
838 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
839 			    sizeof(aarp_org_code)) == 0 &&
840 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
841 				m_adj(m, LLC_SNAPFRAMELEN);
842 				isr = NETISR_AARP;
843 				break;
844 			}
845 		}
846 #endif /* NETATALK */
847 		goto discard;
848 	}
849 	netisr_dispatch(isr, m);
850 	return;
851 
852 discard:
853 	/*
854 	 * Packet is to be discarded.  If netgraph is present,
855 	 * hand the packet to it for last chance processing;
856 	 * otherwise dispose of it.
857 	 */
858 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
859 		KASSERT(ng_ether_input_orphan_p != NULL,
860 		    ("ng_ether_input_orphan_p is NULL"));
861 		/*
862 		 * Put back the ethernet header so netgraph has a
863 		 * consistent view of inbound packets.
864 		 */
865 		M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
866 		(*ng_ether_input_orphan_p)(ifp, m);
867 		return;
868 	}
869 	m_freem(m);
870 }
871 
872 /*
873  * Convert Ethernet address to printable (loggable) representation.
874  * This routine is for compatibility; it's better to just use
875  *
876  *	printf("%6D", <pointer to address>, ":");
877  *
878  * since there's no static buffer involved.
879  */
880 char *
881 ether_sprintf(const u_char *ap)
882 {
883 	static char etherbuf[18];
884 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
885 	return (etherbuf);
886 }
887 
888 /*
889  * Perform common duties while attaching to interface list
890  */
891 void
892 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
893 {
894 	int i;
895 	struct ifaddr *ifa;
896 	struct sockaddr_dl *sdl;
897 
898 	ifp->if_addrlen = ETHER_ADDR_LEN;
899 	ifp->if_hdrlen = ETHER_HDR_LEN;
900 	if_attach(ifp);
901 	ifp->if_mtu = ETHERMTU;
902 	ifp->if_output = ether_output;
903 	ifp->if_input = ether_input;
904 	ifp->if_resolvemulti = ether_resolvemulti;
905 #ifdef VIMAGE
906 	ifp->if_reassign = ether_reassign;
907 #endif
908 	if (ifp->if_baudrate == 0)
909 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
910 	ifp->if_broadcastaddr = etherbroadcastaddr;
911 
912 	ifa = ifp->if_addr;
913 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
914 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
915 	sdl->sdl_type = IFT_ETHER;
916 	sdl->sdl_alen = ifp->if_addrlen;
917 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
918 
919 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
920 	if (ng_ether_attach_p != NULL)
921 		(*ng_ether_attach_p)(ifp);
922 
923 	/* Announce Ethernet MAC address if non-zero. */
924 	for (i = 0; i < ifp->if_addrlen; i++)
925 		if (lla[i] != 0)
926 			break;
927 	if (i != ifp->if_addrlen)
928 		if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
929 }
930 
931 /*
932  * Perform common duties while detaching an Ethernet interface
933  */
934 void
935 ether_ifdetach(struct ifnet *ifp)
936 {
937 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
938 		KASSERT(ng_ether_detach_p != NULL,
939 		    ("ng_ether_detach_p is NULL"));
940 		(*ng_ether_detach_p)(ifp);
941 	}
942 
943 	bpfdetach(ifp);
944 	if_detach(ifp);
945 }
946 
947 #ifdef VIMAGE
948 void
949 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
950 {
951 
952 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
953 		KASSERT(ng_ether_detach_p != NULL,
954 		    ("ng_ether_detach_p is NULL"));
955 		(*ng_ether_detach_p)(ifp);
956 	}
957 
958 	if (ng_ether_attach_p != NULL) {
959 		CURVNET_SET_QUIET(new_vnet);
960 		(*ng_ether_attach_p)(ifp);
961 		CURVNET_RESTORE();
962 	}
963 }
964 #endif
965 
966 SYSCTL_DECL(_net_link);
967 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
968 
969 #if 0
970 /*
971  * This is for reference.  We have a table-driven version
972  * of the little-endian crc32 generator, which is faster
973  * than the double-loop.
974  */
975 uint32_t
976 ether_crc32_le(const uint8_t *buf, size_t len)
977 {
978 	size_t i;
979 	uint32_t crc;
980 	int bit;
981 	uint8_t data;
982 
983 	crc = 0xffffffff;	/* initial value */
984 
985 	for (i = 0; i < len; i++) {
986 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
987 			carry = (crc ^ data) & 1;
988 			crc >>= 1;
989 			if (carry)
990 				crc = (crc ^ ETHER_CRC_POLY_LE);
991 		}
992 	}
993 
994 	return (crc);
995 }
996 #else
997 uint32_t
998 ether_crc32_le(const uint8_t *buf, size_t len)
999 {
1000 	static const uint32_t crctab[] = {
1001 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1002 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1003 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1004 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1005 	};
1006 	size_t i;
1007 	uint32_t crc;
1008 
1009 	crc = 0xffffffff;	/* initial value */
1010 
1011 	for (i = 0; i < len; i++) {
1012 		crc ^= buf[i];
1013 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1014 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1015 	}
1016 
1017 	return (crc);
1018 }
1019 #endif
1020 
1021 uint32_t
1022 ether_crc32_be(const uint8_t *buf, size_t len)
1023 {
1024 	size_t i;
1025 	uint32_t crc, carry;
1026 	int bit;
1027 	uint8_t data;
1028 
1029 	crc = 0xffffffff;	/* initial value */
1030 
1031 	for (i = 0; i < len; i++) {
1032 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1033 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1034 			crc <<= 1;
1035 			if (carry)
1036 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1037 		}
1038 	}
1039 
1040 	return (crc);
1041 }
1042 
1043 int
1044 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1045 {
1046 	struct ifaddr *ifa = (struct ifaddr *) data;
1047 	struct ifreq *ifr = (struct ifreq *) data;
1048 	int error = 0;
1049 
1050 	switch (command) {
1051 	case SIOCSIFADDR:
1052 		ifp->if_flags |= IFF_UP;
1053 
1054 		switch (ifa->ifa_addr->sa_family) {
1055 #ifdef INET
1056 		case AF_INET:
1057 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
1058 			arp_ifinit(ifp, ifa);
1059 			break;
1060 #endif
1061 #ifdef IPX
1062 		/*
1063 		 * XXX - This code is probably wrong
1064 		 */
1065 		case AF_IPX:
1066 			{
1067 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1068 
1069 			if (ipx_nullhost(*ina))
1070 				ina->x_host =
1071 				    *(union ipx_host *)
1072 				    IF_LLADDR(ifp);
1073 			else {
1074 				bcopy((caddr_t) ina->x_host.c_host,
1075 				      (caddr_t) IF_LLADDR(ifp),
1076 				      ETHER_ADDR_LEN);
1077 			}
1078 
1079 			/*
1080 			 * Set new address
1081 			 */
1082 			ifp->if_init(ifp->if_softc);
1083 			break;
1084 			}
1085 #endif
1086 		default:
1087 			ifp->if_init(ifp->if_softc);
1088 			break;
1089 		}
1090 		break;
1091 
1092 	case SIOCGIFADDR:
1093 		{
1094 			struct sockaddr *sa;
1095 
1096 			sa = (struct sockaddr *) & ifr->ifr_data;
1097 			bcopy(IF_LLADDR(ifp),
1098 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1099 		}
1100 		break;
1101 
1102 	case SIOCSIFMTU:
1103 		/*
1104 		 * Set the interface MTU.
1105 		 */
1106 		if (ifr->ifr_mtu > ETHERMTU) {
1107 			error = EINVAL;
1108 		} else {
1109 			ifp->if_mtu = ifr->ifr_mtu;
1110 		}
1111 		break;
1112 	default:
1113 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1114 		break;
1115 	}
1116 	return (error);
1117 }
1118 
1119 static int
1120 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1121 	struct sockaddr *sa)
1122 {
1123 	struct sockaddr_dl *sdl;
1124 #ifdef INET
1125 	struct sockaddr_in *sin;
1126 #endif
1127 #ifdef INET6
1128 	struct sockaddr_in6 *sin6;
1129 #endif
1130 	u_char *e_addr;
1131 
1132 	switch(sa->sa_family) {
1133 	case AF_LINK:
1134 		/*
1135 		 * No mapping needed. Just check that it's a valid MC address.
1136 		 */
1137 		sdl = (struct sockaddr_dl *)sa;
1138 		e_addr = LLADDR(sdl);
1139 		if (!ETHER_IS_MULTICAST(e_addr))
1140 			return EADDRNOTAVAIL;
1141 		*llsa = 0;
1142 		return 0;
1143 
1144 #ifdef INET
1145 	case AF_INET:
1146 		sin = (struct sockaddr_in *)sa;
1147 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1148 			return EADDRNOTAVAIL;
1149 		sdl = malloc(sizeof *sdl, M_IFMADDR,
1150 		       M_NOWAIT|M_ZERO);
1151 		if (sdl == NULL)
1152 			return ENOMEM;
1153 		sdl->sdl_len = sizeof *sdl;
1154 		sdl->sdl_family = AF_LINK;
1155 		sdl->sdl_index = ifp->if_index;
1156 		sdl->sdl_type = IFT_ETHER;
1157 		sdl->sdl_alen = ETHER_ADDR_LEN;
1158 		e_addr = LLADDR(sdl);
1159 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1160 		*llsa = (struct sockaddr *)sdl;
1161 		return 0;
1162 #endif
1163 #ifdef INET6
1164 	case AF_INET6:
1165 		sin6 = (struct sockaddr_in6 *)sa;
1166 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1167 			/*
1168 			 * An IP6 address of 0 means listen to all
1169 			 * of the Ethernet multicast address used for IP6.
1170 			 * (This is used for multicast routers.)
1171 			 */
1172 			ifp->if_flags |= IFF_ALLMULTI;
1173 			*llsa = 0;
1174 			return 0;
1175 		}
1176 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1177 			return EADDRNOTAVAIL;
1178 		sdl = malloc(sizeof *sdl, M_IFMADDR,
1179 		       M_NOWAIT|M_ZERO);
1180 		if (sdl == NULL)
1181 			return (ENOMEM);
1182 		sdl->sdl_len = sizeof *sdl;
1183 		sdl->sdl_family = AF_LINK;
1184 		sdl->sdl_index = ifp->if_index;
1185 		sdl->sdl_type = IFT_ETHER;
1186 		sdl->sdl_alen = ETHER_ADDR_LEN;
1187 		e_addr = LLADDR(sdl);
1188 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1189 		*llsa = (struct sockaddr *)sdl;
1190 		return 0;
1191 #endif
1192 
1193 	default:
1194 		/*
1195 		 * Well, the text isn't quite right, but it's the name
1196 		 * that counts...
1197 		 */
1198 		return EAFNOSUPPORT;
1199 	}
1200 }
1201 
1202 static void*
1203 ether_alloc(u_char type, struct ifnet *ifp)
1204 {
1205 	struct arpcom	*ac;
1206 
1207 	ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1208 	ac->ac_ifp = ifp;
1209 
1210 	return (ac);
1211 }
1212 
1213 static void
1214 ether_free(void *com, u_char type)
1215 {
1216 
1217 	free(com, M_ARPCOM);
1218 }
1219 
1220 static int
1221 ether_modevent(module_t mod, int type, void *data)
1222 {
1223 
1224 	switch (type) {
1225 	case MOD_LOAD:
1226 		if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1227 		break;
1228 	case MOD_UNLOAD:
1229 		if_deregister_com_alloc(IFT_ETHER);
1230 		break;
1231 	default:
1232 		return EOPNOTSUPP;
1233 	}
1234 
1235 	return (0);
1236 }
1237 
1238 static moduledata_t ether_mod = {
1239 	"ether",
1240 	ether_modevent,
1241 	0
1242 };
1243 
1244 void
1245 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1246 {
1247 	struct ether_vlan_header vlan;
1248 	struct mbuf mv, mb;
1249 
1250 	KASSERT((m->m_flags & M_VLANTAG) != 0,
1251 	    ("%s: vlan information not present", __func__));
1252 	KASSERT(m->m_len >= sizeof(struct ether_header),
1253 	    ("%s: mbuf not large enough for header", __func__));
1254 	bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1255 	vlan.evl_proto = vlan.evl_encap_proto;
1256 	vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1257 	vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1258 	m->m_len -= sizeof(struct ether_header);
1259 	m->m_data += sizeof(struct ether_header);
1260 	/*
1261 	 * If a data link has been supplied by the caller, then we will need to
1262 	 * re-create a stack allocated mbuf chain with the following structure:
1263 	 *
1264 	 * (1) mbuf #1 will contain the supplied data link
1265 	 * (2) mbuf #2 will contain the vlan header
1266 	 * (3) mbuf #3 will contain the original mbuf's packet data
1267 	 *
1268 	 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1269 	 */
1270 	if (data != NULL) {
1271 		mv.m_next = m;
1272 		mv.m_data = (caddr_t)&vlan;
1273 		mv.m_len = sizeof(vlan);
1274 		mb.m_next = &mv;
1275 		mb.m_data = data;
1276 		mb.m_len = dlen;
1277 		bpf_mtap(bp, &mb);
1278 	} else
1279 		bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1280 	m->m_len += sizeof(struct ether_header);
1281 	m->m_data -= sizeof(struct ether_header);
1282 }
1283 
1284 struct mbuf *
1285 ether_vlanencap(struct mbuf *m, uint16_t tag)
1286 {
1287 	struct ether_vlan_header *evl;
1288 
1289 	M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1290 	if (m == NULL)
1291 		return (NULL);
1292 	/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1293 
1294 	if (m->m_len < sizeof(*evl)) {
1295 		m = m_pullup(m, sizeof(*evl));
1296 		if (m == NULL)
1297 			return (NULL);
1298 	}
1299 
1300 	/*
1301 	 * Transform the Ethernet header into an Ethernet header
1302 	 * with 802.1Q encapsulation.
1303 	 */
1304 	evl = mtod(m, struct ether_vlan_header *);
1305 	bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1306 	    (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1307 	evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1308 	evl->evl_tag = htons(tag);
1309 	return (m);
1310 }
1311 
1312 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1313 MODULE_VERSION(ether, 1);
1314