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