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