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