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