xref: /titanic_52/usr/src/uts/common/inet/sctp/sctp_notify.c (revision 06e46062ef4f5f4b687cbafb4518fb123fe23920)
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 2005 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/cmn_err.h>
33 #include <sys/tihdr.h>
34 #include <sys/kmem.h>
35 #define	_SUN_TPI_VERSION 2
36 #include <sys/tihdr.h>
37 #include <sys/socket.h>
38 
39 #include <netinet/in.h>
40 #include <netinet/sctp.h>
41 
42 #include <inet/common.h>
43 #include <inet/ip.h>
44 #include "sctp_impl.h"
45 
46 static void
47 sctp_notify(sctp_t *sctp, mblk_t *emp, size_t len)
48 {
49 	struct T_unitdata_ind *tudi;
50 	mblk_t *mp;
51 	sctp_faddr_t *fp;
52 	int32_t rwnd = 0;
53 
54 	if ((mp = allocb(sizeof (*tudi) + sizeof (void *) +
55 		sizeof (struct sockaddr_in6), BPRI_HI)) == NULL) {
56 		/* XXX trouble: don't want to drop events. should queue it. */
57 		freemsg(emp);
58 		return;
59 	}
60 	dprint(3, ("sctp_notify: event %d\n", (*(uint16_t *)emp->b_rptr)));
61 
62 	mp->b_datap->db_type = M_PROTO;
63 	mp->b_flag |= MSGMARK;
64 	mp->b_rptr += sizeof (void *); /* pointer worth of padding */
65 
66 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
67 	tudi->PRIM_type = T_UNITDATA_IND;
68 	tudi->SRC_offset = sizeof (*tudi);
69 	tudi->OPT_length = 0;
70 	tudi->OPT_offset = 0;
71 
72 	fp = sctp->sctp_primary;
73 	ASSERT(fp);
74 
75 	/*
76 	 * Fill in primary remote address.
77 	 */
78 	if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
79 		struct sockaddr_in *sin4;
80 
81 		tudi->SRC_length = sizeof (*sin4);
82 		sin4 = (struct sockaddr_in *)(tudi + 1);
83 		sin4->sin_family = AF_INET;
84 		sin4->sin_port = sctp->sctp_fport;
85 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr, sin4->sin_addr.s_addr);
86 		mp->b_wptr = (uchar_t *)(sin4 + 1);
87 	} else {
88 		struct sockaddr_in6 *sin6;
89 
90 		tudi->SRC_length = sizeof (*sin6);
91 		sin6 = (struct sockaddr_in6 *)(tudi + 1);
92 		sin6->sin6_family = AF_INET6;
93 		sin6->sin6_port = sctp->sctp_fport;
94 		sin6->sin6_addr = fp->faddr;
95 		mp->b_wptr = (uchar_t *)(sin6 + 1);
96 	}
97 
98 	mp->b_cont = emp;
99 
100 	/*
101 	 * Notifications are queued regardless of socket rx space.  So
102 	 * we do not decrement sctp_rwnd here as this will confuse the
103 	 * other side.
104 	 */
105 #ifdef DEBUG
106 	for (emp = mp->b_cont; emp; emp = emp->b_cont) {
107 		rwnd += emp->b_wptr - emp->b_rptr;
108 	}
109 	ASSERT(len == rwnd);
110 #endif
111 
112 	rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd, mp, SCTP_NOTIFICATION);
113 	if (rwnd > sctp->sctp_rwnd) {
114 		sctp->sctp_rwnd = rwnd;
115 	}
116 }
117 
118 void
119 sctp_assoc_event(sctp_t *sctp, uint16_t state, uint16_t error,
120     sctp_chunk_hdr_t *ch)
121 {
122 	struct sctp_assoc_change *sacp;
123 	mblk_t *mp;
124 	uint16_t ch_len;
125 
126 	if (!sctp->sctp_recvassocevnt) {
127 		return;
128 	}
129 
130 	ch_len = (ch != NULL) ? ntohs(ch->sch_len) : 0;
131 
132 	if ((mp = allocb(sizeof (*sacp) + ch_len, BPRI_MED)) == NULL) {
133 		return;
134 	}
135 
136 	sacp = (struct sctp_assoc_change *)mp->b_rptr;
137 	sacp->sac_type = SCTP_ASSOC_CHANGE;
138 	sacp->sac_flags = sctp->sctp_prsctp_aware ? SCTP_PRSCTP_CAPABLE : 0;
139 	sacp->sac_length = sizeof (*sacp) + ch_len;
140 	sacp->sac_state = state;
141 	sacp->sac_error = error;
142 	sacp->sac_outbound_streams = sctp->sctp_num_ostr;
143 	sacp->sac_inbound_streams = sctp->sctp_num_istr;
144 	sacp->sac_assoc_id = 0;
145 
146 	if (ch != NULL)
147 		bcopy(ch, sacp + 1, ch_len);
148 	mp->b_wptr += sacp->sac_length;
149 	sctp_notify(sctp, mp, sacp->sac_length);
150 }
151 
152 /*
153  * Send failure event. Message is expected to have message header still
154  * in place, data follows in subsequent mblk's.
155  */
156 static void
157 sctp_sendfail(sctp_t *sctp, mblk_t *msghdr, uint16_t flags, int error)
158 {
159 	struct sctp_send_failed *sfp;
160 	mblk_t *mp;
161 	sctp_msg_hdr_t *smh;
162 
163 	/* Allocate a mblk for the notification header */
164 	if ((mp = allocb(sizeof (*sfp), BPRI_MED)) == NULL) {
165 		/* give up */
166 		freemsg(msghdr);
167 		return;
168 	}
169 
170 	smh = (sctp_msg_hdr_t *)msghdr->b_rptr;
171 	sfp = (struct sctp_send_failed *)mp->b_rptr;
172 	sfp->ssf_type = SCTP_SEND_FAILED;
173 	sfp->ssf_flags = flags;
174 	sfp->ssf_length = smh->smh_msglen + sizeof (*sfp);
175 	sfp->ssf_error = error;
176 	sfp->ssf_assoc_id = 0;
177 
178 	bzero(&sfp->ssf_info, sizeof (sfp->ssf_info));
179 	sfp->ssf_info.sinfo_stream = smh->smh_sid;
180 	sfp->ssf_info.sinfo_flags = smh->smh_flags;
181 	sfp->ssf_info.sinfo_ppid = smh->smh_ppid;
182 	sfp->ssf_info.sinfo_context = smh->smh_context;
183 	sfp->ssf_info.sinfo_timetolive = TICK_TO_MSEC(smh->smh_ttl);
184 
185 	mp->b_wptr = (uchar_t *)(sfp + 1);
186 	mp->b_cont = msghdr->b_cont;
187 
188 	freeb(msghdr);
189 
190 	sctp_notify(sctp, mp, sfp->ssf_length);
191 
192 }
193 
194 /*
195  * Send failure when the message has been fully chunkified.
196  */
197 static void
198 sctp_sendfail_sent(sctp_t *sctp, mblk_t *meta, int error)
199 {
200 	mblk_t		*mp;
201 	mblk_t		*nmp;
202 	mblk_t		*tail;
203 	uint16_t	flags = SCTP_DATA_SENT;
204 
205 	if (!sctp->sctp_recvsendfailevnt) {
206 		sctp_free_msg(meta);
207 		return;
208 	}
209 
210 	/*
211 	 * We need to remove all data_hdr's.
212 	 */
213 	nmp = meta->b_cont;
214 	tail = meta;
215 	do {
216 		mp = nmp->b_next;
217 		nmp->b_next = NULL;
218 
219 		/*
220 		 * If one of the chunks hasn't been sent yet, say that
221 		 * the message hasn't been sent.
222 		 */
223 		if (!SCTP_CHUNK_ISSENT(nmp)) {
224 			flags = SCTP_DATA_UNSENT;
225 		}
226 		nmp->b_rptr += sizeof (sctp_data_hdr_t);
227 		if (nmp->b_rptr == nmp->b_wptr) {
228 			tail->b_cont = nmp->b_cont;
229 			freeb(nmp);
230 		} else {
231 			tail->b_cont = nmp;
232 		}
233 		while (tail->b_cont) {
234 			tail = tail->b_cont;
235 		}
236 	} while ((nmp = mp) != NULL);
237 
238 	sctp_sendfail(sctp, meta, flags, error);
239 }
240 
241 /*
242  * Send failure when the message hasn't been fully chunkified.
243  */
244 void
245 sctp_sendfail_event(sctp_t *sctp, mblk_t *meta, int error, boolean_t chunkified)
246 {
247 	mblk_t	*mp;
248 	mblk_t	*nmp;
249 	mblk_t	*tail;
250 
251 	if (meta == NULL)
252 		return;
253 
254 	if (!sctp->sctp_recvsendfailevnt) {
255 		sctp_free_msg(meta);
256 		return;
257 	}
258 
259 	/* If the message is fully chunkified */
260 	if (chunkified) {
261 		sctp_sendfail_sent(sctp, meta, error);
262 		return;
263 	}
264 	/*
265 	 * Message might be partially chunkified, we need to remove
266 	 * all data_hdr's.
267 	 */
268 	mp = meta->b_cont;
269 	tail = meta;
270 	while ((nmp = mp->b_next) != NULL) {
271 		mp->b_next = nmp->b_next;
272 		nmp->b_next = NULL;
273 		nmp->b_rptr += sizeof (sctp_data_hdr_t);
274 		if (nmp->b_rptr == nmp->b_wptr) {
275 			tail->b_cont = nmp->b_cont;
276 			freeb(nmp);
277 		} else {
278 			tail->b_cont = nmp;
279 		}
280 		while (tail->b_cont) {
281 			tail = tail->b_cont;
282 		}
283 	}
284 	tail->b_cont = mp;
285 
286 	sctp_sendfail(sctp, meta, SCTP_DATA_UNSENT, error);
287 }
288 
289 void
290 sctp_regift_xmitlist(sctp_t *sctp)
291 {
292 	mblk_t *mp;
293 
294 	if (!sctp->sctp_recvsendfailevnt) {
295 		return;
296 	}
297 
298 	while ((mp = sctp->sctp_xmit_head) != NULL) {
299 		sctp->sctp_xmit_head = mp->b_next;
300 		mp->b_next = NULL;
301 		if (sctp->sctp_xmit_head != NULL)
302 			sctp->sctp_xmit_head->b_prev = NULL;
303 		sctp_sendfail_event(sctp, mp, 0, B_TRUE);
304 	}
305 	while ((mp = sctp->sctp_xmit_unsent) != NULL) {
306 		sctp->sctp_xmit_unsent = mp->b_next;
307 		mp->b_next = NULL;
308 		sctp_sendfail_event(sctp, mp, 0, B_FALSE);
309 	}
310 	sctp->sctp_xmit_tail = sctp->sctp_xmit_unsent_tail = NULL;
311 	sctp->sctp_unacked = sctp->sctp_unsent = 0;
312 }
313 
314 void
315 sctp_intf_event(sctp_t *sctp, in6_addr_t addr, int state, int error)
316 {
317 	struct sctp_paddr_change *spc;
318 	ipaddr_t addr4;
319 	struct sockaddr_in *sin;
320 	struct sockaddr_in6 *sin6;
321 	mblk_t *mp;
322 
323 	if (!sctp->sctp_recvpathevnt) {
324 		return;
325 	}
326 
327 	if ((mp = allocb(sizeof (*spc), BPRI_MED)) == NULL) {
328 		return;
329 	}
330 
331 	spc = (struct sctp_paddr_change *)mp->b_rptr;
332 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
333 	spc->spc_flags = 0;
334 	spc->spc_length = sizeof (*spc);
335 	if (IN6_IS_ADDR_V4MAPPED(&addr)) {
336 		IN6_V4MAPPED_TO_IPADDR(&addr, addr4);
337 		sin = (struct sockaddr_in *)&spc->spc_aaddr;
338 		sin->sin_family = AF_INET;
339 		sin->sin_port = 0;
340 		sin->sin_addr.s_addr = addr4;
341 	} else {
342 		sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
343 		sin6->sin6_family = AF_INET6;
344 		sin6->sin6_port = 0;
345 		sin6->sin6_addr = addr;
346 	}
347 	spc->spc_state = state;
348 	spc->spc_error = error;
349 	spc->spc_assoc_id = 0;
350 
351 	mp->b_wptr = (uchar_t *)(spc + 1);
352 	sctp_notify(sctp, mp, spc->spc_length);
353 }
354 
355 void
356 sctp_error_event(sctp_t *sctp, sctp_chunk_hdr_t *ch)
357 {
358 	struct sctp_remote_error *sre;
359 	mblk_t *mp;
360 	size_t len;
361 	sctp_parm_hdr_t *errh;
362 	uint16_t dlen = 0;
363 	uint16_t error = 0;
364 	void *dtail = NULL;
365 
366 	if (!sctp->sctp_recvpeererr) {
367 		return;
368 	}
369 
370 	if (ntohs(ch->sch_len) > sizeof (*ch)) {
371 		errh = (sctp_parm_hdr_t *)(ch + 1);
372 		error = ntohs(errh->sph_type);
373 		dlen = ntohs(errh->sph_len) - sizeof (*errh);
374 		if (dlen > 0) {
375 			dtail = errh + 1;
376 		}
377 	}
378 
379 	len = sizeof (*sre) + dlen;
380 	if ((mp = allocb(len, BPRI_MED)) == NULL) {
381 		return;
382 	}
383 
384 	sre = (struct sctp_remote_error *)mp->b_rptr;
385 	sre->sre_type = SCTP_REMOTE_ERROR;
386 	sre->sre_flags = 0;
387 	sre->sre_length = len;
388 	sre->sre_assoc_id = 0;
389 	sre->sre_error = error;
390 	if (dtail) {
391 		bcopy(dtail, sre + 1, dlen);
392 	}
393 
394 	mp->b_wptr = mp->b_rptr + len;
395 	sctp_notify(sctp, mp, len);
396 }
397 
398 void
399 sctp_shutdown_event(sctp_t *sctp)
400 {
401 	struct sctp_shutdown_event *sse;
402 	mblk_t *mp;
403 
404 	if (!sctp->sctp_recvshutdownevnt) {
405 		return;
406 	}
407 
408 	if ((mp = allocb(sizeof (*sse), BPRI_MED)) == NULL) {
409 		return;
410 	}
411 
412 	sse = (struct sctp_shutdown_event *)mp->b_rptr;
413 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
414 	sse->sse_flags = 0;
415 	sse->sse_length = sizeof (*sse);
416 	sse->sse_assoc_id = 0;
417 
418 	mp->b_wptr = (uchar_t *)(sse + 1);
419 	sctp_notify(sctp, mp, sse->sse_length);
420 }
421 
422 void
423 sctp_adaption_event(sctp_t *sctp)
424 {
425 	struct sctp_adaption_event *sai;
426 	mblk_t *mp;
427 
428 	if (!sctp->sctp_recvalevnt || !sctp->sctp_recv_adaption) {
429 		return;
430 	}
431 	if ((mp = allocb(sizeof (*sai), BPRI_MED)) == NULL) {
432 		return;
433 	}
434 
435 	sai = (struct sctp_adaption_event *)mp->b_rptr;
436 	sai->sai_type = SCTP_ADAPTION_INDICATION;
437 	sai->sai_flags = 0;
438 	sai->sai_length = sizeof (*sai);
439 	sai->sai_assoc_id = 0;
440 	/*
441 	 * Adaptation code delivered in network byte order.
442 	 */
443 	sai->sai_adaption_ind = sctp->sctp_rx_adaption_code;
444 
445 	mp->b_wptr = (uchar_t *)(sai + 1);
446 	sctp_notify(sctp, mp, sai->sai_length);
447 
448 	sctp->sctp_recv_adaption = 0; /* in case there's a restart later */
449 }
450 
451 /* Send partial deliver event */
452 void
453 sctp_partial_delivery_event(sctp_t *sctp)
454 {
455 	struct sctp_pdapi_event	*pdapi;
456 	mblk_t			*mp;
457 
458 	if (!sctp->sctp_recvpdevnt)
459 		return;
460 
461 	if ((mp = allocb(sizeof (*pdapi), BPRI_MED)) == NULL)
462 		return;
463 
464 	pdapi = (struct sctp_pdapi_event *)mp->b_rptr;
465 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
466 	pdapi->pdapi_flags = 0;
467 	pdapi->pdapi_length = sizeof (*pdapi);
468 	pdapi->pdapi_indication = SCTP_PARTIAL_DELIVERY_ABORTED;
469 	pdapi->pdapi_assoc_id = 0;
470 	mp->b_wptr = (uchar_t *)(pdapi + 1);
471 	sctp_notify(sctp, mp, pdapi->pdapi_length);
472 }
473