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