1 /* $NetBSD: in.c,v 1.125 2008/04/28 20:24:09 martin Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1998 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 59 * POSSIBILITY OF SUCH DAMAGE. 60 */ 61 62 /* 63 * Copyright (c) 1982, 1986, 1991, 1993 64 * The Regents of the University of California. All rights reserved. 65 * 66 * Redistribution and use in source and binary forms, with or without 67 * modification, are permitted provided that the following conditions 68 * are met: 69 * 1. Redistributions of source code must retain the above copyright 70 * notice, this list of conditions and the following disclaimer. 71 * 2. Redistributions in binary form must reproduce the above copyright 72 * notice, this list of conditions and the following disclaimer in the 73 * documentation and/or other materials provided with the distribution. 74 * 3. Neither the name of the University nor the names of its contributors 75 * may be used to endorse or promote products derived from this software 76 * without specific prior written permission. 77 * 78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 88 * SUCH DAMAGE. 89 * 90 * @(#)in.c 8.4 (Berkeley) 1/9/95 91 */ 92 93 #include <sys/cdefs.h> 94 __KERNEL_RCSID(0, "$NetBSD: in.c,v 1.125 2008/04/28 20:24:09 martin Exp $"); 95 96 #include "opt_inet.h" 97 #include "opt_inet_conf.h" 98 #include "opt_mrouting.h" 99 #include "opt_pfil_hooks.h" 100 101 #include <sys/param.h> 102 #include <sys/ioctl.h> 103 #include <sys/errno.h> 104 #include <sys/malloc.h> 105 #include <sys/socket.h> 106 #include <sys/socketvar.h> 107 #include <sys/sysctl.h> 108 #include <sys/systm.h> 109 #include <sys/proc.h> 110 #include <sys/syslog.h> 111 #include <sys/kauth.h> 112 113 #include <net/if.h> 114 #include <net/route.h> 115 116 #include <net/if_ether.h> 117 118 #include <netinet/in_systm.h> 119 #include <netinet/in.h> 120 #include <netinet/in_var.h> 121 #include <netinet/ip.h> 122 #include <netinet/ip_var.h> 123 #include <netinet/in_ifattach.h> 124 #include <netinet/in_pcb.h> 125 #include <netinet/if_inarp.h> 126 #include <netinet/ip_mroute.h> 127 #include <netinet/igmp_var.h> 128 129 #ifdef IPSELSRC 130 #include <netinet/in_selsrc.h> 131 #endif 132 133 #ifdef PFIL_HOOKS 134 #include <net/pfil.h> 135 #endif 136 137 #ifdef INET 138 static u_int in_mask2len(struct in_addr *); 139 static void in_len2mask(struct in_addr *, u_int); 140 static int in_lifaddr_ioctl(struct socket *, u_long, void *, 141 struct ifnet *, struct lwp *); 142 143 static int in_ifaddrpref_ioctl(struct socket *, u_long, void *, 144 struct ifnet *); 145 static int in_addprefix(struct in_ifaddr *, int); 146 static int in_scrubprefix(struct in_ifaddr *); 147 148 #ifndef SUBNETSARELOCAL 149 #define SUBNETSARELOCAL 1 150 #endif 151 152 #ifndef HOSTZEROBROADCAST 153 #define HOSTZEROBROADCAST 1 154 #endif 155 156 int subnetsarelocal = SUBNETSARELOCAL; 157 int hostzeroisbroadcast = HOSTZEROBROADCAST; 158 159 /* 160 * This list is used to keep track of in_multi chains which belong to 161 * deleted interface addresses. We use in_ifaddr so that a chain head 162 * won't be deallocated until all multicast address record are deleted. 163 */ 164 static TAILQ_HEAD(, in_ifaddr) in_mk = TAILQ_HEAD_INITIALIZER(in_mk); 165 166 /* 167 * Return 1 if an internet address is for a ``local'' host 168 * (one to which we have a connection). If subnetsarelocal 169 * is true, this includes other subnets of the local net. 170 * Otherwise, it includes only the directly-connected (sub)nets. 171 */ 172 int 173 in_localaddr(struct in_addr in) 174 { 175 struct in_ifaddr *ia; 176 177 if (subnetsarelocal) { 178 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) 179 if ((in.s_addr & ia->ia_netmask) == ia->ia_net) 180 return (1); 181 } else { 182 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) 183 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet) 184 return (1); 185 } 186 return (0); 187 } 188 189 /* 190 * Determine whether an IP address is in a reserved set of addresses 191 * that may not be forwarded, or whether datagrams to that destination 192 * may be forwarded. 193 */ 194 int 195 in_canforward(struct in_addr in) 196 { 197 u_int32_t net; 198 199 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr)) 200 return (0); 201 if (IN_CLASSA(in.s_addr)) { 202 net = in.s_addr & IN_CLASSA_NET; 203 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 204 return (0); 205 } 206 return (1); 207 } 208 209 /* 210 * Trim a mask in a sockaddr 211 */ 212 void 213 in_socktrim(struct sockaddr_in *ap) 214 { 215 char *cplim = (char *) &ap->sin_addr; 216 char *cp = (char *) (&ap->sin_addr + 1); 217 218 ap->sin_len = 0; 219 while (--cp >= cplim) 220 if (*cp) { 221 (ap)->sin_len = cp - (char *) (ap) + 1; 222 break; 223 } 224 } 225 226 /* 227 * Routine to take an Internet address and convert into a 228 * "dotted quad" representation for printing. 229 */ 230 const char * 231 in_fmtaddr(struct in_addr addr) 232 { 233 static char buf[sizeof("123.456.789.123")]; 234 235 addr.s_addr = ntohl(addr.s_addr); 236 237 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", 238 (addr.s_addr >> 24) & 0xFF, 239 (addr.s_addr >> 16) & 0xFF, 240 (addr.s_addr >> 8) & 0xFF, 241 (addr.s_addr >> 0) & 0xFF); 242 return buf; 243 } 244 245 /* 246 * Maintain the "in_maxmtu" variable, which is the largest 247 * mtu for non-local interfaces with AF_INET addresses assigned 248 * to them that are up. 249 */ 250 unsigned long in_maxmtu; 251 252 void 253 in_setmaxmtu(void) 254 { 255 struct in_ifaddr *ia; 256 struct ifnet *ifp; 257 unsigned long maxmtu = 0; 258 259 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 260 if ((ifp = ia->ia_ifp) == 0) 261 continue; 262 if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP) 263 continue; 264 if (ifp->if_mtu > maxmtu) 265 maxmtu = ifp->if_mtu; 266 } 267 if (maxmtu) 268 in_maxmtu = maxmtu; 269 } 270 271 static u_int 272 in_mask2len(struct in_addr *mask) 273 { 274 u_int x, y; 275 u_char *p; 276 277 p = (u_char *)mask; 278 for (x = 0; x < sizeof(*mask); x++) { 279 if (p[x] != 0xff) 280 break; 281 } 282 y = 0; 283 if (x < sizeof(*mask)) { 284 for (y = 0; y < NBBY; y++) { 285 if ((p[x] & (0x80 >> y)) == 0) 286 break; 287 } 288 } 289 return x * NBBY + y; 290 } 291 292 static void 293 in_len2mask(struct in_addr *mask, u_int len) 294 { 295 u_int i; 296 u_char *p; 297 298 p = (u_char *)mask; 299 bzero(mask, sizeof(*mask)); 300 for (i = 0; i < len / NBBY; i++) 301 p[i] = 0xff; 302 if (len % NBBY) 303 p[i] = (0xff00 >> (len % NBBY)) & 0xff; 304 } 305 306 /* 307 * Generic internet control operations (ioctl's). 308 * Ifp is 0 if not an interface-specific ioctl. 309 */ 310 /* ARGSUSED */ 311 int 312 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp, 313 struct lwp *l) 314 { 315 struct ifreq *ifr = (struct ifreq *)data; 316 struct in_ifaddr *ia = 0; 317 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 318 struct sockaddr_in oldaddr; 319 int error, hostIsNew, maskIsNew; 320 int newifaddr = 0; 321 322 switch (cmd) { 323 case SIOCALIFADDR: 324 case SIOCDLIFADDR: 325 case SIOCSIFADDRPREF: 326 if (l == NULL) 327 return (EPERM); 328 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE, 329 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 330 NULL) != 0) 331 return (EPERM); 332 /*FALLTHROUGH*/ 333 case SIOCGIFADDRPREF: 334 case SIOCGLIFADDR: 335 if (ifp == NULL) 336 return EINVAL; 337 if (cmd == SIOCGIFADDRPREF || cmd == SIOCSIFADDRPREF) 338 return in_ifaddrpref_ioctl(so, cmd, data, ifp); 339 else 340 return in_lifaddr_ioctl(so, cmd, data, ifp, l); 341 } 342 343 /* 344 * Find address for this interface, if it exists. 345 */ 346 if (ifp != NULL) 347 IFP_TO_IA(ifp, ia); 348 349 switch (cmd) { 350 351 case SIOCAIFADDR: 352 case SIOCDIFADDR: 353 case SIOCGIFALIAS: 354 if (ifra->ifra_addr.sin_family == AF_INET) 355 LIST_FOREACH(ia, 356 &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr), 357 ia_hash) { 358 if (ia->ia_ifp == ifp && 359 in_hosteq(ia->ia_addr.sin_addr, 360 ifra->ifra_addr.sin_addr)) 361 break; 362 } 363 if ((cmd == SIOCDIFADDR || cmd == SIOCGIFALIAS) && ia == NULL) 364 return (EADDRNOTAVAIL); 365 366 #if 1 /*def COMPAT_43*/ 367 if (cmd == SIOCDIFADDR && 368 ifra->ifra_addr.sin_family == AF_UNSPEC) { 369 ifra->ifra_addr.sin_family = AF_INET; 370 } 371 #endif 372 /* FALLTHROUGH */ 373 case SIOCSIFADDR: 374 case SIOCSIFDSTADDR: 375 if (ifra->ifra_addr.sin_family != AF_INET) 376 return (EAFNOSUPPORT); 377 /* FALLTHROUGH */ 378 case SIOCSIFNETMASK: 379 if (ifp == NULL) 380 panic("in_control"); 381 382 if (cmd == SIOCGIFALIAS) 383 break; 384 385 if (ia == NULL && 386 (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR)) 387 return (EADDRNOTAVAIL); 388 389 if (l == NULL) 390 return (EPERM); 391 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE, 392 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 393 NULL) != 0) 394 return (EPERM); 395 396 if (ia == 0) { 397 MALLOC(ia, struct in_ifaddr *, sizeof(*ia), 398 M_IFADDR, M_WAITOK); 399 if (ia == 0) 400 return (ENOBUFS); 401 bzero((void *)ia, sizeof *ia); 402 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list); 403 IFAREF(&ia->ia_ifa); 404 ifa_insert(ifp, &ia->ia_ifa); 405 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 406 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 407 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask); 408 #ifdef IPSELSRC 409 ia->ia_ifa.ifa_getifa = in_getifa; 410 #else /* IPSELSRC */ 411 ia->ia_ifa.ifa_getifa = NULL; 412 #endif /* IPSELSRC */ 413 ia->ia_sockmask.sin_len = 8; 414 if (ifp->if_flags & IFF_BROADCAST) { 415 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 416 ia->ia_broadaddr.sin_family = AF_INET; 417 } 418 ia->ia_ifp = ifp; 419 ia->ia_idsalt = arc4random() % 65535; 420 LIST_INIT(&ia->ia_multiaddrs); 421 newifaddr = 1; 422 } 423 break; 424 425 case SIOCSIFBRDADDR: 426 if (l == NULL) 427 return (EPERM); 428 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE, 429 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 430 NULL) != 0) 431 return (EPERM); 432 /* FALLTHROUGH */ 433 434 case SIOCGIFADDR: 435 case SIOCGIFNETMASK: 436 case SIOCGIFDSTADDR: 437 case SIOCGIFBRDADDR: 438 if (ia == 0) 439 return (EADDRNOTAVAIL); 440 break; 441 } 442 error = 0; 443 switch (cmd) { 444 445 case SIOCGIFADDR: 446 ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr)); 447 break; 448 449 case SIOCGIFBRDADDR: 450 if ((ifp->if_flags & IFF_BROADCAST) == 0) 451 return (EINVAL); 452 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr)); 453 break; 454 455 case SIOCGIFDSTADDR: 456 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 457 return (EINVAL); 458 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr)); 459 break; 460 461 case SIOCGIFNETMASK: 462 ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_sockmask)); 463 break; 464 465 case SIOCSIFDSTADDR: 466 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 467 return (EINVAL); 468 oldaddr = ia->ia_dstaddr; 469 ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr)); 470 if (ifp->if_ioctl != NULL && 471 (error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, 472 (void *)ia)) != 0) { 473 ia->ia_dstaddr = oldaddr; 474 return error; 475 } 476 if (ia->ia_flags & IFA_ROUTE) { 477 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr); 478 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST); 479 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr); 480 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP); 481 } 482 break; 483 484 case SIOCSIFBRDADDR: 485 if ((ifp->if_flags & IFF_BROADCAST) == 0) 486 return EINVAL; 487 ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr)); 488 break; 489 490 case SIOCSIFADDR: 491 error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)), 492 1); 493 #ifdef PFIL_HOOKS 494 if (error == 0) 495 (void)pfil_run_hooks(&if_pfil, 496 (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR); 497 #endif 498 break; 499 500 case SIOCSIFNETMASK: 501 in_ifscrub(ifp, ia); 502 ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr)); 503 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 504 error = in_ifinit(ifp, ia, NULL, 0); 505 break; 506 507 case SIOCAIFADDR: 508 maskIsNew = 0; 509 hostIsNew = 1; 510 if (ia->ia_addr.sin_family != AF_INET) 511 ; 512 else if (ifra->ifra_addr.sin_len == 0) { 513 ifra->ifra_addr = ia->ia_addr; 514 hostIsNew = 0; 515 } else if (in_hosteq(ia->ia_addr.sin_addr, 516 ifra->ifra_addr.sin_addr)) 517 hostIsNew = 0; 518 if (ifra->ifra_mask.sin_len) { 519 in_ifscrub(ifp, ia); 520 ia->ia_sockmask = ifra->ifra_mask; 521 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr; 522 maskIsNew = 1; 523 } 524 if ((ifp->if_flags & IFF_POINTOPOINT) && 525 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 526 in_ifscrub(ifp, ia); 527 ia->ia_dstaddr = ifra->ifra_dstaddr; 528 maskIsNew = 1; /* We lie; but the effect's the same */ 529 } 530 if (ifra->ifra_addr.sin_family == AF_INET && 531 (hostIsNew || maskIsNew)) { 532 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 533 } 534 if ((ifp->if_flags & IFF_BROADCAST) && 535 (ifra->ifra_broadaddr.sin_family == AF_INET)) 536 ia->ia_broadaddr = ifra->ifra_broadaddr; 537 #ifdef PFIL_HOOKS 538 if (error == 0) 539 (void)pfil_run_hooks(&if_pfil, 540 (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR); 541 #endif 542 break; 543 544 case SIOCGIFALIAS: 545 ifra->ifra_mask = ia->ia_sockmask; 546 if ((ifp->if_flags & IFF_POINTOPOINT) && 547 (ia->ia_dstaddr.sin_family == AF_INET)) 548 ifra->ifra_dstaddr = ia->ia_dstaddr; 549 else if ((ifp->if_flags & IFF_BROADCAST) && 550 (ia->ia_broadaddr.sin_family == AF_INET)) 551 ifra->ifra_broadaddr = ia->ia_broadaddr; 552 else 553 memset(&ifra->ifra_broadaddr, 0, 554 sizeof(ifra->ifra_broadaddr)); 555 break; 556 557 case SIOCDIFADDR: 558 in_purgeaddr(&ia->ia_ifa); 559 #ifdef PFIL_HOOKS 560 (void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCDIFADDR, 561 ifp, PFIL_IFADDR); 562 #endif 563 break; 564 565 #ifdef MROUTING 566 case SIOCGETVIFCNT: 567 case SIOCGETSGCNT: 568 error = mrt_ioctl(so, cmd, data); 569 break; 570 #endif /* MROUTING */ 571 572 default: 573 if (ifp == NULL || ifp->if_ioctl == NULL) 574 return EOPNOTSUPP; 575 error = (*ifp->if_ioctl)(ifp, cmd, data); 576 in_setmaxmtu(); 577 break; 578 } 579 580 if (error != 0 && newifaddr) { 581 KASSERT(ia != NULL); 582 in_purgeaddr(&ia->ia_ifa); 583 } 584 585 return error; 586 } 587 588 void 589 in_purgeaddr(struct ifaddr *ifa) 590 { 591 struct ifnet *ifp = ifa->ifa_ifp; 592 struct in_ifaddr *ia = (void *) ifa; 593 594 in_ifscrub(ifp, ia); 595 LIST_REMOVE(ia, ia_hash); 596 ifa_remove(ifp, &ia->ia_ifa); 597 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list); 598 if (ia->ia_allhosts != NULL) 599 in_delmulti(ia->ia_allhosts); 600 IFAFREE(&ia->ia_ifa); 601 in_setmaxmtu(); 602 } 603 604 void 605 in_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */ 606 { 607 if_purgeaddrs(ifp, AF_INET, in_purgeaddr); 608 igmp_purgeif(ifp); /* manipulates pools */ 609 #ifdef MROUTING 610 ip_mrouter_detach(ifp); 611 #endif 612 } 613 614 /* 615 * SIOC[GAD]LIFADDR. 616 * SIOCGLIFADDR: get first address. (???) 617 * SIOCGLIFADDR with IFLR_PREFIX: 618 * get first address that matches the specified prefix. 619 * SIOCALIFADDR: add the specified address. 620 * SIOCALIFADDR with IFLR_PREFIX: 621 * EINVAL since we can't deduce hostid part of the address. 622 * SIOCDLIFADDR: delete the specified address. 623 * SIOCDLIFADDR with IFLR_PREFIX: 624 * delete the first address that matches the specified prefix. 625 * return values: 626 * EINVAL on invalid parameters 627 * EADDRNOTAVAIL on prefix match failed/specified address not found 628 * other values may be returned from in_ioctl() 629 */ 630 static int 631 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data, 632 struct ifnet *ifp, struct lwp *l) 633 { 634 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 635 struct ifaddr *ifa; 636 struct sockaddr *sa; 637 638 /* sanity checks */ 639 if (data == NULL || ifp == NULL) { 640 panic("invalid argument to in_lifaddr_ioctl"); 641 /*NOTRECHED*/ 642 } 643 644 switch (cmd) { 645 case SIOCGLIFADDR: 646 /* address must be specified on GET with IFLR_PREFIX */ 647 if ((iflr->flags & IFLR_PREFIX) == 0) 648 break; 649 /*FALLTHROUGH*/ 650 case SIOCALIFADDR: 651 case SIOCDLIFADDR: 652 /* address must be specified on ADD and DELETE */ 653 sa = (struct sockaddr *)&iflr->addr; 654 if (sa->sa_family != AF_INET) 655 return EINVAL; 656 if (sa->sa_len != sizeof(struct sockaddr_in)) 657 return EINVAL; 658 /* XXX need improvement */ 659 sa = (struct sockaddr *)&iflr->dstaddr; 660 if (sa->sa_family 661 && sa->sa_family != AF_INET) 662 return EINVAL; 663 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in)) 664 return EINVAL; 665 break; 666 default: /*shouldn't happen*/ 667 #if 0 668 panic("invalid cmd to in_lifaddr_ioctl"); 669 /*NOTREACHED*/ 670 #else 671 return EOPNOTSUPP; 672 #endif 673 } 674 if (sizeof(struct in_addr) * NBBY < iflr->prefixlen) 675 return EINVAL; 676 677 switch (cmd) { 678 case SIOCALIFADDR: 679 { 680 struct in_aliasreq ifra; 681 682 if (iflr->flags & IFLR_PREFIX) 683 return EINVAL; 684 685 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */ 686 bzero(&ifra, sizeof(ifra)); 687 bcopy(iflr->iflr_name, ifra.ifra_name, 688 sizeof(ifra.ifra_name)); 689 690 bcopy(&iflr->addr, &ifra.ifra_addr, 691 ((struct sockaddr *)&iflr->addr)->sa_len); 692 693 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/ 694 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 695 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 696 } 697 698 ifra.ifra_mask.sin_family = AF_INET; 699 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 700 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 701 702 return in_control(so, SIOCAIFADDR, (void *)&ifra, ifp, l); 703 } 704 case SIOCGLIFADDR: 705 case SIOCDLIFADDR: 706 { 707 struct in_ifaddr *ia; 708 struct in_addr mask, candidate, match; 709 struct sockaddr_in *sin; 710 int cmp; 711 712 bzero(&mask, sizeof(mask)); 713 bzero(&match, sizeof(match)); /* XXX gcc */ 714 if (iflr->flags & IFLR_PREFIX) { 715 /* lookup a prefix rather than address. */ 716 in_len2mask(&mask, iflr->prefixlen); 717 718 sin = (struct sockaddr_in *)&iflr->addr; 719 match.s_addr = sin->sin_addr.s_addr; 720 match.s_addr &= mask.s_addr; 721 722 /* if you set extra bits, that's wrong */ 723 if (match.s_addr != sin->sin_addr.s_addr) 724 return EINVAL; 725 726 cmp = 1; 727 } else { 728 if (cmd == SIOCGLIFADDR) { 729 /* on getting an address, take the 1st match */ 730 cmp = 0; /*XXX*/ 731 } else { 732 /* on deleting an address, do exact match */ 733 in_len2mask(&mask, 32); 734 sin = (struct sockaddr_in *)&iflr->addr; 735 match.s_addr = sin->sin_addr.s_addr; 736 737 cmp = 1; 738 } 739 } 740 741 IFADDR_FOREACH(ifa, ifp) { 742 if (ifa->ifa_addr->sa_family != AF_INET) 743 continue; 744 if (cmp == 0) 745 break; 746 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 747 candidate.s_addr &= mask.s_addr; 748 if (candidate.s_addr == match.s_addr) 749 break; 750 } 751 if (ifa == NULL) 752 return EADDRNOTAVAIL; 753 ia = (struct in_ifaddr *)ifa; 754 755 if (cmd == SIOCGLIFADDR) { 756 /* fill in the if_laddrreq structure */ 757 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 758 759 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 760 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 761 ia->ia_dstaddr.sin_len); 762 } else 763 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 764 765 iflr->prefixlen = 766 in_mask2len(&ia->ia_sockmask.sin_addr); 767 768 iflr->flags = 0; /*XXX*/ 769 770 return 0; 771 } else { 772 struct in_aliasreq ifra; 773 774 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */ 775 bzero(&ifra, sizeof(ifra)); 776 bcopy(iflr->iflr_name, ifra.ifra_name, 777 sizeof(ifra.ifra_name)); 778 779 bcopy(&ia->ia_addr, &ifra.ifra_addr, 780 ia->ia_addr.sin_len); 781 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 782 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 783 ia->ia_dstaddr.sin_len); 784 } 785 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 786 ia->ia_sockmask.sin_len); 787 788 return in_control(so, SIOCDIFADDR, (void *)&ifra, 789 ifp, l); 790 } 791 } 792 } 793 794 return EOPNOTSUPP; /*just for safety*/ 795 } 796 797 static int 798 in_ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, 799 struct ifnet *ifp) 800 { 801 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data; 802 struct ifaddr *ifa; 803 struct sockaddr *sa; 804 struct in_ifaddr *ia = NULL; /* appease gcc -Wuninitialized */ 805 struct in_addr match; 806 struct sockaddr_in *sin; 807 808 /* sanity checks */ 809 if (data == NULL || ifp == NULL) { 810 panic("invalid argument to %s", __func__); 811 /*NOTREACHED*/ 812 } 813 814 /* address must be specified on ADD and DELETE */ 815 sa = (struct sockaddr *)&ifap->ifap_addr; 816 if (sa->sa_family != AF_INET) 817 return EINVAL; 818 if (sa->sa_len != sizeof(struct sockaddr_in)) 819 return EINVAL; 820 821 switch (cmd) { 822 case SIOCSIFADDRPREF: 823 case SIOCGIFADDRPREF: 824 break; 825 default: 826 return EOPNOTSUPP; 827 } 828 829 sin = (struct sockaddr_in *)&ifap->ifap_addr; 830 match.s_addr = sin->sin_addr.s_addr; 831 832 IFADDR_FOREACH(ifa, ifp) { 833 ia = (struct in_ifaddr *)ifa; 834 if (ia->ia_addr.sin_family != AF_INET) 835 continue; 836 if (ia->ia_addr.sin_addr.s_addr == match.s_addr) 837 break; 838 } 839 if (ifa == NULL) 840 return EADDRNOTAVAIL; 841 842 switch (cmd) { 843 case SIOCSIFADDRPREF: 844 ifa->ifa_preference = ifap->ifap_preference; 845 return 0; 846 case SIOCGIFADDRPREF: 847 /* fill in the if_laddrreq structure */ 848 (void)memcpy(&ifap->ifap_addr, &ia->ia_addr, 849 ia->ia_addr.sin_len); 850 ifap->ifap_preference = ifa->ifa_preference; 851 return 0; 852 default: 853 return EOPNOTSUPP; 854 } 855 } 856 857 /* 858 * Delete any existing route for an interface. 859 */ 860 void 861 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 862 { 863 864 in_scrubprefix(ia); 865 } 866 867 /* 868 * Initialize an interface's internet address 869 * and routing table entry. 870 */ 871 int 872 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, 873 const struct sockaddr_in *sin, int scrub) 874 { 875 u_int32_t i; 876 struct sockaddr_in oldaddr; 877 int s = splnet(), flags = RTF_UP, error; 878 879 if (sin == NULL) 880 sin = &ia->ia_addr; 881 882 /* 883 * Set up new addresses. 884 */ 885 oldaddr = ia->ia_addr; 886 if (ia->ia_addr.sin_family == AF_INET) 887 LIST_REMOVE(ia, ia_hash); 888 ia->ia_addr = *sin; 889 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 890 891 /* 892 * Give the interface a chance to initialize 893 * if this is its first address, 894 * and to validate the address if necessary. 895 */ 896 if (ifp->if_ioctl && 897 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (void *)ia))) 898 goto bad; 899 splx(s); 900 if (scrub) { 901 ia->ia_ifa.ifa_addr = sintosa(&oldaddr); 902 in_ifscrub(ifp, ia); 903 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr); 904 } 905 906 i = ia->ia_addr.sin_addr.s_addr; 907 if (IN_CLASSA(i)) 908 ia->ia_netmask = IN_CLASSA_NET; 909 else if (IN_CLASSB(i)) 910 ia->ia_netmask = IN_CLASSB_NET; 911 else 912 ia->ia_netmask = IN_CLASSC_NET; 913 /* 914 * The subnet mask usually includes at least the standard network part, 915 * but may may be smaller in the case of supernetting. 916 * If it is set, we believe it. 917 */ 918 if (ia->ia_subnetmask == 0) { 919 ia->ia_subnetmask = ia->ia_netmask; 920 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask; 921 } else 922 ia->ia_netmask &= ia->ia_subnetmask; 923 924 ia->ia_net = i & ia->ia_netmask; 925 ia->ia_subnet = i & ia->ia_subnetmask; 926 in_socktrim(&ia->ia_sockmask); 927 /* re-calculate the "in_maxmtu" value */ 928 in_setmaxmtu(); 929 /* 930 * Add route for the network. 931 */ 932 ia->ia_ifa.ifa_metric = ifp->if_metric; 933 if (ifp->if_flags & IFF_BROADCAST) { 934 ia->ia_broadaddr.sin_addr.s_addr = 935 ia->ia_subnet | ~ia->ia_subnetmask; 936 ia->ia_netbroadcast.s_addr = 937 ia->ia_net | ~ia->ia_netmask; 938 } else if (ifp->if_flags & IFF_LOOPBACK) { 939 ia->ia_dstaddr = ia->ia_addr; 940 flags |= RTF_HOST; 941 } else if (ifp->if_flags & IFF_POINTOPOINT) { 942 if (ia->ia_dstaddr.sin_family != AF_INET) 943 return (0); 944 flags |= RTF_HOST; 945 } 946 error = in_addprefix(ia, flags); 947 /* 948 * If the interface supports multicast, join the "all hosts" 949 * multicast group on that interface. 950 */ 951 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) { 952 struct in_addr addr; 953 954 addr.s_addr = INADDR_ALLHOSTS_GROUP; 955 ia->ia_allhosts = in_addmulti(&addr, ifp); 956 } 957 return (error); 958 bad: 959 splx(s); 960 LIST_REMOVE(ia, ia_hash); 961 ia->ia_addr = oldaddr; 962 if (ia->ia_addr.sin_family == AF_INET) 963 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), 964 ia, ia_hash); 965 return (error); 966 } 967 968 #define rtinitflags(x) \ 969 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 970 ? RTF_HOST : 0) 971 972 /* 973 * add a route to prefix ("connected route" in cisco terminology). 974 * does nothing if there's some interface address with the same prefix already. 975 */ 976 static int 977 in_addprefix(struct in_ifaddr *target, int flags) 978 { 979 struct in_ifaddr *ia; 980 struct in_addr prefix, mask, p; 981 int error; 982 983 if ((flags & RTF_HOST) != 0) 984 prefix = target->ia_dstaddr.sin_addr; 985 else { 986 prefix = target->ia_addr.sin_addr; 987 mask = target->ia_sockmask.sin_addr; 988 prefix.s_addr &= mask.s_addr; 989 } 990 991 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 992 if (rtinitflags(ia)) 993 p = ia->ia_dstaddr.sin_addr; 994 else { 995 p = ia->ia_addr.sin_addr; 996 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 997 } 998 999 if (prefix.s_addr != p.s_addr) 1000 continue; 1001 1002 /* 1003 * if we got a matching prefix route inserted by other 1004 * interface address, we don't need to bother 1005 * 1006 * XXX RADIX_MPATH implications here? -dyoung 1007 */ 1008 if (ia->ia_flags & IFA_ROUTE) 1009 return 0; 1010 } 1011 1012 /* 1013 * noone seem to have prefix route. insert it. 1014 */ 1015 error = rtinit(&target->ia_ifa, RTM_ADD, flags); 1016 if (error == 0) 1017 target->ia_flags |= IFA_ROUTE; 1018 return error; 1019 } 1020 1021 /* 1022 * remove a route to prefix ("connected route" in cisco terminology). 1023 * re-installs the route by using another interface address, if there's one 1024 * with the same prefix (otherwise we lose the route mistakenly). 1025 */ 1026 static int 1027 in_scrubprefix(struct in_ifaddr *target) 1028 { 1029 struct in_ifaddr *ia; 1030 struct in_addr prefix, mask, p; 1031 int error; 1032 1033 if ((target->ia_flags & IFA_ROUTE) == 0) 1034 return 0; 1035 1036 if (rtinitflags(target)) 1037 prefix = target->ia_dstaddr.sin_addr; 1038 else { 1039 prefix = target->ia_addr.sin_addr; 1040 mask = target->ia_sockmask.sin_addr; 1041 prefix.s_addr &= mask.s_addr; 1042 } 1043 1044 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 1045 if (rtinitflags(ia)) 1046 p = ia->ia_dstaddr.sin_addr; 1047 else { 1048 p = ia->ia_addr.sin_addr; 1049 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 1050 } 1051 1052 if (prefix.s_addr != p.s_addr) 1053 continue; 1054 1055 /* 1056 * if we got a matching prefix route, move IFA_ROUTE to him 1057 */ 1058 if ((ia->ia_flags & IFA_ROUTE) == 0) { 1059 rtinit(&target->ia_ifa, RTM_DELETE, 1060 rtinitflags(target)); 1061 target->ia_flags &= ~IFA_ROUTE; 1062 1063 error = rtinit(&ia->ia_ifa, RTM_ADD, 1064 rtinitflags(ia) | RTF_UP); 1065 if (error == 0) 1066 ia->ia_flags |= IFA_ROUTE; 1067 return error; 1068 } 1069 } 1070 1071 /* 1072 * noone seem to have prefix route. remove it. 1073 */ 1074 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target)); 1075 target->ia_flags &= ~IFA_ROUTE; 1076 return 0; 1077 } 1078 1079 #undef rtinitflags 1080 1081 /* 1082 * Return 1 if the address might be a local broadcast address. 1083 */ 1084 int 1085 in_broadcast(struct in_addr in, struct ifnet *ifp) 1086 { 1087 struct ifaddr *ifa; 1088 1089 if (in.s_addr == INADDR_BROADCAST || 1090 in_nullhost(in)) 1091 return 1; 1092 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1093 return 0; 1094 /* 1095 * Look through the list of addresses for a match 1096 * with a broadcast address. 1097 */ 1098 #define ia (ifatoia(ifa)) 1099 IFADDR_FOREACH(ifa, ifp) 1100 if (ifa->ifa_addr->sa_family == AF_INET && 1101 !in_hosteq(in, ia->ia_addr.sin_addr) && 1102 (in_hosteq(in, ia->ia_broadaddr.sin_addr) || 1103 in_hosteq(in, ia->ia_netbroadcast) || 1104 (hostzeroisbroadcast && 1105 /* 1106 * Check for old-style (host 0) broadcast. 1107 */ 1108 (in.s_addr == ia->ia_subnet || 1109 in.s_addr == ia->ia_net)))) 1110 return 1; 1111 return (0); 1112 #undef ia 1113 } 1114 1115 /* 1116 * Add an address to the list of IP multicast addresses for a given interface. 1117 */ 1118 struct in_multi * 1119 in_addmulti(struct in_addr *ap, struct ifnet *ifp) 1120 { 1121 struct sockaddr_in sin; 1122 struct in_multi *inm; 1123 struct ifreq ifr; 1124 int s = splsoftnet(); 1125 1126 /* 1127 * See if address already in list. 1128 */ 1129 IN_LOOKUP_MULTI(*ap, ifp, inm); 1130 if (inm != NULL) { 1131 /* 1132 * Found it; just increment the reference count. 1133 */ 1134 ++inm->inm_refcount; 1135 } else { 1136 /* 1137 * New address; allocate a new multicast record 1138 * and link it into the interface's multicast list. 1139 */ 1140 inm = pool_get(&inmulti_pool, PR_NOWAIT); 1141 if (inm == NULL) { 1142 splx(s); 1143 return (NULL); 1144 } 1145 inm->inm_addr = *ap; 1146 inm->inm_ifp = ifp; 1147 inm->inm_refcount = 1; 1148 LIST_INSERT_HEAD( 1149 &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp), 1150 inm, inm_list); 1151 /* 1152 * Ask the network driver to update its multicast reception 1153 * filter appropriately for the new address. 1154 */ 1155 sockaddr_in_init(&sin, ap, 0); 1156 ifreq_setaddr(SIOCADDMULTI, &ifr, sintosa(&sin)); 1157 if ((ifp->if_ioctl == NULL) || 1158 (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(void *)&ifr) != 0) { 1159 LIST_REMOVE(inm, inm_list); 1160 pool_put(&inmulti_pool, inm); 1161 splx(s); 1162 return (NULL); 1163 } 1164 /* 1165 * Let IGMP know that we have joined a new IP multicast group. 1166 */ 1167 if (igmp_joingroup(inm) != 0) { 1168 LIST_REMOVE(inm, inm_list); 1169 pool_put(&inmulti_pool, inm); 1170 splx(s); 1171 return (NULL); 1172 } 1173 in_multientries++; 1174 } 1175 splx(s); 1176 return (inm); 1177 } 1178 1179 /* 1180 * Delete a multicast address record. 1181 */ 1182 void 1183 in_delmulti(struct in_multi *inm) 1184 { 1185 struct sockaddr_in sin; 1186 struct ifreq ifr; 1187 int s = splsoftnet(); 1188 1189 if (--inm->inm_refcount == 0) { 1190 /* 1191 * No remaining claims to this record; let IGMP know that 1192 * we are leaving the multicast group. 1193 */ 1194 igmp_leavegroup(inm); 1195 /* 1196 * Unlink from list. 1197 */ 1198 LIST_REMOVE(inm, inm_list); 1199 in_multientries--; 1200 /* 1201 * Notify the network driver to update its multicast reception 1202 * filter. 1203 */ 1204 sockaddr_in_init(&sin, &inm->inm_addr, 0); 1205 ifreq_setaddr(SIOCDELMULTI, &ifr, sintosa(&sin)); 1206 (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI, 1207 (void *)&ifr); 1208 pool_put(&inmulti_pool, inm); 1209 } 1210 splx(s); 1211 } 1212 #endif /* INET */ 1213