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