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