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