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