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