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