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