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