xref: /titanic_52/usr/src/uts/common/inet/sctp/sctp_shutdown.c (revision 03494a9880d80f834bec10a1e8f0a2f8f7c97bf4)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/systm.h>
29 #include <sys/stream.h>
30 #include <sys/ddi.h>
31 #include <sys/sunddi.h>
32 #include <sys/strsubr.h>
33 #include <sys/strsun.h>
34 
35 #include <netinet/in.h>
36 #include <netinet/ip6.h>
37 
38 #include <inet/common.h>
39 #include <inet/ip.h>
40 #include <inet/ip6.h>
41 #include <inet/mib2.h>
42 #include <inet/nd.h>
43 #include <inet/optcom.h>
44 #include <inet/sctp_ip.h>
45 #include <inet/ipclassifier.h>
46 #include "sctp_impl.h"
47 
48 void
49 sctp_send_shutdown(sctp_t *sctp, int rexmit)
50 {
51 	mblk_t *smp;
52 	mblk_t *sendmp;
53 	sctp_chunk_hdr_t *sch;
54 	uint32_t *ctsn;
55 	sctp_faddr_t *fp;
56 	sctp_stack_t	*sctps = sctp->sctp_sctps;
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(sctps, 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 	sctp_stack_t	*sctps = sctp->sctp_sctps;
154 
155 	if (sctp->sctp_state != SCTPS_SHUTDOWN_ACK_SENT)
156 		sctp->sctp_state = SCTPS_SHUTDOWN_RECEIVED;
157 
158 	/* Extract and process the TSN in the shutdown chunk */
159 	if (sch != NULL) {
160 		tsn = (uint32_t *)(sch + 1);
161 		trysend = sctp_cumack(sctp, ntohl(*tsn), &samp);
162 	}
163 
164 	/* Don't allow sending new data */
165 	if (!SCTP_IS_DETACHED(sctp) && !sctp->sctp_ulp_discon_done) {
166 		sctp->sctp_ulp_opctl(sctp->sctp_ulpd, SOCK_OPCTL_SHUT_SEND, 0);
167 		sctp->sctp_ulp_discon_done = B_TRUE;
168 	}
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