xref: /netbsd-src/sys/net/if_ethersubr.c (revision 4e00368f12e7278a94903a082dfe31dfebb70415)
1 /*	$NetBSD: if_ethersubr.c,v 1.228 2016/10/03 11:06:06 ozaki-r Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1989, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)if_ethersubr.c	8.2 (Berkeley) 4/4/96
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.228 2016/10/03 11:06:06 ozaki-r Exp $");
65 
66 #ifdef _KERNEL_OPT
67 #include "opt_inet.h"
68 #include "opt_atalk.h"
69 #include "opt_mbuftrace.h"
70 #include "opt_mpls.h"
71 #include "opt_gateway.h"
72 #include "opt_pppoe.h"
73 #include "opt_net_mpsafe.h"
74 #endif
75 
76 #include "vlan.h"
77 #include "pppoe.h"
78 #include "bridge.h"
79 #include "arp.h"
80 #include "agr.h"
81 
82 #include <sys/sysctl.h>
83 #include <sys/malloc.h>
84 #include <sys/mbuf.h>
85 #include <sys/mutex.h>
86 #include <sys/ioctl.h>
87 #include <sys/errno.h>
88 #include <sys/device.h>
89 #include <sys/rnd.h>
90 #include <sys/rndsource.h>
91 #include <sys/cpu.h>
92 
93 #include <net/if.h>
94 #include <net/netisr.h>
95 #include <net/route.h>
96 #include <net/if_llc.h>
97 #include <net/if_dl.h>
98 #include <net/if_types.h>
99 #include <net/pktqueue.h>
100 
101 #include <net/if_media.h>
102 #include <dev/mii/mii.h>
103 #include <dev/mii/miivar.h>
104 
105 #if NARP == 0
106 /*
107  * XXX there should really be a way to issue this warning from within config(8)
108  */
109 #error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK.
110 #endif
111 
112 #include <net/bpf.h>
113 
114 #include <net/if_ether.h>
115 #include <net/if_vlanvar.h>
116 
117 #if NPPPOE > 0
118 #include <net/if_pppoe.h>
119 #endif
120 
121 #if NAGR > 0
122 #include <net/agr/ieee8023_slowprotocols.h>	/* XXX */
123 #include <net/agr/ieee8023ad.h>
124 #include <net/agr/if_agrvar.h>
125 #endif
126 
127 #if NBRIDGE > 0
128 #include <net/if_bridgevar.h>
129 #endif
130 
131 #include <netinet/in.h>
132 #ifdef INET
133 #include <netinet/in_var.h>
134 #endif
135 #include <netinet/if_inarp.h>
136 
137 #ifdef INET6
138 #ifndef INET
139 #include <netinet/in.h>
140 #endif
141 #include <netinet6/in6_var.h>
142 #include <netinet6/nd6.h>
143 #endif
144 
145 
146 #include "carp.h"
147 #if NCARP > 0
148 #include <netinet/ip_carp.h>
149 #endif
150 
151 #ifdef NETATALK
152 #include <netatalk/at.h>
153 #include <netatalk/at_var.h>
154 #include <netatalk/at_extern.h>
155 
156 #define llc_snap_org_code llc_un.type_snap.org_code
157 #define llc_snap_ether_type llc_un.type_snap.ether_type
158 
159 extern u_char	at_org_code[3];
160 extern u_char	aarp_org_code[3];
161 #endif /* NETATALK */
162 
163 #ifdef MPLS
164 #include <netmpls/mpls.h>
165 #include <netmpls/mpls_var.h>
166 #endif
167 
168 static struct timeval bigpktppslim_last;
169 static int bigpktppslim = 2;	/* XXX */
170 static int bigpktpps_count;
171 static kmutex_t bigpktpps_lock __cacheline_aligned;
172 
173 
174 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
175     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
176 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
177     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
178 #define senderr(e) { error = (e); goto bad;}
179 
180 static	int ether_output(struct ifnet *, struct mbuf *,
181 	    const struct sockaddr *, const struct rtentry *);
182 
183 /*
184  * Ethernet output routine.
185  * Encapsulate a packet of type family for the local net.
186  * Assumes that ifp is actually pointer to ethercom structure.
187  */
188 static int
189 ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
190 	const struct sockaddr * const dst,
191 	const struct rtentry *rt)
192 {
193 	uint16_t etype = 0;
194 	int error = 0, hdrcmplt = 0;
195  	uint8_t esrc[6], edst[6];
196 	struct mbuf *m = m0;
197 	struct mbuf *mcopy = NULL;
198 	struct ether_header *eh;
199 	struct ifnet *ifp = ifp0;
200 #ifdef INET
201 	struct arphdr *ah;
202 #endif /* INET */
203 #ifdef NETATALK
204 	struct at_ifaddr *aa;
205 #endif /* NETATALK */
206 
207 	/*
208 	 * some paths such as carp_output() call ethr_output() with "ifp"
209 	 * argument as other than ether ifnet.
210 	 */
211 	KASSERT(ifp->if_output != ether_output
212 	    || ifp->if_extflags & IFEF_OUTPUT_MPSAFE);
213 
214 #ifdef MBUFTRACE
215 	m_claimm(m, ifp->if_mowner);
216 #endif
217 
218 #if NCARP > 0
219 	if (ifp->if_type == IFT_CARP) {
220 		struct ifaddr *ifa;
221 		int s = pserialize_read_enter();
222 
223 		/* loop back if this is going to the carp interface */
224 		if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
225 		    (ifa = ifa_ifwithaddr(dst)) != NULL) {
226 			if (ifa->ifa_ifp == ifp0) {
227 				pserialize_read_exit(s);
228 				return looutput(ifp0, m, dst, rt);
229 			}
230 		}
231 		pserialize_read_exit(s);
232 
233 		ifp = ifp->if_carpdev;
234 		/* ac = (struct arpcom *)ifp; */
235 
236 		if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) !=
237 		    (IFF_UP|IFF_RUNNING))
238 			senderr(ENETDOWN);
239 	}
240 #endif /* NCARP > 0 */
241 
242 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
243 		senderr(ENETDOWN);
244 
245 	switch (dst->sa_family) {
246 
247 #ifdef INET
248 	case AF_INET:
249 		KERNEL_LOCK(1, NULL);
250 		if (m->m_flags & M_BCAST)
251 			(void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
252 		else if (m->m_flags & M_MCAST)
253 			ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
254 		else if ((error = arpresolve(ifp, rt, m, dst, edst,
255 		    sizeof(edst))) != 0) {
256 			KERNEL_UNLOCK_ONE(NULL);
257 			return error == EWOULDBLOCK ? 0 : error;
258 		}
259 		KERNEL_UNLOCK_ONE(NULL);
260 		/* If broadcasting on a simplex interface, loopback a copy */
261 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
262 			mcopy = m_copy(m, 0, (int)M_COPYALL);
263 		etype = htons(ETHERTYPE_IP);
264 		break;
265 
266 	case AF_ARP:
267 		ah = mtod(m, struct arphdr *);
268 		if (m->m_flags & M_BCAST)
269 			(void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
270 		else {
271 			void *tha = ar_tha(ah);
272 
273 			if (tha == NULL) {
274 				/* fake with ARPHDR_IEEE1394 */
275 				return 0;
276 			}
277 			memcpy(edst, tha, sizeof(edst));
278 		}
279 
280 		ah->ar_hrd = htons(ARPHRD_ETHER);
281 
282 		switch (ntohs(ah->ar_op)) {
283 		case ARPOP_REVREQUEST:
284 		case ARPOP_REVREPLY:
285 			etype = htons(ETHERTYPE_REVARP);
286 			break;
287 
288 		case ARPOP_REQUEST:
289 		case ARPOP_REPLY:
290 		default:
291 			etype = htons(ETHERTYPE_ARP);
292 		}
293 
294 		break;
295 #endif
296 #ifdef INET6
297 	case AF_INET6:
298 		if (!nd6_storelladdr(ifp, rt, m, dst, edst, sizeof(edst))){
299 			/* something bad happened */
300 			return (0);
301 		}
302 		etype = htons(ETHERTYPE_IPV6);
303 		break;
304 #endif
305 #ifdef NETATALK
306     case AF_APPLETALK: {
307 		struct ifaddr *ifa;
308 		int s;
309 
310 		KERNEL_LOCK(1, NULL);
311 		if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
312 #ifdef NETATALKDEBUG
313 			printf("aarpresolv failed\n");
314 #endif /* NETATALKDEBUG */
315 			KERNEL_UNLOCK_ONE(NULL);
316 			return (0);
317 		}
318 		/*
319 		 * ifaddr is the first thing in at_ifaddr
320 		 */
321 		s = pserialize_read_enter();
322 		ifa = at_ifawithnet((const struct sockaddr_at *)dst, ifp);
323 		if (ifa == NULL) {
324 			pserialize_read_exit(s);
325 			KERNEL_UNLOCK_ONE(NULL);
326 			goto bad;
327 		}
328 		aa = (struct at_ifaddr *)ifa;
329 
330 		/*
331 		 * In the phase 2 case, we need to prepend an mbuf for the
332 		 * llc header.  Since we must preserve the value of m,
333 		 * which is passed to us by value, we m_copy() the first
334 		 * mbuf, and use it for our llc header.
335 		 */
336 		if (aa->aa_flags & AFA_PHASE2) {
337 			struct llc llc;
338 
339 			M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
340 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
341 			llc.llc_control = LLC_UI;
342 			memcpy(llc.llc_snap_org_code, at_org_code,
343 			    sizeof(llc.llc_snap_org_code));
344 			llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
345 			memcpy(mtod(m, void *), &llc, sizeof(struct llc));
346 		} else {
347 			etype = htons(ETHERTYPE_ATALK);
348 		}
349 		pserialize_read_exit(s);
350 		KERNEL_UNLOCK_ONE(NULL);
351 		break;
352 	    }
353 #endif /* NETATALK */
354 	case pseudo_AF_HDRCMPLT:
355 		hdrcmplt = 1;
356 		memcpy(esrc,
357 		    ((const struct ether_header *)dst->sa_data)->ether_shost,
358 		    sizeof(esrc));
359 		/* FALLTHROUGH */
360 
361 	case AF_UNSPEC:
362  		memcpy(edst,
363 		    ((const struct ether_header *)dst->sa_data)->ether_dhost,
364 		    sizeof(edst));
365 		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
366 		etype = ((const struct ether_header *)dst->sa_data)->ether_type;
367 		break;
368 
369 	default:
370 		printf("%s: can't handle af%d\n", ifp->if_xname,
371 			dst->sa_family);
372 		senderr(EAFNOSUPPORT);
373 	}
374 
375 #ifdef MPLS
376 	KERNEL_LOCK(1, NULL);
377 	{
378 		struct m_tag *mtag;
379 		mtag = m_tag_find(m, PACKET_TAG_MPLS, NULL);
380 		if (mtag != NULL) {
381 			/* Having the tag itself indicates it's MPLS */
382 			etype = htons(ETHERTYPE_MPLS);
383 			m_tag_delete(m, mtag);
384 		}
385 	}
386 	KERNEL_UNLOCK_ONE(NULL);
387 #endif
388 
389 	if (mcopy)
390 		(void)looutput(ifp, mcopy, dst, rt);
391 
392 	/* If no ether type is set, this must be a 802.2 formatted packet.
393 	 */
394 	if (etype == 0)
395 		etype = htons(m->m_pkthdr.len);
396 	/*
397 	 * Add local net header.  If no space in first mbuf,
398 	 * allocate another.
399 	 */
400 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
401 	if (m == 0)
402 		senderr(ENOBUFS);
403 	eh = mtod(m, struct ether_header *);
404 	/* Note: etype is already in network byte order. */
405 	(void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
406  	memcpy(eh->ether_dhost, edst, sizeof(edst));
407 	if (hdrcmplt)
408 		memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
409 	else
410 	 	memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
411 		    sizeof(eh->ether_shost));
412 
413 #if NCARP > 0
414 	if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
415 	 	memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
416 		    sizeof(eh->ether_shost));
417 	}
418 #endif /* NCARP > 0 */
419 
420 	if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
421 		return (error);
422 	if (m == NULL)
423 		return (0);
424 
425 #if NBRIDGE > 0
426 	/*
427 	 * Bridges require special output handling.
428 	 */
429 	if (ifp->if_bridge)
430 		return (bridge_output(ifp, m, NULL, NULL));
431 #endif
432 
433 #if NCARP > 0
434 	if (ifp != ifp0)
435 		ifp0->if_obytes += m->m_pkthdr.len + ETHER_HDR_LEN;
436 #endif /* NCARP > 0 */
437 
438 #ifdef ALTQ
439 	KERNEL_LOCK(1, NULL);
440 	/*
441 	 * If ALTQ is enabled on the parent interface, do
442 	 * classification; the queueing discipline might not
443 	 * require classification, but might require the
444 	 * address family/header pointer in the pktattr.
445 	 */
446 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
447 		altq_etherclassify(&ifp->if_snd, m);
448 	KERNEL_UNLOCK_ONE(NULL);
449 #endif
450 	return ifq_enqueue(ifp, m);
451 
452 bad:
453 	if (m)
454 		m_freem(m);
455 	return (error);
456 }
457 
458 #ifdef ALTQ
459 /*
460  * This routine is a slight hack to allow a packet to be classified
461  * if the Ethernet headers are present.  It will go away when ALTQ's
462  * classification engine understands link headers.
463  */
464 void
465 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m)
466 {
467 	struct ether_header *eh;
468 	uint16_t ether_type;
469 	int hlen, af, hdrsize;
470 	void *hdr;
471 
472 	hlen = ETHER_HDR_LEN;
473 	eh = mtod(m, struct ether_header *);
474 
475 	ether_type = htons(eh->ether_type);
476 
477 	if (ether_type < ETHERMTU) {
478 		/* LLC/SNAP */
479 		struct llc *llc = (struct llc *)(eh + 1);
480 		hlen += 8;
481 
482 		if (m->m_len < hlen ||
483 		    llc->llc_dsap != LLC_SNAP_LSAP ||
484 		    llc->llc_ssap != LLC_SNAP_LSAP ||
485 		    llc->llc_control != LLC_UI) {
486 			/* Not SNAP. */
487 			goto bad;
488 		}
489 
490 		ether_type = htons(llc->llc_un.type_snap.ether_type);
491 	}
492 
493 	switch (ether_type) {
494 	case ETHERTYPE_IP:
495 		af = AF_INET;
496 		hdrsize = 20;		/* sizeof(struct ip) */
497 		break;
498 
499 	case ETHERTYPE_IPV6:
500 		af = AF_INET6;
501 		hdrsize = 40;		/* sizeof(struct ip6_hdr) */
502 		break;
503 
504 	default:
505 		af = AF_UNSPEC;
506 		hdrsize = 0;
507 		break;
508 	}
509 
510 	while (m->m_len <= hlen) {
511 		hlen -= m->m_len;
512 		m = m->m_next;
513 	}
514 	if (m->m_len < (hlen + hdrsize)) {
515 		/*
516 		 * protocol header not in a single mbuf.
517 		 * We can't cope with this situation right
518 		 * now (but it shouldn't ever happen, really, anyhow).
519 		 */
520 #ifdef DEBUG
521 		printf("altq_etherclassify: headers span multiple mbufs: "
522 		    "%d < %d\n", m->m_len, (hlen + hdrsize));
523 #endif
524 		goto bad;
525 	}
526 
527 	m->m_data += hlen;
528 	m->m_len -= hlen;
529 
530 	hdr = mtod(m, void *);
531 
532 	if (ALTQ_NEEDS_CLASSIFY(ifq))
533 		m->m_pkthdr.pattr_class =
534 		    (*ifq->altq_classify)(ifq->altq_clfier, m, af);
535 	m->m_pkthdr.pattr_af = af;
536 	m->m_pkthdr.pattr_hdr = hdr;
537 
538 	m->m_data -= hlen;
539 	m->m_len += hlen;
540 
541 	return;
542 
543  bad:
544 	m->m_pkthdr.pattr_class = NULL;
545 	m->m_pkthdr.pattr_hdr = NULL;
546 	m->m_pkthdr.pattr_af = AF_UNSPEC;
547 }
548 #endif /* ALTQ */
549 
550 /*
551  * Process a received Ethernet packet;
552  * the packet is in the mbuf chain m with
553  * the ether header.
554  */
555 void
556 ether_input(struct ifnet *ifp, struct mbuf *m)
557 {
558 	struct ethercom *ec = (struct ethercom *) ifp;
559 	pktqueue_t *pktq = NULL;
560 	struct ifqueue *inq = NULL;
561 	uint16_t etype;
562 	struct ether_header *eh;
563 	size_t ehlen;
564 	static int earlypkts;
565 	int isr = 0;
566 #if defined (LLC) || defined(NETATALK)
567 	struct llc *l;
568 #endif
569 
570 	KASSERT(!cpu_intr_p());
571 
572 	if ((ifp->if_flags & IFF_UP) == 0) {
573 		m_freem(m);
574 		return;
575 	}
576 
577 #ifdef MBUFTRACE
578 	m_claimm(m, &ec->ec_rx_mowner);
579 #endif
580 	eh = mtod(m, struct ether_header *);
581 	etype = ntohs(eh->ether_type);
582 	ehlen = sizeof(*eh);
583 
584 	if(__predict_false(earlypkts < 100 || !rnd_initial_entropy)) {
585 		rnd_add_data(NULL, eh, ehlen, 0);
586 		earlypkts++;
587 	}
588 
589 	/*
590 	 * Determine if the packet is within its size limits.
591 	 */
592 	if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
593 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
594 		mutex_enter(&bigpktpps_lock);
595 		if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
596 			    bigpktppslim)) {
597 			printf("%s: discarding oversize frame (len=%d)\n",
598 			    ifp->if_xname, m->m_pkthdr.len);
599 		}
600 		mutex_exit(&bigpktpps_lock);
601 		m_freem(m);
602 		return;
603 	}
604 
605 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
606 		/*
607 		 * If this is not a simplex interface, drop the packet
608 		 * if it came from us.
609 		 */
610 		if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
611 		    memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
612 		    ETHER_ADDR_LEN) == 0) {
613 			m_freem(m);
614 			return;
615 		}
616 
617 		if (memcmp(etherbroadcastaddr,
618 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
619 			m->m_flags |= M_BCAST;
620 		else
621 			m->m_flags |= M_MCAST;
622 		ifp->if_imcasts++;
623 	}
624 
625 	/* If the CRC is still on the packet, trim it off. */
626 	if (m->m_flags & M_HASFCS) {
627 		m_adj(m, -ETHER_CRC_LEN);
628 		m->m_flags &= ~M_HASFCS;
629 	}
630 
631 	ifp->if_ibytes += m->m_pkthdr.len;
632 
633 #if NCARP > 0
634 	if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
635 		/*
636 		 * clear M_PROMISC, in case the packets comes from a
637 		 * vlan
638 		 */
639 		m->m_flags &= ~M_PROMISC;
640 		if (carp_input(m, (uint8_t *)&eh->ether_shost,
641 		    (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
642 			return;
643 	}
644 #endif /* NCARP > 0 */
645 	if ((m->m_flags & (M_BCAST|M_MCAST|M_PROMISC)) == 0 &&
646 	    (ifp->if_flags & IFF_PROMISC) != 0 &&
647 	    memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
648 		   ETHER_ADDR_LEN) != 0) {
649 		m->m_flags |= M_PROMISC;
650 	}
651 
652 	if ((m->m_flags & M_PROMISC) == 0) {
653 		if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
654 			return;
655 		if (m == NULL)
656 			return;
657 
658 		eh = mtod(m, struct ether_header *);
659 		etype = ntohs(eh->ether_type);
660 		ehlen = sizeof(*eh);
661 	}
662 
663 #if NAGR > 0
664 	if (ifp->if_agrprivate &&
665 	    __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
666 		m->m_flags &= ~M_PROMISC;
667 		agr_input(ifp, m);
668 		return;
669 	}
670 #endif /* NAGR > 0 */
671 
672 	/*
673 	 * If VLANs are configured on the interface, check to
674 	 * see if the device performed the decapsulation and
675 	 * provided us with the tag.
676 	 */
677 	if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
678 #if NVLAN > 0
679 		/*
680 		 * vlan_input() will either recursively call ether_input()
681 		 * or drop the packet.
682 		 */
683 		vlan_input(ifp, m);
684 #else
685 		m_freem(m);
686 #endif
687 		return;
688 	}
689 
690 	/*
691 	 * Handle protocols that expect to have the Ethernet header
692 	 * (and possibly FCS) intact.
693 	 */
694 	switch (etype) {
695 	case ETHERTYPE_VLAN: {
696 		struct ether_vlan_header *evl = (void *)eh;
697 		/*
698 		 * If there is a tag of 0, then the VLAN header was probably
699 		 * just being used to store the priority.  Extract the ether
700 		 * type, and if IP or IPV6, let them deal with it.
701 		 */
702 		if (m->m_len <= sizeof(*evl)
703 		    && EVL_VLANOFTAG(evl->evl_tag) == 0) {
704 			etype = ntohs(evl->evl_proto);
705 			ehlen = sizeof(*evl);
706 			if ((m->m_flags & M_PROMISC) == 0
707 			    && (etype == ETHERTYPE_IP
708 				|| etype == ETHERTYPE_IPV6))
709 				break;
710 		}
711 #if NVLAN > 0
712 		/*
713 		 * vlan_input() will either recursively call ether_input()
714 		 * or drop the packet.
715 		 */
716 		if (((struct ethercom *)ifp)->ec_nvlans != 0)
717 			vlan_input(ifp, m);
718 		else
719 #endif /* NVLAN > 0 */
720 			m_freem(m);
721 		return;
722 	}
723 #if NPPPOE > 0
724 	case ETHERTYPE_PPPOEDISC:
725 		pppoedisc_input(ifp, m);
726 		return;
727 	case ETHERTYPE_PPPOE:
728 		pppoe_input(ifp, m);
729 		return;
730 #endif /* NPPPOE > 0 */
731 	case ETHERTYPE_SLOWPROTOCOLS: {
732 		uint8_t subtype;
733 
734 #if defined(DIAGNOSTIC)
735 		if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
736 			panic("ether_input: too short slow protocol packet");
737 		}
738 #endif
739 		m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
740 		switch (subtype) {
741 #if NAGR > 0
742 		case SLOWPROTOCOLS_SUBTYPE_LACP:
743 			if (ifp->if_agrprivate) {
744 				ieee8023ad_lacp_input(ifp, m);
745 				return;
746 			}
747 			break;
748 
749 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
750 			if (ifp->if_agrprivate) {
751 				ieee8023ad_marker_input(ifp, m);
752 				return;
753 			}
754 			break;
755 #endif /* NAGR > 0 */
756 		default:
757 			if (subtype == 0 || subtype > 10) {
758 				/* illegal value */
759 				m_freem(m);
760 				return;
761 			}
762 			/* unknown subtype */
763 			break;
764 		}
765 		/* FALLTHROUGH */
766 	}
767 	default:
768 		if (m->m_flags & M_PROMISC) {
769 			m_freem(m);
770 			return;
771 		}
772 	}
773 
774 	/* If the CRC is still on the packet, trim it off. */
775 	if (m->m_flags & M_HASFCS) {
776 		m_adj(m, -ETHER_CRC_LEN);
777 		m->m_flags &= ~M_HASFCS;
778 	}
779 
780 	if (etype > ETHERMTU + sizeof (struct ether_header)) {
781 		/* Strip off the Ethernet header. */
782 		m_adj(m, ehlen);
783 
784 		switch (etype) {
785 #ifdef INET
786 		case ETHERTYPE_IP:
787 #ifdef GATEWAY
788 			if (ipflow_fastforward(m))
789 				return;
790 #endif
791 			pktq = ip_pktq;
792 			break;
793 
794 		case ETHERTYPE_ARP:
795 			isr = NETISR_ARP;
796 			inq = &arpintrq;
797 			break;
798 
799 		case ETHERTYPE_REVARP:
800 			revarpinput(m);	/* XXX queue? */
801 			return;
802 #endif
803 #ifdef INET6
804 		case ETHERTYPE_IPV6:
805 			if (__predict_false(!in6_present)) {
806 				m_freem(m);
807 				return;
808 			}
809 #ifdef GATEWAY
810 			if (ip6flow_fastforward(&m))
811 				return;
812 #endif
813 			pktq = ip6_pktq;
814 			break;
815 #endif
816 #ifdef NETATALK
817 		case ETHERTYPE_ATALK:
818 			isr = NETISR_ATALK;
819 			inq = &atintrq1;
820 			break;
821 		case ETHERTYPE_AARP:
822 			/* probably this should be done with a NETISR as well */
823 			aarpinput(ifp, m); /* XXX */
824 			return;
825 #endif /* NETATALK */
826 #ifdef MPLS
827 		case ETHERTYPE_MPLS:
828 			isr = NETISR_MPLS;
829 			inq = &mplsintrq;
830 			break;
831 #endif
832 		default:
833 			m_freem(m);
834 			return;
835 		}
836 	} else {
837 #if defined (LLC) || defined (NETATALK)
838 		l = (struct llc *)(eh+1);
839 		switch (l->llc_dsap) {
840 #ifdef NETATALK
841 		case LLC_SNAP_LSAP:
842 			switch (l->llc_control) {
843 			case LLC_UI:
844 				if (l->llc_ssap != LLC_SNAP_LSAP) {
845 					goto dropanyway;
846 				}
847 
848 				if (memcmp(&(l->llc_snap_org_code)[0],
849 				    at_org_code, sizeof(at_org_code)) == 0 &&
850 				    ntohs(l->llc_snap_ether_type) ==
851 				    ETHERTYPE_ATALK) {
852 					inq = &atintrq2;
853 					m_adj(m, sizeof(struct ether_header)
854 					    + sizeof(struct llc));
855 					isr = NETISR_ATALK;
856 					break;
857 				}
858 
859 				if (memcmp(&(l->llc_snap_org_code)[0],
860 				    aarp_org_code,
861 				    sizeof(aarp_org_code)) == 0 &&
862 				    ntohs(l->llc_snap_ether_type) ==
863 				    ETHERTYPE_AARP) {
864 					m_adj( m, sizeof(struct ether_header)
865 					    + sizeof(struct llc));
866 					aarpinput(ifp, m); /* XXX */
867 				    return;
868 				}
869 
870 			default:
871 				goto dropanyway;
872 			}
873 			break;
874 		dropanyway:
875 #endif
876 		default:
877 			m_freem(m);
878 			return;
879 		}
880 #else /* ISO || LLC || NETATALK*/
881 		m_freem(m);
882 		return;
883 #endif /* ISO || LLC || NETATALK*/
884 	}
885 
886 	if (__predict_true(pktq)) {
887 		const uint32_t h = pktq_rps_hash(m);
888 		if (__predict_false(!pktq_enqueue(pktq, m, h))) {
889 			m_freem(m);
890 		}
891 		return;
892 	}
893 
894 	if (__predict_false(!inq)) {
895 		/* Should not happen. */
896 		m_freem(m);
897 		return;
898 	}
899 
900 	IFQ_LOCK(inq);
901 	if (IF_QFULL(inq)) {
902 		IF_DROP(inq);
903 		IFQ_UNLOCK(inq);
904 		m_freem(m);
905 	} else {
906 		IF_ENQUEUE(inq, m);
907 		IFQ_UNLOCK(inq);
908 		schednetisr(isr);
909 	}
910 }
911 
912 /*
913  * Convert Ethernet address to printable (loggable) representation.
914  */
915 char *
916 ether_sprintf(const u_char *ap)
917 {
918 	static char etherbuf[3 * ETHER_ADDR_LEN];
919 	return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
920 }
921 
922 char *
923 ether_snprintf(char *buf, size_t len, const u_char *ap)
924 {
925 	char *cp = buf;
926 	size_t i;
927 
928 	for (i = 0; i < len / 3; i++) {
929 		*cp++ = hexdigits[*ap >> 4];
930 		*cp++ = hexdigits[*ap++ & 0xf];
931 		*cp++ = ':';
932 	}
933 	*--cp = '\0';
934 	return buf;
935 }
936 
937 /*
938  * Perform common duties while attaching to interface list
939  */
940 void
941 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
942 {
943 	struct ethercom *ec = (struct ethercom *)ifp;
944 
945 	ifp->if_extflags |= IFEF_OUTPUT_MPSAFE;
946 	ifp->if_type = IFT_ETHER;
947 	ifp->if_hdrlen = ETHER_HDR_LEN;
948 	ifp->if_dlt = DLT_EN10MB;
949 	ifp->if_mtu = ETHERMTU;
950 	ifp->if_output = ether_output;
951 	ifp->_if_input = ether_input;
952 	if (ifp->if_baudrate == 0)
953 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
954 
955 	if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
956 
957 	LIST_INIT(&ec->ec_multiaddrs);
958 	ifp->if_broadcastaddr = etherbroadcastaddr;
959 	bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
960 #ifdef MBUFTRACE
961 	strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
962 	    sizeof(ec->ec_tx_mowner.mo_name));
963 	strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
964 	    sizeof(ec->ec_tx_mowner.mo_descr));
965 	strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
966 	    sizeof(ec->ec_rx_mowner.mo_name));
967 	strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
968 	    sizeof(ec->ec_rx_mowner.mo_descr));
969 	MOWNER_ATTACH(&ec->ec_tx_mowner);
970 	MOWNER_ATTACH(&ec->ec_rx_mowner);
971 	ifp->if_mowner = &ec->ec_tx_mowner;
972 #endif
973 }
974 
975 void
976 ether_ifdetach(struct ifnet *ifp)
977 {
978 	struct ethercom *ec = (void *) ifp;
979 	struct ether_multi *enm;
980 	int s;
981 
982 	/*
983 	 * Prevent further calls to ioctl (for example turning off
984 	 * promiscuous mode from the bridge code), which eventually can
985 	 * call if_init() which can cause panics because the interface
986 	 * is in the process of being detached. Return device not configured
987 	 * instead.
988 	 */
989 	ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio;
990 
991 #if NBRIDGE > 0
992 	if (ifp->if_bridge)
993 		bridge_ifdetach(ifp);
994 #endif
995 
996 	bpf_detach(ifp);
997 
998 #if NVLAN > 0
999 	if (ec->ec_nvlans)
1000 		vlan_ifdetach(ifp);
1001 #endif
1002 
1003 	s = splnet();
1004 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
1005 		LIST_REMOVE(enm, enm_list);
1006 		free(enm, M_IFMADDR);
1007 		ec->ec_multicnt--;
1008 	}
1009 	splx(s);
1010 
1011 	ifp->if_mowner = NULL;
1012 	MOWNER_DETACH(&ec->ec_rx_mowner);
1013 	MOWNER_DETACH(&ec->ec_tx_mowner);
1014 }
1015 
1016 #if 0
1017 /*
1018  * This is for reference.  We have a table-driven version
1019  * of the little-endian crc32 generator, which is faster
1020  * than the double-loop.
1021  */
1022 uint32_t
1023 ether_crc32_le(const uint8_t *buf, size_t len)
1024 {
1025 	uint32_t c, crc, carry;
1026 	size_t i, j;
1027 
1028 	crc = 0xffffffffU;	/* initial value */
1029 
1030 	for (i = 0; i < len; i++) {
1031 		c = buf[i];
1032 		for (j = 0; j < 8; j++) {
1033 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1034 			crc >>= 1;
1035 			c >>= 1;
1036 			if (carry)
1037 				crc = (crc ^ ETHER_CRC_POLY_LE);
1038 		}
1039 	}
1040 
1041 	return (crc);
1042 }
1043 #else
1044 uint32_t
1045 ether_crc32_le(const uint8_t *buf, size_t len)
1046 {
1047 	static const uint32_t crctab[] = {
1048 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1049 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1050 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1051 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1052 	};
1053 	uint32_t crc;
1054 	size_t i;
1055 
1056 	crc = 0xffffffffU;	/* initial value */
1057 
1058 	for (i = 0; i < len; i++) {
1059 		crc ^= buf[i];
1060 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1061 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1062 	}
1063 
1064 	return (crc);
1065 }
1066 #endif
1067 
1068 uint32_t
1069 ether_crc32_be(const uint8_t *buf, size_t len)
1070 {
1071 	uint32_t c, crc, carry;
1072 	size_t i, j;
1073 
1074 	crc = 0xffffffffU;	/* initial value */
1075 
1076 	for (i = 0; i < len; i++) {
1077 		c = buf[i];
1078 		for (j = 0; j < 8; j++) {
1079 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1080 			crc <<= 1;
1081 			c >>= 1;
1082 			if (carry)
1083 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1084 		}
1085 	}
1086 
1087 	return (crc);
1088 }
1089 
1090 #ifdef INET
1091 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1092     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1093 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1094     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1095 #endif
1096 #ifdef INET6
1097 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1098     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1099 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1100     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1101 #endif
1102 
1103 /*
1104  * ether_aton implementation, not using a static buffer.
1105  */
1106 int
1107 ether_aton_r(u_char *dest, size_t len, const char *str)
1108 {
1109         const u_char *cp = (const void *)str;
1110 	u_char *ep;
1111 
1112 #define atox(c)	(((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1113 
1114 	if (len < ETHER_ADDR_LEN)
1115 		return ENOSPC;
1116 
1117 	ep = dest + ETHER_ADDR_LEN;
1118 
1119 	while (*cp) {
1120                 if (!isxdigit(*cp))
1121                         return EINVAL;
1122 		*dest = atox(*cp);
1123 		cp++;
1124                 if (isxdigit(*cp)) {
1125                         *dest = (*dest << 4) | atox(*cp);
1126 			dest++;
1127 			cp++;
1128                 } else
1129 			dest++;
1130 		if (dest == ep)
1131 			return *cp == '\0' ? 0 : ENAMETOOLONG;
1132 		switch (*cp) {
1133 		case ':':
1134 		case '-':
1135 		case '.':
1136 			cp++;
1137 			break;
1138 		}
1139         }
1140 	return ENOBUFS;
1141 }
1142 
1143 /*
1144  * Convert a sockaddr into an Ethernet address or range of Ethernet
1145  * addresses.
1146  */
1147 int
1148 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
1149     uint8_t addrhi[ETHER_ADDR_LEN])
1150 {
1151 #ifdef INET
1152 	const struct sockaddr_in *sin;
1153 #endif /* INET */
1154 #ifdef INET6
1155 	const struct sockaddr_in6 *sin6;
1156 #endif /* INET6 */
1157 
1158 	switch (sa->sa_family) {
1159 
1160 	case AF_UNSPEC:
1161 		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
1162 		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1163 		break;
1164 
1165 #ifdef INET
1166 	case AF_INET:
1167 		sin = satocsin(sa);
1168 		if (sin->sin_addr.s_addr == INADDR_ANY) {
1169 			/*
1170 			 * An IP address of INADDR_ANY means listen to
1171 			 * or stop listening to all of the Ethernet
1172 			 * multicast addresses used for IP.
1173 			 * (This is for the sake of IP multicast routers.)
1174 			 */
1175 			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
1176 			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1177 		}
1178 		else {
1179 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1180 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1181 		}
1182 		break;
1183 #endif
1184 #ifdef INET6
1185 	case AF_INET6:
1186 		sin6 = satocsin6(sa);
1187 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1188 			/*
1189 			 * An IP6 address of 0 means listen to or stop
1190 			 * listening to all of the Ethernet multicast
1191 			 * address used for IP6.
1192 			 * (This is used for multicast routers.)
1193 			 */
1194 			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
1195 			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1196 		} else {
1197 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1198 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1199 		}
1200 		break;
1201 #endif
1202 
1203 	default:
1204 		return EAFNOSUPPORT;
1205 	}
1206 	return 0;
1207 }
1208 
1209 /*
1210  * Add an Ethernet multicast address or range of addresses to the list for a
1211  * given interface.
1212  */
1213 int
1214 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1215 {
1216 	struct ether_multi *enm;
1217 	u_char addrlo[ETHER_ADDR_LEN];
1218 	u_char addrhi[ETHER_ADDR_LEN];
1219 	int s = splnet(), error;
1220 
1221 	error = ether_multiaddr(sa, addrlo, addrhi);
1222 	if (error != 0) {
1223 		splx(s);
1224 		return error;
1225 	}
1226 
1227 	/*
1228 	 * Verify that we have valid Ethernet multicast addresses.
1229 	 */
1230 	if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1231 		splx(s);
1232 		return EINVAL;
1233 	}
1234 	/*
1235 	 * See if the address range is already in the list.
1236 	 */
1237 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1238 	if (enm != NULL) {
1239 		/*
1240 		 * Found it; just increment the reference count.
1241 		 */
1242 		++enm->enm_refcount;
1243 		splx(s);
1244 		return 0;
1245 	}
1246 	/*
1247 	 * New address or range; malloc a new multicast record
1248 	 * and link it into the interface's multicast list.
1249 	 */
1250 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1251 	if (enm == NULL) {
1252 		splx(s);
1253 		return ENOBUFS;
1254 	}
1255 	memcpy(enm->enm_addrlo, addrlo, 6);
1256 	memcpy(enm->enm_addrhi, addrhi, 6);
1257 	enm->enm_refcount = 1;
1258 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1259 	ec->ec_multicnt++;
1260 	splx(s);
1261 	/*
1262 	 * Return ENETRESET to inform the driver that the list has changed
1263 	 * and its reception filter should be adjusted accordingly.
1264 	 */
1265 	return ENETRESET;
1266 }
1267 
1268 /*
1269  * Delete a multicast address record.
1270  */
1271 int
1272 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1273 {
1274 	struct ether_multi *enm;
1275 	u_char addrlo[ETHER_ADDR_LEN];
1276 	u_char addrhi[ETHER_ADDR_LEN];
1277 	int s = splnet(), error;
1278 
1279 	error = ether_multiaddr(sa, addrlo, addrhi);
1280 	if (error != 0) {
1281 		splx(s);
1282 		return (error);
1283 	}
1284 
1285 	/*
1286 	 * Look ur the address in our list.
1287 	 */
1288 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1289 	if (enm == NULL) {
1290 		splx(s);
1291 		return (ENXIO);
1292 	}
1293 	if (--enm->enm_refcount != 0) {
1294 		/*
1295 		 * Still some claims to this record.
1296 		 */
1297 		splx(s);
1298 		return (0);
1299 	}
1300 	/*
1301 	 * No remaining claims to this record; unlink and free it.
1302 	 */
1303 	LIST_REMOVE(enm, enm_list);
1304 	free(enm, M_IFMADDR);
1305 	ec->ec_multicnt--;
1306 	splx(s);
1307 	/*
1308 	 * Return ENETRESET to inform the driver that the list has changed
1309 	 * and its reception filter should be adjusted accordingly.
1310 	 */
1311 	return (ENETRESET);
1312 }
1313 
1314 void
1315 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
1316 {
1317 	ec->ec_ifflags_cb = cb;
1318 }
1319 
1320 /*
1321  * Common ioctls for Ethernet interfaces.  Note, we must be
1322  * called at splnet().
1323  */
1324 int
1325 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1326 {
1327 	struct ethercom *ec = (void *) ifp;
1328 	struct eccapreq *eccr;
1329 	struct ifreq *ifr = (struct ifreq *)data;
1330 	struct if_laddrreq *iflr = data;
1331 	const struct sockaddr_dl *sdl;
1332 	static const uint8_t zero[ETHER_ADDR_LEN];
1333 	int error;
1334 
1335 	switch (cmd) {
1336 	case SIOCINITIFADDR:
1337 	    {
1338 		struct ifaddr *ifa = (struct ifaddr *)data;
1339 		if (ifa->ifa_addr->sa_family != AF_LINK
1340 		    && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
1341 		       (IFF_UP|IFF_RUNNING)) {
1342 			ifp->if_flags |= IFF_UP;
1343 			if ((error = (*ifp->if_init)(ifp)) != 0)
1344 				return error;
1345 		}
1346 #ifdef INET
1347 		if (ifa->ifa_addr->sa_family == AF_INET)
1348 			arp_ifinit(ifp, ifa);
1349 #endif /* INET */
1350 		return 0;
1351 	    }
1352 
1353 	case SIOCSIFMTU:
1354 	    {
1355 		int maxmtu;
1356 
1357 		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1358 			maxmtu = ETHERMTU_JUMBO;
1359 		else
1360 			maxmtu = ETHERMTU;
1361 
1362 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1363 			return EINVAL;
1364 		else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
1365 			return error;
1366 		else if (ifp->if_flags & IFF_UP) {
1367 			/* Make sure the device notices the MTU change. */
1368 			return (*ifp->if_init)(ifp);
1369 		} else
1370 			return 0;
1371 	    }
1372 
1373 	case SIOCSIFFLAGS:
1374 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1375 			return error;
1376 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1377 		case IFF_RUNNING:
1378 			/*
1379 			 * If interface is marked down and it is running,
1380 			 * then stop and disable it.
1381 			 */
1382 			(*ifp->if_stop)(ifp, 1);
1383 			break;
1384 		case IFF_UP:
1385 			/*
1386 			 * If interface is marked up and it is stopped, then
1387 			 * start it.
1388 			 */
1389 			return (*ifp->if_init)(ifp);
1390 		case IFF_UP|IFF_RUNNING:
1391 			error = 0;
1392 			if (ec->ec_ifflags_cb == NULL ||
1393 			    (error = (*ec->ec_ifflags_cb)(ec)) == ENETRESET) {
1394 				/*
1395 				 * Reset the interface to pick up
1396 				 * changes in any other flags that
1397 				 * affect the hardware state.
1398 				 */
1399 				return (*ifp->if_init)(ifp);
1400 			} else
1401 				return error;
1402 		case 0:
1403 			break;
1404 		}
1405 		return 0;
1406 	case SIOCGETHERCAP:
1407 		eccr = (struct eccapreq *)data;
1408 		eccr->eccr_capabilities = ec->ec_capabilities;
1409 		eccr->eccr_capenable = ec->ec_capenable;
1410 		return 0;
1411 	case SIOCADDMULTI:
1412 		return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1413 	case SIOCDELMULTI:
1414 		return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1415 	case SIOCSIFMEDIA:
1416 	case SIOCGIFMEDIA:
1417 		if (ec->ec_mii == NULL)
1418 			return ENOTTY;
1419 		return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd);
1420 	case SIOCALIFADDR:
1421 		sdl = satocsdl(sstocsa(&iflr->addr));
1422 		if (sdl->sdl_family != AF_LINK)
1423 			;
1424 		else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
1425 			return EINVAL;
1426 		else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
1427 			return EINVAL;
1428 		/*FALLTHROUGH*/
1429 	default:
1430 		return ifioctl_common(ifp, cmd, data);
1431 	}
1432 	return 0;
1433 }
1434 
1435 /*
1436  * Enable/disable passing VLAN packets if the parent interface supports it.
1437  * Return:
1438  * 	 0: Ok
1439  *	-1: Parent interface does not support vlans
1440  *	>0: Error
1441  */
1442 int
1443 ether_enable_vlan_mtu(struct ifnet *ifp)
1444 {
1445 	int error;
1446 	struct ethercom *ec = (void *)ifp;
1447 
1448 	/* Already have VLAN's do nothing. */
1449 	if (ec->ec_nvlans != 0)
1450 		return 0;
1451 
1452 	/* Parent does not support VLAN's */
1453 	if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
1454 		return -1;
1455 
1456 	/*
1457 	 * Parent supports the VLAN_MTU capability,
1458 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
1459 	 * enable it.
1460 	 */
1461 	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1462 
1463 	/* Interface is down, defer for later */
1464 	if ((ifp->if_flags & IFF_UP) == 0)
1465 		return 0;
1466 
1467 	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1468 		return 0;
1469 
1470 	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1471 	return error;
1472 }
1473 
1474 int
1475 ether_disable_vlan_mtu(struct ifnet *ifp)
1476 {
1477 	int error;
1478 	struct ethercom *ec = (void *)ifp;
1479 
1480 	/* We still have VLAN's, defer for later */
1481 	if (ec->ec_nvlans != 0)
1482 		return 0;
1483 
1484 	/* Parent does not support VLAB's, nothing to do. */
1485 	if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
1486 		return -1;
1487 
1488 	/*
1489 	 * Disable Tx/Rx of VLAN-sized frames.
1490 	 */
1491 	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1492 
1493 	/* Interface is down, defer for later */
1494 	if ((ifp->if_flags & IFF_UP) == 0)
1495 		return 0;
1496 
1497 	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1498 		return 0;
1499 
1500 	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1501 	return error;
1502 }
1503 
1504 static int
1505 ether_multicast_sysctl(SYSCTLFN_ARGS)
1506 {
1507 	struct ether_multi *enm;
1508 	struct ether_multi_sysctl addr;
1509 	struct ifnet *ifp;
1510 	struct ethercom *ec;
1511 	int error = 0;
1512 	size_t written;
1513 	struct psref psref;
1514 	int bound;
1515 
1516 	if (namelen != 1)
1517 		return EINVAL;
1518 
1519 	bound = curlwp_bind();
1520 	ifp = if_get_byindex(name[0], &psref);
1521 	if (ifp == NULL) {
1522 		error = ENODEV;
1523 		goto out;
1524 	}
1525 	if (ifp->if_type != IFT_ETHER) {
1526 		if_put(ifp, &psref);
1527 		*oldlenp = 0;
1528 		goto out;
1529 	}
1530 	ec = (struct ethercom *)ifp;
1531 
1532 	if (oldp == NULL) {
1533 		if_put(ifp, &psref);
1534 		*oldlenp = ec->ec_multicnt * sizeof(addr);
1535 		goto out;
1536 	}
1537 
1538 	memset(&addr, 0, sizeof(addr));
1539 	error = 0;
1540 	written = 0;
1541 
1542 	LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
1543 		if (written + sizeof(addr) > *oldlenp)
1544 			break;
1545 		addr.enm_refcount = enm->enm_refcount;
1546 		memcpy(addr.enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
1547 		memcpy(addr.enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
1548 		error = sysctl_copyout(l, &addr, oldp, sizeof(addr));
1549 		if (error)
1550 			break;
1551 		written += sizeof(addr);
1552 		oldp = (char *)oldp + sizeof(addr);
1553 	}
1554 	if_put(ifp, &psref);
1555 
1556 	*oldlenp = written;
1557 out:
1558 	curlwp_bindx(bound);
1559 	return error;
1560 }
1561 
1562 SYSCTL_SETUP(sysctl_net_ether_setup, "sysctl net.ether subtree setup")
1563 {
1564 	const struct sysctlnode *rnode = NULL;
1565 
1566 	sysctl_createv(clog, 0, NULL, &rnode,
1567 		       CTLFLAG_PERMANENT,
1568 		       CTLTYPE_NODE, "ether",
1569 		       SYSCTL_DESCR("Ethernet-specific information"),
1570 		       NULL, 0, NULL, 0,
1571 		       CTL_NET, CTL_CREATE, CTL_EOL);
1572 
1573 	sysctl_createv(clog, 0, &rnode, NULL,
1574 		       CTLFLAG_PERMANENT,
1575 		       CTLTYPE_NODE, "multicast",
1576 		       SYSCTL_DESCR("multicast addresses"),
1577 		       ether_multicast_sysctl, 0, NULL, 0,
1578 		       CTL_CREATE, CTL_EOL);
1579 }
1580 
1581 void
1582 etherinit(void)
1583 {
1584 	mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
1585 }
1586