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