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