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