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