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