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 /* 750 * Find the next live peer address with zero strikes. In case 751 * there is none, find the one with the lowest number of strikes. 752 */ 753 min_strikes = ofp->strikes; 754 nfp = ofp->next; 755 while (nfp != ofp) { 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 nfp = nfp->next; 770 } 771 /* If reached the old address, there is no zero strike path */ 772 if (nfp == ofp) 773 nfp = NULL; 774 775 /* 776 * If there is a peer address with zero strikes we use that, if not 777 * return a peer address with fewer strikes than the one last used, 778 * if neither exist we may as well stay with the old one. 779 */ 780 if (nfp != NULL) 781 return (nfp); 782 if (saved_fp != NULL) 783 return (saved_fp); 784 return (ofp); 785 } 786 787 void 788 sctp_unlink_faddr(sctp_t *sctp, sctp_faddr_t *fp) 789 { 790 sctp_faddr_t *fpp; 791 792 if (!sctp->sctp_faddrs) { 793 return; 794 } 795 796 if (fp->timer_mp != NULL) { 797 sctp_timer_free(fp->timer_mp); 798 fp->timer_mp = NULL; 799 fp->timer_running = 0; 800 } 801 if (fp->rc_timer_mp != NULL) { 802 sctp_timer_free(fp->rc_timer_mp); 803 fp->rc_timer_mp = NULL; 804 fp->rc_timer_running = 0; 805 } 806 if (fp->ixa != NULL) { 807 ixa_refrele(fp->ixa); 808 fp->ixa = NULL; 809 } 810 811 if (fp == sctp->sctp_faddrs) { 812 goto gotit; 813 } 814 815 for (fpp = sctp->sctp_faddrs; fpp->next != fp; fpp = fpp->next) 816 ; 817 818 gotit: 819 ASSERT(sctp->sctp_conn_tfp != NULL); 820 mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 821 if (fp == sctp->sctp_faddrs) { 822 sctp->sctp_faddrs = fp->next; 823 } else { 824 fpp->next = fp->next; 825 } 826 mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 827 kmem_cache_free(sctp_kmem_faddr_cache, fp); 828 sctp->sctp_nfaddrs--; 829 } 830 831 void 832 sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock) 833 { 834 sctp_faddr_t *fp, *fpn; 835 836 if (sctp->sctp_faddrs == NULL) { 837 ASSERT(sctp->sctp_lastfaddr == NULL); 838 return; 839 } 840 841 ASSERT(sctp->sctp_lastfaddr != NULL); 842 sctp->sctp_lastfaddr = NULL; 843 sctp->sctp_current = NULL; 844 sctp->sctp_primary = NULL; 845 846 sctp_free_faddr_timers(sctp); 847 848 if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) { 849 /* in conn fanout; need to hold lock */ 850 mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 851 } 852 853 for (fp = sctp->sctp_faddrs; fp; fp = fpn) { 854 fpn = fp->next; 855 if (fp->ixa != NULL) { 856 ixa_refrele(fp->ixa); 857 fp->ixa = NULL; 858 } 859 kmem_cache_free(sctp_kmem_faddr_cache, fp); 860 sctp->sctp_nfaddrs--; 861 } 862 863 sctp->sctp_faddrs = NULL; 864 ASSERT(sctp->sctp_nfaddrs == 0); 865 if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) { 866 mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 867 } 868 869 } 870 871 void 872 sctp_zap_addrs(sctp_t *sctp) 873 { 874 sctp_zap_faddrs(sctp, 0); 875 sctp_free_saddrs(sctp); 876 } 877 878 /* 879 * Build two SCTP header templates; one for IPv4 and one for IPv6. 880 * Store them in sctp_iphc and sctp_iphc6 respectively (and related fields). 881 * There are no IP addresses in the templates, but the port numbers and 882 * verifier are field in from the conn_t and sctp_t. 883 * 884 * Returns failure if can't allocate memory, or if there is a problem 885 * with a routing header/option. 886 * 887 * We allocate space for the minimum sctp header (sctp_hdr_t). 888 * 889 * We massage an routing option/header. There is no checksum implication 890 * for a routing header for sctp. 891 * 892 * Caller needs to update conn_wroff if desired. 893 * 894 * TSol notes: This assumes that a SCTP association has a single peer label 895 * since we only track a single pair of ipp_label_v4/v6 and not a separate one 896 * for each faddr. 897 */ 898 int 899 sctp_build_hdrs(sctp_t *sctp, int sleep) 900 { 901 conn_t *connp = sctp->sctp_connp; 902 ip_pkt_t *ipp = &connp->conn_xmit_ipp; 903 uint_t ip_hdr_length; 904 uchar_t *hdrs; 905 uint_t hdrs_len; 906 uint_t ulp_hdr_length = sizeof (sctp_hdr_t); 907 ipha_t *ipha; 908 ip6_t *ip6h; 909 sctp_hdr_t *sctph; 910 in6_addr_t v6src, v6dst; 911 ipaddr_t v4src, v4dst; 912 913 v4src = connp->conn_saddr_v4; 914 v4dst = connp->conn_faddr_v4; 915 v6src = connp->conn_saddr_v6; 916 v6dst = connp->conn_faddr_v6; 917 918 /* First do IPv4 header */ 919 ip_hdr_length = ip_total_hdrs_len_v4(ipp); 920 921 /* In case of TX label and IP options it can be too much */ 922 if (ip_hdr_length > IP_MAX_HDR_LENGTH) { 923 /* Preserves existing TX errno for this */ 924 return (EHOSTUNREACH); 925 } 926 hdrs_len = ip_hdr_length + ulp_hdr_length; 927 ASSERT(hdrs_len != 0); 928 929 if (hdrs_len != sctp->sctp_iphc_len) { 930 /* Allocate new before we free any old */ 931 hdrs = kmem_alloc(hdrs_len, sleep); 932 if (hdrs == NULL) 933 return (ENOMEM); 934 935 if (sctp->sctp_iphc != NULL) 936 kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len); 937 sctp->sctp_iphc = hdrs; 938 sctp->sctp_iphc_len = hdrs_len; 939 } else { 940 hdrs = sctp->sctp_iphc; 941 } 942 sctp->sctp_hdr_len = sctp->sctp_iphc_len; 943 sctp->sctp_ip_hdr_len = ip_hdr_length; 944 945 sctph = (sctp_hdr_t *)(hdrs + ip_hdr_length); 946 sctp->sctp_sctph = sctph; 947 sctph->sh_sport = connp->conn_lport; 948 sctph->sh_dport = connp->conn_fport; 949 sctph->sh_verf = sctp->sctp_fvtag; 950 sctph->sh_chksum = 0; 951 952 ipha = (ipha_t *)hdrs; 953 sctp->sctp_ipha = ipha; 954 955 ipha->ipha_src = v4src; 956 ipha->ipha_dst = v4dst; 957 ip_build_hdrs_v4(hdrs, ip_hdr_length, ipp, connp->conn_proto); 958 ipha->ipha_length = htons(hdrs_len); 959 ipha->ipha_fragment_offset_and_flags = 0; 960 961 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) 962 (void) ip_massage_options(ipha, connp->conn_netstack); 963 964 /* Now IPv6 */ 965 ip_hdr_length = ip_total_hdrs_len_v6(ipp); 966 hdrs_len = ip_hdr_length + ulp_hdr_length; 967 ASSERT(hdrs_len != 0); 968 969 if (hdrs_len != sctp->sctp_iphc6_len) { 970 /* Allocate new before we free any old */ 971 hdrs = kmem_alloc(hdrs_len, sleep); 972 if (hdrs == NULL) 973 return (ENOMEM); 974 975 if (sctp->sctp_iphc6 != NULL) 976 kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len); 977 sctp->sctp_iphc6 = hdrs; 978 sctp->sctp_iphc6_len = hdrs_len; 979 } else { 980 hdrs = sctp->sctp_iphc6; 981 } 982 sctp->sctp_hdr6_len = sctp->sctp_iphc6_len; 983 sctp->sctp_ip_hdr6_len = ip_hdr_length; 984 985 sctph = (sctp_hdr_t *)(hdrs + ip_hdr_length); 986 sctp->sctp_sctph6 = sctph; 987 sctph->sh_sport = connp->conn_lport; 988 sctph->sh_dport = connp->conn_fport; 989 sctph->sh_verf = sctp->sctp_fvtag; 990 sctph->sh_chksum = 0; 991 992 ip6h = (ip6_t *)hdrs; 993 sctp->sctp_ip6h = ip6h; 994 995 ip6h->ip6_src = v6src; 996 ip6h->ip6_dst = v6dst; 997 ip_build_hdrs_v6(hdrs, ip_hdr_length, ipp, connp->conn_proto, 998 connp->conn_flowinfo); 999 ip6h->ip6_plen = htons(hdrs_len - IPV6_HDR_LEN); 1000 1001 if (ipp->ipp_fields & IPPF_RTHDR) { 1002 uint8_t *end; 1003 ip6_rthdr_t *rth; 1004 1005 end = (uint8_t *)ip6h + ip_hdr_length; 1006 rth = ip_find_rthdr_v6(ip6h, end); 1007 if (rth != NULL) { 1008 (void) ip_massage_options_v6(ip6h, rth, 1009 connp->conn_netstack); 1010 } 1011 1012 /* 1013 * Verify that the first hop isn't a mapped address. 1014 * Routers along the path need to do this verification 1015 * for subsequent hops. 1016 */ 1017 if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) 1018 return (EADDRNOTAVAIL); 1019 } 1020 return (0); 1021 } 1022 1023 static int 1024 sctp_v4_label(sctp_t *sctp, sctp_faddr_t *fp) 1025 { 1026 conn_t *connp = sctp->sctp_connp; 1027 1028 ASSERT(fp->ixa->ixa_flags & IXAF_IS_IPV4); 1029 return (conn_update_label(connp, fp->ixa, &fp->faddr, 1030 &connp->conn_xmit_ipp)); 1031 } 1032 1033 static int 1034 sctp_v6_label(sctp_t *sctp, sctp_faddr_t *fp) 1035 { 1036 conn_t *connp = sctp->sctp_connp; 1037 1038 ASSERT(!(fp->ixa->ixa_flags & IXAF_IS_IPV4)); 1039 return (conn_update_label(connp, fp->ixa, &fp->faddr, 1040 &connp->conn_xmit_ipp)); 1041 } 1042 1043 /* 1044 * XXX implement more sophisticated logic 1045 * 1046 * Tsol note: We have already verified the addresses using tsol_check_dest 1047 * in sctp_add_faddr, thus no need to redo that here. 1048 * We do setup ipp_label_v4 and ipp_label_v6 based on which addresses 1049 * we have. 1050 */ 1051 int 1052 sctp_set_hdraddrs(sctp_t *sctp) 1053 { 1054 sctp_faddr_t *fp; 1055 int gotv4 = 0; 1056 int gotv6 = 0; 1057 conn_t *connp = sctp->sctp_connp; 1058 1059 ASSERT(sctp->sctp_faddrs != NULL); 1060 ASSERT(sctp->sctp_nsaddrs > 0); 1061 1062 /* Set up using the primary first */ 1063 connp->conn_faddr_v6 = sctp->sctp_primary->faddr; 1064 /* saddr may be unspec; make_mp() will handle this */ 1065 connp->conn_saddr_v6 = sctp->sctp_primary->saddr; 1066 connp->conn_laddr_v6 = connp->conn_saddr_v6; 1067 if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->faddr)) { 1068 if (!is_system_labeled() || 1069 sctp_v4_label(sctp, sctp->sctp_primary) == 0) { 1070 gotv4 = 1; 1071 if (connp->conn_family == AF_INET) { 1072 goto done; 1073 } 1074 } 1075 } else { 1076 if (!is_system_labeled() || 1077 sctp_v6_label(sctp, sctp->sctp_primary) == 0) { 1078 gotv6 = 1; 1079 } 1080 } 1081 1082 for (fp = sctp->sctp_faddrs; fp; fp = fp->next) { 1083 if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->faddr)) { 1084 if (!is_system_labeled() || 1085 sctp_v4_label(sctp, fp) == 0) { 1086 gotv4 = 1; 1087 if (connp->conn_family == AF_INET || gotv6) { 1088 break; 1089 } 1090 } 1091 } else if (!gotv6 && !IN6_IS_ADDR_V4MAPPED(&fp->faddr)) { 1092 if (!is_system_labeled() || 1093 sctp_v6_label(sctp, fp) == 0) { 1094 gotv6 = 1; 1095 if (gotv4) 1096 break; 1097 } 1098 } 1099 } 1100 1101 done: 1102 if (!gotv4 && !gotv6) 1103 return (EACCES); 1104 1105 return (0); 1106 } 1107 1108 /* 1109 * got_errchunk is set B_TRUE only if called from validate_init_params(), when 1110 * an ERROR chunk is already prepended the size of which needs updating for 1111 * additional unrecognized parameters. Other callers either prepend the ERROR 1112 * chunk with the correct size after calling this function, or they are calling 1113 * to add an invalid parameter to an INIT_ACK chunk, in that case no ERROR chunk 1114 * exists, the CAUSE blocks go into the INIT_ACK directly. 1115 * 1116 * *errmp will be non-NULL both when adding an additional CAUSE block to an 1117 * existing prepended COOKIE ERROR chunk (processing params of an INIT_ACK), 1118 * and when adding unrecognized parameters after the first, to an INIT_ACK 1119 * (processing params of an INIT chunk). 1120 */ 1121 void 1122 sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp, 1123 boolean_t got_errchunk) 1124 { 1125 mblk_t *mp; 1126 sctp_parm_hdr_t *ph; 1127 size_t len; 1128 int pad; 1129 sctp_chunk_hdr_t *ecp; 1130 1131 len = sizeof (*ph) + ntohs(uph->sph_len); 1132 if ((pad = len % SCTP_ALIGN) != 0) { 1133 pad = SCTP_ALIGN - pad; 1134 len += pad; 1135 } 1136 mp = allocb(len, BPRI_MED); 1137 if (mp == NULL) { 1138 return; 1139 } 1140 1141 ph = (sctp_parm_hdr_t *)(mp->b_rptr); 1142 ph->sph_type = htons(PARM_UNRECOGNIZED); 1143 ph->sph_len = htons(len - pad); 1144 1145 /* copy in the unrecognized parameter */ 1146 bcopy(uph, ph + 1, ntohs(uph->sph_len)); 1147 1148 if (pad != 0) 1149 bzero((mp->b_rptr + len - pad), pad); 1150 1151 mp->b_wptr = mp->b_rptr + len; 1152 if (*errmp != NULL) { 1153 /* 1154 * Update total length if an ERROR chunk, then link 1155 * this CAUSE block to the possible chain of CAUSE 1156 * blocks attached to the ERROR chunk or INIT_ACK 1157 * being created. 1158 */ 1159 if (got_errchunk) { 1160 /* ERROR chunk already prepended */ 1161 ecp = (sctp_chunk_hdr_t *)((*errmp)->b_rptr); 1162 ecp->sch_len = htons(ntohs(ecp->sch_len) + len); 1163 } 1164 linkb(*errmp, mp); 1165 } else { 1166 *errmp = mp; 1167 } 1168 } 1169 1170 /* 1171 * o Bounds checking 1172 * o Updates remaining 1173 * o Checks alignment 1174 */ 1175 sctp_parm_hdr_t * 1176 sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining) 1177 { 1178 int pad; 1179 uint16_t len; 1180 1181 len = ntohs(current->sph_len); 1182 *remaining -= len; 1183 if (*remaining < sizeof (*current) || len < sizeof (*current)) { 1184 return (NULL); 1185 } 1186 if ((pad = len & (SCTP_ALIGN - 1)) != 0) { 1187 pad = SCTP_ALIGN - pad; 1188 *remaining -= pad; 1189 } 1190 /*LINTED pointer cast may result in improper alignment*/ 1191 current = (sctp_parm_hdr_t *)((char *)current + len + pad); 1192 return (current); 1193 } 1194 1195 /* 1196 * Sets the address parameters given in the INIT chunk into sctp's 1197 * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are 1198 * no address parameters in the INIT chunk, a single faddr is created 1199 * from the ip hdr at the beginning of pkt. 1200 * If there already are existing addresses hanging from sctp, merge 1201 * them in, if the old info contains addresses which are not present 1202 * in this new info, get rid of them, and clean the pointers if there's 1203 * messages which have this as their target address. 1204 * 1205 * We also re-adjust the source address list here since the list may 1206 * contain more than what is actually part of the association. If 1207 * we get here from sctp_send_cookie_echo(), we are on the active 1208 * side and psctp will be NULL and ich will be the INIT-ACK chunk. 1209 * If we get here from sctp_accept_comm(), ich will be the INIT chunk 1210 * and psctp will the listening endpoint. 1211 * 1212 * INIT processing: When processing the INIT we inherit the src address 1213 * list from the listener. For a loopback or linklocal association, we 1214 * delete the list and just take the address from the IP header (since 1215 * that's how we created the INIT-ACK). Additionally, for loopback we 1216 * ignore the address params in the INIT. For determining which address 1217 * types were sent in the INIT-ACK we follow the same logic as in 1218 * creating the INIT-ACK. We delete addresses of the type that are not 1219 * supported by the peer. 1220 * 1221 * INIT-ACK processing: When processing the INIT-ACK since we had not 1222 * included addr params for loopback or linklocal addresses when creating 1223 * the INIT, we just use the address from the IP header. Further, for 1224 * loopback we ignore the addr param list. We mark addresses of the 1225 * type not supported by the peer as unconfirmed. 1226 * 1227 * In case of INIT processing we look for supported address types in the 1228 * supported address param, if present. In both cases the address type in 1229 * the IP header is supported as well as types for addresses in the param 1230 * list, if any. 1231 * 1232 * Once we have the supported address types sctp_check_saddr() runs through 1233 * the source address list and deletes or marks as unconfirmed address of 1234 * types not supported by the peer. 1235 * 1236 * Returns 0 on success, sys errno on failure 1237 */ 1238 int 1239 sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt, 1240 sctp_chunk_hdr_t *ich, uint_t *sctp_options) 1241 { 1242 sctp_init_chunk_t *init; 1243 ipha_t *iph; 1244 ip6_t *ip6h; 1245 in6_addr_t hdrsaddr[1]; 1246 in6_addr_t hdrdaddr[1]; 1247 sctp_parm_hdr_t *ph; 1248 ssize_t remaining; 1249 int isv4; 1250 int err; 1251 sctp_faddr_t *fp; 1252 int supp_af = 0; 1253 boolean_t check_saddr = B_TRUE; 1254 in6_addr_t curaddr; 1255 sctp_stack_t *sctps = sctp->sctp_sctps; 1256 conn_t *connp = sctp->sctp_connp; 1257 1258 if (sctp_options != NULL) 1259 *sctp_options = 0; 1260 1261 /* extract the address from the IP header */ 1262 isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION); 1263 if (isv4) { 1264 iph = (ipha_t *)pkt->b_rptr; 1265 IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdrsaddr); 1266 IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, hdrdaddr); 1267 supp_af |= PARM_SUPP_V4; 1268 } else { 1269 ip6h = (ip6_t *)pkt->b_rptr; 1270 hdrsaddr[0] = ip6h->ip6_src; 1271 hdrdaddr[0] = ip6h->ip6_dst; 1272 supp_af |= PARM_SUPP_V6; 1273 } 1274 1275 /* 1276 * Unfortunately, we can't delay this because adding an faddr 1277 * looks for the presence of the source address (from the ire 1278 * for the faddr) in the source address list. We could have 1279 * delayed this if, say, this was a loopback/linklocal connection. 1280 * Now, we just end up nuking this list and taking the addr from 1281 * the IP header for loopback/linklocal. 1282 */ 1283 if (psctp != NULL && psctp->sctp_nsaddrs > 0) { 1284 ASSERT(sctp->sctp_nsaddrs == 0); 1285 1286 err = sctp_dup_saddrs(psctp, sctp, KM_NOSLEEP); 1287 if (err != 0) 1288 return (err); 1289 } 1290 /* 1291 * We will add the faddr before parsing the address list as this 1292 * might be a loopback connection and we would not have to 1293 * go through the list. 1294 * 1295 * Make sure the header's addr is in the list 1296 */ 1297 fp = sctp_lookup_faddr(sctp, hdrsaddr); 1298 if (fp == NULL) { 1299 /* not included; add it now */ 1300 err = sctp_add_faddr(sctp, hdrsaddr, KM_NOSLEEP, B_TRUE); 1301 if (err != 0) 1302 return (err); 1303 1304 /* sctp_faddrs will be the hdr addr */ 1305 fp = sctp->sctp_faddrs; 1306 } 1307 /* make the header addr the primary */ 1308 1309 if (cl_sctp_assoc_change != NULL && psctp == NULL) 1310 curaddr = sctp->sctp_current->faddr; 1311 1312 sctp->sctp_primary = fp; 1313 sctp->sctp_current = fp; 1314 sctp->sctp_mss = fp->sfa_pmss; 1315 1316 /* For loopback connections & linklocal get address from the header */ 1317 if (sctp->sctp_loopback || sctp->sctp_linklocal) { 1318 if (sctp->sctp_nsaddrs != 0) 1319 sctp_free_saddrs(sctp); 1320 if ((err = sctp_saddr_add_addr(sctp, hdrdaddr, 0)) != 0) 1321 return (err); 1322 /* For loopback ignore address list */ 1323 if (sctp->sctp_loopback) 1324 return (0); 1325 check_saddr = B_FALSE; 1326 } 1327 1328 /* Walk the params in the INIT [ACK], pulling out addr params */ 1329 remaining = ntohs(ich->sch_len) - sizeof (*ich) - 1330 sizeof (sctp_init_chunk_t); 1331 if (remaining < sizeof (*ph)) { 1332 if (check_saddr) { 1333 sctp_check_saddr(sctp, supp_af, psctp == NULL ? 1334 B_FALSE : B_TRUE, hdrdaddr); 1335 } 1336 ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL); 1337 return (0); 1338 } 1339 1340 init = (sctp_init_chunk_t *)(ich + 1); 1341 ph = (sctp_parm_hdr_t *)(init + 1); 1342 1343 /* params will have already been byteordered when validating */ 1344 while (ph != NULL) { 1345 if (ph->sph_type == htons(PARM_SUPP_ADDRS)) { 1346 int plen; 1347 uint16_t *p; 1348 uint16_t addrtype; 1349 1350 ASSERT(psctp != NULL); 1351 plen = ntohs(ph->sph_len); 1352 p = (uint16_t *)(ph + 1); 1353 while (plen > 0) { 1354 addrtype = ntohs(*p); 1355 switch (addrtype) { 1356 case PARM_ADDR6: 1357 supp_af |= PARM_SUPP_V6; 1358 break; 1359 case PARM_ADDR4: 1360 supp_af |= PARM_SUPP_V4; 1361 break; 1362 default: 1363 break; 1364 } 1365 p++; 1366 plen -= sizeof (*p); 1367 } 1368 } else if (ph->sph_type == htons(PARM_ADDR4)) { 1369 if (remaining >= PARM_ADDR4_LEN) { 1370 in6_addr_t addr; 1371 ipaddr_t ta; 1372 1373 supp_af |= PARM_SUPP_V4; 1374 /* 1375 * Screen out broad/multicasts & loopback. 1376 * If the endpoint only accepts v6 address, 1377 * go to the next one. 1378 * 1379 * Subnet broadcast check is done in 1380 * sctp_add_faddr(). If the address is 1381 * a broadcast address, it won't be added. 1382 */ 1383 bcopy(ph + 1, &ta, sizeof (ta)); 1384 if (ta == 0 || 1385 ta == INADDR_BROADCAST || 1386 ta == htonl(INADDR_LOOPBACK) || 1387 CLASSD(ta) || connp->conn_ipv6_v6only) { 1388 goto next; 1389 } 1390 IN6_INADDR_TO_V4MAPPED((struct in_addr *) 1391 (ph + 1), &addr); 1392 1393 /* Check for duplicate. */ 1394 if (sctp_lookup_faddr(sctp, &addr) != NULL) 1395 goto next; 1396 1397 /* OK, add it to the faddr set */ 1398 err = sctp_add_faddr(sctp, &addr, KM_NOSLEEP, 1399 B_FALSE); 1400 /* Something is wrong... Try the next one. */ 1401 if (err != 0) 1402 goto next; 1403 } 1404 } else if (ph->sph_type == htons(PARM_ADDR6) && 1405 connp->conn_family == AF_INET6) { 1406 /* An v4 socket should not take v6 addresses. */ 1407 if (remaining >= PARM_ADDR6_LEN) { 1408 in6_addr_t *addr6; 1409 1410 supp_af |= PARM_SUPP_V6; 1411 addr6 = (in6_addr_t *)(ph + 1); 1412 /* 1413 * Screen out link locals, mcast, loopback 1414 * and bogus v6 address. 1415 */ 1416 if (IN6_IS_ADDR_LINKLOCAL(addr6) || 1417 IN6_IS_ADDR_MULTICAST(addr6) || 1418 IN6_IS_ADDR_LOOPBACK(addr6) || 1419 IN6_IS_ADDR_V4MAPPED(addr6)) { 1420 goto next; 1421 } 1422 /* Check for duplicate. */ 1423 if (sctp_lookup_faddr(sctp, addr6) != NULL) 1424 goto next; 1425 1426 err = sctp_add_faddr(sctp, 1427 (in6_addr_t *)(ph + 1), KM_NOSLEEP, 1428 B_FALSE); 1429 /* Something is wrong... Try the next one. */ 1430 if (err != 0) 1431 goto next; 1432 } 1433 } else if (ph->sph_type == htons(PARM_FORWARD_TSN)) { 1434 if (sctp_options != NULL) 1435 *sctp_options |= SCTP_PRSCTP_OPTION; 1436 } /* else; skip */ 1437 1438 next: 1439 ph = sctp_next_parm(ph, &remaining); 1440 } 1441 if (check_saddr) { 1442 sctp_check_saddr(sctp, supp_af, psctp == NULL ? B_FALSE : 1443 B_TRUE, hdrdaddr); 1444 } 1445 ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL); 1446 /* 1447 * We have the right address list now, update clustering's 1448 * knowledge because when we sent the INIT we had just added 1449 * the address the INIT was sent to. 1450 */ 1451 if (psctp == NULL && cl_sctp_assoc_change != NULL) { 1452 uchar_t *alist; 1453 size_t asize; 1454 uchar_t *dlist; 1455 size_t dsize; 1456 1457 asize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs; 1458 alist = kmem_alloc(asize, KM_NOSLEEP); 1459 if (alist == NULL) { 1460 SCTP_KSTAT(sctps, sctp_cl_assoc_change); 1461 return (ENOMEM); 1462 } 1463 /* 1464 * Just include the address the INIT was sent to in the 1465 * delete list and send the entire faddr list. We could 1466 * do it differently (i.e include all the addresses in the 1467 * add list even if it contains the original address OR 1468 * remove the original address from the add list etc.), but 1469 * this seems reasonable enough. 1470 */ 1471 dsize = sizeof (in6_addr_t); 1472 dlist = kmem_alloc(dsize, KM_NOSLEEP); 1473 if (dlist == NULL) { 1474 kmem_free(alist, asize); 1475 SCTP_KSTAT(sctps, sctp_cl_assoc_change); 1476 return (ENOMEM); 1477 } 1478 bcopy(&curaddr, dlist, sizeof (curaddr)); 1479 sctp_get_faddr_list(sctp, alist, asize); 1480 (*cl_sctp_assoc_change)(connp->conn_family, alist, asize, 1481 sctp->sctp_nfaddrs, dlist, dsize, 1, SCTP_CL_PADDR, 1482 (cl_sctp_handle_t)sctp); 1483 /* alist and dlist will be freed by the clustering module */ 1484 } 1485 return (0); 1486 } 1487 1488 /* 1489 * Returns 0 if the check failed and the restart should be refused, 1490 * 1 if the check succeeded. 1491 */ 1492 int 1493 sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports, 1494 int sleep, sctp_stack_t *sctps, ip_recv_attr_t *ira) 1495 { 1496 sctp_faddr_t *fp, *fphead = NULL; 1497 sctp_parm_hdr_t *ph; 1498 ssize_t remaining; 1499 int isv4; 1500 ipha_t *iph; 1501 ip6_t *ip6h; 1502 in6_addr_t hdraddr[1]; 1503 int retval = 0; 1504 sctp_tf_t *tf; 1505 sctp_t *sctp; 1506 int compres; 1507 sctp_init_chunk_t *init; 1508 int nadded = 0; 1509 1510 /* extract the address from the IP header */ 1511 isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION); 1512 if (isv4) { 1513 iph = (ipha_t *)pkt->b_rptr; 1514 IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr); 1515 } else { 1516 ip6h = (ip6_t *)pkt->b_rptr; 1517 hdraddr[0] = ip6h->ip6_src; 1518 } 1519 1520 /* Walk the params in the INIT [ACK], pulling out addr params */ 1521 remaining = ntohs(ich->sch_len) - sizeof (*ich) - 1522 sizeof (sctp_init_chunk_t); 1523 if (remaining < sizeof (*ph)) { 1524 /* no parameters; restart OK */ 1525 return (1); 1526 } 1527 init = (sctp_init_chunk_t *)(ich + 1); 1528 ph = (sctp_parm_hdr_t *)(init + 1); 1529 1530 while (ph != NULL) { 1531 sctp_faddr_t *fpa = NULL; 1532 1533 /* params will have already been byteordered when validating */ 1534 if (ph->sph_type == htons(PARM_ADDR4)) { 1535 if (remaining >= PARM_ADDR4_LEN) { 1536 in6_addr_t addr; 1537 IN6_INADDR_TO_V4MAPPED((struct in_addr *) 1538 (ph + 1), &addr); 1539 fpa = kmem_cache_alloc(sctp_kmem_faddr_cache, 1540 sleep); 1541 if (fpa == NULL) { 1542 goto done; 1543 } 1544 bzero(fpa, sizeof (*fpa)); 1545 fpa->faddr = addr; 1546 fpa->next = NULL; 1547 } 1548 } else if (ph->sph_type == htons(PARM_ADDR6)) { 1549 if (remaining >= PARM_ADDR6_LEN) { 1550 fpa = kmem_cache_alloc(sctp_kmem_faddr_cache, 1551 sleep); 1552 if (fpa == NULL) { 1553 goto done; 1554 } 1555 bzero(fpa, sizeof (*fpa)); 1556 bcopy(ph + 1, &fpa->faddr, 1557 sizeof (fpa->faddr)); 1558 fpa->next = NULL; 1559 } 1560 } 1561 /* link in the new addr, if it was an addr param */ 1562 if (fpa != NULL) { 1563 if (fphead == NULL) { 1564 fphead = fpa; 1565 } else { 1566 fpa->next = fphead; 1567 fphead = fpa; 1568 } 1569 } 1570 1571 ph = sctp_next_parm(ph, &remaining); 1572 } 1573 1574 if (fphead == NULL) { 1575 /* no addr parameters; restart OK */ 1576 return (1); 1577 } 1578 1579 /* 1580 * got at least one; make sure the header's addr is 1581 * in the list 1582 */ 1583 fp = sctp_lookup_faddr_nosctp(fphead, hdraddr); 1584 if (fp == NULL) { 1585 /* not included; add it now */ 1586 fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep); 1587 if (fp == NULL) { 1588 goto done; 1589 } 1590 bzero(fp, sizeof (*fp)); 1591 fp->faddr = *hdraddr; 1592 fp->next = fphead; 1593 fphead = fp; 1594 } 1595 1596 /* 1597 * Now, we can finally do the check: For each sctp instance 1598 * on the hash line for ports, compare its faddr set against 1599 * the new one. If the new one is a strict subset of any 1600 * existing sctp's faddrs, the restart is OK. However, if there 1601 * is an overlap, this could be an attack, so return failure. 1602 * If all sctp's faddrs are disjoint, this is a legitimate new 1603 * association. 1604 */ 1605 tf = &(sctps->sctps_conn_fanout[SCTP_CONN_HASH(sctps, ports)]); 1606 mutex_enter(&tf->tf_lock); 1607 1608 for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) { 1609 if (ports != sctp->sctp_connp->conn_ports) { 1610 continue; 1611 } 1612 compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs); 1613 if (compres <= SCTP_ADDR_SUBSET) { 1614 retval = 1; 1615 mutex_exit(&tf->tf_lock); 1616 goto done; 1617 } 1618 if (compres == SCTP_ADDR_OVERLAP) { 1619 dprint(1, 1620 ("new assoc from %x:%x:%x:%x overlaps with %p\n", 1621 SCTP_PRINTADDR(*hdraddr), (void *)sctp)); 1622 /* 1623 * While we still hold the lock, we need to 1624 * figure out which addresses have been 1625 * added so we can include them in the abort 1626 * we will send back. Since these faddrs will 1627 * never be used, we overload the rto field 1628 * here, setting it to 0 if the address was 1629 * not added, 1 if it was added. 1630 */ 1631 for (fp = fphead; fp; fp = fp->next) { 1632 if (sctp_lookup_faddr(sctp, &fp->faddr)) { 1633 fp->rto = 0; 1634 } else { 1635 fp->rto = 1; 1636 nadded++; 1637 } 1638 } 1639 mutex_exit(&tf->tf_lock); 1640 goto done; 1641 } 1642 } 1643 mutex_exit(&tf->tf_lock); 1644 1645 /* All faddrs are disjoint; legit new association */ 1646 retval = 1; 1647 1648 done: 1649 /* If are attempted adds, send back an abort listing the addrs */ 1650 if (nadded > 0) { 1651 void *dtail; 1652 size_t dlen; 1653 1654 dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP); 1655 if (dtail == NULL) { 1656 goto cleanup; 1657 } 1658 1659 ph = dtail; 1660 dlen = 0; 1661 for (fp = fphead; fp; fp = fp->next) { 1662 if (fp->rto == 0) { 1663 continue; 1664 } 1665 if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) { 1666 ipaddr_t addr4; 1667 1668 ph->sph_type = htons(PARM_ADDR4); 1669 ph->sph_len = htons(PARM_ADDR4_LEN); 1670 IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4); 1671 ph++; 1672 bcopy(&addr4, ph, sizeof (addr4)); 1673 ph = (sctp_parm_hdr_t *) 1674 ((char *)ph + sizeof (addr4)); 1675 dlen += PARM_ADDR4_LEN; 1676 } else { 1677 ph->sph_type = htons(PARM_ADDR6); 1678 ph->sph_len = htons(PARM_ADDR6_LEN); 1679 ph++; 1680 bcopy(&fp->faddr, ph, sizeof (fp->faddr)); 1681 ph = (sctp_parm_hdr_t *) 1682 ((char *)ph + sizeof (fp->faddr)); 1683 dlen += PARM_ADDR6_LEN; 1684 } 1685 } 1686 1687 /* Send off the abort */ 1688 sctp_send_abort(sctp, sctp_init2vtag(ich), 1689 SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE, 1690 ira); 1691 1692 kmem_free(dtail, PARM_ADDR6_LEN * nadded); 1693 } 1694 1695 cleanup: 1696 /* Clean up */ 1697 if (fphead) { 1698 sctp_faddr_t *fpn; 1699 for (fp = fphead; fp; fp = fpn) { 1700 fpn = fp->next; 1701 if (fp->ixa != NULL) { 1702 ixa_refrele(fp->ixa); 1703 fp->ixa = NULL; 1704 } 1705 kmem_cache_free(sctp_kmem_faddr_cache, fp); 1706 } 1707 } 1708 1709 return (retval); 1710 } 1711 1712 /* 1713 * Reset any state related to transmitted chunks. 1714 */ 1715 void 1716 sctp_congest_reset(sctp_t *sctp) 1717 { 1718 sctp_faddr_t *fp; 1719 sctp_stack_t *sctps = sctp->sctp_sctps; 1720 mblk_t *mp; 1721 1722 for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 1723 fp->ssthresh = sctps->sctps_initial_mtu; 1724 SET_CWND(fp, fp->sfa_pmss, sctps->sctps_slow_start_initial); 1725 fp->suna = 0; 1726 fp->pba = 0; 1727 } 1728 /* 1729 * Clean up the transmit list as well since we have reset accounting 1730 * on all the fps. Send event upstream, if required. 1731 */ 1732 while ((mp = sctp->sctp_xmit_head) != NULL) { 1733 sctp->sctp_xmit_head = mp->b_next; 1734 mp->b_next = NULL; 1735 if (sctp->sctp_xmit_head != NULL) 1736 sctp->sctp_xmit_head->b_prev = NULL; 1737 sctp_sendfail_event(sctp, mp, 0, B_TRUE); 1738 } 1739 sctp->sctp_xmit_head = NULL; 1740 sctp->sctp_xmit_tail = NULL; 1741 sctp->sctp_xmit_unacked = NULL; 1742 1743 sctp->sctp_unacked = 0; 1744 /* 1745 * Any control message as well. We will clean-up this list as well. 1746 * This contains any pending ASCONF request that we have queued/sent. 1747 * If we do get an ACK we will just drop it. However, given that 1748 * we are restarting chances are we aren't going to get any. 1749 */ 1750 if (sctp->sctp_cxmit_list != NULL) 1751 sctp_asconf_free_cxmit(sctp, NULL); 1752 sctp->sctp_cxmit_list = NULL; 1753 sctp->sctp_cchunk_pend = 0; 1754 1755 sctp->sctp_rexmitting = B_FALSE; 1756 sctp->sctp_rxt_nxttsn = 0; 1757 sctp->sctp_rxt_maxtsn = 0; 1758 1759 sctp->sctp_zero_win_probe = B_FALSE; 1760 } 1761 1762 static void 1763 sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr, 1764 mblk_t *timer_mp) 1765 { 1766 sctp_stack_t *sctps = sctp->sctp_sctps; 1767 1768 ASSERT(fp->ixa != NULL); 1769 1770 bcopy(addr, &fp->faddr, sizeof (*addr)); 1771 if (IN6_IS_ADDR_V4MAPPED(addr)) { 1772 fp->isv4 = 1; 1773 /* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */ 1774 fp->sfa_pmss = 1775 (sctps->sctps_initial_mtu - sctp->sctp_hdr_len) & 1776 ~(SCTP_ALIGN - 1); 1777 fp->ixa->ixa_flags |= IXAF_IS_IPV4; 1778 } else { 1779 fp->isv4 = 0; 1780 fp->sfa_pmss = 1781 (sctps->sctps_initial_mtu - sctp->sctp_hdr6_len) & 1782 ~(SCTP_ALIGN - 1); 1783 fp->ixa->ixa_flags &= ~IXAF_IS_IPV4; 1784 } 1785 fp->cwnd = sctps->sctps_slow_start_initial * fp->sfa_pmss; 1786 fp->rto = MIN(sctp->sctp_rto_initial, sctp->sctp_rto_max_init); 1787 SCTP_MAX_RTO(sctp, fp); 1788 fp->srtt = -1; 1789 fp->rtt_updates = 0; 1790 fp->strikes = 0; 1791 fp->max_retr = sctp->sctp_pp_max_rxt; 1792 /* Mark it as not confirmed. */ 1793 fp->state = SCTP_FADDRS_UNCONFIRMED; 1794 fp->hb_interval = sctp->sctp_hb_interval; 1795 fp->ssthresh = sctps->sctps_initial_ssthresh; 1796 fp->suna = 0; 1797 fp->pba = 0; 1798 fp->acked = 0; 1799 fp->lastactive = fp->hb_expiry = ddi_get_lbolt64(); 1800 fp->timer_mp = timer_mp; 1801 fp->hb_pending = B_FALSE; 1802 fp->hb_enabled = B_TRUE; 1803 fp->df = 1; 1804 fp->pmtu_discovered = 0; 1805 fp->next = NULL; 1806 fp->T3expire = 0; 1807 (void) random_get_pseudo_bytes((uint8_t *)&fp->hb_secret, 1808 sizeof (fp->hb_secret)); 1809 fp->rxt_unacked = 0; 1810 1811 sctp_get_dest(sctp, fp); 1812 } 1813 1814 /*ARGSUSED*/ 1815 static int 1816 faddr_constructor(void *buf, void *arg, int flags) 1817 { 1818 sctp_faddr_t *fp = buf; 1819 1820 fp->timer_mp = NULL; 1821 fp->timer_running = 0; 1822 1823 fp->rc_timer_mp = NULL; 1824 fp->rc_timer_running = 0; 1825 1826 return (0); 1827 } 1828 1829 /*ARGSUSED*/ 1830 static void 1831 faddr_destructor(void *buf, void *arg) 1832 { 1833 sctp_faddr_t *fp = buf; 1834 1835 ASSERT(fp->timer_mp == NULL); 1836 ASSERT(fp->timer_running == 0); 1837 1838 ASSERT(fp->rc_timer_mp == NULL); 1839 ASSERT(fp->rc_timer_running == 0); 1840 } 1841 1842 void 1843 sctp_faddr_init(void) 1844 { 1845 sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache", 1846 sizeof (sctp_faddr_t), 0, faddr_constructor, faddr_destructor, 1847 NULL, NULL, NULL, 0); 1848 } 1849 1850 void 1851 sctp_faddr_fini(void) 1852 { 1853 kmem_cache_destroy(sctp_kmem_faddr_cache); 1854 } 1855