1 /* 2 * Copyright (c) 1982, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)in.c 8.4 (Berkeley) 1/9/95 34 * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $ 35 * $DragonFly: src/sys/netinet/in.c,v 1.41 2008/08/17 05:20:10 sephe Exp $ 36 */ 37 38 #include "opt_bootp.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/sockio.h> 43 #include <sys/malloc.h> 44 #include <sys/proc.h> 45 #include <sys/priv.h> 46 #include <sys/msgport.h> 47 #include <sys/socket.h> 48 49 #include <sys/kernel.h> 50 #include <sys/sysctl.h> 51 #include <sys/thread2.h> 52 53 #include <net/if.h> 54 #include <net/if_types.h> 55 #include <net/route.h> 56 #include <net/netmsg2.h> 57 58 #include <netinet/in.h> 59 #include <netinet/in_var.h> 60 #include <netinet/in_pcb.h> 61 62 #include <netinet/igmp_var.h> 63 64 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address"); 65 66 static int in_mask2len (struct in_addr *); 67 static void in_len2mask (struct in_addr *, int); 68 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t, 69 struct ifnet *, struct thread *); 70 71 static void in_socktrim (struct sockaddr_in *); 72 static int in_ifinit(struct ifnet *, struct in_ifaddr *, 73 const struct sockaddr_in *, int); 74 75 static void in_control_dispatch(struct netmsg *); 76 static int in_control_internal(u_long, caddr_t, struct ifnet *, 77 struct thread *); 78 79 static int subnetsarelocal = 0; 80 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW, 81 &subnetsarelocal, 0, ""); 82 83 struct in_multihead in_multihead; /* XXX BSS initialization */ 84 85 extern struct inpcbinfo ripcbinfo; 86 extern struct inpcbinfo udbinfo; 87 88 /* 89 * Return 1 if an internet address is for a ``local'' host 90 * (one to which we have a connection). If subnetsarelocal 91 * is true, this includes other subnets of the local net. 92 * Otherwise, it includes only the directly-connected (sub)nets. 93 */ 94 int 95 in_localaddr(struct in_addr in) 96 { 97 u_long i = ntohl(in.s_addr); 98 struct in_ifaddr_container *iac; 99 struct in_ifaddr *ia; 100 101 if (subnetsarelocal) { 102 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) { 103 ia = iac->ia; 104 105 if ((i & ia->ia_netmask) == ia->ia_net) 106 return (1); 107 } 108 } else { 109 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) { 110 ia = iac->ia; 111 112 if ((i & ia->ia_subnetmask) == ia->ia_subnet) 113 return (1); 114 } 115 } 116 return (0); 117 } 118 119 /* 120 * Determine whether an IP address is in a reserved set of addresses 121 * that may not be forwarded, or whether datagrams to that destination 122 * may be forwarded. 123 */ 124 int 125 in_canforward(struct in_addr in) 126 { 127 u_long i = ntohl(in.s_addr); 128 u_long net; 129 130 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i)) 131 return (0); 132 if (IN_CLASSA(i)) { 133 net = i & IN_CLASSA_NET; 134 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 135 return (0); 136 } 137 return (1); 138 } 139 140 /* 141 * Trim a mask in a sockaddr 142 */ 143 static void 144 in_socktrim(struct sockaddr_in *ap) 145 { 146 char *cplim = (char *) &ap->sin_addr; 147 char *cp = (char *) (&ap->sin_addr + 1); 148 149 ap->sin_len = 0; 150 while (--cp >= cplim) 151 if (*cp) { 152 (ap)->sin_len = cp - (char *) (ap) + 1; 153 break; 154 } 155 } 156 157 static int 158 in_mask2len(struct in_addr *mask) 159 { 160 int x, y; 161 u_char *p; 162 163 p = (u_char *)mask; 164 for (x = 0; x < sizeof *mask; x++) { 165 if (p[x] != 0xff) 166 break; 167 } 168 y = 0; 169 if (x < sizeof *mask) { 170 for (y = 0; y < 8; y++) { 171 if ((p[x] & (0x80 >> y)) == 0) 172 break; 173 } 174 } 175 return x * 8 + y; 176 } 177 178 static void 179 in_len2mask(struct in_addr *mask, int len) 180 { 181 int i; 182 u_char *p; 183 184 p = (u_char *)mask; 185 bzero(mask, sizeof *mask); 186 for (i = 0; i < len / 8; i++) 187 p[i] = 0xff; 188 if (len % 8) 189 p[i] = (0xff00 >> (len % 8)) & 0xff; 190 } 191 192 static int in_interfaces; /* number of external internet interfaces */ 193 194 struct in_control_arg { 195 u_long cmd; 196 caddr_t data; 197 struct ifnet *ifp; 198 struct thread *td; 199 }; 200 201 static void 202 in_control_dispatch(struct netmsg *nmsg) 203 { 204 struct lwkt_msg *msg = &nmsg->nm_lmsg; 205 const struct in_control_arg *arg = msg->u.ms_resultp; 206 int error; 207 208 error = in_control_internal(arg->cmd, arg->data, arg->ifp, arg->td); 209 lwkt_replymsg(msg, error); 210 } 211 212 /* 213 * Generic internet control operations (ioctl's). 214 * Ifp is 0 if not an interface-specific ioctl. 215 * 216 * NOTE! td might be NULL. 217 */ 218 /* ARGSUSED */ 219 int 220 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 221 struct thread *td) 222 { 223 struct netmsg nmsg; 224 struct in_control_arg arg; 225 struct lwkt_msg *msg; 226 int error; 227 228 switch (cmd) { 229 case SIOCALIFADDR: 230 case SIOCDLIFADDR: 231 if (td && (error = priv_check(td, PRIV_ROOT)) != 0) 232 return error; 233 /* FALLTHROUGH */ 234 case SIOCGLIFADDR: 235 if (!ifp) 236 return EINVAL; 237 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 238 } 239 240 KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR, 241 ("recursive SIOC%cLIFADDR!\n", 242 cmd == SIOCDLIFADDR ? 'D' : 'A')); 243 244 /* 245 * IFADDR alterations are serialized by netisr0 246 */ 247 switch (cmd) { 248 case SIOCSIFDSTADDR: 249 case SIOCSIFBRDADDR: 250 case SIOCSIFADDR: 251 case SIOCSIFNETMASK: 252 case SIOCAIFADDR: 253 case SIOCDIFADDR: 254 bzero(&arg, sizeof(arg)); 255 arg.cmd = cmd; 256 arg.data = data; 257 arg.ifp = ifp; 258 arg.td = td; 259 260 netmsg_init(&nmsg, &curthread->td_msgport, 0, 261 in_control_dispatch); 262 msg = &nmsg.nm_lmsg; 263 msg->u.ms_resultp = &arg; 264 265 lwkt_domsg(cpu_portfn(0), msg, 0); 266 return msg->ms_error; 267 default: 268 return in_control_internal(cmd, data, ifp, td); 269 } 270 } 271 272 static void 273 in_ialink_dispatch(struct netmsg *nmsg) 274 { 275 struct lwkt_msg *lmsg = &nmsg->nm_lmsg; 276 struct in_ifaddr *ia = lmsg->u.ms_resultp; 277 struct ifaddr_container *ifac; 278 struct in_ifaddr_container *iac; 279 int cpu = mycpuid; 280 281 crit_enter(); 282 283 ifac = &ia->ia_ifa.ifa_containers[cpu]; 284 ASSERT_IFAC_VALID(ifac); 285 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0, 286 ("ia is on in_ifaddrheads\n")); 287 288 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD; 289 iac = &ifac->ifa_proto_u.u_in_ifac; 290 TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link); 291 292 crit_exit(); 293 294 ifa_forwardmsg(lmsg, cpu + 1); 295 } 296 297 static void 298 in_iaunlink_dispatch(struct netmsg *nmsg) 299 { 300 struct lwkt_msg *lmsg = &nmsg->nm_lmsg; 301 struct in_ifaddr *ia = lmsg->u.ms_resultp; 302 struct ifaddr_container *ifac; 303 struct in_ifaddr_container *iac; 304 int cpu = mycpuid; 305 306 crit_enter(); 307 308 ifac = &ia->ia_ifa.ifa_containers[cpu]; 309 ASSERT_IFAC_VALID(ifac); 310 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD, 311 ("ia is not on in_ifaddrheads\n")); 312 313 iac = &ifac->ifa_proto_u.u_in_ifac; 314 TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link); 315 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD; 316 317 crit_exit(); 318 319 ifa_forwardmsg(lmsg, cpu + 1); 320 } 321 322 static void 323 in_iahashins_dispatch(struct netmsg *nmsg) 324 { 325 struct lwkt_msg *lmsg = &nmsg->nm_lmsg; 326 struct in_ifaddr *ia = lmsg->u.ms_resultp; 327 struct ifaddr_container *ifac; 328 struct in_ifaddr_container *iac; 329 int cpu = mycpuid; 330 331 crit_enter(); 332 333 ifac = &ia->ia_ifa.ifa_containers[cpu]; 334 ASSERT_IFAC_VALID(ifac); 335 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0, 336 ("ia is on in_ifaddrhashtbls\n")); 337 338 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH; 339 iac = &ifac->ifa_proto_u.u_in_ifac; 340 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 341 iac, ia_hash); 342 343 crit_exit(); 344 345 ifa_forwardmsg(lmsg, cpu + 1); 346 } 347 348 static void 349 in_iahashrem_dispatch(struct netmsg *nmsg) 350 { 351 struct lwkt_msg *lmsg = &nmsg->nm_lmsg; 352 struct in_ifaddr *ia = lmsg->u.ms_resultp; 353 struct ifaddr_container *ifac; 354 struct in_ifaddr_container *iac; 355 int cpu = mycpuid; 356 357 crit_enter(); 358 359 ifac = &ia->ia_ifa.ifa_containers[cpu]; 360 ASSERT_IFAC_VALID(ifac); 361 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH, 362 ("ia is not on in_ifaddrhashtbls\n")); 363 364 iac = &ifac->ifa_proto_u.u_in_ifac; 365 LIST_REMOVE(iac, ia_hash); 366 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH; 367 368 crit_exit(); 369 370 ifa_forwardmsg(lmsg, cpu + 1); 371 } 372 373 static void 374 in_ialink(struct in_ifaddr *ia) 375 { 376 struct netmsg nmsg; 377 struct lwkt_msg *lmsg; 378 379 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_ialink_dispatch); 380 lmsg = &nmsg.nm_lmsg; 381 lmsg->u.ms_resultp = ia; 382 383 ifa_domsg(lmsg, 0); 384 } 385 386 void 387 in_iaunlink(struct in_ifaddr *ia) 388 { 389 struct netmsg nmsg; 390 struct lwkt_msg *lmsg; 391 392 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iaunlink_dispatch); 393 lmsg = &nmsg.nm_lmsg; 394 lmsg->u.ms_resultp = ia; 395 396 ifa_domsg(lmsg, 0); 397 } 398 399 void 400 in_iahash_insert(struct in_ifaddr *ia) 401 { 402 struct netmsg nmsg; 403 struct lwkt_msg *lmsg; 404 405 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iahashins_dispatch); 406 lmsg = &nmsg.nm_lmsg; 407 lmsg->u.ms_resultp = ia; 408 409 ifa_domsg(lmsg, 0); 410 } 411 412 void 413 in_iahash_remove(struct in_ifaddr *ia) 414 { 415 struct netmsg nmsg; 416 struct lwkt_msg *lmsg; 417 418 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iahashrem_dispatch); 419 lmsg = &nmsg.nm_lmsg; 420 lmsg->u.ms_resultp = ia; 421 422 ifa_domsg(lmsg, 0); 423 } 424 425 static __inline struct in_ifaddr * 426 in_ianext(struct in_ifaddr *oia) 427 { 428 struct ifaddr_container *ifac; 429 struct in_ifaddr_container *iac; 430 431 ifac = &oia->ia_ifa.ifa_containers[mycpuid]; 432 ASSERT_IFAC_VALID(ifac); 433 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD, 434 ("ia is not on in_ifaddrheads\n")); 435 436 iac = &ifac->ifa_proto_u.u_in_ifac; 437 iac = TAILQ_NEXT(iac, ia_link); 438 if (iac != NULL) 439 return iac->ia; 440 else 441 return NULL; 442 } 443 444 static int 445 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp, 446 struct thread *td) 447 { 448 struct ifreq *ifr = (struct ifreq *)data; 449 struct in_ifaddr *ia = NULL; 450 struct in_addr dst; 451 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 452 struct ifaddr_container *ifac; 453 struct in_ifaddr_container *iac; 454 struct sockaddr_in oldaddr; 455 int hostIsNew, iaIsNew, maskIsNew; 456 int error = 0; 457 458 iaIsNew = 0; 459 460 /* 461 * Find address for this interface, if it exists. 462 * 463 * If an alias address was specified, find that one instead of 464 * the first one on the interface, if possible 465 */ 466 if (ifp) { 467 struct in_ifaddr *iap; 468 469 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; 470 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) { 471 iap = iac->ia; 472 if (iap->ia_ifp == ifp && 473 iap->ia_addr.sin_addr.s_addr == dst.s_addr) { 474 ia = iap; 475 break; 476 } 477 } 478 if (ia == NULL) { 479 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], 480 ifa_link) { 481 iap = ifatoia(ifac->ifa); 482 if (iap->ia_addr.sin_family == AF_INET) { 483 ia = iap; 484 break; 485 } 486 } 487 } 488 } 489 490 switch (cmd) { 491 case SIOCAIFADDR: 492 case SIOCDIFADDR: 493 if (ifp == NULL) 494 return (EADDRNOTAVAIL); 495 if (ifra->ifra_addr.sin_family == AF_INET) { 496 while (ia != NULL) { 497 if (ia->ia_ifp == ifp && 498 ia->ia_addr.sin_addr.s_addr == 499 ifra->ifra_addr.sin_addr.s_addr) 500 break; 501 ia = in_ianext(ia); 502 } 503 if ((ifp->if_flags & IFF_POINTOPOINT) && 504 cmd == SIOCAIFADDR && 505 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) { 506 return EDESTADDRREQ; 507 } 508 } 509 if (cmd == SIOCDIFADDR && ia == NULL) 510 return (EADDRNOTAVAIL); 511 /* FALLTHROUGH */ 512 case SIOCSIFADDR: 513 case SIOCSIFNETMASK: 514 case SIOCSIFDSTADDR: 515 if (td && (error = priv_check(td, PRIV_ROOT)) != 0) 516 return error; 517 518 if (ifp == NULL) 519 return (EADDRNOTAVAIL); 520 521 if (cmd == SIOCSIFDSTADDR && 522 (ifp->if_flags & IFF_POINTOPOINT) == 0) 523 return (EINVAL); 524 525 if (ia == NULL) { 526 struct ifaddr *ifa; 527 int i; 528 529 ia = ifa_create(sizeof(*ia), M_WAITOK); 530 ifa = &ia->ia_ifa; 531 532 /* 533 * Setup per-CPU information 534 */ 535 for (i = 0; i < ncpus; ++i) { 536 ifac = &ifa->ifa_containers[i]; 537 iac = &ifac->ifa_proto_u.u_in_ifac; 538 iac->ia = ia; 539 iac->ia_ifac = ifac; 540 } 541 542 /* 543 * Protect from NETISR_IP traversing address list 544 * while we're modifying it. 545 */ 546 crit_enter(); 547 548 in_ialink(ia); 549 ifa_iflink(ifa, ifp, 1); 550 551 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 552 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 553 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 554 ia->ia_sockmask.sin_len = 8; 555 ia->ia_sockmask.sin_family = AF_INET; 556 if (ifp->if_flags & IFF_BROADCAST) { 557 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr; 558 ia->ia_broadaddr.sin_family = AF_INET; 559 } 560 ia->ia_ifp = ifp; 561 if (!(ifp->if_flags & IFF_LOOPBACK)) 562 in_interfaces++; 563 iaIsNew = 1; 564 565 crit_exit(); 566 } 567 break; 568 569 case SIOCSIFBRDADDR: 570 if (td && (error = priv_check(td, PRIV_ROOT)) != 0) 571 return error; 572 /* FALLTHROUGH */ 573 574 case SIOCGIFADDR: 575 case SIOCGIFNETMASK: 576 case SIOCGIFDSTADDR: 577 case SIOCGIFBRDADDR: 578 if (ia == NULL) 579 return (EADDRNOTAVAIL); 580 break; 581 } 582 583 switch (cmd) { 584 case SIOCGIFADDR: 585 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 586 return (0); 587 588 case SIOCGIFBRDADDR: 589 if ((ifp->if_flags & IFF_BROADCAST) == 0) 590 return (EINVAL); 591 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 592 return (0); 593 594 case SIOCGIFDSTADDR: 595 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 596 return (EINVAL); 597 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 598 return (0); 599 600 case SIOCGIFNETMASK: 601 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 602 return (0); 603 604 case SIOCSIFDSTADDR: 605 KKASSERT(ifp->if_flags & IFF_POINTOPOINT); 606 607 oldaddr = ia->ia_dstaddr; 608 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 609 if (ifp->if_ioctl != NULL) { 610 lwkt_serialize_enter(ifp->if_serializer); 611 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia, 612 td->td_proc->p_ucred); 613 lwkt_serialize_exit(ifp->if_serializer); 614 if (error) { 615 ia->ia_dstaddr = oldaddr; 616 return (error); 617 } 618 } 619 if (ia->ia_flags & IFA_ROUTE) { 620 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 621 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST); 622 ia->ia_ifa.ifa_dstaddr = 623 (struct sockaddr *)&ia->ia_dstaddr; 624 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP); 625 } 626 return (0); 627 628 case SIOCSIFBRDADDR: 629 if ((ifp->if_flags & IFF_BROADCAST) == 0) 630 return (EINVAL); 631 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 632 return (0); 633 634 case SIOCSIFADDR: 635 error = in_ifinit(ifp, ia, 636 (const struct sockaddr_in *)&ifr->ifr_addr, 1); 637 if (error != 0 && iaIsNew) 638 break; 639 if (error == 0) 640 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 641 return (0); 642 643 case SIOCSIFNETMASK: 644 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr; 645 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 646 return (0); 647 648 case SIOCAIFADDR: 649 maskIsNew = 0; 650 hostIsNew = 1; 651 error = 0; 652 if (ia->ia_addr.sin_family == AF_INET) { 653 if (ifra->ifra_addr.sin_len == 0) { 654 ifra->ifra_addr = ia->ia_addr; 655 hostIsNew = 0; 656 } else if (ifra->ifra_addr.sin_addr.s_addr == 657 ia->ia_addr.sin_addr.s_addr) { 658 hostIsNew = 0; 659 } 660 } 661 if (ifra->ifra_mask.sin_len) { 662 in_ifscrub(ifp, ia); 663 ia->ia_sockmask = ifra->ifra_mask; 664 ia->ia_sockmask.sin_family = AF_INET; 665 ia->ia_subnetmask = 666 ntohl(ia->ia_sockmask.sin_addr.s_addr); 667 maskIsNew = 1; 668 } 669 if ((ifp->if_flags & IFF_POINTOPOINT) && 670 ifra->ifra_dstaddr.sin_family == AF_INET) { 671 in_ifscrub(ifp, ia); 672 ia->ia_dstaddr = ifra->ifra_dstaddr; 673 maskIsNew = 1; /* We lie; but the effect's the same */ 674 } 675 if (ifra->ifra_addr.sin_family == AF_INET && 676 (hostIsNew || maskIsNew)) 677 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 678 679 if (error != 0 && iaIsNew) 680 break; 681 682 if ((ifp->if_flags & IFF_BROADCAST) && 683 ifra->ifra_broadaddr.sin_family == AF_INET) 684 ia->ia_broadaddr = ifra->ifra_broadaddr; 685 if (error == 0) 686 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 687 return (error); 688 689 case SIOCDIFADDR: 690 /* 691 * in_ifscrub kills the interface route. 692 */ 693 in_ifscrub(ifp, ia); 694 /* 695 * in_ifadown gets rid of all the rest of 696 * the routes. This is not quite the right 697 * thing to do, but at least if we are running 698 * a routing process they will come back. 699 */ 700 in_ifadown(&ia->ia_ifa, 1); 701 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 702 error = 0; 703 break; 704 705 default: 706 if (ifp == NULL || ifp->if_ioctl == NULL) 707 return (EOPNOTSUPP); 708 lwkt_serialize_enter(ifp->if_serializer); 709 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred); 710 lwkt_serialize_exit(ifp->if_serializer); 711 return (error); 712 } 713 714 KKASSERT(cmd == SIOCDIFADDR || 715 ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew)); 716 717 ifa_ifunlink(&ia->ia_ifa, ifp); 718 in_iaunlink(ia); 719 720 if (cmd == SIOCDIFADDR) { 721 ifac = &ia->ia_ifa.ifa_containers[mycpuid]; 722 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) 723 in_iahash_remove(ia); 724 } 725 #ifdef INVARIANTS 726 else { 727 /* 728 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has 729 * already taken care of the deletion from hash table 730 */ 731 ifac = &ia->ia_ifa.ifa_containers[mycpuid]; 732 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0, 733 ("SIOC%cIFADDR failed on new ia, " 734 "but the new ia is still in hash table\n", 735 cmd == SIOCSIFADDR ? 'S' : 'A')); 736 } 737 #endif 738 739 ifa_destroy(&ia->ia_ifa); 740 741 return (error); 742 } 743 744 /* 745 * SIOC[GAD]LIFADDR. 746 * SIOCGLIFADDR: get first address. (?!?) 747 * SIOCGLIFADDR with IFLR_PREFIX: 748 * get first address that matches the specified prefix. 749 * SIOCALIFADDR: add the specified address. 750 * SIOCALIFADDR with IFLR_PREFIX: 751 * EINVAL since we can't deduce hostid part of the address. 752 * SIOCDLIFADDR: delete the specified address. 753 * SIOCDLIFADDR with IFLR_PREFIX: 754 * delete the first address that matches the specified prefix. 755 * return values: 756 * EINVAL on invalid parameters 757 * EADDRNOTAVAIL on prefix match failed/specified address not found 758 * other values may be returned from in_ioctl() 759 * 760 * NOTE! td might be NULL. 761 */ 762 static int 763 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 764 struct thread *td) 765 { 766 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 767 768 /* sanity checks */ 769 if (!data || !ifp) { 770 panic("invalid argument to in_lifaddr_ioctl"); 771 /*NOTRECHED*/ 772 } 773 774 switch (cmd) { 775 case SIOCGLIFADDR: 776 /* address must be specified on GET with IFLR_PREFIX */ 777 if ((iflr->flags & IFLR_PREFIX) == 0) 778 break; 779 /*FALLTHROUGH*/ 780 case SIOCALIFADDR: 781 case SIOCDLIFADDR: 782 /* address must be specified on ADD and DELETE */ 783 if (iflr->addr.ss_family != AF_INET) 784 return EINVAL; 785 if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 786 return EINVAL; 787 /* XXX need improvement */ 788 if (iflr->dstaddr.ss_family 789 && iflr->dstaddr.ss_family != AF_INET) 790 return EINVAL; 791 if (iflr->dstaddr.ss_family 792 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 793 return EINVAL; 794 break; 795 default: /*shouldn't happen*/ 796 return EOPNOTSUPP; 797 } 798 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 799 return EINVAL; 800 801 switch (cmd) { 802 case SIOCALIFADDR: 803 { 804 struct in_aliasreq ifra; 805 806 if (iflr->flags & IFLR_PREFIX) 807 return EINVAL; 808 809 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 810 bzero(&ifra, sizeof ifra); 811 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name); 812 813 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 814 815 if (iflr->dstaddr.ss_family) { /*XXX*/ 816 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 817 iflr->dstaddr.ss_len); 818 } 819 820 ifra.ifra_mask.sin_family = AF_INET; 821 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 822 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 823 824 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td); 825 } 826 case SIOCGLIFADDR: 827 case SIOCDLIFADDR: 828 { 829 struct ifaddr_container *ifac; 830 struct in_ifaddr *ia; 831 struct in_addr mask, candidate, match; 832 struct sockaddr_in *sin; 833 int cmp; 834 835 bzero(&mask, sizeof mask); 836 if (iflr->flags & IFLR_PREFIX) { 837 /* lookup a prefix rather than address. */ 838 in_len2mask(&mask, iflr->prefixlen); 839 840 sin = (struct sockaddr_in *)&iflr->addr; 841 match.s_addr = sin->sin_addr.s_addr; 842 match.s_addr &= mask.s_addr; 843 844 /* if you set extra bits, that's wrong */ 845 if (match.s_addr != sin->sin_addr.s_addr) 846 return EINVAL; 847 848 cmp = 1; 849 } else { 850 if (cmd == SIOCGLIFADDR) { 851 /* on getting an address, take the 1st match */ 852 match.s_addr = 0; /* gcc4 warning */ 853 cmp = 0; /*XXX*/ 854 } else { 855 /* on deleting an address, do exact match */ 856 in_len2mask(&mask, 32); 857 sin = (struct sockaddr_in *)&iflr->addr; 858 match.s_addr = sin->sin_addr.s_addr; 859 860 cmp = 1; 861 } 862 } 863 864 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 865 struct ifaddr *ifa = ifac->ifa; 866 867 if (ifa->ifa_addr->sa_family != AF_INET6) 868 continue; 869 if (!cmp) 870 break; 871 candidate.s_addr = 872 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 873 candidate.s_addr &= mask.s_addr; 874 if (candidate.s_addr == match.s_addr) 875 break; 876 } 877 if (ifac == NULL) 878 return EADDRNOTAVAIL; 879 ia = (struct in_ifaddr *)(ifac->ifa); 880 881 if (cmd == SIOCGLIFADDR) { 882 /* fill in the if_laddrreq structure */ 883 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 884 885 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 886 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 887 ia->ia_dstaddr.sin_len); 888 } else 889 bzero(&iflr->dstaddr, sizeof iflr->dstaddr); 890 891 iflr->prefixlen = 892 in_mask2len(&ia->ia_sockmask.sin_addr); 893 894 iflr->flags = 0; /*XXX*/ 895 896 return 0; 897 } else { 898 struct in_aliasreq ifra; 899 900 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 901 bzero(&ifra, sizeof ifra); 902 bcopy(iflr->iflr_name, ifra.ifra_name, 903 sizeof ifra.ifra_name); 904 905 bcopy(&ia->ia_addr, &ifra.ifra_addr, 906 ia->ia_addr.sin_len); 907 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 908 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 909 ia->ia_dstaddr.sin_len); 910 } 911 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 912 ia->ia_sockmask.sin_len); 913 914 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 915 ifp, td); 916 } 917 } 918 } 919 920 return EOPNOTSUPP; /*just for safety*/ 921 } 922 923 /* 924 * Delete any existing route for an interface. 925 */ 926 void 927 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 928 { 929 930 if ((ia->ia_flags & IFA_ROUTE) == 0) 931 return; 932 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 933 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST); 934 else 935 rtinit(&ia->ia_ifa, RTM_DELETE, 0); 936 ia->ia_flags &= ~IFA_ROUTE; 937 } 938 939 /* 940 * Initialize an interface's internet address 941 * and routing table entry. 942 */ 943 static int 944 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, 945 const struct sockaddr_in *sin, int scrub) 946 { 947 u_long i = ntohl(sin->sin_addr.s_addr); 948 struct sockaddr_in oldaddr; 949 struct ifaddr_container *ifac; 950 int flags = RTF_UP, error = 0; 951 int was_hash = 0; 952 953 ifac = &ia->ia_ifa.ifa_containers[mycpuid]; 954 oldaddr = ia->ia_addr; 955 956 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) { 957 was_hash = 1; 958 in_iahash_remove(ia); 959 } 960 961 ia->ia_addr = *sin; 962 if (ia->ia_addr.sin_family == AF_INET) 963 in_iahash_insert(ia); 964 965 /* 966 * Give the interface a chance to initialize 967 * if this is its first address, 968 * and to validate the address if necessary. 969 */ 970 if (ifp->if_ioctl != NULL) { 971 lwkt_serialize_enter(ifp->if_serializer); 972 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL); 973 lwkt_serialize_exit(ifp->if_serializer); 974 if (error) 975 goto fail; 976 } 977 978 /* 979 * Delete old route, if requested. 980 */ 981 if (scrub) { 982 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 983 in_ifscrub(ifp, ia); 984 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 985 } 986 987 /* 988 * Calculate netmask/subnetmask. 989 */ 990 if (IN_CLASSA(i)) 991 ia->ia_netmask = IN_CLASSA_NET; 992 else if (IN_CLASSB(i)) 993 ia->ia_netmask = IN_CLASSB_NET; 994 else 995 ia->ia_netmask = IN_CLASSC_NET; 996 /* 997 * The subnet mask usually includes at least the standard network part, 998 * but may may be smaller in the case of supernetting. 999 * If it is set, we believe it. 1000 */ 1001 if (ia->ia_subnetmask == 0) { 1002 ia->ia_subnetmask = ia->ia_netmask; 1003 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 1004 } else { 1005 ia->ia_netmask &= ia->ia_subnetmask; 1006 } 1007 ia->ia_net = i & ia->ia_netmask; 1008 ia->ia_subnet = i & ia->ia_subnetmask; 1009 in_socktrim(&ia->ia_sockmask); 1010 1011 /* 1012 * Add route for the network. 1013 */ 1014 ia->ia_ifa.ifa_metric = ifp->if_metric; 1015 if (ifp->if_flags & IFF_BROADCAST) { 1016 ia->ia_broadaddr.sin_addr.s_addr = 1017 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 1018 ia->ia_netbroadcast.s_addr = 1019 htonl(ia->ia_net | ~ ia->ia_netmask); 1020 } else if (ifp->if_flags & IFF_LOOPBACK) { 1021 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 1022 flags |= RTF_HOST; 1023 } else if (ifp->if_flags & IFF_POINTOPOINT) { 1024 if (ia->ia_dstaddr.sin_family != AF_INET) 1025 return (0); 1026 flags |= RTF_HOST; 1027 } 1028 1029 /*- 1030 * Don't add host routes for interface addresses of 1031 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it 1032 * possible to assign several such address pairs with consistent 1033 * results (no host route) and is required by BOOTP. 1034 * 1035 * XXX: This is ugly ! There should be a way for the caller to 1036 * say that they don't want a host route. 1037 */ 1038 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY || 1039 ia->ia_netmask != IN_CLASSA_NET || 1040 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) { 1041 if ((error = rtinit(&ia->ia_ifa, RTM_ADD, flags)) != 0) 1042 goto fail; 1043 ia->ia_flags |= IFA_ROUTE; 1044 } 1045 1046 /* 1047 * If the interface supports multicast, join the "all hosts" 1048 * multicast group on that interface. 1049 */ 1050 if (ifp->if_flags & IFF_MULTICAST) { 1051 struct in_addr addr; 1052 1053 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 1054 in_addmulti(&addr, ifp); 1055 } 1056 return (0); 1057 fail: 1058 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) 1059 in_iahash_remove(ia); 1060 1061 ia->ia_addr = oldaddr; 1062 if (was_hash) 1063 in_iahash_insert(ia); 1064 return (error); 1065 } 1066 1067 1068 /* 1069 * Return 1 if the address might be a local broadcast address. 1070 */ 1071 int 1072 in_broadcast(struct in_addr in, struct ifnet *ifp) 1073 { 1074 struct ifaddr_container *ifac; 1075 u_long t; 1076 1077 if (in.s_addr == INADDR_BROADCAST || 1078 in.s_addr == INADDR_ANY) 1079 return 1; 1080 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1081 return 0; 1082 t = ntohl(in.s_addr); 1083 /* 1084 * Look through the list of addresses for a match 1085 * with a broadcast address. 1086 */ 1087 #define ia ((struct in_ifaddr *)ifa) 1088 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 1089 struct ifaddr *ifa = ifac->ifa; 1090 1091 if (ifa->ifa_addr->sa_family == AF_INET && 1092 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 1093 in.s_addr == ia->ia_netbroadcast.s_addr || 1094 /* 1095 * Check for old-style (host 0) broadcast. 1096 */ 1097 t == ia->ia_subnet || t == ia->ia_net) && 1098 /* 1099 * Check for an all one subnetmask. These 1100 * only exist when an interface gets a secondary 1101 * address. 1102 */ 1103 ia->ia_subnetmask != (u_long)0xffffffff) 1104 return 1; 1105 } 1106 return (0); 1107 #undef ia 1108 } 1109 /* 1110 * Add an address to the list of IP multicast addresses for a given interface. 1111 */ 1112 struct in_multi * 1113 in_addmulti(struct in_addr *ap, struct ifnet *ifp) 1114 { 1115 struct in_multi *inm; 1116 int error; 1117 struct sockaddr_in sin; 1118 struct ifmultiaddr *ifma; 1119 1120 /* 1121 * Call generic routine to add membership or increment 1122 * refcount. It wants addresses in the form of a sockaddr, 1123 * so we build one here (being careful to zero the unused bytes). 1124 */ 1125 bzero(&sin, sizeof sin); 1126 sin.sin_family = AF_INET; 1127 sin.sin_len = sizeof sin; 1128 sin.sin_addr = *ap; 1129 crit_enter(); 1130 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma); 1131 if (error) { 1132 crit_exit(); 1133 return 0; 1134 } 1135 1136 /* 1137 * If ifma->ifma_protospec is null, then if_addmulti() created 1138 * a new record. Otherwise, we are done. 1139 */ 1140 if (ifma->ifma_protospec != 0) { 1141 crit_exit(); 1142 return ifma->ifma_protospec; 1143 } 1144 1145 /* XXX - if_addmulti uses M_WAITOK. Can this really be called 1146 at interrupt time? If so, need to fix if_addmulti. XXX */ 1147 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO); 1148 inm->inm_addr = *ap; 1149 inm->inm_ifp = ifp; 1150 inm->inm_ifma = ifma; 1151 ifma->ifma_protospec = inm; 1152 LIST_INSERT_HEAD(&in_multihead, inm, inm_link); 1153 1154 /* 1155 * Let IGMP know that we have joined a new IP multicast group. 1156 */ 1157 igmp_joingroup(inm); 1158 crit_exit(); 1159 return (inm); 1160 } 1161 1162 /* 1163 * Delete a multicast address record. 1164 */ 1165 void 1166 in_delmulti(struct in_multi *inm) 1167 { 1168 struct ifmultiaddr *ifma; 1169 struct in_multi my_inm; 1170 1171 crit_enter(); 1172 ifma = inm->inm_ifma; 1173 my_inm.inm_ifp = NULL ; /* don't send the leave msg */ 1174 if (ifma->ifma_refcount == 1) { 1175 /* 1176 * No remaining claims to this record; let IGMP know that 1177 * we are leaving the multicast group. 1178 * But do it after the if_delmulti() which might reset 1179 * the interface and nuke the packet. 1180 */ 1181 my_inm = *inm ; 1182 ifma->ifma_protospec = 0; 1183 LIST_REMOVE(inm, inm_link); 1184 kfree(inm, M_IPMADDR); 1185 } 1186 /* XXX - should be separate API for when we have an ifma? */ 1187 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr); 1188 if (my_inm.inm_ifp != NULL) 1189 igmp_leavegroup(&my_inm); 1190 crit_exit(); 1191 } 1192 1193 void 1194 in_ifdetach(struct ifnet *ifp) 1195 { 1196 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp); 1197 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp); 1198 } 1199