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