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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/systm.h> 31 #include <sys/stream.h> 32 #include <sys/cmn_err.h> 33 #include <sys/socket.h> 34 #include <sys/kmem.h> 35 #include <sys/strsubr.h> 36 #include <sys/strsun.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/mib2.h> 46 #include "sctp_impl.h" 47 #include "sctp_asconf.h" 48 #include "sctp_addr.h" 49 50 typedef struct sctp_asconf_s { 51 mblk_t *head; 52 uint32_t cid; 53 } sctp_asconf_t; 54 55 /* 56 * The ASCONF chunk per-parameter request interface. ph is the 57 * parameter header for the parameter in the request, and cid 58 * is the parameters correlation ID. cont should be set to 1 59 * if the ASCONF framework should continue processing request 60 * parameters following this one, or 0 if it should stop. If 61 * cont is -1, this indicates complete memory depletion, which 62 * will cause the ASCONF framework to abort building a reply. If 63 * act is 1, the callback should take whatever action it needs 64 * to fulfil this request. If act is 0, this request has already 65 * been processed, so the callback should only verify and pass 66 * back error parameters, and not take any action. 67 * 68 * The callback should return an mblk with any reply enclosed, 69 * with the correlation ID in the first four bytes of the 70 * message. A NULL return implies implicit success to the 71 * requestor. 72 */ 73 typedef mblk_t *sctp_asconf_func_t(sctp_t *, sctp_parm_hdr_t *ph, uint32_t cid, 74 sctp_faddr_t *, int *cont, int act); 75 76 /* 77 * The ASCONF chunk per-parameter ACK interface. ph is the parameter 78 * header for the parameter returned in the ACK, and oph is the 79 * original parameter sent out in the ASCONF request. 80 * If the peer implicitly responded OK (by not including an 81 * explicit OK for the request), ph will be NULL. 82 * ph can also point to an Unrecognized Parameter parameter, 83 * in which case the peer did not understand the request 84 * parameter. 85 * 86 * ph and oph parameter headers are in host byte order. Encapsulated 87 * parameters will still be in network byte order. 88 */ 89 typedef void sctp_asconf_ack_func_t(sctp_t *, sctp_parm_hdr_t *ph, 90 sctp_parm_hdr_t *oph, sctp_faddr_t *); 91 92 typedef struct { 93 uint16_t id; 94 sctp_asconf_func_t *asconf; 95 sctp_asconf_ack_func_t *asconf_ack; 96 } dispatch_t; 97 98 static sctp_asconf_func_t sctp_addip_req, sctp_setprim_req, 99 sctp_asconf_unrec_parm; 100 101 static sctp_asconf_ack_func_t sctp_addip_ack, sctp_setprim_ack, 102 sctp_asconf_ack_unrec_parm; 103 104 static const dispatch_t sctp_asconf_dispatch_tbl[] = { 105 /* ID ASCONF ASCONF_ACK */ 106 { PARM_ADD_IP, sctp_addip_req, sctp_addip_ack }, 107 { PARM_DEL_IP, sctp_addip_req, sctp_addip_ack }, 108 { PARM_SET_PRIMARY, sctp_setprim_req, sctp_setprim_ack } 109 }; 110 111 static const dispatch_t sctp_asconf_default_dispatch = { 112 0, sctp_asconf_unrec_parm, sctp_asconf_ack_unrec_parm 113 }; 114 115 /* 116 * ASCONF framework 117 */ 118 119 static const dispatch_t * 120 sctp_lookup_asconf_dispatch(int id) 121 { 122 int i; 123 124 for (i = 0; i < A_CNT(sctp_asconf_dispatch_tbl); i++) { 125 if (sctp_asconf_dispatch_tbl[i].id == id) { 126 return (sctp_asconf_dispatch_tbl + i); 127 } 128 } 129 130 return (&sctp_asconf_default_dispatch); 131 } 132 133 /* 134 * Frees mp on failure 135 */ 136 static mblk_t * 137 sctp_asconf_prepend_errwrap(mblk_t *mp, uint32_t cid) 138 { 139 mblk_t *wmp; 140 sctp_parm_hdr_t *wph; 141 142 /* Prepend a wrapper err cause ind param */ 143 wmp = allocb(sizeof (*wph) + sizeof (cid), BPRI_MED); 144 if (wmp == NULL) { 145 freemsg(mp); 146 return (NULL); 147 } 148 wmp->b_wptr += sizeof (*wph) + sizeof (cid); 149 wph = (sctp_parm_hdr_t *)wmp->b_rptr; 150 wph->sph_type = htons(PARM_ERROR_IND); 151 wph->sph_len = htons(msgdsize(mp) + sizeof (*wph) + sizeof (cid)); 152 bcopy(&cid, wph + 1, sizeof (uint32_t)); 153 154 wmp->b_cont = mp; 155 return (wmp); 156 } 157 158 /*ARGSUSED*/ 159 static mblk_t * 160 sctp_asconf_unrec_parm(sctp_t *sctp, sctp_parm_hdr_t *ph, uint32_t cid, 161 sctp_faddr_t *fp, int *cont, int act) 162 { 163 mblk_t *mp = NULL; 164 165 /* Unrecognized param; check the high order bits */ 166 if ((ph->sph_type & 0xc000) == 0xc000) { 167 /* report unrecognized param, and keep processing */ 168 sctp_add_unrec_parm(ph, &mp); 169 if (mp == NULL) { 170 *cont = -1; 171 return (NULL); 172 } 173 /* Prepend a the CID and a wrapper err cause ind param */ 174 mp = sctp_asconf_prepend_errwrap(mp, cid); 175 if (mp == NULL) { 176 *cont = -1; 177 return (NULL); 178 } 179 180 *cont = 1; 181 return (mp); 182 } 183 if (ph->sph_type & 0x4000) { 184 /* Stop processing and drop; report unrecognized param */ 185 sctp_add_unrec_parm(ph, &mp); 186 if (mp == NULL) { 187 *cont = -1; 188 return (NULL); 189 } 190 /* Prepend a the CID and a wrapper err cause ind param */ 191 mp = sctp_asconf_prepend_errwrap(mp, cid); 192 if (mp == NULL) { 193 *cont = -1; 194 return (NULL); 195 } 196 197 *cont = 0; 198 return (mp); 199 } 200 if (ph->sph_type & 0x8000) { 201 /* skip and continue processing */ 202 *cont = 1; 203 return (NULL); 204 } 205 206 /* 2 high bits are clear; stop processing and drop packet */ 207 *cont = 0; 208 return (NULL); 209 } 210 211 /*ARGSUSED*/ 212 static void 213 sctp_asconf_ack_unrec_parm(sctp_t *sctp, sctp_parm_hdr_t *ph, 214 sctp_parm_hdr_t *oph, sctp_faddr_t *fp) 215 { 216 ASSERT(ph); 217 sctp_error_event(sctp, (sctp_chunk_hdr_t *)ph); 218 } 219 220 static void 221 sctp_asconf_init(sctp_asconf_t *asc) 222 { 223 ASSERT(asc != NULL); 224 225 asc->head = NULL; 226 asc->cid = 0; 227 } 228 229 static int 230 sctp_asconf_add(sctp_asconf_t *asc, mblk_t *mp) 231 { 232 uint32_t *cp; 233 234 /* XXX can't exceed MTU */ 235 236 cp = (uint32_t *)(mp->b_rptr + sizeof (sctp_parm_hdr_t)); 237 *cp = asc->cid++; 238 239 if (asc->head == NULL) 240 asc->head = mp; 241 else 242 linkb(asc->head, mp); 243 244 return (0); 245 } 246 247 static void 248 sctp_asconf_destroy(sctp_asconf_t *asc) 249 { 250 if (asc->head != NULL) { 251 freemsg(asc->head); 252 asc->head = NULL; 253 } 254 asc->cid = 0; 255 } 256 257 static int 258 sctp_asconf_send(sctp_t *sctp, sctp_asconf_t *asc, sctp_faddr_t *fp) 259 { 260 mblk_t *mp, *nmp; 261 sctp_chunk_hdr_t *ch; 262 boolean_t isv4; 263 size_t msgsize; 264 265 ASSERT(asc != NULL && asc->head != NULL); 266 267 isv4 = (fp != NULL) ? fp->isv4 : sctp->sctp_current->isv4; 268 269 /* SCTP chunk header + Serial Number + Address Param TLV */ 270 msgsize = sizeof (*ch) + sizeof (uint32_t) + 271 (isv4 ? PARM_ADDR4_LEN : PARM_ADDR6_LEN); 272 273 mp = allocb(msgsize, BPRI_MED); 274 if (mp == NULL) 275 return (ENOMEM); 276 277 mp->b_wptr += msgsize; 278 mp->b_cont = asc->head; 279 280 ch = (sctp_chunk_hdr_t *)mp->b_rptr; 281 ch->sch_id = CHUNK_ASCONF; 282 ch->sch_flags = 0; 283 ch->sch_len = htons(msgdsize(mp)); 284 285 nmp = msgpullup(mp, -1); 286 if (nmp == NULL) { 287 freeb(mp); 288 return (ENOMEM); 289 } 290 291 /* Clean up the temporary mblk chain */ 292 freemsg(mp); 293 asc->head = NULL; 294 asc->cid = 0; 295 296 /* Queue it ... */ 297 if (sctp->sctp_cxmit_list == NULL) { 298 sctp->sctp_cxmit_list = nmp; 299 } else { 300 linkb(sctp->sctp_cxmit_list, nmp); 301 } 302 303 BUMP_LOCAL(sctp->sctp_obchunks); 304 305 /* And try to send it. */ 306 sctp_wput_asconf(sctp, fp); 307 308 return (0); 309 } 310 311 /* 312 * If the peer does not understand an ASCONF chunk, we simply 313 * clear out the cxmit_list, since we can send nothing further 314 * that the peer will understand. 315 * 316 * Assumes chunk length has already been checked. 317 */ 318 /*ARGSUSED*/ 319 void 320 sctp_asconf_unrec_chunk(sctp_t *sctp, sctp_chunk_hdr_t *ch) 321 { 322 if (sctp->sctp_cxmit_list == NULL) { 323 /* Nothing pending */ 324 return; 325 } 326 327 freemsg(sctp->sctp_cxmit_list); 328 sctp->sctp_cxmit_list = NULL; 329 } 330 331 void 332 sctp_input_asconf(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp) 333 { 334 const dispatch_t *dp; 335 mblk_t *hmp; 336 mblk_t *mp; 337 uint32_t *idp; 338 uint32_t *hidp; 339 ssize_t rlen; 340 sctp_parm_hdr_t *ph; 341 sctp_chunk_hdr_t *ach; 342 int cont; 343 int act; 344 uint16_t plen; 345 346 ASSERT(ch->sch_id == CHUNK_ASCONF); 347 348 idp = (uint32_t *)(ch + 1); 349 rlen = ntohs(ch->sch_len) - sizeof (*ch) - sizeof (*idp); 350 351 if (rlen < 0 || rlen < sizeof (*idp)) { 352 /* nothing there; bail out */ 353 return; 354 } 355 356 /* Check for duplicates */ 357 *idp = ntohl(*idp); 358 if (*idp == (sctp->sctp_fcsn + 1)) { 359 act = 1; 360 } else if (*idp == sctp->sctp_fcsn) { 361 act = 0; 362 } else { 363 /* stale or malicious packet; drop */ 364 return; 365 } 366 367 /* Create the ASCONF_ACK header */ 368 hmp = sctp_make_mp(sctp, fp, sizeof (*ach) + sizeof (*idp)); 369 if (hmp == NULL) { 370 /* Let the peer retransmit */ 371 return; 372 } 373 ach = (sctp_chunk_hdr_t *)hmp->b_wptr; 374 ach->sch_id = CHUNK_ASCONF_ACK; 375 ach->sch_flags = 0; 376 /* Set the length later */ 377 hidp = (uint32_t *)(ach + 1); 378 *hidp = htonl(*idp); 379 hmp->b_wptr = (uchar_t *)(hidp + 1); 380 381 /* Move to the Address Parameter */ 382 ph = (sctp_parm_hdr_t *)(idp + 1); 383 if (rlen <= ntohs(ph->sph_len)) { 384 freeb(hmp); 385 return; 386 } 387 388 /* 389 * We already have the association here, so this address parameter 390 * doesn't seem to be very useful, should we make sure this is part 391 * of the association and send an error, if not? 392 * Ignore it for now. 393 */ 394 rlen -= ntohs(ph->sph_len); 395 ph = (sctp_parm_hdr_t *)((char *)ph + ntohs(ph->sph_len)); 396 cont = 1; 397 while (rlen > 0 && cont) { 398 /* Sanity checks */ 399 if (rlen < sizeof (*ph)) 400 break; 401 plen = ntohs(ph->sph_len); 402 if (plen < sizeof (*ph) || plen > rlen) { 403 break; 404 } 405 idp = (uint32_t *)(ph + 1); 406 dp = sctp_lookup_asconf_dispatch(ntohs(ph->sph_type)); 407 ASSERT(dp); 408 if (dp->asconf) { 409 mp = dp->asconf(sctp, ph, *idp, fp, &cont, act); 410 if (cont == -1) { 411 /* 412 * Not even enough memory to create 413 * an out-of-resources error. Free 414 * everything and return; the peer 415 * should retransmit. 416 */ 417 freemsg(hmp); 418 return; 419 } 420 if (mp != NULL) { 421 linkb(hmp, mp); 422 } 423 } 424 ph = sctp_next_parm(ph, &rlen); 425 if (ph == NULL) 426 break; 427 } 428 429 /* Now that the params have been processed, increment the fcsn */ 430 if (act) { 431 sctp->sctp_fcsn++; 432 } 433 BUMP_LOCAL(sctp->sctp_obchunks); 434 435 if (fp->isv4) 436 ach->sch_len = htons(msgdsize(hmp) - sctp->sctp_hdr_len); 437 else 438 ach->sch_len = htons(msgdsize(hmp) - sctp->sctp_hdr6_len); 439 sctp_set_iplen(sctp, hmp); 440 441 sctp_add_sendq(sctp, hmp); 442 sctp_validate_peer(sctp); 443 } 444 445 static sctp_parm_hdr_t * 446 sctp_lookup_asconf_param(sctp_parm_hdr_t *ph, uint32_t cid, ssize_t rlen) 447 { 448 uint32_t *idp; 449 450 while (rlen > 0) { 451 idp = (uint32_t *)(ph + 1); 452 if (*idp == cid) { 453 return (ph); 454 } 455 ph = sctp_next_parm(ph, &rlen); 456 if (ph == NULL) 457 break; 458 } 459 return (NULL); 460 } 461 462 void 463 sctp_input_asconf_ack(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp) 464 { 465 const dispatch_t *dp; 466 uint32_t *idp; 467 uint32_t *snp; 468 ssize_t rlen; 469 ssize_t plen; 470 sctp_parm_hdr_t *ph; 471 sctp_parm_hdr_t *oph; 472 sctp_parm_hdr_t *fph; 473 mblk_t *mp; 474 sctp_chunk_hdr_t *och; 475 int redosrcs = 0; 476 uint16_t param_len; 477 478 ASSERT(ch->sch_id == CHUNK_ASCONF_ACK); 479 480 snp = (uint32_t *)(ch + 1); 481 rlen = ntohs(ch->sch_len) - sizeof (*ch) - sizeof (*snp); 482 if (rlen < 0) { 483 return; 484 } 485 486 /* Accept only an ACK for the current serial number */ 487 *snp = ntohl(*snp); 488 if (sctp->sctp_cxmit_list == NULL || *snp != (sctp->sctp_lcsn - 1)) { 489 /* Need to send an abort */ 490 return; 491 } 492 sctp->sctp_cchunk_pend = 0; 493 SCTP_FADDR_RC_TIMER_STOP(fp); 494 495 /* 496 * Pass explicit replies to callbacks: 497 * For each reply in the ACK, look up the corresponding 498 * original parameter in the request using the correlation 499 * ID, and pass it to the right callback. 500 */ 501 och = (sctp_chunk_hdr_t *)sctp->sctp_cxmit_list->b_rptr; 502 503 plen = ntohs(och->sch_len) - sizeof (*och) - sizeof (*idp); 504 idp = (uint32_t *)(och + 1); 505 506 /* Get to the 1st ASCONF param, need to skip Address TLV parm */ 507 fph = (sctp_parm_hdr_t *)(idp + 1); 508 plen -= ntohs(fph->sph_len); 509 fph = (sctp_parm_hdr_t *)((char *)fph + ntohs(fph->sph_len)); 510 ph = (sctp_parm_hdr_t *)(snp + 1); 511 while (rlen > 0) { 512 /* Sanity checks */ 513 if (rlen < sizeof (*ph)) { 514 break; 515 } 516 param_len = ntohs(ph->sph_len); 517 if (param_len < sizeof (*ph) || param_len > rlen) { 518 break; 519 } 520 idp = (uint32_t *)(ph + 1); 521 oph = sctp_lookup_asconf_param(fph, *idp, plen); 522 if (oph != NULL) { 523 dp = sctp_lookup_asconf_dispatch(ntohs(oph->sph_type)); 524 ASSERT(dp); 525 if (dp->asconf_ack) { 526 dp->asconf_ack(sctp, ph, oph, fp); 527 528 /* hack. see below */ 529 if (oph->sph_type == htons(PARM_ADD_IP) || 530 oph->sph_type == htons(PARM_DEL_IP)) { 531 redosrcs = 1; 532 } 533 } 534 } 535 536 ph = sctp_next_parm(ph, &rlen); 537 if (ph == NULL) 538 break; 539 } 540 541 /* 542 * Pass implicit replies to callbacks: 543 * For each original request, look up its parameter 544 * in the ACK. If there is no corresponding reply, 545 * call the callback with a NULL parameter, indicating 546 * success. 547 */ 548 rlen = plen; 549 plen = ntohs(ch->sch_len) - sizeof (*ch) - sizeof (*idp); 550 oph = fph; 551 fph = (sctp_parm_hdr_t *)((char *)ch + sizeof (sctp_chunk_hdr_t) + 552 sizeof (uint32_t)); 553 while (rlen > 0) { 554 idp = (uint32_t *)(oph + 1); 555 ph = sctp_lookup_asconf_param(fph, *idp, plen); 556 if (ph == NULL) { 557 dp = sctp_lookup_asconf_dispatch(ntohs(oph->sph_type)); 558 ASSERT(dp); 559 if (dp->asconf_ack) { 560 dp->asconf_ack(sctp, NULL, oph, fp); 561 562 /* hack. see below */ 563 if (oph->sph_type == htons(PARM_ADD_IP) || 564 oph->sph_type == htons(PARM_DEL_IP)) { 565 redosrcs = 1; 566 } 567 } 568 } 569 oph = sctp_next_parm(oph, &rlen); 570 if (oph == NULL) { 571 break; 572 } 573 } 574 575 /* We can now free up the first chunk in the cxmit list */ 576 mp = sctp->sctp_cxmit_list; 577 sctp->sctp_cxmit_list = mp->b_cont; 578 mp->b_cont = NULL; 579 580 fp = SCTP_CHUNK_DEST(mp); 581 ASSERT(fp != NULL && fp->suna >= MBLKL(mp)); 582 fp->suna -= MBLKL(mp); 583 freeb(mp); 584 585 /* can now send the next control chunk */ 586 if (sctp->sctp_cxmit_list != NULL) 587 sctp_wput_asconf(sctp, NULL); 588 589 /* 590 * If an add-ip or del-ip has completed (successfully or 591 * unsuccessfully), the pool of available source addresses 592 * may have changed, so we need to redo faddr source 593 * address selections. This is a bit of a hack since 594 * this really belongs in the add/del-ip code. However, 595 * that code consists of callbacks called for *each* 596 * add/del-ip parameter, and sctp_redo_faddr_srcs() is 597 * expensive enough that we really don't want to be 598 * doing it for each one. So we do it once here. 599 */ 600 if (redosrcs) 601 sctp_redo_faddr_srcs(sctp); 602 } 603 604 static void 605 sctp_rc_timer(sctp_t *sctp, sctp_faddr_t *fp) 606 { 607 #define SCTP_CLR_SENT_FLAG(mp) ((mp)->b_flag &= ~SCTP_CHUNK_FLAG_SENT) 608 sctp_faddr_t *nfp; 609 sctp_faddr_t *ofp; 610 611 ASSERT(fp != NULL); 612 613 fp->rc_timer_running = 0; 614 615 if (sctp->sctp_state != SCTPS_ESTABLISHED || 616 sctp->sctp_cxmit_list == NULL) { 617 return; 618 } 619 /* 620 * Not a retransmission, this was deferred due to some error 621 * condition 622 */ 623 if (!SCTP_CHUNK_ISSENT(sctp->sctp_cxmit_list)) { 624 sctp_wput_asconf(sctp, fp); 625 return; 626 } 627 /* 628 * The sent flag indicates if the msg has been sent on this fp. 629 */ 630 SCTP_CLR_SENT_FLAG(sctp->sctp_cxmit_list); 631 /* Retransmission */ 632 if (sctp->sctp_strikes >= sctp->sctp_pa_max_rxt) { 633 /* time to give up */ 634 BUMP_MIB(&sctp_mib, sctpAborted); 635 sctp_assoc_event(sctp, SCTP_COMM_LOST, 0, NULL); 636 sctp_clean_death(sctp, ETIMEDOUT); 637 return; 638 } 639 if (fp->strikes >= fp->max_retr) { 640 if (sctp_faddr_dead(sctp, fp, SCTP_FADDRS_DOWN) == -1) 641 return; 642 } 643 644 fp->strikes++; 645 sctp->sctp_strikes++; 646 SCTP_CALC_RXT(fp, sctp->sctp_rto_max); 647 648 nfp = sctp_rotate_faddr(sctp, fp); 649 sctp->sctp_cchunk_pend = 0; 650 ofp = SCTP_CHUNK_DEST(sctp->sctp_cxmit_list); 651 SCTP_SET_CHUNK_DEST(sctp->sctp_cxmit_list, NULL); 652 ASSERT(ofp != NULL && ofp == fp); 653 ASSERT(ofp->suna >= MBLKL(sctp->sctp_cxmit_list)); 654 /* 655 * Enter slow start for this destination. 656 * XXX anything in the data path that needs to be considered? 657 */ 658 ofp->ssthresh = ofp->cwnd / 2; 659 if (ofp->ssthresh < 2 * ofp->sfa_pmss) 660 ofp->ssthresh = 2 * ofp->sfa_pmss; 661 ofp->cwnd = ofp->sfa_pmss; 662 ofp->pba = 0; 663 ofp->suna -= MBLKL(sctp->sctp_cxmit_list); 664 /* 665 * The rexmit flags is used to determine if a serial number needs to 666 * be assigned or not, so once set we leave it there. 667 */ 668 if (!SCTP_CHUNK_WANT_REXMIT(sctp->sctp_cxmit_list)) 669 SCTP_CHUNK_REXMIT(sctp->sctp_cxmit_list); 670 sctp_wput_asconf(sctp, nfp); 671 #undef SCTP_CLR_SENT_FLAG 672 } 673 674 void 675 sctp_wput_asconf(sctp_t *sctp, sctp_faddr_t *fp) 676 { 677 #define SCTP_SET_SENT_FLAG(mp) ((mp)->b_flag = SCTP_CHUNK_FLAG_SENT) 678 679 mblk_t *mp; 680 mblk_t *ipmp; 681 uint32_t *snp; 682 sctp_parm_hdr_t *ph; 683 boolean_t isv4; 684 685 if (sctp->sctp_cchunk_pend || sctp->sctp_cxmit_list == NULL || 686 /* Queue it for later transmission if not yet established */ 687 sctp->sctp_state < SCTPS_ESTABLISHED) { 688 ip2dbg(("sctp_wput_asconf: cchunk pending? (%d) or null "\ 689 "sctp_cxmit_list? (%s) or incorrect state? (%x)\n", 690 sctp->sctp_cchunk_pend, sctp->sctp_cxmit_list == NULL ? 691 "yes" : "no", sctp->sctp_state)); 692 return; 693 } 694 695 if (fp == NULL) 696 fp = sctp->sctp_current; 697 698 /* OK to send */ 699 ipmp = sctp_make_mp(sctp, fp, 0); 700 if (ipmp == NULL) { 701 SCTP_FADDR_RC_TIMER_RESTART(sctp, fp, fp->rto); 702 return; 703 } 704 mp = sctp->sctp_cxmit_list; 705 /* Fill in the mandatory Address Parameter TLV */ 706 isv4 = (fp != NULL) ? fp->isv4 : sctp->sctp_current->isv4; 707 ph = (sctp_parm_hdr_t *)(mp->b_rptr + sizeof (sctp_chunk_hdr_t) + 708 sizeof (uint32_t)); 709 if (isv4) { 710 ipha_t *ipha = (ipha_t *)ipmp->b_rptr; 711 in6_addr_t ipaddr; 712 ipaddr_t addr4; 713 714 ph->sph_type = htons(PARM_ADDR4); 715 ph->sph_len = htons(PARM_ADDR4_LEN); 716 if (ipha->ipha_src != INADDR_ANY) { 717 bcopy(&ipha->ipha_src, ph + 1, IP_ADDR_LEN); 718 } else { 719 ipaddr = sctp_get_valid_addr(sctp, B_FALSE); 720 IN6_V4MAPPED_TO_IPADDR(&ipaddr, addr4); 721 bcopy(&addr4, ph + 1, IP_ADDR_LEN); 722 } 723 } else { 724 ip6_t *ip6 = (ip6_t *)ipmp->b_rptr; 725 in6_addr_t ipaddr; 726 727 ph->sph_type = htons(PARM_ADDR6); 728 ph->sph_len = htons(PARM_ADDR6_LEN); 729 if (!IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 730 bcopy(&ip6->ip6_src, ph + 1, IPV6_ADDR_LEN); 731 } else { 732 ipaddr = sctp_get_valid_addr(sctp, B_TRUE); 733 bcopy(&ipaddr, ph + 1, IPV6_ADDR_LEN); 734 } 735 } 736 737 /* Don't exceed CWND */ 738 if ((MBLKL(mp) > (fp->cwnd - fp->suna)) || 739 ((mp = dupb(sctp->sctp_cxmit_list)) == NULL)) { 740 SCTP_FADDR_RC_TIMER_RESTART(sctp, fp, fp->rto); 741 freeb(ipmp); 742 return; 743 } 744 745 /* Set the serial number now, if sending for the first time */ 746 if (!SCTP_CHUNK_WANT_REXMIT(mp)) { 747 snp = (uint32_t *)(mp->b_rptr + sizeof (sctp_chunk_hdr_t)); 748 *snp = htonl(sctp->sctp_lcsn++); 749 } 750 SCTP_CHUNK_CLEAR_FLAGS(mp); 751 fp->suna += MBLKL(mp); 752 /* Attach the header and send the chunk */ 753 ipmp->b_cont = mp; 754 sctp_set_iplen(sctp, ipmp); 755 sctp->sctp_cchunk_pend = 1; 756 757 SCTP_SET_SENT_FLAG(sctp->sctp_cxmit_list); 758 SCTP_SET_CHUNK_DEST(sctp->sctp_cxmit_list, fp); 759 sctp_add_sendq(sctp, ipmp); 760 SCTP_FADDR_RC_TIMER_RESTART(sctp, fp, fp->rto); 761 #undef SCTP_SET_SENT_FLAG 762 } 763 764 /* 765 * Generate ASCONF error param, include errph, if present. 766 */ 767 static mblk_t * 768 sctp_asconf_adderr(int err, sctp_parm_hdr_t *errph, uint32_t cid) 769 { 770 mblk_t *mp; 771 sctp_parm_hdr_t *eph; 772 sctp_parm_hdr_t *wph; 773 size_t len; 774 size_t elen = 0; 775 776 len = sizeof (*wph) + sizeof (*eph) + sizeof (cid); 777 if (errph != NULL) { 778 elen = ntohs(errph->sph_len); 779 len += elen; 780 } 781 mp = allocb(len, BPRI_MED); 782 if (mp == NULL) { 783 return (NULL); 784 } 785 wph = (sctp_parm_hdr_t *)mp->b_rptr; 786 /* error cause wrapper */ 787 wph->sph_type = htons(PARM_ERROR_IND); 788 wph->sph_len = htons(len); 789 bcopy(&cid, wph + 1, sizeof (uint32_t)); 790 791 /* error cause */ 792 eph = (sctp_parm_hdr_t *)((char *)wph + sizeof (sctp_parm_hdr_t) + 793 sizeof (cid)); 794 eph->sph_type = htons(err); 795 eph->sph_len = htons(len - sizeof (*wph) - sizeof (cid)); 796 mp->b_wptr = (uchar_t *)(eph + 1); 797 798 /* details */ 799 if (elen > 0) { 800 bcopy(errph, mp->b_wptr, elen); 801 mp->b_wptr += elen; 802 } 803 return (mp); 804 } 805 806 static mblk_t * 807 sctp_check_addip_addr(sctp_parm_hdr_t *ph, sctp_parm_hdr_t *oph, int *cont, 808 uint32_t cid, in6_addr_t *raddr) 809 { 810 uint16_t atype; 811 uint16_t alen; 812 mblk_t *mp; 813 in6_addr_t addr; 814 ipaddr_t *addr4; 815 816 atype = ntohs(ph->sph_type); 817 alen = ntohs(ph->sph_len); 818 819 if (atype != PARM_ADDR4 && atype != PARM_ADDR6) { 820 mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph, cid); 821 if (mp == NULL) { 822 *cont = -1; 823 } 824 return (mp); 825 } 826 if ((atype == PARM_ADDR4 && alen < PARM_ADDR4_LEN) || 827 (atype == PARM_ADDR6 && alen < PARM_ADDR6_LEN)) { 828 mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph, cid); 829 if (mp == NULL) { 830 *cont = -1; 831 } 832 return (mp); 833 } 834 835 /* Address parameter is present; extract and screen it */ 836 if (atype == PARM_ADDR4) { 837 addr4 = (ipaddr_t *)(ph + 1); 838 IN6_IPADDR_TO_V4MAPPED(*addr4, &addr); 839 840 /* screen XXX loopback to scoping */ 841 if (*addr4 == 0 || *addr4 == INADDR_BROADCAST || 842 *addr4 == htonl(INADDR_LOOPBACK) || IN_MULTICAST(*addr4)) { 843 dprint(1, ("addip: addr not unicast: %x:%x:%x:%x\n", 844 SCTP_PRINTADDR(addr))); 845 mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph, 846 cid); 847 if (mp == NULL) { 848 *cont = -1; 849 } 850 return (mp); 851 } 852 /* 853 * XXX also need to check for subnet 854 * broadcasts. This should probably 855 * wait until we have full access 856 * to the ILL tables. 857 */ 858 859 } else { 860 bcopy(ph + 1, &addr, sizeof (addr)); 861 862 /* screen XXX loopback to scoping */ 863 if (IN6_IS_ADDR_LINKLOCAL(&addr) || 864 IN6_IS_ADDR_MULTICAST(&addr) || 865 IN6_IS_ADDR_LOOPBACK(&addr)) { 866 dprint(1, ("addip: addr not unicast: %x:%x:%x:%x\n", 867 SCTP_PRINTADDR(addr))); 868 mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph, 869 cid); 870 if (mp == NULL) { 871 *cont = -1; 872 } 873 return (mp); 874 } 875 876 } 877 878 /* OK */ 879 *raddr = addr; 880 return (NULL); 881 } 882 883 /* 884 * Handles both add and delete address requests. 885 */ 886 static mblk_t * 887 sctp_addip_req(sctp_t *sctp, sctp_parm_hdr_t *ph, uint32_t cid, 888 sctp_faddr_t *fp, int *cont, int act) 889 { 890 in6_addr_t addr; 891 uint16_t type; 892 mblk_t *mp; 893 sctp_faddr_t *nfp; 894 sctp_parm_hdr_t *oph; 895 896 *cont = 1; 897 898 /* Send back an authorization error if addip is disabled */ 899 if (!sctp_addip_enabled) { 900 mp = sctp_asconf_adderr(SCTP_ERR_UNAUTHORIZED, ph, cid); 901 if (mp == NULL) 902 *cont = -1; 903 return (mp); 904 } 905 /* Check input */ 906 if (ntohs(ph->sph_len) < (sizeof (*ph) * 2)) { 907 mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, ph, cid); 908 if (mp == NULL) { 909 *cont = -1; 910 } 911 return (mp); 912 } 913 914 type = ntohs(ph->sph_type); 915 oph = ph; 916 ph = (sctp_parm_hdr_t *)((char *)ph + sizeof (sctp_parm_hdr_t) + 917 sizeof (cid)); 918 mp = sctp_check_addip_addr(ph, oph, cont, cid, &addr); 919 if (mp != NULL) 920 return (mp); 921 922 if (type == PARM_ADD_IP) { 923 if (sctp_lookup_faddr(sctp, &addr) != NULL) { 924 /* Address is already part of association */ 925 dprint(1, ("addip: addr already here: %x:%x:%x:%x\n", 926 SCTP_PRINTADDR(addr))); 927 mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph, 928 cid); 929 if (mp == NULL) { 930 *cont = -1; 931 } 932 return (mp); 933 } 934 935 if (!act) { 936 return (NULL); 937 } 938 /* Add the new address */ 939 mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 940 if (sctp_add_faddr(sctp, &addr, KM_NOSLEEP) != 0) { 941 mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 942 /* no memory */ 943 *cont = -1; 944 return (NULL); 945 } 946 mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 947 sctp_intf_event(sctp, addr, SCTP_ADDR_ADDED, 0); 948 } else if (type == PARM_DEL_IP) { 949 nfp = sctp_lookup_faddr(sctp, &addr); 950 if (nfp == NULL) { 951 /* 952 * Peer is trying to delete an address that is not 953 * part of the association. 954 */ 955 dprint(1, ("delip: addr not here: %x:%x:%x:%x\n", 956 SCTP_PRINTADDR(addr))); 957 mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph, 958 cid); 959 if (mp == NULL) { 960 *cont = -1; 961 } 962 return (mp); 963 } 964 if (sctp->sctp_faddrs == nfp && nfp->next == NULL) { 965 /* Peer is trying to delete last address */ 966 dprint(1, ("delip: del last addr: %x:%x:%x:%x\n", 967 SCTP_PRINTADDR(addr))); 968 mp = sctp_asconf_adderr(SCTP_ERR_DEL_LAST_ADDR, oph, 969 cid); 970 if (mp == NULL) { 971 *cont = -1; 972 } 973 return (mp); 974 } 975 if (nfp == fp) { 976 /* Peer is trying to delete source address */ 977 dprint(1, ("delip: del src addr: %x:%x:%x:%x\n", 978 SCTP_PRINTADDR(addr))); 979 mp = sctp_asconf_adderr(SCTP_ERR_DEL_SRC_ADDR, oph, 980 cid); 981 if (mp == NULL) { 982 *cont = -1; 983 } 984 return (mp); 985 } 986 if (!act) { 987 return (NULL); 988 } 989 990 sctp_unlink_faddr(sctp, nfp); 991 /* Update all references to the deleted faddr */ 992 if (sctp->sctp_primary == nfp) { 993 sctp->sctp_primary = fp; 994 } 995 if (sctp->sctp_current == nfp) { 996 sctp->sctp_current = fp; 997 sctp->sctp_mss = fp->sfa_pmss; 998 sctp_faddr2hdraddr(fp, sctp); 999 1000 if (!SCTP_IS_DETACHED(sctp)) { 1001 sctp_set_ulp_prop(sctp); 1002 } 1003 } 1004 if (sctp->sctp_lastdata == nfp) { 1005 sctp->sctp_lastdata = fp; 1006 } 1007 if (sctp->sctp_shutdown_faddr == nfp) { 1008 sctp->sctp_shutdown_faddr = nfp; 1009 } 1010 if (sctp->sctp_lastfaddr == nfp) { 1011 for (fp = sctp->sctp_faddrs; fp->next; fp = fp->next) 1012 ; 1013 sctp->sctp_lastfaddr = fp; 1014 } 1015 sctp_intf_event(sctp, addr, SCTP_ADDR_REMOVED, 0); 1016 } else { 1017 ASSERT(0); 1018 } 1019 1020 /* Successful, don't need to return anything. */ 1021 return (NULL); 1022 } 1023 1024 /* 1025 * Handles both add and delete IP ACKs. 1026 */ 1027 /*ARGSUSED*/ 1028 static void 1029 sctp_addip_ack(sctp_t *sctp, sctp_parm_hdr_t *ph, sctp_parm_hdr_t *oph, 1030 sctp_faddr_t *fp) 1031 { 1032 in6_addr_t addr; 1033 sctp_saddr_ipif_t *sp; 1034 ipaddr_t *addr4; 1035 boolean_t backout = B_FALSE; 1036 uint16_t type; 1037 uint32_t *cid; 1038 1039 /* If the peer doesn't understand Add-IP, remember it */ 1040 if (ph != NULL && ph->sph_type == htons(PARM_UNRECOGNIZED)) { 1041 sctp->sctp_understands_addip = B_FALSE; 1042 backout = B_TRUE; 1043 } 1044 1045 /* 1046 * If OK, continue with the add / delete action, otherwise 1047 * back out the action. 1048 */ 1049 if (ph != NULL && ph->sph_type != htons(PARM_SUCCESS)) { 1050 backout = B_TRUE; 1051 sctp_error_event(sctp, (sctp_chunk_hdr_t *)ph); 1052 } 1053 1054 type = ntohs(oph->sph_type); 1055 cid = (uint32_t *)(oph + 1); 1056 oph = (sctp_parm_hdr_t *)(cid + 1); 1057 if (oph->sph_type == htons(PARM_ADDR4)) { 1058 addr4 = (ipaddr_t *)(oph + 1); 1059 IN6_IPADDR_TO_V4MAPPED(*addr4, &addr); 1060 } else { 1061 bcopy(oph + 1, &addr, sizeof (addr)); 1062 } 1063 1064 sp = sctp_saddr_lookup(sctp, &addr); 1065 ASSERT(sp != NULL); 1066 1067 if (type == PARM_ADD_IP) { 1068 if (backout) { 1069 sctp_del_saddr(sctp, sp); 1070 } else { 1071 sp->saddr_ipif_dontsrc = 0; 1072 } 1073 } else if (type == PARM_DEL_IP) { 1074 if (backout) { 1075 sp->saddr_ipif_delete_pending = 0; 1076 sp->saddr_ipif_dontsrc = 0; 1077 } else { 1078 sctp_del_saddr(sctp, sp); 1079 } 1080 } else { 1081 /* Must be either PARM_ADD_IP or PARM_DEL_IP */ 1082 ASSERT(0); 1083 } 1084 } 1085 1086 /*ARGSUSED*/ 1087 static mblk_t * 1088 sctp_setprim_req(sctp_t *sctp, sctp_parm_hdr_t *ph, uint32_t cid, 1089 sctp_faddr_t *fp, int *cont, int act) 1090 { 1091 mblk_t *mp; 1092 sctp_parm_hdr_t *oph; 1093 sctp_faddr_t *nfp; 1094 in6_addr_t addr; 1095 1096 *cont = 1; 1097 1098 /* Check input */ 1099 if (ntohs(ph->sph_len) < (sizeof (*ph) * 2)) { 1100 mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, ph, cid); 1101 if (mp == NULL) { 1102 *cont = -1; 1103 } 1104 return (mp); 1105 } 1106 1107 oph = ph; 1108 ph = (sctp_parm_hdr_t *)((char *)ph + sizeof (sctp_parm_hdr_t) + 1109 sizeof (cid)); 1110 mp = sctp_check_addip_addr(ph, oph, cont, cid, &addr); 1111 if (mp != NULL) { 1112 return (mp); 1113 } 1114 1115 nfp = sctp_lookup_faddr(sctp, &addr); 1116 if (nfp == NULL) { 1117 /* 1118 * Peer is trying to set an address that is not 1119 * part of the association. 1120 */ 1121 dprint(1, ("setprim: addr not here: %x:%x:%x:%x\n", 1122 SCTP_PRINTADDR(addr))); 1123 mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph, cid); 1124 if (mp == NULL) { 1125 *cont = -1; 1126 } 1127 return (mp); 1128 } 1129 1130 sctp_intf_event(sctp, addr, SCTP_ADDR_MADE_PRIM, 0); 1131 sctp->sctp_primary = nfp; 1132 if (nfp->state != SCTP_FADDRS_ALIVE || nfp == sctp->sctp_current) { 1133 return (NULL); 1134 } 1135 sctp->sctp_current = nfp; 1136 sctp->sctp_mss = nfp->sfa_pmss; 1137 1138 /* Reset the addrs in the composite header */ 1139 sctp_faddr2hdraddr(nfp, sctp); 1140 if (!SCTP_IS_DETACHED(sctp)) { 1141 sctp_set_ulp_prop(sctp); 1142 } 1143 1144 return (NULL); 1145 } 1146 1147 /*ARGSUSED*/ 1148 static void 1149 sctp_setprim_ack(sctp_t *sctp, sctp_parm_hdr_t *ph, sctp_parm_hdr_t *oph, 1150 sctp_faddr_t *fp) 1151 { 1152 if (ph != NULL && ph->sph_type != htons(PARM_SUCCESS)) { 1153 /* If the peer doesn't understand Add-IP, remember it */ 1154 if (ph->sph_type == htons(PARM_UNRECOGNIZED)) { 1155 sctp->sctp_understands_addip = B_FALSE; 1156 } 1157 sctp_error_event(sctp, (sctp_chunk_hdr_t *)ph); 1158 } 1159 1160 /* On success we do nothing */ 1161 } 1162 1163 int 1164 sctp_add_ip(sctp_t *sctp, const void *addrs, uint32_t cnt) 1165 { 1166 struct sockaddr_in *sin4; 1167 struct sockaddr_in6 *sin6; 1168 mblk_t *mp; 1169 int error = 0; 1170 int i; 1171 sctp_addip4_t *ad4; 1172 sctp_addip6_t *ad6; 1173 sctp_asconf_t asc[1]; 1174 uint16_t type = htons(PARM_ADD_IP); 1175 boolean_t v4mapped = B_FALSE; 1176 1177 /* Does the peer understand ASCONF and Add-IP? */ 1178 if (!sctp->sctp_understands_asconf || !sctp->sctp_understands_addip) 1179 return (EOPNOTSUPP); 1180 1181 sctp_asconf_init(asc); 1182 1183 /* 1184 * Screen addresses: 1185 * If adding: 1186 * o Must not already be a part of the association 1187 * o Must be AF_INET or AF_INET6 1188 * o XXX Must be valid source address for this node 1189 * o Must be unicast 1190 * o XXX Must fit scoping rules 1191 * If deleting: 1192 * o Must be part of the association 1193 */ 1194 for (i = 0; i < cnt; i++) { 1195 switch (sctp->sctp_family) { 1196 case AF_INET: 1197 sin4 = (struct sockaddr_in *)addrs + i; 1198 v4mapped = B_TRUE; 1199 break; 1200 1201 case AF_INET6: 1202 sin6 = (struct sockaddr_in6 *)addrs + i; 1203 break; 1204 } 1205 1206 if (v4mapped) { 1207 mp = allocb(sizeof (*ad4), BPRI_MED); 1208 if (mp == NULL) { 1209 error = ENOMEM; 1210 goto fail; 1211 } 1212 mp->b_wptr += sizeof (*ad4); 1213 ad4 = (sctp_addip4_t *)mp->b_rptr; 1214 ad4->sad4_addip_ph.sph_type = type; 1215 ad4->sad4_addip_ph.sph_len = 1216 htons(sizeof (sctp_parm_hdr_t) + 1217 PARM_ADDR4_LEN + sizeof (ad4->asconf_req_cid)); 1218 ad4->sad4_addr4_ph.sph_type = htons(PARM_ADDR4); 1219 ad4->sad4_addr4_ph.sph_len = htons(PARM_ADDR4_LEN); 1220 ad4->sad4_addr = sin4->sin_addr.s_addr; 1221 } else { 1222 mp = allocb(sizeof (*ad6), BPRI_MED); 1223 if (mp == NULL) { 1224 error = ENOMEM; 1225 goto fail; 1226 } 1227 mp->b_wptr += sizeof (*ad6); 1228 ad6 = (sctp_addip6_t *)mp->b_rptr; 1229 ad6->sad6_addip_ph.sph_type = type; 1230 ad6->sad6_addip_ph.sph_len = 1231 htons(sizeof (sctp_parm_hdr_t) + 1232 PARM_ADDR6_LEN + sizeof (ad6->asconf_req_cid)); 1233 ad6->sad6_addr6_ph.sph_type = htons(PARM_ADDR6); 1234 ad6->sad6_addr6_ph.sph_len = htons(PARM_ADDR6_LEN); 1235 ad6->sad6_addr = sin6->sin6_addr; 1236 } 1237 error = sctp_asconf_add(asc, mp); 1238 if (error != 0) 1239 goto fail; 1240 } 1241 error = sctp_asconf_send(sctp, asc, sctp->sctp_current); 1242 if (error != 0) 1243 goto fail; 1244 1245 return (0); 1246 1247 fail: 1248 sctp_asconf_destroy(asc); 1249 return (error); 1250 } 1251 1252 int 1253 sctp_del_ip(sctp_t *sctp, const void *addrs, uint32_t cnt) 1254 { 1255 struct sockaddr_in *sin4; 1256 struct sockaddr_in6 *sin6; 1257 mblk_t *mp; 1258 int error = 0; 1259 int i; 1260 int addrcnt = 0; 1261 sctp_addip4_t *ad4; 1262 sctp_addip6_t *ad6; 1263 sctp_asconf_t asc[1]; 1264 sctp_saddr_ipif_t *nsp; 1265 uint16_t type = htons(PARM_DEL_IP); 1266 boolean_t v4mapped = B_FALSE; 1267 in6_addr_t addr; 1268 boolean_t asconf = B_TRUE; 1269 1270 /* Does the peer understand ASCONF and Add-IP? */ 1271 if (sctp->sctp_state <= SCTPS_LISTEN || !sctp_addip_enabled || 1272 !sctp->sctp_understands_asconf || !sctp->sctp_understands_addip) { 1273 asconf = B_FALSE; 1274 } 1275 1276 if (asconf) 1277 sctp_asconf_init(asc); 1278 /* 1279 * Screen addresses: 1280 * If adding: 1281 * o Must not already be a part of the association 1282 * o Must be AF_INET or AF_INET6 1283 * o XXX Must be valid source address for this node 1284 * o Must be unicast 1285 * o XXX Must fit scoping rules 1286 * If deleting: 1287 * o Must be part of the association 1288 */ 1289 for (i = 0; i < cnt; i++) { 1290 switch (sctp->sctp_family) { 1291 case AF_INET: 1292 sin4 = (struct sockaddr_in *)addrs + i; 1293 v4mapped = B_TRUE; 1294 IN6_IPADDR_TO_V4MAPPED(sin4->sin_addr.s_addr, &addr); 1295 break; 1296 1297 case AF_INET6: 1298 sin6 = (struct sockaddr_in6 *)addrs + i; 1299 addr = sin6->sin6_addr; 1300 break; 1301 } 1302 nsp = sctp_saddr_lookup(sctp, &addr); 1303 if (nsp == NULL) { 1304 error = EADDRNOTAVAIL; 1305 goto fail; 1306 } 1307 1308 if (!asconf) 1309 continue; 1310 1311 nsp->saddr_ipif_delete_pending = 1; 1312 nsp->saddr_ipif_dontsrc = 1; 1313 addrcnt++; 1314 if (v4mapped) { 1315 mp = allocb(sizeof (*ad4), BPRI_MED); 1316 if (mp == NULL) { 1317 error = ENOMEM; 1318 goto fail; 1319 } 1320 mp->b_wptr += sizeof (*ad4); 1321 ad4 = (sctp_addip4_t *)mp->b_rptr; 1322 ad4->sad4_addip_ph.sph_type = type; 1323 ad4->sad4_addip_ph.sph_len = 1324 htons(sizeof (sctp_parm_hdr_t) + 1325 PARM_ADDR4_LEN + sizeof (ad4->asconf_req_cid)); 1326 ad4->sad4_addr4_ph.sph_type = htons(PARM_ADDR4); 1327 ad4->sad4_addr4_ph.sph_len = htons(PARM_ADDR4_LEN); 1328 ad4->sad4_addr = sin4->sin_addr.s_addr; 1329 } else { 1330 mp = allocb(sizeof (*ad6), BPRI_MED); 1331 if (mp == NULL) { 1332 error = ENOMEM; 1333 goto fail; 1334 } 1335 mp->b_wptr += sizeof (*ad6); 1336 ad6 = (sctp_addip6_t *)mp->b_rptr; 1337 ad6->sad6_addip_ph.sph_type = type; 1338 ad6->sad6_addip_ph.sph_len = 1339 htons(sizeof (sctp_parm_hdr_t) + PARM_ADDR6_LEN + 1340 sizeof (ad6->asconf_req_cid)); 1341 ad6->sad6_addr6_ph.sph_type = htons(PARM_ADDR6); 1342 ad6->sad6_addr6_ph.sph_len = htons(PARM_ADDR6_LEN); 1343 ad6->sad6_addr = addr; 1344 } 1345 1346 error = sctp_asconf_add(asc, mp); 1347 if (error != 0) 1348 goto fail; 1349 } 1350 1351 if (!asconf) { 1352 sctp_del_saddr_list(sctp, addrs, cnt, B_FALSE); 1353 return (0); 1354 } 1355 error = sctp_asconf_send(sctp, asc, sctp->sctp_current); 1356 if (error != 0) 1357 goto fail; 1358 sctp_redo_faddr_srcs(sctp); 1359 return (0); 1360 1361 fail: 1362 if (!asconf) 1363 return (error); 1364 for (i = 0; i < addrcnt; i++) { 1365 switch (sctp->sctp_family) { 1366 case AF_INET: 1367 sin4 = (struct sockaddr_in *)addrs + i; 1368 IN6_INADDR_TO_V4MAPPED(&(sin4->sin_addr), &addr); 1369 break; 1370 case AF_INET6: 1371 sin6 = (struct sockaddr_in6 *)addrs + i; 1372 addr = sin6->sin6_addr; 1373 break; 1374 } 1375 nsp = sctp_saddr_lookup(sctp, &addr); 1376 ASSERT(nsp != NULL); 1377 nsp->saddr_ipif_delete_pending = 0; 1378 nsp->saddr_ipif_dontsrc = 0; 1379 } 1380 sctp_asconf_destroy(asc); 1381 1382 return (error); 1383 } 1384 1385 int 1386 sctp_set_peerprim(sctp_t *sctp, const void *inp, uint_t inlen) 1387 { 1388 const struct sctp_setprim *prim = inp; 1389 const struct sockaddr_storage *ss; 1390 struct sockaddr_in *sin; 1391 struct sockaddr_in6 *sin6; 1392 in6_addr_t addr; 1393 mblk_t *mp; 1394 sctp_saddr_ipif_t *sp; 1395 sctp_addip4_t *ad4; 1396 sctp_addip6_t *ad6; 1397 sctp_asconf_t asc[1]; 1398 int error = 0; 1399 1400 /* Does the peer understand ASCONF and Add-IP? */ 1401 if (!sctp->sctp_understands_asconf || !sctp->sctp_understands_addip) { 1402 return (EOPNOTSUPP); 1403 } 1404 1405 if (inlen < sizeof (*prim)) 1406 return (EINVAL); 1407 1408 /* Don't do anything if we are not connected */ 1409 if (sctp->sctp_state != SCTPS_ESTABLISHED) 1410 return (EINVAL); 1411 1412 ss = &prim->ssp_addr; 1413 sin = NULL; 1414 sin6 = NULL; 1415 if (ss->ss_family == AF_INET) { 1416 sin = (struct sockaddr_in *)ss; 1417 IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &addr); 1418 } else if (ss->ss_family == AF_INET6) { 1419 sin6 = (struct sockaddr_in6 *)ss; 1420 addr = sin6->sin6_addr; 1421 } else { 1422 return (EAFNOSUPPORT); 1423 } 1424 sp = sctp_saddr_lookup(sctp, &addr); 1425 if (sp == NULL) 1426 return (EADDRNOTAVAIL); 1427 sctp_asconf_init(asc); 1428 if (sin) { 1429 mp = allocb(sizeof (*ad4), BPRI_MED); 1430 if (mp == NULL) { 1431 error = ENOMEM; 1432 goto fail; 1433 } 1434 mp->b_wptr += sizeof (*ad4); 1435 ad4 = (sctp_addip4_t *)mp->b_rptr; 1436 ad4->sad4_addip_ph.sph_type = htons(PARM_SET_PRIMARY); 1437 ad4->sad4_addip_ph.sph_len = htons(sizeof (sctp_parm_hdr_t) + 1438 PARM_ADDR4_LEN + sizeof (ad4->asconf_req_cid)); 1439 ad4->sad4_addr4_ph.sph_type = htons(PARM_ADDR4); 1440 ad4->sad4_addr4_ph.sph_len = htons(PARM_ADDR4_LEN); 1441 ad4->sad4_addr = sin->sin_addr.s_addr; 1442 } else { 1443 mp = allocb(sizeof (*ad6), BPRI_MED); 1444 if (mp == NULL) { 1445 error = ENOMEM; 1446 goto fail; 1447 } 1448 mp->b_wptr += sizeof (*ad6); 1449 ad6 = (sctp_addip6_t *)mp->b_rptr; 1450 ad6->sad6_addip_ph.sph_type = htons(PARM_SET_PRIMARY); 1451 ad6->sad6_addip_ph.sph_len = htons(sizeof (sctp_parm_hdr_t) + 1452 PARM_ADDR6_LEN + sizeof (ad6->asconf_req_cid)); 1453 ad6->sad6_addr6_ph.sph_type = htons(PARM_ADDR6); 1454 ad6->sad6_addr6_ph.sph_len = htons(PARM_ADDR6_LEN); 1455 ad6->sad6_addr = sin6->sin6_addr; 1456 } 1457 1458 error = sctp_asconf_add(asc, mp); 1459 if (error != 0) { 1460 goto fail; 1461 } 1462 1463 error = sctp_asconf_send(sctp, asc, sctp->sctp_current); 1464 if (error == 0) { 1465 return (0); 1466 } 1467 1468 fail: 1469 sctp_asconf_destroy(asc); 1470 return (error); 1471 } 1472