in.c revision 8876
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)in.c	8.2 (Berkeley) 11/15/93
348876Srgrimes * $Id: in.c,v 1.13 1995/04/26 18:10:54 pst Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#include <sys/param.h>
381549Srgrimes#include <sys/systm.h>
391541Srgrimes#include <sys/ioctl.h>
401541Srgrimes#include <sys/errno.h>
411541Srgrimes#include <sys/malloc.h>
421541Srgrimes#include <sys/socket.h>
431541Srgrimes#include <sys/socketvar.h>
447280Swollman#include <sys/queue.h>
451541Srgrimes
461541Srgrimes#include <net/if.h>
471541Srgrimes#include <net/route.h>
481541Srgrimes
491541Srgrimes#include <netinet/in_systm.h>
501541Srgrimes#include <netinet/in.h>
511541Srgrimes#include <netinet/in_var.h>
521541Srgrimes#include <netinet/if_ether.h>
531541Srgrimes
546363Sphk#include <netinet/igmp_var.h>
556363Sphk
561541Srgrimes/*
577280Swollman * This structure is used to keep track of in_multi chains which belong to
587280Swollman * deleted interface addresses.
597280Swollman */
607280Swollmanstatic LIST_HEAD(, multi_kludge) in_mk; /* XXX BSS initialization */
617280Swollman
627280Swollmanstruct multi_kludge {
637280Swollman	LIST_ENTRY(multi_kludge) mk_entry;
647280Swollman	struct ifnet *mk_ifp;
657280Swollman	struct in_multihead mk_head;
667280Swollman};
677280Swollman
687280Swollman/*
691541Srgrimes * Return the network number from an internet address.
701541Srgrimes */
711541Srgrimesu_long
721541Srgrimesin_netof(in)
731541Srgrimes	struct in_addr in;
741541Srgrimes{
751541Srgrimes	register u_long i = ntohl(in.s_addr);
761541Srgrimes	register u_long net;
771541Srgrimes	register struct in_ifaddr *ia;
781541Srgrimes
791541Srgrimes	if (IN_CLASSA(i))
801541Srgrimes		net = i & IN_CLASSA_NET;
811541Srgrimes	else if (IN_CLASSB(i))
821541Srgrimes		net = i & IN_CLASSB_NET;
831541Srgrimes	else if (IN_CLASSC(i))
841541Srgrimes		net = i & IN_CLASSC_NET;
851541Srgrimes	else if (IN_CLASSD(i))
861541Srgrimes		net = i & IN_CLASSD_NET;
871541Srgrimes	else
881541Srgrimes		return (0);
891541Srgrimes
901541Srgrimes	/*
911541Srgrimes	 * Check whether network is a subnet;
921541Srgrimes	 * if so, return subnet number.
931541Srgrimes	 */
941541Srgrimes	for (ia = in_ifaddr; ia; ia = ia->ia_next)
951541Srgrimes		if (net == ia->ia_net)
961541Srgrimes			return (i & ia->ia_subnetmask);
971541Srgrimes	return (net);
981541Srgrimes}
991541Srgrimes
1001541Srgrimes#ifndef SUBNETSARELOCAL
1011541Srgrimes#define	SUBNETSARELOCAL	1
1021541Srgrimes#endif
1031541Srgrimesint subnetsarelocal = SUBNETSARELOCAL;
1041541Srgrimes/*
1051541Srgrimes * Return 1 if an internet address is for a ``local'' host
1061541Srgrimes * (one to which we have a connection).  If subnetsarelocal
1071541Srgrimes * is true, this includes other subnets of the local net.
1081541Srgrimes * Otherwise, it includes only the directly-connected (sub)nets.
1091541Srgrimes */
1101549Srgrimesint
1111541Srgrimesin_localaddr(in)
1121541Srgrimes	struct in_addr in;
1131541Srgrimes{
1141541Srgrimes	register u_long i = ntohl(in.s_addr);
1151541Srgrimes	register struct in_ifaddr *ia;
1161541Srgrimes
1171541Srgrimes	if (subnetsarelocal) {
1181541Srgrimes		for (ia = in_ifaddr; ia; ia = ia->ia_next)
1191541Srgrimes			if ((i & ia->ia_netmask) == ia->ia_net)
1201541Srgrimes				return (1);
1211541Srgrimes	} else {
1221541Srgrimes		for (ia = in_ifaddr; ia; ia = ia->ia_next)
1231541Srgrimes			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
1241541Srgrimes				return (1);
1251541Srgrimes	}
1261541Srgrimes	return (0);
1271541Srgrimes}
1281541Srgrimes
1291541Srgrimes/*
1301541Srgrimes * Determine whether an IP address is in a reserved set of addresses
1311541Srgrimes * that may not be forwarded, or whether datagrams to that destination
1321541Srgrimes * may be forwarded.
1331541Srgrimes */
1341549Srgrimesint
1351541Srgrimesin_canforward(in)
1361541Srgrimes	struct in_addr in;
1371541Srgrimes{
1381541Srgrimes	register u_long i = ntohl(in.s_addr);
1391541Srgrimes	register u_long net;
1401541Srgrimes
1411541Srgrimes	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
1421541Srgrimes		return (0);
1431541Srgrimes	if (IN_CLASSA(i)) {
1441541Srgrimes		net = i & IN_CLASSA_NET;
1451541Srgrimes		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
1461541Srgrimes			return (0);
1471541Srgrimes	}
1481541Srgrimes	return (1);
1491541Srgrimes}
1501541Srgrimes
1511541Srgrimes/*
1521541Srgrimes * Trim a mask in a sockaddr
1531541Srgrimes */
1541541Srgrimesvoid
1551541Srgrimesin_socktrim(ap)
1561541Srgrimesstruct sockaddr_in *ap;
1571541Srgrimes{
1581541Srgrimes    register char *cplim = (char *) &ap->sin_addr;
1591541Srgrimes    register char *cp = (char *) (&ap->sin_addr + 1);
1601541Srgrimes
1611541Srgrimes    ap->sin_len = 0;
1624127Swollman    while (--cp >= cplim)
1631541Srgrimes        if (*cp) {
1641541Srgrimes	    (ap)->sin_len = cp - (char *) (ap) + 1;
1651541Srgrimes	    break;
1661541Srgrimes	}
1671541Srgrimes}
1681541Srgrimes
1691541Srgrimesint	in_interfaces;		/* number of external internet interfaces */
1701541Srgrimes
1711541Srgrimes/*
1721541Srgrimes * Generic internet control operations (ioctl's).
1731541Srgrimes * Ifp is 0 if not an interface-specific ioctl.
1741541Srgrimes */
1751541Srgrimes/* ARGSUSED */
1761549Srgrimesint
1771541Srgrimesin_control(so, cmd, data, ifp)
1781541Srgrimes	struct socket *so;
1791541Srgrimes	int cmd;
1801541Srgrimes	caddr_t data;
1811541Srgrimes	register struct ifnet *ifp;
1821541Srgrimes{
1831541Srgrimes	register struct ifreq *ifr = (struct ifreq *)data;
1841541Srgrimes	register struct in_ifaddr *ia = 0;
1851541Srgrimes	register struct ifaddr *ifa;
1861541Srgrimes	struct in_ifaddr *oia;
1871541Srgrimes	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
1881541Srgrimes	struct sockaddr_in oldaddr;
1891541Srgrimes	int error, hostIsNew, maskIsNew;
1901541Srgrimes	u_long i;
1917280Swollman	struct multi_kludge *mk;
1921541Srgrimes
1931541Srgrimes	/*
1941541Srgrimes	 * Find address for this interface, if it exists.
1951541Srgrimes	 */
1961541Srgrimes	if (ifp)
1971541Srgrimes		for (ia = in_ifaddr; ia; ia = ia->ia_next)
1981541Srgrimes			if (ia->ia_ifp == ifp)
1991541Srgrimes				break;
2001541Srgrimes
2011541Srgrimes	switch (cmd) {
2021541Srgrimes
2031541Srgrimes	case SIOCAIFADDR:
2041541Srgrimes	case SIOCDIFADDR:
2058071Swollman		if (ifra->ifra_addr.sin_family == AF_INET) {
2068071Swollman			for (oia = ia; ia; ia = ia->ia_next) {
2078071Swollman				if (ia->ia_ifp == ifp  &&
2088071Swollman				    ia->ia_addr.sin_addr.s_addr ==
2098071Swollman				    ifra->ifra_addr.sin_addr.s_addr)
2108071Swollman					break;
2118071Swollman			}
2128876Srgrimes			if ((ifp->if_flags & IFF_POINTOPOINT)
2138071Swollman			    && (cmd == SIOCAIFADDR)
2148071Swollman			    && (ifra->ifra_dstaddr.sin_addr.s_addr
2158071Swollman				== INADDR_ANY)) {
2168071Swollman				return EADDRNOTAVAIL;
2178071Swollman			}
2181541Srgrimes		}
2191541Srgrimes		if (cmd == SIOCDIFADDR && ia == 0)
2201541Srgrimes			return (EADDRNOTAVAIL);
2211541Srgrimes		/* FALLTHROUGH */
2221541Srgrimes	case SIOCSIFADDR:
2231541Srgrimes	case SIOCSIFNETMASK:
2241541Srgrimes	case SIOCSIFDSTADDR:
2251541Srgrimes		if ((so->so_state & SS_PRIV) == 0)
2261541Srgrimes			return (EPERM);
2271541Srgrimes
2281541Srgrimes		if (ifp == 0)
2291541Srgrimes			panic("in_control");
2301541Srgrimes		if (ia == (struct in_ifaddr *)0) {
2311541Srgrimes			oia = (struct in_ifaddr *)
2321541Srgrimes				malloc(sizeof *oia, M_IFADDR, M_WAITOK);
2331541Srgrimes			if (oia == (struct in_ifaddr *)NULL)
2341541Srgrimes				return (ENOBUFS);
2351541Srgrimes			bzero((caddr_t)oia, sizeof *oia);
2363311Sphk			ia = in_ifaddr;
2373311Sphk			if (ia) {
2381541Srgrimes				for ( ; ia->ia_next; ia = ia->ia_next)
2391541Srgrimes					continue;
2401541Srgrimes				ia->ia_next = oia;
2411541Srgrimes			} else
2421541Srgrimes				in_ifaddr = oia;
2431541Srgrimes			ia = oia;
2443311Sphk			ifa = ifp->if_addrlist;
2453311Sphk			if (ifa) {
2461541Srgrimes				for ( ; ifa->ifa_next; ifa = ifa->ifa_next)
2471541Srgrimes					continue;
2481541Srgrimes				ifa->ifa_next = (struct ifaddr *) ia;
2491541Srgrimes			} else
2501541Srgrimes				ifp->if_addrlist = (struct ifaddr *) ia;
2511541Srgrimes			ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
2521541Srgrimes			ia->ia_ifa.ifa_dstaddr
2531541Srgrimes					= (struct sockaddr *)&ia->ia_dstaddr;
2541541Srgrimes			ia->ia_ifa.ifa_netmask
2551541Srgrimes					= (struct sockaddr *)&ia->ia_sockmask;
2561541Srgrimes			ia->ia_sockmask.sin_len = 8;
2571541Srgrimes			if (ifp->if_flags & IFF_BROADCAST) {
2581541Srgrimes				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
2591541Srgrimes				ia->ia_broadaddr.sin_family = AF_INET;
2601541Srgrimes			}
2611541Srgrimes			ia->ia_ifp = ifp;
2628090Spst			if (!(ifp->if_flags & IFF_LOOPBACK))
2631541Srgrimes				in_interfaces++;
2641541Srgrimes		}
2651541Srgrimes		break;
2661541Srgrimes
2671541Srgrimes	case SIOCSIFBRDADDR:
2681541Srgrimes		if ((so->so_state & SS_PRIV) == 0)
2691541Srgrimes			return (EPERM);
2701541Srgrimes		/* FALLTHROUGH */
2711541Srgrimes
2721541Srgrimes	case SIOCGIFADDR:
2731541Srgrimes	case SIOCGIFNETMASK:
2741541Srgrimes	case SIOCGIFDSTADDR:
2751541Srgrimes	case SIOCGIFBRDADDR:
2761541Srgrimes		if (ia == (struct in_ifaddr *)0)
2771541Srgrimes			return (EADDRNOTAVAIL);
2781541Srgrimes		break;
2791541Srgrimes	}
2801541Srgrimes	switch (cmd) {
2811541Srgrimes
2821541Srgrimes	case SIOCGIFADDR:
2831541Srgrimes		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
2841541Srgrimes		break;
2851541Srgrimes
2861541Srgrimes	case SIOCGIFBRDADDR:
2871541Srgrimes		if ((ifp->if_flags & IFF_BROADCAST) == 0)
2881541Srgrimes			return (EINVAL);
2891541Srgrimes		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
2901541Srgrimes		break;
2911541Srgrimes
2921541Srgrimes	case SIOCGIFDSTADDR:
2931541Srgrimes		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2941541Srgrimes			return (EINVAL);
2951541Srgrimes		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
2961541Srgrimes		break;
2971541Srgrimes
2981541Srgrimes	case SIOCGIFNETMASK:
2991541Srgrimes		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
3001541Srgrimes		break;
3011541Srgrimes
3021541Srgrimes	case SIOCSIFDSTADDR:
3031541Srgrimes		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
3041541Srgrimes			return (EINVAL);
3051541Srgrimes		oldaddr = ia->ia_dstaddr;
3061541Srgrimes		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
3071541Srgrimes		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
3081541Srgrimes					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
3091541Srgrimes			ia->ia_dstaddr = oldaddr;
3101541Srgrimes			return (error);
3111541Srgrimes		}
3121541Srgrimes		if (ia->ia_flags & IFA_ROUTE) {
3131541Srgrimes			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
3141541Srgrimes			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
3151541Srgrimes			ia->ia_ifa.ifa_dstaddr =
3161541Srgrimes					(struct sockaddr *)&ia->ia_dstaddr;
3171541Srgrimes			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
3181541Srgrimes		}
3191541Srgrimes		break;
3201541Srgrimes
3211541Srgrimes	case SIOCSIFBRDADDR:
3221541Srgrimes		if ((ifp->if_flags & IFF_BROADCAST) == 0)
3231541Srgrimes			return (EINVAL);
3241541Srgrimes		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
3251541Srgrimes		break;
3261541Srgrimes
3271541Srgrimes	case SIOCSIFADDR:
3281541Srgrimes		return (in_ifinit(ifp, ia,
3291541Srgrimes		    (struct sockaddr_in *) &ifr->ifr_addr, 1));
3301541Srgrimes
3311541Srgrimes	case SIOCSIFNETMASK:
3321541Srgrimes		i = ifra->ifra_addr.sin_addr.s_addr;
3331541Srgrimes		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i);
3341541Srgrimes		break;
3351541Srgrimes
3361541Srgrimes	case SIOCAIFADDR:
3371541Srgrimes		maskIsNew = 0;
3381541Srgrimes		hostIsNew = 1;
3391541Srgrimes		error = 0;
3401541Srgrimes		if (ia->ia_addr.sin_family == AF_INET) {
3411541Srgrimes			if (ifra->ifra_addr.sin_len == 0) {
3421541Srgrimes				ifra->ifra_addr = ia->ia_addr;
3431541Srgrimes				hostIsNew = 0;
3441541Srgrimes			} else if (ifra->ifra_addr.sin_addr.s_addr ==
3451541Srgrimes					       ia->ia_addr.sin_addr.s_addr)
3461541Srgrimes				hostIsNew = 0;
3471541Srgrimes		}
3481541Srgrimes		if (ifra->ifra_mask.sin_len) {
3491541Srgrimes			in_ifscrub(ifp, ia);
3501541Srgrimes			ia->ia_sockmask = ifra->ifra_mask;
3511541Srgrimes			ia->ia_subnetmask =
3521541Srgrimes			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
3531541Srgrimes			maskIsNew = 1;
3541541Srgrimes		}
3551541Srgrimes		if ((ifp->if_flags & IFF_POINTOPOINT) &&
3561541Srgrimes		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
3571541Srgrimes			in_ifscrub(ifp, ia);
3581541Srgrimes			ia->ia_dstaddr = ifra->ifra_dstaddr;
3591541Srgrimes			maskIsNew  = 1; /* We lie; but the effect's the same */
3601541Srgrimes		}
3611541Srgrimes		if (ifra->ifra_addr.sin_family == AF_INET &&
3621541Srgrimes		    (hostIsNew || maskIsNew))
3631541Srgrimes			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
3641541Srgrimes		if ((ifp->if_flags & IFF_BROADCAST) &&
3651541Srgrimes		    (ifra->ifra_broadaddr.sin_family == AF_INET))
3661541Srgrimes			ia->ia_broadaddr = ifra->ifra_broadaddr;
3671541Srgrimes		return (error);
3681541Srgrimes
3691541Srgrimes	case SIOCDIFADDR:
3707280Swollman		mk = malloc(sizeof *mk, M_IPMADDR, M_WAITOK);
3717280Swollman		if (!mk)
3727280Swollman			return ENOBUFS;
3737280Swollman
3741541Srgrimes		in_ifscrub(ifp, ia);
3751541Srgrimes		if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia)
3761541Srgrimes			ifp->if_addrlist = ifa->ifa_next;
3771541Srgrimes		else {
3781541Srgrimes			while (ifa->ifa_next &&
3791541Srgrimes			       (ifa->ifa_next != (struct ifaddr *)ia))
3801541Srgrimes				    ifa = ifa->ifa_next;
3811541Srgrimes			if (ifa->ifa_next)
3821541Srgrimes				ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next;
3831541Srgrimes			else
3841541Srgrimes				printf("Couldn't unlink inifaddr from ifp\n");
3851541Srgrimes		}
3861541Srgrimes		oia = ia;
3871541Srgrimes		if (oia == (ia = in_ifaddr))
3881541Srgrimes			in_ifaddr = ia->ia_next;
3891541Srgrimes		else {
3901541Srgrimes			while (ia->ia_next && (ia->ia_next != oia))
3911541Srgrimes				ia = ia->ia_next;
3921541Srgrimes			if (ia->ia_next)
3931541Srgrimes				ia->ia_next = oia->ia_next;
3941541Srgrimes			else
3951541Srgrimes				printf("Didn't unlink inifadr from list\n");
3961541Srgrimes		}
3977280Swollman
3987280Swollman		if (!oia->ia_multiaddrs.lh_first) {
3997280Swollman			IFAFREE(&oia->ia_ifa);
4007280Swollman			FREE(mk, M_IPMADDR);
4017280Swollman			break;
4027280Swollman		}
4037280Swollman
4047280Swollman		/*
4057280Swollman		 * Multicast address kludge:
4067280Swollman		 * If there were any multicast addresses attached to this
4077280Swollman		 * interface address, either move them to another address
4087280Swollman		 * on this interface, or save them until such time as this
4097280Swollman		 * interface is reconfigured for IP.
4107280Swollman		 */
4117280Swollman		IFP_TO_IA(oia->ia_ifp, ia);
4127280Swollman		if (ia) {	/* there is another address */
4137280Swollman			struct in_multi *inm;
4148876Srgrimes			for(inm = oia->ia_multiaddrs.lh_first; inm;
4157280Swollman			    inm = inm->inm_entry.le_next) {
4167280Swollman				IFAFREE(&inm->inm_ia->ia_ifa);
4177280Swollman				ia->ia_ifa.ifa_refcnt++;
4187280Swollman				inm->inm_ia = ia;
4197280Swollman				LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm,
4207280Swollman						 inm_entry);
4217280Swollman			}
4227280Swollman			FREE(mk, M_IPMADDR);
4237280Swollman		} else {	/* last address on this if deleted, save */
4247280Swollman			struct in_multi *inm;
4257280Swollman
4267280Swollman			LIST_INIT(&mk->mk_head);
4277280Swollman			mk->mk_ifp = ifp;
4287280Swollman
4298876Srgrimes			for(inm = oia->ia_multiaddrs.lh_first; inm;
4307280Swollman			    inm = inm->inm_entry.le_next) {
4317280Swollman				LIST_INSERT_HEAD(&mk->mk_head, inm, inm_entry);
4327280Swollman			}
4337280Swollman
4347280Swollman			if (mk->mk_head.lh_first) {
4357280Swollman				LIST_INSERT_HEAD(&in_mk, mk, mk_entry);
4367280Swollman			} else {
4377280Swollman				FREE(mk, M_IPMADDR);
4387280Swollman			}
4397280Swollman		}
4408876Srgrimes
4411541Srgrimes		IFAFREE((&oia->ia_ifa));
4421541Srgrimes		break;
4431541Srgrimes
4441541Srgrimes	default:
4451541Srgrimes		if (ifp == 0 || ifp->if_ioctl == 0)
4461541Srgrimes			return (EOPNOTSUPP);
4471541Srgrimes		return ((*ifp->if_ioctl)(ifp, cmd, data));
4481541Srgrimes	}
4491541Srgrimes	return (0);
4501541Srgrimes}
4511541Srgrimes
4521541Srgrimes/*
4531541Srgrimes * Delete any existing route for an interface.
4541541Srgrimes */
4551541Srgrimesvoid
4561541Srgrimesin_ifscrub(ifp, ia)
4571541Srgrimes	register struct ifnet *ifp;
4581541Srgrimes	register struct in_ifaddr *ia;
4591541Srgrimes{
4601541Srgrimes
4611541Srgrimes	if ((ia->ia_flags & IFA_ROUTE) == 0)
4621541Srgrimes		return;
4631541Srgrimes	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
4641541Srgrimes		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
4651541Srgrimes	else
4661541Srgrimes		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
4671541Srgrimes	ia->ia_flags &= ~IFA_ROUTE;
4681541Srgrimes}
4691541Srgrimes
4701541Srgrimes/*
4711541Srgrimes * Initialize an interface's internet address
4721541Srgrimes * and routing table entry.
4731541Srgrimes */
4741549Srgrimesint
4751541Srgrimesin_ifinit(ifp, ia, sin, scrub)
4761541Srgrimes	register struct ifnet *ifp;
4771541Srgrimes	register struct in_ifaddr *ia;
4781541Srgrimes	struct sockaddr_in *sin;
4791541Srgrimes	int scrub;
4801541Srgrimes{
4811541Srgrimes	register u_long i = ntohl(sin->sin_addr.s_addr);
4821541Srgrimes	struct sockaddr_in oldaddr;
4832112Swollman	int s = splimp(), flags = RTF_UP, error;
4847280Swollman	struct multi_kludge *mk;
4851541Srgrimes
4861541Srgrimes	oldaddr = ia->ia_addr;
4871541Srgrimes	ia->ia_addr = *sin;
4881541Srgrimes	/*
4891541Srgrimes	 * Give the interface a chance to initialize
4901541Srgrimes	 * if this is its first address,
4911541Srgrimes	 * and to validate the address if necessary.
4921541Srgrimes	 */
4931541Srgrimes	if (ifp->if_ioctl &&
4941541Srgrimes	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
4951541Srgrimes		splx(s);
4961541Srgrimes		ia->ia_addr = oldaddr;
4971541Srgrimes		return (error);
4981541Srgrimes	}
4991541Srgrimes	splx(s);
5001541Srgrimes	if (scrub) {
5011541Srgrimes		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
5021541Srgrimes		in_ifscrub(ifp, ia);
5031541Srgrimes		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
5041541Srgrimes	}
5051541Srgrimes	if (IN_CLASSA(i))
5061541Srgrimes		ia->ia_netmask = IN_CLASSA_NET;
5071541Srgrimes	else if (IN_CLASSB(i))
5081541Srgrimes		ia->ia_netmask = IN_CLASSB_NET;
5091541Srgrimes	else
5101541Srgrimes		ia->ia_netmask = IN_CLASSC_NET;
5111541Srgrimes	/*
5121541Srgrimes	 * The subnet mask usually includes at least the standard network part,
5131541Srgrimes	 * but may may be smaller in the case of supernetting.
5141541Srgrimes	 * If it is set, we believe it.
5151541Srgrimes	 */
5161541Srgrimes	if (ia->ia_subnetmask == 0) {
5171541Srgrimes		ia->ia_subnetmask = ia->ia_netmask;
5181541Srgrimes		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
5191541Srgrimes	} else
5201541Srgrimes		ia->ia_netmask &= ia->ia_subnetmask;
5211541Srgrimes	ia->ia_net = i & ia->ia_netmask;
5221541Srgrimes	ia->ia_subnet = i & ia->ia_subnetmask;
5231541Srgrimes	in_socktrim(&ia->ia_sockmask);
5241541Srgrimes	/*
5251541Srgrimes	 * Add route for the network.
5261541Srgrimes	 */
5271541Srgrimes	ia->ia_ifa.ifa_metric = ifp->if_metric;
5281541Srgrimes	if (ifp->if_flags & IFF_BROADCAST) {
5291541Srgrimes		ia->ia_broadaddr.sin_addr.s_addr =
5301541Srgrimes			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
5311541Srgrimes		ia->ia_netbroadcast.s_addr =
5321541Srgrimes			htonl(ia->ia_net | ~ ia->ia_netmask);
5331541Srgrimes	} else if (ifp->if_flags & IFF_LOOPBACK) {
5341541Srgrimes		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
5351541Srgrimes		flags |= RTF_HOST;
5361541Srgrimes	} else if (ifp->if_flags & IFF_POINTOPOINT) {
5371541Srgrimes		if (ia->ia_dstaddr.sin_family != AF_INET)
5381541Srgrimes			return (0);
5391541Srgrimes		flags |= RTF_HOST;
5401541Srgrimes	}
5411541Srgrimes	if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
5421541Srgrimes		ia->ia_flags |= IFA_ROUTE;
5437280Swollman
5447280Swollman	LIST_INIT(&ia->ia_multiaddrs);
5451541Srgrimes	/*
5461541Srgrimes	 * If the interface supports multicast, join the "all hosts"
5471541Srgrimes	 * multicast group on that interface.
5481541Srgrimes	 */
5491541Srgrimes	if (ifp->if_flags & IFF_MULTICAST) {
5501541Srgrimes		struct in_addr addr;
5511541Srgrimes
5527280Swollman		/*
5537280Swollman		 * Continuation of multicast address hack:
5548876Srgrimes		 * If there was a multicast group list previously saved
5557280Swollman		 * for this interface, then we re-attach it to the first
5567280Swollman		 * address configured on the i/f.
5577280Swollman		 */
5587280Swollman		for(mk = in_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
5597280Swollman			if(mk->mk_ifp == ifp) {
5607280Swollman				struct in_multi *inm;
5618876Srgrimes
5628876Srgrimes				for(inm = mk->mk_head.lh_first; inm;
5637280Swollman				    inm = inm->inm_entry.le_next) {
5647280Swollman					IFAFREE(&inm->inm_ia->ia_ifa);
5657280Swollman					ia->ia_ifa.ifa_refcnt++;
5667280Swollman					inm->inm_ia = ia;
5678876Srgrimes					LIST_INSERT_HEAD(&ia->ia_multiaddrs,
5687280Swollman							 inm, inm_entry);
5697280Swollman				}
5707280Swollman				LIST_REMOVE(mk, mk_entry);
5717280Swollman				free(mk, M_IPMADDR);
5727280Swollman				break;
5737280Swollman			}
5747280Swollman		}
5757280Swollman
5761541Srgrimes		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
5771541Srgrimes		in_addmulti(&addr, ifp);
5781541Srgrimes	}
5791541Srgrimes	return (error);
5801541Srgrimes}
5811541Srgrimes
5821541Srgrimes
5831541Srgrimes/*
5841541Srgrimes * Return 1 if the address might be a local broadcast address.
5851541Srgrimes */
5861549Srgrimesint
5871541Srgrimesin_broadcast(in, ifp)
5881541Srgrimes	struct in_addr in;
5891541Srgrimes        struct ifnet *ifp;
5901541Srgrimes{
5911541Srgrimes	register struct ifaddr *ifa;
5921541Srgrimes	u_long t;
5931541Srgrimes
5941541Srgrimes	if (in.s_addr == INADDR_BROADCAST ||
5951541Srgrimes	    in.s_addr == INADDR_ANY)
5961541Srgrimes		return 1;
5971541Srgrimes	if ((ifp->if_flags & IFF_BROADCAST) == 0)
5981541Srgrimes		return 0;
5991541Srgrimes	t = ntohl(in.s_addr);
6001541Srgrimes	/*
6011541Srgrimes	 * Look through the list of addresses for a match
6021541Srgrimes	 * with a broadcast address.
6031541Srgrimes	 */
6041541Srgrimes#define ia ((struct in_ifaddr *)ifa)
6051541Srgrimes	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
6061541Srgrimes		if (ifa->ifa_addr->sa_family == AF_INET &&
6071541Srgrimes		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
6081541Srgrimes		     in.s_addr == ia->ia_netbroadcast.s_addr ||
6091541Srgrimes		     /*
6101541Srgrimes		      * Check for old-style (host 0) broadcast.
6111541Srgrimes		      */
6121541Srgrimes		     t == ia->ia_subnet || t == ia->ia_net))
6131541Srgrimes			    return 1;
6141541Srgrimes	return (0);
6151541Srgrimes#undef ia
6161541Srgrimes}
6171541Srgrimes/*
6181541Srgrimes * Add an address to the list of IP multicast addresses for a given interface.
6191541Srgrimes */
6201541Srgrimesstruct in_multi *
6211541Srgrimesin_addmulti(ap, ifp)
6221541Srgrimes	register struct in_addr *ap;
6231541Srgrimes	register struct ifnet *ifp;
6241541Srgrimes{
6251541Srgrimes	register struct in_multi *inm;
6261541Srgrimes	struct ifreq ifr;
6271541Srgrimes	struct in_ifaddr *ia;
6281541Srgrimes	int s = splnet();
6291541Srgrimes
6301541Srgrimes	/*
6311541Srgrimes	 * See if address already in list.
6321541Srgrimes	 */
6331541Srgrimes	IN_LOOKUP_MULTI(*ap, ifp, inm);
6341541Srgrimes	if (inm != NULL) {
6351541Srgrimes		/*
6361541Srgrimes		 * Found it; just increment the reference count.
6371541Srgrimes		 */
6381541Srgrimes		++inm->inm_refcount;
6391541Srgrimes	}
6401541Srgrimes	else {
6411541Srgrimes		/*
6421541Srgrimes		 * New address; allocate a new multicast record
6431541Srgrimes		 * and link it into the interface's multicast list.
6441541Srgrimes		 */
6451541Srgrimes		inm = (struct in_multi *)malloc(sizeof(*inm),
6461541Srgrimes		    M_IPMADDR, M_NOWAIT);
6471541Srgrimes		if (inm == NULL) {
6481541Srgrimes			splx(s);
6491541Srgrimes			return (NULL);
6501541Srgrimes		}
6511541Srgrimes		inm->inm_addr = *ap;
6521541Srgrimes		inm->inm_ifp = ifp;
6531541Srgrimes		inm->inm_refcount = 1;
6541541Srgrimes		IFP_TO_IA(ifp, ia);
6551541Srgrimes		if (ia == NULL) {
6561541Srgrimes			free(inm, M_IPMADDR);
6571541Srgrimes			splx(s);
6581541Srgrimes			return (NULL);
6591541Srgrimes		}
6601541Srgrimes		inm->inm_ia = ia;
6617280Swollman		ia->ia_ifa.ifa_refcnt++; /* gain a reference */
6627280Swollman		LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_entry);
6637280Swollman
6641541Srgrimes		/*
6651541Srgrimes		 * Ask the network driver to update its multicast reception
6661541Srgrimes		 * filter appropriately for the new address.
6671541Srgrimes		 */
6681541Srgrimes		((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET;
6691541Srgrimes		((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr = *ap;
6701541Srgrimes		if ((ifp->if_ioctl == NULL) ||
6711541Srgrimes		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
6727280Swollman			LIST_REMOVE(inm, inm_entry);
6737280Swollman			IFAFREE(&ia->ia_ifa); /* release reference */
6741541Srgrimes			free(inm, M_IPMADDR);
6751541Srgrimes			splx(s);
6761541Srgrimes			return (NULL);
6771541Srgrimes		}
6781541Srgrimes		/*
6791541Srgrimes		 * Let IGMP know that we have joined a new IP multicast group.
6801541Srgrimes		 */
6811541Srgrimes		igmp_joingroup(inm);
6821541Srgrimes	}
6831541Srgrimes	splx(s);
6841541Srgrimes	return (inm);
6851541Srgrimes}
6861541Srgrimes
6871541Srgrimes/*
6881541Srgrimes * Delete a multicast address record.
6891541Srgrimes */
6901549Srgrimesvoid
6911541Srgrimesin_delmulti(inm)
6921541Srgrimes	register struct in_multi *inm;
6931541Srgrimes{
6941541Srgrimes	register struct in_multi **p;
6951541Srgrimes	struct ifreq ifr;
6961541Srgrimes	int s = splnet();
6971541Srgrimes
6981541Srgrimes	if (--inm->inm_refcount == 0) {
6991541Srgrimes		/*
7001541Srgrimes		 * No remaining claims to this record; let IGMP know that
7011541Srgrimes		 * we are leaving the multicast group.
7021541Srgrimes		 */
7031541Srgrimes		igmp_leavegroup(inm);
7041541Srgrimes		/*
7051541Srgrimes		 * Unlink from list.
7061541Srgrimes		 */
7077280Swollman		LIST_REMOVE(inm, inm_entry);
7087280Swollman		IFAFREE(&inm->inm_ia->ia_ifa); /* release reference */
7097280Swollman
7101541Srgrimes		/*
7111541Srgrimes		 * Notify the network driver to update its multicast reception
7121541Srgrimes		 * filter.
7131541Srgrimes		 */
7141541Srgrimes		((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
7151541Srgrimes		((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr =
7161541Srgrimes								inm->inm_addr;
7171541Srgrimes		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
7181541Srgrimes							     (caddr_t)&ifr);
7191541Srgrimes		free(inm, M_IPMADDR);
7201541Srgrimes	}
7211541Srgrimes	splx(s);
7221541Srgrimes}
723