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