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