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