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