1/*	$NetBSD: if_ethersubr.c,v 1.326 2023/11/02 09:40:47 yamaguchi 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.326 2023/11/02 09:40:47 yamaguchi 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/route.h>
96#include <net/if_llc.h>
97#include <net/if_dl.h>
98#include <net/if_types.h>
99#include <net/pktqueue.h>
100
101#include <net/if_media.h>
102#include <dev/mii/mii.h>
103#include <dev/mii/miivar.h>
104
105#if NARP == 0
106/*
107 * XXX there should really be a way to issue this warning from within config(8)
108 */
109#error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK.
110#endif
111
112#include <net/bpf.h>
113
114#include <net/if_ether.h>
115#include <net/if_vlanvar.h>
116
117#if NPPPOE > 0
118#include <net/if_pppoe.h>
119#endif
120
121#if NAGR > 0
122#include <net/ether_slowprotocols.h>
123#include <net/agr/ieee8023ad.h>
124#include <net/agr/if_agrvar.h>
125#endif
126
127#if NBRIDGE > 0
128#include <net/if_bridgevar.h>
129#endif
130
131#include <netinet/in.h>
132#ifdef INET
133#include <netinet/in_var.h>
134#endif
135#include <netinet/if_inarp.h>
136
137#ifdef INET6
138#ifndef INET
139#include <netinet/in.h>
140#endif
141#include <netinet6/in6_var.h>
142#include <netinet6/nd6.h>
143#endif
144
145#include "carp.h"
146#if NCARP > 0
147#include <netinet/ip_carp.h>
148#endif
149
150#ifdef NETATALK
151#include <netatalk/at.h>
152#include <netatalk/at_var.h>
153#include <netatalk/at_extern.h>
154
155#define llc_snap_org_code llc_un.type_snap.org_code
156#define llc_snap_ether_type llc_un.type_snap.ether_type
157
158extern u_char	at_org_code[3];
159extern u_char	aarp_org_code[3];
160#endif /* NETATALK */
161
162#ifdef MPLS
163#include <netmpls/mpls.h>
164#include <netmpls/mpls_var.h>
165#endif
166
167CTASSERT(sizeof(struct ether_addr) == 6);
168CTASSERT(sizeof(struct ether_header) == 14);
169
170#ifdef DIAGNOSTIC
171static struct timeval bigpktppslim_last;
172static int bigpktppslim = 2;	/* XXX */
173static int bigpktpps_count;
174static kmutex_t bigpktpps_lock __cacheline_aligned;
175#endif
176
177const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
178    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
179const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
180    { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
181#define senderr(e) { error = (e); goto bad;}
182
183static pktq_rps_hash_func_t ether_pktq_rps_hash_p;
184
185static int ether_output(struct ifnet *, struct mbuf *,
186    const struct sockaddr *, const struct rtentry *);
187
188/*
189 * Ethernet output routine.
190 * Encapsulate a packet of type family for the local net.
191 * Assumes that ifp is actually pointer to ethercom structure.
192 */
193static int
194ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
195    const struct sockaddr * const dst, const struct rtentry *rt)
196{
197	uint8_t esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
198	uint16_t etype = 0;
199	int error = 0, hdrcmplt = 0;
200	struct mbuf *m = m0;
201	struct mbuf *mcopy = NULL;
202	struct ether_header *eh;
203	struct ifnet *ifp = ifp0;
204#ifdef INET
205	struct arphdr *ah;
206#endif
207#ifdef NETATALK
208	struct at_ifaddr *aa;
209#endif
210
211#ifdef MBUFTRACE
212	m_claimm(m, ifp->if_mowner);
213#endif
214
215#if NCARP > 0
216	if (ifp->if_type == IFT_CARP) {
217		struct ifaddr *ifa;
218		int s = pserialize_read_enter();
219
220		/* loop back if this is going to the carp interface */
221		if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
222		    (ifa = ifa_ifwithaddr(dst)) != NULL) {
223			if (ifa->ifa_ifp == ifp0) {
224				pserialize_read_exit(s);
225				return looutput(ifp0, m, dst, rt);
226			}
227		}
228		pserialize_read_exit(s);
229
230		ifp = ifp->if_carpdev;
231		/* ac = (struct arpcom *)ifp; */
232
233		if ((ifp0->if_flags & (IFF_UP | IFF_RUNNING)) !=
234		    (IFF_UP | IFF_RUNNING))
235			senderr(ENETDOWN);
236	}
237#endif
238
239	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
240		senderr(ENETDOWN);
241
242	switch (dst->sa_family) {
243
244#ifdef INET
245	case AF_INET:
246		if (m->m_flags & M_BCAST) {
247			memcpy(edst, etherbroadcastaddr, sizeof(edst));
248		} else if (m->m_flags & M_MCAST) {
249			ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
250		} else {
251			error = arpresolve(ifp0, rt, m, dst, edst, sizeof(edst));
252			if (error)
253				return (error == EWOULDBLOCK) ? 0 : error;
254		}
255		/* If broadcasting on a simplex interface, loopback a copy */
256		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
257			mcopy = m_copypacket(m, M_DONTWAIT);
258		etype = htons(ETHERTYPE_IP);
259		break;
260
261	case AF_ARP:
262		ah = mtod(m, struct arphdr *);
263		if (m->m_flags & M_BCAST) {
264			memcpy(edst, etherbroadcastaddr, sizeof(edst));
265		} else {
266			void *tha = ar_tha(ah);
267
268			if (tha == NULL) {
269				/* fake with ARPHRD_IEEE1394 */
270				m_freem(m);
271				return 0;
272			}
273			memcpy(edst, tha, sizeof(edst));
274		}
275
276		ah->ar_hrd = htons(ARPHRD_ETHER);
277
278		switch (ntohs(ah->ar_op)) {
279		case ARPOP_REVREQUEST:
280		case ARPOP_REVREPLY:
281			etype = htons(ETHERTYPE_REVARP);
282			break;
283
284		case ARPOP_REQUEST:
285		case ARPOP_REPLY:
286		default:
287			etype = htons(ETHERTYPE_ARP);
288		}
289		break;
290#endif
291
292#ifdef INET6
293	case AF_INET6:
294		if (m->m_flags & M_BCAST) {
295			memcpy(edst, etherbroadcastaddr, sizeof(edst));
296		} else if (m->m_flags & M_MCAST) {
297			ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
298			    edst);
299		} else {
300			error = nd6_resolve(ifp0, rt, m, dst, edst,
301			    sizeof(edst));
302			if (error)
303				return (error == EWOULDBLOCK) ? 0 : error;
304		}
305		etype = htons(ETHERTYPE_IPV6);
306		break;
307#endif
308
309#ifdef NETATALK
310	case AF_APPLETALK: {
311		struct ifaddr *ifa;
312		int s;
313
314		KERNEL_LOCK(1, NULL);
315
316		if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
317			KERNEL_UNLOCK_ONE(NULL);
318			return 0;
319		}
320
321		/*
322		 * ifaddr is the first thing in at_ifaddr
323		 */
324		s = pserialize_read_enter();
325		ifa = at_ifawithnet((const struct sockaddr_at *)dst, ifp);
326		if (ifa == NULL) {
327			pserialize_read_exit(s);
328			KERNEL_UNLOCK_ONE(NULL);
329			senderr(EADDRNOTAVAIL);
330		}
331		aa = (struct at_ifaddr *)ifa;
332
333		/*
334		 * In the phase 2 case, we need to prepend an mbuf for the
335		 * llc header.
336		 */
337		if (aa->aa_flags & AFA_PHASE2) {
338			struct llc llc;
339
340			M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
341			if (m == NULL) {
342				pserialize_read_exit(s);
343				KERNEL_UNLOCK_ONE(NULL);
344				senderr(ENOBUFS);
345			}
346
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
362	case pseudo_AF_HDRCMPLT:
363		hdrcmplt = 1;
364		memcpy(esrc,
365		    ((const struct ether_header *)dst->sa_data)->ether_shost,
366		    sizeof(esrc));
367		/* FALLTHROUGH */
368
369	case AF_UNSPEC:
370		memcpy(edst,
371		    ((const struct ether_header *)dst->sa_data)->ether_dhost,
372		    sizeof(edst));
373		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
374		etype = ((const struct ether_header *)dst->sa_data)->ether_type;
375		break;
376
377	default:
378		printf("%s: can't handle af%d\n", ifp->if_xname,
379		    dst->sa_family);
380		senderr(EAFNOSUPPORT);
381	}
382
383#ifdef MPLS
384	{
385		struct m_tag *mtag;
386		mtag = m_tag_find(m, PACKET_TAG_MPLS);
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#endif
394
395	if (mcopy)
396		(void)looutput(ifp, mcopy, dst, rt);
397
398	KASSERT((m->m_flags & M_PKTHDR) != 0);
399
400	/*
401	 * If no ether type is set, this must be a 802.2 formatted packet.
402	 */
403	if (etype == 0)
404		etype = htons(m->m_pkthdr.len);
405
406	/*
407	 * Add local net header. If no space in first mbuf, allocate another.
408	 */
409	M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
410	if (m == NULL)
411		senderr(ENOBUFS);
412
413	eh = mtod(m, struct ether_header *);
414	/* Note: etype is already in network byte order. */
415	memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
416	memcpy(eh->ether_dhost, edst, sizeof(edst));
417	if (hdrcmplt) {
418		memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
419	} else {
420	 	memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
421		    sizeof(eh->ether_shost));
422	}
423
424#if NCARP > 0
425	if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
426	 	memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
427		    sizeof(eh->ether_shost));
428	}
429#endif
430
431	if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
432		return error;
433	if (m == NULL)
434		return 0;
435
436#if NBRIDGE > 0
437	/*
438	 * Bridges require special output handling.
439	 */
440	if (ifp->if_bridge)
441		return bridge_output(ifp, m, NULL, NULL);
442#endif
443
444#if NCARP > 0
445	if (ifp != ifp0)
446		if_statadd(ifp0, if_obytes, m->m_pkthdr.len + ETHER_HDR_LEN);
447#endif
448
449#ifdef ALTQ
450	KERNEL_LOCK(1, NULL);
451	/*
452	 * If ALTQ is enabled on the parent interface, do
453	 * classification; the queueing discipline might not
454	 * require classification, but might require the
455	 * address family/header pointer in the pktattr.
456	 */
457	if (ALTQ_IS_ENABLED(&ifp->if_snd))
458		altq_etherclassify(&ifp->if_snd, m);
459	KERNEL_UNLOCK_ONE(NULL);
460#endif
461	return ifq_enqueue(ifp, m);
462
463bad:
464	if_statinc(ifp, if_oerrors);
465	if (m)
466		m_freem(m);
467	return error;
468}
469
470#ifdef ALTQ
471/*
472 * This routine is a slight hack to allow a packet to be classified
473 * if the Ethernet headers are present.  It will go away when ALTQ's
474 * classification engine understands link headers.
475 *
476 * XXX: We may need to do m_pullups here. First to ensure struct ether_header
477 * is indeed contiguous, then to read the LLC and so on.
478 */
479void
480altq_etherclassify(struct ifaltq *ifq, struct mbuf *m)
481{
482	struct ether_header *eh;
483	struct mbuf *mtop = m;
484	uint16_t ether_type;
485	int hlen, af, hdrsize;
486	void *hdr;
487
488	KASSERT((mtop->m_flags & M_PKTHDR) != 0);
489
490	hlen = ETHER_HDR_LEN;
491	eh = mtod(m, struct ether_header *);
492
493	ether_type = htons(eh->ether_type);
494
495	if (ether_type < ETHERMTU) {
496		/* LLC/SNAP */
497		struct llc *llc = (struct llc *)(eh + 1);
498		hlen += 8;
499
500		if (m->m_len < hlen ||
501		    llc->llc_dsap != LLC_SNAP_LSAP ||
502		    llc->llc_ssap != LLC_SNAP_LSAP ||
503		    llc->llc_control != LLC_UI) {
504			/* Not SNAP. */
505			goto bad;
506		}
507
508		ether_type = htons(llc->llc_un.type_snap.ether_type);
509	}
510
511	switch (ether_type) {
512	case ETHERTYPE_IP:
513		af = AF_INET;
514		hdrsize = 20;		/* sizeof(struct ip) */
515		break;
516
517	case ETHERTYPE_IPV6:
518		af = AF_INET6;
519		hdrsize = 40;		/* sizeof(struct ip6_hdr) */
520		break;
521
522	default:
523		af = AF_UNSPEC;
524		hdrsize = 0;
525		break;
526	}
527
528	while (m->m_len <= hlen) {
529		hlen -= m->m_len;
530		m = m->m_next;
531		if (m == NULL)
532			goto bad;
533	}
534
535	if (m->m_len < (hlen + hdrsize)) {
536		/*
537		 * protocol header not in a single mbuf.
538		 * We can't cope with this situation right
539		 * now (but it shouldn't ever happen, really, anyhow).
540		 */
541#ifdef DEBUG
542		printf("altq_etherclassify: headers span multiple mbufs: "
543		    "%d < %d\n", m->m_len, (hlen + hdrsize));
544#endif
545		goto bad;
546	}
547
548	m->m_data += hlen;
549	m->m_len -= hlen;
550
551	hdr = mtod(m, void *);
552
553	if (ALTQ_NEEDS_CLASSIFY(ifq)) {
554		mtop->m_pkthdr.pattr_class =
555		    (*ifq->altq_classify)(ifq->altq_clfier, m, af);
556	}
557	mtop->m_pkthdr.pattr_af = af;
558	mtop->m_pkthdr.pattr_hdr = hdr;
559
560	m->m_data -= hlen;
561	m->m_len += hlen;
562
563	return;
564
565bad:
566	mtop->m_pkthdr.pattr_class = NULL;
567	mtop->m_pkthdr.pattr_hdr = NULL;
568	mtop->m_pkthdr.pattr_af = AF_UNSPEC;
569}
570#endif /* ALTQ */
571
572#if defined (LLC) || defined (NETATALK)
573static void
574ether_input_llc(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh)
575{
576	pktqueue_t *pktq = NULL;
577	struct llc *l;
578
579	if (m->m_len < sizeof(*eh) + sizeof(struct llc))
580		goto error;
581
582	l = (struct llc *)(eh+1);
583	switch (l->llc_dsap) {
584#ifdef NETATALK
585	case LLC_SNAP_LSAP:
586		switch (l->llc_control) {
587		case LLC_UI:
588			if (l->llc_ssap != LLC_SNAP_LSAP)
589				goto error;
590
591			if (memcmp(&(l->llc_snap_org_code)[0],
592			    at_org_code, sizeof(at_org_code)) == 0 &&
593			    ntohs(l->llc_snap_ether_type) ==
594			    ETHERTYPE_ATALK) {
595				pktq = at_pktq2;
596				m_adj(m, sizeof(struct ether_header)
597				    + sizeof(struct llc));
598				break;
599			}
600
601			if (memcmp(&(l->llc_snap_org_code)[0],
602			    aarp_org_code,
603			    sizeof(aarp_org_code)) == 0 &&
604			    ntohs(l->llc_snap_ether_type) ==
605			    ETHERTYPE_AARP) {
606				m_adj(m, sizeof(struct ether_header)
607				    + sizeof(struct llc));
608				aarpinput(ifp, m); /* XXX queue? */
609				return;
610			}
611
612		default:
613			goto error;
614		}
615		break;
616#endif
617	default:
618		goto noproto;
619	}
620
621	KASSERT(pktq != NULL);
622	if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
623		m_freem(m);
624	}
625	return;
626
627noproto:
628	m_freem(m);
629	if_statinc(ifp, if_noproto);
630	return;
631error:
632	m_freem(m);
633	if_statinc(ifp, if_ierrors);
634	return;
635}
636#endif /* defined (LLC) || defined (NETATALK) */
637
638/*
639 * Process a received Ethernet packet;
640 * the packet is in the mbuf chain m with
641 * the ether header.
642 */
643void
644ether_input(struct ifnet *ifp, struct mbuf *m)
645{
646#if NVLAN > 0 || defined(MBUFTRACE)
647	struct ethercom *ec = (struct ethercom *) ifp;
648#endif
649	pktqueue_t *pktq = NULL;
650	uint16_t etype;
651	struct ether_header *eh;
652	size_t ehlen;
653	static int earlypkts;
654
655	/* No RPS for not-IP. */
656	pktq_rps_hash_func_t rps_hash = NULL;
657
658	KASSERT(!cpu_intr_p());
659	KASSERT((m->m_flags & M_PKTHDR) != 0);
660
661	if ((ifp->if_flags & IFF_UP) == 0)
662		goto drop;
663
664#ifdef MBUFTRACE
665	m_claimm(m, &ec->ec_rx_mowner);
666#endif
667
668	if (__predict_false(m->m_len < sizeof(*eh))) {
669		if ((m = m_pullup(m, sizeof(*eh))) == NULL) {
670			if_statinc(ifp, if_ierrors);
671			return;
672		}
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 error;
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 (!vlan_has_tag(m) && etype == ETHERTYPE_VLAN) {
731		m = ether_strip_vlantag(m);
732		if (m == NULL) {
733			if_statinc(ifp, if_ierrors);
734			return;
735		}
736
737		eh = mtod(m, struct ether_header *);
738		etype = ntohs(eh->ether_type);
739		ehlen = sizeof(*eh);
740	}
741
742	if ((m->m_flags & (M_BCAST | M_MCAST | M_PROMISC)) == 0 &&
743	    (ifp->if_flags & IFF_PROMISC) != 0 &&
744	    memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
745	     ETHER_ADDR_LEN) != 0) {
746		m->m_flags |= M_PROMISC;
747	}
748
749	if ((m->m_flags & M_PROMISC) == 0) {
750		if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
751			return;
752		if (m == NULL)
753			return;
754
755		eh = mtod(m, struct ether_header *);
756		etype = ntohs(eh->ether_type);
757	}
758
759	/*
760	 * Processing a logical interfaces that are able
761	 * to configure vlan(4).
762	*/
763#if NAGR > 0
764	if (ifp->if_lagg != NULL &&
765	    __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
766		m->m_flags &= ~M_PROMISC;
767		agr_input(ifp, m);
768		return;
769	}
770#endif
771
772	/*
773	 * VLAN processing.
774	 *
775	 * VLAN provides service delimiting so the frames are
776	 * processed before other handlings. If a VLAN interface
777	 * does not exist to take those frames, they're returned
778	 * to ether_input().
779	 */
780
781	if (vlan_has_tag(m)) {
782		if (EVL_VLANOFTAG(vlan_get_tag(m)) == 0) {
783			if (etype == ETHERTYPE_VLAN ||
784			     etype == ETHERTYPE_QINQ)
785				goto drop;
786
787			/* XXX we should actually use the prio value? */
788			m->m_flags &= ~M_VLANTAG;
789		} else {
790#if NVLAN > 0
791			if (ec->ec_nvlans > 0) {
792				m = vlan_input(ifp, m);
793
794				/* vlan_input() called ether_input() recursively */
795				if (m == NULL)
796					return;
797			}
798#endif
799			/* drop VLAN frames not for this port. */
800			goto noproto;
801		}
802	}
803
804#if NCARP > 0
805	if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
806		/*
807		 * Clear M_PROMISC, in case the packet comes from a
808		 * vlan.
809		 */
810		m->m_flags &= ~M_PROMISC;
811		if (carp_input(m, (uint8_t *)&eh->ether_shost,
812		    (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
813			return;
814	}
815#endif
816
817	/*
818	 * Handle protocols that expect to have the Ethernet header
819	 * (and possibly FCS) intact.
820	 */
821	switch (etype) {
822#if NPPPOE > 0
823	case ETHERTYPE_PPPOEDISC:
824		pppoedisc_input(ifp, m);
825		return;
826
827	case ETHERTYPE_PPPOE:
828		pppoe_input(ifp, m);
829		return;
830#endif
831
832	case ETHERTYPE_SLOWPROTOCOLS: {
833		uint8_t subtype;
834
835		if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype))
836			goto error;
837
838		m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
839		switch (subtype) {
840#if NAGR > 0
841		case SLOWPROTOCOLS_SUBTYPE_LACP:
842			if (ifp->if_lagg != NULL) {
843				ieee8023ad_lacp_input(ifp, m);
844				return;
845			}
846			break;
847
848		case SLOWPROTOCOLS_SUBTYPE_MARKER:
849			if (ifp->if_lagg != NULL) {
850				ieee8023ad_marker_input(ifp, m);
851				return;
852			}
853			break;
854#endif
855
856		default:
857			if (subtype == 0 || subtype > 10) {
858				/* illegal value */
859				goto error;
860			}
861			/* unknown subtype */
862			break;
863		}
864	}
865	/* FALLTHROUGH */
866	default:
867		if (m->m_flags & M_PROMISC)
868			goto drop;
869	}
870
871	/* If the CRC is still on the packet, trim it off. */
872	if (m->m_flags & M_HASFCS) {
873		m_adj(m, -ETHER_CRC_LEN);
874		m->m_flags &= ~M_HASFCS;
875	}
876
877	/* etype represents the size of the payload in this case */
878	if (etype <= ETHERMTU + sizeof(struct ether_header)) {
879		KASSERT(ehlen == sizeof(*eh));
880#if defined (LLC) || defined (NETATALK)
881		ether_input_llc(ifp, m, eh);
882		return;
883#else
884		/* ethertype of 0-1500 is regarded as noproto */
885		goto noproto;
886#endif
887	}
888
889	/* For ARP packets, store the source address so that
890	 * ARP DAD probes can be validated. */
891	if (etype == ETHERTYPE_ARP) {
892		struct m_tag *mtag;
893
894		mtag = m_tag_get(PACKET_TAG_ETHERNET_SRC, ETHER_ADDR_LEN,
895		    M_NOWAIT);
896		if (mtag != NULL) {
897			memcpy(mtag + 1, &eh->ether_shost, ETHER_ADDR_LEN);
898			m_tag_prepend(m, mtag);
899		}
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		rps_hash = atomic_load_relaxed(&ether_pktq_rps_hash_p);
914		break;
915
916	case ETHERTYPE_ARP:
917		pktq = arp_pktq;
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 noproto;
929#ifdef GATEWAY
930		if (ip6flow_fastforward(&m))
931			return;
932#endif
933		pktq = ip6_pktq;
934		rps_hash = atomic_load_relaxed(&ether_pktq_rps_hash_p);
935		break;
936#endif
937
938#ifdef NETATALK
939	case ETHERTYPE_ATALK:
940		pktq = at_pktq1;
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		pktq = mpls_pktq;
951		break;
952#endif
953
954	default:
955		goto noproto;
956	}
957
958	KASSERT(pktq != NULL);
959	const uint32_t h = rps_hash ? pktq_rps_hash(&rps_hash, m) : 0;
960	if (__predict_false(!pktq_enqueue(pktq, m, h))) {
961		m_freem(m);
962	}
963	return;
964
965drop:
966	m_freem(m);
967	if_statinc(ifp, if_iqdrops);
968	return;
969noproto:
970	m_freem(m);
971	if_statinc(ifp, if_noproto);
972	return;
973error:
974	m_freem(m);
975	if_statinc(ifp, if_ierrors);
976	return;
977}
978
979static void
980ether_bpf_mtap(struct bpf_if *bp, struct mbuf *m, u_int direction)
981{
982	struct ether_vlan_header evl;
983	struct m_hdr mh, md;
984
985	KASSERT(bp != NULL);
986
987	if (!vlan_has_tag(m)) {
988		bpf_mtap3(bp, m, direction);
989		return;
990	}
991
992	memcpy(&evl, mtod(m, char *), ETHER_HDR_LEN);
993	evl.evl_proto = evl.evl_encap_proto;
994	evl.evl_encap_proto = htons(ETHERTYPE_VLAN);
995	evl.evl_tag = htons(vlan_get_tag(m));
996
997	md.mh_flags = 0;
998	md.mh_data = m->m_data + ETHER_HDR_LEN;
999	md.mh_len = m->m_len - ETHER_HDR_LEN;
1000	md.mh_next = m->m_next;
1001
1002	mh.mh_flags = 0;
1003	mh.mh_data = (char *)&evl;
1004	mh.mh_len = sizeof(evl);
1005	mh.mh_next = (struct mbuf *)&md;
1006
1007	bpf_mtap3(bp, (struct mbuf *)&mh, direction);
1008}
1009
1010/*
1011 * Convert Ethernet address to printable (loggable) representation.
1012 */
1013char *
1014ether_sprintf(const u_char *ap)
1015{
1016	static char etherbuf[3 * ETHER_ADDR_LEN];
1017	return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
1018}
1019
1020char *
1021ether_snprintf(char *buf, size_t len, const u_char *ap)
1022{
1023	char *cp = buf;
1024	size_t i;
1025
1026	for (i = 0; i < len / 3; i++) {
1027		*cp++ = hexdigits[*ap >> 4];
1028		*cp++ = hexdigits[*ap++ & 0xf];
1029		*cp++ = ':';
1030	}
1031	*--cp = '\0';
1032	return buf;
1033}
1034
1035/*
1036 * Perform common duties while attaching to interface list
1037 */
1038void
1039ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
1040{
1041	struct ethercom *ec = (struct ethercom *)ifp;
1042	char xnamebuf[HOOKNAMSIZ];
1043
1044	if (lla != NULL && ETHER_IS_MULTICAST(lla))
1045		aprint_error("The multicast bit is set in the MAC address. "
1046			"It's wrong.\n");
1047
1048	ifp->if_type = IFT_ETHER;
1049	ifp->if_hdrlen = ETHER_HDR_LEN;
1050	ifp->if_dlt = DLT_EN10MB;
1051	ifp->if_mtu = ETHERMTU;
1052	ifp->if_output = ether_output;
1053	ifp->_if_input = ether_input;
1054	if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING)
1055		ifp->if_bpf_mtap = ether_bpf_mtap;
1056	if (ifp->if_baudrate == 0)
1057		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
1058
1059	if (lla != NULL)
1060		if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
1061
1062	LIST_INIT(&ec->ec_multiaddrs);
1063	SIMPLEQ_INIT(&ec->ec_vids);
1064	ec->ec_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
1065	ec->ec_flags = 0;
1066	ifp->if_broadcastaddr = etherbroadcastaddr;
1067	bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1068	snprintf(xnamebuf, sizeof(xnamebuf),
1069	    "%s-ether_ifdetachhooks", ifp->if_xname);
1070	ec->ec_ifdetach_hooks = simplehook_create(IPL_NET, xnamebuf);
1071#ifdef MBUFTRACE
1072	mowner_init_owner(&ec->ec_tx_mowner, ifp->if_xname, "tx");
1073	mowner_init_owner(&ec->ec_rx_mowner, ifp->if_xname, "rx");
1074	MOWNER_ATTACH(&ec->ec_tx_mowner);
1075	MOWNER_ATTACH(&ec->ec_rx_mowner);
1076	ifp->if_mowner = &ec->ec_tx_mowner;
1077#endif
1078}
1079
1080void
1081ether_ifdetach(struct ifnet *ifp)
1082{
1083	struct ethercom *ec = (void *) ifp;
1084	struct ether_multi *enm;
1085
1086	IFNET_ASSERT_UNLOCKED(ifp);
1087	/*
1088	 * Prevent further calls to ioctl (for example turning off
1089	 * promiscuous mode from the bridge code), which eventually can
1090	 * call if_init() which can cause panics because the interface
1091	 * is in the process of being detached. Return device not configured
1092	 * instead.
1093	 */
1094	ifp->if_ioctl = __FPTRCAST(int (*)(struct ifnet *, u_long, void *),
1095	    enxio);
1096
1097	simplehook_dohooks(ec->ec_ifdetach_hooks);
1098	KASSERT(!simplehook_has_hooks(ec->ec_ifdetach_hooks));
1099	simplehook_destroy(ec->ec_ifdetach_hooks);
1100
1101	bpf_detach(ifp);
1102
1103	ETHER_LOCK(ec);
1104	KASSERT(ec->ec_nvlans == 0);
1105	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
1106		LIST_REMOVE(enm, enm_list);
1107		kmem_free(enm, sizeof(*enm));
1108		ec->ec_multicnt--;
1109	}
1110	ETHER_UNLOCK(ec);
1111
1112	mutex_obj_free(ec->ec_lock);
1113	ec->ec_lock = NULL;
1114
1115	ifp->if_mowner = NULL;
1116	MOWNER_DETACH(&ec->ec_rx_mowner);
1117	MOWNER_DETACH(&ec->ec_tx_mowner);
1118}
1119
1120void *
1121ether_ifdetachhook_establish(struct ifnet *ifp,
1122    void (*fn)(void *), void *arg)
1123{
1124	struct ethercom *ec;
1125	khook_t *hk;
1126
1127	if (ifp->if_type != IFT_ETHER)
1128		return NULL;
1129
1130	ec = (struct ethercom *)ifp;
1131	hk = simplehook_establish(ec->ec_ifdetach_hooks,
1132	    fn, arg);
1133
1134	return (void *)hk;
1135}
1136
1137void
1138ether_ifdetachhook_disestablish(struct ifnet *ifp,
1139    void *vhook, kmutex_t *lock)
1140{
1141	struct ethercom *ec;
1142
1143	if (vhook == NULL)
1144		return;
1145
1146	ec = (struct ethercom *)ifp;
1147	simplehook_disestablish(ec->ec_ifdetach_hooks, vhook, lock);
1148}
1149
1150#if 0
1151/*
1152 * This is for reference.  We have a table-driven version
1153 * of the little-endian crc32 generator, which is faster
1154 * than the double-loop.
1155 */
1156uint32_t
1157ether_crc32_le(const uint8_t *buf, size_t len)
1158{
1159	uint32_t c, crc, carry;
1160	size_t i, j;
1161
1162	crc = 0xffffffffU;	/* initial value */
1163
1164	for (i = 0; i < len; i++) {
1165		c = buf[i];
1166		for (j = 0; j < 8; j++) {
1167			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1168			crc >>= 1;
1169			c >>= 1;
1170			if (carry)
1171				crc = (crc ^ ETHER_CRC_POLY_LE);
1172		}
1173	}
1174
1175	return (crc);
1176}
1177#else
1178uint32_t
1179ether_crc32_le(const uint8_t *buf, size_t len)
1180{
1181	static const uint32_t crctab[] = {
1182		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1183		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1184		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1185		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1186	};
1187	uint32_t crc;
1188	size_t i;
1189
1190	crc = 0xffffffffU;	/* initial value */
1191
1192	for (i = 0; i < len; i++) {
1193		crc ^= buf[i];
1194		crc = (crc >> 4) ^ crctab[crc & 0xf];
1195		crc = (crc >> 4) ^ crctab[crc & 0xf];
1196	}
1197
1198	return (crc);
1199}
1200#endif
1201
1202uint32_t
1203ether_crc32_be(const uint8_t *buf, size_t len)
1204{
1205	uint32_t c, crc, carry;
1206	size_t i, j;
1207
1208	crc = 0xffffffffU;	/* initial value */
1209
1210	for (i = 0; i < len; i++) {
1211		c = buf[i];
1212		for (j = 0; j < 8; j++) {
1213			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1214			crc <<= 1;
1215			c >>= 1;
1216			if (carry)
1217				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1218		}
1219	}
1220
1221	return (crc);
1222}
1223
1224#ifdef INET
1225const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1226    { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1227const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1228    { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1229#endif
1230#ifdef INET6
1231const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1232    { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1233const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1234    { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1235#endif
1236
1237/*
1238 * ether_aton implementation, not using a static buffer.
1239 */
1240int
1241ether_aton_r(u_char *dest, size_t len, const char *str)
1242{
1243	const u_char *cp = (const void *)str;
1244	u_char *ep;
1245
1246#define atox(c)	(((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1247
1248	if (len < ETHER_ADDR_LEN)
1249		return ENOSPC;
1250
1251	ep = dest + ETHER_ADDR_LEN;
1252
1253	while (*cp) {
1254		if (!isxdigit(*cp))
1255			return EINVAL;
1256
1257		*dest = atox(*cp);
1258		cp++;
1259		if (isxdigit(*cp)) {
1260			*dest = (*dest << 4) | atox(*cp);
1261			cp++;
1262		}
1263		dest++;
1264
1265		if (dest == ep)
1266			return (*cp == '\0') ? 0 : ENAMETOOLONG;
1267
1268		switch (*cp) {
1269		case ':':
1270		case '-':
1271		case '.':
1272			cp++;
1273			break;
1274		}
1275	}
1276	return ENOBUFS;
1277}
1278
1279/*
1280 * Convert a sockaddr into an Ethernet address or range of Ethernet
1281 * addresses.
1282 */
1283int
1284ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
1285    uint8_t addrhi[ETHER_ADDR_LEN])
1286{
1287#ifdef INET
1288	const struct sockaddr_in *sin;
1289#endif
1290#ifdef INET6
1291	const struct sockaddr_in6 *sin6;
1292#endif
1293
1294	switch (sa->sa_family) {
1295
1296	case AF_UNSPEC:
1297		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
1298		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1299		break;
1300
1301#ifdef INET
1302	case AF_INET:
1303		sin = satocsin(sa);
1304		if (sin->sin_addr.s_addr == INADDR_ANY) {
1305			/*
1306			 * An IP address of INADDR_ANY means listen to
1307			 * or stop listening to all of the Ethernet
1308			 * multicast addresses used for IP.
1309			 * (This is for the sake of IP multicast routers.)
1310			 */
1311			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
1312			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1313		} else {
1314			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1315			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1316		}
1317		break;
1318#endif
1319#ifdef INET6
1320	case AF_INET6:
1321		sin6 = satocsin6(sa);
1322		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1323			/*
1324			 * An IP6 address of 0 means listen to or stop
1325			 * listening to all of the Ethernet multicast
1326			 * address used for IP6.
1327			 * (This is used for multicast routers.)
1328			 */
1329			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
1330			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1331		} else {
1332			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1333			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1334		}
1335		break;
1336#endif
1337
1338	default:
1339		return EAFNOSUPPORT;
1340	}
1341	return 0;
1342}
1343
1344/*
1345 * Add an Ethernet multicast address or range of addresses to the list for a
1346 * given interface.
1347 */
1348int
1349ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1350{
1351	struct ether_multi *enm, *_enm;
1352	u_char addrlo[ETHER_ADDR_LEN];
1353	u_char addrhi[ETHER_ADDR_LEN];
1354	int error = 0;
1355
1356	/* Allocate out of lock */
1357	enm = kmem_alloc(sizeof(*enm), KM_SLEEP);
1358
1359	ETHER_LOCK(ec);
1360	error = ether_multiaddr(sa, addrlo, addrhi);
1361	if (error != 0)
1362		goto out;
1363
1364	/*
1365	 * Verify that we have valid Ethernet multicast addresses.
1366	 */
1367	if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1368		error = EINVAL;
1369		goto out;
1370	}
1371
1372	/*
1373	 * See if the address range is already in the list.
1374	 */
1375	_enm = ether_lookup_multi(addrlo, addrhi, ec);
1376	if (_enm != NULL) {
1377		/*
1378		 * Found it; just increment the reference count.
1379		 */
1380		++_enm->enm_refcount;
1381		error = 0;
1382		goto out;
1383	}
1384
1385	/*
1386	 * Link a new multicast record into the interface's multicast list.
1387	 */
1388	memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
1389	memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
1390	enm->enm_refcount = 1;
1391	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1392	ec->ec_multicnt++;
1393
1394	/*
1395	 * Return ENETRESET to inform the driver that the list has changed
1396	 * and its reception filter should be adjusted accordingly.
1397	 */
1398	error = ENETRESET;
1399	enm = NULL;
1400
1401out:
1402	ETHER_UNLOCK(ec);
1403	if (enm != NULL)
1404		kmem_free(enm, sizeof(*enm));
1405	return error;
1406}
1407
1408/*
1409 * Delete a multicast address record.
1410 */
1411int
1412ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1413{
1414	struct ether_multi *enm;
1415	u_char addrlo[ETHER_ADDR_LEN];
1416	u_char addrhi[ETHER_ADDR_LEN];
1417	int error;
1418
1419	ETHER_LOCK(ec);
1420	error = ether_multiaddr(sa, addrlo, addrhi);
1421	if (error != 0)
1422		goto error;
1423
1424	/*
1425	 * Look up the address in our list.
1426	 */
1427	enm = ether_lookup_multi(addrlo, addrhi, ec);
1428	if (enm == NULL) {
1429		error = ENXIO;
1430		goto error;
1431	}
1432	if (--enm->enm_refcount != 0) {
1433		/*
1434		 * Still some claims to this record.
1435		 */
1436		error = 0;
1437		goto error;
1438	}
1439
1440	/*
1441	 * No remaining claims to this record; unlink and free it.
1442	 */
1443	LIST_REMOVE(enm, enm_list);
1444	ec->ec_multicnt--;
1445	ETHER_UNLOCK(ec);
1446	kmem_free(enm, sizeof(*enm));
1447
1448	/*
1449	 * Return ENETRESET to inform the driver that the list has changed
1450	 * and its reception filter should be adjusted accordingly.
1451	 */
1452	return ENETRESET;
1453
1454error:
1455	ETHER_UNLOCK(ec);
1456	return error;
1457}
1458
1459void
1460ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
1461{
1462	ec->ec_ifflags_cb = cb;
1463}
1464
1465void
1466ether_set_vlan_cb(struct ethercom *ec, ether_vlancb_t cb)
1467{
1468
1469	ec->ec_vlan_cb = cb;
1470}
1471
1472static int
1473ether_ioctl_reinit(struct ethercom *ec)
1474{
1475	struct ifnet *ifp = &ec->ec_if;
1476	int error;
1477
1478	KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
1479
1480	switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1481	case IFF_RUNNING:
1482		/*
1483		 * If interface is marked down and it is running,
1484		 * then stop and disable it.
1485		 */
1486		if_stop(ifp, 1);
1487		break;
1488	case IFF_UP:
1489		/*
1490		 * If interface is marked up and it is stopped, then
1491		 * start it.
1492		 */
1493		return if_init(ifp);
1494	case IFF_UP | IFF_RUNNING:
1495		error = 0;
1496		if (ec->ec_ifflags_cb != NULL) {
1497			error = (*ec->ec_ifflags_cb)(ec);
1498			if (error == ENETRESET) {
1499				/*
1500				 * Reset the interface to pick up
1501				 * changes in any other flags that
1502				 * affect the hardware state.
1503				 */
1504				return if_init(ifp);
1505			}
1506		} else
1507			error = if_init(ifp);
1508		return error;
1509	case 0:
1510		break;
1511	}
1512
1513	return 0;
1514}
1515
1516/*
1517 * Common ioctls for Ethernet interfaces.  Note, we must be
1518 * called at splnet().
1519 */
1520int
1521ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1522{
1523	struct ethercom *ec = (void *)ifp;
1524	struct eccapreq *eccr;
1525	struct ifreq *ifr = (struct ifreq *)data;
1526	struct if_laddrreq *iflr = data;
1527	const struct sockaddr_dl *sdl;
1528	static const uint8_t zero[ETHER_ADDR_LEN];
1529	int error;
1530
1531	switch (cmd) {
1532	case SIOCINITIFADDR:
1533	    {
1534		struct ifaddr *ifa = (struct ifaddr *)data;
1535		if (ifa->ifa_addr->sa_family != AF_LINK
1536		    && (ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
1537		       (IFF_UP | IFF_RUNNING)) {
1538			ifp->if_flags |= IFF_UP;
1539			if ((error = if_init(ifp)) != 0)
1540				return error;
1541		}
1542#ifdef INET
1543		if (ifa->ifa_addr->sa_family == AF_INET)
1544			arp_ifinit(ifp, ifa);
1545#endif
1546		return 0;
1547	    }
1548
1549	case SIOCSIFMTU:
1550	    {
1551		int maxmtu;
1552
1553		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1554			maxmtu = ETHERMTU_JUMBO;
1555		else
1556			maxmtu = ETHERMTU;
1557
1558		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1559			return EINVAL;
1560		else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
1561			return error;
1562		else if (ifp->if_flags & IFF_UP) {
1563			/* Make sure the device notices the MTU change. */
1564			return if_init(ifp);
1565		} else
1566			return 0;
1567	    }
1568
1569	case SIOCSIFFLAGS:
1570		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1571			return error;
1572		return ether_ioctl_reinit(ec);
1573	case SIOCGIFFLAGS:
1574		error = ifioctl_common(ifp, cmd, data);
1575		if (error == 0) {
1576			/* Set IFF_ALLMULTI for backcompat */
1577			ifr->ifr_flags |= (ec->ec_flags & ETHER_F_ALLMULTI) ?
1578			    IFF_ALLMULTI : 0;
1579		}
1580		return error;
1581	case SIOCGETHERCAP:
1582		eccr = (struct eccapreq *)data;
1583		eccr->eccr_capabilities = ec->ec_capabilities;
1584		eccr->eccr_capenable = ec->ec_capenable;
1585		return 0;
1586	case SIOCSETHERCAP:
1587		eccr = (struct eccapreq *)data;
1588		if ((eccr->eccr_capenable & ~ec->ec_capabilities) != 0)
1589			return EINVAL;
1590		if (eccr->eccr_capenable == ec->ec_capenable)
1591			return 0;
1592#if 0 /* notyet */
1593		ec->ec_capenable = (ec->ec_capenable & ETHERCAP_CANTCHANGE)
1594		    | (eccr->eccr_capenable & ~ETHERCAP_CANTCHANGE);
1595#else
1596		ec->ec_capenable = eccr->eccr_capenable;
1597#endif
1598		return ether_ioctl_reinit(ec);
1599	case SIOCADDMULTI:
1600		return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1601	case SIOCDELMULTI:
1602		return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1603	case SIOCSIFMEDIA:
1604	case SIOCGIFMEDIA:
1605		if (ec->ec_mii != NULL)
1606			return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media,
1607			    cmd);
1608		else if (ec->ec_ifmedia != NULL)
1609			return ifmedia_ioctl(ifp, ifr, ec->ec_ifmedia, cmd);
1610		else
1611			return ENOTTY;
1612		break;
1613	case SIOCALIFADDR:
1614		sdl = satocsdl(sstocsa(&iflr->addr));
1615		if (sdl->sdl_family != AF_LINK)
1616			;
1617		else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
1618			return EINVAL;
1619		else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
1620			return EINVAL;
1621		/*FALLTHROUGH*/
1622	default:
1623		return ifioctl_common(ifp, cmd, data);
1624	}
1625	return 0;
1626}
1627
1628/*
1629 * Enable/disable passing VLAN packets if the parent interface supports it.
1630 * Return:
1631 * 	 0: Ok
1632 *	-1: Parent interface does not support vlans
1633 *	>0: Error
1634 */
1635int
1636ether_enable_vlan_mtu(struct ifnet *ifp)
1637{
1638	int error;
1639	struct ethercom *ec = (void *)ifp;
1640
1641	/* Parent does not support VLAN's */
1642	if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
1643		return -1;
1644
1645	/*
1646	 * Parent supports the VLAN_MTU capability,
1647	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
1648	 * enable it.
1649	 */
1650	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1651
1652	/* Interface is down, defer for later */
1653	if ((ifp->if_flags & IFF_UP) == 0)
1654		return 0;
1655
1656	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1657		return 0;
1658
1659	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1660	return error;
1661}
1662
1663int
1664ether_disable_vlan_mtu(struct ifnet *ifp)
1665{
1666	int error;
1667	struct ethercom *ec = (void *)ifp;
1668
1669	/* We still have VLAN's, defer for later */
1670	if (ec->ec_nvlans != 0)
1671		return 0;
1672
1673	/* Parent does not support VLAB's, nothing to do. */
1674	if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
1675		return -1;
1676
1677	/*
1678	 * Disable Tx/Rx of VLAN-sized frames.
1679	 */
1680	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1681
1682	/* Interface is down, defer for later */
1683	if ((ifp->if_flags & IFF_UP) == 0)
1684		return 0;
1685
1686	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1687		return 0;
1688
1689	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1690	return error;
1691}
1692
1693/*
1694 * Add and delete VLAN TAG
1695 */
1696int
1697ether_add_vlantag(struct ifnet *ifp, uint16_t vtag, bool *vlanmtu_status)
1698{
1699	struct ethercom *ec = (void *)ifp;
1700	struct vlanid_list *vidp;
1701	bool vlanmtu_enabled;
1702	uint16_t vid = EVL_VLANOFTAG(vtag);
1703	int error;
1704
1705	vlanmtu_enabled = false;
1706
1707	/* Add a vid to the list */
1708	vidp = kmem_alloc(sizeof(*vidp), KM_SLEEP);
1709	vidp->vid = vid;
1710
1711	ETHER_LOCK(ec);
1712	ec->ec_nvlans++;
1713	SIMPLEQ_INSERT_TAIL(&ec->ec_vids, vidp, vid_list);
1714	ETHER_UNLOCK(ec);
1715
1716	if (ec->ec_nvlans == 1) {
1717		IFNET_LOCK(ifp);
1718		error = ether_enable_vlan_mtu(ifp);
1719		IFNET_UNLOCK(ifp);
1720
1721		if (error == 0) {
1722			vlanmtu_enabled = true;
1723		} else if (error != -1) {
1724			goto fail;
1725		}
1726	}
1727
1728	if (ec->ec_vlan_cb != NULL) {
1729		error = (*ec->ec_vlan_cb)(ec, vid, true);
1730		if (error != 0)
1731			goto fail;
1732	}
1733
1734	if (vlanmtu_status != NULL)
1735		*vlanmtu_status = vlanmtu_enabled;
1736
1737	return 0;
1738fail:
1739	ETHER_LOCK(ec);
1740	ec->ec_nvlans--;
1741	SIMPLEQ_REMOVE(&ec->ec_vids, vidp, vlanid_list, vid_list);
1742	ETHER_UNLOCK(ec);
1743
1744	if (vlanmtu_enabled) {
1745		IFNET_LOCK(ifp);
1746		(void)ether_disable_vlan_mtu(ifp);
1747		IFNET_UNLOCK(ifp);
1748	}
1749
1750	kmem_free(vidp, sizeof(*vidp));
1751
1752	return error;
1753}
1754
1755int
1756ether_del_vlantag(struct ifnet *ifp, uint16_t vtag)
1757{
1758	struct ethercom *ec = (void *)ifp;
1759	struct vlanid_list *vidp;
1760	uint16_t vid = EVL_VLANOFTAG(vtag);
1761
1762	ETHER_LOCK(ec);
1763	SIMPLEQ_FOREACH(vidp, &ec->ec_vids, vid_list) {
1764		if (vidp->vid == vid) {
1765			SIMPLEQ_REMOVE(&ec->ec_vids, vidp,
1766			    vlanid_list, vid_list);
1767			ec->ec_nvlans--;
1768			break;
1769		}
1770	}
1771	ETHER_UNLOCK(ec);
1772
1773	if (vidp == NULL)
1774		return ENOENT;
1775
1776	if (ec->ec_vlan_cb != NULL) {
1777		(void)(*ec->ec_vlan_cb)(ec, vidp->vid, false);
1778	}
1779
1780	if (ec->ec_nvlans == 0) {
1781		IFNET_LOCK(ifp);
1782		(void)ether_disable_vlan_mtu(ifp);
1783		IFNET_UNLOCK(ifp);
1784	}
1785
1786	kmem_free(vidp, sizeof(*vidp));
1787
1788	return 0;
1789}
1790
1791int
1792ether_inject_vlantag(struct mbuf **mp, uint16_t etype, uint16_t tag)
1793{
1794	static const size_t min_data_len =
1795	    ETHER_MIN_LEN - ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
1796	/* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
1797	static const char vlan_zero_pad_buff[ETHER_MIN_LEN] = { 0 };
1798
1799	struct ether_vlan_header *evl;
1800	struct mbuf *m = *mp;
1801	int error;
1802
1803	error = 0;
1804
1805	M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
1806	if (m == NULL) {
1807		error = ENOBUFS;
1808		goto out;
1809	}
1810
1811	if (m->m_len < sizeof(*evl)) {
1812		m = m_pullup(m, sizeof(*evl));
1813		if (m == NULL) {
1814			error = ENOBUFS;
1815			goto out;
1816		}
1817	}
1818
1819	/*
1820	 * Transform the Ethernet header into an
1821	 * Ethernet header with 802.1Q encapsulation.
1822	 */
1823	memmove(mtod(m, void *),
1824	    mtod(m, char *) + ETHER_VLAN_ENCAP_LEN,
1825	    sizeof(struct ether_header));
1826	evl = mtod(m, struct ether_vlan_header *);
1827	evl->evl_proto = evl->evl_encap_proto;
1828	evl->evl_encap_proto = htons(etype);
1829	evl->evl_tag = htons(tag);
1830
1831	/*
1832	 * To cater for VLAN-aware layer 2 ethernet
1833	 * switches which may need to strip the tag
1834	 * before forwarding the packet, make sure
1835	 * the packet+tag is at least 68 bytes long.
1836	 * This is necessary because our parent will
1837	 * only pad to 64 bytes (ETHER_MIN_LEN) and
1838	 * some switches will not pad by themselves
1839	 * after deleting a tag.
1840	 */
1841	if (m->m_pkthdr.len < min_data_len) {
1842		m_copyback(m, m->m_pkthdr.len,
1843		    min_data_len - m->m_pkthdr.len,
1844		    vlan_zero_pad_buff);
1845	}
1846
1847	m->m_flags &= ~M_VLANTAG;
1848
1849out:
1850	*mp = m;
1851	return error;
1852}
1853
1854struct mbuf *
1855ether_strip_vlantag(struct mbuf *m)
1856{
1857	struct ether_vlan_header *evl;
1858
1859	if (m->m_len < sizeof(*evl) &&
1860	    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1861		return NULL;
1862	}
1863
1864	if (m_makewritable(&m, 0, sizeof(*evl), M_DONTWAIT)) {
1865		m_freem(m);
1866		return NULL;
1867	}
1868
1869	evl = mtod(m, struct ether_vlan_header *);
1870	KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
1871
1872	vlan_set_tag(m, ntohs(evl->evl_tag));
1873
1874	/*
1875	 * Restore the original ethertype.  We'll remove
1876	 * the encapsulation after we've found the vlan
1877	 * interface corresponding to the tag.
1878	 */
1879	evl->evl_encap_proto = evl->evl_proto;
1880
1881	/*
1882	 * Remove the encapsulation header and append tag.
1883	 * The original header has already been fixed up above.
1884	 */
1885	vlan_set_tag(m, ntohs(evl->evl_tag));
1886	memmove((char *)evl + ETHER_VLAN_ENCAP_LEN, evl,
1887	    offsetof(struct ether_vlan_header, evl_encap_proto));
1888	m_adj(m, ETHER_VLAN_ENCAP_LEN);
1889
1890	return m;
1891}
1892
1893static int
1894ether_multicast_sysctl(SYSCTLFN_ARGS)
1895{
1896	struct ether_multi *enm;
1897	struct ifnet *ifp;
1898	struct ethercom *ec;
1899	int error = 0;
1900	size_t written;
1901	struct psref psref;
1902	int bound;
1903	unsigned int multicnt;
1904	struct ether_multi_sysctl *addrs;
1905	int i;
1906
1907	if (namelen != 1)
1908		return EINVAL;
1909
1910	bound = curlwp_bind();
1911	ifp = if_get_byindex(name[0], &psref);
1912	if (ifp == NULL) {
1913		error = ENODEV;
1914		goto out;
1915	}
1916	if (ifp->if_type != IFT_ETHER) {
1917		if_put(ifp, &psref);
1918		*oldlenp = 0;
1919		goto out;
1920	}
1921	ec = (struct ethercom *)ifp;
1922
1923	if (oldp == NULL) {
1924		if_put(ifp, &psref);
1925		*oldlenp = ec->ec_multicnt * sizeof(*addrs);
1926		goto out;
1927	}
1928
1929	/*
1930	 * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which
1931	 * is sleepable, while holding it. Copy data to a local buffer first
1932	 * with the lock taken and then call sysctl_copyout without holding it.
1933	 */
1934retry:
1935	multicnt = ec->ec_multicnt;
1936
1937	if (multicnt == 0) {
1938		if_put(ifp, &psref);
1939		*oldlenp = 0;
1940		goto out;
1941	}
1942
1943	addrs = kmem_zalloc(sizeof(*addrs) * multicnt, KM_SLEEP);
1944
1945	ETHER_LOCK(ec);
1946	if (multicnt != ec->ec_multicnt) {
1947		/* The number of multicast addresses has changed */
1948		ETHER_UNLOCK(ec);
1949		kmem_free(addrs, sizeof(*addrs) * multicnt);
1950		goto retry;
1951	}
1952
1953	i = 0;
1954	LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
1955		struct ether_multi_sysctl *addr = &addrs[i];
1956		addr->enm_refcount = enm->enm_refcount;
1957		memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
1958		memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
1959		i++;
1960	}
1961	ETHER_UNLOCK(ec);
1962
1963	error = 0;
1964	written = 0;
1965	for (i = 0; i < multicnt; i++) {
1966		struct ether_multi_sysctl *addr = &addrs[i];
1967
1968		if (written + sizeof(*addr) > *oldlenp)
1969			break;
1970		error = sysctl_copyout(l, addr, oldp, sizeof(*addr));
1971		if (error)
1972			break;
1973		written += sizeof(*addr);
1974		oldp = (char *)oldp + sizeof(*addr);
1975	}
1976	kmem_free(addrs, sizeof(*addrs) * multicnt);
1977
1978	if_put(ifp, &psref);
1979
1980	*oldlenp = written;
1981out:
1982	curlwp_bindx(bound);
1983	return error;
1984}
1985
1986static void
1987ether_sysctl_setup(struct sysctllog **clog)
1988{
1989	const struct sysctlnode *rnode = NULL;
1990
1991	sysctl_createv(clog, 0, NULL, &rnode,
1992		       CTLFLAG_PERMANENT,
1993		       CTLTYPE_NODE, "ether",
1994		       SYSCTL_DESCR("Ethernet-specific information"),
1995		       NULL, 0, NULL, 0,
1996		       CTL_NET, CTL_CREATE, CTL_EOL);
1997
1998	sysctl_createv(clog, 0, &rnode, NULL,
1999		       CTLFLAG_PERMANENT,
2000		       CTLTYPE_NODE, "multicast",
2001		       SYSCTL_DESCR("multicast addresses"),
2002		       ether_multicast_sysctl, 0, NULL, 0,
2003		       CTL_CREATE, CTL_EOL);
2004
2005	sysctl_createv(clog, 0, &rnode, NULL,
2006		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
2007		       CTLTYPE_STRING, "rps_hash",
2008		       SYSCTL_DESCR("Interface rps hash function control"),
2009		       sysctl_pktq_rps_hash_handler, 0, (void *)&ether_pktq_rps_hash_p,
2010		       PKTQ_RPS_HASH_NAME_LEN,
2011		       CTL_CREATE, CTL_EOL);
2012}
2013
2014void
2015etherinit(void)
2016{
2017
2018#ifdef DIAGNOSTIC
2019	mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
2020#endif
2021	ether_pktq_rps_hash_p = pktq_rps_hash_default;
2022	ether_sysctl_setup(NULL);
2023}
2024