xref: /freebsd/sys/net/if_ethersubr.c (revision 1c6d60de932c8553af44629218cb9697bc0f2ef1)
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 	/*
712 	 * We will rely on rcvif being set properly in the deferred context,
713 	 * so assert it is correct here.
714 	 */
715 	KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
716 
717 	netisr_dispatch(NETISR_ETHER, m);
718 }
719 
720 /*
721  * Upper layer processing for a received Ethernet packet.
722  */
723 void
724 ether_demux(struct ifnet *ifp, struct mbuf *m)
725 {
726 	struct ether_header *eh;
727 	int i, isr;
728 	u_short ether_type;
729 #if defined(NETATALK)
730 	struct llc *l;
731 #endif
732 
733 	KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
734 
735 	/* Do not grab PROMISC frames in case we are re-entered. */
736 	if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
737 		i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL);
738 
739 		if (i != 0 || m == NULL)
740 			return;
741 	}
742 
743 	eh = mtod(m, struct ether_header *);
744 	ether_type = ntohs(eh->ether_type);
745 
746 	/*
747 	 * If this frame has a VLAN tag other than 0, call vlan_input()
748 	 * if its module is loaded. Otherwise, drop.
749 	 */
750 	if ((m->m_flags & M_VLANTAG) &&
751 	    EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
752 		if (ifp->if_vlantrunk == NULL) {
753 			ifp->if_noproto++;
754 			m_freem(m);
755 			return;
756 		}
757 		KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
758 		    __func__));
759 		/* Clear before possibly re-entering ether_input(). */
760 		m->m_flags &= ~M_PROMISC;
761 		(*vlan_input_p)(ifp, m);
762 		return;
763 	}
764 
765 	/*
766 	 * Pass promiscuously received frames to the upper layer if the user
767 	 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
768 	 */
769 	if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
770 		m_freem(m);
771 		return;
772 	}
773 
774 	/*
775 	 * Reset layer specific mbuf flags to avoid confusing upper layers.
776 	 * Strip off Ethernet header.
777 	 */
778 	m->m_flags &= ~M_VLANTAG;
779 	m_clrprotoflags(m);
780 	m_adj(m, ETHER_HDR_LEN);
781 
782 	/*
783 	 * Dispatch frame to upper layer.
784 	 */
785 	switch (ether_type) {
786 #ifdef INET
787 	case ETHERTYPE_IP:
788 		if ((m = ip_fastforward(m)) == NULL)
789 			return;
790 		isr = NETISR_IP;
791 		break;
792 
793 	case ETHERTYPE_ARP:
794 		if (ifp->if_flags & IFF_NOARP) {
795 			/* Discard packet if ARP is disabled on interface */
796 			m_freem(m);
797 			return;
798 		}
799 		isr = NETISR_ARP;
800 		break;
801 #endif
802 #ifdef IPX
803 	case ETHERTYPE_IPX:
804 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
805 			return;
806 		isr = NETISR_IPX;
807 		break;
808 #endif
809 #ifdef INET6
810 	case ETHERTYPE_IPV6:
811 		isr = NETISR_IPV6;
812 		break;
813 #endif
814 #ifdef NETATALK
815 	case ETHERTYPE_AT:
816 		isr = NETISR_ATALK1;
817 		break;
818 	case ETHERTYPE_AARP:
819 		isr = NETISR_AARP;
820 		break;
821 #endif /* NETATALK */
822 	default:
823 #ifdef IPX
824 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
825 			return;
826 #endif /* IPX */
827 #if defined(NETATALK)
828 		if (ether_type > ETHERMTU)
829 			goto discard;
830 		l = mtod(m, struct llc *);
831 		if (l->llc_dsap == LLC_SNAP_LSAP &&
832 		    l->llc_ssap == LLC_SNAP_LSAP &&
833 		    l->llc_control == LLC_UI) {
834 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
835 			    sizeof(at_org_code)) == 0 &&
836 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
837 				m_adj(m, LLC_SNAPFRAMELEN);
838 				isr = NETISR_ATALK2;
839 				break;
840 			}
841 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
842 			    sizeof(aarp_org_code)) == 0 &&
843 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
844 				m_adj(m, LLC_SNAPFRAMELEN);
845 				isr = NETISR_AARP;
846 				break;
847 			}
848 		}
849 #endif /* NETATALK */
850 		goto discard;
851 	}
852 	netisr_dispatch(isr, m);
853 	return;
854 
855 discard:
856 	/*
857 	 * Packet is to be discarded.  If netgraph is present,
858 	 * hand the packet to it for last chance processing;
859 	 * otherwise dispose of it.
860 	 */
861 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
862 		KASSERT(ng_ether_input_orphan_p != NULL,
863 		    ("ng_ether_input_orphan_p is NULL"));
864 		/*
865 		 * Put back the ethernet header so netgraph has a
866 		 * consistent view of inbound packets.
867 		 */
868 		M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
869 		(*ng_ether_input_orphan_p)(ifp, m);
870 		return;
871 	}
872 	m_freem(m);
873 }
874 
875 /*
876  * Convert Ethernet address to printable (loggable) representation.
877  * This routine is for compatibility; it's better to just use
878  *
879  *	printf("%6D", <pointer to address>, ":");
880  *
881  * since there's no static buffer involved.
882  */
883 char *
884 ether_sprintf(const u_char *ap)
885 {
886 	static char etherbuf[18];
887 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
888 	return (etherbuf);
889 }
890 
891 /*
892  * Perform common duties while attaching to interface list
893  */
894 void
895 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
896 {
897 	int i;
898 	struct ifaddr *ifa;
899 	struct sockaddr_dl *sdl;
900 
901 	ifp->if_addrlen = ETHER_ADDR_LEN;
902 	ifp->if_hdrlen = ETHER_HDR_LEN;
903 	if_attach(ifp);
904 	ifp->if_mtu = ETHERMTU;
905 	ifp->if_output = ether_output;
906 	ifp->if_input = ether_input;
907 	ifp->if_resolvemulti = ether_resolvemulti;
908 #ifdef VIMAGE
909 	ifp->if_reassign = ether_reassign;
910 #endif
911 	if (ifp->if_baudrate == 0)
912 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
913 	ifp->if_broadcastaddr = etherbroadcastaddr;
914 
915 	ifa = ifp->if_addr;
916 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
917 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
918 	sdl->sdl_type = IFT_ETHER;
919 	sdl->sdl_alen = ifp->if_addrlen;
920 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
921 
922 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
923 	if (ng_ether_attach_p != NULL)
924 		(*ng_ether_attach_p)(ifp);
925 
926 	/* Announce Ethernet MAC address if non-zero. */
927 	for (i = 0; i < ifp->if_addrlen; i++)
928 		if (lla[i] != 0)
929 			break;
930 	if (i != ifp->if_addrlen)
931 		if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
932 
933 	uuid_ether_add(LLADDR(sdl));
934 }
935 
936 /*
937  * Perform common duties while detaching an Ethernet interface
938  */
939 void
940 ether_ifdetach(struct ifnet *ifp)
941 {
942 	struct sockaddr_dl *sdl;
943 
944 	sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
945 	uuid_ether_del(LLADDR(sdl));
946 
947 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
948 		KASSERT(ng_ether_detach_p != NULL,
949 		    ("ng_ether_detach_p is NULL"));
950 		(*ng_ether_detach_p)(ifp);
951 	}
952 
953 	bpfdetach(ifp);
954 	if_detach(ifp);
955 }
956 
957 #ifdef VIMAGE
958 void
959 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
960 {
961 
962 	if (IFP2AC(ifp)->ac_netgraph != NULL) {
963 		KASSERT(ng_ether_detach_p != NULL,
964 		    ("ng_ether_detach_p is NULL"));
965 		(*ng_ether_detach_p)(ifp);
966 	}
967 
968 	if (ng_ether_attach_p != NULL) {
969 		CURVNET_SET_QUIET(new_vnet);
970 		(*ng_ether_attach_p)(ifp);
971 		CURVNET_RESTORE();
972 	}
973 }
974 #endif
975 
976 SYSCTL_DECL(_net_link);
977 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
978 
979 #if 0
980 /*
981  * This is for reference.  We have a table-driven version
982  * of the little-endian crc32 generator, which is faster
983  * than the double-loop.
984  */
985 uint32_t
986 ether_crc32_le(const uint8_t *buf, size_t len)
987 {
988 	size_t i;
989 	uint32_t crc;
990 	int bit;
991 	uint8_t data;
992 
993 	crc = 0xffffffff;	/* initial value */
994 
995 	for (i = 0; i < len; i++) {
996 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
997 			carry = (crc ^ data) & 1;
998 			crc >>= 1;
999 			if (carry)
1000 				crc = (crc ^ ETHER_CRC_POLY_LE);
1001 		}
1002 	}
1003 
1004 	return (crc);
1005 }
1006 #else
1007 uint32_t
1008 ether_crc32_le(const uint8_t *buf, size_t len)
1009 {
1010 	static const uint32_t crctab[] = {
1011 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1012 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1013 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1014 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1015 	};
1016 	size_t i;
1017 	uint32_t crc;
1018 
1019 	crc = 0xffffffff;	/* initial value */
1020 
1021 	for (i = 0; i < len; i++) {
1022 		crc ^= buf[i];
1023 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1024 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1025 	}
1026 
1027 	return (crc);
1028 }
1029 #endif
1030 
1031 uint32_t
1032 ether_crc32_be(const uint8_t *buf, size_t len)
1033 {
1034 	size_t i;
1035 	uint32_t crc, carry;
1036 	int bit;
1037 	uint8_t data;
1038 
1039 	crc = 0xffffffff;	/* initial value */
1040 
1041 	for (i = 0; i < len; i++) {
1042 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1043 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1044 			crc <<= 1;
1045 			if (carry)
1046 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1047 		}
1048 	}
1049 
1050 	return (crc);
1051 }
1052 
1053 int
1054 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1055 {
1056 	struct ifaddr *ifa = (struct ifaddr *) data;
1057 	struct ifreq *ifr = (struct ifreq *) data;
1058 	int error = 0;
1059 
1060 	switch (command) {
1061 	case SIOCSIFADDR:
1062 		ifp->if_flags |= IFF_UP;
1063 
1064 		switch (ifa->ifa_addr->sa_family) {
1065 #ifdef INET
1066 		case AF_INET:
1067 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
1068 			arp_ifinit(ifp, ifa);
1069 			break;
1070 #endif
1071 #ifdef IPX
1072 		/*
1073 		 * XXX - This code is probably wrong
1074 		 */
1075 		case AF_IPX:
1076 			{
1077 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1078 
1079 			if (ipx_nullhost(*ina))
1080 				ina->x_host =
1081 				    *(union ipx_host *)
1082 				    IF_LLADDR(ifp);
1083 			else {
1084 				bcopy((caddr_t) ina->x_host.c_host,
1085 				      (caddr_t) IF_LLADDR(ifp),
1086 				      ETHER_ADDR_LEN);
1087 			}
1088 
1089 			/*
1090 			 * Set new address
1091 			 */
1092 			ifp->if_init(ifp->if_softc);
1093 			break;
1094 			}
1095 #endif
1096 		default:
1097 			ifp->if_init(ifp->if_softc);
1098 			break;
1099 		}
1100 		break;
1101 
1102 	case SIOCGIFADDR:
1103 		{
1104 			struct sockaddr *sa;
1105 
1106 			sa = (struct sockaddr *) & ifr->ifr_data;
1107 			bcopy(IF_LLADDR(ifp),
1108 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1109 		}
1110 		break;
1111 
1112 	case SIOCSIFMTU:
1113 		/*
1114 		 * Set the interface MTU.
1115 		 */
1116 		if (ifr->ifr_mtu > ETHERMTU) {
1117 			error = EINVAL;
1118 		} else {
1119 			ifp->if_mtu = ifr->ifr_mtu;
1120 		}
1121 		break;
1122 	default:
1123 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1124 		break;
1125 	}
1126 	return (error);
1127 }
1128 
1129 static int
1130 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1131 	struct sockaddr *sa)
1132 {
1133 	struct sockaddr_dl *sdl;
1134 #ifdef INET
1135 	struct sockaddr_in *sin;
1136 #endif
1137 #ifdef INET6
1138 	struct sockaddr_in6 *sin6;
1139 #endif
1140 	u_char *e_addr;
1141 
1142 	switch(sa->sa_family) {
1143 	case AF_LINK:
1144 		/*
1145 		 * No mapping needed. Just check that it's a valid MC address.
1146 		 */
1147 		sdl = (struct sockaddr_dl *)sa;
1148 		e_addr = LLADDR(sdl);
1149 		if (!ETHER_IS_MULTICAST(e_addr))
1150 			return EADDRNOTAVAIL;
1151 		*llsa = 0;
1152 		return 0;
1153 
1154 #ifdef INET
1155 	case AF_INET:
1156 		sin = (struct sockaddr_in *)sa;
1157 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1158 			return EADDRNOTAVAIL;
1159 		sdl = malloc(sizeof *sdl, M_IFMADDR,
1160 		       M_NOWAIT|M_ZERO);
1161 		if (sdl == NULL)
1162 			return ENOMEM;
1163 		sdl->sdl_len = sizeof *sdl;
1164 		sdl->sdl_family = AF_LINK;
1165 		sdl->sdl_index = ifp->if_index;
1166 		sdl->sdl_type = IFT_ETHER;
1167 		sdl->sdl_alen = ETHER_ADDR_LEN;
1168 		e_addr = LLADDR(sdl);
1169 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1170 		*llsa = (struct sockaddr *)sdl;
1171 		return 0;
1172 #endif
1173 #ifdef INET6
1174 	case AF_INET6:
1175 		sin6 = (struct sockaddr_in6 *)sa;
1176 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1177 			/*
1178 			 * An IP6 address of 0 means listen to all
1179 			 * of the Ethernet multicast address used for IP6.
1180 			 * (This is used for multicast routers.)
1181 			 */
1182 			ifp->if_flags |= IFF_ALLMULTI;
1183 			*llsa = 0;
1184 			return 0;
1185 		}
1186 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1187 			return EADDRNOTAVAIL;
1188 		sdl = malloc(sizeof *sdl, M_IFMADDR,
1189 		       M_NOWAIT|M_ZERO);
1190 		if (sdl == NULL)
1191 			return (ENOMEM);
1192 		sdl->sdl_len = sizeof *sdl;
1193 		sdl->sdl_family = AF_LINK;
1194 		sdl->sdl_index = ifp->if_index;
1195 		sdl->sdl_type = IFT_ETHER;
1196 		sdl->sdl_alen = ETHER_ADDR_LEN;
1197 		e_addr = LLADDR(sdl);
1198 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1199 		*llsa = (struct sockaddr *)sdl;
1200 		return 0;
1201 #endif
1202 
1203 	default:
1204 		/*
1205 		 * Well, the text isn't quite right, but it's the name
1206 		 * that counts...
1207 		 */
1208 		return EAFNOSUPPORT;
1209 	}
1210 }
1211 
1212 static void*
1213 ether_alloc(u_char type, struct ifnet *ifp)
1214 {
1215 	struct arpcom	*ac;
1216 
1217 	ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1218 	ac->ac_ifp = ifp;
1219 
1220 	return (ac);
1221 }
1222 
1223 static void
1224 ether_free(void *com, u_char type)
1225 {
1226 
1227 	free(com, M_ARPCOM);
1228 }
1229 
1230 static int
1231 ether_modevent(module_t mod, int type, void *data)
1232 {
1233 
1234 	switch (type) {
1235 	case MOD_LOAD:
1236 		if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1237 		break;
1238 	case MOD_UNLOAD:
1239 		if_deregister_com_alloc(IFT_ETHER);
1240 		break;
1241 	default:
1242 		return EOPNOTSUPP;
1243 	}
1244 
1245 	return (0);
1246 }
1247 
1248 static moduledata_t ether_mod = {
1249 	"ether",
1250 	ether_modevent,
1251 	0
1252 };
1253 
1254 void
1255 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1256 {
1257 	struct ether_vlan_header vlan;
1258 	struct mbuf mv, mb;
1259 
1260 	KASSERT((m->m_flags & M_VLANTAG) != 0,
1261 	    ("%s: vlan information not present", __func__));
1262 	KASSERT(m->m_len >= sizeof(struct ether_header),
1263 	    ("%s: mbuf not large enough for header", __func__));
1264 	bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1265 	vlan.evl_proto = vlan.evl_encap_proto;
1266 	vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1267 	vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1268 	m->m_len -= sizeof(struct ether_header);
1269 	m->m_data += sizeof(struct ether_header);
1270 	/*
1271 	 * If a data link has been supplied by the caller, then we will need to
1272 	 * re-create a stack allocated mbuf chain with the following structure:
1273 	 *
1274 	 * (1) mbuf #1 will contain the supplied data link
1275 	 * (2) mbuf #2 will contain the vlan header
1276 	 * (3) mbuf #3 will contain the original mbuf's packet data
1277 	 *
1278 	 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1279 	 */
1280 	if (data != NULL) {
1281 		mv.m_next = m;
1282 		mv.m_data = (caddr_t)&vlan;
1283 		mv.m_len = sizeof(vlan);
1284 		mb.m_next = &mv;
1285 		mb.m_data = data;
1286 		mb.m_len = dlen;
1287 		bpf_mtap(bp, &mb);
1288 	} else
1289 		bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1290 	m->m_len += sizeof(struct ether_header);
1291 	m->m_data -= sizeof(struct ether_header);
1292 }
1293 
1294 struct mbuf *
1295 ether_vlanencap(struct mbuf *m, uint16_t tag)
1296 {
1297 	struct ether_vlan_header *evl;
1298 
1299 	M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1300 	if (m == NULL)
1301 		return (NULL);
1302 	/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1303 
1304 	if (m->m_len < sizeof(*evl)) {
1305 		m = m_pullup(m, sizeof(*evl));
1306 		if (m == NULL)
1307 			return (NULL);
1308 	}
1309 
1310 	/*
1311 	 * Transform the Ethernet header into an Ethernet header
1312 	 * with 802.1Q encapsulation.
1313 	 */
1314 	evl = mtod(m, struct ether_vlan_header *);
1315 	bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1316 	    (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1317 	evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1318 	evl->evl_tag = htons(tag);
1319 	return (m);
1320 }
1321 
1322 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1323 MODULE_VERSION(ether, 1);
1324