Home | History | Annotate | Line # | Download | only in netinet
in.c revision 1.41
      1  1.41  jonathan /*	$NetBSD: in.c,v 1.41 1998/07/05 00:51:28 jonathan Exp $	*/
      2  1.39       tls 
      3  1.39       tls /*-
      4  1.39       tls  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.39       tls  * All rights reserved.
      6  1.39       tls  *
      7  1.39       tls  * This code is derived from software contributed to The NetBSD Foundation
      8  1.39       tls  * by Public Access Networks Corporation ("Panix").  It was developed under
      9  1.39       tls  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     10  1.39       tls  *
     11  1.39       tls  * Redistribution and use in source and binary forms, with or without
     12  1.39       tls  * modification, are permitted provided that the following conditions
     13  1.39       tls  * are met:
     14  1.39       tls  * 1. Redistributions of source code must retain the above copyright
     15  1.39       tls  *    notice, this list of conditions and the following disclaimer.
     16  1.39       tls  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.39       tls  *    notice, this list of conditions and the following disclaimer in the
     18  1.39       tls  *    documentation and/or other materials provided with the distribution.
     19  1.39       tls  * 3. All advertising materials mentioning features or use of this software
     20  1.39       tls  *    must display the following acknowledgement:
     21  1.39       tls  *	This product includes software developed by the NetBSD
     22  1.39       tls  *	Foundation, Inc. and its contributors.
     23  1.39       tls  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.39       tls  *    contributors may be used to endorse or promote products derived
     25  1.39       tls  *    from this software without specific prior written permission.
     26  1.39       tls  *
     27  1.39       tls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.39       tls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.39       tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.39       tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.39       tls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.39       tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.39       tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.39       tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.39       tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.39       tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.39       tls  * POSSIBILITY OF SUCH DAMAGE.
     38  1.39       tls  */
     39  1.14       cgd 
     40   1.1       cgd /*
     41  1.12   mycroft  * Copyright (c) 1982, 1986, 1991, 1993
     42  1.12   mycroft  *	The Regents of the University of California.  All rights reserved.
     43   1.1       cgd  *
     44   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     45   1.1       cgd  * modification, are permitted provided that the following conditions
     46   1.1       cgd  * are met:
     47   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     48   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     49   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     50   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     51   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     52   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     53   1.1       cgd  *    must display the following acknowledgement:
     54   1.1       cgd  *	This product includes software developed by the University of
     55   1.1       cgd  *	California, Berkeley and its contributors.
     56   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     57   1.1       cgd  *    may be used to endorse or promote products derived from this software
     58   1.1       cgd  *    without specific prior written permission.
     59   1.1       cgd  *
     60   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70   1.1       cgd  * SUCH DAMAGE.
     71   1.1       cgd  *
     72  1.36   thorpej  *	@(#)in.c	8.4 (Berkeley) 1/9/95
     73   1.1       cgd  */
     74  1.37    scottr 
     75  1.41  jonathan #include "opt_inet.h"
     76  1.37    scottr #include "opt_mrouting.h"
     77   1.1       cgd 
     78   1.6   mycroft #include <sys/param.h>
     79   1.6   mycroft #include <sys/ioctl.h>
     80  1.12   mycroft #include <sys/errno.h>
     81  1.12   mycroft #include <sys/malloc.h>
     82   1.6   mycroft #include <sys/socket.h>
     83   1.6   mycroft #include <sys/socketvar.h>
     84  1.26  christos #include <sys/systm.h>
     85  1.27   mycroft #include <sys/proc.h>
     86   1.6   mycroft 
     87   1.6   mycroft #include <net/if.h>
     88   1.6   mycroft #include <net/route.h>
     89   1.6   mycroft 
     90  1.34        is #include <net/if_ether.h>
     91  1.34        is 
     92  1.12   mycroft #include <netinet/in_systm.h>
     93   1.6   mycroft #include <netinet/in.h>
     94   1.6   mycroft #include <netinet/in_var.h>
     95  1.34        is #include <netinet/if_inarp.h>
     96  1.19   mycroft #include <netinet/ip_mroute.h>
     97  1.26  christos #include <netinet/igmp_var.h>
     98   1.8   mycroft 
     99  1.13    chopps #include "ether.h"
    100  1.13    chopps 
    101   1.1       cgd #ifdef INET
    102   1.1       cgd 
    103   1.1       cgd #ifndef SUBNETSARELOCAL
    104   1.1       cgd #define	SUBNETSARELOCAL	1
    105   1.1       cgd #endif
    106   1.1       cgd int subnetsarelocal = SUBNETSARELOCAL;
    107  1.30       mrg 
    108   1.1       cgd /*
    109   1.1       cgd  * Return 1 if an internet address is for a ``local'' host
    110   1.1       cgd  * (one to which we have a connection).  If subnetsarelocal
    111   1.1       cgd  * is true, this includes other subnets of the local net.
    112   1.1       cgd  * Otherwise, it includes only the directly-connected (sub)nets.
    113   1.1       cgd  */
    114   1.8   mycroft int
    115   1.1       cgd in_localaddr(in)
    116   1.1       cgd 	struct in_addr in;
    117   1.1       cgd {
    118   1.1       cgd 	register struct in_ifaddr *ia;
    119   1.1       cgd 
    120   1.1       cgd 	if (subnetsarelocal) {
    121  1.24   mycroft 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
    122  1.20   mycroft 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
    123   1.1       cgd 				return (1);
    124   1.1       cgd 	} else {
    125  1.24   mycroft 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
    126  1.20   mycroft 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
    127   1.1       cgd 				return (1);
    128   1.1       cgd 	}
    129   1.1       cgd 	return (0);
    130   1.1       cgd }
    131   1.1       cgd 
    132   1.1       cgd /*
    133   1.1       cgd  * Determine whether an IP address is in a reserved set of addresses
    134   1.1       cgd  * that may not be forwarded, or whether datagrams to that destination
    135   1.1       cgd  * may be forwarded.
    136   1.1       cgd  */
    137   1.8   mycroft int
    138   1.1       cgd in_canforward(in)
    139   1.1       cgd 	struct in_addr in;
    140   1.1       cgd {
    141  1.18       cgd 	register u_int32_t net;
    142   1.1       cgd 
    143  1.20   mycroft 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
    144   1.1       cgd 		return (0);
    145  1.20   mycroft 	if (IN_CLASSA(in.s_addr)) {
    146  1.20   mycroft 		net = in.s_addr & IN_CLASSA_NET;
    147  1.20   mycroft 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
    148   1.1       cgd 			return (0);
    149   1.1       cgd 	}
    150   1.1       cgd 	return (1);
    151   1.1       cgd }
    152   1.1       cgd 
    153  1.12   mycroft /*
    154  1.12   mycroft  * Trim a mask in a sockaddr
    155  1.12   mycroft  */
    156  1.12   mycroft void
    157  1.12   mycroft in_socktrim(ap)
    158  1.12   mycroft 	struct sockaddr_in *ap;
    159  1.12   mycroft {
    160  1.12   mycroft 	register char *cplim = (char *) &ap->sin_addr;
    161  1.12   mycroft 	register char *cp = (char *) (&ap->sin_addr + 1);
    162  1.12   mycroft 
    163  1.12   mycroft 	ap->sin_len = 0;
    164  1.15   mycroft 	while (--cp >= cplim)
    165  1.12   mycroft 		if (*cp) {
    166  1.12   mycroft 			(ap)->sin_len = cp - (char *) (ap) + 1;
    167  1.12   mycroft 			break;
    168  1.12   mycroft 		}
    169  1.40      matt }
    170  1.40      matt 
    171  1.40      matt /*
    172  1.40      matt  *  Routine to take an Internet address and convert into a
    173  1.40      matt  *  "dotted quad" representation for printing.
    174  1.40      matt  */
    175  1.40      matt const char *
    176  1.40      matt in_fmtaddr(addr)
    177  1.40      matt 	struct in_addr addr;
    178  1.40      matt {
    179  1.40      matt 	static char buf[sizeof("123.456.789.123")];
    180  1.40      matt 
    181  1.40      matt 	addr.s_addr = ntohl(addr.s_addr);
    182  1.40      matt 
    183  1.40      matt 	sprintf(buf, "%d.%d.%d.%d",
    184  1.40      matt 		(addr.s_addr >> 24) & 0xFF,
    185  1.40      matt 		(addr.s_addr >> 16) & 0xFF,
    186  1.40      matt 		(addr.s_addr >>  8) & 0xFF,
    187  1.40      matt 		(addr.s_addr >>  0) & 0xFF);
    188  1.40      matt 	return buf;
    189  1.12   mycroft }
    190  1.12   mycroft 
    191  1.35   thorpej /*
    192  1.35   thorpej  * Maintain the "in_maxmtu" variable, which is the largest
    193  1.35   thorpej  * mtu for non-local interfaces with AF_INET addresses assigned
    194  1.35   thorpej  * to them that are up.
    195  1.35   thorpej  */
    196  1.35   thorpej unsigned long in_maxmtu;
    197  1.35   thorpej 
    198  1.35   thorpej void
    199  1.35   thorpej in_setmaxmtu()
    200  1.35   thorpej {
    201  1.35   thorpej 	register struct in_ifaddr *ia;
    202  1.35   thorpej 	register struct ifnet *ifp;
    203  1.35   thorpej 	unsigned long maxmtu = 0;
    204  1.35   thorpej 
    205  1.35   thorpej 	for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next) {
    206  1.35   thorpej 		if ((ifp = ia->ia_ifp) == 0)
    207  1.35   thorpej 			continue;
    208  1.35   thorpej 		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
    209  1.35   thorpej 			continue;
    210  1.35   thorpej 		if (ifp->if_mtu > maxmtu)
    211  1.38       tls 			maxmtu = ifp->if_mtu;
    212  1.35   thorpej 	}
    213  1.35   thorpej 	if (maxmtu)
    214  1.35   thorpej 		in_maxmtu = maxmtu;
    215  1.35   thorpej }
    216  1.35   thorpej 
    217   1.1       cgd int	in_interfaces;		/* number of external internet interfaces */
    218   1.1       cgd 
    219   1.1       cgd /*
    220   1.1       cgd  * Generic internet control operations (ioctl's).
    221   1.1       cgd  * Ifp is 0 if not an interface-specific ioctl.
    222   1.1       cgd  */
    223   1.1       cgd /* ARGSUSED */
    224   1.8   mycroft int
    225  1.27   mycroft in_control(so, cmd, data, ifp, p)
    226   1.1       cgd 	struct socket *so;
    227  1.18       cgd 	u_long cmd;
    228   1.1       cgd 	caddr_t data;
    229   1.1       cgd 	register struct ifnet *ifp;
    230  1.27   mycroft 	struct proc *p;
    231   1.1       cgd {
    232   1.1       cgd 	register struct ifreq *ifr = (struct ifreq *)data;
    233   1.1       cgd 	register struct in_ifaddr *ia = 0;
    234   1.1       cgd 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
    235   1.1       cgd 	struct sockaddr_in oldaddr;
    236   1.1       cgd 	int error, hostIsNew, maskIsNew;
    237   1.1       cgd 
    238   1.1       cgd 	/*
    239   1.1       cgd 	 * Find address for this interface, if it exists.
    240   1.1       cgd 	 */
    241   1.1       cgd 	if (ifp)
    242  1.38       tls 		IFP_TO_IA(ifp, ia);
    243   1.1       cgd 
    244   1.1       cgd 	switch (cmd) {
    245   1.1       cgd 
    246   1.1       cgd 	case SIOCAIFADDR:
    247   1.1       cgd 	case SIOCDIFADDR:
    248   1.1       cgd 		if (ifra->ifra_addr.sin_family == AF_INET)
    249  1.38       tls 			for (ia = IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr).lh_first;
    250  1.38       tls 			    ia != 0; ia = ia->ia_hash.le_next) {
    251  1.28   mycroft 				if (ia->ia_ifp == ifp  &&
    252  1.38       tls 				    in_hosteq(ia->ia_addr.sin_addr,
    253  1.38       tls 				    ifra->ifra_addr.sin_addr))
    254  1.28   mycroft 					break;
    255  1.28   mycroft 			}
    256   1.1       cgd 		if (cmd == SIOCDIFADDR && ia == 0)
    257   1.1       cgd 			return (EADDRNOTAVAIL);
    258   1.1       cgd 		/* FALLTHROUGH */
    259   1.1       cgd 	case SIOCSIFADDR:
    260   1.1       cgd 	case SIOCSIFNETMASK:
    261   1.1       cgd 	case SIOCSIFDSTADDR:
    262  1.27   mycroft 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    263   1.1       cgd 			return (EPERM);
    264   1.1       cgd 
    265   1.1       cgd 		if (ifp == 0)
    266   1.1       cgd 			panic("in_control");
    267  1.28   mycroft 		if (ia == 0) {
    268  1.28   mycroft 			MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
    269  1.28   mycroft 			       M_IFADDR, M_WAITOK);
    270  1.28   mycroft 			if (ia == 0)
    271   1.1       cgd 				return (ENOBUFS);
    272  1.24   mycroft 			bzero((caddr_t)ia, sizeof *ia);
    273  1.24   mycroft 			TAILQ_INSERT_TAIL(&in_ifaddr, ia, ia_list);
    274  1.24   mycroft 			TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia,
    275  1.24   mycroft 			    ifa_list);
    276  1.21   mycroft 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    277  1.21   mycroft 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    278  1.21   mycroft 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
    279   1.1       cgd 			ia->ia_sockmask.sin_len = 8;
    280   1.1       cgd 			if (ifp->if_flags & IFF_BROADCAST) {
    281   1.1       cgd 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
    282   1.1       cgd 				ia->ia_broadaddr.sin_family = AF_INET;
    283   1.1       cgd 			}
    284   1.1       cgd 			ia->ia_ifp = ifp;
    285  1.24   mycroft 			LIST_INIT(&ia->ia_multiaddrs);
    286  1.17   mycroft 			if ((ifp->if_flags & IFF_LOOPBACK) == 0)
    287   1.1       cgd 				in_interfaces++;
    288   1.1       cgd 		}
    289   1.1       cgd 		break;
    290   1.1       cgd 
    291   1.1       cgd 	case SIOCSIFBRDADDR:
    292  1.27   mycroft 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    293   1.1       cgd 			return (EPERM);
    294   1.1       cgd 		/* FALLTHROUGH */
    295   1.1       cgd 
    296   1.1       cgd 	case SIOCGIFADDR:
    297   1.1       cgd 	case SIOCGIFNETMASK:
    298   1.1       cgd 	case SIOCGIFDSTADDR:
    299   1.1       cgd 	case SIOCGIFBRDADDR:
    300  1.28   mycroft 		if (ia == 0)
    301   1.1       cgd 			return (EADDRNOTAVAIL);
    302   1.1       cgd 		break;
    303   1.1       cgd 	}
    304   1.1       cgd 	switch (cmd) {
    305   1.1       cgd 
    306   1.1       cgd 	case SIOCGIFADDR:
    307  1.21   mycroft 		*satosin(&ifr->ifr_addr) = ia->ia_addr;
    308   1.1       cgd 		break;
    309   1.1       cgd 
    310   1.1       cgd 	case SIOCGIFBRDADDR:
    311   1.1       cgd 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    312   1.1       cgd 			return (EINVAL);
    313  1.21   mycroft 		*satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
    314   1.1       cgd 		break;
    315   1.1       cgd 
    316   1.1       cgd 	case SIOCGIFDSTADDR:
    317   1.1       cgd 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    318   1.1       cgd 			return (EINVAL);
    319  1.21   mycroft 		*satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
    320   1.1       cgd 		break;
    321   1.1       cgd 
    322   1.1       cgd 	case SIOCGIFNETMASK:
    323  1.21   mycroft 		*satosin(&ifr->ifr_addr) = ia->ia_sockmask;
    324   1.1       cgd 		break;
    325   1.1       cgd 
    326   1.1       cgd 	case SIOCSIFDSTADDR:
    327   1.1       cgd 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    328   1.1       cgd 			return (EINVAL);
    329   1.1       cgd 		oldaddr = ia->ia_dstaddr;
    330  1.21   mycroft 		ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
    331  1.12   mycroft 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
    332  1.12   mycroft 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
    333   1.1       cgd 			ia->ia_dstaddr = oldaddr;
    334   1.1       cgd 			return (error);
    335   1.1       cgd 		}
    336   1.1       cgd 		if (ia->ia_flags & IFA_ROUTE) {
    337  1.21   mycroft 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
    338   1.1       cgd 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    339  1.21   mycroft 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    340   1.1       cgd 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
    341   1.1       cgd 		}
    342   1.1       cgd 		break;
    343   1.1       cgd 
    344   1.1       cgd 	case SIOCSIFBRDADDR:
    345   1.1       cgd 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    346   1.1       cgd 			return (EINVAL);
    347  1.21   mycroft 		ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr);
    348   1.1       cgd 		break;
    349   1.1       cgd 
    350   1.1       cgd 	case SIOCSIFADDR:
    351  1.21   mycroft 		return (in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1));
    352   1.1       cgd 
    353   1.1       cgd 	case SIOCSIFNETMASK:
    354  1.20   mycroft 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr =
    355  1.20   mycroft 		    ifra->ifra_addr.sin_addr.s_addr;
    356   1.1       cgd 		break;
    357   1.1       cgd 
    358   1.1       cgd 	case SIOCAIFADDR:
    359   1.1       cgd 		maskIsNew = 0;
    360   1.1       cgd 		hostIsNew = 1;
    361   1.1       cgd 		error = 0;
    362   1.1       cgd 		if (ia->ia_addr.sin_family == AF_INET) {
    363   1.1       cgd 			if (ifra->ifra_addr.sin_len == 0) {
    364   1.1       cgd 				ifra->ifra_addr = ia->ia_addr;
    365   1.1       cgd 				hostIsNew = 0;
    366  1.32   mycroft 			} else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr))
    367   1.1       cgd 				hostIsNew = 0;
    368   1.1       cgd 		}
    369   1.1       cgd 		if (ifra->ifra_mask.sin_len) {
    370   1.1       cgd 			in_ifscrub(ifp, ia);
    371   1.1       cgd 			ia->ia_sockmask = ifra->ifra_mask;
    372  1.20   mycroft 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    373   1.1       cgd 			maskIsNew = 1;
    374   1.1       cgd 		}
    375   1.1       cgd 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    376   1.1       cgd 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
    377   1.1       cgd 			in_ifscrub(ifp, ia);
    378   1.1       cgd 			ia->ia_dstaddr = ifra->ifra_dstaddr;
    379   1.1       cgd 			maskIsNew  = 1; /* We lie; but the effect's the same */
    380   1.1       cgd 		}
    381   1.1       cgd 		if (ifra->ifra_addr.sin_family == AF_INET &&
    382   1.1       cgd 		    (hostIsNew || maskIsNew))
    383   1.1       cgd 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
    384   1.1       cgd 		if ((ifp->if_flags & IFF_BROADCAST) &&
    385   1.1       cgd 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
    386   1.1       cgd 			ia->ia_broadaddr = ifra->ifra_broadaddr;
    387   1.1       cgd 		return (error);
    388   1.1       cgd 
    389   1.1       cgd 	case SIOCDIFADDR:
    390   1.1       cgd 		in_ifscrub(ifp, ia);
    391  1.38       tls 		LIST_REMOVE(ia, ia_hash);
    392  1.24   mycroft 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    393  1.24   mycroft 		TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
    394  1.24   mycroft 		IFAFREE((&ia->ia_ifa));
    395  1.35   thorpej 		in_setmaxmtu();
    396   1.1       cgd 		break;
    397  1.19   mycroft 
    398  1.19   mycroft #ifdef MROUTING
    399  1.19   mycroft 	case SIOCGETVIFCNT:
    400  1.19   mycroft 	case SIOCGETSGCNT:
    401  1.29   mycroft 		return (mrt_ioctl(so, cmd, data));
    402  1.19   mycroft #endif /* MROUTING */
    403   1.1       cgd 
    404   1.1       cgd 	default:
    405   1.1       cgd 		if (ifp == 0 || ifp->if_ioctl == 0)
    406   1.1       cgd 			return (EOPNOTSUPP);
    407  1.35   thorpej 		error = (*ifp->if_ioctl)(ifp, cmd, data);
    408  1.35   thorpej 		in_setmaxmtu();
    409  1.35   thorpej 		return(error);
    410   1.1       cgd 	}
    411   1.1       cgd 	return (0);
    412   1.1       cgd }
    413   1.1       cgd 
    414   1.1       cgd /*
    415   1.1       cgd  * Delete any existing route for an interface.
    416   1.1       cgd  */
    417  1.12   mycroft void
    418   1.1       cgd in_ifscrub(ifp, ia)
    419   1.1       cgd 	register struct ifnet *ifp;
    420   1.1       cgd 	register struct in_ifaddr *ia;
    421   1.1       cgd {
    422   1.1       cgd 
    423   1.1       cgd 	if ((ia->ia_flags & IFA_ROUTE) == 0)
    424   1.1       cgd 		return;
    425   1.1       cgd 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
    426   1.1       cgd 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    427   1.1       cgd 	else
    428   1.1       cgd 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
    429   1.1       cgd 	ia->ia_flags &= ~IFA_ROUTE;
    430   1.1       cgd }
    431   1.1       cgd 
    432   1.1       cgd /*
    433   1.1       cgd  * Initialize an interface's internet address
    434   1.1       cgd  * and routing table entry.
    435   1.1       cgd  */
    436  1.12   mycroft int
    437   1.1       cgd in_ifinit(ifp, ia, sin, scrub)
    438   1.1       cgd 	register struct ifnet *ifp;
    439   1.1       cgd 	register struct in_ifaddr *ia;
    440   1.1       cgd 	struct sockaddr_in *sin;
    441  1.12   mycroft 	int scrub;
    442   1.1       cgd {
    443  1.20   mycroft 	register u_int32_t i = sin->sin_addr.s_addr;
    444   1.1       cgd 	struct sockaddr_in oldaddr;
    445  1.26  christos 	int s = splimp(), flags = RTF_UP, error;
    446   1.1       cgd 
    447  1.32   mycroft 	/*
    448  1.32   mycroft 	 * Set up new addresses.
    449  1.32   mycroft 	 */
    450   1.1       cgd 	oldaddr = ia->ia_addr;
    451  1.38       tls 	if (ia->ia_addr.sin_family == AF_INET)
    452  1.38       tls 		LIST_REMOVE(ia, ia_hash);
    453   1.1       cgd 	ia->ia_addr = *sin;
    454  1.38       tls 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
    455  1.38       tls 
    456   1.1       cgd 	/*
    457   1.1       cgd 	 * Give the interface a chance to initialize
    458   1.1       cgd 	 * if this is its first address,
    459   1.1       cgd 	 * and to validate the address if necessary.
    460   1.1       cgd 	 */
    461  1.12   mycroft 	if (ifp->if_ioctl &&
    462  1.32   mycroft 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)))
    463  1.32   mycroft 		goto bad;
    464   1.1       cgd 	splx(s);
    465   1.1       cgd 	if (scrub) {
    466  1.21   mycroft 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
    467   1.1       cgd 		in_ifscrub(ifp, ia);
    468  1.21   mycroft 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    469   1.1       cgd 	}
    470  1.35   thorpej 
    471   1.1       cgd 	if (IN_CLASSA(i))
    472   1.1       cgd 		ia->ia_netmask = IN_CLASSA_NET;
    473   1.1       cgd 	else if (IN_CLASSB(i))
    474   1.1       cgd 		ia->ia_netmask = IN_CLASSB_NET;
    475   1.1       cgd 	else
    476   1.1       cgd 		ia->ia_netmask = IN_CLASSC_NET;
    477   1.1       cgd 	/*
    478  1.12   mycroft 	 * The subnet mask usually includes at least the standard network part,
    479  1.12   mycroft 	 * but may may be smaller in the case of supernetting.
    480  1.12   mycroft 	 * If it is set, we believe it.
    481   1.1       cgd 	 */
    482  1.12   mycroft 	if (ia->ia_subnetmask == 0) {
    483  1.12   mycroft 		ia->ia_subnetmask = ia->ia_netmask;
    484  1.20   mycroft 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
    485  1.12   mycroft 	} else
    486  1.12   mycroft 		ia->ia_netmask &= ia->ia_subnetmask;
    487  1.35   thorpej 
    488  1.12   mycroft 	ia->ia_net = i & ia->ia_netmask;
    489   1.1       cgd 	ia->ia_subnet = i & ia->ia_subnetmask;
    490  1.12   mycroft 	in_socktrim(&ia->ia_sockmask);
    491  1.35   thorpej 	/* re-calculate the "in_maxmtu" value */
    492  1.35   thorpej 	in_setmaxmtu();
    493   1.1       cgd 	/*
    494   1.1       cgd 	 * Add route for the network.
    495   1.1       cgd 	 */
    496  1.12   mycroft 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    497   1.1       cgd 	if (ifp->if_flags & IFF_BROADCAST) {
    498  1.12   mycroft 		ia->ia_broadaddr.sin_addr.s_addr =
    499  1.20   mycroft 			ia->ia_subnet | ~ia->ia_subnetmask;
    500   1.1       cgd 		ia->ia_netbroadcast.s_addr =
    501  1.20   mycroft 			ia->ia_net | ~ia->ia_netmask;
    502   1.1       cgd 	} else if (ifp->if_flags & IFF_LOOPBACK) {
    503   1.1       cgd 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
    504   1.1       cgd 		flags |= RTF_HOST;
    505   1.1       cgd 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
    506   1.1       cgd 		if (ia->ia_dstaddr.sin_family != AF_INET)
    507   1.1       cgd 			return (0);
    508   1.1       cgd 		flags |= RTF_HOST;
    509   1.1       cgd 	}
    510  1.32   mycroft 	error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags);
    511  1.32   mycroft 	if (!error)
    512   1.1       cgd 		ia->ia_flags |= IFA_ROUTE;
    513   1.5   hpeyerl 	/*
    514   1.5   hpeyerl 	 * If the interface supports multicast, join the "all hosts"
    515   1.5   hpeyerl 	 * multicast group on that interface.
    516   1.5   hpeyerl 	 */
    517   1.5   hpeyerl 	if (ifp->if_flags & IFF_MULTICAST) {
    518   1.5   hpeyerl 		struct in_addr addr;
    519   1.5   hpeyerl 
    520  1.20   mycroft 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
    521   1.5   hpeyerl 		in_addmulti(&addr, ifp);
    522   1.5   hpeyerl 	}
    523   1.1       cgd 	return (error);
    524  1.32   mycroft bad:
    525  1.32   mycroft 	splx(s);
    526  1.38       tls 	LIST_REMOVE(ia, ia_hash);
    527  1.32   mycroft 	ia->ia_addr = oldaddr;
    528  1.38       tls 	if (ia->ia_addr.sin_family == AF_INET)
    529  1.38       tls 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
    530  1.38       tls 		    ia, ia_hash);
    531  1.32   mycroft 	return (error);
    532   1.1       cgd }
    533   1.1       cgd 
    534   1.1       cgd /*
    535   1.1       cgd  * Return 1 if the address might be a local broadcast address.
    536   1.1       cgd  */
    537   1.8   mycroft int
    538  1.12   mycroft in_broadcast(in, ifp)
    539   1.1       cgd 	struct in_addr in;
    540  1.12   mycroft 	struct ifnet *ifp;
    541   1.1       cgd {
    542  1.12   mycroft 	register struct ifaddr *ifa;
    543   1.1       cgd 
    544  1.12   mycroft 	if (in.s_addr == INADDR_BROADCAST ||
    545  1.32   mycroft 	    in_nullhost(in))
    546  1.12   mycroft 		return 1;
    547  1.12   mycroft 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
    548  1.12   mycroft 		return 0;
    549   1.1       cgd 	/*
    550   1.1       cgd 	 * Look through the list of addresses for a match
    551   1.1       cgd 	 * with a broadcast address.
    552   1.1       cgd 	 */
    553  1.22   mycroft #define ia (ifatoia(ifa))
    554  1.24   mycroft 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
    555  1.12   mycroft 		if (ifa->ifa_addr->sa_family == AF_INET &&
    556  1.32   mycroft 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
    557  1.32   mycroft 		     in_hosteq(in, ia->ia_netbroadcast) ||
    558  1.12   mycroft 		     /*
    559  1.12   mycroft 		      * Check for old-style (host 0) broadcast.
    560  1.12   mycroft 		      */
    561  1.20   mycroft 		     in.s_addr == ia->ia_subnet ||
    562  1.20   mycroft 		     in.s_addr == ia->ia_net))
    563  1.12   mycroft 			    return 1;
    564   1.1       cgd 	return (0);
    565  1.12   mycroft #undef ia
    566   1.1       cgd }
    567   1.5   hpeyerl 
    568   1.5   hpeyerl /*
    569   1.5   hpeyerl  * Add an address to the list of IP multicast addresses for a given interface.
    570   1.5   hpeyerl  */
    571   1.5   hpeyerl struct in_multi *
    572   1.5   hpeyerl in_addmulti(ap, ifp)
    573   1.5   hpeyerl 	register struct in_addr *ap;
    574   1.5   hpeyerl 	register struct ifnet *ifp;
    575   1.5   hpeyerl {
    576   1.5   hpeyerl 	register struct in_multi *inm;
    577   1.5   hpeyerl 	struct ifreq ifr;
    578   1.5   hpeyerl 	struct in_ifaddr *ia;
    579  1.25   mycroft 	int s = splsoftnet();
    580   1.5   hpeyerl 
    581   1.5   hpeyerl 	/*
    582   1.5   hpeyerl 	 * See if address already in list.
    583   1.5   hpeyerl 	 */
    584   1.5   hpeyerl 	IN_LOOKUP_MULTI(*ap, ifp, inm);
    585   1.5   hpeyerl 	if (inm != NULL) {
    586   1.5   hpeyerl 		/*
    587   1.5   hpeyerl 		 * Found it; just increment the reference count.
    588   1.5   hpeyerl 		 */
    589   1.5   hpeyerl 		++inm->inm_refcount;
    590  1.24   mycroft 	} else {
    591   1.5   hpeyerl 		/*
    592   1.5   hpeyerl 		 * New address; allocate a new multicast record
    593   1.5   hpeyerl 		 * and link it into the interface's multicast list.
    594   1.5   hpeyerl 		 */
    595   1.5   hpeyerl 		inm = (struct in_multi *)malloc(sizeof(*inm),
    596   1.5   hpeyerl 		    M_IPMADDR, M_NOWAIT);
    597   1.5   hpeyerl 		if (inm == NULL) {
    598   1.5   hpeyerl 			splx(s);
    599   1.5   hpeyerl 			return (NULL);
    600   1.5   hpeyerl 		}
    601   1.5   hpeyerl 		inm->inm_addr = *ap;
    602   1.5   hpeyerl 		inm->inm_ifp = ifp;
    603   1.5   hpeyerl 		inm->inm_refcount = 1;
    604   1.5   hpeyerl 		IFP_TO_IA(ifp, ia);
    605   1.5   hpeyerl 		if (ia == NULL) {
    606   1.5   hpeyerl 			free(inm, M_IPMADDR);
    607   1.5   hpeyerl 			splx(s);
    608   1.5   hpeyerl 			return (NULL);
    609   1.5   hpeyerl 		}
    610   1.5   hpeyerl 		inm->inm_ia = ia;
    611  1.24   mycroft 		LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
    612   1.5   hpeyerl 		/*
    613   1.5   hpeyerl 		 * Ask the network driver to update its multicast reception
    614   1.5   hpeyerl 		 * filter appropriately for the new address.
    615   1.5   hpeyerl 		 */
    616  1.23   mycroft 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
    617  1.21   mycroft 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    618  1.21   mycroft 		satosin(&ifr.ifr_addr)->sin_addr = *ap;
    619  1.12   mycroft 		if ((ifp->if_ioctl == NULL) ||
    620   1.5   hpeyerl 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
    621  1.24   mycroft 			LIST_REMOVE(inm, inm_list);
    622   1.5   hpeyerl 			free(inm, M_IPMADDR);
    623   1.5   hpeyerl 			splx(s);
    624   1.5   hpeyerl 			return (NULL);
    625   1.5   hpeyerl 		}
    626   1.5   hpeyerl 		/*
    627   1.5   hpeyerl 		 * Let IGMP know that we have joined a new IP multicast group.
    628   1.5   hpeyerl 		 */
    629   1.5   hpeyerl 		igmp_joingroup(inm);
    630   1.5   hpeyerl 	}
    631   1.5   hpeyerl 	splx(s);
    632   1.5   hpeyerl 	return (inm);
    633   1.5   hpeyerl }
    634   1.5   hpeyerl 
    635   1.5   hpeyerl /*
    636   1.5   hpeyerl  * Delete a multicast address record.
    637   1.5   hpeyerl  */
    638  1.26  christos void
    639   1.5   hpeyerl in_delmulti(inm)
    640   1.5   hpeyerl 	register struct in_multi *inm;
    641   1.5   hpeyerl {
    642   1.5   hpeyerl 	struct ifreq ifr;
    643  1.25   mycroft 	int s = splsoftnet();
    644   1.5   hpeyerl 
    645   1.5   hpeyerl 	if (--inm->inm_refcount == 0) {
    646   1.5   hpeyerl 		/*
    647   1.5   hpeyerl 		 * No remaining claims to this record; let IGMP know that
    648   1.5   hpeyerl 		 * we are leaving the multicast group.
    649   1.5   hpeyerl 		 */
    650   1.5   hpeyerl 		igmp_leavegroup(inm);
    651   1.5   hpeyerl 		/*
    652   1.5   hpeyerl 		 * Unlink from list.
    653   1.5   hpeyerl 		 */
    654  1.24   mycroft 		LIST_REMOVE(inm, inm_list);
    655   1.5   hpeyerl 		/*
    656   1.5   hpeyerl 		 * Notify the network driver to update its multicast reception
    657   1.5   hpeyerl 		 * filter.
    658   1.5   hpeyerl 		 */
    659  1.21   mycroft 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    660  1.21   mycroft 		satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
    661   1.5   hpeyerl 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
    662   1.5   hpeyerl 							     (caddr_t)&ifr);
    663   1.5   hpeyerl 		free(inm, M_IPMADDR);
    664   1.5   hpeyerl 	}
    665   1.5   hpeyerl 	splx(s);
    666   1.5   hpeyerl }
    667  1.30       mrg #endif
    668