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