2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctputil.h>
41 #include <netinet/sctp_var.h>
42 #include <netinet/sctp_sysctl.h>
44 #include <netinet6/sctp6_var.h>
46 #include <netinet/sctp_header.h>
47 #include <netinet/sctp_output.h>
48 #include <netinet/sctp_uio.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_indata.h>
51 #include <netinet/sctp_auth.h>
52 #include <netinet/sctp_asconf.h>
53 #include <netinet/sctp_bsd_addr.h>
54 #include <netinet/sctp_kdtrace.h>
55 #if defined(INET6) || defined(INET)
56 #include <netinet/tcp_var.h>
58 #include <netinet/udp.h>
59 #include <netinet/udp_var.h>
62 #include <netinet/icmp6.h>
67 #define KTR_SCTP KTR_SUBSYS
70 extern const struct sctp_cc_functions sctp_cc_functions[];
71 extern const struct sctp_ss_functions sctp_ss_functions[];
74 sctp_sblog(struct sockbuf *sb, struct sctp_tcb *stcb, int from, int incr)
76 #if defined(SCTP_LOCAL_TRACE_BUF)
77 struct sctp_cwnd_log sctp_clog;
79 sctp_clog.x.sb.stcb = stcb;
80 sctp_clog.x.sb.so_sbcc = sb->sb_cc;
82 sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
84 sctp_clog.x.sb.stcb_sbcc = 0;
85 sctp_clog.x.sb.incr = incr;
86 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
89 sctp_clog.x.misc.log1,
90 sctp_clog.x.misc.log2,
91 sctp_clog.x.misc.log3,
92 sctp_clog.x.misc.log4);
97 sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
99 #if defined(SCTP_LOCAL_TRACE_BUF)
100 struct sctp_cwnd_log sctp_clog;
102 sctp_clog.x.close.inp = (void *)inp;
103 sctp_clog.x.close.sctp_flags = inp->sctp_flags;
105 sctp_clog.x.close.stcb = (void *)stcb;
106 sctp_clog.x.close.state = (uint16_t)stcb->asoc.state;
108 sctp_clog.x.close.stcb = 0;
109 sctp_clog.x.close.state = 0;
111 sctp_clog.x.close.loc = loc;
112 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
113 SCTP_LOG_EVENT_CLOSE,
115 sctp_clog.x.misc.log1,
116 sctp_clog.x.misc.log2,
117 sctp_clog.x.misc.log3,
118 sctp_clog.x.misc.log4);
123 rto_logging(struct sctp_nets *net, int from)
125 #if defined(SCTP_LOCAL_TRACE_BUF)
126 struct sctp_cwnd_log sctp_clog;
128 memset(&sctp_clog, 0, sizeof(sctp_clog));
129 sctp_clog.x.rto.net = (void *)net;
130 sctp_clog.x.rto.rtt = net->rtt / 1000;
131 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
134 sctp_clog.x.misc.log1,
135 sctp_clog.x.misc.log2,
136 sctp_clog.x.misc.log3,
137 sctp_clog.x.misc.log4);
142 sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
144 #if defined(SCTP_LOCAL_TRACE_BUF)
145 struct sctp_cwnd_log sctp_clog;
147 sctp_clog.x.strlog.stcb = stcb;
148 sctp_clog.x.strlog.n_tsn = tsn;
149 sctp_clog.x.strlog.n_sseq = sseq;
150 sctp_clog.x.strlog.e_tsn = 0;
151 sctp_clog.x.strlog.e_sseq = 0;
152 sctp_clog.x.strlog.strm = stream;
153 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
156 sctp_clog.x.misc.log1,
157 sctp_clog.x.misc.log2,
158 sctp_clog.x.misc.log3,
159 sctp_clog.x.misc.log4);
164 sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
166 #if defined(SCTP_LOCAL_TRACE_BUF)
167 struct sctp_cwnd_log sctp_clog;
169 sctp_clog.x.nagle.stcb = (void *)stcb;
170 sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
171 sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
172 sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
173 sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
174 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
175 SCTP_LOG_EVENT_NAGLE,
177 sctp_clog.x.misc.log1,
178 sctp_clog.x.misc.log2,
179 sctp_clog.x.misc.log3,
180 sctp_clog.x.misc.log4);
185 sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
187 #if defined(SCTP_LOCAL_TRACE_BUF)
188 struct sctp_cwnd_log sctp_clog;
190 sctp_clog.x.sack.cumack = cumack;
191 sctp_clog.x.sack.oldcumack = old_cumack;
192 sctp_clog.x.sack.tsn = tsn;
193 sctp_clog.x.sack.numGaps = gaps;
194 sctp_clog.x.sack.numDups = dups;
195 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
198 sctp_clog.x.misc.log1,
199 sctp_clog.x.misc.log2,
200 sctp_clog.x.misc.log3,
201 sctp_clog.x.misc.log4);
206 sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
208 #if defined(SCTP_LOCAL_TRACE_BUF)
209 struct sctp_cwnd_log sctp_clog;
211 memset(&sctp_clog, 0, sizeof(sctp_clog));
212 sctp_clog.x.map.base = map;
213 sctp_clog.x.map.cum = cum;
214 sctp_clog.x.map.high = high;
215 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
218 sctp_clog.x.misc.log1,
219 sctp_clog.x.misc.log2,
220 sctp_clog.x.misc.log3,
221 sctp_clog.x.misc.log4);
226 sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn, int from)
228 #if defined(SCTP_LOCAL_TRACE_BUF)
229 struct sctp_cwnd_log sctp_clog;
231 memset(&sctp_clog, 0, sizeof(sctp_clog));
232 sctp_clog.x.fr.largest_tsn = biggest_tsn;
233 sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
234 sctp_clog.x.fr.tsn = tsn;
235 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
238 sctp_clog.x.misc.log1,
239 sctp_clog.x.misc.log2,
240 sctp_clog.x.misc.log3,
241 sctp_clog.x.misc.log4);
245 #ifdef SCTP_MBUF_LOGGING
247 sctp_log_mb(struct mbuf *m, int from)
249 #if defined(SCTP_LOCAL_TRACE_BUF)
250 struct sctp_cwnd_log sctp_clog;
252 sctp_clog.x.mb.mp = m;
253 sctp_clog.x.mb.mbuf_flags = (uint8_t)(SCTP_BUF_GET_FLAGS(m));
254 sctp_clog.x.mb.size = (uint16_t)(SCTP_BUF_LEN(m));
255 sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
256 if (SCTP_BUF_IS_EXTENDED(m)) {
257 sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
258 sctp_clog.x.mb.refcnt = (uint8_t)(SCTP_BUF_EXTEND_REFCNT(m));
260 sctp_clog.x.mb.ext = 0;
261 sctp_clog.x.mb.refcnt = 0;
263 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
266 sctp_clog.x.misc.log1,
267 sctp_clog.x.misc.log2,
268 sctp_clog.x.misc.log3,
269 sctp_clog.x.misc.log4);
274 sctp_log_mbc(struct mbuf *m, int from)
278 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
279 sctp_log_mb(mat, from);
285 sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from)
287 #if defined(SCTP_LOCAL_TRACE_BUF)
288 struct sctp_cwnd_log sctp_clog;
290 if (control == NULL) {
291 SCTP_PRINTF("Gak log of NULL?\n");
294 sctp_clog.x.strlog.stcb = control->stcb;
295 sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
296 sctp_clog.x.strlog.n_sseq = (uint16_t)control->mid;
297 sctp_clog.x.strlog.strm = control->sinfo_stream;
298 if (poschk != NULL) {
299 sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
300 sctp_clog.x.strlog.e_sseq = (uint16_t)poschk->mid;
302 sctp_clog.x.strlog.e_tsn = 0;
303 sctp_clog.x.strlog.e_sseq = 0;
305 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
308 sctp_clog.x.misc.log1,
309 sctp_clog.x.misc.log2,
310 sctp_clog.x.misc.log3,
311 sctp_clog.x.misc.log4);
316 sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
318 #if defined(SCTP_LOCAL_TRACE_BUF)
319 struct sctp_cwnd_log sctp_clog;
321 sctp_clog.x.cwnd.net = net;
322 if (stcb->asoc.send_queue_cnt > 255)
323 sctp_clog.x.cwnd.cnt_in_send = 255;
325 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
326 if (stcb->asoc.stream_queue_cnt > 255)
327 sctp_clog.x.cwnd.cnt_in_str = 255;
329 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
332 sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
333 sctp_clog.x.cwnd.inflight = net->flight_size;
334 sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
335 sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
336 sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
338 if (SCTP_CWNDLOG_PRESEND == from) {
339 sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
341 sctp_clog.x.cwnd.cwnd_augment = augment;
342 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
345 sctp_clog.x.misc.log1,
346 sctp_clog.x.misc.log2,
347 sctp_clog.x.misc.log3,
348 sctp_clog.x.misc.log4);
353 sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
355 #if defined(SCTP_LOCAL_TRACE_BUF)
356 struct sctp_cwnd_log sctp_clog;
358 memset(&sctp_clog, 0, sizeof(sctp_clog));
360 sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
363 sctp_clog.x.lock.sock = (void *)NULL;
365 sctp_clog.x.lock.inp = (void *)inp;
367 sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
369 sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
372 sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
373 sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
375 sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
376 sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
378 sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
379 if (inp && (inp->sctp_socket)) {
380 sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
381 sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
382 sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
384 sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
385 sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
386 sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
388 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
391 sctp_clog.x.misc.log1,
392 sctp_clog.x.misc.log2,
393 sctp_clog.x.misc.log3,
394 sctp_clog.x.misc.log4);
399 sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
401 #if defined(SCTP_LOCAL_TRACE_BUF)
402 struct sctp_cwnd_log sctp_clog;
404 memset(&sctp_clog, 0, sizeof(sctp_clog));
405 sctp_clog.x.cwnd.net = net;
406 sctp_clog.x.cwnd.cwnd_new_value = error;
407 sctp_clog.x.cwnd.inflight = net->flight_size;
408 sctp_clog.x.cwnd.cwnd_augment = burst;
409 if (stcb->asoc.send_queue_cnt > 255)
410 sctp_clog.x.cwnd.cnt_in_send = 255;
412 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
413 if (stcb->asoc.stream_queue_cnt > 255)
414 sctp_clog.x.cwnd.cnt_in_str = 255;
416 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
417 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
418 SCTP_LOG_EVENT_MAXBURST,
420 sctp_clog.x.misc.log1,
421 sctp_clog.x.misc.log2,
422 sctp_clog.x.misc.log3,
423 sctp_clog.x.misc.log4);
428 sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
430 #if defined(SCTP_LOCAL_TRACE_BUF)
431 struct sctp_cwnd_log sctp_clog;
433 sctp_clog.x.rwnd.rwnd = peers_rwnd;
434 sctp_clog.x.rwnd.send_size = snd_size;
435 sctp_clog.x.rwnd.overhead = overhead;
436 sctp_clog.x.rwnd.new_rwnd = 0;
437 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
440 sctp_clog.x.misc.log1,
441 sctp_clog.x.misc.log2,
442 sctp_clog.x.misc.log3,
443 sctp_clog.x.misc.log4);
448 sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
450 #if defined(SCTP_LOCAL_TRACE_BUF)
451 struct sctp_cwnd_log sctp_clog;
453 sctp_clog.x.rwnd.rwnd = peers_rwnd;
454 sctp_clog.x.rwnd.send_size = flight_size;
455 sctp_clog.x.rwnd.overhead = overhead;
456 sctp_clog.x.rwnd.new_rwnd = a_rwndval;
457 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
460 sctp_clog.x.misc.log1,
461 sctp_clog.x.misc.log2,
462 sctp_clog.x.misc.log3,
463 sctp_clog.x.misc.log4);
467 #ifdef SCTP_MBCNT_LOGGING
469 sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
471 #if defined(SCTP_LOCAL_TRACE_BUF)
472 struct sctp_cwnd_log sctp_clog;
474 sctp_clog.x.mbcnt.total_queue_size = total_oq;
475 sctp_clog.x.mbcnt.size_change = book;
476 sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
477 sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
478 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
479 SCTP_LOG_EVENT_MBCNT,
481 sctp_clog.x.misc.log1,
482 sctp_clog.x.misc.log2,
483 sctp_clog.x.misc.log3,
484 sctp_clog.x.misc.log4);
490 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
492 #if defined(SCTP_LOCAL_TRACE_BUF)
493 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
501 sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from)
503 #if defined(SCTP_LOCAL_TRACE_BUF)
504 struct sctp_cwnd_log sctp_clog;
506 sctp_clog.x.wake.stcb = (void *)stcb;
507 sctp_clog.x.wake.wake_cnt = wake_cnt;
508 sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
509 sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
510 sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
512 if (stcb->asoc.stream_queue_cnt < 0xff)
513 sctp_clog.x.wake.stream_qcnt = (uint8_t)stcb->asoc.stream_queue_cnt;
515 sctp_clog.x.wake.stream_qcnt = 0xff;
517 if (stcb->asoc.chunks_on_out_queue < 0xff)
518 sctp_clog.x.wake.chunks_on_oque = (uint8_t)stcb->asoc.chunks_on_out_queue;
520 sctp_clog.x.wake.chunks_on_oque = 0xff;
522 sctp_clog.x.wake.sctpflags = 0;
523 /* set in the defered mode stuff */
524 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
525 sctp_clog.x.wake.sctpflags |= 1;
526 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
527 sctp_clog.x.wake.sctpflags |= 2;
528 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
529 sctp_clog.x.wake.sctpflags |= 4;
530 /* what about the sb */
531 if (stcb->sctp_socket) {
532 struct socket *so = stcb->sctp_socket;
534 sctp_clog.x.wake.sbflags = (uint8_t)((so->so_snd.sb_flags & 0x00ff));
536 sctp_clog.x.wake.sbflags = 0xff;
538 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
541 sctp_clog.x.misc.log1,
542 sctp_clog.x.misc.log2,
543 sctp_clog.x.misc.log3,
544 sctp_clog.x.misc.log4);
549 sctp_log_block(uint8_t from, struct sctp_association *asoc, ssize_t sendlen)
551 #if defined(SCTP_LOCAL_TRACE_BUF)
552 struct sctp_cwnd_log sctp_clog;
554 sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
555 sctp_clog.x.blk.send_sent_qcnt = (uint16_t)(asoc->send_queue_cnt + asoc->sent_queue_cnt);
556 sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
557 sctp_clog.x.blk.stream_qcnt = (uint16_t)asoc->stream_queue_cnt;
558 sctp_clog.x.blk.chunks_on_oque = (uint16_t)asoc->chunks_on_out_queue;
559 sctp_clog.x.blk.flight_size = (uint16_t)(asoc->total_flight / 1024);
560 sctp_clog.x.blk.sndlen = (uint32_t)sendlen;
561 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
562 SCTP_LOG_EVENT_BLOCK,
564 sctp_clog.x.misc.log1,
565 sctp_clog.x.misc.log2,
566 sctp_clog.x.misc.log3,
567 sctp_clog.x.misc.log4);
572 sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED)
574 /* May need to fix this if ktrdump does not work */
578 #ifdef SCTP_AUDITING_ENABLED
579 uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
580 static int sctp_audit_indx = 0;
584 sctp_print_audit_report(void)
590 for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
591 if ((sctp_audit_data[i][0] == 0xe0) &&
592 (sctp_audit_data[i][1] == 0x01)) {
595 } else if (sctp_audit_data[i][0] == 0xf0) {
598 } else if ((sctp_audit_data[i][0] == 0xc0) &&
599 (sctp_audit_data[i][1] == 0x01)) {
603 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t)sctp_audit_data[i][0],
604 (uint32_t)sctp_audit_data[i][1]);
609 for (i = 0; i < sctp_audit_indx; i++) {
610 if ((sctp_audit_data[i][0] == 0xe0) &&
611 (sctp_audit_data[i][1] == 0x01)) {
614 } else if (sctp_audit_data[i][0] == 0xf0) {
617 } else if ((sctp_audit_data[i][0] == 0xc0) &&
618 (sctp_audit_data[i][1] == 0x01)) {
622 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t)sctp_audit_data[i][0],
623 (uint32_t)sctp_audit_data[i][1]);
632 sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
633 struct sctp_nets *net)
635 int resend_cnt, tot_out, rep, tot_book_cnt;
636 struct sctp_nets *lnet;
637 struct sctp_tmit_chunk *chk;
639 sctp_audit_data[sctp_audit_indx][0] = 0xAA;
640 sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
642 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
646 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
647 sctp_audit_data[sctp_audit_indx][1] = 0x01;
649 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
655 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
656 sctp_audit_data[sctp_audit_indx][1] = 0x02;
658 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
663 sctp_audit_data[sctp_audit_indx][0] = 0xA1;
664 sctp_audit_data[sctp_audit_indx][1] =
665 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
667 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
672 resend_cnt = tot_out = 0;
673 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
674 if (chk->sent == SCTP_DATAGRAM_RESEND) {
676 } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
677 tot_out += chk->book_size;
681 if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
682 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
683 sctp_audit_data[sctp_audit_indx][1] = 0xA1;
685 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
688 SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
689 resend_cnt, stcb->asoc.sent_queue_retran_cnt);
691 stcb->asoc.sent_queue_retran_cnt = resend_cnt;
692 sctp_audit_data[sctp_audit_indx][0] = 0xA2;
693 sctp_audit_data[sctp_audit_indx][1] =
694 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
696 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
700 if (tot_out != stcb->asoc.total_flight) {
701 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
702 sctp_audit_data[sctp_audit_indx][1] = 0xA2;
704 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
708 SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
709 (int)stcb->asoc.total_flight);
710 stcb->asoc.total_flight = tot_out;
712 if (tot_book_cnt != stcb->asoc.total_flight_count) {
713 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
714 sctp_audit_data[sctp_audit_indx][1] = 0xA5;
716 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
720 SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
722 stcb->asoc.total_flight_count = tot_book_cnt;
725 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
726 tot_out += lnet->flight_size;
728 if (tot_out != stcb->asoc.total_flight) {
729 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
730 sctp_audit_data[sctp_audit_indx][1] = 0xA3;
732 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
736 SCTP_PRINTF("real flight:%d net total was %d\n",
737 stcb->asoc.total_flight, tot_out);
738 /* now corrective action */
739 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
742 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
743 if ((chk->whoTo == lnet) &&
744 (chk->sent < SCTP_DATAGRAM_RESEND)) {
745 tot_out += chk->book_size;
748 if (lnet->flight_size != tot_out) {
749 SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
750 (void *)lnet, lnet->flight_size,
752 lnet->flight_size = tot_out;
757 sctp_print_audit_report();
762 sctp_audit_log(uint8_t ev, uint8_t fd)
765 sctp_audit_data[sctp_audit_indx][0] = ev;
766 sctp_audit_data[sctp_audit_indx][1] = fd;
768 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
776 * sctp_stop_timers_for_shutdown() should be called
777 * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
778 * state to make sure that all timers are stopped.
781 sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
783 struct sctp_inpcb *inp;
784 struct sctp_nets *net;
788 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
789 SCTP_FROM_SCTPUTIL + SCTP_LOC_12);
790 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
791 SCTP_FROM_SCTPUTIL + SCTP_LOC_13);
792 sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
793 SCTP_FROM_SCTPUTIL + SCTP_LOC_14);
794 sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
795 SCTP_FROM_SCTPUTIL + SCTP_LOC_15);
796 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
797 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
798 SCTP_FROM_SCTPUTIL + SCTP_LOC_16);
799 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
800 SCTP_FROM_SCTPUTIL + SCTP_LOC_17);
805 sctp_stop_association_timers(struct sctp_tcb *stcb, bool stop_assoc_kill_timer)
807 struct sctp_inpcb *inp;
808 struct sctp_nets *net;
811 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
812 SCTP_FROM_SCTPUTIL + SCTP_LOC_18);
813 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
814 SCTP_FROM_SCTPUTIL + SCTP_LOC_19);
815 if (stop_assoc_kill_timer) {
816 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
817 SCTP_FROM_SCTPUTIL + SCTP_LOC_20);
819 sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
820 SCTP_FROM_SCTPUTIL + SCTP_LOC_21);
821 sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
822 SCTP_FROM_SCTPUTIL + SCTP_LOC_22);
823 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNGUARD, inp, stcb, NULL,
824 SCTP_FROM_SCTPUTIL + SCTP_LOC_23);
825 /* Mobility adaptation */
826 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, inp, stcb, NULL,
827 SCTP_FROM_SCTPUTIL + SCTP_LOC_24);
828 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
829 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
830 SCTP_FROM_SCTPUTIL + SCTP_LOC_25);
831 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
832 SCTP_FROM_SCTPUTIL + SCTP_LOC_26);
833 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, net,
834 SCTP_FROM_SCTPUTIL + SCTP_LOC_27);
835 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net,
836 SCTP_FROM_SCTPUTIL + SCTP_LOC_28);
837 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, net,
838 SCTP_FROM_SCTPUTIL + SCTP_LOC_29);
839 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
840 SCTP_FROM_SCTPUTIL + SCTP_LOC_30);
841 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
842 SCTP_FROM_SCTPUTIL + SCTP_LOC_31);
847 * A list of sizes based on typical mtu's, used only if next hop size not
848 * returned. These values MUST be multiples of 4 and MUST be ordered.
850 static uint32_t sctp_mtu_sizes[] = {
872 * Return the largest MTU in sctp_mtu_sizes smaller than val.
873 * If val is smaller than the minimum, just return the largest
874 * multiple of 4 smaller or equal to val.
875 * Ensure that the result is a multiple of 4.
878 sctp_get_prev_mtu(uint32_t val)
883 if (val <= sctp_mtu_sizes[0]) {
886 for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
887 if (val <= sctp_mtu_sizes[i]) {
891 KASSERT((sctp_mtu_sizes[i - 1] & 0x00000003) == 0,
892 ("sctp_mtu_sizes[%u] not a multiple of 4", i - 1));
893 return (sctp_mtu_sizes[i - 1]);
897 * Return the smallest MTU in sctp_mtu_sizes larger than val.
898 * If val is larger than the maximum, just return the largest multiple of 4 smaller
900 * Ensure that the result is a multiple of 4.
903 sctp_get_next_mtu(uint32_t val)
905 /* select another MTU that is just bigger than this one */
909 for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
910 if (val < sctp_mtu_sizes[i]) {
911 KASSERT((sctp_mtu_sizes[i] & 0x00000003) == 0,
912 ("sctp_mtu_sizes[%u] not a multiple of 4", i));
913 return (sctp_mtu_sizes[i]);
920 sctp_fill_random_store(struct sctp_pcb *m)
923 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
924 * our counter. The result becomes our good random numbers and we
925 * then setup to give these out. Note that we do no locking to
926 * protect this. This is ok, since if competing folks call this we
927 * will get more gobbled gook in the random store which is what we
928 * want. There is a danger that two guys will use the same random
929 * numbers, but thats ok too since that is random as well :->
932 (void)sctp_hmac(SCTP_HMAC, (uint8_t *)m->random_numbers,
933 sizeof(m->random_numbers), (uint8_t *)&m->random_counter,
934 sizeof(m->random_counter), (uint8_t *)m->random_store);
939 sctp_select_initial_TSN(struct sctp_pcb *inp)
942 * A true implementation should use random selection process to get
943 * the initial stream sequence number, using RFC1750 as a good
948 int store_at, new_store;
950 if (inp->initial_sequence_debug != 0) {
953 ret = inp->initial_sequence_debug;
954 inp->initial_sequence_debug++;
958 store_at = inp->store_at;
959 new_store = store_at + sizeof(uint32_t);
960 if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
963 if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
966 if (new_store == 0) {
967 /* Refill the random store */
968 sctp_fill_random_store(inp);
970 p = &inp->random_store[store_at];
977 sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
983 (void)SCTP_GETTIME_TIMEVAL(&now);
986 x = sctp_select_initial_TSN(&inp->sctp_ep);
991 if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
999 sctp_map_assoc_state(int kernel_state)
1003 if (kernel_state & SCTP_STATE_WAS_ABORTED) {
1004 user_state = SCTP_CLOSED;
1005 } else if (kernel_state & SCTP_STATE_SHUTDOWN_PENDING) {
1006 user_state = SCTP_SHUTDOWN_PENDING;
1008 switch (kernel_state & SCTP_STATE_MASK) {
1009 case SCTP_STATE_EMPTY:
1010 user_state = SCTP_CLOSED;
1012 case SCTP_STATE_INUSE:
1013 user_state = SCTP_CLOSED;
1015 case SCTP_STATE_COOKIE_WAIT:
1016 user_state = SCTP_COOKIE_WAIT;
1018 case SCTP_STATE_COOKIE_ECHOED:
1019 user_state = SCTP_COOKIE_ECHOED;
1021 case SCTP_STATE_OPEN:
1022 user_state = SCTP_ESTABLISHED;
1024 case SCTP_STATE_SHUTDOWN_SENT:
1025 user_state = SCTP_SHUTDOWN_SENT;
1027 case SCTP_STATE_SHUTDOWN_RECEIVED:
1028 user_state = SCTP_SHUTDOWN_RECEIVED;
1030 case SCTP_STATE_SHUTDOWN_ACK_SENT:
1031 user_state = SCTP_SHUTDOWN_ACK_SENT;
1034 user_state = SCTP_CLOSED;
1038 return (user_state);
1042 sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1043 uint32_t override_tag, uint32_t vrf_id, uint16_t o_strms)
1045 struct sctp_association *asoc;
1048 * Anything set to zero is taken care of by the allocation routine's
1053 * Up front select what scoping to apply on addresses I tell my peer
1054 * Not sure what to do with these right now, we will need to come up
1055 * with a way to set them. We may need to pass them through from the
1056 * caller in the sctp_aloc_assoc() function.
1059 #if defined(SCTP_DETAILED_STR_STATS)
1064 /* init all variables to a known value. */
1065 SCTP_SET_STATE(stcb, SCTP_STATE_INUSE);
1066 asoc->max_burst = inp->sctp_ep.max_burst;
1067 asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
1068 asoc->heart_beat_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1069 asoc->cookie_life = inp->sctp_ep.def_cookie_life;
1070 asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
1071 asoc->ecn_supported = inp->ecn_supported;
1072 asoc->prsctp_supported = inp->prsctp_supported;
1073 asoc->idata_supported = inp->idata_supported;
1074 asoc->auth_supported = inp->auth_supported;
1075 asoc->asconf_supported = inp->asconf_supported;
1076 asoc->reconfig_supported = inp->reconfig_supported;
1077 asoc->nrsack_supported = inp->nrsack_supported;
1078 asoc->pktdrop_supported = inp->pktdrop_supported;
1079 asoc->idata_supported = inp->idata_supported;
1080 asoc->sctp_cmt_pf = (uint8_t)0;
1081 asoc->sctp_frag_point = inp->sctp_frag_point;
1082 asoc->sctp_features = inp->sctp_features;
1083 asoc->default_dscp = inp->sctp_ep.default_dscp;
1084 asoc->max_cwnd = inp->max_cwnd;
1086 if (inp->sctp_ep.default_flowlabel) {
1087 asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
1089 if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
1090 asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
1091 asoc->default_flowlabel &= 0x000fffff;
1092 asoc->default_flowlabel |= 0x80000000;
1094 asoc->default_flowlabel = 0;
1098 asoc->sb_send_resv = 0;
1100 asoc->my_vtag = override_tag;
1102 asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
1104 /* Get the nonce tags */
1105 asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1106 asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1107 asoc->vrf_id = vrf_id;
1109 #ifdef SCTP_ASOCLOG_OF_TSNS
1110 asoc->tsn_in_at = 0;
1111 asoc->tsn_out_at = 0;
1112 asoc->tsn_in_wrapped = 0;
1113 asoc->tsn_out_wrapped = 0;
1114 asoc->cumack_log_at = 0;
1115 asoc->cumack_log_atsnt = 0;
1117 #ifdef SCTP_FS_SPEC_LOG
1121 asoc->assoc_up_sent = 0;
1122 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
1123 sctp_select_initial_TSN(&inp->sctp_ep);
1124 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1125 /* we are optimisitic here */
1126 asoc->peer_supports_nat = 0;
1127 asoc->sent_queue_retran_cnt = 0;
1130 asoc->last_net_cmt_send_started = NULL;
1132 /* This will need to be adjusted */
1133 asoc->last_acked_seq = asoc->init_seq_number - 1;
1134 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1135 asoc->asconf_seq_in = asoc->last_acked_seq;
1137 /* here we are different, we hold the next one we expect */
1138 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
1140 asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
1141 asoc->initial_rto = inp->sctp_ep.initial_rto;
1143 asoc->default_mtu = inp->sctp_ep.default_mtu;
1144 asoc->max_init_times = inp->sctp_ep.max_init_times;
1145 asoc->max_send_times = inp->sctp_ep.max_send_times;
1146 asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1147 asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1148 asoc->free_chunk_cnt = 0;
1150 asoc->iam_blocking = 0;
1151 asoc->context = inp->sctp_context;
1152 asoc->local_strreset_support = inp->local_strreset_support;
1153 asoc->def_send = inp->def_send;
1154 asoc->delayed_ack = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1155 asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1156 asoc->pr_sctp_cnt = 0;
1157 asoc->total_output_queue_size = 0;
1159 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1160 asoc->scope.ipv6_addr_legal = 1;
1161 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1162 asoc->scope.ipv4_addr_legal = 1;
1164 asoc->scope.ipv4_addr_legal = 0;
1167 asoc->scope.ipv6_addr_legal = 0;
1168 asoc->scope.ipv4_addr_legal = 1;
1171 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1172 asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1174 asoc->smallest_mtu = inp->sctp_frag_point;
1175 asoc->minrto = inp->sctp_ep.sctp_minrto;
1176 asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1178 asoc->stream_locked_on = 0;
1179 asoc->ecn_echo_cnt_onq = 0;
1180 asoc->stream_locked = 0;
1182 asoc->send_sack = 1;
1184 LIST_INIT(&asoc->sctp_restricted_addrs);
1186 TAILQ_INIT(&asoc->nets);
1187 TAILQ_INIT(&asoc->pending_reply_queue);
1188 TAILQ_INIT(&asoc->asconf_ack_sent);
1189 /* Setup to fill the hb random cache at first HB */
1190 asoc->hb_random_idx = 4;
1192 asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1194 stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1195 stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1197 stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1198 stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1201 * Now the stream parameters, here we allocate space for all streams
1202 * that we request by default.
1204 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1206 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1207 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1209 if (asoc->strmout == NULL) {
1210 /* big trouble no memory */
1211 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1214 for (i = 0; i < asoc->streamoutcnt; i++) {
1216 * inbound side must be set to 0xffff, also NOTE when we get
1217 * the INIT-ACK back (for INIT sender) we MUST reduce the
1218 * count (streamoutcnt) but first check if we sent to any of
1219 * the upper streams that were dropped (if some were). Those
1220 * that were dropped must be notified to the upper layer as
1223 asoc->strmout[i].next_mid_ordered = 0;
1224 asoc->strmout[i].next_mid_unordered = 0;
1225 TAILQ_INIT(&asoc->strmout[i].outqueue);
1226 asoc->strmout[i].chunks_on_queues = 0;
1227 #if defined(SCTP_DETAILED_STR_STATS)
1228 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1229 asoc->strmout[i].abandoned_sent[j] = 0;
1230 asoc->strmout[i].abandoned_unsent[j] = 0;
1233 asoc->strmout[i].abandoned_sent[0] = 0;
1234 asoc->strmout[i].abandoned_unsent[0] = 0;
1236 asoc->strmout[i].sid = i;
1237 asoc->strmout[i].last_msg_incomplete = 0;
1238 asoc->strmout[i].state = SCTP_STREAM_OPENING;
1239 asoc->ss_functions.sctp_ss_init_stream(stcb, &asoc->strmout[i], NULL);
1241 asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1243 /* Now the mapping array */
1244 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1245 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1247 if (asoc->mapping_array == NULL) {
1248 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1249 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1252 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1253 SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1255 if (asoc->nr_mapping_array == NULL) {
1256 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1257 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1258 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1261 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1263 /* Now the init of the other outqueues */
1264 TAILQ_INIT(&asoc->free_chunks);
1265 TAILQ_INIT(&asoc->control_send_queue);
1266 TAILQ_INIT(&asoc->asconf_send_queue);
1267 TAILQ_INIT(&asoc->send_queue);
1268 TAILQ_INIT(&asoc->sent_queue);
1269 TAILQ_INIT(&asoc->resetHead);
1270 asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1271 TAILQ_INIT(&asoc->asconf_queue);
1272 /* authentication fields */
1273 asoc->authinfo.random = NULL;
1274 asoc->authinfo.active_keyid = 0;
1275 asoc->authinfo.assoc_key = NULL;
1276 asoc->authinfo.assoc_keyid = 0;
1277 asoc->authinfo.recv_key = NULL;
1278 asoc->authinfo.recv_keyid = 0;
1279 LIST_INIT(&asoc->shared_keys);
1280 asoc->marked_retrans = 0;
1281 asoc->port = inp->sctp_ep.port;
1285 asoc->timoshutdown = 0;
1286 asoc->timoheartbeat = 0;
1287 asoc->timocookie = 0;
1288 asoc->timoshutdownack = 0;
1289 (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1290 asoc->discontinuity_time = asoc->start_time;
1291 for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1292 asoc->abandoned_unsent[i] = 0;
1293 asoc->abandoned_sent[i] = 0;
1296 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
1297 * freed later when the association is freed.
1303 sctp_print_mapping_array(struct sctp_association *asoc)
1305 unsigned int i, limit;
1307 SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
1308 asoc->mapping_array_size,
1309 asoc->mapping_array_base_tsn,
1310 asoc->cumulative_tsn,
1311 asoc->highest_tsn_inside_map,
1312 asoc->highest_tsn_inside_nr_map);
1313 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1314 if (asoc->mapping_array[limit - 1] != 0) {
1318 SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1319 for (i = 0; i < limit; i++) {
1320 SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1324 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1325 if (asoc->nr_mapping_array[limit - 1]) {
1329 SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1330 for (i = 0; i < limit; i++) {
1331 SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1338 sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1340 /* mapping array needs to grow */
1341 uint8_t *new_array1, *new_array2;
1344 new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1345 SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1346 SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1347 if ((new_array1 == NULL) || (new_array2 == NULL)) {
1348 /* can't get more, forget it */
1349 SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1351 SCTP_FREE(new_array1, SCTP_M_MAP);
1354 SCTP_FREE(new_array2, SCTP_M_MAP);
1358 memset(new_array1, 0, new_size);
1359 memset(new_array2, 0, new_size);
1360 memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1361 memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1362 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1363 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1364 asoc->mapping_array = new_array1;
1365 asoc->nr_mapping_array = new_array2;
1366 asoc->mapping_array_size = new_size;
1372 sctp_iterator_work(struct sctp_iterator *it)
1374 int iteration_count = 0;
1377 struct sctp_inpcb *tinp;
1379 SCTP_INP_INFO_RLOCK();
1380 SCTP_ITERATOR_LOCK();
1381 sctp_it_ctl.cur_it = it;
1383 SCTP_INP_RLOCK(it->inp);
1384 SCTP_INP_DECR_REF(it->inp);
1386 if (it->inp == NULL) {
1387 /* iterator is complete */
1389 sctp_it_ctl.cur_it = NULL;
1390 SCTP_ITERATOR_UNLOCK();
1391 SCTP_INP_INFO_RUNLOCK();
1392 if (it->function_atend != NULL) {
1393 (*it->function_atend) (it->pointer, it->val);
1395 SCTP_FREE(it, SCTP_M_ITER);
1402 SCTP_INP_RLOCK(it->inp);
1404 while (((it->pcb_flags) &&
1405 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1406 ((it->pcb_features) &&
1407 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1408 /* endpoint flags or features don't match, so keep looking */
1409 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1410 SCTP_INP_RUNLOCK(it->inp);
1411 goto done_with_iterator;
1414 it->inp = LIST_NEXT(it->inp, sctp_list);
1415 SCTP_INP_RUNLOCK(tinp);
1416 if (it->inp == NULL) {
1417 goto done_with_iterator;
1419 SCTP_INP_RLOCK(it->inp);
1421 /* now go through each assoc which is in the desired state */
1422 if (it->done_current_ep == 0) {
1423 if (it->function_inp != NULL)
1424 inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1425 it->done_current_ep = 1;
1427 if (it->stcb == NULL) {
1428 /* run the per instance function */
1429 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1431 if ((inp_skip) || it->stcb == NULL) {
1432 if (it->function_inp_end != NULL) {
1433 inp_skip = (*it->function_inp_end) (it->inp,
1437 SCTP_INP_RUNLOCK(it->inp);
1441 SCTP_TCB_LOCK(it->stcb);
1442 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1443 /* not in the right state... keep looking */
1444 SCTP_TCB_UNLOCK(it->stcb);
1447 /* see if we have limited out the iterator loop */
1449 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1450 /* Pause to let others grab the lock */
1451 atomic_add_int(&it->stcb->asoc.refcnt, 1);
1452 SCTP_TCB_UNLOCK(it->stcb);
1453 SCTP_INP_INCR_REF(it->inp);
1454 SCTP_INP_RUNLOCK(it->inp);
1455 SCTP_ITERATOR_UNLOCK();
1456 SCTP_INP_INFO_RUNLOCK();
1457 SCTP_INP_INFO_RLOCK();
1458 SCTP_ITERATOR_LOCK();
1459 if (sctp_it_ctl.iterator_flags) {
1460 /* We won't be staying here */
1461 SCTP_INP_DECR_REF(it->inp);
1462 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1463 if (sctp_it_ctl.iterator_flags &
1464 SCTP_ITERATOR_STOP_CUR_IT) {
1465 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1466 goto done_with_iterator;
1468 if (sctp_it_ctl.iterator_flags &
1469 SCTP_ITERATOR_STOP_CUR_INP) {
1470 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1473 /* If we reach here huh? */
1474 SCTP_PRINTF("Unknown it ctl flag %x\n",
1475 sctp_it_ctl.iterator_flags);
1476 sctp_it_ctl.iterator_flags = 0;
1478 SCTP_INP_RLOCK(it->inp);
1479 SCTP_INP_DECR_REF(it->inp);
1480 SCTP_TCB_LOCK(it->stcb);
1481 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1482 iteration_count = 0;
1485 /* run function on this one */
1486 (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1489 * we lie here, it really needs to have its own type but
1490 * first I must verify that this won't effect things :-0
1492 if (it->no_chunk_output == 0)
1493 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1495 SCTP_TCB_UNLOCK(it->stcb);
1497 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1498 if (it->stcb == NULL) {
1499 /* Run last function */
1500 if (it->function_inp_end != NULL) {
1501 inp_skip = (*it->function_inp_end) (it->inp,
1507 SCTP_INP_RUNLOCK(it->inp);
1509 /* done with all assocs on this endpoint, move on to next endpoint */
1510 it->done_current_ep = 0;
1511 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1514 it->inp = LIST_NEXT(it->inp, sctp_list);
1516 if (it->inp == NULL) {
1517 goto done_with_iterator;
1519 goto select_a_new_ep;
1523 sctp_iterator_worker(void)
1525 struct sctp_iterator *it;
1527 /* This function is called with the WQ lock in place */
1528 sctp_it_ctl.iterator_running = 1;
1529 while ((it = TAILQ_FIRST(&sctp_it_ctl.iteratorhead)) != NULL) {
1530 /* now lets work on this one */
1531 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
1532 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1533 CURVNET_SET(it->vn);
1534 sctp_iterator_work(it);
1536 SCTP_IPI_ITERATOR_WQ_LOCK();
1537 /* sa_ignore FREED_MEMORY */
1539 sctp_it_ctl.iterator_running = 0;
1545 sctp_handle_addr_wq(void)
1547 /* deal with the ADDR wq from the rtsock calls */
1548 struct sctp_laddr *wi, *nwi;
1549 struct sctp_asconf_iterator *asc;
1551 SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1552 sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
1554 /* Try later, no memory */
1555 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1556 (struct sctp_inpcb *)NULL,
1557 (struct sctp_tcb *)NULL,
1558 (struct sctp_nets *)NULL);
1561 LIST_INIT(&asc->list_of_work);
1564 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
1565 LIST_REMOVE(wi, sctp_nxt_addr);
1566 LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
1570 if (asc->cnt == 0) {
1571 SCTP_FREE(asc, SCTP_M_ASC_IT);
1575 ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
1576 sctp_asconf_iterator_stcb,
1577 NULL, /* No ep end for boundall */
1578 SCTP_PCB_FLAGS_BOUNDALL,
1579 SCTP_PCB_ANY_FEATURES,
1580 SCTP_ASOC_ANY_STATE,
1582 sctp_asconf_iterator_end, NULL, 0);
1584 SCTP_PRINTF("Failed to initiate iterator for handle_addr_wq\n");
1586 * Freeing if we are stopping or put back on the
1589 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
1590 sctp_asconf_iterator_end(asc, 0);
1592 LIST_FOREACH(wi, &asc->list_of_work, sctp_nxt_addr) {
1593 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
1595 SCTP_FREE(asc, SCTP_M_ASC_IT);
1602 sctp_timeout_handler(void *t)
1604 struct sctp_inpcb *inp;
1605 struct sctp_tcb *stcb;
1606 struct sctp_nets *net;
1607 struct sctp_timer *tmr;
1608 struct mbuf *op_err;
1609 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1615 tmr = (struct sctp_timer *)t;
1616 inp = (struct sctp_inpcb *)tmr->ep;
1617 stcb = (struct sctp_tcb *)tmr->tcb;
1618 net = (struct sctp_nets *)tmr->net;
1619 CURVNET_SET((struct vnet *)tmr->vnet);
1622 #ifdef SCTP_AUDITING_ENABLED
1623 sctp_audit_log(0xF0, (uint8_t)tmr->type);
1624 sctp_auditing(3, inp, stcb, net);
1627 /* sanity checks... */
1628 if (tmr->self != (void *)tmr) {
1630 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1636 tmr->stopped_from = 0xa001;
1637 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1639 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1645 tmr->stopped_from = 0xa002;
1646 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1650 /* if this is an iterator timeout, get the struct and clear inp */
1651 tmr->stopped_from = 0xa003;
1653 SCTP_INP_INCR_REF(inp);
1654 if ((inp->sctp_socket == NULL) &&
1655 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1656 (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1657 (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1658 (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1659 (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1660 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1661 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1662 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1663 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))) {
1664 SCTP_INP_DECR_REF(inp);
1669 tmr->stopped_from = 0xa004;
1671 atomic_add_int(&stcb->asoc.refcnt, 1);
1672 if (stcb->asoc.state == 0) {
1673 atomic_add_int(&stcb->asoc.refcnt, -1);
1675 SCTP_INP_DECR_REF(inp);
1682 tmr->stopped_from = 0xa005;
1683 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", type);
1684 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1686 SCTP_INP_DECR_REF(inp);
1689 atomic_add_int(&stcb->asoc.refcnt, -1);
1694 tmr->stopped_from = 0xa006;
1697 SCTP_TCB_LOCK(stcb);
1698 atomic_add_int(&stcb->asoc.refcnt, -1);
1699 if ((type != SCTP_TIMER_TYPE_ASOCKILL) &&
1700 ((stcb->asoc.state == 0) ||
1701 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1702 SCTP_TCB_UNLOCK(stcb);
1704 SCTP_INP_DECR_REF(inp);
1709 } else if (inp != NULL) {
1710 if (type != SCTP_TIMER_TYPE_INPKILL) {
1711 SCTP_INP_WLOCK(inp);
1714 SCTP_WQ_ADDR_LOCK();
1716 /* record in stopped what t-o occurred */
1717 tmr->stopped_from = type;
1719 /* mark as being serviced now */
1720 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1722 * Callout has been rescheduled.
1726 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1728 * Not active, so no action.
1732 SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1734 /* call the handler for the appropriate timer type */
1736 case SCTP_TIMER_TYPE_ADDR_WQ:
1737 sctp_handle_addr_wq();
1739 case SCTP_TIMER_TYPE_SEND:
1740 if ((stcb == NULL) || (inp == NULL)) {
1743 SCTP_STAT_INCR(sctps_timodata);
1744 stcb->asoc.timodata++;
1745 stcb->asoc.num_send_timers_up--;
1746 if (stcb->asoc.num_send_timers_up < 0) {
1747 stcb->asoc.num_send_timers_up = 0;
1749 SCTP_TCB_LOCK_ASSERT(stcb);
1750 if (sctp_t3rxt_timer(inp, stcb, net)) {
1751 /* no need to unlock on tcb its gone */
1755 SCTP_TCB_LOCK_ASSERT(stcb);
1756 #ifdef SCTP_AUDITING_ENABLED
1757 sctp_auditing(4, inp, stcb, net);
1759 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1760 if ((stcb->asoc.num_send_timers_up == 0) &&
1761 (stcb->asoc.sent_queue_cnt > 0)) {
1762 struct sctp_tmit_chunk *chk;
1765 * safeguard. If there on some on the sent queue
1766 * somewhere but no timers running something is
1767 * wrong... so we start a timer on the first chunk
1768 * on the send queue on whatever net it is sent to.
1770 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1771 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1775 case SCTP_TIMER_TYPE_INIT:
1776 if ((stcb == NULL) || (inp == NULL)) {
1779 SCTP_STAT_INCR(sctps_timoinit);
1780 stcb->asoc.timoinit++;
1781 if (sctp_t1init_timer(inp, stcb, net)) {
1782 /* no need to unlock on tcb its gone */
1785 /* We do output but not here */
1788 case SCTP_TIMER_TYPE_RECV:
1789 if ((stcb == NULL) || (inp == NULL)) {
1792 SCTP_STAT_INCR(sctps_timosack);
1793 stcb->asoc.timosack++;
1794 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1795 #ifdef SCTP_AUDITING_ENABLED
1796 sctp_auditing(4, inp, stcb, net);
1798 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1800 case SCTP_TIMER_TYPE_SHUTDOWN:
1801 if ((stcb == NULL) || (inp == NULL)) {
1804 if (sctp_shutdown_timer(inp, stcb, net)) {
1805 /* no need to unlock on tcb its gone */
1808 SCTP_STAT_INCR(sctps_timoshutdown);
1809 stcb->asoc.timoshutdown++;
1810 #ifdef SCTP_AUDITING_ENABLED
1811 sctp_auditing(4, inp, stcb, net);
1813 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1815 case SCTP_TIMER_TYPE_HEARTBEAT:
1816 if ((stcb == NULL) || (inp == NULL) || (net == NULL)) {
1819 SCTP_STAT_INCR(sctps_timoheartbeat);
1820 stcb->asoc.timoheartbeat++;
1821 if (sctp_heartbeat_timer(inp, stcb, net)) {
1822 /* no need to unlock on tcb its gone */
1825 #ifdef SCTP_AUDITING_ENABLED
1826 sctp_auditing(4, inp, stcb, net);
1828 if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1829 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1830 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1833 case SCTP_TIMER_TYPE_COOKIE:
1834 if ((stcb == NULL) || (inp == NULL)) {
1838 if (sctp_cookie_timer(inp, stcb, net)) {
1839 /* no need to unlock on tcb its gone */
1842 SCTP_STAT_INCR(sctps_timocookie);
1843 stcb->asoc.timocookie++;
1844 #ifdef SCTP_AUDITING_ENABLED
1845 sctp_auditing(4, inp, stcb, net);
1848 * We consider T3 and Cookie timer pretty much the same with
1849 * respect to where from in chunk_output.
1851 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1853 case SCTP_TIMER_TYPE_NEWCOOKIE:
1861 SCTP_STAT_INCR(sctps_timosecret);
1862 (void)SCTP_GETTIME_TIMEVAL(&tv);
1863 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1864 inp->sctp_ep.last_secret_number =
1865 inp->sctp_ep.current_secret_number;
1866 inp->sctp_ep.current_secret_number++;
1867 if (inp->sctp_ep.current_secret_number >=
1868 SCTP_HOW_MANY_SECRETS) {
1869 inp->sctp_ep.current_secret_number = 0;
1871 secret = (int)inp->sctp_ep.current_secret_number;
1872 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1873 inp->sctp_ep.secret_key[secret][i] =
1874 sctp_select_initial_TSN(&inp->sctp_ep);
1876 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1880 case SCTP_TIMER_TYPE_PATHMTURAISE:
1881 if ((stcb == NULL) || (inp == NULL)) {
1884 SCTP_STAT_INCR(sctps_timopathmtu);
1885 sctp_pathmtu_timer(inp, stcb, net);
1888 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1889 if ((stcb == NULL) || (inp == NULL)) {
1892 if (sctp_shutdownack_timer(inp, stcb, net)) {
1893 /* no need to unlock on tcb its gone */
1896 SCTP_STAT_INCR(sctps_timoshutdownack);
1897 stcb->asoc.timoshutdownack++;
1898 #ifdef SCTP_AUDITING_ENABLED
1899 sctp_auditing(4, inp, stcb, net);
1901 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1903 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1904 if ((stcb == NULL) || (inp == NULL)) {
1907 SCTP_STAT_INCR(sctps_timoshutdownguard);
1908 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
1909 "Shutdown guard timer expired");
1910 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
1911 /* no need to unlock on tcb its gone */
1914 case SCTP_TIMER_TYPE_STRRESET:
1915 if ((stcb == NULL) || (inp == NULL)) {
1918 if (sctp_strreset_timer(inp, stcb, net)) {
1919 /* no need to unlock on tcb its gone */
1922 SCTP_STAT_INCR(sctps_timostrmrst);
1923 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1925 case SCTP_TIMER_TYPE_ASCONF:
1926 if ((stcb == NULL) || (inp == NULL)) {
1929 if (sctp_asconf_timer(inp, stcb, net)) {
1930 /* no need to unlock on tcb its gone */
1933 SCTP_STAT_INCR(sctps_timoasconf);
1934 #ifdef SCTP_AUDITING_ENABLED
1935 sctp_auditing(4, inp, stcb, net);
1937 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1939 case SCTP_TIMER_TYPE_PRIM_DELETED:
1940 if ((stcb == NULL) || (inp == NULL)) {
1943 sctp_delete_prim_timer(inp, stcb, net);
1944 SCTP_STAT_INCR(sctps_timodelprim);
1947 case SCTP_TIMER_TYPE_AUTOCLOSE:
1948 if ((stcb == NULL) || (inp == NULL)) {
1951 SCTP_STAT_INCR(sctps_timoautoclose);
1952 sctp_autoclose_timer(inp, stcb, net);
1953 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1956 case SCTP_TIMER_TYPE_ASOCKILL:
1957 if ((stcb == NULL) || (inp == NULL)) {
1960 SCTP_STAT_INCR(sctps_timoassockill);
1961 /* Can we free it yet? */
1962 SCTP_INP_DECR_REF(inp);
1963 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
1964 SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1965 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1966 so = SCTP_INP_SO(inp);
1967 atomic_add_int(&stcb->asoc.refcnt, 1);
1968 SCTP_TCB_UNLOCK(stcb);
1969 SCTP_SOCKET_LOCK(so, 1);
1970 SCTP_TCB_LOCK(stcb);
1971 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1973 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
1974 SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1975 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1976 SCTP_SOCKET_UNLOCK(so, 1);
1979 * free asoc, always unlocks (or destroy's) so prevent
1980 * duplicate unlock or unlock of a free mtx :-0
1984 case SCTP_TIMER_TYPE_INPKILL:
1985 SCTP_STAT_INCR(sctps_timoinpkill);
1990 * special case, take away our increment since WE are the
1993 SCTP_INP_DECR_REF(inp);
1994 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
1995 SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1996 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
1997 SCTP_CALLED_FROM_INPKILL_TIMER);
2001 SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
2005 #ifdef SCTP_AUDITING_ENABLED
2006 sctp_audit_log(0xF1, (uint8_t)type);
2008 sctp_auditing(5, inp, stcb, net);
2010 if ((did_output) && stcb) {
2012 * Now we need to clean up the control chunk chain if an
2013 * ECNE is on it. It must be marked as UNSENT again so next
2014 * call will continue to send it until such time that we get
2015 * a CWR, to remove it. It is, however, less likely that we
2016 * will find a ecn echo on the chain though.
2018 sctp_fix_ecn_echo(&stcb->asoc);
2022 SCTP_TCB_UNLOCK(stcb);
2023 } else if (inp != NULL) {
2024 SCTP_INP_WUNLOCK(inp);
2026 SCTP_WQ_ADDR_UNLOCK();
2031 SCTP_INP_DECR_REF(inp);
2035 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type = %d)\n", type);
2040 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2041 struct sctp_nets *net)
2044 struct sctp_timer *tmr;
2046 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
2051 SCTP_TCB_LOCK_ASSERT(stcb);
2054 case SCTP_TIMER_TYPE_ADDR_WQ:
2055 /* Only 1 tick away :-) */
2056 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2057 to_ticks = SCTP_ADDRESS_TICK_DELAY;
2059 case SCTP_TIMER_TYPE_SEND:
2060 /* Here we use the RTO timer */
2064 if ((stcb == NULL) || (net == NULL)) {
2067 tmr = &net->rxt_timer;
2068 if (net->RTO == 0) {
2069 rto_val = stcb->asoc.initial_rto;
2073 to_ticks = MSEC_TO_TICKS(rto_val);
2076 case SCTP_TIMER_TYPE_INIT:
2078 * Here we use the INIT timer default usually about 1
2081 if ((stcb == NULL) || (net == NULL)) {
2084 tmr = &net->rxt_timer;
2085 if (net->RTO == 0) {
2086 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2088 to_ticks = MSEC_TO_TICKS(net->RTO);
2091 case SCTP_TIMER_TYPE_RECV:
2093 * Here we use the Delayed-Ack timer value from the inp
2094 * ususually about 200ms.
2099 tmr = &stcb->asoc.dack_timer;
2100 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
2102 case SCTP_TIMER_TYPE_SHUTDOWN:
2103 /* Here we use the RTO of the destination. */
2104 if ((stcb == NULL) || (net == NULL)) {
2107 if (net->RTO == 0) {
2108 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2110 to_ticks = MSEC_TO_TICKS(net->RTO);
2112 tmr = &net->rxt_timer;
2114 case SCTP_TIMER_TYPE_HEARTBEAT:
2116 * the net is used here so that we can add in the RTO. Even
2117 * though we use a different timer. We also add the HB timer
2118 * PLUS a random jitter.
2120 if ((stcb == NULL) || (net == NULL)) {
2126 if ((net->dest_state & SCTP_ADDR_NOHB) &&
2127 !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
2130 if (net->RTO == 0) {
2131 to_ticks = stcb->asoc.initial_rto;
2133 to_ticks = net->RTO;
2135 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2136 jitter = rndval % to_ticks;
2137 if (jitter >= (to_ticks >> 1)) {
2138 to_ticks = to_ticks + (jitter - (to_ticks >> 1));
2140 to_ticks = to_ticks - jitter;
2142 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2143 !(net->dest_state & SCTP_ADDR_PF)) {
2144 to_ticks += net->heart_beat_delay;
2147 * Now we must convert the to_ticks that are now in
2150 to_ticks = MSEC_TO_TICKS(to_ticks);
2151 tmr = &net->hb_timer;
2154 case SCTP_TIMER_TYPE_COOKIE:
2156 * Here we can use the RTO timer from the network since one
2157 * RTT was compelete. If a retran happened then we will be
2158 * using the RTO initial value.
2160 if ((stcb == NULL) || (net == NULL)) {
2163 if (net->RTO == 0) {
2164 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2166 to_ticks = MSEC_TO_TICKS(net->RTO);
2168 tmr = &net->rxt_timer;
2170 case SCTP_TIMER_TYPE_NEWCOOKIE:
2172 * nothing needed but the endpoint here ususually about 60
2175 tmr = &inp->sctp_ep.signature_change;
2176 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2178 case SCTP_TIMER_TYPE_ASOCKILL:
2182 tmr = &stcb->asoc.strreset_timer;
2183 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2185 case SCTP_TIMER_TYPE_INPKILL:
2187 * The inp is setup to die. We re-use the signature_chage
2188 * timer since that has stopped and we are in the GONE
2191 tmr = &inp->sctp_ep.signature_change;
2192 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2194 case SCTP_TIMER_TYPE_PATHMTURAISE:
2196 * Here we use the value found in the EP for PMTU ususually
2199 if ((stcb == NULL) || (net == NULL)) {
2202 if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2205 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2206 tmr = &net->pmtu_timer;
2208 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2209 /* Here we use the RTO of the destination */
2210 if ((stcb == NULL) || (net == NULL)) {
2213 if (net->RTO == 0) {
2214 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2216 to_ticks = MSEC_TO_TICKS(net->RTO);
2218 tmr = &net->rxt_timer;
2220 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2222 * Here we use the endpoints shutdown guard timer usually
2228 if (inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] == 0) {
2229 to_ticks = 5 * MSEC_TO_TICKS(stcb->asoc.maxrto);
2231 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2233 tmr = &stcb->asoc.shut_guard_timer;
2235 case SCTP_TIMER_TYPE_STRRESET:
2237 * Here the timer comes from the stcb but its value is from
2240 if ((stcb == NULL) || (net == NULL)) {
2243 if (net->RTO == 0) {
2244 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2246 to_ticks = MSEC_TO_TICKS(net->RTO);
2248 tmr = &stcb->asoc.strreset_timer;
2250 case SCTP_TIMER_TYPE_ASCONF:
2252 * Here the timer comes from the stcb but its value is from
2255 if ((stcb == NULL) || (net == NULL)) {
2258 if (net->RTO == 0) {
2259 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2261 to_ticks = MSEC_TO_TICKS(net->RTO);
2263 tmr = &stcb->asoc.asconf_timer;
2265 case SCTP_TIMER_TYPE_PRIM_DELETED:
2266 if ((stcb == NULL) || (net != NULL)) {
2269 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2270 tmr = &stcb->asoc.delete_prim_timer;
2272 case SCTP_TIMER_TYPE_AUTOCLOSE:
2276 if (stcb->asoc.sctp_autoclose_ticks == 0) {
2278 * Really an error since stcb is NOT set to
2283 to_ticks = stcb->asoc.sctp_autoclose_ticks;
2284 tmr = &stcb->asoc.autoclose_timer;
2287 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2292 if ((to_ticks <= 0) || (tmr == NULL)) {
2293 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2294 __func__, t_type, to_ticks, (void *)tmr);
2297 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2299 * we do NOT allow you to have it already running. if it is
2300 * we leave the current one up unchanged
2304 /* At this point we can proceed */
2305 if (t_type == SCTP_TIMER_TYPE_SEND) {
2306 stcb->asoc.num_send_timers_up++;
2308 tmr->stopped_from = 0;
2310 tmr->ep = (void *)inp;
2311 tmr->tcb = (void *)stcb;
2312 tmr->net = (void *)net;
2313 tmr->self = (void *)tmr;
2314 tmr->vnet = (void *)curvnet;
2315 tmr->ticks = sctp_get_tick_count();
2316 (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2321 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2322 struct sctp_nets *net, uint32_t from)
2324 struct sctp_timer *tmr;
2326 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2332 SCTP_TCB_LOCK_ASSERT(stcb);
2335 case SCTP_TIMER_TYPE_ADDR_WQ:
2336 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2338 case SCTP_TIMER_TYPE_SEND:
2339 if ((stcb == NULL) || (net == NULL)) {
2342 tmr = &net->rxt_timer;
2344 case SCTP_TIMER_TYPE_INIT:
2345 if ((stcb == NULL) || (net == NULL)) {
2348 tmr = &net->rxt_timer;
2350 case SCTP_TIMER_TYPE_RECV:
2354 tmr = &stcb->asoc.dack_timer;
2356 case SCTP_TIMER_TYPE_SHUTDOWN:
2357 if ((stcb == NULL) || (net == NULL)) {
2360 tmr = &net->rxt_timer;
2362 case SCTP_TIMER_TYPE_HEARTBEAT:
2363 if ((stcb == NULL) || (net == NULL)) {
2366 tmr = &net->hb_timer;
2368 case SCTP_TIMER_TYPE_COOKIE:
2369 if ((stcb == NULL) || (net == NULL)) {
2372 tmr = &net->rxt_timer;
2374 case SCTP_TIMER_TYPE_NEWCOOKIE:
2375 /* nothing needed but the endpoint here */
2376 tmr = &inp->sctp_ep.signature_change;
2378 * We re-use the newcookie timer for the INP kill timer. We
2379 * must assure that we do not kill it by accident.
2382 case SCTP_TIMER_TYPE_ASOCKILL:
2384 * Stop the asoc kill timer.
2389 tmr = &stcb->asoc.strreset_timer;
2392 case SCTP_TIMER_TYPE_INPKILL:
2394 * The inp is setup to die. We re-use the signature_chage
2395 * timer since that has stopped and we are in the GONE
2398 tmr = &inp->sctp_ep.signature_change;
2400 case SCTP_TIMER_TYPE_PATHMTURAISE:
2401 if ((stcb == NULL) || (net == NULL)) {
2404 tmr = &net->pmtu_timer;
2406 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2407 if ((stcb == NULL) || (net == NULL)) {
2410 tmr = &net->rxt_timer;
2412 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2416 tmr = &stcb->asoc.shut_guard_timer;
2418 case SCTP_TIMER_TYPE_STRRESET:
2422 tmr = &stcb->asoc.strreset_timer;
2424 case SCTP_TIMER_TYPE_ASCONF:
2428 tmr = &stcb->asoc.asconf_timer;
2430 case SCTP_TIMER_TYPE_PRIM_DELETED:
2434 tmr = &stcb->asoc.delete_prim_timer;
2436 case SCTP_TIMER_TYPE_AUTOCLOSE:
2440 tmr = &stcb->asoc.autoclose_timer;
2443 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2450 if ((tmr->type != t_type) && tmr->type) {
2452 * Ok we have a timer that is under joint use. Cookie timer
2453 * per chance with the SEND timer. We therefore are NOT
2454 * running the timer that the caller wants stopped. So just
2459 if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2460 stcb->asoc.num_send_timers_up--;
2461 if (stcb->asoc.num_send_timers_up < 0) {
2462 stcb->asoc.num_send_timers_up = 0;
2466 tmr->stopped_from = from;
2467 (void)SCTP_OS_TIMER_STOP(&tmr->timer);
2472 sctp_calculate_len(struct mbuf *m)
2479 tlen += SCTP_BUF_LEN(at);
2480 at = SCTP_BUF_NEXT(at);
2486 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2487 struct sctp_association *asoc, uint32_t mtu)
2490 * Reset the P-MTU size on this association, this involves changing
2491 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2492 * allow the DF flag to be cleared.
2494 struct sctp_tmit_chunk *chk;
2495 unsigned int eff_mtu, ovh;
2497 asoc->smallest_mtu = mtu;
2498 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2499 ovh = SCTP_MIN_OVERHEAD;
2501 ovh = SCTP_MIN_V4_OVERHEAD;
2503 eff_mtu = mtu - ovh;
2504 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2505 if (chk->send_size > eff_mtu) {
2506 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2509 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2510 if (chk->send_size > eff_mtu) {
2511 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2518 * Given an association and starting time of the current RTT period, update
2519 * RTO in number of msecs. net should point to the current network.
2520 * Return 1, if an RTO update was performed, return 0 if no update was
2521 * performed due to invalid starting point.
2525 sctp_calculate_rto(struct sctp_tcb *stcb,
2526 struct sctp_association *asoc,
2527 struct sctp_nets *net,
2528 struct timeval *old,
2532 uint64_t rtt_us; /* RTT in us */
2533 int32_t rtt; /* RTT in ms */
2535 int first_measure = 0;
2537 /************************/
2538 /* 1. calculate new RTT */
2539 /************************/
2540 /* get the current time */
2541 if (stcb->asoc.use_precise_time) {
2542 (void)SCTP_GETPTIME_TIMEVAL(&now);
2544 (void)SCTP_GETTIME_TIMEVAL(&now);
2546 if ((old->tv_sec > now.tv_sec) ||
2547 ((old->tv_sec == now.tv_sec) && (old->tv_sec > now.tv_sec))) {
2548 /* The starting point is in the future. */
2551 timevalsub(&now, old);
2552 rtt_us = (uint64_t)1000000 * (uint64_t)now.tv_sec + (uint64_t)now.tv_usec;
2553 if (rtt_us > SCTP_RTO_UPPER_BOUND * 1000) {
2554 /* The RTT is larger than a sane value. */
2557 /* store the current RTT in us */
2559 /* compute rtt in ms */
2560 rtt = (int32_t)(net->rtt / 1000);
2561 if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2563 * Tell the CC module that a new update has just occurred
2566 (*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2569 * Do we need to determine the lan? We do this only on sacks i.e.
2570 * RTT being determined from data not non-data (HB/INIT->INITACK).
2572 if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2573 (net->lan_type == SCTP_LAN_UNKNOWN)) {
2574 if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2575 net->lan_type = SCTP_LAN_INTERNET;
2577 net->lan_type = SCTP_LAN_LOCAL;
2581 /***************************/
2582 /* 2. update RTTVAR & SRTT */
2583 /***************************/
2585 * Compute the scaled average lastsa and the
2586 * scaled variance lastsv as described in van Jacobson
2587 * Paper "Congestion Avoidance and Control", Annex A.
2589 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
2590 * (net->lastsv >> SCTP_RTT_VAR_SHIFT) is the rttvar
2592 if (net->RTO_measured) {
2593 rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2598 rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2600 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2601 rto_logging(net, SCTP_LOG_RTTVAR);
2604 /* First RTO measurment */
2605 net->RTO_measured = 1;
2607 net->lastsa = rtt << SCTP_RTT_SHIFT;
2608 net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2609 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2610 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2613 if (net->lastsv == 0) {
2614 net->lastsv = SCTP_CLOCK_GRANULARITY;
2616 new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2617 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2618 (stcb->asoc.sat_network_lockout == 0)) {
2619 stcb->asoc.sat_network = 1;
2620 } else if ((!first_measure) && stcb->asoc.sat_network) {
2621 stcb->asoc.sat_network = 0;
2622 stcb->asoc.sat_network_lockout = 1;
2624 /* bound it, per C6/C7 in Section 5.3.1 */
2625 if (new_rto < stcb->asoc.minrto) {
2626 new_rto = stcb->asoc.minrto;
2628 if (new_rto > stcb->asoc.maxrto) {
2629 new_rto = stcb->asoc.maxrto;
2636 * return a pointer to a contiguous piece of data from the given mbuf chain
2637 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2638 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2639 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2642 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t *in_ptr)
2648 if ((off < 0) || (len <= 0))
2651 /* find the desired start location */
2652 while ((m != NULL) && (off > 0)) {
2653 if (off < SCTP_BUF_LEN(m))
2655 off -= SCTP_BUF_LEN(m);
2656 m = SCTP_BUF_NEXT(m);
2661 /* is the current mbuf large enough (eg. contiguous)? */
2662 if ((SCTP_BUF_LEN(m) - off) >= len) {
2663 return (mtod(m, caddr_t)+off);
2665 /* else, it spans more than one mbuf, so save a temp copy... */
2666 while ((m != NULL) && (len > 0)) {
2667 count = min(SCTP_BUF_LEN(m) - off, len);
2668 memcpy(ptr, mtod(m, caddr_t)+off, count);
2672 m = SCTP_BUF_NEXT(m);
2674 if ((m == NULL) && (len > 0))
2677 return ((caddr_t)in_ptr);
2683 struct sctp_paramhdr *
2684 sctp_get_next_param(struct mbuf *m,
2686 struct sctp_paramhdr *pull,
2689 /* This just provides a typed signature to Peter's Pull routine */
2690 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2696 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2698 struct mbuf *m_last;
2704 if (padlen <= M_TRAILINGSPACE(m)) {
2706 * The easy way. We hope the majority of the time we hit
2711 /* Hard way we must grow the mbuf chain */
2712 m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
2713 if (m_last == NULL) {
2716 SCTP_BUF_LEN(m_last) = 0;
2717 SCTP_BUF_NEXT(m_last) = NULL;
2718 SCTP_BUF_NEXT(m) = m_last;
2720 dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
2721 SCTP_BUF_LEN(m_last) += padlen;
2722 memset(dp, 0, padlen);
2727 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2729 /* find the last mbuf in chain and pad it */
2732 if (last_mbuf != NULL) {
2733 return (sctp_add_pad_tombuf(last_mbuf, padval));
2735 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
2736 if (SCTP_BUF_NEXT(m_at) == NULL) {
2737 return (sctp_add_pad_tombuf(m_at, padval));
2745 sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
2746 uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
2747 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2752 struct mbuf *m_notify;
2753 struct sctp_assoc_change *sac;
2754 struct sctp_queued_to_read *control;
2755 unsigned int notif_len;
2758 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2765 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2766 notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
2767 if (abort != NULL) {
2768 abort_len = ntohs(abort->ch.chunk_length);
2770 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
2773 if (abort_len > SCTP_CHUNK_BUFFER_SIZE) {
2774 abort_len = SCTP_CHUNK_BUFFER_SIZE;
2779 if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2780 notif_len += SCTP_ASSOC_SUPPORTS_MAX;
2781 } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2782 notif_len += abort_len;
2784 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2785 if (m_notify == NULL) {
2786 /* Retry with smaller value. */
2787 notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
2788 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2789 if (m_notify == NULL) {
2793 SCTP_BUF_NEXT(m_notify) = NULL;
2794 sac = mtod(m_notify, struct sctp_assoc_change *);
2795 memset(sac, 0, notif_len);
2796 sac->sac_type = SCTP_ASSOC_CHANGE;
2798 sac->sac_length = sizeof(struct sctp_assoc_change);
2799 sac->sac_state = state;
2800 sac->sac_error = error;
2801 /* XXX verify these stream counts */
2802 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2803 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2804 sac->sac_assoc_id = sctp_get_associd(stcb);
2805 if (notif_len > sizeof(struct sctp_assoc_change)) {
2806 if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2808 if (stcb->asoc.prsctp_supported == 1) {
2809 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
2811 if (stcb->asoc.auth_supported == 1) {
2812 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
2814 if (stcb->asoc.asconf_supported == 1) {
2815 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
2817 if (stcb->asoc.idata_supported == 1) {
2818 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_INTERLEAVING;
2820 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
2821 if (stcb->asoc.reconfig_supported == 1) {
2822 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
2824 sac->sac_length += i;
2825 } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2826 memcpy(sac->sac_info, abort, abort_len);
2827 sac->sac_length += abort_len;
2830 SCTP_BUF_LEN(m_notify) = sac->sac_length;
2831 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2832 0, 0, stcb->asoc.context, 0, 0, 0,
2834 if (control != NULL) {
2835 control->length = SCTP_BUF_LEN(m_notify);
2836 control->spec_flags = M_NOTIFICATION;
2837 /* not that we need this */
2838 control->tail_mbuf = m_notify;
2839 sctp_add_to_readq(stcb->sctp_ep, stcb,
2841 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2844 sctp_m_freem(m_notify);
2848 * For 1-to-1 style sockets, we send up and error when an ABORT
2852 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2853 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2854 ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2855 SOCK_LOCK(stcb->sctp_socket);
2857 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
2858 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
2859 stcb->sctp_socket->so_error = ECONNREFUSED;
2861 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
2862 stcb->sctp_socket->so_error = ECONNRESET;
2865 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
2866 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
2867 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
2868 stcb->sctp_socket->so_error = ETIMEDOUT;
2870 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
2871 stcb->sctp_socket->so_error = ECONNABORTED;
2874 SOCK_UNLOCK(stcb->sctp_socket);
2876 /* Wake ANY sleepers */
2877 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2878 so = SCTP_INP_SO(stcb->sctp_ep);
2880 atomic_add_int(&stcb->asoc.refcnt, 1);
2881 SCTP_TCB_UNLOCK(stcb);
2882 SCTP_SOCKET_LOCK(so, 1);
2883 SCTP_TCB_LOCK(stcb);
2884 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2885 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2886 SCTP_SOCKET_UNLOCK(so, 1);
2891 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2892 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2893 ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2894 socantrcvmore(stcb->sctp_socket);
2896 sorwakeup(stcb->sctp_socket);
2897 sowwakeup(stcb->sctp_socket);
2898 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2900 SCTP_SOCKET_UNLOCK(so, 1);
2906 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2907 struct sockaddr *sa, uint32_t error, int so_locked
2908 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2913 struct mbuf *m_notify;
2914 struct sctp_paddr_change *spc;
2915 struct sctp_queued_to_read *control;
2917 if ((stcb == NULL) ||
2918 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2919 /* event not enabled */
2922 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
2923 if (m_notify == NULL)
2925 SCTP_BUF_LEN(m_notify) = 0;
2926 spc = mtod(m_notify, struct sctp_paddr_change *);
2927 memset(spc, 0, sizeof(struct sctp_paddr_change));
2928 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2930 spc->spc_length = sizeof(struct sctp_paddr_change);
2931 switch (sa->sa_family) {
2935 if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2936 in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
2937 (struct sockaddr_in6 *)&spc->spc_aaddr);
2939 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2942 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2949 struct sockaddr_in6 *sin6;
2951 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2953 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2954 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2955 if (sin6->sin6_scope_id == 0) {
2956 /* recover scope_id for user */
2957 (void)sa6_recoverscope(sin6);
2959 /* clear embedded scope_id for user */
2960 in6_clearscope(&sin6->sin6_addr);
2970 spc->spc_state = state;
2971 spc->spc_error = error;
2972 spc->spc_assoc_id = sctp_get_associd(stcb);
2974 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2975 SCTP_BUF_NEXT(m_notify) = NULL;
2977 /* append to socket */
2978 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2979 0, 0, stcb->asoc.context, 0, 0, 0,
2981 if (control == NULL) {
2983 sctp_m_freem(m_notify);
2986 control->length = SCTP_BUF_LEN(m_notify);
2987 control->spec_flags = M_NOTIFICATION;
2988 /* not that we need this */
2989 control->tail_mbuf = m_notify;
2990 sctp_add_to_readq(stcb->sctp_ep, stcb,
2992 &stcb->sctp_socket->so_rcv, 1,
2993 SCTP_READ_LOCK_NOT_HELD,
2999 sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
3000 struct sctp_tmit_chunk *chk, int so_locked
3001 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3006 struct mbuf *m_notify;
3007 struct sctp_send_failed *ssf;
3008 struct sctp_send_failed_event *ssfe;
3009 struct sctp_queued_to_read *control;
3010 struct sctp_chunkhdr *chkhdr;
3011 int notifhdr_len, chk_len, chkhdr_len, padding_len, payload_len;
3013 if ((stcb == NULL) ||
3014 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
3015 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3016 /* event not enabled */
3020 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3021 notifhdr_len = sizeof(struct sctp_send_failed_event);
3023 notifhdr_len = sizeof(struct sctp_send_failed);
3025 m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3026 if (m_notify == NULL)
3029 SCTP_BUF_LEN(m_notify) = notifhdr_len;
3030 if (stcb->asoc.idata_supported) {
3031 chkhdr_len = sizeof(struct sctp_idata_chunk);
3033 chkhdr_len = sizeof(struct sctp_data_chunk);
3035 /* Use some defaults in case we can't access the chunk header */
3036 if (chk->send_size >= chkhdr_len) {
3037 payload_len = chk->send_size - chkhdr_len;
3042 if (chk->data != NULL) {
3043 chkhdr = mtod(chk->data, struct sctp_chunkhdr *);
3044 if (chkhdr != NULL) {
3045 chk_len = ntohs(chkhdr->chunk_length);
3046 if ((chk_len >= chkhdr_len) &&
3047 (chk->send_size >= chk_len) &&
3048 (chk->send_size - chk_len < 4)) {
3049 padding_len = chk->send_size - chk_len;
3050 payload_len = chk->send_size - chkhdr_len - padding_len;
3054 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3055 ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3056 memset(ssfe, 0, notifhdr_len);
3057 ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3059 ssfe->ssfe_flags = SCTP_DATA_SENT;
3061 ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3063 ssfe->ssfe_length = (uint32_t)(notifhdr_len + payload_len);
3064 ssfe->ssfe_error = error;
3065 /* not exactly what the user sent in, but should be close :) */
3066 ssfe->ssfe_info.snd_sid = chk->rec.data.sid;
3067 ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
3068 ssfe->ssfe_info.snd_ppid = chk->rec.data.ppid;
3069 ssfe->ssfe_info.snd_context = chk->rec.data.context;
3070 ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3071 ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3073 ssf = mtod(m_notify, struct sctp_send_failed *);
3074 memset(ssf, 0, notifhdr_len);
3075 ssf->ssf_type = SCTP_SEND_FAILED;
3077 ssf->ssf_flags = SCTP_DATA_SENT;
3079 ssf->ssf_flags = SCTP_DATA_UNSENT;
3081 ssf->ssf_length = (uint32_t)(notifhdr_len + payload_len);
3082 ssf->ssf_error = error;
3083 /* not exactly what the user sent in, but should be close :) */
3084 ssf->ssf_info.sinfo_stream = chk->rec.data.sid;
3085 ssf->ssf_info.sinfo_ssn = (uint16_t)chk->rec.data.mid;
3086 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
3087 ssf->ssf_info.sinfo_ppid = chk->rec.data.ppid;
3088 ssf->ssf_info.sinfo_context = chk->rec.data.context;
3089 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3090 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3092 if (chk->data != NULL) {
3093 /* Trim off the sctp chunk header (it should be there) */
3094 if (chk->send_size == chkhdr_len + payload_len + padding_len) {
3095 m_adj(chk->data, chkhdr_len);
3096 m_adj(chk->data, -padding_len);
3097 sctp_mbuf_crush(chk->data);
3098 chk->send_size -= (chkhdr_len + padding_len);
3101 SCTP_BUF_NEXT(m_notify) = chk->data;
3102 /* Steal off the mbuf */
3105 * For this case, we check the actual socket buffer, since the assoc
3106 * is going away we don't want to overfill the socket buffer for a
3109 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3110 sctp_m_freem(m_notify);
3113 /* append to socket */
3114 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3115 0, 0, stcb->asoc.context, 0, 0, 0,
3117 if (control == NULL) {
3119 sctp_m_freem(m_notify);
3122 control->length = SCTP_BUF_LEN(m_notify);
3123 control->spec_flags = M_NOTIFICATION;
3124 /* not that we need this */
3125 control->tail_mbuf = m_notify;
3126 sctp_add_to_readq(stcb->sctp_ep, stcb,
3128 &stcb->sctp_socket->so_rcv, 1,
3129 SCTP_READ_LOCK_NOT_HELD,
3135 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3136 struct sctp_stream_queue_pending *sp, int so_locked
3137 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3142 struct mbuf *m_notify;
3143 struct sctp_send_failed *ssf;
3144 struct sctp_send_failed_event *ssfe;
3145 struct sctp_queued_to_read *control;
3148 if ((stcb == NULL) ||
3149 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
3150 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3151 /* event not enabled */
3154 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3155 notifhdr_len = sizeof(struct sctp_send_failed_event);
3157 notifhdr_len = sizeof(struct sctp_send_failed);
3159 m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3160 if (m_notify == NULL) {
3164 SCTP_BUF_LEN(m_notify) = notifhdr_len;
3165 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3166 ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3167 memset(ssfe, 0, notifhdr_len);
3168 ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3169 ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3170 ssfe->ssfe_length = (uint32_t)(notifhdr_len + sp->length);
3171 ssfe->ssfe_error = error;
3172 /* not exactly what the user sent in, but should be close :) */
3173 ssfe->ssfe_info.snd_sid = sp->sid;
3174 if (sp->some_taken) {
3175 ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
3177 ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
3179 ssfe->ssfe_info.snd_ppid = sp->ppid;
3180 ssfe->ssfe_info.snd_context = sp->context;
3181 ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3182 ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3184 ssf = mtod(m_notify, struct sctp_send_failed *);
3185 memset(ssf, 0, notifhdr_len);
3186 ssf->ssf_type = SCTP_SEND_FAILED;
3187 ssf->ssf_flags = SCTP_DATA_UNSENT;
3188 ssf->ssf_length = (uint32_t)(notifhdr_len + sp->length);
3189 ssf->ssf_error = error;
3190 /* not exactly what the user sent in, but should be close :) */
3191 ssf->ssf_info.sinfo_stream = sp->sid;
3192 ssf->ssf_info.sinfo_ssn = 0;
3193 if (sp->some_taken) {
3194 ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3196 ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3198 ssf->ssf_info.sinfo_ppid = sp->ppid;
3199 ssf->ssf_info.sinfo_context = sp->context;
3200 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3201 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3203 SCTP_BUF_NEXT(m_notify) = sp->data;
3205 /* Steal off the mbuf */
3208 * For this case, we check the actual socket buffer, since the assoc
3209 * is going away we don't want to overfill the socket buffer for a
3212 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3213 sctp_m_freem(m_notify);
3216 /* append to socket */
3217 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3218 0, 0, stcb->asoc.context, 0, 0, 0,
3220 if (control == NULL) {
3222 sctp_m_freem(m_notify);
3225 control->length = SCTP_BUF_LEN(m_notify);
3226 control->spec_flags = M_NOTIFICATION;
3227 /* not that we need this */
3228 control->tail_mbuf = m_notify;
3229 sctp_add_to_readq(stcb->sctp_ep, stcb,
3231 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3237 sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3239 struct mbuf *m_notify;
3240 struct sctp_adaptation_event *sai;
3241 struct sctp_queued_to_read *control;
3243 if ((stcb == NULL) ||
3244 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3245 /* event not enabled */
3249 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3250 if (m_notify == NULL)
3253 SCTP_BUF_LEN(m_notify) = 0;
3254 sai = mtod(m_notify, struct sctp_adaptation_event *);
3255 memset(sai, 0, sizeof(struct sctp_adaptation_event));
3256 sai->sai_type = SCTP_ADAPTATION_INDICATION;
3258 sai->sai_length = sizeof(struct sctp_adaptation_event);
3259 sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3260 sai->sai_assoc_id = sctp_get_associd(stcb);
3262 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3263 SCTP_BUF_NEXT(m_notify) = NULL;
3265 /* append to socket */
3266 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3267 0, 0, stcb->asoc.context, 0, 0, 0,
3269 if (control == NULL) {
3271 sctp_m_freem(m_notify);
3274 control->length = SCTP_BUF_LEN(m_notify);
3275 control->spec_flags = M_NOTIFICATION;
3276 /* not that we need this */
3277 control->tail_mbuf = m_notify;
3278 sctp_add_to_readq(stcb->sctp_ep, stcb,
3280 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3283 /* This always must be called with the read-queue LOCKED in the INP */
3285 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3286 uint32_t val, int so_locked
3287 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3292 struct mbuf *m_notify;
3293 struct sctp_pdapi_event *pdapi;
3294 struct sctp_queued_to_read *control;
3297 if ((stcb == NULL) ||
3298 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3299 /* event not enabled */
3302 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3306 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3307 if (m_notify == NULL)
3310 SCTP_BUF_LEN(m_notify) = 0;
3311 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3312 memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3313 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3314 pdapi->pdapi_flags = 0;
3315 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3316 pdapi->pdapi_indication = error;
3317 pdapi->pdapi_stream = (val >> 16);
3318 pdapi->pdapi_seq = (val & 0x0000ffff);
3319 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3321 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3322 SCTP_BUF_NEXT(m_notify) = NULL;
3323 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3324 0, 0, stcb->asoc.context, 0, 0, 0,
3326 if (control == NULL) {
3328 sctp_m_freem(m_notify);
3331 control->length = SCTP_BUF_LEN(m_notify);
3332 control->spec_flags = M_NOTIFICATION;
3333 /* not that we need this */
3334 control->tail_mbuf = m_notify;
3335 sb = &stcb->sctp_socket->so_rcv;
3336 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3337 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
3339 sctp_sballoc(stcb, sb, m_notify);
3340 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3341 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3343 control->end_added = 1;
3344 if (stcb->asoc.control_pdapi)
3345 TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
3347 /* we really should not see this case */
3348 TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
3350 if (stcb->sctp_ep && stcb->sctp_socket) {
3351 /* This should always be the case */
3352 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3355 so = SCTP_INP_SO(stcb->sctp_ep);
3357 atomic_add_int(&stcb->asoc.refcnt, 1);
3358 SCTP_TCB_UNLOCK(stcb);
3359 SCTP_SOCKET_LOCK(so, 1);
3360 SCTP_TCB_LOCK(stcb);
3361 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3362 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3363 SCTP_SOCKET_UNLOCK(so, 1);
3368 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3369 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3371 SCTP_SOCKET_UNLOCK(so, 1);
3378 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3380 struct mbuf *m_notify;
3381 struct sctp_shutdown_event *sse;
3382 struct sctp_queued_to_read *control;
3385 * For TCP model AND UDP connected sockets we will send an error up
3386 * when an SHUTDOWN completes
3388 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3389 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3390 /* mark socket closed for read/write and wakeup! */
3391 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3394 so = SCTP_INP_SO(stcb->sctp_ep);
3395 atomic_add_int(&stcb->asoc.refcnt, 1);
3396 SCTP_TCB_UNLOCK(stcb);
3397 SCTP_SOCKET_LOCK(so, 1);
3398 SCTP_TCB_LOCK(stcb);
3399 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3400 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3401 SCTP_SOCKET_UNLOCK(so, 1);
3405 socantsendmore(stcb->sctp_socket);
3406 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3407 SCTP_SOCKET_UNLOCK(so, 1);
3410 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3411 /* event not enabled */
3415 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3416 if (m_notify == NULL)
3419 sse = mtod(m_notify, struct sctp_shutdown_event *);
3420 memset(sse, 0, sizeof(struct sctp_shutdown_event));
3421 sse->sse_type = SCTP_SHUTDOWN_EVENT;
3423 sse->sse_length = sizeof(struct sctp_shutdown_event);
3424 sse->sse_assoc_id = sctp_get_associd(stcb);
3426 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3427 SCTP_BUF_NEXT(m_notify) = NULL;
3429 /* append to socket */
3430 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3431 0, 0, stcb->asoc.context, 0, 0, 0,
3433 if (control == NULL) {
3435 sctp_m_freem(m_notify);
3438 control->length = SCTP_BUF_LEN(m_notify);
3439 control->spec_flags = M_NOTIFICATION;
3440 /* not that we need this */
3441 control->tail_mbuf = m_notify;
3442 sctp_add_to_readq(stcb->sctp_ep, stcb,
3444 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3448 sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3450 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3455 struct mbuf *m_notify;
3456 struct sctp_sender_dry_event *event;
3457 struct sctp_queued_to_read *control;
3459 if ((stcb == NULL) ||
3460 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3461 /* event not enabled */
3465 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3466 if (m_notify == NULL) {
3470 SCTP_BUF_LEN(m_notify) = 0;
3471 event = mtod(m_notify, struct sctp_sender_dry_event *);
3472 memset(event, 0, sizeof(struct sctp_sender_dry_event));
3473 event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3474 event->sender_dry_flags = 0;
3475 event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3476 event->sender_dry_assoc_id = sctp_get_associd(stcb);
3478 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3479 SCTP_BUF_NEXT(m_notify) = NULL;
3481 /* append to socket */
3482 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3483 0, 0, stcb->asoc.context, 0, 0, 0,
3485 if (control == NULL) {
3487 sctp_m_freem(m_notify);
3490 control->length = SCTP_BUF_LEN(m_notify);
3491 control->spec_flags = M_NOTIFICATION;
3492 /* not that we need this */
3493 control->tail_mbuf = m_notify;
3494 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3495 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3500 sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3502 struct mbuf *m_notify;
3503 struct sctp_queued_to_read *control;
3504 struct sctp_stream_change_event *stradd;
3506 if ((stcb == NULL) ||
3507 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3508 /* event not enabled */
3511 if ((stcb->asoc.peer_req_out) && flag) {
3512 /* Peer made the request, don't tell the local user */
3513 stcb->asoc.peer_req_out = 0;
3516 stcb->asoc.peer_req_out = 0;
3517 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3518 if (m_notify == NULL)
3521 SCTP_BUF_LEN(m_notify) = 0;
3522 stradd = mtod(m_notify, struct sctp_stream_change_event *);
3523 memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3524 stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3525 stradd->strchange_flags = flag;
3526 stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3527 stradd->strchange_assoc_id = sctp_get_associd(stcb);
3528 stradd->strchange_instrms = numberin;
3529 stradd->strchange_outstrms = numberout;
3530 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3531 SCTP_BUF_NEXT(m_notify) = NULL;
3532 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3534 sctp_m_freem(m_notify);
3537 /* append to socket */
3538 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3539 0, 0, stcb->asoc.context, 0, 0, 0,
3541 if (control == NULL) {
3543 sctp_m_freem(m_notify);
3546 control->length = SCTP_BUF_LEN(m_notify);
3547 control->spec_flags = M_NOTIFICATION;
3548 /* not that we need this */
3549 control->tail_mbuf = m_notify;
3550 sctp_add_to_readq(stcb->sctp_ep, stcb,
3552 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3556 sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3558 struct mbuf *m_notify;
3559 struct sctp_queued_to_read *control;
3560 struct sctp_assoc_reset_event *strasoc;
3562 if ((stcb == NULL) ||
3563 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3564 /* event not enabled */
3567 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3568 if (m_notify == NULL)
3571 SCTP_BUF_LEN(m_notify) = 0;
3572 strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3573 memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3574 strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3575 strasoc->assocreset_flags = flag;
3576 strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3577 strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3578 strasoc->assocreset_local_tsn = sending_tsn;
3579 strasoc->assocreset_remote_tsn = recv_tsn;
3580 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3581 SCTP_BUF_NEXT(m_notify) = NULL;
3582 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3584 sctp_m_freem(m_notify);
3587 /* append to socket */
3588 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3589 0, 0, stcb->asoc.context, 0, 0, 0,
3591 if (control == NULL) {
3593 sctp_m_freem(m_notify);
3596 control->length = SCTP_BUF_LEN(m_notify);
3597 control->spec_flags = M_NOTIFICATION;
3598 /* not that we need this */
3599 control->tail_mbuf = m_notify;
3600 sctp_add_to_readq(stcb->sctp_ep, stcb,
3602 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3608 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3609 int number_entries, uint16_t *list, int flag)
3611 struct mbuf *m_notify;
3612 struct sctp_queued_to_read *control;
3613 struct sctp_stream_reset_event *strreset;
3616 if ((stcb == NULL) ||
3617 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3618 /* event not enabled */
3622 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3623 if (m_notify == NULL)
3626 SCTP_BUF_LEN(m_notify) = 0;
3627 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3628 if (len > M_TRAILINGSPACE(m_notify)) {
3629 /* never enough room */
3630 sctp_m_freem(m_notify);
3633 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3634 memset(strreset, 0, len);
3635 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3636 strreset->strreset_flags = flag;
3637 strreset->strreset_length = len;
3638 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3639 if (number_entries) {
3642 for (i = 0; i < number_entries; i++) {
3643 strreset->strreset_stream_list[i] = ntohs(list[i]);
3646 SCTP_BUF_LEN(m_notify) = len;
3647 SCTP_BUF_NEXT(m_notify) = NULL;
3648 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3650 sctp_m_freem(m_notify);
3653 /* append to socket */
3654 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3655 0, 0, stcb->asoc.context, 0, 0, 0,
3657 if (control == NULL) {
3659 sctp_m_freem(m_notify);
3662 control->length = SCTP_BUF_LEN(m_notify);
3663 control->spec_flags = M_NOTIFICATION;
3664 /* not that we need this */
3665 control->tail_mbuf = m_notify;
3666 sctp_add_to_readq(stcb->sctp_ep, stcb,
3668 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3673 sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3675 struct mbuf *m_notify;
3676 struct sctp_remote_error *sre;
3677 struct sctp_queued_to_read *control;
3678 unsigned int notif_len;
3681 if ((stcb == NULL) ||
3682 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3685 if (chunk != NULL) {
3686 chunk_len = ntohs(chunk->ch.chunk_length);
3688 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
3691 if (chunk_len > SCTP_CHUNK_BUFFER_SIZE) {
3692 chunk_len = SCTP_CHUNK_BUFFER_SIZE;
3697 notif_len = (unsigned int)(sizeof(struct sctp_remote_error) + chunk_len);
3698 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3699 if (m_notify == NULL) {
3700 /* Retry with smaller value. */
3701 notif_len = (unsigned int)sizeof(struct sctp_remote_error);
3702 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3703 if (m_notify == NULL) {
3707 SCTP_BUF_NEXT(m_notify) = NULL;
3708 sre = mtod(m_notify, struct sctp_remote_error *);
3709 memset(sre, 0, notif_len);
3710 sre->sre_type = SCTP_REMOTE_ERROR;
3712 sre->sre_length = sizeof(struct sctp_remote_error);
3713 sre->sre_error = error;
3714 sre->sre_assoc_id = sctp_get_associd(stcb);
3715 if (notif_len > sizeof(struct sctp_remote_error)) {
3716 memcpy(sre->sre_data, chunk, chunk_len);
3717 sre->sre_length += chunk_len;
3719 SCTP_BUF_LEN(m_notify) = sre->sre_length;
3720 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3721 0, 0, stcb->asoc.context, 0, 0, 0,
3723 if (control != NULL) {
3724 control->length = SCTP_BUF_LEN(m_notify);
3725 control->spec_flags = M_NOTIFICATION;
3726 /* not that we need this */
3727 control->tail_mbuf = m_notify;
3728 sctp_add_to_readq(stcb->sctp_ep, stcb,
3730 &stcb->sctp_socket->so_rcv, 1,
3731 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3733 sctp_m_freem(m_notify);
3739 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3740 uint32_t error, void *data, int so_locked
3741 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3746 if ((stcb == NULL) ||
3747 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3748 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3749 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3750 /* If the socket is gone we are out of here */
3753 if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3756 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3757 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3758 if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
3759 (notification == SCTP_NOTIFY_INTERFACE_UP) ||
3760 (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
3761 /* Don't report these in front states */
3765 switch (notification) {
3766 case SCTP_NOTIFY_ASSOC_UP:
3767 if (stcb->asoc.assoc_up_sent == 0) {
3768 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
3769 stcb->asoc.assoc_up_sent = 1;
3771 if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
3772 sctp_notify_adaptation_layer(stcb);
3774 if (stcb->asoc.auth_supported == 0) {
3775 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3779 case SCTP_NOTIFY_ASSOC_DOWN:
3780 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
3782 case SCTP_NOTIFY_INTERFACE_DOWN:
3784 struct sctp_nets *net;
3786 net = (struct sctp_nets *)data;
3787 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3788 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3791 case SCTP_NOTIFY_INTERFACE_UP:
3793 struct sctp_nets *net;
3795 net = (struct sctp_nets *)data;
3796 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3797 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3800 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3802 struct sctp_nets *net;
3804 net = (struct sctp_nets *)data;
3805 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3806 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3809 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3810 sctp_notify_send_failed2(stcb, error,
3811 (struct sctp_stream_queue_pending *)data, so_locked);
3813 case SCTP_NOTIFY_SENT_DG_FAIL:
3814 sctp_notify_send_failed(stcb, 1, error,
3815 (struct sctp_tmit_chunk *)data, so_locked);
3817 case SCTP_NOTIFY_UNSENT_DG_FAIL:
3818 sctp_notify_send_failed(stcb, 0, error,
3819 (struct sctp_tmit_chunk *)data, so_locked);
3821 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3825 val = *((uint32_t *)data);
3827 sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3830 case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
3831 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3832 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3833 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
3835 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
3838 case SCTP_NOTIFY_ASSOC_REM_ABORTED:
3839 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3840 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3841 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
3843 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
3846 case SCTP_NOTIFY_ASSOC_RESTART:
3847 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
3848 if (stcb->asoc.auth_supported == 0) {
3849 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3853 case SCTP_NOTIFY_STR_RESET_SEND:
3854 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_OUTGOING_SSN);
3856 case SCTP_NOTIFY_STR_RESET_RECV:
3857 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_INCOMING);
3859 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3860 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3861 (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
3863 case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
3864 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3865 (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
3867 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3868 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3869 (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
3871 case SCTP_NOTIFY_STR_RESET_DENIED_IN:
3872 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3873 (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
3875 case SCTP_NOTIFY_ASCONF_ADD_IP:
3876 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3879 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3880 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3883 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3884 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3887 case SCTP_NOTIFY_PEER_SHUTDOWN:
3888 sctp_notify_shutdown_event(stcb);
3890 case SCTP_NOTIFY_AUTH_NEW_KEY:
3891 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
3892 (uint16_t)(uintptr_t)data,
3895 case SCTP_NOTIFY_AUTH_FREE_KEY:
3896 sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3897 (uint16_t)(uintptr_t)data,
3900 case SCTP_NOTIFY_NO_PEER_AUTH:
3901 sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3902 (uint16_t)(uintptr_t)data,
3905 case SCTP_NOTIFY_SENDER_DRY:
3906 sctp_notify_sender_dry_event(stcb, so_locked);
3908 case SCTP_NOTIFY_REMOTE_ERROR:
3909 sctp_notify_remote_error(stcb, error, data);
3912 SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3913 __func__, notification, notification);
3919 sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
3920 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3925 struct sctp_association *asoc;
3926 struct sctp_stream_out *outs;
3927 struct sctp_tmit_chunk *chk, *nchk;
3928 struct sctp_stream_queue_pending *sp, *nsp;
3935 if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3936 /* already being freed */
3939 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3940 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3941 (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
3944 /* now through all the gunk freeing chunks */
3945 if (holds_lock == 0) {
3946 SCTP_TCB_SEND_LOCK(stcb);
3948 /* sent queue SHOULD be empty */
3949 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3950 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3951 asoc->sent_queue_cnt--;
3952 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
3953 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
3954 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
3957 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
3961 if (chk->data != NULL) {
3962 sctp_free_bufspace(stcb, asoc, chk, 1);
3963 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
3964 error, chk, so_locked);
3966 sctp_m_freem(chk->data);
3970 sctp_free_a_chunk(stcb, chk, so_locked);
3971 /* sa_ignore FREED_MEMORY */
3973 /* pending send queue SHOULD be empty */
3974 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3975 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3976 asoc->send_queue_cnt--;
3977 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
3978 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
3981 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
3984 if (chk->data != NULL) {
3985 sctp_free_bufspace(stcb, asoc, chk, 1);
3986 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
3987 error, chk, so_locked);
3989 sctp_m_freem(chk->data);
3993 sctp_free_a_chunk(stcb, chk, so_locked);
3994 /* sa_ignore FREED_MEMORY */
3996 for (i = 0; i < asoc->streamoutcnt; i++) {
3997 /* For each stream */
3998 outs = &asoc->strmout[i];
3999 /* clean up any sends there */
4000 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
4001 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
4002 TAILQ_REMOVE(&outs->outqueue, sp, next);
4003 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
4004 sctp_free_spbufspace(stcb, asoc, sp);
4006 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
4007 error, (void *)sp, so_locked);
4009 sctp_m_freem(sp->data);
4011 sp->tail_mbuf = NULL;
4016 sctp_free_remote_addr(sp->net);
4019 /* Free the chunk */
4020 sctp_free_a_strmoq(stcb, sp, so_locked);
4021 /* sa_ignore FREED_MEMORY */
4025 if (holds_lock == 0) {
4026 SCTP_TCB_SEND_UNLOCK(stcb);
4031 sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
4032 struct sctp_abort_chunk *abort, int so_locked
4033 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4041 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4042 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4043 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4044 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
4046 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4047 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4048 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4051 /* Tell them we lost the asoc */
4052 sctp_report_all_outbound(stcb, error, 0, so_locked);
4054 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
4056 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
4061 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4062 struct mbuf *m, int iphlen,
4063 struct sockaddr *src, struct sockaddr *dst,
4064 struct sctphdr *sh, struct mbuf *op_err,
4065 uint8_t mflowtype, uint32_t mflowid,
4066 uint32_t vrf_id, uint16_t port)
4069 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4075 vtag = stcb->asoc.peer_vtag;
4076 vrf_id = stcb->asoc.vrf_id;
4078 sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
4079 mflowtype, mflowid, inp->fibnum,
4082 /* We have a TCB to abort, send notification too */
4083 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
4084 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4085 /* Ok, now lets free it */
4086 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4087 so = SCTP_INP_SO(inp);
4088 atomic_add_int(&stcb->asoc.refcnt, 1);
4089 SCTP_TCB_UNLOCK(stcb);
4090 SCTP_SOCKET_LOCK(so, 1);
4091 SCTP_TCB_LOCK(stcb);
4092 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4094 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4095 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4096 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4097 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4099 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4100 SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
4101 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4102 SCTP_SOCKET_UNLOCK(so, 1);
4106 #ifdef SCTP_ASOCLOG_OF_TSNS
4108 sctp_print_out_track_log(struct sctp_tcb *stcb)
4113 SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
4114 SCTP_PRINTF("IN bound TSN log-aaa\n");
4115 if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
4116 SCTP_PRINTF("None rcvd\n");
4119 if (stcb->asoc.tsn_in_wrapped) {
4120 for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
4121 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4122 stcb->asoc.in_tsnlog[i].tsn,
4123 stcb->asoc.in_tsnlog[i].strm,
4124 stcb->asoc.in_tsnlog[i].seq,
4125 stcb->asoc.in_tsnlog[i].flgs,
4126 stcb->asoc.in_tsnlog[i].sz);
4129 if (stcb->asoc.tsn_in_at) {
4130 for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
4131 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4132 stcb->asoc.in_tsnlog[i].tsn,
4133 stcb->asoc.in_tsnlog[i].strm,
4134 stcb->asoc.in_tsnlog[i].seq,
4135 stcb->asoc.in_tsnlog[i].flgs,
4136 stcb->asoc.in_tsnlog[i].sz);
4140 SCTP_PRINTF("OUT bound TSN log-aaa\n");
4141 if ((stcb->asoc.tsn_out_at == 0) &&
4142 (stcb->asoc.tsn_out_wrapped == 0)) {
4143 SCTP_PRINTF("None sent\n");
4145 if (stcb->asoc.tsn_out_wrapped) {
4146 for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4147 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4148 stcb->asoc.out_tsnlog[i].tsn,
4149 stcb->asoc.out_tsnlog[i].strm,
4150 stcb->asoc.out_tsnlog[i].seq,
4151 stcb->asoc.out_tsnlog[i].flgs,
4152 stcb->asoc.out_tsnlog[i].sz);
4155 if (stcb->asoc.tsn_out_at) {
4156 for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4157 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4158 stcb->asoc.out_tsnlog[i].tsn,
4159 stcb->asoc.out_tsnlog[i].strm,
4160 stcb->asoc.out_tsnlog[i].seq,
4161 stcb->asoc.out_tsnlog[i].flgs,
4162 stcb->asoc.out_tsnlog[i].sz);
4170 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4171 struct mbuf *op_err,
4173 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4178 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4182 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4183 so = SCTP_INP_SO(inp);
4186 /* Got to have a TCB */
4187 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4188 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4189 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4190 SCTP_CALLED_DIRECTLY_NOCMPSET);
4195 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4197 /* notify the peer */
4198 sctp_send_abort_tcb(stcb, op_err, so_locked);
4199 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4200 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4201 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4202 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4204 /* notify the ulp */
4205 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4206 sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4208 /* now free the asoc */
4209 #ifdef SCTP_ASOCLOG_OF_TSNS
4210 sctp_print_out_track_log(stcb);
4212 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4214 atomic_add_int(&stcb->asoc.refcnt, 1);
4215 SCTP_TCB_UNLOCK(stcb);
4216 SCTP_SOCKET_LOCK(so, 1);
4217 SCTP_TCB_LOCK(stcb);
4218 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4221 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4222 SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4223 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4225 SCTP_SOCKET_UNLOCK(so, 1);
4231 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4232 struct sockaddr *src, struct sockaddr *dst,
4233 struct sctphdr *sh, struct sctp_inpcb *inp,
4235 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4236 uint32_t vrf_id, uint16_t port)
4238 struct sctp_chunkhdr *ch, chunk_buf;
4239 unsigned int chk_length;
4240 int contains_init_chunk;
4242 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4243 /* Generate a TO address for future reference */
4244 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4245 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4246 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4247 SCTP_CALLED_DIRECTLY_NOCMPSET);
4250 contains_init_chunk = 0;
4251 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4252 sizeof(*ch), (uint8_t *)&chunk_buf);
4253 while (ch != NULL) {
4254 chk_length = ntohs(ch->chunk_length);
4255 if (chk_length < sizeof(*ch)) {
4256 /* break to abort land */
4259 switch (ch->chunk_type) {
4261 contains_init_chunk = 1;
4263 case SCTP_PACKET_DROPPED:
4264 /* we don't respond to pkt-dropped */
4266 case SCTP_ABORT_ASSOCIATION:
4267 /* we don't respond with an ABORT to an ABORT */
4269 case SCTP_SHUTDOWN_COMPLETE:
4271 * we ignore it since we are not waiting for it and
4275 case SCTP_SHUTDOWN_ACK:
4276 sctp_send_shutdown_complete2(src, dst, sh,
4277 mflowtype, mflowid, fibnum,
4283 offset += SCTP_SIZE32(chk_length);
4284 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4285 sizeof(*ch), (uint8_t *)&chunk_buf);
4287 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4288 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4289 (contains_init_chunk == 0))) {
4290 sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4291 mflowtype, mflowid, fibnum,
4297 * check the inbound datagram to make sure there is not an abort inside it,
4298 * if there is return 1, else return 0.
4301 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t *vtagfill)
4303 struct sctp_chunkhdr *ch;
4304 struct sctp_init_chunk *init_chk, chunk_buf;
4306 unsigned int chk_length;
4308 offset = iphlen + sizeof(struct sctphdr);
4309 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4310 (uint8_t *)&chunk_buf);
4311 while (ch != NULL) {
4312 chk_length = ntohs(ch->chunk_length);
4313 if (chk_length < sizeof(*ch)) {
4314 /* packet is probably corrupt */
4317 /* we seem to be ok, is it an abort? */
4318 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4319 /* yep, tell them */
4322 if (ch->chunk_type == SCTP_INITIATION) {
4323 /* need to update the Vtag */
4324 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4325 offset, sizeof(*init_chk), (uint8_t *)&chunk_buf);
4326 if (init_chk != NULL) {
4327 *vtagfill = ntohl(init_chk->init.initiate_tag);
4330 /* Nope, move to the next chunk */
4331 offset += SCTP_SIZE32(chk_length);
4332 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4333 sizeof(*ch), (uint8_t *)&chunk_buf);
4339 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4340 * set (i.e. it's 0) so, create this function to compare link local scopes
4344 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4346 struct sockaddr_in6 a, b;
4352 if (a.sin6_scope_id == 0)
4353 if (sa6_recoverscope(&a)) {
4354 /* can't get scope, so can't match */
4357 if (b.sin6_scope_id == 0)
4358 if (sa6_recoverscope(&b)) {
4359 /* can't get scope, so can't match */
4362 if (a.sin6_scope_id != b.sin6_scope_id)
4369 * returns a sockaddr_in6 with embedded scope recovered and removed
4371 struct sockaddr_in6 *
4372 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4374 /* check and strip embedded scope junk */
4375 if (addr->sin6_family == AF_INET6) {
4376 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4377 if (addr->sin6_scope_id == 0) {
4379 if (!sa6_recoverscope(store)) {
4380 /* use the recovered scope */
4384 /* else, return the original "to" addr */
4385 in6_clearscope(&addr->sin6_addr);
4394 * are the two addresses the same? currently a "scopeless" check returns: 1
4398 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4402 if (sa1 == NULL || sa2 == NULL)
4405 /* must be the same family */
4406 if (sa1->sa_family != sa2->sa_family)
4409 switch (sa1->sa_family) {
4413 /* IPv6 addresses */
4414 struct sockaddr_in6 *sin6_1, *sin6_2;
4416 sin6_1 = (struct sockaddr_in6 *)sa1;
4417 sin6_2 = (struct sockaddr_in6 *)sa2;
4418 return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4425 /* IPv4 addresses */
4426 struct sockaddr_in *sin_1, *sin_2;
4428 sin_1 = (struct sockaddr_in *)sa1;
4429 sin_2 = (struct sockaddr_in *)sa2;
4430 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
4434 /* we don't do these... */
4440 sctp_print_address(struct sockaddr *sa)
4443 char ip6buf[INET6_ADDRSTRLEN];
4446 switch (sa->sa_family) {
4450 struct sockaddr_in6 *sin6;
4452 sin6 = (struct sockaddr_in6 *)sa;
4453 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
4454 ip6_sprintf(ip6buf, &sin6->sin6_addr),
4455 ntohs(sin6->sin6_port),
4456 sin6->sin6_scope_id);
4463 struct sockaddr_in *sin;
4466 sin = (struct sockaddr_in *)sa;
4467 p = (unsigned char *)&sin->sin_addr;
4468 SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4469 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
4480 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4481 struct sctp_inpcb *new_inp,
4482 struct sctp_tcb *stcb,
4486 * go through our old INP and pull off any control structures that
4487 * belong to stcb and move then to the new inp.
4489 struct socket *old_so, *new_so;
4490 struct sctp_queued_to_read *control, *nctl;
4491 struct sctp_readhead tmp_queue;
4495 old_so = old_inp->sctp_socket;
4496 new_so = new_inp->sctp_socket;
4497 TAILQ_INIT(&tmp_queue);
4498 error = sblock(&old_so->so_rcv, waitflags);
4501 * Gak, can't get sblock, we have a problem. data will be
4502 * left stranded.. and we don't dare look at it since the
4503 * other thread may be reading something. Oh well, its a
4504 * screwed up app that does a peeloff OR a accept while
4505 * reading from the main socket... actually its only the
4506 * peeloff() case, since I think read will fail on a
4507 * listening socket..
4511 /* lock the socket buffers */
4512 SCTP_INP_READ_LOCK(old_inp);
4513 TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4514 /* Pull off all for out target stcb */
4515 if (control->stcb == stcb) {
4516 /* remove it we want it */
4517 TAILQ_REMOVE(&old_inp->read_queue, control, next);
4518 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4521 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4522 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
4524 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4525 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4526 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4528 m = SCTP_BUF_NEXT(m);
4532 SCTP_INP_READ_UNLOCK(old_inp);
4533 /* Remove the sb-lock on the old socket */
4535 sbunlock(&old_so->so_rcv);
4536 /* Now we move them over to the new socket buffer */
4537 SCTP_INP_READ_LOCK(new_inp);
4538 TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4539 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4542 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4543 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4545 sctp_sballoc(stcb, &new_so->so_rcv, m);
4546 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4547 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4549 m = SCTP_BUF_NEXT(m);
4552 SCTP_INP_READ_UNLOCK(new_inp);
4556 sctp_wakeup_the_read_socket(struct sctp_inpcb *inp,
4557 struct sctp_tcb *stcb,
4559 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4564 if ((inp != NULL) && (inp->sctp_socket != NULL)) {
4565 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4568 so = SCTP_INP_SO(inp);
4571 atomic_add_int(&stcb->asoc.refcnt, 1);
4572 SCTP_TCB_UNLOCK(stcb);
4574 SCTP_SOCKET_LOCK(so, 1);
4576 SCTP_TCB_LOCK(stcb);
4577 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4579 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4580 SCTP_SOCKET_UNLOCK(so, 1);
4585 sctp_sorwakeup(inp, inp->sctp_socket);
4586 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4588 SCTP_SOCKET_UNLOCK(so, 1);
4595 sctp_add_to_readq(struct sctp_inpcb *inp,
4596 struct sctp_tcb *stcb,
4597 struct sctp_queued_to_read *control,
4600 int inp_read_lock_held,
4602 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4608 * Here we must place the control on the end of the socket read
4609 * queue AND increment sb_cc so that select will work properly on
4612 struct mbuf *m, *prev = NULL;
4617 panic("Gak, inp NULL on add_to_readq");
4621 if (inp_read_lock_held == 0)
4622 SCTP_INP_READ_LOCK(inp);
4623 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4624 if (!control->on_strm_q) {
4625 sctp_free_remote_addr(control->whoFrom);
4626 if (control->data) {
4627 sctp_m_freem(control->data);
4628 control->data = NULL;
4630 sctp_free_a_readq(stcb, control);
4632 if (inp_read_lock_held == 0)
4633 SCTP_INP_READ_UNLOCK(inp);
4636 if (!(control->spec_flags & M_NOTIFICATION)) {
4637 atomic_add_int(&inp->total_recvs, 1);
4638 if (!control->do_not_ref_stcb) {
4639 atomic_add_int(&stcb->total_recvs, 1);
4643 control->held_length = 0;
4644 control->length = 0;
4646 if (SCTP_BUF_LEN(m) == 0) {
4647 /* Skip mbufs with NO length */
4650 control->data = sctp_m_free(m);
4653 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4654 m = SCTP_BUF_NEXT(prev);
4657 control->tail_mbuf = prev;
4662 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4663 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4665 sctp_sballoc(stcb, sb, m);
4666 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4667 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4669 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4670 m = SCTP_BUF_NEXT(m);
4673 control->tail_mbuf = prev;
4675 /* Everything got collapsed out?? */
4676 if (!control->on_strm_q) {
4677 sctp_free_remote_addr(control->whoFrom);
4678 sctp_free_a_readq(stcb, control);
4680 if (inp_read_lock_held == 0)
4681 SCTP_INP_READ_UNLOCK(inp);
4685 control->end_added = 1;
4687 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4688 control->on_read_q = 1;
4689 if (inp_read_lock_held == 0)
4690 SCTP_INP_READ_UNLOCK(inp);
4691 if (inp && inp->sctp_socket) {
4692 sctp_wakeup_the_read_socket(inp, stcb, so_locked);
4696 /*************HOLD THIS COMMENT FOR PATCH FILE OF
4697 *************ALTERNATE ROUTING CODE
4700 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4701 *************ALTERNATE ROUTING CODE
4705 sctp_generate_cause(uint16_t code, char *info)
4708 struct sctp_gen_error_cause *cause;
4712 if ((code == 0) || (info == NULL)) {
4715 info_len = strlen(info);
4716 if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
4719 len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
4720 m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4722 SCTP_BUF_LEN(m) = len;
4723 cause = mtod(m, struct sctp_gen_error_cause *);
4724 cause->code = htons(code);
4725 cause->length = htons(len);
4726 memcpy(cause->info, info, info_len);
4732 sctp_generate_no_user_data_cause(uint32_t tsn)
4735 struct sctp_error_no_user_data *no_user_data_cause;
4738 len = (uint16_t)sizeof(struct sctp_error_no_user_data);
4739 m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4741 SCTP_BUF_LEN(m) = len;
4742 no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
4743 no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
4744 no_user_data_cause->cause.length = htons(len);
4745 no_user_data_cause->tsn = htonl(tsn);
4750 #ifdef SCTP_MBCNT_LOGGING
4752 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4753 struct sctp_tmit_chunk *tp1, int chk_cnt)
4755 if (tp1->data == NULL) {
4758 asoc->chunks_on_out_queue -= chk_cnt;
4759 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4760 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4761 asoc->total_output_queue_size,
4766 if (asoc->total_output_queue_size >= tp1->book_size) {
4767 atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4769 asoc->total_output_queue_size = 0;
4772 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4773 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4774 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4775 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4777 stcb->sctp_socket->so_snd.sb_cc = 0;
4786 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4787 uint8_t sent, int so_locked
4788 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4793 struct sctp_stream_out *strq;
4794 struct sctp_tmit_chunk *chk = NULL, *tp2;
4795 struct sctp_stream_queue_pending *sp;
4798 uint8_t foundeom = 0;
4801 int do_wakeup_routine = 0;
4803 sid = tp1->rec.data.sid;
4804 mid = tp1->rec.data.mid;
4805 if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
4806 stcb->asoc.abandoned_sent[0]++;
4807 stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4808 stcb->asoc.strmout[sid].abandoned_sent[0]++;
4809 #if defined(SCTP_DETAILED_STR_STATS)
4810 stcb->asoc.strmout[sid].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4813 stcb->asoc.abandoned_unsent[0]++;
4814 stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4815 stcb->asoc.strmout[sid].abandoned_unsent[0]++;
4816 #if defined(SCTP_DETAILED_STR_STATS)
4817 stcb->asoc.strmout[sid].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4821 ret_sz += tp1->book_size;
4822 if (tp1->data != NULL) {
4823 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4824 sctp_flight_size_decrease(tp1);
4825 sctp_total_flight_decrease(stcb, tp1);
4827 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4828 stcb->asoc.peers_rwnd += tp1->send_size;
4829 stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
4831 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
4833 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
4836 sctp_m_freem(tp1->data);
4839 do_wakeup_routine = 1;
4840 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4841 stcb->asoc.sent_queue_cnt_removeable--;
4844 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4845 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4846 SCTP_DATA_NOT_FRAG) {
4847 /* not frag'ed we ae done */
4850 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4851 /* end of frag, we are done */
4856 * Its a begin or middle piece, we must mark all of
4860 tp1 = TAILQ_NEXT(tp1, sctp_next);
4862 } while (tp1 && notdone);
4863 if (foundeom == 0) {
4865 * The multi-part message was scattered across the send and
4868 TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
4869 if ((tp1->rec.data.sid != sid) ||
4870 (!SCTP_MID_EQ(stcb->asoc.idata_supported, tp1->rec.data.mid, mid))) {
4874 * save to chk in case we have some on stream out
4875 * queue. If so and we have an un-transmitted one we
4876 * don't have to fudge the TSN.
4879 ret_sz += tp1->book_size;
4880 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4882 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
4884 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
4887 sctp_m_freem(tp1->data);
4890 /* No flight involved here book the size to 0 */
4892 if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4895 do_wakeup_routine = 1;
4896 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4897 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4899 * on to the sent queue so we can wait for it to be
4902 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4904 stcb->asoc.send_queue_cnt--;
4905 stcb->asoc.sent_queue_cnt++;
4908 if (foundeom == 0) {
4910 * Still no eom found. That means there is stuff left on the
4911 * stream out queue.. yuck.
4913 SCTP_TCB_SEND_LOCK(stcb);
4914 strq = &stcb->asoc.strmout[sid];
4915 sp = TAILQ_FIRST(&strq->outqueue);
4917 sp->discard_rest = 1;
4919 * We may need to put a chunk on the queue that
4920 * holds the TSN that would have been sent with the
4924 /* Yep, we have to */
4925 sctp_alloc_a_chunk(stcb, chk);
4928 * we are hosed. All we can do is
4929 * nothing.. which will cause an
4930 * abort if the peer is paying
4935 memset(chk, 0, sizeof(*chk));
4936 chk->rec.data.rcv_flags = 0;
4937 chk->sent = SCTP_FORWARD_TSN_SKIP;
4938 chk->asoc = &stcb->asoc;
4939 if (stcb->asoc.idata_supported == 0) {
4940 if (sp->sinfo_flags & SCTP_UNORDERED) {
4941 chk->rec.data.mid = 0;
4943 chk->rec.data.mid = strq->next_mid_ordered;
4946 if (sp->sinfo_flags & SCTP_UNORDERED) {
4947 chk->rec.data.mid = strq->next_mid_unordered;
4949 chk->rec.data.mid = strq->next_mid_ordered;
4952 chk->rec.data.sid = sp->sid;
4953 chk->rec.data.ppid = sp->ppid;
4954 chk->rec.data.context = sp->context;
4955 chk->flags = sp->act_flags;
4957 chk->rec.data.tsn = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
4958 strq->chunks_on_queues++;
4959 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
4960 stcb->asoc.sent_queue_cnt++;
4961 stcb->asoc.pr_sctp_cnt++;
4963 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4964 if (sp->sinfo_flags & SCTP_UNORDERED) {
4965 chk->rec.data.rcv_flags |= SCTP_DATA_UNORDERED;
4967 if (stcb->asoc.idata_supported == 0) {
4968 if ((sp->sinfo_flags & SCTP_UNORDERED) == 0) {
4969 strq->next_mid_ordered++;
4972 if (sp->sinfo_flags & SCTP_UNORDERED) {
4973 strq->next_mid_unordered++;
4975 strq->next_mid_ordered++;
4981 * Pull any data to free up the SB and allow
4982 * sender to "add more" while we will throw
4985 sctp_free_spbufspace(stcb, &stcb->asoc, sp);
4986 ret_sz += sp->length;
4987 do_wakeup_routine = 1;
4989 sctp_m_freem(sp->data);
4991 sp->tail_mbuf = NULL;
4995 SCTP_TCB_SEND_UNLOCK(stcb);
4997 if (do_wakeup_routine) {
4998 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5001 so = SCTP_INP_SO(stcb->sctp_ep);
5003 atomic_add_int(&stcb->asoc.refcnt, 1);
5004 SCTP_TCB_UNLOCK(stcb);
5005 SCTP_SOCKET_LOCK(so, 1);
5006 SCTP_TCB_LOCK(stcb);
5007 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5008 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
5009 /* assoc was freed while we were unlocked */
5010 SCTP_SOCKET_UNLOCK(so, 1);
5015 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
5016 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5018 SCTP_SOCKET_UNLOCK(so, 1);
5026 * checks to see if the given address, sa, is one that is currently known by
5027 * the kernel note: can't distinguish the same address on multiple interfaces
5028 * and doesn't handle multiple addresses with different zone/scope id's note:
5029 * ifa_ifwithaddr() compares the entire sockaddr struct
5032 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
5035 struct sctp_laddr *laddr;
5037 if (holds_lock == 0) {
5038 SCTP_INP_RLOCK(inp);
5041 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5042 if (laddr->ifa == NULL)
5044 if (addr->sa_family != laddr->ifa->address.sa.sa_family)
5047 if (addr->sa_family == AF_INET) {
5048 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5049 laddr->ifa->address.sin.sin_addr.s_addr) {
5051 if (holds_lock == 0) {
5052 SCTP_INP_RUNLOCK(inp);
5054 return (laddr->ifa);
5060 if (addr->sa_family == AF_INET6) {
5061 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5062 &laddr->ifa->address.sin6)) {
5064 if (holds_lock == 0) {
5065 SCTP_INP_RUNLOCK(inp);
5067 return (laddr->ifa);
5073 if (holds_lock == 0) {
5074 SCTP_INP_RUNLOCK(inp);
5080 sctp_get_ifa_hash_val(struct sockaddr *addr)
5082 switch (addr->sa_family) {
5086 struct sockaddr_in *sin;
5088 sin = (struct sockaddr_in *)addr;
5089 return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5095 struct sockaddr_in6 *sin6;
5096 uint32_t hash_of_addr;
5098 sin6 = (struct sockaddr_in6 *)addr;
5099 hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
5100 sin6->sin6_addr.s6_addr32[1] +
5101 sin6->sin6_addr.s6_addr32[2] +
5102 sin6->sin6_addr.s6_addr32[3]);
5103 hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
5104 return (hash_of_addr);
5114 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
5116 struct sctp_ifa *sctp_ifap;
5117 struct sctp_vrf *vrf;
5118 struct sctp_ifalist *hash_head;
5119 uint32_t hash_of_addr;
5121 if (holds_lock == 0)
5122 SCTP_IPI_ADDR_RLOCK();
5124 vrf = sctp_find_vrf(vrf_id);
5126 if (holds_lock == 0)
5127 SCTP_IPI_ADDR_RUNLOCK();
5131 hash_of_addr = sctp_get_ifa_hash_val(addr);
5133 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5134 if (hash_head == NULL) {
5135 SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5136 hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
5137 (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
5138 sctp_print_address(addr);
5139 SCTP_PRINTF("No such bucket for address\n");
5140 if (holds_lock == 0)
5141 SCTP_IPI_ADDR_RUNLOCK();
5145 LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5146 if (addr->sa_family != sctp_ifap->address.sa.sa_family)
5149 if (addr->sa_family == AF_INET) {
5150 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5151 sctp_ifap->address.sin.sin_addr.s_addr) {
5153 if (holds_lock == 0)
5154 SCTP_IPI_ADDR_RUNLOCK();
5161 if (addr->sa_family == AF_INET6) {
5162 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5163 &sctp_ifap->address.sin6)) {
5165 if (holds_lock == 0)
5166 SCTP_IPI_ADDR_RUNLOCK();
5173 if (holds_lock == 0)
5174 SCTP_IPI_ADDR_RUNLOCK();
5179 sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
5182 /* User pulled some data, do we need a rwnd update? */
5185 struct socket *so = NULL;
5190 atomic_add_int(&stcb->asoc.refcnt, 1);
5192 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5193 (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | SCTP_STATE_SHUTDOWN_RECEIVED))) {
5194 /* Pre-check If we are freeing no update */
5197 SCTP_INP_INCR_REF(stcb->sctp_ep);
5198 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5199 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5202 so = stcb->sctp_socket;
5206 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5207 /* Have you have freed enough to look */
5209 /* Yep, its worth a look and the lock overhead */
5211 /* Figure out what the rwnd would be */
5212 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5213 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5214 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5218 if (dif >= rwnd_req) {
5220 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5223 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5225 * One last check before we allow the guy possibly
5226 * to get in. There is a race, where the guy has not
5227 * reached the gate. In that case
5231 SCTP_TCB_LOCK(stcb);
5232 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5233 /* No reports here */
5234 SCTP_TCB_UNLOCK(stcb);
5237 SCTP_STAT_INCR(sctps_wu_sacks_sent);
5238 sctp_send_sack(stcb, SCTP_SO_LOCKED);
5240 sctp_chunk_output(stcb->sctp_ep, stcb,
5241 SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5242 /* make sure no timer is running */
5243 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
5244 SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5245 SCTP_TCB_UNLOCK(stcb);
5247 /* Update how much we have pending */
5248 stcb->freed_by_sorcv_sincelast = dif;
5251 if (so && r_unlocked && hold_rlock) {
5252 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5255 SCTP_INP_DECR_REF(stcb->sctp_ep);
5257 atomic_add_int(&stcb->asoc.refcnt, -1);
5262 sctp_sorecvmsg(struct socket *so,
5265 struct sockaddr *from,
5268 struct sctp_sndrcvinfo *sinfo,
5272 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5273 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5274 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5275 * On the way out we may send out any combination of:
5276 * MSG_NOTIFICATION MSG_EOR
5279 struct sctp_inpcb *inp = NULL;
5283 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
5284 struct mbuf *m = NULL;
5285 struct sctp_tcb *stcb = NULL;
5286 int wakeup_read_socket = 0;
5287 int freecnt_applied = 0;
5288 int out_flags = 0, in_flags = 0;
5289 int block_allowed = 1;
5290 uint32_t freed_so_far = 0;
5291 ssize_t copied_so_far = 0;
5292 int in_eeor_mode = 0;
5293 int no_rcv_needed = 0;
5294 uint32_t rwnd_req = 0;
5295 int hold_sblock = 0;
5298 uint32_t held_length = 0;
5299 int sockbuf_lock = 0;
5302 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5307 in_flags = *msg_flags;
5308 if (in_flags & MSG_PEEK)
5309 SCTP_STAT_INCR(sctps_read_peeks);
5313 slen = uio->uio_resid;
5315 /* Pull in and set up our int flags */
5316 if (in_flags & MSG_OOB) {
5317 /* Out of band's NOT supported */
5318 return (EOPNOTSUPP);
5320 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5321 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5324 if ((in_flags & (MSG_DONTWAIT
5327 SCTP_SO_IS_NBIO(so)) {
5330 /* setup the endpoint */
5331 inp = (struct sctp_inpcb *)so->so_pcb;
5333 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5336 rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5337 /* Must be at least a MTU's worth */
5338 if (rwnd_req < SCTP_MIN_RWND)
5339 rwnd_req = SCTP_MIN_RWND;
5340 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5341 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5342 sctp_misc_ints(SCTP_SORECV_ENTER,
5343 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
5345 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5346 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5347 rwnd_req, block_allowed, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
5351 error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5353 goto release_unlocked;
5360 if (hold_sblock == 0) {
5361 SOCKBUF_LOCK(&so->so_rcv);
5364 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5365 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5368 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5370 error = so->so_error;
5371 if ((in_flags & MSG_PEEK) == 0)
5375 if (so->so_rcv.sb_cc == 0) {
5382 if (so->so_rcv.sb_cc <= held_length) {
5384 error = so->so_error;
5385 if ((in_flags & MSG_PEEK) == 0) {
5390 if ((so->so_rcv.sb_cc == 0) &&
5391 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5392 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5393 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5395 * For active open side clear flags for
5396 * re-use passive open is blocked by
5399 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5401 * You were aborted, passive side
5404 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5407 so->so_state &= ~(SS_ISCONNECTING |
5408 SS_ISDISCONNECTING |
5412 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5413 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5420 if (block_allowed) {
5421 error = sbwait(&so->so_rcv);
5426 goto restart_nosblocks;
5428 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5429 error = EWOULDBLOCK;
5433 if (hold_sblock == 1) {
5434 SOCKBUF_UNLOCK(&so->so_rcv);
5437 /* we possibly have data we can read */
5438 /* sa_ignore FREED_MEMORY */
5439 control = TAILQ_FIRST(&inp->read_queue);
5440 if (control == NULL) {
5442 * This could be happening since the appender did the
5443 * increment but as not yet did the tailq insert onto the
5446 if (hold_rlock == 0) {
5447 SCTP_INP_READ_LOCK(inp);
5449 control = TAILQ_FIRST(&inp->read_queue);
5450 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5452 panic("Huh, its non zero and nothing on control?");
5454 so->so_rcv.sb_cc = 0;
5456 SCTP_INP_READ_UNLOCK(inp);
5461 if ((control->length == 0) &&
5462 (control->do_not_ref_stcb)) {
5464 * Clean up code for freeing assoc that left behind a
5465 * pdapi.. maybe a peer in EEOR that just closed after
5466 * sending and never indicated a EOR.
5468 if (hold_rlock == 0) {
5470 SCTP_INP_READ_LOCK(inp);
5472 control->held_length = 0;
5473 if (control->data) {
5474 /* Hmm there is data here .. fix */
5478 m_tmp = control->data;
5480 cnt += SCTP_BUF_LEN(m_tmp);
5481 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5482 control->tail_mbuf = m_tmp;
5483 control->end_added = 1;
5485 m_tmp = SCTP_BUF_NEXT(m_tmp);
5487 control->length = cnt;
5490 TAILQ_REMOVE(&inp->read_queue, control, next);
5491 /* Add back any hiddend data */
5492 sctp_free_remote_addr(control->whoFrom);
5493 sctp_free_a_readq(stcb, control);
5497 SCTP_INP_READ_UNLOCK(inp);
5501 if ((control->length == 0) &&
5502 (control->end_added == 1)) {
5504 * Do we also need to check for (control->pdapi_aborted ==
5507 if (hold_rlock == 0) {
5509 SCTP_INP_READ_LOCK(inp);
5511 TAILQ_REMOVE(&inp->read_queue, control, next);
5512 if (control->data) {
5514 panic("control->data not null but control->length == 0");
5516 SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5517 sctp_m_freem(control->data);
5518 control->data = NULL;
5521 if (control->aux_data) {
5522 sctp_m_free(control->aux_data);
5523 control->aux_data = NULL;
5526 if (control->on_strm_q) {
5527 panic("About to free ctl:%p so:%p and its in %d",
5528 control, so, control->on_strm_q);
5531 sctp_free_remote_addr(control->whoFrom);
5532 sctp_free_a_readq(stcb, control);
5535 SCTP_INP_READ_UNLOCK(inp);
5539 if (control->length == 0) {
5540 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5542 /* find a more suitable one then this */
5543 ctl = TAILQ_NEXT(control, next);
5545 if ((ctl->stcb != control->stcb) && (ctl->length) &&
5547 (ctl->spec_flags & M_NOTIFICATION) ||
5548 ((ctl->do_not_ref_stcb == 0) &&
5549 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
5552 * If we have a different TCB next, and there is data
5553 * present. If we have already taken some (pdapi), OR we can
5554 * ref the tcb and no delivery as started on this stream, we
5555 * take it. Note we allow a notification on a different
5556 * assoc to be delivered..
5560 } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
5562 ((ctl->some_taken) ||
5563 ((ctl->do_not_ref_stcb == 0) &&
5564 ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5565 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
5567 * If we have the same tcb, and there is data present, and we
5568 * have the strm interleave feature present. Then if we have
5569 * taken some (pdapi) or we can refer to tht tcb AND we have
5570 * not started a delivery for this stream, we can take it.
5571 * Note we do NOT allow a notificaiton on the same assoc to
5577 ctl = TAILQ_NEXT(ctl, next);
5581 * if we reach here, not suitable replacement is available
5582 * <or> fragment interleave is NOT on. So stuff the sb_cc
5583 * into the our held count, and its time to sleep again.
5585 held_length = so->so_rcv.sb_cc;
5586 control->held_length = so->so_rcv.sb_cc;
5589 /* Clear the held length since there is something to read */
5590 control->held_length = 0;
5593 * If we reach here, control has a some data for us to read off.
5594 * Note that stcb COULD be NULL.
5596 if (hold_rlock == 0) {
5598 SCTP_INP_READ_LOCK(inp);
5600 control->some_taken++;
5601 stcb = control->stcb;
5603 if ((control->do_not_ref_stcb == 0) &&
5604 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5605 if (freecnt_applied == 0)
5607 } else if (control->do_not_ref_stcb == 0) {
5608 /* you can't free it on me please */
5610 * The lock on the socket buffer protects us so the
5611 * free code will stop. But since we used the
5612 * socketbuf lock and the sender uses the tcb_lock
5613 * to increment, we need to use the atomic add to
5616 if (freecnt_applied) {
5618 panic("refcnt already incremented");
5620 SCTP_PRINTF("refcnt already incremented?\n");
5623 atomic_add_int(&stcb->asoc.refcnt, 1);
5624 freecnt_applied = 1;
5627 * Setup to remember how much we have not yet told
5628 * the peer our rwnd has opened up. Note we grab the
5629 * value from the tcb from last time. Note too that
5630 * sack sending clears this when a sack is sent,
5631 * which is fine. Once we hit the rwnd_req, we then
5632 * will go to the sctp_user_rcvd() that will not
5633 * lock until it KNOWs it MUST send a WUP-SACK.
5635 freed_so_far = (uint32_t)stcb->freed_by_sorcv_sincelast;
5636 stcb->freed_by_sorcv_sincelast = 0;
5640 ((control->spec_flags & M_NOTIFICATION) == 0) &&
5641 control->do_not_ref_stcb == 0) {
5642 stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5645 /* First lets get off the sinfo and sockaddr info */
5646 if ((sinfo != NULL) && (filling_sinfo != 0)) {
5647 sinfo->sinfo_stream = control->sinfo_stream;
5648 sinfo->sinfo_ssn = (uint16_t)control->mid;
5649 sinfo->sinfo_flags = control->sinfo_flags;
5650 sinfo->sinfo_ppid = control->sinfo_ppid;
5651 sinfo->sinfo_context = control->sinfo_context;
5652 sinfo->sinfo_timetolive = control->sinfo_timetolive;
5653 sinfo->sinfo_tsn = control->sinfo_tsn;
5654 sinfo->sinfo_cumtsn = control->sinfo_cumtsn;
5655 sinfo->sinfo_assoc_id = control->sinfo_assoc_id;
5656 nxt = TAILQ_NEXT(control, next);
5657 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
5658 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
5659 struct sctp_extrcvinfo *s_extra;
5661 s_extra = (struct sctp_extrcvinfo *)sinfo;
5664 s_extra->serinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5665 if (nxt->sinfo_flags & SCTP_UNORDERED) {
5666 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5668 if (nxt->spec_flags & M_NOTIFICATION) {
5669 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5671 s_extra->serinfo_next_aid = nxt->sinfo_assoc_id;
5672 s_extra->serinfo_next_length = nxt->length;
5673 s_extra->serinfo_next_ppid = nxt->sinfo_ppid;
5674 s_extra->serinfo_next_stream = nxt->sinfo_stream;
5675 if (nxt->tail_mbuf != NULL) {
5676 if (nxt->end_added) {
5677 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5682 * we explicitly 0 this, since the memcpy
5683 * got some other things beyond the older
5684 * sinfo_ that is on the control's structure
5688 s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
5689 s_extra->serinfo_next_aid = 0;
5690 s_extra->serinfo_next_length = 0;
5691 s_extra->serinfo_next_ppid = 0;
5692 s_extra->serinfo_next_stream = 0;
5696 * update off the real current cum-ack, if we have an stcb.
5698 if ((control->do_not_ref_stcb == 0) && stcb)
5699 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5701 * mask off the high bits, we keep the actual chunk bits in
5704 sinfo->sinfo_flags &= 0x00ff;
5705 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
5706 sinfo->sinfo_flags |= SCTP_UNORDERED;
5709 #ifdef SCTP_ASOCLOG_OF_TSNS
5711 int index, newindex;
5712 struct sctp_pcbtsn_rlog *entry;
5715 index = inp->readlog_index;
5716 newindex = index + 1;
5717 if (newindex >= SCTP_READ_LOG_SIZE) {
5720 } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
5721 entry = &inp->readlog[index];
5722 entry->vtag = control->sinfo_assoc_id;
5723 entry->strm = control->sinfo_stream;
5724 entry->seq = (uint16_t)control->mid;
5725 entry->sz = control->length;
5726 entry->flgs = control->sinfo_flags;
5729 if ((fromlen > 0) && (from != NULL)) {
5730 union sctp_sockstore store;
5733 switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5736 len = sizeof(struct sockaddr_in6);
5737 store.sin6 = control->whoFrom->ro._l_addr.sin6;
5738 store.sin6.sin6_port = control->port_from;
5744 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
5745 len = sizeof(struct sockaddr_in6);
5746 in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
5748 store.sin6.sin6_port = control->port_from;
5750 len = sizeof(struct sockaddr_in);
5751 store.sin = control->whoFrom->ro._l_addr.sin;
5752 store.sin.sin_port = control->port_from;
5755 len = sizeof(struct sockaddr_in);
5756 store.sin = control->whoFrom->ro._l_addr.sin;
5757 store.sin.sin_port = control->port_from;
5765 memcpy(from, &store, min((size_t)fromlen, len));
5768 struct sockaddr_in6 lsa6, *from6;
5770 from6 = (struct sockaddr_in6 *)from;
5771 sctp_recover_scope_mac(from6, (&lsa6));
5776 SCTP_INP_READ_UNLOCK(inp);
5780 SOCKBUF_UNLOCK(&so->so_rcv);
5783 /* now copy out what data we can */
5785 /* copy out each mbuf in the chain up to length */
5789 /* Move out all we can */
5790 cp_len = uio->uio_resid;
5791 my_len = SCTP_BUF_LEN(m);
5792 if (cp_len > my_len) {
5793 /* not enough in this buf */
5797 SCTP_INP_READ_UNLOCK(inp);
5801 error = uiomove(mtod(m, char *), (int)cp_len, uio);
5803 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5807 if ((control->do_not_ref_stcb == 0) && stcb &&
5808 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5812 /* error we are out of here */
5815 SCTP_INP_READ_LOCK(inp);
5817 if (cp_len == SCTP_BUF_LEN(m)) {
5818 if ((SCTP_BUF_NEXT(m) == NULL) &&
5819 (control->end_added)) {
5820 out_flags |= MSG_EOR;
5821 if ((control->do_not_ref_stcb == 0) &&
5822 (control->stcb != NULL) &&
5823 ((control->spec_flags & M_NOTIFICATION) == 0))
5824 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5826 if (control->spec_flags & M_NOTIFICATION) {
5827 out_flags |= MSG_NOTIFICATION;
5829 /* we ate up the mbuf */
5830 if (in_flags & MSG_PEEK) {
5832 m = SCTP_BUF_NEXT(m);
5833 copied_so_far += cp_len;
5835 /* dispose of the mbuf */
5836 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5837 sctp_sblog(&so->so_rcv,
5838 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5840 sctp_sbfree(control, stcb, &so->so_rcv, m);
5841 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5842 sctp_sblog(&so->so_rcv,
5843 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5845 copied_so_far += cp_len;
5846 freed_so_far += (uint32_t)cp_len;
5847 freed_so_far += MSIZE;
5848 atomic_subtract_int(&control->length, cp_len);
5849 control->data = sctp_m_free(m);
5852 * been through it all, must hold sb
5853 * lock ok to null tail
5855 if (control->data == NULL) {
5857 if ((control->end_added == 0) ||
5858 (TAILQ_NEXT(control, next) == NULL)) {
5866 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5867 panic("Hmm we don't own the lock?");
5871 control->tail_mbuf = NULL;
5873 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5874 panic("end_added, nothing left and no MSG_EOR");
5880 /* Do we need to trim the mbuf? */
5881 if (control->spec_flags & M_NOTIFICATION) {
5882 out_flags |= MSG_NOTIFICATION;
5884 if ((in_flags & MSG_PEEK) == 0) {
5885 SCTP_BUF_RESV_UF(m, cp_len);
5886 SCTP_BUF_LEN(m) -= (int)cp_len;
5887 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5888 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, (int)cp_len);
5890 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5891 if ((control->do_not_ref_stcb == 0) &&
5893 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5895 copied_so_far += cp_len;
5896 freed_so_far += (uint32_t)cp_len;
5897 freed_so_far += MSIZE;
5898 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5899 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5900 SCTP_LOG_SBRESULT, 0);
5902 atomic_subtract_int(&control->length, cp_len);
5904 copied_so_far += cp_len;
5907 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5910 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5911 (control->do_not_ref_stcb == 0) &&
5912 (freed_so_far >= rwnd_req)) {
5913 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5915 } /* end while(m) */
5917 * At this point we have looked at it all and we either have
5918 * a MSG_EOR/or read all the user wants... <OR>
5919 * control->length == 0.
5921 if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5922 /* we are done with this control */
5923 if (control->length == 0) {
5924 if (control->data) {
5926 panic("control->data not null at read eor?");
5928 SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5929 sctp_m_freem(control->data);
5930 control->data = NULL;
5934 if (hold_rlock == 0) {
5935 SCTP_INP_READ_LOCK(inp);
5938 TAILQ_REMOVE(&inp->read_queue, control, next);
5939 /* Add back any hiddend data */
5940 if (control->held_length) {
5942 control->held_length = 0;
5943 wakeup_read_socket = 1;
5945 if (control->aux_data) {
5946 sctp_m_free(control->aux_data);
5947 control->aux_data = NULL;
5949 no_rcv_needed = control->do_not_ref_stcb;
5950 sctp_free_remote_addr(control->whoFrom);
5951 control->data = NULL;
5953 if (control->on_strm_q) {
5954 panic("About to free ctl:%p so:%p and its in %d",
5955 control, so, control->on_strm_q);
5958 sctp_free_a_readq(stcb, control);
5960 if ((freed_so_far >= rwnd_req) &&
5961 (no_rcv_needed == 0))
5962 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5966 * The user did not read all of this
5967 * message, turn off the returned MSG_EOR
5968 * since we are leaving more behind on the
5972 if (control->end_added &&
5973 (control->data == NULL) &&
5974 (control->tail_mbuf == NULL)) {
5975 panic("Gak, control->length is corrupt?");
5978 no_rcv_needed = control->do_not_ref_stcb;
5979 out_flags &= ~MSG_EOR;
5982 if (out_flags & MSG_EOR) {
5985 if ((uio->uio_resid == 0) ||
5987 (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))) {
5991 * If I hit here the receiver wants more and this message is
5992 * NOT done (pd-api). So two questions. Can we block? if not
5993 * we are done. Did the user NOT set MSG_WAITALL?
5995 if (block_allowed == 0) {
5999 * We need to wait for more data a few things: - We don't
6000 * sbunlock() so we don't get someone else reading. - We
6001 * must be sure to account for the case where what is added
6002 * is NOT to our control when we wakeup.
6006 * Do we need to tell the transport a rwnd update might be
6007 * needed before we go to sleep?
6009 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6010 ((freed_so_far >= rwnd_req) &&
6011 (control->do_not_ref_stcb == 0) &&
6012 (no_rcv_needed == 0))) {
6013 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6016 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
6020 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
6023 if (hold_rlock == 1) {
6024 SCTP_INP_READ_UNLOCK(inp);
6027 if (hold_sblock == 0) {
6028 SOCKBUF_LOCK(&so->so_rcv);
6031 if ((copied_so_far) && (control->length == 0) &&
6032 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
6035 if (so->so_rcv.sb_cc <= control->held_length) {
6036 error = sbwait(&so->so_rcv);
6040 control->held_length = 0;
6043 SOCKBUF_UNLOCK(&so->so_rcv);
6046 if (control->length == 0) {
6047 /* still nothing here */
6048 if (control->end_added == 1) {
6049 /* he aborted, or is done i.e.did a shutdown */
6050 out_flags |= MSG_EOR;
6051 if (control->pdapi_aborted) {
6052 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6053 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6055 out_flags |= MSG_TRUNC;
6057 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6058 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6060 goto done_with_control;
6062 if (so->so_rcv.sb_cc > held_length) {
6063 control->held_length = so->so_rcv.sb_cc;
6066 goto wait_some_more;
6067 } else if (control->data == NULL) {
6069 * we must re-sync since data is probably being
6072 SCTP_INP_READ_LOCK(inp);
6073 if ((control->length > 0) && (control->data == NULL)) {
6075 * big trouble.. we have the lock and its
6079 panic("Impossible data==NULL length !=0");
6081 out_flags |= MSG_EOR;
6082 out_flags |= MSG_TRUNC;
6083 control->length = 0;
6084 SCTP_INP_READ_UNLOCK(inp);
6085 goto done_with_control;
6087 SCTP_INP_READ_UNLOCK(inp);
6088 /* We will fall around to get more data */
6093 * Give caller back the mbuf chain,
6094 * store in uio_resid the length
6096 wakeup_read_socket = 0;
6097 if ((control->end_added == 0) ||
6098 (TAILQ_NEXT(control, next) == NULL)) {
6099 /* Need to get rlock */
6100 if (hold_rlock == 0) {
6101 SCTP_INP_READ_LOCK(inp);
6105 if (control->end_added) {
6106 out_flags |= MSG_EOR;
6107 if ((control->do_not_ref_stcb == 0) &&
6108 (control->stcb != NULL) &&
6109 ((control->spec_flags & M_NOTIFICATION) == 0))
6110 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6112 if (control->spec_flags & M_NOTIFICATION) {
6113 out_flags |= MSG_NOTIFICATION;
6115 uio->uio_resid = control->length;
6116 *mp = control->data;
6119 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6120 sctp_sblog(&so->so_rcv,
6121 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
6123 sctp_sbfree(control, stcb, &so->so_rcv, m);
6124 freed_so_far += (uint32_t)SCTP_BUF_LEN(m);
6125 freed_so_far += MSIZE;
6126 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6127 sctp_sblog(&so->so_rcv,
6128 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
6130 m = SCTP_BUF_NEXT(m);
6132 control->data = control->tail_mbuf = NULL;
6133 control->length = 0;
6134 if (out_flags & MSG_EOR) {
6135 /* Done with this control */
6136 goto done_with_control;
6140 if (hold_rlock == 1) {
6141 SCTP_INP_READ_UNLOCK(inp);
6144 if (hold_sblock == 1) {
6145 SOCKBUF_UNLOCK(&so->so_rcv);
6149 sbunlock(&so->so_rcv);
6154 SOCKBUF_UNLOCK(&so->so_rcv);
6157 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6158 if ((freed_so_far >= rwnd_req) &&
6159 (control && (control->do_not_ref_stcb == 0)) &&
6160 (no_rcv_needed == 0))
6161 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6165 *msg_flags = out_flags;
6167 if (((out_flags & MSG_EOR) == 0) &&
6168 ((in_flags & MSG_PEEK) == 0) &&
6170 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6171 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
6172 struct sctp_extrcvinfo *s_extra;
6174 s_extra = (struct sctp_extrcvinfo *)sinfo;
6175 s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
6177 if (hold_rlock == 1) {
6178 SCTP_INP_READ_UNLOCK(inp);
6181 SOCKBUF_UNLOCK(&so->so_rcv);
6184 sbunlock(&so->so_rcv);
6187 if (freecnt_applied) {
6189 * The lock on the socket buffer protects us so the free
6190 * code will stop. But since we used the socketbuf lock and
6191 * the sender uses the tcb_lock to increment, we need to use
6192 * the atomic add to the refcnt.
6196 panic("stcb for refcnt has gone NULL?");
6202 /* Save the value back for next time */
6203 stcb->freed_by_sorcv_sincelast = freed_so_far;
6204 atomic_add_int(&stcb->asoc.refcnt, -1);
6206 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6208 sctp_misc_ints(SCTP_SORECV_DONE,
6210 (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6214 sctp_misc_ints(SCTP_SORECV_DONE,
6216 (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6222 if (wakeup_read_socket) {
6223 sctp_sorwakeup(inp, so);
6229 #ifdef SCTP_MBUF_LOGGING
6231 sctp_m_free(struct mbuf *m)
6233 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6234 sctp_log_mb(m, SCTP_MBUF_IFREE);
6240 sctp_m_freem(struct mbuf *mb)
6243 mb = sctp_m_free(mb);
6249 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
6252 * Given a local address. For all associations that holds the
6253 * address, request a peer-set-primary.
6255 struct sctp_ifa *ifa;
6256 struct sctp_laddr *wi;
6258 ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
6260 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
6261 return (EADDRNOTAVAIL);
6264 * Now that we have the ifa we must awaken the iterator with this
6267 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6269 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
6272 /* Now incr the count and int wi structure */
6273 SCTP_INCR_LADDR_COUNT();
6274 memset(wi, 0, sizeof(*wi));
6275 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
6277 wi->action = SCTP_SET_PRIM_ADDR;
6278 atomic_add_int(&ifa->refcount, 1);
6280 /* Now add it to the work queue */
6281 SCTP_WQ_ADDR_LOCK();
6283 * Should this really be a tailq? As it is we will process the
6286 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6287 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
6288 (struct sctp_inpcb *)NULL,
6289 (struct sctp_tcb *)NULL,
6290 (struct sctp_nets *)NULL);
6291 SCTP_WQ_ADDR_UNLOCK();
6297 sctp_soreceive(struct socket *so,
6298 struct sockaddr **psa,
6301 struct mbuf **controlp,
6305 uint8_t sockbuf[256];
6306 struct sockaddr *from;
6307 struct sctp_extrcvinfo sinfo;
6308 int filling_sinfo = 1;
6310 struct sctp_inpcb *inp;
6312 inp = (struct sctp_inpcb *)so->so_pcb;
6313 /* pickup the assoc we are reading from */
6315 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6318 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6319 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6320 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6321 (controlp == NULL)) {
6322 /* user does not want the sndrcv ctl */
6326 from = (struct sockaddr *)sockbuf;
6327 fromlen = sizeof(sockbuf);
6334 if (filling_sinfo) {
6335 memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6337 if (flagsp != NULL) {
6342 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, &flags,
6343 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6344 if (flagsp != NULL) {
6347 if (controlp != NULL) {
6348 /* copy back the sinfo in a CMSG format */
6349 if (filling_sinfo && ((flags & MSG_NOTIFICATION) == 0)) {
6350 *controlp = sctp_build_ctl_nchunk(inp,
6351 (struct sctp_sndrcvinfo *)&sinfo);
6357 /* copy back the address info */
6358 if (from && from->sa_len) {
6359 *psa = sodupsockaddr(from, M_NOWAIT);
6372 sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6373 int totaddr, int *error)
6377 struct sctp_inpcb *inp;
6378 struct sockaddr *sa;
6381 struct sockaddr_in *sin;
6384 struct sockaddr_in6 *sin6;
6388 inp = stcb->sctp_ep;
6390 for (i = 0; i < totaddr; i++) {
6391 switch (sa->sa_family) {
6394 incr = sizeof(struct sockaddr_in);
6395 sin = (struct sockaddr_in *)sa;
6396 if ((sin->sin_addr.s_addr == INADDR_ANY) ||
6397 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
6398 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
6399 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6400 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6401 SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
6405 if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
6406 SCTP_DONOT_SETSCOPE,
6407 SCTP_ADDR_IS_CONFIRMED)) {
6408 /* assoc gone no un-lock */
6409 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6410 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6411 SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
6420 incr = sizeof(struct sockaddr_in6);
6421 sin6 = (struct sockaddr_in6 *)sa;
6422 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
6423 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
6424 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6425 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6426 SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
6430 if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
6431 SCTP_DONOT_SETSCOPE,
6432 SCTP_ADDR_IS_CONFIRMED)) {
6433 /* assoc gone no un-lock */
6434 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6435 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6436 SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
6446 sa = (struct sockaddr *)((caddr_t)sa + incr);
6453 sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6454 unsigned int totaddr,
6455 unsigned int *num_v4, unsigned int *num_v6,
6458 struct sockaddr *sa;
6459 struct sctp_tcb *stcb;
6460 unsigned int incr, at, i;
6464 *num_v6 = *num_v4 = 0;
6465 /* account and validate addresses */
6469 for (i = 0; i < totaddr; i++) {
6470 if (at + sizeof(struct sockaddr) > limit) {
6473 switch (sa->sa_family) {
6476 incr = (unsigned int)sizeof(struct sockaddr_in);
6477 if (sa->sa_len != incr) {
6486 struct sockaddr_in6 *sin6;
6488 sin6 = (struct sockaddr_in6 *)sa;
6489 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6490 /* Must be non-mapped for connectx */
6493 incr = (unsigned int)sizeof(struct sockaddr_in6);
6494 if (sa->sa_len != incr) {
6504 if ((at + incr) > limit) {
6507 SCTP_INP_INCR_REF(inp);
6508 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
6510 SCTP_TCB_UNLOCK(stcb);
6513 SCTP_INP_DECR_REF(inp);
6516 sa = (struct sockaddr *)((caddr_t)sa + incr);
6522 * sctp_bindx(ADD) for one address.
6523 * assumes all arguments are valid/checked by caller.
6526 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
6527 struct sockaddr *sa, sctp_assoc_t assoc_id,
6528 uint32_t vrf_id, int *error, void *p)
6530 struct sockaddr *addr_touse;
6531 #if defined(INET) && defined(INET6)
6532 struct sockaddr_in sin;
6535 /* see if we're bound all already! */
6536 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6537 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6543 if (sa->sa_family == AF_INET6) {
6545 struct sockaddr_in6 *sin6;
6548 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6549 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6553 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6554 /* can only bind v6 on PF_INET6 sockets */
6555 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6560 sin6 = (struct sockaddr_in6 *)addr_touse;
6561 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6562 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6563 SCTP_IPV6_V6ONLY(inp)) {
6564 /* can't bind v4-mapped on PF_INET sockets */
6565 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6569 in6_sin6_2_sin(&sin, sin6);
6570 addr_touse = (struct sockaddr *)&sin;
6576 if (sa->sa_family == AF_INET) {
6577 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6578 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6582 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6583 SCTP_IPV6_V6ONLY(inp)) {
6584 /* can't bind v4 on PF_INET sockets */
6585 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6591 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
6593 /* Can't get proc for Net/Open BSD */
6594 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6598 *error = sctp_inpcb_bind(so, addr_touse, NULL, p);
6602 * No locks required here since bind and mgmt_ep_sa all do their own
6603 * locking. If we do something for the FIX: below we may need to
6604 * lock in that case.
6606 if (assoc_id == 0) {
6607 /* add the address */
6608 struct sctp_inpcb *lep;
6609 struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
6611 /* validate the incoming port */
6612 if ((lsin->sin_port != 0) &&
6613 (lsin->sin_port != inp->sctp_lport)) {
6614 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6618 /* user specified 0 port, set it to existing port */
6619 lsin->sin_port = inp->sctp_lport;
6622 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
6625 * We must decrement the refcount since we have the
6626 * ep already and are binding. No remove going on
6629 SCTP_INP_DECR_REF(lep);
6632 /* already bound to it.. ok */
6634 } else if (lep == NULL) {
6635 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
6636 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6637 SCTP_ADD_IP_ADDRESS,
6640 *error = EADDRINUSE;
6646 * FIX: decide whether we allow assoc based bindx
6652 * sctp_bindx(DELETE) for one address.
6653 * assumes all arguments are valid/checked by caller.
6656 sctp_bindx_delete_address(struct sctp_inpcb *inp,
6657 struct sockaddr *sa, sctp_assoc_t assoc_id,
6658 uint32_t vrf_id, int *error)
6660 struct sockaddr *addr_touse;
6661 #if defined(INET) && defined(INET6)
6662 struct sockaddr_in sin;
6665 /* see if we're bound all already! */
6666 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6667 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6673 if (sa->sa_family == AF_INET6) {
6675 struct sockaddr_in6 *sin6;
6678 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6679 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6683 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6684 /* can only bind v6 on PF_INET6 sockets */
6685 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6690 sin6 = (struct sockaddr_in6 *)addr_touse;
6691 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6692 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6693 SCTP_IPV6_V6ONLY(inp)) {
6694 /* can't bind mapped-v4 on PF_INET sockets */
6695 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6699 in6_sin6_2_sin(&sin, sin6);
6700 addr_touse = (struct sockaddr *)&sin;
6706 if (sa->sa_family == AF_INET) {
6707 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6708 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6712 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6713 SCTP_IPV6_V6ONLY(inp)) {
6714 /* can't bind v4 on PF_INET sockets */
6715 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6722 * No lock required mgmt_ep_sa does its own locking. If the FIX:
6723 * below is ever changed we may need to lock before calling
6724 * association level binding.
6726 if (assoc_id == 0) {
6727 /* delete the address */
6728 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6729 SCTP_DEL_IP_ADDRESS,
6733 * FIX: decide whether we allow assoc based bindx
6739 * returns the valid local address count for an assoc, taking into account
6743 sctp_local_addr_count(struct sctp_tcb *stcb)
6747 int ipv4_local_scope, ipv4_addr_legal;
6750 int local_scope, site_scope, ipv6_addr_legal;
6752 struct sctp_vrf *vrf;
6753 struct sctp_ifn *sctp_ifn;
6754 struct sctp_ifa *sctp_ifa;
6757 /* Turn on all the appropriate scopes */
6758 loopback_scope = stcb->asoc.scope.loopback_scope;
6760 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
6761 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
6764 local_scope = stcb->asoc.scope.local_scope;
6765 site_scope = stcb->asoc.scope.site_scope;
6766 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
6768 SCTP_IPI_ADDR_RLOCK();
6769 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
6771 /* no vrf, no addresses */
6772 SCTP_IPI_ADDR_RUNLOCK();
6776 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6778 * bound all case: go through all ifns on the vrf
6780 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
6781 if ((loopback_scope == 0) &&
6782 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
6785 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
6786 if (sctp_is_addr_restricted(stcb, sctp_ifa))
6788 switch (sctp_ifa->address.sa.sa_family) {
6791 if (ipv4_addr_legal) {
6792 struct sockaddr_in *sin;
6794 sin = &sctp_ifa->address.sin;
6795 if (sin->sin_addr.s_addr == 0) {
6802 if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
6803 &sin->sin_addr) != 0) {
6806 if ((ipv4_local_scope == 0) &&
6807 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
6810 /* count this one */
6819 if (ipv6_addr_legal) {
6820 struct sockaddr_in6 *sin6;
6822 sin6 = &sctp_ifa->address.sin6;
6823 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6826 if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
6827 &sin6->sin6_addr) != 0) {
6830 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
6831 if (local_scope == 0)
6833 if (sin6->sin6_scope_id == 0) {
6834 if (sa6_recoverscope(sin6) != 0)
6847 if ((site_scope == 0) &&
6848 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
6851 /* count this one */
6866 struct sctp_laddr *laddr;
6868 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
6870 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
6873 /* count this one */
6877 SCTP_IPI_ADDR_RUNLOCK();
6881 #if defined(SCTP_LOCAL_TRACE_BUF)
6884 sctp_log_trace(uint32_t subsys, const char *str SCTP_UNUSED, uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f)
6886 uint32_t saveindex, newindex;
6889 saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6890 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6893 newindex = saveindex + 1;
6895 } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6896 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6899 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6900 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6901 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6902 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6903 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6904 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6905 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6906 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6911 sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
6912 const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
6916 struct ip6_hdr *ip6;
6918 struct mbuf *sp, *last;
6919 struct udphdr *uhdr;
6922 if ((m->m_flags & M_PKTHDR) == 0) {
6923 /* Can't handle one that is not a pkt hdr */
6926 /* Pull the src port */
6927 iph = mtod(m, struct ip *);
6928 uhdr = (struct udphdr *)((caddr_t)iph + off);
6929 port = uhdr->uh_sport;
6931 * Split out the mbuf chain. Leave the IP header in m, place the
6934 sp = m_split(m, off, M_NOWAIT);
6936 /* Gak, drop packet, we can't do a split */
6939 if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
6940 /* Gak, packet can't have an SCTP header in it - too small */
6944 /* Now pull up the UDP header and SCTP header together */
6945 sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
6947 /* Gak pullup failed */
6950 /* Trim out the UDP header */
6951 m_adj(sp, sizeof(struct udphdr));
6953 /* Now reconstruct the mbuf chain */
6954 for (last = m; last->m_next; last = last->m_next);
6956 m->m_pkthdr.len += sp->m_pkthdr.len;
6958 * The CSUM_DATA_VALID flags indicates that the HW checked the UDP
6959 * checksum and it was valid. Since CSUM_DATA_VALID ==
6960 * CSUM_SCTP_VALID this would imply that the HW also verified the
6961 * SCTP checksum. Therefore, clear the bit.
6963 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6964 "sctp_recv_udp_tunneled_packet(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6966 if_name(m->m_pkthdr.rcvif),
6967 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6968 m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
6969 iph = mtod(m, struct ip *);
6970 switch (iph->ip_v) {
6973 iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
6974 sctp_input_with_port(m, off, port);
6978 case IPV6_VERSION >> 4:
6979 ip6 = mtod(m, struct ip6_hdr *);
6980 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
6981 sctp6_input_with_port(&m, &off, port);
6995 sctp_recv_icmp_tunneled_packet(int cmd, struct sockaddr *sa, void *vip, void *ctx SCTP_UNUSED)
6997 struct ip *outer_ip, *inner_ip;
7001 struct sctp_inpcb *inp;
7002 struct sctp_tcb *stcb;
7003 struct sctp_nets *net;
7004 struct sctp_init_chunk *ch;
7005 struct sockaddr_in src, dst;
7008 inner_ip = (struct ip *)vip;
7009 icmp = (struct icmp *)((caddr_t)inner_ip -
7010 (sizeof(struct icmp) - sizeof(struct ip)));
7011 outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
7012 if (ntohs(outer_ip->ip_len) <
7013 sizeof(struct ip) + 8 + (inner_ip->ip_hl << 2) + sizeof(struct udphdr) + 8) {
7016 udp = (struct udphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
7017 sh = (struct sctphdr *)(udp + 1);
7018 memset(&src, 0, sizeof(struct sockaddr_in));
7019 src.sin_family = AF_INET;
7020 src.sin_len = sizeof(struct sockaddr_in);
7021 src.sin_port = sh->src_port;
7022 src.sin_addr = inner_ip->ip_src;
7023 memset(&dst, 0, sizeof(struct sockaddr_in));
7024 dst.sin_family = AF_INET;
7025 dst.sin_len = sizeof(struct sockaddr_in);
7026 dst.sin_port = sh->dest_port;
7027 dst.sin_addr = inner_ip->ip_dst;
7029 * 'dst' holds the dest of the packet that failed to be sent. 'src'
7030 * holds our local endpoint address. Thus we reverse the dst and the
7031 * src in the lookup.
7035 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7036 (struct sockaddr *)&src,
7038 SCTP_DEFAULT_VRFID);
7039 if ((stcb != NULL) &&
7042 /* Check the UDP port numbers */
7043 if ((udp->uh_dport != net->port) ||
7044 (udp->uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7045 SCTP_TCB_UNLOCK(stcb);
7048 /* Check the verification tag */
7049 if (ntohl(sh->v_tag) != 0) {
7051 * This must be the verification tag used for
7052 * sending out packets. We don't consider packets
7053 * reflecting the verification tag.
7055 if (ntohl(sh->v_tag) != stcb->asoc.peer_vtag) {
7056 SCTP_TCB_UNLOCK(stcb);
7060 if (ntohs(outer_ip->ip_len) >=
7062 8 + (inner_ip->ip_hl << 2) + 8 + 20) {
7064 * In this case we can check if we got an
7065 * INIT chunk and if the initiate tag
7068 ch = (struct sctp_init_chunk *)(sh + 1);
7069 if ((ch->ch.chunk_type != SCTP_INITIATION) ||
7070 (ntohl(ch->init.initiate_tag) != stcb->asoc.my_vtag)) {
7071 SCTP_TCB_UNLOCK(stcb);
7075 SCTP_TCB_UNLOCK(stcb);
7079 type = icmp->icmp_type;
7080 code = icmp->icmp_code;
7081 if ((type == ICMP_UNREACH) &&
7082 (code == ICMP_UNREACH_PORT)) {
7083 code = ICMP_UNREACH_PROTOCOL;
7085 sctp_notify(inp, stcb, net, type, code,
7086 ntohs(inner_ip->ip_len),
7087 (uint32_t)ntohs(icmp->icmp_nextmtu));
7089 if ((stcb == NULL) && (inp != NULL)) {
7090 /* reduce ref-count */
7091 SCTP_INP_WLOCK(inp);
7092 SCTP_INP_DECR_REF(inp);
7093 SCTP_INP_WUNLOCK(inp);
7096 SCTP_TCB_UNLOCK(stcb);
7105 sctp_recv_icmp6_tunneled_packet(int cmd, struct sockaddr *sa, void *d, void *ctx SCTP_UNUSED)
7107 struct ip6ctlparam *ip6cp;
7108 struct sctp_inpcb *inp;
7109 struct sctp_tcb *stcb;
7110 struct sctp_nets *net;
7113 struct sockaddr_in6 src, dst;
7116 ip6cp = (struct ip6ctlparam *)d;
7118 * XXX: We assume that when IPV6 is non NULL, M and OFF are valid.
7120 if (ip6cp->ip6c_m == NULL) {
7124 * Check if we can safely examine the ports and the verification tag
7125 * of the SCTP common header.
7127 if (ip6cp->ip6c_m->m_pkthdr.len <
7128 ip6cp->ip6c_off + sizeof(struct udphdr) + offsetof(struct sctphdr, checksum)) {
7131 /* Copy out the UDP header. */
7132 memset(&udp, 0, sizeof(struct udphdr));
7133 m_copydata(ip6cp->ip6c_m,
7135 sizeof(struct udphdr),
7137 /* Copy out the port numbers and the verification tag. */
7138 memset(&sh, 0, sizeof(struct sctphdr));
7139 m_copydata(ip6cp->ip6c_m,
7140 ip6cp->ip6c_off + sizeof(struct udphdr),
7141 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
7143 memset(&src, 0, sizeof(struct sockaddr_in6));
7144 src.sin6_family = AF_INET6;
7145 src.sin6_len = sizeof(struct sockaddr_in6);
7146 src.sin6_port = sh.src_port;
7147 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
7148 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7151 memset(&dst, 0, sizeof(struct sockaddr_in6));
7152 dst.sin6_family = AF_INET6;
7153 dst.sin6_len = sizeof(struct sockaddr_in6);
7154 dst.sin6_port = sh.dest_port;
7155 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
7156 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7161 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7162 (struct sockaddr *)&src,
7163 &inp, &net, 1, SCTP_DEFAULT_VRFID);
7164 if ((stcb != NULL) &&
7167 /* Check the UDP port numbers */
7168 if ((udp.uh_dport != net->port) ||
7169 (udp.uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7170 SCTP_TCB_UNLOCK(stcb);
7173 /* Check the verification tag */
7174 if (ntohl(sh.v_tag) != 0) {
7176 * This must be the verification tag used for
7177 * sending out packets. We don't consider packets
7178 * reflecting the verification tag.
7180 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
7181 SCTP_TCB_UNLOCK(stcb);
7185 if (ip6cp->ip6c_m->m_pkthdr.len >=
7186 ip6cp->ip6c_off + sizeof(struct udphdr) +
7187 sizeof(struct sctphdr) +
7188 sizeof(struct sctp_chunkhdr) +
7189 offsetof(struct sctp_init, a_rwnd)) {
7191 * In this case we can check if we got an
7192 * INIT chunk and if the initiate tag
7195 uint32_t initiate_tag;
7198 m_copydata(ip6cp->ip6c_m,
7200 sizeof(struct udphdr) +
7201 sizeof(struct sctphdr),
7203 (caddr_t)&chunk_type);
7204 m_copydata(ip6cp->ip6c_m,
7206 sizeof(struct udphdr) +
7207 sizeof(struct sctphdr) +
7208 sizeof(struct sctp_chunkhdr),
7210 (caddr_t)&initiate_tag);
7211 if ((chunk_type != SCTP_INITIATION) ||
7212 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
7213 SCTP_TCB_UNLOCK(stcb);
7217 SCTP_TCB_UNLOCK(stcb);
7221 type = ip6cp->ip6c_icmp6->icmp6_type;
7222 code = ip6cp->ip6c_icmp6->icmp6_code;
7223 if ((type == ICMP6_DST_UNREACH) &&
7224 (code == ICMP6_DST_UNREACH_NOPORT)) {
7225 type = ICMP6_PARAM_PROB;
7226 code = ICMP6_PARAMPROB_NEXTHEADER;
7228 sctp6_notify(inp, stcb, net, type, code,
7229 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
7231 if ((stcb == NULL) && (inp != NULL)) {
7232 /* reduce inp's ref-count */
7233 SCTP_INP_WLOCK(inp);
7234 SCTP_INP_DECR_REF(inp);
7235 SCTP_INP_WUNLOCK(inp);
7238 SCTP_TCB_UNLOCK(stcb);
7245 sctp_over_udp_stop(void)
7248 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7252 if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7253 soclose(SCTP_BASE_INFO(udp4_tun_socket));
7254 SCTP_BASE_INFO(udp4_tun_socket) = NULL;
7258 if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
7259 soclose(SCTP_BASE_INFO(udp6_tun_socket));
7260 SCTP_BASE_INFO(udp6_tun_socket) = NULL;
7266 sctp_over_udp_start(void)
7271 struct sockaddr_in sin;
7274 struct sockaddr_in6 sin6;
7277 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7280 port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
7281 if (ntohs(port) == 0) {
7282 /* Must have a port set */
7286 if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7287 /* Already running -- must stop first */
7292 if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
7293 /* Already running -- must stop first */
7298 if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
7299 SOCK_DGRAM, IPPROTO_UDP,
7300 curthread->td_ucred, curthread))) {
7301 sctp_over_udp_stop();
7304 /* Call the special UDP hook. */
7305 if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
7306 sctp_recv_udp_tunneled_packet,
7307 sctp_recv_icmp_tunneled_packet,
7309 sctp_over_udp_stop();
7312 /* Ok, we have a socket, bind it to the port. */
7313 memset(&sin, 0, sizeof(struct sockaddr_in));
7314 sin.sin_len = sizeof(struct sockaddr_in);
7315 sin.sin_family = AF_INET;
7316 sin.sin_port = htons(port);
7317 if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
7318 (struct sockaddr *)&sin, curthread))) {
7319 sctp_over_udp_stop();
7324 if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
7325 SOCK_DGRAM, IPPROTO_UDP,
7326 curthread->td_ucred, curthread))) {
7327 sctp_over_udp_stop();
7330 /* Call the special UDP hook. */
7331 if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
7332 sctp_recv_udp_tunneled_packet,
7333 sctp_recv_icmp6_tunneled_packet,
7335 sctp_over_udp_stop();
7338 /* Ok, we have a socket, bind it to the port. */
7339 memset(&sin6, 0, sizeof(struct sockaddr_in6));
7340 sin6.sin6_len = sizeof(struct sockaddr_in6);
7341 sin6.sin6_family = AF_INET6;
7342 sin6.sin6_port = htons(port);
7343 if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
7344 (struct sockaddr *)&sin6, curthread))) {
7345 sctp_over_udp_stop();
7353 * sctp_min_mtu ()returns the minimum of all non-zero arguments.
7354 * If all arguments are zero, zero is returned.
7357 sctp_min_mtu(uint32_t mtu1, uint32_t mtu2, uint32_t mtu3)
7362 return (min(mtu1, min(mtu2, mtu3)));
7364 return (min(mtu1, mtu2));
7368 return (min(mtu1, mtu3));
7376 return (min(mtu2, mtu3));
7387 sctp_hc_set_mtu(union sctp_sockstore *addr, uint16_t fibnum, uint32_t mtu)
7389 struct in_conninfo inc;
7391 memset(&inc, 0, sizeof(struct in_conninfo));
7392 inc.inc_fibnum = fibnum;
7393 switch (addr->sa.sa_family) {
7396 inc.inc_faddr = addr->sin.sin_addr;
7401 inc.inc_flags |= INC_ISIPV6;
7402 inc.inc6_faddr = addr->sin6.sin6_addr;
7408 tcp_hc_updatemtu(&inc, (u_long)mtu);
7412 sctp_hc_get_mtu(union sctp_sockstore *addr, uint16_t fibnum)
7414 struct in_conninfo inc;
7416 memset(&inc, 0, sizeof(struct in_conninfo));
7417 inc.inc_fibnum = fibnum;
7418 switch (addr->sa.sa_family) {
7421 inc.inc_faddr = addr->sin.sin_addr;
7426 inc.inc_flags |= INC_ISIPV6;
7427 inc.inc6_faddr = addr->sin6.sin6_addr;
7433 return ((uint32_t)tcp_hc_getmtu(&inc));
7437 sctp_set_state(struct sctp_tcb *stcb, int new_state)
7439 #if defined(KDTRACE_HOOKS)
7440 int old_state = stcb->asoc.state;
7443 KASSERT((new_state & ~SCTP_STATE_MASK) == 0,
7444 ("sctp_set_state: Can't set substate (new_state = %x)",
7446 stcb->asoc.state = (stcb->asoc.state & ~SCTP_STATE_MASK) | new_state;
7447 if ((new_state == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7448 (new_state == SCTP_STATE_SHUTDOWN_SENT) ||
7449 (new_state == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
7450 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
7452 #if defined(KDTRACE_HOOKS)
7453 if (((old_state & SCTP_STATE_MASK) != new_state) &&
7454 !(((old_state & SCTP_STATE_MASK) == SCTP_STATE_EMPTY) &&
7455 (new_state == SCTP_STATE_INUSE))) {
7456 SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
7462 sctp_add_substate(struct sctp_tcb *stcb, int substate)
7464 #if defined(KDTRACE_HOOKS)
7465 int old_state = stcb->asoc.state;
7468 KASSERT((substate & SCTP_STATE_MASK) == 0,
7469 ("sctp_add_substate: Can't set state (substate = %x)",
7471 stcb->asoc.state |= substate;
7472 #if defined(KDTRACE_HOOKS)
7473 if (((substate & SCTP_STATE_ABOUT_TO_BE_FREED) &&
7474 ((old_state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) ||
7475 ((substate & SCTP_STATE_SHUTDOWN_PENDING) &&
7476 ((old_state & SCTP_STATE_SHUTDOWN_PENDING) == 0))) {
7477 SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);