xref: /freebsd/sys/net/if_ethersubr.c (revision a9413f6ca0c534957978b294aaab4bdb1f1b984f)
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_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_netgraph.h"
36 #include "opt_mbuf_profiling.h"
37 #include "opt_rss.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mbuf.h>
46 #include <sys/random.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sysctl.h>
50 #include <sys/uuid.h>
51 
52 #include <net/if.h>
53 #include <net/if_var.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/pfil.h>
66 #include <net/vnet.h>
67 
68 #include <netpfil/pf/pf_mtag.h>
69 
70 #if defined(INET) || defined(INET6)
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_ether.h>
74 #include <netinet/in_rss.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 #include <net/rt_nhops.h>
82 
83 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
84 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
85 		const struct sockaddr *dst, short *tp, int *hlen);
86 
87 #include <security/mac/mac_framework.h>
88 
89 #ifdef CTASSERT
90 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
91 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
92 #endif
93 
94 VNET_DEFINE(struct pfil_head, link_pfil_hook);	/* Packet filter hooks */
95 
96 /* netgraph node hooks for ng_ether(4) */
97 void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
98 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
99 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
100 void	(*ng_ether_attach_p)(struct ifnet *ifp);
101 void	(*ng_ether_detach_p)(struct ifnet *ifp);
102 
103 void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
104 
105 /* if_bridge(4) support */
106 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
107 int	(*bridge_output_p)(struct ifnet *, struct mbuf *,
108 		struct sockaddr *, struct rtentry *);
109 void	(*bridge_dn_p)(struct mbuf *, struct ifnet *);
110 
111 /* if_lagg(4) support */
112 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
113 
114 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
115 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
116 
117 static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
118 		struct sockaddr *);
119 #ifdef VIMAGE
120 static	void ether_reassign(struct ifnet *, struct vnet *, char *);
121 #endif
122 
123 int ether_output_full(struct ifnet *ifp, struct mbuf *m,
124     const struct sockaddr *dst, struct route *ro);
125 int ether_output2(struct ifnet *ifp, struct mbuf *m, struct nhop_prepend *nh,
126     int af);
127 
128 static int loopback_frame(struct ifnet *ifp, struct mbuf *m, int family,
129     int hlen);
130 
131 #define	ETHER_IS_BROADCAST(addr) \
132 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
133 
134 #define senderr(e) do { error = (e); goto bad;} while (0)
135 
136 static void
137 update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
138 {
139 	int csum_flags = 0;
140 
141 	if (src->m_pkthdr.csum_flags & CSUM_IP)
142 		csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
143 	if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
144 		csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
145 	if (src->m_pkthdr.csum_flags & CSUM_SCTP)
146 		csum_flags |= CSUM_SCTP_VALID;
147 	dst->m_pkthdr.csum_flags |= csum_flags;
148 	if (csum_flags & CSUM_DATA_VALID)
149 		dst->m_pkthdr.csum_data = 0xffff;
150 }
151 
152 int
153 ether_output(struct ifnet *ifp, struct mbuf *m,
154 	const struct sockaddr *dst, struct route *ro)
155 {
156 	if (ro != NULL && (ro->ro_flags & RT_NHOP))
157 		return (ether_output2(ifp, m, (struct nhop_prepend *)ro->ro_lle,
158 		    (ro->ro_flags >> 8) & 0xFF));
159 
160 	return (ether_output_full(ifp, m, dst, ro));
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_full(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 	case pseudo_AF_HDRCMPLT:
253 	    {
254 		const struct ether_header *eh;
255 
256 		hdrcmplt = 1;
257 		eh = (const struct ether_header *)dst->sa_data;
258 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
259 		/* FALLTHROUGH */
260 
261 	case AF_UNSPEC:
262 		loop_copy = 0; /* if this is for us, don't do it */
263 		eh = (const struct ether_header *)dst->sa_data;
264 		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
265 		type = eh->ether_type;
266 		break;
267             }
268 	default:
269 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
270 		senderr(EAFNOSUPPORT);
271 	}
272 
273 	if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
274 		update_mbuf_csumflags(m, m);
275 		return (if_simloop(ifp, m, dst->sa_family, 0));
276 	}
277 
278 	/*
279 	 * Add local net header.  If no space in first mbuf,
280 	 * allocate another.
281 	 */
282 	M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
283 	if (m == NULL)
284 		senderr(ENOBUFS);
285 	eh = mtod(m, struct ether_header *);
286 	(void)memcpy(&eh->ether_type, &type,
287 		sizeof(eh->ether_type));
288 	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
289 	if (hdrcmplt)
290 		(void)memcpy(eh->ether_shost, esrc,
291 			sizeof(eh->ether_shost));
292 	else
293 		(void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
294 			sizeof(eh->ether_shost));
295 
296 	/*
297 	 * If a simplex interface, and the packet is being sent to our
298 	 * Ethernet address or a broadcast address, loopback a copy.
299 	 * XXX To make a simplex device behave exactly like a duplex
300 	 * device, we should copy in the case of sending to our own
301 	 * ethernet address (thus letting the original actually appear
302 	 * on the wire). However, we don't do that here for security
303 	 * reasons and compatibility with the original behavior.
304 	 */
305 	if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
306 	    ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
307 		if ((m->m_flags & M_BCAST) || (bcmp(eh->ether_dhost,
308 		    eh->ether_shost, ETHER_ADDR_LEN) == 0)) {
309 			/* Either broadcast or to-us L2 header */
310 			if (loopback_frame(ifp, m, dst->sa_family, hlen) == 1)
311 				return (0);
312 		}
313 	}
314 
315        /*
316 	* Bridges require special output handling.
317 	*/
318 	if (ifp->if_bridge) {
319 		BRIDGE_OUTPUT(ifp, m, error);
320 		return (error);
321 	}
322 
323 #if defined(INET) || defined(INET6)
324 	if (ifp->if_carp &&
325 	    (error = (*carp_output_p)(ifp, m, dst)))
326 		goto bad;
327 #endif
328 
329 	/* Handle ng_ether(4) processing, if any */
330 	if (ifp->if_l2com != NULL) {
331 		KASSERT(ng_ether_output_p != NULL,
332 		    ("ng_ether_output_p is NULL"));
333 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
334 bad:			if (m != NULL)
335 				m_freem(m);
336 			return (error);
337 		}
338 		if (m == NULL)
339 			return (0);
340 	}
341 
342 	/* Continue with link-layer output */
343 	return ether_output_frame(ifp, m);
344 }
345 
346 /*
347  * We assume this function to be called for
348  * ip[6]_output(), with already pre-compiled L2 header.
349  *
350  * Function assumes all loopback routing is already done on L3,
351  * so the only reason to push packet (copy) to host is M_BCAST flag.
352  */
353 int
354 ether_output2(struct ifnet *ifp, struct mbuf *m, struct nhop_prepend *nh,int af)
355 {
356 	int error;
357 
358 #ifdef MAC
359 	error = mac_ifnet_check_transmit(ifp, m);
360 	if (error)
361 		senderr(error);
362 #endif
363 
364 	M_PROFILE(m);
365 	if (ifp->if_flags & IFF_MONITOR)
366 		senderr(ENETDOWN);
367 	if (!((ifp->if_flags & IFF_UP) &&
368 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
369 		senderr(ENETDOWN);
370 
371 	if ((ifp->if_flags & IFF_SIMPLEX) && (m->m_flags & M_BCAST)) {
372 		/* We have to copy frame to-us */
373 		if (loopback_frame(NH_LIFP(nh), m, af, nh->nh_count) != 0)
374 			return (0);
375 	}
376 
377        /*
378 	* Bridges require special output handling.
379 	*/
380 	if (ifp->if_bridge) {
381 		BRIDGE_OUTPUT(ifp, m, error);
382 		return (error);
383 	}
384 
385 #if defined(INET) || defined(INET6)
386 	if (ifp->if_carp) {
387 		struct sockaddr_in dst;
388 		memset(&dst, 0, sizeof(dst));
389 		//dst.sin_addr =
390 	    	error = (*carp_output_p)(ifp, m,
391 		    (const struct sockaddr *)&dst);
392 		if (error != 0)
393 			goto bad;
394 	}
395 #endif
396 
397 	/* Handle ng_ether(4) processing, if any */
398 	if (ifp->if_l2com != 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 static int
415 loopback_frame(struct ifnet *ifp, struct mbuf *m, int family, int hlen)
416 {
417 	struct ether_header *eh;
418 
419 	if (m->m_flags & M_BCAST) {
420 		struct mbuf *n;
421 
422 		/*
423 		 * Because if_simloop() modifies the packet, we need a
424 		 * writable copy through m_dup() instead of a readonly
425 		 * one as m_copy[m] would give us. The alternative would
426 		 * be to modify if_simloop() to handle the readonly mbuf,
427 		 * but performancewise it is mostly equivalent (trading
428 		 * extra data copying vs. extra locking).
429 		 *
430 		 * XXX This is a local workaround.  A number of less
431 		 * often used kernel parts suffer from the same bug.
432 		 * See PR kern/105943 for a proposed general solution.
433 		 */
434 		if ((n = m_dup(m, M_NOWAIT)) != NULL) {
435 			update_mbuf_csumflags(m, n);
436 			if_simloop(ifp, n, family, hlen);
437 		} else
438 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
439 	} else {
440 		eh = mtod(m, struct ether_header *);
441 		if (bcmp(eh->ether_dhost, eh->ether_shost,
442 		    ETHER_ADDR_LEN) == 0) {
443 			update_mbuf_csumflags(m, m);
444 			if_simloop(ifp, m, family, hlen);
445 			return (1);
446 		}
447 	}
448 
449 	return (0);
450 }
451 
452 /*
453  * Ethernet link layer output routine to send a raw frame to the device.
454  *
455  * This assumes that the 14 byte Ethernet header is present and contiguous
456  * in the first mbuf (if BRIDGE'ing).
457  */
458 int
459 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
460 {
461 	int i;
462 
463 	if (PFIL_HOOKED(&V_link_pfil_hook)) {
464 		i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_OUT, NULL);
465 
466 		if (i != 0)
467 			return (EACCES);
468 
469 		if (m == NULL)
470 			return (0);
471 	}
472 
473 	/*
474 	 * Queue message on interface, update output statistics if
475 	 * successful, and start output if interface not yet active.
476 	 */
477 	return ((ifp->if_transmit)(ifp, m));
478 }
479 
480 #if defined(INET) || defined(INET6)
481 #endif
482 
483 /*
484  * Process a received Ethernet packet; the packet is in the
485  * mbuf chain m with the ethernet header at the front.
486  */
487 static void
488 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
489 {
490 	struct ether_header *eh;
491 	u_short etype;
492 
493 	if ((ifp->if_flags & IFF_UP) == 0) {
494 		m_freem(m);
495 		return;
496 	}
497 #ifdef DIAGNOSTIC
498 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
499 		if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
500 		m_freem(m);
501 		return;
502 	}
503 #endif
504 	/*
505 	 * Do consistency checks to verify assumptions
506 	 * made by code past this point.
507 	 */
508 	if ((m->m_flags & M_PKTHDR) == 0) {
509 		if_printf(ifp, "discard frame w/o packet header\n");
510 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
511 		m_freem(m);
512 		return;
513 	}
514 	if (m->m_len < ETHER_HDR_LEN) {
515 		/* XXX maybe should pullup? */
516 		if_printf(ifp, "discard frame w/o leading ethernet "
517 				"header (len %u pkt len %u)\n",
518 				m->m_len, m->m_pkthdr.len);
519 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
520 		m_freem(m);
521 		return;
522 	}
523 	eh = mtod(m, struct ether_header *);
524 	etype = ntohs(eh->ether_type);
525 	if (m->m_pkthdr.rcvif == NULL) {
526 		if_printf(ifp, "discard frame w/o interface pointer\n");
527 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
528 		m_freem(m);
529 		return;
530 	}
531 #ifdef DIAGNOSTIC
532 	if (m->m_pkthdr.rcvif != ifp) {
533 		if_printf(ifp, "Warning, frame marked as received on %s\n",
534 			m->m_pkthdr.rcvif->if_xname);
535 	}
536 #endif
537 
538 	CURVNET_SET_QUIET(ifp->if_vnet);
539 
540 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
541 		if (ETHER_IS_BROADCAST(eh->ether_dhost))
542 			m->m_flags |= M_BCAST;
543 		else
544 			m->m_flags |= M_MCAST;
545 		if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
546 	}
547 
548 #ifdef MAC
549 	/*
550 	 * Tag the mbuf with an appropriate MAC label before any other
551 	 * consumers can get to it.
552 	 */
553 	mac_ifnet_create_mbuf(ifp, m);
554 #endif
555 
556 	/*
557 	 * Give bpf a chance at the packet.
558 	 */
559 	ETHER_BPF_MTAP(ifp, m);
560 
561 	/*
562 	 * If the CRC is still on the packet, trim it off. We do this once
563 	 * and once only in case we are re-entered. Nothing else on the
564 	 * Ethernet receive path expects to see the FCS.
565 	 */
566 	if (m->m_flags & M_HASFCS) {
567 		m_adj(m, -ETHER_CRC_LEN);
568 		m->m_flags &= ~M_HASFCS;
569 	}
570 
571 	if (!(ifp->if_capenable & IFCAP_HWSTATS))
572 		if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
573 
574 	/* Allow monitor mode to claim this frame, after stats are updated. */
575 	if (ifp->if_flags & IFF_MONITOR) {
576 		m_freem(m);
577 		CURVNET_RESTORE();
578 		return;
579 	}
580 
581 	/* Handle input from a lagg(4) port */
582 	if (ifp->if_type == IFT_IEEE8023ADLAG) {
583 		KASSERT(lagg_input_p != NULL,
584 		    ("%s: if_lagg not loaded!", __func__));
585 		m = (*lagg_input_p)(ifp, m);
586 		if (m != NULL)
587 			ifp = m->m_pkthdr.rcvif;
588 		else {
589 			CURVNET_RESTORE();
590 			return;
591 		}
592 	}
593 
594 	/*
595 	 * If the hardware did not process an 802.1Q tag, do this now,
596 	 * to allow 802.1P priority frames to be passed to the main input
597 	 * path correctly.
598 	 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
599 	 */
600 	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
601 		struct ether_vlan_header *evl;
602 
603 		if (m->m_len < sizeof(*evl) &&
604 		    (m = m_pullup(m, sizeof(*evl))) == NULL) {
605 #ifdef DIAGNOSTIC
606 			if_printf(ifp, "cannot pullup VLAN header\n");
607 #endif
608 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
609 			m_freem(m);
610 			CURVNET_RESTORE();
611 			return;
612 		}
613 
614 		evl = mtod(m, struct ether_vlan_header *);
615 		m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
616 		m->m_flags |= M_VLANTAG;
617 
618 		bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
619 		    ETHER_HDR_LEN - ETHER_TYPE_LEN);
620 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
621 		eh = mtod(m, struct ether_header *);
622 	}
623 
624 	M_SETFIB(m, ifp->if_fib);
625 
626 	/* Allow ng_ether(4) to claim this frame. */
627 	if (ifp->if_l2com != NULL) {
628 		KASSERT(ng_ether_input_p != NULL,
629 		    ("%s: ng_ether_input_p is NULL", __func__));
630 		m->m_flags &= ~M_PROMISC;
631 		(*ng_ether_input_p)(ifp, &m);
632 		if (m == NULL) {
633 			CURVNET_RESTORE();
634 			return;
635 		}
636 		eh = mtod(m, struct ether_header *);
637 	}
638 
639 	/*
640 	 * Allow if_bridge(4) to claim this frame.
641 	 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
642 	 * and the frame should be delivered locally.
643 	 */
644 	if (ifp->if_bridge != NULL) {
645 		m->m_flags &= ~M_PROMISC;
646 		BRIDGE_INPUT(ifp, m);
647 		if (m == NULL) {
648 			CURVNET_RESTORE();
649 			return;
650 		}
651 		eh = mtod(m, struct ether_header *);
652 	}
653 
654 #if defined(INET) || defined(INET6)
655 	/*
656 	 * Clear M_PROMISC on frame so that carp(4) will see it when the
657 	 * mbuf flows up to Layer 3.
658 	 * FreeBSD's implementation of carp(4) uses the inprotosw
659 	 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
660 	 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
661 	 * is outside the scope of the M_PROMISC test below.
662 	 * TODO: Maintain a hash table of ethernet addresses other than
663 	 * ether_dhost which may be active on this ifp.
664 	 */
665 	if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
666 		m->m_flags &= ~M_PROMISC;
667 	} else
668 #endif
669 	{
670 		/*
671 		 * If the frame received was not for our MAC address, set the
672 		 * M_PROMISC flag on the mbuf chain. The frame may need to
673 		 * be seen by the rest of the Ethernet input path in case of
674 		 * re-entry (e.g. bridge, vlan, netgraph) but should not be
675 		 * seen by upper protocol layers.
676 		 */
677 		if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
678 		    bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
679 			m->m_flags |= M_PROMISC;
680 	}
681 
682 	random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER);
683 
684 	ether_demux(ifp, m);
685 	CURVNET_RESTORE();
686 }
687 
688 /*
689  * Ethernet input dispatch; by default, direct dispatch here regardless of
690  * global configuration.  However, if RSS is enabled, hook up RSS affinity
691  * so that when deferred or hybrid dispatch is enabled, we can redistribute
692  * load based on RSS.
693  *
694  * XXXRW: Would be nice if the ifnet passed up a flag indicating whether or
695  * not it had already done work distribution via multi-queue.  Then we could
696  * direct dispatch in the event load balancing was already complete and
697  * handle the case of interfaces with different capabilities better.
698  *
699  * XXXRW: Sort of want an M_DISTRIBUTED flag to avoid multiple distributions
700  * at multiple layers?
701  *
702  * XXXRW: For now, enable all this only if RSS is compiled in, although it
703  * works fine without RSS.  Need to characterise the performance overhead
704  * of the detour through the netisr code in the event the result is always
705  * direct dispatch.
706  */
707 static void
708 ether_nh_input(struct mbuf *m)
709 {
710 
711 	ether_input_internal(m->m_pkthdr.rcvif, m);
712 }
713 
714 static struct netisr_handler	ether_nh = {
715 	.nh_name = "ether",
716 	.nh_handler = ether_nh_input,
717 	.nh_proto = NETISR_ETHER,
718 #ifdef RSS
719 	.nh_policy = NETISR_POLICY_CPU,
720 	.nh_dispatch = NETISR_DISPATCH_DIRECT,
721 	.nh_m2cpuid = rss_m2cpuid,
722 #else
723 	.nh_policy = NETISR_POLICY_SOURCE,
724 	.nh_dispatch = NETISR_DISPATCH_DIRECT,
725 #endif
726 };
727 
728 static void
729 ether_init(__unused void *arg)
730 {
731 
732 	netisr_register(&ether_nh);
733 }
734 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
735 
736 static void
737 vnet_ether_init(__unused void *arg)
738 {
739 	int i;
740 
741 	/* Initialize packet filter hooks. */
742 	V_link_pfil_hook.ph_type = PFIL_TYPE_AF;
743 	V_link_pfil_hook.ph_af = AF_LINK;
744 	if ((i = pfil_head_register(&V_link_pfil_hook)) != 0)
745 		printf("%s: WARNING: unable to register pfil link hook, "
746 			"error %d\n", __func__, i);
747 }
748 VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
749     vnet_ether_init, NULL);
750 
751 static void
752 vnet_ether_destroy(__unused void *arg)
753 {
754 	int i;
755 
756 	if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0)
757 		printf("%s: WARNING: unable to unregister pfil link hook, "
758 			"error %d\n", __func__, i);
759 }
760 VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
761     vnet_ether_destroy, NULL);
762 
763 
764 
765 static void
766 ether_input(struct ifnet *ifp, struct mbuf *m)
767 {
768 
769 	struct mbuf *mn;
770 
771 	/*
772 	 * The drivers are allowed to pass in a chain of packets linked with
773 	 * m_nextpkt. We split them up into separate packets here and pass
774 	 * them up. This allows the drivers to amortize the receive lock.
775 	 */
776 	while (m) {
777 		mn = m->m_nextpkt;
778 		m->m_nextpkt = NULL;
779 
780 		/*
781 		 * We will rely on rcvif being set properly in the deferred context,
782 		 * so assert it is correct here.
783 		 */
784 		KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
785 		netisr_dispatch(NETISR_ETHER, m);
786 		m = mn;
787 	}
788 }
789 
790 /*
791  * Upper layer processing for a received Ethernet packet.
792  */
793 void
794 ether_demux(struct ifnet *ifp, struct mbuf *m)
795 {
796 	struct ether_header *eh;
797 	int i, isr;
798 	u_short ether_type;
799 
800 	KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
801 
802 	/* Do not grab PROMISC frames in case we are re-entered. */
803 	if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
804 		i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL);
805 
806 		if (i != 0 || m == NULL)
807 			return;
808 	}
809 
810 	eh = mtod(m, struct ether_header *);
811 	ether_type = ntohs(eh->ether_type);
812 
813 	/*
814 	 * If this frame has a VLAN tag other than 0, call vlan_input()
815 	 * if its module is loaded. Otherwise, drop.
816 	 */
817 	if ((m->m_flags & M_VLANTAG) &&
818 	    EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
819 		if (ifp->if_vlantrunk == NULL) {
820 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
821 			m_freem(m);
822 			return;
823 		}
824 		KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
825 		    __func__));
826 		/* Clear before possibly re-entering ether_input(). */
827 		m->m_flags &= ~M_PROMISC;
828 		(*vlan_input_p)(ifp, m);
829 		return;
830 	}
831 
832 	/*
833 	 * Pass promiscuously received frames to the upper layer if the user
834 	 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
835 	 */
836 	if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
837 		m_freem(m);
838 		return;
839 	}
840 
841 	/*
842 	 * Reset layer specific mbuf flags to avoid confusing upper layers.
843 	 * Strip off Ethernet header.
844 	 */
845 	m->m_flags &= ~M_VLANTAG;
846 	m_clrprotoflags(m);
847 	m_adj(m, ETHER_HDR_LEN);
848 
849 	/*
850 	 * Dispatch frame to upper layer.
851 	 */
852 	switch (ether_type) {
853 #ifdef INET
854 	case ETHERTYPE_IP:
855 		if ((m = ip_fastforward(m)) == NULL)
856 			return;
857 		isr = NETISR_IP;
858 		break;
859 
860 	case ETHERTYPE_ARP:
861 		if (ifp->if_flags & IFF_NOARP) {
862 			/* Discard packet if ARP is disabled on interface */
863 			m_freem(m);
864 			return;
865 		}
866 		isr = NETISR_ARP;
867 		break;
868 #endif
869 #ifdef INET6
870 	case ETHERTYPE_IPV6:
871 		isr = NETISR_IPV6;
872 		break;
873 #endif
874 	default:
875 		goto discard;
876 	}
877 	netisr_dispatch(isr, m);
878 	return;
879 
880 discard:
881 	/*
882 	 * Packet is to be discarded.  If netgraph is present,
883 	 * hand the packet to it for last chance processing;
884 	 * otherwise dispose of it.
885 	 */
886 	if (ifp->if_l2com != NULL) {
887 		KASSERT(ng_ether_input_orphan_p != NULL,
888 		    ("ng_ether_input_orphan_p is NULL"));
889 		/*
890 		 * Put back the ethernet header so netgraph has a
891 		 * consistent view of inbound packets.
892 		 */
893 		M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
894 		(*ng_ether_input_orphan_p)(ifp, m);
895 		return;
896 	}
897 	m_freem(m);
898 }
899 
900 /*
901  * Convert Ethernet address to printable (loggable) representation.
902  * This routine is for compatibility; it's better to just use
903  *
904  *	printf("%6D", <pointer to address>, ":");
905  *
906  * since there's no static buffer involved.
907  */
908 char *
909 ether_sprintf(const u_char *ap)
910 {
911 	static char etherbuf[18];
912 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
913 	return (etherbuf);
914 }
915 
916 /*
917  * Perform common duties while attaching to interface list
918  */
919 void
920 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
921 {
922 	int i;
923 	struct ifaddr *ifa;
924 	struct sockaddr_dl *sdl;
925 
926 	ifp->if_addrlen = ETHER_ADDR_LEN;
927 	ifp->if_hdrlen = ETHER_HDR_LEN;
928 	if_attach(ifp);
929 	ifp->if_mtu = ETHERMTU;
930 	ifp->if_output = ether_output;
931 	ifp->if_input = ether_input;
932 	ifp->if_resolvemulti = ether_resolvemulti;
933 #ifdef VIMAGE
934 	ifp->if_reassign = ether_reassign;
935 #endif
936 	if (ifp->if_baudrate == 0)
937 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
938 	ifp->if_broadcastaddr = etherbroadcastaddr;
939 
940 	ifa = ifp->if_addr;
941 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
942 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
943 	sdl->sdl_type = IFT_ETHER;
944 	sdl->sdl_alen = ifp->if_addrlen;
945 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
946 
947 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
948 	if (ng_ether_attach_p != NULL)
949 		(*ng_ether_attach_p)(ifp);
950 
951 	/* Announce Ethernet MAC address if non-zero. */
952 	for (i = 0; i < ifp->if_addrlen; i++)
953 		if (lla[i] != 0)
954 			break;
955 	if (i != ifp->if_addrlen)
956 		if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
957 
958 	uuid_ether_add(LLADDR(sdl));
959 }
960 
961 /*
962  * Perform common duties while detaching an Ethernet interface
963  */
964 void
965 ether_ifdetach(struct ifnet *ifp)
966 {
967 	struct sockaddr_dl *sdl;
968 
969 	sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
970 	uuid_ether_del(LLADDR(sdl));
971 
972 	if (ifp->if_l2com != NULL) {
973 		KASSERT(ng_ether_detach_p != NULL,
974 		    ("ng_ether_detach_p is NULL"));
975 		(*ng_ether_detach_p)(ifp);
976 	}
977 
978 	bpfdetach(ifp);
979 	if_detach(ifp);
980 }
981 
982 #ifdef VIMAGE
983 void
984 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
985 {
986 
987 	if (ifp->if_l2com != NULL) {
988 		KASSERT(ng_ether_detach_p != NULL,
989 		    ("ng_ether_detach_p is NULL"));
990 		(*ng_ether_detach_p)(ifp);
991 	}
992 
993 	if (ng_ether_attach_p != NULL) {
994 		CURVNET_SET_QUIET(new_vnet);
995 		(*ng_ether_attach_p)(ifp);
996 		CURVNET_RESTORE();
997 	}
998 }
999 #endif
1000 
1001 SYSCTL_DECL(_net_link);
1002 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
1003 
1004 #if 0
1005 /*
1006  * This is for reference.  We have a table-driven version
1007  * of the little-endian crc32 generator, which is faster
1008  * than the double-loop.
1009  */
1010 uint32_t
1011 ether_crc32_le(const uint8_t *buf, size_t len)
1012 {
1013 	size_t i;
1014 	uint32_t crc;
1015 	int bit;
1016 	uint8_t data;
1017 
1018 	crc = 0xffffffff;	/* initial value */
1019 
1020 	for (i = 0; i < len; i++) {
1021 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1022 			carry = (crc ^ data) & 1;
1023 			crc >>= 1;
1024 			if (carry)
1025 				crc = (crc ^ ETHER_CRC_POLY_LE);
1026 		}
1027 	}
1028 
1029 	return (crc);
1030 }
1031 #else
1032 uint32_t
1033 ether_crc32_le(const uint8_t *buf, size_t len)
1034 {
1035 	static const uint32_t crctab[] = {
1036 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1037 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1038 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1039 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1040 	};
1041 	size_t i;
1042 	uint32_t crc;
1043 
1044 	crc = 0xffffffff;	/* initial value */
1045 
1046 	for (i = 0; i < len; i++) {
1047 		crc ^= buf[i];
1048 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1049 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1050 	}
1051 
1052 	return (crc);
1053 }
1054 #endif
1055 
1056 uint32_t
1057 ether_crc32_be(const uint8_t *buf, size_t len)
1058 {
1059 	size_t i;
1060 	uint32_t crc, carry;
1061 	int bit;
1062 	uint8_t data;
1063 
1064 	crc = 0xffffffff;	/* initial value */
1065 
1066 	for (i = 0; i < len; i++) {
1067 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1068 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1069 			crc <<= 1;
1070 			if (carry)
1071 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1072 		}
1073 	}
1074 
1075 	return (crc);
1076 }
1077 
1078 int
1079 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1080 {
1081 	struct ifaddr *ifa = (struct ifaddr *) data;
1082 	struct ifreq *ifr = (struct ifreq *) data;
1083 	int error = 0;
1084 
1085 	switch (command) {
1086 	case SIOCSIFADDR:
1087 		ifp->if_flags |= IFF_UP;
1088 
1089 		switch (ifa->ifa_addr->sa_family) {
1090 #ifdef INET
1091 		case AF_INET:
1092 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
1093 			arp_ifinit(ifp, ifa);
1094 			break;
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 = link_init_sdl(ifp, *llsa, IFT_ETHER);
1160 		sdl->sdl_alen = ETHER_ADDR_LEN;
1161 		e_addr = LLADDR(sdl);
1162 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1163 		*llsa = (struct sockaddr *)sdl;
1164 		return 0;
1165 #endif
1166 #ifdef INET6
1167 	case AF_INET6:
1168 		sin6 = (struct sockaddr_in6 *)sa;
1169 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1170 			/*
1171 			 * An IP6 address of 0 means listen to all
1172 			 * of the Ethernet multicast address used for IP6.
1173 			 * (This is used for multicast routers.)
1174 			 */
1175 			ifp->if_flags |= IFF_ALLMULTI;
1176 			*llsa = 0;
1177 			return 0;
1178 		}
1179 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1180 			return EADDRNOTAVAIL;
1181 		sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1182 		sdl->sdl_alen = ETHER_ADDR_LEN;
1183 		e_addr = LLADDR(sdl);
1184 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1185 		*llsa = (struct sockaddr *)sdl;
1186 		return 0;
1187 #endif
1188 
1189 	default:
1190 		/*
1191 		 * Well, the text isn't quite right, but it's the name
1192 		 * that counts...
1193 		 */
1194 		return EAFNOSUPPORT;
1195 	}
1196 }
1197 
1198 static moduledata_t ether_mod = {
1199 	.name = "ether",
1200 };
1201 
1202 void
1203 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1204 {
1205 	struct ether_vlan_header vlan;
1206 	struct mbuf mv, mb;
1207 
1208 	KASSERT((m->m_flags & M_VLANTAG) != 0,
1209 	    ("%s: vlan information not present", __func__));
1210 	KASSERT(m->m_len >= sizeof(struct ether_header),
1211 	    ("%s: mbuf not large enough for header", __func__));
1212 	bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1213 	vlan.evl_proto = vlan.evl_encap_proto;
1214 	vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1215 	vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1216 	m->m_len -= sizeof(struct ether_header);
1217 	m->m_data += sizeof(struct ether_header);
1218 	/*
1219 	 * If a data link has been supplied by the caller, then we will need to
1220 	 * re-create a stack allocated mbuf chain with the following structure:
1221 	 *
1222 	 * (1) mbuf #1 will contain the supplied data link
1223 	 * (2) mbuf #2 will contain the vlan header
1224 	 * (3) mbuf #3 will contain the original mbuf's packet data
1225 	 *
1226 	 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1227 	 */
1228 	if (data != NULL) {
1229 		mv.m_next = m;
1230 		mv.m_data = (caddr_t)&vlan;
1231 		mv.m_len = sizeof(vlan);
1232 		mb.m_next = &mv;
1233 		mb.m_data = data;
1234 		mb.m_len = dlen;
1235 		bpf_mtap(bp, &mb);
1236 	} else
1237 		bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1238 	m->m_len += sizeof(struct ether_header);
1239 	m->m_data -= sizeof(struct ether_header);
1240 }
1241 
1242 struct mbuf *
1243 ether_vlanencap(struct mbuf *m, uint16_t tag)
1244 {
1245 	struct ether_vlan_header *evl;
1246 
1247 	M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1248 	if (m == NULL)
1249 		return (NULL);
1250 	/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1251 
1252 	if (m->m_len < sizeof(*evl)) {
1253 		m = m_pullup(m, sizeof(*evl));
1254 		if (m == NULL)
1255 			return (NULL);
1256 	}
1257 
1258 	/*
1259 	 * Transform the Ethernet header into an Ethernet header
1260 	 * with 802.1Q encapsulation.
1261 	 */
1262 	evl = mtod(m, struct ether_vlan_header *);
1263 	bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1264 	    (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1265 	evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1266 	evl->evl_tag = htons(tag);
1267 	return (m);
1268 }
1269 
1270 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1271 MODULE_VERSION(ether, 1);
1272