xref: /freebsd/sys/netinet/in.c (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
1 /*
2  * Copyright (c) 1982, 1986, 1991, 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  *	@(#)in.c	8.4 (Berkeley) 1/9/95
34  *	$Id: in.c,v 1.20 1995/12/09 20:43:52 phk Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/ioctl.h>
40 #include <sys/errno.h>
41 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/queue.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 
48 #include <net/if.h>
49 #include <net/route.h>
50 
51 #include <netinet/in_systm.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/if_ether.h>
55 
56 #include <netinet/igmp_var.h>
57 
58 /*
59  * This structure is used to keep track of in_multi chains which belong to
60  * deleted interface addresses.
61  */
62 static LIST_HEAD(, multi_kludge) in_mk; /* XXX BSS initialization */
63 
64 struct multi_kludge {
65 	LIST_ENTRY(multi_kludge) mk_entry;
66 	struct ifnet *mk_ifp;
67 	struct in_multihead mk_head;
68 };
69 
70 static void	in_socktrim __P((struct sockaddr_in *));
71 static int	in_ifinit __P((struct ifnet *,
72 	    struct in_ifaddr *, struct sockaddr_in *, int));
73 static void	in_ifscrub __P((struct ifnet *, struct in_ifaddr *));
74 
75 static int subnetsarelocal = 1;
76 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
77 	&subnetsarelocal, 0, "");
78 /*
79  * Return 1 if an internet address is for a ``local'' host
80  * (one to which we have a connection).  If subnetsarelocal
81  * is true, this includes other subnets of the local net.
82  * Otherwise, it includes only the directly-connected (sub)nets.
83  */
84 int
85 in_localaddr(in)
86 	struct in_addr in;
87 {
88 	register u_long i = ntohl(in.s_addr);
89 	register struct in_ifaddr *ia;
90 
91 	if (subnetsarelocal) {
92 		for (ia = in_ifaddr; ia; ia = ia->ia_next)
93 			if ((i & ia->ia_netmask) == ia->ia_net)
94 				return (1);
95 	} else {
96 		for (ia = in_ifaddr; ia; ia = ia->ia_next)
97 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
98 				return (1);
99 	}
100 	return (0);
101 }
102 
103 /*
104  * Determine whether an IP address is in a reserved set of addresses
105  * that may not be forwarded, or whether datagrams to that destination
106  * may be forwarded.
107  */
108 int
109 in_canforward(in)
110 	struct in_addr in;
111 {
112 	register u_long i = ntohl(in.s_addr);
113 	register u_long net;
114 
115 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
116 		return (0);
117 	if (IN_CLASSA(i)) {
118 		net = i & IN_CLASSA_NET;
119 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
120 			return (0);
121 	}
122 	return (1);
123 }
124 
125 /*
126  * Trim a mask in a sockaddr
127  */
128 static void
129 in_socktrim(ap)
130 struct sockaddr_in *ap;
131 {
132     register char *cplim = (char *) &ap->sin_addr;
133     register char *cp = (char *) (&ap->sin_addr + 1);
134 
135     ap->sin_len = 0;
136     while (--cp >= cplim)
137         if (*cp) {
138 	    (ap)->sin_len = cp - (char *) (ap) + 1;
139 	    break;
140 	}
141 }
142 
143 static int in_interfaces;	/* number of external internet interfaces */
144 
145 /*
146  * Generic internet control operations (ioctl's).
147  * Ifp is 0 if not an interface-specific ioctl.
148  */
149 /* ARGSUSED */
150 int
151 in_control(so, cmd, data, ifp)
152 	struct socket *so;
153 	u_long cmd;
154 	caddr_t data;
155 	register struct ifnet *ifp;
156 {
157 	register struct ifreq *ifr = (struct ifreq *)data;
158 	register struct in_ifaddr *ia = 0;
159 	register struct ifaddr *ifa;
160 	struct in_ifaddr *oia;
161 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
162 	struct sockaddr_in oldaddr;
163 	int error, hostIsNew, maskIsNew;
164 	u_long i;
165 	struct multi_kludge *mk;
166 
167 	/*
168 	 * Find address for this interface, if it exists.
169 	 */
170 	if (ifp)
171 		for (ia = in_ifaddr; ia; ia = ia->ia_next)
172 			if (ia->ia_ifp == ifp)
173 				break;
174 
175 	switch (cmd) {
176 
177 	case SIOCAIFADDR:
178 	case SIOCDIFADDR:
179 		if (ifra->ifra_addr.sin_family == AF_INET) {
180 			for (oia = ia; ia; ia = ia->ia_next) {
181 				if (ia->ia_ifp == ifp  &&
182 				    ia->ia_addr.sin_addr.s_addr ==
183 				    ifra->ifra_addr.sin_addr.s_addr)
184 					break;
185 			}
186 			if ((ifp->if_flags & IFF_POINTOPOINT)
187 			    && (cmd == SIOCAIFADDR)
188 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
189 				== INADDR_ANY)) {
190 				return EDESTADDRREQ;
191 			}
192 		}
193 		if (cmd == SIOCDIFADDR && ia == 0)
194 			return (EADDRNOTAVAIL);
195 		/* FALLTHROUGH */
196 	case SIOCSIFADDR:
197 	case SIOCSIFNETMASK:
198 	case SIOCSIFDSTADDR:
199 		if ((so->so_state & SS_PRIV) == 0)
200 			return (EPERM);
201 
202 		if (ifp == 0)
203 			panic("in_control");
204 		if (ia == (struct in_ifaddr *)0) {
205 			oia = (struct in_ifaddr *)
206 				malloc(sizeof *oia, M_IFADDR, M_WAITOK);
207 			if (oia == (struct in_ifaddr *)NULL)
208 				return (ENOBUFS);
209 			bzero((caddr_t)oia, sizeof *oia);
210 			ia = in_ifaddr;
211 			if (ia) {
212 				for ( ; ia->ia_next; ia = ia->ia_next)
213 					continue;
214 				ia->ia_next = oia;
215 			} else
216 				in_ifaddr = oia;
217 			ia = oia;
218 			ifa = ifp->if_addrlist;
219 			if (ifa) {
220 				for ( ; ifa->ifa_next; ifa = ifa->ifa_next)
221 					continue;
222 				ifa->ifa_next = (struct ifaddr *) ia;
223 			} else
224 				ifp->if_addrlist = (struct ifaddr *) ia;
225 			ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
226 			ia->ia_ifa.ifa_dstaddr
227 					= (struct sockaddr *)&ia->ia_dstaddr;
228 			ia->ia_ifa.ifa_netmask
229 					= (struct sockaddr *)&ia->ia_sockmask;
230 			ia->ia_sockmask.sin_len = 8;
231 			if (ifp->if_flags & IFF_BROADCAST) {
232 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
233 				ia->ia_broadaddr.sin_family = AF_INET;
234 			}
235 			ia->ia_ifp = ifp;
236 			if (!(ifp->if_flags & IFF_LOOPBACK))
237 				in_interfaces++;
238 		}
239 		break;
240 
241 	case SIOCSIFBRDADDR:
242 		if ((so->so_state & SS_PRIV) == 0)
243 			return (EPERM);
244 		/* FALLTHROUGH */
245 
246 	case SIOCGIFADDR:
247 	case SIOCGIFNETMASK:
248 	case SIOCGIFDSTADDR:
249 	case SIOCGIFBRDADDR:
250 		if (ia == (struct in_ifaddr *)0)
251 			return (EADDRNOTAVAIL);
252 		break;
253 	}
254 	switch (cmd) {
255 
256 	case SIOCGIFADDR:
257 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
258 		break;
259 
260 	case SIOCGIFBRDADDR:
261 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
262 			return (EINVAL);
263 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
264 		break;
265 
266 	case SIOCGIFDSTADDR:
267 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
268 			return (EINVAL);
269 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
270 		break;
271 
272 	case SIOCGIFNETMASK:
273 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
274 		break;
275 
276 	case SIOCSIFDSTADDR:
277 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
278 			return (EINVAL);
279 		oldaddr = ia->ia_dstaddr;
280 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
281 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
282 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
283 			ia->ia_dstaddr = oldaddr;
284 			return (error);
285 		}
286 		if (ia->ia_flags & IFA_ROUTE) {
287 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
288 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
289 			ia->ia_ifa.ifa_dstaddr =
290 					(struct sockaddr *)&ia->ia_dstaddr;
291 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
292 		}
293 		break;
294 
295 	case SIOCSIFBRDADDR:
296 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
297 			return (EINVAL);
298 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
299 		break;
300 
301 	case SIOCSIFADDR:
302 		return (in_ifinit(ifp, ia,
303 		    (struct sockaddr_in *) &ifr->ifr_addr, 1));
304 
305 	case SIOCSIFNETMASK:
306 		i = ifra->ifra_addr.sin_addr.s_addr;
307 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i);
308 		break;
309 
310 	case SIOCAIFADDR:
311 		maskIsNew = 0;
312 		hostIsNew = 1;
313 		error = 0;
314 		if (ia->ia_addr.sin_family == AF_INET) {
315 			if (ifra->ifra_addr.sin_len == 0) {
316 				ifra->ifra_addr = ia->ia_addr;
317 				hostIsNew = 0;
318 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
319 					       ia->ia_addr.sin_addr.s_addr)
320 				hostIsNew = 0;
321 		}
322 		if (ifra->ifra_mask.sin_len) {
323 			in_ifscrub(ifp, ia);
324 			ia->ia_sockmask = ifra->ifra_mask;
325 			ia->ia_subnetmask =
326 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
327 			maskIsNew = 1;
328 		}
329 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
330 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
331 			in_ifscrub(ifp, ia);
332 			ia->ia_dstaddr = ifra->ifra_dstaddr;
333 			maskIsNew  = 1; /* We lie; but the effect's the same */
334 		}
335 		if (ifra->ifra_addr.sin_family == AF_INET &&
336 		    (hostIsNew || maskIsNew))
337 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
338 		if ((ifp->if_flags & IFF_BROADCAST) &&
339 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
340 			ia->ia_broadaddr = ifra->ifra_broadaddr;
341 		return (error);
342 
343 	case SIOCDIFADDR:
344 		mk = malloc(sizeof *mk, M_IPMADDR, M_WAITOK);
345 		if (!mk)
346 			return ENOBUFS;
347 
348 		in_ifscrub(ifp, ia);
349 		if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia)
350 			ifp->if_addrlist = ifa->ifa_next;
351 		else {
352 			while (ifa->ifa_next &&
353 			       (ifa->ifa_next != (struct ifaddr *)ia))
354 				    ifa = ifa->ifa_next;
355 			if (ifa->ifa_next)
356 				ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next;
357 			else
358 				printf("Couldn't unlink inifaddr from ifp\n");
359 		}
360 		oia = ia;
361 		if (oia == (ia = in_ifaddr))
362 			in_ifaddr = ia->ia_next;
363 		else {
364 			while (ia->ia_next && (ia->ia_next != oia))
365 				ia = ia->ia_next;
366 			if (ia->ia_next)
367 				ia->ia_next = oia->ia_next;
368 			else
369 				printf("Didn't unlink inifadr from list\n");
370 		}
371 
372 		if (!oia->ia_multiaddrs.lh_first) {
373 			IFAFREE(&oia->ia_ifa);
374 			FREE(mk, M_IPMADDR);
375 			break;
376 		}
377 
378 		/*
379 		 * Multicast address kludge:
380 		 * If there were any multicast addresses attached to this
381 		 * interface address, either move them to another address
382 		 * on this interface, or save them until such time as this
383 		 * interface is reconfigured for IP.
384 		 */
385 		IFP_TO_IA(oia->ia_ifp, ia);
386 		if (ia) {	/* there is another address */
387 			struct in_multi *inm;
388 			for(inm = oia->ia_multiaddrs.lh_first; inm;
389 			    inm = inm->inm_entry.le_next) {
390 				IFAFREE(&inm->inm_ia->ia_ifa);
391 				ia->ia_ifa.ifa_refcnt++;
392 				inm->inm_ia = ia;
393 				LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm,
394 						 inm_entry);
395 			}
396 			FREE(mk, M_IPMADDR);
397 		} else {	/* last address on this if deleted, save */
398 			struct in_multi *inm;
399 
400 			LIST_INIT(&mk->mk_head);
401 			mk->mk_ifp = ifp;
402 
403 			for(inm = oia->ia_multiaddrs.lh_first; inm;
404 			    inm = inm->inm_entry.le_next) {
405 				LIST_INSERT_HEAD(&mk->mk_head, inm, inm_entry);
406 			}
407 
408 			if (mk->mk_head.lh_first) {
409 				LIST_INSERT_HEAD(&in_mk, mk, mk_entry);
410 			} else {
411 				FREE(mk, M_IPMADDR);
412 			}
413 		}
414 
415 		IFAFREE((&oia->ia_ifa));
416 		break;
417 
418 	default:
419 		if (ifp == 0 || ifp->if_ioctl == 0)
420 			return (EOPNOTSUPP);
421 		return ((*ifp->if_ioctl)(ifp, cmd, data));
422 	}
423 	return (0);
424 }
425 
426 /*
427  * Delete any existing route for an interface.
428  */
429 static void
430 in_ifscrub(ifp, ia)
431 	register struct ifnet *ifp;
432 	register struct in_ifaddr *ia;
433 {
434 
435 	if ((ia->ia_flags & IFA_ROUTE) == 0)
436 		return;
437 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
438 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
439 	else
440 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
441 	ia->ia_flags &= ~IFA_ROUTE;
442 }
443 
444 /*
445  * Initialize an interface's internet address
446  * and routing table entry.
447  */
448 static int
449 in_ifinit(ifp, ia, sin, scrub)
450 	register struct ifnet *ifp;
451 	register struct in_ifaddr *ia;
452 	struct sockaddr_in *sin;
453 	int scrub;
454 {
455 	register u_long i = ntohl(sin->sin_addr.s_addr);
456 	struct sockaddr_in oldaddr;
457 	int s = splimp(), flags = RTF_UP, error;
458 	struct multi_kludge *mk;
459 
460 	oldaddr = ia->ia_addr;
461 	ia->ia_addr = *sin;
462 	/*
463 	 * Give the interface a chance to initialize
464 	 * if this is its first address,
465 	 * and to validate the address if necessary.
466 	 */
467 	if (ifp->if_ioctl &&
468 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
469 		splx(s);
470 		ia->ia_addr = oldaddr;
471 		return (error);
472 	}
473 	splx(s);
474 	if (scrub) {
475 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
476 		in_ifscrub(ifp, ia);
477 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
478 	}
479 	if (IN_CLASSA(i))
480 		ia->ia_netmask = IN_CLASSA_NET;
481 	else if (IN_CLASSB(i))
482 		ia->ia_netmask = IN_CLASSB_NET;
483 	else
484 		ia->ia_netmask = IN_CLASSC_NET;
485 	/*
486 	 * The subnet mask usually includes at least the standard network part,
487 	 * but may may be smaller in the case of supernetting.
488 	 * If it is set, we believe it.
489 	 */
490 	if (ia->ia_subnetmask == 0) {
491 		ia->ia_subnetmask = ia->ia_netmask;
492 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
493 	} else
494 		ia->ia_netmask &= ia->ia_subnetmask;
495 	ia->ia_net = i & ia->ia_netmask;
496 	ia->ia_subnet = i & ia->ia_subnetmask;
497 	in_socktrim(&ia->ia_sockmask);
498 	/*
499 	 * Add route for the network.
500 	 */
501 	ia->ia_ifa.ifa_metric = ifp->if_metric;
502 	if (ifp->if_flags & IFF_BROADCAST) {
503 		ia->ia_broadaddr.sin_addr.s_addr =
504 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
505 		ia->ia_netbroadcast.s_addr =
506 			htonl(ia->ia_net | ~ ia->ia_netmask);
507 	} else if (ifp->if_flags & IFF_LOOPBACK) {
508 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
509 		flags |= RTF_HOST;
510 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
511 		if (ia->ia_dstaddr.sin_family != AF_INET)
512 			return (0);
513 		flags |= RTF_HOST;
514 	}
515 	if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
516 		ia->ia_flags |= IFA_ROUTE;
517 
518 	LIST_INIT(&ia->ia_multiaddrs);
519 	/*
520 	 * If the interface supports multicast, join the "all hosts"
521 	 * multicast group on that interface.
522 	 */
523 	if (ifp->if_flags & IFF_MULTICAST) {
524 		struct in_addr addr;
525 
526 		/*
527 		 * Continuation of multicast address hack:
528 		 * If there was a multicast group list previously saved
529 		 * for this interface, then we re-attach it to the first
530 		 * address configured on the i/f.
531 		 */
532 		for(mk = in_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
533 			if(mk->mk_ifp == ifp) {
534 				struct in_multi *inm;
535 
536 				for(inm = mk->mk_head.lh_first; inm;
537 				    inm = inm->inm_entry.le_next) {
538 					IFAFREE(&inm->inm_ia->ia_ifa);
539 					ia->ia_ifa.ifa_refcnt++;
540 					inm->inm_ia = ia;
541 					LIST_INSERT_HEAD(&ia->ia_multiaddrs,
542 							 inm, inm_entry);
543 				}
544 				LIST_REMOVE(mk, mk_entry);
545 				free(mk, M_IPMADDR);
546 				break;
547 			}
548 		}
549 
550 		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
551 		in_addmulti(&addr, ifp);
552 	}
553 	return (error);
554 }
555 
556 
557 /*
558  * Return 1 if the address might be a local broadcast address.
559  */
560 int
561 in_broadcast(in, ifp)
562 	struct in_addr in;
563         struct ifnet *ifp;
564 {
565 	register struct ifaddr *ifa;
566 	u_long t;
567 
568 	if (in.s_addr == INADDR_BROADCAST ||
569 	    in.s_addr == INADDR_ANY)
570 		return 1;
571 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
572 		return 0;
573 	t = ntohl(in.s_addr);
574 	/*
575 	 * Look through the list of addresses for a match
576 	 * with a broadcast address.
577 	 */
578 #define ia ((struct in_ifaddr *)ifa)
579 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
580 		if (ifa->ifa_addr->sa_family == AF_INET &&
581 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
582 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
583 		     /*
584 		      * Check for old-style (host 0) broadcast.
585 		      */
586 		     t == ia->ia_subnet || t == ia->ia_net) &&
587 		     /*
588 		      * Check for an all one subnetmask. These
589 		      * only exist when an interface gets a secondary
590 		      * address.
591 		      */
592 		     ia->ia_subnetmask != (u_long)0xffffffff)
593 			    return 1;
594 	return (0);
595 #undef ia
596 }
597 /*
598  * Add an address to the list of IP multicast addresses for a given interface.
599  */
600 struct in_multi *
601 in_addmulti(ap, ifp)
602 	register struct in_addr *ap;
603 	register struct ifnet *ifp;
604 {
605 	register struct in_multi *inm;
606 	struct ifreq ifr;
607 	struct in_ifaddr *ia;
608 	int s = splnet();
609 
610 	/*
611 	 * See if address already in list.
612 	 */
613 	IN_LOOKUP_MULTI(*ap, ifp, inm);
614 	if (inm != NULL) {
615 		/*
616 		 * Found it; just increment the reference count.
617 		 */
618 		++inm->inm_refcount;
619 	}
620 	else {
621 		/*
622 		 * New address; allocate a new multicast record
623 		 * and link it into the interface's multicast list.
624 		 */
625 		inm = (struct in_multi *)malloc(sizeof(*inm),
626 		    M_IPMADDR, M_NOWAIT);
627 		if (inm == NULL) {
628 			splx(s);
629 			return (NULL);
630 		}
631 		inm->inm_addr = *ap;
632 		inm->inm_ifp = ifp;
633 		inm->inm_refcount = 1;
634 		IFP_TO_IA(ifp, ia);
635 		if (ia == NULL) {
636 			free(inm, M_IPMADDR);
637 			splx(s);
638 			return (NULL);
639 		}
640 		inm->inm_ia = ia;
641 		ia->ia_ifa.ifa_refcnt++; /* gain a reference */
642 		LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_entry);
643 
644 		/*
645 		 * Ask the network driver to update its multicast reception
646 		 * filter appropriately for the new address.
647 		 */
648 		((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET;
649 		((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr = *ap;
650 		if ((ifp->if_ioctl == NULL) ||
651 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
652 			LIST_REMOVE(inm, inm_entry);
653 			IFAFREE(&ia->ia_ifa); /* release reference */
654 			free(inm, M_IPMADDR);
655 			splx(s);
656 			return (NULL);
657 		}
658 		/*
659 		 * Let IGMP know that we have joined a new IP multicast group.
660 		 */
661 		igmp_joingroup(inm);
662 	}
663 	splx(s);
664 	return (inm);
665 }
666 
667 /*
668  * Delete a multicast address record.
669  */
670 void
671 in_delmulti(inm)
672 	register struct in_multi *inm;
673 {
674 	struct ifreq ifr;
675 	int s = splnet();
676 
677 	if (--inm->inm_refcount == 0) {
678 		/*
679 		 * No remaining claims to this record; let IGMP know that
680 		 * we are leaving the multicast group.
681 		 */
682 		igmp_leavegroup(inm);
683 		/*
684 		 * Unlink from list.
685 		 */
686 		LIST_REMOVE(inm, inm_entry);
687 		IFAFREE(&inm->inm_ia->ia_ifa); /* release reference */
688 
689 		/*
690 		 * Notify the network driver to update its multicast reception
691 		 * filter.
692 		 */
693 		((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
694 		((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr =
695 								inm->inm_addr;
696 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
697 							     (caddr_t)&ifr);
698 		free(inm, M_IPMADDR);
699 	}
700 	splx(s);
701 }
702