xref: /netbsd-src/sys/net/if_ethersubr.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: if_ethersubr.c,v 1.156 2007/10/08 16:18:04 ad 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.156 2007/10/08 16:18:04 ad 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 #include <sys/cpu.h>
94 #include <sys/intr.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 
175 extern u_char	at_org_code[3];
176 extern u_char	aarp_org_code[3];
177 #endif /* NETATALK */
178 
179 static struct timeval bigpktppslim_last;
180 static int bigpktppslim = 2;	/* XXX */
181 static int bigpktpps_count;
182 
183 
184 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
185     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
186 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
187     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
188 #define senderr(e) { error = (e); goto bad;}
189 
190 static	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  */
198 static int
199 ether_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 		const struct sockaddr_dl *sdl;
381 
382 		if (rt && (sdl = satocsdl(rt->rt_gateway)) &&
383 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
384 			memcpy(edst, CLLADDR(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, CLLADDR(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, CLLADDR(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, CLLADDR(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 
508 bad:
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  */
520 void
521 altq_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  */
612 void
613 ether_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(CLLADDR(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(CLLADDR(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 		softint_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 					    CLLADDR(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  */
1036 char *
1037 ether_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 
1044 char *
1045 ether_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  */
1062 void
1063 ether_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 	(void)sockaddr_dl_setaddr(ifp->if_sadl, ifp->if_sadl->sdl_len,
1079 	    lla, ifp->if_addrlen);
1080 
1081 	LIST_INIT(&ec->ec_multiaddrs);
1082 	ifp->if_broadcastaddr = etherbroadcastaddr;
1083 #if NBPFILTER > 0
1084 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1085 #endif
1086 #ifdef MBUFTRACE
1087 	strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
1088 	    sizeof(ec->ec_tx_mowner.mo_name));
1089 	strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
1090 	    sizeof(ec->ec_tx_mowner.mo_descr));
1091 	strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
1092 	    sizeof(ec->ec_rx_mowner.mo_name));
1093 	strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
1094 	    sizeof(ec->ec_rx_mowner.mo_descr));
1095 	MOWNER_ATTACH(&ec->ec_tx_mowner);
1096 	MOWNER_ATTACH(&ec->ec_rx_mowner);
1097 	ifp->if_mowner = &ec->ec_tx_mowner;
1098 #endif
1099 }
1100 
1101 void
1102 ether_ifdetach(struct ifnet *ifp)
1103 {
1104 	struct ethercom *ec = (void *) ifp;
1105 	struct ether_multi *enm;
1106 	int s;
1107 
1108 #if NBRIDGE > 0
1109 	if (ifp->if_bridge)
1110 		bridge_ifdetach(ifp);
1111 #endif
1112 
1113 #if NBPFILTER > 0
1114 	bpfdetach(ifp);
1115 #endif
1116 
1117 #if NVLAN > 0
1118 	if (ec->ec_nvlans)
1119 		vlan_ifdetach(ifp);
1120 #endif
1121 
1122 	s = splnet();
1123 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
1124 		LIST_REMOVE(enm, enm_list);
1125 		free(enm, M_IFMADDR);
1126 		ec->ec_multicnt--;
1127 	}
1128 	splx(s);
1129 
1130 #if 0	/* done in if_detach() */
1131 	if_free_sadl(ifp);
1132 #endif
1133 
1134 	MOWNER_DETACH(&ec->ec_rx_mowner);
1135 	MOWNER_DETACH(&ec->ec_tx_mowner);
1136 }
1137 
1138 #if 0
1139 /*
1140  * This is for reference.  We have a table-driven version
1141  * of the little-endian crc32 generator, which is faster
1142  * than the double-loop.
1143  */
1144 u_int32_t
1145 ether_crc32_le(const u_int8_t *buf, size_t len)
1146 {
1147 	u_int32_t c, crc, carry;
1148 	size_t i, j;
1149 
1150 	crc = 0xffffffffU;	/* initial value */
1151 
1152 	for (i = 0; i < len; i++) {
1153 		c = buf[i];
1154 		for (j = 0; j < 8; j++) {
1155 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1156 			crc >>= 1;
1157 			c >>= 1;
1158 			if (carry)
1159 				crc = (crc ^ ETHER_CRC_POLY_LE);
1160 		}
1161 	}
1162 
1163 	return (crc);
1164 }
1165 #else
1166 u_int32_t
1167 ether_crc32_le(const u_int8_t *buf, size_t len)
1168 {
1169 	static const u_int32_t crctab[] = {
1170 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1171 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1172 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1173 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1174 	};
1175 	u_int32_t crc;
1176 	size_t i;
1177 
1178 	crc = 0xffffffffU;	/* initial value */
1179 
1180 	for (i = 0; i < len; i++) {
1181 		crc ^= buf[i];
1182 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1183 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1184 	}
1185 
1186 	return (crc);
1187 }
1188 #endif
1189 
1190 u_int32_t
1191 ether_crc32_be(const u_int8_t *buf, size_t len)
1192 {
1193 	u_int32_t c, crc, carry;
1194 	size_t i, j;
1195 
1196 	crc = 0xffffffffU;	/* initial value */
1197 
1198 	for (i = 0; i < len; i++) {
1199 		c = buf[i];
1200 		for (j = 0; j < 8; j++) {
1201 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1202 			crc <<= 1;
1203 			c >>= 1;
1204 			if (carry)
1205 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1206 		}
1207 	}
1208 
1209 	return (crc);
1210 }
1211 
1212 #ifdef INET
1213 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1214     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1215 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1216     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1217 #endif
1218 #ifdef INET6
1219 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1220     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1221 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1222     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1223 #endif
1224 
1225 /*
1226  * ether_aton implementation, not using a static buffer.
1227  */
1228 int
1229 ether_nonstatic_aton(u_char *dest, char *str)
1230 {
1231         int i;
1232         char *cp = str;
1233         u_char val[6];
1234 
1235 #define set_value                       \
1236         if (*cp > '9' && *cp < 'a')     \
1237                 *cp -= 'A' - 10;        \
1238         else if (*cp > '9')             \
1239                 *cp -= 'a' - 10;        \
1240         else                            \
1241                 *cp -= '0'
1242 
1243         for (i = 0; i < 6; i++, cp++) {
1244                 if (!isxdigit(*cp))
1245                         return (1);
1246                 set_value;
1247                 val[i] = *cp++;
1248                 if (isxdigit(*cp)) {
1249                         set_value;
1250                         val[i] *= 16;
1251                         val[i] += *cp++;
1252                 }
1253                 if (*cp == ':' || i == 5)
1254                         continue;
1255                 else
1256                         return 1;
1257         }
1258         memcpy(dest, val, 6);
1259 
1260         return 0;
1261 }
1262 
1263 
1264 /*
1265  * Convert a sockaddr into an Ethernet address or range of Ethernet
1266  * addresses.
1267  */
1268 int
1269 ether_multiaddr(const struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
1270     u_int8_t addrhi[ETHER_ADDR_LEN])
1271 {
1272 #ifdef INET
1273 	const struct sockaddr_in *sin;
1274 #endif /* INET */
1275 #ifdef INET6
1276 	const struct sockaddr_in6 *sin6;
1277 #endif /* INET6 */
1278 
1279 	switch (sa->sa_family) {
1280 
1281 	case AF_UNSPEC:
1282 		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
1283 		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1284 		break;
1285 
1286 #ifdef INET
1287 	case AF_INET:
1288 		sin = satocsin(sa);
1289 		if (sin->sin_addr.s_addr == INADDR_ANY) {
1290 			/*
1291 			 * An IP address of INADDR_ANY means listen to
1292 			 * or stop listening to all of the Ethernet
1293 			 * multicast addresses used for IP.
1294 			 * (This is for the sake of IP multicast routers.)
1295 			 */
1296 			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
1297 			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1298 		}
1299 		else {
1300 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1301 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1302 		}
1303 		break;
1304 #endif
1305 #ifdef INET6
1306 	case AF_INET6:
1307 		sin6 = satocsin6(sa);
1308 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1309 			/*
1310 			 * An IP6 address of 0 means listen to or stop
1311 			 * listening to all of the Ethernet multicast
1312 			 * address used for IP6.
1313 			 * (This is used for multicast routers.)
1314 			 */
1315 			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
1316 			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1317 		} else {
1318 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1319 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1320 		}
1321 		break;
1322 #endif
1323 
1324 	default:
1325 		return EAFNOSUPPORT;
1326 	}
1327 	return 0;
1328 }
1329 
1330 /*
1331  * Add an Ethernet multicast address or range of addresses to the list for a
1332  * given interface.
1333  */
1334 int
1335 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1336 {
1337 	struct ether_multi *enm;
1338 	u_char addrlo[ETHER_ADDR_LEN];
1339 	u_char addrhi[ETHER_ADDR_LEN];
1340 	int s = splnet(), error;
1341 
1342 	error = ether_multiaddr(sa, addrlo, 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  */
1392 int
1393 ether_delmulti(const struct sockaddr *sa, 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(sa, addrlo, addrhi);
1401 	if (error != 0) {
1402 		splx(s);
1403 		return (error);
1404 	}
1405 
1406 	/*
1407 	 * Look ur the address in our list.
1408 	 */
1409 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1410 	if (enm == NULL) {
1411 		splx(s);
1412 		return (ENXIO);
1413 	}
1414 	if (--enm->enm_refcount != 0) {
1415 		/*
1416 		 * Still some claims to this record.
1417 		 */
1418 		splx(s);
1419 		return (0);
1420 	}
1421 	/*
1422 	 * No remaining claims to this record; unlink and free it.
1423 	 */
1424 	LIST_REMOVE(enm, enm_list);
1425 	free(enm, M_IFMADDR);
1426 	ec->ec_multicnt--;
1427 	splx(s);
1428 	/*
1429 	 * Return ENETRESET to inform the driver that the list has changed
1430 	 * and its reception filter should be adjusted accordingly.
1431 	 */
1432 	return (ENETRESET);
1433 }
1434 
1435 /*
1436  * Common ioctls for Ethernet interfaces.  Note, we must be
1437  * called at splnet().
1438  */
1439 int
1440 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1441 {
1442 	struct ethercom *ec = (void *) ifp;
1443 	struct ifreq *ifr = (struct ifreq *)data;
1444 	struct ifaddr *ifa = (struct ifaddr *)data;
1445 	int error = 0;
1446 
1447 	switch (cmd) {
1448 	case SIOCSIFADDR:
1449 		ifp->if_flags |= IFF_UP;
1450 		switch (ifa->ifa_addr->sa_family) {
1451 		case AF_LINK:
1452 		    {
1453 			const struct sockaddr_dl *sdl = satocsdl(ifa->ifa_addr);
1454 
1455 			if (sdl->sdl_type != IFT_ETHER ||
1456 			    sdl->sdl_alen != ifp->if_addrlen) {
1457 				error = EINVAL;
1458 				break;
1459 			}
1460 
1461 			(void)sockaddr_dl_setaddr(ifp->if_sadl,
1462 			    ifp->if_sadl->sdl_len, CLLADDR(sdl),
1463 			    ifp->if_addrlen);
1464 
1465 			/* Set new address. */
1466 			error = (*ifp->if_init)(ifp);
1467 			break;
1468 		    }
1469 #ifdef INET
1470 		case AF_INET:
1471 			if ((ifp->if_flags & IFF_RUNNING) == 0 &&
1472 			    (error = (*ifp->if_init)(ifp)) != 0)
1473 				break;
1474 			arp_ifinit(ifp, ifa);
1475 			break;
1476 #endif /* INET */
1477 		default:
1478 			if ((ifp->if_flags & IFF_RUNNING) == 0)
1479 				error = (*ifp->if_init)(ifp);
1480 			break;
1481 		}
1482 		break;
1483 
1484 	case SIOCGIFADDR:
1485 		memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
1486 		    CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1487 		break;
1488 
1489 	case SIOCSIFMTU:
1490 	    {
1491 		int maxmtu;
1492 
1493 		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1494 			maxmtu = ETHERMTU_JUMBO;
1495 		else
1496 			maxmtu = ETHERMTU;
1497 
1498 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1499 			error = EINVAL;
1500 		else {
1501 			ifp->if_mtu = ifr->ifr_mtu;
1502 
1503 			/* Make sure the device notices the MTU change. */
1504 			if (ifp->if_flags & IFF_UP)
1505 				error = (*ifp->if_init)(ifp);
1506 		}
1507 		break;
1508 	    }
1509 
1510 	case SIOCSIFFLAGS:
1511 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
1512 			/*
1513 			 * If interface is marked down and it is running,
1514 			 * then stop and disable it.
1515 			 */
1516 			(*ifp->if_stop)(ifp, 1);
1517 		} else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
1518 			/*
1519 			 * If interface is marked up and it is stopped, then
1520 			 * start it.
1521 			 */
1522 			error = (*ifp->if_init)(ifp);
1523 		} else if ((ifp->if_flags & IFF_UP) != 0) {
1524 			/*
1525 			 * Reset the interface to pick up changes in any other
1526 			 * flags that affect the hardware state.
1527 			 */
1528 			error = (*ifp->if_init)(ifp);
1529 		}
1530 		break;
1531 
1532 	case SIOCADDMULTI:
1533 		error = ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1534 		break;
1535 
1536 	case SIOCDELMULTI:
1537 		error = ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1538 		break;
1539 
1540 	default:
1541 		error = ENOTTY;
1542 	}
1543 
1544 	return (error);
1545 }
1546