in.c revision 192011
1139823Simp/*- 21541Srgrimes * Copyright (c) 1982, 1986, 1991, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 4137668Smlaier * Copyright (C) 2001 WIDE Project. All rights reserved. 51541Srgrimes * 61541Srgrimes * Redistribution and use in source and binary forms, with or without 71541Srgrimes * modification, are permitted provided that the following conditions 81541Srgrimes * are met: 91541Srgrimes * 1. Redistributions of source code must retain the above copyright 101541Srgrimes * notice, this list of conditions and the following disclaimer. 111541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 121541Srgrimes * notice, this list of conditions and the following disclaimer in the 131541Srgrimes * documentation and/or other materials provided with the distribution. 141541Srgrimes * 4. Neither the name of the University nor the names of its contributors 151541Srgrimes * may be used to endorse or promote products derived from this software 161541Srgrimes * without specific prior written permission. 171541Srgrimes * 181541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 191541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 201541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 211541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 221541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 231541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 241541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 251541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 261541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 271541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 281541Srgrimes * SUCH DAMAGE. 291541Srgrimes * 3010939Swollman * @(#)in.c 8.4 (Berkeley) 1/9/95 311541Srgrimes */ 321541Srgrimes 33172467Ssilby#include <sys/cdefs.h> 34172467Ssilby__FBSDID("$FreeBSD: head/sys/netinet/in.c 192011 2009-05-12 07:41:20Z qingli $"); 35172467Ssilby 36143868Sglebius#include "opt_carp.h" 37143868Sglebius 381541Srgrimes#include <sys/param.h> 391549Srgrimes#include <sys/systm.h> 4024204Sbde#include <sys/sockio.h> 411541Srgrimes#include <sys/malloc.h> 42164033Srwatson#include <sys/priv.h> 431541Srgrimes#include <sys/socket.h> 44186948Sbz#include <sys/jail.h> 4512704Sphk#include <sys/kernel.h> 46186948Sbz#include <sys/proc.h> 4712704Sphk#include <sys/sysctl.h> 48192011Sqingli#include <sys/syslog.h> 49181803Sbz#include <sys/vimage.h> 501541Srgrimes 511541Srgrimes#include <net/if.h> 52192011Sqingli#include <net/if_dl.h> 53186119Sqingli#include <net/if_llatbl.h> 5455009Sshin#include <net/if_types.h> 551541Srgrimes#include <net/route.h> 56192011Sqingli#include <net/vnet.h> 571541Srgrimes 581541Srgrimes#include <netinet/in.h> 591541Srgrimes#include <netinet/in_var.h> 6081127Sume#include <netinet/in_pcb.h> 61170613Sbms#include <netinet/ip_var.h> 62185571Sbz#include <netinet/vinet.h> 63189592Sbms#include <netinet/igmp_var.h> 641541Srgrimes 6592723Salfredstatic int in_mask2len(struct in_addr *); 6692723Salfredstatic void in_len2mask(struct in_addr *, int); 6792723Salfredstatic int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 6892723Salfred struct ifnet *, struct thread *); 6955009Sshin 70137628Smlaierstatic int in_addprefix(struct in_ifaddr *, int); 71137628Smlaierstatic int in_scrubprefix(struct in_ifaddr *); 7292723Salfredstatic void in_socktrim(struct sockaddr_in *); 7392723Salfredstatic int in_ifinit(struct ifnet *, 7492723Salfred struct in_ifaddr *, struct sockaddr_in *, int); 75167729Sbmsstatic void in_purgemaddrs(struct ifnet *); 761541Srgrimes 77185088Szec#ifdef VIMAGE_GLOBALS 78185088Szecstatic int subnetsarelocal; 79185088Szecstatic int sameprefixcarponly; 80185088Szecextern struct inpcbinfo ripcbinfo; 81185088Szec#endif 82185088Szec 83183550SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, subnets_are_local, 84183550Szec CTLFLAG_RW, subnetsarelocal, 0, 85183550Szec "Treat all subnets as directly connected"); 86183550SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, same_prefix_carp_only, 87183550Szec CTLFLAG_RW, sameprefixcarponly, 0, 88149221Sglebius "Refuse to create same prefixes on different interfaces"); 8921666Swollman 901541Srgrimes/* 911541Srgrimes * Return 1 if an internet address is for a ``local'' host 921541Srgrimes * (one to which we have a connection). If subnetsarelocal 931541Srgrimes * is true, this includes other subnets of the local net. 941541Srgrimes * Otherwise, it includes only the directly-connected (sub)nets. 951541Srgrimes */ 961549Srgrimesint 97169454Srwatsonin_localaddr(struct in_addr in) 981541Srgrimes{ 99183550Szec INIT_VNET_INET(curvnet); 1001541Srgrimes register u_long i = ntohl(in.s_addr); 1011541Srgrimes register struct in_ifaddr *ia; 1021541Srgrimes 103181803Sbz if (V_subnetsarelocal) { 104181803Sbz TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) 1051541Srgrimes if ((i & ia->ia_netmask) == ia->ia_net) 1061541Srgrimes return (1); 1071541Srgrimes } else { 108181803Sbz TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) 1091541Srgrimes if ((i & ia->ia_subnetmask) == ia->ia_subnet) 1101541Srgrimes return (1); 1111541Srgrimes } 1121541Srgrimes return (0); 1131541Srgrimes} 1141541Srgrimes 1151541Srgrimes/* 116133486Sandre * Return 1 if an internet address is for the local host and configured 117133486Sandre * on one of its interfaces. 118133486Sandre */ 119133486Sandreint 120169454Srwatsonin_localip(struct in_addr in) 121133486Sandre{ 122183550Szec INIT_VNET_INET(curvnet); 123133486Sandre struct in_ifaddr *ia; 124133486Sandre 125133486Sandre LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 126133486Sandre if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) 127184295Sbz return (1); 128133486Sandre } 129184295Sbz return (0); 130133486Sandre} 131133486Sandre 132133486Sandre/* 1331541Srgrimes * Determine whether an IP address is in a reserved set of addresses 1341541Srgrimes * that may not be forwarded, or whether datagrams to that destination 1351541Srgrimes * may be forwarded. 1361541Srgrimes */ 1371549Srgrimesint 138169454Srwatsonin_canforward(struct in_addr in) 1391541Srgrimes{ 1401541Srgrimes register u_long i = ntohl(in.s_addr); 1411541Srgrimes register u_long net; 1421541Srgrimes 143166450Sbms if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i)) 1441541Srgrimes return (0); 1451541Srgrimes if (IN_CLASSA(i)) { 1461541Srgrimes net = i & IN_CLASSA_NET; 1471541Srgrimes if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 1481541Srgrimes return (0); 1491541Srgrimes } 1501541Srgrimes return (1); 1511541Srgrimes} 1521541Srgrimes 1531541Srgrimes/* 1541541Srgrimes * Trim a mask in a sockaddr 1551541Srgrimes */ 15612296Sphkstatic void 157169454Srwatsonin_socktrim(struct sockaddr_in *ap) 1581541Srgrimes{ 1591541Srgrimes register char *cplim = (char *) &ap->sin_addr; 1601541Srgrimes register char *cp = (char *) (&ap->sin_addr + 1); 1611541Srgrimes 1621541Srgrimes ap->sin_len = 0; 1634127Swollman while (--cp >= cplim) 164133874Srwatson if (*cp) { 1651541Srgrimes (ap)->sin_len = cp - (char *) (ap) + 1; 1661541Srgrimes break; 1671541Srgrimes } 1681541Srgrimes} 1691541Srgrimes 17055009Sshinstatic int 17155009Sshinin_mask2len(mask) 17255009Sshin struct in_addr *mask; 17355009Sshin{ 17455009Sshin int x, y; 17555009Sshin u_char *p; 17655009Sshin 17755009Sshin p = (u_char *)mask; 17855009Sshin for (x = 0; x < sizeof(*mask); x++) { 17955009Sshin if (p[x] != 0xff) 18055009Sshin break; 18155009Sshin } 18255009Sshin y = 0; 18355009Sshin if (x < sizeof(*mask)) { 18455009Sshin for (y = 0; y < 8; y++) { 18555009Sshin if ((p[x] & (0x80 >> y)) == 0) 18655009Sshin break; 18755009Sshin } 18855009Sshin } 189184295Sbz return (x * 8 + y); 19055009Sshin} 19155009Sshin 19255009Sshinstatic void 193169454Srwatsonin_len2mask(struct in_addr *mask, int len) 19455009Sshin{ 19555009Sshin int i; 19655009Sshin u_char *p; 19755009Sshin 19855009Sshin p = (u_char *)mask; 19955009Sshin bzero(mask, sizeof(*mask)); 20055009Sshin for (i = 0; i < len / 8; i++) 20155009Sshin p[i] = 0xff; 20255009Sshin if (len % 8) 20355009Sshin p[i] = (0xff00 >> (len % 8)) & 0xff; 20455009Sshin} 20555009Sshin 2061541Srgrimes/* 2071541Srgrimes * Generic internet control operations (ioctl's). 208191443Srwatson * 209191443Srwatson * ifp is NULL if not an interface-specific ioctl. 2101541Srgrimes */ 2111541Srgrimes/* ARGSUSED */ 2121549Srgrimesint 213169454Srwatsonin_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 214169454Srwatson struct thread *td) 2151541Srgrimes{ 216183550Szec INIT_VNET_INET(curvnet); /* both so and ifp can be NULL here! */ 2171541Srgrimes register struct ifreq *ifr = (struct ifreq *)data; 218184295Sbz register struct in_ifaddr *ia, *iap; 2191541Srgrimes register struct ifaddr *ifa; 220168032Sbms struct in_addr allhosts_addr; 22184102Sjlemon struct in_addr dst; 2221541Srgrimes struct in_ifaddr *oia; 223189592Sbms struct in_ifinfo *ii; 2241541Srgrimes struct in_aliasreq *ifra = (struct in_aliasreq *)data; 2251541Srgrimes struct sockaddr_in oldaddr; 22687124Sbrian int error, hostIsNew, iaIsNew, maskIsNew, s; 227168032Sbms int iaIsFirst; 2281541Srgrimes 229184295Sbz ia = NULL; 230168032Sbms iaIsFirst = 0; 23187124Sbrian iaIsNew = 0; 232168032Sbms allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 23387124Sbrian 234191443Srwatson /* 235191443Srwatson * Filter out ioctls we implement directly; forward the rest on to 236191443Srwatson * in_lifaddr_ioctl() and ifp->if_ioctl(). 237191443Srwatson */ 23855009Sshin switch (cmd) { 239191443Srwatson case SIOCAIFADDR: 240191443Srwatson case SIOCDIFADDR: 241191443Srwatson case SIOCGIFADDR: 242191443Srwatson case SIOCGIFBRDADDR: 243191443Srwatson case SIOCGIFDSTADDR: 244191443Srwatson case SIOCGIFNETMASK: 245191443Srwatson case SIOCSIFADDR: 246191443Srwatson case SIOCSIFBRDADDR: 247191443Srwatson case SIOCSIFDSTADDR: 248191443Srwatson case SIOCSIFNETMASK: 249191443Srwatson break; 250191443Srwatson 25155009Sshin case SIOCALIFADDR: 252164033Srwatson if (td != NULL) { 253164033Srwatson error = priv_check(td, PRIV_NET_ADDIFADDR); 254164033Srwatson if (error) 255164033Srwatson return (error); 256164033Srwatson } 257184295Sbz if (ifp == NULL) 258184295Sbz return (EINVAL); 259164033Srwatson return in_lifaddr_ioctl(so, cmd, data, ifp, td); 260164033Srwatson 26155009Sshin case SIOCDLIFADDR: 262164033Srwatson if (td != NULL) { 263164033Srwatson error = priv_check(td, PRIV_NET_DELIFADDR); 264164033Srwatson if (error) 265164033Srwatson return (error); 266164033Srwatson } 267184295Sbz if (ifp == NULL) 268184295Sbz return (EINVAL); 269164033Srwatson return in_lifaddr_ioctl(so, cmd, data, ifp, td); 270164033Srwatson 27155009Sshin case SIOCGLIFADDR: 272184295Sbz if (ifp == NULL) 273184295Sbz return (EINVAL); 27483366Sjulian return in_lifaddr_ioctl(so, cmd, data, ifp, td); 275191443Srwatson 276191443Srwatson default: 277191443Srwatson if (ifp == NULL || ifp->if_ioctl == NULL) 278191443Srwatson return (EOPNOTSUPP); 279191443Srwatson return ((*ifp->if_ioctl)(ifp, cmd, data)); 28055009Sshin } 28155009Sshin 282191443Srwatson if (ifp == NULL) 283191443Srwatson return (EADDRNOTAVAIL); 284191443Srwatson 2851541Srgrimes /* 286191456Srwatson * Security checks before we get involved in any work. 287191456Srwatson */ 288191456Srwatson switch (cmd) { 289191456Srwatson case SIOCAIFADDR: 290191456Srwatson case SIOCSIFADDR: 291191456Srwatson case SIOCSIFBRDADDR: 292191456Srwatson case SIOCSIFNETMASK: 293191456Srwatson case SIOCSIFDSTADDR: 294191456Srwatson if (td != NULL) { 295191456Srwatson error = priv_check(td, PRIV_NET_ADDIFADDR); 296191456Srwatson if (error) 297191456Srwatson return (error); 298191456Srwatson } 299191456Srwatson break; 300191456Srwatson 301191456Srwatson case SIOCDIFADDR: 302191456Srwatson if (td != NULL) { 303191456Srwatson error = priv_check(td, PRIV_NET_DELIFADDR); 304191456Srwatson if (error) 305191456Srwatson return (error); 306191456Srwatson } 307191456Srwatson break; 308191456Srwatson } 309191456Srwatson 310191456Srwatson /* 3111541Srgrimes * Find address for this interface, if it exists. 31214632Sfenner * 313191443Srwatson * If an alias address was specified, find that one instead of the 314191443Srwatson * first one on the interface, if possible. 3151541Srgrimes */ 316191443Srwatson dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; 317191443Srwatson LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) { 318191443Srwatson if (iap->ia_ifp == ifp && 319191443Srwatson iap->ia_addr.sin_addr.s_addr == dst.s_addr) { 320191443Srwatson if (td == NULL || prison_check_ip4(td->td_ucred, 321191443Srwatson &dst) == 0) 322191443Srwatson ia = iap; 323191443Srwatson break; 324191443Srwatson } 325191443Srwatson } 326191500Srwatson IF_ADDR_LOCK(ifp); 327191443Srwatson if (ia == NULL) { 328191443Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 329191443Srwatson iap = ifatoia(ifa); 330191443Srwatson if (iap->ia_addr.sin_family == AF_INET) { 331191443Srwatson if (td != NULL && 332191443Srwatson prison_check_ip4(td->td_ucred, 333191443Srwatson &iap->ia_addr.sin_addr) != 0) 334191443Srwatson continue; 335191443Srwatson ia = iap; 33684102Sjlemon break; 33784102Sjlemon } 338191443Srwatson } 33984102Sjlemon } 340191443Srwatson if (ia == NULL) 341191443Srwatson iaIsFirst = 1; 3421541Srgrimes 343191500Srwatson error = 0; 3441541Srgrimes switch (cmd) { 3451541Srgrimes case SIOCAIFADDR: 3461541Srgrimes case SIOCDIFADDR: 3478071Swollman if (ifra->ifra_addr.sin_family == AF_INET) { 34871999Sphk for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) { 3498071Swollman if (ia->ia_ifp == ifp && 3508071Swollman ia->ia_addr.sin_addr.s_addr == 3518071Swollman ifra->ifra_addr.sin_addr.s_addr) 3528071Swollman break; 3538071Swollman } 3548876Srgrimes if ((ifp->if_flags & IFF_POINTOPOINT) 3558071Swollman && (cmd == SIOCAIFADDR) 3568071Swollman && (ifra->ifra_dstaddr.sin_addr.s_addr 3578071Swollman == INADDR_ANY)) { 358191500Srwatson error = EDESTADDRREQ; 359191500Srwatson goto out_unlock; 3608071Swollman } 3611541Srgrimes } 362191500Srwatson if (cmd == SIOCDIFADDR && ia == NULL) { 363191500Srwatson error = EADDRNOTAVAIL; 364191500Srwatson goto out_unlock; 365191500Srwatson } 3661541Srgrimes /* FALLTHROUGH */ 3671541Srgrimes case SIOCSIFADDR: 3681541Srgrimes case SIOCSIFNETMASK: 3691541Srgrimes case SIOCSIFDSTADDR: 370184295Sbz if (ia == NULL) { 37120407Swollman ia = (struct in_ifaddr *) 372191500Srwatson malloc(sizeof *ia, M_IFADDR, M_NOWAIT | 373191500Srwatson M_ZERO); 374191500Srwatson if (ia == NULL) { 375191500Srwatson error = ENOBUFS; 376191500Srwatson goto out_unlock; 377191500Srwatson } 378191500Srwatson 37920407Swollman ifa = &ia->ia_ifa; 380108033Shsu IFA_LOCK_INIT(ifa); 38120407Swollman ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 38220407Swollman ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 38320407Swollman ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 384108033Shsu ifa->ifa_refcnt = 1; 385108033Shsu 3861541Srgrimes ia->ia_sockmask.sin_len = 8; 38785740Sdes ia->ia_sockmask.sin_family = AF_INET; 3881541Srgrimes if (ifp->if_flags & IFF_BROADCAST) { 3891541Srgrimes ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 3901541Srgrimes ia->ia_broadaddr.sin_family = AF_INET; 3911541Srgrimes } 3921541Srgrimes ia->ia_ifp = ifp; 393151824Sglebius 394191285Srwatson TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 395191285Srwatson s = splnet(); 396181803Sbz TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 39715092Sdg splx(s); 39887124Sbrian iaIsNew = 1; 3991541Srgrimes } 4001541Srgrimes break; 4011541Srgrimes 4021541Srgrimes case SIOCSIFBRDADDR: 4031541Srgrimes case SIOCGIFADDR: 4041541Srgrimes case SIOCGIFNETMASK: 4051541Srgrimes case SIOCGIFDSTADDR: 4061541Srgrimes case SIOCGIFBRDADDR: 407191500Srwatson if (ia == NULL) { 408191500Srwatson error = EADDRNOTAVAIL; 409191500Srwatson goto out_unlock; 410191500Srwatson } 4111541Srgrimes break; 4121541Srgrimes } 413191500Srwatson 414191500Srwatson /* 415191500Srwatson * Most paths in this switch return directly or via out_unlock. Only 416191500Srwatson * paths that remove the address break in order to hit common removal 417191500Srwatson * code. 418191500Srwatson * 419191500Srwatson * XXXRW: We enter the switch with IF_ADDR_LOCK() held, but leave 420191500Srwatson * without it. This is a bug. 421191500Srwatson */ 422191500Srwatson IF_ADDR_LOCK_ASSERT(ifp); 4231541Srgrimes switch (cmd) { 4241541Srgrimes case SIOCGIFADDR: 4251541Srgrimes *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 426191500Srwatson goto out_unlock; 4271541Srgrimes 4281541Srgrimes case SIOCGIFBRDADDR: 429191500Srwatson if ((ifp->if_flags & IFF_BROADCAST) == 0) { 430191500Srwatson error = EINVAL; 431191500Srwatson goto out_unlock; 432191500Srwatson } 4331541Srgrimes *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 434191500Srwatson goto out_unlock; 4351541Srgrimes 4361541Srgrimes case SIOCGIFDSTADDR: 437191500Srwatson if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 438191500Srwatson error = EINVAL; 439191500Srwatson goto out_unlock; 440191500Srwatson } 4411541Srgrimes *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 442191500Srwatson goto out_unlock; 4431541Srgrimes 4441541Srgrimes case SIOCGIFNETMASK: 4451541Srgrimes *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 446191500Srwatson goto out_unlock; 4471541Srgrimes 4481541Srgrimes case SIOCSIFDSTADDR: 449191500Srwatson if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 450191500Srwatson error = EINVAL; 451191500Srwatson goto out_unlock; 452191500Srwatson } 4531541Srgrimes oldaddr = ia->ia_dstaddr; 4541541Srgrimes ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 455191500Srwatson IF_ADDR_UNLOCK(ifp); 456191500Srwatson 457191500Srwatson /* 458191500Srwatson * XXXRW: Locks dropped for if_ioctl and rtinit, but ia is 459191500Srwatson * still being used. 460191500Srwatson */ 461184295Sbz if (ifp->if_ioctl != NULL) { 462146883Siedowse error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, 463146883Siedowse (caddr_t)ia); 464146883Siedowse if (error) { 465146883Siedowse ia->ia_dstaddr = oldaddr; 466146883Siedowse return (error); 467146883Siedowse } 4681541Srgrimes } 4691541Srgrimes if (ia->ia_flags & IFA_ROUTE) { 4701541Srgrimes ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 4711541Srgrimes rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 4721541Srgrimes ia->ia_ifa.ifa_dstaddr = 4731541Srgrimes (struct sockaddr *)&ia->ia_dstaddr; 4741541Srgrimes rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 4751541Srgrimes } 47687124Sbrian return (0); 4771541Srgrimes 4781541Srgrimes case SIOCSIFBRDADDR: 479191500Srwatson if ((ifp->if_flags & IFF_BROADCAST) == 0) { 480191500Srwatson error = EINVAL; 481191500Srwatson goto out_unlock; 482191500Srwatson } 4831541Srgrimes ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 484191500Srwatson goto out_unlock; 4851541Srgrimes 4861541Srgrimes case SIOCSIFADDR: 487191500Srwatson IF_ADDR_UNLOCK(ifp); 488191500Srwatson 489191500Srwatson /* 490191500Srwatson * XXXRW: Locks dropped for in_ifinit and in_joingroup, but ia 491191500Srwatson * is still being used. 492191500Srwatson */ 49387124Sbrian error = in_ifinit(ifp, ia, 49487124Sbrian (struct sockaddr_in *) &ifr->ifr_addr, 1); 49587124Sbrian if (error != 0 && iaIsNew) 49687124Sbrian break; 497168032Sbms if (error == 0) { 498189603Sbms ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 499189592Sbms if (iaIsFirst && 500189592Sbms (ifp->if_flags & IFF_MULTICAST) != 0) { 501189592Sbms error = in_joingroup(ifp, &allhosts_addr, 502189592Sbms NULL, &ii->ii_allhosts); 503189592Sbms } 504126264Smlaier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 505168032Sbms } 50687124Sbrian return (0); 5071541Srgrimes 5081541Srgrimes case SIOCSIFNETMASK: 50985740Sdes ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr; 51085740Sdes ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 511191500Srwatson goto out_unlock; 5121541Srgrimes 5131541Srgrimes case SIOCAIFADDR: 5141541Srgrimes maskIsNew = 0; 5151541Srgrimes hostIsNew = 1; 5161541Srgrimes error = 0; 5171541Srgrimes if (ia->ia_addr.sin_family == AF_INET) { 5181541Srgrimes if (ifra->ifra_addr.sin_len == 0) { 5191541Srgrimes ifra->ifra_addr = ia->ia_addr; 5201541Srgrimes hostIsNew = 0; 5211541Srgrimes } else if (ifra->ifra_addr.sin_addr.s_addr == 5221541Srgrimes ia->ia_addr.sin_addr.s_addr) 5231541Srgrimes hostIsNew = 0; 5241541Srgrimes } 525191500Srwatson IF_ADDR_UNLOCK(ifp); 526191500Srwatson 527191500Srwatson /* 528191500Srwatson * XXXRW: Locks dropped for in_ifscrub and in_ifinit, but ia 529191500Srwatson * is still being used. 530191500Srwatson */ 5311541Srgrimes if (ifra->ifra_mask.sin_len) { 5321541Srgrimes in_ifscrub(ifp, ia); 5331541Srgrimes ia->ia_sockmask = ifra->ifra_mask; 53485740Sdes ia->ia_sockmask.sin_family = AF_INET; 5351541Srgrimes ia->ia_subnetmask = 5361541Srgrimes ntohl(ia->ia_sockmask.sin_addr.s_addr); 5371541Srgrimes maskIsNew = 1; 5381541Srgrimes } 5391541Srgrimes if ((ifp->if_flags & IFF_POINTOPOINT) && 5401541Srgrimes (ifra->ifra_dstaddr.sin_family == AF_INET)) { 5411541Srgrimes in_ifscrub(ifp, ia); 5421541Srgrimes ia->ia_dstaddr = ifra->ifra_dstaddr; 5431541Srgrimes maskIsNew = 1; /* We lie; but the effect's the same */ 5441541Srgrimes } 5451541Srgrimes if (ifra->ifra_addr.sin_family == AF_INET && 5461541Srgrimes (hostIsNew || maskIsNew)) 5471541Srgrimes error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 54887124Sbrian if (error != 0 && iaIsNew) 54987124Sbrian break; 55087124Sbrian 5511541Srgrimes if ((ifp->if_flags & IFF_BROADCAST) && 5521541Srgrimes (ifra->ifra_broadaddr.sin_family == AF_INET)) 5531541Srgrimes ia->ia_broadaddr = ifra->ifra_broadaddr; 554168032Sbms if (error == 0) { 555189603Sbms ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 556189592Sbms if (iaIsFirst && 557189592Sbms (ifp->if_flags & IFF_MULTICAST) != 0) { 558189592Sbms error = in_joingroup(ifp, &allhosts_addr, 559189592Sbms NULL, &ii->ii_allhosts); 560189592Sbms } 561126264Smlaier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 562168032Sbms } 5631541Srgrimes return (error); 5641541Srgrimes 5651541Srgrimes case SIOCDIFADDR: 566191500Srwatson IF_ADDR_UNLOCK(ifp); 567191500Srwatson 56874299Sru /* 569191500Srwatson * XXXRW: Locks dropped for in_ifscrub and in_ifadown, but ia 570191500Srwatson * is still being used. 571191500Srwatson * 57274299Sru * in_ifscrub kills the interface route. 57374299Sru */ 5741541Srgrimes in_ifscrub(ifp, ia); 575191500Srwatson 57615092Sdg /* 57774299Sru * in_ifadown gets rid of all the rest of 57874299Sru * the routes. This is not quite the right 57974299Sru * thing to do, but at least if we are running 58074299Sru * a routing process they will come back. 58174299Sru */ 58276469Sru in_ifadown(&ia->ia_ifa, 1); 583126264Smlaier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 58487124Sbrian error = 0; 5851541Srgrimes break; 5861541Srgrimes 5871541Srgrimes default: 588191443Srwatson panic("in_control: unsupported ioctl"); 5891541Srgrimes } 59087124Sbrian 59187124Sbrian /* 592191500Srwatson * XXXRW: In a more ideal world, we would still be holding 593191500Srwatson * IF_ADDR_LOCK here. 59487124Sbrian */ 595191285Srwatson IF_ADDR_LOCK(ifp); 596191285Srwatson TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 597191285Srwatson IF_ADDR_UNLOCK(ifp); 59887124Sbrian s = splnet(); 599181803Sbz TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 600168032Sbms if (ia->ia_addr.sin_family == AF_INET) { 601154777Sandre LIST_REMOVE(ia, ia_hash); 602168032Sbms /* 603168032Sbms * If this is the last IPv4 address configured on this 604168032Sbms * interface, leave the all-hosts group. 605189592Sbms * No state-change report need be transmitted. 606168032Sbms */ 607168032Sbms oia = NULL; 608168032Sbms IFP_TO_IA(ifp, oia); 609168032Sbms if (oia == NULL) { 610189603Sbms ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 611168032Sbms IN_MULTI_LOCK(); 612189592Sbms if (ii->ii_allhosts) { 613189592Sbms (void)in_leavegroup_locked(ii->ii_allhosts, 614189592Sbms NULL); 615189592Sbms ii->ii_allhosts = NULL; 616189592Sbms } 617168032Sbms IN_MULTI_UNLOCK(); 618168032Sbms } 619168032Sbms } 62087124Sbrian IFAFREE(&ia->ia_ifa); 62187124Sbrian splx(s); 62287124Sbrian 62387124Sbrian return (error); 624191500Srwatson 625191500Srwatsonout_unlock: 626191500Srwatson IF_ADDR_UNLOCK(ifp); 627191500Srwatson return (error); 6281541Srgrimes} 6291541Srgrimes 6301541Srgrimes/* 63155009Sshin * SIOC[GAD]LIFADDR. 63255009Sshin * SIOCGLIFADDR: get first address. (?!?) 63355009Sshin * SIOCGLIFADDR with IFLR_PREFIX: 63455009Sshin * get first address that matches the specified prefix. 63555009Sshin * SIOCALIFADDR: add the specified address. 63655009Sshin * SIOCALIFADDR with IFLR_PREFIX: 63755009Sshin * EINVAL since we can't deduce hostid part of the address. 63855009Sshin * SIOCDLIFADDR: delete the specified address. 63955009Sshin * SIOCDLIFADDR with IFLR_PREFIX: 64055009Sshin * delete the first address that matches the specified prefix. 64155009Sshin * return values: 64255009Sshin * EINVAL on invalid parameters 64355009Sshin * EADDRNOTAVAIL on prefix match failed/specified address not found 64455009Sshin * other values may be returned from in_ioctl() 64555009Sshin */ 64655009Sshinstatic int 647169454Srwatsonin_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 648169454Srwatson struct ifnet *ifp, struct thread *td) 64955009Sshin{ 65055009Sshin struct if_laddrreq *iflr = (struct if_laddrreq *)data; 65155009Sshin struct ifaddr *ifa; 65255009Sshin 65355009Sshin /* sanity checks */ 654184295Sbz if (data == NULL || ifp == NULL) { 65555009Sshin panic("invalid argument to in_lifaddr_ioctl"); 65655009Sshin /*NOTRECHED*/ 65755009Sshin } 65855009Sshin 65955009Sshin switch (cmd) { 66055009Sshin case SIOCGLIFADDR: 66155009Sshin /* address must be specified on GET with IFLR_PREFIX */ 66255009Sshin if ((iflr->flags & IFLR_PREFIX) == 0) 66355009Sshin break; 66455009Sshin /*FALLTHROUGH*/ 66555009Sshin case SIOCALIFADDR: 66655009Sshin case SIOCDLIFADDR: 66755009Sshin /* address must be specified on ADD and DELETE */ 66855917Sshin if (iflr->addr.ss_family != AF_INET) 669184295Sbz return (EINVAL); 67055917Sshin if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 671184295Sbz return (EINVAL); 67255009Sshin /* XXX need improvement */ 67355917Sshin if (iflr->dstaddr.ss_family 67455917Sshin && iflr->dstaddr.ss_family != AF_INET) 675184295Sbz return (EINVAL); 67655917Sshin if (iflr->dstaddr.ss_family 67755917Sshin && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 678184295Sbz return (EINVAL); 67955009Sshin break; 68055009Sshin default: /*shouldn't happen*/ 681184295Sbz return (EOPNOTSUPP); 68255009Sshin } 68355009Sshin if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 684184295Sbz return (EINVAL); 68555009Sshin 68655009Sshin switch (cmd) { 68755009Sshin case SIOCALIFADDR: 68855009Sshin { 68955009Sshin struct in_aliasreq ifra; 69055009Sshin 69155009Sshin if (iflr->flags & IFLR_PREFIX) 692184295Sbz return (EINVAL); 69355009Sshin 69455009Sshin /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 69555009Sshin bzero(&ifra, sizeof(ifra)); 69655009Sshin bcopy(iflr->iflr_name, ifra.ifra_name, 69755009Sshin sizeof(ifra.ifra_name)); 69855009Sshin 69955917Sshin bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 70055009Sshin 70155917Sshin if (iflr->dstaddr.ss_family) { /*XXX*/ 70255009Sshin bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 70355917Sshin iflr->dstaddr.ss_len); 70455009Sshin } 70555009Sshin 70655009Sshin ifra.ifra_mask.sin_family = AF_INET; 70755009Sshin ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 70855009Sshin in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 70955009Sshin 710184295Sbz return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td)); 71155009Sshin } 71255009Sshin case SIOCGLIFADDR: 71355009Sshin case SIOCDLIFADDR: 71455009Sshin { 71555009Sshin struct in_ifaddr *ia; 71655009Sshin struct in_addr mask, candidate, match; 71755009Sshin struct sockaddr_in *sin; 71855009Sshin 71955009Sshin bzero(&mask, sizeof(mask)); 720170855Smjacob bzero(&match, sizeof(match)); 72155009Sshin if (iflr->flags & IFLR_PREFIX) { 72255009Sshin /* lookup a prefix rather than address. */ 72355009Sshin in_len2mask(&mask, iflr->prefixlen); 72455009Sshin 72555009Sshin sin = (struct sockaddr_in *)&iflr->addr; 72655009Sshin match.s_addr = sin->sin_addr.s_addr; 72755009Sshin match.s_addr &= mask.s_addr; 72855009Sshin 72955009Sshin /* if you set extra bits, that's wrong */ 73055009Sshin if (match.s_addr != sin->sin_addr.s_addr) 731184295Sbz return (EINVAL); 73255009Sshin 73355009Sshin } else { 734170855Smjacob /* on getting an address, take the 1st match */ 735170855Smjacob /* on deleting an address, do exact match */ 736170855Smjacob if (cmd != SIOCGLIFADDR) { 73755009Sshin in_len2mask(&mask, 32); 73855009Sshin sin = (struct sockaddr_in *)&iflr->addr; 73955009Sshin match.s_addr = sin->sin_addr.s_addr; 74055009Sshin } 74155009Sshin } 74255009Sshin 74355009Sshin TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 74455009Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 74555009Sshin continue; 746170855Smjacob if (match.s_addr == 0) 74755009Sshin break; 74855009Sshin candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 74955009Sshin candidate.s_addr &= mask.s_addr; 75055009Sshin if (candidate.s_addr == match.s_addr) 75155009Sshin break; 75255009Sshin } 753184295Sbz if (ifa == NULL) 754184295Sbz return (EADDRNOTAVAIL); 75555009Sshin ia = (struct in_ifaddr *)ifa; 75655009Sshin 75755009Sshin if (cmd == SIOCGLIFADDR) { 75855009Sshin /* fill in the if_laddrreq structure */ 75955009Sshin bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 76055009Sshin 76155009Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 76255009Sshin bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 76355009Sshin ia->ia_dstaddr.sin_len); 76455009Sshin } else 76555009Sshin bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 76655009Sshin 76755009Sshin iflr->prefixlen = 76855009Sshin in_mask2len(&ia->ia_sockmask.sin_addr); 76955009Sshin 77055009Sshin iflr->flags = 0; /*XXX*/ 77155009Sshin 772184295Sbz return (0); 77355009Sshin } else { 77455009Sshin struct in_aliasreq ifra; 77555009Sshin 77655009Sshin /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 77755009Sshin bzero(&ifra, sizeof(ifra)); 77855009Sshin bcopy(iflr->iflr_name, ifra.ifra_name, 77955009Sshin sizeof(ifra.ifra_name)); 78055009Sshin 78155009Sshin bcopy(&ia->ia_addr, &ifra.ifra_addr, 78255009Sshin ia->ia_addr.sin_len); 78355009Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 78455009Sshin bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 78555009Sshin ia->ia_dstaddr.sin_len); 78655009Sshin } 78755009Sshin bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 78855009Sshin ia->ia_sockmask.sin_len); 78955009Sshin 790184295Sbz return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 791184295Sbz ifp, td)); 79255009Sshin } 79355009Sshin } 79455009Sshin } 79555009Sshin 796184295Sbz return (EOPNOTSUPP); /*just for safety*/ 79755009Sshin} 79855009Sshin 79955009Sshin/* 8001541Srgrimes * Delete any existing route for an interface. 8011541Srgrimes */ 80222672Swollmanvoid 803169454Srwatsonin_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 8041541Srgrimes{ 805169454Srwatson 806137628Smlaier in_scrubprefix(ia); 8071541Srgrimes} 8081541Srgrimes 8091541Srgrimes/* 8101541Srgrimes * Initialize an interface's internet address 8111541Srgrimes * and routing table entry. 8121541Srgrimes */ 81312296Sphkstatic int 814169454Srwatsonin_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, 815169454Srwatson int scrub) 8161541Srgrimes{ 817183550Szec INIT_VNET_INET(ifp->if_vnet); 8181541Srgrimes register u_long i = ntohl(sin->sin_addr.s_addr); 8191541Srgrimes struct sockaddr_in oldaddr; 820192011Sqingli struct rtentry *rt = NULL; 821192011Sqingli struct rt_addrinfo info; 822192011Sqingli static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 82394326Sbrian int s = splimp(), flags = RTF_UP, error = 0; 8241541Srgrimes 8251541Srgrimes oldaddr = ia->ia_addr; 826105748Ssuz if (oldaddr.sin_family == AF_INET) 827105748Ssuz LIST_REMOVE(ia, ia_hash); 8281541Srgrimes ia->ia_addr = *sin; 829105748Ssuz if (ia->ia_addr.sin_family == AF_INET) 830105748Ssuz LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 831105748Ssuz ia, ia_hash); 8321541Srgrimes /* 8331541Srgrimes * Give the interface a chance to initialize 8341541Srgrimes * if this is its first address, 8351541Srgrimes * and to validate the address if necessary. 8361541Srgrimes */ 837184295Sbz if (ifp->if_ioctl != NULL) { 838146883Siedowse error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 839146883Siedowse if (error) { 840146883Siedowse splx(s); 841146883Siedowse /* LIST_REMOVE(ia, ia_hash) is done in in_control */ 842146883Siedowse ia->ia_addr = oldaddr; 843146883Siedowse if (ia->ia_addr.sin_family == AF_INET) 844146883Siedowse LIST_INSERT_HEAD(INADDR_HASH( 845146883Siedowse ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 846179971Sgonzo else 847179971Sgonzo /* 848179971Sgonzo * If oldaddr family is not AF_INET (e.g. 849179971Sgonzo * interface has been just created) in_control 850179971Sgonzo * does not call LIST_REMOVE, and we end up 851179971Sgonzo * with bogus ia entries in hash 852179971Sgonzo */ 853179971Sgonzo LIST_REMOVE(ia, ia_hash); 854146883Siedowse return (error); 855146883Siedowse } 8561541Srgrimes } 8571541Srgrimes splx(s); 8581541Srgrimes if (scrub) { 8591541Srgrimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 8601541Srgrimes in_ifscrub(ifp, ia); 8611541Srgrimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 8621541Srgrimes } 8631541Srgrimes if (IN_CLASSA(i)) 8641541Srgrimes ia->ia_netmask = IN_CLASSA_NET; 8651541Srgrimes else if (IN_CLASSB(i)) 8661541Srgrimes ia->ia_netmask = IN_CLASSB_NET; 8671541Srgrimes else 8681541Srgrimes ia->ia_netmask = IN_CLASSC_NET; 8691541Srgrimes /* 8701541Srgrimes * The subnet mask usually includes at least the standard network part, 8711541Srgrimes * but may may be smaller in the case of supernetting. 8721541Srgrimes * If it is set, we believe it. 8731541Srgrimes */ 8741541Srgrimes if (ia->ia_subnetmask == 0) { 8751541Srgrimes ia->ia_subnetmask = ia->ia_netmask; 8761541Srgrimes ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 8771541Srgrimes } else 8781541Srgrimes ia->ia_netmask &= ia->ia_subnetmask; 8791541Srgrimes ia->ia_net = i & ia->ia_netmask; 8801541Srgrimes ia->ia_subnet = i & ia->ia_subnetmask; 8811541Srgrimes in_socktrim(&ia->ia_sockmask); 882143868Sglebius#ifdef DEV_CARP 8831541Srgrimes /* 884143868Sglebius * XXX: carp(4) does not have interface route 885143868Sglebius */ 886143868Sglebius if (ifp->if_type == IFT_CARP) 887143868Sglebius return (0); 888143868Sglebius#endif 889143868Sglebius /* 8901541Srgrimes * Add route for the network. 8911541Srgrimes */ 8921541Srgrimes ia->ia_ifa.ifa_metric = ifp->if_metric; 8931541Srgrimes if (ifp->if_flags & IFF_BROADCAST) { 8941541Srgrimes ia->ia_broadaddr.sin_addr.s_addr = 8951541Srgrimes htonl(ia->ia_subnet | ~ia->ia_subnetmask); 8961541Srgrimes ia->ia_netbroadcast.s_addr = 8971541Srgrimes htonl(ia->ia_net | ~ ia->ia_netmask); 8981541Srgrimes } else if (ifp->if_flags & IFF_LOOPBACK) { 899137833Smlaier ia->ia_dstaddr = ia->ia_addr; 9001541Srgrimes flags |= RTF_HOST; 9011541Srgrimes } else if (ifp->if_flags & IFF_POINTOPOINT) { 9021541Srgrimes if (ia->ia_dstaddr.sin_family != AF_INET) 9031541Srgrimes return (0); 9041541Srgrimes flags |= RTF_HOST; 9051541Srgrimes } 906137628Smlaier if ((error = in_addprefix(ia, flags)) != 0) 907137628Smlaier return (error); 90894326Sbrian 909192011Sqingli /* 910192011Sqingli * add a loopback route to self 911192011Sqingli */ 912192011Sqingli if (!(ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) { 913192011Sqingli bzero(&info, sizeof(info)); 914192011Sqingli info.rti_ifp = V_loif; 915192011Sqingli info.rti_flags = ia->ia_flags | RTF_HOST | RTF_STATIC; 916192011Sqingli info.rti_info[RTAX_DST] = (struct sockaddr *)&ia->ia_addr; 917192011Sqingli info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 918192011Sqingli error = rtrequest1_fib(RTM_ADD, &info, &rt, 0); 919192011Sqingli 920192011Sqingli if (error == 0 && rt != NULL) { 921192011Sqingli RT_LOCK(rt); 922192011Sqingli ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 923192011Sqingli rt->rt_ifp->if_type; 924192011Sqingli ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 925192011Sqingli rt->rt_ifp->if_index; 926192011Sqingli RT_REMREF(rt); 927192011Sqingli RT_UNLOCK(rt); 928192011Sqingli } else if (error != 0) 929192011Sqingli log(LOG_INFO, "in_ifinit: insertion failed\n"); 930192011Sqingli } 931192011Sqingli 9321541Srgrimes return (error); 9331541Srgrimes} 9341541Srgrimes 935137628Smlaier#define rtinitflags(x) \ 936137628Smlaier ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 937137628Smlaier ? RTF_HOST : 0) 938137628Smlaier/* 939170855Smjacob * Check if we have a route for the given prefix already or add one accordingly. 940137628Smlaier */ 941137628Smlaierstatic int 942169454Srwatsonin_addprefix(struct in_ifaddr *target, int flags) 943137628Smlaier{ 944183550Szec INIT_VNET_INET(curvnet); 945137628Smlaier struct in_ifaddr *ia; 946151555Sglebius struct in_addr prefix, mask, p, m; 947137628Smlaier int error; 9481541Srgrimes 949170855Smjacob if ((flags & RTF_HOST) != 0) { 950137628Smlaier prefix = target->ia_dstaddr.sin_addr; 951170855Smjacob mask.s_addr = 0; 952170855Smjacob } else { 953137628Smlaier prefix = target->ia_addr.sin_addr; 954137628Smlaier mask = target->ia_sockmask.sin_addr; 955137628Smlaier prefix.s_addr &= mask.s_addr; 956137628Smlaier } 957137628Smlaier 958181803Sbz TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 959151555Sglebius if (rtinitflags(ia)) { 960137628Smlaier p = ia->ia_addr.sin_addr; 961151555Sglebius 962151555Sglebius if (prefix.s_addr != p.s_addr) 963151555Sglebius continue; 964151555Sglebius } else { 965151555Sglebius p = ia->ia_addr.sin_addr; 966151555Sglebius m = ia->ia_sockmask.sin_addr; 967151555Sglebius p.s_addr &= m.s_addr; 968151555Sglebius 969151555Sglebius if (prefix.s_addr != p.s_addr || 970151555Sglebius mask.s_addr != m.s_addr) 971151555Sglebius continue; 972137628Smlaier } 973137628Smlaier 974137628Smlaier /* 975137628Smlaier * If we got a matching prefix route inserted by other 976137628Smlaier * interface address, we are done here. 977137628Smlaier */ 978149221Sglebius if (ia->ia_flags & IFA_ROUTE) { 979181803Sbz if (V_sameprefixcarponly && 980149221Sglebius target->ia_ifp->if_type != IFT_CARP && 981149221Sglebius ia->ia_ifp->if_type != IFT_CARP) 982149221Sglebius return (EEXIST); 983149221Sglebius else 984149221Sglebius return (0); 985149221Sglebius } 986137628Smlaier } 987137628Smlaier 988137628Smlaier /* 989137628Smlaier * No-one seem to have this prefix route, so we try to insert it. 990137628Smlaier */ 991137628Smlaier error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 992137628Smlaier if (!error) 993137628Smlaier target->ia_flags |= IFA_ROUTE; 994184295Sbz return (error); 995137628Smlaier} 996137628Smlaier 997186119Sqingliextern void arp_ifscrub(struct ifnet *ifp, uint32_t addr); 998186119Sqingli 9991541Srgrimes/* 1000137628Smlaier * If there is no other address in the system that can serve a route to the 1001137628Smlaier * same prefix, remove the route. Hand over the route to the new address 1002137628Smlaier * otherwise. 1003137628Smlaier */ 1004137628Smlaierstatic int 1005169454Srwatsonin_scrubprefix(struct in_ifaddr *target) 1006137628Smlaier{ 1007183550Szec INIT_VNET_INET(curvnet); 1008137628Smlaier struct in_ifaddr *ia; 1009137628Smlaier struct in_addr prefix, mask, p; 1010137628Smlaier int error; 1011192011Sqingli struct rt_addrinfo info; 1012192011Sqingli struct sockaddr_dl null_sdl; 1013137628Smlaier 1014137628Smlaier if ((target->ia_flags & IFA_ROUTE) == 0) 1015184295Sbz return (0); 1016137628Smlaier 1017192011Sqingli if (!(target->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) { 1018192011Sqingli bzero(&null_sdl, sizeof(null_sdl)); 1019192011Sqingli null_sdl.sdl_len = sizeof(null_sdl); 1020192011Sqingli null_sdl.sdl_family = AF_LINK; 1021192011Sqingli null_sdl.sdl_type = V_loif->if_type; 1022192011Sqingli null_sdl.sdl_index = V_loif->if_index; 1023192011Sqingli bzero(&info, sizeof(info)); 1024192011Sqingli info.rti_flags = target->ia_flags | RTF_HOST | RTF_STATIC; 1025192011Sqingli info.rti_info[RTAX_DST] = (struct sockaddr *)&target->ia_addr; 1026192011Sqingli info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 1027192011Sqingli error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0); 1028192011Sqingli 1029192011Sqingli if (error != 0) 1030192011Sqingli log(LOG_INFO, "in_scrubprefix: deletion failed\n"); 1031192011Sqingli } 1032192011Sqingli 1033137628Smlaier if (rtinitflags(target)) 1034137628Smlaier prefix = target->ia_dstaddr.sin_addr; 1035137628Smlaier else { 1036137628Smlaier prefix = target->ia_addr.sin_addr; 1037137628Smlaier mask = target->ia_sockmask.sin_addr; 1038137628Smlaier prefix.s_addr &= mask.s_addr; 1039186119Sqingli /* remove arp cache */ 1040186119Sqingli arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr); 1041137628Smlaier } 1042137628Smlaier 1043181803Sbz TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1044137628Smlaier if (rtinitflags(ia)) 1045137628Smlaier p = ia->ia_dstaddr.sin_addr; 1046137628Smlaier else { 1047137628Smlaier p = ia->ia_addr.sin_addr; 1048137628Smlaier p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 1049137628Smlaier } 1050137628Smlaier 1051137628Smlaier if (prefix.s_addr != p.s_addr) 1052137628Smlaier continue; 1053137628Smlaier 1054137628Smlaier /* 1055137628Smlaier * If we got a matching prefix address, move IFA_ROUTE and 1056137628Smlaier * the route itself to it. Make sure that routing daemons 1057137628Smlaier * get a heads-up. 1058143868Sglebius * 1059143868Sglebius * XXX: a special case for carp(4) interface 1060137628Smlaier */ 1061143868Sglebius if ((ia->ia_flags & IFA_ROUTE) == 0 1062143868Sglebius#ifdef DEV_CARP 1063143868Sglebius && (ia->ia_ifp->if_type != IFT_CARP) 1064143868Sglebius#endif 1065143868Sglebius ) { 1066137628Smlaier rtinit(&(target->ia_ifa), (int)RTM_DELETE, 1067137628Smlaier rtinitflags(target)); 1068137628Smlaier target->ia_flags &= ~IFA_ROUTE; 1069137628Smlaier 1070137628Smlaier error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 1071137628Smlaier rtinitflags(ia) | RTF_UP); 1072137628Smlaier if (error == 0) 1073137628Smlaier ia->ia_flags |= IFA_ROUTE; 1074184295Sbz return (error); 1075137628Smlaier } 1076137628Smlaier } 1077137628Smlaier 1078137628Smlaier /* 1079137628Smlaier * As no-one seem to have this prefix, we can remove the route. 1080137628Smlaier */ 1081137628Smlaier rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 1082137628Smlaier target->ia_flags &= ~IFA_ROUTE; 1083184295Sbz return (0); 1084137628Smlaier} 1085137628Smlaier 1086137628Smlaier#undef rtinitflags 1087137628Smlaier 1088137628Smlaier/* 10891541Srgrimes * Return 1 if the address might be a local broadcast address. 10901541Srgrimes */ 10911549Srgrimesint 1092169454Srwatsonin_broadcast(struct in_addr in, struct ifnet *ifp) 10931541Srgrimes{ 10941541Srgrimes register struct ifaddr *ifa; 10951541Srgrimes u_long t; 10961541Srgrimes 10971541Srgrimes if (in.s_addr == INADDR_BROADCAST || 10981541Srgrimes in.s_addr == INADDR_ANY) 1099184295Sbz return (1); 11001541Srgrimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 1101184295Sbz return (0); 11021541Srgrimes t = ntohl(in.s_addr); 11031541Srgrimes /* 11041541Srgrimes * Look through the list of addresses for a match 11051541Srgrimes * with a broadcast address. 11061541Srgrimes */ 11071541Srgrimes#define ia ((struct in_ifaddr *)ifa) 110874362Sphk TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 11091541Srgrimes if (ifa->ifa_addr->sa_family == AF_INET && 11101541Srgrimes (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 11111541Srgrimes in.s_addr == ia->ia_netbroadcast.s_addr || 11121541Srgrimes /* 11131541Srgrimes * Check for old-style (host 0) broadcast. 11141541Srgrimes */ 111513351Sguido t == ia->ia_subnet || t == ia->ia_net) && 111613351Sguido /* 111713351Sguido * Check for an all one subnetmask. These 111813351Sguido * only exist when an interface gets a secondary 111913351Sguido * address. 112013351Sguido */ 112113351Sguido ia->ia_subnetmask != (u_long)0xffffffff) 1122184295Sbz return (1); 11231541Srgrimes return (0); 11241541Srgrimes#undef ia 11251541Srgrimes} 1126167729Sbms 11271541Srgrimes/* 1128189592Sbms * On interface removal, clean up IPv4 data structures hung off of the ifnet. 1129189592Sbms */ 1130189592Sbmsvoid 1131189592Sbmsin_ifdetach(struct ifnet *ifp) 1132189592Sbms{ 1133189592Sbms INIT_VNET_INET(ifp->if_vnet); 1134189592Sbms 1135189592Sbms in_pcbpurgeif0(&V_ripcbinfo, ifp); 1136189592Sbms in_pcbpurgeif0(&V_udbinfo, ifp); 1137189592Sbms in_purgemaddrs(ifp); 1138189592Sbms} 1139189592Sbms 1140189592Sbms/* 1141167729Sbms * Delete all IPv4 multicast address records, and associated link-layer 1142167729Sbms * multicast address records, associated with ifp. 1143189592Sbms * XXX It looks like domifdetach runs AFTER the link layer cleanup. 1144189931Sbms * XXX This should not race with ifma_protospec being set during 1145189931Sbms * a new allocation, if it does, we have bigger problems. 1146162718Sbms */ 1147167729Sbmsstatic void 1148167729Sbmsin_purgemaddrs(struct ifnet *ifp) 1149162718Sbms{ 1150189592Sbms LIST_HEAD(,in_multi) purgeinms; 1151189592Sbms struct in_multi *inm, *tinm; 1152189592Sbms struct ifmultiaddr *ifma; 1153162718Sbms 1154189592Sbms LIST_INIT(&purgeinms); 1155162718Sbms IN_MULTI_LOCK(); 1156189592Sbms 1157189592Sbms /* 1158189592Sbms * Extract list of in_multi associated with the detaching ifp 1159189592Sbms * which the PF_INET layer is about to release. 1160189592Sbms * We need to do this as IF_ADDR_LOCK() may be re-acquired 1161189592Sbms * by code further down. 1162189592Sbms */ 1163189592Sbms IF_ADDR_LOCK(ifp); 1164189592Sbms TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1165189931Sbms if (ifma->ifma_addr->sa_family != AF_INET || 1166189931Sbms ifma->ifma_protospec == NULL) 1167189592Sbms continue; 1168189931Sbms#if 0 1169189931Sbms KASSERT(ifma->ifma_protospec != NULL, 1170189931Sbms ("%s: ifma_protospec is NULL", __func__)); 1171189931Sbms#endif 1172189592Sbms inm = (struct in_multi *)ifma->ifma_protospec; 1173189592Sbms LIST_INSERT_HEAD(&purgeinms, inm, inm_link); 1174162718Sbms } 1175189592Sbms IF_ADDR_UNLOCK(ifp); 1176150296Srwatson 1177189592Sbms LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) { 1178191476Srwatson LIST_REMOVE(inm, inm_link); 1179189592Sbms inm_release_locked(inm); 1180189592Sbms } 1181189592Sbms igmp_ifdetach(ifp); 1182150296Srwatson 1183189592Sbms IN_MULTI_UNLOCK(); 1184150296Srwatson} 1185186119Sqingli 1186186119Sqingli#include <net/if_dl.h> 1187186119Sqingli#include <netinet/if_ether.h> 1188186119Sqingli 1189186119Sqinglistruct in_llentry { 1190186119Sqingli struct llentry base; 1191186119Sqingli struct sockaddr_in l3_addr4; 1192186119Sqingli}; 1193186119Sqingli 1194186119Sqinglistatic struct llentry * 1195186119Sqingliin_lltable_new(const struct sockaddr *l3addr, u_int flags) 1196186119Sqingli{ 1197186119Sqingli struct in_llentry *lle; 1198186119Sqingli 1199186119Sqingli lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO); 1200186119Sqingli if (lle == NULL) /* NB: caller generates msg */ 1201186119Sqingli return NULL; 1202186119Sqingli 1203186119Sqingli callout_init(&lle->base.la_timer, CALLOUT_MPSAFE); 1204186119Sqingli /* 1205186119Sqingli * For IPv4 this will trigger "arpresolve" to generate 1206186119Sqingli * an ARP request. 1207186119Sqingli */ 1208186119Sqingli lle->base.la_expire = time_second; /* mark expired */ 1209186119Sqingli lle->l3_addr4 = *(const struct sockaddr_in *)l3addr; 1210186119Sqingli lle->base.lle_refcnt = 1; 1211186119Sqingli LLE_LOCK_INIT(&lle->base); 1212186119Sqingli return &lle->base; 1213186119Sqingli} 1214186119Sqingli 1215186119Sqingli/* 1216186119Sqingli * Deletes an address from the address table. 1217186119Sqingli * This function is called by the timer functions 1218186119Sqingli * such as arptimer() and nd6_llinfo_timer(), and 1219186119Sqingli * the caller does the locking. 1220186119Sqingli */ 1221186119Sqinglistatic void 1222186119Sqingliin_lltable_free(struct lltable *llt, struct llentry *lle) 1223186119Sqingli{ 1224186150Skmacy LLE_WUNLOCK(lle); 1225186150Skmacy LLE_LOCK_DESTROY(lle); 1226186119Sqingli free(lle, M_LLTABLE); 1227186119Sqingli} 1228186119Sqingli 1229186119Sqinglistatic int 1230186119Sqingliin_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr) 1231186119Sqingli{ 1232186119Sqingli struct rtentry *rt; 1233186119Sqingli 1234186119Sqingli KASSERT(l3addr->sa_family == AF_INET, 1235186119Sqingli ("sin_family %d", l3addr->sa_family)); 1236186119Sqingli 1237186119Sqingli /* XXX rtalloc1 should take a const param */ 1238186119Sqingli rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 1239186119Sqingli if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 1240186119Sqingli log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 1241186119Sqingli inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 1242186119Sqingli if (rt != NULL) 1243186119Sqingli RTFREE_LOCKED(rt); 1244186119Sqingli return (EINVAL); 1245186119Sqingli } 1246186119Sqingli RTFREE_LOCKED(rt); 1247186119Sqingli return 0; 1248186119Sqingli} 1249186119Sqingli 1250186119Sqingli/* 1251186119Sqingli * Return NULL if not found or marked for deletion. 1252186119Sqingli * If found return lle read locked. 1253186119Sqingli */ 1254186119Sqinglistatic struct llentry * 1255186119Sqingliin_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1256186119Sqingli{ 1257186119Sqingli const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1258186119Sqingli struct ifnet *ifp = llt->llt_ifp; 1259186119Sqingli struct llentry *lle; 1260186119Sqingli struct llentries *lleh; 1261186119Sqingli u_int hashkey; 1262186119Sqingli 1263186119Sqingli IF_AFDATA_LOCK_ASSERT(ifp); 1264186119Sqingli KASSERT(l3addr->sa_family == AF_INET, 1265186119Sqingli ("sin_family %d", l3addr->sa_family)); 1266186119Sqingli 1267186119Sqingli hashkey = sin->sin_addr.s_addr; 1268186119Sqingli lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 1269186119Sqingli LIST_FOREACH(lle, lleh, lle_next) { 1270186708Sqingli struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle); 1271186119Sqingli if (lle->la_flags & LLE_DELETED) 1272186119Sqingli continue; 1273186708Sqingli if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr) 1274186119Sqingli break; 1275186119Sqingli } 1276186119Sqingli if (lle == NULL) { 1277186119Sqingli#ifdef DIAGNOSTICS 1278186119Sqingli if (flags & LLE_DELETE) 1279186119Sqingli log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 1280186119Sqingli#endif 1281186119Sqingli if (!(flags & LLE_CREATE)) 1282186119Sqingli return (NULL); 1283186119Sqingli /* 1284186119Sqingli * A route that covers the given address must have 1285186119Sqingli * been installed 1st because we are doing a resolution, 1286186119Sqingli * verify this. 1287186119Sqingli */ 1288186119Sqingli if (!(flags & LLE_IFADDR) && 1289186119Sqingli in_lltable_rtcheck(ifp, l3addr) != 0) 1290186119Sqingli goto done; 1291186119Sqingli 1292186119Sqingli lle = in_lltable_new(l3addr, flags); 1293186119Sqingli if (lle == NULL) { 1294186119Sqingli log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 1295186119Sqingli goto done; 1296186119Sqingli } 1297186119Sqingli lle->la_flags = flags & ~LLE_CREATE; 1298186119Sqingli if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 1299186119Sqingli bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 1300186119Sqingli lle->la_flags |= (LLE_VALID | LLE_STATIC); 1301186119Sqingli } 1302186119Sqingli 1303186119Sqingli lle->lle_tbl = llt; 1304186119Sqingli lle->lle_head = lleh; 1305186119Sqingli LIST_INSERT_HEAD(lleh, lle, lle_next); 1306186119Sqingli } else if (flags & LLE_DELETE) { 1307186119Sqingli if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 1308186119Sqingli LLE_WLOCK(lle); 1309186119Sqingli lle->la_flags = LLE_DELETED; 1310186119Sqingli LLE_WUNLOCK(lle); 1311186119Sqingli#ifdef DIAGNOSTICS 1312186119Sqingli log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 1313186119Sqingli#endif 1314186119Sqingli } 1315186119Sqingli lle = (void *)-1; 1316186119Sqingli 1317186119Sqingli } 1318186544Sbz if (LLE_IS_VALID(lle)) { 1319186119Sqingli if (flags & LLE_EXCLUSIVE) 1320186119Sqingli LLE_WLOCK(lle); 1321186119Sqingli else 1322186119Sqingli LLE_RLOCK(lle); 1323186119Sqingli } 1324186119Sqinglidone: 1325186119Sqingli return (lle); 1326186119Sqingli} 1327186119Sqingli 1328186119Sqinglistatic int 1329186119Sqingliin_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 1330186119Sqingli{ 1331186119Sqingli#define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle)) 1332186119Sqingli struct ifnet *ifp = llt->llt_ifp; 1333186119Sqingli struct llentry *lle; 1334186119Sqingli /* XXX stack use */ 1335186119Sqingli struct { 1336186119Sqingli struct rt_msghdr rtm; 1337186119Sqingli struct sockaddr_inarp sin; 1338186119Sqingli struct sockaddr_dl sdl; 1339186119Sqingli } arpc; 1340186119Sqingli int error, i; 1341186119Sqingli 1342186119Sqingli /* XXXXX 1343186119Sqingli * current IFNET_RLOCK() is mapped to IFNET_WLOCK() 1344186119Sqingli * so it is okay to use this ASSERT, change it when 1345186119Sqingli * IFNET lock is finalized 1346186119Sqingli */ 1347186119Sqingli IFNET_WLOCK_ASSERT(); 1348186119Sqingli 1349186119Sqingli error = 0; 1350186119Sqingli for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 1351186119Sqingli LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 1352186119Sqingli struct sockaddr_dl *sdl; 1353186119Sqingli 1354186119Sqingli /* skip deleted entries */ 1355186119Sqingli if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 1356186119Sqingli continue; 1357186980Sbz /* Skip if jailed and not a valid IP of the prison. */ 1358188144Sjamie if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 1359186980Sbz continue; 1360186119Sqingli /* 1361186119Sqingli * produce a msg made of: 1362186119Sqingli * struct rt_msghdr; 1363186119Sqingli * struct sockaddr_inarp; (IPv4) 1364186119Sqingli * struct sockaddr_dl; 1365186119Sqingli */ 1366186119Sqingli bzero(&arpc, sizeof(arpc)); 1367186119Sqingli arpc.rtm.rtm_msglen = sizeof(arpc); 1368186935Sharti arpc.rtm.rtm_version = RTM_VERSION; 1369186935Sharti arpc.rtm.rtm_type = RTM_GET; 1370186935Sharti arpc.rtm.rtm_flags = RTF_UP; 1371186935Sharti arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 1372186119Sqingli arpc.sin.sin_family = AF_INET; 1373186119Sqingli arpc.sin.sin_len = sizeof(arpc.sin); 1374186119Sqingli arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr; 1375186119Sqingli 1376186119Sqingli /* publish */ 1377186119Sqingli if (lle->la_flags & LLE_PUB) { 1378186119Sqingli arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 1379186119Sqingli /* proxy only */ 1380186119Sqingli if (lle->la_flags & LLE_PROXY) 1381186119Sqingli arpc.sin.sin_other = SIN_PROXY; 1382186119Sqingli } 1383186119Sqingli 1384186119Sqingli sdl = &arpc.sdl; 1385186119Sqingli sdl->sdl_family = AF_LINK; 1386186119Sqingli sdl->sdl_len = sizeof(*sdl); 1387186119Sqingli sdl->sdl_alen = ifp->if_addrlen; 1388186119Sqingli sdl->sdl_index = ifp->if_index; 1389186119Sqingli sdl->sdl_type = ifp->if_type; 1390186119Sqingli bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 1391186119Sqingli 1392186119Sqingli arpc.rtm.rtm_rmx.rmx_expire = 1393186119Sqingli lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 1394186500Sqingli arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 1395186119Sqingli if (lle->la_flags & LLE_STATIC) 1396186119Sqingli arpc.rtm.rtm_flags |= RTF_STATIC; 1397186119Sqingli arpc.rtm.rtm_index = ifp->if_index; 1398186119Sqingli error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 1399186119Sqingli if (error) 1400186119Sqingli break; 1401186119Sqingli } 1402186119Sqingli } 1403186119Sqingli return error; 1404186119Sqingli#undef SIN 1405186119Sqingli} 1406186119Sqingli 1407186119Sqinglivoid * 1408186119Sqingliin_domifattach(struct ifnet *ifp) 1409189592Sbms{ 1410189592Sbms struct in_ifinfo *ii; 1411189592Sbms struct lltable *llt; 1412189592Sbms 1413189592Sbms ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1414189592Sbms 1415189592Sbms llt = lltable_init(ifp, AF_INET); 1416186119Sqingli if (llt != NULL) { 1417186119Sqingli llt->llt_new = in_lltable_new; 1418186119Sqingli llt->llt_free = in_lltable_free; 1419186119Sqingli llt->llt_rtcheck = in_lltable_rtcheck; 1420186119Sqingli llt->llt_lookup = in_lltable_lookup; 1421186119Sqingli llt->llt_dump = in_lltable_dump; 1422186119Sqingli } 1423189592Sbms ii->ii_llt = llt; 1424189592Sbms 1425189592Sbms ii->ii_igmp = igmp_domifattach(ifp); 1426189592Sbms 1427189592Sbms return ii; 1428186119Sqingli} 1429186119Sqingli 1430186119Sqinglivoid 1431189592Sbmsin_domifdetach(struct ifnet *ifp, void *aux) 1432186119Sqingli{ 1433189592Sbms struct in_ifinfo *ii = (struct in_ifinfo *)aux; 1434186119Sqingli 1435189592Sbms igmp_domifdetach(ifp); 1436189592Sbms lltable_free(ii->ii_llt); 1437189592Sbms free(ii, M_IFADDR); 1438186119Sqingli} 1439