Home | History | Annotate | Line # | Download | only in net
if_ethersubr.c revision 1.76
      1 /*	$NetBSD: if_ethersubr.c,v 1.76 2001/04/07 18:01:48 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 "vlan.h"
     75 #include "bpfilter.h"
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/kernel.h>
     80 #include <sys/malloc.h>
     81 #include <sys/mbuf.h>
     82 #include <sys/protosw.h>
     83 #include <sys/socket.h>
     84 #include <sys/ioctl.h>
     85 #include <sys/errno.h>
     86 #include <sys/syslog.h>
     87 
     88 #include <machine/cpu.h>
     89 
     90 #include <net/if.h>
     91 #include <net/netisr.h>
     92 #include <net/route.h>
     93 #include <net/if_llc.h>
     94 #include <net/if_dl.h>
     95 #include <net/if_types.h>
     96 
     97 #if NBPFILTER > 0
     98 #include <net/bpf.h>
     99 #endif
    100 
    101 #include <net/if_ether.h>
    102 #if NVLAN > 0
    103 #include <net/if_vlanvar.h>
    104 #endif
    105 
    106 #include <netinet/in.h>
    107 #ifdef INET
    108 #include <netinet/in_var.h>
    109 #endif
    110 #include <netinet/if_inarp.h>
    111 
    112 #ifdef INET6
    113 #ifndef INET
    114 #include <netinet/in.h>
    115 #endif
    116 #include <netinet6/in6_var.h>
    117 #include <netinet6/nd6.h>
    118 #endif
    119 
    120 #ifdef NS
    121 #include <netns/ns.h>
    122 #include <netns/ns_if.h>
    123 #endif
    124 
    125 #ifdef IPX
    126 #include <netipx/ipx.h>
    127 #include <netipx/ipx_if.h>
    128 #endif
    129 
    130 #ifdef ISO
    131 #include <netiso/argo_debug.h>
    132 #include <netiso/iso.h>
    133 #include <netiso/iso_var.h>
    134 #include <netiso/iso_snpac.h>
    135 #endif
    136 
    137 #ifdef LLC
    138 #include <netccitt/dll.h>
    139 #include <netccitt/llc_var.h>
    140 #endif
    141 
    142 #if defined(LLC) && defined(CCITT)
    143 extern struct ifqueue pkintrq;
    144 #endif
    145 
    146 #ifdef NETATALK
    147 #include <netatalk/at.h>
    148 #include <netatalk/at_var.h>
    149 #include <netatalk/at_extern.h>
    150 
    151 #define llc_snap_org_code llc_un.type_snap.org_code
    152 #define llc_snap_ether_type llc_un.type_snap.ether_type
    153 
    154 extern u_char	at_org_code[3];
    155 extern u_char	aarp_org_code[3];
    156 #endif /* NETATALK */
    157 
    158 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    159 #define senderr(e) { error = (e); goto bad;}
    160 
    161 #define SIN(x) ((struct sockaddr_in *)x)
    162 
    163 static	int ether_output __P((struct ifnet *, struct mbuf *,
    164 	    struct sockaddr *, struct rtentry *));
    165 static	void ether_input __P((struct ifnet *, struct mbuf *));
    166 
    167 /*
    168  * Ethernet output routine.
    169  * Encapsulate a packet of type family for the local net.
    170  * Assumes that ifp is actually pointer to ethercom structure.
    171  */
    172 static int
    173 ether_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
    174 	struct rtentry *rt0)
    175 {
    176 	u_int16_t etype = 0;
    177 	int s, len, error = 0, hdrcmplt = 0;
    178  	u_char esrc[6], edst[6];
    179 	struct mbuf *m = m0;
    180 	struct rtentry *rt;
    181 	struct mbuf *mcopy = (struct mbuf *)0;
    182 	struct ether_header *eh;
    183 	ALTQ_DECL(struct altq_pktattr pktattr;)
    184 #ifdef INET
    185 	struct arphdr *ah;
    186 #endif /* INET */
    187 #ifdef NETATALK
    188 	struct at_ifaddr *aa;
    189 #endif /* NETATALK */
    190 	short mflags;
    191 
    192 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
    193 		senderr(ENETDOWN);
    194 	ifp->if_lastchange = time;
    195 	if ((rt = rt0) != NULL) {
    196 		if ((rt->rt_flags & RTF_UP) == 0) {
    197 			if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
    198 				rt->rt_refcnt--;
    199 				if (rt->rt_ifp != ifp)
    200 					return (*rt->rt_ifp->if_output)
    201 							(ifp, m0, dst, rt);
    202 			} else
    203 				senderr(EHOSTUNREACH);
    204 		}
    205 		if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
    206 			if (rt->rt_gwroute == 0)
    207 				goto lookup;
    208 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
    209 				rtfree(rt); rt = rt0;
    210 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
    211 				if ((rt = rt->rt_gwroute) == 0)
    212 					senderr(EHOSTUNREACH);
    213 				/* the "G" test below also prevents rt == rt0 */
    214 				if ((rt->rt_flags & RTF_GATEWAY) ||
    215 				    (rt->rt_ifp != ifp)) {
    216 					rt->rt_refcnt--;
    217 					rt0->rt_gwroute = 0;
    218 					senderr(EHOSTUNREACH);
    219 				}
    220 			}
    221 		}
    222 		if (rt->rt_flags & RTF_REJECT)
    223 			if (rt->rt_rmx.rmx_expire == 0 ||
    224 			    time.tv_sec < rt->rt_rmx.rmx_expire)
    225 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
    226 	}
    227 
    228 	/*
    229 	 * If the queueing discipline needs packet classification,
    230 	 * do it before prepending link headers.
    231 	 */
    232 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    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 	mflags = m->m_flags;
    481 	len = m->m_pkthdr.len;
    482 	s = splimp();
    483 	/*
    484 	 * Queue message on interface, and start output if interface
    485 	 * not yet active.
    486 	 */
    487 	IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    488 	if (error) {
    489 		/* mbuf is already freed */
    490 		splx(s);
    491 		return (error);
    492 	}
    493 	ifp->if_obytes += len;
    494 	if (mflags & M_MCAST)
    495 		ifp->if_omcasts++;
    496 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    497 		(*ifp->if_start)(ifp);
    498 	splx(s);
    499 	return (error);
    500 
    501 bad:
    502 	if (m)
    503 		m_freem(m);
    504 	return (error);
    505 }
    506 
    507 #ifdef ALTQ
    508 /*
    509  * This routine is a slight hack to allow a packet to be classified
    510  * if the Ethernet headers are present.  It will go away when ALTQ's
    511  * classification engine understands link headers.
    512  */
    513 void
    514 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
    515     struct altq_pktattr *pktattr)
    516 {
    517 	struct ether_header *eh;
    518 	u_int16_t ether_type;
    519 	int hlen, af, hdrsize;
    520 	caddr_t hdr;
    521 
    522 	hlen = ETHER_HDR_LEN;
    523 	eh = mtod(m, struct ether_header *);
    524 
    525 	ether_type = htons(eh->ether_type);
    526 
    527 	if (ether_type < ETHERMTU) {
    528 		/* LLC/SNAP */
    529 		struct llc *llc = (struct llc *)(eh + 1);
    530 		hlen += 8;
    531 
    532 		if (m->m_len < hlen ||
    533 		    llc->llc_dsap != LLC_SNAP_LSAP ||
    534 		    llc->llc_ssap != LLC_SNAP_LSAP ||
    535 		    llc->llc_control != LLC_UI) {
    536 			/* Not SNAP. */
    537 			goto bad;
    538 		}
    539 
    540 		ether_type = htons(llc->llc_un.type_snap.ether_type);
    541 	}
    542 
    543 	switch (ether_type) {
    544 	case ETHERTYPE_IP:
    545 		af = AF_INET;
    546 		hdrsize = 20;		/* sizeof(struct ip) */
    547 		break;
    548 
    549 	case ETHERTYPE_IPV6:
    550 		af = AF_INET6;
    551 		hdrsize = 40;		/* sizeof(struct ip6_hdr) */
    552 		break;
    553 
    554 	default:
    555 		af = AF_UNSPEC;
    556 		hdrsize = 0;
    557 		break;
    558 	}
    559 
    560 	if (m->m_len < (hlen + hdrsize)) {
    561 		/*
    562 		 * Ethernet and protocol header not in a single
    563 		 * mbuf.  We can't cope with this situation right
    564 		 * now (but it shouldn't ever happen, really, anyhow).
    565 		 * XXX Should use m_pulldown().
    566 		 */
    567 		printf("altq_etherclassify: headers span multiple mbufs\n");
    568 		goto bad;
    569 	}
    570 
    571 	m->m_data += hlen;
    572 	m->m_len -= hlen;
    573 
    574 	hdr = mtod(m, caddr_t);
    575 
    576 	if (ALTQ_NEEDS_CLASSIFY(ifq))
    577 		pktattr->pattr_class =
    578 		    (*ifq->altq_classify)(ifq->altq_clfier, m, af);
    579 	pktattr->pattr_af = af;
    580 	pktattr->pattr_hdr = hdr;
    581 
    582 	m->m_data -= hlen;
    583 	m->m_len += hlen;
    584 
    585 	return;
    586 
    587  bad:
    588 	pktattr->pattr_class = NULL;
    589 	pktattr->pattr_hdr = NULL;
    590 	pktattr->pattr_af = AF_UNSPEC;
    591 }
    592 #endif /* ALTQ */
    593 
    594 /*
    595  * Process a received Ethernet packet;
    596  * the packet is in the mbuf chain m with
    597  * the ether header.
    598  */
    599 static void
    600 ether_input(struct ifnet *ifp, struct mbuf *m)
    601 {
    602 	struct ifqueue *inq;
    603 	u_int16_t etype;
    604 	int s;
    605 	struct ether_header *eh;
    606 	struct mbuf *n;
    607 #if defined (ISO) || defined (LLC) || defined(NETATALK)
    608 	struct llc *l;
    609 #endif
    610 
    611 	if ((ifp->if_flags & IFF_UP) == 0) {
    612 		m_freem(m);
    613 		return;
    614 	}
    615 
    616 	eh = mtod(m, struct ether_header *);
    617 	etype = ntohs(eh->ether_type);
    618 
    619 	/*
    620 	 * Determine if the packet is within its size limits.
    621 	 */
    622 	if (m->m_pkthdr.len > ETHER_MAX_FRAME(etype, m->m_flags & M_HASFCS)) {
    623 		printf("%s: discarding oversize frame (len=%d)\n",
    624 		    ifp->if_xname, m->m_pkthdr.len);
    625 		m_freem(m);
    626 		return;
    627 	}
    628 
    629 	ifp->if_lastchange = time;
    630 	ifp->if_ibytes += m->m_pkthdr.len;
    631 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
    632 		if (memcmp(etherbroadcastaddr,
    633 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
    634 			m->m_flags |= M_BCAST;
    635 		else
    636 			m->m_flags |= M_MCAST;
    637 		ifp->if_imcasts++;
    638 	} else if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    639 		   memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
    640 			  ETHER_ADDR_LEN) != 0) {
    641 		m_freem(m);
    642 		return;
    643 	}
    644 
    645 	/* Check if the mbuf has a VLAN tag */
    646 	n = m_aux_find(m, AF_LINK, ETHERTYPE_VLAN);
    647 	if (n) {
    648 #if NVLAN > 0
    649 		/*
    650 		 * vlan_input() will either recursively call ether_input()
    651 		 * or drop the packet.
    652 		 */
    653 		if (((struct ethercom *)ifp)->ec_nvlans != 0)
    654 			vlan_input(ifp, m);
    655 		else
    656 #endif
    657 			m_freem(m);
    658 		return;
    659 	}
    660 
    661 	/*
    662 	 * Handle protocols that expect to have the Ethernet header
    663 	 * (and possibly FCS) intact.
    664 	 */
    665 	switch (etype) {
    666 #if NVLAN > 0
    667 	case ETHERTYPE_VLAN:
    668 		/*
    669 		 * vlan_input() will either recursively call ether_input()
    670 		 * or drop the packet.
    671 		 */
    672 		if (((struct ethercom *)ifp)->ec_nvlans != 0)
    673 			vlan_input(ifp, m);
    674 		else
    675 			m_freem(m);
    676 		return;
    677 #endif /* NVLAN > 0 */
    678 	default:
    679 		/* Nothing. */
    680 	}
    681 
    682 	/* Strip off the Ethernet header. */
    683 	m_adj(m, sizeof(struct ether_header));
    684 
    685 	/* If the CRC is still on the packet, trim it off. */
    686 	if (m->m_flags & M_HASFCS)
    687 		m_adj(m, -ETHER_CRC_LEN);
    688 
    689 	switch (etype) {
    690 #ifdef INET
    691 	case ETHERTYPE_IP:
    692 #ifdef GATEWAY
    693 		if (ipflow_fastforward(m))
    694 			return;
    695 #endif
    696 		schednetisr(NETISR_IP);
    697 		inq = &ipintrq;
    698 		break;
    699 
    700 	case ETHERTYPE_ARP:
    701 		schednetisr(NETISR_ARP);
    702 		inq = &arpintrq;
    703 		break;
    704 
    705 	case ETHERTYPE_REVARP:
    706 		revarpinput(m);	/* XXX queue? */
    707 		return;
    708 #endif
    709 #ifdef INET6
    710 	case ETHERTYPE_IPV6:
    711 		schednetisr(NETISR_IPV6);
    712 		inq = &ip6intrq;
    713 		break;
    714 #endif
    715 #ifdef NS
    716 	case ETHERTYPE_NS:
    717 		schednetisr(NETISR_NS);
    718 		inq = &nsintrq;
    719 		break;
    720 
    721 #endif
    722 #ifdef IPX
    723 	case ETHERTYPE_IPX:
    724 		schednetisr(NETISR_IPX);
    725 		inq = &ipxintrq;
    726 		break;
    727 #endif
    728 #ifdef NETATALK
    729         case ETHERTYPE_ATALK:
    730                 schednetisr(NETISR_ATALK);
    731                 inq = &atintrq1;
    732                 break;
    733         case ETHERTYPE_AARP:
    734 		/* probably this should be done with a NETISR as well */
    735                 aarpinput(ifp, m); /* XXX */
    736                 return;
    737 #endif /* NETATALK */
    738 	default:
    739 #if defined (ISO) || defined (LLC) || defined (NETATALK)
    740 		if (etype > ETHERMTU)
    741 			goto dropanyway;
    742 		l = mtod(m, struct llc *);
    743 		switch (l->llc_dsap) {
    744 #ifdef NETATALK
    745 		case LLC_SNAP_LSAP:
    746 			switch (l->llc_control) {
    747 			case LLC_UI:
    748 				if (l->llc_ssap != LLC_SNAP_LSAP) {
    749 					goto dropanyway;
    750 				}
    751 
    752 				if (Bcmp(&(l->llc_snap_org_code)[0],
    753 				    at_org_code, sizeof(at_org_code)) == 0 &&
    754 				    ntohs(l->llc_snap_ether_type) ==
    755 				    ETHERTYPE_ATALK) {
    756 					inq = &atintrq2;
    757 					m_adj(m, sizeof(struct llc));
    758 					schednetisr(NETISR_ATALK);
    759 					break;
    760 				}
    761 
    762 				if (Bcmp(&(l->llc_snap_org_code)[0],
    763 				    aarp_org_code,
    764 				    sizeof(aarp_org_code)) == 0 &&
    765 				    ntohs(l->llc_snap_ether_type) ==
    766 				    ETHERTYPE_AARP) {
    767 					m_adj( m, sizeof(struct llc));
    768 					aarpinput(ifp, m); /* XXX */
    769 				    return;
    770 				}
    771 
    772 			default:
    773 				goto dropanyway;
    774 			}
    775 			break;
    776 #endif /* NETATALK */
    777 #ifdef	ISO
    778 		case LLC_ISO_LSAP:
    779 			switch (l->llc_control) {
    780 			case LLC_UI:
    781 				/* LLC_UI_P forbidden in class 1 service */
    782 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
    783 				    (l->llc_ssap == LLC_ISO_LSAP)) {
    784 					/* LSAP for ISO */
    785 					if (m->m_pkthdr.len > etype)
    786 						m_adj(m, etype - m->m_pkthdr.len);
    787 					m->m_data += 3;		/* XXX */
    788 					m->m_len -= 3;		/* XXX */
    789 					m->m_pkthdr.len -= 3;	/* XXX */
    790 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
    791 					if (m == 0)
    792 						return;
    793 					*mtod(m, struct ether_header *) = *eh;
    794 #ifdef ARGO_DEBUG
    795 					if (argo_debug[D_ETHER])
    796 						printf("clnp packet");
    797 #endif
    798 					schednetisr(NETISR_ISO);
    799 					inq = &clnlintrq;
    800 					break;
    801 				}
    802 				goto dropanyway;
    803 
    804 			case LLC_XID:
    805 			case LLC_XID_P:
    806 				if(m->m_len < 6)
    807 					goto dropanyway;
    808 				l->llc_window = 0;
    809 				l->llc_fid = 9;
    810 				l->llc_class = 1;
    811 				l->llc_dsap = l->llc_ssap = 0;
    812 				/* Fall through to */
    813 			case LLC_TEST:
    814 			case LLC_TEST_P:
    815 			{
    816 				struct sockaddr sa;
    817 				struct ether_header *eh2;
    818 				int i;
    819 				u_char c = l->llc_dsap;
    820 
    821 				l->llc_dsap = l->llc_ssap;
    822 				l->llc_ssap = c;
    823 				if (m->m_flags & (M_BCAST | M_MCAST))
    824 					bcopy(LLADDR(ifp->if_sadl),
    825 					      (caddr_t)eh->ether_dhost, 6);
    826 				sa.sa_family = AF_UNSPEC;
    827 				sa.sa_len = sizeof(sa);
    828 				eh2 = (struct ether_header *)sa.sa_data;
    829 				for (i = 0; i < 6; i++) {
    830 					eh2->ether_shost[i] = c =
    831 					    eh->ether_dhost[i];
    832 					eh2->ether_dhost[i] =
    833 					    eh->ether_dhost[i] =
    834 					    eh->ether_shost[i];
    835 					eh->ether_shost[i] = c;
    836 				}
    837 				ifp->if_output(ifp, m, &sa, NULL);
    838 				return;
    839 			}
    840 			default:
    841 				m_freem(m);
    842 				return;
    843 			}
    844 			break;
    845 #endif /* ISO */
    846 #ifdef LLC
    847 		case LLC_X25_LSAP:
    848 		{
    849 			if (m->m_pkthdr.len > etype)
    850 				m_adj(m, etype - m->m_pkthdr.len);
    851 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
    852 			if (m == 0)
    853 				return;
    854 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
    855 					    eh->ether_dhost, LLC_X25_LSAP, 6,
    856 					    mtod(m, struct sdl_hdr *)))
    857 				panic("ETHER cons addr failure");
    858 			mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
    859 #ifdef LLC_DEBUG
    860 				printf("llc packet\n");
    861 #endif /* LLC_DEBUG */
    862 			schednetisr(NETISR_CCITT);
    863 			inq = &llcintrq;
    864 			break;
    865 		}
    866 #endif /* LLC */
    867 		dropanyway:
    868 		default:
    869 			m_freem(m);
    870 			return;
    871 		}
    872 #else /* ISO || LLC  || NETATALK*/
    873 	    m_freem(m);
    874 	    return;
    875 #endif /* ISO || LLC || NETATALK*/
    876 	}
    877 
    878 	s = splimp();
    879 	if (IF_QFULL(inq)) {
    880 		IF_DROP(inq);
    881 		m_freem(m);
    882 	} else
    883 		IF_ENQUEUE(inq, m);
    884 	splx(s);
    885 }
    886 
    887 /*
    888  * Convert Ethernet address to printable (loggable) representation.
    889  */
    890 static char digits[] = "0123456789abcdef";
    891 char *
    892 ether_sprintf(const u_char *ap)
    893 {
    894 	static char etherbuf[18];
    895 	char *cp = etherbuf;
    896 	int i;
    897 
    898 	for (i = 0; i < 6; i++) {
    899 		*cp++ = digits[*ap >> 4];
    900 		*cp++ = digits[*ap++ & 0xf];
    901 		*cp++ = ':';
    902 	}
    903 	*--cp = 0;
    904 	return (etherbuf);
    905 }
    906 
    907 /*
    908  * Perform common duties while attaching to interface list
    909  */
    910 void
    911 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
    912 {
    913 
    914 	ifp->if_type = IFT_ETHER;
    915 	ifp->if_addrlen = ETHER_ADDR_LEN;
    916 	ifp->if_hdrlen = 14;
    917 	ifp->if_dlt = DLT_EN10MB;
    918 	ifp->if_mtu = ETHERMTU;
    919 	ifp->if_output = ether_output;
    920 	ifp->if_input = ether_input;
    921 	if (ifp->if_baudrate == 0)
    922 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
    923 
    924 	if_alloc_sadl(ifp);
    925 	memcpy(LLADDR(ifp->if_sadl), lla, ifp->if_addrlen);
    926 
    927 	LIST_INIT(&((struct ethercom *)ifp)->ec_multiaddrs);
    928 	ifp->if_broadcastaddr = etherbroadcastaddr;
    929 #if NBPFILTER > 0
    930 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
    931 #endif
    932 }
    933 
    934 void
    935 ether_ifdetach(struct ifnet *ifp)
    936 {
    937 	struct ethercom *ec = (void *) ifp;
    938 	struct ether_multi *enm;
    939 	int s;
    940 
    941 #if NBPFILTER > 0
    942 	bpfdetach(ifp);
    943 #endif
    944 
    945 #if NVLAN > 0
    946 	if (ec->ec_nvlans)
    947 		vlan_ifdetach(ifp);
    948 #endif
    949 
    950 	s = splimp();
    951 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
    952 		LIST_REMOVE(enm, enm_list);
    953 		free(enm, M_IFADDR);
    954 		ec->ec_multicnt--;
    955 	}
    956 	splx(s);
    957 
    958 	if_free_sadl(ifp);
    959 }
    960 
    961 #if 0
    962 /*
    963  * This is for reference.  We have a table-driven version
    964  * of the little-endian crc32 generator, which is faster
    965  * than the double-loop.
    966  */
    967 u_int32_t
    968 ether_crc32_le(const u_int8_t *buf, size_t len)
    969 {
    970 	u_int32_t c, crc, carry;
    971 	size_t i, j;
    972 
    973 	crc = 0xffffffffU;	/* initial value */
    974 
    975 	for (i = 0; i < len; i++) {
    976 		c = buf[i];
    977 		for (j = 0; j < 8; j++) {
    978 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
    979 			crc >>= 1;
    980 			c >>= 1;
    981 			if (carry)
    982 				crc = (crc ^ ETHER_CRC_POLY_LE);
    983 		}
    984 	}
    985 
    986 	return (crc);
    987 }
    988 #else
    989 u_int32_t
    990 ether_crc32_le(const u_int8_t *buf, size_t len)
    991 {
    992 	static const u_int32_t crctab[] = {
    993 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
    994 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
    995 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
    996 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
    997 	};
    998 	u_int32_t crc;
    999 	int i;
   1000 
   1001 	crc = 0xffffffffU;	/* initial value */
   1002 
   1003 	for (i = 0; i < len; i++) {
   1004 		crc ^= buf[i];
   1005 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1006 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1007 	}
   1008 
   1009 	return (crc);
   1010 }
   1011 #endif
   1012 
   1013 u_int32_t
   1014 ether_crc32_be(const u_int8_t *buf, size_t len)
   1015 {
   1016 	u_int32_t c, crc, carry;
   1017 	size_t i, j;
   1018 
   1019 	crc = 0xffffffffU;	/* initial value */
   1020 
   1021 	for (i = 0; i < len; i++) {
   1022 		c = buf[i];
   1023 		for (j = 0; j < 8; j++) {
   1024 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
   1025 			crc <<= 1;
   1026 			c >>= 1;
   1027 			if (carry)
   1028 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
   1029 		}
   1030 	}
   1031 
   1032 	return (crc);
   1033 }
   1034 
   1035 #ifdef INET
   1036 u_char	ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
   1037 u_char	ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
   1038 #endif
   1039 #ifdef INET6
   1040 u_char	ether_ip6multicast_min[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
   1041 u_char	ether_ip6multicast_max[6] = { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
   1042 #endif
   1043 
   1044 /*
   1045  * Convert a sockaddr into an Ethernet address or range of Ethernet
   1046  * addresses.
   1047  */
   1048 int
   1049 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
   1050     u_int8_t addrhi[ETHER_ADDR_LEN])
   1051 {
   1052 #ifdef INET
   1053 	struct sockaddr_in *sin;
   1054 #endif /* INET */
   1055 #ifdef INET6
   1056 	struct sockaddr_in6 *sin6;
   1057 #endif /* INET6 */
   1058 
   1059 	switch (sa->sa_family) {
   1060 
   1061 	case AF_UNSPEC:
   1062 		bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
   1063 		bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
   1064 		break;
   1065 
   1066 #ifdef INET
   1067 	case AF_INET:
   1068 		sin = satosin(sa);
   1069 		if (sin->sin_addr.s_addr == INADDR_ANY) {
   1070 			/*
   1071 			 * An IP address of INADDR_ANY means listen to
   1072 			 * or stop listening to all of the Ethernet
   1073 			 * multicast addresses used for IP.
   1074 			 * (This is for the sake of IP multicast routers.)
   1075 			 */
   1076 			bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
   1077 			bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
   1078 		}
   1079 		else {
   1080 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
   1081 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
   1082 		}
   1083 		break;
   1084 #endif
   1085 #ifdef INET6
   1086 	case AF_INET6:
   1087 		sin6 = satosin6(sa);
   1088 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1089 			/*
   1090 			 * An IP6 address of 0 means listen to or stop
   1091 			 * listening to all of the Ethernet multicast
   1092 			 * address used for IP6.
   1093 			 * (This is used for multicast routers.)
   1094 			 */
   1095 			bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
   1096 			bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
   1097 		} else {
   1098 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
   1099 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
   1100 		}
   1101 		break;
   1102 #endif
   1103 
   1104 	default:
   1105 		return (EAFNOSUPPORT);
   1106 	}
   1107 	return (0);
   1108 }
   1109 
   1110 /*
   1111  * Add an Ethernet multicast address or range of addresses to the list for a
   1112  * given interface.
   1113  */
   1114 int
   1115 ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
   1116 {
   1117 	struct ether_multi *enm;
   1118 	u_char addrlo[ETHER_ADDR_LEN];
   1119 	u_char addrhi[ETHER_ADDR_LEN];
   1120 	int s = splimp(), error;
   1121 
   1122 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
   1123 	if (error != 0) {
   1124 		splx(s);
   1125 		return (error);
   1126 	}
   1127 
   1128 	/*
   1129 	 * Verify that we have valid Ethernet multicast addresses.
   1130 	 */
   1131 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
   1132 		splx(s);
   1133 		return (EINVAL);
   1134 	}
   1135 	/*
   1136 	 * See if the address range is already in the list.
   1137 	 */
   1138 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
   1139 	if (enm != NULL) {
   1140 		/*
   1141 		 * Found it; just increment the reference count.
   1142 		 */
   1143 		++enm->enm_refcount;
   1144 		splx(s);
   1145 		return (0);
   1146 	}
   1147 	/*
   1148 	 * New address or range; malloc a new multicast record
   1149 	 * and link it into the interface's multicast list.
   1150 	 */
   1151 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
   1152 	if (enm == NULL) {
   1153 		splx(s);
   1154 		return (ENOBUFS);
   1155 	}
   1156 	bcopy(addrlo, enm->enm_addrlo, 6);
   1157 	bcopy(addrhi, enm->enm_addrhi, 6);
   1158 	enm->enm_ec = ec;
   1159 	enm->enm_refcount = 1;
   1160 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
   1161 	ec->ec_multicnt++;
   1162 	splx(s);
   1163 	/*
   1164 	 * Return ENETRESET to inform the driver that the list has changed
   1165 	 * and its reception filter should be adjusted accordingly.
   1166 	 */
   1167 	return (ENETRESET);
   1168 }
   1169 
   1170 /*
   1171  * Delete a multicast address record.
   1172  */
   1173 int
   1174 ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
   1175 {
   1176 	struct ether_multi *enm;
   1177 	u_char addrlo[ETHER_ADDR_LEN];
   1178 	u_char addrhi[ETHER_ADDR_LEN];
   1179 	int s = splimp(), error;
   1180 
   1181 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
   1182 	if (error != 0) {
   1183 		splx(s);
   1184 		return (error);
   1185 	}
   1186 
   1187 	/*
   1188 	 * Look ur the address in our list.
   1189 	 */
   1190 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
   1191 	if (enm == NULL) {
   1192 		splx(s);
   1193 		return (ENXIO);
   1194 	}
   1195 	if (--enm->enm_refcount != 0) {
   1196 		/*
   1197 		 * Still some claims to this record.
   1198 		 */
   1199 		splx(s);
   1200 		return (0);
   1201 	}
   1202 	/*
   1203 	 * No remaining claims to this record; unlink and free it.
   1204 	 */
   1205 	LIST_REMOVE(enm, enm_list);
   1206 	free(enm, M_IFMADDR);
   1207 	ec->ec_multicnt--;
   1208 	splx(s);
   1209 	/*
   1210 	 * Return ENETRESET to inform the driver that the list has changed
   1211 	 * and its reception filter should be adjusted accordingly.
   1212 	 */
   1213 	return (ENETRESET);
   1214 }
   1215 
   1216 /*
   1217  * Common ioctls for Ethernet interfaces.  Note, we must be
   1218  * called at splnet().
   1219  */
   1220 int
   1221 ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1222 {
   1223 	struct ethercom *ec = (void *) ifp;
   1224 	struct ifreq *ifr = (struct ifreq *)data;
   1225 	struct ifaddr *ifa = (struct ifaddr *)data;
   1226 	int error = 0;
   1227 
   1228 	switch (cmd) {
   1229 	case SIOCSIFADDR:
   1230 		ifp->if_flags |= IFF_UP;
   1231 		switch (ifa->ifa_addr->sa_family) {
   1232 		case AF_LINK:
   1233 		    {
   1234 			struct sockaddr_dl *sdl =
   1235 			    (struct sockaddr_dl *) ifa->ifa_addr;
   1236 
   1237 			if (sdl->sdl_type != IFT_ETHER ||
   1238 			    sdl->sdl_alen != ifp->if_addrlen) {
   1239 				error = EINVAL;
   1240 				break;
   1241 			}
   1242 
   1243 			memcpy(LLADDR(ifp->if_sadl), LLADDR(sdl),
   1244 			    ifp->if_addrlen);
   1245 
   1246 			/* Set new address. */
   1247 			error = (*ifp->if_init)(ifp);
   1248 			break;
   1249 		    }
   1250 #ifdef INET
   1251 		case AF_INET:
   1252 			if ((error = (*ifp->if_init)(ifp)) != 0)
   1253 				break;
   1254 			arp_ifinit(ifp, ifa);
   1255 			break;
   1256 #endif /* INET */
   1257 #ifdef NS
   1258 		case AF_NS:
   1259 		    {
   1260 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1261 
   1262 			if (ns_nullhost(*ina))
   1263 				ina->x_host = *(union ns_host *)
   1264 				    LLADDR(ifp->if_sadl);
   1265 			else
   1266 				memcpy(LLADDR(ifp->if_sadl),
   1267 				    ina->x_host.c_host, ifp->if_addrlen);
   1268 			/* Set new address. */
   1269 			error = (*ifp->if_init)(ifp);
   1270 			break;
   1271 		    }
   1272 #endif /* NS */
   1273 		default:
   1274 			error = (*ifp->if_init)(ifp);
   1275 			break;
   1276 		}
   1277 		break;
   1278 
   1279 	case SIOCGIFADDR:
   1280 		memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
   1281 		    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1282 		break;
   1283 
   1284 	case SIOCSIFMTU:
   1285 		if (ifr->ifr_mtu > ETHERMTU)
   1286 			error = EINVAL;
   1287 		else
   1288 			ifp->if_mtu = ifr->ifr_mtu;
   1289 		break;
   1290 
   1291 	case SIOCSIFFLAGS:
   1292 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
   1293 			/*
   1294 			 * If interface is marked down and it is running,
   1295 			 * then stop and disable it.
   1296 			 */
   1297 			(*ifp->if_stop)(ifp, 1);
   1298 		} else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
   1299 			/*
   1300 			 * If interface is marked up and it is stopped, then
   1301 			 * start it.
   1302 			 */
   1303 			error = (*ifp->if_init)(ifp);
   1304 		} else if ((ifp->if_flags & IFF_UP) != 0) {
   1305 			/*
   1306 			 * Reset the interface to pick up changes in any other
   1307 			 * flags that affect the hardware state.
   1308 			 */
   1309 			error = (*ifp->if_init)(ifp);
   1310 		}
   1311 		break;
   1312 
   1313 	case SIOCADDMULTI:
   1314 		error = ether_addmulti(ifr, ec);
   1315 		break;
   1316 
   1317 	case SIOCDELMULTI:
   1318 		error = ether_delmulti(ifr, ec);
   1319 		break;
   1320 
   1321 	default:
   1322 		error = ENOTTY;
   1323 	}
   1324 
   1325 	return (error);
   1326 }
   1327