xref: /titanic_41/usr/src/uts/common/inet/sctp/sctp_shutdown.c (revision 74e20cfe817b82802b16fac8690dadcda76f54f5)
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 2006 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 			SCTP_KSTAT(sctp_send_shutdown_failed);
114 			goto done;
115 		}
116 		sch = (sctp_chunk_hdr_t *)sendmp->b_wptr;
117 		sendmp->b_wptr += sizeof (*sch) + sizeof (*ctsn);
118 
119 		/* shutdown w/o sack, update lastacked */
120 		sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
121 	}
122 
123 	sch->sch_id = CHUNK_SHUTDOWN;
124 	sch->sch_flags = 0;
125 	sch->sch_len = htons(sizeof (*sch) + sizeof (*ctsn));
126 
127 	ctsn = (uint32_t *)(sch + 1);
128 	*ctsn = htonl(sctp->sctp_lastacked);
129 
130 	/* Link the shutdown chunk in after the IP/SCTP header */
131 
132 	sctp_set_iplen(sctp, sendmp);
133 
134 	BUMP_LOCAL(sctp->sctp_obchunks);
135 
136 	/* Send the shutdown and restart the timer */
137 	sctp_add_sendq(sctp, sendmp);
138 
139 done:
140 	sctp->sctp_state = SCTPS_SHUTDOWN_SENT;
141 	SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
142 	    sctp->sctp_current->rto);
143 }
144 
145 int
146 sctp_shutdown_received(sctp_t *sctp, sctp_chunk_hdr_t *sch, boolean_t crwsd,
147     boolean_t rexmit, sctp_faddr_t *fp)
148 {
149 	mblk_t *samp;
150 	sctp_chunk_hdr_t *sach;
151 	uint32_t *tsn;
152 	int trysend = 0;
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 	if (fp == NULL) {
176 		/* rotate faddrs if we are retransmitting */
177 		if (!rexmit)
178 			fp = sctp->sctp_current;
179 		else
180 			fp = sctp_rotate_faddr(sctp, sctp->sctp_shutdown_faddr);
181 	}
182 	sctp->sctp_shutdown_faddr = fp;
183 
184 	samp = sctp_make_mp(sctp, fp, sizeof (*sach));
185 	if (samp == NULL) {
186 		SCTP_KSTAT(sctp_send_shutdown_ack_failed);
187 		goto dotimer;
188 	}
189 
190 	sach = (sctp_chunk_hdr_t *)samp->b_wptr;
191 	sach->sch_id = CHUNK_SHUTDOWN_ACK;
192 	sach->sch_flags = 0;
193 	sach->sch_len = htons(sizeof (*sach));
194 
195 	samp->b_wptr += sizeof (*sach);
196 
197 	/*
198 	 * bundle a "cookie received while shutting down" error if
199 	 * the caller asks for it.
200 	 */
201 	if (crwsd) {
202 		mblk_t *errmp;
203 
204 		errmp = sctp_make_err(sctp, SCTP_ERR_COOKIE_SHUT, NULL, 0);
205 		if (errmp != NULL) {
206 			linkb(samp, errmp);
207 			BUMP_LOCAL(sctp->sctp_obchunks);
208 		}
209 	}
210 
211 	sctp_set_iplen(sctp, samp);
212 
213 	BUMP_LOCAL(sctp->sctp_obchunks);
214 
215 	sctp_add_sendq(sctp, samp);
216 
217 dotimer:
218 	sctp->sctp_state = SCTPS_SHUTDOWN_ACK_SENT;
219 	SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
220 	    sctp->sctp_current->rto);
221 
222 	return (trysend);
223 }
224 
225 void
226 sctp_shutdown_complete(sctp_t *sctp)
227 {
228 	mblk_t *scmp;
229 	sctp_chunk_hdr_t *scch;
230 
231 	scmp = sctp_make_mp(sctp, NULL, sizeof (*scch));
232 	if (scmp == NULL) {
233 		/* XXX use timer approach */
234 		SCTP_KSTAT(sctp_send_shutdown_comp_failed);
235 		return;
236 	}
237 
238 	scch = (sctp_chunk_hdr_t *)scmp->b_wptr;
239 	scch->sch_id = CHUNK_SHUTDOWN_COMPLETE;
240 	scch->sch_flags = 0;
241 	scch->sch_len = htons(sizeof (*scch));
242 
243 	scmp->b_wptr += sizeof (*scch);
244 
245 	sctp_set_iplen(sctp, scmp);
246 
247 	BUMP_LOCAL(sctp->sctp_obchunks);
248 
249 	sctp_add_sendq(sctp, scmp);
250 }
251 
252 /*
253  * Similar to sctp_shutdown_complete(), except that since this
254  * is out-of-the-blue, we can't use an sctp's association information,
255  * and instead must draw all necessary info from the incoming packet.
256  */
257 void
258 sctp_ootb_shutdown_ack(sctp_t *gsctp, mblk_t *inmp, uint_t ip_hdr_len)
259 {
260 	boolean_t		isv4;
261 	ipha_t			*inip4h;
262 	ip6_t			*inip6h;
263 	sctp_hdr_t		*insctph;
264 	sctp_chunk_hdr_t	*scch;
265 	int			i;
266 	uint16_t		port;
267 	mblk_t			*mp1;
268 
269 	isv4 = (IPH_HDR_VERSION(inmp->b_rptr) == IPV4_VERSION);
270 
271 	/*
272 	 * The gsctp should contain the minimal IP header.  So the
273 	 * incoming mblk should be able to hold the new SCTP packet.
274 	 */
275 	ASSERT(MBLKL(inmp) >= sizeof (*insctph) + sizeof (*scch) +
276 	    (isv4 ? gsctp->sctp_ip_hdr_len : gsctp->sctp_ip_hdr6_len));
277 
278 	/*
279 	 * Check to see if we can reuse the incoming mblk.  There should
280 	 * not be other reference and the db_base of the mblk should be
281 	 * properly aligned.  Since this packet comes from below,
282 	 * there should be enough header space to fill in what the lower
283 	 * layers want to add.  And we will not stash anything there.
284 	 */
285 	if (!IS_P2ALIGNED(DB_BASE(inmp), sizeof (ire_t *)) ||
286 	    DB_REF(inmp) != 1) {
287 		mp1 = allocb(MBLKL(inmp) + sctp_wroff_xtra, BPRI_MED);
288 		if (mp1 == NULL) {
289 			freeb(inmp);
290 			return;
291 		}
292 		mp1->b_rptr += sctp_wroff_xtra;
293 		mp1->b_wptr = mp1->b_rptr + MBLKL(inmp);
294 		bcopy(inmp->b_rptr, mp1->b_rptr, MBLKL(inmp));
295 		freeb(inmp);
296 		inmp = mp1;
297 	} else {
298 		ASSERT(DB_CKSUMFLAGS(inmp) == 0);
299 	}
300 
301 	/*
302 	 * We follow the logic in tcp_xmit_early_reset() in that we skip
303 	 * reversing source route (i.e. relpace all IP options with EOL).
304 	 */
305 	if (isv4) {
306 		ipaddr_t	v4addr;
307 
308 		inip4h = (ipha_t *)inmp->b_rptr;
309 		for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++)
310 			inmp->b_rptr[i] = IPOPT_EOL;
311 		/* Swap addresses */
312 		inip4h->ipha_length = htons(ip_hdr_len + sizeof (*insctph) +
313 		    sizeof (*scch));
314 		v4addr = inip4h->ipha_src;
315 		inip4h->ipha_src = inip4h->ipha_dst;
316 		inip4h->ipha_dst = v4addr;
317 		inip4h->ipha_ident = 0;
318 		inip4h->ipha_ttl = (uchar_t)sctp_ipv4_ttl;
319 	} else {
320 		in6_addr_t	v6addr;
321 
322 		inip6h = (ip6_t *)inmp->b_rptr;
323 		/* Remove any extension headers assuming partial overlay */
324 		if (ip_hdr_len > IPV6_HDR_LEN) {
325 			uint8_t	*to;
326 
327 			to = inmp->b_rptr + ip_hdr_len - IPV6_HDR_LEN;
328 			ovbcopy(inip6h, to, IPV6_HDR_LEN);
329 			inmp->b_rptr += ip_hdr_len - IPV6_HDR_LEN;
330 			ip_hdr_len = IPV6_HDR_LEN;
331 			inip6h = (ip6_t *)inmp->b_rptr;
332 			inip6h->ip6_nxt = IPPROTO_SCTP;
333 		}
334 		inip6h->ip6_plen = htons(ip_hdr_len + sizeof (*insctph) +
335 		    sizeof (*scch) - IPV6_HDR_LEN);
336 		v6addr = inip6h->ip6_src;
337 		inip6h->ip6_src = inip6h->ip6_dst;
338 		inip6h->ip6_dst = v6addr;
339 		inip6h->ip6_hops = (uchar_t)sctp_ipv6_hoplimit;
340 	}
341 	insctph = (sctp_hdr_t *)(inmp->b_rptr + ip_hdr_len);
342 
343 	/* Swap ports.  Verification tag is reused. */
344 	port = insctph->sh_sport;
345 	insctph->sh_sport = insctph->sh_dport;
346 	insctph->sh_dport = port;
347 
348 	/* Lay in the shutdown complete chunk */
349 	scch = (sctp_chunk_hdr_t *)(insctph + 1);
350 	scch->sch_id = CHUNK_SHUTDOWN_COMPLETE;
351 	scch->sch_len = htons(sizeof (*scch));
352 	scch->sch_flags = 0;
353 
354 	/* Set the T-bit */
355 	SCTP_SET_TBIT(scch);
356 
357 	BUMP_LOCAL(gsctp->sctp_obchunks);
358 	/* Nothing to stash... */
359 	SCTP_STASH_IPINFO(inmp, (ire_t *)NULL);
360 
361 	sctp_add_sendq(gsctp, inmp);
362 }
363