xref: /freebsd/sys/net/if_ethersubr.c (revision a316b26e50bbed7cf655fbba726ab87d8ab7599d)
1 /*
2  * Copyright (c) 1982, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
34  * $Id: if_ethersubr.c,v 1.4 1994/11/24 14:29:38 davidg Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/ioctl.h>
45 #include <sys/errno.h>
46 #include <sys/syslog.h>
47 
48 #include <machine/cpu.h>
49 
50 #include <net/if.h>
51 #include <net/netisr.h>
52 #include <net/route.h>
53 #include <net/if_llc.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 
57 #ifdef INET
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #endif
61 #include <netinet/if_ether.h>
62 
63 #ifdef NS
64 #include <netns/ns.h>
65 #include <netns/ns_if.h>
66 #endif
67 
68 #ifdef ISO
69 #include <netiso/argo_debug.h>
70 #include <netiso/iso.h>
71 #include <netiso/iso_var.h>
72 #include <netiso/iso_snpac.h>
73 #endif
74 
75 #ifdef LLC
76 #include <netccitt/dll.h>
77 #include <netccitt/llc_var.h>
78 #endif
79 
80 #if defined(LLC) && defined(CCITT)
81 extern struct ifqueue pkintrq;
82 #endif
83 
84 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
85 extern	struct ifnet loif;
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 	short type;
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 	int off, len = m->m_pkthdr.len;
110 	struct arpcom *ac = (struct arpcom *)ifp;
111 
112 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
113 		senderr(ENETDOWN);
114 	ifp->if_lastchange = time;
115 	if (rt = rt0) {
116 		if ((rt->rt_flags & RTF_UP) == 0) {
117 			if (rt0 = rt = rtalloc1(dst, 1, 0UL))
118 				rt->rt_refcnt--;
119 			else
120 				senderr(EHOSTUNREACH);
121 		}
122 		if (rt->rt_flags & RTF_GATEWAY) {
123 			if (rt->rt_gwroute == 0)
124 				goto lookup;
125 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
126 				rtfree(rt); rt = rt0;
127 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
128 							  0UL);
129 				if ((rt = rt->rt_gwroute) == 0)
130 					senderr(EHOSTUNREACH);
131 			}
132 		}
133 		if (rt->rt_flags & RTF_REJECT)
134 			if (rt->rt_rmx.rmx_expire == 0 ||
135 			    time.tv_sec < rt->rt_rmx.rmx_expire)
136 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
137 	}
138 	switch (dst->sa_family) {
139 
140 #ifdef INET
141 	case AF_INET:
142 		if (!arpresolve(ac, rt, m, dst, edst, rt0))
143 			return (0);	/* if not yet resolved */
144 		/* If broadcasting on a simplex interface, loopback a copy */
145 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
146 			mcopy = m_copy(m, 0, (int)M_COPYALL);
147 		off = m->m_pkthdr.len - m->m_len;
148 		type = ETHERTYPE_IP;
149 		break;
150 #endif
151 #ifdef NS
152 	case AF_NS:
153 		type = ETHERTYPE_NS;
154  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
155 		    (caddr_t)edst, sizeof (edst));
156 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
157 			return (looutput(ifp, m, dst, rt));
158 		/* If broadcasting on a simplex interface, loopback a copy */
159 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
160 			mcopy = m_copy(m, 0, (int)M_COPYALL);
161 		break;
162 #endif
163 #ifdef	ISO
164 	case AF_ISO: {
165 		int	snpalen;
166 		struct	llc *l;
167 		register struct sockaddr_dl *sdl;
168 
169 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
170 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
171 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
172 		} else if (error =
173 			    iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
174 					    (char *)edst, &snpalen))
175 			goto bad; /* Not Resolved */
176 		/* If broadcasting on a simplex interface, loopback a copy */
177 		if (*edst & 1)
178 			m->m_flags |= (M_BCAST|M_MCAST);
179 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
180 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
181 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
182 			if (mcopy) {
183 				eh = mtod(mcopy, struct ether_header *);
184 				bcopy((caddr_t)edst,
185 				      (caddr_t)eh->ether_dhost, sizeof (edst));
186 				bcopy((caddr_t)ac->ac_enaddr,
187 				      (caddr_t)eh->ether_shost, sizeof (edst));
188 			}
189 		}
190 		M_PREPEND(m, 3, M_DONTWAIT);
191 		if (m == NULL)
192 			return (0);
193 		type = m->m_pkthdr.len;
194 		l = mtod(m, struct llc *);
195 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
196 		l->llc_control = LLC_UI;
197 		len += 3;
198 		IFDEBUG(D_ETHER)
199 			int i;
200 			printf("unoutput: sending pkt to: ");
201 			for (i=0; i<6; i++)
202 				printf("%x ", edst[i] & 0xff);
203 			printf("\n");
204 		ENDDEBUG
205 		} break;
206 #endif /* ISO */
207 #ifdef	LLC
208 /*	case AF_NSAP: */
209 	case AF_CCITT: {
210 		register struct sockaddr_dl *sdl =
211 			(struct sockaddr_dl *) rt -> rt_gateway;
212 
213 		if (sdl && sdl->sdl_family == AF_LINK
214 		    && sdl->sdl_alen > 0) {
215 			bcopy(LLADDR(sdl), (char *)edst,
216 				sizeof(edst));
217 		} else goto bad; /* Not a link interface ? Funny ... */
218 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
219 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
220 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
221 			if (mcopy) {
222 				eh = mtod(mcopy, struct ether_header *);
223 				bcopy((caddr_t)edst,
224 				      (caddr_t)eh->ether_dhost, sizeof (edst));
225 				bcopy((caddr_t)ac->ac_enaddr,
226 				      (caddr_t)eh->ether_shost, sizeof (edst));
227 			}
228 		}
229 		type = m->m_pkthdr.len;
230 #ifdef LLC_DEBUG
231 		{
232 			int i;
233 			register struct llc *l = mtod(m, struct llc *);
234 
235 			printf("ether_output: sending LLC2 pkt to: ");
236 			for (i=0; i<6; i++)
237 				printf("%x ", edst[i] & 0xff);
238 			printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
239 			       type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
240 			       l->llc_control & 0xff);
241 
242 		}
243 #endif /* LLC_DEBUG */
244 		} break;
245 #endif /* LLC */
246 
247 	case AF_UNSPEC:
248 		eh = (struct ether_header *)dst->sa_data;
249  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
250 		type = eh->ether_type;
251 		break;
252 
253 	default:
254 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
255 			dst->sa_family);
256 		senderr(EAFNOSUPPORT);
257 	}
258 
259 
260 	if (mcopy)
261 		(void) looutput(ifp, mcopy, dst, rt);
262 	/*
263 	 * Add local net header.  If no space in first mbuf,
264 	 * allocate another.
265 	 */
266 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
267 	if (m == 0)
268 		senderr(ENOBUFS);
269 	eh = mtod(m, struct ether_header *);
270 	type = htons((u_short)type);
271 	bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
272 		sizeof(eh->ether_type));
273  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
274  	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
275 	    sizeof(eh->ether_shost));
276 	s = splimp();
277 	/*
278 	 * Queue message on interface, and start output if interface
279 	 * not yet active.
280 	 */
281 	if (IF_QFULL(&ifp->if_snd)) {
282 		IF_DROP(&ifp->if_snd);
283 		splx(s);
284 		senderr(ENOBUFS);
285 	}
286 	IF_ENQUEUE(&ifp->if_snd, m);
287 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
288 		(*ifp->if_start)(ifp);
289 	splx(s);
290 	ifp->if_obytes += len + sizeof (struct ether_header);
291 	if (m->m_flags & M_MCAST)
292 		ifp->if_omcasts++;
293 	return (error);
294 
295 bad:
296 	if (m)
297 		m_freem(m);
298 	return (error);
299 }
300 
301 /*
302  * Process a received Ethernet packet;
303  * the packet is in the mbuf chain m without
304  * the ether header, which is provided separately.
305  */
306 void
307 ether_input(ifp, eh, m)
308 	struct ifnet *ifp;
309 	register struct ether_header *eh;
310 	struct mbuf *m;
311 {
312 	register struct ifqueue *inq;
313 	register struct llc *l;
314 	struct arpcom *ac = (struct arpcom *)ifp;
315 	u_short ether_type;
316 	int s;
317 
318 	if ((ifp->if_flags & IFF_UP) == 0) {
319 		m_freem(m);
320 		return;
321 	}
322 	ifp->if_lastchange = time;
323 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
324 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
325 	    sizeof(etherbroadcastaddr)) == 0)
326 		m->m_flags |= M_BCAST;
327 	else if (eh->ether_dhost[0] & 1)
328 		m->m_flags |= M_MCAST;
329 	if (m->m_flags & (M_BCAST|M_MCAST))
330 		ifp->if_imcasts++;
331 
332 	ether_type = ntohs(eh->ether_type);
333 
334 	switch (ether_type) {
335 #ifdef INET
336 	case ETHERTYPE_IP:
337 		schednetisr(NETISR_IP);
338 		inq = &ipintrq;
339 		break;
340 
341 	case ETHERTYPE_ARP:
342 		schednetisr(NETISR_ARP);
343 		inq = &arpintrq;
344 		break;
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 (ether_type > 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 > ether_type)
368 						m_adj(m, ether_type - 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 > ether_type)
429 				m_adj(m, ether_type - 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 = ether_type;
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 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
502 		if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
503 		    sdl->sdl_family == AF_LINK) {
504 			sdl->sdl_type = IFT_ETHER;
505 			sdl->sdl_alen = ifp->if_addrlen;
506 			bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
507 			      LLADDR(sdl), ifp->if_addrlen);
508 			break;
509 		}
510 }
511 
512 u_char	ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
513 u_char	ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
514 /*
515  * Add an Ethernet multicast address or range of addresses to the list for a
516  * given interface.
517  */
518 int
519 ether_addmulti(ifr, ac)
520 	struct ifreq *ifr;
521 	register struct arpcom *ac;
522 {
523 	register struct ether_multi *enm;
524 	struct sockaddr_in *sin;
525 	u_char addrlo[6];
526 	u_char addrhi[6];
527 	int s = splimp();
528 
529 	switch (ifr->ifr_addr.sa_family) {
530 
531 	case AF_UNSPEC:
532 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
533 		bcopy(addrlo, addrhi, 6);
534 		break;
535 
536 #ifdef INET
537 	case AF_INET:
538 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
539 		if (sin->sin_addr.s_addr == INADDR_ANY) {
540 			/*
541 			 * An IP address of INADDR_ANY means listen to all
542 			 * of the Ethernet multicast addresses used for IP.
543 			 * (This is for the sake of IP multicast routers.)
544 			 */
545 			bcopy(ether_ipmulticast_min, addrlo, 6);
546 			bcopy(ether_ipmulticast_max, addrhi, 6);
547 		}
548 		else {
549 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
550 			bcopy(addrlo, addrhi, 6);
551 		}
552 		break;
553 #endif
554 
555 	default:
556 		splx(s);
557 		return (EAFNOSUPPORT);
558 	}
559 
560 	/*
561 	 * Verify that we have valid Ethernet multicast addresses.
562 	 */
563 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
564 		splx(s);
565 		return (EINVAL);
566 	}
567 	/*
568 	 * See if the address range is already in the list.
569 	 */
570 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
571 	if (enm != NULL) {
572 		/*
573 		 * Found it; just increment the reference count.
574 		 */
575 		++enm->enm_refcount;
576 		splx(s);
577 		return (0);
578 	}
579 	/*
580 	 * New address or range; malloc a new multicast record
581 	 * and link it into the interface's multicast list.
582 	 */
583 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
584 	if (enm == NULL) {
585 		splx(s);
586 		return (ENOBUFS);
587 	}
588 	bcopy(addrlo, enm->enm_addrlo, 6);
589 	bcopy(addrhi, enm->enm_addrhi, 6);
590 	enm->enm_ac = ac;
591 	enm->enm_refcount = 1;
592 	enm->enm_next = ac->ac_multiaddrs;
593 	ac->ac_multiaddrs = enm;
594 	ac->ac_multicnt++;
595 	splx(s);
596 	/*
597 	 * Return ENETRESET to inform the driver that the list has changed
598 	 * and its reception filter should be adjusted accordingly.
599 	 */
600 	return (ENETRESET);
601 }
602 
603 /*
604  * Delete a multicast address record.
605  */
606 int
607 ether_delmulti(ifr, ac)
608 	struct ifreq *ifr;
609 	register struct arpcom *ac;
610 {
611 	register struct ether_multi *enm;
612 	register struct ether_multi **p;
613 	struct sockaddr_in *sin;
614 	u_char addrlo[6];
615 	u_char addrhi[6];
616 	int s = splimp();
617 
618 	switch (ifr->ifr_addr.sa_family) {
619 
620 	case AF_UNSPEC:
621 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
622 		bcopy(addrlo, addrhi, 6);
623 		break;
624 
625 #ifdef INET
626 	case AF_INET:
627 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
628 		if (sin->sin_addr.s_addr == INADDR_ANY) {
629 			/*
630 			 * An IP address of INADDR_ANY means stop listening
631 			 * to the range of Ethernet multicast addresses used
632 			 * for IP.
633 			 */
634 			bcopy(ether_ipmulticast_min, addrlo, 6);
635 			bcopy(ether_ipmulticast_max, addrhi, 6);
636 		}
637 		else {
638 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
639 			bcopy(addrlo, addrhi, 6);
640 		}
641 		break;
642 #endif
643 
644 	default:
645 		splx(s);
646 		return (EAFNOSUPPORT);
647 	}
648 
649 	/*
650 	 * Look up the address in our list.
651 	 */
652 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
653 	if (enm == NULL) {
654 		splx(s);
655 		return (ENXIO);
656 	}
657 	if (--enm->enm_refcount != 0) {
658 		/*
659 		 * Still some claims to this record.
660 		 */
661 		splx(s);
662 		return (0);
663 	}
664 	/*
665 	 * No remaining claims to this record; unlink and free it.
666 	 */
667 	for (p = &enm->enm_ac->ac_multiaddrs;
668 	     *p != enm;
669 	     p = &(*p)->enm_next)
670 		continue;
671 	*p = (*p)->enm_next;
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