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 #if defined(INET6) || defined(INET)
55 #include <netinet/tcp_var.h>
57 #include <netinet/udp.h>
58 #include <netinet/udp_var.h>
59 #include <netinet/in_kdtrace.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_association *asoc;
784 struct sctp_nets *net;
788 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
789 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
790 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
791 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
792 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
793 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
794 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
795 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
800 * A list of sizes based on typical mtu's, used only if next hop size not
801 * returned. These values MUST be multiples of 4 and MUST be ordered.
803 static uint32_t sctp_mtu_sizes[] = {
825 * Return the largest MTU in sctp_mtu_sizes smaller than val.
826 * If val is smaller than the minimum, just return the largest
827 * multiple of 4 smaller or equal to val.
828 * Ensure that the result is a multiple of 4.
831 sctp_get_prev_mtu(uint32_t val)
836 if (val <= sctp_mtu_sizes[0]) {
839 for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
840 if (val <= sctp_mtu_sizes[i]) {
844 KASSERT((sctp_mtu_sizes[i - 1] & 0x00000003) == 0,
845 ("sctp_mtu_sizes[%u] not a multiple of 4", i - 1));
846 return (sctp_mtu_sizes[i - 1]);
850 * Return the smallest MTU in sctp_mtu_sizes larger than val.
851 * If val is larger than the maximum, just return the largest multiple of 4 smaller
853 * Ensure that the result is a multiple of 4.
856 sctp_get_next_mtu(uint32_t val)
858 /* select another MTU that is just bigger than this one */
862 for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
863 if (val < sctp_mtu_sizes[i]) {
864 KASSERT((sctp_mtu_sizes[i] & 0x00000003) == 0,
865 ("sctp_mtu_sizes[%u] not a multiple of 4", i));
866 return (sctp_mtu_sizes[i]);
873 sctp_fill_random_store(struct sctp_pcb *m)
876 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
877 * our counter. The result becomes our good random numbers and we
878 * then setup to give these out. Note that we do no locking to
879 * protect this. This is ok, since if competing folks call this we
880 * will get more gobbled gook in the random store which is what we
881 * want. There is a danger that two guys will use the same random
882 * numbers, but thats ok too since that is random as well :->
885 (void)sctp_hmac(SCTP_HMAC, (uint8_t *)m->random_numbers,
886 sizeof(m->random_numbers), (uint8_t *)&m->random_counter,
887 sizeof(m->random_counter), (uint8_t *)m->random_store);
892 sctp_select_initial_TSN(struct sctp_pcb *inp)
895 * A true implementation should use random selection process to get
896 * the initial stream sequence number, using RFC1750 as a good
901 int store_at, new_store;
903 if (inp->initial_sequence_debug != 0) {
906 ret = inp->initial_sequence_debug;
907 inp->initial_sequence_debug++;
911 store_at = inp->store_at;
912 new_store = store_at + sizeof(uint32_t);
913 if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
916 if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
919 if (new_store == 0) {
920 /* Refill the random store */
921 sctp_fill_random_store(inp);
923 p = &inp->random_store[store_at];
930 sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
936 (void)SCTP_GETTIME_TIMEVAL(&now);
939 x = sctp_select_initial_TSN(&inp->sctp_ep);
944 if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
952 sctp_map_assoc_state(int kernel_state)
956 if (kernel_state & SCTP_STATE_WAS_ABORTED) {
957 user_state = SCTP_CLOSED;
958 } else if (kernel_state & SCTP_STATE_SHUTDOWN_PENDING) {
959 user_state = SCTP_SHUTDOWN_PENDING;
961 switch (kernel_state & SCTP_STATE_MASK) {
962 case SCTP_STATE_EMPTY:
963 user_state = SCTP_CLOSED;
965 case SCTP_STATE_INUSE:
966 user_state = SCTP_CLOSED;
968 case SCTP_STATE_COOKIE_WAIT:
969 user_state = SCTP_COOKIE_WAIT;
971 case SCTP_STATE_COOKIE_ECHOED:
972 user_state = SCTP_COOKIE_ECHOED;
974 case SCTP_STATE_OPEN:
975 user_state = SCTP_ESTABLISHED;
977 case SCTP_STATE_SHUTDOWN_SENT:
978 user_state = SCTP_SHUTDOWN_SENT;
980 case SCTP_STATE_SHUTDOWN_RECEIVED:
981 user_state = SCTP_SHUTDOWN_RECEIVED;
983 case SCTP_STATE_SHUTDOWN_ACK_SENT:
984 user_state = SCTP_SHUTDOWN_ACK_SENT;
987 user_state = SCTP_CLOSED;
995 sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
996 uint32_t override_tag, uint32_t vrf_id, uint16_t o_strms)
998 struct sctp_association *asoc;
1001 * Anything set to zero is taken care of by the allocation routine's
1006 * Up front select what scoping to apply on addresses I tell my peer
1007 * Not sure what to do with these right now, we will need to come up
1008 * with a way to set them. We may need to pass them through from the
1009 * caller in the sctp_aloc_assoc() function.
1012 #if defined(SCTP_DETAILED_STR_STATS)
1017 /* init all variables to a known value. */
1018 SCTP_SET_STATE(stcb, SCTP_STATE_INUSE);
1019 asoc->max_burst = inp->sctp_ep.max_burst;
1020 asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
1021 asoc->heart_beat_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1022 asoc->cookie_life = inp->sctp_ep.def_cookie_life;
1023 asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
1024 asoc->ecn_supported = inp->ecn_supported;
1025 asoc->prsctp_supported = inp->prsctp_supported;
1026 asoc->idata_supported = inp->idata_supported;
1027 asoc->auth_supported = inp->auth_supported;
1028 asoc->asconf_supported = inp->asconf_supported;
1029 asoc->reconfig_supported = inp->reconfig_supported;
1030 asoc->nrsack_supported = inp->nrsack_supported;
1031 asoc->pktdrop_supported = inp->pktdrop_supported;
1032 asoc->idata_supported = inp->idata_supported;
1033 asoc->sctp_cmt_pf = (uint8_t)0;
1034 asoc->sctp_frag_point = inp->sctp_frag_point;
1035 asoc->sctp_features = inp->sctp_features;
1036 asoc->default_dscp = inp->sctp_ep.default_dscp;
1037 asoc->max_cwnd = inp->max_cwnd;
1039 if (inp->sctp_ep.default_flowlabel) {
1040 asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
1042 if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
1043 asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
1044 asoc->default_flowlabel &= 0x000fffff;
1045 asoc->default_flowlabel |= 0x80000000;
1047 asoc->default_flowlabel = 0;
1051 asoc->sb_send_resv = 0;
1053 asoc->my_vtag = override_tag;
1055 asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
1057 /* Get the nonce tags */
1058 asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1059 asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1060 asoc->vrf_id = vrf_id;
1062 #ifdef SCTP_ASOCLOG_OF_TSNS
1063 asoc->tsn_in_at = 0;
1064 asoc->tsn_out_at = 0;
1065 asoc->tsn_in_wrapped = 0;
1066 asoc->tsn_out_wrapped = 0;
1067 asoc->cumack_log_at = 0;
1068 asoc->cumack_log_atsnt = 0;
1070 #ifdef SCTP_FS_SPEC_LOG
1074 asoc->assoc_up_sent = 0;
1075 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
1076 sctp_select_initial_TSN(&inp->sctp_ep);
1077 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1078 /* we are optimisitic here */
1079 asoc->peer_supports_nat = 0;
1080 asoc->sent_queue_retran_cnt = 0;
1083 asoc->last_net_cmt_send_started = NULL;
1085 /* This will need to be adjusted */
1086 asoc->last_acked_seq = asoc->init_seq_number - 1;
1087 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1088 asoc->asconf_seq_in = asoc->last_acked_seq;
1090 /* here we are different, we hold the next one we expect */
1091 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
1093 asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
1094 asoc->initial_rto = inp->sctp_ep.initial_rto;
1096 asoc->default_mtu = inp->sctp_ep.default_mtu;
1097 asoc->max_init_times = inp->sctp_ep.max_init_times;
1098 asoc->max_send_times = inp->sctp_ep.max_send_times;
1099 asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1100 asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1101 asoc->free_chunk_cnt = 0;
1103 asoc->iam_blocking = 0;
1104 asoc->context = inp->sctp_context;
1105 asoc->local_strreset_support = inp->local_strreset_support;
1106 asoc->def_send = inp->def_send;
1107 asoc->delayed_ack = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1108 asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1109 asoc->pr_sctp_cnt = 0;
1110 asoc->total_output_queue_size = 0;
1112 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1113 asoc->scope.ipv6_addr_legal = 1;
1114 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1115 asoc->scope.ipv4_addr_legal = 1;
1117 asoc->scope.ipv4_addr_legal = 0;
1120 asoc->scope.ipv6_addr_legal = 0;
1121 asoc->scope.ipv4_addr_legal = 1;
1124 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1125 asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1127 asoc->smallest_mtu = inp->sctp_frag_point;
1128 asoc->minrto = inp->sctp_ep.sctp_minrto;
1129 asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1131 asoc->stream_locked_on = 0;
1132 asoc->ecn_echo_cnt_onq = 0;
1133 asoc->stream_locked = 0;
1135 asoc->send_sack = 1;
1137 LIST_INIT(&asoc->sctp_restricted_addrs);
1139 TAILQ_INIT(&asoc->nets);
1140 TAILQ_INIT(&asoc->pending_reply_queue);
1141 TAILQ_INIT(&asoc->asconf_ack_sent);
1142 /* Setup to fill the hb random cache at first HB */
1143 asoc->hb_random_idx = 4;
1145 asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1147 stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1148 stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1150 stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1151 stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1154 * Now the stream parameters, here we allocate space for all streams
1155 * that we request by default.
1157 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1159 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1160 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1162 if (asoc->strmout == NULL) {
1163 /* big trouble no memory */
1164 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1167 for (i = 0; i < asoc->streamoutcnt; i++) {
1169 * inbound side must be set to 0xffff, also NOTE when we get
1170 * the INIT-ACK back (for INIT sender) we MUST reduce the
1171 * count (streamoutcnt) but first check if we sent to any of
1172 * the upper streams that were dropped (if some were). Those
1173 * that were dropped must be notified to the upper layer as
1176 asoc->strmout[i].next_mid_ordered = 0;
1177 asoc->strmout[i].next_mid_unordered = 0;
1178 TAILQ_INIT(&asoc->strmout[i].outqueue);
1179 asoc->strmout[i].chunks_on_queues = 0;
1180 #if defined(SCTP_DETAILED_STR_STATS)
1181 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1182 asoc->strmout[i].abandoned_sent[j] = 0;
1183 asoc->strmout[i].abandoned_unsent[j] = 0;
1186 asoc->strmout[i].abandoned_sent[0] = 0;
1187 asoc->strmout[i].abandoned_unsent[0] = 0;
1189 asoc->strmout[i].sid = i;
1190 asoc->strmout[i].last_msg_incomplete = 0;
1191 asoc->strmout[i].state = SCTP_STREAM_OPENING;
1192 asoc->ss_functions.sctp_ss_init_stream(stcb, &asoc->strmout[i], NULL);
1194 asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1196 /* Now the mapping array */
1197 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1198 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1200 if (asoc->mapping_array == NULL) {
1201 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1202 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1205 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1206 SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1208 if (asoc->nr_mapping_array == NULL) {
1209 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1210 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1211 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1214 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1216 /* Now the init of the other outqueues */
1217 TAILQ_INIT(&asoc->free_chunks);
1218 TAILQ_INIT(&asoc->control_send_queue);
1219 TAILQ_INIT(&asoc->asconf_send_queue);
1220 TAILQ_INIT(&asoc->send_queue);
1221 TAILQ_INIT(&asoc->sent_queue);
1222 TAILQ_INIT(&asoc->resetHead);
1223 asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1224 TAILQ_INIT(&asoc->asconf_queue);
1225 /* authentication fields */
1226 asoc->authinfo.random = NULL;
1227 asoc->authinfo.active_keyid = 0;
1228 asoc->authinfo.assoc_key = NULL;
1229 asoc->authinfo.assoc_keyid = 0;
1230 asoc->authinfo.recv_key = NULL;
1231 asoc->authinfo.recv_keyid = 0;
1232 LIST_INIT(&asoc->shared_keys);
1233 asoc->marked_retrans = 0;
1234 asoc->port = inp->sctp_ep.port;
1238 asoc->timoshutdown = 0;
1239 asoc->timoheartbeat = 0;
1240 asoc->timocookie = 0;
1241 asoc->timoshutdownack = 0;
1242 (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1243 asoc->discontinuity_time = asoc->start_time;
1244 for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1245 asoc->abandoned_unsent[i] = 0;
1246 asoc->abandoned_sent[i] = 0;
1249 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
1250 * freed later when the association is freed.
1256 sctp_print_mapping_array(struct sctp_association *asoc)
1258 unsigned int i, limit;
1260 SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
1261 asoc->mapping_array_size,
1262 asoc->mapping_array_base_tsn,
1263 asoc->cumulative_tsn,
1264 asoc->highest_tsn_inside_map,
1265 asoc->highest_tsn_inside_nr_map);
1266 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1267 if (asoc->mapping_array[limit - 1] != 0) {
1271 SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1272 for (i = 0; i < limit; i++) {
1273 SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1277 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1278 if (asoc->nr_mapping_array[limit - 1]) {
1282 SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1283 for (i = 0; i < limit; i++) {
1284 SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1291 sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1293 /* mapping array needs to grow */
1294 uint8_t *new_array1, *new_array2;
1297 new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1298 SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1299 SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1300 if ((new_array1 == NULL) || (new_array2 == NULL)) {
1301 /* can't get more, forget it */
1302 SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1304 SCTP_FREE(new_array1, SCTP_M_MAP);
1307 SCTP_FREE(new_array2, SCTP_M_MAP);
1311 memset(new_array1, 0, new_size);
1312 memset(new_array2, 0, new_size);
1313 memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1314 memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1315 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1316 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1317 asoc->mapping_array = new_array1;
1318 asoc->nr_mapping_array = new_array2;
1319 asoc->mapping_array_size = new_size;
1325 sctp_iterator_work(struct sctp_iterator *it)
1327 int iteration_count = 0;
1330 struct sctp_inpcb *tinp;
1332 SCTP_INP_INFO_RLOCK();
1333 SCTP_ITERATOR_LOCK();
1334 sctp_it_ctl.cur_it = it;
1336 SCTP_INP_RLOCK(it->inp);
1337 SCTP_INP_DECR_REF(it->inp);
1339 if (it->inp == NULL) {
1340 /* iterator is complete */
1342 sctp_it_ctl.cur_it = NULL;
1343 SCTP_ITERATOR_UNLOCK();
1344 SCTP_INP_INFO_RUNLOCK();
1345 if (it->function_atend != NULL) {
1346 (*it->function_atend) (it->pointer, it->val);
1348 SCTP_FREE(it, SCTP_M_ITER);
1355 SCTP_INP_RLOCK(it->inp);
1357 while (((it->pcb_flags) &&
1358 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1359 ((it->pcb_features) &&
1360 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1361 /* endpoint flags or features don't match, so keep looking */
1362 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1363 SCTP_INP_RUNLOCK(it->inp);
1364 goto done_with_iterator;
1367 it->inp = LIST_NEXT(it->inp, sctp_list);
1368 SCTP_INP_RUNLOCK(tinp);
1369 if (it->inp == NULL) {
1370 goto done_with_iterator;
1372 SCTP_INP_RLOCK(it->inp);
1374 /* now go through each assoc which is in the desired state */
1375 if (it->done_current_ep == 0) {
1376 if (it->function_inp != NULL)
1377 inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1378 it->done_current_ep = 1;
1380 if (it->stcb == NULL) {
1381 /* run the per instance function */
1382 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1384 if ((inp_skip) || it->stcb == NULL) {
1385 if (it->function_inp_end != NULL) {
1386 inp_skip = (*it->function_inp_end) (it->inp,
1390 SCTP_INP_RUNLOCK(it->inp);
1394 SCTP_TCB_LOCK(it->stcb);
1395 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1396 /* not in the right state... keep looking */
1397 SCTP_TCB_UNLOCK(it->stcb);
1400 /* see if we have limited out the iterator loop */
1402 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1403 /* Pause to let others grab the lock */
1404 atomic_add_int(&it->stcb->asoc.refcnt, 1);
1405 SCTP_TCB_UNLOCK(it->stcb);
1406 SCTP_INP_INCR_REF(it->inp);
1407 SCTP_INP_RUNLOCK(it->inp);
1408 SCTP_ITERATOR_UNLOCK();
1409 SCTP_INP_INFO_RUNLOCK();
1410 SCTP_INP_INFO_RLOCK();
1411 SCTP_ITERATOR_LOCK();
1412 if (sctp_it_ctl.iterator_flags) {
1413 /* We won't be staying here */
1414 SCTP_INP_DECR_REF(it->inp);
1415 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1416 if (sctp_it_ctl.iterator_flags &
1417 SCTP_ITERATOR_STOP_CUR_IT) {
1418 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1419 goto done_with_iterator;
1421 if (sctp_it_ctl.iterator_flags &
1422 SCTP_ITERATOR_STOP_CUR_INP) {
1423 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1426 /* If we reach here huh? */
1427 SCTP_PRINTF("Unknown it ctl flag %x\n",
1428 sctp_it_ctl.iterator_flags);
1429 sctp_it_ctl.iterator_flags = 0;
1431 SCTP_INP_RLOCK(it->inp);
1432 SCTP_INP_DECR_REF(it->inp);
1433 SCTP_TCB_LOCK(it->stcb);
1434 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1435 iteration_count = 0;
1438 /* run function on this one */
1439 (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1442 * we lie here, it really needs to have its own type but
1443 * first I must verify that this won't effect things :-0
1445 if (it->no_chunk_output == 0)
1446 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1448 SCTP_TCB_UNLOCK(it->stcb);
1450 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1451 if (it->stcb == NULL) {
1452 /* Run last function */
1453 if (it->function_inp_end != NULL) {
1454 inp_skip = (*it->function_inp_end) (it->inp,
1460 SCTP_INP_RUNLOCK(it->inp);
1462 /* done with all assocs on this endpoint, move on to next endpoint */
1463 it->done_current_ep = 0;
1464 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1467 it->inp = LIST_NEXT(it->inp, sctp_list);
1469 if (it->inp == NULL) {
1470 goto done_with_iterator;
1472 goto select_a_new_ep;
1476 sctp_iterator_worker(void)
1478 struct sctp_iterator *it, *nit;
1480 /* This function is called with the WQ lock in place */
1482 sctp_it_ctl.iterator_running = 1;
1483 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
1484 /* now lets work on this one */
1485 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
1486 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1487 CURVNET_SET(it->vn);
1488 sctp_iterator_work(it);
1490 SCTP_IPI_ITERATOR_WQ_LOCK();
1491 /* sa_ignore FREED_MEMORY */
1493 sctp_it_ctl.iterator_running = 0;
1499 sctp_handle_addr_wq(void)
1501 /* deal with the ADDR wq from the rtsock calls */
1502 struct sctp_laddr *wi, *nwi;
1503 struct sctp_asconf_iterator *asc;
1505 SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1506 sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
1508 /* Try later, no memory */
1509 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1510 (struct sctp_inpcb *)NULL,
1511 (struct sctp_tcb *)NULL,
1512 (struct sctp_nets *)NULL);
1515 LIST_INIT(&asc->list_of_work);
1518 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
1519 LIST_REMOVE(wi, sctp_nxt_addr);
1520 LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
1524 if (asc->cnt == 0) {
1525 SCTP_FREE(asc, SCTP_M_ASC_IT);
1529 ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
1530 sctp_asconf_iterator_stcb,
1531 NULL, /* No ep end for boundall */
1532 SCTP_PCB_FLAGS_BOUNDALL,
1533 SCTP_PCB_ANY_FEATURES,
1534 SCTP_ASOC_ANY_STATE,
1536 sctp_asconf_iterator_end, NULL, 0);
1538 SCTP_PRINTF("Failed to initiate iterator for handle_addr_wq\n");
1540 * Freeing if we are stopping or put back on the
1543 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
1544 sctp_asconf_iterator_end(asc, 0);
1546 LIST_FOREACH(wi, &asc->list_of_work, sctp_nxt_addr) {
1547 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
1549 SCTP_FREE(asc, SCTP_M_ASC_IT);
1556 sctp_timeout_handler(void *t)
1558 struct sctp_inpcb *inp;
1559 struct sctp_tcb *stcb;
1560 struct sctp_nets *net;
1561 struct sctp_timer *tmr;
1562 struct mbuf *op_err;
1563 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1569 tmr = (struct sctp_timer *)t;
1570 inp = (struct sctp_inpcb *)tmr->ep;
1571 stcb = (struct sctp_tcb *)tmr->tcb;
1572 net = (struct sctp_nets *)tmr->net;
1573 CURVNET_SET((struct vnet *)tmr->vnet);
1576 #ifdef SCTP_AUDITING_ENABLED
1577 sctp_audit_log(0xF0, (uint8_t)tmr->type);
1578 sctp_auditing(3, inp, stcb, net);
1581 /* sanity checks... */
1582 if (tmr->self != (void *)tmr) {
1584 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1590 tmr->stopped_from = 0xa001;
1591 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1593 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1599 tmr->stopped_from = 0xa002;
1600 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1604 /* if this is an iterator timeout, get the struct and clear inp */
1605 tmr->stopped_from = 0xa003;
1607 SCTP_INP_INCR_REF(inp);
1608 if ((inp->sctp_socket == NULL) &&
1609 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1610 (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1611 (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1612 (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1613 (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1614 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1615 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1616 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1617 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))) {
1618 SCTP_INP_DECR_REF(inp);
1623 tmr->stopped_from = 0xa004;
1625 atomic_add_int(&stcb->asoc.refcnt, 1);
1626 if (stcb->asoc.state == 0) {
1627 atomic_add_int(&stcb->asoc.refcnt, -1);
1629 SCTP_INP_DECR_REF(inp);
1636 tmr->stopped_from = 0xa005;
1637 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", type);
1638 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1640 SCTP_INP_DECR_REF(inp);
1643 atomic_add_int(&stcb->asoc.refcnt, -1);
1648 tmr->stopped_from = 0xa006;
1651 SCTP_TCB_LOCK(stcb);
1652 atomic_add_int(&stcb->asoc.refcnt, -1);
1653 if ((type != SCTP_TIMER_TYPE_ASOCKILL) &&
1654 ((stcb->asoc.state == 0) ||
1655 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1656 SCTP_TCB_UNLOCK(stcb);
1658 SCTP_INP_DECR_REF(inp);
1663 } else if (inp != NULL) {
1664 if (type != SCTP_TIMER_TYPE_INPKILL) {
1665 SCTP_INP_WLOCK(inp);
1668 SCTP_WQ_ADDR_LOCK();
1670 /* record in stopped what t-o occurred */
1671 tmr->stopped_from = type;
1673 /* mark as being serviced now */
1674 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1676 * Callout has been rescheduled.
1680 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1682 * Not active, so no action.
1686 SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1688 /* call the handler for the appropriate timer type */
1690 case SCTP_TIMER_TYPE_ADDR_WQ:
1691 sctp_handle_addr_wq();
1693 case SCTP_TIMER_TYPE_SEND:
1694 if ((stcb == NULL) || (inp == NULL)) {
1697 SCTP_STAT_INCR(sctps_timodata);
1698 stcb->asoc.timodata++;
1699 stcb->asoc.num_send_timers_up--;
1700 if (stcb->asoc.num_send_timers_up < 0) {
1701 stcb->asoc.num_send_timers_up = 0;
1703 SCTP_TCB_LOCK_ASSERT(stcb);
1704 if (sctp_t3rxt_timer(inp, stcb, net)) {
1705 /* no need to unlock on tcb its gone */
1709 SCTP_TCB_LOCK_ASSERT(stcb);
1710 #ifdef SCTP_AUDITING_ENABLED
1711 sctp_auditing(4, inp, stcb, net);
1713 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1714 if ((stcb->asoc.num_send_timers_up == 0) &&
1715 (stcb->asoc.sent_queue_cnt > 0)) {
1716 struct sctp_tmit_chunk *chk;
1719 * safeguard. If there on some on the sent queue
1720 * somewhere but no timers running something is
1721 * wrong... so we start a timer on the first chunk
1722 * on the send queue on whatever net it is sent to.
1724 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1725 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1729 case SCTP_TIMER_TYPE_INIT:
1730 if ((stcb == NULL) || (inp == NULL)) {
1733 SCTP_STAT_INCR(sctps_timoinit);
1734 stcb->asoc.timoinit++;
1735 if (sctp_t1init_timer(inp, stcb, net)) {
1736 /* no need to unlock on tcb its gone */
1739 /* We do output but not here */
1742 case SCTP_TIMER_TYPE_RECV:
1743 if ((stcb == NULL) || (inp == NULL)) {
1746 SCTP_STAT_INCR(sctps_timosack);
1747 stcb->asoc.timosack++;
1748 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1749 #ifdef SCTP_AUDITING_ENABLED
1750 sctp_auditing(4, inp, stcb, net);
1752 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1754 case SCTP_TIMER_TYPE_SHUTDOWN:
1755 if ((stcb == NULL) || (inp == NULL)) {
1758 if (sctp_shutdown_timer(inp, stcb, net)) {
1759 /* no need to unlock on tcb its gone */
1762 SCTP_STAT_INCR(sctps_timoshutdown);
1763 stcb->asoc.timoshutdown++;
1764 #ifdef SCTP_AUDITING_ENABLED
1765 sctp_auditing(4, inp, stcb, net);
1767 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1769 case SCTP_TIMER_TYPE_HEARTBEAT:
1770 if ((stcb == NULL) || (inp == NULL) || (net == NULL)) {
1773 SCTP_STAT_INCR(sctps_timoheartbeat);
1774 stcb->asoc.timoheartbeat++;
1775 if (sctp_heartbeat_timer(inp, stcb, net)) {
1776 /* no need to unlock on tcb its gone */
1779 #ifdef SCTP_AUDITING_ENABLED
1780 sctp_auditing(4, inp, stcb, net);
1782 if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1783 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1784 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1787 case SCTP_TIMER_TYPE_COOKIE:
1788 if ((stcb == NULL) || (inp == NULL)) {
1792 if (sctp_cookie_timer(inp, stcb, net)) {
1793 /* no need to unlock on tcb its gone */
1796 SCTP_STAT_INCR(sctps_timocookie);
1797 stcb->asoc.timocookie++;
1798 #ifdef SCTP_AUDITING_ENABLED
1799 sctp_auditing(4, inp, stcb, net);
1802 * We consider T3 and Cookie timer pretty much the same with
1803 * respect to where from in chunk_output.
1805 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1807 case SCTP_TIMER_TYPE_NEWCOOKIE:
1815 SCTP_STAT_INCR(sctps_timosecret);
1816 (void)SCTP_GETTIME_TIMEVAL(&tv);
1817 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1818 inp->sctp_ep.last_secret_number =
1819 inp->sctp_ep.current_secret_number;
1820 inp->sctp_ep.current_secret_number++;
1821 if (inp->sctp_ep.current_secret_number >=
1822 SCTP_HOW_MANY_SECRETS) {
1823 inp->sctp_ep.current_secret_number = 0;
1825 secret = (int)inp->sctp_ep.current_secret_number;
1826 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1827 inp->sctp_ep.secret_key[secret][i] =
1828 sctp_select_initial_TSN(&inp->sctp_ep);
1830 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1834 case SCTP_TIMER_TYPE_PATHMTURAISE:
1835 if ((stcb == NULL) || (inp == NULL)) {
1838 SCTP_STAT_INCR(sctps_timopathmtu);
1839 sctp_pathmtu_timer(inp, stcb, net);
1842 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1843 if ((stcb == NULL) || (inp == NULL)) {
1846 if (sctp_shutdownack_timer(inp, stcb, net)) {
1847 /* no need to unlock on tcb its gone */
1850 SCTP_STAT_INCR(sctps_timoshutdownack);
1851 stcb->asoc.timoshutdownack++;
1852 #ifdef SCTP_AUDITING_ENABLED
1853 sctp_auditing(4, inp, stcb, net);
1855 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1857 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1858 if ((stcb == NULL) || (inp == NULL)) {
1861 SCTP_STAT_INCR(sctps_timoshutdownguard);
1862 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
1863 "Shutdown guard timer expired");
1864 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
1865 /* no need to unlock on tcb its gone */
1868 case SCTP_TIMER_TYPE_STRRESET:
1869 if ((stcb == NULL) || (inp == NULL)) {
1872 if (sctp_strreset_timer(inp, stcb, net)) {
1873 /* no need to unlock on tcb its gone */
1876 SCTP_STAT_INCR(sctps_timostrmrst);
1877 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1879 case SCTP_TIMER_TYPE_ASCONF:
1880 if ((stcb == NULL) || (inp == NULL)) {
1883 if (sctp_asconf_timer(inp, stcb, net)) {
1884 /* no need to unlock on tcb its gone */
1887 SCTP_STAT_INCR(sctps_timoasconf);
1888 #ifdef SCTP_AUDITING_ENABLED
1889 sctp_auditing(4, inp, stcb, net);
1891 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1893 case SCTP_TIMER_TYPE_PRIM_DELETED:
1894 if ((stcb == NULL) || (inp == NULL)) {
1897 sctp_delete_prim_timer(inp, stcb, net);
1898 SCTP_STAT_INCR(sctps_timodelprim);
1901 case SCTP_TIMER_TYPE_AUTOCLOSE:
1902 if ((stcb == NULL) || (inp == NULL)) {
1905 SCTP_STAT_INCR(sctps_timoautoclose);
1906 sctp_autoclose_timer(inp, stcb, net);
1907 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1910 case SCTP_TIMER_TYPE_ASOCKILL:
1911 if ((stcb == NULL) || (inp == NULL)) {
1914 SCTP_STAT_INCR(sctps_timoassockill);
1915 /* Can we free it yet? */
1916 SCTP_INP_DECR_REF(inp);
1917 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
1918 SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1919 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1920 so = SCTP_INP_SO(inp);
1921 atomic_add_int(&stcb->asoc.refcnt, 1);
1922 SCTP_TCB_UNLOCK(stcb);
1923 SCTP_SOCKET_LOCK(so, 1);
1924 SCTP_TCB_LOCK(stcb);
1925 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1927 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
1928 SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1929 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1930 SCTP_SOCKET_UNLOCK(so, 1);
1933 * free asoc, always unlocks (or destroy's) so prevent
1934 * duplicate unlock or unlock of a free mtx :-0
1938 case SCTP_TIMER_TYPE_INPKILL:
1939 SCTP_STAT_INCR(sctps_timoinpkill);
1944 * special case, take away our increment since WE are the
1947 SCTP_INP_DECR_REF(inp);
1948 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
1949 SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1950 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
1951 SCTP_CALLED_FROM_INPKILL_TIMER);
1955 SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1959 #ifdef SCTP_AUDITING_ENABLED
1960 sctp_audit_log(0xF1, (uint8_t)type);
1962 sctp_auditing(5, inp, stcb, net);
1964 if ((did_output) && stcb) {
1966 * Now we need to clean up the control chunk chain if an
1967 * ECNE is on it. It must be marked as UNSENT again so next
1968 * call will continue to send it until such time that we get
1969 * a CWR, to remove it. It is, however, less likely that we
1970 * will find a ecn echo on the chain though.
1972 sctp_fix_ecn_echo(&stcb->asoc);
1976 SCTP_TCB_UNLOCK(stcb);
1977 } else if (inp != NULL) {
1978 SCTP_INP_WUNLOCK(inp);
1980 SCTP_WQ_ADDR_UNLOCK();
1985 SCTP_INP_DECR_REF(inp);
1989 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type = %d)\n", type);
1994 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1995 struct sctp_nets *net)
1998 struct sctp_timer *tmr;
2000 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
2005 SCTP_TCB_LOCK_ASSERT(stcb);
2008 case SCTP_TIMER_TYPE_ADDR_WQ:
2009 /* Only 1 tick away :-) */
2010 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2011 to_ticks = SCTP_ADDRESS_TICK_DELAY;
2013 case SCTP_TIMER_TYPE_SEND:
2014 /* Here we use the RTO timer */
2018 if ((stcb == NULL) || (net == NULL)) {
2021 tmr = &net->rxt_timer;
2022 if (net->RTO == 0) {
2023 rto_val = stcb->asoc.initial_rto;
2027 to_ticks = MSEC_TO_TICKS(rto_val);
2030 case SCTP_TIMER_TYPE_INIT:
2032 * Here we use the INIT timer default usually about 1
2035 if ((stcb == NULL) || (net == NULL)) {
2038 tmr = &net->rxt_timer;
2039 if (net->RTO == 0) {
2040 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2042 to_ticks = MSEC_TO_TICKS(net->RTO);
2045 case SCTP_TIMER_TYPE_RECV:
2047 * Here we use the Delayed-Ack timer value from the inp
2048 * ususually about 200ms.
2053 tmr = &stcb->asoc.dack_timer;
2054 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
2056 case SCTP_TIMER_TYPE_SHUTDOWN:
2057 /* Here we use the RTO of the destination. */
2058 if ((stcb == NULL) || (net == NULL)) {
2061 if (net->RTO == 0) {
2062 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2064 to_ticks = MSEC_TO_TICKS(net->RTO);
2066 tmr = &net->rxt_timer;
2068 case SCTP_TIMER_TYPE_HEARTBEAT:
2070 * the net is used here so that we can add in the RTO. Even
2071 * though we use a different timer. We also add the HB timer
2072 * PLUS a random jitter.
2074 if ((stcb == NULL) || (net == NULL)) {
2080 if ((net->dest_state & SCTP_ADDR_NOHB) &&
2081 !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
2084 if (net->RTO == 0) {
2085 to_ticks = stcb->asoc.initial_rto;
2087 to_ticks = net->RTO;
2089 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2090 jitter = rndval % to_ticks;
2091 if (jitter >= (to_ticks >> 1)) {
2092 to_ticks = to_ticks + (jitter - (to_ticks >> 1));
2094 to_ticks = to_ticks - jitter;
2096 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2097 !(net->dest_state & SCTP_ADDR_PF)) {
2098 to_ticks += net->heart_beat_delay;
2101 * Now we must convert the to_ticks that are now in
2104 to_ticks = MSEC_TO_TICKS(to_ticks);
2105 tmr = &net->hb_timer;
2108 case SCTP_TIMER_TYPE_COOKIE:
2110 * Here we can use the RTO timer from the network since one
2111 * RTT was compelete. If a retran happened then we will be
2112 * using the RTO initial value.
2114 if ((stcb == NULL) || (net == NULL)) {
2117 if (net->RTO == 0) {
2118 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2120 to_ticks = MSEC_TO_TICKS(net->RTO);
2122 tmr = &net->rxt_timer;
2124 case SCTP_TIMER_TYPE_NEWCOOKIE:
2126 * nothing needed but the endpoint here ususually about 60
2129 tmr = &inp->sctp_ep.signature_change;
2130 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2132 case SCTP_TIMER_TYPE_ASOCKILL:
2136 tmr = &stcb->asoc.strreset_timer;
2137 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2139 case SCTP_TIMER_TYPE_INPKILL:
2141 * The inp is setup to die. We re-use the signature_chage
2142 * timer since that has stopped and we are in the GONE
2145 tmr = &inp->sctp_ep.signature_change;
2146 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2148 case SCTP_TIMER_TYPE_PATHMTURAISE:
2150 * Here we use the value found in the EP for PMTU ususually
2153 if ((stcb == NULL) || (net == NULL)) {
2156 if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2159 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2160 tmr = &net->pmtu_timer;
2162 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2163 /* Here we use the RTO of the destination */
2164 if ((stcb == NULL) || (net == NULL)) {
2167 if (net->RTO == 0) {
2168 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2170 to_ticks = MSEC_TO_TICKS(net->RTO);
2172 tmr = &net->rxt_timer;
2174 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2176 * Here we use the endpoints shutdown guard timer usually
2182 if (inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] == 0) {
2183 to_ticks = 5 * MSEC_TO_TICKS(stcb->asoc.maxrto);
2185 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2187 tmr = &stcb->asoc.shut_guard_timer;
2189 case SCTP_TIMER_TYPE_STRRESET:
2191 * Here the timer comes from the stcb but its value is from
2194 if ((stcb == NULL) || (net == NULL)) {
2197 if (net->RTO == 0) {
2198 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2200 to_ticks = MSEC_TO_TICKS(net->RTO);
2202 tmr = &stcb->asoc.strreset_timer;
2204 case SCTP_TIMER_TYPE_ASCONF:
2206 * Here the timer comes from the stcb but its value is from
2209 if ((stcb == NULL) || (net == NULL)) {
2212 if (net->RTO == 0) {
2213 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2215 to_ticks = MSEC_TO_TICKS(net->RTO);
2217 tmr = &stcb->asoc.asconf_timer;
2219 case SCTP_TIMER_TYPE_PRIM_DELETED:
2220 if ((stcb == NULL) || (net != NULL)) {
2223 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2224 tmr = &stcb->asoc.delete_prim_timer;
2226 case SCTP_TIMER_TYPE_AUTOCLOSE:
2230 if (stcb->asoc.sctp_autoclose_ticks == 0) {
2232 * Really an error since stcb is NOT set to
2237 to_ticks = stcb->asoc.sctp_autoclose_ticks;
2238 tmr = &stcb->asoc.autoclose_timer;
2241 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2246 if ((to_ticks <= 0) || (tmr == NULL)) {
2247 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2248 __func__, t_type, to_ticks, (void *)tmr);
2251 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2253 * we do NOT allow you to have it already running. if it is
2254 * we leave the current one up unchanged
2258 /* At this point we can proceed */
2259 if (t_type == SCTP_TIMER_TYPE_SEND) {
2260 stcb->asoc.num_send_timers_up++;
2262 tmr->stopped_from = 0;
2264 tmr->ep = (void *)inp;
2265 tmr->tcb = (void *)stcb;
2266 tmr->net = (void *)net;
2267 tmr->self = (void *)tmr;
2268 tmr->vnet = (void *)curvnet;
2269 tmr->ticks = sctp_get_tick_count();
2270 (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2275 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2276 struct sctp_nets *net, uint32_t from)
2278 struct sctp_timer *tmr;
2280 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2286 SCTP_TCB_LOCK_ASSERT(stcb);
2289 case SCTP_TIMER_TYPE_ADDR_WQ:
2290 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2292 case SCTP_TIMER_TYPE_SEND:
2293 if ((stcb == NULL) || (net == NULL)) {
2296 tmr = &net->rxt_timer;
2298 case SCTP_TIMER_TYPE_INIT:
2299 if ((stcb == NULL) || (net == NULL)) {
2302 tmr = &net->rxt_timer;
2304 case SCTP_TIMER_TYPE_RECV:
2308 tmr = &stcb->asoc.dack_timer;
2310 case SCTP_TIMER_TYPE_SHUTDOWN:
2311 if ((stcb == NULL) || (net == NULL)) {
2314 tmr = &net->rxt_timer;
2316 case SCTP_TIMER_TYPE_HEARTBEAT:
2317 if ((stcb == NULL) || (net == NULL)) {
2320 tmr = &net->hb_timer;
2322 case SCTP_TIMER_TYPE_COOKIE:
2323 if ((stcb == NULL) || (net == NULL)) {
2326 tmr = &net->rxt_timer;
2328 case SCTP_TIMER_TYPE_NEWCOOKIE:
2329 /* nothing needed but the endpoint here */
2330 tmr = &inp->sctp_ep.signature_change;
2332 * We re-use the newcookie timer for the INP kill timer. We
2333 * must assure that we do not kill it by accident.
2336 case SCTP_TIMER_TYPE_ASOCKILL:
2338 * Stop the asoc kill timer.
2343 tmr = &stcb->asoc.strreset_timer;
2346 case SCTP_TIMER_TYPE_INPKILL:
2348 * The inp is setup to die. We re-use the signature_chage
2349 * timer since that has stopped and we are in the GONE
2352 tmr = &inp->sctp_ep.signature_change;
2354 case SCTP_TIMER_TYPE_PATHMTURAISE:
2355 if ((stcb == NULL) || (net == NULL)) {
2358 tmr = &net->pmtu_timer;
2360 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2361 if ((stcb == NULL) || (net == NULL)) {
2364 tmr = &net->rxt_timer;
2366 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2370 tmr = &stcb->asoc.shut_guard_timer;
2372 case SCTP_TIMER_TYPE_STRRESET:
2376 tmr = &stcb->asoc.strreset_timer;
2378 case SCTP_TIMER_TYPE_ASCONF:
2382 tmr = &stcb->asoc.asconf_timer;
2384 case SCTP_TIMER_TYPE_PRIM_DELETED:
2388 tmr = &stcb->asoc.delete_prim_timer;
2390 case SCTP_TIMER_TYPE_AUTOCLOSE:
2394 tmr = &stcb->asoc.autoclose_timer;
2397 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2404 if ((tmr->type != t_type) && tmr->type) {
2406 * Ok we have a timer that is under joint use. Cookie timer
2407 * per chance with the SEND timer. We therefore are NOT
2408 * running the timer that the caller wants stopped. So just
2413 if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2414 stcb->asoc.num_send_timers_up--;
2415 if (stcb->asoc.num_send_timers_up < 0) {
2416 stcb->asoc.num_send_timers_up = 0;
2420 tmr->stopped_from = from;
2421 (void)SCTP_OS_TIMER_STOP(&tmr->timer);
2426 sctp_calculate_len(struct mbuf *m)
2433 tlen += SCTP_BUF_LEN(at);
2434 at = SCTP_BUF_NEXT(at);
2440 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2441 struct sctp_association *asoc, uint32_t mtu)
2444 * Reset the P-MTU size on this association, this involves changing
2445 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2446 * allow the DF flag to be cleared.
2448 struct sctp_tmit_chunk *chk;
2449 unsigned int eff_mtu, ovh;
2451 asoc->smallest_mtu = mtu;
2452 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2453 ovh = SCTP_MIN_OVERHEAD;
2455 ovh = SCTP_MIN_V4_OVERHEAD;
2457 eff_mtu = mtu - ovh;
2458 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2459 if (chk->send_size > eff_mtu) {
2460 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2463 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2464 if (chk->send_size > eff_mtu) {
2465 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2472 * given an association and starting time of the current RTT period return
2473 * RTO in number of msecs net should point to the current network
2477 sctp_calculate_rto(struct sctp_tcb *stcb,
2478 struct sctp_association *asoc,
2479 struct sctp_nets *net,
2480 struct timeval *old,
2484 * given an association and the starting time of the current RTT
2485 * period (in value1/value2) return RTO in number of msecs.
2487 int32_t rtt; /* RTT in ms */
2489 int first_measure = 0;
2492 /************************/
2493 /* 1. calculate new RTT */
2494 /************************/
2495 /* get the current time */
2496 if (stcb->asoc.use_precise_time) {
2497 (void)SCTP_GETPTIME_TIMEVAL(&now);
2499 (void)SCTP_GETTIME_TIMEVAL(&now);
2501 timevalsub(&now, old);
2502 /* store the current RTT in us */
2503 net->rtt = (uint64_t)1000000 * (uint64_t)now.tv_sec +
2504 (uint64_t)now.tv_usec;
2505 /* compute rtt in ms */
2506 rtt = (int32_t)(net->rtt / 1000);
2507 if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2509 * Tell the CC module that a new update has just occurred
2512 (*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2515 * Do we need to determine the lan? We do this only on sacks i.e.
2516 * RTT being determined from data not non-data (HB/INIT->INITACK).
2518 if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2519 (net->lan_type == SCTP_LAN_UNKNOWN)) {
2520 if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2521 net->lan_type = SCTP_LAN_INTERNET;
2523 net->lan_type = SCTP_LAN_LOCAL;
2527 /***************************/
2528 /* 2. update RTTVAR & SRTT */
2529 /***************************/
2531 * Compute the scaled average lastsa and the
2532 * scaled variance lastsv as described in van Jacobson
2533 * Paper "Congestion Avoidance and Control", Annex A.
2535 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
2536 * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar
2538 if (net->RTO_measured) {
2539 rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2544 rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2546 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2547 rto_logging(net, SCTP_LOG_RTTVAR);
2550 /* First RTO measurment */
2551 net->RTO_measured = 1;
2553 net->lastsa = rtt << SCTP_RTT_SHIFT;
2554 net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2555 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2556 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2559 if (net->lastsv == 0) {
2560 net->lastsv = SCTP_CLOCK_GRANULARITY;
2562 new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2563 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2564 (stcb->asoc.sat_network_lockout == 0)) {
2565 stcb->asoc.sat_network = 1;
2566 } else if ((!first_measure) && stcb->asoc.sat_network) {
2567 stcb->asoc.sat_network = 0;
2568 stcb->asoc.sat_network_lockout = 1;
2570 /* bound it, per C6/C7 in Section 5.3.1 */
2571 if (new_rto < stcb->asoc.minrto) {
2572 new_rto = stcb->asoc.minrto;
2574 if (new_rto > stcb->asoc.maxrto) {
2575 new_rto = stcb->asoc.maxrto;
2577 /* we are now returning the RTO */
2582 * return a pointer to a contiguous piece of data from the given mbuf chain
2583 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2584 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2585 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2588 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t *in_ptr)
2594 if ((off < 0) || (len <= 0))
2597 /* find the desired start location */
2598 while ((m != NULL) && (off > 0)) {
2599 if (off < SCTP_BUF_LEN(m))
2601 off -= SCTP_BUF_LEN(m);
2602 m = SCTP_BUF_NEXT(m);
2607 /* is the current mbuf large enough (eg. contiguous)? */
2608 if ((SCTP_BUF_LEN(m) - off) >= len) {
2609 return (mtod(m, caddr_t)+off);
2611 /* else, it spans more than one mbuf, so save a temp copy... */
2612 while ((m != NULL) && (len > 0)) {
2613 count = min(SCTP_BUF_LEN(m) - off, len);
2614 memcpy(ptr, mtod(m, caddr_t)+off, count);
2618 m = SCTP_BUF_NEXT(m);
2620 if ((m == NULL) && (len > 0))
2623 return ((caddr_t)in_ptr);
2629 struct sctp_paramhdr *
2630 sctp_get_next_param(struct mbuf *m,
2632 struct sctp_paramhdr *pull,
2635 /* This just provides a typed signature to Peter's Pull routine */
2636 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2642 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2644 struct mbuf *m_last;
2650 if (padlen <= M_TRAILINGSPACE(m)) {
2652 * The easy way. We hope the majority of the time we hit
2657 /* Hard way we must grow the mbuf chain */
2658 m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
2659 if (m_last == NULL) {
2662 SCTP_BUF_LEN(m_last) = 0;
2663 SCTP_BUF_NEXT(m_last) = NULL;
2664 SCTP_BUF_NEXT(m) = m_last;
2666 dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
2667 SCTP_BUF_LEN(m_last) += padlen;
2668 memset(dp, 0, padlen);
2673 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2675 /* find the last mbuf in chain and pad it */
2678 if (last_mbuf != NULL) {
2679 return (sctp_add_pad_tombuf(last_mbuf, padval));
2681 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
2682 if (SCTP_BUF_NEXT(m_at) == NULL) {
2683 return (sctp_add_pad_tombuf(m_at, padval));
2691 sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
2692 uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
2693 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2698 struct mbuf *m_notify;
2699 struct sctp_assoc_change *sac;
2700 struct sctp_queued_to_read *control;
2701 unsigned int notif_len;
2704 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2711 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2712 notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
2713 if (abort != NULL) {
2714 abort_len = ntohs(abort->ch.chunk_length);
2716 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
2719 if (abort_len > SCTP_CHUNK_BUFFER_SIZE) {
2720 abort_len = SCTP_CHUNK_BUFFER_SIZE;
2725 if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2726 notif_len += SCTP_ASSOC_SUPPORTS_MAX;
2727 } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2728 notif_len += abort_len;
2730 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2731 if (m_notify == NULL) {
2732 /* Retry with smaller value. */
2733 notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
2734 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2735 if (m_notify == NULL) {
2739 SCTP_BUF_NEXT(m_notify) = NULL;
2740 sac = mtod(m_notify, struct sctp_assoc_change *);
2741 memset(sac, 0, notif_len);
2742 sac->sac_type = SCTP_ASSOC_CHANGE;
2744 sac->sac_length = sizeof(struct sctp_assoc_change);
2745 sac->sac_state = state;
2746 sac->sac_error = error;
2747 /* XXX verify these stream counts */
2748 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2749 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2750 sac->sac_assoc_id = sctp_get_associd(stcb);
2751 if (notif_len > sizeof(struct sctp_assoc_change)) {
2752 if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2754 if (stcb->asoc.prsctp_supported == 1) {
2755 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
2757 if (stcb->asoc.auth_supported == 1) {
2758 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
2760 if (stcb->asoc.asconf_supported == 1) {
2761 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
2763 if (stcb->asoc.idata_supported == 1) {
2764 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_INTERLEAVING;
2766 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
2767 if (stcb->asoc.reconfig_supported == 1) {
2768 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
2770 sac->sac_length += i;
2771 } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2772 memcpy(sac->sac_info, abort, abort_len);
2773 sac->sac_length += abort_len;
2776 SCTP_BUF_LEN(m_notify) = sac->sac_length;
2777 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2778 0, 0, stcb->asoc.context, 0, 0, 0,
2780 if (control != NULL) {
2781 control->length = SCTP_BUF_LEN(m_notify);
2782 control->spec_flags = M_NOTIFICATION;
2783 /* not that we need this */
2784 control->tail_mbuf = m_notify;
2785 sctp_add_to_readq(stcb->sctp_ep, stcb,
2787 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2790 sctp_m_freem(m_notify);
2794 * For 1-to-1 style sockets, we send up and error when an ABORT
2798 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2799 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2800 ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2801 SOCK_LOCK(stcb->sctp_socket);
2803 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
2804 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
2805 stcb->sctp_socket->so_error = ECONNREFUSED;
2807 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
2808 stcb->sctp_socket->so_error = ECONNRESET;
2811 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
2812 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
2813 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
2814 stcb->sctp_socket->so_error = ETIMEDOUT;
2816 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
2817 stcb->sctp_socket->so_error = ECONNABORTED;
2820 SOCK_UNLOCK(stcb->sctp_socket);
2822 /* Wake ANY sleepers */
2823 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2824 so = SCTP_INP_SO(stcb->sctp_ep);
2826 atomic_add_int(&stcb->asoc.refcnt, 1);
2827 SCTP_TCB_UNLOCK(stcb);
2828 SCTP_SOCKET_LOCK(so, 1);
2829 SCTP_TCB_LOCK(stcb);
2830 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2831 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2832 SCTP_SOCKET_UNLOCK(so, 1);
2837 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2838 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2839 ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2840 socantrcvmore(stcb->sctp_socket);
2842 sorwakeup(stcb->sctp_socket);
2843 sowwakeup(stcb->sctp_socket);
2844 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2846 SCTP_SOCKET_UNLOCK(so, 1);
2852 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2853 struct sockaddr *sa, uint32_t error, int so_locked
2854 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2859 struct mbuf *m_notify;
2860 struct sctp_paddr_change *spc;
2861 struct sctp_queued_to_read *control;
2863 if ((stcb == NULL) ||
2864 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2865 /* event not enabled */
2868 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
2869 if (m_notify == NULL)
2871 SCTP_BUF_LEN(m_notify) = 0;
2872 spc = mtod(m_notify, struct sctp_paddr_change *);
2873 memset(spc, 0, sizeof(struct sctp_paddr_change));
2874 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2876 spc->spc_length = sizeof(struct sctp_paddr_change);
2877 switch (sa->sa_family) {
2881 if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2882 in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
2883 (struct sockaddr_in6 *)&spc->spc_aaddr);
2885 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2888 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2895 struct sockaddr_in6 *sin6;
2897 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2899 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2900 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2901 if (sin6->sin6_scope_id == 0) {
2902 /* recover scope_id for user */
2903 (void)sa6_recoverscope(sin6);
2905 /* clear embedded scope_id for user */
2906 in6_clearscope(&sin6->sin6_addr);
2916 spc->spc_state = state;
2917 spc->spc_error = error;
2918 spc->spc_assoc_id = sctp_get_associd(stcb);
2920 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2921 SCTP_BUF_NEXT(m_notify) = NULL;
2923 /* append to socket */
2924 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2925 0, 0, stcb->asoc.context, 0, 0, 0,
2927 if (control == NULL) {
2929 sctp_m_freem(m_notify);
2932 control->length = SCTP_BUF_LEN(m_notify);
2933 control->spec_flags = M_NOTIFICATION;
2934 /* not that we need this */
2935 control->tail_mbuf = m_notify;
2936 sctp_add_to_readq(stcb->sctp_ep, stcb,
2938 &stcb->sctp_socket->so_rcv, 1,
2939 SCTP_READ_LOCK_NOT_HELD,
2945 sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
2946 struct sctp_tmit_chunk *chk, int so_locked
2947 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2952 struct mbuf *m_notify;
2953 struct sctp_send_failed *ssf;
2954 struct sctp_send_failed_event *ssfe;
2955 struct sctp_queued_to_read *control;
2956 struct sctp_chunkhdr *chkhdr;
2957 int notifhdr_len, chk_len, chkhdr_len, padding_len, payload_len;
2959 if ((stcb == NULL) ||
2960 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
2961 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2962 /* event not enabled */
2966 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
2967 notifhdr_len = sizeof(struct sctp_send_failed_event);
2969 notifhdr_len = sizeof(struct sctp_send_failed);
2971 m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
2972 if (m_notify == NULL)
2975 SCTP_BUF_LEN(m_notify) = notifhdr_len;
2976 if (stcb->asoc.idata_supported) {
2977 chkhdr_len = sizeof(struct sctp_idata_chunk);
2979 chkhdr_len = sizeof(struct sctp_data_chunk);
2981 /* Use some defaults in case we can't access the chunk header */
2982 if (chk->send_size >= chkhdr_len) {
2983 payload_len = chk->send_size - chkhdr_len;
2988 if (chk->data != NULL) {
2989 chkhdr = mtod(chk->data, struct sctp_chunkhdr *);
2990 if (chkhdr != NULL) {
2991 chk_len = ntohs(chkhdr->chunk_length);
2992 if ((chk_len >= chkhdr_len) &&
2993 (chk->send_size >= chk_len) &&
2994 (chk->send_size - chk_len < 4)) {
2995 padding_len = chk->send_size - chk_len;
2996 payload_len = chk->send_size - chkhdr_len - padding_len;
3000 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3001 ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3002 memset(ssfe, 0, notifhdr_len);
3003 ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3005 ssfe->ssfe_flags = SCTP_DATA_SENT;
3007 ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3009 ssfe->ssfe_length = (uint32_t)(notifhdr_len + payload_len);
3010 ssfe->ssfe_error = error;
3011 /* not exactly what the user sent in, but should be close :) */
3012 ssfe->ssfe_info.snd_sid = chk->rec.data.sid;
3013 ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
3014 ssfe->ssfe_info.snd_ppid = chk->rec.data.ppid;
3015 ssfe->ssfe_info.snd_context = chk->rec.data.context;
3016 ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3017 ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3019 ssf = mtod(m_notify, struct sctp_send_failed *);
3020 memset(ssf, 0, notifhdr_len);
3021 ssf->ssf_type = SCTP_SEND_FAILED;
3023 ssf->ssf_flags = SCTP_DATA_SENT;
3025 ssf->ssf_flags = SCTP_DATA_UNSENT;
3027 ssf->ssf_length = (uint32_t)(notifhdr_len + payload_len);
3028 ssf->ssf_error = error;
3029 /* not exactly what the user sent in, but should be close :) */
3030 ssf->ssf_info.sinfo_stream = chk->rec.data.sid;
3031 ssf->ssf_info.sinfo_ssn = (uint16_t)chk->rec.data.mid;
3032 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
3033 ssf->ssf_info.sinfo_ppid = chk->rec.data.ppid;
3034 ssf->ssf_info.sinfo_context = chk->rec.data.context;
3035 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3036 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3038 if (chk->data != NULL) {
3039 /* Trim off the sctp chunk header (it should be there) */
3040 if (chk->send_size == chkhdr_len + payload_len + padding_len) {
3041 m_adj(chk->data, chkhdr_len);
3042 m_adj(chk->data, -padding_len);
3043 sctp_mbuf_crush(chk->data);
3044 chk->send_size -= (chkhdr_len + padding_len);
3047 SCTP_BUF_NEXT(m_notify) = chk->data;
3048 /* Steal off the mbuf */
3051 * For this case, we check the actual socket buffer, since the assoc
3052 * is going away we don't want to overfill the socket buffer for a
3055 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3056 sctp_m_freem(m_notify);
3059 /* append to socket */
3060 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3061 0, 0, stcb->asoc.context, 0, 0, 0,
3063 if (control == NULL) {
3065 sctp_m_freem(m_notify);
3068 control->length = SCTP_BUF_LEN(m_notify);
3069 control->spec_flags = M_NOTIFICATION;
3070 /* not that we need this */
3071 control->tail_mbuf = m_notify;
3072 sctp_add_to_readq(stcb->sctp_ep, stcb,
3074 &stcb->sctp_socket->so_rcv, 1,
3075 SCTP_READ_LOCK_NOT_HELD,
3081 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3082 struct sctp_stream_queue_pending *sp, int so_locked
3083 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3088 struct mbuf *m_notify;
3089 struct sctp_send_failed *ssf;
3090 struct sctp_send_failed_event *ssfe;
3091 struct sctp_queued_to_read *control;
3094 if ((stcb == NULL) ||
3095 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
3096 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3097 /* event not enabled */
3100 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3101 notifhdr_len = sizeof(struct sctp_send_failed_event);
3103 notifhdr_len = sizeof(struct sctp_send_failed);
3105 m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3106 if (m_notify == NULL) {
3110 SCTP_BUF_LEN(m_notify) = notifhdr_len;
3111 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3112 ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3113 memset(ssfe, 0, notifhdr_len);
3114 ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3115 ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3116 ssfe->ssfe_length = (uint32_t)(notifhdr_len + sp->length);
3117 ssfe->ssfe_error = error;
3118 /* not exactly what the user sent in, but should be close :) */
3119 ssfe->ssfe_info.snd_sid = sp->sid;
3120 if (sp->some_taken) {
3121 ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
3123 ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
3125 ssfe->ssfe_info.snd_ppid = sp->ppid;
3126 ssfe->ssfe_info.snd_context = sp->context;
3127 ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3128 ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3130 ssf = mtod(m_notify, struct sctp_send_failed *);
3131 memset(ssf, 0, notifhdr_len);
3132 ssf->ssf_type = SCTP_SEND_FAILED;
3133 ssf->ssf_flags = SCTP_DATA_UNSENT;
3134 ssf->ssf_length = (uint32_t)(notifhdr_len + sp->length);
3135 ssf->ssf_error = error;
3136 /* not exactly what the user sent in, but should be close :) */
3137 ssf->ssf_info.sinfo_stream = sp->sid;
3138 ssf->ssf_info.sinfo_ssn = 0;
3139 if (sp->some_taken) {
3140 ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3142 ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3144 ssf->ssf_info.sinfo_ppid = sp->ppid;
3145 ssf->ssf_info.sinfo_context = sp->context;
3146 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3147 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3149 SCTP_BUF_NEXT(m_notify) = sp->data;
3151 /* Steal off the mbuf */
3154 * For this case, we check the actual socket buffer, since the assoc
3155 * is going away we don't want to overfill the socket buffer for a
3158 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3159 sctp_m_freem(m_notify);
3162 /* append to socket */
3163 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3164 0, 0, stcb->asoc.context, 0, 0, 0,
3166 if (control == NULL) {
3168 sctp_m_freem(m_notify);
3171 control->length = SCTP_BUF_LEN(m_notify);
3172 control->spec_flags = M_NOTIFICATION;
3173 /* not that we need this */
3174 control->tail_mbuf = m_notify;
3175 sctp_add_to_readq(stcb->sctp_ep, stcb,
3177 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3183 sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3185 struct mbuf *m_notify;
3186 struct sctp_adaptation_event *sai;
3187 struct sctp_queued_to_read *control;
3189 if ((stcb == NULL) ||
3190 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3191 /* event not enabled */
3195 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3196 if (m_notify == NULL)
3199 SCTP_BUF_LEN(m_notify) = 0;
3200 sai = mtod(m_notify, struct sctp_adaptation_event *);
3201 memset(sai, 0, sizeof(struct sctp_adaptation_event));
3202 sai->sai_type = SCTP_ADAPTATION_INDICATION;
3204 sai->sai_length = sizeof(struct sctp_adaptation_event);
3205 sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3206 sai->sai_assoc_id = sctp_get_associd(stcb);
3208 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3209 SCTP_BUF_NEXT(m_notify) = NULL;
3211 /* append to socket */
3212 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3213 0, 0, stcb->asoc.context, 0, 0, 0,
3215 if (control == NULL) {
3217 sctp_m_freem(m_notify);
3220 control->length = SCTP_BUF_LEN(m_notify);
3221 control->spec_flags = M_NOTIFICATION;
3222 /* not that we need this */
3223 control->tail_mbuf = m_notify;
3224 sctp_add_to_readq(stcb->sctp_ep, stcb,
3226 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3229 /* This always must be called with the read-queue LOCKED in the INP */
3231 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3232 uint32_t val, int so_locked
3233 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3238 struct mbuf *m_notify;
3239 struct sctp_pdapi_event *pdapi;
3240 struct sctp_queued_to_read *control;
3243 if ((stcb == NULL) ||
3244 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3245 /* event not enabled */
3248 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3252 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3253 if (m_notify == NULL)
3256 SCTP_BUF_LEN(m_notify) = 0;
3257 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3258 memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3259 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3260 pdapi->pdapi_flags = 0;
3261 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3262 pdapi->pdapi_indication = error;
3263 pdapi->pdapi_stream = (val >> 16);
3264 pdapi->pdapi_seq = (val & 0x0000ffff);
3265 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3267 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3268 SCTP_BUF_NEXT(m_notify) = NULL;
3269 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3270 0, 0, stcb->asoc.context, 0, 0, 0,
3272 if (control == NULL) {
3274 sctp_m_freem(m_notify);
3277 control->length = SCTP_BUF_LEN(m_notify);
3278 control->spec_flags = M_NOTIFICATION;
3279 /* not that we need this */
3280 control->tail_mbuf = m_notify;
3281 sb = &stcb->sctp_socket->so_rcv;
3282 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3283 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
3285 sctp_sballoc(stcb, sb, m_notify);
3286 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3287 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3289 control->end_added = 1;
3290 if (stcb->asoc.control_pdapi)
3291 TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
3293 /* we really should not see this case */
3294 TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
3296 if (stcb->sctp_ep && stcb->sctp_socket) {
3297 /* This should always be the case */
3298 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3301 so = SCTP_INP_SO(stcb->sctp_ep);
3303 atomic_add_int(&stcb->asoc.refcnt, 1);
3304 SCTP_TCB_UNLOCK(stcb);
3305 SCTP_SOCKET_LOCK(so, 1);
3306 SCTP_TCB_LOCK(stcb);
3307 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3308 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3309 SCTP_SOCKET_UNLOCK(so, 1);
3314 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3315 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3317 SCTP_SOCKET_UNLOCK(so, 1);
3324 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3326 struct mbuf *m_notify;
3327 struct sctp_shutdown_event *sse;
3328 struct sctp_queued_to_read *control;
3331 * For TCP model AND UDP connected sockets we will send an error up
3332 * when an SHUTDOWN completes
3334 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3335 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3336 /* mark socket closed for read/write and wakeup! */
3337 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3340 so = SCTP_INP_SO(stcb->sctp_ep);
3341 atomic_add_int(&stcb->asoc.refcnt, 1);
3342 SCTP_TCB_UNLOCK(stcb);
3343 SCTP_SOCKET_LOCK(so, 1);
3344 SCTP_TCB_LOCK(stcb);
3345 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3346 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3347 SCTP_SOCKET_UNLOCK(so, 1);
3351 socantsendmore(stcb->sctp_socket);
3352 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3353 SCTP_SOCKET_UNLOCK(so, 1);
3356 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3357 /* event not enabled */
3361 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3362 if (m_notify == NULL)
3365 sse = mtod(m_notify, struct sctp_shutdown_event *);
3366 memset(sse, 0, sizeof(struct sctp_shutdown_event));
3367 sse->sse_type = SCTP_SHUTDOWN_EVENT;
3369 sse->sse_length = sizeof(struct sctp_shutdown_event);
3370 sse->sse_assoc_id = sctp_get_associd(stcb);
3372 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3373 SCTP_BUF_NEXT(m_notify) = NULL;
3375 /* append to socket */
3376 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3377 0, 0, stcb->asoc.context, 0, 0, 0,
3379 if (control == NULL) {
3381 sctp_m_freem(m_notify);
3384 control->length = SCTP_BUF_LEN(m_notify);
3385 control->spec_flags = M_NOTIFICATION;
3386 /* not that we need this */
3387 control->tail_mbuf = m_notify;
3388 sctp_add_to_readq(stcb->sctp_ep, stcb,
3390 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3394 sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3396 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3401 struct mbuf *m_notify;
3402 struct sctp_sender_dry_event *event;
3403 struct sctp_queued_to_read *control;
3405 if ((stcb == NULL) ||
3406 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3407 /* event not enabled */
3411 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3412 if (m_notify == NULL) {
3416 SCTP_BUF_LEN(m_notify) = 0;
3417 event = mtod(m_notify, struct sctp_sender_dry_event *);
3418 memset(event, 0, sizeof(struct sctp_sender_dry_event));
3419 event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3420 event->sender_dry_flags = 0;
3421 event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3422 event->sender_dry_assoc_id = sctp_get_associd(stcb);
3424 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3425 SCTP_BUF_NEXT(m_notify) = NULL;
3427 /* append to socket */
3428 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3429 0, 0, stcb->asoc.context, 0, 0, 0,
3431 if (control == NULL) {
3433 sctp_m_freem(m_notify);
3436 control->length = SCTP_BUF_LEN(m_notify);
3437 control->spec_flags = M_NOTIFICATION;
3438 /* not that we need this */
3439 control->tail_mbuf = m_notify;
3440 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3441 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3446 sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3448 struct mbuf *m_notify;
3449 struct sctp_queued_to_read *control;
3450 struct sctp_stream_change_event *stradd;
3452 if ((stcb == NULL) ||
3453 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3454 /* event not enabled */
3457 if ((stcb->asoc.peer_req_out) && flag) {
3458 /* Peer made the request, don't tell the local user */
3459 stcb->asoc.peer_req_out = 0;
3462 stcb->asoc.peer_req_out = 0;
3463 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3464 if (m_notify == NULL)
3467 SCTP_BUF_LEN(m_notify) = 0;
3468 stradd = mtod(m_notify, struct sctp_stream_change_event *);
3469 memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3470 stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3471 stradd->strchange_flags = flag;
3472 stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3473 stradd->strchange_assoc_id = sctp_get_associd(stcb);
3474 stradd->strchange_instrms = numberin;
3475 stradd->strchange_outstrms = numberout;
3476 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3477 SCTP_BUF_NEXT(m_notify) = NULL;
3478 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3480 sctp_m_freem(m_notify);
3483 /* append to socket */
3484 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3485 0, 0, stcb->asoc.context, 0, 0, 0,
3487 if (control == NULL) {
3489 sctp_m_freem(m_notify);
3492 control->length = SCTP_BUF_LEN(m_notify);
3493 control->spec_flags = M_NOTIFICATION;
3494 /* not that we need this */
3495 control->tail_mbuf = m_notify;
3496 sctp_add_to_readq(stcb->sctp_ep, stcb,
3498 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3502 sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3504 struct mbuf *m_notify;
3505 struct sctp_queued_to_read *control;
3506 struct sctp_assoc_reset_event *strasoc;
3508 if ((stcb == NULL) ||
3509 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3510 /* event not enabled */
3513 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3514 if (m_notify == NULL)
3517 SCTP_BUF_LEN(m_notify) = 0;
3518 strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3519 memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3520 strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3521 strasoc->assocreset_flags = flag;
3522 strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3523 strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3524 strasoc->assocreset_local_tsn = sending_tsn;
3525 strasoc->assocreset_remote_tsn = recv_tsn;
3526 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3527 SCTP_BUF_NEXT(m_notify) = NULL;
3528 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3530 sctp_m_freem(m_notify);
3533 /* append to socket */
3534 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3535 0, 0, stcb->asoc.context, 0, 0, 0,
3537 if (control == NULL) {
3539 sctp_m_freem(m_notify);
3542 control->length = SCTP_BUF_LEN(m_notify);
3543 control->spec_flags = M_NOTIFICATION;
3544 /* not that we need this */
3545 control->tail_mbuf = m_notify;
3546 sctp_add_to_readq(stcb->sctp_ep, stcb,
3548 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3554 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3555 int number_entries, uint16_t *list, int flag)
3557 struct mbuf *m_notify;
3558 struct sctp_queued_to_read *control;
3559 struct sctp_stream_reset_event *strreset;
3562 if ((stcb == NULL) ||
3563 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3564 /* event not enabled */
3568 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3569 if (m_notify == NULL)
3572 SCTP_BUF_LEN(m_notify) = 0;
3573 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3574 if (len > M_TRAILINGSPACE(m_notify)) {
3575 /* never enough room */
3576 sctp_m_freem(m_notify);
3579 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3580 memset(strreset, 0, len);
3581 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3582 strreset->strreset_flags = flag;
3583 strreset->strreset_length = len;
3584 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3585 if (number_entries) {
3588 for (i = 0; i < number_entries; i++) {
3589 strreset->strreset_stream_list[i] = ntohs(list[i]);
3592 SCTP_BUF_LEN(m_notify) = len;
3593 SCTP_BUF_NEXT(m_notify) = NULL;
3594 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3596 sctp_m_freem(m_notify);
3599 /* append to socket */
3600 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3601 0, 0, stcb->asoc.context, 0, 0, 0,
3603 if (control == NULL) {
3605 sctp_m_freem(m_notify);
3608 control->length = SCTP_BUF_LEN(m_notify);
3609 control->spec_flags = M_NOTIFICATION;
3610 /* not that we need this */
3611 control->tail_mbuf = m_notify;
3612 sctp_add_to_readq(stcb->sctp_ep, stcb,
3614 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3619 sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3621 struct mbuf *m_notify;
3622 struct sctp_remote_error *sre;
3623 struct sctp_queued_to_read *control;
3624 unsigned int notif_len;
3627 if ((stcb == NULL) ||
3628 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3631 if (chunk != NULL) {
3632 chunk_len = ntohs(chunk->ch.chunk_length);
3634 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
3637 if (chunk_len > SCTP_CHUNK_BUFFER_SIZE) {
3638 chunk_len = SCTP_CHUNK_BUFFER_SIZE;
3643 notif_len = (unsigned int)(sizeof(struct sctp_remote_error) + chunk_len);
3644 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3645 if (m_notify == NULL) {
3646 /* Retry with smaller value. */
3647 notif_len = (unsigned int)sizeof(struct sctp_remote_error);
3648 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3649 if (m_notify == NULL) {
3653 SCTP_BUF_NEXT(m_notify) = NULL;
3654 sre = mtod(m_notify, struct sctp_remote_error *);
3655 memset(sre, 0, notif_len);
3656 sre->sre_type = SCTP_REMOTE_ERROR;
3658 sre->sre_length = sizeof(struct sctp_remote_error);
3659 sre->sre_error = error;
3660 sre->sre_assoc_id = sctp_get_associd(stcb);
3661 if (notif_len > sizeof(struct sctp_remote_error)) {
3662 memcpy(sre->sre_data, chunk, chunk_len);
3663 sre->sre_length += chunk_len;
3665 SCTP_BUF_LEN(m_notify) = sre->sre_length;
3666 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3667 0, 0, stcb->asoc.context, 0, 0, 0,
3669 if (control != NULL) {
3670 control->length = SCTP_BUF_LEN(m_notify);
3671 control->spec_flags = M_NOTIFICATION;
3672 /* not that we need this */
3673 control->tail_mbuf = m_notify;
3674 sctp_add_to_readq(stcb->sctp_ep, stcb,
3676 &stcb->sctp_socket->so_rcv, 1,
3677 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3679 sctp_m_freem(m_notify);
3685 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3686 uint32_t error, void *data, int so_locked
3687 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3692 if ((stcb == NULL) ||
3693 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3694 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3695 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3696 /* If the socket is gone we are out of here */
3699 if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3702 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3703 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3704 if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
3705 (notification == SCTP_NOTIFY_INTERFACE_UP) ||
3706 (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
3707 /* Don't report these in front states */
3711 switch (notification) {
3712 case SCTP_NOTIFY_ASSOC_UP:
3713 if (stcb->asoc.assoc_up_sent == 0) {
3714 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
3715 stcb->asoc.assoc_up_sent = 1;
3717 if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
3718 sctp_notify_adaptation_layer(stcb);
3720 if (stcb->asoc.auth_supported == 0) {
3721 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3725 case SCTP_NOTIFY_ASSOC_DOWN:
3726 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
3728 case SCTP_NOTIFY_INTERFACE_DOWN:
3730 struct sctp_nets *net;
3732 net = (struct sctp_nets *)data;
3733 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3734 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3737 case SCTP_NOTIFY_INTERFACE_UP:
3739 struct sctp_nets *net;
3741 net = (struct sctp_nets *)data;
3742 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3743 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3746 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3748 struct sctp_nets *net;
3750 net = (struct sctp_nets *)data;
3751 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3752 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3755 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3756 sctp_notify_send_failed2(stcb, error,
3757 (struct sctp_stream_queue_pending *)data, so_locked);
3759 case SCTP_NOTIFY_SENT_DG_FAIL:
3760 sctp_notify_send_failed(stcb, 1, error,
3761 (struct sctp_tmit_chunk *)data, so_locked);
3763 case SCTP_NOTIFY_UNSENT_DG_FAIL:
3764 sctp_notify_send_failed(stcb, 0, error,
3765 (struct sctp_tmit_chunk *)data, so_locked);
3767 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3771 val = *((uint32_t *)data);
3773 sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3776 case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
3777 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3778 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3779 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
3781 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
3784 case SCTP_NOTIFY_ASSOC_REM_ABORTED:
3785 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3786 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3787 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
3789 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
3792 case SCTP_NOTIFY_ASSOC_RESTART:
3793 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
3794 if (stcb->asoc.auth_supported == 0) {
3795 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3799 case SCTP_NOTIFY_STR_RESET_SEND:
3800 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_OUTGOING_SSN);
3802 case SCTP_NOTIFY_STR_RESET_RECV:
3803 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_INCOMING);
3805 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3806 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3807 (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
3809 case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
3810 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3811 (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
3813 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3814 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3815 (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
3817 case SCTP_NOTIFY_STR_RESET_DENIED_IN:
3818 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3819 (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
3821 case SCTP_NOTIFY_ASCONF_ADD_IP:
3822 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3825 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3826 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3829 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3830 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3833 case SCTP_NOTIFY_PEER_SHUTDOWN:
3834 sctp_notify_shutdown_event(stcb);
3836 case SCTP_NOTIFY_AUTH_NEW_KEY:
3837 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
3838 (uint16_t)(uintptr_t)data,
3841 case SCTP_NOTIFY_AUTH_FREE_KEY:
3842 sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3843 (uint16_t)(uintptr_t)data,
3846 case SCTP_NOTIFY_NO_PEER_AUTH:
3847 sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3848 (uint16_t)(uintptr_t)data,
3851 case SCTP_NOTIFY_SENDER_DRY:
3852 sctp_notify_sender_dry_event(stcb, so_locked);
3854 case SCTP_NOTIFY_REMOTE_ERROR:
3855 sctp_notify_remote_error(stcb, error, data);
3858 SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3859 __func__, notification, notification);
3865 sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
3866 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3871 struct sctp_association *asoc;
3872 struct sctp_stream_out *outs;
3873 struct sctp_tmit_chunk *chk, *nchk;
3874 struct sctp_stream_queue_pending *sp, *nsp;
3881 if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3882 /* already being freed */
3885 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3886 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3887 (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
3890 /* now through all the gunk freeing chunks */
3891 if (holds_lock == 0) {
3892 SCTP_TCB_SEND_LOCK(stcb);
3894 /* sent queue SHOULD be empty */
3895 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3896 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3897 asoc->sent_queue_cnt--;
3898 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
3899 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
3900 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
3903 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
3907 if (chk->data != NULL) {
3908 sctp_free_bufspace(stcb, asoc, chk, 1);
3909 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
3910 error, chk, so_locked);
3912 sctp_m_freem(chk->data);
3916 sctp_free_a_chunk(stcb, chk, so_locked);
3917 /* sa_ignore FREED_MEMORY */
3919 /* pending send queue SHOULD be empty */
3920 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3921 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3922 asoc->send_queue_cnt--;
3923 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
3924 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
3927 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
3930 if (chk->data != NULL) {
3931 sctp_free_bufspace(stcb, asoc, chk, 1);
3932 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
3933 error, chk, so_locked);
3935 sctp_m_freem(chk->data);
3939 sctp_free_a_chunk(stcb, chk, so_locked);
3940 /* sa_ignore FREED_MEMORY */
3942 for (i = 0; i < asoc->streamoutcnt; i++) {
3943 /* For each stream */
3944 outs = &asoc->strmout[i];
3945 /* clean up any sends there */
3946 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
3947 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
3948 TAILQ_REMOVE(&outs->outqueue, sp, next);
3949 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
3950 sctp_free_spbufspace(stcb, asoc, sp);
3952 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3953 error, (void *)sp, so_locked);
3955 sctp_m_freem(sp->data);
3957 sp->tail_mbuf = NULL;
3962 sctp_free_remote_addr(sp->net);
3965 /* Free the chunk */
3966 sctp_free_a_strmoq(stcb, sp, so_locked);
3967 /* sa_ignore FREED_MEMORY */
3971 if (holds_lock == 0) {
3972 SCTP_TCB_SEND_UNLOCK(stcb);
3977 sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
3978 struct sctp_abort_chunk *abort, int so_locked
3979 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3987 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3988 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3989 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3990 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3992 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3993 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3994 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3997 /* Tell them we lost the asoc */
3998 sctp_report_all_outbound(stcb, error, 0, so_locked);
4000 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
4002 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
4007 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4008 struct mbuf *m, int iphlen,
4009 struct sockaddr *src, struct sockaddr *dst,
4010 struct sctphdr *sh, struct mbuf *op_err,
4011 uint8_t mflowtype, uint32_t mflowid,
4012 uint32_t vrf_id, uint16_t port)
4015 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4021 vtag = stcb->asoc.peer_vtag;
4022 vrf_id = stcb->asoc.vrf_id;
4024 sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
4025 mflowtype, mflowid, inp->fibnum,
4028 /* We have a TCB to abort, send notification too */
4029 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
4030 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4031 /* Ok, now lets free it */
4032 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4033 so = SCTP_INP_SO(inp);
4034 atomic_add_int(&stcb->asoc.refcnt, 1);
4035 SCTP_TCB_UNLOCK(stcb);
4036 SCTP_SOCKET_LOCK(so, 1);
4037 SCTP_TCB_LOCK(stcb);
4038 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4040 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4041 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4042 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4043 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4045 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4046 SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
4047 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4048 SCTP_SOCKET_UNLOCK(so, 1);
4052 #ifdef SCTP_ASOCLOG_OF_TSNS
4054 sctp_print_out_track_log(struct sctp_tcb *stcb)
4059 SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
4060 SCTP_PRINTF("IN bound TSN log-aaa\n");
4061 if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
4062 SCTP_PRINTF("None rcvd\n");
4065 if (stcb->asoc.tsn_in_wrapped) {
4066 for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
4067 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4068 stcb->asoc.in_tsnlog[i].tsn,
4069 stcb->asoc.in_tsnlog[i].strm,
4070 stcb->asoc.in_tsnlog[i].seq,
4071 stcb->asoc.in_tsnlog[i].flgs,
4072 stcb->asoc.in_tsnlog[i].sz);
4075 if (stcb->asoc.tsn_in_at) {
4076 for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
4077 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4078 stcb->asoc.in_tsnlog[i].tsn,
4079 stcb->asoc.in_tsnlog[i].strm,
4080 stcb->asoc.in_tsnlog[i].seq,
4081 stcb->asoc.in_tsnlog[i].flgs,
4082 stcb->asoc.in_tsnlog[i].sz);
4086 SCTP_PRINTF("OUT bound TSN log-aaa\n");
4087 if ((stcb->asoc.tsn_out_at == 0) &&
4088 (stcb->asoc.tsn_out_wrapped == 0)) {
4089 SCTP_PRINTF("None sent\n");
4091 if (stcb->asoc.tsn_out_wrapped) {
4092 for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4093 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4094 stcb->asoc.out_tsnlog[i].tsn,
4095 stcb->asoc.out_tsnlog[i].strm,
4096 stcb->asoc.out_tsnlog[i].seq,
4097 stcb->asoc.out_tsnlog[i].flgs,
4098 stcb->asoc.out_tsnlog[i].sz);
4101 if (stcb->asoc.tsn_out_at) {
4102 for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4103 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4104 stcb->asoc.out_tsnlog[i].tsn,
4105 stcb->asoc.out_tsnlog[i].strm,
4106 stcb->asoc.out_tsnlog[i].seq,
4107 stcb->asoc.out_tsnlog[i].flgs,
4108 stcb->asoc.out_tsnlog[i].sz);
4116 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4117 struct mbuf *op_err,
4119 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4124 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4128 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4129 so = SCTP_INP_SO(inp);
4132 /* Got to have a TCB */
4133 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4134 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4135 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4136 SCTP_CALLED_DIRECTLY_NOCMPSET);
4141 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4143 /* notify the peer */
4144 sctp_send_abort_tcb(stcb, op_err, so_locked);
4145 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4146 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4147 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4148 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4150 /* notify the ulp */
4151 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4152 sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4154 /* now free the asoc */
4155 #ifdef SCTP_ASOCLOG_OF_TSNS
4156 sctp_print_out_track_log(stcb);
4158 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4160 atomic_add_int(&stcb->asoc.refcnt, 1);
4161 SCTP_TCB_UNLOCK(stcb);
4162 SCTP_SOCKET_LOCK(so, 1);
4163 SCTP_TCB_LOCK(stcb);
4164 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4167 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4168 SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4169 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4171 SCTP_SOCKET_UNLOCK(so, 1);
4177 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4178 struct sockaddr *src, struct sockaddr *dst,
4179 struct sctphdr *sh, struct sctp_inpcb *inp,
4181 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4182 uint32_t vrf_id, uint16_t port)
4184 struct sctp_chunkhdr *ch, chunk_buf;
4185 unsigned int chk_length;
4186 int contains_init_chunk;
4188 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4189 /* Generate a TO address for future reference */
4190 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4191 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4192 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4193 SCTP_CALLED_DIRECTLY_NOCMPSET);
4196 contains_init_chunk = 0;
4197 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4198 sizeof(*ch), (uint8_t *)&chunk_buf);
4199 while (ch != NULL) {
4200 chk_length = ntohs(ch->chunk_length);
4201 if (chk_length < sizeof(*ch)) {
4202 /* break to abort land */
4205 switch (ch->chunk_type) {
4207 contains_init_chunk = 1;
4209 case SCTP_PACKET_DROPPED:
4210 /* we don't respond to pkt-dropped */
4212 case SCTP_ABORT_ASSOCIATION:
4213 /* we don't respond with an ABORT to an ABORT */
4215 case SCTP_SHUTDOWN_COMPLETE:
4217 * we ignore it since we are not waiting for it and
4221 case SCTP_SHUTDOWN_ACK:
4222 sctp_send_shutdown_complete2(src, dst, sh,
4223 mflowtype, mflowid, fibnum,
4229 offset += SCTP_SIZE32(chk_length);
4230 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4231 sizeof(*ch), (uint8_t *)&chunk_buf);
4233 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4234 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4235 (contains_init_chunk == 0))) {
4236 sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4237 mflowtype, mflowid, fibnum,
4243 * check the inbound datagram to make sure there is not an abort inside it,
4244 * if there is return 1, else return 0.
4247 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t *vtagfill)
4249 struct sctp_chunkhdr *ch;
4250 struct sctp_init_chunk *init_chk, chunk_buf;
4252 unsigned int chk_length;
4254 offset = iphlen + sizeof(struct sctphdr);
4255 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4256 (uint8_t *)&chunk_buf);
4257 while (ch != NULL) {
4258 chk_length = ntohs(ch->chunk_length);
4259 if (chk_length < sizeof(*ch)) {
4260 /* packet is probably corrupt */
4263 /* we seem to be ok, is it an abort? */
4264 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4265 /* yep, tell them */
4268 if (ch->chunk_type == SCTP_INITIATION) {
4269 /* need to update the Vtag */
4270 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4271 offset, sizeof(*init_chk), (uint8_t *)&chunk_buf);
4272 if (init_chk != NULL) {
4273 *vtagfill = ntohl(init_chk->init.initiate_tag);
4276 /* Nope, move to the next chunk */
4277 offset += SCTP_SIZE32(chk_length);
4278 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4279 sizeof(*ch), (uint8_t *)&chunk_buf);
4285 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4286 * set (i.e. it's 0) so, create this function to compare link local scopes
4290 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4292 struct sockaddr_in6 a, b;
4298 if (a.sin6_scope_id == 0)
4299 if (sa6_recoverscope(&a)) {
4300 /* can't get scope, so can't match */
4303 if (b.sin6_scope_id == 0)
4304 if (sa6_recoverscope(&b)) {
4305 /* can't get scope, so can't match */
4308 if (a.sin6_scope_id != b.sin6_scope_id)
4315 * returns a sockaddr_in6 with embedded scope recovered and removed
4317 struct sockaddr_in6 *
4318 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4320 /* check and strip embedded scope junk */
4321 if (addr->sin6_family == AF_INET6) {
4322 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4323 if (addr->sin6_scope_id == 0) {
4325 if (!sa6_recoverscope(store)) {
4326 /* use the recovered scope */
4330 /* else, return the original "to" addr */
4331 in6_clearscope(&addr->sin6_addr);
4340 * are the two addresses the same? currently a "scopeless" check returns: 1
4344 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4348 if (sa1 == NULL || sa2 == NULL)
4351 /* must be the same family */
4352 if (sa1->sa_family != sa2->sa_family)
4355 switch (sa1->sa_family) {
4359 /* IPv6 addresses */
4360 struct sockaddr_in6 *sin6_1, *sin6_2;
4362 sin6_1 = (struct sockaddr_in6 *)sa1;
4363 sin6_2 = (struct sockaddr_in6 *)sa2;
4364 return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4371 /* IPv4 addresses */
4372 struct sockaddr_in *sin_1, *sin_2;
4374 sin_1 = (struct sockaddr_in *)sa1;
4375 sin_2 = (struct sockaddr_in *)sa2;
4376 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
4380 /* we don't do these... */
4386 sctp_print_address(struct sockaddr *sa)
4389 char ip6buf[INET6_ADDRSTRLEN];
4392 switch (sa->sa_family) {
4396 struct sockaddr_in6 *sin6;
4398 sin6 = (struct sockaddr_in6 *)sa;
4399 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
4400 ip6_sprintf(ip6buf, &sin6->sin6_addr),
4401 ntohs(sin6->sin6_port),
4402 sin6->sin6_scope_id);
4409 struct sockaddr_in *sin;
4412 sin = (struct sockaddr_in *)sa;
4413 p = (unsigned char *)&sin->sin_addr;
4414 SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4415 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
4426 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4427 struct sctp_inpcb *new_inp,
4428 struct sctp_tcb *stcb,
4432 * go through our old INP and pull off any control structures that
4433 * belong to stcb and move then to the new inp.
4435 struct socket *old_so, *new_so;
4436 struct sctp_queued_to_read *control, *nctl;
4437 struct sctp_readhead tmp_queue;
4441 old_so = old_inp->sctp_socket;
4442 new_so = new_inp->sctp_socket;
4443 TAILQ_INIT(&tmp_queue);
4444 error = sblock(&old_so->so_rcv, waitflags);
4447 * Gak, can't get sblock, we have a problem. data will be
4448 * left stranded.. and we don't dare look at it since the
4449 * other thread may be reading something. Oh well, its a
4450 * screwed up app that does a peeloff OR a accept while
4451 * reading from the main socket... actually its only the
4452 * peeloff() case, since I think read will fail on a
4453 * listening socket..
4457 /* lock the socket buffers */
4458 SCTP_INP_READ_LOCK(old_inp);
4459 TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4460 /* Pull off all for out target stcb */
4461 if (control->stcb == stcb) {
4462 /* remove it we want it */
4463 TAILQ_REMOVE(&old_inp->read_queue, control, next);
4464 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4467 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4468 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
4470 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4471 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4472 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4474 m = SCTP_BUF_NEXT(m);
4478 SCTP_INP_READ_UNLOCK(old_inp);
4479 /* Remove the sb-lock on the old socket */
4481 sbunlock(&old_so->so_rcv);
4482 /* Now we move them over to the new socket buffer */
4483 SCTP_INP_READ_LOCK(new_inp);
4484 TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4485 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4488 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4489 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4491 sctp_sballoc(stcb, &new_so->so_rcv, m);
4492 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4493 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4495 m = SCTP_BUF_NEXT(m);
4498 SCTP_INP_READ_UNLOCK(new_inp);
4502 sctp_wakeup_the_read_socket(struct sctp_inpcb *inp,
4503 struct sctp_tcb *stcb,
4505 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4510 if ((inp != NULL) && (inp->sctp_socket != NULL)) {
4511 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4514 so = SCTP_INP_SO(inp);
4517 atomic_add_int(&stcb->asoc.refcnt, 1);
4518 SCTP_TCB_UNLOCK(stcb);
4520 SCTP_SOCKET_LOCK(so, 1);
4522 SCTP_TCB_LOCK(stcb);
4523 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4525 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4526 SCTP_SOCKET_UNLOCK(so, 1);
4531 sctp_sorwakeup(inp, inp->sctp_socket);
4532 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4534 SCTP_SOCKET_UNLOCK(so, 1);
4541 sctp_add_to_readq(struct sctp_inpcb *inp,
4542 struct sctp_tcb *stcb,
4543 struct sctp_queued_to_read *control,
4546 int inp_read_lock_held,
4548 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4554 * Here we must place the control on the end of the socket read
4555 * queue AND increment sb_cc so that select will work properly on
4558 struct mbuf *m, *prev = NULL;
4563 panic("Gak, inp NULL on add_to_readq");
4567 if (inp_read_lock_held == 0)
4568 SCTP_INP_READ_LOCK(inp);
4569 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4570 if (!control->on_strm_q) {
4571 sctp_free_remote_addr(control->whoFrom);
4572 if (control->data) {
4573 sctp_m_freem(control->data);
4574 control->data = NULL;
4576 sctp_free_a_readq(stcb, control);
4578 if (inp_read_lock_held == 0)
4579 SCTP_INP_READ_UNLOCK(inp);
4582 if (!(control->spec_flags & M_NOTIFICATION)) {
4583 atomic_add_int(&inp->total_recvs, 1);
4584 if (!control->do_not_ref_stcb) {
4585 atomic_add_int(&stcb->total_recvs, 1);
4589 control->held_length = 0;
4590 control->length = 0;
4592 if (SCTP_BUF_LEN(m) == 0) {
4593 /* Skip mbufs with NO length */
4596 control->data = sctp_m_free(m);
4599 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4600 m = SCTP_BUF_NEXT(prev);
4603 control->tail_mbuf = prev;
4608 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4609 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4611 sctp_sballoc(stcb, sb, m);
4612 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4613 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4615 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4616 m = SCTP_BUF_NEXT(m);
4619 control->tail_mbuf = prev;
4621 /* Everything got collapsed out?? */
4622 if (!control->on_strm_q) {
4623 sctp_free_remote_addr(control->whoFrom);
4624 sctp_free_a_readq(stcb, control);
4626 if (inp_read_lock_held == 0)
4627 SCTP_INP_READ_UNLOCK(inp);
4631 control->end_added = 1;
4633 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4634 control->on_read_q = 1;
4635 if (inp_read_lock_held == 0)
4636 SCTP_INP_READ_UNLOCK(inp);
4637 if (inp && inp->sctp_socket) {
4638 sctp_wakeup_the_read_socket(inp, stcb, so_locked);
4642 /*************HOLD THIS COMMENT FOR PATCH FILE OF
4643 *************ALTERNATE ROUTING CODE
4646 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4647 *************ALTERNATE ROUTING CODE
4651 sctp_generate_cause(uint16_t code, char *info)
4654 struct sctp_gen_error_cause *cause;
4658 if ((code == 0) || (info == NULL)) {
4661 info_len = strlen(info);
4662 if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
4665 len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
4666 m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4668 SCTP_BUF_LEN(m) = len;
4669 cause = mtod(m, struct sctp_gen_error_cause *);
4670 cause->code = htons(code);
4671 cause->length = htons(len);
4672 memcpy(cause->info, info, info_len);
4678 sctp_generate_no_user_data_cause(uint32_t tsn)
4681 struct sctp_error_no_user_data *no_user_data_cause;
4684 len = (uint16_t)sizeof(struct sctp_error_no_user_data);
4685 m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4687 SCTP_BUF_LEN(m) = len;
4688 no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
4689 no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
4690 no_user_data_cause->cause.length = htons(len);
4691 no_user_data_cause->tsn = htonl(tsn);
4696 #ifdef SCTP_MBCNT_LOGGING
4698 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4699 struct sctp_tmit_chunk *tp1, int chk_cnt)
4701 if (tp1->data == NULL) {
4704 asoc->chunks_on_out_queue -= chk_cnt;
4705 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4706 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4707 asoc->total_output_queue_size,
4712 if (asoc->total_output_queue_size >= tp1->book_size) {
4713 atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4715 asoc->total_output_queue_size = 0;
4718 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4719 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4720 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4721 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4723 stcb->sctp_socket->so_snd.sb_cc = 0;
4732 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4733 uint8_t sent, int so_locked
4734 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4739 struct sctp_stream_out *strq;
4740 struct sctp_tmit_chunk *chk = NULL, *tp2;
4741 struct sctp_stream_queue_pending *sp;
4744 uint8_t foundeom = 0;
4747 int do_wakeup_routine = 0;
4749 sid = tp1->rec.data.sid;
4750 mid = tp1->rec.data.mid;
4751 if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
4752 stcb->asoc.abandoned_sent[0]++;
4753 stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4754 stcb->asoc.strmout[sid].abandoned_sent[0]++;
4755 #if defined(SCTP_DETAILED_STR_STATS)
4756 stcb->asoc.strmout[sid].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4759 stcb->asoc.abandoned_unsent[0]++;
4760 stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4761 stcb->asoc.strmout[sid].abandoned_unsent[0]++;
4762 #if defined(SCTP_DETAILED_STR_STATS)
4763 stcb->asoc.strmout[sid].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4767 ret_sz += tp1->book_size;
4768 if (tp1->data != NULL) {
4769 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4770 sctp_flight_size_decrease(tp1);
4771 sctp_total_flight_decrease(stcb, tp1);
4773 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4774 stcb->asoc.peers_rwnd += tp1->send_size;
4775 stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
4777 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
4779 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
4782 sctp_m_freem(tp1->data);
4785 do_wakeup_routine = 1;
4786 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4787 stcb->asoc.sent_queue_cnt_removeable--;
4790 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4791 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4792 SCTP_DATA_NOT_FRAG) {
4793 /* not frag'ed we ae done */
4796 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4797 /* end of frag, we are done */
4802 * Its a begin or middle piece, we must mark all of
4806 tp1 = TAILQ_NEXT(tp1, sctp_next);
4808 } while (tp1 && notdone);
4809 if (foundeom == 0) {
4811 * The multi-part message was scattered across the send and
4814 TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
4815 if ((tp1->rec.data.sid != sid) ||
4816 (!SCTP_MID_EQ(stcb->asoc.idata_supported, tp1->rec.data.mid, mid))) {
4820 * save to chk in case we have some on stream out
4821 * queue. If so and we have an un-transmitted one we
4822 * don't have to fudge the TSN.
4825 ret_sz += tp1->book_size;
4826 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4828 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
4830 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
4833 sctp_m_freem(tp1->data);
4836 /* No flight involved here book the size to 0 */
4838 if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4841 do_wakeup_routine = 1;
4842 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4843 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4845 * on to the sent queue so we can wait for it to be
4848 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4850 stcb->asoc.send_queue_cnt--;
4851 stcb->asoc.sent_queue_cnt++;
4854 if (foundeom == 0) {
4856 * Still no eom found. That means there is stuff left on the
4857 * stream out queue.. yuck.
4859 SCTP_TCB_SEND_LOCK(stcb);
4860 strq = &stcb->asoc.strmout[sid];
4861 sp = TAILQ_FIRST(&strq->outqueue);
4863 sp->discard_rest = 1;
4865 * We may need to put a chunk on the queue that
4866 * holds the TSN that would have been sent with the
4870 /* Yep, we have to */
4871 sctp_alloc_a_chunk(stcb, chk);
4874 * we are hosed. All we can do is
4875 * nothing.. which will cause an
4876 * abort if the peer is paying
4881 memset(chk, 0, sizeof(*chk));
4882 chk->rec.data.rcv_flags = 0;
4883 chk->sent = SCTP_FORWARD_TSN_SKIP;
4884 chk->asoc = &stcb->asoc;
4885 if (stcb->asoc.idata_supported == 0) {
4886 if (sp->sinfo_flags & SCTP_UNORDERED) {
4887 chk->rec.data.mid = 0;
4889 chk->rec.data.mid = strq->next_mid_ordered;
4892 if (sp->sinfo_flags & SCTP_UNORDERED) {
4893 chk->rec.data.mid = strq->next_mid_unordered;
4895 chk->rec.data.mid = strq->next_mid_ordered;
4898 chk->rec.data.sid = sp->sid;
4899 chk->rec.data.ppid = sp->ppid;
4900 chk->rec.data.context = sp->context;
4901 chk->flags = sp->act_flags;
4903 chk->rec.data.tsn = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
4904 strq->chunks_on_queues++;
4905 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
4906 stcb->asoc.sent_queue_cnt++;
4907 stcb->asoc.pr_sctp_cnt++;
4909 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4910 if (sp->sinfo_flags & SCTP_UNORDERED) {
4911 chk->rec.data.rcv_flags |= SCTP_DATA_UNORDERED;
4913 if (stcb->asoc.idata_supported == 0) {
4914 if ((sp->sinfo_flags & SCTP_UNORDERED) == 0) {
4915 strq->next_mid_ordered++;
4918 if (sp->sinfo_flags & SCTP_UNORDERED) {
4919 strq->next_mid_unordered++;
4921 strq->next_mid_ordered++;
4927 * Pull any data to free up the SB and allow
4928 * sender to "add more" while we will throw
4931 sctp_free_spbufspace(stcb, &stcb->asoc, sp);
4932 ret_sz += sp->length;
4933 do_wakeup_routine = 1;
4935 sctp_m_freem(sp->data);
4937 sp->tail_mbuf = NULL;
4941 SCTP_TCB_SEND_UNLOCK(stcb);
4943 if (do_wakeup_routine) {
4944 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4947 so = SCTP_INP_SO(stcb->sctp_ep);
4949 atomic_add_int(&stcb->asoc.refcnt, 1);
4950 SCTP_TCB_UNLOCK(stcb);
4951 SCTP_SOCKET_LOCK(so, 1);
4952 SCTP_TCB_LOCK(stcb);
4953 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4954 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4955 /* assoc was freed while we were unlocked */
4956 SCTP_SOCKET_UNLOCK(so, 1);
4961 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
4962 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4964 SCTP_SOCKET_UNLOCK(so, 1);
4972 * checks to see if the given address, sa, is one that is currently known by
4973 * the kernel note: can't distinguish the same address on multiple interfaces
4974 * and doesn't handle multiple addresses with different zone/scope id's note:
4975 * ifa_ifwithaddr() compares the entire sockaddr struct
4978 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
4981 struct sctp_laddr *laddr;
4983 if (holds_lock == 0) {
4984 SCTP_INP_RLOCK(inp);
4987 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4988 if (laddr->ifa == NULL)
4990 if (addr->sa_family != laddr->ifa->address.sa.sa_family)
4993 if (addr->sa_family == AF_INET) {
4994 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4995 laddr->ifa->address.sin.sin_addr.s_addr) {
4997 if (holds_lock == 0) {
4998 SCTP_INP_RUNLOCK(inp);
5000 return (laddr->ifa);
5006 if (addr->sa_family == AF_INET6) {
5007 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5008 &laddr->ifa->address.sin6)) {
5010 if (holds_lock == 0) {
5011 SCTP_INP_RUNLOCK(inp);
5013 return (laddr->ifa);
5019 if (holds_lock == 0) {
5020 SCTP_INP_RUNLOCK(inp);
5026 sctp_get_ifa_hash_val(struct sockaddr *addr)
5028 switch (addr->sa_family) {
5032 struct sockaddr_in *sin;
5034 sin = (struct sockaddr_in *)addr;
5035 return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5041 struct sockaddr_in6 *sin6;
5042 uint32_t hash_of_addr;
5044 sin6 = (struct sockaddr_in6 *)addr;
5045 hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
5046 sin6->sin6_addr.s6_addr32[1] +
5047 sin6->sin6_addr.s6_addr32[2] +
5048 sin6->sin6_addr.s6_addr32[3]);
5049 hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
5050 return (hash_of_addr);
5060 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
5062 struct sctp_ifa *sctp_ifap;
5063 struct sctp_vrf *vrf;
5064 struct sctp_ifalist *hash_head;
5065 uint32_t hash_of_addr;
5067 if (holds_lock == 0)
5068 SCTP_IPI_ADDR_RLOCK();
5070 vrf = sctp_find_vrf(vrf_id);
5072 if (holds_lock == 0)
5073 SCTP_IPI_ADDR_RUNLOCK();
5077 hash_of_addr = sctp_get_ifa_hash_val(addr);
5079 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5080 if (hash_head == NULL) {
5081 SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5082 hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
5083 (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
5084 sctp_print_address(addr);
5085 SCTP_PRINTF("No such bucket for address\n");
5086 if (holds_lock == 0)
5087 SCTP_IPI_ADDR_RUNLOCK();
5091 LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5092 if (addr->sa_family != sctp_ifap->address.sa.sa_family)
5095 if (addr->sa_family == AF_INET) {
5096 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5097 sctp_ifap->address.sin.sin_addr.s_addr) {
5099 if (holds_lock == 0)
5100 SCTP_IPI_ADDR_RUNLOCK();
5107 if (addr->sa_family == AF_INET6) {
5108 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5109 &sctp_ifap->address.sin6)) {
5111 if (holds_lock == 0)
5112 SCTP_IPI_ADDR_RUNLOCK();
5119 if (holds_lock == 0)
5120 SCTP_IPI_ADDR_RUNLOCK();
5125 sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
5128 /* User pulled some data, do we need a rwnd update? */
5131 struct socket *so = NULL;
5136 atomic_add_int(&stcb->asoc.refcnt, 1);
5138 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5139 (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | SCTP_STATE_SHUTDOWN_RECEIVED))) {
5140 /* Pre-check If we are freeing no update */
5143 SCTP_INP_INCR_REF(stcb->sctp_ep);
5144 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5145 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5148 so = stcb->sctp_socket;
5152 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5153 /* Have you have freed enough to look */
5155 /* Yep, its worth a look and the lock overhead */
5157 /* Figure out what the rwnd would be */
5158 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5159 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5160 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5164 if (dif >= rwnd_req) {
5166 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5169 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5171 * One last check before we allow the guy possibly
5172 * to get in. There is a race, where the guy has not
5173 * reached the gate. In that case
5177 SCTP_TCB_LOCK(stcb);
5178 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5179 /* No reports here */
5180 SCTP_TCB_UNLOCK(stcb);
5183 SCTP_STAT_INCR(sctps_wu_sacks_sent);
5184 sctp_send_sack(stcb, SCTP_SO_LOCKED);
5186 sctp_chunk_output(stcb->sctp_ep, stcb,
5187 SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5188 /* make sure no timer is running */
5189 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
5190 SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5191 SCTP_TCB_UNLOCK(stcb);
5193 /* Update how much we have pending */
5194 stcb->freed_by_sorcv_sincelast = dif;
5197 if (so && r_unlocked && hold_rlock) {
5198 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5201 SCTP_INP_DECR_REF(stcb->sctp_ep);
5203 atomic_add_int(&stcb->asoc.refcnt, -1);
5208 sctp_sorecvmsg(struct socket *so,
5211 struct sockaddr *from,
5214 struct sctp_sndrcvinfo *sinfo,
5218 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5219 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5220 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5221 * On the way out we may send out any combination of:
5222 * MSG_NOTIFICATION MSG_EOR
5225 struct sctp_inpcb *inp = NULL;
5229 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
5230 struct mbuf *m = NULL;
5231 struct sctp_tcb *stcb = NULL;
5232 int wakeup_read_socket = 0;
5233 int freecnt_applied = 0;
5234 int out_flags = 0, in_flags = 0;
5235 int block_allowed = 1;
5236 uint32_t freed_so_far = 0;
5237 ssize_t copied_so_far = 0;
5238 int in_eeor_mode = 0;
5239 int no_rcv_needed = 0;
5240 uint32_t rwnd_req = 0;
5241 int hold_sblock = 0;
5244 uint32_t held_length = 0;
5245 int sockbuf_lock = 0;
5248 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5253 in_flags = *msg_flags;
5254 if (in_flags & MSG_PEEK)
5255 SCTP_STAT_INCR(sctps_read_peeks);
5259 slen = uio->uio_resid;
5261 /* Pull in and set up our int flags */
5262 if (in_flags & MSG_OOB) {
5263 /* Out of band's NOT supported */
5264 return (EOPNOTSUPP);
5266 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5267 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5270 if ((in_flags & (MSG_DONTWAIT
5273 SCTP_SO_IS_NBIO(so)) {
5276 /* setup the endpoint */
5277 inp = (struct sctp_inpcb *)so->so_pcb;
5279 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5282 rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5283 /* Must be at least a MTU's worth */
5284 if (rwnd_req < SCTP_MIN_RWND)
5285 rwnd_req = SCTP_MIN_RWND;
5286 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5287 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5288 sctp_misc_ints(SCTP_SORECV_ENTER,
5289 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
5291 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5292 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5293 rwnd_req, block_allowed, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
5297 error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5299 goto release_unlocked;
5306 if (hold_sblock == 0) {
5307 SOCKBUF_LOCK(&so->so_rcv);
5310 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5311 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5314 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5316 error = so->so_error;
5317 if ((in_flags & MSG_PEEK) == 0)
5321 if (so->so_rcv.sb_cc == 0) {
5328 if (so->so_rcv.sb_cc <= held_length) {
5330 error = so->so_error;
5331 if ((in_flags & MSG_PEEK) == 0) {
5336 if ((so->so_rcv.sb_cc == 0) &&
5337 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5338 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5339 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5341 * For active open side clear flags for
5342 * re-use passive open is blocked by
5345 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5347 * You were aborted, passive side
5350 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5353 so->so_state &= ~(SS_ISCONNECTING |
5354 SS_ISDISCONNECTING |
5358 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5359 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5366 if (block_allowed) {
5367 error = sbwait(&so->so_rcv);
5372 goto restart_nosblocks;
5374 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5375 error = EWOULDBLOCK;
5379 if (hold_sblock == 1) {
5380 SOCKBUF_UNLOCK(&so->so_rcv);
5383 /* we possibly have data we can read */
5384 /* sa_ignore FREED_MEMORY */
5385 control = TAILQ_FIRST(&inp->read_queue);
5386 if (control == NULL) {
5388 * This could be happening since the appender did the
5389 * increment but as not yet did the tailq insert onto the
5392 if (hold_rlock == 0) {
5393 SCTP_INP_READ_LOCK(inp);
5395 control = TAILQ_FIRST(&inp->read_queue);
5396 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5398 panic("Huh, its non zero and nothing on control?");
5400 so->so_rcv.sb_cc = 0;
5402 SCTP_INP_READ_UNLOCK(inp);
5407 if ((control->length == 0) &&
5408 (control->do_not_ref_stcb)) {
5410 * Clean up code for freeing assoc that left behind a
5411 * pdapi.. maybe a peer in EEOR that just closed after
5412 * sending and never indicated a EOR.
5414 if (hold_rlock == 0) {
5416 SCTP_INP_READ_LOCK(inp);
5418 control->held_length = 0;
5419 if (control->data) {
5420 /* Hmm there is data here .. fix */
5424 m_tmp = control->data;
5426 cnt += SCTP_BUF_LEN(m_tmp);
5427 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5428 control->tail_mbuf = m_tmp;
5429 control->end_added = 1;
5431 m_tmp = SCTP_BUF_NEXT(m_tmp);
5433 control->length = cnt;
5436 TAILQ_REMOVE(&inp->read_queue, control, next);
5437 /* Add back any hiddend data */
5438 sctp_free_remote_addr(control->whoFrom);
5439 sctp_free_a_readq(stcb, control);
5443 SCTP_INP_READ_UNLOCK(inp);
5447 if ((control->length == 0) &&
5448 (control->end_added == 1)) {
5450 * Do we also need to check for (control->pdapi_aborted ==
5453 if (hold_rlock == 0) {
5455 SCTP_INP_READ_LOCK(inp);
5457 TAILQ_REMOVE(&inp->read_queue, control, next);
5458 if (control->data) {
5460 panic("control->data not null but control->length == 0");
5462 SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5463 sctp_m_freem(control->data);
5464 control->data = NULL;
5467 if (control->aux_data) {
5468 sctp_m_free(control->aux_data);
5469 control->aux_data = NULL;
5472 if (control->on_strm_q) {
5473 panic("About to free ctl:%p so:%p and its in %d",
5474 control, so, control->on_strm_q);
5477 sctp_free_remote_addr(control->whoFrom);
5478 sctp_free_a_readq(stcb, control);
5481 SCTP_INP_READ_UNLOCK(inp);
5485 if (control->length == 0) {
5486 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5488 /* find a more suitable one then this */
5489 ctl = TAILQ_NEXT(control, next);
5491 if ((ctl->stcb != control->stcb) && (ctl->length) &&
5493 (ctl->spec_flags & M_NOTIFICATION) ||
5494 ((ctl->do_not_ref_stcb == 0) &&
5495 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
5498 * If we have a different TCB next, and there is data
5499 * present. If we have already taken some (pdapi), OR we can
5500 * ref the tcb and no delivery as started on this stream, we
5501 * take it. Note we allow a notification on a different
5502 * assoc to be delivered..
5506 } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
5508 ((ctl->some_taken) ||
5509 ((ctl->do_not_ref_stcb == 0) &&
5510 ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5511 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
5513 * If we have the same tcb, and there is data present, and we
5514 * have the strm interleave feature present. Then if we have
5515 * taken some (pdapi) or we can refer to tht tcb AND we have
5516 * not started a delivery for this stream, we can take it.
5517 * Note we do NOT allow a notificaiton on the same assoc to
5523 ctl = TAILQ_NEXT(ctl, next);
5527 * if we reach here, not suitable replacement is available
5528 * <or> fragment interleave is NOT on. So stuff the sb_cc
5529 * into the our held count, and its time to sleep again.
5531 held_length = so->so_rcv.sb_cc;
5532 control->held_length = so->so_rcv.sb_cc;
5535 /* Clear the held length since there is something to read */
5536 control->held_length = 0;
5539 * If we reach here, control has a some data for us to read off.
5540 * Note that stcb COULD be NULL.
5542 if (hold_rlock == 0) {
5544 SCTP_INP_READ_LOCK(inp);
5546 control->some_taken++;
5547 stcb = control->stcb;
5549 if ((control->do_not_ref_stcb == 0) &&
5550 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5551 if (freecnt_applied == 0)
5553 } else if (control->do_not_ref_stcb == 0) {
5554 /* you can't free it on me please */
5556 * The lock on the socket buffer protects us so the
5557 * free code will stop. But since we used the
5558 * socketbuf lock and the sender uses the tcb_lock
5559 * to increment, we need to use the atomic add to
5562 if (freecnt_applied) {
5564 panic("refcnt already incremented");
5566 SCTP_PRINTF("refcnt already incremented?\n");
5569 atomic_add_int(&stcb->asoc.refcnt, 1);
5570 freecnt_applied = 1;
5573 * Setup to remember how much we have not yet told
5574 * the peer our rwnd has opened up. Note we grab the
5575 * value from the tcb from last time. Note too that
5576 * sack sending clears this when a sack is sent,
5577 * which is fine. Once we hit the rwnd_req, we then
5578 * will go to the sctp_user_rcvd() that will not
5579 * lock until it KNOWs it MUST send a WUP-SACK.
5581 freed_so_far = (uint32_t)stcb->freed_by_sorcv_sincelast;
5582 stcb->freed_by_sorcv_sincelast = 0;
5586 ((control->spec_flags & M_NOTIFICATION) == 0) &&
5587 control->do_not_ref_stcb == 0) {
5588 stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5591 /* First lets get off the sinfo and sockaddr info */
5592 if ((sinfo != NULL) && (filling_sinfo != 0)) {
5593 sinfo->sinfo_stream = control->sinfo_stream;
5594 sinfo->sinfo_ssn = (uint16_t)control->mid;
5595 sinfo->sinfo_flags = control->sinfo_flags;
5596 sinfo->sinfo_ppid = control->sinfo_ppid;
5597 sinfo->sinfo_context = control->sinfo_context;
5598 sinfo->sinfo_timetolive = control->sinfo_timetolive;
5599 sinfo->sinfo_tsn = control->sinfo_tsn;
5600 sinfo->sinfo_cumtsn = control->sinfo_cumtsn;
5601 sinfo->sinfo_assoc_id = control->sinfo_assoc_id;
5602 nxt = TAILQ_NEXT(control, next);
5603 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
5604 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
5605 struct sctp_extrcvinfo *s_extra;
5607 s_extra = (struct sctp_extrcvinfo *)sinfo;
5610 s_extra->serinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5611 if (nxt->sinfo_flags & SCTP_UNORDERED) {
5612 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5614 if (nxt->spec_flags & M_NOTIFICATION) {
5615 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5617 s_extra->serinfo_next_aid = nxt->sinfo_assoc_id;
5618 s_extra->serinfo_next_length = nxt->length;
5619 s_extra->serinfo_next_ppid = nxt->sinfo_ppid;
5620 s_extra->serinfo_next_stream = nxt->sinfo_stream;
5621 if (nxt->tail_mbuf != NULL) {
5622 if (nxt->end_added) {
5623 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5628 * we explicitly 0 this, since the memcpy
5629 * got some other things beyond the older
5630 * sinfo_ that is on the control's structure
5634 s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
5635 s_extra->serinfo_next_aid = 0;
5636 s_extra->serinfo_next_length = 0;
5637 s_extra->serinfo_next_ppid = 0;
5638 s_extra->serinfo_next_stream = 0;
5642 * update off the real current cum-ack, if we have an stcb.
5644 if ((control->do_not_ref_stcb == 0) && stcb)
5645 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5647 * mask off the high bits, we keep the actual chunk bits in
5650 sinfo->sinfo_flags &= 0x00ff;
5651 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
5652 sinfo->sinfo_flags |= SCTP_UNORDERED;
5655 #ifdef SCTP_ASOCLOG_OF_TSNS
5657 int index, newindex;
5658 struct sctp_pcbtsn_rlog *entry;
5661 index = inp->readlog_index;
5662 newindex = index + 1;
5663 if (newindex >= SCTP_READ_LOG_SIZE) {
5666 } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
5667 entry = &inp->readlog[index];
5668 entry->vtag = control->sinfo_assoc_id;
5669 entry->strm = control->sinfo_stream;
5670 entry->seq = (uint16_t)control->mid;
5671 entry->sz = control->length;
5672 entry->flgs = control->sinfo_flags;
5675 if ((fromlen > 0) && (from != NULL)) {
5676 union sctp_sockstore store;
5679 switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5682 len = sizeof(struct sockaddr_in6);
5683 store.sin6 = control->whoFrom->ro._l_addr.sin6;
5684 store.sin6.sin6_port = control->port_from;
5690 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
5691 len = sizeof(struct sockaddr_in6);
5692 in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
5694 store.sin6.sin6_port = control->port_from;
5696 len = sizeof(struct sockaddr_in);
5697 store.sin = control->whoFrom->ro._l_addr.sin;
5698 store.sin.sin_port = control->port_from;
5701 len = sizeof(struct sockaddr_in);
5702 store.sin = control->whoFrom->ro._l_addr.sin;
5703 store.sin.sin_port = control->port_from;
5711 memcpy(from, &store, min((size_t)fromlen, len));
5714 struct sockaddr_in6 lsa6, *from6;
5716 from6 = (struct sockaddr_in6 *)from;
5717 sctp_recover_scope_mac(from6, (&lsa6));
5722 SCTP_INP_READ_UNLOCK(inp);
5726 SOCKBUF_UNLOCK(&so->so_rcv);
5729 /* now copy out what data we can */
5731 /* copy out each mbuf in the chain up to length */
5735 /* Move out all we can */
5736 cp_len = uio->uio_resid;
5737 my_len = SCTP_BUF_LEN(m);
5738 if (cp_len > my_len) {
5739 /* not enough in this buf */
5743 SCTP_INP_READ_UNLOCK(inp);
5747 error = uiomove(mtod(m, char *), (int)cp_len, uio);
5749 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5753 if ((control->do_not_ref_stcb == 0) && stcb &&
5754 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5758 /* error we are out of here */
5761 SCTP_INP_READ_LOCK(inp);
5763 if (cp_len == SCTP_BUF_LEN(m)) {
5764 if ((SCTP_BUF_NEXT(m) == NULL) &&
5765 (control->end_added)) {
5766 out_flags |= MSG_EOR;
5767 if ((control->do_not_ref_stcb == 0) &&
5768 (control->stcb != NULL) &&
5769 ((control->spec_flags & M_NOTIFICATION) == 0))
5770 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5772 if (control->spec_flags & M_NOTIFICATION) {
5773 out_flags |= MSG_NOTIFICATION;
5775 /* we ate up the mbuf */
5776 if (in_flags & MSG_PEEK) {
5778 m = SCTP_BUF_NEXT(m);
5779 copied_so_far += cp_len;
5781 /* dispose of the mbuf */
5782 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5783 sctp_sblog(&so->so_rcv,
5784 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5786 sctp_sbfree(control, stcb, &so->so_rcv, m);
5787 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5788 sctp_sblog(&so->so_rcv,
5789 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5791 copied_so_far += cp_len;
5792 freed_so_far += (uint32_t)cp_len;
5793 freed_so_far += MSIZE;
5794 atomic_subtract_int(&control->length, cp_len);
5795 control->data = sctp_m_free(m);
5798 * been through it all, must hold sb
5799 * lock ok to null tail
5801 if (control->data == NULL) {
5803 if ((control->end_added == 0) ||
5804 (TAILQ_NEXT(control, next) == NULL)) {
5812 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5813 panic("Hmm we don't own the lock?");
5817 control->tail_mbuf = NULL;
5819 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5820 panic("end_added, nothing left and no MSG_EOR");
5826 /* Do we need to trim the mbuf? */
5827 if (control->spec_flags & M_NOTIFICATION) {
5828 out_flags |= MSG_NOTIFICATION;
5830 if ((in_flags & MSG_PEEK) == 0) {
5831 SCTP_BUF_RESV_UF(m, cp_len);
5832 SCTP_BUF_LEN(m) -= (int)cp_len;
5833 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5834 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, (int)cp_len);
5836 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5837 if ((control->do_not_ref_stcb == 0) &&
5839 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5841 copied_so_far += cp_len;
5842 freed_so_far += (uint32_t)cp_len;
5843 freed_so_far += MSIZE;
5844 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5845 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5846 SCTP_LOG_SBRESULT, 0);
5848 atomic_subtract_int(&control->length, cp_len);
5850 copied_so_far += cp_len;
5853 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5856 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5857 (control->do_not_ref_stcb == 0) &&
5858 (freed_so_far >= rwnd_req)) {
5859 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5861 } /* end while(m) */
5863 * At this point we have looked at it all and we either have
5864 * a MSG_EOR/or read all the user wants... <OR>
5865 * control->length == 0.
5867 if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5868 /* we are done with this control */
5869 if (control->length == 0) {
5870 if (control->data) {
5872 panic("control->data not null at read eor?");
5874 SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5875 sctp_m_freem(control->data);
5876 control->data = NULL;
5880 if (hold_rlock == 0) {
5881 SCTP_INP_READ_LOCK(inp);
5884 TAILQ_REMOVE(&inp->read_queue, control, next);
5885 /* Add back any hiddend data */
5886 if (control->held_length) {
5888 control->held_length = 0;
5889 wakeup_read_socket = 1;
5891 if (control->aux_data) {
5892 sctp_m_free(control->aux_data);
5893 control->aux_data = NULL;
5895 no_rcv_needed = control->do_not_ref_stcb;
5896 sctp_free_remote_addr(control->whoFrom);
5897 control->data = NULL;
5899 if (control->on_strm_q) {
5900 panic("About to free ctl:%p so:%p and its in %d",
5901 control, so, control->on_strm_q);
5904 sctp_free_a_readq(stcb, control);
5906 if ((freed_so_far >= rwnd_req) &&
5907 (no_rcv_needed == 0))
5908 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5912 * The user did not read all of this
5913 * message, turn off the returned MSG_EOR
5914 * since we are leaving more behind on the
5918 if (control->end_added &&
5919 (control->data == NULL) &&
5920 (control->tail_mbuf == NULL)) {
5921 panic("Gak, control->length is corrupt?");
5924 no_rcv_needed = control->do_not_ref_stcb;
5925 out_flags &= ~MSG_EOR;
5928 if (out_flags & MSG_EOR) {
5931 if ((uio->uio_resid == 0) ||
5933 (copied_so_far >= (uint32_t)max(so->so_rcv.sb_lowat, 1)))) {
5937 * If I hit here the receiver wants more and this message is
5938 * NOT done (pd-api). So two questions. Can we block? if not
5939 * we are done. Did the user NOT set MSG_WAITALL?
5941 if (block_allowed == 0) {
5945 * We need to wait for more data a few things: - We don't
5946 * sbunlock() so we don't get someone else reading. - We
5947 * must be sure to account for the case where what is added
5948 * is NOT to our control when we wakeup.
5952 * Do we need to tell the transport a rwnd update might be
5953 * needed before we go to sleep?
5955 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5956 ((freed_so_far >= rwnd_req) &&
5957 (control->do_not_ref_stcb == 0) &&
5958 (no_rcv_needed == 0))) {
5959 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5962 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5966 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5969 if (hold_rlock == 1) {
5970 SCTP_INP_READ_UNLOCK(inp);
5973 if (hold_sblock == 0) {
5974 SOCKBUF_LOCK(&so->so_rcv);
5977 if ((copied_so_far) && (control->length == 0) &&
5978 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
5981 if (so->so_rcv.sb_cc <= control->held_length) {
5982 error = sbwait(&so->so_rcv);
5986 control->held_length = 0;
5989 SOCKBUF_UNLOCK(&so->so_rcv);
5992 if (control->length == 0) {
5993 /* still nothing here */
5994 if (control->end_added == 1) {
5995 /* he aborted, or is done i.e.did a shutdown */
5996 out_flags |= MSG_EOR;
5997 if (control->pdapi_aborted) {
5998 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5999 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6001 out_flags |= MSG_TRUNC;
6003 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6004 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6006 goto done_with_control;
6008 if (so->so_rcv.sb_cc > held_length) {
6009 control->held_length = so->so_rcv.sb_cc;
6012 goto wait_some_more;
6013 } else if (control->data == NULL) {
6015 * we must re-sync since data is probably being
6018 SCTP_INP_READ_LOCK(inp);
6019 if ((control->length > 0) && (control->data == NULL)) {
6021 * big trouble.. we have the lock and its
6025 panic("Impossible data==NULL length !=0");
6027 out_flags |= MSG_EOR;
6028 out_flags |= MSG_TRUNC;
6029 control->length = 0;
6030 SCTP_INP_READ_UNLOCK(inp);
6031 goto done_with_control;
6033 SCTP_INP_READ_UNLOCK(inp);
6034 /* We will fall around to get more data */
6039 * Give caller back the mbuf chain,
6040 * store in uio_resid the length
6042 wakeup_read_socket = 0;
6043 if ((control->end_added == 0) ||
6044 (TAILQ_NEXT(control, next) == NULL)) {
6045 /* Need to get rlock */
6046 if (hold_rlock == 0) {
6047 SCTP_INP_READ_LOCK(inp);
6051 if (control->end_added) {
6052 out_flags |= MSG_EOR;
6053 if ((control->do_not_ref_stcb == 0) &&
6054 (control->stcb != NULL) &&
6055 ((control->spec_flags & M_NOTIFICATION) == 0))
6056 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6058 if (control->spec_flags & M_NOTIFICATION) {
6059 out_flags |= MSG_NOTIFICATION;
6061 uio->uio_resid = control->length;
6062 *mp = control->data;
6065 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6066 sctp_sblog(&so->so_rcv,
6067 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
6069 sctp_sbfree(control, stcb, &so->so_rcv, m);
6070 freed_so_far += (uint32_t)SCTP_BUF_LEN(m);
6071 freed_so_far += MSIZE;
6072 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6073 sctp_sblog(&so->so_rcv,
6074 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
6076 m = SCTP_BUF_NEXT(m);
6078 control->data = control->tail_mbuf = NULL;
6079 control->length = 0;
6080 if (out_flags & MSG_EOR) {
6081 /* Done with this control */
6082 goto done_with_control;
6086 if (hold_rlock == 1) {
6087 SCTP_INP_READ_UNLOCK(inp);
6090 if (hold_sblock == 1) {
6091 SOCKBUF_UNLOCK(&so->so_rcv);
6095 sbunlock(&so->so_rcv);
6100 SOCKBUF_UNLOCK(&so->so_rcv);
6103 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6104 if ((freed_so_far >= rwnd_req) &&
6105 (control && (control->do_not_ref_stcb == 0)) &&
6106 (no_rcv_needed == 0))
6107 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6111 *msg_flags = out_flags;
6113 if (((out_flags & MSG_EOR) == 0) &&
6114 ((in_flags & MSG_PEEK) == 0) &&
6116 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6117 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
6118 struct sctp_extrcvinfo *s_extra;
6120 s_extra = (struct sctp_extrcvinfo *)sinfo;
6121 s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
6123 if (hold_rlock == 1) {
6124 SCTP_INP_READ_UNLOCK(inp);
6127 SOCKBUF_UNLOCK(&so->so_rcv);
6130 sbunlock(&so->so_rcv);
6133 if (freecnt_applied) {
6135 * The lock on the socket buffer protects us so the free
6136 * code will stop. But since we used the socketbuf lock and
6137 * the sender uses the tcb_lock to increment, we need to use
6138 * the atomic add to the refcnt.
6142 panic("stcb for refcnt has gone NULL?");
6148 /* Save the value back for next time */
6149 stcb->freed_by_sorcv_sincelast = freed_so_far;
6150 atomic_add_int(&stcb->asoc.refcnt, -1);
6152 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6154 sctp_misc_ints(SCTP_SORECV_DONE,
6156 (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6160 sctp_misc_ints(SCTP_SORECV_DONE,
6162 (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6168 if (wakeup_read_socket) {
6169 sctp_sorwakeup(inp, so);
6175 #ifdef SCTP_MBUF_LOGGING
6177 sctp_m_free(struct mbuf *m)
6179 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6180 sctp_log_mb(m, SCTP_MBUF_IFREE);
6186 sctp_m_freem(struct mbuf *mb)
6189 mb = sctp_m_free(mb);
6195 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
6198 * Given a local address. For all associations that holds the
6199 * address, request a peer-set-primary.
6201 struct sctp_ifa *ifa;
6202 struct sctp_laddr *wi;
6204 ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
6206 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
6207 return (EADDRNOTAVAIL);
6210 * Now that we have the ifa we must awaken the iterator with this
6213 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6215 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
6218 /* Now incr the count and int wi structure */
6219 SCTP_INCR_LADDR_COUNT();
6220 memset(wi, 0, sizeof(*wi));
6221 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
6223 wi->action = SCTP_SET_PRIM_ADDR;
6224 atomic_add_int(&ifa->refcount, 1);
6226 /* Now add it to the work queue */
6227 SCTP_WQ_ADDR_LOCK();
6229 * Should this really be a tailq? As it is we will process the
6232 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6233 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
6234 (struct sctp_inpcb *)NULL,
6235 (struct sctp_tcb *)NULL,
6236 (struct sctp_nets *)NULL);
6237 SCTP_WQ_ADDR_UNLOCK();
6243 sctp_soreceive(struct socket *so,
6244 struct sockaddr **psa,
6247 struct mbuf **controlp,
6251 uint8_t sockbuf[256];
6252 struct sockaddr *from;
6253 struct sctp_extrcvinfo sinfo;
6254 int filling_sinfo = 1;
6256 struct sctp_inpcb *inp;
6258 inp = (struct sctp_inpcb *)so->so_pcb;
6259 /* pickup the assoc we are reading from */
6261 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6264 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6265 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6266 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6267 (controlp == NULL)) {
6268 /* user does not want the sndrcv ctl */
6272 from = (struct sockaddr *)sockbuf;
6273 fromlen = sizeof(sockbuf);
6280 if (filling_sinfo) {
6281 memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6283 if (flagsp != NULL) {
6288 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, &flags,
6289 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6290 if (flagsp != NULL) {
6293 if (controlp != NULL) {
6294 /* copy back the sinfo in a CMSG format */
6295 if (filling_sinfo && ((flags & MSG_NOTIFICATION) == 0)) {
6296 *controlp = sctp_build_ctl_nchunk(inp,
6297 (struct sctp_sndrcvinfo *)&sinfo);
6303 /* copy back the address info */
6304 if (from && from->sa_len) {
6305 *psa = sodupsockaddr(from, M_NOWAIT);
6318 sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6319 int totaddr, int *error)
6323 struct sctp_inpcb *inp;
6324 struct sockaddr *sa;
6327 struct sockaddr_in *sin;
6330 struct sockaddr_in6 *sin6;
6334 inp = stcb->sctp_ep;
6336 for (i = 0; i < totaddr; i++) {
6337 switch (sa->sa_family) {
6340 incr = sizeof(struct sockaddr_in);
6341 sin = (struct sockaddr_in *)sa;
6342 if ((sin->sin_addr.s_addr == INADDR_ANY) ||
6343 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
6344 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
6345 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6346 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6347 SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
6351 if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
6352 SCTP_DONOT_SETSCOPE,
6353 SCTP_ADDR_IS_CONFIRMED)) {
6354 /* assoc gone no un-lock */
6355 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6356 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6357 SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
6366 incr = sizeof(struct sockaddr_in6);
6367 sin6 = (struct sockaddr_in6 *)sa;
6368 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
6369 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
6370 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6371 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6372 SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
6376 if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
6377 SCTP_DONOT_SETSCOPE,
6378 SCTP_ADDR_IS_CONFIRMED)) {
6379 /* assoc gone no un-lock */
6380 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6381 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6382 SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
6392 sa = (struct sockaddr *)((caddr_t)sa + incr);
6399 sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6400 unsigned int totaddr,
6401 unsigned int *num_v4, unsigned int *num_v6,
6404 struct sockaddr *sa;
6405 struct sctp_tcb *stcb;
6406 unsigned int incr, at, i;
6410 *num_v6 = *num_v4 = 0;
6411 /* account and validate addresses */
6415 for (i = 0; i < totaddr; i++) {
6416 if (at + sizeof(struct sockaddr) > limit) {
6419 switch (sa->sa_family) {
6422 incr = (unsigned int)sizeof(struct sockaddr_in);
6423 if (sa->sa_len != incr) {
6432 struct sockaddr_in6 *sin6;
6434 sin6 = (struct sockaddr_in6 *)sa;
6435 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6436 /* Must be non-mapped for connectx */
6439 incr = (unsigned int)sizeof(struct sockaddr_in6);
6440 if (sa->sa_len != incr) {
6450 if ((at + incr) > limit) {
6453 SCTP_INP_INCR_REF(inp);
6454 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
6456 SCTP_TCB_UNLOCK(stcb);
6459 SCTP_INP_DECR_REF(inp);
6462 sa = (struct sockaddr *)((caddr_t)sa + incr);
6468 * sctp_bindx(ADD) for one address.
6469 * assumes all arguments are valid/checked by caller.
6472 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
6473 struct sockaddr *sa, sctp_assoc_t assoc_id,
6474 uint32_t vrf_id, int *error, void *p)
6476 struct sockaddr *addr_touse;
6477 #if defined(INET) && defined(INET6)
6478 struct sockaddr_in sin;
6481 /* see if we're bound all already! */
6482 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6483 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6489 if (sa->sa_family == AF_INET6) {
6491 struct sockaddr_in6 *sin6;
6494 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6495 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6499 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6500 /* can only bind v6 on PF_INET6 sockets */
6501 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6506 sin6 = (struct sockaddr_in6 *)addr_touse;
6507 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6508 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6509 SCTP_IPV6_V6ONLY(inp)) {
6510 /* can't bind v4-mapped on PF_INET sockets */
6511 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6515 in6_sin6_2_sin(&sin, sin6);
6516 addr_touse = (struct sockaddr *)&sin;
6522 if (sa->sa_family == AF_INET) {
6523 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6524 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6528 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6529 SCTP_IPV6_V6ONLY(inp)) {
6530 /* can't bind v4 on PF_INET sockets */
6531 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6537 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
6539 /* Can't get proc for Net/Open BSD */
6540 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6544 *error = sctp_inpcb_bind(so, addr_touse, NULL, p);
6548 * No locks required here since bind and mgmt_ep_sa all do their own
6549 * locking. If we do something for the FIX: below we may need to
6550 * lock in that case.
6552 if (assoc_id == 0) {
6553 /* add the address */
6554 struct sctp_inpcb *lep;
6555 struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
6557 /* validate the incoming port */
6558 if ((lsin->sin_port != 0) &&
6559 (lsin->sin_port != inp->sctp_lport)) {
6560 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6564 /* user specified 0 port, set it to existing port */
6565 lsin->sin_port = inp->sctp_lport;
6568 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
6571 * We must decrement the refcount since we have the
6572 * ep already and are binding. No remove going on
6575 SCTP_INP_DECR_REF(lep);
6578 /* already bound to it.. ok */
6580 } else if (lep == NULL) {
6581 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
6582 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6583 SCTP_ADD_IP_ADDRESS,
6586 *error = EADDRINUSE;
6592 * FIX: decide whether we allow assoc based bindx
6598 * sctp_bindx(DELETE) for one address.
6599 * assumes all arguments are valid/checked by caller.
6602 sctp_bindx_delete_address(struct sctp_inpcb *inp,
6603 struct sockaddr *sa, sctp_assoc_t assoc_id,
6604 uint32_t vrf_id, int *error)
6606 struct sockaddr *addr_touse;
6607 #if defined(INET) && defined(INET6)
6608 struct sockaddr_in sin;
6611 /* see if we're bound all already! */
6612 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6613 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6619 if (sa->sa_family == AF_INET6) {
6621 struct sockaddr_in6 *sin6;
6624 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6625 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6629 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6630 /* can only bind v6 on PF_INET6 sockets */
6631 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6636 sin6 = (struct sockaddr_in6 *)addr_touse;
6637 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6638 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6639 SCTP_IPV6_V6ONLY(inp)) {
6640 /* can't bind mapped-v4 on PF_INET sockets */
6641 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6645 in6_sin6_2_sin(&sin, sin6);
6646 addr_touse = (struct sockaddr *)&sin;
6652 if (sa->sa_family == AF_INET) {
6653 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6654 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6658 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6659 SCTP_IPV6_V6ONLY(inp)) {
6660 /* can't bind v4 on PF_INET sockets */
6661 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6668 * No lock required mgmt_ep_sa does its own locking. If the FIX:
6669 * below is ever changed we may need to lock before calling
6670 * association level binding.
6672 if (assoc_id == 0) {
6673 /* delete the address */
6674 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6675 SCTP_DEL_IP_ADDRESS,
6679 * FIX: decide whether we allow assoc based bindx
6685 * returns the valid local address count for an assoc, taking into account
6689 sctp_local_addr_count(struct sctp_tcb *stcb)
6693 int ipv4_local_scope, ipv4_addr_legal;
6696 int local_scope, site_scope, ipv6_addr_legal;
6698 struct sctp_vrf *vrf;
6699 struct sctp_ifn *sctp_ifn;
6700 struct sctp_ifa *sctp_ifa;
6703 /* Turn on all the appropriate scopes */
6704 loopback_scope = stcb->asoc.scope.loopback_scope;
6706 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
6707 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
6710 local_scope = stcb->asoc.scope.local_scope;
6711 site_scope = stcb->asoc.scope.site_scope;
6712 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
6714 SCTP_IPI_ADDR_RLOCK();
6715 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
6717 /* no vrf, no addresses */
6718 SCTP_IPI_ADDR_RUNLOCK();
6722 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6724 * bound all case: go through all ifns on the vrf
6726 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
6727 if ((loopback_scope == 0) &&
6728 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
6731 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
6732 if (sctp_is_addr_restricted(stcb, sctp_ifa))
6734 switch (sctp_ifa->address.sa.sa_family) {
6737 if (ipv4_addr_legal) {
6738 struct sockaddr_in *sin;
6740 sin = &sctp_ifa->address.sin;
6741 if (sin->sin_addr.s_addr == 0) {
6748 if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
6749 &sin->sin_addr) != 0) {
6752 if ((ipv4_local_scope == 0) &&
6753 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
6756 /* count this one */
6765 if (ipv6_addr_legal) {
6766 struct sockaddr_in6 *sin6;
6768 sin6 = &sctp_ifa->address.sin6;
6769 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6772 if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
6773 &sin6->sin6_addr) != 0) {
6776 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
6777 if (local_scope == 0)
6779 if (sin6->sin6_scope_id == 0) {
6780 if (sa6_recoverscope(sin6) != 0)
6793 if ((site_scope == 0) &&
6794 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
6797 /* count this one */
6812 struct sctp_laddr *laddr;
6814 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
6816 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
6819 /* count this one */
6823 SCTP_IPI_ADDR_RUNLOCK();
6827 #if defined(SCTP_LOCAL_TRACE_BUF)
6830 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)
6832 uint32_t saveindex, newindex;
6835 saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6836 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6839 newindex = saveindex + 1;
6841 } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6842 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6845 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6846 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6847 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6848 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6849 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6850 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6851 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6852 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6857 sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
6858 const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
6862 struct ip6_hdr *ip6;
6864 struct mbuf *sp, *last;
6865 struct udphdr *uhdr;
6868 if ((m->m_flags & M_PKTHDR) == 0) {
6869 /* Can't handle one that is not a pkt hdr */
6872 /* Pull the src port */
6873 iph = mtod(m, struct ip *);
6874 uhdr = (struct udphdr *)((caddr_t)iph + off);
6875 port = uhdr->uh_sport;
6877 * Split out the mbuf chain. Leave the IP header in m, place the
6880 sp = m_split(m, off, M_NOWAIT);
6882 /* Gak, drop packet, we can't do a split */
6885 if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
6886 /* Gak, packet can't have an SCTP header in it - too small */
6890 /* Now pull up the UDP header and SCTP header together */
6891 sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
6893 /* Gak pullup failed */
6896 /* Trim out the UDP header */
6897 m_adj(sp, sizeof(struct udphdr));
6899 /* Now reconstruct the mbuf chain */
6900 for (last = m; last->m_next; last = last->m_next);
6902 m->m_pkthdr.len += sp->m_pkthdr.len;
6904 * The CSUM_DATA_VALID flags indicates that the HW checked the UDP
6905 * checksum and it was valid. Since CSUM_DATA_VALID ==
6906 * CSUM_SCTP_VALID this would imply that the HW also verified the
6907 * SCTP checksum. Therefore, clear the bit.
6909 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6910 "sctp_recv_udp_tunneled_packet(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6912 if_name(m->m_pkthdr.rcvif),
6913 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6914 m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
6915 iph = mtod(m, struct ip *);
6916 switch (iph->ip_v) {
6919 iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
6920 sctp_input_with_port(m, off, port);
6924 case IPV6_VERSION >> 4:
6925 ip6 = mtod(m, struct ip6_hdr *);
6926 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
6927 sctp6_input_with_port(&m, &off, port);
6941 sctp_recv_icmp_tunneled_packet(int cmd, struct sockaddr *sa, void *vip, void *ctx SCTP_UNUSED)
6943 struct ip *outer_ip, *inner_ip;
6947 struct sctp_inpcb *inp;
6948 struct sctp_tcb *stcb;
6949 struct sctp_nets *net;
6950 struct sctp_init_chunk *ch;
6951 struct sockaddr_in src, dst;
6954 inner_ip = (struct ip *)vip;
6955 icmp = (struct icmp *)((caddr_t)inner_ip -
6956 (sizeof(struct icmp) - sizeof(struct ip)));
6957 outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
6958 if (ntohs(outer_ip->ip_len) <
6959 sizeof(struct ip) + 8 + (inner_ip->ip_hl << 2) + sizeof(struct udphdr) + 8) {
6962 udp = (struct udphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
6963 sh = (struct sctphdr *)(udp + 1);
6964 memset(&src, 0, sizeof(struct sockaddr_in));
6965 src.sin_family = AF_INET;
6966 src.sin_len = sizeof(struct sockaddr_in);
6967 src.sin_port = sh->src_port;
6968 src.sin_addr = inner_ip->ip_src;
6969 memset(&dst, 0, sizeof(struct sockaddr_in));
6970 dst.sin_family = AF_INET;
6971 dst.sin_len = sizeof(struct sockaddr_in);
6972 dst.sin_port = sh->dest_port;
6973 dst.sin_addr = inner_ip->ip_dst;
6975 * 'dst' holds the dest of the packet that failed to be sent. 'src'
6976 * holds our local endpoint address. Thus we reverse the dst and the
6977 * src in the lookup.
6981 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
6982 (struct sockaddr *)&src,
6984 SCTP_DEFAULT_VRFID);
6985 if ((stcb != NULL) &&
6988 /* Check the UDP port numbers */
6989 if ((udp->uh_dport != net->port) ||
6990 (udp->uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
6991 SCTP_TCB_UNLOCK(stcb);
6994 /* Check the verification tag */
6995 if (ntohl(sh->v_tag) != 0) {
6997 * This must be the verification tag used for
6998 * sending out packets. We don't consider packets
6999 * reflecting the verification tag.
7001 if (ntohl(sh->v_tag) != stcb->asoc.peer_vtag) {
7002 SCTP_TCB_UNLOCK(stcb);
7006 if (ntohs(outer_ip->ip_len) >=
7008 8 + (inner_ip->ip_hl << 2) + 8 + 20) {
7010 * In this case we can check if we got an
7011 * INIT chunk and if the initiate tag
7014 ch = (struct sctp_init_chunk *)(sh + 1);
7015 if ((ch->ch.chunk_type != SCTP_INITIATION) ||
7016 (ntohl(ch->init.initiate_tag) != stcb->asoc.my_vtag)) {
7017 SCTP_TCB_UNLOCK(stcb);
7021 SCTP_TCB_UNLOCK(stcb);
7025 type = icmp->icmp_type;
7026 code = icmp->icmp_code;
7027 if ((type == ICMP_UNREACH) &&
7028 (code == ICMP_UNREACH_PORT)) {
7029 code = ICMP_UNREACH_PROTOCOL;
7031 sctp_notify(inp, stcb, net, type, code,
7032 ntohs(inner_ip->ip_len),
7033 (uint32_t)ntohs(icmp->icmp_nextmtu));
7035 if ((stcb == NULL) && (inp != NULL)) {
7036 /* reduce ref-count */
7037 SCTP_INP_WLOCK(inp);
7038 SCTP_INP_DECR_REF(inp);
7039 SCTP_INP_WUNLOCK(inp);
7042 SCTP_TCB_UNLOCK(stcb);
7051 sctp_recv_icmp6_tunneled_packet(int cmd, struct sockaddr *sa, void *d, void *ctx SCTP_UNUSED)
7053 struct ip6ctlparam *ip6cp;
7054 struct sctp_inpcb *inp;
7055 struct sctp_tcb *stcb;
7056 struct sctp_nets *net;
7059 struct sockaddr_in6 src, dst;
7062 ip6cp = (struct ip6ctlparam *)d;
7064 * XXX: We assume that when IPV6 is non NULL, M and OFF are valid.
7066 if (ip6cp->ip6c_m == NULL) {
7070 * Check if we can safely examine the ports and the verification tag
7071 * of the SCTP common header.
7073 if (ip6cp->ip6c_m->m_pkthdr.len <
7074 ip6cp->ip6c_off + sizeof(struct udphdr) + offsetof(struct sctphdr, checksum)) {
7077 /* Copy out the UDP header. */
7078 memset(&udp, 0, sizeof(struct udphdr));
7079 m_copydata(ip6cp->ip6c_m,
7081 sizeof(struct udphdr),
7083 /* Copy out the port numbers and the verification tag. */
7084 memset(&sh, 0, sizeof(struct sctphdr));
7085 m_copydata(ip6cp->ip6c_m,
7086 ip6cp->ip6c_off + sizeof(struct udphdr),
7087 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
7089 memset(&src, 0, sizeof(struct sockaddr_in6));
7090 src.sin6_family = AF_INET6;
7091 src.sin6_len = sizeof(struct sockaddr_in6);
7092 src.sin6_port = sh.src_port;
7093 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
7094 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7097 memset(&dst, 0, sizeof(struct sockaddr_in6));
7098 dst.sin6_family = AF_INET6;
7099 dst.sin6_len = sizeof(struct sockaddr_in6);
7100 dst.sin6_port = sh.dest_port;
7101 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
7102 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7107 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7108 (struct sockaddr *)&src,
7109 &inp, &net, 1, SCTP_DEFAULT_VRFID);
7110 if ((stcb != NULL) &&
7113 /* Check the UDP port numbers */
7114 if ((udp.uh_dport != net->port) ||
7115 (udp.uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7116 SCTP_TCB_UNLOCK(stcb);
7119 /* Check the verification tag */
7120 if (ntohl(sh.v_tag) != 0) {
7122 * This must be the verification tag used for
7123 * sending out packets. We don't consider packets
7124 * reflecting the verification tag.
7126 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
7127 SCTP_TCB_UNLOCK(stcb);
7131 if (ip6cp->ip6c_m->m_pkthdr.len >=
7132 ip6cp->ip6c_off + sizeof(struct udphdr) +
7133 sizeof(struct sctphdr) +
7134 sizeof(struct sctp_chunkhdr) +
7135 offsetof(struct sctp_init, a_rwnd)) {
7137 * In this case we can check if we got an
7138 * INIT chunk and if the initiate tag
7141 uint32_t initiate_tag;
7144 m_copydata(ip6cp->ip6c_m,
7146 sizeof(struct udphdr) +
7147 sizeof(struct sctphdr),
7149 (caddr_t)&chunk_type);
7150 m_copydata(ip6cp->ip6c_m,
7152 sizeof(struct udphdr) +
7153 sizeof(struct sctphdr) +
7154 sizeof(struct sctp_chunkhdr),
7156 (caddr_t)&initiate_tag);
7157 if ((chunk_type != SCTP_INITIATION) ||
7158 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
7159 SCTP_TCB_UNLOCK(stcb);
7163 SCTP_TCB_UNLOCK(stcb);
7167 type = ip6cp->ip6c_icmp6->icmp6_type;
7168 code = ip6cp->ip6c_icmp6->icmp6_code;
7169 if ((type == ICMP6_DST_UNREACH) &&
7170 (code == ICMP6_DST_UNREACH_NOPORT)) {
7171 type = ICMP6_PARAM_PROB;
7172 code = ICMP6_PARAMPROB_NEXTHEADER;
7174 sctp6_notify(inp, stcb, net, type, code,
7175 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
7177 if ((stcb == NULL) && (inp != NULL)) {
7178 /* reduce inp's ref-count */
7179 SCTP_INP_WLOCK(inp);
7180 SCTP_INP_DECR_REF(inp);
7181 SCTP_INP_WUNLOCK(inp);
7184 SCTP_TCB_UNLOCK(stcb);
7191 sctp_over_udp_stop(void)
7194 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7198 if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7199 soclose(SCTP_BASE_INFO(udp4_tun_socket));
7200 SCTP_BASE_INFO(udp4_tun_socket) = NULL;
7204 if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
7205 soclose(SCTP_BASE_INFO(udp6_tun_socket));
7206 SCTP_BASE_INFO(udp6_tun_socket) = NULL;
7212 sctp_over_udp_start(void)
7217 struct sockaddr_in sin;
7220 struct sockaddr_in6 sin6;
7223 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7226 port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
7227 if (ntohs(port) == 0) {
7228 /* Must have a port set */
7232 if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7233 /* Already running -- must stop first */
7238 if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
7239 /* Already running -- must stop first */
7244 if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
7245 SOCK_DGRAM, IPPROTO_UDP,
7246 curthread->td_ucred, curthread))) {
7247 sctp_over_udp_stop();
7250 /* Call the special UDP hook. */
7251 if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
7252 sctp_recv_udp_tunneled_packet,
7253 sctp_recv_icmp_tunneled_packet,
7255 sctp_over_udp_stop();
7258 /* Ok, we have a socket, bind it to the port. */
7259 memset(&sin, 0, sizeof(struct sockaddr_in));
7260 sin.sin_len = sizeof(struct sockaddr_in);
7261 sin.sin_family = AF_INET;
7262 sin.sin_port = htons(port);
7263 if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
7264 (struct sockaddr *)&sin, curthread))) {
7265 sctp_over_udp_stop();
7270 if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
7271 SOCK_DGRAM, IPPROTO_UDP,
7272 curthread->td_ucred, curthread))) {
7273 sctp_over_udp_stop();
7276 /* Call the special UDP hook. */
7277 if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
7278 sctp_recv_udp_tunneled_packet,
7279 sctp_recv_icmp6_tunneled_packet,
7281 sctp_over_udp_stop();
7284 /* Ok, we have a socket, bind it to the port. */
7285 memset(&sin6, 0, sizeof(struct sockaddr_in6));
7286 sin6.sin6_len = sizeof(struct sockaddr_in6);
7287 sin6.sin6_family = AF_INET6;
7288 sin6.sin6_port = htons(port);
7289 if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
7290 (struct sockaddr *)&sin6, curthread))) {
7291 sctp_over_udp_stop();
7299 * sctp_min_mtu ()returns the minimum of all non-zero arguments.
7300 * If all arguments are zero, zero is returned.
7303 sctp_min_mtu(uint32_t mtu1, uint32_t mtu2, uint32_t mtu3)
7308 return (min(mtu1, min(mtu2, mtu3)));
7310 return (min(mtu1, mtu2));
7314 return (min(mtu1, mtu3));
7322 return (min(mtu2, mtu3));
7333 sctp_hc_set_mtu(union sctp_sockstore *addr, uint16_t fibnum, uint32_t mtu)
7335 struct in_conninfo inc;
7337 memset(&inc, 0, sizeof(struct in_conninfo));
7338 inc.inc_fibnum = fibnum;
7339 switch (addr->sa.sa_family) {
7342 inc.inc_faddr = addr->sin.sin_addr;
7347 inc.inc_flags |= INC_ISIPV6;
7348 inc.inc6_faddr = addr->sin6.sin6_addr;
7354 tcp_hc_updatemtu(&inc, (u_long)mtu);
7358 sctp_hc_get_mtu(union sctp_sockstore *addr, uint16_t fibnum)
7360 struct in_conninfo inc;
7362 memset(&inc, 0, sizeof(struct in_conninfo));
7363 inc.inc_fibnum = fibnum;
7364 switch (addr->sa.sa_family) {
7367 inc.inc_faddr = addr->sin.sin_addr;
7372 inc.inc_flags |= INC_ISIPV6;
7373 inc.inc6_faddr = addr->sin6.sin6_addr;
7379 return ((uint32_t)tcp_hc_getmtu(&inc));
7383 sctp_set_state(struct sctp_tcb *stcb, int new_state)
7385 #if defined(KDTRACE_HOOKS)
7386 int old_state = stcb->asoc.state;
7389 KASSERT((new_state & ~SCTP_STATE_MASK) == 0,
7390 ("sctp_set_state: Can't set substate (new_state = %x)",
7392 stcb->asoc.state = (stcb->asoc.state & ~SCTP_STATE_MASK) | new_state;
7393 if ((new_state == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7394 (new_state == SCTP_STATE_SHUTDOWN_SENT) ||
7395 (new_state == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
7396 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
7398 #if defined(KDTRACE_HOOKS)
7399 if (((old_state & SCTP_STATE_MASK) != new_state) &&
7400 !(((old_state & SCTP_STATE_MASK) == SCTP_STATE_EMPTY) &&
7401 (new_state == SCTP_STATE_INUSE))) {
7402 SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
7408 sctp_add_substate(struct sctp_tcb *stcb, int substate)
7410 #if defined(KDTRACE_HOOKS)
7411 int old_state = stcb->asoc.state;
7414 KASSERT((substate & SCTP_STATE_MASK) == 0,
7415 ("sctp_add_substate: Can't set state (substate = %x)",
7417 stcb->asoc.state |= substate;
7418 #if defined(KDTRACE_HOOKS)
7419 if (((substate & SCTP_STATE_ABOUT_TO_BE_FREED) &&
7420 ((old_state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) ||
7421 ((substate & SCTP_STATE_SHUTDOWN_PENDING) &&
7422 ((old_state & SCTP_STATE_SHUTDOWN_PENDING) == 0))) {
7423 SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);