xref: /titanic_50/usr/src/uts/common/inet/sctp/sctp_shutdown.c (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
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) && !sctp->sctp_ulp_discon_done) {
168 		sctp->sctp_ulp_disconnecting(sctp->sctp_ulpd);
169 		sctp->sctp_ulp_discon_done = B_TRUE;
170 	}
171 
172 	/*
173 	 * If there is unsent or unacked data, try sending them out now.
174 	 * The other side should acknowledge them.  After we have flushed
175 	 * the transmit queue, we can complete the shutdown sequence.
176 	 */
177 	if (sctp->sctp_xmit_head != NULL || sctp->sctp_xmit_unsent != NULL)
178 		return (1);
179 
180 	if (fp == NULL) {
181 		/* rotate faddrs if we are retransmitting */
182 		if (!rexmit)
183 			fp = sctp->sctp_current;
184 		else
185 			fp = sctp_rotate_faddr(sctp, sctp->sctp_shutdown_faddr);
186 	}
187 	sctp->sctp_shutdown_faddr = fp;
188 
189 	samp = sctp_make_mp(sctp, fp, sizeof (*sach));
190 	if (samp == NULL) {
191 		SCTP_KSTAT(sctps, sctp_send_shutdown_ack_failed);
192 		goto dotimer;
193 	}
194 
195 	sach = (sctp_chunk_hdr_t *)samp->b_wptr;
196 	sach->sch_id = CHUNK_SHUTDOWN_ACK;
197 	sach->sch_flags = 0;
198 	sach->sch_len = htons(sizeof (*sach));
199 
200 	samp->b_wptr += sizeof (*sach);
201 
202 	/*
203 	 * bundle a "cookie received while shutting down" error if
204 	 * the caller asks for it.
205 	 */
206 	if (crwsd) {
207 		mblk_t *errmp;
208 
209 		errmp = sctp_make_err(sctp, SCTP_ERR_COOKIE_SHUT, NULL, 0);
210 		if (errmp != NULL) {
211 			linkb(samp, errmp);
212 			BUMP_LOCAL(sctp->sctp_obchunks);
213 		}
214 	}
215 
216 	sctp_set_iplen(sctp, samp);
217 
218 	BUMP_LOCAL(sctp->sctp_obchunks);
219 
220 	sctp_add_sendq(sctp, samp);
221 
222 dotimer:
223 	sctp->sctp_state = SCTPS_SHUTDOWN_ACK_SENT;
224 	SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
225 	    sctp->sctp_current->rto);
226 
227 	return (trysend);
228 }
229 
230 void
231 sctp_shutdown_complete(sctp_t *sctp)
232 {
233 	mblk_t *scmp;
234 	sctp_chunk_hdr_t *scch;
235 	sctp_stack_t	*sctps = sctp->sctp_sctps;
236 
237 	scmp = sctp_make_mp(sctp, NULL, sizeof (*scch));
238 	if (scmp == NULL) {
239 		/* XXX use timer approach */
240 		SCTP_KSTAT(sctps, sctp_send_shutdown_comp_failed);
241 		return;
242 	}
243 
244 	scch = (sctp_chunk_hdr_t *)scmp->b_wptr;
245 	scch->sch_id = CHUNK_SHUTDOWN_COMPLETE;
246 	scch->sch_flags = 0;
247 	scch->sch_len = htons(sizeof (*scch));
248 
249 	scmp->b_wptr += sizeof (*scch);
250 
251 	sctp_set_iplen(sctp, scmp);
252 
253 	BUMP_LOCAL(sctp->sctp_obchunks);
254 
255 	sctp_add_sendq(sctp, scmp);
256 }
257 
258 /*
259  * Similar to sctp_shutdown_complete(), except that since this
260  * is out-of-the-blue, we can't use an sctp's association information,
261  * and instead must draw all necessary info from the incoming packet.
262  */
263 void
264 sctp_ootb_shutdown_ack(sctp_t *gsctp, mblk_t *inmp, uint_t ip_hdr_len)
265 {
266 	boolean_t		isv4;
267 	ipha_t			*inip4h;
268 	ip6_t			*inip6h;
269 	sctp_hdr_t		*insctph;
270 	sctp_chunk_hdr_t	*scch;
271 	int			i;
272 	uint16_t		port;
273 	mblk_t			*mp1;
274 	sctp_stack_t	*sctps = gsctp->sctp_sctps;
275 
276 	isv4 = (IPH_HDR_VERSION(inmp->b_rptr) == IPV4_VERSION);
277 
278 	/*
279 	 * The gsctp should contain the minimal IP header.  So the
280 	 * incoming mblk should be able to hold the new SCTP packet.
281 	 */
282 	ASSERT(MBLKL(inmp) >= sizeof (*insctph) + sizeof (*scch) +
283 	    (isv4 ? gsctp->sctp_ip_hdr_len : gsctp->sctp_ip_hdr6_len));
284 
285 	/*
286 	 * Check to see if we can reuse the incoming mblk.  There should
287 	 * not be other reference and the db_base of the mblk should be
288 	 * properly aligned.  Since this packet comes from below,
289 	 * there should be enough header space to fill in what the lower
290 	 * layers want to add.  And we will not stash anything there.
291 	 */
292 	if (!IS_P2ALIGNED(DB_BASE(inmp), sizeof (ire_t *)) ||
293 	    DB_REF(inmp) != 1) {
294 		mp1 = allocb(MBLKL(inmp) + sctps->sctps_wroff_xtra, BPRI_MED);
295 		if (mp1 == NULL) {
296 			freeb(inmp);
297 			return;
298 		}
299 		mp1->b_rptr += sctps->sctps_wroff_xtra;
300 		mp1->b_wptr = mp1->b_rptr + MBLKL(inmp);
301 		bcopy(inmp->b_rptr, mp1->b_rptr, MBLKL(inmp));
302 		freeb(inmp);
303 		inmp = mp1;
304 	} else {
305 		ASSERT(DB_CKSUMFLAGS(inmp) == 0);
306 	}
307 
308 	/*
309 	 * We follow the logic in tcp_xmit_early_reset() in that we skip
310 	 * reversing source route (i.e. relpace all IP options with EOL).
311 	 */
312 	if (isv4) {
313 		ipaddr_t	v4addr;
314 
315 		inip4h = (ipha_t *)inmp->b_rptr;
316 		for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++)
317 			inmp->b_rptr[i] = IPOPT_EOL;
318 		/* Swap addresses */
319 		inip4h->ipha_length = htons(ip_hdr_len + sizeof (*insctph) +
320 		    sizeof (*scch));
321 		v4addr = inip4h->ipha_src;
322 		inip4h->ipha_src = inip4h->ipha_dst;
323 		inip4h->ipha_dst = v4addr;
324 		inip4h->ipha_ident = 0;
325 		inip4h->ipha_ttl = (uchar_t)sctps->sctps_ipv4_ttl;
326 	} else {
327 		in6_addr_t	v6addr;
328 
329 		inip6h = (ip6_t *)inmp->b_rptr;
330 		/* Remove any extension headers assuming partial overlay */
331 		if (ip_hdr_len > IPV6_HDR_LEN) {
332 			uint8_t	*to;
333 
334 			to = inmp->b_rptr + ip_hdr_len - IPV6_HDR_LEN;
335 			ovbcopy(inip6h, to, IPV6_HDR_LEN);
336 			inmp->b_rptr += ip_hdr_len - IPV6_HDR_LEN;
337 			ip_hdr_len = IPV6_HDR_LEN;
338 			inip6h = (ip6_t *)inmp->b_rptr;
339 			inip6h->ip6_nxt = IPPROTO_SCTP;
340 		}
341 		inip6h->ip6_plen = htons(ip_hdr_len + sizeof (*insctph) +
342 		    sizeof (*scch) - IPV6_HDR_LEN);
343 		v6addr = inip6h->ip6_src;
344 		inip6h->ip6_src = inip6h->ip6_dst;
345 		inip6h->ip6_dst = v6addr;
346 		inip6h->ip6_hops = (uchar_t)sctps->sctps_ipv6_hoplimit;
347 	}
348 	insctph = (sctp_hdr_t *)(inmp->b_rptr + ip_hdr_len);
349 
350 	/* Swap ports.  Verification tag is reused. */
351 	port = insctph->sh_sport;
352 	insctph->sh_sport = insctph->sh_dport;
353 	insctph->sh_dport = port;
354 
355 	/* Lay in the shutdown complete chunk */
356 	scch = (sctp_chunk_hdr_t *)(insctph + 1);
357 	scch->sch_id = CHUNK_SHUTDOWN_COMPLETE;
358 	scch->sch_len = htons(sizeof (*scch));
359 	scch->sch_flags = 0;
360 
361 	/* Set the T-bit */
362 	SCTP_SET_TBIT(scch);
363 
364 	BUMP_LOCAL(gsctp->sctp_obchunks);
365 	/* Nothing to stash... */
366 	SCTP_STASH_IPINFO(inmp, (ire_t *)NULL);
367 
368 	sctp_add_sendq(gsctp, inmp);
369 }
370