Home | History | Annotate | Line # | Download | only in net
if_ethersubr.c revision 1.15
      1 /*	$NetBSD: if_ethersubr.c,v 1.15 1995/09/29 03:37:43 phil Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1989, 1993
      5  *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/malloc.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/protosw.h>
     44 #include <sys/socket.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/errno.h>
     47 #include <sys/syslog.h>
     48 
     49 #include <machine/cpu.h>
     50 
     51 #include <net/if.h>
     52 #include <net/netisr.h>
     53 #include <net/route.h>
     54 #include <net/if_llc.h>
     55 #include <net/if_dl.h>
     56 #include <net/if_types.h>
     57 
     58 #include <netinet/in.h>
     59 #ifdef INET
     60 #include <netinet/in_var.h>
     61 #endif
     62 #include <netinet/if_ether.h>
     63 
     64 #ifdef NS
     65 #include <netns/ns.h>
     66 #include <netns/ns_if.h>
     67 #endif
     68 
     69 #ifdef ISO
     70 #include <netiso/argo_debug.h>
     71 #include <netiso/iso.h>
     72 #include <netiso/iso_var.h>
     73 #include <netiso/iso_snpac.h>
     74 #endif
     75 
     76 #ifdef LLC
     77 #include <netccitt/dll.h>
     78 #include <netccitt/llc_var.h>
     79 #endif
     80 
     81 #if defined(LLC) && defined(CCITT)
     82 extern struct ifqueue pkintrq;
     83 #endif
     84 
     85 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
     86 #define senderr(e) { error = (e); goto bad;}
     87 
     88 /*
     89  * Ethernet output routine.
     90  * Encapsulate a packet of type family for the local net.
     91  * Use trailer local net encapsulation if enough data in first
     92  * packet leaves a multiple of 512 bytes of data in remainder.
     93  * Assumes that ifp is actually pointer to arpcom structure.
     94  */
     95 int
     96 ether_output(ifp, m0, dst, rt0)
     97 	register struct ifnet *ifp;
     98 	struct mbuf *m0;
     99 	struct sockaddr *dst;
    100 	struct rtentry *rt0;
    101 {
    102 	u_int16_t etype;
    103 	int s, error = 0;
    104  	u_char edst[6];
    105 	register struct mbuf *m = m0;
    106 	register struct rtentry *rt;
    107 	struct mbuf *mcopy = (struct mbuf *)0;
    108 	register struct ether_header *eh;
    109 	struct arpcom *ac = (struct arpcom *)ifp;
    110 
    111 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
    112 		senderr(ENETDOWN);
    113 	ifp->if_lastchange = time;
    114 	if (rt = rt0) {
    115 		if ((rt->rt_flags & RTF_UP) == 0) {
    116 			if (rt0 = rt = rtalloc1(dst, 1))
    117 				rt->rt_refcnt--;
    118 			else
    119 				senderr(EHOSTUNREACH);
    120 		}
    121 		if (rt->rt_flags & RTF_GATEWAY) {
    122 			if (rt->rt_gwroute == 0)
    123 				goto lookup;
    124 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
    125 				rtfree(rt); rt = rt0;
    126 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
    127 				if ((rt = rt->rt_gwroute) == 0)
    128 					senderr(EHOSTUNREACH);
    129 			}
    130 		}
    131 		if (rt->rt_flags & RTF_REJECT)
    132 			if (rt->rt_rmx.rmx_expire == 0 ||
    133 			    time.tv_sec < rt->rt_rmx.rmx_expire)
    134 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
    135 	}
    136 	switch (dst->sa_family) {
    137 
    138 #ifdef INET
    139 	case AF_INET:
    140 		if (!arpresolve(ac, rt, m, dst, edst))
    141 			return (0);	/* if not yet resolved */
    142 		/* If broadcasting on a simplex interface, loopback a copy */
    143 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    144 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    145 		etype = ETHERTYPE_IP;
    146 		break;
    147 #endif
    148 #ifdef NS
    149 	case AF_NS:
    150 		etype = ETHERTYPE_NS;
    151  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
    152 		    (caddr_t)edst, sizeof (edst));
    153 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
    154 			return (looutput(ifp, m, dst, rt));
    155 		/* If broadcasting on a simplex interface, loopback a copy */
    156 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    157 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    158 		break;
    159 #endif
    160 #ifdef	ISO
    161 	case AF_ISO: {
    162 		int	snpalen;
    163 		struct	llc *l;
    164 		register struct sockaddr_dl *sdl;
    165 
    166 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
    167 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
    168 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
    169 		} else if (error =
    170 			    iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
    171 					    (char *)edst, &snpalen))
    172 			goto bad; /* Not Resolved */
    173 		/* If broadcasting on a simplex interface, loopback a copy */
    174 		if (*edst & 1)
    175 			m->m_flags |= (M_BCAST|M_MCAST);
    176 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
    177 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
    178 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
    179 			if (mcopy) {
    180 				eh = mtod(mcopy, struct ether_header *);
    181 				bcopy((caddr_t)edst,
    182 				      (caddr_t)eh->ether_dhost, sizeof (edst));
    183 				bcopy((caddr_t)ac->ac_enaddr,
    184 				      (caddr_t)eh->ether_shost, sizeof (edst));
    185 			}
    186 		}
    187 		M_PREPEND(m, 3, M_DONTWAIT);
    188 		if (m == NULL)
    189 			return (0);
    190 		etype = m->m_pkthdr.len;
    191 		l = mtod(m, struct llc *);
    192 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
    193 		l->llc_control = LLC_UI;
    194 		IFDEBUG(D_ETHER)
    195 			int i;
    196 			printf("unoutput: sending pkt to: ");
    197 			for (i=0; i<6; i++)
    198 				printf("%x ", edst[i] & 0xff);
    199 			printf("\n");
    200 		ENDDEBUG
    201 		} break;
    202 #endif /* ISO */
    203 #ifdef	LLC
    204 /*	case AF_NSAP: */
    205 	case AF_CCITT: {
    206 		register struct sockaddr_dl *sdl =
    207 			(struct sockaddr_dl *) rt -> rt_gateway;
    208 
    209 		if (sdl && sdl->sdl_family == AF_LINK
    210 		    && sdl->sdl_alen > 0) {
    211 			bcopy(LLADDR(sdl), (char *)edst,
    212 				sizeof(edst));
    213 		} else goto bad; /* Not a link interface ? Funny ... */
    214 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
    215 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
    216 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
    217 			if (mcopy) {
    218 				eh = mtod(mcopy, struct ether_header *);
    219 				bcopy((caddr_t)edst,
    220 				      (caddr_t)eh->ether_dhost, sizeof (edst));
    221 				bcopy((caddr_t)ac->ac_enaddr,
    222 				      (caddr_t)eh->ether_shost, sizeof (edst));
    223 			}
    224 		}
    225 		etype = m->m_pkthdr.len;
    226 #ifdef LLC_DEBUG
    227 		{
    228 			int i;
    229 			register struct llc *l = mtod(m, struct llc *);
    230 
    231 			printf("ether_output: sending LLC2 pkt to: ");
    232 			for (i=0; i<6; i++)
    233 				printf("%x ", edst[i] & 0xff);
    234 			printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
    235 			       etype & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
    236 			       l->llc_control & 0xff);
    237 
    238 		}
    239 #endif /* LLC_DEBUG */
    240 		} break;
    241 #endif /* LLC */
    242 
    243 	case AF_UNSPEC:
    244 		eh = (struct ether_header *)dst->sa_data;
    245  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
    246 		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
    247 		etype = ntohs(eh->ether_type);
    248 		break;
    249 
    250 	default:
    251 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
    252 			dst->sa_family);
    253 		senderr(EAFNOSUPPORT);
    254 	}
    255 
    256 
    257 	if (mcopy)
    258 		(void) looutput(ifp, mcopy, dst, rt);
    259 	/*
    260 	 * Add local net header.  If no space in first mbuf,
    261 	 * allocate another.
    262 	 */
    263 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
    264 	if (m == 0)
    265 		senderr(ENOBUFS);
    266 	eh = mtod(m, struct ether_header *);
    267 	etype = htons(etype);
    268 	bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type,
    269 		sizeof(eh->ether_type));
    270  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
    271  	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
    272 	    sizeof(eh->ether_shost));
    273 	s = splimp();
    274 	/*
    275 	 * Queue message on interface, and start output if interface
    276 	 * not yet active.
    277 	 */
    278 	if (IF_QFULL(&ifp->if_snd)) {
    279 		IF_DROP(&ifp->if_snd);
    280 		splx(s);
    281 		senderr(ENOBUFS);
    282 	}
    283 	ifp->if_obytes += m->m_pkthdr.len;
    284 	IF_ENQUEUE(&ifp->if_snd, m);
    285 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    286 		(*ifp->if_start)(ifp);
    287 	splx(s);
    288 	if (m->m_flags & M_MCAST)
    289 		ifp->if_omcasts++;
    290 	return (error);
    291 
    292 bad:
    293 	if (m)
    294 		m_freem(m);
    295 	return (error);
    296 }
    297 
    298 /*
    299  * Process a received Ethernet packet;
    300  * the packet is in the mbuf chain m without
    301  * the ether header, which is provided separately.
    302  */
    303 void
    304 ether_input(ifp, eh, m)
    305 	struct ifnet *ifp;
    306 	register struct ether_header *eh;
    307 	struct mbuf *m;
    308 {
    309 	register struct ifqueue *inq;
    310 	register struct llc *l;
    311 	u_int16_t etype;
    312 	struct arpcom *ac = (struct arpcom *)ifp;
    313 	int s;
    314 
    315 	if ((ifp->if_flags & IFF_UP) == 0) {
    316 		m_freem(m);
    317 		return;
    318 	}
    319 	ifp->if_lastchange = time;
    320 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
    321 	if (eh->ether_dhost[0] & 1) {
    322 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
    323 		    sizeof(etherbroadcastaddr)) == 0)
    324 			m->m_flags |= M_BCAST;
    325 		else
    326 			m->m_flags |= M_MCAST;
    327 	}
    328 	if (m->m_flags & (M_BCAST|M_MCAST))
    329 		ifp->if_imcasts++;
    330 
    331 	etype = ntohs(eh->ether_type);
    332 	switch (etype) {
    333 #ifdef INET
    334 	case ETHERTYPE_IP:
    335 		schednetisr(NETISR_IP);
    336 		inq = &ipintrq;
    337 		break;
    338 
    339 	case ETHERTYPE_ARP:
    340 		schednetisr(NETISR_ARP);
    341 		inq = &arpintrq;
    342 		break;
    343 
    344 	case ETHERTYPE_REVARP:
    345 		revarpinput(m);	/* XXX queue? */
    346 		return;
    347 #endif
    348 #ifdef NS
    349 	case ETHERTYPE_NS:
    350 		schednetisr(NETISR_NS);
    351 		inq = &nsintrq;
    352 		break;
    353 
    354 #endif
    355 	default:
    356 #if defined (ISO) || defined (LLC)
    357 		if (etype > ETHERMTU)
    358 			goto dropanyway;
    359 		l = mtod(m, struct llc *);
    360 		switch (l->llc_dsap) {
    361 #ifdef	ISO
    362 		case LLC_ISO_LSAP:
    363 			switch (l->llc_control) {
    364 			case LLC_UI:
    365 				/* LLC_UI_P forbidden in class 1 service */
    366 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
    367 				    (l->llc_ssap == LLC_ISO_LSAP)) {
    368 					/* LSAP for ISO */
    369 					if (m->m_pkthdr.len > etype)
    370 						m_adj(m, etype - m->m_pkthdr.len);
    371 					m->m_data += 3;		/* XXX */
    372 					m->m_len -= 3;		/* XXX */
    373 					m->m_pkthdr.len -= 3;	/* XXX */
    374 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
    375 					if (m == 0)
    376 						return;
    377 					*mtod(m, struct ether_header *) = *eh;
    378 					IFDEBUG(D_ETHER)
    379 						printf("clnp packet");
    380 					ENDDEBUG
    381 					schednetisr(NETISR_ISO);
    382 					inq = &clnlintrq;
    383 					break;
    384 				}
    385 				goto dropanyway;
    386 
    387 			case LLC_XID:
    388 			case LLC_XID_P:
    389 				if(m->m_len < 6)
    390 					goto dropanyway;
    391 				l->llc_window = 0;
    392 				l->llc_fid = 9;
    393 				l->llc_class = 1;
    394 				l->llc_dsap = l->llc_ssap = 0;
    395 				/* Fall through to */
    396 			case LLC_TEST:
    397 			case LLC_TEST_P:
    398 			{
    399 				struct sockaddr sa;
    400 				register struct ether_header *eh2;
    401 				int i;
    402 				u_char c = l->llc_dsap;
    403 
    404 				l->llc_dsap = l->llc_ssap;
    405 				l->llc_ssap = c;
    406 				if (m->m_flags & (M_BCAST | M_MCAST))
    407 					bcopy((caddr_t)ac->ac_enaddr,
    408 					      (caddr_t)eh->ether_dhost, 6);
    409 				sa.sa_family = AF_UNSPEC;
    410 				sa.sa_len = sizeof(sa);
    411 				eh2 = (struct ether_header *)sa.sa_data;
    412 				for (i = 0; i < 6; i++) {
    413 					eh2->ether_shost[i] = c = eh->ether_dhost[i];
    414 					eh2->ether_dhost[i] =
    415 						eh->ether_dhost[i] = eh->ether_shost[i];
    416 					eh->ether_shost[i] = c;
    417 				}
    418 				ifp->if_output(ifp, m, &sa, NULL);
    419 				return;
    420 			}
    421 			default:
    422 				m_freem(m);
    423 				return;
    424 			}
    425 			break;
    426 #endif /* ISO */
    427 #ifdef LLC
    428 		case LLC_X25_LSAP:
    429 		{
    430 			if (m->m_pkthdr.len > etype)
    431 				m_adj(m, etype - m->m_pkthdr.len);
    432 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
    433 			if (m == 0)
    434 				return;
    435 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
    436 					    eh->ether_dhost, LLC_X25_LSAP, 6,
    437 					    mtod(m, struct sdl_hdr *)))
    438 				panic("ETHER cons addr failure");
    439 			mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
    440 #ifdef LLC_DEBUG
    441 				printf("llc packet\n");
    442 #endif /* LLC_DEBUG */
    443 			schednetisr(NETISR_CCITT);
    444 			inq = &llcintrq;
    445 			break;
    446 		}
    447 #endif /* LLC */
    448 		dropanyway:
    449 		default:
    450 			m_freem(m);
    451 			return;
    452 		}
    453 #else /* ISO || LLC */
    454 	    m_freem(m);
    455 	    return;
    456 #endif /* ISO || LLC */
    457 	}
    458 
    459 	s = splimp();
    460 	if (IF_QFULL(inq)) {
    461 		IF_DROP(inq);
    462 		m_freem(m);
    463 	} else
    464 		IF_ENQUEUE(inq, m);
    465 	splx(s);
    466 }
    467 
    468 /*
    469  * Convert Ethernet address to printable (loggable) representation.
    470  */
    471 static char digits[] = "0123456789abcdef";
    472 char *
    473 ether_sprintf(ap)
    474 	register u_char *ap;
    475 {
    476 	register i;
    477 	static char etherbuf[18];
    478 	register char *cp = etherbuf;
    479 
    480 	for (i = 0; i < 6; i++) {
    481 		*cp++ = digits[*ap >> 4];
    482 		*cp++ = digits[*ap++ & 0xf];
    483 		*cp++ = ':';
    484 	}
    485 	*--cp = 0;
    486 	return (etherbuf);
    487 }
    488 
    489 /*
    490  * Perform common duties while attaching to interface list
    491  */
    492 void
    493 ether_ifattach(ifp)
    494 	register struct ifnet *ifp;
    495 {
    496 	register struct ifaddr *ifa;
    497 	register struct sockaddr_dl *sdl;
    498 
    499 	ifp->if_type = IFT_ETHER;
    500 	ifp->if_addrlen = 6;
    501 	ifp->if_hdrlen = 14;
    502 	ifp->if_mtu = ETHERMTU;
    503 	ifp->if_output = ether_output;
    504 	for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
    505 	    ifa = ifa->ifa_list.tqe_next)
    506 		if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
    507 		    sdl->sdl_family == AF_LINK) {
    508 			sdl->sdl_type = IFT_ETHER;
    509 			sdl->sdl_alen = ifp->if_addrlen;
    510 			bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
    511 			      LLADDR(sdl), ifp->if_addrlen);
    512 			break;
    513 		}
    514 	LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
    515 }
    516 
    517 u_char	ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
    518 u_char	ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
    519 /*
    520  * Add an Ethernet multicast address or range of addresses to the list for a
    521  * given interface.
    522  */
    523 int
    524 ether_addmulti(ifr, ac)
    525 	struct ifreq *ifr;
    526 	register struct arpcom *ac;
    527 {
    528 	register struct ether_multi *enm;
    529 	struct sockaddr_in *sin;
    530 	u_char addrlo[6];
    531 	u_char addrhi[6];
    532 	int s = splimp();
    533 
    534 	switch (ifr->ifr_addr.sa_family) {
    535 
    536 	case AF_UNSPEC:
    537 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
    538 		bcopy(addrlo, addrhi, 6);
    539 		break;
    540 
    541 #ifdef INET
    542 	case AF_INET:
    543 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
    544 		if (sin->sin_addr.s_addr == INADDR_ANY) {
    545 			/*
    546 			 * An IP address of INADDR_ANY means listen to all
    547 			 * of the Ethernet multicast addresses used for IP.
    548 			 * (This is for the sake of IP multicast routers.)
    549 			 */
    550 			bcopy(ether_ipmulticast_min, addrlo, 6);
    551 			bcopy(ether_ipmulticast_max, addrhi, 6);
    552 		}
    553 		else {
    554 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
    555 			bcopy(addrlo, addrhi, 6);
    556 		}
    557 		break;
    558 #endif
    559 
    560 	default:
    561 		splx(s);
    562 		return (EAFNOSUPPORT);
    563 	}
    564 
    565 	/*
    566 	 * Verify that we have valid Ethernet multicast addresses.
    567 	 */
    568 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
    569 		splx(s);
    570 		return (EINVAL);
    571 	}
    572 	/*
    573 	 * See if the address range is already in the list.
    574 	 */
    575 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
    576 	if (enm != NULL) {
    577 		/*
    578 		 * Found it; just increment the reference count.
    579 		 */
    580 		++enm->enm_refcount;
    581 		splx(s);
    582 		return (0);
    583 	}
    584 	/*
    585 	 * New address or range; malloc a new multicast record
    586 	 * and link it into the interface's multicast list.
    587 	 */
    588 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
    589 	if (enm == NULL) {
    590 		splx(s);
    591 		return (ENOBUFS);
    592 	}
    593 	bcopy(addrlo, enm->enm_addrlo, 6);
    594 	bcopy(addrhi, enm->enm_addrhi, 6);
    595 	enm->enm_ac = ac;
    596 	enm->enm_refcount = 1;
    597 	LIST_INSERT_HEAD(&ac->ac_multiaddrs, enm, enm_list);
    598 	ac->ac_multicnt++;
    599 	splx(s);
    600 	/*
    601 	 * Return ENETRESET to inform the driver that the list has changed
    602 	 * and its reception filter should be adjusted accordingly.
    603 	 */
    604 	return (ENETRESET);
    605 }
    606 
    607 /*
    608  * Delete a multicast address record.
    609  */
    610 int
    611 ether_delmulti(ifr, ac)
    612 	struct ifreq *ifr;
    613 	register struct arpcom *ac;
    614 {
    615 	register struct ether_multi *enm;
    616 	register struct ether_multi **p;
    617 	struct sockaddr_in *sin;
    618 	u_char addrlo[6];
    619 	u_char addrhi[6];
    620 	int s = splimp();
    621 
    622 	switch (ifr->ifr_addr.sa_family) {
    623 
    624 	case AF_UNSPEC:
    625 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
    626 		bcopy(addrlo, addrhi, 6);
    627 		break;
    628 
    629 #ifdef INET
    630 	case AF_INET:
    631 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
    632 		if (sin->sin_addr.s_addr == INADDR_ANY) {
    633 			/*
    634 			 * An IP address of INADDR_ANY means stop listening
    635 			 * to the range of Ethernet multicast addresses used
    636 			 * for IP.
    637 			 */
    638 			bcopy(ether_ipmulticast_min, addrlo, 6);
    639 			bcopy(ether_ipmulticast_max, addrhi, 6);
    640 		}
    641 		else {
    642 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
    643 			bcopy(addrlo, addrhi, 6);
    644 		}
    645 		break;
    646 #endif
    647 
    648 	default:
    649 		splx(s);
    650 		return (EAFNOSUPPORT);
    651 	}
    652 
    653 	/*
    654 	 * Look up the address in our list.
    655 	 */
    656 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
    657 	if (enm == NULL) {
    658 		splx(s);
    659 		return (ENXIO);
    660 	}
    661 	if (--enm->enm_refcount != 0) {
    662 		/*
    663 		 * Still some claims to this record.
    664 		 */
    665 		splx(s);
    666 		return (0);
    667 	}
    668 	/*
    669 	 * No remaining claims to this record; unlink and free it.
    670 	 */
    671 	LIST_REMOVE(enm, enm_list);
    672 	free(enm, M_IFMADDR);
    673 	ac->ac_multicnt--;
    674 	splx(s);
    675 	/*
    676 	 * Return ENETRESET to inform the driver that the list has changed
    677 	 * and its reception filter should be adjusted accordingly.
    678 	 */
    679 	return (ENETRESET);
    680 }
    681