2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctputil.h>
41 #include <netinet/sctp_var.h>
42 #include <netinet/sctp_sysctl.h>
44 #include <netinet6/sctp6_var.h>
46 #include <netinet/sctp_header.h>
47 #include <netinet/sctp_output.h>
48 #include <netinet/sctp_uio.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_indata.h>
51 #include <netinet/sctp_auth.h>
52 #include <netinet/sctp_asconf.h>
53 #include <netinet/sctp_bsd_addr.h>
54 #if defined(INET6) || defined(INET)
55 #include <netinet/tcp_var.h>
57 #include <netinet/udp.h>
58 #include <netinet/udp_var.h>
61 #include <netinet/icmp6.h>
66 #define KTR_SCTP KTR_SUBSYS
69 extern const struct sctp_cc_functions sctp_cc_functions[];
70 extern const struct sctp_ss_functions sctp_ss_functions[];
73 sctp_sblog(struct sockbuf *sb, struct sctp_tcb *stcb, int from, int incr)
75 struct sctp_cwnd_log sctp_clog;
77 sctp_clog.x.sb.stcb = stcb;
78 sctp_clog.x.sb.so_sbcc = sb->sb_cc;
80 sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
82 sctp_clog.x.sb.stcb_sbcc = 0;
83 sctp_clog.x.sb.incr = incr;
84 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
87 sctp_clog.x.misc.log1,
88 sctp_clog.x.misc.log2,
89 sctp_clog.x.misc.log3,
90 sctp_clog.x.misc.log4);
94 sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
96 struct sctp_cwnd_log sctp_clog;
98 sctp_clog.x.close.inp = (void *)inp;
99 sctp_clog.x.close.sctp_flags = inp->sctp_flags;
101 sctp_clog.x.close.stcb = (void *)stcb;
102 sctp_clog.x.close.state = (uint16_t)stcb->asoc.state;
104 sctp_clog.x.close.stcb = 0;
105 sctp_clog.x.close.state = 0;
107 sctp_clog.x.close.loc = loc;
108 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
109 SCTP_LOG_EVENT_CLOSE,
111 sctp_clog.x.misc.log1,
112 sctp_clog.x.misc.log2,
113 sctp_clog.x.misc.log3,
114 sctp_clog.x.misc.log4);
118 rto_logging(struct sctp_nets *net, int from)
120 struct sctp_cwnd_log sctp_clog;
122 memset(&sctp_clog, 0, sizeof(sctp_clog));
123 sctp_clog.x.rto.net = (void *)net;
124 sctp_clog.x.rto.rtt = net->rtt / 1000;
125 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
128 sctp_clog.x.misc.log1,
129 sctp_clog.x.misc.log2,
130 sctp_clog.x.misc.log3,
131 sctp_clog.x.misc.log4);
135 sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
137 struct sctp_cwnd_log sctp_clog;
139 sctp_clog.x.strlog.stcb = stcb;
140 sctp_clog.x.strlog.n_tsn = tsn;
141 sctp_clog.x.strlog.n_sseq = sseq;
142 sctp_clog.x.strlog.e_tsn = 0;
143 sctp_clog.x.strlog.e_sseq = 0;
144 sctp_clog.x.strlog.strm = stream;
145 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
148 sctp_clog.x.misc.log1,
149 sctp_clog.x.misc.log2,
150 sctp_clog.x.misc.log3,
151 sctp_clog.x.misc.log4);
155 sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
157 struct sctp_cwnd_log sctp_clog;
159 sctp_clog.x.nagle.stcb = (void *)stcb;
160 sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
161 sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
162 sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
163 sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
164 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
165 SCTP_LOG_EVENT_NAGLE,
167 sctp_clog.x.misc.log1,
168 sctp_clog.x.misc.log2,
169 sctp_clog.x.misc.log3,
170 sctp_clog.x.misc.log4);
174 sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
176 struct sctp_cwnd_log sctp_clog;
178 sctp_clog.x.sack.cumack = cumack;
179 sctp_clog.x.sack.oldcumack = old_cumack;
180 sctp_clog.x.sack.tsn = tsn;
181 sctp_clog.x.sack.numGaps = gaps;
182 sctp_clog.x.sack.numDups = dups;
183 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
186 sctp_clog.x.misc.log1,
187 sctp_clog.x.misc.log2,
188 sctp_clog.x.misc.log3,
189 sctp_clog.x.misc.log4);
193 sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
195 struct sctp_cwnd_log sctp_clog;
197 memset(&sctp_clog, 0, sizeof(sctp_clog));
198 sctp_clog.x.map.base = map;
199 sctp_clog.x.map.cum = cum;
200 sctp_clog.x.map.high = high;
201 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
204 sctp_clog.x.misc.log1,
205 sctp_clog.x.misc.log2,
206 sctp_clog.x.misc.log3,
207 sctp_clog.x.misc.log4);
211 sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn, int from)
213 struct sctp_cwnd_log sctp_clog;
215 memset(&sctp_clog, 0, sizeof(sctp_clog));
216 sctp_clog.x.fr.largest_tsn = biggest_tsn;
217 sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
218 sctp_clog.x.fr.tsn = tsn;
219 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
222 sctp_clog.x.misc.log1,
223 sctp_clog.x.misc.log2,
224 sctp_clog.x.misc.log3,
225 sctp_clog.x.misc.log4);
228 #ifdef SCTP_MBUF_LOGGING
230 sctp_log_mb(struct mbuf *m, int from)
232 struct sctp_cwnd_log sctp_clog;
234 sctp_clog.x.mb.mp = m;
235 sctp_clog.x.mb.mbuf_flags = (uint8_t)(SCTP_BUF_GET_FLAGS(m));
236 sctp_clog.x.mb.size = (uint16_t)(SCTP_BUF_LEN(m));
237 sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
238 if (SCTP_BUF_IS_EXTENDED(m)) {
239 sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
240 sctp_clog.x.mb.refcnt = (uint8_t)(SCTP_BUF_EXTEND_REFCNT(m));
242 sctp_clog.x.mb.ext = 0;
243 sctp_clog.x.mb.refcnt = 0;
245 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
248 sctp_clog.x.misc.log1,
249 sctp_clog.x.misc.log2,
250 sctp_clog.x.misc.log3,
251 sctp_clog.x.misc.log4);
255 sctp_log_mbc(struct mbuf *m, int from)
259 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
260 sctp_log_mb(mat, from);
266 sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from)
268 struct sctp_cwnd_log sctp_clog;
270 if (control == NULL) {
271 SCTP_PRINTF("Gak log of NULL?\n");
274 sctp_clog.x.strlog.stcb = control->stcb;
275 sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
276 sctp_clog.x.strlog.n_sseq = (uint16_t)control->mid;
277 sctp_clog.x.strlog.strm = control->sinfo_stream;
278 if (poschk != NULL) {
279 sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
280 sctp_clog.x.strlog.e_sseq = (uint16_t)poschk->mid;
282 sctp_clog.x.strlog.e_tsn = 0;
283 sctp_clog.x.strlog.e_sseq = 0;
285 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
288 sctp_clog.x.misc.log1,
289 sctp_clog.x.misc.log2,
290 sctp_clog.x.misc.log3,
291 sctp_clog.x.misc.log4);
295 sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
297 struct sctp_cwnd_log sctp_clog;
299 sctp_clog.x.cwnd.net = net;
300 if (stcb->asoc.send_queue_cnt > 255)
301 sctp_clog.x.cwnd.cnt_in_send = 255;
303 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
304 if (stcb->asoc.stream_queue_cnt > 255)
305 sctp_clog.x.cwnd.cnt_in_str = 255;
307 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
310 sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
311 sctp_clog.x.cwnd.inflight = net->flight_size;
312 sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
313 sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
314 sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
316 if (SCTP_CWNDLOG_PRESEND == from) {
317 sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
319 sctp_clog.x.cwnd.cwnd_augment = augment;
320 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
323 sctp_clog.x.misc.log1,
324 sctp_clog.x.misc.log2,
325 sctp_clog.x.misc.log3,
326 sctp_clog.x.misc.log4);
330 sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
332 struct sctp_cwnd_log sctp_clog;
334 memset(&sctp_clog, 0, sizeof(sctp_clog));
336 sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
339 sctp_clog.x.lock.sock = (void *)NULL;
341 sctp_clog.x.lock.inp = (void *)inp;
343 sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
345 sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
348 sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
349 sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
351 sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
352 sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
354 sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
355 if (inp && (inp->sctp_socket)) {
356 sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
357 sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
358 sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
360 sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
361 sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
362 sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
364 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
367 sctp_clog.x.misc.log1,
368 sctp_clog.x.misc.log2,
369 sctp_clog.x.misc.log3,
370 sctp_clog.x.misc.log4);
374 sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
376 struct sctp_cwnd_log sctp_clog;
378 memset(&sctp_clog, 0, sizeof(sctp_clog));
379 sctp_clog.x.cwnd.net = net;
380 sctp_clog.x.cwnd.cwnd_new_value = error;
381 sctp_clog.x.cwnd.inflight = net->flight_size;
382 sctp_clog.x.cwnd.cwnd_augment = burst;
383 if (stcb->asoc.send_queue_cnt > 255)
384 sctp_clog.x.cwnd.cnt_in_send = 255;
386 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
387 if (stcb->asoc.stream_queue_cnt > 255)
388 sctp_clog.x.cwnd.cnt_in_str = 255;
390 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
391 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
392 SCTP_LOG_EVENT_MAXBURST,
394 sctp_clog.x.misc.log1,
395 sctp_clog.x.misc.log2,
396 sctp_clog.x.misc.log3,
397 sctp_clog.x.misc.log4);
401 sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
403 struct sctp_cwnd_log sctp_clog;
405 sctp_clog.x.rwnd.rwnd = peers_rwnd;
406 sctp_clog.x.rwnd.send_size = snd_size;
407 sctp_clog.x.rwnd.overhead = overhead;
408 sctp_clog.x.rwnd.new_rwnd = 0;
409 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
412 sctp_clog.x.misc.log1,
413 sctp_clog.x.misc.log2,
414 sctp_clog.x.misc.log3,
415 sctp_clog.x.misc.log4);
419 sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
421 struct sctp_cwnd_log sctp_clog;
423 sctp_clog.x.rwnd.rwnd = peers_rwnd;
424 sctp_clog.x.rwnd.send_size = flight_size;
425 sctp_clog.x.rwnd.overhead = overhead;
426 sctp_clog.x.rwnd.new_rwnd = a_rwndval;
427 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
430 sctp_clog.x.misc.log1,
431 sctp_clog.x.misc.log2,
432 sctp_clog.x.misc.log3,
433 sctp_clog.x.misc.log4);
436 #ifdef SCTP_MBCNT_LOGGING
438 sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
440 struct sctp_cwnd_log sctp_clog;
442 sctp_clog.x.mbcnt.total_queue_size = total_oq;
443 sctp_clog.x.mbcnt.size_change = book;
444 sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
445 sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
446 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
447 SCTP_LOG_EVENT_MBCNT,
449 sctp_clog.x.misc.log1,
450 sctp_clog.x.misc.log2,
451 sctp_clog.x.misc.log3,
452 sctp_clog.x.misc.log4);
457 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
459 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
466 sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from)
468 struct sctp_cwnd_log sctp_clog;
470 sctp_clog.x.wake.stcb = (void *)stcb;
471 sctp_clog.x.wake.wake_cnt = wake_cnt;
472 sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
473 sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
474 sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
476 if (stcb->asoc.stream_queue_cnt < 0xff)
477 sctp_clog.x.wake.stream_qcnt = (uint8_t)stcb->asoc.stream_queue_cnt;
479 sctp_clog.x.wake.stream_qcnt = 0xff;
481 if (stcb->asoc.chunks_on_out_queue < 0xff)
482 sctp_clog.x.wake.chunks_on_oque = (uint8_t)stcb->asoc.chunks_on_out_queue;
484 sctp_clog.x.wake.chunks_on_oque = 0xff;
486 sctp_clog.x.wake.sctpflags = 0;
487 /* set in the defered mode stuff */
488 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
489 sctp_clog.x.wake.sctpflags |= 1;
490 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
491 sctp_clog.x.wake.sctpflags |= 2;
492 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
493 sctp_clog.x.wake.sctpflags |= 4;
494 /* what about the sb */
495 if (stcb->sctp_socket) {
496 struct socket *so = stcb->sctp_socket;
498 sctp_clog.x.wake.sbflags = (uint8_t)((so->so_snd.sb_flags & 0x00ff));
500 sctp_clog.x.wake.sbflags = 0xff;
502 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
505 sctp_clog.x.misc.log1,
506 sctp_clog.x.misc.log2,
507 sctp_clog.x.misc.log3,
508 sctp_clog.x.misc.log4);
512 sctp_log_block(uint8_t from, struct sctp_association *asoc, size_t sendlen)
514 struct sctp_cwnd_log sctp_clog;
516 sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
517 sctp_clog.x.blk.send_sent_qcnt = (uint16_t)(asoc->send_queue_cnt + asoc->sent_queue_cnt);
518 sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
519 sctp_clog.x.blk.stream_qcnt = (uint16_t)asoc->stream_queue_cnt;
520 sctp_clog.x.blk.chunks_on_oque = (uint16_t)asoc->chunks_on_out_queue;
521 sctp_clog.x.blk.flight_size = (uint16_t)(asoc->total_flight / 1024);
522 sctp_clog.x.blk.sndlen = (uint32_t)sendlen;
523 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
524 SCTP_LOG_EVENT_BLOCK,
526 sctp_clog.x.misc.log1,
527 sctp_clog.x.misc.log2,
528 sctp_clog.x.misc.log3,
529 sctp_clog.x.misc.log4);
533 sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED)
535 /* May need to fix this if ktrdump does not work */
539 #ifdef SCTP_AUDITING_ENABLED
540 uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
541 static int sctp_audit_indx = 0;
545 sctp_print_audit_report(void)
551 for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
552 if ((sctp_audit_data[i][0] == 0xe0) &&
553 (sctp_audit_data[i][1] == 0x01)) {
556 } else if (sctp_audit_data[i][0] == 0xf0) {
559 } else if ((sctp_audit_data[i][0] == 0xc0) &&
560 (sctp_audit_data[i][1] == 0x01)) {
564 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t)sctp_audit_data[i][0],
565 (uint32_t)sctp_audit_data[i][1]);
570 for (i = 0; i < sctp_audit_indx; i++) {
571 if ((sctp_audit_data[i][0] == 0xe0) &&
572 (sctp_audit_data[i][1] == 0x01)) {
575 } else if (sctp_audit_data[i][0] == 0xf0) {
578 } else if ((sctp_audit_data[i][0] == 0xc0) &&
579 (sctp_audit_data[i][1] == 0x01)) {
583 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t)sctp_audit_data[i][0],
584 (uint32_t)sctp_audit_data[i][1]);
593 sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
594 struct sctp_nets *net)
596 int resend_cnt, tot_out, rep, tot_book_cnt;
597 struct sctp_nets *lnet;
598 struct sctp_tmit_chunk *chk;
600 sctp_audit_data[sctp_audit_indx][0] = 0xAA;
601 sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
603 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
607 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
608 sctp_audit_data[sctp_audit_indx][1] = 0x01;
610 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
616 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
617 sctp_audit_data[sctp_audit_indx][1] = 0x02;
619 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
624 sctp_audit_data[sctp_audit_indx][0] = 0xA1;
625 sctp_audit_data[sctp_audit_indx][1] =
626 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
628 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
633 resend_cnt = tot_out = 0;
634 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
635 if (chk->sent == SCTP_DATAGRAM_RESEND) {
637 } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
638 tot_out += chk->book_size;
642 if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
643 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
644 sctp_audit_data[sctp_audit_indx][1] = 0xA1;
646 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
649 SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
650 resend_cnt, stcb->asoc.sent_queue_retran_cnt);
652 stcb->asoc.sent_queue_retran_cnt = resend_cnt;
653 sctp_audit_data[sctp_audit_indx][0] = 0xA2;
654 sctp_audit_data[sctp_audit_indx][1] =
655 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
657 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
661 if (tot_out != stcb->asoc.total_flight) {
662 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
663 sctp_audit_data[sctp_audit_indx][1] = 0xA2;
665 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
669 SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
670 (int)stcb->asoc.total_flight);
671 stcb->asoc.total_flight = tot_out;
673 if (tot_book_cnt != stcb->asoc.total_flight_count) {
674 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
675 sctp_audit_data[sctp_audit_indx][1] = 0xA5;
677 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
681 SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
683 stcb->asoc.total_flight_count = tot_book_cnt;
686 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
687 tot_out += lnet->flight_size;
689 if (tot_out != stcb->asoc.total_flight) {
690 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
691 sctp_audit_data[sctp_audit_indx][1] = 0xA3;
693 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
697 SCTP_PRINTF("real flight:%d net total was %d\n",
698 stcb->asoc.total_flight, tot_out);
699 /* now corrective action */
700 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
703 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
704 if ((chk->whoTo == lnet) &&
705 (chk->sent < SCTP_DATAGRAM_RESEND)) {
706 tot_out += chk->book_size;
709 if (lnet->flight_size != tot_out) {
710 SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
711 (void *)lnet, lnet->flight_size,
713 lnet->flight_size = tot_out;
718 sctp_print_audit_report();
723 sctp_audit_log(uint8_t ev, uint8_t fd)
726 sctp_audit_data[sctp_audit_indx][0] = ev;
727 sctp_audit_data[sctp_audit_indx][1] = fd;
729 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
737 * sctp_stop_timers_for_shutdown() should be called
738 * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
739 * state to make sure that all timers are stopped.
742 sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
744 struct sctp_association *asoc;
745 struct sctp_nets *net;
749 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
750 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
751 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
752 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
753 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
754 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
755 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
756 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
761 * a list of sizes based on typical mtu's, used only if next hop size not
764 static uint32_t sctp_mtu_sizes[] = {
786 * Return the largest MTU smaller than val. If there is no
787 * entry, just return val.
790 sctp_get_prev_mtu(uint32_t val)
794 if (val <= sctp_mtu_sizes[0]) {
797 for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
798 if (val <= sctp_mtu_sizes[i]) {
802 return (sctp_mtu_sizes[i - 1]);
806 * Return the smallest MTU larger than val. If there is no
807 * entry, just return val.
810 sctp_get_next_mtu(uint32_t val)
812 /* select another MTU that is just bigger than this one */
815 for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
816 if (val < sctp_mtu_sizes[i]) {
817 return (sctp_mtu_sizes[i]);
824 sctp_fill_random_store(struct sctp_pcb *m)
827 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
828 * our counter. The result becomes our good random numbers and we
829 * then setup to give these out. Note that we do no locking to
830 * protect this. This is ok, since if competing folks call this we
831 * will get more gobbled gook in the random store which is what we
832 * want. There is a danger that two guys will use the same random
833 * numbers, but thats ok too since that is random as well :->
836 (void)sctp_hmac(SCTP_HMAC, (uint8_t *)m->random_numbers,
837 sizeof(m->random_numbers), (uint8_t *)&m->random_counter,
838 sizeof(m->random_counter), (uint8_t *)m->random_store);
843 sctp_select_initial_TSN(struct sctp_pcb *inp)
846 * A true implementation should use random selection process to get
847 * the initial stream sequence number, using RFC1750 as a good
852 int store_at, new_store;
854 if (inp->initial_sequence_debug != 0) {
857 ret = inp->initial_sequence_debug;
858 inp->initial_sequence_debug++;
862 store_at = inp->store_at;
863 new_store = store_at + sizeof(uint32_t);
864 if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
867 if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
870 if (new_store == 0) {
871 /* Refill the random store */
872 sctp_fill_random_store(inp);
874 p = &inp->random_store[store_at];
881 sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
887 (void)SCTP_GETTIME_TIMEVAL(&now);
890 x = sctp_select_initial_TSN(&inp->sctp_ep);
895 if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
903 sctp_map_assoc_state(int kernel_state)
907 if (kernel_state & SCTP_STATE_WAS_ABORTED) {
908 user_state = SCTP_CLOSED;
909 } else if (kernel_state & SCTP_STATE_SHUTDOWN_PENDING) {
910 user_state = SCTP_SHUTDOWN_PENDING;
912 switch (kernel_state & SCTP_STATE_MASK) {
913 case SCTP_STATE_EMPTY:
914 user_state = SCTP_CLOSED;
916 case SCTP_STATE_INUSE:
917 user_state = SCTP_CLOSED;
919 case SCTP_STATE_COOKIE_WAIT:
920 user_state = SCTP_COOKIE_WAIT;
922 case SCTP_STATE_COOKIE_ECHOED:
923 user_state = SCTP_COOKIE_ECHOED;
925 case SCTP_STATE_OPEN:
926 user_state = SCTP_ESTABLISHED;
928 case SCTP_STATE_SHUTDOWN_SENT:
929 user_state = SCTP_SHUTDOWN_SENT;
931 case SCTP_STATE_SHUTDOWN_RECEIVED:
932 user_state = SCTP_SHUTDOWN_RECEIVED;
934 case SCTP_STATE_SHUTDOWN_ACK_SENT:
935 user_state = SCTP_SHUTDOWN_ACK_SENT;
938 user_state = SCTP_CLOSED;
946 sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
947 uint32_t override_tag, uint32_t vrf_id, uint16_t o_strms)
949 struct sctp_association *asoc;
952 * Anything set to zero is taken care of by the allocation routine's
957 * Up front select what scoping to apply on addresses I tell my peer
958 * Not sure what to do with these right now, we will need to come up
959 * with a way to set them. We may need to pass them through from the
960 * caller in the sctp_aloc_assoc() function.
963 #if defined(SCTP_DETAILED_STR_STATS)
968 /* init all variables to a known value. */
969 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE);
970 asoc->max_burst = inp->sctp_ep.max_burst;
971 asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
972 asoc->heart_beat_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
973 asoc->cookie_life = inp->sctp_ep.def_cookie_life;
974 asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
975 asoc->ecn_supported = inp->ecn_supported;
976 asoc->prsctp_supported = inp->prsctp_supported;
977 asoc->idata_supported = inp->idata_supported;
978 asoc->auth_supported = inp->auth_supported;
979 asoc->asconf_supported = inp->asconf_supported;
980 asoc->reconfig_supported = inp->reconfig_supported;
981 asoc->nrsack_supported = inp->nrsack_supported;
982 asoc->pktdrop_supported = inp->pktdrop_supported;
983 asoc->idata_supported = inp->idata_supported;
984 asoc->sctp_cmt_pf = (uint8_t)0;
985 asoc->sctp_frag_point = inp->sctp_frag_point;
986 asoc->sctp_features = inp->sctp_features;
987 asoc->default_dscp = inp->sctp_ep.default_dscp;
988 asoc->max_cwnd = inp->max_cwnd;
990 if (inp->sctp_ep.default_flowlabel) {
991 asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
993 if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
994 asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
995 asoc->default_flowlabel &= 0x000fffff;
996 asoc->default_flowlabel |= 0x80000000;
998 asoc->default_flowlabel = 0;
1002 asoc->sb_send_resv = 0;
1004 asoc->my_vtag = override_tag;
1006 asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
1008 /* Get the nonce tags */
1009 asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1010 asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1011 asoc->vrf_id = vrf_id;
1013 #ifdef SCTP_ASOCLOG_OF_TSNS
1014 asoc->tsn_in_at = 0;
1015 asoc->tsn_out_at = 0;
1016 asoc->tsn_in_wrapped = 0;
1017 asoc->tsn_out_wrapped = 0;
1018 asoc->cumack_log_at = 0;
1019 asoc->cumack_log_atsnt = 0;
1021 #ifdef SCTP_FS_SPEC_LOG
1025 asoc->assoc_up_sent = 0;
1026 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
1027 sctp_select_initial_TSN(&inp->sctp_ep);
1028 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1029 /* we are optimisitic here */
1030 asoc->peer_supports_nat = 0;
1031 asoc->sent_queue_retran_cnt = 0;
1034 asoc->last_net_cmt_send_started = NULL;
1036 /* This will need to be adjusted */
1037 asoc->last_acked_seq = asoc->init_seq_number - 1;
1038 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1039 asoc->asconf_seq_in = asoc->last_acked_seq;
1041 /* here we are different, we hold the next one we expect */
1042 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
1044 asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
1045 asoc->initial_rto = inp->sctp_ep.initial_rto;
1047 asoc->default_mtu = inp->sctp_ep.default_mtu;
1048 asoc->max_init_times = inp->sctp_ep.max_init_times;
1049 asoc->max_send_times = inp->sctp_ep.max_send_times;
1050 asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1051 asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1052 asoc->free_chunk_cnt = 0;
1054 asoc->iam_blocking = 0;
1055 asoc->context = inp->sctp_context;
1056 asoc->local_strreset_support = inp->local_strreset_support;
1057 asoc->def_send = inp->def_send;
1058 asoc->delayed_ack = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1059 asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1060 asoc->pr_sctp_cnt = 0;
1061 asoc->total_output_queue_size = 0;
1063 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1064 asoc->scope.ipv6_addr_legal = 1;
1065 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1066 asoc->scope.ipv4_addr_legal = 1;
1068 asoc->scope.ipv4_addr_legal = 0;
1071 asoc->scope.ipv6_addr_legal = 0;
1072 asoc->scope.ipv4_addr_legal = 1;
1075 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1076 asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1078 asoc->smallest_mtu = inp->sctp_frag_point;
1079 asoc->minrto = inp->sctp_ep.sctp_minrto;
1080 asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1082 asoc->stream_locked_on = 0;
1083 asoc->ecn_echo_cnt_onq = 0;
1084 asoc->stream_locked = 0;
1086 asoc->send_sack = 1;
1088 LIST_INIT(&asoc->sctp_restricted_addrs);
1090 TAILQ_INIT(&asoc->nets);
1091 TAILQ_INIT(&asoc->pending_reply_queue);
1092 TAILQ_INIT(&asoc->asconf_ack_sent);
1093 /* Setup to fill the hb random cache at first HB */
1094 asoc->hb_random_idx = 4;
1096 asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1098 stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1099 stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1101 stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1102 stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1105 * Now the stream parameters, here we allocate space for all streams
1106 * that we request by default.
1108 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1110 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1111 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1113 if (asoc->strmout == NULL) {
1114 /* big trouble no memory */
1115 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1118 for (i = 0; i < asoc->streamoutcnt; i++) {
1120 * inbound side must be set to 0xffff, also NOTE when we get
1121 * the INIT-ACK back (for INIT sender) we MUST reduce the
1122 * count (streamoutcnt) but first check if we sent to any of
1123 * the upper streams that were dropped (if some were). Those
1124 * that were dropped must be notified to the upper layer as
1127 asoc->strmout[i].next_mid_ordered = 0;
1128 asoc->strmout[i].next_mid_unordered = 0;
1129 TAILQ_INIT(&asoc->strmout[i].outqueue);
1130 asoc->strmout[i].chunks_on_queues = 0;
1131 #if defined(SCTP_DETAILED_STR_STATS)
1132 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1133 asoc->strmout[i].abandoned_sent[j] = 0;
1134 asoc->strmout[i].abandoned_unsent[j] = 0;
1137 asoc->strmout[i].abandoned_sent[0] = 0;
1138 asoc->strmout[i].abandoned_unsent[0] = 0;
1140 asoc->strmout[i].sid = i;
1141 asoc->strmout[i].last_msg_incomplete = 0;
1142 asoc->strmout[i].state = SCTP_STREAM_OPENING;
1143 asoc->ss_functions.sctp_ss_init_stream(stcb, &asoc->strmout[i], NULL);
1145 asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1147 /* Now the mapping array */
1148 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1149 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1151 if (asoc->mapping_array == NULL) {
1152 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1153 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1156 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1157 SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1159 if (asoc->nr_mapping_array == NULL) {
1160 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1161 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1162 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1165 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1167 /* Now the init of the other outqueues */
1168 TAILQ_INIT(&asoc->free_chunks);
1169 TAILQ_INIT(&asoc->control_send_queue);
1170 TAILQ_INIT(&asoc->asconf_send_queue);
1171 TAILQ_INIT(&asoc->send_queue);
1172 TAILQ_INIT(&asoc->sent_queue);
1173 TAILQ_INIT(&asoc->resetHead);
1174 asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1175 TAILQ_INIT(&asoc->asconf_queue);
1176 /* authentication fields */
1177 asoc->authinfo.random = NULL;
1178 asoc->authinfo.active_keyid = 0;
1179 asoc->authinfo.assoc_key = NULL;
1180 asoc->authinfo.assoc_keyid = 0;
1181 asoc->authinfo.recv_key = NULL;
1182 asoc->authinfo.recv_keyid = 0;
1183 LIST_INIT(&asoc->shared_keys);
1184 asoc->marked_retrans = 0;
1185 asoc->port = inp->sctp_ep.port;
1189 asoc->timoshutdown = 0;
1190 asoc->timoheartbeat = 0;
1191 asoc->timocookie = 0;
1192 asoc->timoshutdownack = 0;
1193 (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1194 asoc->discontinuity_time = asoc->start_time;
1195 for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1196 asoc->abandoned_unsent[i] = 0;
1197 asoc->abandoned_sent[i] = 0;
1200 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
1201 * freed later when the association is freed.
1207 sctp_print_mapping_array(struct sctp_association *asoc)
1209 unsigned int i, limit;
1211 SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
1212 asoc->mapping_array_size,
1213 asoc->mapping_array_base_tsn,
1214 asoc->cumulative_tsn,
1215 asoc->highest_tsn_inside_map,
1216 asoc->highest_tsn_inside_nr_map);
1217 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1218 if (asoc->mapping_array[limit - 1] != 0) {
1222 SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1223 for (i = 0; i < limit; i++) {
1224 SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1228 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1229 if (asoc->nr_mapping_array[limit - 1]) {
1233 SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1234 for (i = 0; i < limit; i++) {
1235 SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1242 sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1244 /* mapping array needs to grow */
1245 uint8_t *new_array1, *new_array2;
1248 new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1249 SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1250 SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1251 if ((new_array1 == NULL) || (new_array2 == NULL)) {
1252 /* can't get more, forget it */
1253 SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1255 SCTP_FREE(new_array1, SCTP_M_MAP);
1258 SCTP_FREE(new_array2, SCTP_M_MAP);
1262 memset(new_array1, 0, new_size);
1263 memset(new_array2, 0, new_size);
1264 memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1265 memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1266 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1267 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1268 asoc->mapping_array = new_array1;
1269 asoc->nr_mapping_array = new_array2;
1270 asoc->mapping_array_size = new_size;
1276 sctp_iterator_work(struct sctp_iterator *it)
1278 int iteration_count = 0;
1281 struct sctp_inpcb *tinp;
1283 SCTP_INP_INFO_RLOCK();
1284 SCTP_ITERATOR_LOCK();
1285 sctp_it_ctl.cur_it = it;
1287 SCTP_INP_RLOCK(it->inp);
1288 SCTP_INP_DECR_REF(it->inp);
1290 if (it->inp == NULL) {
1291 /* iterator is complete */
1293 sctp_it_ctl.cur_it = NULL;
1294 SCTP_ITERATOR_UNLOCK();
1295 SCTP_INP_INFO_RUNLOCK();
1296 if (it->function_atend != NULL) {
1297 (*it->function_atend) (it->pointer, it->val);
1299 SCTP_FREE(it, SCTP_M_ITER);
1306 SCTP_INP_RLOCK(it->inp);
1308 while (((it->pcb_flags) &&
1309 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1310 ((it->pcb_features) &&
1311 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1312 /* endpoint flags or features don't match, so keep looking */
1313 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1314 SCTP_INP_RUNLOCK(it->inp);
1315 goto done_with_iterator;
1318 it->inp = LIST_NEXT(it->inp, sctp_list);
1319 SCTP_INP_RUNLOCK(tinp);
1320 if (it->inp == NULL) {
1321 goto done_with_iterator;
1323 SCTP_INP_RLOCK(it->inp);
1325 /* now go through each assoc which is in the desired state */
1326 if (it->done_current_ep == 0) {
1327 if (it->function_inp != NULL)
1328 inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1329 it->done_current_ep = 1;
1331 if (it->stcb == NULL) {
1332 /* run the per instance function */
1333 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1335 if ((inp_skip) || it->stcb == NULL) {
1336 if (it->function_inp_end != NULL) {
1337 inp_skip = (*it->function_inp_end) (it->inp,
1341 SCTP_INP_RUNLOCK(it->inp);
1345 SCTP_TCB_LOCK(it->stcb);
1346 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1347 /* not in the right state... keep looking */
1348 SCTP_TCB_UNLOCK(it->stcb);
1351 /* see if we have limited out the iterator loop */
1353 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1354 /* Pause to let others grab the lock */
1355 atomic_add_int(&it->stcb->asoc.refcnt, 1);
1356 SCTP_TCB_UNLOCK(it->stcb);
1357 SCTP_INP_INCR_REF(it->inp);
1358 SCTP_INP_RUNLOCK(it->inp);
1359 SCTP_ITERATOR_UNLOCK();
1360 SCTP_INP_INFO_RUNLOCK();
1361 SCTP_INP_INFO_RLOCK();
1362 SCTP_ITERATOR_LOCK();
1363 if (sctp_it_ctl.iterator_flags) {
1364 /* We won't be staying here */
1365 SCTP_INP_DECR_REF(it->inp);
1366 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1367 if (sctp_it_ctl.iterator_flags &
1368 SCTP_ITERATOR_STOP_CUR_IT) {
1369 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1370 goto done_with_iterator;
1372 if (sctp_it_ctl.iterator_flags &
1373 SCTP_ITERATOR_STOP_CUR_INP) {
1374 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1377 /* If we reach here huh? */
1378 SCTP_PRINTF("Unknown it ctl flag %x\n",
1379 sctp_it_ctl.iterator_flags);
1380 sctp_it_ctl.iterator_flags = 0;
1382 SCTP_INP_RLOCK(it->inp);
1383 SCTP_INP_DECR_REF(it->inp);
1384 SCTP_TCB_LOCK(it->stcb);
1385 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1386 iteration_count = 0;
1388 /* run function on this one */
1389 (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1392 * we lie here, it really needs to have its own type but
1393 * first I must verify that this won't effect things :-0
1395 if (it->no_chunk_output == 0)
1396 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1398 SCTP_TCB_UNLOCK(it->stcb);
1400 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1401 if (it->stcb == NULL) {
1402 /* Run last function */
1403 if (it->function_inp_end != NULL) {
1404 inp_skip = (*it->function_inp_end) (it->inp,
1410 SCTP_INP_RUNLOCK(it->inp);
1412 /* done with all assocs on this endpoint, move on to next endpoint */
1413 it->done_current_ep = 0;
1414 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1417 it->inp = LIST_NEXT(it->inp, sctp_list);
1419 if (it->inp == NULL) {
1420 goto done_with_iterator;
1422 goto select_a_new_ep;
1426 sctp_iterator_worker(void)
1428 struct sctp_iterator *it, *nit;
1430 /* This function is called with the WQ lock in place */
1432 sctp_it_ctl.iterator_running = 1;
1433 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
1434 /* now lets work on this one */
1435 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
1436 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1437 CURVNET_SET(it->vn);
1438 sctp_iterator_work(it);
1440 SCTP_IPI_ITERATOR_WQ_LOCK();
1441 /* sa_ignore FREED_MEMORY */
1443 sctp_it_ctl.iterator_running = 0;
1449 sctp_handle_addr_wq(void)
1451 /* deal with the ADDR wq from the rtsock calls */
1452 struct sctp_laddr *wi, *nwi;
1453 struct sctp_asconf_iterator *asc;
1455 SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1456 sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
1458 /* Try later, no memory */
1459 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1460 (struct sctp_inpcb *)NULL,
1461 (struct sctp_tcb *)NULL,
1462 (struct sctp_nets *)NULL);
1465 LIST_INIT(&asc->list_of_work);
1468 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
1469 LIST_REMOVE(wi, sctp_nxt_addr);
1470 LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
1474 if (asc->cnt == 0) {
1475 SCTP_FREE(asc, SCTP_M_ASC_IT);
1479 ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
1480 sctp_asconf_iterator_stcb,
1481 NULL, /* No ep end for boundall */
1482 SCTP_PCB_FLAGS_BOUNDALL,
1483 SCTP_PCB_ANY_FEATURES,
1484 SCTP_ASOC_ANY_STATE,
1486 sctp_asconf_iterator_end, NULL, 0);
1488 SCTP_PRINTF("Failed to initiate iterator for handle_addr_wq\n");
1490 * Freeing if we are stopping or put back on the
1493 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
1494 sctp_asconf_iterator_end(asc, 0);
1496 LIST_FOREACH(wi, &asc->list_of_work, sctp_nxt_addr) {
1497 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
1499 SCTP_FREE(asc, SCTP_M_ASC_IT);
1506 sctp_timeout_handler(void *t)
1508 struct sctp_inpcb *inp;
1509 struct sctp_tcb *stcb;
1510 struct sctp_nets *net;
1511 struct sctp_timer *tmr;
1512 struct mbuf *op_err;
1513 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1519 tmr = (struct sctp_timer *)t;
1520 inp = (struct sctp_inpcb *)tmr->ep;
1521 stcb = (struct sctp_tcb *)tmr->tcb;
1522 net = (struct sctp_nets *)tmr->net;
1523 CURVNET_SET((struct vnet *)tmr->vnet);
1526 #ifdef SCTP_AUDITING_ENABLED
1527 sctp_audit_log(0xF0, (uint8_t)tmr->type);
1528 sctp_auditing(3, inp, stcb, net);
1531 /* sanity checks... */
1532 if (tmr->self != (void *)tmr) {
1534 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1540 tmr->stopped_from = 0xa001;
1541 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1543 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1549 tmr->stopped_from = 0xa002;
1550 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1554 /* if this is an iterator timeout, get the struct and clear inp */
1555 tmr->stopped_from = 0xa003;
1557 SCTP_INP_INCR_REF(inp);
1558 if ((inp->sctp_socket == NULL) &&
1559 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1560 (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1561 (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1562 (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1563 (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1564 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1565 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1566 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1567 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))) {
1568 SCTP_INP_DECR_REF(inp);
1573 tmr->stopped_from = 0xa004;
1575 atomic_add_int(&stcb->asoc.refcnt, 1);
1576 if (stcb->asoc.state == 0) {
1577 atomic_add_int(&stcb->asoc.refcnt, -1);
1579 SCTP_INP_DECR_REF(inp);
1586 tmr->stopped_from = 0xa005;
1587 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", type);
1588 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1590 SCTP_INP_DECR_REF(inp);
1593 atomic_add_int(&stcb->asoc.refcnt, -1);
1598 tmr->stopped_from = 0xa006;
1601 SCTP_TCB_LOCK(stcb);
1602 atomic_add_int(&stcb->asoc.refcnt, -1);
1603 if ((type != SCTP_TIMER_TYPE_ASOCKILL) &&
1604 ((stcb->asoc.state == 0) ||
1605 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1606 SCTP_TCB_UNLOCK(stcb);
1608 SCTP_INP_DECR_REF(inp);
1613 } else if (inp != NULL) {
1614 if (type != SCTP_TIMER_TYPE_INPKILL) {
1615 SCTP_INP_WLOCK(inp);
1618 SCTP_WQ_ADDR_LOCK();
1620 /* record in stopped what t-o occurred */
1621 tmr->stopped_from = type;
1623 /* mark as being serviced now */
1624 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1626 * Callout has been rescheduled.
1630 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1632 * Not active, so no action.
1636 SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1638 /* call the handler for the appropriate timer type */
1640 case SCTP_TIMER_TYPE_ADDR_WQ:
1641 sctp_handle_addr_wq();
1643 case SCTP_TIMER_TYPE_SEND:
1644 if ((stcb == NULL) || (inp == NULL)) {
1647 SCTP_STAT_INCR(sctps_timodata);
1648 stcb->asoc.timodata++;
1649 stcb->asoc.num_send_timers_up--;
1650 if (stcb->asoc.num_send_timers_up < 0) {
1651 stcb->asoc.num_send_timers_up = 0;
1653 SCTP_TCB_LOCK_ASSERT(stcb);
1654 if (sctp_t3rxt_timer(inp, stcb, net)) {
1655 /* no need to unlock on tcb its gone */
1659 SCTP_TCB_LOCK_ASSERT(stcb);
1660 #ifdef SCTP_AUDITING_ENABLED
1661 sctp_auditing(4, inp, stcb, net);
1663 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1664 if ((stcb->asoc.num_send_timers_up == 0) &&
1665 (stcb->asoc.sent_queue_cnt > 0)) {
1666 struct sctp_tmit_chunk *chk;
1669 * safeguard. If there on some on the sent queue
1670 * somewhere but no timers running something is
1671 * wrong... so we start a timer on the first chunk
1672 * on the send queue on whatever net it is sent to.
1674 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1675 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1679 case SCTP_TIMER_TYPE_INIT:
1680 if ((stcb == NULL) || (inp == NULL)) {
1683 SCTP_STAT_INCR(sctps_timoinit);
1684 stcb->asoc.timoinit++;
1685 if (sctp_t1init_timer(inp, stcb, net)) {
1686 /* no need to unlock on tcb its gone */
1689 /* We do output but not here */
1692 case SCTP_TIMER_TYPE_RECV:
1693 if ((stcb == NULL) || (inp == NULL)) {
1696 SCTP_STAT_INCR(sctps_timosack);
1697 stcb->asoc.timosack++;
1698 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1699 #ifdef SCTP_AUDITING_ENABLED
1700 sctp_auditing(4, inp, stcb, net);
1702 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1704 case SCTP_TIMER_TYPE_SHUTDOWN:
1705 if ((stcb == NULL) || (inp == NULL)) {
1708 if (sctp_shutdown_timer(inp, stcb, net)) {
1709 /* no need to unlock on tcb its gone */
1712 SCTP_STAT_INCR(sctps_timoshutdown);
1713 stcb->asoc.timoshutdown++;
1714 #ifdef SCTP_AUDITING_ENABLED
1715 sctp_auditing(4, inp, stcb, net);
1717 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1719 case SCTP_TIMER_TYPE_HEARTBEAT:
1720 if ((stcb == NULL) || (inp == NULL) || (net == NULL)) {
1723 SCTP_STAT_INCR(sctps_timoheartbeat);
1724 stcb->asoc.timoheartbeat++;
1725 if (sctp_heartbeat_timer(inp, stcb, net)) {
1726 /* no need to unlock on tcb its gone */
1729 #ifdef SCTP_AUDITING_ENABLED
1730 sctp_auditing(4, inp, stcb, net);
1732 if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1733 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1734 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1737 case SCTP_TIMER_TYPE_COOKIE:
1738 if ((stcb == NULL) || (inp == NULL)) {
1741 if (sctp_cookie_timer(inp, stcb, net)) {
1742 /* no need to unlock on tcb its gone */
1745 SCTP_STAT_INCR(sctps_timocookie);
1746 stcb->asoc.timocookie++;
1747 #ifdef SCTP_AUDITING_ENABLED
1748 sctp_auditing(4, inp, stcb, net);
1751 * We consider T3 and Cookie timer pretty much the same with
1752 * respect to where from in chunk_output.
1754 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1756 case SCTP_TIMER_TYPE_NEWCOOKIE:
1764 SCTP_STAT_INCR(sctps_timosecret);
1765 (void)SCTP_GETTIME_TIMEVAL(&tv);
1766 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1767 inp->sctp_ep.last_secret_number =
1768 inp->sctp_ep.current_secret_number;
1769 inp->sctp_ep.current_secret_number++;
1770 if (inp->sctp_ep.current_secret_number >=
1771 SCTP_HOW_MANY_SECRETS) {
1772 inp->sctp_ep.current_secret_number = 0;
1774 secret = (int)inp->sctp_ep.current_secret_number;
1775 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1776 inp->sctp_ep.secret_key[secret][i] =
1777 sctp_select_initial_TSN(&inp->sctp_ep);
1779 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1783 case SCTP_TIMER_TYPE_PATHMTURAISE:
1784 if ((stcb == NULL) || (inp == NULL)) {
1787 SCTP_STAT_INCR(sctps_timopathmtu);
1788 sctp_pathmtu_timer(inp, stcb, net);
1791 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1792 if ((stcb == NULL) || (inp == NULL)) {
1795 if (sctp_shutdownack_timer(inp, stcb, net)) {
1796 /* no need to unlock on tcb its gone */
1799 SCTP_STAT_INCR(sctps_timoshutdownack);
1800 stcb->asoc.timoshutdownack++;
1801 #ifdef SCTP_AUDITING_ENABLED
1802 sctp_auditing(4, inp, stcb, net);
1804 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1806 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1807 if ((stcb == NULL) || (inp == NULL)) {
1810 SCTP_STAT_INCR(sctps_timoshutdownguard);
1811 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
1812 "Shutdown guard timer expired");
1813 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
1814 /* no need to unlock on tcb its gone */
1817 case SCTP_TIMER_TYPE_STRRESET:
1818 if ((stcb == NULL) || (inp == NULL)) {
1821 if (sctp_strreset_timer(inp, stcb, net)) {
1822 /* no need to unlock on tcb its gone */
1825 SCTP_STAT_INCR(sctps_timostrmrst);
1826 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1828 case SCTP_TIMER_TYPE_ASCONF:
1829 if ((stcb == NULL) || (inp == NULL)) {
1832 if (sctp_asconf_timer(inp, stcb, net)) {
1833 /* no need to unlock on tcb its gone */
1836 SCTP_STAT_INCR(sctps_timoasconf);
1837 #ifdef SCTP_AUDITING_ENABLED
1838 sctp_auditing(4, inp, stcb, net);
1840 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1842 case SCTP_TIMER_TYPE_PRIM_DELETED:
1843 if ((stcb == NULL) || (inp == NULL)) {
1846 sctp_delete_prim_timer(inp, stcb, net);
1847 SCTP_STAT_INCR(sctps_timodelprim);
1850 case SCTP_TIMER_TYPE_AUTOCLOSE:
1851 if ((stcb == NULL) || (inp == NULL)) {
1854 SCTP_STAT_INCR(sctps_timoautoclose);
1855 sctp_autoclose_timer(inp, stcb, net);
1856 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1859 case SCTP_TIMER_TYPE_ASOCKILL:
1860 if ((stcb == NULL) || (inp == NULL)) {
1863 SCTP_STAT_INCR(sctps_timoassockill);
1864 /* Can we free it yet? */
1865 SCTP_INP_DECR_REF(inp);
1866 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
1867 SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1868 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1869 so = SCTP_INP_SO(inp);
1870 atomic_add_int(&stcb->asoc.refcnt, 1);
1871 SCTP_TCB_UNLOCK(stcb);
1872 SCTP_SOCKET_LOCK(so, 1);
1873 SCTP_TCB_LOCK(stcb);
1874 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1876 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
1877 SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1878 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1879 SCTP_SOCKET_UNLOCK(so, 1);
1882 * free asoc, always unlocks (or destroy's) so prevent
1883 * duplicate unlock or unlock of a free mtx :-0
1887 case SCTP_TIMER_TYPE_INPKILL:
1888 SCTP_STAT_INCR(sctps_timoinpkill);
1893 * special case, take away our increment since WE are the
1896 SCTP_INP_DECR_REF(inp);
1897 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
1898 SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1899 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
1900 SCTP_CALLED_FROM_INPKILL_TIMER);
1904 SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1908 #ifdef SCTP_AUDITING_ENABLED
1909 sctp_audit_log(0xF1, (uint8_t)type);
1911 sctp_auditing(5, inp, stcb, net);
1913 if ((did_output) && stcb) {
1915 * Now we need to clean up the control chunk chain if an
1916 * ECNE is on it. It must be marked as UNSENT again so next
1917 * call will continue to send it until such time that we get
1918 * a CWR, to remove it. It is, however, less likely that we
1919 * will find a ecn echo on the chain though.
1921 sctp_fix_ecn_echo(&stcb->asoc);
1925 SCTP_TCB_UNLOCK(stcb);
1926 } else if (inp != NULL) {
1927 SCTP_INP_WUNLOCK(inp);
1929 SCTP_WQ_ADDR_UNLOCK();
1934 SCTP_INP_DECR_REF(inp);
1937 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type = %d)\n", type);
1942 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1943 struct sctp_nets *net)
1946 struct sctp_timer *tmr;
1948 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1953 SCTP_TCB_LOCK_ASSERT(stcb);
1956 case SCTP_TIMER_TYPE_ADDR_WQ:
1957 /* Only 1 tick away :-) */
1958 tmr = &SCTP_BASE_INFO(addr_wq_timer);
1959 to_ticks = SCTP_ADDRESS_TICK_DELAY;
1961 case SCTP_TIMER_TYPE_SEND:
1962 /* Here we use the RTO timer */
1966 if ((stcb == NULL) || (net == NULL)) {
1969 tmr = &net->rxt_timer;
1970 if (net->RTO == 0) {
1971 rto_val = stcb->asoc.initial_rto;
1975 to_ticks = MSEC_TO_TICKS(rto_val);
1978 case SCTP_TIMER_TYPE_INIT:
1980 * Here we use the INIT timer default usually about 1
1983 if ((stcb == NULL) || (net == NULL)) {
1986 tmr = &net->rxt_timer;
1987 if (net->RTO == 0) {
1988 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1990 to_ticks = MSEC_TO_TICKS(net->RTO);
1993 case SCTP_TIMER_TYPE_RECV:
1995 * Here we use the Delayed-Ack timer value from the inp
1996 * ususually about 200ms.
2001 tmr = &stcb->asoc.dack_timer;
2002 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
2004 case SCTP_TIMER_TYPE_SHUTDOWN:
2005 /* Here we use the RTO of the destination. */
2006 if ((stcb == NULL) || (net == NULL)) {
2009 if (net->RTO == 0) {
2010 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2012 to_ticks = MSEC_TO_TICKS(net->RTO);
2014 tmr = &net->rxt_timer;
2016 case SCTP_TIMER_TYPE_HEARTBEAT:
2018 * the net is used here so that we can add in the RTO. Even
2019 * though we use a different timer. We also add the HB timer
2020 * PLUS a random jitter.
2022 if ((stcb == NULL) || (net == NULL)) {
2028 if ((net->dest_state & SCTP_ADDR_NOHB) &&
2029 !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
2032 if (net->RTO == 0) {
2033 to_ticks = stcb->asoc.initial_rto;
2035 to_ticks = net->RTO;
2037 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2038 jitter = rndval % to_ticks;
2039 if (jitter >= (to_ticks >> 1)) {
2040 to_ticks = to_ticks + (jitter - (to_ticks >> 1));
2042 to_ticks = to_ticks - jitter;
2044 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2045 !(net->dest_state & SCTP_ADDR_PF)) {
2046 to_ticks += net->heart_beat_delay;
2049 * Now we must convert the to_ticks that are now in
2052 to_ticks = MSEC_TO_TICKS(to_ticks);
2053 tmr = &net->hb_timer;
2056 case SCTP_TIMER_TYPE_COOKIE:
2058 * Here we can use the RTO timer from the network since one
2059 * RTT was compelete. If a retran happened then we will be
2060 * using the RTO initial value.
2062 if ((stcb == NULL) || (net == NULL)) {
2065 if (net->RTO == 0) {
2066 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2068 to_ticks = MSEC_TO_TICKS(net->RTO);
2070 tmr = &net->rxt_timer;
2072 case SCTP_TIMER_TYPE_NEWCOOKIE:
2074 * nothing needed but the endpoint here ususually about 60
2077 tmr = &inp->sctp_ep.signature_change;
2078 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2080 case SCTP_TIMER_TYPE_ASOCKILL:
2084 tmr = &stcb->asoc.strreset_timer;
2085 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2087 case SCTP_TIMER_TYPE_INPKILL:
2089 * The inp is setup to die. We re-use the signature_chage
2090 * timer since that has stopped and we are in the GONE
2093 tmr = &inp->sctp_ep.signature_change;
2094 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2096 case SCTP_TIMER_TYPE_PATHMTURAISE:
2098 * Here we use the value found in the EP for PMTU ususually
2101 if ((stcb == NULL) || (net == NULL)) {
2104 if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2107 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2108 tmr = &net->pmtu_timer;
2110 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2111 /* Here we use the RTO of the destination */
2112 if ((stcb == NULL) || (net == NULL)) {
2115 if (net->RTO == 0) {
2116 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2118 to_ticks = MSEC_TO_TICKS(net->RTO);
2120 tmr = &net->rxt_timer;
2122 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2124 * Here we use the endpoints shutdown guard timer usually
2130 if (inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] == 0) {
2131 to_ticks = 5 * MSEC_TO_TICKS(stcb->asoc.maxrto);
2133 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2135 tmr = &stcb->asoc.shut_guard_timer;
2137 case SCTP_TIMER_TYPE_STRRESET:
2139 * Here the timer comes from the stcb but its value is from
2142 if ((stcb == NULL) || (net == NULL)) {
2145 if (net->RTO == 0) {
2146 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2148 to_ticks = MSEC_TO_TICKS(net->RTO);
2150 tmr = &stcb->asoc.strreset_timer;
2152 case SCTP_TIMER_TYPE_ASCONF:
2154 * Here the timer comes from the stcb but its value is from
2157 if ((stcb == NULL) || (net == NULL)) {
2160 if (net->RTO == 0) {
2161 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2163 to_ticks = MSEC_TO_TICKS(net->RTO);
2165 tmr = &stcb->asoc.asconf_timer;
2167 case SCTP_TIMER_TYPE_PRIM_DELETED:
2168 if ((stcb == NULL) || (net != NULL)) {
2171 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2172 tmr = &stcb->asoc.delete_prim_timer;
2174 case SCTP_TIMER_TYPE_AUTOCLOSE:
2178 if (stcb->asoc.sctp_autoclose_ticks == 0) {
2180 * Really an error since stcb is NOT set to
2185 to_ticks = stcb->asoc.sctp_autoclose_ticks;
2186 tmr = &stcb->asoc.autoclose_timer;
2189 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2194 if ((to_ticks <= 0) || (tmr == NULL)) {
2195 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2196 __func__, t_type, to_ticks, (void *)tmr);
2199 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2201 * we do NOT allow you to have it already running. if it is
2202 * we leave the current one up unchanged
2206 /* At this point we can proceed */
2207 if (t_type == SCTP_TIMER_TYPE_SEND) {
2208 stcb->asoc.num_send_timers_up++;
2210 tmr->stopped_from = 0;
2212 tmr->ep = (void *)inp;
2213 tmr->tcb = (void *)stcb;
2214 tmr->net = (void *)net;
2215 tmr->self = (void *)tmr;
2216 tmr->vnet = (void *)curvnet;
2217 tmr->ticks = sctp_get_tick_count();
2218 (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2223 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2224 struct sctp_nets *net, uint32_t from)
2226 struct sctp_timer *tmr;
2228 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2234 SCTP_TCB_LOCK_ASSERT(stcb);
2237 case SCTP_TIMER_TYPE_ADDR_WQ:
2238 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2240 case SCTP_TIMER_TYPE_SEND:
2241 if ((stcb == NULL) || (net == NULL)) {
2244 tmr = &net->rxt_timer;
2246 case SCTP_TIMER_TYPE_INIT:
2247 if ((stcb == NULL) || (net == NULL)) {
2250 tmr = &net->rxt_timer;
2252 case SCTP_TIMER_TYPE_RECV:
2256 tmr = &stcb->asoc.dack_timer;
2258 case SCTP_TIMER_TYPE_SHUTDOWN:
2259 if ((stcb == NULL) || (net == NULL)) {
2262 tmr = &net->rxt_timer;
2264 case SCTP_TIMER_TYPE_HEARTBEAT:
2265 if ((stcb == NULL) || (net == NULL)) {
2268 tmr = &net->hb_timer;
2270 case SCTP_TIMER_TYPE_COOKIE:
2271 if ((stcb == NULL) || (net == NULL)) {
2274 tmr = &net->rxt_timer;
2276 case SCTP_TIMER_TYPE_NEWCOOKIE:
2277 /* nothing needed but the endpoint here */
2278 tmr = &inp->sctp_ep.signature_change;
2280 * We re-use the newcookie timer for the INP kill timer. We
2281 * must assure that we do not kill it by accident.
2284 case SCTP_TIMER_TYPE_ASOCKILL:
2286 * Stop the asoc kill timer.
2291 tmr = &stcb->asoc.strreset_timer;
2294 case SCTP_TIMER_TYPE_INPKILL:
2296 * The inp is setup to die. We re-use the signature_chage
2297 * timer since that has stopped and we are in the GONE
2300 tmr = &inp->sctp_ep.signature_change;
2302 case SCTP_TIMER_TYPE_PATHMTURAISE:
2303 if ((stcb == NULL) || (net == NULL)) {
2306 tmr = &net->pmtu_timer;
2308 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2309 if ((stcb == NULL) || (net == NULL)) {
2312 tmr = &net->rxt_timer;
2314 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2318 tmr = &stcb->asoc.shut_guard_timer;
2320 case SCTP_TIMER_TYPE_STRRESET:
2324 tmr = &stcb->asoc.strreset_timer;
2326 case SCTP_TIMER_TYPE_ASCONF:
2330 tmr = &stcb->asoc.asconf_timer;
2332 case SCTP_TIMER_TYPE_PRIM_DELETED:
2336 tmr = &stcb->asoc.delete_prim_timer;
2338 case SCTP_TIMER_TYPE_AUTOCLOSE:
2342 tmr = &stcb->asoc.autoclose_timer;
2345 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2352 if ((tmr->type != t_type) && tmr->type) {
2354 * Ok we have a timer that is under joint use. Cookie timer
2355 * per chance with the SEND timer. We therefore are NOT
2356 * running the timer that the caller wants stopped. So just
2361 if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2362 stcb->asoc.num_send_timers_up--;
2363 if (stcb->asoc.num_send_timers_up < 0) {
2364 stcb->asoc.num_send_timers_up = 0;
2368 tmr->stopped_from = from;
2369 (void)SCTP_OS_TIMER_STOP(&tmr->timer);
2374 sctp_calculate_len(struct mbuf *m)
2381 tlen += SCTP_BUF_LEN(at);
2382 at = SCTP_BUF_NEXT(at);
2388 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2389 struct sctp_association *asoc, uint32_t mtu)
2392 * Reset the P-MTU size on this association, this involves changing
2393 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2394 * allow the DF flag to be cleared.
2396 struct sctp_tmit_chunk *chk;
2397 unsigned int eff_mtu, ovh;
2399 asoc->smallest_mtu = mtu;
2400 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2401 ovh = SCTP_MIN_OVERHEAD;
2403 ovh = SCTP_MIN_V4_OVERHEAD;
2405 eff_mtu = mtu - ovh;
2406 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2407 if (chk->send_size > eff_mtu) {
2408 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2411 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2412 if (chk->send_size > eff_mtu) {
2413 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2420 * given an association and starting time of the current RTT period return
2421 * RTO in number of msecs net should point to the current network
2425 sctp_calculate_rto(struct sctp_tcb *stcb,
2426 struct sctp_association *asoc,
2427 struct sctp_nets *net,
2428 struct timeval *old,
2432 * given an association and the starting time of the current RTT
2433 * period (in value1/value2) return RTO in number of msecs.
2435 int32_t rtt; /* RTT in ms */
2437 int first_measure = 0;
2440 /************************/
2441 /* 1. calculate new RTT */
2442 /************************/
2443 /* get the current time */
2444 if (stcb->asoc.use_precise_time) {
2445 (void)SCTP_GETPTIME_TIMEVAL(&now);
2447 (void)SCTP_GETTIME_TIMEVAL(&now);
2449 timevalsub(&now, old);
2450 /* store the current RTT in us */
2451 net->rtt = (uint64_t)1000000 *(uint64_t)now.tv_sec +
2452 (uint64_t)now.tv_usec;
2454 /* compute rtt in ms */
2455 rtt = (int32_t)(net->rtt / 1000);
2456 if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2458 * Tell the CC module that a new update has just occurred
2461 (*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2464 * Do we need to determine the lan? We do this only on sacks i.e.
2465 * RTT being determined from data not non-data (HB/INIT->INITACK).
2467 if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2468 (net->lan_type == SCTP_LAN_UNKNOWN)) {
2469 if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2470 net->lan_type = SCTP_LAN_INTERNET;
2472 net->lan_type = SCTP_LAN_LOCAL;
2475 /***************************/
2476 /* 2. update RTTVAR & SRTT */
2477 /***************************/
2479 * Compute the scaled average lastsa and the
2480 * scaled variance lastsv as described in van Jacobson
2481 * Paper "Congestion Avoidance and Control", Annex A.
2483 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
2484 * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar
2486 if (net->RTO_measured) {
2487 rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2492 rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2494 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2495 rto_logging(net, SCTP_LOG_RTTVAR);
2498 /* First RTO measurment */
2499 net->RTO_measured = 1;
2501 net->lastsa = rtt << SCTP_RTT_SHIFT;
2502 net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2503 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2504 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2507 if (net->lastsv == 0) {
2508 net->lastsv = SCTP_CLOCK_GRANULARITY;
2510 new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2511 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2512 (stcb->asoc.sat_network_lockout == 0)) {
2513 stcb->asoc.sat_network = 1;
2514 } else if ((!first_measure) && stcb->asoc.sat_network) {
2515 stcb->asoc.sat_network = 0;
2516 stcb->asoc.sat_network_lockout = 1;
2518 /* bound it, per C6/C7 in Section 5.3.1 */
2519 if (new_rto < stcb->asoc.minrto) {
2520 new_rto = stcb->asoc.minrto;
2522 if (new_rto > stcb->asoc.maxrto) {
2523 new_rto = stcb->asoc.maxrto;
2525 /* we are now returning the RTO */
2530 * return a pointer to a contiguous piece of data from the given mbuf chain
2531 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2532 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2533 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2536 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t *in_ptr)
2542 if ((off < 0) || (len <= 0))
2545 /* find the desired start location */
2546 while ((m != NULL) && (off > 0)) {
2547 if (off < SCTP_BUF_LEN(m))
2549 off -= SCTP_BUF_LEN(m);
2550 m = SCTP_BUF_NEXT(m);
2555 /* is the current mbuf large enough (eg. contiguous)? */
2556 if ((SCTP_BUF_LEN(m) - off) >= len) {
2557 return (mtod(m, caddr_t)+off);
2559 /* else, it spans more than one mbuf, so save a temp copy... */
2560 while ((m != NULL) && (len > 0)) {
2561 count = min(SCTP_BUF_LEN(m) - off, len);
2562 memcpy(ptr, mtod(m, caddr_t)+off, count);
2566 m = SCTP_BUF_NEXT(m);
2568 if ((m == NULL) && (len > 0))
2571 return ((caddr_t)in_ptr);
2577 struct sctp_paramhdr *
2578 sctp_get_next_param(struct mbuf *m,
2580 struct sctp_paramhdr *pull,
2583 /* This just provides a typed signature to Peter's Pull routine */
2584 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2590 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2592 struct mbuf *m_last;
2598 if (padlen <= M_TRAILINGSPACE(m)) {
2600 * The easy way. We hope the majority of the time we hit
2605 /* Hard way we must grow the mbuf chain */
2606 m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
2607 if (m_last == NULL) {
2610 SCTP_BUF_LEN(m_last) = 0;
2611 SCTP_BUF_NEXT(m_last) = NULL;
2612 SCTP_BUF_NEXT(m) = m_last;
2614 dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
2615 SCTP_BUF_LEN(m_last) += padlen;
2616 memset(dp, 0, padlen);
2621 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2623 /* find the last mbuf in chain and pad it */
2626 if (last_mbuf != NULL) {
2627 return (sctp_add_pad_tombuf(last_mbuf, padval));
2629 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
2630 if (SCTP_BUF_NEXT(m_at) == NULL) {
2631 return (sctp_add_pad_tombuf(m_at, padval));
2639 sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
2640 uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
2641 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2646 struct mbuf *m_notify;
2647 struct sctp_assoc_change *sac;
2648 struct sctp_queued_to_read *control;
2649 unsigned int notif_len;
2652 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2659 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2660 notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
2661 if (abort != NULL) {
2662 abort_len = ntohs(abort->ch.chunk_length);
2664 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
2667 if (abort_len > SCTP_CHUNK_BUFFER_SIZE) {
2668 abort_len = SCTP_CHUNK_BUFFER_SIZE;
2673 if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2674 notif_len += SCTP_ASSOC_SUPPORTS_MAX;
2675 } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2676 notif_len += abort_len;
2678 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2679 if (m_notify == NULL) {
2680 /* Retry with smaller value. */
2681 notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
2682 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2683 if (m_notify == NULL) {
2687 SCTP_BUF_NEXT(m_notify) = NULL;
2688 sac = mtod(m_notify, struct sctp_assoc_change *);
2689 memset(sac, 0, notif_len);
2690 sac->sac_type = SCTP_ASSOC_CHANGE;
2692 sac->sac_length = sizeof(struct sctp_assoc_change);
2693 sac->sac_state = state;
2694 sac->sac_error = error;
2695 /* XXX verify these stream counts */
2696 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2697 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2698 sac->sac_assoc_id = sctp_get_associd(stcb);
2699 if (notif_len > sizeof(struct sctp_assoc_change)) {
2700 if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2702 if (stcb->asoc.prsctp_supported == 1) {
2703 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
2705 if (stcb->asoc.auth_supported == 1) {
2706 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
2708 if (stcb->asoc.asconf_supported == 1) {
2709 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
2711 if (stcb->asoc.idata_supported == 1) {
2712 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_INTERLEAVING;
2714 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
2715 if (stcb->asoc.reconfig_supported == 1) {
2716 sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
2718 sac->sac_length += i;
2719 } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2720 memcpy(sac->sac_info, abort, abort_len);
2721 sac->sac_length += abort_len;
2724 SCTP_BUF_LEN(m_notify) = sac->sac_length;
2725 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2726 0, 0, stcb->asoc.context, 0, 0, 0,
2728 if (control != NULL) {
2729 control->length = SCTP_BUF_LEN(m_notify);
2730 control->spec_flags = M_NOTIFICATION;
2731 /* not that we need this */
2732 control->tail_mbuf = m_notify;
2733 sctp_add_to_readq(stcb->sctp_ep, stcb,
2735 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2738 sctp_m_freem(m_notify);
2742 * For 1-to-1 style sockets, we send up and error when an ABORT
2746 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2747 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2748 ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2749 SOCK_LOCK(stcb->sctp_socket);
2751 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
2752 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
2753 stcb->sctp_socket->so_error = ECONNREFUSED;
2755 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
2756 stcb->sctp_socket->so_error = ECONNRESET;
2759 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2760 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2761 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
2762 stcb->sctp_socket->so_error = ETIMEDOUT;
2764 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
2765 stcb->sctp_socket->so_error = ECONNABORTED;
2768 SOCK_UNLOCK(stcb->sctp_socket);
2770 /* Wake ANY sleepers */
2771 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2772 so = SCTP_INP_SO(stcb->sctp_ep);
2774 atomic_add_int(&stcb->asoc.refcnt, 1);
2775 SCTP_TCB_UNLOCK(stcb);
2776 SCTP_SOCKET_LOCK(so, 1);
2777 SCTP_TCB_LOCK(stcb);
2778 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2779 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2780 SCTP_SOCKET_UNLOCK(so, 1);
2785 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2786 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2787 ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2788 socantrcvmore(stcb->sctp_socket);
2790 sorwakeup(stcb->sctp_socket);
2791 sowwakeup(stcb->sctp_socket);
2792 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2794 SCTP_SOCKET_UNLOCK(so, 1);
2800 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2801 struct sockaddr *sa, uint32_t error, int so_locked
2802 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2807 struct mbuf *m_notify;
2808 struct sctp_paddr_change *spc;
2809 struct sctp_queued_to_read *control;
2811 if ((stcb == NULL) ||
2812 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2813 /* event not enabled */
2816 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
2817 if (m_notify == NULL)
2819 SCTP_BUF_LEN(m_notify) = 0;
2820 spc = mtod(m_notify, struct sctp_paddr_change *);
2821 memset(spc, 0, sizeof(struct sctp_paddr_change));
2822 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2824 spc->spc_length = sizeof(struct sctp_paddr_change);
2825 switch (sa->sa_family) {
2829 if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2830 in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
2831 (struct sockaddr_in6 *)&spc->spc_aaddr);
2833 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2836 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2843 struct sockaddr_in6 *sin6;
2845 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2847 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2848 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2849 if (sin6->sin6_scope_id == 0) {
2850 /* recover scope_id for user */
2851 (void)sa6_recoverscope(sin6);
2853 /* clear embedded scope_id for user */
2854 in6_clearscope(&sin6->sin6_addr);
2864 spc->spc_state = state;
2865 spc->spc_error = error;
2866 spc->spc_assoc_id = sctp_get_associd(stcb);
2868 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2869 SCTP_BUF_NEXT(m_notify) = NULL;
2871 /* append to socket */
2872 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2873 0, 0, stcb->asoc.context, 0, 0, 0,
2875 if (control == NULL) {
2877 sctp_m_freem(m_notify);
2880 control->length = SCTP_BUF_LEN(m_notify);
2881 control->spec_flags = M_NOTIFICATION;
2882 /* not that we need this */
2883 control->tail_mbuf = m_notify;
2884 sctp_add_to_readq(stcb->sctp_ep, stcb,
2886 &stcb->sctp_socket->so_rcv, 1,
2887 SCTP_READ_LOCK_NOT_HELD,
2893 sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
2894 struct sctp_tmit_chunk *chk, int so_locked
2895 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2900 struct mbuf *m_notify;
2901 struct sctp_send_failed *ssf;
2902 struct sctp_send_failed_event *ssfe;
2903 struct sctp_queued_to_read *control;
2904 struct sctp_chunkhdr *chkhdr;
2905 int notifhdr_len, chk_len, chkhdr_len, padding_len, payload_len;
2907 if ((stcb == NULL) ||
2908 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
2909 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2910 /* event not enabled */
2913 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
2914 notifhdr_len = sizeof(struct sctp_send_failed_event);
2916 notifhdr_len = sizeof(struct sctp_send_failed);
2918 m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
2919 if (m_notify == NULL)
2922 SCTP_BUF_LEN(m_notify) = notifhdr_len;
2923 if (stcb->asoc.idata_supported) {
2924 chkhdr_len = sizeof(struct sctp_idata_chunk);
2926 chkhdr_len = sizeof(struct sctp_data_chunk);
2928 /* Use some defaults in case we can't access the chunk header */
2929 if (chk->send_size >= chkhdr_len) {
2930 payload_len = chk->send_size - chkhdr_len;
2935 if (chk->data != NULL) {
2936 chkhdr = mtod(chk->data, struct sctp_chunkhdr *);
2937 if (chkhdr != NULL) {
2938 chk_len = ntohs(chkhdr->chunk_length);
2939 if ((chk_len >= chkhdr_len) &&
2940 (chk->send_size >= chk_len) &&
2941 (chk->send_size - chk_len < 4)) {
2942 padding_len = chk->send_size - chk_len;
2943 payload_len = chk->send_size - chkhdr_len - padding_len;
2947 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
2948 ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2949 memset(ssfe, 0, notifhdr_len);
2950 ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
2952 ssfe->ssfe_flags = SCTP_DATA_SENT;
2954 ssfe->ssfe_flags = SCTP_DATA_UNSENT;
2956 ssfe->ssfe_length = (uint32_t)(notifhdr_len + payload_len);
2957 ssfe->ssfe_error = error;
2958 /* not exactly what the user sent in, but should be close :) */
2959 ssfe->ssfe_info.snd_sid = chk->rec.data.sid;
2960 ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
2961 ssfe->ssfe_info.snd_ppid = chk->rec.data.ppid;
2962 ssfe->ssfe_info.snd_context = chk->rec.data.context;
2963 ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
2964 ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
2966 ssf = mtod(m_notify, struct sctp_send_failed *);
2967 memset(ssf, 0, notifhdr_len);
2968 ssf->ssf_type = SCTP_SEND_FAILED;
2970 ssf->ssf_flags = SCTP_DATA_SENT;
2972 ssf->ssf_flags = SCTP_DATA_UNSENT;
2974 ssf->ssf_length = (uint32_t)(notifhdr_len + payload_len);
2975 ssf->ssf_error = error;
2976 /* not exactly what the user sent in, but should be close :) */
2977 ssf->ssf_info.sinfo_stream = chk->rec.data.sid;
2978 ssf->ssf_info.sinfo_ssn = (uint16_t)chk->rec.data.mid;
2979 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
2980 ssf->ssf_info.sinfo_ppid = chk->rec.data.ppid;
2981 ssf->ssf_info.sinfo_context = chk->rec.data.context;
2982 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2983 ssf->ssf_assoc_id = sctp_get_associd(stcb);
2985 if (chk->data != NULL) {
2986 /* Trim off the sctp chunk header (it should be there) */
2987 if (chk->send_size == chkhdr_len + payload_len + padding_len) {
2988 m_adj(chk->data, chkhdr_len);
2989 m_adj(chk->data, -padding_len);
2990 sctp_mbuf_crush(chk->data);
2991 chk->send_size -= (chkhdr_len + padding_len);
2994 SCTP_BUF_NEXT(m_notify) = chk->data;
2995 /* Steal off the mbuf */
2998 * For this case, we check the actual socket buffer, since the assoc
2999 * is going away we don't want to overfill the socket buffer for a
3002 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3003 sctp_m_freem(m_notify);
3006 /* append to socket */
3007 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3008 0, 0, stcb->asoc.context, 0, 0, 0,
3010 if (control == NULL) {
3012 sctp_m_freem(m_notify);
3015 control->length = SCTP_BUF_LEN(m_notify);
3016 control->spec_flags = M_NOTIFICATION;
3017 /* not that we need this */
3018 control->tail_mbuf = m_notify;
3019 sctp_add_to_readq(stcb->sctp_ep, stcb,
3021 &stcb->sctp_socket->so_rcv, 1,
3022 SCTP_READ_LOCK_NOT_HELD,
3028 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3029 struct sctp_stream_queue_pending *sp, int so_locked
3030 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3035 struct mbuf *m_notify;
3036 struct sctp_send_failed *ssf;
3037 struct sctp_send_failed_event *ssfe;
3038 struct sctp_queued_to_read *control;
3041 if ((stcb == NULL) ||
3042 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
3043 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3044 /* event not enabled */
3047 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3048 notifhdr_len = sizeof(struct sctp_send_failed_event);
3050 notifhdr_len = sizeof(struct sctp_send_failed);
3052 m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3053 if (m_notify == NULL) {
3057 SCTP_BUF_LEN(m_notify) = notifhdr_len;
3058 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3059 ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3060 memset(ssfe, 0, notifhdr_len);
3061 ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3062 ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3063 ssfe->ssfe_length = (uint32_t)(notifhdr_len + sp->length);
3064 ssfe->ssfe_error = error;
3065 /* not exactly what the user sent in, but should be close :) */
3066 ssfe->ssfe_info.snd_sid = sp->sid;
3067 if (sp->some_taken) {
3068 ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
3070 ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
3072 ssfe->ssfe_info.snd_ppid = sp->ppid;
3073 ssfe->ssfe_info.snd_context = sp->context;
3074 ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3075 ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3077 ssf = mtod(m_notify, struct sctp_send_failed *);
3078 memset(ssf, 0, notifhdr_len);
3079 ssf->ssf_type = SCTP_SEND_FAILED;
3080 ssf->ssf_flags = SCTP_DATA_UNSENT;
3081 ssf->ssf_length = (uint32_t)(notifhdr_len + sp->length);
3082 ssf->ssf_error = error;
3083 /* not exactly what the user sent in, but should be close :) */
3084 ssf->ssf_info.sinfo_stream = sp->sid;
3085 ssf->ssf_info.sinfo_ssn = 0;
3086 if (sp->some_taken) {
3087 ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3089 ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3091 ssf->ssf_info.sinfo_ppid = sp->ppid;
3092 ssf->ssf_info.sinfo_context = sp->context;
3093 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3094 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3096 SCTP_BUF_NEXT(m_notify) = sp->data;
3098 /* Steal off the mbuf */
3101 * For this case, we check the actual socket buffer, since the assoc
3102 * is going away we don't want to overfill the socket buffer for a
3105 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3106 sctp_m_freem(m_notify);
3109 /* append to socket */
3110 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3111 0, 0, stcb->asoc.context, 0, 0, 0,
3113 if (control == NULL) {
3115 sctp_m_freem(m_notify);
3118 control->length = SCTP_BUF_LEN(m_notify);
3119 control->spec_flags = M_NOTIFICATION;
3120 /* not that we need this */
3121 control->tail_mbuf = m_notify;
3122 sctp_add_to_readq(stcb->sctp_ep, stcb,
3124 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3130 sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3132 struct mbuf *m_notify;
3133 struct sctp_adaptation_event *sai;
3134 struct sctp_queued_to_read *control;
3136 if ((stcb == NULL) ||
3137 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3138 /* event not enabled */
3141 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3142 if (m_notify == NULL)
3145 SCTP_BUF_LEN(m_notify) = 0;
3146 sai = mtod(m_notify, struct sctp_adaptation_event *);
3147 memset(sai, 0, sizeof(struct sctp_adaptation_event));
3148 sai->sai_type = SCTP_ADAPTATION_INDICATION;
3150 sai->sai_length = sizeof(struct sctp_adaptation_event);
3151 sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3152 sai->sai_assoc_id = sctp_get_associd(stcb);
3154 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3155 SCTP_BUF_NEXT(m_notify) = NULL;
3157 /* append to socket */
3158 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3159 0, 0, stcb->asoc.context, 0, 0, 0,
3161 if (control == NULL) {
3163 sctp_m_freem(m_notify);
3166 control->length = SCTP_BUF_LEN(m_notify);
3167 control->spec_flags = M_NOTIFICATION;
3168 /* not that we need this */
3169 control->tail_mbuf = m_notify;
3170 sctp_add_to_readq(stcb->sctp_ep, stcb,
3172 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3175 /* This always must be called with the read-queue LOCKED in the INP */
3177 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3178 uint32_t val, int so_locked
3179 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3184 struct mbuf *m_notify;
3185 struct sctp_pdapi_event *pdapi;
3186 struct sctp_queued_to_read *control;
3189 if ((stcb == NULL) ||
3190 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3191 /* event not enabled */
3194 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3197 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3198 if (m_notify == NULL)
3201 SCTP_BUF_LEN(m_notify) = 0;
3202 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3203 memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3204 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3205 pdapi->pdapi_flags = 0;
3206 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3207 pdapi->pdapi_indication = error;
3208 pdapi->pdapi_stream = (val >> 16);
3209 pdapi->pdapi_seq = (val & 0x0000ffff);
3210 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3212 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3213 SCTP_BUF_NEXT(m_notify) = NULL;
3214 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3215 0, 0, stcb->asoc.context, 0, 0, 0,
3217 if (control == NULL) {
3219 sctp_m_freem(m_notify);
3222 control->length = SCTP_BUF_LEN(m_notify);
3223 control->spec_flags = M_NOTIFICATION;
3224 /* not that we need this */
3225 control->tail_mbuf = m_notify;
3226 sb = &stcb->sctp_socket->so_rcv;
3227 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3228 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
3230 sctp_sballoc(stcb, sb, m_notify);
3231 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3232 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3234 control->end_added = 1;
3235 if (stcb->asoc.control_pdapi)
3236 TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
3238 /* we really should not see this case */
3239 TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
3241 if (stcb->sctp_ep && stcb->sctp_socket) {
3242 /* This should always be the case */
3243 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3246 so = SCTP_INP_SO(stcb->sctp_ep);
3248 atomic_add_int(&stcb->asoc.refcnt, 1);
3249 SCTP_TCB_UNLOCK(stcb);
3250 SCTP_SOCKET_LOCK(so, 1);
3251 SCTP_TCB_LOCK(stcb);
3252 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3253 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3254 SCTP_SOCKET_UNLOCK(so, 1);
3259 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3260 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3262 SCTP_SOCKET_UNLOCK(so, 1);
3269 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3271 struct mbuf *m_notify;
3272 struct sctp_shutdown_event *sse;
3273 struct sctp_queued_to_read *control;
3276 * For TCP model AND UDP connected sockets we will send an error up
3277 * when an SHUTDOWN completes
3279 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3280 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3281 /* mark socket closed for read/write and wakeup! */
3282 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3285 so = SCTP_INP_SO(stcb->sctp_ep);
3286 atomic_add_int(&stcb->asoc.refcnt, 1);
3287 SCTP_TCB_UNLOCK(stcb);
3288 SCTP_SOCKET_LOCK(so, 1);
3289 SCTP_TCB_LOCK(stcb);
3290 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3291 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3292 SCTP_SOCKET_UNLOCK(so, 1);
3296 socantsendmore(stcb->sctp_socket);
3297 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3298 SCTP_SOCKET_UNLOCK(so, 1);
3301 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3302 /* event not enabled */
3305 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3306 if (m_notify == NULL)
3309 sse = mtod(m_notify, struct sctp_shutdown_event *);
3310 memset(sse, 0, sizeof(struct sctp_shutdown_event));
3311 sse->sse_type = SCTP_SHUTDOWN_EVENT;
3313 sse->sse_length = sizeof(struct sctp_shutdown_event);
3314 sse->sse_assoc_id = sctp_get_associd(stcb);
3316 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3317 SCTP_BUF_NEXT(m_notify) = NULL;
3319 /* append to socket */
3320 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3321 0, 0, stcb->asoc.context, 0, 0, 0,
3323 if (control == NULL) {
3325 sctp_m_freem(m_notify);
3328 control->length = SCTP_BUF_LEN(m_notify);
3329 control->spec_flags = M_NOTIFICATION;
3330 /* not that we need this */
3331 control->tail_mbuf = m_notify;
3332 sctp_add_to_readq(stcb->sctp_ep, stcb,
3334 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3338 sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3340 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3345 struct mbuf *m_notify;
3346 struct sctp_sender_dry_event *event;
3347 struct sctp_queued_to_read *control;
3349 if ((stcb == NULL) ||
3350 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3351 /* event not enabled */
3354 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3355 if (m_notify == NULL) {
3359 SCTP_BUF_LEN(m_notify) = 0;
3360 event = mtod(m_notify, struct sctp_sender_dry_event *);
3361 memset(event, 0, sizeof(struct sctp_sender_dry_event));
3362 event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3363 event->sender_dry_flags = 0;
3364 event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3365 event->sender_dry_assoc_id = sctp_get_associd(stcb);
3367 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3368 SCTP_BUF_NEXT(m_notify) = NULL;
3370 /* append to socket */
3371 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3372 0, 0, stcb->asoc.context, 0, 0, 0,
3374 if (control == NULL) {
3376 sctp_m_freem(m_notify);
3379 control->length = SCTP_BUF_LEN(m_notify);
3380 control->spec_flags = M_NOTIFICATION;
3381 /* not that we need this */
3382 control->tail_mbuf = m_notify;
3383 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3384 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3389 sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3391 struct mbuf *m_notify;
3392 struct sctp_queued_to_read *control;
3393 struct sctp_stream_change_event *stradd;
3395 if ((stcb == NULL) ||
3396 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3397 /* event not enabled */
3400 if ((stcb->asoc.peer_req_out) && flag) {
3401 /* Peer made the request, don't tell the local user */
3402 stcb->asoc.peer_req_out = 0;
3405 stcb->asoc.peer_req_out = 0;
3406 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3407 if (m_notify == NULL)
3410 SCTP_BUF_LEN(m_notify) = 0;
3411 stradd = mtod(m_notify, struct sctp_stream_change_event *);
3412 memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3413 stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3414 stradd->strchange_flags = flag;
3415 stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3416 stradd->strchange_assoc_id = sctp_get_associd(stcb);
3417 stradd->strchange_instrms = numberin;
3418 stradd->strchange_outstrms = numberout;
3419 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3420 SCTP_BUF_NEXT(m_notify) = NULL;
3421 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3423 sctp_m_freem(m_notify);
3426 /* append to socket */
3427 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3428 0, 0, stcb->asoc.context, 0, 0, 0,
3430 if (control == NULL) {
3432 sctp_m_freem(m_notify);
3435 control->length = SCTP_BUF_LEN(m_notify);
3436 control->spec_flags = M_NOTIFICATION;
3437 /* not that we need this */
3438 control->tail_mbuf = m_notify;
3439 sctp_add_to_readq(stcb->sctp_ep, stcb,
3441 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3445 sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3447 struct mbuf *m_notify;
3448 struct sctp_queued_to_read *control;
3449 struct sctp_assoc_reset_event *strasoc;
3451 if ((stcb == NULL) ||
3452 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3453 /* event not enabled */
3456 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3457 if (m_notify == NULL)
3460 SCTP_BUF_LEN(m_notify) = 0;
3461 strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3462 memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3463 strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3464 strasoc->assocreset_flags = flag;
3465 strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3466 strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3467 strasoc->assocreset_local_tsn = sending_tsn;
3468 strasoc->assocreset_remote_tsn = recv_tsn;
3469 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3470 SCTP_BUF_NEXT(m_notify) = NULL;
3471 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3473 sctp_m_freem(m_notify);
3476 /* append to socket */
3477 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3478 0, 0, stcb->asoc.context, 0, 0, 0,
3480 if (control == NULL) {
3482 sctp_m_freem(m_notify);
3485 control->length = SCTP_BUF_LEN(m_notify);
3486 control->spec_flags = M_NOTIFICATION;
3487 /* not that we need this */
3488 control->tail_mbuf = m_notify;
3489 sctp_add_to_readq(stcb->sctp_ep, stcb,
3491 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3497 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3498 int number_entries, uint16_t *list, int flag)
3500 struct mbuf *m_notify;
3501 struct sctp_queued_to_read *control;
3502 struct sctp_stream_reset_event *strreset;
3505 if ((stcb == NULL) ||
3506 (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3507 /* event not enabled */
3510 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3511 if (m_notify == NULL)
3514 SCTP_BUF_LEN(m_notify) = 0;
3515 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3516 if (len > M_TRAILINGSPACE(m_notify)) {
3517 /* never enough room */
3518 sctp_m_freem(m_notify);
3521 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3522 memset(strreset, 0, len);
3523 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3524 strreset->strreset_flags = flag;
3525 strreset->strreset_length = len;
3526 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3527 if (number_entries) {
3530 for (i = 0; i < number_entries; i++) {
3531 strreset->strreset_stream_list[i] = ntohs(list[i]);
3534 SCTP_BUF_LEN(m_notify) = len;
3535 SCTP_BUF_NEXT(m_notify) = NULL;
3536 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3538 sctp_m_freem(m_notify);
3541 /* append to socket */
3542 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3543 0, 0, stcb->asoc.context, 0, 0, 0,
3545 if (control == NULL) {
3547 sctp_m_freem(m_notify);
3550 control->length = SCTP_BUF_LEN(m_notify);
3551 control->spec_flags = M_NOTIFICATION;
3552 /* not that we need this */
3553 control->tail_mbuf = m_notify;
3554 sctp_add_to_readq(stcb->sctp_ep, stcb,
3556 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3561 sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3563 struct mbuf *m_notify;
3564 struct sctp_remote_error *sre;
3565 struct sctp_queued_to_read *control;
3566 unsigned int notif_len;
3569 if ((stcb == NULL) ||
3570 sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3573 if (chunk != NULL) {
3574 chunk_len = ntohs(chunk->ch.chunk_length);
3576 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
3579 if (chunk_len > SCTP_CHUNK_BUFFER_SIZE) {
3580 chunk_len = SCTP_CHUNK_BUFFER_SIZE;
3585 notif_len = (unsigned int)(sizeof(struct sctp_remote_error) + chunk_len);
3586 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3587 if (m_notify == NULL) {
3588 /* Retry with smaller value. */
3589 notif_len = (unsigned int)sizeof(struct sctp_remote_error);
3590 m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3591 if (m_notify == NULL) {
3595 SCTP_BUF_NEXT(m_notify) = NULL;
3596 sre = mtod(m_notify, struct sctp_remote_error *);
3597 memset(sre, 0, notif_len);
3598 sre->sre_type = SCTP_REMOTE_ERROR;
3600 sre->sre_length = sizeof(struct sctp_remote_error);
3601 sre->sre_error = error;
3602 sre->sre_assoc_id = sctp_get_associd(stcb);
3603 if (notif_len > sizeof(struct sctp_remote_error)) {
3604 memcpy(sre->sre_data, chunk, chunk_len);
3605 sre->sre_length += chunk_len;
3607 SCTP_BUF_LEN(m_notify) = sre->sre_length;
3608 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3609 0, 0, stcb->asoc.context, 0, 0, 0,
3611 if (control != NULL) {
3612 control->length = SCTP_BUF_LEN(m_notify);
3613 control->spec_flags = M_NOTIFICATION;
3614 /* not that we need this */
3615 control->tail_mbuf = m_notify;
3616 sctp_add_to_readq(stcb->sctp_ep, stcb,
3618 &stcb->sctp_socket->so_rcv, 1,
3619 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3621 sctp_m_freem(m_notify);
3627 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3628 uint32_t error, void *data, int so_locked
3629 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3634 if ((stcb == NULL) ||
3635 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3636 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3637 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3638 /* If the socket is gone we are out of here */
3641 if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3644 if ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3645 (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED)) {
3646 if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
3647 (notification == SCTP_NOTIFY_INTERFACE_UP) ||
3648 (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
3649 /* Don't report these in front states */
3653 switch (notification) {
3654 case SCTP_NOTIFY_ASSOC_UP:
3655 if (stcb->asoc.assoc_up_sent == 0) {
3656 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
3657 stcb->asoc.assoc_up_sent = 1;
3659 if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
3660 sctp_notify_adaptation_layer(stcb);
3662 if (stcb->asoc.auth_supported == 0) {
3663 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3667 case SCTP_NOTIFY_ASSOC_DOWN:
3668 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
3670 case SCTP_NOTIFY_INTERFACE_DOWN:
3672 struct sctp_nets *net;
3674 net = (struct sctp_nets *)data;
3675 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3676 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3679 case SCTP_NOTIFY_INTERFACE_UP:
3681 struct sctp_nets *net;
3683 net = (struct sctp_nets *)data;
3684 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3685 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3688 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3690 struct sctp_nets *net;
3692 net = (struct sctp_nets *)data;
3693 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3694 (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3697 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3698 sctp_notify_send_failed2(stcb, error,
3699 (struct sctp_stream_queue_pending *)data, so_locked);
3701 case SCTP_NOTIFY_SENT_DG_FAIL:
3702 sctp_notify_send_failed(stcb, 1, error,
3703 (struct sctp_tmit_chunk *)data, so_locked);
3705 case SCTP_NOTIFY_UNSENT_DG_FAIL:
3706 sctp_notify_send_failed(stcb, 0, error,
3707 (struct sctp_tmit_chunk *)data, so_locked);
3709 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3713 val = *((uint32_t *)data);
3715 sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3718 case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
3719 if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3720 ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3721 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
3723 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
3726 case SCTP_NOTIFY_ASSOC_REM_ABORTED:
3727 if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3728 ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3729 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
3731 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
3734 case SCTP_NOTIFY_ASSOC_RESTART:
3735 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
3736 if (stcb->asoc.auth_supported == 0) {
3737 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3741 case SCTP_NOTIFY_STR_RESET_SEND:
3742 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_OUTGOING_SSN);
3744 case SCTP_NOTIFY_STR_RESET_RECV:
3745 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_INCOMING);
3747 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3748 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3749 (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
3751 case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
3752 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3753 (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
3755 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3756 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3757 (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
3759 case SCTP_NOTIFY_STR_RESET_DENIED_IN:
3760 sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
3761 (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
3763 case SCTP_NOTIFY_ASCONF_ADD_IP:
3764 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3767 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3768 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3771 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3772 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3775 case SCTP_NOTIFY_PEER_SHUTDOWN:
3776 sctp_notify_shutdown_event(stcb);
3778 case SCTP_NOTIFY_AUTH_NEW_KEY:
3779 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
3780 (uint16_t)(uintptr_t)data,
3783 case SCTP_NOTIFY_AUTH_FREE_KEY:
3784 sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3785 (uint16_t)(uintptr_t)data,
3788 case SCTP_NOTIFY_NO_PEER_AUTH:
3789 sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3790 (uint16_t)(uintptr_t)data,
3793 case SCTP_NOTIFY_SENDER_DRY:
3794 sctp_notify_sender_dry_event(stcb, so_locked);
3796 case SCTP_NOTIFY_REMOTE_ERROR:
3797 sctp_notify_remote_error(stcb, error, data);
3800 SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3801 __func__, notification, notification);
3807 sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
3808 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3813 struct sctp_association *asoc;
3814 struct sctp_stream_out *outs;
3815 struct sctp_tmit_chunk *chk, *nchk;
3816 struct sctp_stream_queue_pending *sp, *nsp;
3823 if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3824 /* already being freed */
3827 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3828 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3829 (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
3832 /* now through all the gunk freeing chunks */
3833 if (holds_lock == 0) {
3834 SCTP_TCB_SEND_LOCK(stcb);
3836 /* sent queue SHOULD be empty */
3837 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3838 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3839 asoc->sent_queue_cnt--;
3840 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
3841 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
3842 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
3845 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
3849 if (chk->data != NULL) {
3850 sctp_free_bufspace(stcb, asoc, chk, 1);
3851 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
3852 error, chk, so_locked);
3854 sctp_m_freem(chk->data);
3858 sctp_free_a_chunk(stcb, chk, so_locked);
3859 /* sa_ignore FREED_MEMORY */
3861 /* pending send queue SHOULD be empty */
3862 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3863 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3864 asoc->send_queue_cnt--;
3865 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
3866 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
3869 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
3872 if (chk->data != NULL) {
3873 sctp_free_bufspace(stcb, asoc, chk, 1);
3874 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
3875 error, chk, so_locked);
3877 sctp_m_freem(chk->data);
3881 sctp_free_a_chunk(stcb, chk, so_locked);
3882 /* sa_ignore FREED_MEMORY */
3884 for (i = 0; i < asoc->streamoutcnt; i++) {
3885 /* For each stream */
3886 outs = &asoc->strmout[i];
3887 /* clean up any sends there */
3888 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
3889 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
3890 TAILQ_REMOVE(&outs->outqueue, sp, next);
3891 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, holds_lock);
3892 sctp_free_spbufspace(stcb, asoc, sp);
3894 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3895 error, (void *)sp, so_locked);
3897 sctp_m_freem(sp->data);
3899 sp->tail_mbuf = NULL;
3904 sctp_free_remote_addr(sp->net);
3907 /* Free the chunk */
3908 sctp_free_a_strmoq(stcb, sp, so_locked);
3909 /* sa_ignore FREED_MEMORY */
3913 if (holds_lock == 0) {
3914 SCTP_TCB_SEND_UNLOCK(stcb);
3919 sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
3920 struct sctp_abort_chunk *abort, int so_locked
3921 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3929 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3930 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3931 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3932 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3934 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3935 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3936 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3939 /* Tell them we lost the asoc */
3940 sctp_report_all_outbound(stcb, error, 1, so_locked);
3942 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
3944 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
3949 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3950 struct mbuf *m, int iphlen,
3951 struct sockaddr *src, struct sockaddr *dst,
3952 struct sctphdr *sh, struct mbuf *op_err,
3953 uint8_t mflowtype, uint32_t mflowid,
3954 uint32_t vrf_id, uint16_t port)
3957 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3963 vtag = stcb->asoc.peer_vtag;
3964 vrf_id = stcb->asoc.vrf_id;
3966 sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
3967 mflowtype, mflowid, inp->fibnum,
3970 /* We have a TCB to abort, send notification too */
3971 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
3972 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3973 /* Ok, now lets free it */
3974 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3975 so = SCTP_INP_SO(inp);
3976 atomic_add_int(&stcb->asoc.refcnt, 1);
3977 SCTP_TCB_UNLOCK(stcb);
3978 SCTP_SOCKET_LOCK(so, 1);
3979 SCTP_TCB_LOCK(stcb);
3980 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3982 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3983 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3984 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3985 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3987 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
3988 SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
3989 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3990 SCTP_SOCKET_UNLOCK(so, 1);
3994 #ifdef SCTP_ASOCLOG_OF_TSNS
3996 sctp_print_out_track_log(struct sctp_tcb *stcb)
4001 SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
4002 SCTP_PRINTF("IN bound TSN log-aaa\n");
4003 if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
4004 SCTP_PRINTF("None rcvd\n");
4007 if (stcb->asoc.tsn_in_wrapped) {
4008 for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
4009 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4010 stcb->asoc.in_tsnlog[i].tsn,
4011 stcb->asoc.in_tsnlog[i].strm,
4012 stcb->asoc.in_tsnlog[i].seq,
4013 stcb->asoc.in_tsnlog[i].flgs,
4014 stcb->asoc.in_tsnlog[i].sz);
4017 if (stcb->asoc.tsn_in_at) {
4018 for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
4019 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4020 stcb->asoc.in_tsnlog[i].tsn,
4021 stcb->asoc.in_tsnlog[i].strm,
4022 stcb->asoc.in_tsnlog[i].seq,
4023 stcb->asoc.in_tsnlog[i].flgs,
4024 stcb->asoc.in_tsnlog[i].sz);
4028 SCTP_PRINTF("OUT bound TSN log-aaa\n");
4029 if ((stcb->asoc.tsn_out_at == 0) &&
4030 (stcb->asoc.tsn_out_wrapped == 0)) {
4031 SCTP_PRINTF("None sent\n");
4033 if (stcb->asoc.tsn_out_wrapped) {
4034 for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4035 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4036 stcb->asoc.out_tsnlog[i].tsn,
4037 stcb->asoc.out_tsnlog[i].strm,
4038 stcb->asoc.out_tsnlog[i].seq,
4039 stcb->asoc.out_tsnlog[i].flgs,
4040 stcb->asoc.out_tsnlog[i].sz);
4043 if (stcb->asoc.tsn_out_at) {
4044 for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4045 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4046 stcb->asoc.out_tsnlog[i].tsn,
4047 stcb->asoc.out_tsnlog[i].strm,
4048 stcb->asoc.out_tsnlog[i].seq,
4049 stcb->asoc.out_tsnlog[i].flgs,
4050 stcb->asoc.out_tsnlog[i].sz);
4058 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4059 struct mbuf *op_err,
4061 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4066 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4070 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4071 so = SCTP_INP_SO(inp);
4074 /* Got to have a TCB */
4075 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4076 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4077 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4078 SCTP_CALLED_DIRECTLY_NOCMPSET);
4083 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
4085 /* notify the peer */
4086 sctp_send_abort_tcb(stcb, op_err, so_locked);
4087 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4088 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
4089 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4090 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4092 /* notify the ulp */
4093 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4094 sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4096 /* now free the asoc */
4097 #ifdef SCTP_ASOCLOG_OF_TSNS
4098 sctp_print_out_track_log(stcb);
4100 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4102 atomic_add_int(&stcb->asoc.refcnt, 1);
4103 SCTP_TCB_UNLOCK(stcb);
4104 SCTP_SOCKET_LOCK(so, 1);
4105 SCTP_TCB_LOCK(stcb);
4106 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4109 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4110 SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4111 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4113 SCTP_SOCKET_UNLOCK(so, 1);
4119 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4120 struct sockaddr *src, struct sockaddr *dst,
4121 struct sctphdr *sh, struct sctp_inpcb *inp,
4123 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4124 uint32_t vrf_id, uint16_t port)
4126 struct sctp_chunkhdr *ch, chunk_buf;
4127 unsigned int chk_length;
4128 int contains_init_chunk;
4130 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4131 /* Generate a TO address for future reference */
4132 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4133 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4134 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4135 SCTP_CALLED_DIRECTLY_NOCMPSET);
4138 contains_init_chunk = 0;
4139 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4140 sizeof(*ch), (uint8_t *)&chunk_buf);
4141 while (ch != NULL) {
4142 chk_length = ntohs(ch->chunk_length);
4143 if (chk_length < sizeof(*ch)) {
4144 /* break to abort land */
4147 switch (ch->chunk_type) {
4149 contains_init_chunk = 1;
4151 case SCTP_PACKET_DROPPED:
4152 /* we don't respond to pkt-dropped */
4154 case SCTP_ABORT_ASSOCIATION:
4155 /* we don't respond with an ABORT to an ABORT */
4157 case SCTP_SHUTDOWN_COMPLETE:
4159 * we ignore it since we are not waiting for it and
4163 case SCTP_SHUTDOWN_ACK:
4164 sctp_send_shutdown_complete2(src, dst, sh,
4165 mflowtype, mflowid, fibnum,
4171 offset += SCTP_SIZE32(chk_length);
4172 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4173 sizeof(*ch), (uint8_t *)&chunk_buf);
4175 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4176 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4177 (contains_init_chunk == 0))) {
4178 sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4179 mflowtype, mflowid, fibnum,
4185 * check the inbound datagram to make sure there is not an abort inside it,
4186 * if there is return 1, else return 0.
4189 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t *vtagfill)
4191 struct sctp_chunkhdr *ch;
4192 struct sctp_init_chunk *init_chk, chunk_buf;
4194 unsigned int chk_length;
4196 offset = iphlen + sizeof(struct sctphdr);
4197 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4198 (uint8_t *)&chunk_buf);
4199 while (ch != NULL) {
4200 chk_length = ntohs(ch->chunk_length);
4201 if (chk_length < sizeof(*ch)) {
4202 /* packet is probably corrupt */
4205 /* we seem to be ok, is it an abort? */
4206 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4207 /* yep, tell them */
4210 if (ch->chunk_type == SCTP_INITIATION) {
4211 /* need to update the Vtag */
4212 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4213 offset, sizeof(*init_chk), (uint8_t *)&chunk_buf);
4214 if (init_chk != NULL) {
4215 *vtagfill = ntohl(init_chk->init.initiate_tag);
4218 /* Nope, move to the next chunk */
4219 offset += SCTP_SIZE32(chk_length);
4220 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4221 sizeof(*ch), (uint8_t *)&chunk_buf);
4227 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4228 * set (i.e. it's 0) so, create this function to compare link local scopes
4232 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4234 struct sockaddr_in6 a, b;
4240 if (a.sin6_scope_id == 0)
4241 if (sa6_recoverscope(&a)) {
4242 /* can't get scope, so can't match */
4245 if (b.sin6_scope_id == 0)
4246 if (sa6_recoverscope(&b)) {
4247 /* can't get scope, so can't match */
4250 if (a.sin6_scope_id != b.sin6_scope_id)
4257 * returns a sockaddr_in6 with embedded scope recovered and removed
4259 struct sockaddr_in6 *
4260 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4262 /* check and strip embedded scope junk */
4263 if (addr->sin6_family == AF_INET6) {
4264 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4265 if (addr->sin6_scope_id == 0) {
4267 if (!sa6_recoverscope(store)) {
4268 /* use the recovered scope */
4272 /* else, return the original "to" addr */
4273 in6_clearscope(&addr->sin6_addr);
4282 * are the two addresses the same? currently a "scopeless" check returns: 1
4286 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4290 if (sa1 == NULL || sa2 == NULL)
4293 /* must be the same family */
4294 if (sa1->sa_family != sa2->sa_family)
4297 switch (sa1->sa_family) {
4301 /* IPv6 addresses */
4302 struct sockaddr_in6 *sin6_1, *sin6_2;
4304 sin6_1 = (struct sockaddr_in6 *)sa1;
4305 sin6_2 = (struct sockaddr_in6 *)sa2;
4306 return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4313 /* IPv4 addresses */
4314 struct sockaddr_in *sin_1, *sin_2;
4316 sin_1 = (struct sockaddr_in *)sa1;
4317 sin_2 = (struct sockaddr_in *)sa2;
4318 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
4322 /* we don't do these... */
4328 sctp_print_address(struct sockaddr *sa)
4331 char ip6buf[INET6_ADDRSTRLEN];
4334 switch (sa->sa_family) {
4338 struct sockaddr_in6 *sin6;
4340 sin6 = (struct sockaddr_in6 *)sa;
4341 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
4342 ip6_sprintf(ip6buf, &sin6->sin6_addr),
4343 ntohs(sin6->sin6_port),
4344 sin6->sin6_scope_id);
4351 struct sockaddr_in *sin;
4354 sin = (struct sockaddr_in *)sa;
4355 p = (unsigned char *)&sin->sin_addr;
4356 SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4357 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
4368 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4369 struct sctp_inpcb *new_inp,
4370 struct sctp_tcb *stcb,
4374 * go through our old INP and pull off any control structures that
4375 * belong to stcb and move then to the new inp.
4377 struct socket *old_so, *new_so;
4378 struct sctp_queued_to_read *control, *nctl;
4379 struct sctp_readhead tmp_queue;
4383 old_so = old_inp->sctp_socket;
4384 new_so = new_inp->sctp_socket;
4385 TAILQ_INIT(&tmp_queue);
4386 error = sblock(&old_so->so_rcv, waitflags);
4389 * Gak, can't get sblock, we have a problem. data will be
4390 * left stranded.. and we don't dare look at it since the
4391 * other thread may be reading something. Oh well, its a
4392 * screwed up app that does a peeloff OR a accept while
4393 * reading from the main socket... actually its only the
4394 * peeloff() case, since I think read will fail on a
4395 * listening socket..
4399 /* lock the socket buffers */
4400 SCTP_INP_READ_LOCK(old_inp);
4401 TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4402 /* Pull off all for out target stcb */
4403 if (control->stcb == stcb) {
4404 /* remove it we want it */
4405 TAILQ_REMOVE(&old_inp->read_queue, control, next);
4406 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4409 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4410 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
4412 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4413 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4414 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4416 m = SCTP_BUF_NEXT(m);
4420 SCTP_INP_READ_UNLOCK(old_inp);
4421 /* Remove the sb-lock on the old socket */
4423 sbunlock(&old_so->so_rcv);
4424 /* Now we move them over to the new socket buffer */
4425 SCTP_INP_READ_LOCK(new_inp);
4426 TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4427 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4430 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4431 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4433 sctp_sballoc(stcb, &new_so->so_rcv, m);
4434 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4435 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4437 m = SCTP_BUF_NEXT(m);
4440 SCTP_INP_READ_UNLOCK(new_inp);
4444 sctp_wakeup_the_read_socket(struct sctp_inpcb *inp,
4445 struct sctp_tcb *stcb,
4447 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4452 if ((inp != NULL) && (inp->sctp_socket != NULL)) {
4453 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4456 so = SCTP_INP_SO(inp);
4459 atomic_add_int(&stcb->asoc.refcnt, 1);
4460 SCTP_TCB_UNLOCK(stcb);
4462 SCTP_SOCKET_LOCK(so, 1);
4464 SCTP_TCB_LOCK(stcb);
4465 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4467 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4468 SCTP_SOCKET_UNLOCK(so, 1);
4473 sctp_sorwakeup(inp, inp->sctp_socket);
4474 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4476 SCTP_SOCKET_UNLOCK(so, 1);
4483 sctp_add_to_readq(struct sctp_inpcb *inp,
4484 struct sctp_tcb *stcb,
4485 struct sctp_queued_to_read *control,
4488 int inp_read_lock_held,
4490 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4496 * Here we must place the control on the end of the socket read
4497 * queue AND increment sb_cc so that select will work properly on
4500 struct mbuf *m, *prev = NULL;
4505 panic("Gak, inp NULL on add_to_readq");
4509 if (inp_read_lock_held == 0)
4510 SCTP_INP_READ_LOCK(inp);
4511 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4512 sctp_free_remote_addr(control->whoFrom);
4513 if (control->data) {
4514 sctp_m_freem(control->data);
4515 control->data = NULL;
4517 sctp_free_a_readq(stcb, control);
4518 if (inp_read_lock_held == 0)
4519 SCTP_INP_READ_UNLOCK(inp);
4522 if (!(control->spec_flags & M_NOTIFICATION)) {
4523 atomic_add_int(&inp->total_recvs, 1);
4524 if (!control->do_not_ref_stcb) {
4525 atomic_add_int(&stcb->total_recvs, 1);
4529 control->held_length = 0;
4530 control->length = 0;
4532 if (SCTP_BUF_LEN(m) == 0) {
4533 /* Skip mbufs with NO length */
4536 control->data = sctp_m_free(m);
4539 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4540 m = SCTP_BUF_NEXT(prev);
4543 control->tail_mbuf = prev;
4548 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4549 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4551 sctp_sballoc(stcb, sb, m);
4552 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4553 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4555 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4556 m = SCTP_BUF_NEXT(m);
4559 control->tail_mbuf = prev;
4561 /* Everything got collapsed out?? */
4562 sctp_free_remote_addr(control->whoFrom);
4563 sctp_free_a_readq(stcb, control);
4564 if (inp_read_lock_held == 0)
4565 SCTP_INP_READ_UNLOCK(inp);
4569 control->end_added = 1;
4571 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4572 control->on_read_q = 1;
4573 if (inp_read_lock_held == 0)
4574 SCTP_INP_READ_UNLOCK(inp);
4575 if (inp && inp->sctp_socket) {
4576 sctp_wakeup_the_read_socket(inp, stcb, so_locked);
4580 /*************HOLD THIS COMMENT FOR PATCH FILE OF
4581 *************ALTERNATE ROUTING CODE
4584 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4585 *************ALTERNATE ROUTING CODE
4589 sctp_generate_cause(uint16_t code, char *info)
4592 struct sctp_gen_error_cause *cause;
4596 if ((code == 0) || (info == NULL)) {
4599 info_len = strlen(info);
4600 if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
4603 len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
4604 m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4606 SCTP_BUF_LEN(m) = len;
4607 cause = mtod(m, struct sctp_gen_error_cause *);
4608 cause->code = htons(code);
4609 cause->length = htons(len);
4610 memcpy(cause->info, info, info_len);
4616 sctp_generate_no_user_data_cause(uint32_t tsn)
4619 struct sctp_error_no_user_data *no_user_data_cause;
4622 len = (uint16_t)sizeof(struct sctp_error_no_user_data);
4623 m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4625 SCTP_BUF_LEN(m) = len;
4626 no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
4627 no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
4628 no_user_data_cause->cause.length = htons(len);
4629 no_user_data_cause->tsn = htonl(tsn);
4634 #ifdef SCTP_MBCNT_LOGGING
4636 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4637 struct sctp_tmit_chunk *tp1, int chk_cnt)
4639 if (tp1->data == NULL) {
4642 asoc->chunks_on_out_queue -= chk_cnt;
4643 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4644 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4645 asoc->total_output_queue_size,
4650 if (asoc->total_output_queue_size >= tp1->book_size) {
4651 atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4653 asoc->total_output_queue_size = 0;
4656 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4657 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4658 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4659 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4661 stcb->sctp_socket->so_snd.sb_cc = 0;
4670 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4671 uint8_t sent, int so_locked
4672 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4677 struct sctp_stream_out *strq;
4678 struct sctp_tmit_chunk *chk = NULL, *tp2;
4679 struct sctp_stream_queue_pending *sp;
4682 uint8_t foundeom = 0;
4685 int do_wakeup_routine = 0;
4687 sid = tp1->rec.data.sid;
4688 mid = tp1->rec.data.mid;
4689 if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
4690 stcb->asoc.abandoned_sent[0]++;
4691 stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4692 stcb->asoc.strmout[sid].abandoned_sent[0]++;
4693 #if defined(SCTP_DETAILED_STR_STATS)
4694 stcb->asoc.strmout[sid].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4697 stcb->asoc.abandoned_unsent[0]++;
4698 stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4699 stcb->asoc.strmout[sid].abandoned_unsent[0]++;
4700 #if defined(SCTP_DETAILED_STR_STATS)
4701 stcb->asoc.strmout[sid].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4705 ret_sz += tp1->book_size;
4706 if (tp1->data != NULL) {
4707 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4708 sctp_flight_size_decrease(tp1);
4709 sctp_total_flight_decrease(stcb, tp1);
4711 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4712 stcb->asoc.peers_rwnd += tp1->send_size;
4713 stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
4715 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
4717 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
4720 sctp_m_freem(tp1->data);
4723 do_wakeup_routine = 1;
4724 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4725 stcb->asoc.sent_queue_cnt_removeable--;
4728 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4729 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4730 SCTP_DATA_NOT_FRAG) {
4731 /* not frag'ed we ae done */
4734 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4735 /* end of frag, we are done */
4740 * Its a begin or middle piece, we must mark all of
4744 tp1 = TAILQ_NEXT(tp1, sctp_next);
4746 } while (tp1 && notdone);
4747 if (foundeom == 0) {
4749 * The multi-part message was scattered across the send and
4752 TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
4753 if ((tp1->rec.data.sid != sid) ||
4754 (!SCTP_MID_EQ(stcb->asoc.idata_supported, tp1->rec.data.mid, mid))) {
4758 * save to chk in case we have some on stream out
4759 * queue. If so and we have an un-transmitted one we
4760 * don't have to fudge the TSN.
4763 ret_sz += tp1->book_size;
4764 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4766 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
4768 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
4771 sctp_m_freem(tp1->data);
4774 /* No flight involved here book the size to 0 */
4776 if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4779 do_wakeup_routine = 1;
4780 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4781 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4783 * on to the sent queue so we can wait for it to be
4786 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4788 stcb->asoc.send_queue_cnt--;
4789 stcb->asoc.sent_queue_cnt++;
4792 if (foundeom == 0) {
4794 * Still no eom found. That means there is stuff left on the
4795 * stream out queue.. yuck.
4797 SCTP_TCB_SEND_LOCK(stcb);
4798 strq = &stcb->asoc.strmout[sid];
4799 sp = TAILQ_FIRST(&strq->outqueue);
4801 sp->discard_rest = 1;
4803 * We may need to put a chunk on the queue that
4804 * holds the TSN that would have been sent with the
4808 /* Yep, we have to */
4809 sctp_alloc_a_chunk(stcb, chk);
4812 * we are hosed. All we can do is
4813 * nothing.. which will cause an
4814 * abort if the peer is paying
4819 memset(chk, 0, sizeof(*chk));
4820 chk->rec.data.rcv_flags = 0;
4821 chk->sent = SCTP_FORWARD_TSN_SKIP;
4822 chk->asoc = &stcb->asoc;
4823 if (stcb->asoc.idata_supported == 0) {
4824 if (sp->sinfo_flags & SCTP_UNORDERED) {
4825 chk->rec.data.mid = 0;
4827 chk->rec.data.mid = strq->next_mid_ordered;
4830 if (sp->sinfo_flags & SCTP_UNORDERED) {
4831 chk->rec.data.mid = strq->next_mid_unordered;
4833 chk->rec.data.mid = strq->next_mid_ordered;
4836 chk->rec.data.sid = sp->sid;
4837 chk->rec.data.ppid = sp->ppid;
4838 chk->rec.data.context = sp->context;
4839 chk->flags = sp->act_flags;
4841 chk->rec.data.tsn = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
4842 strq->chunks_on_queues++;
4843 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
4844 stcb->asoc.sent_queue_cnt++;
4845 stcb->asoc.pr_sctp_cnt++;
4847 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4848 if (sp->sinfo_flags & SCTP_UNORDERED) {
4849 chk->rec.data.rcv_flags |= SCTP_DATA_UNORDERED;
4851 if (stcb->asoc.idata_supported == 0) {
4852 if ((sp->sinfo_flags & SCTP_UNORDERED) == 0) {
4853 strq->next_mid_ordered++;
4856 if (sp->sinfo_flags & SCTP_UNORDERED) {
4857 strq->next_mid_unordered++;
4859 strq->next_mid_ordered++;
4865 * Pull any data to free up the SB and allow
4866 * sender to "add more" while we will throw
4869 sctp_free_spbufspace(stcb, &stcb->asoc, sp);
4870 ret_sz += sp->length;
4871 do_wakeup_routine = 1;
4873 sctp_m_freem(sp->data);
4875 sp->tail_mbuf = NULL;
4879 SCTP_TCB_SEND_UNLOCK(stcb);
4881 if (do_wakeup_routine) {
4882 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4885 so = SCTP_INP_SO(stcb->sctp_ep);
4887 atomic_add_int(&stcb->asoc.refcnt, 1);
4888 SCTP_TCB_UNLOCK(stcb);
4889 SCTP_SOCKET_LOCK(so, 1);
4890 SCTP_TCB_LOCK(stcb);
4891 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4892 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4893 /* assoc was freed while we were unlocked */
4894 SCTP_SOCKET_UNLOCK(so, 1);
4899 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
4900 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4902 SCTP_SOCKET_UNLOCK(so, 1);
4910 * checks to see if the given address, sa, is one that is currently known by
4911 * the kernel note: can't distinguish the same address on multiple interfaces
4912 * and doesn't handle multiple addresses with different zone/scope id's note:
4913 * ifa_ifwithaddr() compares the entire sockaddr struct
4916 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
4919 struct sctp_laddr *laddr;
4921 if (holds_lock == 0) {
4922 SCTP_INP_RLOCK(inp);
4924 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4925 if (laddr->ifa == NULL)
4927 if (addr->sa_family != laddr->ifa->address.sa.sa_family)
4930 if (addr->sa_family == AF_INET) {
4931 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4932 laddr->ifa->address.sin.sin_addr.s_addr) {
4934 if (holds_lock == 0) {
4935 SCTP_INP_RUNLOCK(inp);
4937 return (laddr->ifa);
4943 if (addr->sa_family == AF_INET6) {
4944 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4945 &laddr->ifa->address.sin6)) {
4947 if (holds_lock == 0) {
4948 SCTP_INP_RUNLOCK(inp);
4950 return (laddr->ifa);
4956 if (holds_lock == 0) {
4957 SCTP_INP_RUNLOCK(inp);
4963 sctp_get_ifa_hash_val(struct sockaddr *addr)
4965 switch (addr->sa_family) {
4969 struct sockaddr_in *sin;
4971 sin = (struct sockaddr_in *)addr;
4972 return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
4978 struct sockaddr_in6 *sin6;
4979 uint32_t hash_of_addr;
4981 sin6 = (struct sockaddr_in6 *)addr;
4982 hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
4983 sin6->sin6_addr.s6_addr32[1] +
4984 sin6->sin6_addr.s6_addr32[2] +
4985 sin6->sin6_addr.s6_addr32[3]);
4986 hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
4987 return (hash_of_addr);
4997 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
4999 struct sctp_ifa *sctp_ifap;
5000 struct sctp_vrf *vrf;
5001 struct sctp_ifalist *hash_head;
5002 uint32_t hash_of_addr;
5004 if (holds_lock == 0)
5005 SCTP_IPI_ADDR_RLOCK();
5007 vrf = sctp_find_vrf(vrf_id);
5009 if (holds_lock == 0)
5010 SCTP_IPI_ADDR_RUNLOCK();
5013 hash_of_addr = sctp_get_ifa_hash_val(addr);
5015 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5016 if (hash_head == NULL) {
5017 SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5018 hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
5019 (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
5020 sctp_print_address(addr);
5021 SCTP_PRINTF("No such bucket for address\n");
5022 if (holds_lock == 0)
5023 SCTP_IPI_ADDR_RUNLOCK();
5027 LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5028 if (addr->sa_family != sctp_ifap->address.sa.sa_family)
5031 if (addr->sa_family == AF_INET) {
5032 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5033 sctp_ifap->address.sin.sin_addr.s_addr) {
5035 if (holds_lock == 0)
5036 SCTP_IPI_ADDR_RUNLOCK();
5043 if (addr->sa_family == AF_INET6) {
5044 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5045 &sctp_ifap->address.sin6)) {
5047 if (holds_lock == 0)
5048 SCTP_IPI_ADDR_RUNLOCK();
5055 if (holds_lock == 0)
5056 SCTP_IPI_ADDR_RUNLOCK();
5061 sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
5064 /* User pulled some data, do we need a rwnd update? */
5067 struct socket *so = NULL;
5072 atomic_add_int(&stcb->asoc.refcnt, 1);
5074 if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
5075 SCTP_STATE_SHUTDOWN_RECEIVED |
5076 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5077 /* Pre-check If we are freeing no update */
5080 SCTP_INP_INCR_REF(stcb->sctp_ep);
5081 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5082 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5085 so = stcb->sctp_socket;
5089 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5090 /* Have you have freed enough to look */
5092 /* Yep, its worth a look and the lock overhead */
5094 /* Figure out what the rwnd would be */
5095 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5096 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5097 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5101 if (dif >= rwnd_req) {
5103 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5106 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5108 * One last check before we allow the guy possibly
5109 * to get in. There is a race, where the guy has not
5110 * reached the gate. In that case
5114 SCTP_TCB_LOCK(stcb);
5115 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5116 /* No reports here */
5117 SCTP_TCB_UNLOCK(stcb);
5120 SCTP_STAT_INCR(sctps_wu_sacks_sent);
5121 sctp_send_sack(stcb, SCTP_SO_LOCKED);
5123 sctp_chunk_output(stcb->sctp_ep, stcb,
5124 SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5125 /* make sure no timer is running */
5126 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
5127 SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5128 SCTP_TCB_UNLOCK(stcb);
5130 /* Update how much we have pending */
5131 stcb->freed_by_sorcv_sincelast = dif;
5134 if (so && r_unlocked && hold_rlock) {
5135 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5137 SCTP_INP_DECR_REF(stcb->sctp_ep);
5139 atomic_add_int(&stcb->asoc.refcnt, -1);
5144 sctp_sorecvmsg(struct socket *so,
5147 struct sockaddr *from,
5150 struct sctp_sndrcvinfo *sinfo,
5154 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5155 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5156 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5157 * On the way out we may send out any combination of:
5158 * MSG_NOTIFICATION MSG_EOR
5161 struct sctp_inpcb *inp = NULL;
5163 int cp_len = 0, error = 0;
5164 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
5165 struct mbuf *m = NULL;
5166 struct sctp_tcb *stcb = NULL;
5167 int wakeup_read_socket = 0;
5168 int freecnt_applied = 0;
5169 int out_flags = 0, in_flags = 0;
5170 int block_allowed = 1;
5171 uint32_t freed_so_far = 0;
5172 uint32_t copied_so_far = 0;
5173 int in_eeor_mode = 0;
5174 int no_rcv_needed = 0;
5175 uint32_t rwnd_req = 0;
5176 int hold_sblock = 0;
5179 uint32_t held_length = 0;
5180 int sockbuf_lock = 0;
5183 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5187 in_flags = *msg_flags;
5188 if (in_flags & MSG_PEEK)
5189 SCTP_STAT_INCR(sctps_read_peeks);
5193 slen = uio->uio_resid;
5195 /* Pull in and set up our int flags */
5196 if (in_flags & MSG_OOB) {
5197 /* Out of band's NOT supported */
5198 return (EOPNOTSUPP);
5200 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5201 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5204 if ((in_flags & (MSG_DONTWAIT
5207 SCTP_SO_IS_NBIO(so)) {
5210 /* setup the endpoint */
5211 inp = (struct sctp_inpcb *)so->so_pcb;
5213 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5216 rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5217 /* Must be at least a MTU's worth */
5218 if (rwnd_req < SCTP_MIN_RWND)
5219 rwnd_req = SCTP_MIN_RWND;
5220 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5221 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5222 sctp_misc_ints(SCTP_SORECV_ENTER,
5223 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
5225 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5226 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5227 rwnd_req, block_allowed, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
5229 error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5231 goto release_unlocked;
5238 if (hold_sblock == 0) {
5239 SOCKBUF_LOCK(&so->so_rcv);
5242 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5243 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5246 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5248 error = so->so_error;
5249 if ((in_flags & MSG_PEEK) == 0)
5253 if (so->so_rcv.sb_cc == 0) {
5260 if (so->so_rcv.sb_cc <= held_length) {
5262 error = so->so_error;
5263 if ((in_flags & MSG_PEEK) == 0) {
5268 if ((so->so_rcv.sb_cc == 0) &&
5269 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5270 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5271 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5273 * For active open side clear flags for
5274 * re-use passive open is blocked by
5277 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5279 * You were aborted, passive side
5282 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5285 so->so_state &= ~(SS_ISCONNECTING |
5286 SS_ISDISCONNECTING |
5290 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5291 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5298 if (block_allowed) {
5299 error = sbwait(&so->so_rcv);
5304 goto restart_nosblocks;
5306 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5307 error = EWOULDBLOCK;
5311 if (hold_sblock == 1) {
5312 SOCKBUF_UNLOCK(&so->so_rcv);
5315 /* we possibly have data we can read */
5316 /* sa_ignore FREED_MEMORY */
5317 control = TAILQ_FIRST(&inp->read_queue);
5318 if (control == NULL) {
5320 * This could be happening since the appender did the
5321 * increment but as not yet did the tailq insert onto the
5324 if (hold_rlock == 0) {
5325 SCTP_INP_READ_LOCK(inp);
5327 control = TAILQ_FIRST(&inp->read_queue);
5328 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5330 panic("Huh, its non zero and nothing on control?");
5332 so->so_rcv.sb_cc = 0;
5334 SCTP_INP_READ_UNLOCK(inp);
5338 if ((control->length == 0) &&
5339 (control->do_not_ref_stcb)) {
5341 * Clean up code for freeing assoc that left behind a
5342 * pdapi.. maybe a peer in EEOR that just closed after
5343 * sending and never indicated a EOR.
5345 if (hold_rlock == 0) {
5347 SCTP_INP_READ_LOCK(inp);
5349 control->held_length = 0;
5350 if (control->data) {
5351 /* Hmm there is data here .. fix */
5355 m_tmp = control->data;
5357 cnt += SCTP_BUF_LEN(m_tmp);
5358 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5359 control->tail_mbuf = m_tmp;
5360 control->end_added = 1;
5362 m_tmp = SCTP_BUF_NEXT(m_tmp);
5364 control->length = cnt;
5367 TAILQ_REMOVE(&inp->read_queue, control, next);
5368 /* Add back any hiddend data */
5369 sctp_free_remote_addr(control->whoFrom);
5370 sctp_free_a_readq(stcb, control);
5374 SCTP_INP_READ_UNLOCK(inp);
5378 if ((control->length == 0) &&
5379 (control->end_added == 1)) {
5381 * Do we also need to check for (control->pdapi_aborted ==
5384 if (hold_rlock == 0) {
5386 SCTP_INP_READ_LOCK(inp);
5388 TAILQ_REMOVE(&inp->read_queue, control, next);
5389 if (control->data) {
5391 panic("control->data not null but control->length == 0");
5393 SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5394 sctp_m_freem(control->data);
5395 control->data = NULL;
5398 if (control->aux_data) {
5399 sctp_m_free(control->aux_data);
5400 control->aux_data = NULL;
5403 if (control->on_strm_q) {
5404 panic("About to free ctl:%p so:%p and its in %d",
5405 control, so, control->on_strm_q);
5408 sctp_free_remote_addr(control->whoFrom);
5409 sctp_free_a_readq(stcb, control);
5412 SCTP_INP_READ_UNLOCK(inp);
5416 if (control->length == 0) {
5417 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5419 /* find a more suitable one then this */
5420 ctl = TAILQ_NEXT(control, next);
5422 if ((ctl->stcb != control->stcb) && (ctl->length) &&
5424 (ctl->spec_flags & M_NOTIFICATION) ||
5425 ((ctl->do_not_ref_stcb == 0) &&
5426 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
5429 * If we have a different TCB next, and there is data
5430 * present. If we have already taken some (pdapi), OR we can
5431 * ref the tcb and no delivery as started on this stream, we
5432 * take it. Note we allow a notification on a different
5433 * assoc to be delivered..
5437 } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
5439 ((ctl->some_taken) ||
5440 ((ctl->do_not_ref_stcb == 0) &&
5441 ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5442 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
5444 * If we have the same tcb, and there is data present, and we
5445 * have the strm interleave feature present. Then if we have
5446 * taken some (pdapi) or we can refer to tht tcb AND we have
5447 * not started a delivery for this stream, we can take it.
5448 * Note we do NOT allow a notificaiton on the same assoc to
5454 ctl = TAILQ_NEXT(ctl, next);
5458 * if we reach here, not suitable replacement is available
5459 * <or> fragment interleave is NOT on. So stuff the sb_cc
5460 * into the our held count, and its time to sleep again.
5462 held_length = so->so_rcv.sb_cc;
5463 control->held_length = so->so_rcv.sb_cc;
5466 /* Clear the held length since there is something to read */
5467 control->held_length = 0;
5470 * If we reach here, control has a some data for us to read off.
5471 * Note that stcb COULD be NULL.
5473 if (hold_rlock == 0) {
5475 SCTP_INP_READ_LOCK(inp);
5477 control->some_taken++;
5478 stcb = control->stcb;
5480 if ((control->do_not_ref_stcb == 0) &&
5481 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5482 if (freecnt_applied == 0)
5484 } else if (control->do_not_ref_stcb == 0) {
5485 /* you can't free it on me please */
5487 * The lock on the socket buffer protects us so the
5488 * free code will stop. But since we used the
5489 * socketbuf lock and the sender uses the tcb_lock
5490 * to increment, we need to use the atomic add to
5493 if (freecnt_applied) {
5495 panic("refcnt already incremented");
5497 SCTP_PRINTF("refcnt already incremented?\n");
5500 atomic_add_int(&stcb->asoc.refcnt, 1);
5501 freecnt_applied = 1;
5504 * Setup to remember how much we have not yet told
5505 * the peer our rwnd has opened up. Note we grab the
5506 * value from the tcb from last time. Note too that
5507 * sack sending clears this when a sack is sent,
5508 * which is fine. Once we hit the rwnd_req, we then
5509 * will go to the sctp_user_rcvd() that will not
5510 * lock until it KNOWs it MUST send a WUP-SACK.
5512 freed_so_far = stcb->freed_by_sorcv_sincelast;
5513 stcb->freed_by_sorcv_sincelast = 0;
5517 ((control->spec_flags & M_NOTIFICATION) == 0) &&
5518 control->do_not_ref_stcb == 0) {
5519 stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5521 /* First lets get off the sinfo and sockaddr info */
5522 if ((sinfo != NULL) && (filling_sinfo != 0)) {
5523 sinfo->sinfo_stream = control->sinfo_stream;
5524 sinfo->sinfo_ssn = (uint16_t)control->mid;
5525 sinfo->sinfo_flags = control->sinfo_flags;
5526 sinfo->sinfo_ppid = control->sinfo_ppid;
5527 sinfo->sinfo_context = control->sinfo_context;
5528 sinfo->sinfo_timetolive = control->sinfo_timetolive;
5529 sinfo->sinfo_tsn = control->sinfo_tsn;
5530 sinfo->sinfo_cumtsn = control->sinfo_cumtsn;
5531 sinfo->sinfo_assoc_id = control->sinfo_assoc_id;
5532 nxt = TAILQ_NEXT(control, next);
5533 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
5534 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
5535 struct sctp_extrcvinfo *s_extra;
5537 s_extra = (struct sctp_extrcvinfo *)sinfo;
5540 s_extra->serinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5541 if (nxt->sinfo_flags & SCTP_UNORDERED) {
5542 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5544 if (nxt->spec_flags & M_NOTIFICATION) {
5545 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5547 s_extra->serinfo_next_aid = nxt->sinfo_assoc_id;
5548 s_extra->serinfo_next_length = nxt->length;
5549 s_extra->serinfo_next_ppid = nxt->sinfo_ppid;
5550 s_extra->serinfo_next_stream = nxt->sinfo_stream;
5551 if (nxt->tail_mbuf != NULL) {
5552 if (nxt->end_added) {
5553 s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5558 * we explicitly 0 this, since the memcpy
5559 * got some other things beyond the older
5560 * sinfo_ that is on the control's structure
5564 s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
5565 s_extra->serinfo_next_aid = 0;
5566 s_extra->serinfo_next_length = 0;
5567 s_extra->serinfo_next_ppid = 0;
5568 s_extra->serinfo_next_stream = 0;
5572 * update off the real current cum-ack, if we have an stcb.
5574 if ((control->do_not_ref_stcb == 0) && stcb)
5575 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5577 * mask off the high bits, we keep the actual chunk bits in
5580 sinfo->sinfo_flags &= 0x00ff;
5581 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
5582 sinfo->sinfo_flags |= SCTP_UNORDERED;
5585 #ifdef SCTP_ASOCLOG_OF_TSNS
5587 int index, newindex;
5588 struct sctp_pcbtsn_rlog *entry;
5591 index = inp->readlog_index;
5592 newindex = index + 1;
5593 if (newindex >= SCTP_READ_LOG_SIZE) {
5596 } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
5597 entry = &inp->readlog[index];
5598 entry->vtag = control->sinfo_assoc_id;
5599 entry->strm = control->sinfo_stream;
5600 entry->seq = (uint16_t)control->mid;
5601 entry->sz = control->length;
5602 entry->flgs = control->sinfo_flags;
5605 if ((fromlen > 0) && (from != NULL)) {
5606 union sctp_sockstore store;
5609 switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5612 len = sizeof(struct sockaddr_in6);
5613 store.sin6 = control->whoFrom->ro._l_addr.sin6;
5614 store.sin6.sin6_port = control->port_from;
5620 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
5621 len = sizeof(struct sockaddr_in6);
5622 in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
5624 store.sin6.sin6_port = control->port_from;
5626 len = sizeof(struct sockaddr_in);
5627 store.sin = control->whoFrom->ro._l_addr.sin;
5628 store.sin.sin_port = control->port_from;
5631 len = sizeof(struct sockaddr_in);
5632 store.sin = control->whoFrom->ro._l_addr.sin;
5633 store.sin.sin_port = control->port_from;
5641 memcpy(from, &store, min((size_t)fromlen, len));
5644 struct sockaddr_in6 lsa6, *from6;
5646 from6 = (struct sockaddr_in6 *)from;
5647 sctp_recover_scope_mac(from6, (&lsa6));
5652 SCTP_INP_READ_UNLOCK(inp);
5656 SOCKBUF_UNLOCK(&so->so_rcv);
5659 /* now copy out what data we can */
5661 /* copy out each mbuf in the chain up to length */
5665 /* Move out all we can */
5666 cp_len = (int)uio->uio_resid;
5667 my_len = (int)SCTP_BUF_LEN(m);
5668 if (cp_len > my_len) {
5669 /* not enough in this buf */
5673 SCTP_INP_READ_UNLOCK(inp);
5677 error = uiomove(mtod(m, char *), cp_len, uio);
5679 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5682 if ((control->do_not_ref_stcb == 0) && stcb &&
5683 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5687 /* error we are out of here */
5690 SCTP_INP_READ_LOCK(inp);
5692 if (cp_len == SCTP_BUF_LEN(m)) {
5693 if ((SCTP_BUF_NEXT(m) == NULL) &&
5694 (control->end_added)) {
5695 out_flags |= MSG_EOR;
5696 if ((control->do_not_ref_stcb == 0) &&
5697 (control->stcb != NULL) &&
5698 ((control->spec_flags & M_NOTIFICATION) == 0))
5699 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5701 if (control->spec_flags & M_NOTIFICATION) {
5702 out_flags |= MSG_NOTIFICATION;
5704 /* we ate up the mbuf */
5705 if (in_flags & MSG_PEEK) {
5707 m = SCTP_BUF_NEXT(m);
5708 copied_so_far += cp_len;
5710 /* dispose of the mbuf */
5711 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5712 sctp_sblog(&so->so_rcv,
5713 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5715 sctp_sbfree(control, stcb, &so->so_rcv, m);
5716 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5717 sctp_sblog(&so->so_rcv,
5718 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5720 copied_so_far += cp_len;
5721 freed_so_far += cp_len;
5722 freed_so_far += MSIZE;
5723 atomic_subtract_int(&control->length, cp_len);
5724 control->data = sctp_m_free(m);
5727 * been through it all, must hold sb
5728 * lock ok to null tail
5730 if (control->data == NULL) {
5732 if ((control->end_added == 0) ||
5733 (TAILQ_NEXT(control, next) == NULL)) {
5741 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5742 panic("Hmm we don't own the lock?");
5746 control->tail_mbuf = NULL;
5748 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5749 panic("end_added, nothing left and no MSG_EOR");
5755 /* Do we need to trim the mbuf? */
5756 if (control->spec_flags & M_NOTIFICATION) {
5757 out_flags |= MSG_NOTIFICATION;
5759 if ((in_flags & MSG_PEEK) == 0) {
5760 SCTP_BUF_RESV_UF(m, cp_len);
5761 SCTP_BUF_LEN(m) -= cp_len;
5762 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5763 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
5765 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5766 if ((control->do_not_ref_stcb == 0) &&
5768 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5770 copied_so_far += cp_len;
5771 freed_so_far += cp_len;
5772 freed_so_far += MSIZE;
5773 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5774 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5775 SCTP_LOG_SBRESULT, 0);
5777 atomic_subtract_int(&control->length, cp_len);
5779 copied_so_far += cp_len;
5782 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5785 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5786 (control->do_not_ref_stcb == 0) &&
5787 (freed_so_far >= rwnd_req)) {
5788 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5790 } /* end while(m) */
5792 * At this point we have looked at it all and we either have
5793 * a MSG_EOR/or read all the user wants... <OR>
5794 * control->length == 0.
5796 if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5797 /* we are done with this control */
5798 if (control->length == 0) {
5799 if (control->data) {
5801 panic("control->data not null at read eor?");
5803 SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5804 sctp_m_freem(control->data);
5805 control->data = NULL;
5809 if (hold_rlock == 0) {
5810 SCTP_INP_READ_LOCK(inp);
5813 TAILQ_REMOVE(&inp->read_queue, control, next);
5814 /* Add back any hiddend data */
5815 if (control->held_length) {
5817 control->held_length = 0;
5818 wakeup_read_socket = 1;
5820 if (control->aux_data) {
5821 sctp_m_free(control->aux_data);
5822 control->aux_data = NULL;
5824 no_rcv_needed = control->do_not_ref_stcb;
5825 sctp_free_remote_addr(control->whoFrom);
5826 control->data = NULL;
5828 if (control->on_strm_q) {
5829 panic("About to free ctl:%p so:%p and its in %d",
5830 control, so, control->on_strm_q);
5833 sctp_free_a_readq(stcb, control);
5835 if ((freed_so_far >= rwnd_req) &&
5836 (no_rcv_needed == 0))
5837 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5841 * The user did not read all of this
5842 * message, turn off the returned MSG_EOR
5843 * since we are leaving more behind on the
5847 if (control->end_added &&
5848 (control->data == NULL) &&
5849 (control->tail_mbuf == NULL)) {
5850 panic("Gak, control->length is corrupt?");
5853 no_rcv_needed = control->do_not_ref_stcb;
5854 out_flags &= ~MSG_EOR;
5857 if (out_flags & MSG_EOR) {
5860 if ((uio->uio_resid == 0) ||
5862 (copied_so_far >= (uint32_t)max(so->so_rcv.sb_lowat, 1)))) {
5866 * If I hit here the receiver wants more and this message is
5867 * NOT done (pd-api). So two questions. Can we block? if not
5868 * we are done. Did the user NOT set MSG_WAITALL?
5870 if (block_allowed == 0) {
5874 * We need to wait for more data a few things: - We don't
5875 * sbunlock() so we don't get someone else reading. - We
5876 * must be sure to account for the case where what is added
5877 * is NOT to our control when we wakeup.
5881 * Do we need to tell the transport a rwnd update might be
5882 * needed before we go to sleep?
5884 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5885 ((freed_so_far >= rwnd_req) &&
5886 (control->do_not_ref_stcb == 0) &&
5887 (no_rcv_needed == 0))) {
5888 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5891 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5894 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5897 if (hold_rlock == 1) {
5898 SCTP_INP_READ_UNLOCK(inp);
5901 if (hold_sblock == 0) {
5902 SOCKBUF_LOCK(&so->so_rcv);
5905 if ((copied_so_far) && (control->length == 0) &&
5906 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
5909 if (so->so_rcv.sb_cc <= control->held_length) {
5910 error = sbwait(&so->so_rcv);
5914 control->held_length = 0;
5917 SOCKBUF_UNLOCK(&so->so_rcv);
5920 if (control->length == 0) {
5921 /* still nothing here */
5922 if (control->end_added == 1) {
5923 /* he aborted, or is done i.e.did a shutdown */
5924 out_flags |= MSG_EOR;
5925 if (control->pdapi_aborted) {
5926 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5927 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5929 out_flags |= MSG_TRUNC;
5931 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5932 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5934 goto done_with_control;
5936 if (so->so_rcv.sb_cc > held_length) {
5937 control->held_length = so->so_rcv.sb_cc;
5940 goto wait_some_more;
5941 } else if (control->data == NULL) {
5943 * we must re-sync since data is probably being
5946 SCTP_INP_READ_LOCK(inp);
5947 if ((control->length > 0) && (control->data == NULL)) {
5949 * big trouble.. we have the lock and its
5953 panic("Impossible data==NULL length !=0");
5955 out_flags |= MSG_EOR;
5956 out_flags |= MSG_TRUNC;
5957 control->length = 0;
5958 SCTP_INP_READ_UNLOCK(inp);
5959 goto done_with_control;
5961 SCTP_INP_READ_UNLOCK(inp);
5962 /* We will fall around to get more data */
5967 * Give caller back the mbuf chain,
5968 * store in uio_resid the length
5970 wakeup_read_socket = 0;
5971 if ((control->end_added == 0) ||
5972 (TAILQ_NEXT(control, next) == NULL)) {
5973 /* Need to get rlock */
5974 if (hold_rlock == 0) {
5975 SCTP_INP_READ_LOCK(inp);
5979 if (control->end_added) {
5980 out_flags |= MSG_EOR;
5981 if ((control->do_not_ref_stcb == 0) &&
5982 (control->stcb != NULL) &&
5983 ((control->spec_flags & M_NOTIFICATION) == 0))
5984 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5986 if (control->spec_flags & M_NOTIFICATION) {
5987 out_flags |= MSG_NOTIFICATION;
5989 uio->uio_resid = control->length;
5990 *mp = control->data;
5993 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5994 sctp_sblog(&so->so_rcv,
5995 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5997 sctp_sbfree(control, stcb, &so->so_rcv, m);
5998 freed_so_far += SCTP_BUF_LEN(m);
5999 freed_so_far += MSIZE;
6000 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6001 sctp_sblog(&so->so_rcv,
6002 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
6004 m = SCTP_BUF_NEXT(m);
6006 control->data = control->tail_mbuf = NULL;
6007 control->length = 0;
6008 if (out_flags & MSG_EOR) {
6009 /* Done with this control */
6010 goto done_with_control;
6014 if (hold_rlock == 1) {
6015 SCTP_INP_READ_UNLOCK(inp);
6018 if (hold_sblock == 1) {
6019 SOCKBUF_UNLOCK(&so->so_rcv);
6022 sbunlock(&so->so_rcv);
6027 SOCKBUF_UNLOCK(&so->so_rcv);
6030 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6031 if ((freed_so_far >= rwnd_req) &&
6032 (control && (control->do_not_ref_stcb == 0)) &&
6033 (no_rcv_needed == 0))
6034 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6038 *msg_flags = out_flags;
6040 if (((out_flags & MSG_EOR) == 0) &&
6041 ((in_flags & MSG_PEEK) == 0) &&
6043 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6044 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
6045 struct sctp_extrcvinfo *s_extra;
6047 s_extra = (struct sctp_extrcvinfo *)sinfo;
6048 s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
6050 if (hold_rlock == 1) {
6051 SCTP_INP_READ_UNLOCK(inp);
6054 SOCKBUF_UNLOCK(&so->so_rcv);
6057 sbunlock(&so->so_rcv);
6059 if (freecnt_applied) {
6061 * The lock on the socket buffer protects us so the free
6062 * code will stop. But since we used the socketbuf lock and
6063 * the sender uses the tcb_lock to increment, we need to use
6064 * the atomic add to the refcnt.
6068 panic("stcb for refcnt has gone NULL?");
6074 /* Save the value back for next time */
6075 stcb->freed_by_sorcv_sincelast = freed_so_far;
6076 atomic_add_int(&stcb->asoc.refcnt, -1);
6078 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6080 sctp_misc_ints(SCTP_SORECV_DONE,
6082 (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6086 sctp_misc_ints(SCTP_SORECV_DONE,
6088 (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6094 if (wakeup_read_socket) {
6095 sctp_sorwakeup(inp, so);
6101 #ifdef SCTP_MBUF_LOGGING
6103 sctp_m_free(struct mbuf *m)
6105 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6106 sctp_log_mb(m, SCTP_MBUF_IFREE);
6112 sctp_m_freem(struct mbuf *mb)
6115 mb = sctp_m_free(mb);
6121 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
6124 * Given a local address. For all associations that holds the
6125 * address, request a peer-set-primary.
6127 struct sctp_ifa *ifa;
6128 struct sctp_laddr *wi;
6130 ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
6132 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
6133 return (EADDRNOTAVAIL);
6136 * Now that we have the ifa we must awaken the iterator with this
6139 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6141 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
6144 /* Now incr the count and int wi structure */
6145 SCTP_INCR_LADDR_COUNT();
6146 memset(wi, 0, sizeof(*wi));
6147 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
6149 wi->action = SCTP_SET_PRIM_ADDR;
6150 atomic_add_int(&ifa->refcount, 1);
6152 /* Now add it to the work queue */
6153 SCTP_WQ_ADDR_LOCK();
6155 * Should this really be a tailq? As it is we will process the
6158 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6159 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
6160 (struct sctp_inpcb *)NULL,
6161 (struct sctp_tcb *)NULL,
6162 (struct sctp_nets *)NULL);
6163 SCTP_WQ_ADDR_UNLOCK();
6169 sctp_soreceive(struct socket *so,
6170 struct sockaddr **psa,
6173 struct mbuf **controlp,
6177 uint8_t sockbuf[256];
6178 struct sockaddr *from;
6179 struct sctp_extrcvinfo sinfo;
6180 int filling_sinfo = 1;
6182 struct sctp_inpcb *inp;
6184 inp = (struct sctp_inpcb *)so->so_pcb;
6185 /* pickup the assoc we are reading from */
6187 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6190 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6191 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6192 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6193 (controlp == NULL)) {
6194 /* user does not want the sndrcv ctl */
6198 from = (struct sockaddr *)sockbuf;
6199 fromlen = sizeof(sockbuf);
6206 if (filling_sinfo) {
6207 memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6209 if (flagsp != NULL) {
6214 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, &flags,
6215 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6216 if (flagsp != NULL) {
6219 if (controlp != NULL) {
6220 /* copy back the sinfo in a CMSG format */
6221 if (filling_sinfo && ((flags & MSG_NOTIFICATION) == 0)) {
6222 *controlp = sctp_build_ctl_nchunk(inp,
6223 (struct sctp_sndrcvinfo *)&sinfo);
6229 /* copy back the address info */
6230 if (from && from->sa_len) {
6231 *psa = sodupsockaddr(from, M_NOWAIT);
6244 sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6245 int totaddr, int *error)
6249 struct sctp_inpcb *inp;
6250 struct sockaddr *sa;
6253 struct sockaddr_in *sin;
6256 struct sockaddr_in6 *sin6;
6260 inp = stcb->sctp_ep;
6262 for (i = 0; i < totaddr; i++) {
6263 switch (sa->sa_family) {
6266 incr = sizeof(struct sockaddr_in);
6267 sin = (struct sockaddr_in *)sa;
6268 if ((sin->sin_addr.s_addr == INADDR_ANY) ||
6269 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
6270 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
6271 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6272 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6273 SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
6277 if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
6278 SCTP_DONOT_SETSCOPE,
6279 SCTP_ADDR_IS_CONFIRMED)) {
6280 /* assoc gone no un-lock */
6281 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6282 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6283 SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
6292 incr = sizeof(struct sockaddr_in6);
6293 sin6 = (struct sockaddr_in6 *)sa;
6294 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
6295 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
6296 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6297 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6298 SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
6302 if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
6303 SCTP_DONOT_SETSCOPE,
6304 SCTP_ADDR_IS_CONFIRMED)) {
6305 /* assoc gone no un-lock */
6306 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6307 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6308 SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
6318 sa = (struct sockaddr *)((caddr_t)sa + incr);
6325 sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6326 unsigned int *totaddr,
6327 unsigned int *num_v4, unsigned int *num_v6, int *error,
6328 unsigned int limit, int *bad_addr)
6330 struct sockaddr *sa;
6331 struct sctp_tcb *stcb = NULL;
6332 unsigned int incr, at, i;
6336 *error = *num_v6 = *num_v4 = 0;
6337 /* account and validate addresses */
6338 for (i = 0; i < *totaddr; i++) {
6339 switch (sa->sa_family) {
6342 incr = (unsigned int)sizeof(struct sockaddr_in);
6343 if (sa->sa_len != incr) {
6344 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6355 struct sockaddr_in6 *sin6;
6357 sin6 = (struct sockaddr_in6 *)sa;
6358 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6359 /* Must be non-mapped for connectx */
6360 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6365 incr = (unsigned int)sizeof(struct sockaddr_in6);
6366 if (sa->sa_len != incr) {
6367 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6382 if (i == *totaddr) {
6385 SCTP_INP_INCR_REF(inp);
6386 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
6388 /* Already have or am bring up an association */
6391 SCTP_INP_DECR_REF(inp);
6393 if ((at + incr) > limit) {
6397 sa = (struct sockaddr *)((caddr_t)sa + incr);
6399 return ((struct sctp_tcb *)NULL);
6403 * sctp_bindx(ADD) for one address.
6404 * assumes all arguments are valid/checked by caller.
6407 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
6408 struct sockaddr *sa, sctp_assoc_t assoc_id,
6409 uint32_t vrf_id, int *error, void *p)
6411 struct sockaddr *addr_touse;
6412 #if defined(INET) && defined(INET6)
6413 struct sockaddr_in sin;
6416 /* see if we're bound all already! */
6417 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6418 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6424 if (sa->sa_family == AF_INET6) {
6426 struct sockaddr_in6 *sin6;
6429 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6430 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6434 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6435 /* can only bind v6 on PF_INET6 sockets */
6436 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6441 sin6 = (struct sockaddr_in6 *)addr_touse;
6442 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6443 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6444 SCTP_IPV6_V6ONLY(inp)) {
6445 /* can't bind v4-mapped on PF_INET sockets */
6446 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6450 in6_sin6_2_sin(&sin, sin6);
6451 addr_touse = (struct sockaddr *)&sin;
6457 if (sa->sa_family == AF_INET) {
6458 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6459 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6463 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6464 SCTP_IPV6_V6ONLY(inp)) {
6465 /* can't bind v4 on PF_INET sockets */
6466 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6472 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
6474 /* Can't get proc for Net/Open BSD */
6475 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6479 *error = sctp_inpcb_bind(so, addr_touse, NULL, p);
6483 * No locks required here since bind and mgmt_ep_sa all do their own
6484 * locking. If we do something for the FIX: below we may need to
6485 * lock in that case.
6487 if (assoc_id == 0) {
6488 /* add the address */
6489 struct sctp_inpcb *lep;
6490 struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
6492 /* validate the incoming port */
6493 if ((lsin->sin_port != 0) &&
6494 (lsin->sin_port != inp->sctp_lport)) {
6495 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6499 /* user specified 0 port, set it to existing port */
6500 lsin->sin_port = inp->sctp_lport;
6503 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
6506 * We must decrement the refcount since we have the
6507 * ep already and are binding. No remove going on
6510 SCTP_INP_DECR_REF(lep);
6513 /* already bound to it.. ok */
6515 } else if (lep == NULL) {
6516 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
6517 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6518 SCTP_ADD_IP_ADDRESS,
6521 *error = EADDRINUSE;
6527 * FIX: decide whether we allow assoc based bindx
6533 * sctp_bindx(DELETE) for one address.
6534 * assumes all arguments are valid/checked by caller.
6537 sctp_bindx_delete_address(struct sctp_inpcb *inp,
6538 struct sockaddr *sa, sctp_assoc_t assoc_id,
6539 uint32_t vrf_id, int *error)
6541 struct sockaddr *addr_touse;
6542 #if defined(INET) && defined(INET6)
6543 struct sockaddr_in sin;
6546 /* see if we're bound all already! */
6547 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6548 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6554 if (sa->sa_family == AF_INET6) {
6556 struct sockaddr_in6 *sin6;
6559 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6560 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6564 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6565 /* can only bind v6 on PF_INET6 sockets */
6566 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6571 sin6 = (struct sockaddr_in6 *)addr_touse;
6572 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6573 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6574 SCTP_IPV6_V6ONLY(inp)) {
6575 /* can't bind mapped-v4 on PF_INET sockets */
6576 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6580 in6_sin6_2_sin(&sin, sin6);
6581 addr_touse = (struct sockaddr *)&sin;
6587 if (sa->sa_family == AF_INET) {
6588 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6589 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6593 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6594 SCTP_IPV6_V6ONLY(inp)) {
6595 /* can't bind v4 on PF_INET sockets */
6596 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6603 * No lock required mgmt_ep_sa does its own locking. If the FIX:
6604 * below is ever changed we may need to lock before calling
6605 * association level binding.
6607 if (assoc_id == 0) {
6608 /* delete the address */
6609 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6610 SCTP_DEL_IP_ADDRESS,
6614 * FIX: decide whether we allow assoc based bindx
6620 * returns the valid local address count for an assoc, taking into account
6624 sctp_local_addr_count(struct sctp_tcb *stcb)
6628 int ipv4_local_scope, ipv4_addr_legal;
6631 int local_scope, site_scope, ipv6_addr_legal;
6633 struct sctp_vrf *vrf;
6634 struct sctp_ifn *sctp_ifn;
6635 struct sctp_ifa *sctp_ifa;
6638 /* Turn on all the appropriate scopes */
6639 loopback_scope = stcb->asoc.scope.loopback_scope;
6641 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
6642 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
6645 local_scope = stcb->asoc.scope.local_scope;
6646 site_scope = stcb->asoc.scope.site_scope;
6647 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
6649 SCTP_IPI_ADDR_RLOCK();
6650 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
6652 /* no vrf, no addresses */
6653 SCTP_IPI_ADDR_RUNLOCK();
6656 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6658 * bound all case: go through all ifns on the vrf
6660 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
6661 if ((loopback_scope == 0) &&
6662 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
6665 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
6666 if (sctp_is_addr_restricted(stcb, sctp_ifa))
6668 switch (sctp_ifa->address.sa.sa_family) {
6671 if (ipv4_addr_legal) {
6672 struct sockaddr_in *sin;
6674 sin = &sctp_ifa->address.sin;
6675 if (sin->sin_addr.s_addr == 0) {
6682 if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
6683 &sin->sin_addr) != 0) {
6686 if ((ipv4_local_scope == 0) &&
6687 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
6690 /* count this one */
6699 if (ipv6_addr_legal) {
6700 struct sockaddr_in6 *sin6;
6702 sin6 = &sctp_ifa->address.sin6;
6703 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6706 if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
6707 &sin6->sin6_addr) != 0) {
6710 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
6711 if (local_scope == 0)
6713 if (sin6->sin6_scope_id == 0) {
6714 if (sa6_recoverscope(sin6) != 0)
6727 if ((site_scope == 0) &&
6728 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
6731 /* count this one */
6746 struct sctp_laddr *laddr;
6748 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
6750 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
6753 /* count this one */
6757 SCTP_IPI_ADDR_RUNLOCK();
6761 #if defined(SCTP_LOCAL_TRACE_BUF)
6764 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)
6766 uint32_t saveindex, newindex;
6769 saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6770 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6773 newindex = saveindex + 1;
6775 } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6776 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6779 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6780 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6781 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6782 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6783 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6784 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6785 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6786 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6791 sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
6792 const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
6796 struct ip6_hdr *ip6;
6798 struct mbuf *sp, *last;
6799 struct udphdr *uhdr;
6802 if ((m->m_flags & M_PKTHDR) == 0) {
6803 /* Can't handle one that is not a pkt hdr */
6806 /* Pull the src port */
6807 iph = mtod(m, struct ip *);
6808 uhdr = (struct udphdr *)((caddr_t)iph + off);
6809 port = uhdr->uh_sport;
6811 * Split out the mbuf chain. Leave the IP header in m, place the
6814 sp = m_split(m, off, M_NOWAIT);
6816 /* Gak, drop packet, we can't do a split */
6819 if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
6820 /* Gak, packet can't have an SCTP header in it - too small */
6824 /* Now pull up the UDP header and SCTP header together */
6825 sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
6827 /* Gak pullup failed */
6830 /* Trim out the UDP header */
6831 m_adj(sp, sizeof(struct udphdr));
6833 /* Now reconstruct the mbuf chain */
6834 for (last = m; last->m_next; last = last->m_next);
6836 m->m_pkthdr.len += sp->m_pkthdr.len;
6838 * The CSUM_DATA_VALID flags indicates that the HW checked the UDP
6839 * checksum and it was valid. Since CSUM_DATA_VALID ==
6840 * CSUM_SCTP_VALID this would imply that the HW also verified the
6841 * SCTP checksum. Therefore, clear the bit.
6843 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6844 "sctp_recv_udp_tunneled_packet(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6846 if_name(m->m_pkthdr.rcvif),
6847 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6848 m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
6849 iph = mtod(m, struct ip *);
6850 switch (iph->ip_v) {
6853 iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
6854 sctp_input_with_port(m, off, port);
6858 case IPV6_VERSION >> 4:
6859 ip6 = mtod(m, struct ip6_hdr *);
6860 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
6861 sctp6_input_with_port(&m, &off, port);
6875 sctp_recv_icmp_tunneled_packet(int cmd, struct sockaddr *sa, void *vip, void *ctx SCTP_UNUSED)
6877 struct ip *outer_ip, *inner_ip;
6881 struct sctp_inpcb *inp;
6882 struct sctp_tcb *stcb;
6883 struct sctp_nets *net;
6884 struct sctp_init_chunk *ch;
6885 struct sockaddr_in src, dst;
6888 inner_ip = (struct ip *)vip;
6889 icmp = (struct icmp *)((caddr_t)inner_ip -
6890 (sizeof(struct icmp) - sizeof(struct ip)));
6891 outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
6892 if (ntohs(outer_ip->ip_len) <
6893 sizeof(struct ip) + 8 + (inner_ip->ip_hl << 2) + sizeof(struct udphdr) + 8) {
6896 udp = (struct udphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
6897 sh = (struct sctphdr *)(udp + 1);
6898 memset(&src, 0, sizeof(struct sockaddr_in));
6899 src.sin_family = AF_INET;
6900 src.sin_len = sizeof(struct sockaddr_in);
6901 src.sin_port = sh->src_port;
6902 src.sin_addr = inner_ip->ip_src;
6903 memset(&dst, 0, sizeof(struct sockaddr_in));
6904 dst.sin_family = AF_INET;
6905 dst.sin_len = sizeof(struct sockaddr_in);
6906 dst.sin_port = sh->dest_port;
6907 dst.sin_addr = inner_ip->ip_dst;
6909 * 'dst' holds the dest of the packet that failed to be sent. 'src'
6910 * holds our local endpoint address. Thus we reverse the dst and the
6911 * src in the lookup.
6915 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
6916 (struct sockaddr *)&src,
6918 SCTP_DEFAULT_VRFID);
6919 if ((stcb != NULL) &&
6922 /* Check the UDP port numbers */
6923 if ((udp->uh_dport != net->port) ||
6924 (udp->uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
6925 SCTP_TCB_UNLOCK(stcb);
6928 /* Check the verification tag */
6929 if (ntohl(sh->v_tag) != 0) {
6931 * This must be the verification tag used for
6932 * sending out packets. We don't consider packets
6933 * reflecting the verification tag.
6935 if (ntohl(sh->v_tag) != stcb->asoc.peer_vtag) {
6936 SCTP_TCB_UNLOCK(stcb);
6940 if (ntohs(outer_ip->ip_len) >=
6942 8 + (inner_ip->ip_hl << 2) + 8 + 20) {
6944 * In this case we can check if we got an
6945 * INIT chunk and if the initiate tag
6948 ch = (struct sctp_init_chunk *)(sh + 1);
6949 if ((ch->ch.chunk_type != SCTP_INITIATION) ||
6950 (ntohl(ch->init.initiate_tag) != stcb->asoc.my_vtag)) {
6951 SCTP_TCB_UNLOCK(stcb);
6955 SCTP_TCB_UNLOCK(stcb);
6959 type = icmp->icmp_type;
6960 code = icmp->icmp_code;
6961 if ((type == ICMP_UNREACH) &&
6962 (code == ICMP_UNREACH_PORT)) {
6963 code = ICMP_UNREACH_PROTOCOL;
6965 sctp_notify(inp, stcb, net, type, code,
6966 ntohs(inner_ip->ip_len),
6967 (uint32_t)ntohs(icmp->icmp_nextmtu));
6969 if ((stcb == NULL) && (inp != NULL)) {
6970 /* reduce ref-count */
6971 SCTP_INP_WLOCK(inp);
6972 SCTP_INP_DECR_REF(inp);
6973 SCTP_INP_WUNLOCK(inp);
6976 SCTP_TCB_UNLOCK(stcb);
6985 sctp_recv_icmp6_tunneled_packet(int cmd, struct sockaddr *sa, void *d, void *ctx SCTP_UNUSED)
6987 struct ip6ctlparam *ip6cp;
6988 struct sctp_inpcb *inp;
6989 struct sctp_tcb *stcb;
6990 struct sctp_nets *net;
6993 struct sockaddr_in6 src, dst;
6996 ip6cp = (struct ip6ctlparam *)d;
6998 * XXX: We assume that when IPV6 is non NULL, M and OFF are valid.
7000 if (ip6cp->ip6c_m == NULL) {
7004 * Check if we can safely examine the ports and the verification tag
7005 * of the SCTP common header.
7007 if (ip6cp->ip6c_m->m_pkthdr.len <
7008 ip6cp->ip6c_off + sizeof(struct udphdr) + offsetof(struct sctphdr, checksum)) {
7011 /* Copy out the UDP header. */
7012 memset(&udp, 0, sizeof(struct udphdr));
7013 m_copydata(ip6cp->ip6c_m,
7015 sizeof(struct udphdr),
7017 /* Copy out the port numbers and the verification tag. */
7018 memset(&sh, 0, sizeof(struct sctphdr));
7019 m_copydata(ip6cp->ip6c_m,
7020 ip6cp->ip6c_off + sizeof(struct udphdr),
7021 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
7023 memset(&src, 0, sizeof(struct sockaddr_in6));
7024 src.sin6_family = AF_INET6;
7025 src.sin6_len = sizeof(struct sockaddr_in6);
7026 src.sin6_port = sh.src_port;
7027 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
7028 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7031 memset(&dst, 0, sizeof(struct sockaddr_in6));
7032 dst.sin6_family = AF_INET6;
7033 dst.sin6_len = sizeof(struct sockaddr_in6);
7034 dst.sin6_port = sh.dest_port;
7035 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
7036 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7041 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7042 (struct sockaddr *)&src,
7043 &inp, &net, 1, SCTP_DEFAULT_VRFID);
7044 if ((stcb != NULL) &&
7047 /* Check the UDP port numbers */
7048 if ((udp.uh_dport != net->port) ||
7049 (udp.uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7050 SCTP_TCB_UNLOCK(stcb);
7053 /* Check the verification tag */
7054 if (ntohl(sh.v_tag) != 0) {
7056 * This must be the verification tag used for
7057 * sending out packets. We don't consider packets
7058 * reflecting the verification tag.
7060 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
7061 SCTP_TCB_UNLOCK(stcb);
7065 if (ip6cp->ip6c_m->m_pkthdr.len >=
7066 ip6cp->ip6c_off + sizeof(struct udphdr) +
7067 sizeof(struct sctphdr) +
7068 sizeof(struct sctp_chunkhdr) +
7069 offsetof(struct sctp_init, a_rwnd)) {
7071 * In this case we can check if we got an
7072 * INIT chunk and if the initiate tag
7075 uint32_t initiate_tag;
7078 m_copydata(ip6cp->ip6c_m,
7080 sizeof(struct udphdr) +
7081 sizeof(struct sctphdr),
7083 (caddr_t)&chunk_type);
7084 m_copydata(ip6cp->ip6c_m,
7086 sizeof(struct udphdr) +
7087 sizeof(struct sctphdr) +
7088 sizeof(struct sctp_chunkhdr),
7090 (caddr_t)&initiate_tag);
7091 if ((chunk_type != SCTP_INITIATION) ||
7092 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
7093 SCTP_TCB_UNLOCK(stcb);
7097 SCTP_TCB_UNLOCK(stcb);
7101 type = ip6cp->ip6c_icmp6->icmp6_type;
7102 code = ip6cp->ip6c_icmp6->icmp6_code;
7103 if ((type == ICMP6_DST_UNREACH) &&
7104 (code == ICMP6_DST_UNREACH_NOPORT)) {
7105 type = ICMP6_PARAM_PROB;
7106 code = ICMP6_PARAMPROB_NEXTHEADER;
7108 sctp6_notify(inp, stcb, net, type, code,
7109 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
7111 if ((stcb == NULL) && (inp != NULL)) {
7112 /* reduce inp's ref-count */
7113 SCTP_INP_WLOCK(inp);
7114 SCTP_INP_DECR_REF(inp);
7115 SCTP_INP_WUNLOCK(inp);
7118 SCTP_TCB_UNLOCK(stcb);
7125 sctp_over_udp_stop(void)
7128 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7132 if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7133 soclose(SCTP_BASE_INFO(udp4_tun_socket));
7134 SCTP_BASE_INFO(udp4_tun_socket) = NULL;
7138 if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
7139 soclose(SCTP_BASE_INFO(udp6_tun_socket));
7140 SCTP_BASE_INFO(udp6_tun_socket) = NULL;
7146 sctp_over_udp_start(void)
7151 struct sockaddr_in sin;
7154 struct sockaddr_in6 sin6;
7157 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7160 port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
7161 if (ntohs(port) == 0) {
7162 /* Must have a port set */
7166 if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7167 /* Already running -- must stop first */
7172 if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
7173 /* Already running -- must stop first */
7178 if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
7179 SOCK_DGRAM, IPPROTO_UDP,
7180 curthread->td_ucred, curthread))) {
7181 sctp_over_udp_stop();
7184 /* Call the special UDP hook. */
7185 if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
7186 sctp_recv_udp_tunneled_packet,
7187 sctp_recv_icmp_tunneled_packet,
7189 sctp_over_udp_stop();
7192 /* Ok, we have a socket, bind it to the port. */
7193 memset(&sin, 0, sizeof(struct sockaddr_in));
7194 sin.sin_len = sizeof(struct sockaddr_in);
7195 sin.sin_family = AF_INET;
7196 sin.sin_port = htons(port);
7197 if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
7198 (struct sockaddr *)&sin, curthread))) {
7199 sctp_over_udp_stop();
7204 if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
7205 SOCK_DGRAM, IPPROTO_UDP,
7206 curthread->td_ucred, curthread))) {
7207 sctp_over_udp_stop();
7210 /* Call the special UDP hook. */
7211 if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
7212 sctp_recv_udp_tunneled_packet,
7213 sctp_recv_icmp6_tunneled_packet,
7215 sctp_over_udp_stop();
7218 /* Ok, we have a socket, bind it to the port. */
7219 memset(&sin6, 0, sizeof(struct sockaddr_in6));
7220 sin6.sin6_len = sizeof(struct sockaddr_in6);
7221 sin6.sin6_family = AF_INET6;
7222 sin6.sin6_port = htons(port);
7223 if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
7224 (struct sockaddr *)&sin6, curthread))) {
7225 sctp_over_udp_stop();
7233 * sctp_min_mtu ()returns the minimum of all non-zero arguments.
7234 * If all arguments are zero, zero is returned.
7237 sctp_min_mtu(uint32_t mtu1, uint32_t mtu2, uint32_t mtu3)
7242 return (min(mtu1, min(mtu2, mtu3)));
7244 return (min(mtu1, mtu2));
7248 return (min(mtu1, mtu3));
7256 return (min(mtu2, mtu3));
7267 sctp_hc_set_mtu(union sctp_sockstore *addr, uint16_t fibnum, uint32_t mtu)
7269 struct in_conninfo inc;
7271 memset(&inc, 0, sizeof(struct in_conninfo));
7272 inc.inc_fibnum = fibnum;
7273 switch (addr->sa.sa_family) {
7276 inc.inc_faddr = addr->sin.sin_addr;
7281 inc.inc_flags |= INC_ISIPV6;
7282 inc.inc6_faddr = addr->sin6.sin6_addr;
7288 tcp_hc_updatemtu(&inc, (u_long)mtu);
7292 sctp_hc_get_mtu(union sctp_sockstore *addr, uint16_t fibnum)
7294 struct in_conninfo inc;
7296 memset(&inc, 0, sizeof(struct in_conninfo));
7297 inc.inc_fibnum = fibnum;
7298 switch (addr->sa.sa_family) {
7301 inc.inc_faddr = addr->sin.sin_addr;
7306 inc.inc_flags |= INC_ISIPV6;
7307 inc.inc6_faddr = addr->sin6.sin6_addr;
7313 return ((uint32_t)tcp_hc_getmtu(&inc));