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