10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51735Skcpoon * Common Development and Distribution License (the "License"). 61735Skcpoon * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211735Skcpoon 220Sstevel@tonic-gate /* 231735Skcpoon * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/systm.h> 310Sstevel@tonic-gate #include <sys/stream.h> 320Sstevel@tonic-gate #include <sys/ddi.h> 330Sstevel@tonic-gate #include <sys/sunddi.h> 340Sstevel@tonic-gate #include <sys/strsubr.h> 350Sstevel@tonic-gate #include <sys/strsun.h> 360Sstevel@tonic-gate 370Sstevel@tonic-gate #include <netinet/in.h> 380Sstevel@tonic-gate #include <netinet/ip6.h> 390Sstevel@tonic-gate 400Sstevel@tonic-gate #include <inet/common.h> 410Sstevel@tonic-gate #include <inet/ip.h> 420Sstevel@tonic-gate #include <inet/ip6.h> 430Sstevel@tonic-gate #include <inet/mib2.h> 440Sstevel@tonic-gate #include <inet/nd.h> 450Sstevel@tonic-gate #include <inet/optcom.h> 460Sstevel@tonic-gate #include <inet/sctp_ip.h> 470Sstevel@tonic-gate #include "sctp_impl.h" 480Sstevel@tonic-gate 490Sstevel@tonic-gate void 500Sstevel@tonic-gate sctp_send_shutdown(sctp_t *sctp, int rexmit) 510Sstevel@tonic-gate { 520Sstevel@tonic-gate mblk_t *smp; 530Sstevel@tonic-gate mblk_t *sendmp; 540Sstevel@tonic-gate sctp_chunk_hdr_t *sch; 550Sstevel@tonic-gate uint32_t *ctsn; 560Sstevel@tonic-gate sctp_faddr_t *fp; 570Sstevel@tonic-gate 580Sstevel@tonic-gate if (sctp->sctp_state != SCTPS_ESTABLISHED && 590Sstevel@tonic-gate sctp->sctp_state != SCTPS_SHUTDOWN_PENDING && 600Sstevel@tonic-gate sctp->sctp_state != SCTPS_SHUTDOWN_SENT) { 610Sstevel@tonic-gate return; 620Sstevel@tonic-gate } 630Sstevel@tonic-gate 640Sstevel@tonic-gate if (sctp->sctp_state == SCTPS_ESTABLISHED) { 650Sstevel@tonic-gate sctp->sctp_state = SCTPS_SHUTDOWN_PENDING; 660Sstevel@tonic-gate /* 670Sstevel@tonic-gate * We set an upper bound on how long we will 680Sstevel@tonic-gate * wait for a shutdown-ack from the peer. This 690Sstevel@tonic-gate * is to prevent the receiver from attempting 700Sstevel@tonic-gate * to create a half-closed state indefinately. 710Sstevel@tonic-gate * See archive from IETF TSVWG mailing list 720Sstevel@tonic-gate * for June 2001 for more information. 730Sstevel@tonic-gate * Since we will not be calculating RTTs after 740Sstevel@tonic-gate * sending the shutdown, we can overload out_time 750Sstevel@tonic-gate * to track how long we have waited. 760Sstevel@tonic-gate */ 770Sstevel@tonic-gate sctp->sctp_out_time = lbolt64; 780Sstevel@tonic-gate } 790Sstevel@tonic-gate 800Sstevel@tonic-gate /* 810Sstevel@tonic-gate * If there is unsent (or unacked) data, wait for it to get ack'd 820Sstevel@tonic-gate */ 830Sstevel@tonic-gate if (sctp->sctp_xmit_head != NULL || sctp->sctp_xmit_unsent != NULL) { 840Sstevel@tonic-gate return; 850Sstevel@tonic-gate } 860Sstevel@tonic-gate 870Sstevel@tonic-gate /* rotate faddrs if we are retransmitting */ 880Sstevel@tonic-gate if (!rexmit) { 890Sstevel@tonic-gate fp = sctp->sctp_current; 900Sstevel@tonic-gate } else { 910Sstevel@tonic-gate fp = sctp_rotate_faddr(sctp, sctp->sctp_shutdown_faddr); 920Sstevel@tonic-gate } 930Sstevel@tonic-gate 940Sstevel@tonic-gate sctp->sctp_shutdown_faddr = fp; 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* Link in a SACK if resending the shutdown */ 970Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_SHUTDOWN_PENDING && 980Sstevel@tonic-gate (sendmp = sctp_make_sack(sctp, fp, NULL)) != NULL) { 990Sstevel@tonic-gate 1000Sstevel@tonic-gate smp = allocb(sizeof (*sch) + sizeof (*ctsn), BPRI_MED); 1010Sstevel@tonic-gate if (smp == NULL) { 1020Sstevel@tonic-gate freemsg(sendmp); 1030Sstevel@tonic-gate goto done; 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate linkb(sendmp, smp); 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate sch = (sctp_chunk_hdr_t *)smp->b_rptr; 1080Sstevel@tonic-gate smp->b_wptr = smp->b_rptr + sizeof (*sch) + sizeof (*ctsn); 1090Sstevel@tonic-gate } else { 1100Sstevel@tonic-gate sendmp = sctp_make_mp(sctp, fp, 1110Sstevel@tonic-gate sizeof (*sch) + sizeof (*ctsn)); 1120Sstevel@tonic-gate if (sendmp == NULL) { 1131735Skcpoon SCTP_KSTAT(sctp_send_shutdown_failed); 1140Sstevel@tonic-gate goto done; 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate sch = (sctp_chunk_hdr_t *)sendmp->b_wptr; 1170Sstevel@tonic-gate sendmp->b_wptr += sizeof (*sch) + sizeof (*ctsn); 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* shutdown w/o sack, update lastacked */ 1200Sstevel@tonic-gate sctp->sctp_lastacked = sctp->sctp_ftsn - 1; 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate sch->sch_id = CHUNK_SHUTDOWN; 1240Sstevel@tonic-gate sch->sch_flags = 0; 1250Sstevel@tonic-gate sch->sch_len = htons(sizeof (*sch) + sizeof (*ctsn)); 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate ctsn = (uint32_t *)(sch + 1); 1280Sstevel@tonic-gate *ctsn = htonl(sctp->sctp_lastacked); 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* Link the shutdown chunk in after the IP/SCTP header */ 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate sctp_set_iplen(sctp, sendmp); 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate BUMP_LOCAL(sctp->sctp_obchunks); 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* Send the shutdown and restart the timer */ 1370Sstevel@tonic-gate sctp_add_sendq(sctp, sendmp); 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate done: 1400Sstevel@tonic-gate sctp->sctp_state = SCTPS_SHUTDOWN_SENT; 1410Sstevel@tonic-gate SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current, 1420Sstevel@tonic-gate sctp->sctp_current->rto); 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate int 1461735Skcpoon sctp_shutdown_received(sctp_t *sctp, sctp_chunk_hdr_t *sch, boolean_t crwsd, 1471735Skcpoon boolean_t rexmit, sctp_faddr_t *fp) 1480Sstevel@tonic-gate { 1490Sstevel@tonic-gate mblk_t *samp; 1500Sstevel@tonic-gate sctp_chunk_hdr_t *sach; 1510Sstevel@tonic-gate uint32_t *tsn; 1520Sstevel@tonic-gate int trysend = 0; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate if (sctp->sctp_state != SCTPS_SHUTDOWN_ACK_SENT) 1550Sstevel@tonic-gate sctp->sctp_state = SCTPS_SHUTDOWN_RECEIVED; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* Extract and process the TSN in the shutdown chunk */ 1580Sstevel@tonic-gate if (sch != NULL) { 1590Sstevel@tonic-gate tsn = (uint32_t *)(sch + 1); 1600Sstevel@tonic-gate trysend = sctp_cumack(sctp, ntohl(*tsn), &samp); 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate /* Don't allow sending new data */ 1640Sstevel@tonic-gate if (!SCTP_IS_DETACHED(sctp)) 1650Sstevel@tonic-gate sctp->sctp_ulp_disconnecting(sctp->sctp_ulpd); 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * If there is unsent or unacked data, try sending them out now. 1690Sstevel@tonic-gate * The other side should acknowledge them. After we have flushed 1700Sstevel@tonic-gate * the transmit queue, we can complete the shutdown sequence. 1710Sstevel@tonic-gate */ 1720Sstevel@tonic-gate if (sctp->sctp_xmit_head != NULL || sctp->sctp_xmit_unsent != NULL) 1730Sstevel@tonic-gate return (1); 1740Sstevel@tonic-gate 1751735Skcpoon if (fp == NULL) { 1761735Skcpoon /* rotate faddrs if we are retransmitting */ 1771735Skcpoon if (!rexmit) 1781735Skcpoon fp = sctp->sctp_current; 1791735Skcpoon else 1801735Skcpoon fp = sctp_rotate_faddr(sctp, sctp->sctp_shutdown_faddr); 1811735Skcpoon } 1821735Skcpoon sctp->sctp_shutdown_faddr = fp; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate samp = sctp_make_mp(sctp, fp, sizeof (*sach)); 1851735Skcpoon if (samp == NULL) { 1861735Skcpoon SCTP_KSTAT(sctp_send_shutdown_ack_failed); 1870Sstevel@tonic-gate goto dotimer; 1881735Skcpoon } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate sach = (sctp_chunk_hdr_t *)samp->b_wptr; 1910Sstevel@tonic-gate sach->sch_id = CHUNK_SHUTDOWN_ACK; 1920Sstevel@tonic-gate sach->sch_flags = 0; 1930Sstevel@tonic-gate sach->sch_len = htons(sizeof (*sach)); 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate samp->b_wptr += sizeof (*sach); 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate /* 1980Sstevel@tonic-gate * bundle a "cookie received while shutting down" error if 1990Sstevel@tonic-gate * the caller asks for it. 2000Sstevel@tonic-gate */ 2010Sstevel@tonic-gate if (crwsd) { 2020Sstevel@tonic-gate mblk_t *errmp; 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate errmp = sctp_make_err(sctp, SCTP_ERR_COOKIE_SHUT, NULL, 0); 2050Sstevel@tonic-gate if (errmp != NULL) { 2060Sstevel@tonic-gate linkb(samp, errmp); 2070Sstevel@tonic-gate BUMP_LOCAL(sctp->sctp_obchunks); 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate sctp_set_iplen(sctp, samp); 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate BUMP_LOCAL(sctp->sctp_obchunks); 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate sctp_add_sendq(sctp, samp); 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate dotimer: 2180Sstevel@tonic-gate sctp->sctp_state = SCTPS_SHUTDOWN_ACK_SENT; 2190Sstevel@tonic-gate SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current, 2200Sstevel@tonic-gate sctp->sctp_current->rto); 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate return (trysend); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate void 2260Sstevel@tonic-gate sctp_shutdown_complete(sctp_t *sctp) 2270Sstevel@tonic-gate { 2280Sstevel@tonic-gate mblk_t *scmp; 2290Sstevel@tonic-gate sctp_chunk_hdr_t *scch; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate scmp = sctp_make_mp(sctp, NULL, sizeof (*scch)); 2320Sstevel@tonic-gate if (scmp == NULL) { 2330Sstevel@tonic-gate /* XXX use timer approach */ 2341735Skcpoon SCTP_KSTAT(sctp_send_shutdown_comp_failed); 2350Sstevel@tonic-gate return; 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate scch = (sctp_chunk_hdr_t *)scmp->b_wptr; 2390Sstevel@tonic-gate scch->sch_id = CHUNK_SHUTDOWN_COMPLETE; 2400Sstevel@tonic-gate scch->sch_flags = 0; 2410Sstevel@tonic-gate scch->sch_len = htons(sizeof (*scch)); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate scmp->b_wptr += sizeof (*scch); 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate sctp_set_iplen(sctp, scmp); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate BUMP_LOCAL(sctp->sctp_obchunks); 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate sctp_add_sendq(sctp, scmp); 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * Similar to sctp_shutdown_complete(), except that since this 2540Sstevel@tonic-gate * is out-of-the-blue, we can't use an sctp's association information, 2550Sstevel@tonic-gate * and instead must draw all necessary info from the incoming packet. 2560Sstevel@tonic-gate */ 2570Sstevel@tonic-gate void 2580Sstevel@tonic-gate sctp_ootb_shutdown_ack(sctp_t *gsctp, mblk_t *inmp, uint_t ip_hdr_len) 2590Sstevel@tonic-gate { 2600Sstevel@tonic-gate boolean_t isv4; 2610Sstevel@tonic-gate ipha_t *inip4h; 2620Sstevel@tonic-gate ip6_t *inip6h; 2630Sstevel@tonic-gate sctp_hdr_t *insctph; 2640Sstevel@tonic-gate sctp_chunk_hdr_t *scch; 2650Sstevel@tonic-gate int i; 2660Sstevel@tonic-gate uint16_t port; 2670Sstevel@tonic-gate mblk_t *mp1; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate isv4 = (IPH_HDR_VERSION(inmp->b_rptr) == IPV4_VERSION); 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate /* 2720Sstevel@tonic-gate * The gsctp should contain the minimal IP header. So the 2730Sstevel@tonic-gate * incoming mblk should be able to hold the new SCTP packet. 2740Sstevel@tonic-gate */ 2750Sstevel@tonic-gate ASSERT(MBLKL(inmp) >= sizeof (*insctph) + sizeof (*scch) + 2760Sstevel@tonic-gate (isv4 ? gsctp->sctp_ip_hdr_len : gsctp->sctp_ip_hdr6_len)); 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate /* 2790Sstevel@tonic-gate * Check to see if we can reuse the incoming mblk. There should 2801735Skcpoon * not be other reference and the db_base of the mblk should be 2811735Skcpoon * properly aligned. Since this packet comes from below, 2820Sstevel@tonic-gate * there should be enough header space to fill in what the lower 2830Sstevel@tonic-gate * layers want to add. And we will not stash anything there. 2840Sstevel@tonic-gate */ 2851735Skcpoon if (!IS_P2ALIGNED(DB_BASE(inmp), sizeof (ire_t *)) || 2861735Skcpoon DB_REF(inmp) != 1) { 2870Sstevel@tonic-gate mp1 = allocb(MBLKL(inmp) + sctp_wroff_xtra, BPRI_MED); 2880Sstevel@tonic-gate if (mp1 == NULL) { 2890Sstevel@tonic-gate freeb(inmp); 2900Sstevel@tonic-gate return; 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate mp1->b_rptr += sctp_wroff_xtra; 2930Sstevel@tonic-gate mp1->b_wptr = mp1->b_rptr + MBLKL(inmp); 2940Sstevel@tonic-gate bcopy(inmp->b_rptr, mp1->b_rptr, MBLKL(inmp)); 2950Sstevel@tonic-gate freeb(inmp); 2960Sstevel@tonic-gate inmp = mp1; 297*1932Svi117747 } else { 298*1932Svi117747 ASSERT(DB_CKSUMFLAGS(inmp) == 0); 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate /* 3020Sstevel@tonic-gate * We follow the logic in tcp_xmit_early_reset() in that we skip 3030Sstevel@tonic-gate * reversing source route (i.e. relpace all IP options with EOL). 3040Sstevel@tonic-gate */ 3050Sstevel@tonic-gate if (isv4) { 3060Sstevel@tonic-gate ipaddr_t v4addr; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate inip4h = (ipha_t *)inmp->b_rptr; 3090Sstevel@tonic-gate for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 3100Sstevel@tonic-gate inmp->b_rptr[i] = IPOPT_EOL; 3110Sstevel@tonic-gate /* Swap addresses */ 3120Sstevel@tonic-gate inip4h->ipha_length = htons(ip_hdr_len + sizeof (*insctph) + 3130Sstevel@tonic-gate sizeof (*scch)); 3140Sstevel@tonic-gate v4addr = inip4h->ipha_src; 3150Sstevel@tonic-gate inip4h->ipha_src = inip4h->ipha_dst; 3160Sstevel@tonic-gate inip4h->ipha_dst = v4addr; 3170Sstevel@tonic-gate inip4h->ipha_ident = 0; 3180Sstevel@tonic-gate inip4h->ipha_ttl = (uchar_t)sctp_ipv4_ttl; 3190Sstevel@tonic-gate } else { 3200Sstevel@tonic-gate in6_addr_t v6addr; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate inip6h = (ip6_t *)inmp->b_rptr; 3230Sstevel@tonic-gate /* Remove any extension headers assuming partial overlay */ 3240Sstevel@tonic-gate if (ip_hdr_len > IPV6_HDR_LEN) { 3250Sstevel@tonic-gate uint8_t *to; 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate to = inmp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 3280Sstevel@tonic-gate ovbcopy(inip6h, to, IPV6_HDR_LEN); 3290Sstevel@tonic-gate inmp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 3300Sstevel@tonic-gate ip_hdr_len = IPV6_HDR_LEN; 3310Sstevel@tonic-gate inip6h = (ip6_t *)inmp->b_rptr; 3320Sstevel@tonic-gate inip6h->ip6_nxt = IPPROTO_SCTP; 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate inip6h->ip6_plen = htons(ip_hdr_len + sizeof (*insctph) + 3350Sstevel@tonic-gate sizeof (*scch) - IPV6_HDR_LEN); 3360Sstevel@tonic-gate v6addr = inip6h->ip6_src; 3370Sstevel@tonic-gate inip6h->ip6_src = inip6h->ip6_dst; 3380Sstevel@tonic-gate inip6h->ip6_dst = v6addr; 3390Sstevel@tonic-gate inip6h->ip6_hops = (uchar_t)sctp_ipv6_hoplimit; 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate insctph = (sctp_hdr_t *)(inmp->b_rptr + ip_hdr_len); 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate /* Swap ports. Verification tag is reused. */ 3440Sstevel@tonic-gate port = insctph->sh_sport; 3450Sstevel@tonic-gate insctph->sh_sport = insctph->sh_dport; 3460Sstevel@tonic-gate insctph->sh_dport = port; 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate /* Lay in the shutdown complete chunk */ 3490Sstevel@tonic-gate scch = (sctp_chunk_hdr_t *)(insctph + 1); 3500Sstevel@tonic-gate scch->sch_id = CHUNK_SHUTDOWN_COMPLETE; 3510Sstevel@tonic-gate scch->sch_len = htons(sizeof (*scch)); 3520Sstevel@tonic-gate scch->sch_flags = 0; 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate /* Set the T-bit */ 3550Sstevel@tonic-gate SCTP_SET_TBIT(scch); 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate BUMP_LOCAL(gsctp->sctp_obchunks); 3580Sstevel@tonic-gate /* Nothing to stash... */ 3590Sstevel@tonic-gate SCTP_STASH_IPINFO(inmp, (ire_t *)NULL); 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate sctp_add_sendq(gsctp, inmp); 3620Sstevel@tonic-gate } 363