xref: /titanic_51/usr/src/uts/common/inet/sctp/sctp_shutdown.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
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/ddi.h>
33 #include <sys/sunddi.h>
34 #include <sys/strsubr.h>
35 #include <sys/strsun.h>
36 
37 #include <netinet/in.h>
38 #include <netinet/ip6.h>
39 
40 #include <inet/common.h>
41 #include <inet/ip.h>
42 #include <inet/ip6.h>
43 #include <inet/mib2.h>
44 #include <inet/nd.h>
45 #include <inet/optcom.h>
46 #include <inet/sctp_ip.h>
47 #include "sctp_impl.h"
48 
49 void
50 sctp_send_shutdown(sctp_t *sctp, int rexmit)
51 {
52 	mblk_t *smp;
53 	mblk_t *sendmp;
54 	sctp_chunk_hdr_t *sch;
55 	uint32_t *ctsn;
56 	sctp_faddr_t *fp;
57 
58 	if (sctp->sctp_state != SCTPS_ESTABLISHED &&
59 	    sctp->sctp_state != SCTPS_SHUTDOWN_PENDING &&
60 	    sctp->sctp_state != SCTPS_SHUTDOWN_SENT) {
61 		return;
62 	}
63 
64 	if (sctp->sctp_state == SCTPS_ESTABLISHED) {
65 		sctp->sctp_state = SCTPS_SHUTDOWN_PENDING;
66 		/*
67 		 * We set an upper bound on how long we will
68 		 * wait for a shutdown-ack from the peer. This
69 		 * is to prevent the receiver from attempting
70 		 * to create a half-closed state indefinately.
71 		 * See archive from IETF TSVWG mailing list
72 		 * for June 2001 for more information.
73 		 * Since we will not be calculating RTTs after
74 		 * sending the shutdown, we can overload out_time
75 		 * to track how long we have waited.
76 		 */
77 		sctp->sctp_out_time = lbolt64;
78 	}
79 
80 	/*
81 	 * If there is unsent (or unacked) data, wait for it to get ack'd
82 	 */
83 	if (sctp->sctp_xmit_head != NULL || sctp->sctp_xmit_unsent != NULL) {
84 		return;
85 	}
86 
87 	/* rotate faddrs if we are retransmitting */
88 	if (!rexmit) {
89 		fp = sctp->sctp_current;
90 	} else {
91 		fp = sctp_rotate_faddr(sctp, sctp->sctp_shutdown_faddr);
92 	}
93 
94 	sctp->sctp_shutdown_faddr = fp;
95 
96 	/* Link in a SACK if resending the shutdown */
97 	if (sctp->sctp_state > SCTPS_SHUTDOWN_PENDING &&
98 	    (sendmp = sctp_make_sack(sctp, fp, NULL)) != NULL) {
99 
100 		smp = allocb(sizeof (*sch) + sizeof (*ctsn), BPRI_MED);
101 		if (smp == NULL) {
102 			freemsg(sendmp);
103 			goto done;
104 		}
105 		linkb(sendmp, smp);
106 
107 		sch = (sctp_chunk_hdr_t *)smp->b_rptr;
108 		smp->b_wptr = smp->b_rptr + sizeof (*sch) + sizeof (*ctsn);
109 	} else {
110 		sendmp = sctp_make_mp(sctp, fp,
111 		    sizeof (*sch) + sizeof (*ctsn));
112 		if (sendmp == NULL) {
113 			goto done;
114 		}
115 		sch = (sctp_chunk_hdr_t *)sendmp->b_wptr;
116 		sendmp->b_wptr += sizeof (*sch) + sizeof (*ctsn);
117 
118 		/* shutdown w/o sack, update lastacked */
119 		sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
120 	}
121 
122 	sch->sch_id = CHUNK_SHUTDOWN;
123 	sch->sch_flags = 0;
124 	sch->sch_len = htons(sizeof (*sch) + sizeof (*ctsn));
125 
126 	ctsn = (uint32_t *)(sch + 1);
127 	*ctsn = htonl(sctp->sctp_lastacked);
128 
129 	/* Link the shutdown chunk in after the IP/SCTP header */
130 
131 	sctp_set_iplen(sctp, sendmp);
132 
133 	BUMP_LOCAL(sctp->sctp_obchunks);
134 
135 	/* Send the shutdown and restart the timer */
136 	sctp_add_sendq(sctp, sendmp);
137 
138 done:
139 	sctp->sctp_state = SCTPS_SHUTDOWN_SENT;
140 	SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
141 	    sctp->sctp_current->rto);
142 }
143 
144 int
145 sctp_shutdown_received(sctp_t *sctp, sctp_chunk_hdr_t *sch, int crwsd,
146     int rexmit)
147 {
148 	mblk_t *samp;
149 	sctp_chunk_hdr_t *sach;
150 	uint32_t *tsn;
151 	int trysend = 0;
152 	sctp_faddr_t *fp;
153 
154 	if (sctp->sctp_state != SCTPS_SHUTDOWN_ACK_SENT)
155 		sctp->sctp_state = SCTPS_SHUTDOWN_RECEIVED;
156 
157 	/* Extract and process the TSN in the shutdown chunk */
158 	if (sch != NULL) {
159 		tsn = (uint32_t *)(sch + 1);
160 		trysend = sctp_cumack(sctp, ntohl(*tsn), &samp);
161 	}
162 
163 	/* Don't allow sending new data */
164 	if (!SCTP_IS_DETACHED(sctp))
165 		sctp->sctp_ulp_disconnecting(sctp->sctp_ulpd);
166 
167 	/*
168 	 * If there is unsent or unacked data, try sending them out now.
169 	 * The other side should acknowledge them.  After we have flushed
170 	 * the transmit queue, we can complete the shutdown sequence.
171 	 */
172 	if (sctp->sctp_xmit_head != NULL || sctp->sctp_xmit_unsent != NULL)
173 		return (1);
174 
175 	/* rotate faddrs if we are retransmitting */
176 	if (!rexmit)
177 		fp = sctp->sctp_current;
178 	else
179 		fp = sctp_rotate_faddr(sctp, sctp->sctp_shutdown_faddr);
180 
181 	samp = sctp_make_mp(sctp, fp, sizeof (*sach));
182 	if (samp == NULL)
183 		goto dotimer;
184 
185 	sach = (sctp_chunk_hdr_t *)samp->b_wptr;
186 	sach->sch_id = CHUNK_SHUTDOWN_ACK;
187 	sach->sch_flags = 0;
188 	sach->sch_len = htons(sizeof (*sach));
189 
190 	samp->b_wptr += sizeof (*sach);
191 
192 	/*
193 	 * bundle a "cookie received while shutting down" error if
194 	 * the caller asks for it.
195 	 */
196 	if (crwsd) {
197 		mblk_t *errmp;
198 
199 		errmp = sctp_make_err(sctp, SCTP_ERR_COOKIE_SHUT, NULL, 0);
200 		if (errmp != NULL) {
201 			linkb(samp, errmp);
202 			BUMP_LOCAL(sctp->sctp_obchunks);
203 		}
204 	}
205 
206 	sctp_set_iplen(sctp, samp);
207 
208 	BUMP_LOCAL(sctp->sctp_obchunks);
209 
210 	sctp_add_sendq(sctp, samp);
211 
212 dotimer:
213 	sctp->sctp_state = SCTPS_SHUTDOWN_ACK_SENT;
214 	SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
215 	    sctp->sctp_current->rto);
216 
217 	return (trysend);
218 }
219 
220 void
221 sctp_shutdown_complete(sctp_t *sctp)
222 {
223 	mblk_t *scmp;
224 	sctp_chunk_hdr_t *scch;
225 
226 	scmp = sctp_make_mp(sctp, NULL, sizeof (*scch));
227 	if (scmp == NULL) {
228 		/* XXX use timer approach */
229 		return;
230 	}
231 
232 	scch = (sctp_chunk_hdr_t *)scmp->b_wptr;
233 	scch->sch_id = CHUNK_SHUTDOWN_COMPLETE;
234 	scch->sch_flags = 0;
235 	scch->sch_len = htons(sizeof (*scch));
236 
237 	scmp->b_wptr += sizeof (*scch);
238 
239 	sctp_set_iplen(sctp, scmp);
240 
241 	BUMP_LOCAL(sctp->sctp_obchunks);
242 
243 	sctp_add_sendq(sctp, scmp);
244 }
245 
246 /*
247  * Similar to sctp_shutdown_complete(), except that since this
248  * is out-of-the-blue, we can't use an sctp's association information,
249  * and instead must draw all necessary info from the incoming packet.
250  */
251 void
252 sctp_ootb_shutdown_ack(sctp_t *gsctp, mblk_t *inmp, uint_t ip_hdr_len)
253 {
254 	boolean_t		isv4;
255 	ipha_t			*inip4h;
256 	ip6_t			*inip6h;
257 	sctp_hdr_t		*insctph;
258 	sctp_chunk_hdr_t	*scch;
259 	int			i;
260 	uint16_t		port;
261 	mblk_t			*mp1;
262 
263 	isv4 = (IPH_HDR_VERSION(inmp->b_rptr) == IPV4_VERSION);
264 
265 	/*
266 	 * The gsctp should contain the minimal IP header.  So the
267 	 * incoming mblk should be able to hold the new SCTP packet.
268 	 */
269 	ASSERT(MBLKL(inmp) >= sizeof (*insctph) + sizeof (*scch) +
270 	    (isv4 ? gsctp->sctp_ip_hdr_len : gsctp->sctp_ip_hdr6_len));
271 
272 	/*
273 	 * Check to see if we can reuse the incoming mblk.  There should
274 	 * not be other reference.  Since this packet comes from below,
275 	 * there should be enough header space to fill in what the lower
276 	 * layers want to add.  And we will not stash anything there.
277 	 */
278 	if (DB_REF(inmp) != 1) {
279 		mp1 = allocb(MBLKL(inmp) + sctp_wroff_xtra, BPRI_MED);
280 		if (mp1 == NULL) {
281 			freeb(inmp);
282 			return;
283 		}
284 		mp1->b_rptr += sctp_wroff_xtra;
285 		mp1->b_wptr = mp1->b_rptr + MBLKL(inmp);
286 		bcopy(inmp->b_rptr, mp1->b_rptr, MBLKL(inmp));
287 		freeb(inmp);
288 		inmp = mp1;
289 	}
290 
291 	/*
292 	 * We follow the logic in tcp_xmit_early_reset() in that we skip
293 	 * reversing source route (i.e. relpace all IP options with EOL).
294 	 */
295 	if (isv4) {
296 		ipaddr_t	v4addr;
297 
298 		inip4h = (ipha_t *)inmp->b_rptr;
299 		for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++)
300 			inmp->b_rptr[i] = IPOPT_EOL;
301 		/* Swap addresses */
302 		inip4h->ipha_length = htons(ip_hdr_len + sizeof (*insctph) +
303 		    sizeof (*scch));
304 		v4addr = inip4h->ipha_src;
305 		inip4h->ipha_src = inip4h->ipha_dst;
306 		inip4h->ipha_dst = v4addr;
307 		inip4h->ipha_ident = 0;
308 		inip4h->ipha_ttl = (uchar_t)sctp_ipv4_ttl;
309 	} else {
310 		in6_addr_t	v6addr;
311 
312 		inip6h = (ip6_t *)inmp->b_rptr;
313 		/* Remove any extension headers assuming partial overlay */
314 		if (ip_hdr_len > IPV6_HDR_LEN) {
315 			uint8_t	*to;
316 
317 			to = inmp->b_rptr + ip_hdr_len - IPV6_HDR_LEN;
318 			ovbcopy(inip6h, to, IPV6_HDR_LEN);
319 			inmp->b_rptr += ip_hdr_len - IPV6_HDR_LEN;
320 			ip_hdr_len = IPV6_HDR_LEN;
321 			inip6h = (ip6_t *)inmp->b_rptr;
322 			inip6h->ip6_nxt = IPPROTO_SCTP;
323 		}
324 		inip6h->ip6_plen = htons(ip_hdr_len + sizeof (*insctph) +
325 		    sizeof (*scch) - IPV6_HDR_LEN);
326 		v6addr = inip6h->ip6_src;
327 		inip6h->ip6_src = inip6h->ip6_dst;
328 		inip6h->ip6_dst = v6addr;
329 		inip6h->ip6_hops = (uchar_t)sctp_ipv6_hoplimit;
330 	}
331 	insctph = (sctp_hdr_t *)(inmp->b_rptr + ip_hdr_len);
332 
333 	/* Swap ports.  Verification tag is reused. */
334 	port = insctph->sh_sport;
335 	insctph->sh_sport = insctph->sh_dport;
336 	insctph->sh_dport = port;
337 
338 	/* Lay in the shutdown complete chunk */
339 	scch = (sctp_chunk_hdr_t *)(insctph + 1);
340 	scch->sch_id = CHUNK_SHUTDOWN_COMPLETE;
341 	scch->sch_len = htons(sizeof (*scch));
342 	scch->sch_flags = 0;
343 
344 	/* Set the T-bit */
345 	SCTP_SET_TBIT(scch);
346 
347 	BUMP_LOCAL(gsctp->sctp_obchunks);
348 	/* Nothing to stash... */
349 	SCTP_STASH_IPINFO(inmp, (ire_t *)NULL);
350 
351 	sctp_add_sendq(gsctp, inmp);
352 }
353