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