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