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