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, ssize_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 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 /* sent queue SHOULD be empty */
3888 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3889 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3890 asoc->sent_queue_cnt--;
3891 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
3892 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
3893 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
3896 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
3900 if (chk->data != NULL) {
3901 sctp_free_bufspace(stcb, asoc, chk, 1);
3902 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
3903 error, chk, so_locked);
3905 sctp_m_freem(chk->data);
3909 sctp_free_a_chunk(stcb, chk, so_locked);
3910 /* sa_ignore FREED_MEMORY */
3912 /* pending send queue SHOULD be empty */
3913 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3914 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3915 asoc->send_queue_cnt--;
3916 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
3917 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
3920 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
3923 if (chk->data != NULL) {
3924 sctp_free_bufspace(stcb, asoc, chk, 1);
3925 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
3926 error, chk, so_locked);
3928 sctp_m_freem(chk->data);
3932 sctp_free_a_chunk(stcb, chk, so_locked);
3933 /* sa_ignore FREED_MEMORY */
3935 for (i = 0; i < asoc->streamoutcnt; i++) {
3936 /* For each stream */
3937 outs = &asoc->strmout[i];
3938 /* clean up any sends there */
3939 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
3940 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
3941 TAILQ_REMOVE(&outs->outqueue, sp, next);
3942 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
3943 sctp_free_spbufspace(stcb, asoc, sp);
3945 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3946 error, (void *)sp, so_locked);
3948 sctp_m_freem(sp->data);
3950 sp->tail_mbuf = NULL;
3955 sctp_free_remote_addr(sp->net);
3958 /* Free the chunk */
3959 sctp_free_a_strmoq(stcb, sp, so_locked);
3960 /* sa_ignore FREED_MEMORY */
3966 sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
3967 struct sctp_abort_chunk *abort, int so_locked
3968 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3976 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3977 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3978 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3979 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3981 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3982 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3983 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3986 SCTP_TCB_SEND_LOCK(stcb);
3987 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
3988 /* Tell them we lost the asoc */
3989 sctp_report_all_outbound(stcb, error, so_locked);
3990 SCTP_TCB_SEND_UNLOCK(stcb);
3992 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
3994 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
3999 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4000 struct mbuf *m, int iphlen,
4001 struct sockaddr *src, struct sockaddr *dst,
4002 struct sctphdr *sh, struct mbuf *op_err,
4003 uint8_t mflowtype, uint32_t mflowid,
4004 uint32_t vrf_id, uint16_t port)
4007 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4013 vtag = stcb->asoc.peer_vtag;
4014 vrf_id = stcb->asoc.vrf_id;
4016 sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
4017 mflowtype, mflowid, inp->fibnum,
4020 /* We have a TCB to abort, send notification too */
4021 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
4022 /* Ok, now lets free it */
4023 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4024 so = SCTP_INP_SO(inp);
4025 atomic_add_int(&stcb->asoc.refcnt, 1);
4026 SCTP_TCB_UNLOCK(stcb);
4027 SCTP_SOCKET_LOCK(so, 1);
4028 SCTP_TCB_LOCK(stcb);
4029 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4031 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4032 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4033 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4034 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4036 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4037 SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
4038 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4039 SCTP_SOCKET_UNLOCK(so, 1);
4043 #ifdef SCTP_ASOCLOG_OF_TSNS
4045 sctp_print_out_track_log(struct sctp_tcb *stcb)
4050 SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
4051 SCTP_PRINTF("IN bound TSN log-aaa\n");
4052 if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
4053 SCTP_PRINTF("None rcvd\n");
4056 if (stcb->asoc.tsn_in_wrapped) {
4057 for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
4058 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4059 stcb->asoc.in_tsnlog[i].tsn,
4060 stcb->asoc.in_tsnlog[i].strm,
4061 stcb->asoc.in_tsnlog[i].seq,
4062 stcb->asoc.in_tsnlog[i].flgs,
4063 stcb->asoc.in_tsnlog[i].sz);
4066 if (stcb->asoc.tsn_in_at) {
4067 for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
4068 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4069 stcb->asoc.in_tsnlog[i].tsn,
4070 stcb->asoc.in_tsnlog[i].strm,
4071 stcb->asoc.in_tsnlog[i].seq,
4072 stcb->asoc.in_tsnlog[i].flgs,
4073 stcb->asoc.in_tsnlog[i].sz);
4077 SCTP_PRINTF("OUT bound TSN log-aaa\n");
4078 if ((stcb->asoc.tsn_out_at == 0) &&
4079 (stcb->asoc.tsn_out_wrapped == 0)) {
4080 SCTP_PRINTF("None sent\n");
4082 if (stcb->asoc.tsn_out_wrapped) {
4083 for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4084 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4085 stcb->asoc.out_tsnlog[i].tsn,
4086 stcb->asoc.out_tsnlog[i].strm,
4087 stcb->asoc.out_tsnlog[i].seq,
4088 stcb->asoc.out_tsnlog[i].flgs,
4089 stcb->asoc.out_tsnlog[i].sz);
4092 if (stcb->asoc.tsn_out_at) {
4093 for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4094 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4095 stcb->asoc.out_tsnlog[i].tsn,
4096 stcb->asoc.out_tsnlog[i].strm,
4097 stcb->asoc.out_tsnlog[i].seq,
4098 stcb->asoc.out_tsnlog[i].flgs,
4099 stcb->asoc.out_tsnlog[i].sz);
4107 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4108 struct mbuf *op_err,
4110 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4115 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4119 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4120 so = SCTP_INP_SO(inp);
4123 /* Got to have a TCB */
4124 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4125 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4126 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4127 SCTP_CALLED_DIRECTLY_NOCMPSET);
4132 /* notify the peer */
4133 sctp_send_abort_tcb(stcb, op_err, so_locked);
4134 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4135 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4136 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4137 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4139 /* notify the ulp */
4140 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4141 sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4143 /* now free the asoc */
4144 #ifdef SCTP_ASOCLOG_OF_TSNS
4145 sctp_print_out_track_log(stcb);
4147 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4149 atomic_add_int(&stcb->asoc.refcnt, 1);
4150 SCTP_TCB_UNLOCK(stcb);
4151 SCTP_SOCKET_LOCK(so, 1);
4152 SCTP_TCB_LOCK(stcb);
4153 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4156 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4157 SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4158 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4160 SCTP_SOCKET_UNLOCK(so, 1);
4166 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4167 struct sockaddr *src, struct sockaddr *dst,
4168 struct sctphdr *sh, struct sctp_inpcb *inp,
4170 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4171 uint32_t vrf_id, uint16_t port)
4173 struct sctp_chunkhdr *ch, chunk_buf;
4174 unsigned int chk_length;
4175 int contains_init_chunk;
4177 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4178 /* Generate a TO address for future reference */
4179 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4180 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4181 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4182 SCTP_CALLED_DIRECTLY_NOCMPSET);
4185 contains_init_chunk = 0;
4186 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4187 sizeof(*ch), (uint8_t *)&chunk_buf);
4188 while (ch != NULL) {
4189 chk_length = ntohs(ch->chunk_length);
4190 if (chk_length < sizeof(*ch)) {
4191 /* break to abort land */
4194 switch (ch->chunk_type) {
4196 contains_init_chunk = 1;
4198 case SCTP_PACKET_DROPPED:
4199 /* we don't respond to pkt-dropped */
4201 case SCTP_ABORT_ASSOCIATION:
4202 /* we don't respond with an ABORT to an ABORT */
4204 case SCTP_SHUTDOWN_COMPLETE:
4206 * we ignore it since we are not waiting for it and
4210 case SCTP_SHUTDOWN_ACK:
4211 sctp_send_shutdown_complete2(src, dst, sh,
4212 mflowtype, mflowid, fibnum,
4218 offset += SCTP_SIZE32(chk_length);
4219 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4220 sizeof(*ch), (uint8_t *)&chunk_buf);
4222 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4223 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4224 (contains_init_chunk == 0))) {
4225 sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4226 mflowtype, mflowid, fibnum,
4232 * check the inbound datagram to make sure there is not an abort inside it,
4233 * if there is return 1, else return 0.
4236 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t *vtagfill)
4238 struct sctp_chunkhdr *ch;
4239 struct sctp_init_chunk *init_chk, chunk_buf;
4241 unsigned int chk_length;
4243 offset = iphlen + sizeof(struct sctphdr);
4244 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4245 (uint8_t *)&chunk_buf);
4246 while (ch != NULL) {
4247 chk_length = ntohs(ch->chunk_length);
4248 if (chk_length < sizeof(*ch)) {
4249 /* packet is probably corrupt */
4252 /* we seem to be ok, is it an abort? */
4253 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4254 /* yep, tell them */
4257 if (ch->chunk_type == SCTP_INITIATION) {
4258 /* need to update the Vtag */
4259 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4260 offset, sizeof(*init_chk), (uint8_t *)&chunk_buf);
4261 if (init_chk != NULL) {
4262 *vtagfill = ntohl(init_chk->init.initiate_tag);
4265 /* Nope, move to the next chunk */
4266 offset += SCTP_SIZE32(chk_length);
4267 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4268 sizeof(*ch), (uint8_t *)&chunk_buf);
4274 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4275 * set (i.e. it's 0) so, create this function to compare link local scopes
4279 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4281 struct sockaddr_in6 a, b;
4287 if (a.sin6_scope_id == 0)
4288 if (sa6_recoverscope(&a)) {
4289 /* can't get scope, so can't match */
4292 if (b.sin6_scope_id == 0)
4293 if (sa6_recoverscope(&b)) {
4294 /* can't get scope, so can't match */
4297 if (a.sin6_scope_id != b.sin6_scope_id)
4304 * returns a sockaddr_in6 with embedded scope recovered and removed
4306 struct sockaddr_in6 *
4307 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4309 /* check and strip embedded scope junk */
4310 if (addr->sin6_family == AF_INET6) {
4311 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4312 if (addr->sin6_scope_id == 0) {
4314 if (!sa6_recoverscope(store)) {
4315 /* use the recovered scope */
4319 /* else, return the original "to" addr */
4320 in6_clearscope(&addr->sin6_addr);
4329 * are the two addresses the same? currently a "scopeless" check returns: 1
4333 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4337 if (sa1 == NULL || sa2 == NULL)
4340 /* must be the same family */
4341 if (sa1->sa_family != sa2->sa_family)
4344 switch (sa1->sa_family) {
4348 /* IPv6 addresses */
4349 struct sockaddr_in6 *sin6_1, *sin6_2;
4351 sin6_1 = (struct sockaddr_in6 *)sa1;
4352 sin6_2 = (struct sockaddr_in6 *)sa2;
4353 return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4360 /* IPv4 addresses */
4361 struct sockaddr_in *sin_1, *sin_2;
4363 sin_1 = (struct sockaddr_in *)sa1;
4364 sin_2 = (struct sockaddr_in *)sa2;
4365 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
4369 /* we don't do these... */
4375 sctp_print_address(struct sockaddr *sa)
4378 char ip6buf[INET6_ADDRSTRLEN];
4381 switch (sa->sa_family) {
4385 struct sockaddr_in6 *sin6;
4387 sin6 = (struct sockaddr_in6 *)sa;
4388 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
4389 ip6_sprintf(ip6buf, &sin6->sin6_addr),
4390 ntohs(sin6->sin6_port),
4391 sin6->sin6_scope_id);
4398 struct sockaddr_in *sin;
4401 sin = (struct sockaddr_in *)sa;
4402 p = (unsigned char *)&sin->sin_addr;
4403 SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4404 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
4415 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4416 struct sctp_inpcb *new_inp,
4417 struct sctp_tcb *stcb,
4421 * go through our old INP and pull off any control structures that
4422 * belong to stcb and move then to the new inp.
4424 struct socket *old_so, *new_so;
4425 struct sctp_queued_to_read *control, *nctl;
4426 struct sctp_readhead tmp_queue;
4430 old_so = old_inp->sctp_socket;
4431 new_so = new_inp->sctp_socket;
4432 TAILQ_INIT(&tmp_queue);
4433 error = sblock(&old_so->so_rcv, waitflags);
4436 * Gak, can't get sblock, we have a problem. data will be
4437 * left stranded.. and we don't dare look at it since the
4438 * other thread may be reading something. Oh well, its a
4439 * screwed up app that does a peeloff OR a accept while
4440 * reading from the main socket... actually its only the
4441 * peeloff() case, since I think read will fail on a
4442 * listening socket..
4446 /* lock the socket buffers */
4447 SCTP_INP_READ_LOCK(old_inp);
4448 TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4449 /* Pull off all for out target stcb */
4450 if (control->stcb == stcb) {
4451 /* remove it we want it */
4452 TAILQ_REMOVE(&old_inp->read_queue, control, next);
4453 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4456 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4457 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
4459 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4460 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4461 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4463 m = SCTP_BUF_NEXT(m);
4467 SCTP_INP_READ_UNLOCK(old_inp);
4468 /* Remove the sb-lock on the old socket */
4470 sbunlock(&old_so->so_rcv);
4471 /* Now we move them over to the new socket buffer */
4472 SCTP_INP_READ_LOCK(new_inp);
4473 TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4474 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4477 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4478 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4480 sctp_sballoc(stcb, &new_so->so_rcv, m);
4481 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4482 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4484 m = SCTP_BUF_NEXT(m);
4487 SCTP_INP_READ_UNLOCK(new_inp);
4491 sctp_wakeup_the_read_socket(struct sctp_inpcb *inp,
4492 struct sctp_tcb *stcb,
4494 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4499 if ((inp != NULL) && (inp->sctp_socket != NULL)) {
4500 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4503 so = SCTP_INP_SO(inp);
4506 atomic_add_int(&stcb->asoc.refcnt, 1);
4507 SCTP_TCB_UNLOCK(stcb);
4509 SCTP_SOCKET_LOCK(so, 1);
4511 SCTP_TCB_LOCK(stcb);
4512 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4514 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4515 SCTP_SOCKET_UNLOCK(so, 1);
4520 sctp_sorwakeup(inp, inp->sctp_socket);
4521 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4523 SCTP_SOCKET_UNLOCK(so, 1);
4530 sctp_add_to_readq(struct sctp_inpcb *inp,
4531 struct sctp_tcb *stcb,
4532 struct sctp_queued_to_read *control,
4535 int inp_read_lock_held,
4537 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4543 * Here we must place the control on the end of the socket read
4544 * queue AND increment sb_cc so that select will work properly on
4547 struct mbuf *m, *prev = NULL;
4552 panic("Gak, inp NULL on add_to_readq");
4556 if (inp_read_lock_held == 0)
4557 SCTP_INP_READ_LOCK(inp);
4558 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4559 sctp_free_remote_addr(control->whoFrom);
4560 if (control->data) {
4561 sctp_m_freem(control->data);
4562 control->data = NULL;
4564 sctp_free_a_readq(stcb, control);
4565 if (inp_read_lock_held == 0)
4566 SCTP_INP_READ_UNLOCK(inp);
4569 if (!(control->spec_flags & M_NOTIFICATION)) {
4570 atomic_add_int(&inp->total_recvs, 1);
4571 if (!control->do_not_ref_stcb) {
4572 atomic_add_int(&stcb->total_recvs, 1);
4576 control->held_length = 0;
4577 control->length = 0;
4579 if (SCTP_BUF_LEN(m) == 0) {
4580 /* Skip mbufs with NO length */
4583 control->data = sctp_m_free(m);
4586 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4587 m = SCTP_BUF_NEXT(prev);
4590 control->tail_mbuf = prev;
4595 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4596 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4598 sctp_sballoc(stcb, sb, m);
4599 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4600 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4602 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4603 m = SCTP_BUF_NEXT(m);
4606 control->tail_mbuf = prev;
4608 /* Everything got collapsed out?? */
4609 sctp_free_remote_addr(control->whoFrom);
4610 sctp_free_a_readq(stcb, control);
4611 if (inp_read_lock_held == 0)
4612 SCTP_INP_READ_UNLOCK(inp);
4616 control->end_added = 1;
4618 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4619 control->on_read_q = 1;
4620 if (inp_read_lock_held == 0)
4621 SCTP_INP_READ_UNLOCK(inp);
4622 if (inp && inp->sctp_socket) {
4623 sctp_wakeup_the_read_socket(inp, stcb, so_locked);
4627 /*************HOLD THIS COMMENT FOR PATCH FILE OF
4628 *************ALTERNATE ROUTING CODE
4631 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4632 *************ALTERNATE ROUTING CODE
4636 sctp_generate_cause(uint16_t code, char *info)
4639 struct sctp_gen_error_cause *cause;
4643 if ((code == 0) || (info == NULL)) {
4646 info_len = strlen(info);
4647 if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
4650 len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
4651 m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4653 SCTP_BUF_LEN(m) = len;
4654 cause = mtod(m, struct sctp_gen_error_cause *);
4655 cause->code = htons(code);
4656 cause->length = htons(len);
4657 memcpy(cause->info, info, info_len);
4663 sctp_generate_no_user_data_cause(uint32_t tsn)
4666 struct sctp_error_no_user_data *no_user_data_cause;
4669 len = (uint16_t)sizeof(struct sctp_error_no_user_data);
4670 m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4672 SCTP_BUF_LEN(m) = len;
4673 no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
4674 no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
4675 no_user_data_cause->cause.length = htons(len);
4676 no_user_data_cause->tsn = htonl(tsn);
4681 #ifdef SCTP_MBCNT_LOGGING
4683 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4684 struct sctp_tmit_chunk *tp1, int chk_cnt)
4686 if (tp1->data == NULL) {
4689 asoc->chunks_on_out_queue -= chk_cnt;
4690 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4691 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4692 asoc->total_output_queue_size,
4697 if (asoc->total_output_queue_size >= tp1->book_size) {
4698 atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4700 asoc->total_output_queue_size = 0;
4703 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4704 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4705 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4706 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4708 stcb->sctp_socket->so_snd.sb_cc = 0;
4717 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4718 uint8_t sent, int so_locked
4719 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4724 struct sctp_stream_out *strq;
4725 struct sctp_tmit_chunk *chk = NULL, *tp2;
4726 struct sctp_stream_queue_pending *sp;
4729 uint8_t foundeom = 0;
4732 int do_wakeup_routine = 0;
4734 sid = tp1->rec.data.sid;
4735 mid = tp1->rec.data.mid;
4736 if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
4737 stcb->asoc.abandoned_sent[0]++;
4738 stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4739 stcb->asoc.strmout[sid].abandoned_sent[0]++;
4740 #if defined(SCTP_DETAILED_STR_STATS)
4741 stcb->asoc.strmout[stream].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4744 stcb->asoc.abandoned_unsent[0]++;
4745 stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4746 stcb->asoc.strmout[sid].abandoned_unsent[0]++;
4747 #if defined(SCTP_DETAILED_STR_STATS)
4748 stcb->asoc.strmout[stream].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4752 ret_sz += tp1->book_size;
4753 if (tp1->data != NULL) {
4754 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4755 sctp_flight_size_decrease(tp1);
4756 sctp_total_flight_decrease(stcb, tp1);
4758 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4759 stcb->asoc.peers_rwnd += tp1->send_size;
4760 stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
4762 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
4764 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
4767 sctp_m_freem(tp1->data);
4770 do_wakeup_routine = 1;
4771 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4772 stcb->asoc.sent_queue_cnt_removeable--;
4775 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4776 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4777 SCTP_DATA_NOT_FRAG) {
4778 /* not frag'ed we ae done */
4781 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4782 /* end of frag, we are done */
4787 * Its a begin or middle piece, we must mark all of
4791 tp1 = TAILQ_NEXT(tp1, sctp_next);
4793 } while (tp1 && notdone);
4794 if (foundeom == 0) {
4796 * The multi-part message was scattered across the send and
4799 TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
4800 if ((tp1->rec.data.sid != sid) ||
4801 (!SCTP_MID_EQ(stcb->asoc.idata_supported, tp1->rec.data.mid, mid))) {
4805 * save to chk in case we have some on stream out
4806 * queue. If so and we have an un-transmitted one we
4807 * don't have to fudge the TSN.
4810 ret_sz += tp1->book_size;
4811 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4813 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
4815 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
4818 sctp_m_freem(tp1->data);
4821 /* No flight involved here book the size to 0 */
4823 if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4826 do_wakeup_routine = 1;
4827 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4828 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4830 * on to the sent queue so we can wait for it to be
4833 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4835 stcb->asoc.send_queue_cnt--;
4836 stcb->asoc.sent_queue_cnt++;
4839 if (foundeom == 0) {
4841 * Still no eom found. That means there is stuff left on the
4842 * stream out queue.. yuck.
4844 SCTP_TCB_SEND_LOCK(stcb);
4845 strq = &stcb->asoc.strmout[sid];
4846 sp = TAILQ_FIRST(&strq->outqueue);
4848 sp->discard_rest = 1;
4850 * We may need to put a chunk on the queue that
4851 * holds the TSN that would have been sent with the
4855 /* Yep, we have to */
4856 sctp_alloc_a_chunk(stcb, chk);
4859 * we are hosed. All we can do is
4860 * nothing.. which will cause an
4861 * abort if the peer is paying
4866 memset(chk, 0, sizeof(*chk));
4867 chk->rec.data.rcv_flags = 0;
4868 chk->sent = SCTP_FORWARD_TSN_SKIP;
4869 chk->asoc = &stcb->asoc;
4870 if (stcb->asoc.idata_supported == 0) {
4871 if (sp->sinfo_flags & SCTP_UNORDERED) {
4872 chk->rec.data.mid = 0;
4874 chk->rec.data.mid = strq->next_mid_ordered;
4877 if (sp->sinfo_flags & SCTP_UNORDERED) {
4878 chk->rec.data.mid = strq->next_mid_unordered;
4880 chk->rec.data.mid = strq->next_mid_ordered;
4883 chk->rec.data.sid = sp->sid;
4884 chk->rec.data.ppid = sp->ppid;
4885 chk->rec.data.context = sp->context;
4886 chk->flags = sp->act_flags;
4888 chk->rec.data.tsn = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
4889 strq->chunks_on_queues++;
4890 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
4891 stcb->asoc.sent_queue_cnt++;
4892 stcb->asoc.pr_sctp_cnt++;
4894 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4895 if (sp->sinfo_flags & SCTP_UNORDERED) {
4896 chk->rec.data.rcv_flags |= SCTP_DATA_UNORDERED;
4898 if (stcb->asoc.idata_supported == 0) {
4899 if ((sp->sinfo_flags & SCTP_UNORDERED) == 0) {
4900 strq->next_mid_ordered++;
4903 if (sp->sinfo_flags & SCTP_UNORDERED) {
4904 strq->next_mid_unordered++;
4906 strq->next_mid_ordered++;
4912 * Pull any data to free up the SB and allow
4913 * sender to "add more" while we will throw
4916 sctp_free_spbufspace(stcb, &stcb->asoc, sp);
4917 ret_sz += sp->length;
4918 do_wakeup_routine = 1;
4920 sctp_m_freem(sp->data);
4922 sp->tail_mbuf = NULL;
4926 SCTP_TCB_SEND_UNLOCK(stcb);
4928 if (do_wakeup_routine) {
4929 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4932 so = SCTP_INP_SO(stcb->sctp_ep);
4934 atomic_add_int(&stcb->asoc.refcnt, 1);
4935 SCTP_TCB_UNLOCK(stcb);
4936 SCTP_SOCKET_LOCK(so, 1);
4937 SCTP_TCB_LOCK(stcb);
4938 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4939 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4940 /* assoc was freed while we were unlocked */
4941 SCTP_SOCKET_UNLOCK(so, 1);
4946 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
4947 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4949 SCTP_SOCKET_UNLOCK(so, 1);
4957 * checks to see if the given address, sa, is one that is currently known by
4958 * the kernel note: can't distinguish the same address on multiple interfaces
4959 * and doesn't handle multiple addresses with different zone/scope id's note:
4960 * ifa_ifwithaddr() compares the entire sockaddr struct
4963 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
4966 struct sctp_laddr *laddr;
4968 if (holds_lock == 0) {
4969 SCTP_INP_RLOCK(inp);
4972 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4973 if (laddr->ifa == NULL)
4975 if (addr->sa_family != laddr->ifa->address.sa.sa_family)
4978 if (addr->sa_family == AF_INET) {
4979 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4980 laddr->ifa->address.sin.sin_addr.s_addr) {
4982 if (holds_lock == 0) {
4983 SCTP_INP_RUNLOCK(inp);
4985 return (laddr->ifa);
4991 if (addr->sa_family == AF_INET6) {
4992 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4993 &laddr->ifa->address.sin6)) {
4995 if (holds_lock == 0) {
4996 SCTP_INP_RUNLOCK(inp);
4998 return (laddr->ifa);
5004 if (holds_lock == 0) {
5005 SCTP_INP_RUNLOCK(inp);
5011 sctp_get_ifa_hash_val(struct sockaddr *addr)
5013 switch (addr->sa_family) {
5017 struct sockaddr_in *sin;
5019 sin = (struct sockaddr_in *)addr;
5020 return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5026 struct sockaddr_in6 *sin6;
5027 uint32_t hash_of_addr;
5029 sin6 = (struct sockaddr_in6 *)addr;
5030 hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
5031 sin6->sin6_addr.s6_addr32[1] +
5032 sin6->sin6_addr.s6_addr32[2] +
5033 sin6->sin6_addr.s6_addr32[3]);
5034 hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
5035 return (hash_of_addr);
5045 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
5047 struct sctp_ifa *sctp_ifap;
5048 struct sctp_vrf *vrf;
5049 struct sctp_ifalist *hash_head;
5050 uint32_t hash_of_addr;
5052 if (holds_lock == 0)
5053 SCTP_IPI_ADDR_RLOCK();
5055 vrf = sctp_find_vrf(vrf_id);
5057 if (holds_lock == 0)
5058 SCTP_IPI_ADDR_RUNLOCK();
5062 hash_of_addr = sctp_get_ifa_hash_val(addr);
5064 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5065 if (hash_head == NULL) {
5066 SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5067 hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
5068 (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
5069 sctp_print_address(addr);
5070 SCTP_PRINTF("No such bucket for address\n");
5071 if (holds_lock == 0)
5072 SCTP_IPI_ADDR_RUNLOCK();
5076 LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5077 if (addr->sa_family != sctp_ifap->address.sa.sa_family)
5080 if (addr->sa_family == AF_INET) {
5081 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5082 sctp_ifap->address.sin.sin_addr.s_addr) {
5084 if (holds_lock == 0)
5085 SCTP_IPI_ADDR_RUNLOCK();
5092 if (addr->sa_family == AF_INET6) {
5093 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5094 &sctp_ifap->address.sin6)) {
5096 if (holds_lock == 0)
5097 SCTP_IPI_ADDR_RUNLOCK();
5104 if (holds_lock == 0)
5105 SCTP_IPI_ADDR_RUNLOCK();
5110 sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
5113 /* User pulled some data, do we need a rwnd update? */
5116 struct socket *so = NULL;
5121 atomic_add_int(&stcb->asoc.refcnt, 1);
5123 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5124 (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | SCTP_STATE_SHUTDOWN_RECEIVED))) {
5125 /* Pre-check If we are freeing no update */
5128 SCTP_INP_INCR_REF(stcb->sctp_ep);
5129 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5130 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5133 so = stcb->sctp_socket;
5137 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5138 /* Have you have freed enough to look */
5140 /* Yep, its worth a look and the lock overhead */
5142 /* Figure out what the rwnd would be */
5143 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5144 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5145 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5149 if (dif >= rwnd_req) {
5151 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5154 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5156 * One last check before we allow the guy possibly
5157 * to get in. There is a race, where the guy has not
5158 * reached the gate. In that case
5162 SCTP_TCB_LOCK(stcb);
5163 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5164 /* No reports here */
5165 SCTP_TCB_UNLOCK(stcb);
5168 SCTP_STAT_INCR(sctps_wu_sacks_sent);
5169 sctp_send_sack(stcb, SCTP_SO_LOCKED);
5171 sctp_chunk_output(stcb->sctp_ep, stcb,
5172 SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5173 /* make sure no timer is running */
5174 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
5175 SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5176 SCTP_TCB_UNLOCK(stcb);
5178 /* Update how much we have pending */
5179 stcb->freed_by_sorcv_sincelast = dif;
5182 if (so && r_unlocked && hold_rlock) {
5183 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5186 SCTP_INP_DECR_REF(stcb->sctp_ep);
5188 atomic_add_int(&stcb->asoc.refcnt, -1);
5193 sctp_sorecvmsg(struct socket *so,
5196 struct sockaddr *from,
5199 struct sctp_sndrcvinfo *sinfo,
5203 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5204 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5205 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5206 * On the way out we may send out any combination of:
5207 * MSG_NOTIFICATION MSG_EOR
5210 struct sctp_inpcb *inp = NULL;
5214 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
5215 struct mbuf *m = NULL;
5216 struct sctp_tcb *stcb = NULL;
5217 int wakeup_read_socket = 0;
5218 int freecnt_applied = 0;
5219 int out_flags = 0, in_flags = 0;
5220 int block_allowed = 1;
5221 uint32_t freed_so_far = 0;
5222 ssize_t copied_so_far = 0;
5223 int in_eeor_mode = 0;
5224 int no_rcv_needed = 0;
5225 uint32_t rwnd_req = 0;
5226 int hold_sblock = 0;
5229 uint32_t held_length = 0;
5230 int sockbuf_lock = 0;
5233 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5238 in_flags = *msg_flags;
5239 if (in_flags & MSG_PEEK)
5240 SCTP_STAT_INCR(sctps_read_peeks);
5244 slen = uio->uio_resid;
5246 /* Pull in and set up our int flags */
5247 if (in_flags & MSG_OOB) {
5248 /* Out of band's NOT supported */
5249 return (EOPNOTSUPP);
5251 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5252 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5255 if ((in_flags & (MSG_DONTWAIT
5258 SCTP_SO_IS_NBIO(so)) {
5261 /* setup the endpoint */
5262 inp = (struct sctp_inpcb *)so->so_pcb;
5264 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5267 rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5268 /* Must be at least a MTU's worth */
5269 if (rwnd_req < SCTP_MIN_RWND)
5270 rwnd_req = SCTP_MIN_RWND;
5271 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5272 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5273 sctp_misc_ints(SCTP_SORECV_ENTER,
5274 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
5276 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5277 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5278 rwnd_req, block_allowed, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
5282 error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5284 goto release_unlocked;
5291 if (hold_sblock == 0) {
5292 SOCKBUF_LOCK(&so->so_rcv);
5295 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5296 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5299 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5301 error = so->so_error;
5302 if ((in_flags & MSG_PEEK) == 0)
5306 if (so->so_rcv.sb_cc == 0) {
5313 if (so->so_rcv.sb_cc <= held_length) {
5315 error = so->so_error;
5316 if ((in_flags & MSG_PEEK) == 0) {
5321 if ((so->so_rcv.sb_cc == 0) &&
5322 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5323 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5324 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5326 * For active open side clear flags for
5327 * re-use passive open is blocked by
5330 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5332 * You were aborted, passive side
5335 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5338 so->so_state &= ~(SS_ISCONNECTING |
5339 SS_ISDISCONNECTING |
5343 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5344 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5351 if (block_allowed) {
5352 error = sbwait(&so->so_rcv);
5357 goto restart_nosblocks;
5359 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5360 error = EWOULDBLOCK;
5364 if (hold_sblock == 1) {
5365 SOCKBUF_UNLOCK(&so->so_rcv);
5368 /* we possibly have data we can read */
5369 /* sa_ignore FREED_MEMORY */
5370 control = TAILQ_FIRST(&inp->read_queue);
5371 if (control == NULL) {
5373 * This could be happening since the appender did the
5374 * increment but as not yet did the tailq insert onto the
5377 if (hold_rlock == 0) {
5378 SCTP_INP_READ_LOCK(inp);
5380 control = TAILQ_FIRST(&inp->read_queue);
5381 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5383 panic("Huh, its non zero and nothing on control?");
5385 so->so_rcv.sb_cc = 0;
5387 SCTP_INP_READ_UNLOCK(inp);
5392 if ((control->length == 0) &&
5393 (control->do_not_ref_stcb)) {
5395 * Clean up code for freeing assoc that left behind a
5396 * pdapi.. maybe a peer in EEOR that just closed after
5397 * sending and never indicated a EOR.
5399 if (hold_rlock == 0) {
5401 SCTP_INP_READ_LOCK(inp);
5403 control->held_length = 0;
5404 if (control->data) {
5405 /* Hmm there is data here .. fix */
5409 m_tmp = control->data;
5411 cnt += SCTP_BUF_LEN(m_tmp);
5412 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5413 control->tail_mbuf = m_tmp;
5414 control->end_added = 1;
5416 m_tmp = SCTP_BUF_NEXT(m_tmp);
5418 control->length = cnt;
5421 TAILQ_REMOVE(&inp->read_queue, control, next);
5422 /* Add back any hiddend data */
5423 sctp_free_remote_addr(control->whoFrom);
5424 sctp_free_a_readq(stcb, control);
5428 SCTP_INP_READ_UNLOCK(inp);
5432 if ((control->length == 0) &&
5433 (control->end_added == 1)) {
5435 * Do we also need to check for (control->pdapi_aborted ==
5438 if (hold_rlock == 0) {
5440 SCTP_INP_READ_LOCK(inp);
5442 TAILQ_REMOVE(&inp->read_queue, control, next);
5443 if (control->data) {
5445 panic("control->data not null but control->length == 0");
5447 SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5448 sctp_m_freem(control->data);
5449 control->data = NULL;
5452 if (control->aux_data) {
5453 sctp_m_free(control->aux_data);
5454 control->aux_data = NULL;
5457 if (control->on_strm_q) {
5458 panic("About to free ctl:%p so:%p and its in %d",
5459 control, so, control->on_strm_q);
5462 sctp_free_remote_addr(control->whoFrom);
5463 sctp_free_a_readq(stcb, control);
5466 SCTP_INP_READ_UNLOCK(inp);
5470 if (control->length == 0) {
5471 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5473 /* find a more suitable one then this */
5474 ctl = TAILQ_NEXT(control, next);
5476 if ((ctl->stcb != control->stcb) && (ctl->length) &&
5478 (ctl->spec_flags & M_NOTIFICATION) ||
5479 ((ctl->do_not_ref_stcb == 0) &&
5480 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
5483 * If we have a different TCB next, and there is data
5484 * present. If we have already taken some (pdapi), OR we can
5485 * ref the tcb and no delivery as started on this stream, we
5486 * take it. Note we allow a notification on a different
5487 * assoc to be delivered..
5491 } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
5493 ((ctl->some_taken) ||
5494 ((ctl->do_not_ref_stcb == 0) &&
5495 ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5496 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
5498 * If we have the same tcb, and there is data present, and we
5499 * have the strm interleave feature present. Then if we have
5500 * taken some (pdapi) or we can refer to tht tcb AND we have
5501 * not started a delivery for this stream, we can take it.
5502 * Note we do NOT allow a notificaiton on the same assoc to
5508 ctl = TAILQ_NEXT(ctl, next);
5512 * if we reach here, not suitable replacement is available
5513 * <or> fragment interleave is NOT on. So stuff the sb_cc
5514 * into the our held count, and its time to sleep again.
5516 held_length = so->so_rcv.sb_cc;
5517 control->held_length = so->so_rcv.sb_cc;
5520 /* Clear the held length since there is something to read */
5521 control->held_length = 0;
5524 * If we reach here, control has a some data for us to read off.
5525 * Note that stcb COULD be NULL.
5527 if (hold_rlock == 0) {
5529 SCTP_INP_READ_LOCK(inp);
5531 control->some_taken++;
5532 stcb = control->stcb;
5534 if ((control->do_not_ref_stcb == 0) &&
5535 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5536 if (freecnt_applied == 0)
5538 } else if (control->do_not_ref_stcb == 0) {
5539 /* you can't free it on me please */
5541 * The lock on the socket buffer protects us so the
5542 * free code will stop. But since we used the
5543 * socketbuf lock and the sender uses the tcb_lock
5544 * to increment, we need to use the atomic add to
5547 if (freecnt_applied) {
5549 panic("refcnt already incremented");
5551 SCTP_PRINTF("refcnt already incremented?\n");
5554 atomic_add_int(&stcb->asoc.refcnt, 1);
5555 freecnt_applied = 1;
5558 * Setup to remember how much we have not yet told
5559 * the peer our rwnd has opened up. Note we grab the
5560 * value from the tcb from last time. Note too that
5561 * sack sending clears this when a sack is sent,
5562 * which is fine. Once we hit the rwnd_req, we then
5563 * will go to the sctp_user_rcvd() that will not
5564 * lock until it KNOWs it MUST send a WUP-SACK.
5566 freed_so_far = (uint32_t)stcb->freed_by_sorcv_sincelast;
5567 stcb->freed_by_sorcv_sincelast = 0;
5571 ((control->spec_flags & M_NOTIFICATION) == 0) &&
5572 control->do_not_ref_stcb == 0) {
5573 stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5576 /* First lets get off the sinfo and sockaddr info */
5577 if ((sinfo != NULL) && (filling_sinfo != 0)) {
5578 sinfo->sinfo_stream = control->sinfo_stream;
5579 sinfo->sinfo_ssn = (uint16_t)control->mid;
5580 sinfo->sinfo_flags = control->sinfo_flags;
5581 sinfo->sinfo_ppid = control->sinfo_ppid;
5582 sinfo->sinfo_context = control->sinfo_context;
5583 sinfo->sinfo_timetolive = control->sinfo_timetolive;
5584 sinfo->sinfo_tsn = control->sinfo_tsn;
5585 sinfo->sinfo_cumtsn = control->sinfo_cumtsn;
5586 sinfo->sinfo_assoc_id = control->sinfo_assoc_id;
5587 nxt = TAILQ_NEXT(control, next);
5588 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
5589 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
5590 struct sctp_extrcvinfo *s_extra;
5592 s_extra = (struct sctp_extrcvinfo *)sinfo;
5595 s_extra->serinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5596 if (nxt->sinfo_flags & SCTP_UNORDERED) {
5597 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5599 if (nxt->spec_flags & M_NOTIFICATION) {
5600 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5602 s_extra->serinfo_next_aid = nxt->sinfo_assoc_id;
5603 s_extra->serinfo_next_length = nxt->length;
5604 s_extra->serinfo_next_ppid = nxt->sinfo_ppid;
5605 s_extra->serinfo_next_stream = nxt->sinfo_stream;
5606 if (nxt->tail_mbuf != NULL) {
5607 if (nxt->end_added) {
5608 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5613 * we explicitly 0 this, since the memcpy
5614 * got some other things beyond the older
5615 * sinfo_ that is on the control's structure
5619 s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
5620 s_extra->serinfo_next_aid = 0;
5621 s_extra->serinfo_next_length = 0;
5622 s_extra->serinfo_next_ppid = 0;
5623 s_extra->serinfo_next_stream = 0;
5627 * update off the real current cum-ack, if we have an stcb.
5629 if ((control->do_not_ref_stcb == 0) && stcb)
5630 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5632 * mask off the high bits, we keep the actual chunk bits in
5635 sinfo->sinfo_flags &= 0x00ff;
5636 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
5637 sinfo->sinfo_flags |= SCTP_UNORDERED;
5640 #ifdef SCTP_ASOCLOG_OF_TSNS
5642 int index, newindex;
5643 struct sctp_pcbtsn_rlog *entry;
5646 index = inp->readlog_index;
5647 newindex = index + 1;
5648 if (newindex >= SCTP_READ_LOG_SIZE) {
5651 } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
5652 entry = &inp->readlog[index];
5653 entry->vtag = control->sinfo_assoc_id;
5654 entry->strm = control->sinfo_stream;
5655 entry->seq = (uint16_t)control->mid;
5656 entry->sz = control->length;
5657 entry->flgs = control->sinfo_flags;
5660 if ((fromlen > 0) && (from != NULL)) {
5661 union sctp_sockstore store;
5664 switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5667 len = sizeof(struct sockaddr_in6);
5668 store.sin6 = control->whoFrom->ro._l_addr.sin6;
5669 store.sin6.sin6_port = control->port_from;
5675 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
5676 len = sizeof(struct sockaddr_in6);
5677 in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
5679 store.sin6.sin6_port = control->port_from;
5681 len = sizeof(struct sockaddr_in);
5682 store.sin = control->whoFrom->ro._l_addr.sin;
5683 store.sin.sin_port = control->port_from;
5686 len = sizeof(struct sockaddr_in);
5687 store.sin = control->whoFrom->ro._l_addr.sin;
5688 store.sin.sin_port = control->port_from;
5696 memcpy(from, &store, min((size_t)fromlen, len));
5699 struct sockaddr_in6 lsa6, *from6;
5701 from6 = (struct sockaddr_in6 *)from;
5702 sctp_recover_scope_mac(from6, (&lsa6));
5707 SCTP_INP_READ_UNLOCK(inp);
5711 SOCKBUF_UNLOCK(&so->so_rcv);
5714 /* now copy out what data we can */
5716 /* copy out each mbuf in the chain up to length */
5720 /* Move out all we can */
5721 cp_len = uio->uio_resid;
5722 my_len = SCTP_BUF_LEN(m);
5723 if (cp_len > my_len) {
5724 /* not enough in this buf */
5728 SCTP_INP_READ_UNLOCK(inp);
5732 error = uiomove(mtod(m, char *), (int)cp_len, uio);
5734 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5738 if ((control->do_not_ref_stcb == 0) && stcb &&
5739 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5743 /* error we are out of here */
5746 SCTP_INP_READ_LOCK(inp);
5748 if (cp_len == SCTP_BUF_LEN(m)) {
5749 if ((SCTP_BUF_NEXT(m) == NULL) &&
5750 (control->end_added)) {
5751 out_flags |= MSG_EOR;
5752 if ((control->do_not_ref_stcb == 0) &&
5753 (control->stcb != NULL) &&
5754 ((control->spec_flags & M_NOTIFICATION) == 0))
5755 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5757 if (control->spec_flags & M_NOTIFICATION) {
5758 out_flags |= MSG_NOTIFICATION;
5760 /* we ate up the mbuf */
5761 if (in_flags & MSG_PEEK) {
5763 m = SCTP_BUF_NEXT(m);
5764 copied_so_far += cp_len;
5766 /* dispose of the mbuf */
5767 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5768 sctp_sblog(&so->so_rcv,
5769 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5771 sctp_sbfree(control, stcb, &so->so_rcv, m);
5772 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5773 sctp_sblog(&so->so_rcv,
5774 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5776 copied_so_far += cp_len;
5777 freed_so_far += (uint32_t)cp_len;
5778 freed_so_far += MSIZE;
5779 atomic_subtract_int(&control->length, cp_len);
5780 control->data = sctp_m_free(m);
5783 * been through it all, must hold sb
5784 * lock ok to null tail
5786 if (control->data == NULL) {
5788 if ((control->end_added == 0) ||
5789 (TAILQ_NEXT(control, next) == NULL)) {
5797 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5798 panic("Hmm we don't own the lock?");
5802 control->tail_mbuf = NULL;
5804 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5805 panic("end_added, nothing left and no MSG_EOR");
5811 /* Do we need to trim the mbuf? */
5812 if (control->spec_flags & M_NOTIFICATION) {
5813 out_flags |= MSG_NOTIFICATION;
5815 if ((in_flags & MSG_PEEK) == 0) {
5816 SCTP_BUF_RESV_UF(m, cp_len);
5817 SCTP_BUF_LEN(m) -= (int)cp_len;
5818 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5819 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, (int)cp_len);
5821 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5822 if ((control->do_not_ref_stcb == 0) &&
5824 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5826 copied_so_far += cp_len;
5827 freed_so_far += (uint32_t)cp_len;
5828 freed_so_far += MSIZE;
5829 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5830 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5831 SCTP_LOG_SBRESULT, 0);
5833 atomic_subtract_int(&control->length, cp_len);
5835 copied_so_far += cp_len;
5838 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5841 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5842 (control->do_not_ref_stcb == 0) &&
5843 (freed_so_far >= rwnd_req)) {
5844 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5846 } /* end while(m) */
5848 * At this point we have looked at it all and we either have
5849 * a MSG_EOR/or read all the user wants... <OR>
5850 * control->length == 0.
5852 if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5853 /* we are done with this control */
5854 if (control->length == 0) {
5855 if (control->data) {
5857 panic("control->data not null at read eor?");
5859 SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5860 sctp_m_freem(control->data);
5861 control->data = NULL;
5865 if (hold_rlock == 0) {
5866 SCTP_INP_READ_LOCK(inp);
5869 TAILQ_REMOVE(&inp->read_queue, control, next);
5870 /* Add back any hiddend data */
5871 if (control->held_length) {
5873 control->held_length = 0;
5874 wakeup_read_socket = 1;
5876 if (control->aux_data) {
5877 sctp_m_free(control->aux_data);
5878 control->aux_data = NULL;
5880 no_rcv_needed = control->do_not_ref_stcb;
5881 sctp_free_remote_addr(control->whoFrom);
5882 control->data = NULL;
5884 if (control->on_strm_q) {
5885 panic("About to free ctl:%p so:%p and its in %d",
5886 control, so, control->on_strm_q);
5889 sctp_free_a_readq(stcb, control);
5891 if ((freed_so_far >= rwnd_req) &&
5892 (no_rcv_needed == 0))
5893 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5897 * The user did not read all of this
5898 * message, turn off the returned MSG_EOR
5899 * since we are leaving more behind on the
5903 if (control->end_added &&
5904 (control->data == NULL) &&
5905 (control->tail_mbuf == NULL)) {
5906 panic("Gak, control->length is corrupt?");
5909 no_rcv_needed = control->do_not_ref_stcb;
5910 out_flags &= ~MSG_EOR;
5913 if (out_flags & MSG_EOR) {
5916 if ((uio->uio_resid == 0) ||
5918 (copied_so_far >= (uint32_t)max(so->so_rcv.sb_lowat, 1)))) {
5922 * If I hit here the receiver wants more and this message is
5923 * NOT done (pd-api). So two questions. Can we block? if not
5924 * we are done. Did the user NOT set MSG_WAITALL?
5926 if (block_allowed == 0) {
5930 * We need to wait for more data a few things: - We don't
5931 * sbunlock() so we don't get someone else reading. - We
5932 * must be sure to account for the case where what is added
5933 * is NOT to our control when we wakeup.
5937 * Do we need to tell the transport a rwnd update might be
5938 * needed before we go to sleep?
5940 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5941 ((freed_so_far >= rwnd_req) &&
5942 (control->do_not_ref_stcb == 0) &&
5943 (no_rcv_needed == 0))) {
5944 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5947 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5951 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5954 if (hold_rlock == 1) {
5955 SCTP_INP_READ_UNLOCK(inp);
5958 if (hold_sblock == 0) {
5959 SOCKBUF_LOCK(&so->so_rcv);
5962 if ((copied_so_far) && (control->length == 0) &&
5963 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
5966 if (so->so_rcv.sb_cc <= control->held_length) {
5967 error = sbwait(&so->so_rcv);
5971 control->held_length = 0;
5974 SOCKBUF_UNLOCK(&so->so_rcv);
5977 if (control->length == 0) {
5978 /* still nothing here */
5979 if (control->end_added == 1) {
5980 /* he aborted, or is done i.e.did a shutdown */
5981 out_flags |= MSG_EOR;
5982 if (control->pdapi_aborted) {
5983 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5984 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5986 out_flags |= MSG_TRUNC;
5988 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5989 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5991 goto done_with_control;
5993 if (so->so_rcv.sb_cc > held_length) {
5994 control->held_length = so->so_rcv.sb_cc;
5997 goto wait_some_more;
5998 } else if (control->data == NULL) {
6000 * we must re-sync since data is probably being
6003 SCTP_INP_READ_LOCK(inp);
6004 if ((control->length > 0) && (control->data == NULL)) {
6006 * big trouble.. we have the lock and its
6010 panic("Impossible data==NULL length !=0");
6012 out_flags |= MSG_EOR;
6013 out_flags |= MSG_TRUNC;
6014 control->length = 0;
6015 SCTP_INP_READ_UNLOCK(inp);
6016 goto done_with_control;
6018 SCTP_INP_READ_UNLOCK(inp);
6019 /* We will fall around to get more data */
6024 * Give caller back the mbuf chain,
6025 * store in uio_resid the length
6027 wakeup_read_socket = 0;
6028 if ((control->end_added == 0) ||
6029 (TAILQ_NEXT(control, next) == NULL)) {
6030 /* Need to get rlock */
6031 if (hold_rlock == 0) {
6032 SCTP_INP_READ_LOCK(inp);
6036 if (control->end_added) {
6037 out_flags |= MSG_EOR;
6038 if ((control->do_not_ref_stcb == 0) &&
6039 (control->stcb != NULL) &&
6040 ((control->spec_flags & M_NOTIFICATION) == 0))
6041 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6043 if (control->spec_flags & M_NOTIFICATION) {
6044 out_flags |= MSG_NOTIFICATION;
6046 uio->uio_resid = control->length;
6047 *mp = control->data;
6050 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6051 sctp_sblog(&so->so_rcv,
6052 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
6054 sctp_sbfree(control, stcb, &so->so_rcv, m);
6055 freed_so_far += (uint32_t)SCTP_BUF_LEN(m);
6056 freed_so_far += MSIZE;
6057 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6058 sctp_sblog(&so->so_rcv,
6059 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
6061 m = SCTP_BUF_NEXT(m);
6063 control->data = control->tail_mbuf = NULL;
6064 control->length = 0;
6065 if (out_flags & MSG_EOR) {
6066 /* Done with this control */
6067 goto done_with_control;
6071 if (hold_rlock == 1) {
6072 SCTP_INP_READ_UNLOCK(inp);
6075 if (hold_sblock == 1) {
6076 SOCKBUF_UNLOCK(&so->so_rcv);
6080 sbunlock(&so->so_rcv);
6085 SOCKBUF_UNLOCK(&so->so_rcv);
6088 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6089 if ((freed_so_far >= rwnd_req) &&
6090 (control && (control->do_not_ref_stcb == 0)) &&
6091 (no_rcv_needed == 0))
6092 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6096 *msg_flags = out_flags;
6098 if (((out_flags & MSG_EOR) == 0) &&
6099 ((in_flags & MSG_PEEK) == 0) &&
6101 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6102 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
6103 struct sctp_extrcvinfo *s_extra;
6105 s_extra = (struct sctp_extrcvinfo *)sinfo;
6106 s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
6108 if (hold_rlock == 1) {
6109 SCTP_INP_READ_UNLOCK(inp);
6112 SOCKBUF_UNLOCK(&so->so_rcv);
6115 sbunlock(&so->so_rcv);
6118 if (freecnt_applied) {
6120 * The lock on the socket buffer protects us so the free
6121 * code will stop. But since we used the socketbuf lock and
6122 * the sender uses the tcb_lock to increment, we need to use
6123 * the atomic add to the refcnt.
6127 panic("stcb for refcnt has gone NULL?");
6133 /* Save the value back for next time */
6134 stcb->freed_by_sorcv_sincelast = freed_so_far;
6135 atomic_add_int(&stcb->asoc.refcnt, -1);
6137 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6139 sctp_misc_ints(SCTP_SORECV_DONE,
6141 (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6145 sctp_misc_ints(SCTP_SORECV_DONE,
6147 (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6153 if (wakeup_read_socket) {
6154 sctp_sorwakeup(inp, so);
6160 #ifdef SCTP_MBUF_LOGGING
6162 sctp_m_free(struct mbuf *m)
6164 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6165 sctp_log_mb(m, SCTP_MBUF_IFREE);
6171 sctp_m_freem(struct mbuf *mb)
6174 mb = sctp_m_free(mb);
6180 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
6183 * Given a local address. For all associations that holds the
6184 * address, request a peer-set-primary.
6186 struct sctp_ifa *ifa;
6187 struct sctp_laddr *wi;
6189 ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
6191 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
6192 return (EADDRNOTAVAIL);
6195 * Now that we have the ifa we must awaken the iterator with this
6198 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6200 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
6203 /* Now incr the count and int wi structure */
6204 SCTP_INCR_LADDR_COUNT();
6205 memset(wi, 0, sizeof(*wi));
6206 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
6208 wi->action = SCTP_SET_PRIM_ADDR;
6209 atomic_add_int(&ifa->refcount, 1);
6211 /* Now add it to the work queue */
6212 SCTP_WQ_ADDR_LOCK();
6214 * Should this really be a tailq? As it is we will process the
6217 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6218 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
6219 (struct sctp_inpcb *)NULL,
6220 (struct sctp_tcb *)NULL,
6221 (struct sctp_nets *)NULL);
6222 SCTP_WQ_ADDR_UNLOCK();
6228 sctp_soreceive(struct socket *so,
6229 struct sockaddr **psa,
6232 struct mbuf **controlp,
6236 uint8_t sockbuf[256];
6237 struct sockaddr *from;
6238 struct sctp_extrcvinfo sinfo;
6239 int filling_sinfo = 1;
6241 struct sctp_inpcb *inp;
6243 inp = (struct sctp_inpcb *)so->so_pcb;
6244 /* pickup the assoc we are reading from */
6246 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6249 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6250 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6251 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6252 (controlp == NULL)) {
6253 /* user does not want the sndrcv ctl */
6257 from = (struct sockaddr *)sockbuf;
6258 fromlen = sizeof(sockbuf);
6265 if (filling_sinfo) {
6266 memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6268 if (flagsp != NULL) {
6273 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, &flags,
6274 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6275 if (flagsp != NULL) {
6278 if (controlp != NULL) {
6279 /* copy back the sinfo in a CMSG format */
6280 if (filling_sinfo && ((flags & MSG_NOTIFICATION) == 0)) {
6281 *controlp = sctp_build_ctl_nchunk(inp,
6282 (struct sctp_sndrcvinfo *)&sinfo);
6288 /* copy back the address info */
6289 if (from && from->sa_len) {
6290 *psa = sodupsockaddr(from, M_NOWAIT);
6303 sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6304 int totaddr, int *error)
6308 struct sctp_inpcb *inp;
6309 struct sockaddr *sa;
6312 struct sockaddr_in *sin;
6315 struct sockaddr_in6 *sin6;
6319 inp = stcb->sctp_ep;
6321 for (i = 0; i < totaddr; i++) {
6322 switch (sa->sa_family) {
6325 incr = sizeof(struct sockaddr_in);
6326 sin = (struct sockaddr_in *)sa;
6327 if ((sin->sin_addr.s_addr == INADDR_ANY) ||
6328 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
6329 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
6330 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6331 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6332 SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
6336 if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
6337 SCTP_DONOT_SETSCOPE,
6338 SCTP_ADDR_IS_CONFIRMED)) {
6339 /* assoc gone no un-lock */
6340 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6341 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6342 SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
6351 incr = sizeof(struct sockaddr_in6);
6352 sin6 = (struct sockaddr_in6 *)sa;
6353 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
6354 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
6355 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6356 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6357 SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
6361 if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
6362 SCTP_DONOT_SETSCOPE,
6363 SCTP_ADDR_IS_CONFIRMED)) {
6364 /* assoc gone no un-lock */
6365 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6366 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6367 SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
6377 sa = (struct sockaddr *)((caddr_t)sa + incr);
6384 sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6385 unsigned int totaddr,
6386 unsigned int *num_v4, unsigned int *num_v6,
6389 struct sockaddr *sa;
6390 struct sctp_tcb *stcb;
6391 unsigned int incr, at, i;
6395 *num_v6 = *num_v4 = 0;
6396 /* account and validate addresses */
6400 for (i = 0; i < totaddr; i++) {
6401 if (at + sizeof(struct sockaddr) > limit) {
6404 switch (sa->sa_family) {
6407 incr = (unsigned int)sizeof(struct sockaddr_in);
6408 if (sa->sa_len != incr) {
6417 struct sockaddr_in6 *sin6;
6419 sin6 = (struct sockaddr_in6 *)sa;
6420 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6421 /* Must be non-mapped for connectx */
6424 incr = (unsigned int)sizeof(struct sockaddr_in6);
6425 if (sa->sa_len != incr) {
6435 if ((at + incr) > limit) {
6438 SCTP_INP_INCR_REF(inp);
6439 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
6441 SCTP_TCB_UNLOCK(stcb);
6444 SCTP_INP_DECR_REF(inp);
6447 sa = (struct sockaddr *)((caddr_t)sa + incr);
6453 * sctp_bindx(ADD) for one address.
6454 * assumes all arguments are valid/checked by caller.
6457 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
6458 struct sockaddr *sa, sctp_assoc_t assoc_id,
6459 uint32_t vrf_id, int *error, void *p)
6461 struct sockaddr *addr_touse;
6462 #if defined(INET) && defined(INET6)
6463 struct sockaddr_in sin;
6466 /* see if we're bound all already! */
6467 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6468 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6474 if (sa->sa_family == AF_INET6) {
6476 struct sockaddr_in6 *sin6;
6479 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6480 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6484 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6485 /* can only bind v6 on PF_INET6 sockets */
6486 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6491 sin6 = (struct sockaddr_in6 *)addr_touse;
6492 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6493 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6494 SCTP_IPV6_V6ONLY(inp)) {
6495 /* can't bind v4-mapped on PF_INET sockets */
6496 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6500 in6_sin6_2_sin(&sin, sin6);
6501 addr_touse = (struct sockaddr *)&sin;
6507 if (sa->sa_family == AF_INET) {
6508 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6509 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6513 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6514 SCTP_IPV6_V6ONLY(inp)) {
6515 /* can't bind v4 on PF_INET sockets */
6516 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6522 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
6524 /* Can't get proc for Net/Open BSD */
6525 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6529 *error = sctp_inpcb_bind(so, addr_touse, NULL, p);
6533 * No locks required here since bind and mgmt_ep_sa all do their own
6534 * locking. If we do something for the FIX: below we may need to
6535 * lock in that case.
6537 if (assoc_id == 0) {
6538 /* add the address */
6539 struct sctp_inpcb *lep;
6540 struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
6542 /* validate the incoming port */
6543 if ((lsin->sin_port != 0) &&
6544 (lsin->sin_port != inp->sctp_lport)) {
6545 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6549 /* user specified 0 port, set it to existing port */
6550 lsin->sin_port = inp->sctp_lport;
6553 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
6556 * We must decrement the refcount since we have the
6557 * ep already and are binding. No remove going on
6560 SCTP_INP_DECR_REF(lep);
6563 /* already bound to it.. ok */
6565 } else if (lep == NULL) {
6566 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
6567 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6568 SCTP_ADD_IP_ADDRESS,
6571 *error = EADDRINUSE;
6577 * FIX: decide whether we allow assoc based bindx
6583 * sctp_bindx(DELETE) for one address.
6584 * assumes all arguments are valid/checked by caller.
6587 sctp_bindx_delete_address(struct sctp_inpcb *inp,
6588 struct sockaddr *sa, sctp_assoc_t assoc_id,
6589 uint32_t vrf_id, int *error)
6591 struct sockaddr *addr_touse;
6592 #if defined(INET) && defined(INET6)
6593 struct sockaddr_in sin;
6596 /* see if we're bound all already! */
6597 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6598 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6604 if (sa->sa_family == AF_INET6) {
6606 struct sockaddr_in6 *sin6;
6609 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6610 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6614 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6615 /* can only bind v6 on PF_INET6 sockets */
6616 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6621 sin6 = (struct sockaddr_in6 *)addr_touse;
6622 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6623 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6624 SCTP_IPV6_V6ONLY(inp)) {
6625 /* can't bind mapped-v4 on PF_INET sockets */
6626 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6630 in6_sin6_2_sin(&sin, sin6);
6631 addr_touse = (struct sockaddr *)&sin;
6637 if (sa->sa_family == AF_INET) {
6638 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6639 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6643 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6644 SCTP_IPV6_V6ONLY(inp)) {
6645 /* can't bind v4 on PF_INET sockets */
6646 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6653 * No lock required mgmt_ep_sa does its own locking. If the FIX:
6654 * below is ever changed we may need to lock before calling
6655 * association level binding.
6657 if (assoc_id == 0) {
6658 /* delete the address */
6659 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6660 SCTP_DEL_IP_ADDRESS,
6664 * FIX: decide whether we allow assoc based bindx
6670 * returns the valid local address count for an assoc, taking into account
6674 sctp_local_addr_count(struct sctp_tcb *stcb)
6678 int ipv4_local_scope, ipv4_addr_legal;
6681 int local_scope, site_scope, ipv6_addr_legal;
6683 struct sctp_vrf *vrf;
6684 struct sctp_ifn *sctp_ifn;
6685 struct sctp_ifa *sctp_ifa;
6688 /* Turn on all the appropriate scopes */
6689 loopback_scope = stcb->asoc.scope.loopback_scope;
6691 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
6692 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
6695 local_scope = stcb->asoc.scope.local_scope;
6696 site_scope = stcb->asoc.scope.site_scope;
6697 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
6699 SCTP_IPI_ADDR_RLOCK();
6700 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
6702 /* no vrf, no addresses */
6703 SCTP_IPI_ADDR_RUNLOCK();
6707 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6709 * bound all case: go through all ifns on the vrf
6711 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
6712 if ((loopback_scope == 0) &&
6713 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
6716 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
6717 if (sctp_is_addr_restricted(stcb, sctp_ifa))
6719 switch (sctp_ifa->address.sa.sa_family) {
6722 if (ipv4_addr_legal) {
6723 struct sockaddr_in *sin;
6725 sin = &sctp_ifa->address.sin;
6726 if (sin->sin_addr.s_addr == 0) {
6733 if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
6734 &sin->sin_addr) != 0) {
6737 if ((ipv4_local_scope == 0) &&
6738 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
6741 /* count this one */
6750 if (ipv6_addr_legal) {
6751 struct sockaddr_in6 *sin6;
6753 sin6 = &sctp_ifa->address.sin6;
6754 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6757 if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
6758 &sin6->sin6_addr) != 0) {
6761 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
6762 if (local_scope == 0)
6764 if (sin6->sin6_scope_id == 0) {
6765 if (sa6_recoverscope(sin6) != 0)
6778 if ((site_scope == 0) &&
6779 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
6782 /* count this one */
6797 struct sctp_laddr *laddr;
6799 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
6801 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
6804 /* count this one */
6808 SCTP_IPI_ADDR_RUNLOCK();
6812 #if defined(SCTP_LOCAL_TRACE_BUF)
6815 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)
6817 uint32_t saveindex, newindex;
6820 saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6821 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6824 newindex = saveindex + 1;
6826 } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6827 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6830 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6831 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6832 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6833 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6834 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6835 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6836 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6837 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6842 sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
6843 const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
6847 struct ip6_hdr *ip6;
6849 struct mbuf *sp, *last;
6850 struct udphdr *uhdr;
6853 if ((m->m_flags & M_PKTHDR) == 0) {
6854 /* Can't handle one that is not a pkt hdr */
6857 /* Pull the src port */
6858 iph = mtod(m, struct ip *);
6859 uhdr = (struct udphdr *)((caddr_t)iph + off);
6860 port = uhdr->uh_sport;
6862 * Split out the mbuf chain. Leave the IP header in m, place the
6865 sp = m_split(m, off, M_NOWAIT);
6867 /* Gak, drop packet, we can't do a split */
6870 if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
6871 /* Gak, packet can't have an SCTP header in it - too small */
6875 /* Now pull up the UDP header and SCTP header together */
6876 sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
6878 /* Gak pullup failed */
6881 /* Trim out the UDP header */
6882 m_adj(sp, sizeof(struct udphdr));
6884 /* Now reconstruct the mbuf chain */
6885 for (last = m; last->m_next; last = last->m_next);
6887 m->m_pkthdr.len += sp->m_pkthdr.len;
6889 * The CSUM_DATA_VALID flags indicates that the HW checked the UDP
6890 * checksum and it was valid. Since CSUM_DATA_VALID ==
6891 * CSUM_SCTP_VALID this would imply that the HW also verified the
6892 * SCTP checksum. Therefore, clear the bit.
6894 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6895 "sctp_recv_udp_tunneled_packet(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6897 if_name(m->m_pkthdr.rcvif),
6898 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6899 m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
6900 iph = mtod(m, struct ip *);
6901 switch (iph->ip_v) {
6904 iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
6905 sctp_input_with_port(m, off, port);
6909 case IPV6_VERSION >> 4:
6910 ip6 = mtod(m, struct ip6_hdr *);
6911 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
6912 sctp6_input_with_port(&m, &off, port);
6926 sctp_recv_icmp_tunneled_packet(int cmd, struct sockaddr *sa, void *vip, void *ctx SCTP_UNUSED)
6928 struct ip *outer_ip, *inner_ip;
6932 struct sctp_inpcb *inp;
6933 struct sctp_tcb *stcb;
6934 struct sctp_nets *net;
6935 struct sctp_init_chunk *ch;
6936 struct sockaddr_in src, dst;
6939 inner_ip = (struct ip *)vip;
6940 icmp = (struct icmp *)((caddr_t)inner_ip -
6941 (sizeof(struct icmp) - sizeof(struct ip)));
6942 outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
6943 if (ntohs(outer_ip->ip_len) <
6944 sizeof(struct ip) + 8 + (inner_ip->ip_hl << 2) + sizeof(struct udphdr) + 8) {
6947 udp = (struct udphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
6948 sh = (struct sctphdr *)(udp + 1);
6949 memset(&src, 0, sizeof(struct sockaddr_in));
6950 src.sin_family = AF_INET;
6951 src.sin_len = sizeof(struct sockaddr_in);
6952 src.sin_port = sh->src_port;
6953 src.sin_addr = inner_ip->ip_src;
6954 memset(&dst, 0, sizeof(struct sockaddr_in));
6955 dst.sin_family = AF_INET;
6956 dst.sin_len = sizeof(struct sockaddr_in);
6957 dst.sin_port = sh->dest_port;
6958 dst.sin_addr = inner_ip->ip_dst;
6960 * 'dst' holds the dest of the packet that failed to be sent. 'src'
6961 * holds our local endpoint address. Thus we reverse the dst and the
6962 * src in the lookup.
6966 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
6967 (struct sockaddr *)&src,
6969 SCTP_DEFAULT_VRFID);
6970 if ((stcb != NULL) &&
6973 /* Check the UDP port numbers */
6974 if ((udp->uh_dport != net->port) ||
6975 (udp->uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
6976 SCTP_TCB_UNLOCK(stcb);
6979 /* Check the verification tag */
6980 if (ntohl(sh->v_tag) != 0) {
6982 * This must be the verification tag used for
6983 * sending out packets. We don't consider packets
6984 * reflecting the verification tag.
6986 if (ntohl(sh->v_tag) != stcb->asoc.peer_vtag) {
6987 SCTP_TCB_UNLOCK(stcb);
6991 if (ntohs(outer_ip->ip_len) >=
6993 8 + (inner_ip->ip_hl << 2) + 8 + 20) {
6995 * In this case we can check if we got an
6996 * INIT chunk and if the initiate tag
6999 ch = (struct sctp_init_chunk *)(sh + 1);
7000 if ((ch->ch.chunk_type != SCTP_INITIATION) ||
7001 (ntohl(ch->init.initiate_tag) != stcb->asoc.my_vtag)) {
7002 SCTP_TCB_UNLOCK(stcb);
7006 SCTP_TCB_UNLOCK(stcb);
7010 type = icmp->icmp_type;
7011 code = icmp->icmp_code;
7012 if ((type == ICMP_UNREACH) &&
7013 (code == ICMP_UNREACH_PORT)) {
7014 code = ICMP_UNREACH_PROTOCOL;
7016 sctp_notify(inp, stcb, net, type, code,
7017 ntohs(inner_ip->ip_len),
7018 (uint32_t)ntohs(icmp->icmp_nextmtu));
7020 if ((stcb == NULL) && (inp != NULL)) {
7021 /* reduce ref-count */
7022 SCTP_INP_WLOCK(inp);
7023 SCTP_INP_DECR_REF(inp);
7024 SCTP_INP_WUNLOCK(inp);
7027 SCTP_TCB_UNLOCK(stcb);
7036 sctp_recv_icmp6_tunneled_packet(int cmd, struct sockaddr *sa, void *d, void *ctx SCTP_UNUSED)
7038 struct ip6ctlparam *ip6cp;
7039 struct sctp_inpcb *inp;
7040 struct sctp_tcb *stcb;
7041 struct sctp_nets *net;
7044 struct sockaddr_in6 src, dst;
7047 ip6cp = (struct ip6ctlparam *)d;
7049 * XXX: We assume that when IPV6 is non NULL, M and OFF are valid.
7051 if (ip6cp->ip6c_m == NULL) {
7055 * Check if we can safely examine the ports and the verification tag
7056 * of the SCTP common header.
7058 if (ip6cp->ip6c_m->m_pkthdr.len <
7059 ip6cp->ip6c_off + sizeof(struct udphdr) + offsetof(struct sctphdr, checksum)) {
7062 /* Copy out the UDP header. */
7063 memset(&udp, 0, sizeof(struct udphdr));
7064 m_copydata(ip6cp->ip6c_m,
7066 sizeof(struct udphdr),
7068 /* Copy out the port numbers and the verification tag. */
7069 memset(&sh, 0, sizeof(struct sctphdr));
7070 m_copydata(ip6cp->ip6c_m,
7071 ip6cp->ip6c_off + sizeof(struct udphdr),
7072 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
7074 memset(&src, 0, sizeof(struct sockaddr_in6));
7075 src.sin6_family = AF_INET6;
7076 src.sin6_len = sizeof(struct sockaddr_in6);
7077 src.sin6_port = sh.src_port;
7078 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
7079 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7082 memset(&dst, 0, sizeof(struct sockaddr_in6));
7083 dst.sin6_family = AF_INET6;
7084 dst.sin6_len = sizeof(struct sockaddr_in6);
7085 dst.sin6_port = sh.dest_port;
7086 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
7087 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7092 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7093 (struct sockaddr *)&src,
7094 &inp, &net, 1, SCTP_DEFAULT_VRFID);
7095 if ((stcb != NULL) &&
7098 /* Check the UDP port numbers */
7099 if ((udp.uh_dport != net->port) ||
7100 (udp.uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7101 SCTP_TCB_UNLOCK(stcb);
7104 /* Check the verification tag */
7105 if (ntohl(sh.v_tag) != 0) {
7107 * This must be the verification tag used for
7108 * sending out packets. We don't consider packets
7109 * reflecting the verification tag.
7111 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
7112 SCTP_TCB_UNLOCK(stcb);
7116 if (ip6cp->ip6c_m->m_pkthdr.len >=
7117 ip6cp->ip6c_off + sizeof(struct udphdr) +
7118 sizeof(struct sctphdr) +
7119 sizeof(struct sctp_chunkhdr) +
7120 offsetof(struct sctp_init, a_rwnd)) {
7122 * In this case we can check if we got an
7123 * INIT chunk and if the initiate tag
7126 uint32_t initiate_tag;
7129 m_copydata(ip6cp->ip6c_m,
7131 sizeof(struct udphdr) +
7132 sizeof(struct sctphdr),
7134 (caddr_t)&chunk_type);
7135 m_copydata(ip6cp->ip6c_m,
7137 sizeof(struct udphdr) +
7138 sizeof(struct sctphdr) +
7139 sizeof(struct sctp_chunkhdr),
7141 (caddr_t)&initiate_tag);
7142 if ((chunk_type != SCTP_INITIATION) ||
7143 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
7144 SCTP_TCB_UNLOCK(stcb);
7148 SCTP_TCB_UNLOCK(stcb);
7152 type = ip6cp->ip6c_icmp6->icmp6_type;
7153 code = ip6cp->ip6c_icmp6->icmp6_code;
7154 if ((type == ICMP6_DST_UNREACH) &&
7155 (code == ICMP6_DST_UNREACH_NOPORT)) {
7156 type = ICMP6_PARAM_PROB;
7157 code = ICMP6_PARAMPROB_NEXTHEADER;
7159 sctp6_notify(inp, stcb, net, type, code,
7160 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
7162 if ((stcb == NULL) && (inp != NULL)) {
7163 /* reduce inp's ref-count */
7164 SCTP_INP_WLOCK(inp);
7165 SCTP_INP_DECR_REF(inp);
7166 SCTP_INP_WUNLOCK(inp);
7169 SCTP_TCB_UNLOCK(stcb);
7176 sctp_over_udp_stop(void)
7179 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7183 if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7184 soclose(SCTP_BASE_INFO(udp4_tun_socket));
7185 SCTP_BASE_INFO(udp4_tun_socket) = NULL;
7189 if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
7190 soclose(SCTP_BASE_INFO(udp6_tun_socket));
7191 SCTP_BASE_INFO(udp6_tun_socket) = NULL;
7197 sctp_over_udp_start(void)
7202 struct sockaddr_in sin;
7205 struct sockaddr_in6 sin6;
7208 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7211 port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
7212 if (ntohs(port) == 0) {
7213 /* Must have a port set */
7217 if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7218 /* Already running -- must stop first */
7223 if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
7224 /* Already running -- must stop first */
7229 if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
7230 SOCK_DGRAM, IPPROTO_UDP,
7231 curthread->td_ucred, curthread))) {
7232 sctp_over_udp_stop();
7235 /* Call the special UDP hook. */
7236 if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
7237 sctp_recv_udp_tunneled_packet,
7238 sctp_recv_icmp_tunneled_packet,
7240 sctp_over_udp_stop();
7243 /* Ok, we have a socket, bind it to the port. */
7244 memset(&sin, 0, sizeof(struct sockaddr_in));
7245 sin.sin_len = sizeof(struct sockaddr_in);
7246 sin.sin_family = AF_INET;
7247 sin.sin_port = htons(port);
7248 if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
7249 (struct sockaddr *)&sin, curthread))) {
7250 sctp_over_udp_stop();
7255 if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
7256 SOCK_DGRAM, IPPROTO_UDP,
7257 curthread->td_ucred, curthread))) {
7258 sctp_over_udp_stop();
7261 /* Call the special UDP hook. */
7262 if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
7263 sctp_recv_udp_tunneled_packet,
7264 sctp_recv_icmp6_tunneled_packet,
7266 sctp_over_udp_stop();
7269 /* Ok, we have a socket, bind it to the port. */
7270 memset(&sin6, 0, sizeof(struct sockaddr_in6));
7271 sin6.sin6_len = sizeof(struct sockaddr_in6);
7272 sin6.sin6_family = AF_INET6;
7273 sin6.sin6_port = htons(port);
7274 if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
7275 (struct sockaddr *)&sin6, curthread))) {
7276 sctp_over_udp_stop();
7283 #if defined(INET6) || defined(INET)
7286 * sctp_min_mtu ()returns the minimum of all non-zero arguments.
7287 * If all arguments are zero, zero is returned.
7290 sctp_min_mtu(uint32_t mtu1, uint32_t mtu2, uint32_t mtu3)
7295 return (min(mtu1, min(mtu2, mtu3)));
7297 return (min(mtu1, mtu2));
7301 return (min(mtu1, mtu3));
7309 return (min(mtu2, mtu3));
7320 sctp_hc_set_mtu(union sctp_sockstore *addr, uint16_t fibnum, uint32_t mtu)
7322 struct in_conninfo inc;
7324 memset(&inc, 0, sizeof(struct in_conninfo));
7325 inc.inc_fibnum = fibnum;
7326 switch (addr->sa.sa_family) {
7329 inc.inc_faddr = addr->sin.sin_addr;
7334 inc.inc_flags |= INC_ISIPV6;
7335 inc.inc6_faddr = addr->sin6.sin6_addr;
7341 tcp_hc_updatemtu(&inc, (u_long)mtu);
7345 sctp_hc_get_mtu(union sctp_sockstore *addr, uint16_t fibnum)
7347 struct in_conninfo inc;
7349 memset(&inc, 0, sizeof(struct in_conninfo));
7350 inc.inc_fibnum = fibnum;
7351 switch (addr->sa.sa_family) {
7354 inc.inc_faddr = addr->sin.sin_addr;
7359 inc.inc_flags |= INC_ISIPV6;
7360 inc.inc6_faddr = addr->sin6.sin6_addr;
7366 return ((uint32_t)tcp_hc_getmtu(&inc));