2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctputil.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
42 #include <netinet6/sctp6_var.h>
44 #include <netinet/sctp_header.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_auth.h>
50 #include <netinet/sctp_asconf.h>
51 #include <netinet/sctp_bsd_addr.h>
52 #if defined(INET6) || defined(INET)
53 #include <netinet/tcp_var.h>
55 #include <netinet/udp.h>
56 #include <netinet/udp_var.h>
59 #include <netinet/icmp6.h>
64 #define KTR_SCTP KTR_SUBSYS
67 extern const struct sctp_cc_functions sctp_cc_functions[];
68 extern const struct sctp_ss_functions sctp_ss_functions[];
71 sctp_sblog(struct sockbuf *sb, struct sctp_tcb *stcb, int from, int incr)
73 #if defined(SCTP_LOCAL_TRACE_BUF)
74 struct sctp_cwnd_log sctp_clog;
76 sctp_clog.x.sb.stcb = stcb;
77 sctp_clog.x.sb.so_sbcc = sb->sb_cc;
79 sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
81 sctp_clog.x.sb.stcb_sbcc = 0;
82 sctp_clog.x.sb.incr = incr;
83 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
86 sctp_clog.x.misc.log1,
87 sctp_clog.x.misc.log2,
88 sctp_clog.x.misc.log3,
89 sctp_clog.x.misc.log4);
94 sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
96 #if defined(SCTP_LOCAL_TRACE_BUF)
97 struct sctp_cwnd_log sctp_clog;
99 sctp_clog.x.close.inp = (void *)inp;
100 sctp_clog.x.close.sctp_flags = inp->sctp_flags;
102 sctp_clog.x.close.stcb = (void *)stcb;
103 sctp_clog.x.close.state = (uint16_t)stcb->asoc.state;
105 sctp_clog.x.close.stcb = 0;
106 sctp_clog.x.close.state = 0;
108 sctp_clog.x.close.loc = loc;
109 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
110 SCTP_LOG_EVENT_CLOSE,
112 sctp_clog.x.misc.log1,
113 sctp_clog.x.misc.log2,
114 sctp_clog.x.misc.log3,
115 sctp_clog.x.misc.log4);
120 rto_logging(struct sctp_nets *net, int from)
122 #if defined(SCTP_LOCAL_TRACE_BUF)
123 struct sctp_cwnd_log sctp_clog;
125 memset(&sctp_clog, 0, sizeof(sctp_clog));
126 sctp_clog.x.rto.net = (void *)net;
127 sctp_clog.x.rto.rtt = net->rtt / 1000;
128 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
131 sctp_clog.x.misc.log1,
132 sctp_clog.x.misc.log2,
133 sctp_clog.x.misc.log3,
134 sctp_clog.x.misc.log4);
139 sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
141 #if defined(SCTP_LOCAL_TRACE_BUF)
142 struct sctp_cwnd_log sctp_clog;
144 sctp_clog.x.strlog.stcb = stcb;
145 sctp_clog.x.strlog.n_tsn = tsn;
146 sctp_clog.x.strlog.n_sseq = sseq;
147 sctp_clog.x.strlog.e_tsn = 0;
148 sctp_clog.x.strlog.e_sseq = 0;
149 sctp_clog.x.strlog.strm = stream;
150 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
153 sctp_clog.x.misc.log1,
154 sctp_clog.x.misc.log2,
155 sctp_clog.x.misc.log3,
156 sctp_clog.x.misc.log4);
161 sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
163 #if defined(SCTP_LOCAL_TRACE_BUF)
164 struct sctp_cwnd_log sctp_clog;
166 sctp_clog.x.nagle.stcb = (void *)stcb;
167 sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
168 sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
169 sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
170 sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
171 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
172 SCTP_LOG_EVENT_NAGLE,
174 sctp_clog.x.misc.log1,
175 sctp_clog.x.misc.log2,
176 sctp_clog.x.misc.log3,
177 sctp_clog.x.misc.log4);
182 sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
184 #if defined(SCTP_LOCAL_TRACE_BUF)
185 struct sctp_cwnd_log sctp_clog;
187 sctp_clog.x.sack.cumack = cumack;
188 sctp_clog.x.sack.oldcumack = old_cumack;
189 sctp_clog.x.sack.tsn = tsn;
190 sctp_clog.x.sack.numGaps = gaps;
191 sctp_clog.x.sack.numDups = dups;
192 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
195 sctp_clog.x.misc.log1,
196 sctp_clog.x.misc.log2,
197 sctp_clog.x.misc.log3,
198 sctp_clog.x.misc.log4);
203 sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
205 #if defined(SCTP_LOCAL_TRACE_BUF)
206 struct sctp_cwnd_log sctp_clog;
208 memset(&sctp_clog, 0, sizeof(sctp_clog));
209 sctp_clog.x.map.base = map;
210 sctp_clog.x.map.cum = cum;
211 sctp_clog.x.map.high = high;
212 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
215 sctp_clog.x.misc.log1,
216 sctp_clog.x.misc.log2,
217 sctp_clog.x.misc.log3,
218 sctp_clog.x.misc.log4);
223 sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn, int from)
225 #if defined(SCTP_LOCAL_TRACE_BUF)
226 struct sctp_cwnd_log sctp_clog;
228 memset(&sctp_clog, 0, sizeof(sctp_clog));
229 sctp_clog.x.fr.largest_tsn = biggest_tsn;
230 sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
231 sctp_clog.x.fr.tsn = tsn;
232 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
235 sctp_clog.x.misc.log1,
236 sctp_clog.x.misc.log2,
237 sctp_clog.x.misc.log3,
238 sctp_clog.x.misc.log4);
242 #ifdef SCTP_MBUF_LOGGING
244 sctp_log_mb(struct mbuf *m, int from)
246 #if defined(SCTP_LOCAL_TRACE_BUF)
247 struct sctp_cwnd_log sctp_clog;
249 sctp_clog.x.mb.mp = m;
250 sctp_clog.x.mb.mbuf_flags = (uint8_t)(SCTP_BUF_GET_FLAGS(m));
251 sctp_clog.x.mb.size = (uint16_t)(SCTP_BUF_LEN(m));
252 sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
253 if (SCTP_BUF_IS_EXTENDED(m)) {
254 sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
255 sctp_clog.x.mb.refcnt = (uint8_t)(SCTP_BUF_EXTEND_REFCNT(m));
257 sctp_clog.x.mb.ext = 0;
258 sctp_clog.x.mb.refcnt = 0;
260 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
263 sctp_clog.x.misc.log1,
264 sctp_clog.x.misc.log2,
265 sctp_clog.x.misc.log3,
266 sctp_clog.x.misc.log4);
271 sctp_log_mbc(struct mbuf *m, int from)
275 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
276 sctp_log_mb(mat, from);
282 sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from)
284 #if defined(SCTP_LOCAL_TRACE_BUF)
285 struct sctp_cwnd_log sctp_clog;
287 if (control == NULL) {
288 SCTP_PRINTF("Gak log of NULL?\n");
291 sctp_clog.x.strlog.stcb = control->stcb;
292 sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
293 sctp_clog.x.strlog.n_sseq = (uint16_t)control->mid;
294 sctp_clog.x.strlog.strm = control->sinfo_stream;
295 if (poschk != NULL) {
296 sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
297 sctp_clog.x.strlog.e_sseq = (uint16_t)poschk->mid;
299 sctp_clog.x.strlog.e_tsn = 0;
300 sctp_clog.x.strlog.e_sseq = 0;
302 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
305 sctp_clog.x.misc.log1,
306 sctp_clog.x.misc.log2,
307 sctp_clog.x.misc.log3,
308 sctp_clog.x.misc.log4);
313 sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
315 #if defined(SCTP_LOCAL_TRACE_BUF)
316 struct sctp_cwnd_log sctp_clog;
318 sctp_clog.x.cwnd.net = net;
319 if (stcb->asoc.send_queue_cnt > 255)
320 sctp_clog.x.cwnd.cnt_in_send = 255;
322 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
323 if (stcb->asoc.stream_queue_cnt > 255)
324 sctp_clog.x.cwnd.cnt_in_str = 255;
326 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
329 sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
330 sctp_clog.x.cwnd.inflight = net->flight_size;
331 sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
332 sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
333 sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
335 if (SCTP_CWNDLOG_PRESEND == from) {
336 sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
338 sctp_clog.x.cwnd.cwnd_augment = augment;
339 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
342 sctp_clog.x.misc.log1,
343 sctp_clog.x.misc.log2,
344 sctp_clog.x.misc.log3,
345 sctp_clog.x.misc.log4);
350 sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
352 #if defined(SCTP_LOCAL_TRACE_BUF)
353 struct sctp_cwnd_log sctp_clog;
355 memset(&sctp_clog, 0, sizeof(sctp_clog));
357 sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
360 sctp_clog.x.lock.sock = (void *)NULL;
362 sctp_clog.x.lock.inp = (void *)inp;
364 sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
366 sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
369 sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
370 sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
372 sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
373 sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
375 sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
376 if (inp && (inp->sctp_socket)) {
377 sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
378 sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
379 sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
381 sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
382 sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
383 sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
385 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
388 sctp_clog.x.misc.log1,
389 sctp_clog.x.misc.log2,
390 sctp_clog.x.misc.log3,
391 sctp_clog.x.misc.log4);
396 sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
398 #if defined(SCTP_LOCAL_TRACE_BUF)
399 struct sctp_cwnd_log sctp_clog;
401 memset(&sctp_clog, 0, sizeof(sctp_clog));
402 sctp_clog.x.cwnd.net = net;
403 sctp_clog.x.cwnd.cwnd_new_value = error;
404 sctp_clog.x.cwnd.inflight = net->flight_size;
405 sctp_clog.x.cwnd.cwnd_augment = burst;
406 if (stcb->asoc.send_queue_cnt > 255)
407 sctp_clog.x.cwnd.cnt_in_send = 255;
409 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
410 if (stcb->asoc.stream_queue_cnt > 255)
411 sctp_clog.x.cwnd.cnt_in_str = 255;
413 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
414 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
415 SCTP_LOG_EVENT_MAXBURST,
417 sctp_clog.x.misc.log1,
418 sctp_clog.x.misc.log2,
419 sctp_clog.x.misc.log3,
420 sctp_clog.x.misc.log4);
425 sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
427 #if defined(SCTP_LOCAL_TRACE_BUF)
428 struct sctp_cwnd_log sctp_clog;
430 sctp_clog.x.rwnd.rwnd = peers_rwnd;
431 sctp_clog.x.rwnd.send_size = snd_size;
432 sctp_clog.x.rwnd.overhead = overhead;
433 sctp_clog.x.rwnd.new_rwnd = 0;
434 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
437 sctp_clog.x.misc.log1,
438 sctp_clog.x.misc.log2,
439 sctp_clog.x.misc.log3,
440 sctp_clog.x.misc.log4);
445 sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
447 #if defined(SCTP_LOCAL_TRACE_BUF)
448 struct sctp_cwnd_log sctp_clog;
450 sctp_clog.x.rwnd.rwnd = peers_rwnd;
451 sctp_clog.x.rwnd.send_size = flight_size;
452 sctp_clog.x.rwnd.overhead = overhead;
453 sctp_clog.x.rwnd.new_rwnd = a_rwndval;
454 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
457 sctp_clog.x.misc.log1,
458 sctp_clog.x.misc.log2,
459 sctp_clog.x.misc.log3,
460 sctp_clog.x.misc.log4);
464 #ifdef SCTP_MBCNT_LOGGING
466 sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
468 #if defined(SCTP_LOCAL_TRACE_BUF)
469 struct sctp_cwnd_log sctp_clog;
471 sctp_clog.x.mbcnt.total_queue_size = total_oq;
472 sctp_clog.x.mbcnt.size_change = book;
473 sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
474 sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
475 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
476 SCTP_LOG_EVENT_MBCNT,
478 sctp_clog.x.misc.log1,
479 sctp_clog.x.misc.log2,
480 sctp_clog.x.misc.log3,
481 sctp_clog.x.misc.log4);
487 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
489 #if defined(SCTP_LOCAL_TRACE_BUF)
490 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
498 sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from)
500 #if defined(SCTP_LOCAL_TRACE_BUF)
501 struct sctp_cwnd_log sctp_clog;
503 sctp_clog.x.wake.stcb = (void *)stcb;
504 sctp_clog.x.wake.wake_cnt = wake_cnt;
505 sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
506 sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
507 sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
509 if (stcb->asoc.stream_queue_cnt < 0xff)
510 sctp_clog.x.wake.stream_qcnt = (uint8_t)stcb->asoc.stream_queue_cnt;
512 sctp_clog.x.wake.stream_qcnt = 0xff;
514 if (stcb->asoc.chunks_on_out_queue < 0xff)
515 sctp_clog.x.wake.chunks_on_oque = (uint8_t)stcb->asoc.chunks_on_out_queue;
517 sctp_clog.x.wake.chunks_on_oque = 0xff;
519 sctp_clog.x.wake.sctpflags = 0;
520 /* set in the defered mode stuff */
521 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
522 sctp_clog.x.wake.sctpflags |= 1;
523 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
524 sctp_clog.x.wake.sctpflags |= 2;
525 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
526 sctp_clog.x.wake.sctpflags |= 4;
527 /* what about the sb */
528 if (stcb->sctp_socket) {
529 struct socket *so = stcb->sctp_socket;
531 sctp_clog.x.wake.sbflags = (uint8_t)((so->so_snd.sb_flags & 0x00ff));
533 sctp_clog.x.wake.sbflags = 0xff;
535 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
538 sctp_clog.x.misc.log1,
539 sctp_clog.x.misc.log2,
540 sctp_clog.x.misc.log3,
541 sctp_clog.x.misc.log4);
546 sctp_log_block(uint8_t from, struct sctp_association *asoc, size_t sendlen)
548 #if defined(SCTP_LOCAL_TRACE_BUF)
549 struct sctp_cwnd_log sctp_clog;
551 sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
552 sctp_clog.x.blk.send_sent_qcnt = (uint16_t)(asoc->send_queue_cnt + asoc->sent_queue_cnt);
553 sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
554 sctp_clog.x.blk.stream_qcnt = (uint16_t)asoc->stream_queue_cnt;
555 sctp_clog.x.blk.chunks_on_oque = (uint16_t)asoc->chunks_on_out_queue;
556 sctp_clog.x.blk.flight_size = (uint16_t)(asoc->total_flight / 1024);
557 sctp_clog.x.blk.sndlen = (uint32_t)sendlen;
558 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
559 SCTP_LOG_EVENT_BLOCK,
561 sctp_clog.x.misc.log1,
562 sctp_clog.x.misc.log2,
563 sctp_clog.x.misc.log3,
564 sctp_clog.x.misc.log4);
569 sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED)
571 /* May need to fix this if ktrdump does not work */
575 #ifdef SCTP_AUDITING_ENABLED
576 uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
577 static int sctp_audit_indx = 0;
581 sctp_print_audit_report(void)
587 for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
588 if ((sctp_audit_data[i][0] == 0xe0) &&
589 (sctp_audit_data[i][1] == 0x01)) {
592 } else if (sctp_audit_data[i][0] == 0xf0) {
595 } else if ((sctp_audit_data[i][0] == 0xc0) &&
596 (sctp_audit_data[i][1] == 0x01)) {
600 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t)sctp_audit_data[i][0],
601 (uint32_t)sctp_audit_data[i][1]);
606 for (i = 0; i < sctp_audit_indx; i++) {
607 if ((sctp_audit_data[i][0] == 0xe0) &&
608 (sctp_audit_data[i][1] == 0x01)) {
611 } else if (sctp_audit_data[i][0] == 0xf0) {
614 } else if ((sctp_audit_data[i][0] == 0xc0) &&
615 (sctp_audit_data[i][1] == 0x01)) {
619 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t)sctp_audit_data[i][0],
620 (uint32_t)sctp_audit_data[i][1]);
629 sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
630 struct sctp_nets *net)
632 int resend_cnt, tot_out, rep, tot_book_cnt;
633 struct sctp_nets *lnet;
634 struct sctp_tmit_chunk *chk;
636 sctp_audit_data[sctp_audit_indx][0] = 0xAA;
637 sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
639 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
643 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
644 sctp_audit_data[sctp_audit_indx][1] = 0x01;
646 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
652 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
653 sctp_audit_data[sctp_audit_indx][1] = 0x02;
655 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
660 sctp_audit_data[sctp_audit_indx][0] = 0xA1;
661 sctp_audit_data[sctp_audit_indx][1] =
662 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
664 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
669 resend_cnt = tot_out = 0;
670 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
671 if (chk->sent == SCTP_DATAGRAM_RESEND) {
673 } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
674 tot_out += chk->book_size;
678 if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
679 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
680 sctp_audit_data[sctp_audit_indx][1] = 0xA1;
682 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
685 SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
686 resend_cnt, stcb->asoc.sent_queue_retran_cnt);
688 stcb->asoc.sent_queue_retran_cnt = resend_cnt;
689 sctp_audit_data[sctp_audit_indx][0] = 0xA2;
690 sctp_audit_data[sctp_audit_indx][1] =
691 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
693 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
697 if (tot_out != stcb->asoc.total_flight) {
698 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
699 sctp_audit_data[sctp_audit_indx][1] = 0xA2;
701 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
705 SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
706 (int)stcb->asoc.total_flight);
707 stcb->asoc.total_flight = tot_out;
709 if (tot_book_cnt != stcb->asoc.total_flight_count) {
710 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
711 sctp_audit_data[sctp_audit_indx][1] = 0xA5;
713 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
717 SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
719 stcb->asoc.total_flight_count = tot_book_cnt;
722 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
723 tot_out += lnet->flight_size;
725 if (tot_out != stcb->asoc.total_flight) {
726 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
727 sctp_audit_data[sctp_audit_indx][1] = 0xA3;
729 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
733 SCTP_PRINTF("real flight:%d net total was %d\n",
734 stcb->asoc.total_flight, tot_out);
735 /* now corrective action */
736 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
739 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
740 if ((chk->whoTo == lnet) &&
741 (chk->sent < SCTP_DATAGRAM_RESEND)) {
742 tot_out += chk->book_size;
745 if (lnet->flight_size != tot_out) {
746 SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
747 (void *)lnet, lnet->flight_size,
749 lnet->flight_size = tot_out;
754 sctp_print_audit_report();
759 sctp_audit_log(uint8_t ev, uint8_t fd)
762 sctp_audit_data[sctp_audit_indx][0] = ev;
763 sctp_audit_data[sctp_audit_indx][1] = fd;
765 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
773 * sctp_stop_timers_for_shutdown() should be called
774 * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
775 * state to make sure that all timers are stopped.
778 sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
780 struct sctp_association *asoc;
781 struct sctp_nets *net;
785 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
786 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
787 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
788 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
789 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
790 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
791 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
792 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
797 * A list of sizes based on typical mtu's, used only if next hop size not
798 * returned. These values MUST be multiples of 4 and MUST be ordered.
800 static uint32_t sctp_mtu_sizes[] = {
822 * Return the largest MTU in sctp_mtu_sizes smaller than val.
823 * If val is smaller than the minimum, just return the largest
824 * multiple of 4 smaller or equal to val.
825 * Ensure that the result is a multiple of 4.
828 sctp_get_prev_mtu(uint32_t val)
833 if (val <= sctp_mtu_sizes[0]) {
836 for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
837 if (val <= sctp_mtu_sizes[i]) {
841 KASSERT((sctp_mtu_sizes[i - 1] & 0x00000003) == 0,
842 ("sctp_mtu_sizes[%u] not a multiple of 4", i - 1));
843 return (sctp_mtu_sizes[i - 1]);
847 * Return the smallest MTU in sctp_mtu_sizes larger than val.
848 * If val is larger than the maximum, just return the largest multiple of 4 smaller
850 * Ensure that the result is a multiple of 4.
853 sctp_get_next_mtu(uint32_t val)
855 /* select another MTU that is just bigger than this one */
859 for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
860 if (val < sctp_mtu_sizes[i]) {
861 KASSERT((sctp_mtu_sizes[i] & 0x00000003) == 0,
862 ("sctp_mtu_sizes[%u] not a multiple of 4", i));
863 return (sctp_mtu_sizes[i]);
870 sctp_fill_random_store(struct sctp_pcb *m)
873 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
874 * our counter. The result becomes our good random numbers and we
875 * then setup to give these out. Note that we do no locking to
876 * protect this. This is ok, since if competing folks call this we
877 * will get more gobbled gook in the random store which is what we
878 * want. There is a danger that two guys will use the same random
879 * numbers, but thats ok too since that is random as well :->
882 (void)sctp_hmac(SCTP_HMAC, (uint8_t *)m->random_numbers,
883 sizeof(m->random_numbers), (uint8_t *)&m->random_counter,
884 sizeof(m->random_counter), (uint8_t *)m->random_store);
889 sctp_select_initial_TSN(struct sctp_pcb *inp)
892 * A true implementation should use random selection process to get
893 * the initial stream sequence number, using RFC1750 as a good
898 int store_at, new_store;
900 if (inp->initial_sequence_debug != 0) {
903 ret = inp->initial_sequence_debug;
904 inp->initial_sequence_debug++;
908 store_at = inp->store_at;
909 new_store = store_at + sizeof(uint32_t);
910 if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
913 if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
916 if (new_store == 0) {
917 /* Refill the random store */
918 sctp_fill_random_store(inp);
920 p = &inp->random_store[store_at];
927 sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
933 (void)SCTP_GETTIME_TIMEVAL(&now);
936 x = sctp_select_initial_TSN(&inp->sctp_ep);
941 if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
949 sctp_map_assoc_state(int kernel_state)
953 if (kernel_state & SCTP_STATE_WAS_ABORTED) {
954 user_state = SCTP_CLOSED;
955 } else if (kernel_state & SCTP_STATE_SHUTDOWN_PENDING) {
956 user_state = SCTP_SHUTDOWN_PENDING;
958 switch (kernel_state & SCTP_STATE_MASK) {
959 case SCTP_STATE_EMPTY:
960 user_state = SCTP_CLOSED;
962 case SCTP_STATE_INUSE:
963 user_state = SCTP_CLOSED;
965 case SCTP_STATE_COOKIE_WAIT:
966 user_state = SCTP_COOKIE_WAIT;
968 case SCTP_STATE_COOKIE_ECHOED:
969 user_state = SCTP_COOKIE_ECHOED;
971 case SCTP_STATE_OPEN:
972 user_state = SCTP_ESTABLISHED;
974 case SCTP_STATE_SHUTDOWN_SENT:
975 user_state = SCTP_SHUTDOWN_SENT;
977 case SCTP_STATE_SHUTDOWN_RECEIVED:
978 user_state = SCTP_SHUTDOWN_RECEIVED;
980 case SCTP_STATE_SHUTDOWN_ACK_SENT:
981 user_state = SCTP_SHUTDOWN_ACK_SENT;
984 user_state = SCTP_CLOSED;
992 sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
993 uint32_t override_tag, uint32_t vrf_id, uint16_t o_strms)
995 struct sctp_association *asoc;
998 * Anything set to zero is taken care of by the allocation routine's
1003 * Up front select what scoping to apply on addresses I tell my peer
1004 * Not sure what to do with these right now, we will need to come up
1005 * with a way to set them. We may need to pass them through from the
1006 * caller in the sctp_aloc_assoc() function.
1009 #if defined(SCTP_DETAILED_STR_STATS)
1014 /* init all variables to a known value. */
1015 SCTP_SET_STATE(stcb, SCTP_STATE_INUSE);
1016 asoc->max_burst = inp->sctp_ep.max_burst;
1017 asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
1018 asoc->heart_beat_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1019 asoc->cookie_life = inp->sctp_ep.def_cookie_life;
1020 asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
1021 asoc->ecn_supported = inp->ecn_supported;
1022 asoc->prsctp_supported = inp->prsctp_supported;
1023 asoc->idata_supported = inp->idata_supported;
1024 asoc->auth_supported = inp->auth_supported;
1025 asoc->asconf_supported = inp->asconf_supported;
1026 asoc->reconfig_supported = inp->reconfig_supported;
1027 asoc->nrsack_supported = inp->nrsack_supported;
1028 asoc->pktdrop_supported = inp->pktdrop_supported;
1029 asoc->idata_supported = inp->idata_supported;
1030 asoc->sctp_cmt_pf = (uint8_t)0;
1031 asoc->sctp_frag_point = inp->sctp_frag_point;
1032 asoc->sctp_features = inp->sctp_features;
1033 asoc->default_dscp = inp->sctp_ep.default_dscp;
1034 asoc->max_cwnd = inp->max_cwnd;
1036 if (inp->sctp_ep.default_flowlabel) {
1037 asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
1039 if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
1040 asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
1041 asoc->default_flowlabel &= 0x000fffff;
1042 asoc->default_flowlabel |= 0x80000000;
1044 asoc->default_flowlabel = 0;
1048 asoc->sb_send_resv = 0;
1050 asoc->my_vtag = override_tag;
1052 asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
1054 /* Get the nonce tags */
1055 asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1056 asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1057 asoc->vrf_id = vrf_id;
1059 #ifdef SCTP_ASOCLOG_OF_TSNS
1060 asoc->tsn_in_at = 0;
1061 asoc->tsn_out_at = 0;
1062 asoc->tsn_in_wrapped = 0;
1063 asoc->tsn_out_wrapped = 0;
1064 asoc->cumack_log_at = 0;
1065 asoc->cumack_log_atsnt = 0;
1067 #ifdef SCTP_FS_SPEC_LOG
1071 asoc->assoc_up_sent = 0;
1072 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
1073 sctp_select_initial_TSN(&inp->sctp_ep);
1074 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1075 /* we are optimisitic here */
1076 asoc->peer_supports_nat = 0;
1077 asoc->sent_queue_retran_cnt = 0;
1080 asoc->last_net_cmt_send_started = NULL;
1082 /* This will need to be adjusted */
1083 asoc->last_acked_seq = asoc->init_seq_number - 1;
1084 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1085 asoc->asconf_seq_in = asoc->last_acked_seq;
1087 /* here we are different, we hold the next one we expect */
1088 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
1090 asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
1091 asoc->initial_rto = inp->sctp_ep.initial_rto;
1093 asoc->default_mtu = inp->sctp_ep.default_mtu;
1094 asoc->max_init_times = inp->sctp_ep.max_init_times;
1095 asoc->max_send_times = inp->sctp_ep.max_send_times;
1096 asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1097 asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1098 asoc->free_chunk_cnt = 0;
1100 asoc->iam_blocking = 0;
1101 asoc->context = inp->sctp_context;
1102 asoc->local_strreset_support = inp->local_strreset_support;
1103 asoc->def_send = inp->def_send;
1104 asoc->delayed_ack = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1105 asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1106 asoc->pr_sctp_cnt = 0;
1107 asoc->total_output_queue_size = 0;
1109 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1110 asoc->scope.ipv6_addr_legal = 1;
1111 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1112 asoc->scope.ipv4_addr_legal = 1;
1114 asoc->scope.ipv4_addr_legal = 0;
1117 asoc->scope.ipv6_addr_legal = 0;
1118 asoc->scope.ipv4_addr_legal = 1;
1121 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1122 asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1124 asoc->smallest_mtu = inp->sctp_frag_point;
1125 asoc->minrto = inp->sctp_ep.sctp_minrto;
1126 asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1128 asoc->stream_locked_on = 0;
1129 asoc->ecn_echo_cnt_onq = 0;
1130 asoc->stream_locked = 0;
1132 asoc->send_sack = 1;
1134 LIST_INIT(&asoc->sctp_restricted_addrs);
1136 TAILQ_INIT(&asoc->nets);
1137 TAILQ_INIT(&asoc->pending_reply_queue);
1138 TAILQ_INIT(&asoc->asconf_ack_sent);
1139 /* Setup to fill the hb random cache at first HB */
1140 asoc->hb_random_idx = 4;
1142 asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1144 stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1145 stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1147 stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1148 stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1151 * Now the stream parameters, here we allocate space for all streams
1152 * that we request by default.
1154 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1156 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1157 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1159 if (asoc->strmout == NULL) {
1160 /* big trouble no memory */
1161 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1164 for (i = 0; i < asoc->streamoutcnt; i++) {
1166 * inbound side must be set to 0xffff, also NOTE when we get
1167 * the INIT-ACK back (for INIT sender) we MUST reduce the
1168 * count (streamoutcnt) but first check if we sent to any of
1169 * the upper streams that were dropped (if some were). Those
1170 * that were dropped must be notified to the upper layer as
1173 asoc->strmout[i].next_mid_ordered = 0;
1174 asoc->strmout[i].next_mid_unordered = 0;
1175 TAILQ_INIT(&asoc->strmout[i].outqueue);
1176 asoc->strmout[i].chunks_on_queues = 0;
1177 #if defined(SCTP_DETAILED_STR_STATS)
1178 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1179 asoc->strmout[i].abandoned_sent[j] = 0;
1180 asoc->strmout[i].abandoned_unsent[j] = 0;
1183 asoc->strmout[i].abandoned_sent[0] = 0;
1184 asoc->strmout[i].abandoned_unsent[0] = 0;
1186 asoc->strmout[i].sid = i;
1187 asoc->strmout[i].last_msg_incomplete = 0;
1188 asoc->strmout[i].state = SCTP_STREAM_OPENING;
1189 asoc->ss_functions.sctp_ss_init_stream(stcb, &asoc->strmout[i], NULL);
1191 asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1193 /* Now the mapping array */
1194 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1195 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1197 if (asoc->mapping_array == NULL) {
1198 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1199 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1202 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1203 SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1205 if (asoc->nr_mapping_array == NULL) {
1206 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1207 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1208 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1211 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1213 /* Now the init of the other outqueues */
1214 TAILQ_INIT(&asoc->free_chunks);
1215 TAILQ_INIT(&asoc->control_send_queue);
1216 TAILQ_INIT(&asoc->asconf_send_queue);
1217 TAILQ_INIT(&asoc->send_queue);
1218 TAILQ_INIT(&asoc->sent_queue);
1219 TAILQ_INIT(&asoc->resetHead);
1220 asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1221 TAILQ_INIT(&asoc->asconf_queue);
1222 /* authentication fields */
1223 asoc->authinfo.random = NULL;
1224 asoc->authinfo.active_keyid = 0;
1225 asoc->authinfo.assoc_key = NULL;
1226 asoc->authinfo.assoc_keyid = 0;
1227 asoc->authinfo.recv_key = NULL;
1228 asoc->authinfo.recv_keyid = 0;
1229 LIST_INIT(&asoc->shared_keys);
1230 asoc->marked_retrans = 0;
1231 asoc->port = inp->sctp_ep.port;
1235 asoc->timoshutdown = 0;
1236 asoc->timoheartbeat = 0;
1237 asoc->timocookie = 0;
1238 asoc->timoshutdownack = 0;
1239 (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1240 asoc->discontinuity_time = asoc->start_time;
1241 for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1242 asoc->abandoned_unsent[i] = 0;
1243 asoc->abandoned_sent[i] = 0;
1246 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
1247 * freed later when the association is freed.
1253 sctp_print_mapping_array(struct sctp_association *asoc)
1255 unsigned int i, limit;
1257 SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
1258 asoc->mapping_array_size,
1259 asoc->mapping_array_base_tsn,
1260 asoc->cumulative_tsn,
1261 asoc->highest_tsn_inside_map,
1262 asoc->highest_tsn_inside_nr_map);
1263 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1264 if (asoc->mapping_array[limit - 1] != 0) {
1268 SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1269 for (i = 0; i < limit; i++) {
1270 SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1274 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1275 if (asoc->nr_mapping_array[limit - 1]) {
1279 SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1280 for (i = 0; i < limit; i++) {
1281 SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1288 sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1290 /* mapping array needs to grow */
1291 uint8_t *new_array1, *new_array2;
1294 new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1295 SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1296 SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1297 if ((new_array1 == NULL) || (new_array2 == NULL)) {
1298 /* can't get more, forget it */
1299 SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1301 SCTP_FREE(new_array1, SCTP_M_MAP);
1304 SCTP_FREE(new_array2, SCTP_M_MAP);
1308 memset(new_array1, 0, new_size);
1309 memset(new_array2, 0, new_size);
1310 memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1311 memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1312 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1313 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1314 asoc->mapping_array = new_array1;
1315 asoc->nr_mapping_array = new_array2;
1316 asoc->mapping_array_size = new_size;
1322 sctp_iterator_work(struct sctp_iterator *it)
1324 int iteration_count = 0;
1327 struct sctp_inpcb *tinp;
1329 SCTP_INP_INFO_RLOCK();
1330 SCTP_ITERATOR_LOCK();
1331 sctp_it_ctl.cur_it = it;
1333 SCTP_INP_RLOCK(it->inp);
1334 SCTP_INP_DECR_REF(it->inp);
1336 if (it->inp == NULL) {
1337 /* iterator is complete */
1339 sctp_it_ctl.cur_it = NULL;
1340 SCTP_ITERATOR_UNLOCK();
1341 SCTP_INP_INFO_RUNLOCK();
1342 if (it->function_atend != NULL) {
1343 (*it->function_atend) (it->pointer, it->val);
1345 SCTP_FREE(it, SCTP_M_ITER);
1352 SCTP_INP_RLOCK(it->inp);
1354 while (((it->pcb_flags) &&
1355 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1356 ((it->pcb_features) &&
1357 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1358 /* endpoint flags or features don't match, so keep looking */
1359 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1360 SCTP_INP_RUNLOCK(it->inp);
1361 goto done_with_iterator;
1364 it->inp = LIST_NEXT(it->inp, sctp_list);
1365 SCTP_INP_RUNLOCK(tinp);
1366 if (it->inp == NULL) {
1367 goto done_with_iterator;
1369 SCTP_INP_RLOCK(it->inp);
1371 /* now go through each assoc which is in the desired state */
1372 if (it->done_current_ep == 0) {
1373 if (it->function_inp != NULL)
1374 inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1375 it->done_current_ep = 1;
1377 if (it->stcb == NULL) {
1378 /* run the per instance function */
1379 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1381 if ((inp_skip) || it->stcb == NULL) {
1382 if (it->function_inp_end != NULL) {
1383 inp_skip = (*it->function_inp_end) (it->inp,
1387 SCTP_INP_RUNLOCK(it->inp);
1391 SCTP_TCB_LOCK(it->stcb);
1392 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1393 /* not in the right state... keep looking */
1394 SCTP_TCB_UNLOCK(it->stcb);
1397 /* see if we have limited out the iterator loop */
1399 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1400 /* Pause to let others grab the lock */
1401 atomic_add_int(&it->stcb->asoc.refcnt, 1);
1402 SCTP_TCB_UNLOCK(it->stcb);
1403 SCTP_INP_INCR_REF(it->inp);
1404 SCTP_INP_RUNLOCK(it->inp);
1405 SCTP_ITERATOR_UNLOCK();
1406 SCTP_INP_INFO_RUNLOCK();
1407 SCTP_INP_INFO_RLOCK();
1408 SCTP_ITERATOR_LOCK();
1409 if (sctp_it_ctl.iterator_flags) {
1410 /* We won't be staying here */
1411 SCTP_INP_DECR_REF(it->inp);
1412 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1413 if (sctp_it_ctl.iterator_flags &
1414 SCTP_ITERATOR_STOP_CUR_IT) {
1415 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1416 goto done_with_iterator;
1418 if (sctp_it_ctl.iterator_flags &
1419 SCTP_ITERATOR_STOP_CUR_INP) {
1420 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1423 /* If we reach here huh? */
1424 SCTP_PRINTF("Unknown it ctl flag %x\n",
1425 sctp_it_ctl.iterator_flags);
1426 sctp_it_ctl.iterator_flags = 0;
1428 SCTP_INP_RLOCK(it->inp);
1429 SCTP_INP_DECR_REF(it->inp);
1430 SCTP_TCB_LOCK(it->stcb);
1431 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1432 iteration_count = 0;
1435 /* run function on this one */
1436 (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1439 * we lie here, it really needs to have its own type but
1440 * first I must verify that this won't effect things :-0
1442 if (it->no_chunk_output == 0)
1443 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1445 SCTP_TCB_UNLOCK(it->stcb);
1447 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1448 if (it->stcb == NULL) {
1449 /* Run last function */
1450 if (it->function_inp_end != NULL) {
1451 inp_skip = (*it->function_inp_end) (it->inp,
1457 SCTP_INP_RUNLOCK(it->inp);
1459 /* done with all assocs on this endpoint, move on to next endpoint */
1460 it->done_current_ep = 0;
1461 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1464 it->inp = LIST_NEXT(it->inp, sctp_list);
1466 if (it->inp == NULL) {
1467 goto done_with_iterator;
1469 goto select_a_new_ep;
1473 sctp_iterator_worker(void)
1475 struct sctp_iterator *it, *nit;
1477 /* This function is called with the WQ lock in place */
1479 sctp_it_ctl.iterator_running = 1;
1480 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
1481 /* now lets work on this one */
1482 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
1483 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1484 CURVNET_SET(it->vn);
1485 sctp_iterator_work(it);
1487 SCTP_IPI_ITERATOR_WQ_LOCK();
1488 /* sa_ignore FREED_MEMORY */
1490 sctp_it_ctl.iterator_running = 0;
1496 sctp_handle_addr_wq(void)
1498 /* deal with the ADDR wq from the rtsock calls */
1499 struct sctp_laddr *wi, *nwi;
1500 struct sctp_asconf_iterator *asc;
1502 SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1503 sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
1505 /* Try later, no memory */
1506 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1507 (struct sctp_inpcb *)NULL,
1508 (struct sctp_tcb *)NULL,
1509 (struct sctp_nets *)NULL);
1512 LIST_INIT(&asc->list_of_work);
1515 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
1516 LIST_REMOVE(wi, sctp_nxt_addr);
1517 LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
1521 if (asc->cnt == 0) {
1522 SCTP_FREE(asc, SCTP_M_ASC_IT);
1526 ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
1527 sctp_asconf_iterator_stcb,
1528 NULL, /* No ep end for boundall */
1529 SCTP_PCB_FLAGS_BOUNDALL,
1530 SCTP_PCB_ANY_FEATURES,
1531 SCTP_ASOC_ANY_STATE,
1533 sctp_asconf_iterator_end, NULL, 0);
1535 SCTP_PRINTF("Failed to initiate iterator for handle_addr_wq\n");
1537 * Freeing if we are stopping or put back on the
1540 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
1541 sctp_asconf_iterator_end(asc, 0);
1543 LIST_FOREACH(wi, &asc->list_of_work, sctp_nxt_addr) {
1544 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
1546 SCTP_FREE(asc, SCTP_M_ASC_IT);
1553 sctp_timeout_handler(void *t)
1555 struct sctp_inpcb *inp;
1556 struct sctp_tcb *stcb;
1557 struct sctp_nets *net;
1558 struct sctp_timer *tmr;
1559 struct mbuf *op_err;
1560 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1566 tmr = (struct sctp_timer *)t;
1567 inp = (struct sctp_inpcb *)tmr->ep;
1568 stcb = (struct sctp_tcb *)tmr->tcb;
1569 net = (struct sctp_nets *)tmr->net;
1570 CURVNET_SET((struct vnet *)tmr->vnet);
1573 #ifdef SCTP_AUDITING_ENABLED
1574 sctp_audit_log(0xF0, (uint8_t)tmr->type);
1575 sctp_auditing(3, inp, stcb, net);
1578 /* sanity checks... */
1579 if (tmr->self != (void *)tmr) {
1581 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1587 tmr->stopped_from = 0xa001;
1588 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1590 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1596 tmr->stopped_from = 0xa002;
1597 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1601 /* if this is an iterator timeout, get the struct and clear inp */
1602 tmr->stopped_from = 0xa003;
1604 SCTP_INP_INCR_REF(inp);
1605 if ((inp->sctp_socket == NULL) &&
1606 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1607 (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1608 (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1609 (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1610 (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1611 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1612 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1613 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1614 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))) {
1615 SCTP_INP_DECR_REF(inp);
1620 tmr->stopped_from = 0xa004;
1622 atomic_add_int(&stcb->asoc.refcnt, 1);
1623 if (stcb->asoc.state == 0) {
1624 atomic_add_int(&stcb->asoc.refcnt, -1);
1626 SCTP_INP_DECR_REF(inp);
1633 tmr->stopped_from = 0xa005;
1634 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", type);
1635 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1637 SCTP_INP_DECR_REF(inp);
1640 atomic_add_int(&stcb->asoc.refcnt, -1);
1645 tmr->stopped_from = 0xa006;
1648 SCTP_TCB_LOCK(stcb);
1649 atomic_add_int(&stcb->asoc.refcnt, -1);
1650 if ((type != SCTP_TIMER_TYPE_ASOCKILL) &&
1651 ((stcb->asoc.state == 0) ||
1652 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1653 SCTP_TCB_UNLOCK(stcb);
1655 SCTP_INP_DECR_REF(inp);
1660 } else if (inp != NULL) {
1661 if (type != SCTP_TIMER_TYPE_INPKILL) {
1662 SCTP_INP_WLOCK(inp);
1665 SCTP_WQ_ADDR_LOCK();
1667 /* record in stopped what t-o occurred */
1668 tmr->stopped_from = type;
1670 /* mark as being serviced now */
1671 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1673 * Callout has been rescheduled.
1677 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1679 * Not active, so no action.
1683 SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1685 /* call the handler for the appropriate timer type */
1687 case SCTP_TIMER_TYPE_ADDR_WQ:
1688 sctp_handle_addr_wq();
1690 case SCTP_TIMER_TYPE_SEND:
1691 if ((stcb == NULL) || (inp == NULL)) {
1694 SCTP_STAT_INCR(sctps_timodata);
1695 stcb->asoc.timodata++;
1696 stcb->asoc.num_send_timers_up--;
1697 if (stcb->asoc.num_send_timers_up < 0) {
1698 stcb->asoc.num_send_timers_up = 0;
1700 SCTP_TCB_LOCK_ASSERT(stcb);
1701 if (sctp_t3rxt_timer(inp, stcb, net)) {
1702 /* no need to unlock on tcb its gone */
1706 SCTP_TCB_LOCK_ASSERT(stcb);
1707 #ifdef SCTP_AUDITING_ENABLED
1708 sctp_auditing(4, inp, stcb, net);
1710 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1711 if ((stcb->asoc.num_send_timers_up == 0) &&
1712 (stcb->asoc.sent_queue_cnt > 0)) {
1713 struct sctp_tmit_chunk *chk;
1716 * safeguard. If there on some on the sent queue
1717 * somewhere but no timers running something is
1718 * wrong... so we start a timer on the first chunk
1719 * on the send queue on whatever net it is sent to.
1721 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1722 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1726 case SCTP_TIMER_TYPE_INIT:
1727 if ((stcb == NULL) || (inp == NULL)) {
1730 SCTP_STAT_INCR(sctps_timoinit);
1731 stcb->asoc.timoinit++;
1732 if (sctp_t1init_timer(inp, stcb, net)) {
1733 /* no need to unlock on tcb its gone */
1736 /* We do output but not here */
1739 case SCTP_TIMER_TYPE_RECV:
1740 if ((stcb == NULL) || (inp == NULL)) {
1743 SCTP_STAT_INCR(sctps_timosack);
1744 stcb->asoc.timosack++;
1745 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1746 #ifdef SCTP_AUDITING_ENABLED
1747 sctp_auditing(4, inp, stcb, net);
1749 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1751 case SCTP_TIMER_TYPE_SHUTDOWN:
1752 if ((stcb == NULL) || (inp == NULL)) {
1755 if (sctp_shutdown_timer(inp, stcb, net)) {
1756 /* no need to unlock on tcb its gone */
1759 SCTP_STAT_INCR(sctps_timoshutdown);
1760 stcb->asoc.timoshutdown++;
1761 #ifdef SCTP_AUDITING_ENABLED
1762 sctp_auditing(4, inp, stcb, net);
1764 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1766 case SCTP_TIMER_TYPE_HEARTBEAT:
1767 if ((stcb == NULL) || (inp == NULL) || (net == NULL)) {
1770 SCTP_STAT_INCR(sctps_timoheartbeat);
1771 stcb->asoc.timoheartbeat++;
1772 if (sctp_heartbeat_timer(inp, stcb, net)) {
1773 /* no need to unlock on tcb its gone */
1776 #ifdef SCTP_AUDITING_ENABLED
1777 sctp_auditing(4, inp, stcb, net);
1779 if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1780 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1781 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1784 case SCTP_TIMER_TYPE_COOKIE:
1785 if ((stcb == NULL) || (inp == NULL)) {
1789 if (sctp_cookie_timer(inp, stcb, net)) {
1790 /* no need to unlock on tcb its gone */
1793 SCTP_STAT_INCR(sctps_timocookie);
1794 stcb->asoc.timocookie++;
1795 #ifdef SCTP_AUDITING_ENABLED
1796 sctp_auditing(4, inp, stcb, net);
1799 * We consider T3 and Cookie timer pretty much the same with
1800 * respect to where from in chunk_output.
1802 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1804 case SCTP_TIMER_TYPE_NEWCOOKIE:
1812 SCTP_STAT_INCR(sctps_timosecret);
1813 (void)SCTP_GETTIME_TIMEVAL(&tv);
1814 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1815 inp->sctp_ep.last_secret_number =
1816 inp->sctp_ep.current_secret_number;
1817 inp->sctp_ep.current_secret_number++;
1818 if (inp->sctp_ep.current_secret_number >=
1819 SCTP_HOW_MANY_SECRETS) {
1820 inp->sctp_ep.current_secret_number = 0;
1822 secret = (int)inp->sctp_ep.current_secret_number;
1823 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1824 inp->sctp_ep.secret_key[secret][i] =
1825 sctp_select_initial_TSN(&inp->sctp_ep);
1827 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1831 case SCTP_TIMER_TYPE_PATHMTURAISE:
1832 if ((stcb == NULL) || (inp == NULL)) {
1835 SCTP_STAT_INCR(sctps_timopathmtu);
1836 sctp_pathmtu_timer(inp, stcb, net);
1839 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1840 if ((stcb == NULL) || (inp == NULL)) {
1843 if (sctp_shutdownack_timer(inp, stcb, net)) {
1844 /* no need to unlock on tcb its gone */
1847 SCTP_STAT_INCR(sctps_timoshutdownack);
1848 stcb->asoc.timoshutdownack++;
1849 #ifdef SCTP_AUDITING_ENABLED
1850 sctp_auditing(4, inp, stcb, net);
1852 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1854 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1855 if ((stcb == NULL) || (inp == NULL)) {
1858 SCTP_STAT_INCR(sctps_timoshutdownguard);
1859 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
1860 "Shutdown guard timer expired");
1861 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
1862 /* no need to unlock on tcb its gone */
1865 case SCTP_TIMER_TYPE_STRRESET:
1866 if ((stcb == NULL) || (inp == NULL)) {
1869 if (sctp_strreset_timer(inp, stcb, net)) {
1870 /* no need to unlock on tcb its gone */
1873 SCTP_STAT_INCR(sctps_timostrmrst);
1874 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1876 case SCTP_TIMER_TYPE_ASCONF:
1877 if ((stcb == NULL) || (inp == NULL)) {
1880 if (sctp_asconf_timer(inp, stcb, net)) {
1881 /* no need to unlock on tcb its gone */
1884 SCTP_STAT_INCR(sctps_timoasconf);
1885 #ifdef SCTP_AUDITING_ENABLED
1886 sctp_auditing(4, inp, stcb, net);
1888 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1890 case SCTP_TIMER_TYPE_PRIM_DELETED:
1891 if ((stcb == NULL) || (inp == NULL)) {
1894 sctp_delete_prim_timer(inp, stcb, net);
1895 SCTP_STAT_INCR(sctps_timodelprim);
1898 case SCTP_TIMER_TYPE_AUTOCLOSE:
1899 if ((stcb == NULL) || (inp == NULL)) {
1902 SCTP_STAT_INCR(sctps_timoautoclose);
1903 sctp_autoclose_timer(inp, stcb, net);
1904 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1907 case SCTP_TIMER_TYPE_ASOCKILL:
1908 if ((stcb == NULL) || (inp == NULL)) {
1911 SCTP_STAT_INCR(sctps_timoassockill);
1912 /* Can we free it yet? */
1913 SCTP_INP_DECR_REF(inp);
1914 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
1915 SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1916 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1917 so = SCTP_INP_SO(inp);
1918 atomic_add_int(&stcb->asoc.refcnt, 1);
1919 SCTP_TCB_UNLOCK(stcb);
1920 SCTP_SOCKET_LOCK(so, 1);
1921 SCTP_TCB_LOCK(stcb);
1922 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1924 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
1925 SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1926 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1927 SCTP_SOCKET_UNLOCK(so, 1);
1930 * free asoc, always unlocks (or destroy's) so prevent
1931 * duplicate unlock or unlock of a free mtx :-0
1935 case SCTP_TIMER_TYPE_INPKILL:
1936 SCTP_STAT_INCR(sctps_timoinpkill);
1941 * special case, take away our increment since WE are the
1944 SCTP_INP_DECR_REF(inp);
1945 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
1946 SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1947 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
1948 SCTP_CALLED_FROM_INPKILL_TIMER);
1952 SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1956 #ifdef SCTP_AUDITING_ENABLED
1957 sctp_audit_log(0xF1, (uint8_t)type);
1959 sctp_auditing(5, inp, stcb, net);
1961 if ((did_output) && stcb) {
1963 * Now we need to clean up the control chunk chain if an
1964 * ECNE is on it. It must be marked as UNSENT again so next
1965 * call will continue to send it until such time that we get
1966 * a CWR, to remove it. It is, however, less likely that we
1967 * will find a ecn echo on the chain though.
1969 sctp_fix_ecn_echo(&stcb->asoc);
1973 SCTP_TCB_UNLOCK(stcb);
1974 } else if (inp != NULL) {
1975 SCTP_INP_WUNLOCK(inp);
1977 SCTP_WQ_ADDR_UNLOCK();
1982 SCTP_INP_DECR_REF(inp);
1986 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type = %d)\n", type);
1991 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1992 struct sctp_nets *net)
1995 struct sctp_timer *tmr;
1997 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
2002 SCTP_TCB_LOCK_ASSERT(stcb);
2005 case SCTP_TIMER_TYPE_ADDR_WQ:
2006 /* Only 1 tick away :-) */
2007 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2008 to_ticks = SCTP_ADDRESS_TICK_DELAY;
2010 case SCTP_TIMER_TYPE_SEND:
2011 /* Here we use the RTO timer */
2015 if ((stcb == NULL) || (net == NULL)) {
2018 tmr = &net->rxt_timer;
2019 if (net->RTO == 0) {
2020 rto_val = stcb->asoc.initial_rto;
2024 to_ticks = MSEC_TO_TICKS(rto_val);
2027 case SCTP_TIMER_TYPE_INIT:
2029 * Here we use the INIT timer default usually about 1
2032 if ((stcb == NULL) || (net == NULL)) {
2035 tmr = &net->rxt_timer;
2036 if (net->RTO == 0) {
2037 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2039 to_ticks = MSEC_TO_TICKS(net->RTO);
2042 case SCTP_TIMER_TYPE_RECV:
2044 * Here we use the Delayed-Ack timer value from the inp
2045 * ususually about 200ms.
2050 tmr = &stcb->asoc.dack_timer;
2051 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
2053 case SCTP_TIMER_TYPE_SHUTDOWN:
2054 /* Here we use the RTO of the destination. */
2055 if ((stcb == NULL) || (net == NULL)) {
2058 if (net->RTO == 0) {
2059 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2061 to_ticks = MSEC_TO_TICKS(net->RTO);
2063 tmr = &net->rxt_timer;
2065 case SCTP_TIMER_TYPE_HEARTBEAT:
2067 * the net is used here so that we can add in the RTO. Even
2068 * though we use a different timer. We also add the HB timer
2069 * PLUS a random jitter.
2071 if ((stcb == NULL) || (net == NULL)) {
2077 if ((net->dest_state & SCTP_ADDR_NOHB) &&
2078 !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
2081 if (net->RTO == 0) {
2082 to_ticks = stcb->asoc.initial_rto;
2084 to_ticks = net->RTO;
2086 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2087 jitter = rndval % to_ticks;
2088 if (jitter >= (to_ticks >> 1)) {
2089 to_ticks = to_ticks + (jitter - (to_ticks >> 1));
2091 to_ticks = to_ticks - jitter;
2093 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2094 !(net->dest_state & SCTP_ADDR_PF)) {
2095 to_ticks += net->heart_beat_delay;
2098 * Now we must convert the to_ticks that are now in
2101 to_ticks = MSEC_TO_TICKS(to_ticks);
2102 tmr = &net->hb_timer;
2105 case SCTP_TIMER_TYPE_COOKIE:
2107 * Here we can use the RTO timer from the network since one
2108 * RTT was compelete. If a retran happened then we will be
2109 * using the RTO initial value.
2111 if ((stcb == NULL) || (net == NULL)) {
2114 if (net->RTO == 0) {
2115 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2117 to_ticks = MSEC_TO_TICKS(net->RTO);
2119 tmr = &net->rxt_timer;
2121 case SCTP_TIMER_TYPE_NEWCOOKIE:
2123 * nothing needed but the endpoint here ususually about 60
2126 tmr = &inp->sctp_ep.signature_change;
2127 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2129 case SCTP_TIMER_TYPE_ASOCKILL:
2133 tmr = &stcb->asoc.strreset_timer;
2134 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2136 case SCTP_TIMER_TYPE_INPKILL:
2138 * The inp is setup to die. We re-use the signature_chage
2139 * timer since that has stopped and we are in the GONE
2142 tmr = &inp->sctp_ep.signature_change;
2143 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2145 case SCTP_TIMER_TYPE_PATHMTURAISE:
2147 * Here we use the value found in the EP for PMTU ususually
2150 if ((stcb == NULL) || (net == NULL)) {
2153 if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2156 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2157 tmr = &net->pmtu_timer;
2159 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2160 /* Here we use the RTO of the destination */
2161 if ((stcb == NULL) || (net == NULL)) {
2164 if (net->RTO == 0) {
2165 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2167 to_ticks = MSEC_TO_TICKS(net->RTO);
2169 tmr = &net->rxt_timer;
2171 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2173 * Here we use the endpoints shutdown guard timer usually
2179 if (inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] == 0) {
2180 to_ticks = 5 * MSEC_TO_TICKS(stcb->asoc.maxrto);
2182 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2184 tmr = &stcb->asoc.shut_guard_timer;
2186 case SCTP_TIMER_TYPE_STRRESET:
2188 * Here the timer comes from the stcb but its value is from
2191 if ((stcb == NULL) || (net == NULL)) {
2194 if (net->RTO == 0) {
2195 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2197 to_ticks = MSEC_TO_TICKS(net->RTO);
2199 tmr = &stcb->asoc.strreset_timer;
2201 case SCTP_TIMER_TYPE_ASCONF:
2203 * Here the timer comes from the stcb but its value is from
2206 if ((stcb == NULL) || (net == NULL)) {
2209 if (net->RTO == 0) {
2210 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2212 to_ticks = MSEC_TO_TICKS(net->RTO);
2214 tmr = &stcb->asoc.asconf_timer;
2216 case SCTP_TIMER_TYPE_PRIM_DELETED:
2217 if ((stcb == NULL) || (net != NULL)) {
2220 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2221 tmr = &stcb->asoc.delete_prim_timer;
2223 case SCTP_TIMER_TYPE_AUTOCLOSE:
2227 if (stcb->asoc.sctp_autoclose_ticks == 0) {
2229 * Really an error since stcb is NOT set to
2234 to_ticks = stcb->asoc.sctp_autoclose_ticks;
2235 tmr = &stcb->asoc.autoclose_timer;
2238 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2243 if ((to_ticks <= 0) || (tmr == NULL)) {
2244 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2245 __func__, t_type, to_ticks, (void *)tmr);
2248 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2250 * we do NOT allow you to have it already running. if it is
2251 * we leave the current one up unchanged
2255 /* At this point we can proceed */
2256 if (t_type == SCTP_TIMER_TYPE_SEND) {
2257 stcb->asoc.num_send_timers_up++;
2259 tmr->stopped_from = 0;
2261 tmr->ep = (void *)inp;
2262 tmr->tcb = (void *)stcb;
2263 tmr->net = (void *)net;
2264 tmr->self = (void *)tmr;
2265 tmr->vnet = (void *)curvnet;
2266 tmr->ticks = sctp_get_tick_count();
2267 (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2272 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2273 struct sctp_nets *net, uint32_t from)
2275 struct sctp_timer *tmr;
2277 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2283 SCTP_TCB_LOCK_ASSERT(stcb);
2286 case SCTP_TIMER_TYPE_ADDR_WQ:
2287 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2289 case SCTP_TIMER_TYPE_SEND:
2290 if ((stcb == NULL) || (net == NULL)) {
2293 tmr = &net->rxt_timer;
2295 case SCTP_TIMER_TYPE_INIT:
2296 if ((stcb == NULL) || (net == NULL)) {
2299 tmr = &net->rxt_timer;
2301 case SCTP_TIMER_TYPE_RECV:
2305 tmr = &stcb->asoc.dack_timer;
2307 case SCTP_TIMER_TYPE_SHUTDOWN:
2308 if ((stcb == NULL) || (net == NULL)) {
2311 tmr = &net->rxt_timer;
2313 case SCTP_TIMER_TYPE_HEARTBEAT:
2314 if ((stcb == NULL) || (net == NULL)) {
2317 tmr = &net->hb_timer;
2319 case SCTP_TIMER_TYPE_COOKIE:
2320 if ((stcb == NULL) || (net == NULL)) {
2323 tmr = &net->rxt_timer;
2325 case SCTP_TIMER_TYPE_NEWCOOKIE:
2326 /* nothing needed but the endpoint here */
2327 tmr = &inp->sctp_ep.signature_change;
2329 * We re-use the newcookie timer for the INP kill timer. We
2330 * must assure that we do not kill it by accident.
2333 case SCTP_TIMER_TYPE_ASOCKILL:
2335 * Stop the asoc kill timer.
2340 tmr = &stcb->asoc.strreset_timer;
2343 case SCTP_TIMER_TYPE_INPKILL:
2345 * The inp is setup to die. We re-use the signature_chage
2346 * timer since that has stopped and we are in the GONE
2349 tmr = &inp->sctp_ep.signature_change;
2351 case SCTP_TIMER_TYPE_PATHMTURAISE:
2352 if ((stcb == NULL) || (net == NULL)) {
2355 tmr = &net->pmtu_timer;
2357 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2358 if ((stcb == NULL) || (net == NULL)) {
2361 tmr = &net->rxt_timer;
2363 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2367 tmr = &stcb->asoc.shut_guard_timer;
2369 case SCTP_TIMER_TYPE_STRRESET:
2373 tmr = &stcb->asoc.strreset_timer;
2375 case SCTP_TIMER_TYPE_ASCONF:
2379 tmr = &stcb->asoc.asconf_timer;
2381 case SCTP_TIMER_TYPE_PRIM_DELETED:
2385 tmr = &stcb->asoc.delete_prim_timer;
2387 case SCTP_TIMER_TYPE_AUTOCLOSE:
2391 tmr = &stcb->asoc.autoclose_timer;
2394 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2401 if ((tmr->type != t_type) && tmr->type) {
2403 * Ok we have a timer that is under joint use. Cookie timer
2404 * per chance with the SEND timer. We therefore are NOT
2405 * running the timer that the caller wants stopped. So just
2410 if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2411 stcb->asoc.num_send_timers_up--;
2412 if (stcb->asoc.num_send_timers_up < 0) {
2413 stcb->asoc.num_send_timers_up = 0;
2417 tmr->stopped_from = from;
2418 (void)SCTP_OS_TIMER_STOP(&tmr->timer);
2423 sctp_calculate_len(struct mbuf *m)
2430 tlen += SCTP_BUF_LEN(at);
2431 at = SCTP_BUF_NEXT(at);
2437 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2438 struct sctp_association *asoc, uint32_t mtu)
2441 * Reset the P-MTU size on this association, this involves changing
2442 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2443 * allow the DF flag to be cleared.
2445 struct sctp_tmit_chunk *chk;
2446 unsigned int eff_mtu, ovh;
2448 asoc->smallest_mtu = mtu;
2449 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2450 ovh = SCTP_MIN_OVERHEAD;
2452 ovh = SCTP_MIN_V4_OVERHEAD;
2454 eff_mtu = mtu - ovh;
2455 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2456 if (chk->send_size > eff_mtu) {
2457 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2460 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2461 if (chk->send_size > eff_mtu) {
2462 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2469 * given an association and starting time of the current RTT period return
2470 * RTO in number of msecs net should point to the current network
2474 sctp_calculate_rto(struct sctp_tcb *stcb,
2475 struct sctp_association *asoc,
2476 struct sctp_nets *net,
2477 struct timeval *old,
2481 * given an association and the starting time of the current RTT
2482 * period (in value1/value2) return RTO in number of msecs.
2484 int32_t rtt; /* RTT in ms */
2486 int first_measure = 0;
2489 /************************/
2490 /* 1. calculate new RTT */
2491 /************************/
2492 /* get the current time */
2493 if (stcb->asoc.use_precise_time) {
2494 (void)SCTP_GETPTIME_TIMEVAL(&now);
2496 (void)SCTP_GETTIME_TIMEVAL(&now);
2498 timevalsub(&now, old);
2499 /* store the current RTT in us */
2500 net->rtt = (uint64_t)1000000 * (uint64_t)now.tv_sec +
2501 (uint64_t)now.tv_usec;
2502 /* compute rtt in ms */
2503 rtt = (int32_t)(net->rtt / 1000);
2504 if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2506 * Tell the CC module that a new update has just occurred
2509 (*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2512 * Do we need to determine the lan? We do this only on sacks i.e.
2513 * RTT being determined from data not non-data (HB/INIT->INITACK).
2515 if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2516 (net->lan_type == SCTP_LAN_UNKNOWN)) {
2517 if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2518 net->lan_type = SCTP_LAN_INTERNET;
2520 net->lan_type = SCTP_LAN_LOCAL;
2524 /***************************/
2525 /* 2. update RTTVAR & SRTT */
2526 /***************************/
2528 * Compute the scaled average lastsa and the
2529 * scaled variance lastsv as described in van Jacobson
2530 * Paper "Congestion Avoidance and Control", Annex A.
2532 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
2533 * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar
2535 if (net->RTO_measured) {
2536 rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2541 rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2543 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2544 rto_logging(net, SCTP_LOG_RTTVAR);
2547 /* First RTO measurment */
2548 net->RTO_measured = 1;
2550 net->lastsa = rtt << SCTP_RTT_SHIFT;
2551 net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2552 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2553 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2556 if (net->lastsv == 0) {
2557 net->lastsv = SCTP_CLOCK_GRANULARITY;
2559 new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2560 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2561 (stcb->asoc.sat_network_lockout == 0)) {
2562 stcb->asoc.sat_network = 1;
2563 } else if ((!first_measure) && stcb->asoc.sat_network) {
2564 stcb->asoc.sat_network = 0;
2565 stcb->asoc.sat_network_lockout = 1;
2567 /* bound it, per C6/C7 in Section 5.3.1 */
2568 if (new_rto < stcb->asoc.minrto) {
2569 new_rto = stcb->asoc.minrto;
2571 if (new_rto > stcb->asoc.maxrto) {
2572 new_rto = stcb->asoc.maxrto;
2574 /* we are now returning the RTO */
2579 * return a pointer to a contiguous piece of data from the given mbuf chain
2580 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2581 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2582 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2585 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t *in_ptr)
2591 if ((off < 0) || (len <= 0))
2594 /* find the desired start location */
2595 while ((m != NULL) && (off > 0)) {
2596 if (off < SCTP_BUF_LEN(m))
2598 off -= SCTP_BUF_LEN(m);
2599 m = SCTP_BUF_NEXT(m);
2604 /* is the current mbuf large enough (eg. contiguous)? */
2605 if ((SCTP_BUF_LEN(m) - off) >= len) {
2606 return (mtod(m, caddr_t)+off);
2608 /* else, it spans more than one mbuf, so save a temp copy... */
2609 while ((m != NULL) && (len > 0)) {
2610 count = min(SCTP_BUF_LEN(m) - off, len);
2611 memcpy(ptr, mtod(m, caddr_t)+off, count);
2615 m = SCTP_BUF_NEXT(m);
2617 if ((m == NULL) && (len > 0))
2620 return ((caddr_t)in_ptr);
2626 struct sctp_paramhdr *
2627 sctp_get_next_param(struct mbuf *m,
2629 struct sctp_paramhdr *pull,
2632 /* This just provides a typed signature to Peter's Pull routine */
2633 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2639 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2641 struct mbuf *m_last;
2647 if (padlen <= M_TRAILINGSPACE(m)) {
2649 * The easy way. We hope the majority of the time we hit
2654 /* Hard way we must grow the mbuf chain */
2655 m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
2656 if (m_last == NULL) {
2659 SCTP_BUF_LEN(m_last) = 0;
2660 SCTP_BUF_NEXT(m_last) = NULL;
2661 SCTP_BUF_NEXT(m) = m_last;
2663 dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
2664 SCTP_BUF_LEN(m_last) += padlen;
2665 memset(dp, 0, padlen);
2670 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2672 /* find the last mbuf in chain and pad it */
2675 if (last_mbuf != NULL) {
2676 return (sctp_add_pad_tombuf(last_mbuf, padval));
2678 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
2679 if (SCTP_BUF_NEXT(m_at) == NULL) {
2680 return (sctp_add_pad_tombuf(m_at, padval));
2688 sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
2689 uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
2690 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2695 struct mbuf *m_notify;
2696 struct sctp_assoc_change *sac;
2697 struct sctp_queued_to_read *control;
2698 unsigned int notif_len;
2701 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2708 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2709 notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
2710 if (abort != NULL) {
2711 abort_len = ntohs(abort->ch.chunk_length);
2713 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
2716 if (abort_len > SCTP_CHUNK_BUFFER_SIZE) {
2717 abort_len = SCTP_CHUNK_BUFFER_SIZE;
2722 if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2723 notif_len += SCTP_ASSOC_SUPPORTS_MAX;
2724 } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2725 notif_len += abort_len;
2727 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2728 if (m_notify == NULL) {
2729 /* Retry with smaller value. */
2730 notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
2731 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2732 if (m_notify == NULL) {
2736 SCTP_BUF_NEXT(m_notify) = NULL;
2737 sac = mtod(m_notify, struct sctp_assoc_change *);
2738 memset(sac, 0, notif_len);
2739 sac->sac_type = SCTP_ASSOC_CHANGE;
2741 sac->sac_length = sizeof(struct sctp_assoc_change);
2742 sac->sac_state = state;
2743 sac->sac_error = error;
2744 /* XXX verify these stream counts */
2745 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2746 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2747 sac->sac_assoc_id = sctp_get_associd(stcb);
2748 if (notif_len > sizeof(struct sctp_assoc_change)) {
2749 if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2751 if (stcb->asoc.prsctp_supported == 1) {
2752 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
2754 if (stcb->asoc.auth_supported == 1) {
2755 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
2757 if (stcb->asoc.asconf_supported == 1) {
2758 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
2760 if (stcb->asoc.idata_supported == 1) {
2761 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_INTERLEAVING;
2763 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
2764 if (stcb->asoc.reconfig_supported == 1) {
2765 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
2767 sac->sac_length += i;
2768 } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2769 memcpy(sac->sac_info, abort, abort_len);
2770 sac->sac_length += abort_len;
2773 SCTP_BUF_LEN(m_notify) = sac->sac_length;
2774 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2775 0, 0, stcb->asoc.context, 0, 0, 0,
2777 if (control != NULL) {
2778 control->length = SCTP_BUF_LEN(m_notify);
2779 control->spec_flags = M_NOTIFICATION;
2780 /* not that we need this */
2781 control->tail_mbuf = m_notify;
2782 sctp_add_to_readq(stcb->sctp_ep, stcb,
2784 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2787 sctp_m_freem(m_notify);
2791 * For 1-to-1 style sockets, we send up and error when an ABORT
2795 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2796 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2797 ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2798 SOCK_LOCK(stcb->sctp_socket);
2800 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
2801 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
2802 stcb->sctp_socket->so_error = ECONNREFUSED;
2804 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
2805 stcb->sctp_socket->so_error = ECONNRESET;
2808 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
2809 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
2810 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
2811 stcb->sctp_socket->so_error = ETIMEDOUT;
2813 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
2814 stcb->sctp_socket->so_error = ECONNABORTED;
2818 /* Wake ANY sleepers */
2819 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2820 so = SCTP_INP_SO(stcb->sctp_ep);
2822 atomic_add_int(&stcb->asoc.refcnt, 1);
2823 SCTP_TCB_UNLOCK(stcb);
2824 SCTP_SOCKET_LOCK(so, 1);
2825 SCTP_TCB_LOCK(stcb);
2826 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2827 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2828 SCTP_SOCKET_UNLOCK(so, 1);
2833 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2834 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2835 ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2836 socantrcvmore_locked(stcb->sctp_socket);
2838 sorwakeup(stcb->sctp_socket);
2839 sowwakeup(stcb->sctp_socket);
2840 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2842 SCTP_SOCKET_UNLOCK(so, 1);
2848 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2849 struct sockaddr *sa, uint32_t error, int so_locked
2850 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2855 struct mbuf *m_notify;
2856 struct sctp_paddr_change *spc;
2857 struct sctp_queued_to_read *control;
2859 if ((stcb == NULL) ||
2860 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2861 /* event not enabled */
2864 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
2865 if (m_notify == NULL)
2867 SCTP_BUF_LEN(m_notify) = 0;
2868 spc = mtod(m_notify, struct sctp_paddr_change *);
2869 memset(spc, 0, sizeof(struct sctp_paddr_change));
2870 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2872 spc->spc_length = sizeof(struct sctp_paddr_change);
2873 switch (sa->sa_family) {
2877 if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2878 in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
2879 (struct sockaddr_in6 *)&spc->spc_aaddr);
2881 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2884 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2891 struct sockaddr_in6 *sin6;
2893 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2895 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2896 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2897 if (sin6->sin6_scope_id == 0) {
2898 /* recover scope_id for user */
2899 (void)sa6_recoverscope(sin6);
2901 /* clear embedded scope_id for user */
2902 in6_clearscope(&sin6->sin6_addr);
2912 spc->spc_state = state;
2913 spc->spc_error = error;
2914 spc->spc_assoc_id = sctp_get_associd(stcb);
2916 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2917 SCTP_BUF_NEXT(m_notify) = NULL;
2919 /* append to socket */
2920 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2921 0, 0, stcb->asoc.context, 0, 0, 0,
2923 if (control == NULL) {
2925 sctp_m_freem(m_notify);
2928 control->length = SCTP_BUF_LEN(m_notify);
2929 control->spec_flags = M_NOTIFICATION;
2930 /* not that we need this */
2931 control->tail_mbuf = m_notify;
2932 sctp_add_to_readq(stcb->sctp_ep, stcb,
2934 &stcb->sctp_socket->so_rcv, 1,
2935 SCTP_READ_LOCK_NOT_HELD,
2941 sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
2942 struct sctp_tmit_chunk *chk, int so_locked
2943 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2948 struct mbuf *m_notify;
2949 struct sctp_send_failed *ssf;
2950 struct sctp_send_failed_event *ssfe;
2951 struct sctp_queued_to_read *control;
2952 struct sctp_chunkhdr *chkhdr;
2953 int notifhdr_len, chk_len, chkhdr_len, padding_len, payload_len;
2955 if ((stcb == NULL) ||
2956 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
2957 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2958 /* event not enabled */
2962 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
2963 notifhdr_len = sizeof(struct sctp_send_failed_event);
2965 notifhdr_len = sizeof(struct sctp_send_failed);
2967 m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
2968 if (m_notify == NULL)
2971 SCTP_BUF_LEN(m_notify) = notifhdr_len;
2972 if (stcb->asoc.idata_supported) {
2973 chkhdr_len = sizeof(struct sctp_idata_chunk);
2975 chkhdr_len = sizeof(struct sctp_data_chunk);
2977 /* Use some defaults in case we can't access the chunk header */
2978 if (chk->send_size >= chkhdr_len) {
2979 payload_len = chk->send_size - chkhdr_len;
2984 if (chk->data != NULL) {
2985 chkhdr = mtod(chk->data, struct sctp_chunkhdr *);
2986 if (chkhdr != NULL) {
2987 chk_len = ntohs(chkhdr->chunk_length);
2988 if ((chk_len >= chkhdr_len) &&
2989 (chk->send_size >= chk_len) &&
2990 (chk->send_size - chk_len < 4)) {
2991 padding_len = chk->send_size - chk_len;
2992 payload_len = chk->send_size - chkhdr_len - padding_len;
2996 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
2997 ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2998 memset(ssfe, 0, notifhdr_len);
2999 ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3001 ssfe->ssfe_flags = SCTP_DATA_SENT;
3003 ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3005 ssfe->ssfe_length = (uint32_t)(notifhdr_len + payload_len);
3006 ssfe->ssfe_error = error;
3007 /* not exactly what the user sent in, but should be close :) */
3008 ssfe->ssfe_info.snd_sid = chk->rec.data.sid;
3009 ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
3010 ssfe->ssfe_info.snd_ppid = chk->rec.data.ppid;
3011 ssfe->ssfe_info.snd_context = chk->rec.data.context;
3012 ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3013 ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3015 ssf = mtod(m_notify, struct sctp_send_failed *);
3016 memset(ssf, 0, notifhdr_len);
3017 ssf->ssf_type = SCTP_SEND_FAILED;
3019 ssf->ssf_flags = SCTP_DATA_SENT;
3021 ssf->ssf_flags = SCTP_DATA_UNSENT;
3023 ssf->ssf_length = (uint32_t)(notifhdr_len + payload_len);
3024 ssf->ssf_error = error;
3025 /* not exactly what the user sent in, but should be close :) */
3026 ssf->ssf_info.sinfo_stream = chk->rec.data.sid;
3027 ssf->ssf_info.sinfo_ssn = (uint16_t)chk->rec.data.mid;
3028 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
3029 ssf->ssf_info.sinfo_ppid = chk->rec.data.ppid;
3030 ssf->ssf_info.sinfo_context = chk->rec.data.context;
3031 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3032 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3034 if (chk->data != NULL) {
3035 /* Trim off the sctp chunk header (it should be there) */
3036 if (chk->send_size == chkhdr_len + payload_len + padding_len) {
3037 m_adj(chk->data, chkhdr_len);
3038 m_adj(chk->data, -padding_len);
3039 sctp_mbuf_crush(chk->data);
3040 chk->send_size -= (chkhdr_len + padding_len);
3043 SCTP_BUF_NEXT(m_notify) = chk->data;
3044 /* Steal off the mbuf */
3047 * For this case, we check the actual socket buffer, since the assoc
3048 * is going away we don't want to overfill the socket buffer for a
3051 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3052 sctp_m_freem(m_notify);
3055 /* append to socket */
3056 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3057 0, 0, stcb->asoc.context, 0, 0, 0,
3059 if (control == NULL) {
3061 sctp_m_freem(m_notify);
3064 control->length = SCTP_BUF_LEN(m_notify);
3065 control->spec_flags = M_NOTIFICATION;
3066 /* not that we need this */
3067 control->tail_mbuf = m_notify;
3068 sctp_add_to_readq(stcb->sctp_ep, stcb,
3070 &stcb->sctp_socket->so_rcv, 1,
3071 SCTP_READ_LOCK_NOT_HELD,
3077 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3078 struct sctp_stream_queue_pending *sp, int so_locked
3079 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3084 struct mbuf *m_notify;
3085 struct sctp_send_failed *ssf;
3086 struct sctp_send_failed_event *ssfe;
3087 struct sctp_queued_to_read *control;
3090 if ((stcb == NULL) ||
3091 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
3092 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3093 /* event not enabled */
3096 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3097 notifhdr_len = sizeof(struct sctp_send_failed_event);
3099 notifhdr_len = sizeof(struct sctp_send_failed);
3101 m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3102 if (m_notify == NULL) {
3106 SCTP_BUF_LEN(m_notify) = notifhdr_len;
3107 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3108 ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3109 memset(ssfe, 0, notifhdr_len);
3110 ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3111 ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3112 ssfe->ssfe_length = (uint32_t)(notifhdr_len + sp->length);
3113 ssfe->ssfe_error = error;
3114 /* not exactly what the user sent in, but should be close :) */
3115 ssfe->ssfe_info.snd_sid = sp->sid;
3116 if (sp->some_taken) {
3117 ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
3119 ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
3121 ssfe->ssfe_info.snd_ppid = sp->ppid;
3122 ssfe->ssfe_info.snd_context = sp->context;
3123 ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3124 ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3126 ssf = mtod(m_notify, struct sctp_send_failed *);
3127 memset(ssf, 0, notifhdr_len);
3128 ssf->ssf_type = SCTP_SEND_FAILED;
3129 ssf->ssf_flags = SCTP_DATA_UNSENT;
3130 ssf->ssf_length = (uint32_t)(notifhdr_len + sp->length);
3131 ssf->ssf_error = error;
3132 /* not exactly what the user sent in, but should be close :) */
3133 ssf->ssf_info.sinfo_stream = sp->sid;
3134 ssf->ssf_info.sinfo_ssn = 0;
3135 if (sp->some_taken) {
3136 ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3138 ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3140 ssf->ssf_info.sinfo_ppid = sp->ppid;
3141 ssf->ssf_info.sinfo_context = sp->context;
3142 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3143 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3145 SCTP_BUF_NEXT(m_notify) = sp->data;
3147 /* Steal off the mbuf */
3150 * For this case, we check the actual socket buffer, since the assoc
3151 * is going away we don't want to overfill the socket buffer for a
3154 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3155 sctp_m_freem(m_notify);
3158 /* append to socket */
3159 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3160 0, 0, stcb->asoc.context, 0, 0, 0,
3162 if (control == NULL) {
3164 sctp_m_freem(m_notify);
3167 control->length = SCTP_BUF_LEN(m_notify);
3168 control->spec_flags = M_NOTIFICATION;
3169 /* not that we need this */
3170 control->tail_mbuf = m_notify;
3171 sctp_add_to_readq(stcb->sctp_ep, stcb,
3173 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3179 sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3181 struct mbuf *m_notify;
3182 struct sctp_adaptation_event *sai;
3183 struct sctp_queued_to_read *control;
3185 if ((stcb == NULL) ||
3186 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3187 /* event not enabled */
3191 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3192 if (m_notify == NULL)
3195 SCTP_BUF_LEN(m_notify) = 0;
3196 sai = mtod(m_notify, struct sctp_adaptation_event *);
3197 memset(sai, 0, sizeof(struct sctp_adaptation_event));
3198 sai->sai_type = SCTP_ADAPTATION_INDICATION;
3200 sai->sai_length = sizeof(struct sctp_adaptation_event);
3201 sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3202 sai->sai_assoc_id = sctp_get_associd(stcb);
3204 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3205 SCTP_BUF_NEXT(m_notify) = NULL;
3207 /* append to socket */
3208 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3209 0, 0, stcb->asoc.context, 0, 0, 0,
3211 if (control == NULL) {
3213 sctp_m_freem(m_notify);
3216 control->length = SCTP_BUF_LEN(m_notify);
3217 control->spec_flags = M_NOTIFICATION;
3218 /* not that we need this */
3219 control->tail_mbuf = m_notify;
3220 sctp_add_to_readq(stcb->sctp_ep, stcb,
3222 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3225 /* This always must be called with the read-queue LOCKED in the INP */
3227 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3228 uint32_t val, int so_locked
3229 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3234 struct mbuf *m_notify;
3235 struct sctp_pdapi_event *pdapi;
3236 struct sctp_queued_to_read *control;
3239 if ((stcb == NULL) ||
3240 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3241 /* event not enabled */
3244 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3248 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3249 if (m_notify == NULL)
3252 SCTP_BUF_LEN(m_notify) = 0;
3253 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3254 memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3255 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3256 pdapi->pdapi_flags = 0;
3257 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3258 pdapi->pdapi_indication = error;
3259 pdapi->pdapi_stream = (val >> 16);
3260 pdapi->pdapi_seq = (val & 0x0000ffff);
3261 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3263 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3264 SCTP_BUF_NEXT(m_notify) = NULL;
3265 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3266 0, 0, stcb->asoc.context, 0, 0, 0,
3268 if (control == NULL) {
3270 sctp_m_freem(m_notify);
3273 control->length = SCTP_BUF_LEN(m_notify);
3274 control->spec_flags = M_NOTIFICATION;
3275 /* not that we need this */
3276 control->tail_mbuf = m_notify;
3277 sb = &stcb->sctp_socket->so_rcv;
3278 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3279 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
3281 sctp_sballoc(stcb, sb, m_notify);
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_SBRESULT, 0);
3285 control->end_added = 1;
3286 if (stcb->asoc.control_pdapi)
3287 TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
3289 /* we really should not see this case */
3290 TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
3292 if (stcb->sctp_ep && stcb->sctp_socket) {
3293 /* This should always be the case */
3294 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3297 so = SCTP_INP_SO(stcb->sctp_ep);
3299 atomic_add_int(&stcb->asoc.refcnt, 1);
3300 SCTP_TCB_UNLOCK(stcb);
3301 SCTP_SOCKET_LOCK(so, 1);
3302 SCTP_TCB_LOCK(stcb);
3303 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3304 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3305 SCTP_SOCKET_UNLOCK(so, 1);
3310 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3311 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3313 SCTP_SOCKET_UNLOCK(so, 1);
3320 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3322 struct mbuf *m_notify;
3323 struct sctp_shutdown_event *sse;
3324 struct sctp_queued_to_read *control;
3327 * For TCP model AND UDP connected sockets we will send an error up
3328 * when an SHUTDOWN completes
3330 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3331 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3332 /* mark socket closed for read/write and wakeup! */
3333 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3336 so = SCTP_INP_SO(stcb->sctp_ep);
3337 atomic_add_int(&stcb->asoc.refcnt, 1);
3338 SCTP_TCB_UNLOCK(stcb);
3339 SCTP_SOCKET_LOCK(so, 1);
3340 SCTP_TCB_LOCK(stcb);
3341 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3342 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3343 SCTP_SOCKET_UNLOCK(so, 1);
3347 socantsendmore(stcb->sctp_socket);
3348 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3349 SCTP_SOCKET_UNLOCK(so, 1);
3352 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3353 /* event not enabled */
3357 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3358 if (m_notify == NULL)
3361 sse = mtod(m_notify, struct sctp_shutdown_event *);
3362 memset(sse, 0, sizeof(struct sctp_shutdown_event));
3363 sse->sse_type = SCTP_SHUTDOWN_EVENT;
3365 sse->sse_length = sizeof(struct sctp_shutdown_event);
3366 sse->sse_assoc_id = sctp_get_associd(stcb);
3368 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3369 SCTP_BUF_NEXT(m_notify) = NULL;
3371 /* append to socket */
3372 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3373 0, 0, stcb->asoc.context, 0, 0, 0,
3375 if (control == NULL) {
3377 sctp_m_freem(m_notify);
3380 control->length = SCTP_BUF_LEN(m_notify);
3381 control->spec_flags = M_NOTIFICATION;
3382 /* not that we need this */
3383 control->tail_mbuf = m_notify;
3384 sctp_add_to_readq(stcb->sctp_ep, stcb,
3386 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3390 sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3392 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3397 struct mbuf *m_notify;
3398 struct sctp_sender_dry_event *event;
3399 struct sctp_queued_to_read *control;
3401 if ((stcb == NULL) ||
3402 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3403 /* event not enabled */
3407 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3408 if (m_notify == NULL) {
3412 SCTP_BUF_LEN(m_notify) = 0;
3413 event = mtod(m_notify, struct sctp_sender_dry_event *);
3414 memset(event, 0, sizeof(struct sctp_sender_dry_event));
3415 event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3416 event->sender_dry_flags = 0;
3417 event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3418 event->sender_dry_assoc_id = sctp_get_associd(stcb);
3420 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3421 SCTP_BUF_NEXT(m_notify) = NULL;
3423 /* append to socket */
3424 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3425 0, 0, stcb->asoc.context, 0, 0, 0,
3427 if (control == NULL) {
3429 sctp_m_freem(m_notify);
3432 control->length = SCTP_BUF_LEN(m_notify);
3433 control->spec_flags = M_NOTIFICATION;
3434 /* not that we need this */
3435 control->tail_mbuf = m_notify;
3436 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3437 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3442 sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3444 struct mbuf *m_notify;
3445 struct sctp_queued_to_read *control;
3446 struct sctp_stream_change_event *stradd;
3448 if ((stcb == NULL) ||
3449 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3450 /* event not enabled */
3453 if ((stcb->asoc.peer_req_out) && flag) {
3454 /* Peer made the request, don't tell the local user */
3455 stcb->asoc.peer_req_out = 0;
3458 stcb->asoc.peer_req_out = 0;
3459 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3460 if (m_notify == NULL)
3463 SCTP_BUF_LEN(m_notify) = 0;
3464 stradd = mtod(m_notify, struct sctp_stream_change_event *);
3465 memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3466 stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3467 stradd->strchange_flags = flag;
3468 stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3469 stradd->strchange_assoc_id = sctp_get_associd(stcb);
3470 stradd->strchange_instrms = numberin;
3471 stradd->strchange_outstrms = numberout;
3472 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3473 SCTP_BUF_NEXT(m_notify) = NULL;
3474 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3476 sctp_m_freem(m_notify);
3479 /* append to socket */
3480 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3481 0, 0, stcb->asoc.context, 0, 0, 0,
3483 if (control == NULL) {
3485 sctp_m_freem(m_notify);
3488 control->length = SCTP_BUF_LEN(m_notify);
3489 control->spec_flags = M_NOTIFICATION;
3490 /* not that we need this */
3491 control->tail_mbuf = m_notify;
3492 sctp_add_to_readq(stcb->sctp_ep, stcb,
3494 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3498 sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3500 struct mbuf *m_notify;
3501 struct sctp_queued_to_read *control;
3502 struct sctp_assoc_reset_event *strasoc;
3504 if ((stcb == NULL) ||
3505 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3506 /* event not enabled */
3509 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3510 if (m_notify == NULL)
3513 SCTP_BUF_LEN(m_notify) = 0;
3514 strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3515 memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3516 strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3517 strasoc->assocreset_flags = flag;
3518 strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3519 strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3520 strasoc->assocreset_local_tsn = sending_tsn;
3521 strasoc->assocreset_remote_tsn = recv_tsn;
3522 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3523 SCTP_BUF_NEXT(m_notify) = NULL;
3524 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3526 sctp_m_freem(m_notify);
3529 /* append to socket */
3530 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3531 0, 0, stcb->asoc.context, 0, 0, 0,
3533 if (control == NULL) {
3535 sctp_m_freem(m_notify);
3538 control->length = SCTP_BUF_LEN(m_notify);
3539 control->spec_flags = M_NOTIFICATION;
3540 /* not that we need this */
3541 control->tail_mbuf = m_notify;
3542 sctp_add_to_readq(stcb->sctp_ep, stcb,
3544 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3550 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3551 int number_entries, uint16_t *list, int flag)
3553 struct mbuf *m_notify;
3554 struct sctp_queued_to_read *control;
3555 struct sctp_stream_reset_event *strreset;
3558 if ((stcb == NULL) ||
3559 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3560 /* event not enabled */
3564 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3565 if (m_notify == NULL)
3568 SCTP_BUF_LEN(m_notify) = 0;
3569 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3570 if (len > M_TRAILINGSPACE(m_notify)) {
3571 /* never enough room */
3572 sctp_m_freem(m_notify);
3575 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3576 memset(strreset, 0, len);
3577 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3578 strreset->strreset_flags = flag;
3579 strreset->strreset_length = len;
3580 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3581 if (number_entries) {
3584 for (i = 0; i < number_entries; i++) {
3585 strreset->strreset_stream_list[i] = ntohs(list[i]);
3588 SCTP_BUF_LEN(m_notify) = len;
3589 SCTP_BUF_NEXT(m_notify) = NULL;
3590 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3592 sctp_m_freem(m_notify);
3595 /* append to socket */
3596 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3597 0, 0, stcb->asoc.context, 0, 0, 0,
3599 if (control == NULL) {
3601 sctp_m_freem(m_notify);
3604 control->length = SCTP_BUF_LEN(m_notify);
3605 control->spec_flags = M_NOTIFICATION;
3606 /* not that we need this */
3607 control->tail_mbuf = m_notify;
3608 sctp_add_to_readq(stcb->sctp_ep, stcb,
3610 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3615 sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3617 struct mbuf *m_notify;
3618 struct sctp_remote_error *sre;
3619 struct sctp_queued_to_read *control;
3620 unsigned int notif_len;
3623 if ((stcb == NULL) ||
3624 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3627 if (chunk != NULL) {
3628 chunk_len = ntohs(chunk->ch.chunk_length);
3630 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
3633 if (chunk_len > SCTP_CHUNK_BUFFER_SIZE) {
3634 chunk_len = SCTP_CHUNK_BUFFER_SIZE;
3639 notif_len = (unsigned int)(sizeof(struct sctp_remote_error) + chunk_len);
3640 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3641 if (m_notify == NULL) {
3642 /* Retry with smaller value. */
3643 notif_len = (unsigned int)sizeof(struct sctp_remote_error);
3644 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3645 if (m_notify == NULL) {
3649 SCTP_BUF_NEXT(m_notify) = NULL;
3650 sre = mtod(m_notify, struct sctp_remote_error *);
3651 memset(sre, 0, notif_len);
3652 sre->sre_type = SCTP_REMOTE_ERROR;
3654 sre->sre_length = sizeof(struct sctp_remote_error);
3655 sre->sre_error = error;
3656 sre->sre_assoc_id = sctp_get_associd(stcb);
3657 if (notif_len > sizeof(struct sctp_remote_error)) {
3658 memcpy(sre->sre_data, chunk, chunk_len);
3659 sre->sre_length += chunk_len;
3661 SCTP_BUF_LEN(m_notify) = sre->sre_length;
3662 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3663 0, 0, stcb->asoc.context, 0, 0, 0,
3665 if (control != NULL) {
3666 control->length = SCTP_BUF_LEN(m_notify);
3667 control->spec_flags = M_NOTIFICATION;
3668 /* not that we need this */
3669 control->tail_mbuf = m_notify;
3670 sctp_add_to_readq(stcb->sctp_ep, stcb,
3672 &stcb->sctp_socket->so_rcv, 1,
3673 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3675 sctp_m_freem(m_notify);
3681 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3682 uint32_t error, void *data, int so_locked
3683 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3688 if ((stcb == NULL) ||
3689 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3690 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3691 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3692 /* If the socket is gone we are out of here */
3695 if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3698 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3699 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3700 if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
3701 (notification == SCTP_NOTIFY_INTERFACE_UP) ||
3702 (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
3703 /* Don't report these in front states */
3707 switch (notification) {
3708 case SCTP_NOTIFY_ASSOC_UP:
3709 if (stcb->asoc.assoc_up_sent == 0) {
3710 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
3711 stcb->asoc.assoc_up_sent = 1;
3713 if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
3714 sctp_notify_adaptation_layer(stcb);
3716 if (stcb->asoc.auth_supported == 0) {
3717 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3721 case SCTP_NOTIFY_ASSOC_DOWN:
3722 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
3724 case SCTP_NOTIFY_INTERFACE_DOWN:
3726 struct sctp_nets *net;
3728 net = (struct sctp_nets *)data;
3729 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3730 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3733 case SCTP_NOTIFY_INTERFACE_UP:
3735 struct sctp_nets *net;
3737 net = (struct sctp_nets *)data;
3738 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3739 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3742 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3744 struct sctp_nets *net;
3746 net = (struct sctp_nets *)data;
3747 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3748 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3751 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3752 sctp_notify_send_failed2(stcb, error,
3753 (struct sctp_stream_queue_pending *)data, so_locked);
3755 case SCTP_NOTIFY_SENT_DG_FAIL:
3756 sctp_notify_send_failed(stcb, 1, error,
3757 (struct sctp_tmit_chunk *)data, so_locked);
3759 case SCTP_NOTIFY_UNSENT_DG_FAIL:
3760 sctp_notify_send_failed(stcb, 0, error,
3761 (struct sctp_tmit_chunk *)data, so_locked);
3763 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3767 val = *((uint32_t *)data);
3769 sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3772 case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
3773 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3774 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3775 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
3777 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
3780 case SCTP_NOTIFY_ASSOC_REM_ABORTED:
3781 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3782 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3783 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
3785 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
3788 case SCTP_NOTIFY_ASSOC_RESTART:
3789 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
3790 if (stcb->asoc.auth_supported == 0) {
3791 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3795 case SCTP_NOTIFY_STR_RESET_SEND:
3796 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_OUTGOING_SSN);
3798 case SCTP_NOTIFY_STR_RESET_RECV:
3799 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_INCOMING);
3801 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3802 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3803 (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
3805 case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
3806 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3807 (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
3809 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3810 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3811 (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
3813 case SCTP_NOTIFY_STR_RESET_DENIED_IN:
3814 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3815 (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
3817 case SCTP_NOTIFY_ASCONF_ADD_IP:
3818 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3821 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3822 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3825 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3826 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3829 case SCTP_NOTIFY_PEER_SHUTDOWN:
3830 sctp_notify_shutdown_event(stcb);
3832 case SCTP_NOTIFY_AUTH_NEW_KEY:
3833 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
3834 (uint16_t)(uintptr_t)data,
3837 case SCTP_NOTIFY_AUTH_FREE_KEY:
3838 sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3839 (uint16_t)(uintptr_t)data,
3842 case SCTP_NOTIFY_NO_PEER_AUTH:
3843 sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3844 (uint16_t)(uintptr_t)data,
3847 case SCTP_NOTIFY_SENDER_DRY:
3848 sctp_notify_sender_dry_event(stcb, so_locked);
3850 case SCTP_NOTIFY_REMOTE_ERROR:
3851 sctp_notify_remote_error(stcb, error, data);
3854 SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3855 __func__, notification, notification);
3861 sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
3862 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3867 struct sctp_association *asoc;
3868 struct sctp_stream_out *outs;
3869 struct sctp_tmit_chunk *chk, *nchk;
3870 struct sctp_stream_queue_pending *sp, *nsp;
3877 if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3878 /* already being freed */
3881 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3882 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3883 (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
3886 /* now through all the gunk freeing chunks */
3887 if (holds_lock == 0) {
3888 SCTP_TCB_SEND_LOCK(stcb);
3890 /* sent queue SHOULD be empty */
3891 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3892 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3893 asoc->sent_queue_cnt--;
3894 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
3895 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
3896 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
3899 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
3903 if (chk->data != NULL) {
3904 sctp_free_bufspace(stcb, asoc, chk, 1);
3905 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
3906 error, chk, so_locked);
3908 sctp_m_freem(chk->data);
3912 sctp_free_a_chunk(stcb, chk, so_locked);
3913 /* sa_ignore FREED_MEMORY */
3915 /* pending send queue SHOULD be empty */
3916 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3917 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3918 asoc->send_queue_cnt--;
3919 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
3920 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
3923 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
3926 if (chk->data != NULL) {
3927 sctp_free_bufspace(stcb, asoc, chk, 1);
3928 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
3929 error, chk, so_locked);
3931 sctp_m_freem(chk->data);
3935 sctp_free_a_chunk(stcb, chk, so_locked);
3936 /* sa_ignore FREED_MEMORY */
3938 for (i = 0; i < asoc->streamoutcnt; i++) {
3939 /* For each stream */
3940 outs = &asoc->strmout[i];
3941 /* clean up any sends there */
3942 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
3943 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
3944 TAILQ_REMOVE(&outs->outqueue, sp, next);
3945 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
3946 sctp_free_spbufspace(stcb, asoc, sp);
3948 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3949 error, (void *)sp, so_locked);
3951 sctp_m_freem(sp->data);
3953 sp->tail_mbuf = NULL;
3958 sctp_free_remote_addr(sp->net);
3961 /* Free the chunk */
3962 sctp_free_a_strmoq(stcb, sp, so_locked);
3963 /* sa_ignore FREED_MEMORY */
3967 if (holds_lock == 0) {
3968 SCTP_TCB_SEND_UNLOCK(stcb);
3973 sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
3974 struct sctp_abort_chunk *abort, int so_locked
3975 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3983 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3984 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3985 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3986 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3988 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3989 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3990 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3993 /* Tell them we lost the asoc */
3994 sctp_report_all_outbound(stcb, error, 1, so_locked);
3996 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
3998 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
4003 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4004 struct mbuf *m, int iphlen,
4005 struct sockaddr *src, struct sockaddr *dst,
4006 struct sctphdr *sh, struct mbuf *op_err,
4007 uint8_t mflowtype, uint32_t mflowid,
4008 uint32_t vrf_id, uint16_t port)
4011 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4017 vtag = stcb->asoc.peer_vtag;
4018 vrf_id = stcb->asoc.vrf_id;
4020 sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
4021 mflowtype, mflowid, inp->fibnum,
4024 /* We have a TCB to abort, send notification too */
4025 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
4026 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4027 /* Ok, now lets free it */
4028 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4029 so = SCTP_INP_SO(inp);
4030 atomic_add_int(&stcb->asoc.refcnt, 1);
4031 SCTP_TCB_UNLOCK(stcb);
4032 SCTP_SOCKET_LOCK(so, 1);
4033 SCTP_TCB_LOCK(stcb);
4034 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4036 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4037 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4038 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4039 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4041 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4042 SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
4043 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4044 SCTP_SOCKET_UNLOCK(so, 1);
4048 #ifdef SCTP_ASOCLOG_OF_TSNS
4050 sctp_print_out_track_log(struct sctp_tcb *stcb)
4055 SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
4056 SCTP_PRINTF("IN bound TSN log-aaa\n");
4057 if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
4058 SCTP_PRINTF("None rcvd\n");
4061 if (stcb->asoc.tsn_in_wrapped) {
4062 for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
4063 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4064 stcb->asoc.in_tsnlog[i].tsn,
4065 stcb->asoc.in_tsnlog[i].strm,
4066 stcb->asoc.in_tsnlog[i].seq,
4067 stcb->asoc.in_tsnlog[i].flgs,
4068 stcb->asoc.in_tsnlog[i].sz);
4071 if (stcb->asoc.tsn_in_at) {
4072 for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
4073 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4074 stcb->asoc.in_tsnlog[i].tsn,
4075 stcb->asoc.in_tsnlog[i].strm,
4076 stcb->asoc.in_tsnlog[i].seq,
4077 stcb->asoc.in_tsnlog[i].flgs,
4078 stcb->asoc.in_tsnlog[i].sz);
4082 SCTP_PRINTF("OUT bound TSN log-aaa\n");
4083 if ((stcb->asoc.tsn_out_at == 0) &&
4084 (stcb->asoc.tsn_out_wrapped == 0)) {
4085 SCTP_PRINTF("None sent\n");
4087 if (stcb->asoc.tsn_out_wrapped) {
4088 for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4089 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4090 stcb->asoc.out_tsnlog[i].tsn,
4091 stcb->asoc.out_tsnlog[i].strm,
4092 stcb->asoc.out_tsnlog[i].seq,
4093 stcb->asoc.out_tsnlog[i].flgs,
4094 stcb->asoc.out_tsnlog[i].sz);
4097 if (stcb->asoc.tsn_out_at) {
4098 for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4099 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4100 stcb->asoc.out_tsnlog[i].tsn,
4101 stcb->asoc.out_tsnlog[i].strm,
4102 stcb->asoc.out_tsnlog[i].seq,
4103 stcb->asoc.out_tsnlog[i].flgs,
4104 stcb->asoc.out_tsnlog[i].sz);
4112 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4113 struct mbuf *op_err,
4115 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4120 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4124 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4125 so = SCTP_INP_SO(inp);
4128 /* Got to have a TCB */
4129 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4130 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4131 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4132 SCTP_CALLED_DIRECTLY_NOCMPSET);
4137 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4139 /* notify the peer */
4140 sctp_send_abort_tcb(stcb, op_err, so_locked);
4141 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4142 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4143 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4144 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4146 /* notify the ulp */
4147 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4148 sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4150 /* now free the asoc */
4151 #ifdef SCTP_ASOCLOG_OF_TSNS
4152 sctp_print_out_track_log(stcb);
4154 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4156 atomic_add_int(&stcb->asoc.refcnt, 1);
4157 SCTP_TCB_UNLOCK(stcb);
4158 SCTP_SOCKET_LOCK(so, 1);
4159 SCTP_TCB_LOCK(stcb);
4160 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4163 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4164 SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4165 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4167 SCTP_SOCKET_UNLOCK(so, 1);
4173 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4174 struct sockaddr *src, struct sockaddr *dst,
4175 struct sctphdr *sh, struct sctp_inpcb *inp,
4177 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4178 uint32_t vrf_id, uint16_t port)
4180 struct sctp_chunkhdr *ch, chunk_buf;
4181 unsigned int chk_length;
4182 int contains_init_chunk;
4184 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4185 /* Generate a TO address for future reference */
4186 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4187 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4188 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4189 SCTP_CALLED_DIRECTLY_NOCMPSET);
4192 contains_init_chunk = 0;
4193 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4194 sizeof(*ch), (uint8_t *)&chunk_buf);
4195 while (ch != NULL) {
4196 chk_length = ntohs(ch->chunk_length);
4197 if (chk_length < sizeof(*ch)) {
4198 /* break to abort land */
4201 switch (ch->chunk_type) {
4203 contains_init_chunk = 1;
4205 case SCTP_PACKET_DROPPED:
4206 /* we don't respond to pkt-dropped */
4208 case SCTP_ABORT_ASSOCIATION:
4209 /* we don't respond with an ABORT to an ABORT */
4211 case SCTP_SHUTDOWN_COMPLETE:
4213 * we ignore it since we are not waiting for it and
4217 case SCTP_SHUTDOWN_ACK:
4218 sctp_send_shutdown_complete2(src, dst, sh,
4219 mflowtype, mflowid, fibnum,
4225 offset += SCTP_SIZE32(chk_length);
4226 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4227 sizeof(*ch), (uint8_t *)&chunk_buf);
4229 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4230 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4231 (contains_init_chunk == 0))) {
4232 sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4233 mflowtype, mflowid, fibnum,
4239 * check the inbound datagram to make sure there is not an abort inside it,
4240 * if there is return 1, else return 0.
4243 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t *vtagfill)
4245 struct sctp_chunkhdr *ch;
4246 struct sctp_init_chunk *init_chk, chunk_buf;
4248 unsigned int chk_length;
4250 offset = iphlen + sizeof(struct sctphdr);
4251 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4252 (uint8_t *)&chunk_buf);
4253 while (ch != NULL) {
4254 chk_length = ntohs(ch->chunk_length);
4255 if (chk_length < sizeof(*ch)) {
4256 /* packet is probably corrupt */
4259 /* we seem to be ok, is it an abort? */
4260 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4261 /* yep, tell them */
4264 if (ch->chunk_type == SCTP_INITIATION) {
4265 /* need to update the Vtag */
4266 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4267 offset, sizeof(*init_chk), (uint8_t *)&chunk_buf);
4268 if (init_chk != NULL) {
4269 *vtagfill = ntohl(init_chk->init.initiate_tag);
4272 /* Nope, move to the next chunk */
4273 offset += SCTP_SIZE32(chk_length);
4274 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4275 sizeof(*ch), (uint8_t *)&chunk_buf);
4281 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4282 * set (i.e. it's 0) so, create this function to compare link local scopes
4286 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4288 struct sockaddr_in6 a, b;
4294 if (a.sin6_scope_id == 0)
4295 if (sa6_recoverscope(&a)) {
4296 /* can't get scope, so can't match */
4299 if (b.sin6_scope_id == 0)
4300 if (sa6_recoverscope(&b)) {
4301 /* can't get scope, so can't match */
4304 if (a.sin6_scope_id != b.sin6_scope_id)
4311 * returns a sockaddr_in6 with embedded scope recovered and removed
4313 struct sockaddr_in6 *
4314 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4316 /* check and strip embedded scope junk */
4317 if (addr->sin6_family == AF_INET6) {
4318 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4319 if (addr->sin6_scope_id == 0) {
4321 if (!sa6_recoverscope(store)) {
4322 /* use the recovered scope */
4326 /* else, return the original "to" addr */
4327 in6_clearscope(&addr->sin6_addr);
4336 * are the two addresses the same? currently a "scopeless" check returns: 1
4340 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4344 if (sa1 == NULL || sa2 == NULL)
4347 /* must be the same family */
4348 if (sa1->sa_family != sa2->sa_family)
4351 switch (sa1->sa_family) {
4355 /* IPv6 addresses */
4356 struct sockaddr_in6 *sin6_1, *sin6_2;
4358 sin6_1 = (struct sockaddr_in6 *)sa1;
4359 sin6_2 = (struct sockaddr_in6 *)sa2;
4360 return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4367 /* IPv4 addresses */
4368 struct sockaddr_in *sin_1, *sin_2;
4370 sin_1 = (struct sockaddr_in *)sa1;
4371 sin_2 = (struct sockaddr_in *)sa2;
4372 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
4376 /* we don't do these... */
4382 sctp_print_address(struct sockaddr *sa)
4385 char ip6buf[INET6_ADDRSTRLEN];
4388 switch (sa->sa_family) {
4392 struct sockaddr_in6 *sin6;
4394 sin6 = (struct sockaddr_in6 *)sa;
4395 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
4396 ip6_sprintf(ip6buf, &sin6->sin6_addr),
4397 ntohs(sin6->sin6_port),
4398 sin6->sin6_scope_id);
4405 struct sockaddr_in *sin;
4408 sin = (struct sockaddr_in *)sa;
4409 p = (unsigned char *)&sin->sin_addr;
4410 SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4411 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
4422 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4423 struct sctp_inpcb *new_inp,
4424 struct sctp_tcb *stcb,
4428 * go through our old INP and pull off any control structures that
4429 * belong to stcb and move then to the new inp.
4431 struct socket *old_so, *new_so;
4432 struct sctp_queued_to_read *control, *nctl;
4433 struct sctp_readhead tmp_queue;
4437 old_so = old_inp->sctp_socket;
4438 new_so = new_inp->sctp_socket;
4439 TAILQ_INIT(&tmp_queue);
4440 error = sblock(&old_so->so_rcv, waitflags);
4443 * Gak, can't get sblock, we have a problem. data will be
4444 * left stranded.. and we don't dare look at it since the
4445 * other thread may be reading something. Oh well, its a
4446 * screwed up app that does a peeloff OR a accept while
4447 * reading from the main socket... actually its only the
4448 * peeloff() case, since I think read will fail on a
4449 * listening socket..
4453 /* lock the socket buffers */
4454 SCTP_INP_READ_LOCK(old_inp);
4455 TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4456 /* Pull off all for out target stcb */
4457 if (control->stcb == stcb) {
4458 /* remove it we want it */
4459 TAILQ_REMOVE(&old_inp->read_queue, control, next);
4460 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4463 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4464 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
4466 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
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_SBRESULT, 0);
4470 m = SCTP_BUF_NEXT(m);
4474 SCTP_INP_READ_UNLOCK(old_inp);
4475 /* Remove the sb-lock on the old socket */
4477 sbunlock(&old_so->so_rcv);
4478 /* Now we move them over to the new socket buffer */
4479 SCTP_INP_READ_LOCK(new_inp);
4480 TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4481 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4484 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4485 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4487 sctp_sballoc(stcb, &new_so->so_rcv, m);
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_SBRESULT, 0);
4491 m = SCTP_BUF_NEXT(m);
4494 SCTP_INP_READ_UNLOCK(new_inp);
4498 sctp_wakeup_the_read_socket(struct sctp_inpcb *inp,
4499 struct sctp_tcb *stcb,
4501 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4506 if ((inp != NULL) && (inp->sctp_socket != NULL)) {
4507 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4510 so = SCTP_INP_SO(inp);
4513 atomic_add_int(&stcb->asoc.refcnt, 1);
4514 SCTP_TCB_UNLOCK(stcb);
4516 SCTP_SOCKET_LOCK(so, 1);
4518 SCTP_TCB_LOCK(stcb);
4519 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4521 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4522 SCTP_SOCKET_UNLOCK(so, 1);
4527 sctp_sorwakeup(inp, inp->sctp_socket);
4528 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4530 SCTP_SOCKET_UNLOCK(so, 1);
4537 sctp_add_to_readq(struct sctp_inpcb *inp,
4538 struct sctp_tcb *stcb,
4539 struct sctp_queued_to_read *control,
4542 int inp_read_lock_held,
4544 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4550 * Here we must place the control on the end of the socket read
4551 * queue AND increment sb_cc so that select will work properly on
4554 struct mbuf *m, *prev = NULL;
4559 panic("Gak, inp NULL on add_to_readq");
4563 if (inp_read_lock_held == 0)
4564 SCTP_INP_READ_LOCK(inp);
4565 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4566 sctp_free_remote_addr(control->whoFrom);
4567 if (control->data) {
4568 sctp_m_freem(control->data);
4569 control->data = NULL;
4571 sctp_free_a_readq(stcb, control);
4572 if (inp_read_lock_held == 0)
4573 SCTP_INP_READ_UNLOCK(inp);
4576 if (!(control->spec_flags & M_NOTIFICATION)) {
4577 atomic_add_int(&inp->total_recvs, 1);
4578 if (!control->do_not_ref_stcb) {
4579 atomic_add_int(&stcb->total_recvs, 1);
4583 control->held_length = 0;
4584 control->length = 0;
4586 if (SCTP_BUF_LEN(m) == 0) {
4587 /* Skip mbufs with NO length */
4590 control->data = sctp_m_free(m);
4593 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4594 m = SCTP_BUF_NEXT(prev);
4597 control->tail_mbuf = prev;
4602 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4603 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4605 sctp_sballoc(stcb, sb, m);
4606 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4607 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4609 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4610 m = SCTP_BUF_NEXT(m);
4613 control->tail_mbuf = prev;
4615 /* Everything got collapsed out?? */
4616 sctp_free_remote_addr(control->whoFrom);
4617 sctp_free_a_readq(stcb, control);
4618 if (inp_read_lock_held == 0)
4619 SCTP_INP_READ_UNLOCK(inp);
4623 control->end_added = 1;
4625 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4626 control->on_read_q = 1;
4627 if (inp_read_lock_held == 0)
4628 SCTP_INP_READ_UNLOCK(inp);
4629 if (inp && inp->sctp_socket) {
4630 sctp_wakeup_the_read_socket(inp, stcb, so_locked);
4634 /*************HOLD THIS COMMENT FOR PATCH FILE OF
4635 *************ALTERNATE ROUTING CODE
4638 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4639 *************ALTERNATE ROUTING CODE
4643 sctp_generate_cause(uint16_t code, char *info)
4646 struct sctp_gen_error_cause *cause;
4650 if ((code == 0) || (info == NULL)) {
4653 info_len = strlen(info);
4654 if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
4657 len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
4658 m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4660 SCTP_BUF_LEN(m) = len;
4661 cause = mtod(m, struct sctp_gen_error_cause *);
4662 cause->code = htons(code);
4663 cause->length = htons(len);
4664 memcpy(cause->info, info, info_len);
4670 sctp_generate_no_user_data_cause(uint32_t tsn)
4673 struct sctp_error_no_user_data *no_user_data_cause;
4676 len = (uint16_t)sizeof(struct sctp_error_no_user_data);
4677 m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4679 SCTP_BUF_LEN(m) = len;
4680 no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
4681 no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
4682 no_user_data_cause->cause.length = htons(len);
4683 no_user_data_cause->tsn = htonl(tsn);
4688 #ifdef SCTP_MBCNT_LOGGING
4690 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4691 struct sctp_tmit_chunk *tp1, int chk_cnt)
4693 if (tp1->data == NULL) {
4696 asoc->chunks_on_out_queue -= chk_cnt;
4697 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4698 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4699 asoc->total_output_queue_size,
4704 if (asoc->total_output_queue_size >= tp1->book_size) {
4705 atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4707 asoc->total_output_queue_size = 0;
4710 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4711 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4712 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4713 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4715 stcb->sctp_socket->so_snd.sb_cc = 0;
4724 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4725 uint8_t sent, int so_locked
4726 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4731 struct sctp_stream_out *strq;
4732 struct sctp_tmit_chunk *chk = NULL, *tp2;
4733 struct sctp_stream_queue_pending *sp;
4736 uint8_t foundeom = 0;
4739 int do_wakeup_routine = 0;
4741 sid = tp1->rec.data.sid;
4742 mid = tp1->rec.data.mid;
4743 if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
4744 stcb->asoc.abandoned_sent[0]++;
4745 stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4746 stcb->asoc.strmout[sid].abandoned_sent[0]++;
4747 #if defined(SCTP_DETAILED_STR_STATS)
4748 stcb->asoc.strmout[stream].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4751 stcb->asoc.abandoned_unsent[0]++;
4752 stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4753 stcb->asoc.strmout[sid].abandoned_unsent[0]++;
4754 #if defined(SCTP_DETAILED_STR_STATS)
4755 stcb->asoc.strmout[stream].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4759 ret_sz += tp1->book_size;
4760 if (tp1->data != NULL) {
4761 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4762 sctp_flight_size_decrease(tp1);
4763 sctp_total_flight_decrease(stcb, tp1);
4765 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4766 stcb->asoc.peers_rwnd += tp1->send_size;
4767 stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
4769 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
4771 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
4774 sctp_m_freem(tp1->data);
4777 do_wakeup_routine = 1;
4778 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4779 stcb->asoc.sent_queue_cnt_removeable--;
4782 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4783 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4784 SCTP_DATA_NOT_FRAG) {
4785 /* not frag'ed we ae done */
4788 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4789 /* end of frag, we are done */
4794 * Its a begin or middle piece, we must mark all of
4798 tp1 = TAILQ_NEXT(tp1, sctp_next);
4800 } while (tp1 && notdone);
4801 if (foundeom == 0) {
4803 * The multi-part message was scattered across the send and
4806 TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
4807 if ((tp1->rec.data.sid != sid) ||
4808 (!SCTP_MID_EQ(stcb->asoc.idata_supported, tp1->rec.data.mid, mid))) {
4812 * save to chk in case we have some on stream out
4813 * queue. If so and we have an un-transmitted one we
4814 * don't have to fudge the TSN.
4817 ret_sz += tp1->book_size;
4818 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4820 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
4822 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
4825 sctp_m_freem(tp1->data);
4828 /* No flight involved here book the size to 0 */
4830 if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4833 do_wakeup_routine = 1;
4834 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4835 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4837 * on to the sent queue so we can wait for it to be
4840 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4842 stcb->asoc.send_queue_cnt--;
4843 stcb->asoc.sent_queue_cnt++;
4846 if (foundeom == 0) {
4848 * Still no eom found. That means there is stuff left on the
4849 * stream out queue.. yuck.
4851 SCTP_TCB_SEND_LOCK(stcb);
4852 strq = &stcb->asoc.strmout[sid];
4853 sp = TAILQ_FIRST(&strq->outqueue);
4855 sp->discard_rest = 1;
4857 * We may need to put a chunk on the queue that
4858 * holds the TSN that would have been sent with the
4862 /* Yep, we have to */
4863 sctp_alloc_a_chunk(stcb, chk);
4866 * we are hosed. All we can do is
4867 * nothing.. which will cause an
4868 * abort if the peer is paying
4873 memset(chk, 0, sizeof(*chk));
4874 chk->rec.data.rcv_flags = 0;
4875 chk->sent = SCTP_FORWARD_TSN_SKIP;
4876 chk->asoc = &stcb->asoc;
4877 if (stcb->asoc.idata_supported == 0) {
4878 if (sp->sinfo_flags & SCTP_UNORDERED) {
4879 chk->rec.data.mid = 0;
4881 chk->rec.data.mid = strq->next_mid_ordered;
4884 if (sp->sinfo_flags & SCTP_UNORDERED) {
4885 chk->rec.data.mid = strq->next_mid_unordered;
4887 chk->rec.data.mid = strq->next_mid_ordered;
4890 chk->rec.data.sid = sp->sid;
4891 chk->rec.data.ppid = sp->ppid;
4892 chk->rec.data.context = sp->context;
4893 chk->flags = sp->act_flags;
4895 chk->rec.data.tsn = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
4896 strq->chunks_on_queues++;
4897 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
4898 stcb->asoc.sent_queue_cnt++;
4899 stcb->asoc.pr_sctp_cnt++;
4901 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4902 if (sp->sinfo_flags & SCTP_UNORDERED) {
4903 chk->rec.data.rcv_flags |= SCTP_DATA_UNORDERED;
4905 if (stcb->asoc.idata_supported == 0) {
4906 if ((sp->sinfo_flags & SCTP_UNORDERED) == 0) {
4907 strq->next_mid_ordered++;
4910 if (sp->sinfo_flags & SCTP_UNORDERED) {
4911 strq->next_mid_unordered++;
4913 strq->next_mid_ordered++;
4919 * Pull any data to free up the SB and allow
4920 * sender to "add more" while we will throw
4923 sctp_free_spbufspace(stcb, &stcb->asoc, sp);
4924 ret_sz += sp->length;
4925 do_wakeup_routine = 1;
4927 sctp_m_freem(sp->data);
4929 sp->tail_mbuf = NULL;
4933 SCTP_TCB_SEND_UNLOCK(stcb);
4935 if (do_wakeup_routine) {
4936 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4939 so = SCTP_INP_SO(stcb->sctp_ep);
4941 atomic_add_int(&stcb->asoc.refcnt, 1);
4942 SCTP_TCB_UNLOCK(stcb);
4943 SCTP_SOCKET_LOCK(so, 1);
4944 SCTP_TCB_LOCK(stcb);
4945 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4946 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4947 /* assoc was freed while we were unlocked */
4948 SCTP_SOCKET_UNLOCK(so, 1);
4953 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
4954 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4956 SCTP_SOCKET_UNLOCK(so, 1);
4964 * checks to see if the given address, sa, is one that is currently known by
4965 * the kernel note: can't distinguish the same address on multiple interfaces
4966 * and doesn't handle multiple addresses with different zone/scope id's note:
4967 * ifa_ifwithaddr() compares the entire sockaddr struct
4970 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
4973 struct sctp_laddr *laddr;
4975 if (holds_lock == 0) {
4976 SCTP_INP_RLOCK(inp);
4979 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4980 if (laddr->ifa == NULL)
4982 if (addr->sa_family != laddr->ifa->address.sa.sa_family)
4985 if (addr->sa_family == AF_INET) {
4986 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4987 laddr->ifa->address.sin.sin_addr.s_addr) {
4989 if (holds_lock == 0) {
4990 SCTP_INP_RUNLOCK(inp);
4992 return (laddr->ifa);
4998 if (addr->sa_family == AF_INET6) {
4999 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5000 &laddr->ifa->address.sin6)) {
5002 if (holds_lock == 0) {
5003 SCTP_INP_RUNLOCK(inp);
5005 return (laddr->ifa);
5011 if (holds_lock == 0) {
5012 SCTP_INP_RUNLOCK(inp);
5018 sctp_get_ifa_hash_val(struct sockaddr *addr)
5020 switch (addr->sa_family) {
5024 struct sockaddr_in *sin;
5026 sin = (struct sockaddr_in *)addr;
5027 return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5033 struct sockaddr_in6 *sin6;
5034 uint32_t hash_of_addr;
5036 sin6 = (struct sockaddr_in6 *)addr;
5037 hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
5038 sin6->sin6_addr.s6_addr32[1] +
5039 sin6->sin6_addr.s6_addr32[2] +
5040 sin6->sin6_addr.s6_addr32[3]);
5041 hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
5042 return (hash_of_addr);
5052 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
5054 struct sctp_ifa *sctp_ifap;
5055 struct sctp_vrf *vrf;
5056 struct sctp_ifalist *hash_head;
5057 uint32_t hash_of_addr;
5059 if (holds_lock == 0)
5060 SCTP_IPI_ADDR_RLOCK();
5062 vrf = sctp_find_vrf(vrf_id);
5064 if (holds_lock == 0)
5065 SCTP_IPI_ADDR_RUNLOCK();
5069 hash_of_addr = sctp_get_ifa_hash_val(addr);
5071 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5072 if (hash_head == NULL) {
5073 SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5074 hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
5075 (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
5076 sctp_print_address(addr);
5077 SCTP_PRINTF("No such bucket for address\n");
5078 if (holds_lock == 0)
5079 SCTP_IPI_ADDR_RUNLOCK();
5083 LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5084 if (addr->sa_family != sctp_ifap->address.sa.sa_family)
5087 if (addr->sa_family == AF_INET) {
5088 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5089 sctp_ifap->address.sin.sin_addr.s_addr) {
5091 if (holds_lock == 0)
5092 SCTP_IPI_ADDR_RUNLOCK();
5099 if (addr->sa_family == AF_INET6) {
5100 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5101 &sctp_ifap->address.sin6)) {
5103 if (holds_lock == 0)
5104 SCTP_IPI_ADDR_RUNLOCK();
5111 if (holds_lock == 0)
5112 SCTP_IPI_ADDR_RUNLOCK();
5117 sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
5120 /* User pulled some data, do we need a rwnd update? */
5123 struct socket *so = NULL;
5128 atomic_add_int(&stcb->asoc.refcnt, 1);
5130 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5131 (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | SCTP_STATE_SHUTDOWN_RECEIVED))) {
5132 /* Pre-check If we are freeing no update */
5135 SCTP_INP_INCR_REF(stcb->sctp_ep);
5136 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5137 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5140 so = stcb->sctp_socket;
5144 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5145 /* Have you have freed enough to look */
5147 /* Yep, its worth a look and the lock overhead */
5149 /* Figure out what the rwnd would be */
5150 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5151 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5152 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5156 if (dif >= rwnd_req) {
5158 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5161 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5163 * One last check before we allow the guy possibly
5164 * to get in. There is a race, where the guy has not
5165 * reached the gate. In that case
5169 SCTP_TCB_LOCK(stcb);
5170 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5171 /* No reports here */
5172 SCTP_TCB_UNLOCK(stcb);
5175 SCTP_STAT_INCR(sctps_wu_sacks_sent);
5176 sctp_send_sack(stcb, SCTP_SO_LOCKED);
5178 sctp_chunk_output(stcb->sctp_ep, stcb,
5179 SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5180 /* make sure no timer is running */
5181 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
5182 SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5183 SCTP_TCB_UNLOCK(stcb);
5185 /* Update how much we have pending */
5186 stcb->freed_by_sorcv_sincelast = dif;
5189 if (so && r_unlocked && hold_rlock) {
5190 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5193 SCTP_INP_DECR_REF(stcb->sctp_ep);
5195 atomic_add_int(&stcb->asoc.refcnt, -1);
5200 sctp_sorecvmsg(struct socket *so,
5203 struct sockaddr *from,
5206 struct sctp_sndrcvinfo *sinfo,
5210 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5211 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5212 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5213 * On the way out we may send out any combination of:
5214 * MSG_NOTIFICATION MSG_EOR
5217 struct sctp_inpcb *inp = NULL;
5219 int cp_len = 0, error = 0;
5220 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
5221 struct mbuf *m = NULL;
5222 struct sctp_tcb *stcb = NULL;
5223 int wakeup_read_socket = 0;
5224 int freecnt_applied = 0;
5225 int out_flags = 0, in_flags = 0;
5226 int block_allowed = 1;
5227 uint32_t freed_so_far = 0;
5228 uint32_t copied_so_far = 0;
5229 int in_eeor_mode = 0;
5230 int no_rcv_needed = 0;
5231 uint32_t rwnd_req = 0;
5232 int hold_sblock = 0;
5235 uint32_t held_length = 0;
5236 int sockbuf_lock = 0;
5239 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5244 in_flags = *msg_flags;
5245 if (in_flags & MSG_PEEK)
5246 SCTP_STAT_INCR(sctps_read_peeks);
5250 slen = uio->uio_resid;
5252 /* Pull in and set up our int flags */
5253 if (in_flags & MSG_OOB) {
5254 /* Out of band's NOT supported */
5255 return (EOPNOTSUPP);
5257 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5258 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5261 if ((in_flags & (MSG_DONTWAIT
5264 SCTP_SO_IS_NBIO(so)) {
5267 /* setup the endpoint */
5268 inp = (struct sctp_inpcb *)so->so_pcb;
5270 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5273 rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5274 /* Must be at least a MTU's worth */
5275 if (rwnd_req < SCTP_MIN_RWND)
5276 rwnd_req = SCTP_MIN_RWND;
5277 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5278 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5279 sctp_misc_ints(SCTP_SORECV_ENTER,
5280 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
5282 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5283 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5284 rwnd_req, block_allowed, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
5288 error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5290 goto release_unlocked;
5297 if (hold_sblock == 0) {
5298 SOCKBUF_LOCK(&so->so_rcv);
5301 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5302 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5305 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5307 error = so->so_error;
5308 if ((in_flags & MSG_PEEK) == 0)
5312 if (so->so_rcv.sb_cc == 0) {
5319 if (so->so_rcv.sb_cc <= held_length) {
5321 error = so->so_error;
5322 if ((in_flags & MSG_PEEK) == 0) {
5327 if ((so->so_rcv.sb_cc == 0) &&
5328 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5329 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5330 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5332 * For active open side clear flags for
5333 * re-use passive open is blocked by
5336 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5338 * You were aborted, passive side
5341 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5344 so->so_state &= ~(SS_ISCONNECTING |
5345 SS_ISDISCONNECTING |
5349 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5350 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5357 if (block_allowed) {
5358 error = sbwait(&so->so_rcv);
5363 goto restart_nosblocks;
5365 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5366 error = EWOULDBLOCK;
5370 if (hold_sblock == 1) {
5371 SOCKBUF_UNLOCK(&so->so_rcv);
5374 /* we possibly have data we can read */
5375 /* sa_ignore FREED_MEMORY */
5376 control = TAILQ_FIRST(&inp->read_queue);
5377 if (control == NULL) {
5379 * This could be happening since the appender did the
5380 * increment but as not yet did the tailq insert onto the
5383 if (hold_rlock == 0) {
5384 SCTP_INP_READ_LOCK(inp);
5386 control = TAILQ_FIRST(&inp->read_queue);
5387 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5389 panic("Huh, its non zero and nothing on control?");
5391 so->so_rcv.sb_cc = 0;
5393 SCTP_INP_READ_UNLOCK(inp);
5398 if ((control->length == 0) &&
5399 (control->do_not_ref_stcb)) {
5401 * Clean up code for freeing assoc that left behind a
5402 * pdapi.. maybe a peer in EEOR that just closed after
5403 * sending and never indicated a EOR.
5405 if (hold_rlock == 0) {
5407 SCTP_INP_READ_LOCK(inp);
5409 control->held_length = 0;
5410 if (control->data) {
5411 /* Hmm there is data here .. fix */
5415 m_tmp = control->data;
5417 cnt += SCTP_BUF_LEN(m_tmp);
5418 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5419 control->tail_mbuf = m_tmp;
5420 control->end_added = 1;
5422 m_tmp = SCTP_BUF_NEXT(m_tmp);
5424 control->length = cnt;
5427 TAILQ_REMOVE(&inp->read_queue, control, next);
5428 /* Add back any hiddend data */
5429 sctp_free_remote_addr(control->whoFrom);
5430 sctp_free_a_readq(stcb, control);
5434 SCTP_INP_READ_UNLOCK(inp);
5438 if ((control->length == 0) &&
5439 (control->end_added == 1)) {
5441 * Do we also need to check for (control->pdapi_aborted ==
5444 if (hold_rlock == 0) {
5446 SCTP_INP_READ_LOCK(inp);
5448 TAILQ_REMOVE(&inp->read_queue, control, next);
5449 if (control->data) {
5451 panic("control->data not null but control->length == 0");
5453 SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5454 sctp_m_freem(control->data);
5455 control->data = NULL;
5458 if (control->aux_data) {
5459 sctp_m_free(control->aux_data);
5460 control->aux_data = NULL;
5463 if (control->on_strm_q) {
5464 panic("About to free ctl:%p so:%p and its in %d",
5465 control, so, control->on_strm_q);
5468 sctp_free_remote_addr(control->whoFrom);
5469 sctp_free_a_readq(stcb, control);
5472 SCTP_INP_READ_UNLOCK(inp);
5476 if (control->length == 0) {
5477 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5479 /* find a more suitable one then this */
5480 ctl = TAILQ_NEXT(control, next);
5482 if ((ctl->stcb != control->stcb) && (ctl->length) &&
5484 (ctl->spec_flags & M_NOTIFICATION) ||
5485 ((ctl->do_not_ref_stcb == 0) &&
5486 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
5489 * If we have a different TCB next, and there is data
5490 * present. If we have already taken some (pdapi), OR we can
5491 * ref the tcb and no delivery as started on this stream, we
5492 * take it. Note we allow a notification on a different
5493 * assoc to be delivered..
5497 } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
5499 ((ctl->some_taken) ||
5500 ((ctl->do_not_ref_stcb == 0) &&
5501 ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5502 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
5504 * If we have the same tcb, and there is data present, and we
5505 * have the strm interleave feature present. Then if we have
5506 * taken some (pdapi) or we can refer to tht tcb AND we have
5507 * not started a delivery for this stream, we can take it.
5508 * Note we do NOT allow a notificaiton on the same assoc to
5514 ctl = TAILQ_NEXT(ctl, next);
5518 * if we reach here, not suitable replacement is available
5519 * <or> fragment interleave is NOT on. So stuff the sb_cc
5520 * into the our held count, and its time to sleep again.
5522 held_length = so->so_rcv.sb_cc;
5523 control->held_length = so->so_rcv.sb_cc;
5526 /* Clear the held length since there is something to read */
5527 control->held_length = 0;
5530 * If we reach here, control has a some data for us to read off.
5531 * Note that stcb COULD be NULL.
5533 if (hold_rlock == 0) {
5535 SCTP_INP_READ_LOCK(inp);
5537 control->some_taken++;
5538 stcb = control->stcb;
5540 if ((control->do_not_ref_stcb == 0) &&
5541 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5542 if (freecnt_applied == 0)
5544 } else if (control->do_not_ref_stcb == 0) {
5545 /* you can't free it on me please */
5547 * The lock on the socket buffer protects us so the
5548 * free code will stop. But since we used the
5549 * socketbuf lock and the sender uses the tcb_lock
5550 * to increment, we need to use the atomic add to
5553 if (freecnt_applied) {
5555 panic("refcnt already incremented");
5557 SCTP_PRINTF("refcnt already incremented?\n");
5560 atomic_add_int(&stcb->asoc.refcnt, 1);
5561 freecnt_applied = 1;
5564 * Setup to remember how much we have not yet told
5565 * the peer our rwnd has opened up. Note we grab the
5566 * value from the tcb from last time. Note too that
5567 * sack sending clears this when a sack is sent,
5568 * which is fine. Once we hit the rwnd_req, we then
5569 * will go to the sctp_user_rcvd() that will not
5570 * lock until it KNOWs it MUST send a WUP-SACK.
5572 freed_so_far = stcb->freed_by_sorcv_sincelast;
5573 stcb->freed_by_sorcv_sincelast = 0;
5577 ((control->spec_flags & M_NOTIFICATION) == 0) &&
5578 control->do_not_ref_stcb == 0) {
5579 stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5582 /* First lets get off the sinfo and sockaddr info */
5583 if ((sinfo != NULL) && (filling_sinfo != 0)) {
5584 sinfo->sinfo_stream = control->sinfo_stream;
5585 sinfo->sinfo_ssn = (uint16_t)control->mid;
5586 sinfo->sinfo_flags = control->sinfo_flags;
5587 sinfo->sinfo_ppid = control->sinfo_ppid;
5588 sinfo->sinfo_context = control->sinfo_context;
5589 sinfo->sinfo_timetolive = control->sinfo_timetolive;
5590 sinfo->sinfo_tsn = control->sinfo_tsn;
5591 sinfo->sinfo_cumtsn = control->sinfo_cumtsn;
5592 sinfo->sinfo_assoc_id = control->sinfo_assoc_id;
5593 nxt = TAILQ_NEXT(control, next);
5594 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
5595 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
5596 struct sctp_extrcvinfo *s_extra;
5598 s_extra = (struct sctp_extrcvinfo *)sinfo;
5601 s_extra->serinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5602 if (nxt->sinfo_flags & SCTP_UNORDERED) {
5603 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5605 if (nxt->spec_flags & M_NOTIFICATION) {
5606 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5608 s_extra->serinfo_next_aid = nxt->sinfo_assoc_id;
5609 s_extra->serinfo_next_length = nxt->length;
5610 s_extra->serinfo_next_ppid = nxt->sinfo_ppid;
5611 s_extra->serinfo_next_stream = nxt->sinfo_stream;
5612 if (nxt->tail_mbuf != NULL) {
5613 if (nxt->end_added) {
5614 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5619 * we explicitly 0 this, since the memcpy
5620 * got some other things beyond the older
5621 * sinfo_ that is on the control's structure
5625 s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
5626 s_extra->serinfo_next_aid = 0;
5627 s_extra->serinfo_next_length = 0;
5628 s_extra->serinfo_next_ppid = 0;
5629 s_extra->serinfo_next_stream = 0;
5633 * update off the real current cum-ack, if we have an stcb.
5635 if ((control->do_not_ref_stcb == 0) && stcb)
5636 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5638 * mask off the high bits, we keep the actual chunk bits in
5641 sinfo->sinfo_flags &= 0x00ff;
5642 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
5643 sinfo->sinfo_flags |= SCTP_UNORDERED;
5646 #ifdef SCTP_ASOCLOG_OF_TSNS
5648 int index, newindex;
5649 struct sctp_pcbtsn_rlog *entry;
5652 index = inp->readlog_index;
5653 newindex = index + 1;
5654 if (newindex >= SCTP_READ_LOG_SIZE) {
5657 } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
5658 entry = &inp->readlog[index];
5659 entry->vtag = control->sinfo_assoc_id;
5660 entry->strm = control->sinfo_stream;
5661 entry->seq = (uint16_t)control->mid;
5662 entry->sz = control->length;
5663 entry->flgs = control->sinfo_flags;
5666 if ((fromlen > 0) && (from != NULL)) {
5667 union sctp_sockstore store;
5670 switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5673 len = sizeof(struct sockaddr_in6);
5674 store.sin6 = control->whoFrom->ro._l_addr.sin6;
5675 store.sin6.sin6_port = control->port_from;
5681 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
5682 len = sizeof(struct sockaddr_in6);
5683 in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
5685 store.sin6.sin6_port = control->port_from;
5687 len = sizeof(struct sockaddr_in);
5688 store.sin = control->whoFrom->ro._l_addr.sin;
5689 store.sin.sin_port = control->port_from;
5692 len = sizeof(struct sockaddr_in);
5693 store.sin = control->whoFrom->ro._l_addr.sin;
5694 store.sin.sin_port = control->port_from;
5702 memcpy(from, &store, min((size_t)fromlen, len));
5705 struct sockaddr_in6 lsa6, *from6;
5707 from6 = (struct sockaddr_in6 *)from;
5708 sctp_recover_scope_mac(from6, (&lsa6));
5713 SCTP_INP_READ_UNLOCK(inp);
5717 SOCKBUF_UNLOCK(&so->so_rcv);
5720 /* now copy out what data we can */
5722 /* copy out each mbuf in the chain up to length */
5726 /* Move out all we can */
5727 cp_len = (int)uio->uio_resid;
5728 my_len = (int)SCTP_BUF_LEN(m);
5729 if (cp_len > my_len) {
5730 /* not enough in this buf */
5734 SCTP_INP_READ_UNLOCK(inp);
5738 error = uiomove(mtod(m, char *), cp_len, uio);
5740 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5744 if ((control->do_not_ref_stcb == 0) && stcb &&
5745 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5749 /* error we are out of here */
5752 SCTP_INP_READ_LOCK(inp);
5754 if (cp_len == SCTP_BUF_LEN(m)) {
5755 if ((SCTP_BUF_NEXT(m) == NULL) &&
5756 (control->end_added)) {
5757 out_flags |= MSG_EOR;
5758 if ((control->do_not_ref_stcb == 0) &&
5759 (control->stcb != NULL) &&
5760 ((control->spec_flags & M_NOTIFICATION) == 0))
5761 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5763 if (control->spec_flags & M_NOTIFICATION) {
5764 out_flags |= MSG_NOTIFICATION;
5766 /* we ate up the mbuf */
5767 if (in_flags & MSG_PEEK) {
5769 m = SCTP_BUF_NEXT(m);
5770 copied_so_far += cp_len;
5772 /* dispose of the mbuf */
5773 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5774 sctp_sblog(&so->so_rcv,
5775 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5777 sctp_sbfree(control, stcb, &so->so_rcv, m);
5778 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5779 sctp_sblog(&so->so_rcv,
5780 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5782 copied_so_far += cp_len;
5783 freed_so_far += cp_len;
5784 freed_so_far += MSIZE;
5785 atomic_subtract_int(&control->length, cp_len);
5786 control->data = sctp_m_free(m);
5789 * been through it all, must hold sb
5790 * lock ok to null tail
5792 if (control->data == NULL) {
5794 if ((control->end_added == 0) ||
5795 (TAILQ_NEXT(control, next) == NULL)) {
5803 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5804 panic("Hmm we don't own the lock?");
5808 control->tail_mbuf = NULL;
5810 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5811 panic("end_added, nothing left and no MSG_EOR");
5817 /* Do we need to trim the mbuf? */
5818 if (control->spec_flags & M_NOTIFICATION) {
5819 out_flags |= MSG_NOTIFICATION;
5821 if ((in_flags & MSG_PEEK) == 0) {
5822 SCTP_BUF_RESV_UF(m, cp_len);
5823 SCTP_BUF_LEN(m) -= cp_len;
5824 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5825 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
5827 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5828 if ((control->do_not_ref_stcb == 0) &&
5830 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5832 copied_so_far += cp_len;
5833 freed_so_far += cp_len;
5834 freed_so_far += MSIZE;
5835 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5836 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5837 SCTP_LOG_SBRESULT, 0);
5839 atomic_subtract_int(&control->length, cp_len);
5841 copied_so_far += cp_len;
5844 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5847 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5848 (control->do_not_ref_stcb == 0) &&
5849 (freed_so_far >= rwnd_req)) {
5850 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5852 } /* end while(m) */
5854 * At this point we have looked at it all and we either have
5855 * a MSG_EOR/or read all the user wants... <OR>
5856 * control->length == 0.
5858 if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5859 /* we are done with this control */
5860 if (control->length == 0) {
5861 if (control->data) {
5863 panic("control->data not null at read eor?");
5865 SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5866 sctp_m_freem(control->data);
5867 control->data = NULL;
5871 if (hold_rlock == 0) {
5872 SCTP_INP_READ_LOCK(inp);
5875 TAILQ_REMOVE(&inp->read_queue, control, next);
5876 /* Add back any hiddend data */
5877 if (control->held_length) {
5879 control->held_length = 0;
5880 wakeup_read_socket = 1;
5882 if (control->aux_data) {
5883 sctp_m_free(control->aux_data);
5884 control->aux_data = NULL;
5886 no_rcv_needed = control->do_not_ref_stcb;
5887 sctp_free_remote_addr(control->whoFrom);
5888 control->data = NULL;
5890 if (control->on_strm_q) {
5891 panic("About to free ctl:%p so:%p and its in %d",
5892 control, so, control->on_strm_q);
5895 sctp_free_a_readq(stcb, control);
5897 if ((freed_so_far >= rwnd_req) &&
5898 (no_rcv_needed == 0))
5899 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5903 * The user did not read all of this
5904 * message, turn off the returned MSG_EOR
5905 * since we are leaving more behind on the
5909 if (control->end_added &&
5910 (control->data == NULL) &&
5911 (control->tail_mbuf == NULL)) {
5912 panic("Gak, control->length is corrupt?");
5915 no_rcv_needed = control->do_not_ref_stcb;
5916 out_flags &= ~MSG_EOR;
5919 if (out_flags & MSG_EOR) {
5922 if ((uio->uio_resid == 0) ||
5924 (copied_so_far >= (uint32_t)max(so->so_rcv.sb_lowat, 1)))) {
5928 * If I hit here the receiver wants more and this message is
5929 * NOT done (pd-api). So two questions. Can we block? if not
5930 * we are done. Did the user NOT set MSG_WAITALL?
5932 if (block_allowed == 0) {
5936 * We need to wait for more data a few things: - We don't
5937 * sbunlock() so we don't get someone else reading. - We
5938 * must be sure to account for the case where what is added
5939 * is NOT to our control when we wakeup.
5943 * Do we need to tell the transport a rwnd update might be
5944 * needed before we go to sleep?
5946 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5947 ((freed_so_far >= rwnd_req) &&
5948 (control->do_not_ref_stcb == 0) &&
5949 (no_rcv_needed == 0))) {
5950 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5953 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5957 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5960 if (hold_rlock == 1) {
5961 SCTP_INP_READ_UNLOCK(inp);
5964 if (hold_sblock == 0) {
5965 SOCKBUF_LOCK(&so->so_rcv);
5968 if ((copied_so_far) && (control->length == 0) &&
5969 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
5972 if (so->so_rcv.sb_cc <= control->held_length) {
5973 error = sbwait(&so->so_rcv);
5977 control->held_length = 0;
5980 SOCKBUF_UNLOCK(&so->so_rcv);
5983 if (control->length == 0) {
5984 /* still nothing here */
5985 if (control->end_added == 1) {
5986 /* he aborted, or is done i.e.did a shutdown */
5987 out_flags |= MSG_EOR;
5988 if (control->pdapi_aborted) {
5989 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5990 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5992 out_flags |= MSG_TRUNC;
5994 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5995 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5997 goto done_with_control;
5999 if (so->so_rcv.sb_cc > held_length) {
6000 control->held_length = so->so_rcv.sb_cc;
6003 goto wait_some_more;
6004 } else if (control->data == NULL) {
6006 * we must re-sync since data is probably being
6009 SCTP_INP_READ_LOCK(inp);
6010 if ((control->length > 0) && (control->data == NULL)) {
6012 * big trouble.. we have the lock and its
6016 panic("Impossible data==NULL length !=0");
6018 out_flags |= MSG_EOR;
6019 out_flags |= MSG_TRUNC;
6020 control->length = 0;
6021 SCTP_INP_READ_UNLOCK(inp);
6022 goto done_with_control;
6024 SCTP_INP_READ_UNLOCK(inp);
6025 /* We will fall around to get more data */
6030 * Give caller back the mbuf chain,
6031 * store in uio_resid the length
6033 wakeup_read_socket = 0;
6034 if ((control->end_added == 0) ||
6035 (TAILQ_NEXT(control, next) == NULL)) {
6036 /* Need to get rlock */
6037 if (hold_rlock == 0) {
6038 SCTP_INP_READ_LOCK(inp);
6042 if (control->end_added) {
6043 out_flags |= MSG_EOR;
6044 if ((control->do_not_ref_stcb == 0) &&
6045 (control->stcb != NULL) &&
6046 ((control->spec_flags & M_NOTIFICATION) == 0))
6047 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6049 if (control->spec_flags & M_NOTIFICATION) {
6050 out_flags |= MSG_NOTIFICATION;
6052 uio->uio_resid = control->length;
6053 *mp = control->data;
6056 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6057 sctp_sblog(&so->so_rcv,
6058 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
6060 sctp_sbfree(control, stcb, &so->so_rcv, m);
6061 freed_so_far += SCTP_BUF_LEN(m);
6062 freed_so_far += MSIZE;
6063 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6064 sctp_sblog(&so->so_rcv,
6065 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
6067 m = SCTP_BUF_NEXT(m);
6069 control->data = control->tail_mbuf = NULL;
6070 control->length = 0;
6071 if (out_flags & MSG_EOR) {
6072 /* Done with this control */
6073 goto done_with_control;
6077 if (hold_rlock == 1) {
6078 SCTP_INP_READ_UNLOCK(inp);
6081 if (hold_sblock == 1) {
6082 SOCKBUF_UNLOCK(&so->so_rcv);
6086 sbunlock(&so->so_rcv);
6091 SOCKBUF_UNLOCK(&so->so_rcv);
6094 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6095 if ((freed_so_far >= rwnd_req) &&
6096 (control && (control->do_not_ref_stcb == 0)) &&
6097 (no_rcv_needed == 0))
6098 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6102 *msg_flags = out_flags;
6104 if (((out_flags & MSG_EOR) == 0) &&
6105 ((in_flags & MSG_PEEK) == 0) &&
6107 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6108 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
6109 struct sctp_extrcvinfo *s_extra;
6111 s_extra = (struct sctp_extrcvinfo *)sinfo;
6112 s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
6114 if (hold_rlock == 1) {
6115 SCTP_INP_READ_UNLOCK(inp);
6118 SOCKBUF_UNLOCK(&so->so_rcv);
6121 sbunlock(&so->so_rcv);
6124 if (freecnt_applied) {
6126 * The lock on the socket buffer protects us so the free
6127 * code will stop. But since we used the socketbuf lock and
6128 * the sender uses the tcb_lock to increment, we need to use
6129 * the atomic add to the refcnt.
6133 panic("stcb for refcnt has gone NULL?");
6139 /* Save the value back for next time */
6140 stcb->freed_by_sorcv_sincelast = freed_so_far;
6141 atomic_add_int(&stcb->asoc.refcnt, -1);
6143 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6145 sctp_misc_ints(SCTP_SORECV_DONE,
6147 (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6151 sctp_misc_ints(SCTP_SORECV_DONE,
6153 (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6159 if (wakeup_read_socket) {
6160 sctp_sorwakeup(inp, so);
6166 #ifdef SCTP_MBUF_LOGGING
6168 sctp_m_free(struct mbuf *m)
6170 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6171 sctp_log_mb(m, SCTP_MBUF_IFREE);
6177 sctp_m_freem(struct mbuf *mb)
6180 mb = sctp_m_free(mb);
6186 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
6189 * Given a local address. For all associations that holds the
6190 * address, request a peer-set-primary.
6192 struct sctp_ifa *ifa;
6193 struct sctp_laddr *wi;
6195 ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
6197 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
6198 return (EADDRNOTAVAIL);
6201 * Now that we have the ifa we must awaken the iterator with this
6204 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6206 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
6209 /* Now incr the count and int wi structure */
6210 SCTP_INCR_LADDR_COUNT();
6211 memset(wi, 0, sizeof(*wi));
6212 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
6214 wi->action = SCTP_SET_PRIM_ADDR;
6215 atomic_add_int(&ifa->refcount, 1);
6217 /* Now add it to the work queue */
6218 SCTP_WQ_ADDR_LOCK();
6220 * Should this really be a tailq? As it is we will process the
6223 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6224 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
6225 (struct sctp_inpcb *)NULL,
6226 (struct sctp_tcb *)NULL,
6227 (struct sctp_nets *)NULL);
6228 SCTP_WQ_ADDR_UNLOCK();
6234 sctp_soreceive(struct socket *so,
6235 struct sockaddr **psa,
6238 struct mbuf **controlp,
6242 uint8_t sockbuf[256];
6243 struct sockaddr *from;
6244 struct sctp_extrcvinfo sinfo;
6245 int filling_sinfo = 1;
6247 struct sctp_inpcb *inp;
6249 inp = (struct sctp_inpcb *)so->so_pcb;
6250 /* pickup the assoc we are reading from */
6252 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6255 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6256 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6257 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6258 (controlp == NULL)) {
6259 /* user does not want the sndrcv ctl */
6263 from = (struct sockaddr *)sockbuf;
6264 fromlen = sizeof(sockbuf);
6271 if (filling_sinfo) {
6272 memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6274 if (flagsp != NULL) {
6279 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, &flags,
6280 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6281 if (flagsp != NULL) {
6284 if (controlp != NULL) {
6285 /* copy back the sinfo in a CMSG format */
6286 if (filling_sinfo && ((flags & MSG_NOTIFICATION) == 0)) {
6287 *controlp = sctp_build_ctl_nchunk(inp,
6288 (struct sctp_sndrcvinfo *)&sinfo);
6294 /* copy back the address info */
6295 if (from && from->sa_len) {
6296 *psa = sodupsockaddr(from, M_NOWAIT);
6309 sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6310 int totaddr, int *error)
6314 struct sctp_inpcb *inp;
6315 struct sockaddr *sa;
6318 struct sockaddr_in *sin;
6321 struct sockaddr_in6 *sin6;
6325 inp = stcb->sctp_ep;
6327 for (i = 0; i < totaddr; i++) {
6328 switch (sa->sa_family) {
6331 incr = sizeof(struct sockaddr_in);
6332 sin = (struct sockaddr_in *)sa;
6333 if ((sin->sin_addr.s_addr == INADDR_ANY) ||
6334 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
6335 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
6336 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6337 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6338 SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
6342 if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
6343 SCTP_DONOT_SETSCOPE,
6344 SCTP_ADDR_IS_CONFIRMED)) {
6345 /* assoc gone no un-lock */
6346 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6347 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6348 SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
6357 incr = sizeof(struct sockaddr_in6);
6358 sin6 = (struct sockaddr_in6 *)sa;
6359 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
6360 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
6361 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6362 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6363 SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
6367 if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
6368 SCTP_DONOT_SETSCOPE,
6369 SCTP_ADDR_IS_CONFIRMED)) {
6370 /* assoc gone no un-lock */
6371 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6372 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6373 SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
6383 sa = (struct sockaddr *)((caddr_t)sa + incr);
6390 sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6391 unsigned int *totaddr,
6392 unsigned int *num_v4, unsigned int *num_v6, int *error,
6393 unsigned int limit, int *bad_addr)
6395 struct sockaddr *sa;
6396 struct sctp_tcb *stcb = NULL;
6397 unsigned int incr, at, i;
6401 *error = *num_v6 = *num_v4 = 0;
6402 /* account and validate addresses */
6403 for (i = 0; i < *totaddr; i++) {
6404 switch (sa->sa_family) {
6407 incr = (unsigned int)sizeof(struct sockaddr_in);
6408 if (sa->sa_len != incr) {
6409 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6420 struct sockaddr_in6 *sin6;
6422 sin6 = (struct sockaddr_in6 *)sa;
6423 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6424 /* Must be non-mapped for connectx */
6425 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6430 incr = (unsigned int)sizeof(struct sockaddr_in6);
6431 if (sa->sa_len != incr) {
6432 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6447 if (i == *totaddr) {
6450 SCTP_INP_INCR_REF(inp);
6451 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
6453 /* Already have or am bring up an association */
6456 SCTP_INP_DECR_REF(inp);
6458 if ((at + incr) > limit) {
6462 sa = (struct sockaddr *)((caddr_t)sa + incr);
6464 return ((struct sctp_tcb *)NULL);
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();
7298 #if defined(INET6) || defined(INET)
7301 * sctp_min_mtu ()returns the minimum of all non-zero arguments.
7302 * If all arguments are zero, zero is returned.
7305 sctp_min_mtu(uint32_t mtu1, uint32_t mtu2, uint32_t mtu3)
7310 return (min(mtu1, min(mtu2, mtu3)));
7312 return (min(mtu1, mtu2));
7316 return (min(mtu1, mtu3));
7324 return (min(mtu2, mtu3));
7335 sctp_hc_set_mtu(union sctp_sockstore *addr, uint16_t fibnum, uint32_t mtu)
7337 struct in_conninfo inc;
7339 memset(&inc, 0, sizeof(struct in_conninfo));
7340 inc.inc_fibnum = fibnum;
7341 switch (addr->sa.sa_family) {
7344 inc.inc_faddr = addr->sin.sin_addr;
7349 inc.inc_flags |= INC_ISIPV6;
7350 inc.inc6_faddr = addr->sin6.sin6_addr;
7356 tcp_hc_updatemtu(&inc, (u_long)mtu);
7360 sctp_hc_get_mtu(union sctp_sockstore *addr, uint16_t fibnum)
7362 struct in_conninfo inc;
7364 memset(&inc, 0, sizeof(struct in_conninfo));
7365 inc.inc_fibnum = fibnum;
7366 switch (addr->sa.sa_family) {
7369 inc.inc_faddr = addr->sin.sin_addr;
7374 inc.inc_flags |= INC_ISIPV6;
7375 inc.inc6_faddr = addr->sin6.sin6_addr;
7381 return ((uint32_t)tcp_hc_getmtu(&inc));