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