1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/systm.h> 28 #include <sys/stream.h> 29 #include <sys/strsubr.h> 30 #include <sys/ddi.h> 31 #include <sys/sunddi.h> 32 #include <sys/kmem.h> 33 #include <sys/socket.h> 34 #include <sys/random.h> 35 #include <sys/tsol/tndb.h> 36 #include <sys/tsol/tnet.h> 37 38 #include <netinet/in.h> 39 #include <netinet/ip6.h> 40 #include <netinet/sctp.h> 41 42 #include <inet/common.h> 43 #include <inet/ip.h> 44 #include <inet/ip6.h> 45 #include <inet/ip_ire.h> 46 #include <inet/ip_if.h> 47 #include <inet/ip_ndp.h> 48 #include <inet/mib2.h> 49 #include <inet/nd.h> 50 #include <inet/optcom.h> 51 #include <inet/sctp_ip.h> 52 #include <inet/ipclassifier.h> 53 54 #include "sctp_impl.h" 55 #include "sctp_addr.h" 56 #include "sctp_asconf.h" 57 58 static struct kmem_cache *sctp_kmem_faddr_cache; 59 static void sctp_init_faddr(sctp_t *, sctp_faddr_t *, in6_addr_t *, mblk_t *); 60 61 /* Set the source address. Refer to comments in sctp_get_dest(). */ 62 void 63 sctp_set_saddr(sctp_t *sctp, sctp_faddr_t *fp) 64 { 65 boolean_t v6 = !fp->isv4; 66 boolean_t addr_set; 67 68 fp->saddr = sctp_get_valid_addr(sctp, v6, &addr_set); 69 /* 70 * If there is no source address avaialble, mark this peer address 71 * as unreachable for now. When the heartbeat timer fires, it will 72 * call sctp_get_dest() to re-check if there is any source address 73 * available. 74 */ 75 if (!addr_set) 76 fp->state = SCTP_FADDRS_UNREACH; 77 } 78 79 /* 80 * Call this function to get information about a peer addr fp. 81 * 82 * Uses ip_attr_connect to avoid explicit use of ire and source address 83 * selection. 84 */ 85 void 86 sctp_get_dest(sctp_t *sctp, sctp_faddr_t *fp) 87 { 88 in6_addr_t laddr; 89 in6_addr_t nexthop; 90 sctp_saddr_ipif_t *sp; 91 int hdrlen; 92 sctp_stack_t *sctps = sctp->sctp_sctps; 93 conn_t *connp = sctp->sctp_connp; 94 iulp_t uinfo; 95 uint_t pmtu; 96 int error; 97 uint32_t flags = IPDF_VERIFY_DST | IPDF_IPSEC | 98 IPDF_SELECT_SRC | IPDF_UNIQUE_DCE; 99 100 /* 101 * Tell sctp_make_mp it needs to call us again should we not 102 * complete and set the saddr. 103 */ 104 fp->saddr = ipv6_all_zeros; 105 106 /* 107 * If this addr is not reachable, mark it as unconfirmed for now, the 108 * state will be changed back to unreachable later in this function 109 * if it is still the case. 110 */ 111 if (fp->state == SCTP_FADDRS_UNREACH) { 112 fp->state = SCTP_FADDRS_UNCONFIRMED; 113 } 114 115 /* 116 * Socket is connected - enable PMTU discovery. 117 */ 118 if (!sctps->sctps_ignore_path_mtu) 119 fp->ixa->ixa_flags |= IXAF_PMTU_DISCOVERY; 120 121 ip_attr_nexthop(&connp->conn_xmit_ipp, fp->ixa, &fp->faddr, 122 &nexthop); 123 124 laddr = fp->saddr; 125 error = ip_attr_connect(connp, fp->ixa, &laddr, &fp->faddr, &nexthop, 126 connp->conn_fport, &laddr, &uinfo, flags); 127 128 if (error != 0) { 129 dprint(3, ("sctp_get_dest: no ire for %x:%x:%x:%x\n", 130 SCTP_PRINTADDR(fp->faddr))); 131 /* 132 * It is tempting to just leave the src addr 133 * unspecified and let IP figure it out, but we 134 * *cannot* do this, since IP may choose a src addr 135 * that is not part of this association... unless 136 * this sctp has bound to all addrs. So if the dest 137 * lookup fails, try to find one in our src addr 138 * list, unless the sctp has bound to all addrs, in 139 * which case we change the src addr to unspec. 140 * 141 * Note that if this is a v6 endpoint but it does 142 * not have any v4 address at this point (e.g. may 143 * have been deleted), sctp_get_valid_addr() will 144 * return mapped INADDR_ANY. In this case, this 145 * address should be marked not reachable so that 146 * it won't be used to send data. 147 */ 148 sctp_set_saddr(sctp, fp); 149 if (fp->state == SCTP_FADDRS_UNREACH) 150 return; 151 goto check_current; 152 } 153 ASSERT(fp->ixa->ixa_ire != NULL); 154 ASSERT(!(fp->ixa->ixa_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))); 155 156 if (!sctp->sctp_loopback) 157 sctp->sctp_loopback = uinfo.iulp_loopback; 158 159 /* Make sure the laddr is part of this association */ 160 if ((sp = sctp_saddr_lookup(sctp, &laddr, 0)) != NULL && 161 !sp->saddr_ipif_dontsrc) { 162 if (sp->saddr_ipif_unconfirmed == 1) 163 sp->saddr_ipif_unconfirmed = 0; 164 /* We did IPsec policy lookup for laddr already */ 165 fp->saddr = laddr; 166 } else { 167 dprint(2, ("sctp_get_dest: src addr is not part of assoc " 168 "%x:%x:%x:%x\n", SCTP_PRINTADDR(laddr))); 169 170 /* 171 * Set the src to the first saddr and hope for the best. 172 * Note that this case should very seldomly 173 * happen. One scenario this can happen is an app 174 * explicitly bind() to an address. But that address is 175 * not the preferred source address to send to the peer. 176 */ 177 sctp_set_saddr(sctp, fp); 178 if (fp->state == SCTP_FADDRS_UNREACH) { 179 return; 180 } 181 } 182 183 /* 184 * Pull out RTO information for this faddr and use it if we don't 185 * have any yet. 186 */ 187 if (fp->srtt == -1 && uinfo.iulp_rtt != 0) { 188 /* The cached value is in ms. */ 189 fp->srtt = MSEC_TO_TICK(uinfo.iulp_rtt); 190 fp->rttvar = MSEC_TO_TICK(uinfo.iulp_rtt_sd); 191 fp->rto = 3 * fp->srtt; 192 193 /* Bound the RTO by configured min and max values */ 194 if (fp->rto < sctp->sctp_rto_min) { 195 fp->rto = sctp->sctp_rto_min; 196 } 197 if (fp->rto > sctp->sctp_rto_max) { 198 fp->rto = sctp->sctp_rto_max; 199 } 200 SCTP_MAX_RTO(sctp, fp); 201 } 202 pmtu = uinfo.iulp_mtu; 203 204 /* 205 * Record the MTU for this faddr. If the MTU for this faddr has 206 * changed, check if the assc MTU will also change. 207 */ 208 if (fp->isv4) { 209 hdrlen = sctp->sctp_hdr_len; 210 } else { 211 hdrlen = sctp->sctp_hdr6_len; 212 } 213 if ((fp->sfa_pmss + hdrlen) != pmtu) { 214 /* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */ 215 fp->sfa_pmss = (pmtu - hdrlen) & ~(SCTP_ALIGN - 1); 216 if (fp->cwnd < (fp->sfa_pmss * 2)) { 217 SET_CWND(fp, fp->sfa_pmss, 218 sctps->sctps_slow_start_initial); 219 } 220 } 221 222 check_current: 223 if (fp == sctp->sctp_current) 224 sctp_set_faddr_current(sctp, fp); 225 } 226 227 void 228 sctp_update_dce(sctp_t *sctp) 229 { 230 sctp_faddr_t *fp; 231 sctp_stack_t *sctps = sctp->sctp_sctps; 232 iulp_t uinfo; 233 ip_stack_t *ipst = sctps->sctps_netstack->netstack_ip; 234 uint_t ifindex; 235 236 for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 237 bzero(&uinfo, sizeof (uinfo)); 238 /* 239 * Only record the PMTU for this faddr if we actually have 240 * done discovery. This prevents initialized default from 241 * clobbering any real info that IP may have. 242 */ 243 if (fp->pmtu_discovered) { 244 if (fp->isv4) { 245 uinfo.iulp_mtu = fp->sfa_pmss + 246 sctp->sctp_hdr_len; 247 } else { 248 uinfo.iulp_mtu = fp->sfa_pmss + 249 sctp->sctp_hdr6_len; 250 } 251 } 252 if (sctps->sctps_rtt_updates != 0 && 253 fp->rtt_updates >= sctps->sctps_rtt_updates) { 254 /* 255 * dce_update_uinfo() merges these values with the 256 * old values. 257 */ 258 uinfo.iulp_rtt = TICK_TO_MSEC(fp->srtt); 259 uinfo.iulp_rtt_sd = TICK_TO_MSEC(fp->rttvar); 260 fp->rtt_updates = 0; 261 } 262 ifindex = 0; 263 if (IN6_IS_ADDR_LINKSCOPE(&fp->faddr)) { 264 /* 265 * If we are going to create a DCE we'd better have 266 * an ifindex 267 */ 268 if (fp->ixa->ixa_nce != NULL) { 269 ifindex = fp->ixa->ixa_nce->nce_common-> 270 ncec_ill->ill_phyint->phyint_ifindex; 271 } else { 272 continue; 273 } 274 } 275 276 (void) dce_update_uinfo(&fp->faddr, ifindex, &uinfo, ipst); 277 } 278 } 279 280 /* 281 * The sender must later set the total length in the IP header. 282 */ 283 mblk_t * 284 sctp_make_mp(sctp_t *sctp, sctp_faddr_t *fp, int trailer) 285 { 286 mblk_t *mp; 287 size_t ipsctplen; 288 int isv4; 289 sctp_stack_t *sctps = sctp->sctp_sctps; 290 boolean_t src_changed = B_FALSE; 291 292 ASSERT(fp != NULL); 293 isv4 = fp->isv4; 294 295 if (SCTP_IS_ADDR_UNSPEC(isv4, fp->saddr) || 296 (fp->ixa->ixa_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) { 297 /* Need to pick a source */ 298 sctp_get_dest(sctp, fp); 299 /* 300 * Although we still may not get an IRE, the source address 301 * may be changed in sctp_get_ire(). Set src_changed to 302 * true so that the source address is copied again. 303 */ 304 src_changed = B_TRUE; 305 } 306 307 /* There is no suitable source address to use, return. */ 308 if (fp->state == SCTP_FADDRS_UNREACH) 309 return (NULL); 310 311 ASSERT(fp->ixa->ixa_ire != NULL); 312 ASSERT(!SCTP_IS_ADDR_UNSPEC(isv4, fp->saddr)); 313 314 if (isv4) { 315 ipsctplen = sctp->sctp_hdr_len; 316 } else { 317 ipsctplen = sctp->sctp_hdr6_len; 318 } 319 320 mp = allocb(ipsctplen + sctps->sctps_wroff_xtra + trailer, BPRI_MED); 321 if (mp == NULL) { 322 ip1dbg(("sctp_make_mp: error making mp..\n")); 323 return (NULL); 324 } 325 mp->b_rptr += sctps->sctps_wroff_xtra; 326 mp->b_wptr = mp->b_rptr + ipsctplen; 327 328 ASSERT(OK_32PTR(mp->b_wptr)); 329 330 if (isv4) { 331 ipha_t *iph = (ipha_t *)mp->b_rptr; 332 333 bcopy(sctp->sctp_iphc, mp->b_rptr, ipsctplen); 334 if (fp != sctp->sctp_current || src_changed) { 335 /* Fix the source and destination addresses. */ 336 IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst); 337 IN6_V4MAPPED_TO_IPADDR(&fp->saddr, iph->ipha_src); 338 } 339 /* set or clear the don't fragment bit */ 340 if (fp->df) { 341 iph->ipha_fragment_offset_and_flags = htons(IPH_DF); 342 } else { 343 iph->ipha_fragment_offset_and_flags = 0; 344 } 345 } else { 346 bcopy(sctp->sctp_iphc6, mp->b_rptr, ipsctplen); 347 if (fp != sctp->sctp_current || src_changed) { 348 /* Fix the source and destination addresses. */ 349 ((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr; 350 ((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr; 351 } 352 } 353 ASSERT(sctp->sctp_connp != NULL); 354 return (mp); 355 } 356 357 /* 358 * Notify upper layers about preferred write offset, write size. 359 */ 360 void 361 sctp_set_ulp_prop(sctp_t *sctp) 362 { 363 int hdrlen; 364 struct sock_proto_props sopp; 365 366 sctp_stack_t *sctps = sctp->sctp_sctps; 367 368 if (sctp->sctp_current->isv4) { 369 hdrlen = sctp->sctp_hdr_len; 370 } else { 371 hdrlen = sctp->sctp_hdr6_len; 372 } 373 ASSERT(sctp->sctp_ulpd); 374 375 sctp->sctp_connp->conn_wroff = sctps->sctps_wroff_xtra + hdrlen + 376 sizeof (sctp_data_hdr_t); 377 378 ASSERT(sctp->sctp_current->sfa_pmss == sctp->sctp_mss); 379 bzero(&sopp, sizeof (sopp)); 380 sopp.sopp_flags = SOCKOPT_MAXBLK|SOCKOPT_WROFF; 381 sopp.sopp_wroff = sctp->sctp_connp->conn_wroff; 382 sopp.sopp_maxblk = sctp->sctp_mss - sizeof (sctp_data_hdr_t); 383 sctp->sctp_ulp_prop(sctp->sctp_ulpd, &sopp); 384 } 385 386 /* 387 * Set the lengths in the packet and the transmit attributes. 388 */ 389 void 390 sctp_set_iplen(sctp_t *sctp, mblk_t *mp, ip_xmit_attr_t *ixa) 391 { 392 uint16_t sum = 0; 393 ipha_t *iph; 394 ip6_t *ip6h; 395 mblk_t *pmp = mp; 396 boolean_t isv4; 397 398 isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION); 399 for (; pmp; pmp = pmp->b_cont) 400 sum += pmp->b_wptr - pmp->b_rptr; 401 402 ixa->ixa_pktlen = sum; 403 if (isv4) { 404 iph = (ipha_t *)mp->b_rptr; 405 iph->ipha_length = htons(sum); 406 ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr_len; 407 } else { 408 ip6h = (ip6_t *)mp->b_rptr; 409 ip6h->ip6_plen = htons(sum - IPV6_HDR_LEN); 410 ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr6_len; 411 } 412 } 413 414 int 415 sctp_compare_faddrsets(sctp_faddr_t *a1, sctp_faddr_t *a2) 416 { 417 int na1 = 0; 418 int overlap = 0; 419 int equal = 1; 420 int onematch; 421 sctp_faddr_t *fp1, *fp2; 422 423 for (fp1 = a1; fp1; fp1 = fp1->next) { 424 onematch = 0; 425 for (fp2 = a2; fp2; fp2 = fp2->next) { 426 if (IN6_ARE_ADDR_EQUAL(&fp1->faddr, &fp2->faddr)) { 427 overlap++; 428 onematch = 1; 429 break; 430 } 431 if (!onematch) { 432 equal = 0; 433 } 434 } 435 na1++; 436 } 437 438 if (equal) { 439 return (SCTP_ADDR_EQUAL); 440 } 441 if (overlap == na1) { 442 return (SCTP_ADDR_SUBSET); 443 } 444 if (overlap) { 445 return (SCTP_ADDR_OVERLAP); 446 } 447 return (SCTP_ADDR_DISJOINT); 448 } 449 450 /* 451 * Returns 0 on success, ENOMEM on memory allocation failure, EHOSTUNREACH 452 * if the connection credentials fail remote host accreditation or 453 * if the new destination does not support the previously established 454 * connection security label. If sleep is true, this function should 455 * never fail for a memory allocation failure. The boolean parameter 456 * "first" decides whether the newly created faddr structure should be 457 * added at the beginning of the list or at the end. 458 * 459 * Note: caller must hold conn fanout lock. 460 */ 461 int 462 sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep, boolean_t first) 463 { 464 sctp_faddr_t *faddr; 465 mblk_t *timer_mp; 466 int err; 467 conn_t *connp = sctp->sctp_connp; 468 469 if (is_system_labeled()) { 470 ip_xmit_attr_t *ixa = connp->conn_ixa; 471 ts_label_t *effective_tsl = NULL; 472 473 ASSERT(ixa->ixa_tsl != NULL); 474 475 /* 476 * Verify the destination is allowed to receive packets 477 * at the security label of the connection we are initiating. 478 * 479 * tsol_check_dest() will create a new effective label for 480 * this connection with a modified label or label flags only 481 * if there are changes from the original label. 482 * 483 * Accept whatever label we get if this is the first 484 * destination address for this connection. The security 485 * label and label flags must match any previuous settings 486 * for all subsequent destination addresses. 487 */ 488 if (IN6_IS_ADDR_V4MAPPED(addr)) { 489 uint32_t dst; 490 IN6_V4MAPPED_TO_IPADDR(addr, dst); 491 err = tsol_check_dest(ixa->ixa_tsl, 492 &dst, IPV4_VERSION, connp->conn_mac_mode, 493 connp->conn_zone_is_global, &effective_tsl); 494 } else { 495 err = tsol_check_dest(ixa->ixa_tsl, 496 addr, IPV6_VERSION, connp->conn_mac_mode, 497 connp->conn_zone_is_global, &effective_tsl); 498 } 499 if (err != 0) 500 return (err); 501 502 if (sctp->sctp_faddrs == NULL && effective_tsl != NULL) { 503 ip_xmit_attr_replace_tsl(ixa, effective_tsl); 504 } else if (effective_tsl != NULL) { 505 label_rele(effective_tsl); 506 return (EHOSTUNREACH); 507 } 508 } 509 510 if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL) 511 return (ENOMEM); 512 bzero(faddr, sizeof (*faddr)); 513 timer_mp = sctp_timer_alloc((sctp), sctp_rexmit_timer, sleep); 514 if (timer_mp == NULL) { 515 kmem_cache_free(sctp_kmem_faddr_cache, faddr); 516 return (ENOMEM); 517 } 518 ((sctpt_t *)(timer_mp->b_rptr))->sctpt_faddr = faddr; 519 520 /* Start with any options set on the conn */ 521 faddr->ixa = conn_get_ixa_exclusive(connp); 522 if (faddr->ixa == NULL) { 523 freemsg(timer_mp); 524 kmem_cache_free(sctp_kmem_faddr_cache, faddr); 525 return (ENOMEM); 526 } 527 faddr->ixa->ixa_notify_cookie = connp->conn_sctp; 528 529 sctp_init_faddr(sctp, faddr, addr, timer_mp); 530 ASSERT(faddr->ixa->ixa_cred != NULL); 531 532 /* ip_attr_connect didn't allow broadcats/multicast dest */ 533 ASSERT(faddr->next == NULL); 534 535 if (sctp->sctp_faddrs == NULL) { 536 ASSERT(sctp->sctp_lastfaddr == NULL); 537 /* only element on list; first and last are same */ 538 sctp->sctp_faddrs = sctp->sctp_lastfaddr = faddr; 539 } else if (first) { 540 ASSERT(sctp->sctp_lastfaddr != NULL); 541 faddr->next = sctp->sctp_faddrs; 542 sctp->sctp_faddrs = faddr; 543 } else { 544 sctp->sctp_lastfaddr->next = faddr; 545 sctp->sctp_lastfaddr = faddr; 546 } 547 sctp->sctp_nfaddrs++; 548 549 return (0); 550 } 551 552 sctp_faddr_t * 553 sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr) 554 { 555 sctp_faddr_t *fp; 556 557 for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 558 if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) 559 break; 560 } 561 562 return (fp); 563 } 564 565 sctp_faddr_t * 566 sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr) 567 { 568 for (; fp; fp = fp->next) { 569 if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) { 570 break; 571 } 572 } 573 574 return (fp); 575 } 576 577 /* 578 * To change the currently used peer address to the specified one. 579 */ 580 void 581 sctp_set_faddr_current(sctp_t *sctp, sctp_faddr_t *fp) 582 { 583 /* Now setup the composite header. */ 584 if (fp->isv4) { 585 IN6_V4MAPPED_TO_IPADDR(&fp->faddr, 586 sctp->sctp_ipha->ipha_dst); 587 IN6_V4MAPPED_TO_IPADDR(&fp->saddr, sctp->sctp_ipha->ipha_src); 588 /* update don't fragment bit */ 589 if (fp->df) { 590 sctp->sctp_ipha->ipha_fragment_offset_and_flags = 591 htons(IPH_DF); 592 } else { 593 sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0; 594 } 595 } else { 596 sctp->sctp_ip6h->ip6_dst = fp->faddr; 597 sctp->sctp_ip6h->ip6_src = fp->saddr; 598 } 599 600 sctp->sctp_current = fp; 601 sctp->sctp_mss = fp->sfa_pmss; 602 603 /* Update the uppper layer for the change. */ 604 if (!SCTP_IS_DETACHED(sctp)) 605 sctp_set_ulp_prop(sctp); 606 } 607 608 void 609 sctp_redo_faddr_srcs(sctp_t *sctp) 610 { 611 sctp_faddr_t *fp; 612 613 for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 614 sctp_get_dest(sctp, fp); 615 } 616 } 617 618 void 619 sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp) 620 { 621 int64_t now = ddi_get_lbolt64(); 622 623 fp->strikes = 0; 624 sctp->sctp_strikes = 0; 625 fp->lastactive = now; 626 fp->hb_expiry = now + SET_HB_INTVL(fp); 627 fp->hb_pending = B_FALSE; 628 if (fp->state != SCTP_FADDRS_ALIVE) { 629 fp->state = SCTP_FADDRS_ALIVE; 630 sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_AVAILABLE, 0); 631 /* Should have a full IRE now */ 632 sctp_get_dest(sctp, fp); 633 634 /* 635 * If this is the primary, switch back to it now. And 636 * we probably want to reset the source addr used to reach 637 * it. 638 * Note that if we didn't find a source in sctp_get_dest 639 * then we'd be unreachable at this point in time. 640 */ 641 if (fp == sctp->sctp_primary && 642 fp->state != SCTP_FADDRS_UNREACH) { 643 sctp_set_faddr_current(sctp, fp); 644 return; 645 } 646 } 647 } 648 649 int 650 sctp_is_a_faddr_clean(sctp_t *sctp) 651 { 652 sctp_faddr_t *fp; 653 654 for (fp = sctp->sctp_faddrs; fp; fp = fp->next) { 655 if (fp->state == SCTP_FADDRS_ALIVE && fp->strikes == 0) { 656 return (1); 657 } 658 } 659 660 return (0); 661 } 662 663 /* 664 * Returns 0 if there is at leave one other active faddr, -1 if there 665 * are none. If there are none left, faddr_dead() will start killing the 666 * association. 667 * If the downed faddr was the current faddr, a new current faddr 668 * will be chosen. 669 */ 670 int 671 sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate) 672 { 673 sctp_faddr_t *ofp; 674 sctp_stack_t *sctps = sctp->sctp_sctps; 675 676 if (fp->state == SCTP_FADDRS_ALIVE) { 677 sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_UNREACHABLE, 0); 678 } 679 fp->state = newstate; 680 681 dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n", 682 SCTP_PRINTADDR(fp->faddr), newstate)); 683 684 if (fp == sctp->sctp_current) { 685 /* Current faddr down; need to switch it */ 686 sctp->sctp_current = NULL; 687 } 688 689 /* Find next alive faddr */ 690 ofp = fp; 691 for (fp = fp->next; fp != NULL; fp = fp->next) { 692 if (fp->state == SCTP_FADDRS_ALIVE) { 693 break; 694 } 695 } 696 697 if (fp == NULL) { 698 /* Continue from beginning of list */ 699 for (fp = sctp->sctp_faddrs; fp != ofp; fp = fp->next) { 700 if (fp->state == SCTP_FADDRS_ALIVE) { 701 break; 702 } 703 } 704 } 705 706 /* 707 * Find a new fp, so if the current faddr is dead, use the new fp 708 * as the current one. 709 */ 710 if (fp != ofp) { 711 if (sctp->sctp_current == NULL) { 712 dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n", 713 SCTP_PRINTADDR(fp->faddr))); 714 /* 715 * Note that we don't need to reset the source addr 716 * of the new fp. 717 */ 718 sctp_set_faddr_current(sctp, fp); 719 } 720 return (0); 721 } 722 723 724 /* All faddrs are down; kill the association */ 725 dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n")); 726 BUMP_MIB(&sctps->sctps_mib, sctpAborted); 727 sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ? 728 SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL); 729 sctp_clean_death(sctp, sctp->sctp_client_errno ? 730 sctp->sctp_client_errno : ETIMEDOUT); 731 732 return (-1); 733 } 734 735 sctp_faddr_t * 736 sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp) 737 { 738 sctp_faddr_t *nfp = NULL; 739 sctp_faddr_t *saved_fp = NULL; 740 int min_strikes; 741 742 if (ofp == NULL) { 743 ofp = sctp->sctp_current; 744 } 745 /* Nothing to do */ 746 if (sctp->sctp_nfaddrs < 2) 747 return (ofp); 748 749 min_strikes = ofp->strikes; 750 751 /* 752 * Find the next live peer address with zero strikes. In case 753 * there is none, find the one with the lowest number of strikes. 754 */ 755 for (nfp = ofp->next; nfp != ofp; nfp = nfp->next) { 756 /* If reached end of list, continue scan from the head */ 757 if (nfp == NULL) { 758 nfp = sctp->sctp_faddrs; 759 continue; 760 } 761 if (nfp->state == SCTP_FADDRS_ALIVE) { 762 if (nfp->strikes == 0) 763 break; 764 if (nfp->strikes < min_strikes) { 765 min_strikes = nfp->strikes; 766 saved_fp = nfp; 767 } 768 } 769 } 770 /* If reached the old address, there is no zero strike path */ 771 if (nfp == ofp) 772 nfp = NULL; 773 774 /* 775 * If there is a peer address with zero strikes we use that, if not 776 * return a peer address with fewer strikes than the one last used, 777 * if neither exist we may as well stay with the old one. 778 */ 779 if (nfp != NULL) 780 return (nfp); 781 if (saved_fp != NULL) 782 return (saved_fp); 783 return (ofp); 784 } 785 786 void 787 sctp_unlink_faddr(sctp_t *sctp, sctp_faddr_t *fp) 788 { 789 sctp_faddr_t *fpp; 790 791 if (!sctp->sctp_faddrs) { 792 return; 793 } 794 795 if (fp->timer_mp != NULL) { 796 sctp_timer_free(fp->timer_mp); 797 fp->timer_mp = NULL; 798 fp->timer_running = 0; 799 } 800 if (fp->rc_timer_mp != NULL) { 801 sctp_timer_free(fp->rc_timer_mp); 802 fp->rc_timer_mp = NULL; 803 fp->rc_timer_running = 0; 804 } 805 if (fp->ixa != NULL) { 806 ixa_refrele(fp->ixa); 807 fp->ixa = NULL; 808 } 809 810 if (fp == sctp->sctp_faddrs) { 811 goto gotit; 812 } 813 814 for (fpp = sctp->sctp_faddrs; fpp->next != fp; fpp = fpp->next) 815 ; 816 817 gotit: 818 ASSERT(sctp->sctp_conn_tfp != NULL); 819 mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 820 if (fp == sctp->sctp_faddrs) { 821 sctp->sctp_faddrs = fp->next; 822 } else { 823 fpp->next = fp->next; 824 } 825 mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 826 kmem_cache_free(sctp_kmem_faddr_cache, fp); 827 sctp->sctp_nfaddrs--; 828 } 829 830 void 831 sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock) 832 { 833 sctp_faddr_t *fp, *fpn; 834 835 if (sctp->sctp_faddrs == NULL) { 836 ASSERT(sctp->sctp_lastfaddr == NULL); 837 return; 838 } 839 840 ASSERT(sctp->sctp_lastfaddr != NULL); 841 sctp->sctp_lastfaddr = NULL; 842 sctp->sctp_current = NULL; 843 sctp->sctp_primary = NULL; 844 845 sctp_free_faddr_timers(sctp); 846 847 if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) { 848 /* in conn fanout; need to hold lock */ 849 mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 850 } 851 852 for (fp = sctp->sctp_faddrs; fp; fp = fpn) { 853 fpn = fp->next; 854 if (fp->ixa != NULL) { 855 ixa_refrele(fp->ixa); 856 fp->ixa = NULL; 857 } 858 kmem_cache_free(sctp_kmem_faddr_cache, fp); 859 sctp->sctp_nfaddrs--; 860 } 861 862 sctp->sctp_faddrs = NULL; 863 ASSERT(sctp->sctp_nfaddrs == 0); 864 if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) { 865 mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 866 } 867 868 } 869 870 void 871 sctp_zap_addrs(sctp_t *sctp) 872 { 873 sctp_zap_faddrs(sctp, 0); 874 sctp_free_saddrs(sctp); 875 } 876 877 /* 878 * Build two SCTP header templates; one for IPv4 and one for IPv6. 879 * Store them in sctp_iphc and sctp_iphc6 respectively (and related fields). 880 * There are no IP addresses in the templates, but the port numbers and 881 * verifier are field in from the conn_t and sctp_t. 882 * 883 * Returns failure if can't allocate memory, or if there is a problem 884 * with a routing header/option. 885 * 886 * We allocate space for the minimum sctp header (sctp_hdr_t). 887 * 888 * We massage an routing option/header. There is no checksum implication 889 * for a routing header for sctp. 890 * 891 * Caller needs to update conn_wroff if desired. 892 * 893 * TSol notes: This assumes that a SCTP association has a single peer label 894 * since we only track a single pair of ipp_label_v4/v6 and not a separate one 895 * for each faddr. 896 */ 897 int 898 sctp_build_hdrs(sctp_t *sctp, int sleep) 899 { 900 conn_t *connp = sctp->sctp_connp; 901 ip_pkt_t *ipp = &connp->conn_xmit_ipp; 902 uint_t ip_hdr_length; 903 uchar_t *hdrs; 904 uint_t hdrs_len; 905 uint_t ulp_hdr_length = sizeof (sctp_hdr_t); 906 ipha_t *ipha; 907 ip6_t *ip6h; 908 sctp_hdr_t *sctph; 909 in6_addr_t v6src, v6dst; 910 ipaddr_t v4src, v4dst; 911 912 v4src = connp->conn_saddr_v4; 913 v4dst = connp->conn_faddr_v4; 914 v6src = connp->conn_saddr_v6; 915 v6dst = connp->conn_faddr_v6; 916 917 /* First do IPv4 header */ 918 ip_hdr_length = ip_total_hdrs_len_v4(ipp); 919 920 /* In case of TX label and IP options it can be too much */ 921 if (ip_hdr_length > IP_MAX_HDR_LENGTH) { 922 /* Preserves existing TX errno for this */ 923 return (EHOSTUNREACH); 924 } 925 hdrs_len = ip_hdr_length + ulp_hdr_length; 926 ASSERT(hdrs_len != 0); 927 928 if (hdrs_len != sctp->sctp_iphc_len) { 929 /* Allocate new before we free any old */ 930 hdrs = kmem_alloc(hdrs_len, sleep); 931 if (hdrs == NULL) 932 return (ENOMEM); 933 934 if (sctp->sctp_iphc != NULL) 935 kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len); 936 sctp->sctp_iphc = hdrs; 937 sctp->sctp_iphc_len = hdrs_len; 938 } else { 939 hdrs = sctp->sctp_iphc; 940 } 941 sctp->sctp_hdr_len = sctp->sctp_iphc_len; 942 sctp->sctp_ip_hdr_len = ip_hdr_length; 943 944 sctph = (sctp_hdr_t *)(hdrs + ip_hdr_length); 945 sctp->sctp_sctph = sctph; 946 sctph->sh_sport = connp->conn_lport; 947 sctph->sh_dport = connp->conn_fport; 948 sctph->sh_verf = sctp->sctp_fvtag; 949 sctph->sh_chksum = 0; 950 951 ipha = (ipha_t *)hdrs; 952 sctp->sctp_ipha = ipha; 953 954 ipha->ipha_src = v4src; 955 ipha->ipha_dst = v4dst; 956 ip_build_hdrs_v4(hdrs, ip_hdr_length, ipp, connp->conn_proto); 957 ipha->ipha_length = htons(hdrs_len); 958 ipha->ipha_fragment_offset_and_flags = 0; 959 960 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) 961 (void) ip_massage_options(ipha, connp->conn_netstack); 962 963 /* Now IPv6 */ 964 ip_hdr_length = ip_total_hdrs_len_v6(ipp); 965 hdrs_len = ip_hdr_length + ulp_hdr_length; 966 ASSERT(hdrs_len != 0); 967 968 if (hdrs_len != sctp->sctp_iphc6_len) { 969 /* Allocate new before we free any old */ 970 hdrs = kmem_alloc(hdrs_len, sleep); 971 if (hdrs == NULL) 972 return (ENOMEM); 973 974 if (sctp->sctp_iphc6 != NULL) 975 kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len); 976 sctp->sctp_iphc6 = hdrs; 977 sctp->sctp_iphc6_len = hdrs_len; 978 } else { 979 hdrs = sctp->sctp_iphc6; 980 } 981 sctp->sctp_hdr6_len = sctp->sctp_iphc6_len; 982 sctp->sctp_ip_hdr6_len = ip_hdr_length; 983 984 sctph = (sctp_hdr_t *)(hdrs + ip_hdr_length); 985 sctp->sctp_sctph6 = sctph; 986 sctph->sh_sport = connp->conn_lport; 987 sctph->sh_dport = connp->conn_fport; 988 sctph->sh_verf = sctp->sctp_fvtag; 989 sctph->sh_chksum = 0; 990 991 ip6h = (ip6_t *)hdrs; 992 sctp->sctp_ip6h = ip6h; 993 994 ip6h->ip6_src = v6src; 995 ip6h->ip6_dst = v6dst; 996 ip_build_hdrs_v6(hdrs, ip_hdr_length, ipp, connp->conn_proto, 997 connp->conn_flowinfo); 998 ip6h->ip6_plen = htons(hdrs_len - IPV6_HDR_LEN); 999 1000 if (ipp->ipp_fields & IPPF_RTHDR) { 1001 uint8_t *end; 1002 ip6_rthdr_t *rth; 1003 1004 end = (uint8_t *)ip6h + ip_hdr_length; 1005 rth = ip_find_rthdr_v6(ip6h, end); 1006 if (rth != NULL) { 1007 (void) ip_massage_options_v6(ip6h, rth, 1008 connp->conn_netstack); 1009 } 1010 1011 /* 1012 * Verify that the first hop isn't a mapped address. 1013 * Routers along the path need to do this verification 1014 * for subsequent hops. 1015 */ 1016 if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) 1017 return (EADDRNOTAVAIL); 1018 } 1019 return (0); 1020 } 1021 1022 static int 1023 sctp_v4_label(sctp_t *sctp, sctp_faddr_t *fp) 1024 { 1025 conn_t *connp = sctp->sctp_connp; 1026 1027 ASSERT(fp->ixa->ixa_flags & IXAF_IS_IPV4); 1028 return (conn_update_label(connp, fp->ixa, &fp->faddr, 1029 &connp->conn_xmit_ipp)); 1030 } 1031 1032 static int 1033 sctp_v6_label(sctp_t *sctp, sctp_faddr_t *fp) 1034 { 1035 conn_t *connp = sctp->sctp_connp; 1036 1037 ASSERT(!(fp->ixa->ixa_flags & IXAF_IS_IPV4)); 1038 return (conn_update_label(connp, fp->ixa, &fp->faddr, 1039 &connp->conn_xmit_ipp)); 1040 } 1041 1042 /* 1043 * XXX implement more sophisticated logic 1044 * 1045 * Tsol note: We have already verified the addresses using tsol_check_dest 1046 * in sctp_add_faddr, thus no need to redo that here. 1047 * We do setup ipp_label_v4 and ipp_label_v6 based on which addresses 1048 * we have. 1049 */ 1050 int 1051 sctp_set_hdraddrs(sctp_t *sctp) 1052 { 1053 sctp_faddr_t *fp; 1054 int gotv4 = 0; 1055 int gotv6 = 0; 1056 conn_t *connp = sctp->sctp_connp; 1057 1058 ASSERT(sctp->sctp_faddrs != NULL); 1059 ASSERT(sctp->sctp_nsaddrs > 0); 1060 1061 /* Set up using the primary first */ 1062 connp->conn_faddr_v6 = sctp->sctp_primary->faddr; 1063 /* saddr may be unspec; make_mp() will handle this */ 1064 connp->conn_saddr_v6 = sctp->sctp_primary->saddr; 1065 connp->conn_laddr_v6 = connp->conn_saddr_v6; 1066 if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->faddr)) { 1067 if (!is_system_labeled() || 1068 sctp_v4_label(sctp, sctp->sctp_primary) == 0) { 1069 gotv4 = 1; 1070 if (connp->conn_family == AF_INET) { 1071 goto done; 1072 } 1073 } 1074 } else { 1075 if (!is_system_labeled() || 1076 sctp_v6_label(sctp, sctp->sctp_primary) == 0) { 1077 gotv6 = 1; 1078 } 1079 } 1080 1081 for (fp = sctp->sctp_faddrs; fp; fp = fp->next) { 1082 if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->faddr)) { 1083 if (!is_system_labeled() || 1084 sctp_v4_label(sctp, fp) == 0) { 1085 gotv4 = 1; 1086 if (connp->conn_family == AF_INET || gotv6) { 1087 break; 1088 } 1089 } 1090 } else if (!gotv6 && !IN6_IS_ADDR_V4MAPPED(&fp->faddr)) { 1091 if (!is_system_labeled() || 1092 sctp_v6_label(sctp, fp) == 0) { 1093 gotv6 = 1; 1094 if (gotv4) 1095 break; 1096 } 1097 } 1098 } 1099 1100 done: 1101 if (!gotv4 && !gotv6) 1102 return (EACCES); 1103 1104 return (0); 1105 } 1106 1107 /* 1108 * got_errchunk is set B_TRUE only if called from validate_init_params(), when 1109 * an ERROR chunk is already prepended the size of which needs updating for 1110 * additional unrecognized parameters. Other callers either prepend the ERROR 1111 * chunk with the correct size after calling this function, or they are calling 1112 * to add an invalid parameter to an INIT_ACK chunk, in that case no ERROR chunk 1113 * exists, the CAUSE blocks go into the INIT_ACK directly. 1114 * 1115 * *errmp will be non-NULL both when adding an additional CAUSE block to an 1116 * existing prepended COOKIE ERROR chunk (processing params of an INIT_ACK), 1117 * and when adding unrecognized parameters after the first, to an INIT_ACK 1118 * (processing params of an INIT chunk). 1119 */ 1120 void 1121 sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp, 1122 boolean_t got_errchunk) 1123 { 1124 mblk_t *mp; 1125 sctp_parm_hdr_t *ph; 1126 size_t len; 1127 int pad; 1128 sctp_chunk_hdr_t *ecp; 1129 1130 len = sizeof (*ph) + ntohs(uph->sph_len); 1131 if ((pad = len % SCTP_ALIGN) != 0) { 1132 pad = SCTP_ALIGN - pad; 1133 len += pad; 1134 } 1135 mp = allocb(len, BPRI_MED); 1136 if (mp == NULL) { 1137 return; 1138 } 1139 1140 ph = (sctp_parm_hdr_t *)(mp->b_rptr); 1141 ph->sph_type = htons(PARM_UNRECOGNIZED); 1142 ph->sph_len = htons(len - pad); 1143 1144 /* copy in the unrecognized parameter */ 1145 bcopy(uph, ph + 1, ntohs(uph->sph_len)); 1146 1147 if (pad != 0) 1148 bzero((mp->b_rptr + len - pad), pad); 1149 1150 mp->b_wptr = mp->b_rptr + len; 1151 if (*errmp != NULL) { 1152 /* 1153 * Update total length if an ERROR chunk, then link 1154 * this CAUSE block to the possible chain of CAUSE 1155 * blocks attached to the ERROR chunk or INIT_ACK 1156 * being created. 1157 */ 1158 if (got_errchunk) { 1159 /* ERROR chunk already prepended */ 1160 ecp = (sctp_chunk_hdr_t *)((*errmp)->b_rptr); 1161 ecp->sch_len = htons(ntohs(ecp->sch_len) + len); 1162 } 1163 linkb(*errmp, mp); 1164 } else { 1165 *errmp = mp; 1166 } 1167 } 1168 1169 /* 1170 * o Bounds checking 1171 * o Updates remaining 1172 * o Checks alignment 1173 */ 1174 sctp_parm_hdr_t * 1175 sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining) 1176 { 1177 int pad; 1178 uint16_t len; 1179 1180 len = ntohs(current->sph_len); 1181 *remaining -= len; 1182 if (*remaining < sizeof (*current) || len < sizeof (*current)) { 1183 return (NULL); 1184 } 1185 if ((pad = len & (SCTP_ALIGN - 1)) != 0) { 1186 pad = SCTP_ALIGN - pad; 1187 *remaining -= pad; 1188 } 1189 /*LINTED pointer cast may result in improper alignment*/ 1190 current = (sctp_parm_hdr_t *)((char *)current + len + pad); 1191 return (current); 1192 } 1193 1194 /* 1195 * Sets the address parameters given in the INIT chunk into sctp's 1196 * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are 1197 * no address parameters in the INIT chunk, a single faddr is created 1198 * from the ip hdr at the beginning of pkt. 1199 * If there already are existing addresses hanging from sctp, merge 1200 * them in, if the old info contains addresses which are not present 1201 * in this new info, get rid of them, and clean the pointers if there's 1202 * messages which have this as their target address. 1203 * 1204 * We also re-adjust the source address list here since the list may 1205 * contain more than what is actually part of the association. If 1206 * we get here from sctp_send_cookie_echo(), we are on the active 1207 * side and psctp will be NULL and ich will be the INIT-ACK chunk. 1208 * If we get here from sctp_accept_comm(), ich will be the INIT chunk 1209 * and psctp will the listening endpoint. 1210 * 1211 * INIT processing: When processing the INIT we inherit the src address 1212 * list from the listener. For a loopback or linklocal association, we 1213 * delete the list and just take the address from the IP header (since 1214 * that's how we created the INIT-ACK). Additionally, for loopback we 1215 * ignore the address params in the INIT. For determining which address 1216 * types were sent in the INIT-ACK we follow the same logic as in 1217 * creating the INIT-ACK. We delete addresses of the type that are not 1218 * supported by the peer. 1219 * 1220 * INIT-ACK processing: When processing the INIT-ACK since we had not 1221 * included addr params for loopback or linklocal addresses when creating 1222 * the INIT, we just use the address from the IP header. Further, for 1223 * loopback we ignore the addr param list. We mark addresses of the 1224 * type not supported by the peer as unconfirmed. 1225 * 1226 * In case of INIT processing we look for supported address types in the 1227 * supported address param, if present. In both cases the address type in 1228 * the IP header is supported as well as types for addresses in the param 1229 * list, if any. 1230 * 1231 * Once we have the supported address types sctp_check_saddr() runs through 1232 * the source address list and deletes or marks as unconfirmed address of 1233 * types not supported by the peer. 1234 * 1235 * Returns 0 on success, sys errno on failure 1236 */ 1237 int 1238 sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt, 1239 sctp_chunk_hdr_t *ich, uint_t *sctp_options) 1240 { 1241 sctp_init_chunk_t *init; 1242 ipha_t *iph; 1243 ip6_t *ip6h; 1244 in6_addr_t hdrsaddr[1]; 1245 in6_addr_t hdrdaddr[1]; 1246 sctp_parm_hdr_t *ph; 1247 ssize_t remaining; 1248 int isv4; 1249 int err; 1250 sctp_faddr_t *fp; 1251 int supp_af = 0; 1252 boolean_t check_saddr = B_TRUE; 1253 in6_addr_t curaddr; 1254 sctp_stack_t *sctps = sctp->sctp_sctps; 1255 conn_t *connp = sctp->sctp_connp; 1256 1257 if (sctp_options != NULL) 1258 *sctp_options = 0; 1259 1260 /* extract the address from the IP header */ 1261 isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION); 1262 if (isv4) { 1263 iph = (ipha_t *)pkt->b_rptr; 1264 IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdrsaddr); 1265 IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, hdrdaddr); 1266 supp_af |= PARM_SUPP_V4; 1267 } else { 1268 ip6h = (ip6_t *)pkt->b_rptr; 1269 hdrsaddr[0] = ip6h->ip6_src; 1270 hdrdaddr[0] = ip6h->ip6_dst; 1271 supp_af |= PARM_SUPP_V6; 1272 } 1273 1274 /* 1275 * Unfortunately, we can't delay this because adding an faddr 1276 * looks for the presence of the source address (from the ire 1277 * for the faddr) in the source address list. We could have 1278 * delayed this if, say, this was a loopback/linklocal connection. 1279 * Now, we just end up nuking this list and taking the addr from 1280 * the IP header for loopback/linklocal. 1281 */ 1282 if (psctp != NULL && psctp->sctp_nsaddrs > 0) { 1283 ASSERT(sctp->sctp_nsaddrs == 0); 1284 1285 err = sctp_dup_saddrs(psctp, sctp, KM_NOSLEEP); 1286 if (err != 0) 1287 return (err); 1288 } 1289 /* 1290 * We will add the faddr before parsing the address list as this 1291 * might be a loopback connection and we would not have to 1292 * go through the list. 1293 * 1294 * Make sure the header's addr is in the list 1295 */ 1296 fp = sctp_lookup_faddr(sctp, hdrsaddr); 1297 if (fp == NULL) { 1298 /* not included; add it now */ 1299 err = sctp_add_faddr(sctp, hdrsaddr, KM_NOSLEEP, B_TRUE); 1300 if (err != 0) 1301 return (err); 1302 1303 /* sctp_faddrs will be the hdr addr */ 1304 fp = sctp->sctp_faddrs; 1305 } 1306 /* make the header addr the primary */ 1307 1308 if (cl_sctp_assoc_change != NULL && psctp == NULL) 1309 curaddr = sctp->sctp_current->faddr; 1310 1311 sctp->sctp_primary = fp; 1312 sctp->sctp_current = fp; 1313 sctp->sctp_mss = fp->sfa_pmss; 1314 1315 /* For loopback connections & linklocal get address from the header */ 1316 if (sctp->sctp_loopback || sctp->sctp_linklocal) { 1317 if (sctp->sctp_nsaddrs != 0) 1318 sctp_free_saddrs(sctp); 1319 if ((err = sctp_saddr_add_addr(sctp, hdrdaddr, 0)) != 0) 1320 return (err); 1321 /* For loopback ignore address list */ 1322 if (sctp->sctp_loopback) 1323 return (0); 1324 check_saddr = B_FALSE; 1325 } 1326 1327 /* Walk the params in the INIT [ACK], pulling out addr params */ 1328 remaining = ntohs(ich->sch_len) - sizeof (*ich) - 1329 sizeof (sctp_init_chunk_t); 1330 if (remaining < sizeof (*ph)) { 1331 if (check_saddr) { 1332 sctp_check_saddr(sctp, supp_af, psctp == NULL ? 1333 B_FALSE : B_TRUE, hdrdaddr); 1334 } 1335 ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL); 1336 return (0); 1337 } 1338 1339 init = (sctp_init_chunk_t *)(ich + 1); 1340 ph = (sctp_parm_hdr_t *)(init + 1); 1341 1342 /* params will have already been byteordered when validating */ 1343 while (ph != NULL) { 1344 if (ph->sph_type == htons(PARM_SUPP_ADDRS)) { 1345 int plen; 1346 uint16_t *p; 1347 uint16_t addrtype; 1348 1349 ASSERT(psctp != NULL); 1350 plen = ntohs(ph->sph_len); 1351 p = (uint16_t *)(ph + 1); 1352 while (plen > 0) { 1353 addrtype = ntohs(*p); 1354 switch (addrtype) { 1355 case PARM_ADDR6: 1356 supp_af |= PARM_SUPP_V6; 1357 break; 1358 case PARM_ADDR4: 1359 supp_af |= PARM_SUPP_V4; 1360 break; 1361 default: 1362 break; 1363 } 1364 p++; 1365 plen -= sizeof (*p); 1366 } 1367 } else if (ph->sph_type == htons(PARM_ADDR4)) { 1368 if (remaining >= PARM_ADDR4_LEN) { 1369 in6_addr_t addr; 1370 ipaddr_t ta; 1371 1372 supp_af |= PARM_SUPP_V4; 1373 /* 1374 * Screen out broad/multicasts & loopback. 1375 * If the endpoint only accepts v6 address, 1376 * go to the next one. 1377 * 1378 * Subnet broadcast check is done in 1379 * sctp_add_faddr(). If the address is 1380 * a broadcast address, it won't be added. 1381 */ 1382 bcopy(ph + 1, &ta, sizeof (ta)); 1383 if (ta == 0 || 1384 ta == INADDR_BROADCAST || 1385 ta == htonl(INADDR_LOOPBACK) || 1386 CLASSD(ta) || connp->conn_ipv6_v6only) { 1387 goto next; 1388 } 1389 IN6_INADDR_TO_V4MAPPED((struct in_addr *) 1390 (ph + 1), &addr); 1391 1392 /* Check for duplicate. */ 1393 if (sctp_lookup_faddr(sctp, &addr) != NULL) 1394 goto next; 1395 1396 /* OK, add it to the faddr set */ 1397 err = sctp_add_faddr(sctp, &addr, KM_NOSLEEP, 1398 B_FALSE); 1399 /* Something is wrong... Try the next one. */ 1400 if (err != 0) 1401 goto next; 1402 } 1403 } else if (ph->sph_type == htons(PARM_ADDR6) && 1404 connp->conn_family == AF_INET6) { 1405 /* An v4 socket should not take v6 addresses. */ 1406 if (remaining >= PARM_ADDR6_LEN) { 1407 in6_addr_t *addr6; 1408 1409 supp_af |= PARM_SUPP_V6; 1410 addr6 = (in6_addr_t *)(ph + 1); 1411 /* 1412 * Screen out link locals, mcast, loopback 1413 * and bogus v6 address. 1414 */ 1415 if (IN6_IS_ADDR_LINKLOCAL(addr6) || 1416 IN6_IS_ADDR_MULTICAST(addr6) || 1417 IN6_IS_ADDR_LOOPBACK(addr6) || 1418 IN6_IS_ADDR_V4MAPPED(addr6)) { 1419 goto next; 1420 } 1421 /* Check for duplicate. */ 1422 if (sctp_lookup_faddr(sctp, addr6) != NULL) 1423 goto next; 1424 1425 err = sctp_add_faddr(sctp, 1426 (in6_addr_t *)(ph + 1), KM_NOSLEEP, 1427 B_FALSE); 1428 /* Something is wrong... Try the next one. */ 1429 if (err != 0) 1430 goto next; 1431 } 1432 } else if (ph->sph_type == htons(PARM_FORWARD_TSN)) { 1433 if (sctp_options != NULL) 1434 *sctp_options |= SCTP_PRSCTP_OPTION; 1435 } /* else; skip */ 1436 1437 next: 1438 ph = sctp_next_parm(ph, &remaining); 1439 } 1440 if (check_saddr) { 1441 sctp_check_saddr(sctp, supp_af, psctp == NULL ? B_FALSE : 1442 B_TRUE, hdrdaddr); 1443 } 1444 ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL); 1445 /* 1446 * We have the right address list now, update clustering's 1447 * knowledge because when we sent the INIT we had just added 1448 * the address the INIT was sent to. 1449 */ 1450 if (psctp == NULL && cl_sctp_assoc_change != NULL) { 1451 uchar_t *alist; 1452 size_t asize; 1453 uchar_t *dlist; 1454 size_t dsize; 1455 1456 asize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs; 1457 alist = kmem_alloc(asize, KM_NOSLEEP); 1458 if (alist == NULL) { 1459 SCTP_KSTAT(sctps, sctp_cl_assoc_change); 1460 return (ENOMEM); 1461 } 1462 /* 1463 * Just include the address the INIT was sent to in the 1464 * delete list and send the entire faddr list. We could 1465 * do it differently (i.e include all the addresses in the 1466 * add list even if it contains the original address OR 1467 * remove the original address from the add list etc.), but 1468 * this seems reasonable enough. 1469 */ 1470 dsize = sizeof (in6_addr_t); 1471 dlist = kmem_alloc(dsize, KM_NOSLEEP); 1472 if (dlist == NULL) { 1473 kmem_free(alist, asize); 1474 SCTP_KSTAT(sctps, sctp_cl_assoc_change); 1475 return (ENOMEM); 1476 } 1477 bcopy(&curaddr, dlist, sizeof (curaddr)); 1478 sctp_get_faddr_list(sctp, alist, asize); 1479 (*cl_sctp_assoc_change)(connp->conn_family, alist, asize, 1480 sctp->sctp_nfaddrs, dlist, dsize, 1, SCTP_CL_PADDR, 1481 (cl_sctp_handle_t)sctp); 1482 /* alist and dlist will be freed by the clustering module */ 1483 } 1484 return (0); 1485 } 1486 1487 /* 1488 * Returns 0 if the check failed and the restart should be refused, 1489 * 1 if the check succeeded. 1490 */ 1491 int 1492 sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports, 1493 int sleep, sctp_stack_t *sctps, ip_recv_attr_t *ira) 1494 { 1495 sctp_faddr_t *fp, *fphead = NULL; 1496 sctp_parm_hdr_t *ph; 1497 ssize_t remaining; 1498 int isv4; 1499 ipha_t *iph; 1500 ip6_t *ip6h; 1501 in6_addr_t hdraddr[1]; 1502 int retval = 0; 1503 sctp_tf_t *tf; 1504 sctp_t *sctp; 1505 int compres; 1506 sctp_init_chunk_t *init; 1507 int nadded = 0; 1508 1509 /* extract the address from the IP header */ 1510 isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION); 1511 if (isv4) { 1512 iph = (ipha_t *)pkt->b_rptr; 1513 IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr); 1514 } else { 1515 ip6h = (ip6_t *)pkt->b_rptr; 1516 hdraddr[0] = ip6h->ip6_src; 1517 } 1518 1519 /* Walk the params in the INIT [ACK], pulling out addr params */ 1520 remaining = ntohs(ich->sch_len) - sizeof (*ich) - 1521 sizeof (sctp_init_chunk_t); 1522 if (remaining < sizeof (*ph)) { 1523 /* no parameters; restart OK */ 1524 return (1); 1525 } 1526 init = (sctp_init_chunk_t *)(ich + 1); 1527 ph = (sctp_parm_hdr_t *)(init + 1); 1528 1529 while (ph != NULL) { 1530 sctp_faddr_t *fpa = NULL; 1531 1532 /* params will have already been byteordered when validating */ 1533 if (ph->sph_type == htons(PARM_ADDR4)) { 1534 if (remaining >= PARM_ADDR4_LEN) { 1535 in6_addr_t addr; 1536 IN6_INADDR_TO_V4MAPPED((struct in_addr *) 1537 (ph + 1), &addr); 1538 fpa = kmem_cache_alloc(sctp_kmem_faddr_cache, 1539 sleep); 1540 if (fpa == NULL) { 1541 goto done; 1542 } 1543 bzero(fpa, sizeof (*fpa)); 1544 fpa->faddr = addr; 1545 fpa->next = NULL; 1546 } 1547 } else if (ph->sph_type == htons(PARM_ADDR6)) { 1548 if (remaining >= PARM_ADDR6_LEN) { 1549 fpa = kmem_cache_alloc(sctp_kmem_faddr_cache, 1550 sleep); 1551 if (fpa == NULL) { 1552 goto done; 1553 } 1554 bzero(fpa, sizeof (*fpa)); 1555 bcopy(ph + 1, &fpa->faddr, 1556 sizeof (fpa->faddr)); 1557 fpa->next = NULL; 1558 } 1559 } 1560 /* link in the new addr, if it was an addr param */ 1561 if (fpa != NULL) { 1562 if (fphead == NULL) { 1563 fphead = fpa; 1564 } else { 1565 fpa->next = fphead; 1566 fphead = fpa; 1567 } 1568 } 1569 1570 ph = sctp_next_parm(ph, &remaining); 1571 } 1572 1573 if (fphead == NULL) { 1574 /* no addr parameters; restart OK */ 1575 return (1); 1576 } 1577 1578 /* 1579 * got at least one; make sure the header's addr is 1580 * in the list 1581 */ 1582 fp = sctp_lookup_faddr_nosctp(fphead, hdraddr); 1583 if (fp == NULL) { 1584 /* not included; add it now */ 1585 fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep); 1586 if (fp == NULL) { 1587 goto done; 1588 } 1589 bzero(fp, sizeof (*fp)); 1590 fp->faddr = *hdraddr; 1591 fp->next = fphead; 1592 fphead = fp; 1593 } 1594 1595 /* 1596 * Now, we can finally do the check: For each sctp instance 1597 * on the hash line for ports, compare its faddr set against 1598 * the new one. If the new one is a strict subset of any 1599 * existing sctp's faddrs, the restart is OK. However, if there 1600 * is an overlap, this could be an attack, so return failure. 1601 * If all sctp's faddrs are disjoint, this is a legitimate new 1602 * association. 1603 */ 1604 tf = &(sctps->sctps_conn_fanout[SCTP_CONN_HASH(sctps, ports)]); 1605 mutex_enter(&tf->tf_lock); 1606 1607 for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) { 1608 if (ports != sctp->sctp_connp->conn_ports) { 1609 continue; 1610 } 1611 compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs); 1612 if (compres <= SCTP_ADDR_SUBSET) { 1613 retval = 1; 1614 mutex_exit(&tf->tf_lock); 1615 goto done; 1616 } 1617 if (compres == SCTP_ADDR_OVERLAP) { 1618 dprint(1, 1619 ("new assoc from %x:%x:%x:%x overlaps with %p\n", 1620 SCTP_PRINTADDR(*hdraddr), (void *)sctp)); 1621 /* 1622 * While we still hold the lock, we need to 1623 * figure out which addresses have been 1624 * added so we can include them in the abort 1625 * we will send back. Since these faddrs will 1626 * never be used, we overload the rto field 1627 * here, setting it to 0 if the address was 1628 * not added, 1 if it was added. 1629 */ 1630 for (fp = fphead; fp; fp = fp->next) { 1631 if (sctp_lookup_faddr(sctp, &fp->faddr)) { 1632 fp->rto = 0; 1633 } else { 1634 fp->rto = 1; 1635 nadded++; 1636 } 1637 } 1638 mutex_exit(&tf->tf_lock); 1639 goto done; 1640 } 1641 } 1642 mutex_exit(&tf->tf_lock); 1643 1644 /* All faddrs are disjoint; legit new association */ 1645 retval = 1; 1646 1647 done: 1648 /* If are attempted adds, send back an abort listing the addrs */ 1649 if (nadded > 0) { 1650 void *dtail; 1651 size_t dlen; 1652 1653 dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP); 1654 if (dtail == NULL) { 1655 goto cleanup; 1656 } 1657 1658 ph = dtail; 1659 dlen = 0; 1660 for (fp = fphead; fp; fp = fp->next) { 1661 if (fp->rto == 0) { 1662 continue; 1663 } 1664 if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) { 1665 ipaddr_t addr4; 1666 1667 ph->sph_type = htons(PARM_ADDR4); 1668 ph->sph_len = htons(PARM_ADDR4_LEN); 1669 IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4); 1670 ph++; 1671 bcopy(&addr4, ph, sizeof (addr4)); 1672 ph = (sctp_parm_hdr_t *) 1673 ((char *)ph + sizeof (addr4)); 1674 dlen += PARM_ADDR4_LEN; 1675 } else { 1676 ph->sph_type = htons(PARM_ADDR6); 1677 ph->sph_len = htons(PARM_ADDR6_LEN); 1678 ph++; 1679 bcopy(&fp->faddr, ph, sizeof (fp->faddr)); 1680 ph = (sctp_parm_hdr_t *) 1681 ((char *)ph + sizeof (fp->faddr)); 1682 dlen += PARM_ADDR6_LEN; 1683 } 1684 } 1685 1686 /* Send off the abort */ 1687 sctp_send_abort(sctp, sctp_init2vtag(ich), 1688 SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE, 1689 ira); 1690 1691 kmem_free(dtail, PARM_ADDR6_LEN * nadded); 1692 } 1693 1694 cleanup: 1695 /* Clean up */ 1696 if (fphead) { 1697 sctp_faddr_t *fpn; 1698 for (fp = fphead; fp; fp = fpn) { 1699 fpn = fp->next; 1700 if (fp->ixa != NULL) { 1701 ixa_refrele(fp->ixa); 1702 fp->ixa = NULL; 1703 } 1704 kmem_cache_free(sctp_kmem_faddr_cache, fp); 1705 } 1706 } 1707 1708 return (retval); 1709 } 1710 1711 /* 1712 * Reset any state related to transmitted chunks. 1713 */ 1714 void 1715 sctp_congest_reset(sctp_t *sctp) 1716 { 1717 sctp_faddr_t *fp; 1718 sctp_stack_t *sctps = sctp->sctp_sctps; 1719 mblk_t *mp; 1720 1721 for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 1722 fp->ssthresh = sctps->sctps_initial_mtu; 1723 SET_CWND(fp, fp->sfa_pmss, sctps->sctps_slow_start_initial); 1724 fp->suna = 0; 1725 fp->pba = 0; 1726 } 1727 /* 1728 * Clean up the transmit list as well since we have reset accounting 1729 * on all the fps. Send event upstream, if required. 1730 */ 1731 while ((mp = sctp->sctp_xmit_head) != NULL) { 1732 sctp->sctp_xmit_head = mp->b_next; 1733 mp->b_next = NULL; 1734 if (sctp->sctp_xmit_head != NULL) 1735 sctp->sctp_xmit_head->b_prev = NULL; 1736 sctp_sendfail_event(sctp, mp, 0, B_TRUE); 1737 } 1738 sctp->sctp_xmit_head = NULL; 1739 sctp->sctp_xmit_tail = NULL; 1740 sctp->sctp_xmit_unacked = NULL; 1741 1742 sctp->sctp_unacked = 0; 1743 /* 1744 * Any control message as well. We will clean-up this list as well. 1745 * This contains any pending ASCONF request that we have queued/sent. 1746 * If we do get an ACK we will just drop it. However, given that 1747 * we are restarting chances are we aren't going to get any. 1748 */ 1749 if (sctp->sctp_cxmit_list != NULL) 1750 sctp_asconf_free_cxmit(sctp, NULL); 1751 sctp->sctp_cxmit_list = NULL; 1752 sctp->sctp_cchunk_pend = 0; 1753 1754 sctp->sctp_rexmitting = B_FALSE; 1755 sctp->sctp_rxt_nxttsn = 0; 1756 sctp->sctp_rxt_maxtsn = 0; 1757 1758 sctp->sctp_zero_win_probe = B_FALSE; 1759 } 1760 1761 static void 1762 sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr, 1763 mblk_t *timer_mp) 1764 { 1765 sctp_stack_t *sctps = sctp->sctp_sctps; 1766 1767 ASSERT(fp->ixa != NULL); 1768 1769 bcopy(addr, &fp->faddr, sizeof (*addr)); 1770 if (IN6_IS_ADDR_V4MAPPED(addr)) { 1771 fp->isv4 = 1; 1772 /* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */ 1773 fp->sfa_pmss = 1774 (sctps->sctps_initial_mtu - sctp->sctp_hdr_len) & 1775 ~(SCTP_ALIGN - 1); 1776 fp->ixa->ixa_flags |= IXAF_IS_IPV4; 1777 } else { 1778 fp->isv4 = 0; 1779 fp->sfa_pmss = 1780 (sctps->sctps_initial_mtu - sctp->sctp_hdr6_len) & 1781 ~(SCTP_ALIGN - 1); 1782 fp->ixa->ixa_flags &= ~IXAF_IS_IPV4; 1783 } 1784 fp->cwnd = sctps->sctps_slow_start_initial * fp->sfa_pmss; 1785 fp->rto = MIN(sctp->sctp_rto_initial, sctp->sctp_rto_max_init); 1786 SCTP_MAX_RTO(sctp, fp); 1787 fp->srtt = -1; 1788 fp->rtt_updates = 0; 1789 fp->strikes = 0; 1790 fp->max_retr = sctp->sctp_pp_max_rxt; 1791 /* Mark it as not confirmed. */ 1792 fp->state = SCTP_FADDRS_UNCONFIRMED; 1793 fp->hb_interval = sctp->sctp_hb_interval; 1794 fp->ssthresh = sctps->sctps_initial_ssthresh; 1795 fp->suna = 0; 1796 fp->pba = 0; 1797 fp->acked = 0; 1798 fp->lastactive = fp->hb_expiry = ddi_get_lbolt64(); 1799 fp->timer_mp = timer_mp; 1800 fp->hb_pending = B_FALSE; 1801 fp->hb_enabled = B_TRUE; 1802 fp->df = 1; 1803 fp->pmtu_discovered = 0; 1804 fp->next = NULL; 1805 fp->T3expire = 0; 1806 (void) random_get_pseudo_bytes((uint8_t *)&fp->hb_secret, 1807 sizeof (fp->hb_secret)); 1808 fp->rxt_unacked = 0; 1809 1810 sctp_get_dest(sctp, fp); 1811 } 1812 1813 /*ARGSUSED*/ 1814 static int 1815 faddr_constructor(void *buf, void *arg, int flags) 1816 { 1817 sctp_faddr_t *fp = buf; 1818 1819 fp->timer_mp = NULL; 1820 fp->timer_running = 0; 1821 1822 fp->rc_timer_mp = NULL; 1823 fp->rc_timer_running = 0; 1824 1825 return (0); 1826 } 1827 1828 /*ARGSUSED*/ 1829 static void 1830 faddr_destructor(void *buf, void *arg) 1831 { 1832 sctp_faddr_t *fp = buf; 1833 1834 ASSERT(fp->timer_mp == NULL); 1835 ASSERT(fp->timer_running == 0); 1836 1837 ASSERT(fp->rc_timer_mp == NULL); 1838 ASSERT(fp->rc_timer_running == 0); 1839 } 1840 1841 void 1842 sctp_faddr_init(void) 1843 { 1844 sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache", 1845 sizeof (sctp_faddr_t), 0, faddr_constructor, faddr_destructor, 1846 NULL, NULL, NULL, 0); 1847 } 1848 1849 void 1850 sctp_faddr_fini(void) 1851 { 1852 kmem_cache_destroy(sctp_kmem_faddr_cache); 1853 } 1854