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 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/systm.h> 29 #include <sys/stream.h> 30 #include <sys/strsubr.h> 31 #include <sys/ddi.h> 32 #include <sys/sunddi.h> 33 #include <sys/kmem.h> 34 #include <sys/socket.h> 35 #include <sys/random.h> 36 #include <sys/tsol/tndb.h> 37 #include <sys/tsol/tnet.h> 38 39 #include <netinet/in.h> 40 #include <netinet/ip6.h> 41 #include <netinet/sctp.h> 42 43 #include <inet/common.h> 44 #include <inet/ip.h> 45 #include <inet/ip6.h> 46 #include <inet/ip_ire.h> 47 #include <inet/ip_if.h> 48 #include <inet/ip_ndp.h> 49 #include <inet/mib2.h> 50 #include <inet/nd.h> 51 #include <inet/optcom.h> 52 #include <inet/sctp_ip.h> 53 #include <inet/ipclassifier.h> 54 55 #include "sctp_impl.h" 56 #include "sctp_addr.h" 57 #include "sctp_asconf.h" 58 59 static struct kmem_cache *sctp_kmem_faddr_cache; 60 static void sctp_init_faddr(sctp_t *, sctp_faddr_t *, in6_addr_t *, mblk_t *); 61 62 /* Set the source address. Refer to comments in sctp_get_dest(). */ 63 void 64 sctp_set_saddr(sctp_t *sctp, sctp_faddr_t *fp) 65 { 66 boolean_t v6 = !fp->isv4; 67 boolean_t addr_set; 68 69 fp->saddr = sctp_get_valid_addr(sctp, v6, &addr_set); 70 /* 71 * If there is no source address avaialble, mark this peer address 72 * as unreachable for now. When the heartbeat timer fires, it will 73 * call sctp_get_dest() to re-check if there is any source address 74 * available. 75 */ 76 if (!addr_set) 77 fp->state = SCTP_FADDRS_UNREACH; 78 } 79 80 /* 81 * Call this function to get information about a peer addr fp. 82 * 83 * Uses ip_attr_connect to avoid explicit use of ire and source address 84 * selection. 85 */ 86 void 87 sctp_get_dest(sctp_t *sctp, sctp_faddr_t *fp) 88 { 89 in6_addr_t laddr; 90 in6_addr_t nexthop; 91 sctp_saddr_ipif_t *sp; 92 int hdrlen; 93 sctp_stack_t *sctps = sctp->sctp_sctps; 94 conn_t *connp = sctp->sctp_connp; 95 iulp_t uinfo; 96 uint_t pmtu; 97 int error; 98 uint32_t flags = IPDF_VERIFY_DST | IPDF_IPSEC | 99 IPDF_SELECT_SRC | IPDF_UNIQUE_DCE; 100 101 /* 102 * Tell sctp_make_mp it needs to call us again should we not 103 * complete and set the saddr. 104 */ 105 fp->saddr = ipv6_all_zeros; 106 107 /* 108 * If this addr is not reachable, mark it as unconfirmed for now, the 109 * state will be changed back to unreachable later in this function 110 * if it is still the case. 111 */ 112 if (fp->state == SCTP_FADDRS_UNREACH) { 113 fp->state = SCTP_FADDRS_UNCONFIRMED; 114 } 115 116 /* 117 * Socket is connected - enable PMTU discovery. 118 */ 119 if (!sctps->sctps_ignore_path_mtu) 120 fp->ixa->ixa_flags |= IXAF_PMTU_DISCOVERY; 121 122 ip_attr_nexthop(&connp->conn_xmit_ipp, fp->ixa, &fp->faddr, 123 &nexthop); 124 125 laddr = fp->saddr; 126 error = ip_attr_connect(connp, fp->ixa, &laddr, &fp->faddr, &nexthop, 127 connp->conn_fport, &laddr, &uinfo, flags); 128 129 if (error != 0) { 130 dprint(3, ("sctp_get_dest: no ire for %x:%x:%x:%x\n", 131 SCTP_PRINTADDR(fp->faddr))); 132 /* 133 * It is tempting to just leave the src addr 134 * unspecified and let IP figure it out, but we 135 * *cannot* do this, since IP may choose a src addr 136 * that is not part of this association... unless 137 * this sctp has bound to all addrs. So if the dest 138 * lookup fails, try to find one in our src addr 139 * list, unless the sctp has bound to all addrs, in 140 * which case we change the src addr to unspec. 141 * 142 * Note that if this is a v6 endpoint but it does 143 * not have any v4 address at this point (e.g. may 144 * have been deleted), sctp_get_valid_addr() will 145 * return mapped INADDR_ANY. In this case, this 146 * address should be marked not reachable so that 147 * it won't be used to send data. 148 */ 149 sctp_set_saddr(sctp, fp); 150 if (fp->state == SCTP_FADDRS_UNREACH) 151 return; 152 goto check_current; 153 } 154 ASSERT(fp->ixa->ixa_ire != NULL); 155 ASSERT(!(fp->ixa->ixa_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))); 156 157 if (!sctp->sctp_loopback) 158 sctp->sctp_loopback = uinfo.iulp_loopback; 159 160 /* Make sure the laddr is part of this association */ 161 if ((sp = sctp_saddr_lookup(sctp, &laddr, 0)) != NULL && 162 !sp->saddr_ipif_dontsrc) { 163 if (sp->saddr_ipif_unconfirmed == 1) 164 sp->saddr_ipif_unconfirmed = 0; 165 /* We did IPsec policy lookup for laddr already */ 166 fp->saddr = laddr; 167 } else { 168 dprint(2, ("sctp_get_dest: src addr is not part of assoc " 169 "%x:%x:%x:%x\n", SCTP_PRINTADDR(laddr))); 170 171 /* 172 * Set the src to the first saddr and hope for the best. 173 * Note that this case should very seldomly 174 * happen. One scenario this can happen is an app 175 * explicitly bind() to an address. But that address is 176 * not the preferred source address to send to the peer. 177 */ 178 sctp_set_saddr(sctp, fp); 179 if (fp->state == SCTP_FADDRS_UNREACH) { 180 return; 181 } 182 } 183 184 /* 185 * Pull out RTO information for this faddr and use it if we don't 186 * have any yet. 187 */ 188 if (fp->srtt == -1 && uinfo.iulp_rtt != 0) { 189 /* The cached value is in ms. */ 190 fp->srtt = MSEC_TO_TICK(uinfo.iulp_rtt); 191 fp->rttvar = MSEC_TO_TICK(uinfo.iulp_rtt_sd); 192 fp->rto = 3 * fp->srtt; 193 194 /* Bound the RTO by configured min and max values */ 195 if (fp->rto < sctp->sctp_rto_min) { 196 fp->rto = sctp->sctp_rto_min; 197 } 198 if (fp->rto > sctp->sctp_rto_max) { 199 fp->rto = sctp->sctp_rto_max; 200 } 201 SCTP_MAX_RTO(sctp, fp); 202 } 203 pmtu = uinfo.iulp_mtu; 204 205 /* 206 * Record the MTU for this faddr. If the MTU for this faddr has 207 * changed, check if the assc MTU will also change. 208 */ 209 if (fp->isv4) { 210 hdrlen = sctp->sctp_hdr_len; 211 } else { 212 hdrlen = sctp->sctp_hdr6_len; 213 } 214 if ((fp->sfa_pmss + hdrlen) != pmtu) { 215 /* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */ 216 fp->sfa_pmss = (pmtu - hdrlen) & ~(SCTP_ALIGN - 1); 217 if (fp->cwnd < (fp->sfa_pmss * 2)) { 218 SET_CWND(fp, fp->sfa_pmss, 219 sctps->sctps_slow_start_initial); 220 } 221 } 222 223 check_current: 224 if (fp == sctp->sctp_current) 225 sctp_set_faddr_current(sctp, fp); 226 } 227 228 void 229 sctp_update_dce(sctp_t *sctp) 230 { 231 sctp_faddr_t *fp; 232 sctp_stack_t *sctps = sctp->sctp_sctps; 233 iulp_t uinfo; 234 ip_stack_t *ipst = sctps->sctps_netstack->netstack_ip; 235 uint_t ifindex; 236 237 for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 238 bzero(&uinfo, sizeof (uinfo)); 239 /* 240 * Only record the PMTU for this faddr if we actually have 241 * done discovery. This prevents initialized default from 242 * clobbering any real info that IP may have. 243 */ 244 if (fp->pmtu_discovered) { 245 if (fp->isv4) { 246 uinfo.iulp_mtu = fp->sfa_pmss + 247 sctp->sctp_hdr_len; 248 } else { 249 uinfo.iulp_mtu = fp->sfa_pmss + 250 sctp->sctp_hdr6_len; 251 } 252 } 253 if (sctps->sctps_rtt_updates != 0 && 254 fp->rtt_updates >= sctps->sctps_rtt_updates) { 255 /* 256 * dce_update_uinfo() merges these values with the 257 * old values. 258 */ 259 uinfo.iulp_rtt = TICK_TO_MSEC(fp->srtt); 260 uinfo.iulp_rtt_sd = TICK_TO_MSEC(fp->rttvar); 261 fp->rtt_updates = 0; 262 } 263 ifindex = 0; 264 if (IN6_IS_ADDR_LINKSCOPE(&fp->faddr)) { 265 /* 266 * If we are going to create a DCE we'd better have 267 * an ifindex 268 */ 269 if (fp->ixa->ixa_nce != NULL) { 270 ifindex = fp->ixa->ixa_nce->nce_common-> 271 ncec_ill->ill_phyint->phyint_ifindex; 272 } else { 273 continue; 274 } 275 } 276 277 (void) dce_update_uinfo(&fp->faddr, ifindex, &uinfo, ipst); 278 } 279 } 280 281 /* 282 * The sender must later set the total length in the IP header. 283 */ 284 mblk_t * 285 sctp_make_mp(sctp_t *sctp, sctp_faddr_t *fp, int trailer) 286 { 287 mblk_t *mp; 288 size_t ipsctplen; 289 int isv4; 290 sctp_stack_t *sctps = sctp->sctp_sctps; 291 boolean_t src_changed = B_FALSE; 292 293 ASSERT(fp != NULL); 294 isv4 = fp->isv4; 295 296 if (SCTP_IS_ADDR_UNSPEC(isv4, fp->saddr) || 297 (fp->ixa->ixa_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) { 298 /* Need to pick a source */ 299 sctp_get_dest(sctp, fp); 300 /* 301 * Although we still may not get an IRE, the source address 302 * may be changed in sctp_get_ire(). Set src_changed to 303 * true so that the source address is copied again. 304 */ 305 src_changed = B_TRUE; 306 } 307 308 /* There is no suitable source address to use, return. */ 309 if (fp->state == SCTP_FADDRS_UNREACH) 310 return (NULL); 311 312 ASSERT(fp->ixa->ixa_ire != NULL); 313 ASSERT(!SCTP_IS_ADDR_UNSPEC(isv4, fp->saddr)); 314 315 if (isv4) { 316 ipsctplen = sctp->sctp_hdr_len; 317 } else { 318 ipsctplen = sctp->sctp_hdr6_len; 319 } 320 321 mp = allocb(ipsctplen + sctps->sctps_wroff_xtra + trailer, BPRI_MED); 322 if (mp == NULL) { 323 ip1dbg(("sctp_make_mp: error making mp..\n")); 324 return (NULL); 325 } 326 mp->b_rptr += sctps->sctps_wroff_xtra; 327 mp->b_wptr = mp->b_rptr + ipsctplen; 328 329 ASSERT(OK_32PTR(mp->b_wptr)); 330 331 if (isv4) { 332 ipha_t *iph = (ipha_t *)mp->b_rptr; 333 334 bcopy(sctp->sctp_iphc, mp->b_rptr, ipsctplen); 335 if (fp != sctp->sctp_current || src_changed) { 336 /* Fix the source and destination addresses. */ 337 IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst); 338 IN6_V4MAPPED_TO_IPADDR(&fp->saddr, iph->ipha_src); 339 } 340 /* set or clear the don't fragment bit */ 341 if (fp->df) { 342 iph->ipha_fragment_offset_and_flags = htons(IPH_DF); 343 } else { 344 iph->ipha_fragment_offset_and_flags = 0; 345 } 346 } else { 347 bcopy(sctp->sctp_iphc6, mp->b_rptr, ipsctplen); 348 if (fp != sctp->sctp_current || src_changed) { 349 /* Fix the source and destination addresses. */ 350 ((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr; 351 ((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr; 352 } 353 } 354 ASSERT(sctp->sctp_connp != NULL); 355 return (mp); 356 } 357 358 /* 359 * Notify upper layers about preferred write offset, write size. 360 */ 361 void 362 sctp_set_ulp_prop(sctp_t *sctp) 363 { 364 int hdrlen; 365 struct sock_proto_props sopp; 366 367 sctp_stack_t *sctps = sctp->sctp_sctps; 368 369 if (sctp->sctp_current->isv4) { 370 hdrlen = sctp->sctp_hdr_len; 371 } else { 372 hdrlen = sctp->sctp_hdr6_len; 373 } 374 ASSERT(sctp->sctp_ulpd); 375 376 sctp->sctp_connp->conn_wroff = sctps->sctps_wroff_xtra + hdrlen + 377 sizeof (sctp_data_hdr_t); 378 379 ASSERT(sctp->sctp_current->sfa_pmss == sctp->sctp_mss); 380 bzero(&sopp, sizeof (sopp)); 381 sopp.sopp_flags = SOCKOPT_MAXBLK|SOCKOPT_WROFF; 382 sopp.sopp_wroff = sctp->sctp_connp->conn_wroff; 383 sopp.sopp_maxblk = sctp->sctp_mss - sizeof (sctp_data_hdr_t); 384 sctp->sctp_ulp_prop(sctp->sctp_ulpd, &sopp); 385 } 386 387 /* 388 * Set the lengths in the packet and the transmit attributes. 389 */ 390 void 391 sctp_set_iplen(sctp_t *sctp, mblk_t *mp, ip_xmit_attr_t *ixa) 392 { 393 uint16_t sum = 0; 394 ipha_t *iph; 395 ip6_t *ip6h; 396 mblk_t *pmp = mp; 397 boolean_t isv4; 398 399 isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION); 400 for (; pmp; pmp = pmp->b_cont) 401 sum += pmp->b_wptr - pmp->b_rptr; 402 403 ixa->ixa_pktlen = sum; 404 if (isv4) { 405 iph = (ipha_t *)mp->b_rptr; 406 iph->ipha_length = htons(sum); 407 ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr_len; 408 } else { 409 ip6h = (ip6_t *)mp->b_rptr; 410 ip6h->ip6_plen = htons(sum - IPV6_HDR_LEN); 411 ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr6_len; 412 } 413 } 414 415 int 416 sctp_compare_faddrsets(sctp_faddr_t *a1, sctp_faddr_t *a2) 417 { 418 int na1 = 0; 419 int overlap = 0; 420 int equal = 1; 421 int onematch; 422 sctp_faddr_t *fp1, *fp2; 423 424 for (fp1 = a1; fp1; fp1 = fp1->next) { 425 onematch = 0; 426 for (fp2 = a2; fp2; fp2 = fp2->next) { 427 if (IN6_ARE_ADDR_EQUAL(&fp1->faddr, &fp2->faddr)) { 428 overlap++; 429 onematch = 1; 430 break; 431 } 432 if (!onematch) { 433 equal = 0; 434 } 435 } 436 na1++; 437 } 438 439 if (equal) { 440 return (SCTP_ADDR_EQUAL); 441 } 442 if (overlap == na1) { 443 return (SCTP_ADDR_SUBSET); 444 } 445 if (overlap) { 446 return (SCTP_ADDR_OVERLAP); 447 } 448 return (SCTP_ADDR_DISJOINT); 449 } 450 451 /* 452 * Returns 0 on success, ENOMEM on memory allocation failure, EHOSTUNREACH 453 * if the connection credentials fail remote host accreditation or 454 * if the new destination does not support the previously established 455 * connection security label. If sleep is true, this function should 456 * never fail for a memory allocation failure. The boolean parameter 457 * "first" decides whether the newly created faddr structure should be 458 * added at the beginning of the list or at the end. 459 * 460 * Note: caller must hold conn fanout lock. 461 */ 462 int 463 sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep, boolean_t first) 464 { 465 sctp_faddr_t *faddr; 466 mblk_t *timer_mp; 467 int err; 468 conn_t *connp = sctp->sctp_connp; 469 470 if (is_system_labeled()) { 471 ip_xmit_attr_t *ixa = connp->conn_ixa; 472 ts_label_t *effective_tsl = NULL; 473 474 ASSERT(ixa->ixa_tsl != NULL); 475 476 /* 477 * Verify the destination is allowed to receive packets 478 * at the security label of the connection we are initiating. 479 * 480 * tsol_check_dest() will create a new effective label for 481 * this connection with a modified label or label flags only 482 * if there are changes from the original label. 483 * 484 * Accept whatever label we get if this is the first 485 * destination address for this connection. The security 486 * label and label flags must match any previuous settings 487 * for all subsequent destination addresses. 488 */ 489 if (IN6_IS_ADDR_V4MAPPED(addr)) { 490 uint32_t dst; 491 IN6_V4MAPPED_TO_IPADDR(addr, dst); 492 err = tsol_check_dest(ixa->ixa_tsl, 493 &dst, IPV4_VERSION, connp->conn_mac_mode, 494 connp->conn_zone_is_global, &effective_tsl); 495 } else { 496 err = tsol_check_dest(ixa->ixa_tsl, 497 addr, IPV6_VERSION, connp->conn_mac_mode, 498 connp->conn_zone_is_global, &effective_tsl); 499 } 500 if (err != 0) 501 return (err); 502 503 if (sctp->sctp_faddrs == NULL && effective_tsl != NULL) { 504 ip_xmit_attr_replace_tsl(ixa, effective_tsl); 505 } else if (effective_tsl != NULL) { 506 label_rele(effective_tsl); 507 return (EHOSTUNREACH); 508 } 509 } 510 511 if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL) 512 return (ENOMEM); 513 bzero(faddr, sizeof (*faddr)); 514 timer_mp = sctp_timer_alloc((sctp), sctp_rexmit_timer, sleep); 515 if (timer_mp == NULL) { 516 kmem_cache_free(sctp_kmem_faddr_cache, faddr); 517 return (ENOMEM); 518 } 519 ((sctpt_t *)(timer_mp->b_rptr))->sctpt_faddr = faddr; 520 521 /* Start with any options set on the conn */ 522 faddr->ixa = conn_get_ixa_exclusive(connp); 523 if (faddr->ixa == NULL) { 524 freemsg(timer_mp); 525 kmem_cache_free(sctp_kmem_faddr_cache, faddr); 526 return (ENOMEM); 527 } 528 faddr->ixa->ixa_notify_cookie = connp->conn_sctp; 529 530 sctp_init_faddr(sctp, faddr, addr, timer_mp); 531 ASSERT(faddr->ixa->ixa_cred != NULL); 532 533 /* ip_attr_connect didn't allow broadcats/multicast dest */ 534 ASSERT(faddr->next == NULL); 535 536 if (sctp->sctp_faddrs == NULL) { 537 ASSERT(sctp->sctp_lastfaddr == NULL); 538 /* only element on list; first and last are same */ 539 sctp->sctp_faddrs = sctp->sctp_lastfaddr = faddr; 540 } else if (first) { 541 ASSERT(sctp->sctp_lastfaddr != NULL); 542 faddr->next = sctp->sctp_faddrs; 543 sctp->sctp_faddrs = faddr; 544 } else { 545 sctp->sctp_lastfaddr->next = faddr; 546 sctp->sctp_lastfaddr = faddr; 547 } 548 sctp->sctp_nfaddrs++; 549 550 return (0); 551 } 552 553 sctp_faddr_t * 554 sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr) 555 { 556 sctp_faddr_t *fp; 557 558 for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 559 if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) 560 break; 561 } 562 563 return (fp); 564 } 565 566 sctp_faddr_t * 567 sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr) 568 { 569 for (; fp; fp = fp->next) { 570 if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) { 571 break; 572 } 573 } 574 575 return (fp); 576 } 577 578 /* 579 * To change the currently used peer address to the specified one. 580 */ 581 void 582 sctp_set_faddr_current(sctp_t *sctp, sctp_faddr_t *fp) 583 { 584 /* Now setup the composite header. */ 585 if (fp->isv4) { 586 IN6_V4MAPPED_TO_IPADDR(&fp->faddr, 587 sctp->sctp_ipha->ipha_dst); 588 IN6_V4MAPPED_TO_IPADDR(&fp->saddr, sctp->sctp_ipha->ipha_src); 589 /* update don't fragment bit */ 590 if (fp->df) { 591 sctp->sctp_ipha->ipha_fragment_offset_and_flags = 592 htons(IPH_DF); 593 } else { 594 sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0; 595 } 596 } else { 597 sctp->sctp_ip6h->ip6_dst = fp->faddr; 598 sctp->sctp_ip6h->ip6_src = fp->saddr; 599 } 600 601 sctp->sctp_current = fp; 602 sctp->sctp_mss = fp->sfa_pmss; 603 604 /* Update the uppper layer for the change. */ 605 if (!SCTP_IS_DETACHED(sctp)) 606 sctp_set_ulp_prop(sctp); 607 } 608 609 void 610 sctp_redo_faddr_srcs(sctp_t *sctp) 611 { 612 sctp_faddr_t *fp; 613 614 for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 615 sctp_get_dest(sctp, fp); 616 } 617 } 618 619 void 620 sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp) 621 { 622 int64_t now = ddi_get_lbolt64(); 623 624 fp->strikes = 0; 625 sctp->sctp_strikes = 0; 626 fp->lastactive = now; 627 fp->hb_expiry = now + SET_HB_INTVL(fp); 628 fp->hb_pending = B_FALSE; 629 if (fp->state != SCTP_FADDRS_ALIVE) { 630 fp->state = SCTP_FADDRS_ALIVE; 631 sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_AVAILABLE, 0); 632 /* Should have a full IRE now */ 633 sctp_get_dest(sctp, fp); 634 635 /* 636 * If this is the primary, switch back to it now. And 637 * we probably want to reset the source addr used to reach 638 * it. 639 * Note that if we didn't find a source in sctp_get_dest 640 * then we'd be unreachable at this point in time. 641 */ 642 if (fp == sctp->sctp_primary && 643 fp->state != SCTP_FADDRS_UNREACH) { 644 sctp_set_faddr_current(sctp, fp); 645 return; 646 } 647 } 648 } 649 650 int 651 sctp_is_a_faddr_clean(sctp_t *sctp) 652 { 653 sctp_faddr_t *fp; 654 655 for (fp = sctp->sctp_faddrs; fp; fp = fp->next) { 656 if (fp->state == SCTP_FADDRS_ALIVE && fp->strikes == 0) { 657 return (1); 658 } 659 } 660 661 return (0); 662 } 663 664 /* 665 * Returns 0 if there is at leave one other active faddr, -1 if there 666 * are none. If there are none left, faddr_dead() will start killing the 667 * association. 668 * If the downed faddr was the current faddr, a new current faddr 669 * will be chosen. 670 */ 671 int 672 sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate) 673 { 674 sctp_faddr_t *ofp; 675 sctp_stack_t *sctps = sctp->sctp_sctps; 676 677 if (fp->state == SCTP_FADDRS_ALIVE) { 678 sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_UNREACHABLE, 0); 679 } 680 fp->state = newstate; 681 682 dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n", 683 SCTP_PRINTADDR(fp->faddr), newstate)); 684 685 if (fp == sctp->sctp_current) { 686 /* Current faddr down; need to switch it */ 687 sctp->sctp_current = NULL; 688 } 689 690 /* Find next alive faddr */ 691 ofp = fp; 692 for (fp = fp->next; fp != NULL; fp = fp->next) { 693 if (fp->state == SCTP_FADDRS_ALIVE) { 694 break; 695 } 696 } 697 698 if (fp == NULL) { 699 /* Continue from beginning of list */ 700 for (fp = sctp->sctp_faddrs; fp != ofp; fp = fp->next) { 701 if (fp->state == SCTP_FADDRS_ALIVE) { 702 break; 703 } 704 } 705 } 706 707 /* 708 * Find a new fp, so if the current faddr is dead, use the new fp 709 * as the current one. 710 */ 711 if (fp != ofp) { 712 if (sctp->sctp_current == NULL) { 713 dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n", 714 SCTP_PRINTADDR(fp->faddr))); 715 /* 716 * Note that we don't need to reset the source addr 717 * of the new fp. 718 */ 719 sctp_set_faddr_current(sctp, fp); 720 } 721 return (0); 722 } 723 724 725 /* All faddrs are down; kill the association */ 726 dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n")); 727 BUMP_MIB(&sctps->sctps_mib, sctpAborted); 728 sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ? 729 SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL); 730 sctp_clean_death(sctp, sctp->sctp_client_errno ? 731 sctp->sctp_client_errno : ETIMEDOUT); 732 733 return (-1); 734 } 735 736 sctp_faddr_t * 737 sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp) 738 { 739 sctp_faddr_t *nfp = NULL; 740 sctp_faddr_t *saved_fp = NULL; 741 int min_strikes; 742 743 if (ofp == NULL) { 744 ofp = sctp->sctp_current; 745 } 746 /* Nothing to do */ 747 if (sctp->sctp_nfaddrs < 2) 748 return (ofp); 749 750 min_strikes = ofp->strikes; 751 752 /* 753 * Find the next live peer address with zero strikes. In case 754 * there is none, find the one with the lowest number of strikes. 755 */ 756 for (nfp = ofp->next; nfp != ofp; nfp = nfp->next) { 757 /* If reached end of list, continue scan from the head */ 758 if (nfp == NULL) { 759 nfp = sctp->sctp_faddrs; 760 continue; 761 } 762 if (nfp->state == SCTP_FADDRS_ALIVE) { 763 if (nfp->strikes == 0) 764 break; 765 if (nfp->strikes < min_strikes) { 766 min_strikes = nfp->strikes; 767 saved_fp = nfp; 768 } 769 } 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_init_rto_max); 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