2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctputil.c,v 1.37 2005/03/07 23:26:09 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctputil.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
43 #include <netinet/sctp_header.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_timer.h>
47 #include <netinet/sctp_indata.h>/* for sctp_deliver_data() */
48 #include <netinet/sctp_auth.h>
49 #include <netinet/sctp_asconf.h>
50 #include <netinet/sctp_cc_functions.h>
51 #include <netinet/sctp_bsd_addr.h>
53 #define NUMBER_OF_MTU_SIZES 18
57 #define KTR_SCTP KTR_SUBSYS
61 sctp_sblog(struct sockbuf *sb,
62 struct sctp_tcb *stcb, int from, int incr)
64 struct sctp_cwnd_log sctp_clog;
66 sctp_clog.x.sb.stcb = stcb;
67 sctp_clog.x.sb.so_sbcc = sb->sb_cc;
69 sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
71 sctp_clog.x.sb.stcb_sbcc = 0;
72 sctp_clog.x.sb.incr = incr;
73 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
76 sctp_clog.x.misc.log1,
77 sctp_clog.x.misc.log2,
78 sctp_clog.x.misc.log3,
79 sctp_clog.x.misc.log4);
83 sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
85 struct sctp_cwnd_log sctp_clog;
87 sctp_clog.x.close.inp = (void *)inp;
88 sctp_clog.x.close.sctp_flags = inp->sctp_flags;
90 sctp_clog.x.close.stcb = (void *)stcb;
91 sctp_clog.x.close.state = (uint16_t) stcb->asoc.state;
93 sctp_clog.x.close.stcb = 0;
94 sctp_clog.x.close.state = 0;
96 sctp_clog.x.close.loc = loc;
97 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
100 sctp_clog.x.misc.log1,
101 sctp_clog.x.misc.log2,
102 sctp_clog.x.misc.log3,
103 sctp_clog.x.misc.log4);
108 rto_logging(struct sctp_nets *net, int from)
110 struct sctp_cwnd_log sctp_clog;
112 memset(&sctp_clog, 0, sizeof(sctp_clog));
113 sctp_clog.x.rto.net = (void *)net;
114 sctp_clog.x.rto.rtt = net->prev_rtt;
115 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
118 sctp_clog.x.misc.log1,
119 sctp_clog.x.misc.log2,
120 sctp_clog.x.misc.log3,
121 sctp_clog.x.misc.log4);
126 sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
128 struct sctp_cwnd_log sctp_clog;
130 sctp_clog.x.strlog.stcb = stcb;
131 sctp_clog.x.strlog.n_tsn = tsn;
132 sctp_clog.x.strlog.n_sseq = sseq;
133 sctp_clog.x.strlog.e_tsn = 0;
134 sctp_clog.x.strlog.e_sseq = 0;
135 sctp_clog.x.strlog.strm = stream;
136 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
139 sctp_clog.x.misc.log1,
140 sctp_clog.x.misc.log2,
141 sctp_clog.x.misc.log3,
142 sctp_clog.x.misc.log4);
147 sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
149 struct sctp_cwnd_log sctp_clog;
151 sctp_clog.x.nagle.stcb = (void *)stcb;
152 sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
153 sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
154 sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
155 sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
156 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
157 SCTP_LOG_EVENT_NAGLE,
159 sctp_clog.x.misc.log1,
160 sctp_clog.x.misc.log2,
161 sctp_clog.x.misc.log3,
162 sctp_clog.x.misc.log4);
167 sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
169 struct sctp_cwnd_log sctp_clog;
171 sctp_clog.x.sack.cumack = cumack;
172 sctp_clog.x.sack.oldcumack = old_cumack;
173 sctp_clog.x.sack.tsn = tsn;
174 sctp_clog.x.sack.numGaps = gaps;
175 sctp_clog.x.sack.numDups = dups;
176 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
179 sctp_clog.x.misc.log1,
180 sctp_clog.x.misc.log2,
181 sctp_clog.x.misc.log3,
182 sctp_clog.x.misc.log4);
186 sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
188 struct sctp_cwnd_log sctp_clog;
190 memset(&sctp_clog, 0, sizeof(sctp_clog));
191 sctp_clog.x.map.base = map;
192 sctp_clog.x.map.cum = cum;
193 sctp_clog.x.map.high = high;
194 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
197 sctp_clog.x.misc.log1,
198 sctp_clog.x.misc.log2,
199 sctp_clog.x.misc.log3,
200 sctp_clog.x.misc.log4);
204 sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn,
207 struct sctp_cwnd_log sctp_clog;
209 memset(&sctp_clog, 0, sizeof(sctp_clog));
210 sctp_clog.x.fr.largest_tsn = biggest_tsn;
211 sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
212 sctp_clog.x.fr.tsn = tsn;
213 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
216 sctp_clog.x.misc.log1,
217 sctp_clog.x.misc.log2,
218 sctp_clog.x.misc.log3,
219 sctp_clog.x.misc.log4);
225 sctp_log_mb(struct mbuf *m, int from)
227 struct sctp_cwnd_log sctp_clog;
229 sctp_clog.x.mb.mp = m;
230 sctp_clog.x.mb.mbuf_flags = (uint8_t) (SCTP_BUF_GET_FLAGS(m));
231 sctp_clog.x.mb.size = (uint16_t) (SCTP_BUF_LEN(m));
232 sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
233 if (SCTP_BUF_IS_EXTENDED(m)) {
234 sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
235 sctp_clog.x.mb.refcnt = (uint8_t) (SCTP_BUF_EXTEND_REFCNT(m));
237 sctp_clog.x.mb.ext = 0;
238 sctp_clog.x.mb.refcnt = 0;
240 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
243 sctp_clog.x.misc.log1,
244 sctp_clog.x.misc.log2,
245 sctp_clog.x.misc.log3,
246 sctp_clog.x.misc.log4);
251 sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk,
254 struct sctp_cwnd_log sctp_clog;
256 if (control == NULL) {
257 SCTP_PRINTF("Gak log of NULL?\n");
260 sctp_clog.x.strlog.stcb = control->stcb;
261 sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
262 sctp_clog.x.strlog.n_sseq = control->sinfo_ssn;
263 sctp_clog.x.strlog.strm = control->sinfo_stream;
264 if (poschk != NULL) {
265 sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
266 sctp_clog.x.strlog.e_sseq = poschk->sinfo_ssn;
268 sctp_clog.x.strlog.e_tsn = 0;
269 sctp_clog.x.strlog.e_sseq = 0;
271 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
274 sctp_clog.x.misc.log1,
275 sctp_clog.x.misc.log2,
276 sctp_clog.x.misc.log3,
277 sctp_clog.x.misc.log4);
282 sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
284 struct sctp_cwnd_log sctp_clog;
286 sctp_clog.x.cwnd.net = net;
287 if (stcb->asoc.send_queue_cnt > 255)
288 sctp_clog.x.cwnd.cnt_in_send = 255;
290 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
291 if (stcb->asoc.stream_queue_cnt > 255)
292 sctp_clog.x.cwnd.cnt_in_str = 255;
294 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
297 sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
298 sctp_clog.x.cwnd.inflight = net->flight_size;
299 sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
300 sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
301 sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
303 if (SCTP_CWNDLOG_PRESEND == from) {
304 sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
306 sctp_clog.x.cwnd.cwnd_augment = augment;
307 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
310 sctp_clog.x.misc.log1,
311 sctp_clog.x.misc.log2,
312 sctp_clog.x.misc.log3,
313 sctp_clog.x.misc.log4);
318 sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
320 struct sctp_cwnd_log sctp_clog;
322 memset(&sctp_clog, 0, sizeof(sctp_clog));
324 sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
327 sctp_clog.x.lock.sock = (void *)NULL;
329 sctp_clog.x.lock.inp = (void *)inp;
331 sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
333 sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
336 sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
337 sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
339 sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
340 sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
342 sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
343 if (inp->sctp_socket) {
344 sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
345 sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
346 sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
348 sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
349 sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
350 sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
352 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
355 sctp_clog.x.misc.log1,
356 sctp_clog.x.misc.log2,
357 sctp_clog.x.misc.log3,
358 sctp_clog.x.misc.log4);
363 sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
365 struct sctp_cwnd_log sctp_clog;
367 memset(&sctp_clog, 0, sizeof(sctp_clog));
368 sctp_clog.x.cwnd.net = net;
369 sctp_clog.x.cwnd.cwnd_new_value = error;
370 sctp_clog.x.cwnd.inflight = net->flight_size;
371 sctp_clog.x.cwnd.cwnd_augment = burst;
372 if (stcb->asoc.send_queue_cnt > 255)
373 sctp_clog.x.cwnd.cnt_in_send = 255;
375 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
376 if (stcb->asoc.stream_queue_cnt > 255)
377 sctp_clog.x.cwnd.cnt_in_str = 255;
379 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
380 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
381 SCTP_LOG_EVENT_MAXBURST,
383 sctp_clog.x.misc.log1,
384 sctp_clog.x.misc.log2,
385 sctp_clog.x.misc.log3,
386 sctp_clog.x.misc.log4);
391 sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
393 struct sctp_cwnd_log sctp_clog;
395 sctp_clog.x.rwnd.rwnd = peers_rwnd;
396 sctp_clog.x.rwnd.send_size = snd_size;
397 sctp_clog.x.rwnd.overhead = overhead;
398 sctp_clog.x.rwnd.new_rwnd = 0;
399 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
402 sctp_clog.x.misc.log1,
403 sctp_clog.x.misc.log2,
404 sctp_clog.x.misc.log3,
405 sctp_clog.x.misc.log4);
409 sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
411 struct sctp_cwnd_log sctp_clog;
413 sctp_clog.x.rwnd.rwnd = peers_rwnd;
414 sctp_clog.x.rwnd.send_size = flight_size;
415 sctp_clog.x.rwnd.overhead = overhead;
416 sctp_clog.x.rwnd.new_rwnd = a_rwndval;
417 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
420 sctp_clog.x.misc.log1,
421 sctp_clog.x.misc.log2,
422 sctp_clog.x.misc.log3,
423 sctp_clog.x.misc.log4);
427 sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
429 struct sctp_cwnd_log sctp_clog;
431 sctp_clog.x.mbcnt.total_queue_size = total_oq;
432 sctp_clog.x.mbcnt.size_change = book;
433 sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
434 sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
435 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
436 SCTP_LOG_EVENT_MBCNT,
438 sctp_clog.x.misc.log1,
439 sctp_clog.x.misc.log2,
440 sctp_clog.x.misc.log3,
441 sctp_clog.x.misc.log4);
446 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
448 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
455 sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t cumtsn, uint32_t wake_cnt, int from)
457 struct sctp_cwnd_log sctp_clog;
459 sctp_clog.x.wake.stcb = (void *)stcb;
460 sctp_clog.x.wake.wake_cnt = wake_cnt;
461 sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
462 sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
463 sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
465 if (stcb->asoc.stream_queue_cnt < 0xff)
466 sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
468 sctp_clog.x.wake.stream_qcnt = 0xff;
470 if (stcb->asoc.chunks_on_out_queue < 0xff)
471 sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
473 sctp_clog.x.wake.chunks_on_oque = 0xff;
475 sctp_clog.x.wake.sctpflags = 0;
476 /* set in the defered mode stuff */
477 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
478 sctp_clog.x.wake.sctpflags |= 1;
479 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
480 sctp_clog.x.wake.sctpflags |= 2;
481 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
482 sctp_clog.x.wake.sctpflags |= 4;
483 /* what about the sb */
484 if (stcb->sctp_socket) {
485 struct socket *so = stcb->sctp_socket;
487 sctp_clog.x.wake.sbflags = (uint8_t) ((so->so_snd.sb_flags & 0x00ff));
489 sctp_clog.x.wake.sbflags = 0xff;
491 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
494 sctp_clog.x.misc.log1,
495 sctp_clog.x.misc.log2,
496 sctp_clog.x.misc.log3,
497 sctp_clog.x.misc.log4);
502 sctp_log_block(uint8_t from, struct socket *so, struct sctp_association *asoc, int sendlen)
504 struct sctp_cwnd_log sctp_clog;
506 sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
507 sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
508 sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
509 sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
510 sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
511 sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight / 1024);
512 sctp_clog.x.blk.sndlen = sendlen;
513 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
514 SCTP_LOG_EVENT_BLOCK,
516 sctp_clog.x.misc.log1,
517 sctp_clog.x.misc.log2,
518 sctp_clog.x.misc.log3,
519 sctp_clog.x.misc.log4);
524 sctp_fill_stat_log(void *optval, size_t *optsize)
526 /* May need to fix this if ktrdump does not work */
530 #ifdef SCTP_AUDITING_ENABLED
531 uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
532 static int sctp_audit_indx = 0;
536 sctp_print_audit_report(void)
542 for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
543 if ((sctp_audit_data[i][0] == 0xe0) &&
544 (sctp_audit_data[i][1] == 0x01)) {
547 } else if (sctp_audit_data[i][0] == 0xf0) {
550 } else if ((sctp_audit_data[i][0] == 0xc0) &&
551 (sctp_audit_data[i][1] == 0x01)) {
555 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
556 (uint32_t) sctp_audit_data[i][1]);
561 for (i = 0; i < sctp_audit_indx; i++) {
562 if ((sctp_audit_data[i][0] == 0xe0) &&
563 (sctp_audit_data[i][1] == 0x01)) {
566 } else if (sctp_audit_data[i][0] == 0xf0) {
569 } else if ((sctp_audit_data[i][0] == 0xc0) &&
570 (sctp_audit_data[i][1] == 0x01)) {
574 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
575 (uint32_t) sctp_audit_data[i][1]);
584 sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
585 struct sctp_nets *net)
587 int resend_cnt, tot_out, rep, tot_book_cnt;
588 struct sctp_nets *lnet;
589 struct sctp_tmit_chunk *chk;
591 sctp_audit_data[sctp_audit_indx][0] = 0xAA;
592 sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
594 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
598 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
599 sctp_audit_data[sctp_audit_indx][1] = 0x01;
601 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
607 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
608 sctp_audit_data[sctp_audit_indx][1] = 0x02;
610 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
615 sctp_audit_data[sctp_audit_indx][0] = 0xA1;
616 sctp_audit_data[sctp_audit_indx][1] =
617 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
619 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
624 resend_cnt = tot_out = 0;
625 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
626 if (chk->sent == SCTP_DATAGRAM_RESEND) {
628 } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
629 tot_out += chk->book_size;
633 if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
634 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
635 sctp_audit_data[sctp_audit_indx][1] = 0xA1;
637 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
640 SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
641 resend_cnt, stcb->asoc.sent_queue_retran_cnt);
643 stcb->asoc.sent_queue_retran_cnt = resend_cnt;
644 sctp_audit_data[sctp_audit_indx][0] = 0xA2;
645 sctp_audit_data[sctp_audit_indx][1] =
646 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
648 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
652 if (tot_out != stcb->asoc.total_flight) {
653 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
654 sctp_audit_data[sctp_audit_indx][1] = 0xA2;
656 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
660 SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
661 (int)stcb->asoc.total_flight);
662 stcb->asoc.total_flight = tot_out;
664 if (tot_book_cnt != stcb->asoc.total_flight_count) {
665 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
666 sctp_audit_data[sctp_audit_indx][1] = 0xA5;
668 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
672 SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
674 stcb->asoc.total_flight_count = tot_book_cnt;
677 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
678 tot_out += lnet->flight_size;
680 if (tot_out != stcb->asoc.total_flight) {
681 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
682 sctp_audit_data[sctp_audit_indx][1] = 0xA3;
684 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
688 SCTP_PRINTF("real flight:%d net total was %d\n",
689 stcb->asoc.total_flight, tot_out);
690 /* now corrective action */
691 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
694 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
695 if ((chk->whoTo == lnet) &&
696 (chk->sent < SCTP_DATAGRAM_RESEND)) {
697 tot_out += chk->book_size;
700 if (lnet->flight_size != tot_out) {
701 SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
702 lnet, lnet->flight_size,
704 lnet->flight_size = tot_out;
709 sctp_print_audit_report();
714 sctp_audit_log(uint8_t ev, uint8_t fd)
717 sctp_audit_data[sctp_audit_indx][0] = ev;
718 sctp_audit_data[sctp_audit_indx][1] = fd;
720 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
728 * a list of sizes based on typical mtu's, used only if next hop size not
731 static int sctp_mtu_sizes[] = {
753 sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
755 struct sctp_association *asoc;
756 struct sctp_nets *net;
760 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
761 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
762 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
763 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
764 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
765 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
766 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
767 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
768 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
773 find_next_best_mtu(int totsz)
778 * if we are in here we must find the next best fit based on the
779 * size of the dg that failed to be sent.
782 for (i = 0; i < NUMBER_OF_MTU_SIZES; i++) {
783 if (totsz < sctp_mtu_sizes[i]) {
790 return (sctp_mtu_sizes[perfer]);
794 sctp_fill_random_store(struct sctp_pcb *m)
797 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
798 * our counter. The result becomes our good random numbers and we
799 * then setup to give these out. Note that we do no locking to
800 * protect this. This is ok, since if competing folks call this we
801 * will get more gobbled gook in the random store which is what we
802 * want. There is a danger that two guys will use the same random
803 * numbers, but thats ok too since that is random as well :->
806 (void)sctp_hmac(SCTP_HMAC, (uint8_t *) m->random_numbers,
807 sizeof(m->random_numbers), (uint8_t *) & m->random_counter,
808 sizeof(m->random_counter), (uint8_t *) m->random_store);
813 sctp_select_initial_TSN(struct sctp_pcb *inp)
816 * A true implementation should use random selection process to get
817 * the initial stream sequence number, using RFC1750 as a good
822 int store_at, new_store;
824 if (inp->initial_sequence_debug != 0) {
827 ret = inp->initial_sequence_debug;
828 inp->initial_sequence_debug++;
832 store_at = inp->store_at;
833 new_store = store_at + sizeof(uint32_t);
834 if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
837 if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
840 if (new_store == 0) {
841 /* Refill the random store */
842 sctp_fill_random_store(inp);
844 p = &inp->random_store[store_at];
851 sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int save_in_twait)
853 uint32_t x, not_done;
856 (void)SCTP_GETTIME_TIMEVAL(&now);
859 x = sctp_select_initial_TSN(&inp->sctp_ep);
864 if (sctp_is_vtag_good(inp, x, lport, rport, &now, save_in_twait)) {
872 sctp_init_asoc(struct sctp_inpcb *m, struct sctp_tcb *stcb,
873 uint32_t override_tag, uint32_t vrf_id)
875 struct sctp_association *asoc;
878 * Anything set to zero is taken care of by the allocation routine's
883 * Up front select what scoping to apply on addresses I tell my peer
884 * Not sure what to do with these right now, we will need to come up
885 * with a way to set them. We may need to pass them through from the
886 * caller in the sctp_aloc_assoc() function.
891 /* init all variables to a known value. */
892 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE);
893 asoc->max_burst = m->sctp_ep.max_burst;
894 asoc->heart_beat_delay = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
895 asoc->cookie_life = m->sctp_ep.def_cookie_life;
896 asoc->sctp_cmt_on_off = (uint8_t) SCTP_BASE_SYSCTL(sctp_cmt_on_off);
897 /* EY Init nr_sack variable */
898 asoc->sctp_nr_sack_on_off = (uint8_t) SCTP_BASE_SYSCTL(sctp_nr_sack_on_off);
899 /* JRS 5/21/07 - Init CMT PF variables */
900 asoc->sctp_cmt_pf = (uint8_t) SCTP_BASE_SYSCTL(sctp_cmt_pf);
901 asoc->sctp_frag_point = m->sctp_frag_point;
903 asoc->default_tos = m->ip_inp.inp.inp_ip_tos;
905 asoc->default_tos = 0;
909 asoc->default_flowlabel = ((struct in6pcb *)m)->in6p_flowinfo;
911 asoc->default_flowlabel = 0;
913 asoc->sb_send_resv = 0;
915 asoc->my_vtag = override_tag;
917 asoc->my_vtag = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
919 /* Get the nonce tags */
920 asoc->my_vtag_nonce = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
921 asoc->peer_vtag_nonce = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
922 asoc->vrf_id = vrf_id;
924 if (sctp_is_feature_on(m, SCTP_PCB_FLAGS_DONOT_HEARTBEAT))
925 asoc->hb_is_disabled = 1;
927 asoc->hb_is_disabled = 0;
929 #ifdef SCTP_ASOCLOG_OF_TSNS
931 asoc->tsn_out_at = 0;
932 asoc->tsn_in_wrapped = 0;
933 asoc->tsn_out_wrapped = 0;
934 asoc->cumack_log_at = 0;
935 asoc->cumack_log_atsnt = 0;
937 #ifdef SCTP_FS_SPEC_LOG
941 asoc->assoc_up_sent = 0;
942 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
943 sctp_select_initial_TSN(&m->sctp_ep);
944 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
945 /* we are optimisitic here */
946 asoc->peer_supports_pktdrop = 1;
947 asoc->peer_supports_nat = 0;
948 asoc->sent_queue_retran_cnt = 0;
951 asoc->last_net_cmt_send_started = NULL;
953 /* This will need to be adjusted */
954 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
955 asoc->last_acked_seq = asoc->init_seq_number - 1;
956 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
957 asoc->asconf_seq_in = asoc->last_acked_seq;
959 /* here we are different, we hold the next one we expect */
960 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
962 asoc->initial_init_rto_max = m->sctp_ep.initial_init_rto_max;
963 asoc->initial_rto = m->sctp_ep.initial_rto;
965 asoc->max_init_times = m->sctp_ep.max_init_times;
966 asoc->max_send_times = m->sctp_ep.max_send_times;
967 asoc->def_net_failure = m->sctp_ep.def_net_failure;
968 asoc->free_chunk_cnt = 0;
970 asoc->iam_blocking = 0;
971 /* ECN Nonce initialization */
972 asoc->context = m->sctp_context;
973 asoc->def_send = m->def_send;
974 asoc->ecn_nonce_allowed = 0;
975 asoc->receiver_nonce_sum = 1;
976 asoc->nonce_sum_expect_base = 1;
977 asoc->nonce_sum_check = 1;
978 asoc->nonce_resync_tsn = 0;
979 asoc->nonce_wait_for_ecne = 0;
980 asoc->nonce_wait_tsn = 0;
981 asoc->delayed_ack = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
982 asoc->sack_freq = m->sctp_ep.sctp_sack_freq;
983 asoc->pr_sctp_cnt = 0;
984 asoc->total_output_queue_size = 0;
986 if (m->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
989 /* Its a V6 socket */
990 inp6 = (struct in6pcb *)m;
991 asoc->ipv6_addr_legal = 1;
992 /* Now look at the binding flag to see if V4 will be legal */
993 if (SCTP_IPV6_V6ONLY(inp6) == 0) {
994 asoc->ipv4_addr_legal = 1;
996 /* V4 addresses are NOT legal on the association */
997 asoc->ipv4_addr_legal = 0;
1000 /* Its a V4 socket, no - V6 */
1001 asoc->ipv4_addr_legal = 1;
1002 asoc->ipv6_addr_legal = 0;
1005 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(m->sctp_socket), SCTP_MINIMAL_RWND);
1006 asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(m->sctp_socket);
1008 asoc->smallest_mtu = m->sctp_frag_point;
1009 #ifdef SCTP_PRINT_FOR_B_AND_M
1010 SCTP_PRINTF("smallest_mtu init'd with asoc to :%d\n",
1011 asoc->smallest_mtu);
1013 asoc->minrto = m->sctp_ep.sctp_minrto;
1014 asoc->maxrto = m->sctp_ep.sctp_maxrto;
1016 asoc->locked_on_sending = NULL;
1017 asoc->stream_locked_on = 0;
1018 asoc->ecn_echo_cnt_onq = 0;
1019 asoc->stream_locked = 0;
1021 asoc->send_sack = 1;
1023 LIST_INIT(&asoc->sctp_restricted_addrs);
1025 TAILQ_INIT(&asoc->nets);
1026 TAILQ_INIT(&asoc->pending_reply_queue);
1027 TAILQ_INIT(&asoc->asconf_ack_sent);
1028 /* Setup to fill the hb random cache at first HB */
1029 asoc->hb_random_idx = 4;
1031 asoc->sctp_autoclose_ticks = m->sctp_ep.auto_close_time;
1034 * JRS - Pick the default congestion control module based on the
1037 switch (m->sctp_ep.sctp_default_cc_module) {
1038 /* JRS - Standard TCP congestion control */
1039 case SCTP_CC_RFC2581:
1041 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
1042 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
1043 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
1044 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
1045 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
1046 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
1047 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1048 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1049 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
1052 /* JRS - High Speed TCP congestion control (Floyd) */
1055 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
1056 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
1057 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
1058 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
1059 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
1060 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
1061 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1062 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1063 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
1066 /* JRS - HTCP congestion control */
1069 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
1070 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
1071 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
1072 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
1073 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
1074 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
1075 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1076 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1077 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
1080 /* JRS - By default, use RFC2581 */
1083 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
1084 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
1085 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
1086 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
1087 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
1088 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
1089 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1090 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1091 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
1097 * Now the stream parameters, here we allocate space for all streams
1098 * that we request by default.
1100 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1101 m->sctp_ep.pre_open_stream_count;
1102 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1103 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1105 if (asoc->strmout == NULL) {
1106 /* big trouble no memory */
1107 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1110 for (i = 0; i < asoc->streamoutcnt; i++) {
1112 * inbound side must be set to 0xffff, also NOTE when we get
1113 * the INIT-ACK back (for INIT sender) we MUST reduce the
1114 * count (streamoutcnt) but first check if we sent to any of
1115 * the upper streams that were dropped (if some were). Those
1116 * that were dropped must be notified to the upper layer as
1119 asoc->strmout[i].next_sequence_sent = 0x0;
1120 TAILQ_INIT(&asoc->strmout[i].outqueue);
1121 asoc->strmout[i].stream_no = i;
1122 asoc->strmout[i].last_msg_incomplete = 0;
1123 asoc->strmout[i].next_spoke.tqe_next = 0;
1124 asoc->strmout[i].next_spoke.tqe_prev = 0;
1126 /* Now the mapping array */
1127 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1128 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1130 if (asoc->mapping_array == NULL) {
1131 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1132 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1135 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1136 SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1138 if (asoc->nr_mapping_array == NULL) {
1139 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1140 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1141 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1144 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1146 /* Now the init of the other outqueues */
1147 TAILQ_INIT(&asoc->free_chunks);
1148 TAILQ_INIT(&asoc->out_wheel);
1149 TAILQ_INIT(&asoc->control_send_queue);
1150 TAILQ_INIT(&asoc->asconf_send_queue);
1151 TAILQ_INIT(&asoc->send_queue);
1152 TAILQ_INIT(&asoc->sent_queue);
1153 TAILQ_INIT(&asoc->reasmqueue);
1154 TAILQ_INIT(&asoc->resetHead);
1155 asoc->max_inbound_streams = m->sctp_ep.max_open_streams_intome;
1156 TAILQ_INIT(&asoc->asconf_queue);
1157 /* authentication fields */
1158 asoc->authinfo.random = NULL;
1159 asoc->authinfo.active_keyid = 0;
1160 asoc->authinfo.assoc_key = NULL;
1161 asoc->authinfo.assoc_keyid = 0;
1162 asoc->authinfo.recv_key = NULL;
1163 asoc->authinfo.recv_keyid = 0;
1164 LIST_INIT(&asoc->shared_keys);
1165 asoc->marked_retrans = 0;
1169 asoc->timoshutdown = 0;
1170 asoc->timoheartbeat = 0;
1171 asoc->timocookie = 0;
1172 asoc->timoshutdownack = 0;
1173 (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1174 asoc->discontinuity_time = asoc->start_time;
1176 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
1177 * freed later when the association is freed.
1183 sctp_print_mapping_array(struct sctp_association *asoc)
1185 unsigned int i, limit;
1187 printf("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
1188 asoc->mapping_array_size,
1189 asoc->mapping_array_base_tsn,
1190 asoc->cumulative_tsn,
1191 asoc->highest_tsn_inside_map,
1192 asoc->highest_tsn_inside_nr_map);
1193 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1194 if (asoc->mapping_array[limit - 1]) {
1198 printf("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1199 for (i = 0; i < limit; i++) {
1200 printf("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1201 if (((i + 1) % 16) == 0)
1206 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1207 if (asoc->nr_mapping_array[limit - 1]) {
1211 printf("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1212 for (i = 0; i < limit; i++) {
1213 printf("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1220 sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1222 /* mapping array needs to grow */
1223 uint8_t *new_array1, *new_array2;
1226 new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1227 SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1228 SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1229 if ((new_array1 == NULL) || (new_array2 == NULL)) {
1230 /* can't get more, forget it */
1231 SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1233 SCTP_FREE(new_array1, SCTP_M_MAP);
1236 SCTP_FREE(new_array2, SCTP_M_MAP);
1240 memset(new_array1, 0, new_size);
1241 memset(new_array2, 0, new_size);
1242 memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1243 memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1244 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1245 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1246 asoc->mapping_array = new_array1;
1247 asoc->nr_mapping_array = new_array2;
1248 asoc->mapping_array_size = new_size;
1254 sctp_iterator_work(struct sctp_iterator *it)
1256 int iteration_count = 0;
1259 struct sctp_inpcb *tinp;
1261 SCTP_INP_INFO_RLOCK();
1262 SCTP_ITERATOR_LOCK();
1264 SCTP_INP_RLOCK(it->inp);
1265 SCTP_INP_DECR_REF(it->inp);
1267 if (it->inp == NULL) {
1268 /* iterator is complete */
1270 SCTP_ITERATOR_UNLOCK();
1271 SCTP_INP_INFO_RUNLOCK();
1272 if (it->function_atend != NULL) {
1273 (*it->function_atend) (it->pointer, it->val);
1275 SCTP_FREE(it, SCTP_M_ITER);
1282 SCTP_INP_RLOCK(it->inp);
1284 while (((it->pcb_flags) &&
1285 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1286 ((it->pcb_features) &&
1287 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1288 /* endpoint flags or features don't match, so keep looking */
1289 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1290 SCTP_INP_RUNLOCK(it->inp);
1291 goto done_with_iterator;
1294 it->inp = LIST_NEXT(it->inp, sctp_list);
1295 SCTP_INP_RUNLOCK(tinp);
1296 if (it->inp == NULL) {
1297 goto done_with_iterator;
1299 SCTP_INP_RLOCK(it->inp);
1301 /* now go through each assoc which is in the desired state */
1302 if (it->done_current_ep == 0) {
1303 if (it->function_inp != NULL)
1304 inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1305 it->done_current_ep = 1;
1307 if (it->stcb == NULL) {
1308 /* run the per instance function */
1309 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1311 if ((inp_skip) || it->stcb == NULL) {
1312 if (it->function_inp_end != NULL) {
1313 inp_skip = (*it->function_inp_end) (it->inp,
1317 SCTP_INP_RUNLOCK(it->inp);
1321 SCTP_TCB_LOCK(it->stcb);
1322 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1323 /* not in the right state... keep looking */
1324 SCTP_TCB_UNLOCK(it->stcb);
1327 /* see if we have limited out the iterator loop */
1329 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1330 /* Pause to let others grab the lock */
1331 atomic_add_int(&it->stcb->asoc.refcnt, 1);
1332 SCTP_TCB_UNLOCK(it->stcb);
1333 SCTP_INP_INCR_REF(it->inp);
1334 SCTP_INP_RUNLOCK(it->inp);
1335 SCTP_ITERATOR_UNLOCK();
1336 SCTP_INP_INFO_RUNLOCK();
1337 SCTP_INP_INFO_RLOCK();
1338 SCTP_ITERATOR_LOCK();
1339 if (sctp_it_ctl.iterator_flags) {
1340 /* We won't be staying here */
1341 SCTP_INP_DECR_REF(it->inp);
1342 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1343 if (sctp_it_ctl.iterator_flags &
1344 SCTP_ITERATOR_MUST_EXIT) {
1345 goto done_with_iterator;
1347 if (sctp_it_ctl.iterator_flags &
1348 SCTP_ITERATOR_STOP_CUR_IT) {
1349 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1350 goto done_with_iterator;
1352 if (sctp_it_ctl.iterator_flags &
1353 SCTP_ITERATOR_STOP_CUR_INP) {
1354 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1357 /* If we reach here huh? */
1358 printf("Unknown it ctl flag %x\n",
1359 sctp_it_ctl.iterator_flags);
1360 sctp_it_ctl.iterator_flags = 0;
1362 SCTP_INP_RLOCK(it->inp);
1363 SCTP_INP_DECR_REF(it->inp);
1364 SCTP_TCB_LOCK(it->stcb);
1365 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1366 iteration_count = 0;
1368 /* run function on this one */
1369 (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1372 * we lie here, it really needs to have its own type but
1373 * first I must verify that this won't effect things :-0
1375 if (it->no_chunk_output == 0)
1376 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1378 SCTP_TCB_UNLOCK(it->stcb);
1380 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1381 if (it->stcb == NULL) {
1382 /* Run last function */
1383 if (it->function_inp_end != NULL) {
1384 inp_skip = (*it->function_inp_end) (it->inp,
1390 SCTP_INP_RUNLOCK(it->inp);
1392 /* done with all assocs on this endpoint, move on to next endpoint */
1393 it->done_current_ep = 0;
1394 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1397 it->inp = LIST_NEXT(it->inp, sctp_list);
1399 if (it->inp == NULL) {
1400 goto done_with_iterator;
1402 goto select_a_new_ep;
1406 sctp_iterator_worker(void)
1408 struct sctp_iterator *it = NULL;
1410 /* This function is called with the WQ lock in place */
1412 sctp_it_ctl.iterator_running = 1;
1413 sctp_it_ctl.cur_it = it = TAILQ_FIRST(&sctp_it_ctl.iteratorhead);
1415 /* now lets work on this one */
1416 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
1417 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1418 CURVNET_SET(it->vn);
1419 sctp_iterator_work(it);
1422 SCTP_IPI_ITERATOR_WQ_LOCK();
1423 if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) {
1424 sctp_it_ctl.cur_it = NULL;
1427 /* sa_ignore FREED_MEMORY */
1428 sctp_it_ctl.cur_it = it = TAILQ_FIRST(&sctp_it_ctl.iteratorhead);
1430 sctp_it_ctl.iterator_running = 0;
1436 sctp_handle_addr_wq(void)
1438 /* deal with the ADDR wq from the rtsock calls */
1439 struct sctp_laddr *wi;
1440 struct sctp_asconf_iterator *asc;
1442 SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1443 sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
1445 /* Try later, no memory */
1446 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1447 (struct sctp_inpcb *)NULL,
1448 (struct sctp_tcb *)NULL,
1449 (struct sctp_nets *)NULL);
1452 LIST_INIT(&asc->list_of_work);
1455 SCTP_WQ_ADDR_LOCK();
1456 wi = LIST_FIRST(&SCTP_BASE_INFO(addr_wq));
1457 while (wi != NULL) {
1458 LIST_REMOVE(wi, sctp_nxt_addr);
1459 LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
1461 wi = LIST_FIRST(&SCTP_BASE_INFO(addr_wq));
1463 SCTP_WQ_ADDR_UNLOCK();
1465 if (asc->cnt == 0) {
1466 SCTP_FREE(asc, SCTP_M_ASC_IT);
1468 (void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
1469 sctp_asconf_iterator_stcb,
1470 NULL, /* No ep end for boundall */
1471 SCTP_PCB_FLAGS_BOUNDALL,
1472 SCTP_PCB_ANY_FEATURES,
1473 SCTP_ASOC_ANY_STATE,
1475 sctp_asconf_iterator_end, NULL, 0);
1483 sctp_timeout_handler(void *t)
1485 struct sctp_inpcb *inp;
1486 struct sctp_tcb *stcb;
1487 struct sctp_nets *net;
1488 struct sctp_timer *tmr;
1490 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1494 int did_output, type;
1496 tmr = (struct sctp_timer *)t;
1497 inp = (struct sctp_inpcb *)tmr->ep;
1498 stcb = (struct sctp_tcb *)tmr->tcb;
1499 net = (struct sctp_nets *)tmr->net;
1500 CURVNET_SET((struct vnet *)tmr->vnet);
1503 #ifdef SCTP_AUDITING_ENABLED
1504 sctp_audit_log(0xF0, (uint8_t) tmr->type);
1505 sctp_auditing(3, inp, stcb, net);
1508 /* sanity checks... */
1509 if (tmr->self != (void *)tmr) {
1511 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1517 tmr->stopped_from = 0xa001;
1518 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1520 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1526 tmr->stopped_from = 0xa002;
1527 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1531 /* if this is an iterator timeout, get the struct and clear inp */
1532 tmr->stopped_from = 0xa003;
1535 SCTP_INP_INCR_REF(inp);
1536 if ((inp->sctp_socket == 0) &&
1537 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1538 (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1539 (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1540 (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1541 (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1542 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1543 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1544 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1545 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1547 SCTP_INP_DECR_REF(inp);
1552 tmr->stopped_from = 0xa004;
1554 atomic_add_int(&stcb->asoc.refcnt, 1);
1555 if (stcb->asoc.state == 0) {
1556 atomic_add_int(&stcb->asoc.refcnt, -1);
1558 SCTP_INP_DECR_REF(inp);
1564 tmr->stopped_from = 0xa005;
1565 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type);
1566 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1568 SCTP_INP_DECR_REF(inp);
1571 atomic_add_int(&stcb->asoc.refcnt, -1);
1576 tmr->stopped_from = 0xa006;
1579 SCTP_TCB_LOCK(stcb);
1580 atomic_add_int(&stcb->asoc.refcnt, -1);
1581 if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) &&
1582 ((stcb->asoc.state == 0) ||
1583 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1584 SCTP_TCB_UNLOCK(stcb);
1586 SCTP_INP_DECR_REF(inp);
1592 /* record in stopped what t-o occured */
1593 tmr->stopped_from = tmr->type;
1595 /* mark as being serviced now */
1596 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1598 * Callout has been rescheduled.
1602 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1604 * Not active, so no action.
1608 SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1610 /* call the handler for the appropriate timer type */
1611 switch (tmr->type) {
1612 case SCTP_TIMER_TYPE_ZERO_COPY:
1616 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1617 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
1620 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1624 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1625 SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket);
1628 case SCTP_TIMER_TYPE_ADDR_WQ:
1629 sctp_handle_addr_wq();
1631 case SCTP_TIMER_TYPE_SEND:
1632 if ((stcb == NULL) || (inp == NULL)) {
1635 SCTP_STAT_INCR(sctps_timodata);
1636 stcb->asoc.timodata++;
1637 stcb->asoc.num_send_timers_up--;
1638 if (stcb->asoc.num_send_timers_up < 0) {
1639 stcb->asoc.num_send_timers_up = 0;
1641 SCTP_TCB_LOCK_ASSERT(stcb);
1642 cur_oerr = stcb->asoc.overall_error_count;
1643 retcode = sctp_t3rxt_timer(inp, stcb, net);
1645 /* no need to unlock on tcb its gone */
1649 SCTP_TCB_LOCK_ASSERT(stcb);
1650 #ifdef SCTP_AUDITING_ENABLED
1651 sctp_auditing(4, inp, stcb, net);
1653 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1654 if ((stcb->asoc.num_send_timers_up == 0) &&
1655 (stcb->asoc.sent_queue_cnt > 0)
1657 struct sctp_tmit_chunk *chk;
1660 * safeguard. If there on some on the sent queue
1661 * somewhere but no timers running something is
1662 * wrong... so we start a timer on the first chunk
1663 * on the send queue on whatever net it is sent to.
1665 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1666 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1670 case SCTP_TIMER_TYPE_INIT:
1671 if ((stcb == NULL) || (inp == NULL)) {
1674 SCTP_STAT_INCR(sctps_timoinit);
1675 stcb->asoc.timoinit++;
1676 if (sctp_t1init_timer(inp, stcb, net)) {
1677 /* no need to unlock on tcb its gone */
1680 /* We do output but not here */
1683 case SCTP_TIMER_TYPE_RECV:
1684 if ((stcb == NULL) || (inp == NULL)) {
1687 SCTP_STAT_INCR(sctps_timosack);
1688 stcb->asoc.timosack++;
1689 sctp_send_sack(stcb);
1691 #ifdef SCTP_AUDITING_ENABLED
1692 sctp_auditing(4, inp, stcb, net);
1694 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1696 case SCTP_TIMER_TYPE_SHUTDOWN:
1697 if ((stcb == NULL) || (inp == NULL)) {
1700 if (sctp_shutdown_timer(inp, stcb, net)) {
1701 /* no need to unlock on tcb its gone */
1704 SCTP_STAT_INCR(sctps_timoshutdown);
1705 stcb->asoc.timoshutdown++;
1706 #ifdef SCTP_AUDITING_ENABLED
1707 sctp_auditing(4, inp, stcb, net);
1709 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1711 case SCTP_TIMER_TYPE_HEARTBEAT:
1713 struct sctp_nets *lnet;
1714 int cnt_of_unconf = 0;
1716 if ((stcb == NULL) || (inp == NULL)) {
1719 SCTP_STAT_INCR(sctps_timoheartbeat);
1720 stcb->asoc.timoheartbeat++;
1721 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1722 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1723 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
1727 if (cnt_of_unconf == 0) {
1728 if (sctp_heartbeat_timer(inp, stcb, lnet,
1730 /* no need to unlock on tcb its gone */
1734 #ifdef SCTP_AUDITING_ENABLED
1735 sctp_auditing(4, inp, stcb, lnet);
1737 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
1738 stcb->sctp_ep, stcb, lnet);
1739 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1742 case SCTP_TIMER_TYPE_COOKIE:
1743 if ((stcb == NULL) || (inp == NULL)) {
1746 if (sctp_cookie_timer(inp, stcb, net)) {
1747 /* no need to unlock on tcb its gone */
1750 SCTP_STAT_INCR(sctps_timocookie);
1751 stcb->asoc.timocookie++;
1752 #ifdef SCTP_AUDITING_ENABLED
1753 sctp_auditing(4, inp, stcb, net);
1756 * We consider T3 and Cookie timer pretty much the same with
1757 * respect to where from in chunk_output.
1759 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1761 case SCTP_TIMER_TYPE_NEWCOOKIE:
1769 SCTP_STAT_INCR(sctps_timosecret);
1770 (void)SCTP_GETTIME_TIMEVAL(&tv);
1771 SCTP_INP_WLOCK(inp);
1772 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1773 inp->sctp_ep.last_secret_number =
1774 inp->sctp_ep.current_secret_number;
1775 inp->sctp_ep.current_secret_number++;
1776 if (inp->sctp_ep.current_secret_number >=
1777 SCTP_HOW_MANY_SECRETS) {
1778 inp->sctp_ep.current_secret_number = 0;
1780 secret = (int)inp->sctp_ep.current_secret_number;
1781 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1782 inp->sctp_ep.secret_key[secret][i] =
1783 sctp_select_initial_TSN(&inp->sctp_ep);
1785 SCTP_INP_WUNLOCK(inp);
1786 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1790 case SCTP_TIMER_TYPE_PATHMTURAISE:
1791 if ((stcb == NULL) || (inp == NULL)) {
1794 SCTP_STAT_INCR(sctps_timopathmtu);
1795 sctp_pathmtu_timer(inp, stcb, net);
1798 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1799 if ((stcb == NULL) || (inp == NULL)) {
1802 if (sctp_shutdownack_timer(inp, stcb, net)) {
1803 /* no need to unlock on tcb its gone */
1806 SCTP_STAT_INCR(sctps_timoshutdownack);
1807 stcb->asoc.timoshutdownack++;
1808 #ifdef SCTP_AUDITING_ENABLED
1809 sctp_auditing(4, inp, stcb, net);
1811 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1813 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1814 if ((stcb == NULL) || (inp == NULL)) {
1817 SCTP_STAT_INCR(sctps_timoshutdownguard);
1818 sctp_abort_an_association(inp, stcb,
1819 SCTP_SHUTDOWN_GUARD_EXPIRES, NULL, SCTP_SO_NOT_LOCKED);
1820 /* no need to unlock on tcb its gone */
1823 case SCTP_TIMER_TYPE_STRRESET:
1824 if ((stcb == NULL) || (inp == NULL)) {
1827 if (sctp_strreset_timer(inp, stcb, net)) {
1828 /* no need to unlock on tcb its gone */
1831 SCTP_STAT_INCR(sctps_timostrmrst);
1832 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1834 case SCTP_TIMER_TYPE_EARLYFR:
1835 /* Need to do FR of things for net */
1836 if ((stcb == NULL) || (inp == NULL)) {
1839 SCTP_STAT_INCR(sctps_timoearlyfr);
1840 sctp_early_fr_timer(inp, stcb, net);
1842 case SCTP_TIMER_TYPE_ASCONF:
1843 if ((stcb == NULL) || (inp == NULL)) {
1846 if (sctp_asconf_timer(inp, stcb, net)) {
1847 /* no need to unlock on tcb its gone */
1850 SCTP_STAT_INCR(sctps_timoasconf);
1851 #ifdef SCTP_AUDITING_ENABLED
1852 sctp_auditing(4, inp, stcb, net);
1854 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1856 case SCTP_TIMER_TYPE_PRIM_DELETED:
1857 if ((stcb == NULL) || (inp == NULL)) {
1860 sctp_delete_prim_timer(inp, stcb, net);
1861 SCTP_STAT_INCR(sctps_timodelprim);
1864 case SCTP_TIMER_TYPE_AUTOCLOSE:
1865 if ((stcb == NULL) || (inp == NULL)) {
1868 SCTP_STAT_INCR(sctps_timoautoclose);
1869 sctp_autoclose_timer(inp, stcb, net);
1870 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1873 case SCTP_TIMER_TYPE_ASOCKILL:
1874 if ((stcb == NULL) || (inp == NULL)) {
1877 SCTP_STAT_INCR(sctps_timoassockill);
1878 /* Can we free it yet? */
1879 SCTP_INP_DECR_REF(inp);
1880 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1881 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1882 so = SCTP_INP_SO(inp);
1883 atomic_add_int(&stcb->asoc.refcnt, 1);
1884 SCTP_TCB_UNLOCK(stcb);
1885 SCTP_SOCKET_LOCK(so, 1);
1886 SCTP_TCB_LOCK(stcb);
1887 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1889 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1890 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1891 SCTP_SOCKET_UNLOCK(so, 1);
1894 * free asoc, always unlocks (or destroy's) so prevent
1895 * duplicate unlock or unlock of a free mtx :-0
1899 case SCTP_TIMER_TYPE_INPKILL:
1900 SCTP_STAT_INCR(sctps_timoinpkill);
1905 * special case, take away our increment since WE are the
1908 SCTP_INP_DECR_REF(inp);
1909 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1910 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
1911 SCTP_CALLED_FROM_INPKILL_TIMER);
1915 SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1919 #ifdef SCTP_AUDITING_ENABLED
1920 sctp_audit_log(0xF1, (uint8_t) tmr->type);
1922 sctp_auditing(5, inp, stcb, net);
1924 if ((did_output) && stcb) {
1926 * Now we need to clean up the control chunk chain if an
1927 * ECNE is on it. It must be marked as UNSENT again so next
1928 * call will continue to send it until such time that we get
1929 * a CWR, to remove it. It is, however, less likely that we
1930 * will find a ecn echo on the chain though.
1932 sctp_fix_ecn_echo(&stcb->asoc);
1936 SCTP_TCB_UNLOCK(stcb);
1940 SCTP_INP_DECR_REF(inp);
1943 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n",
1949 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1950 struct sctp_nets *net)
1953 struct sctp_timer *tmr;
1955 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1962 SCTP_TCB_LOCK_ASSERT(stcb);
1965 case SCTP_TIMER_TYPE_ZERO_COPY:
1966 tmr = &inp->sctp_ep.zero_copy_timer;
1967 to_ticks = SCTP_ZERO_COPY_TICK_DELAY;
1969 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1970 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
1971 to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY;
1973 case SCTP_TIMER_TYPE_ADDR_WQ:
1974 /* Only 1 tick away :-) */
1975 tmr = &SCTP_BASE_INFO(addr_wq_timer);
1976 to_ticks = SCTP_ADDRESS_TICK_DELAY;
1978 case SCTP_TIMER_TYPE_SEND:
1979 /* Here we use the RTO timer */
1983 if ((stcb == NULL) || (net == NULL)) {
1986 tmr = &net->rxt_timer;
1987 if (net->RTO == 0) {
1988 rto_val = stcb->asoc.initial_rto;
1992 to_ticks = MSEC_TO_TICKS(rto_val);
1995 case SCTP_TIMER_TYPE_INIT:
1997 * Here we use the INIT timer default usually about 1
2000 if ((stcb == NULL) || (net == NULL)) {
2003 tmr = &net->rxt_timer;
2004 if (net->RTO == 0) {
2005 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2007 to_ticks = MSEC_TO_TICKS(net->RTO);
2010 case SCTP_TIMER_TYPE_RECV:
2012 * Here we use the Delayed-Ack timer value from the inp
2013 * ususually about 200ms.
2018 tmr = &stcb->asoc.dack_timer;
2019 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
2021 case SCTP_TIMER_TYPE_SHUTDOWN:
2022 /* Here we use the RTO of the destination. */
2023 if ((stcb == NULL) || (net == NULL)) {
2026 if (net->RTO == 0) {
2027 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2029 to_ticks = MSEC_TO_TICKS(net->RTO);
2031 tmr = &net->rxt_timer;
2033 case SCTP_TIMER_TYPE_HEARTBEAT:
2035 * the net is used here so that we can add in the RTO. Even
2036 * though we use a different timer. We also add the HB timer
2037 * PLUS a random jitter.
2039 if ((inp == NULL) || (stcb == NULL)) {
2043 uint8_t this_random;
2044 int cnt_of_unconf = 0;
2045 struct sctp_nets *lnet;
2047 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
2048 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2049 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
2053 if (cnt_of_unconf) {
2055 (void)sctp_heartbeat_timer(inp, stcb, lnet, cnt_of_unconf);
2057 if (stcb->asoc.hb_random_idx > 3) {
2058 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2059 memcpy(stcb->asoc.hb_random_values, &rndval,
2060 sizeof(stcb->asoc.hb_random_values));
2061 stcb->asoc.hb_random_idx = 0;
2063 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
2064 stcb->asoc.hb_random_idx++;
2065 stcb->asoc.hb_ect_randombit = 0;
2067 * this_random will be 0 - 256 ms RTO is in ms.
2069 if ((stcb->asoc.hb_is_disabled) &&
2070 (cnt_of_unconf == 0)) {
2076 delay = stcb->asoc.heart_beat_delay;
2077 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
2078 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2079 ((lnet->dest_state & SCTP_ADDR_OUT_OF_SCOPE) == 0) &&
2080 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
2084 if (net->RTO == 0) {
2085 /* Never been checked */
2086 to_ticks = this_random + stcb->asoc.initial_rto + delay;
2088 /* set rto_val to the ms */
2089 to_ticks = delay + net->RTO + this_random;
2092 if (cnt_of_unconf) {
2093 to_ticks = this_random + stcb->asoc.initial_rto;
2095 to_ticks = stcb->asoc.heart_beat_delay + this_random + stcb->asoc.initial_rto;
2099 * Now we must convert the to_ticks that are now in
2102 to_ticks = MSEC_TO_TICKS(to_ticks);
2103 tmr = &stcb->asoc.hb_timer;
2106 case SCTP_TIMER_TYPE_COOKIE:
2108 * Here we can use the RTO timer from the network since one
2109 * RTT was compelete. If a retran happened then we will be
2110 * using the RTO initial value.
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_NEWCOOKIE:
2124 * nothing needed but the endpoint here ususually about 60
2130 tmr = &inp->sctp_ep.signature_change;
2131 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2133 case SCTP_TIMER_TYPE_ASOCKILL:
2137 tmr = &stcb->asoc.strreset_timer;
2138 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2140 case SCTP_TIMER_TYPE_INPKILL:
2142 * The inp is setup to die. We re-use the signature_chage
2143 * timer since that has stopped and we are in the GONE
2149 tmr = &inp->sctp_ep.signature_change;
2150 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2152 case SCTP_TIMER_TYPE_PATHMTURAISE:
2154 * Here we use the value found in the EP for PMTU ususually
2157 if ((stcb == NULL) || (inp == NULL)) {
2163 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2164 tmr = &net->pmtu_timer;
2166 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2167 /* Here we use the RTO of the destination */
2168 if ((stcb == NULL) || (net == NULL)) {
2171 if (net->RTO == 0) {
2172 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2174 to_ticks = MSEC_TO_TICKS(net->RTO);
2176 tmr = &net->rxt_timer;
2178 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2180 * Here we use the endpoints shutdown guard timer usually
2183 if ((inp == NULL) || (stcb == NULL)) {
2186 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2187 tmr = &stcb->asoc.shut_guard_timer;
2189 case SCTP_TIMER_TYPE_STRRESET:
2191 * Here the timer comes from the stcb but its value is from
2194 if ((stcb == NULL) || (net == NULL)) {
2197 if (net->RTO == 0) {
2198 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2200 to_ticks = MSEC_TO_TICKS(net->RTO);
2202 tmr = &stcb->asoc.strreset_timer;
2205 case SCTP_TIMER_TYPE_EARLYFR:
2209 if ((stcb == NULL) || (net == NULL)) {
2212 if (net->flight_size > net->cwnd) {
2213 /* no need to start */
2216 SCTP_STAT_INCR(sctps_earlyfrstart);
2217 if (net->lastsa == 0) {
2218 /* Hmm no rtt estimate yet? */
2219 msec = stcb->asoc.initial_rto >> 2;
2221 msec = ((net->lastsa >> 2) + net->lastsv) >> 1;
2223 if (msec < SCTP_BASE_SYSCTL(sctp_early_fr_msec)) {
2224 msec = SCTP_BASE_SYSCTL(sctp_early_fr_msec);
2225 if (msec < SCTP_MINFR_MSEC_FLOOR) {
2226 msec = SCTP_MINFR_MSEC_FLOOR;
2229 to_ticks = MSEC_TO_TICKS(msec);
2230 tmr = &net->fr_timer;
2233 case SCTP_TIMER_TYPE_ASCONF:
2235 * Here the timer comes from the stcb but its value is from
2238 if ((stcb == NULL) || (net == NULL)) {
2241 if (net->RTO == 0) {
2242 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2244 to_ticks = MSEC_TO_TICKS(net->RTO);
2246 tmr = &stcb->asoc.asconf_timer;
2248 case SCTP_TIMER_TYPE_PRIM_DELETED:
2249 if ((stcb == NULL) || (net != NULL)) {
2252 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2253 tmr = &stcb->asoc.delete_prim_timer;
2255 case SCTP_TIMER_TYPE_AUTOCLOSE:
2259 if (stcb->asoc.sctp_autoclose_ticks == 0) {
2261 * Really an error since stcb is NOT set to
2266 to_ticks = stcb->asoc.sctp_autoclose_ticks;
2267 tmr = &stcb->asoc.autoclose_timer;
2270 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2271 __FUNCTION__, t_type);
2275 if ((to_ticks <= 0) || (tmr == NULL)) {
2276 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2277 __FUNCTION__, t_type, to_ticks, tmr);
2280 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2282 * we do NOT allow you to have it already running. if it is
2283 * we leave the current one up unchanged
2287 /* At this point we can proceed */
2288 if (t_type == SCTP_TIMER_TYPE_SEND) {
2289 stcb->asoc.num_send_timers_up++;
2291 tmr->stopped_from = 0;
2293 tmr->ep = (void *)inp;
2294 tmr->tcb = (void *)stcb;
2295 tmr->net = (void *)net;
2296 tmr->self = (void *)tmr;
2297 tmr->vnet = (void *)curvnet;
2298 tmr->ticks = sctp_get_tick_count();
2299 (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2304 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2305 struct sctp_nets *net, uint32_t from)
2307 struct sctp_timer *tmr;
2309 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2315 SCTP_TCB_LOCK_ASSERT(stcb);
2318 case SCTP_TIMER_TYPE_ZERO_COPY:
2319 tmr = &inp->sctp_ep.zero_copy_timer;
2321 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
2322 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
2324 case SCTP_TIMER_TYPE_ADDR_WQ:
2325 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2327 case SCTP_TIMER_TYPE_EARLYFR:
2328 if ((stcb == NULL) || (net == NULL)) {
2331 tmr = &net->fr_timer;
2332 SCTP_STAT_INCR(sctps_earlyfrstop);
2334 case SCTP_TIMER_TYPE_SEND:
2335 if ((stcb == NULL) || (net == NULL)) {
2338 tmr = &net->rxt_timer;
2340 case SCTP_TIMER_TYPE_INIT:
2341 if ((stcb == NULL) || (net == NULL)) {
2344 tmr = &net->rxt_timer;
2346 case SCTP_TIMER_TYPE_RECV:
2350 tmr = &stcb->asoc.dack_timer;
2352 case SCTP_TIMER_TYPE_SHUTDOWN:
2353 if ((stcb == NULL) || (net == NULL)) {
2356 tmr = &net->rxt_timer;
2358 case SCTP_TIMER_TYPE_HEARTBEAT:
2362 tmr = &stcb->asoc.hb_timer;
2364 case SCTP_TIMER_TYPE_COOKIE:
2365 if ((stcb == NULL) || (net == NULL)) {
2368 tmr = &net->rxt_timer;
2370 case SCTP_TIMER_TYPE_NEWCOOKIE:
2371 /* nothing needed but the endpoint here */
2372 tmr = &inp->sctp_ep.signature_change;
2374 * We re-use the newcookie timer for the INP kill timer. We
2375 * must assure that we do not kill it by accident.
2378 case SCTP_TIMER_TYPE_ASOCKILL:
2380 * Stop the asoc kill timer.
2385 tmr = &stcb->asoc.strreset_timer;
2388 case SCTP_TIMER_TYPE_INPKILL:
2390 * The inp is setup to die. We re-use the signature_chage
2391 * timer since that has stopped and we are in the GONE
2394 tmr = &inp->sctp_ep.signature_change;
2396 case SCTP_TIMER_TYPE_PATHMTURAISE:
2397 if ((stcb == NULL) || (net == NULL)) {
2400 tmr = &net->pmtu_timer;
2402 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2403 if ((stcb == NULL) || (net == NULL)) {
2406 tmr = &net->rxt_timer;
2408 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2412 tmr = &stcb->asoc.shut_guard_timer;
2414 case SCTP_TIMER_TYPE_STRRESET:
2418 tmr = &stcb->asoc.strreset_timer;
2420 case SCTP_TIMER_TYPE_ASCONF:
2424 tmr = &stcb->asoc.asconf_timer;
2426 case SCTP_TIMER_TYPE_PRIM_DELETED:
2430 tmr = &stcb->asoc.delete_prim_timer;
2432 case SCTP_TIMER_TYPE_AUTOCLOSE:
2436 tmr = &stcb->asoc.autoclose_timer;
2439 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2440 __FUNCTION__, t_type);
2446 if ((tmr->type != t_type) && tmr->type) {
2448 * Ok we have a timer that is under joint use. Cookie timer
2449 * per chance with the SEND timer. We therefore are NOT
2450 * running the timer that the caller wants stopped. So just
2455 if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2456 stcb->asoc.num_send_timers_up--;
2457 if (stcb->asoc.num_send_timers_up < 0) {
2458 stcb->asoc.num_send_timers_up = 0;
2462 tmr->stopped_from = from;
2463 (void)SCTP_OS_TIMER_STOP(&tmr->timer);
2468 sctp_calculate_len(struct mbuf *m)
2475 tlen += SCTP_BUF_LEN(at);
2476 at = SCTP_BUF_NEXT(at);
2482 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2483 struct sctp_association *asoc, uint32_t mtu)
2486 * Reset the P-MTU size on this association, this involves changing
2487 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2488 * allow the DF flag to be cleared.
2490 struct sctp_tmit_chunk *chk;
2491 unsigned int eff_mtu, ovh;
2493 #ifdef SCTP_PRINT_FOR_B_AND_M
2494 SCTP_PRINTF("sctp_mtu_size_reset(%p, asoc:%p mtu:%d\n",
2497 asoc->smallest_mtu = mtu;
2498 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2499 ovh = SCTP_MIN_OVERHEAD;
2501 ovh = SCTP_MIN_V4_OVERHEAD;
2503 eff_mtu = mtu - ovh;
2504 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2506 if (chk->send_size > eff_mtu) {
2507 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2510 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2511 if (chk->send_size > eff_mtu) {
2512 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2519 * given an association and starting time of the current RTT period return
2520 * RTO in number of msecs net should point to the current network
2523 sctp_calculate_rto(struct sctp_tcb *stcb,
2524 struct sctp_association *asoc,
2525 struct sctp_nets *net,
2526 struct timeval *told,
2530 * given an association and the starting time of the current RTT
2531 * period (in value1/value2) return RTO in number of msecs.
2535 uint32_t new_rto = 0;
2536 int first_measure = 0;
2537 struct timeval now, then, *old;
2539 /* Copy it out for sparc64 */
2540 if (safe == sctp_align_unsafe_makecopy) {
2542 memcpy(&then, told, sizeof(struct timeval));
2543 } else if (safe == sctp_align_safe_nocopy) {
2547 SCTP_PRINTF("Huh, bad rto calc call\n");
2550 /************************/
2551 /* 1. calculate new RTT */
2552 /************************/
2553 /* get the current time */
2554 (void)SCTP_GETTIME_TIMEVAL(&now);
2555 /* compute the RTT value */
2556 if ((u_long)now.tv_sec > (u_long)old->tv_sec) {
2557 calc_time = ((u_long)now.tv_sec - (u_long)old->tv_sec) * 1000;
2558 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2559 calc_time += (((u_long)now.tv_usec -
2560 (u_long)old->tv_usec) / 1000);
2561 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2562 /* Borrow 1,000ms from current calculation */
2564 /* Add in the slop over */
2565 calc_time += ((int)now.tv_usec / 1000);
2566 /* Add in the pre-second ms's */
2567 calc_time += (((int)1000000 - (int)old->tv_usec) / 1000);
2569 } else if ((u_long)now.tv_sec == (u_long)old->tv_sec) {
2570 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2571 calc_time = ((u_long)now.tv_usec -
2572 (u_long)old->tv_usec) / 1000;
2573 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2574 /* impossible .. garbage in nothing out */
2576 } else if ((u_long)now.tv_usec == (u_long)old->tv_usec) {
2578 * We have to have 1 usec :-D this must be the
2583 /* impossible .. garbage in nothing out */
2587 /* Clock wrapped? */
2590 /***************************/
2591 /* 2. update RTTVAR & SRTT */
2592 /***************************/
2593 net->rtt = o_calctime = calc_time;
2594 /* this is Van Jacobson's integer version */
2595 if (net->RTO_measured) {
2596 calc_time -= (net->lastsa >> SCTP_RTT_SHIFT); /* take away 1/8th when
2598 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2599 rto_logging(net, SCTP_LOG_RTTVAR);
2601 net->prev_rtt = o_calctime;
2602 net->lastsa += calc_time; /* add 7/8th into sa when
2604 if (calc_time < 0) {
2605 calc_time = -calc_time;
2607 calc_time -= (net->lastsv >> SCTP_RTT_VAR_SHIFT); /* take away 1/4 when
2609 net->lastsv += calc_time;
2610 if (net->lastsv == 0) {
2611 net->lastsv = SCTP_CLOCK_GRANULARITY;
2614 /* First RTO measurment */
2615 net->RTO_measured = 1;
2616 net->lastsa = calc_time << SCTP_RTT_SHIFT; /* Multiply by 8 when
2618 net->lastsv = calc_time;
2619 if (net->lastsv == 0) {
2620 net->lastsv = SCTP_CLOCK_GRANULARITY;
2623 net->prev_rtt = o_calctime;
2624 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2625 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2629 new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2630 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2631 (stcb->asoc.sat_network_lockout == 0)) {
2632 stcb->asoc.sat_network = 1;
2633 } else if ((!first_measure) && stcb->asoc.sat_network) {
2634 stcb->asoc.sat_network = 0;
2635 stcb->asoc.sat_network_lockout = 1;
2637 /* bound it, per C6/C7 in Section 5.3.1 */
2638 if (new_rto < stcb->asoc.minrto) {
2639 new_rto = stcb->asoc.minrto;
2641 if (new_rto > stcb->asoc.maxrto) {
2642 new_rto = stcb->asoc.maxrto;
2644 /* we are now returning the RTO */
2649 * return a pointer to a contiguous piece of data from the given mbuf chain
2650 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2651 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2652 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2655 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2661 if ((off < 0) || (len <= 0))
2664 /* find the desired start location */
2665 while ((m != NULL) && (off > 0)) {
2666 if (off < SCTP_BUF_LEN(m))
2668 off -= SCTP_BUF_LEN(m);
2669 m = SCTP_BUF_NEXT(m);
2674 /* is the current mbuf large enough (eg. contiguous)? */
2675 if ((SCTP_BUF_LEN(m) - off) >= len) {
2676 return (mtod(m, caddr_t)+off);
2678 /* else, it spans more than one mbuf, so save a temp copy... */
2679 while ((m != NULL) && (len > 0)) {
2680 count = min(SCTP_BUF_LEN(m) - off, len);
2681 bcopy(mtod(m, caddr_t)+off, ptr, count);
2685 m = SCTP_BUF_NEXT(m);
2687 if ((m == NULL) && (len > 0))
2690 return ((caddr_t)in_ptr);
2696 struct sctp_paramhdr *
2697 sctp_get_next_param(struct mbuf *m,
2699 struct sctp_paramhdr *pull,
2702 /* This just provides a typed signature to Peter's Pull routine */
2703 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2709 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2712 * add padlen bytes of 0 filled padding to the end of the mbuf. If
2713 * padlen is > 3 this routine will fail.
2719 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
2722 if (padlen <= M_TRAILINGSPACE(m)) {
2724 * The easy way. We hope the majority of the time we hit
2727 dp = (uint8_t *) (mtod(m, caddr_t)+SCTP_BUF_LEN(m));
2728 SCTP_BUF_LEN(m) += padlen;
2730 /* Hard way we must grow the mbuf */
2733 tmp = sctp_get_mbuf_for_msg(padlen, 0, M_DONTWAIT, 1, MT_DATA);
2735 /* Out of space GAK! we are in big trouble. */
2736 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
2739 /* setup and insert in middle */
2740 SCTP_BUF_LEN(tmp) = padlen;
2741 SCTP_BUF_NEXT(tmp) = NULL;
2742 SCTP_BUF_NEXT(m) = tmp;
2743 dp = mtod(tmp, uint8_t *);
2745 /* zero out the pad */
2746 for (i = 0; i < padlen; i++) {
2754 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2756 /* find the last mbuf in chain and pad it */
2761 return (sctp_add_pad_tombuf(last_mbuf, padval));
2764 if (SCTP_BUF_NEXT(m_at) == NULL) {
2765 return (sctp_add_pad_tombuf(m_at, padval));
2767 m_at = SCTP_BUF_NEXT(m_at);
2770 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
2775 sctp_notify_assoc_change(uint32_t event, struct sctp_tcb *stcb,
2776 uint32_t error, void *data, int so_locked
2777 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2782 struct mbuf *m_notify;
2783 struct sctp_assoc_change *sac;
2784 struct sctp_queued_to_read *control;
2786 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2792 * For TCP model AND UDP connected sockets we will send an error up
2793 * when an ABORT comes in.
2795 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2796 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2797 ((event == SCTP_COMM_LOST) || (event == SCTP_CANT_STR_ASSOC))) {
2798 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
2799 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
2800 stcb->sctp_socket->so_error = ECONNREFUSED;
2802 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
2803 stcb->sctp_socket->so_error = ECONNRESET;
2805 /* Wake ANY sleepers */
2806 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2807 so = SCTP_INP_SO(stcb->sctp_ep);
2809 atomic_add_int(&stcb->asoc.refcnt, 1);
2810 SCTP_TCB_UNLOCK(stcb);
2811 SCTP_SOCKET_LOCK(so, 1);
2812 SCTP_TCB_LOCK(stcb);
2813 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2814 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2815 SCTP_SOCKET_UNLOCK(so, 1);
2820 sorwakeup(stcb->sctp_socket);
2821 sowwakeup(stcb->sctp_socket);
2822 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2824 SCTP_SOCKET_UNLOCK(so, 1);
2828 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2829 /* event not enabled */
2832 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_change), 0, M_DONTWAIT, 1, MT_DATA);
2833 if (m_notify == NULL)
2836 SCTP_BUF_LEN(m_notify) = 0;
2838 sac = mtod(m_notify, struct sctp_assoc_change *);
2839 sac->sac_type = SCTP_ASSOC_CHANGE;
2841 sac->sac_length = sizeof(struct sctp_assoc_change);
2842 sac->sac_state = event;
2843 sac->sac_error = error;
2844 /* XXX verify these stream counts */
2845 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2846 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2847 sac->sac_assoc_id = sctp_get_associd(stcb);
2848 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_change);
2849 SCTP_BUF_NEXT(m_notify) = NULL;
2850 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2853 if (control == NULL) {
2855 sctp_m_freem(m_notify);
2858 control->length = SCTP_BUF_LEN(m_notify);
2859 /* not that we need this */
2860 control->tail_mbuf = m_notify;
2861 control->spec_flags = M_NOTIFICATION;
2862 sctp_add_to_readq(stcb->sctp_ep, stcb,
2864 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2866 if (event == SCTP_COMM_LOST) {
2867 /* Wake up any sleeper */
2868 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2869 so = SCTP_INP_SO(stcb->sctp_ep);
2871 atomic_add_int(&stcb->asoc.refcnt, 1);
2872 SCTP_TCB_UNLOCK(stcb);
2873 SCTP_SOCKET_LOCK(so, 1);
2874 SCTP_TCB_LOCK(stcb);
2875 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2876 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2877 SCTP_SOCKET_UNLOCK(so, 1);
2882 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
2883 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2885 SCTP_SOCKET_UNLOCK(so, 1);
2892 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2893 struct sockaddr *sa, uint32_t error)
2895 struct mbuf *m_notify;
2896 struct sctp_paddr_change *spc;
2897 struct sctp_queued_to_read *control;
2899 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2900 /* event not enabled */
2903 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_DONTWAIT, 1, MT_DATA);
2904 if (m_notify == NULL)
2906 SCTP_BUF_LEN(m_notify) = 0;
2907 spc = mtod(m_notify, struct sctp_paddr_change *);
2908 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2910 spc->spc_length = sizeof(struct sctp_paddr_change);
2911 switch (sa->sa_family) {
2913 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2918 struct sockaddr_in6 *sin6;
2920 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2922 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2923 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2924 if (sin6->sin6_scope_id == 0) {
2925 /* recover scope_id for user */
2926 (void)sa6_recoverscope(sin6);
2928 /* clear embedded scope_id for user */
2929 in6_clearscope(&sin6->sin6_addr);
2939 spc->spc_state = state;
2940 spc->spc_error = error;
2941 spc->spc_assoc_id = sctp_get_associd(stcb);
2943 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2944 SCTP_BUF_NEXT(m_notify) = NULL;
2946 /* append to socket */
2947 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2950 if (control == NULL) {
2952 sctp_m_freem(m_notify);
2955 control->length = SCTP_BUF_LEN(m_notify);
2956 control->spec_flags = M_NOTIFICATION;
2957 /* not that we need this */
2958 control->tail_mbuf = m_notify;
2959 sctp_add_to_readq(stcb->sctp_ep, stcb,
2961 &stcb->sctp_socket->so_rcv, 1,
2962 SCTP_READ_LOCK_NOT_HELD,
2963 SCTP_SO_NOT_LOCKED);
2968 sctp_notify_send_failed(struct sctp_tcb *stcb, uint32_t error,
2969 struct sctp_tmit_chunk *chk, int so_locked
2970 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2975 struct mbuf *m_notify;
2976 struct sctp_send_failed *ssf;
2977 struct sctp_queued_to_read *control;
2980 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)) {
2981 /* event not enabled */
2984 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 0, M_DONTWAIT, 1, MT_DATA);
2985 if (m_notify == NULL)
2988 length = sizeof(struct sctp_send_failed) + chk->send_size;
2989 length -= sizeof(struct sctp_data_chunk);
2990 SCTP_BUF_LEN(m_notify) = 0;
2991 ssf = mtod(m_notify, struct sctp_send_failed *);
2992 ssf->ssf_type = SCTP_SEND_FAILED;
2993 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
2994 ssf->ssf_flags = SCTP_DATA_UNSENT;
2996 ssf->ssf_flags = SCTP_DATA_SENT;
2997 ssf->ssf_length = length;
2998 ssf->ssf_error = error;
2999 /* not exactly what the user sent in, but should be close :) */
3000 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
3001 ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
3002 ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
3003 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
3004 ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
3005 ssf->ssf_info.sinfo_context = chk->rec.data.context;
3006 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3007 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3011 * trim off the sctp chunk header(it should be there)
3013 if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
3014 m_adj(chk->data, sizeof(struct sctp_data_chunk));
3015 sctp_mbuf_crush(chk->data);
3016 chk->send_size -= sizeof(struct sctp_data_chunk);
3019 SCTP_BUF_NEXT(m_notify) = chk->data;
3020 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
3021 /* Steal off the mbuf */
3024 * For this case, we check the actual socket buffer, since the assoc
3025 * is going away we don't want to overfill the socket buffer for a
3028 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3029 sctp_m_freem(m_notify);
3032 /* append to socket */
3033 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3036 if (control == NULL) {
3038 sctp_m_freem(m_notify);
3041 control->spec_flags = M_NOTIFICATION;
3042 sctp_add_to_readq(stcb->sctp_ep, stcb,
3044 &stcb->sctp_socket->so_rcv, 1,
3045 SCTP_READ_LOCK_NOT_HELD,
3051 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3052 struct sctp_stream_queue_pending *sp, int so_locked
3053 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3058 struct mbuf *m_notify;
3059 struct sctp_send_failed *ssf;
3060 struct sctp_queued_to_read *control;
3063 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)) {
3064 /* event not enabled */
3067 length = sizeof(struct sctp_send_failed) + sp->length;
3068 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 0, M_DONTWAIT, 1, MT_DATA);
3069 if (m_notify == NULL)
3072 SCTP_BUF_LEN(m_notify) = 0;
3073 ssf = mtod(m_notify, struct sctp_send_failed *);
3074 ssf->ssf_type = SCTP_SEND_FAILED;
3075 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
3076 ssf->ssf_flags = SCTP_DATA_UNSENT;
3078 ssf->ssf_flags = SCTP_DATA_SENT;
3079 ssf->ssf_length = length;
3080 ssf->ssf_error = error;
3081 /* not exactly what the user sent in, but should be close :) */
3082 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
3083 ssf->ssf_info.sinfo_stream = sp->stream;
3084 ssf->ssf_info.sinfo_ssn = sp->strseq;
3085 if (sp->some_taken) {
3086 ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3088 ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3090 ssf->ssf_info.sinfo_ppid = sp->ppid;
3091 ssf->ssf_info.sinfo_context = sp->context;
3092 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3093 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3094 SCTP_BUF_NEXT(m_notify) = sp->data;
3095 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
3097 /* Steal off the mbuf */
3100 * For this case, we check the actual socket buffer, since the assoc
3101 * is going away we don't want to overfill the socket buffer for a
3104 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3105 sctp_m_freem(m_notify);
3108 /* append to socket */
3109 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3112 if (control == NULL) {
3114 sctp_m_freem(m_notify);
3117 control->spec_flags = M_NOTIFICATION;
3118 sctp_add_to_readq(stcb->sctp_ep, stcb,
3120 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3126 sctp_notify_adaptation_layer(struct sctp_tcb *stcb,
3129 struct mbuf *m_notify;
3130 struct sctp_adaptation_event *sai;
3131 struct sctp_queued_to_read *control;
3133 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3134 /* event not enabled */
3137 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_DONTWAIT, 1, MT_DATA);
3138 if (m_notify == NULL)
3141 SCTP_BUF_LEN(m_notify) = 0;
3142 sai = mtod(m_notify, struct sctp_adaptation_event *);
3143 sai->sai_type = SCTP_ADAPTATION_INDICATION;
3145 sai->sai_length = sizeof(struct sctp_adaptation_event);
3146 sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3147 sai->sai_assoc_id = sctp_get_associd(stcb);
3149 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3150 SCTP_BUF_NEXT(m_notify) = NULL;
3152 /* append to socket */
3153 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3156 if (control == NULL) {
3158 sctp_m_freem(m_notify);
3161 control->length = SCTP_BUF_LEN(m_notify);
3162 control->spec_flags = M_NOTIFICATION;
3163 /* not that we need this */
3164 control->tail_mbuf = m_notify;
3165 sctp_add_to_readq(stcb->sctp_ep, stcb,
3167 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3170 /* This always must be called with the read-queue LOCKED in the INP */
3172 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3173 uint32_t val, int so_locked
3174 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3179 struct mbuf *m_notify;
3180 struct sctp_pdapi_event *pdapi;
3181 struct sctp_queued_to_read *control;
3184 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3185 /* event not enabled */
3188 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_DONTWAIT, 1, MT_DATA);
3189 if (m_notify == NULL)
3192 SCTP_BUF_LEN(m_notify) = 0;
3193 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3194 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3195 pdapi->pdapi_flags = 0;
3196 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3197 pdapi->pdapi_indication = error;
3198 pdapi->pdapi_stream = (val >> 16);
3199 pdapi->pdapi_seq = (val & 0x0000ffff);
3200 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3202 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3203 SCTP_BUF_NEXT(m_notify) = NULL;
3204 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3207 if (control == NULL) {
3209 sctp_m_freem(m_notify);
3212 control->spec_flags = M_NOTIFICATION;
3213 control->length = SCTP_BUF_LEN(m_notify);
3214 /* not that we need this */
3215 control->tail_mbuf = m_notify;
3216 control->held_length = 0;
3217 control->length = 0;
3218 sb = &stcb->sctp_socket->so_rcv;
3219 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3220 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
3222 sctp_sballoc(stcb, sb, m_notify);
3223 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3224 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3226 atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify));
3227 control->end_added = 1;
3228 if (stcb->asoc.control_pdapi)
3229 TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
3231 /* we really should not see this case */
3232 TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
3234 if (stcb->sctp_ep && stcb->sctp_socket) {
3235 /* This should always be the case */
3236 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3239 so = SCTP_INP_SO(stcb->sctp_ep);
3241 atomic_add_int(&stcb->asoc.refcnt, 1);
3242 SCTP_TCB_UNLOCK(stcb);
3243 SCTP_SOCKET_LOCK(so, 1);
3244 SCTP_TCB_LOCK(stcb);
3245 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3246 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3247 SCTP_SOCKET_UNLOCK(so, 1);
3252 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3253 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3255 SCTP_SOCKET_UNLOCK(so, 1);
3262 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3264 struct mbuf *m_notify;
3265 struct sctp_shutdown_event *sse;
3266 struct sctp_queued_to_read *control;
3269 * For TCP model AND UDP connected sockets we will send an error up
3270 * when an SHUTDOWN completes
3272 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3273 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3274 /* mark socket closed for read/write and wakeup! */
3275 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3278 so = SCTP_INP_SO(stcb->sctp_ep);
3279 atomic_add_int(&stcb->asoc.refcnt, 1);
3280 SCTP_TCB_UNLOCK(stcb);
3281 SCTP_SOCKET_LOCK(so, 1);
3282 SCTP_TCB_LOCK(stcb);
3283 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3284 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3285 SCTP_SOCKET_UNLOCK(so, 1);
3289 socantsendmore(stcb->sctp_socket);
3290 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3291 SCTP_SOCKET_UNLOCK(so, 1);
3294 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3295 /* event not enabled */
3298 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_DONTWAIT, 1, MT_DATA);
3299 if (m_notify == NULL)
3302 sse = mtod(m_notify, struct sctp_shutdown_event *);
3303 sse->sse_type = SCTP_SHUTDOWN_EVENT;
3305 sse->sse_length = sizeof(struct sctp_shutdown_event);
3306 sse->sse_assoc_id = sctp_get_associd(stcb);
3308 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3309 SCTP_BUF_NEXT(m_notify) = NULL;
3311 /* append to socket */
3312 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3315 if (control == NULL) {
3317 sctp_m_freem(m_notify);
3320 control->spec_flags = M_NOTIFICATION;
3321 control->length = SCTP_BUF_LEN(m_notify);
3322 /* not that we need this */
3323 control->tail_mbuf = m_notify;
3324 sctp_add_to_readq(stcb->sctp_ep, stcb,
3326 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3330 sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3332 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3337 struct mbuf *m_notify;
3338 struct sctp_sender_dry_event *event;
3339 struct sctp_queued_to_read *control;
3341 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_DRYEVNT)) {
3342 /* event not enabled */
3345 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_DONTWAIT, 1, MT_DATA);
3346 if (m_notify == NULL) {
3350 SCTP_BUF_LEN(m_notify) = 0;
3351 event = mtod(m_notify, struct sctp_sender_dry_event *);
3352 event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3353 event->sender_dry_flags = 0;
3354 event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3355 event->sender_dry_assoc_id = sctp_get_associd(stcb);
3357 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3358 SCTP_BUF_NEXT(m_notify) = NULL;
3360 /* append to socket */
3361 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3362 0, 0, 0, 0, 0, 0, m_notify);
3363 if (control == NULL) {
3365 sctp_m_freem(m_notify);
3368 control->length = SCTP_BUF_LEN(m_notify);
3369 control->spec_flags = M_NOTIFICATION;
3370 /* not that we need this */
3371 control->tail_mbuf = m_notify;
3372 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3373 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3378 sctp_notify_stream_reset_add(struct sctp_tcb *stcb, int number_entries, int flag)
3380 struct mbuf *m_notify;
3381 struct sctp_queued_to_read *control;
3382 struct sctp_stream_reset_event *strreset;
3385 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
3386 /* event not enabled */
3389 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3390 if (m_notify == NULL)
3393 SCTP_BUF_LEN(m_notify) = 0;
3394 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3395 if (len > M_TRAILINGSPACE(m_notify)) {
3396 /* never enough room */
3397 sctp_m_freem(m_notify);
3400 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3401 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3402 strreset->strreset_flags = SCTP_STRRESET_ADD_STREAM | flag;
3403 strreset->strreset_length = len;
3404 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3405 strreset->strreset_list[0] = number_entries;
3407 SCTP_BUF_LEN(m_notify) = len;
3408 SCTP_BUF_NEXT(m_notify) = NULL;
3409 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3411 sctp_m_freem(m_notify);
3414 /* append to socket */
3415 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3418 if (control == NULL) {
3420 sctp_m_freem(m_notify);
3423 control->spec_flags = M_NOTIFICATION;
3424 control->length = SCTP_BUF_LEN(m_notify);
3425 /* not that we need this */
3426 control->tail_mbuf = m_notify;
3427 sctp_add_to_readq(stcb->sctp_ep, stcb,
3429 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3434 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3435 int number_entries, uint16_t * list, int flag)
3437 struct mbuf *m_notify;
3438 struct sctp_queued_to_read *control;
3439 struct sctp_stream_reset_event *strreset;
3442 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
3443 /* event not enabled */
3446 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3447 if (m_notify == NULL)
3450 SCTP_BUF_LEN(m_notify) = 0;
3451 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3452 if (len > M_TRAILINGSPACE(m_notify)) {
3453 /* never enough room */
3454 sctp_m_freem(m_notify);
3457 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3458 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3459 if (number_entries == 0) {
3460 strreset->strreset_flags = flag | SCTP_STRRESET_ALL_STREAMS;
3462 strreset->strreset_flags = flag | SCTP_STRRESET_STREAM_LIST;
3464 strreset->strreset_length = len;
3465 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3466 if (number_entries) {
3469 for (i = 0; i < number_entries; i++) {
3470 strreset->strreset_list[i] = ntohs(list[i]);
3473 SCTP_BUF_LEN(m_notify) = len;
3474 SCTP_BUF_NEXT(m_notify) = NULL;
3475 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3477 sctp_m_freem(m_notify);
3480 /* append to socket */
3481 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3484 if (control == NULL) {
3486 sctp_m_freem(m_notify);
3489 control->spec_flags = M_NOTIFICATION;
3490 control->length = SCTP_BUF_LEN(m_notify);
3491 /* not that we need this */
3492 control->tail_mbuf = m_notify;
3493 sctp_add_to_readq(stcb->sctp_ep, stcb,
3495 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3500 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3501 uint32_t error, void *data, int so_locked
3502 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3507 if ((stcb == NULL) ||
3508 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3509 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3510 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3511 /* If the socket is gone we are out of here */
3514 if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3517 if (stcb && ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3518 (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED))) {
3519 if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
3520 (notification == SCTP_NOTIFY_INTERFACE_UP) ||
3521 (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
3522 /* Don't report these in front states */
3526 switch (notification) {
3527 case SCTP_NOTIFY_ASSOC_UP:
3528 if (stcb->asoc.assoc_up_sent == 0) {
3529 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, so_locked);
3530 stcb->asoc.assoc_up_sent = 1;
3532 if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
3533 sctp_notify_adaptation_layer(stcb, error);
3535 if (stcb->asoc.peer_supports_auth == 0) {
3536 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3540 case SCTP_NOTIFY_ASSOC_DOWN:
3541 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, so_locked);
3543 case SCTP_NOTIFY_INTERFACE_DOWN:
3545 struct sctp_nets *net;
3547 net = (struct sctp_nets *)data;
3548 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3549 (struct sockaddr *)&net->ro._l_addr, error);
3552 case SCTP_NOTIFY_INTERFACE_UP:
3554 struct sctp_nets *net;
3556 net = (struct sctp_nets *)data;
3557 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3558 (struct sockaddr *)&net->ro._l_addr, error);
3561 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3563 struct sctp_nets *net;
3565 net = (struct sctp_nets *)data;
3566 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3567 (struct sockaddr *)&net->ro._l_addr, error);
3570 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3571 sctp_notify_send_failed2(stcb, error,
3572 (struct sctp_stream_queue_pending *)data, so_locked);
3574 case SCTP_NOTIFY_DG_FAIL:
3575 sctp_notify_send_failed(stcb, error,
3576 (struct sctp_tmit_chunk *)data, so_locked);
3578 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3582 val = *((uint32_t *) data);
3584 sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3587 case SCTP_NOTIFY_STRDATA_ERR:
3589 case SCTP_NOTIFY_ASSOC_ABORTED:
3590 if ((stcb) && (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3591 ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED))) {
3592 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, NULL, so_locked);
3594 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, NULL, so_locked);
3597 case SCTP_NOTIFY_PEER_OPENED_STREAM:
3599 case SCTP_NOTIFY_STREAM_OPENED_OK:
3601 case SCTP_NOTIFY_ASSOC_RESTART:
3602 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, data, so_locked);
3603 if (stcb->asoc.peer_supports_auth == 0) {
3604 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3608 case SCTP_NOTIFY_HB_RESP:
3610 case SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK:
3611 sctp_notify_stream_reset_add(stcb, error, SCTP_STRRESET_INBOUND_STR);
3613 case SCTP_NOTIFY_STR_RESET_ADD_OK:
3614 sctp_notify_stream_reset_add(stcb, error, SCTP_STRRESET_OUTBOUND_STR);
3616 case SCTP_NOTIFY_STR_RESET_ADD_FAIL:
3617 sctp_notify_stream_reset_add(stcb, error, (SCTP_STRRESET_FAILED | SCTP_STRRESET_OUTBOUND_STR));
3620 case SCTP_NOTIFY_STR_RESET_SEND:
3621 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_OUTBOUND_STR);
3623 case SCTP_NOTIFY_STR_RESET_RECV:
3624 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_INBOUND_STR);
3626 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3627 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_OUTBOUND_STR | SCTP_STRRESET_FAILED));
3629 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3630 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_INBOUND_STR | SCTP_STRRESET_FAILED));
3632 case SCTP_NOTIFY_ASCONF_ADD_IP:
3633 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3636 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3637 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3640 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3641 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3644 case SCTP_NOTIFY_ASCONF_SUCCESS:
3646 case SCTP_NOTIFY_ASCONF_FAILED:
3648 case SCTP_NOTIFY_PEER_SHUTDOWN:
3649 sctp_notify_shutdown_event(stcb);
3651 case SCTP_NOTIFY_AUTH_NEW_KEY:
3652 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY, error,
3653 (uint16_t) (uintptr_t) data,
3656 case SCTP_NOTIFY_AUTH_FREE_KEY:
3657 sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3658 (uint16_t) (uintptr_t) data,
3661 case SCTP_NOTIFY_NO_PEER_AUTH:
3662 sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3663 (uint16_t) (uintptr_t) data,
3666 case SCTP_NOTIFY_SENDER_DRY:
3667 sctp_notify_sender_dry_event(stcb, so_locked);
3670 SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3671 __FUNCTION__, notification, notification);
3677 sctp_report_all_outbound(struct sctp_tcb *stcb, int holds_lock, int so_locked
3678 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3683 struct sctp_association *asoc;
3684 struct sctp_stream_out *outs;
3685 struct sctp_tmit_chunk *chk;
3686 struct sctp_stream_queue_pending *sp;
3694 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3695 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3696 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3699 /* now through all the gunk freeing chunks */
3700 if (holds_lock == 0) {
3701 SCTP_TCB_SEND_LOCK(stcb);
3703 /* sent queue SHOULD be empty */
3704 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3705 chk = TAILQ_FIRST(&asoc->sent_queue);
3707 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3708 asoc->sent_queue_cnt--;
3709 if (chk->data != NULL) {
3710 sctp_free_bufspace(stcb, asoc, chk, 1);
3711 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3712 SCTP_NOTIFY_DATAGRAM_SENT, chk, so_locked);
3714 sctp_m_freem(chk->data);
3718 sctp_free_a_chunk(stcb, chk);
3719 /* sa_ignore FREED_MEMORY */
3720 chk = TAILQ_FIRST(&asoc->sent_queue);
3723 /* pending send queue SHOULD be empty */
3724 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3725 chk = TAILQ_FIRST(&asoc->send_queue);
3727 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3728 asoc->send_queue_cnt--;
3729 if (chk->data != NULL) {
3730 sctp_free_bufspace(stcb, asoc, chk, 1);
3731 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3732 SCTP_NOTIFY_DATAGRAM_UNSENT, chk, so_locked);
3734 sctp_m_freem(chk->data);
3738 sctp_free_a_chunk(stcb, chk);
3739 /* sa_ignore FREED_MEMORY */
3740 chk = TAILQ_FIRST(&asoc->send_queue);
3743 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3744 /* For each stream */
3745 outs = &stcb->asoc.strmout[i];
3746 /* clean up any sends there */
3747 stcb->asoc.locked_on_sending = NULL;
3748 sp = TAILQ_FIRST(&outs->outqueue);
3750 stcb->asoc.stream_queue_cnt--;
3751 TAILQ_REMOVE(&outs->outqueue, sp, next);
3752 sctp_free_spbufspace(stcb, asoc, sp);
3753 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3754 SCTP_NOTIFY_DATAGRAM_UNSENT, (void *)sp, so_locked);
3756 sctp_m_freem(sp->data);
3760 sctp_free_remote_addr(sp->net);
3762 /* Free the chunk */
3763 sctp_free_a_strmoq(stcb, sp);
3764 /* sa_ignore FREED_MEMORY */
3765 sp = TAILQ_FIRST(&outs->outqueue);
3769 if (holds_lock == 0) {
3770 SCTP_TCB_SEND_UNLOCK(stcb);
3775 sctp_abort_notification(struct sctp_tcb *stcb, int error, int so_locked
3776 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3785 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3786 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3787 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3790 /* Tell them we lost the asoc */
3791 sctp_report_all_outbound(stcb, 1, so_locked);
3792 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3793 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3794 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3795 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3797 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_ABORTED, stcb, error, NULL, so_locked);
3801 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3802 struct mbuf *m, int iphlen, struct sctphdr *sh, struct mbuf *op_err,
3803 uint32_t vrf_id, uint16_t port)
3807 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3814 /* We have a TCB to abort, send notification too */
3815 vtag = stcb->asoc.peer_vtag;
3816 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
3817 /* get the assoc vrf id and table id */
3818 vrf_id = stcb->asoc.vrf_id;
3819 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3821 sctp_send_abort(m, iphlen, sh, vtag, op_err, vrf_id, port);
3823 /* Ok, now lets free it */
3824 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3825 so = SCTP_INP_SO(inp);
3826 atomic_add_int(&stcb->asoc.refcnt, 1);
3827 SCTP_TCB_UNLOCK(stcb);
3828 SCTP_SOCKET_LOCK(so, 1);
3829 SCTP_TCB_LOCK(stcb);
3830 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3832 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
3833 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3834 SCTP_SOCKET_UNLOCK(so, 1);
3839 #ifdef SCTP_ASOCLOG_OF_TSNS
3841 sctp_print_out_track_log(struct sctp_tcb *stcb)
3846 SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
3847 SCTP_PRINTF("IN bound TSN log-aaa\n");
3848 if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
3849 SCTP_PRINTF("None rcvd\n");
3852 if (stcb->asoc.tsn_in_wrapped) {
3853 for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
3854 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3855 stcb->asoc.in_tsnlog[i].tsn,
3856 stcb->asoc.in_tsnlog[i].strm,
3857 stcb->asoc.in_tsnlog[i].seq,
3858 stcb->asoc.in_tsnlog[i].flgs,
3859 stcb->asoc.in_tsnlog[i].sz);
3862 if (stcb->asoc.tsn_in_at) {
3863 for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
3864 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3865 stcb->asoc.in_tsnlog[i].tsn,
3866 stcb->asoc.in_tsnlog[i].strm,
3867 stcb->asoc.in_tsnlog[i].seq,
3868 stcb->asoc.in_tsnlog[i].flgs,
3869 stcb->asoc.in_tsnlog[i].sz);
3873 SCTP_PRINTF("OUT bound TSN log-aaa\n");
3874 if ((stcb->asoc.tsn_out_at == 0) &&
3875 (stcb->asoc.tsn_out_wrapped == 0)) {
3876 SCTP_PRINTF("None sent\n");
3878 if (stcb->asoc.tsn_out_wrapped) {
3879 for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
3880 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3881 stcb->asoc.out_tsnlog[i].tsn,
3882 stcb->asoc.out_tsnlog[i].strm,
3883 stcb->asoc.out_tsnlog[i].seq,
3884 stcb->asoc.out_tsnlog[i].flgs,
3885 stcb->asoc.out_tsnlog[i].sz);
3888 if (stcb->asoc.tsn_out_at) {
3889 for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
3890 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3891 stcb->asoc.out_tsnlog[i].tsn,
3892 stcb->asoc.out_tsnlog[i].strm,
3893 stcb->asoc.out_tsnlog[i].seq,
3894 stcb->asoc.out_tsnlog[i].flgs,
3895 stcb->asoc.out_tsnlog[i].sz);
3904 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3905 int error, struct mbuf *op_err,
3907 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3914 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3919 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3920 so = SCTP_INP_SO(inp);
3923 /* Got to have a TCB */
3924 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3925 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3926 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3927 SCTP_CALLED_DIRECTLY_NOCMPSET);
3932 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3934 vtag = stcb->asoc.peer_vtag;
3935 /* notify the ulp */
3936 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0)
3937 sctp_abort_notification(stcb, error, so_locked);
3938 /* notify the peer */
3939 #if defined(SCTP_PANIC_ON_ABORT)
3940 panic("aborting an association");
3942 sctp_send_abort_tcb(stcb, op_err, so_locked);
3943 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3944 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3945 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3946 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3948 /* now free the asoc */
3949 #ifdef SCTP_ASOCLOG_OF_TSNS
3950 sctp_print_out_track_log(stcb);
3952 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3954 atomic_add_int(&stcb->asoc.refcnt, 1);
3955 SCTP_TCB_UNLOCK(stcb);
3956 SCTP_SOCKET_LOCK(so, 1);
3957 SCTP_TCB_LOCK(stcb);
3958 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3961 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
3962 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3964 SCTP_SOCKET_UNLOCK(so, 1);
3970 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
3971 struct sctp_inpcb *inp, struct mbuf *op_err, uint32_t vrf_id, uint16_t port)
3973 struct sctp_chunkhdr *ch, chunk_buf;
3974 unsigned int chk_length;
3976 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
3977 /* Generate a TO address for future reference */
3978 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3979 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3980 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3981 SCTP_CALLED_DIRECTLY_NOCMPSET);
3984 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3985 sizeof(*ch), (uint8_t *) & chunk_buf);
3986 while (ch != NULL) {
3987 chk_length = ntohs(ch->chunk_length);
3988 if (chk_length < sizeof(*ch)) {
3989 /* break to abort land */
3992 switch (ch->chunk_type) {
3993 case SCTP_COOKIE_ECHO:
3994 /* We hit here only if the assoc is being freed */
3996 case SCTP_PACKET_DROPPED:
3997 /* we don't respond to pkt-dropped */
3999 case SCTP_ABORT_ASSOCIATION:
4000 /* we don't respond with an ABORT to an ABORT */
4002 case SCTP_SHUTDOWN_COMPLETE:
4004 * we ignore it since we are not waiting for it and
4008 case SCTP_SHUTDOWN_ACK:
4009 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
4014 offset += SCTP_SIZE32(chk_length);
4015 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4016 sizeof(*ch), (uint8_t *) & chunk_buf);
4018 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
4022 * check the inbound datagram to make sure there is not an abort inside it,
4023 * if there is return 1, else return 0.
4026 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
4028 struct sctp_chunkhdr *ch;
4029 struct sctp_init_chunk *init_chk, chunk_buf;
4031 unsigned int chk_length;
4033 offset = iphlen + sizeof(struct sctphdr);
4034 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4035 (uint8_t *) & chunk_buf);
4036 while (ch != NULL) {
4037 chk_length = ntohs(ch->chunk_length);
4038 if (chk_length < sizeof(*ch)) {
4039 /* packet is probably corrupt */
4042 /* we seem to be ok, is it an abort? */
4043 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4044 /* yep, tell them */
4047 if (ch->chunk_type == SCTP_INITIATION) {
4048 /* need to update the Vtag */
4049 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4050 offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
4051 if (init_chk != NULL) {
4052 *vtagfill = ntohl(init_chk->init.initiate_tag);
4055 /* Nope, move to the next chunk */
4056 offset += SCTP_SIZE32(chk_length);
4057 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4058 sizeof(*ch), (uint8_t *) & chunk_buf);
4064 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4065 * set (i.e. it's 0) so, create this function to compare link local scopes
4069 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4071 struct sockaddr_in6 a, b;
4077 if (a.sin6_scope_id == 0)
4078 if (sa6_recoverscope(&a)) {
4079 /* can't get scope, so can't match */
4082 if (b.sin6_scope_id == 0)
4083 if (sa6_recoverscope(&b)) {
4084 /* can't get scope, so can't match */
4087 if (a.sin6_scope_id != b.sin6_scope_id)
4094 * returns a sockaddr_in6 with embedded scope recovered and removed
4096 struct sockaddr_in6 *
4097 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4099 /* check and strip embedded scope junk */
4100 if (addr->sin6_family == AF_INET6) {
4101 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4102 if (addr->sin6_scope_id == 0) {
4104 if (!sa6_recoverscope(store)) {
4105 /* use the recovered scope */
4109 /* else, return the original "to" addr */
4110 in6_clearscope(&addr->sin6_addr);
4120 * are the two addresses the same? currently a "scopeless" check returns: 1
4124 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4128 if (sa1 == NULL || sa2 == NULL)
4131 /* must be the same family */
4132 if (sa1->sa_family != sa2->sa_family)
4135 switch (sa1->sa_family) {
4139 /* IPv6 addresses */
4140 struct sockaddr_in6 *sin6_1, *sin6_2;
4142 sin6_1 = (struct sockaddr_in6 *)sa1;
4143 sin6_2 = (struct sockaddr_in6 *)sa2;
4144 return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4150 /* IPv4 addresses */
4151 struct sockaddr_in *sin_1, *sin_2;
4153 sin_1 = (struct sockaddr_in *)sa1;
4154 sin_2 = (struct sockaddr_in *)sa2;
4155 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
4158 /* we don't do these... */
4164 sctp_print_address(struct sockaddr *sa)
4167 char ip6buf[INET6_ADDRSTRLEN];
4172 switch (sa->sa_family) {
4176 struct sockaddr_in6 *sin6;
4178 sin6 = (struct sockaddr_in6 *)sa;
4179 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
4180 ip6_sprintf(ip6buf, &sin6->sin6_addr),
4181 ntohs(sin6->sin6_port),
4182 sin6->sin6_scope_id);
4188 struct sockaddr_in *sin;
4191 sin = (struct sockaddr_in *)sa;
4192 p = (unsigned char *)&sin->sin_addr;
4193 SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4194 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
4204 sctp_print_address_pkt(struct ip *iph, struct sctphdr *sh)
4206 switch (iph->ip_v) {
4209 struct sockaddr_in lsa, fsa;
4211 bzero(&lsa, sizeof(lsa));
4212 lsa.sin_len = sizeof(lsa);
4213 lsa.sin_family = AF_INET;
4214 lsa.sin_addr = iph->ip_src;
4215 lsa.sin_port = sh->src_port;
4216 bzero(&fsa, sizeof(fsa));
4217 fsa.sin_len = sizeof(fsa);
4218 fsa.sin_family = AF_INET;
4219 fsa.sin_addr = iph->ip_dst;
4220 fsa.sin_port = sh->dest_port;
4221 SCTP_PRINTF("src: ");
4222 sctp_print_address((struct sockaddr *)&lsa);
4223 SCTP_PRINTF("dest: ");
4224 sctp_print_address((struct sockaddr *)&fsa);
4228 case IPV6_VERSION >> 4:
4230 struct ip6_hdr *ip6;
4231 struct sockaddr_in6 lsa6, fsa6;
4233 ip6 = (struct ip6_hdr *)iph;
4234 bzero(&lsa6, sizeof(lsa6));
4235 lsa6.sin6_len = sizeof(lsa6);
4236 lsa6.sin6_family = AF_INET6;
4237 lsa6.sin6_addr = ip6->ip6_src;
4238 lsa6.sin6_port = sh->src_port;
4239 bzero(&fsa6, sizeof(fsa6));
4240 fsa6.sin6_len = sizeof(fsa6);
4241 fsa6.sin6_family = AF_INET6;
4242 fsa6.sin6_addr = ip6->ip6_dst;
4243 fsa6.sin6_port = sh->dest_port;
4244 SCTP_PRINTF("src: ");
4245 sctp_print_address((struct sockaddr *)&lsa6);
4246 SCTP_PRINTF("dest: ");
4247 sctp_print_address((struct sockaddr *)&fsa6);
4258 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4259 struct sctp_inpcb *new_inp,
4260 struct sctp_tcb *stcb,
4264 * go through our old INP and pull off any control structures that
4265 * belong to stcb and move then to the new inp.
4267 struct socket *old_so, *new_so;
4268 struct sctp_queued_to_read *control, *nctl;
4269 struct sctp_readhead tmp_queue;
4273 old_so = old_inp->sctp_socket;
4274 new_so = new_inp->sctp_socket;
4275 TAILQ_INIT(&tmp_queue);
4276 error = sblock(&old_so->so_rcv, waitflags);
4279 * Gak, can't get sblock, we have a problem. data will be
4280 * left stranded.. and we don't dare look at it since the
4281 * other thread may be reading something. Oh well, its a
4282 * screwed up app that does a peeloff OR a accept while
4283 * reading from the main socket... actually its only the
4284 * peeloff() case, since I think read will fail on a
4285 * listening socket..
4289 /* lock the socket buffers */
4290 SCTP_INP_READ_LOCK(old_inp);
4291 control = TAILQ_FIRST(&old_inp->read_queue);
4292 /* Pull off all for out target stcb */
4294 nctl = TAILQ_NEXT(control, next);
4295 if (control->stcb == stcb) {
4296 /* remove it we want it */
4297 TAILQ_REMOVE(&old_inp->read_queue, control, next);
4298 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4301 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4302 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
4304 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4305 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4306 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4308 m = SCTP_BUF_NEXT(m);
4313 SCTP_INP_READ_UNLOCK(old_inp);
4314 /* Remove the sb-lock on the old socket */
4316 sbunlock(&old_so->so_rcv);
4317 /* Now we move them over to the new socket buffer */
4318 control = TAILQ_FIRST(&tmp_queue);
4319 SCTP_INP_READ_LOCK(new_inp);
4321 nctl = TAILQ_NEXT(control, next);
4322 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4325 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4326 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4328 sctp_sballoc(stcb, &new_so->so_rcv, m);
4329 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4330 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4332 m = SCTP_BUF_NEXT(m);
4336 SCTP_INP_READ_UNLOCK(new_inp);
4340 sctp_add_to_readq(struct sctp_inpcb *inp,
4341 struct sctp_tcb *stcb,
4342 struct sctp_queued_to_read *control,
4345 int inp_read_lock_held,
4347 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4353 * Here we must place the control on the end of the socket read
4354 * queue AND increment sb_cc so that select will work properly on
4357 struct mbuf *m, *prev = NULL;
4362 panic("Gak, inp NULL on add_to_readq");
4366 if (inp_read_lock_held == 0)
4367 SCTP_INP_READ_LOCK(inp);
4368 if (!(control->spec_flags & M_NOTIFICATION)) {
4369 atomic_add_int(&inp->total_recvs, 1);
4370 if (!control->do_not_ref_stcb) {
4371 atomic_add_int(&stcb->total_recvs, 1);
4375 control->held_length = 0;
4376 control->length = 0;
4378 if (SCTP_BUF_LEN(m) == 0) {
4379 /* Skip mbufs with NO length */
4382 control->data = sctp_m_free(m);
4385 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4386 m = SCTP_BUF_NEXT(prev);
4389 control->tail_mbuf = prev;
4394 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4395 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4397 sctp_sballoc(stcb, sb, m);
4398 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4399 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4401 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4402 m = SCTP_BUF_NEXT(m);
4405 control->tail_mbuf = prev;
4407 /* Everything got collapsed out?? */
4408 if (inp_read_lock_held == 0)
4409 SCTP_INP_READ_UNLOCK(inp);
4413 control->end_added = 1;
4415 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4416 if (inp_read_lock_held == 0)
4417 SCTP_INP_READ_UNLOCK(inp);
4418 if (inp && inp->sctp_socket) {
4419 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
4420 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4422 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4425 so = SCTP_INP_SO(inp);
4427 atomic_add_int(&stcb->asoc.refcnt, 1);
4428 SCTP_TCB_UNLOCK(stcb);
4429 SCTP_SOCKET_LOCK(so, 1);
4430 SCTP_TCB_LOCK(stcb);
4431 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4432 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4433 SCTP_SOCKET_UNLOCK(so, 1);
4438 sctp_sorwakeup(inp, inp->sctp_socket);
4439 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4441 SCTP_SOCKET_UNLOCK(so, 1);
4450 sctp_append_to_readq(struct sctp_inpcb *inp,
4451 struct sctp_tcb *stcb,
4452 struct sctp_queued_to_read *control,
4459 * A partial delivery API event is underway. OR we are appending on
4460 * the reassembly queue.
4462 * If PDAPI this means we need to add m to the end of the data.
4463 * Increase the length in the control AND increment the sb_cc.
4464 * Otherwise sb is NULL and all we need to do is put it at the end
4465 * of the mbuf chain.
4468 struct mbuf *mm, *tail = NULL, *prev = NULL;
4471 SCTP_INP_READ_LOCK(inp);
4473 if (control == NULL) {
4476 SCTP_INP_READ_UNLOCK(inp);
4480 if (control->end_added) {
4481 /* huh this one is complete? */
4489 if (SCTP_BUF_LEN(mm) == 0) {
4490 /* Skip mbufs with NO lenght */
4493 m = sctp_m_free(mm);
4496 SCTP_BUF_NEXT(prev) = sctp_m_free(mm);
4497 mm = SCTP_BUF_NEXT(prev);
4502 len += SCTP_BUF_LEN(mm);
4504 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4505 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm));
4507 sctp_sballoc(stcb, sb, mm);
4508 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4509 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4512 mm = SCTP_BUF_NEXT(mm);
4517 /* Really there should always be a prev */
4519 /* Huh nothing left? */
4521 panic("Nothing left to add?");
4528 if (control->tail_mbuf) {
4530 SCTP_BUF_NEXT(control->tail_mbuf) = m;
4531 control->tail_mbuf = tail;
4535 if (control->data != NULL) {
4536 panic("This should NOT happen");
4540 control->tail_mbuf = tail;
4542 atomic_add_int(&control->length, len);
4544 /* message is complete */
4545 if (stcb && (control == stcb->asoc.control_pdapi)) {
4546 stcb->asoc.control_pdapi = NULL;
4548 control->held_length = 0;
4549 control->end_added = 1;
4552 control->do_not_ref_stcb = 1;
4555 * When we are appending in partial delivery, the cum-ack is used
4556 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
4557 * is populated in the outbound sinfo structure from the true cumack
4558 * if the association exists...
4560 control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
4562 SCTP_INP_READ_UNLOCK(inp);
4564 if (inp && inp->sctp_socket) {
4565 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
4566 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4568 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4571 so = SCTP_INP_SO(inp);
4572 atomic_add_int(&stcb->asoc.refcnt, 1);
4573 SCTP_TCB_UNLOCK(stcb);
4574 SCTP_SOCKET_LOCK(so, 1);
4575 SCTP_TCB_LOCK(stcb);
4576 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4577 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4578 SCTP_SOCKET_UNLOCK(so, 1);
4582 sctp_sorwakeup(inp, inp->sctp_socket);
4583 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4584 SCTP_SOCKET_UNLOCK(so, 1);
4593 /*************HOLD THIS COMMENT FOR PATCH FILE OF
4594 *************ALTERNATE ROUTING CODE
4597 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4598 *************ALTERNATE ROUTING CODE
4602 sctp_generate_invmanparam(int err)
4604 /* Return a MBUF with a invalid mandatory parameter */
4607 m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
4609 struct sctp_paramhdr *ph;
4611 SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
4612 ph = mtod(m, struct sctp_paramhdr *);
4613 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4614 ph->param_type = htons(err);
4619 #ifdef SCTP_MBCNT_LOGGING
4621 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4622 struct sctp_tmit_chunk *tp1, int chk_cnt)
4624 if (tp1->data == NULL) {
4627 asoc->chunks_on_out_queue -= chk_cnt;
4628 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4629 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4630 asoc->total_output_queue_size,
4635 if (asoc->total_output_queue_size >= tp1->book_size) {
4636 atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4638 asoc->total_output_queue_size = 0;
4641 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4642 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4643 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4644 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4646 stcb->sctp_socket->so_snd.sb_cc = 0;
4655 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4656 int reason, int so_locked
4657 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4662 struct sctp_stream_out *strq;
4663 struct sctp_tmit_chunk *chk = NULL;
4664 struct sctp_stream_queue_pending *sp;
4665 uint16_t stream = 0, seq = 0;
4666 uint8_t foundeom = 0;
4669 int do_wakeup_routine = 0;
4671 stream = tp1->rec.data.stream_number;
4672 seq = tp1->rec.data.stream_seq;
4674 ret_sz += tp1->book_size;
4675 if (tp1->data != NULL) {
4676 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4677 sctp_flight_size_decrease(tp1);
4678 sctp_total_flight_decrease(stcb, tp1);
4680 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4681 stcb->asoc.peers_rwnd += tp1->send_size;
4682 stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
4683 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, reason, tp1, so_locked);
4685 sctp_m_freem(tp1->data);
4688 do_wakeup_routine = 1;
4689 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4690 stcb->asoc.sent_queue_cnt_removeable--;
4693 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4694 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4695 SCTP_DATA_NOT_FRAG) {
4696 /* not frag'ed we ae done */
4699 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4700 /* end of frag, we are done */
4705 * Its a begin or middle piece, we must mark all of
4709 tp1 = TAILQ_NEXT(tp1, sctp_next);
4711 } while (tp1 && notdone);
4712 if (foundeom == 0) {
4714 * The multi-part message was scattered across the send and
4718 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
4720 * recurse throught the send_queue too, starting at the
4724 (tp1->rec.data.stream_number == stream) &&
4725 (tp1->rec.data.stream_seq == seq)) {
4727 * save to chk in case we have some on stream out
4728 * queue. If so and we have an un-transmitted one we
4729 * don't have to fudge the TSN.
4732 ret_sz += tp1->book_size;
4733 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4734 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, reason, tp1, so_locked);
4736 sctp_m_freem(tp1->data);
4739 /* No flight involved here book the size to 0 */
4741 if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4744 do_wakeup_routine = 1;
4745 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4746 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4748 * on to the sent queue so we can wait for it to be
4751 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4753 stcb->asoc.send_queue_cnt--;
4754 stcb->asoc.sent_queue_cnt++;
4758 if (foundeom == 0) {
4760 * Still no eom found. That means there is stuff left on the
4761 * stream out queue.. yuck.
4763 strq = &stcb->asoc.strmout[stream];
4764 SCTP_TCB_SEND_LOCK(stcb);
4765 sp = TAILQ_FIRST(&strq->outqueue);
4766 while (sp->strseq <= seq) {
4767 /* Check if its our SEQ */
4768 if (sp->strseq == seq) {
4769 sp->discard_rest = 1;
4771 * We may need to put a chunk on the queue
4772 * that holds the TSN that would have been
4773 * sent with the LAST bit.
4776 /* Yep, we have to */
4777 sctp_alloc_a_chunk(stcb, chk);
4780 * we are hosed. All we can
4781 * do is nothing.. which
4782 * will cause an abort if
4783 * the peer is paying
4788 memset(chk, 0, sizeof(*chk));
4789 chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG;
4790 chk->sent = SCTP_FORWARD_TSN_SKIP;
4791 chk->asoc = &stcb->asoc;
4792 chk->rec.data.stream_seq = sp->strseq;
4793 chk->rec.data.stream_number = sp->stream;
4794 chk->rec.data.payloadtype = sp->ppid;
4795 chk->rec.data.context = sp->context;
4796 chk->flags = sp->act_flags;
4797 chk->whoTo = sp->net;
4798 atomic_add_int(&chk->whoTo->ref_count, 1);
4799 chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
4800 stcb->asoc.pr_sctp_cnt++;
4801 chk->pr_sctp_on = 1;
4802 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
4803 stcb->asoc.sent_queue_cnt++;
4804 stcb->asoc.pr_sctp_cnt++;
4806 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4811 * Pull any data to free up the SB
4812 * and allow sender to "add more"
4813 * whilc we will throw away :-)
4815 sctp_free_spbufspace(stcb, &stcb->asoc,
4817 ret_sz += sp->length;
4818 do_wakeup_routine = 1;
4820 sctp_m_freem(sp->data);
4823 sp->tail_mbuf = NULL;
4827 /* Next one please */
4828 sp = TAILQ_NEXT(sp, next);
4831 SCTP_TCB_SEND_UNLOCK(stcb);
4833 if (do_wakeup_routine) {
4834 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4837 so = SCTP_INP_SO(stcb->sctp_ep);
4839 atomic_add_int(&stcb->asoc.refcnt, 1);
4840 SCTP_TCB_UNLOCK(stcb);
4841 SCTP_SOCKET_LOCK(so, 1);
4842 SCTP_TCB_LOCK(stcb);
4843 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4844 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4845 /* assoc was freed while we were unlocked */
4846 SCTP_SOCKET_UNLOCK(so, 1);
4851 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
4852 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4854 SCTP_SOCKET_UNLOCK(so, 1);
4862 * checks to see if the given address, sa, is one that is currently known by
4863 * the kernel note: can't distinguish the same address on multiple interfaces
4864 * and doesn't handle multiple addresses with different zone/scope id's note:
4865 * ifa_ifwithaddr() compares the entire sockaddr struct
4868 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
4871 struct sctp_laddr *laddr;
4873 if (holds_lock == 0) {
4874 SCTP_INP_RLOCK(inp);
4876 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4877 if (laddr->ifa == NULL)
4879 if (addr->sa_family != laddr->ifa->address.sa.sa_family)
4881 if (addr->sa_family == AF_INET) {
4882 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4883 laddr->ifa->address.sin.sin_addr.s_addr) {
4885 if (holds_lock == 0) {
4886 SCTP_INP_RUNLOCK(inp);
4888 return (laddr->ifa);
4893 if (addr->sa_family == AF_INET6) {
4894 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4895 &laddr->ifa->address.sin6)) {
4897 if (holds_lock == 0) {
4898 SCTP_INP_RUNLOCK(inp);
4900 return (laddr->ifa);
4906 if (holds_lock == 0) {
4907 SCTP_INP_RUNLOCK(inp);
4913 sctp_get_ifa_hash_val(struct sockaddr *addr)
4915 if (addr->sa_family == AF_INET) {
4916 struct sockaddr_in *sin;
4918 sin = (struct sockaddr_in *)addr;
4919 return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
4920 } else if (addr->sa_family == AF_INET6) {
4921 struct sockaddr_in6 *sin6;
4922 uint32_t hash_of_addr;
4924 sin6 = (struct sockaddr_in6 *)addr;
4925 hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
4926 sin6->sin6_addr.s6_addr32[1] +
4927 sin6->sin6_addr.s6_addr32[2] +
4928 sin6->sin6_addr.s6_addr32[3]);
4929 hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
4930 return (hash_of_addr);
4936 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
4938 struct sctp_ifa *sctp_ifap;
4939 struct sctp_vrf *vrf;
4940 struct sctp_ifalist *hash_head;
4941 uint32_t hash_of_addr;
4943 if (holds_lock == 0)
4944 SCTP_IPI_ADDR_RLOCK();
4946 vrf = sctp_find_vrf(vrf_id);
4949 if (holds_lock == 0)
4950 SCTP_IPI_ADDR_RUNLOCK();
4953 hash_of_addr = sctp_get_ifa_hash_val(addr);
4955 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
4956 if (hash_head == NULL) {
4957 SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
4958 hash_of_addr, (uint32_t) vrf->vrf_addr_hashmark,
4959 (uint32_t) (hash_of_addr & vrf->vrf_addr_hashmark));
4960 sctp_print_address(addr);
4961 SCTP_PRINTF("No such bucket for address\n");
4962 if (holds_lock == 0)
4963 SCTP_IPI_ADDR_RUNLOCK();
4967 LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
4968 if (sctp_ifap == NULL) {
4970 panic("Huh LIST_FOREACH corrupt");
4973 SCTP_PRINTF("LIST corrupt of sctp_ifap's?\n");
4977 if (addr->sa_family != sctp_ifap->address.sa.sa_family)
4979 if (addr->sa_family == AF_INET) {
4980 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4981 sctp_ifap->address.sin.sin_addr.s_addr) {
4983 if (holds_lock == 0)
4984 SCTP_IPI_ADDR_RUNLOCK();
4990 if (addr->sa_family == AF_INET6) {
4991 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4992 &sctp_ifap->address.sin6)) {
4994 if (holds_lock == 0)
4995 SCTP_IPI_ADDR_RUNLOCK();
5002 if (holds_lock == 0)
5003 SCTP_IPI_ADDR_RUNLOCK();
5008 sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t * freed_so_far, int hold_rlock,
5011 /* User pulled some data, do we need a rwnd update? */
5014 struct socket *so = NULL;
5019 atomic_add_int(&stcb->asoc.refcnt, 1);
5021 if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
5022 SCTP_STATE_SHUTDOWN_RECEIVED |
5023 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5024 /* Pre-check If we are freeing no update */
5027 SCTP_INP_INCR_REF(stcb->sctp_ep);
5028 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5029 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5032 so = stcb->sctp_socket;
5036 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5037 /* Have you have freed enough to look */
5039 /* Yep, its worth a look and the lock overhead */
5041 /* Figure out what the rwnd would be */
5042 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5043 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5044 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5048 if (dif >= rwnd_req) {
5050 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5053 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5055 * One last check before we allow the guy possibly
5056 * to get in. There is a race, where the guy has not
5057 * reached the gate. In that case
5061 SCTP_TCB_LOCK(stcb);
5062 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5063 /* No reports here */
5064 SCTP_TCB_UNLOCK(stcb);
5067 SCTP_STAT_INCR(sctps_wu_sacks_sent);
5068 sctp_send_sack(stcb);
5070 sctp_chunk_output(stcb->sctp_ep, stcb,
5071 SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5072 /* make sure no timer is running */
5073 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5074 SCTP_TCB_UNLOCK(stcb);
5076 /* Update how much we have pending */
5077 stcb->freed_by_sorcv_sincelast = dif;
5080 if (so && r_unlocked && hold_rlock) {
5081 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5083 SCTP_INP_DECR_REF(stcb->sctp_ep);
5085 atomic_add_int(&stcb->asoc.refcnt, -1);
5090 sctp_sorecvmsg(struct socket *so,
5093 struct sockaddr *from,
5096 struct sctp_sndrcvinfo *sinfo,
5100 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5101 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5102 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5103 * On the way out we may send out any combination of:
5104 * MSG_NOTIFICATION MSG_EOR
5107 struct sctp_inpcb *inp = NULL;
5109 int cp_len = 0, error = 0;
5110 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
5111 struct mbuf *m = NULL, *embuf = NULL;
5112 struct sctp_tcb *stcb = NULL;
5113 int wakeup_read_socket = 0;
5114 int freecnt_applied = 0;
5115 int out_flags = 0, in_flags = 0;
5116 int block_allowed = 1;
5117 uint32_t freed_so_far = 0;
5118 uint32_t copied_so_far = 0;
5119 int in_eeor_mode = 0;
5120 int no_rcv_needed = 0;
5121 uint32_t rwnd_req = 0;
5122 int hold_sblock = 0;
5125 uint32_t held_length = 0;
5126 int sockbuf_lock = 0;
5129 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5133 in_flags = *msg_flags;
5134 if (in_flags & MSG_PEEK)
5135 SCTP_STAT_INCR(sctps_read_peeks);
5139 slen = uio->uio_resid;
5141 /* Pull in and set up our int flags */
5142 if (in_flags & MSG_OOB) {
5143 /* Out of band's NOT supported */
5144 return (EOPNOTSUPP);
5146 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5147 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5150 if ((in_flags & (MSG_DONTWAIT
5153 SCTP_SO_IS_NBIO(so)) {
5156 /* setup the endpoint */
5157 inp = (struct sctp_inpcb *)so->so_pcb;
5159 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5162 rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5163 /* Must be at least a MTU's worth */
5164 if (rwnd_req < SCTP_MIN_RWND)
5165 rwnd_req = SCTP_MIN_RWND;
5166 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5167 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5168 sctp_misc_ints(SCTP_SORECV_ENTER,
5169 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
5171 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5172 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5173 rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
5175 error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5178 goto release_unlocked;
5184 if (hold_sblock == 0) {
5185 SOCKBUF_LOCK(&so->so_rcv);
5188 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5189 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5192 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5194 error = so->so_error;
5195 if ((in_flags & MSG_PEEK) == 0)
5199 if (so->so_rcv.sb_cc == 0) {
5200 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5207 if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
5208 /* we need to wait for data */
5209 if ((so->so_rcv.sb_cc == 0) &&
5210 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5211 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5212 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5214 * For active open side clear flags for
5215 * re-use passive open is blocked by
5218 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5220 * You were aborted, passive side
5223 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5226 * You get this once if you are
5229 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5231 * Remove flag if on the
5234 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
5237 so->so_state &= ~(SS_ISCONNECTING |
5238 SS_ISDISCONNECTING |
5242 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5243 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5246 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
5252 error = sbwait(&so->so_rcv);
5257 goto restart_nosblocks;
5258 } else if (so->so_rcv.sb_cc == 0) {
5260 error = so->so_error;
5261 if ((in_flags & MSG_PEEK) == 0)
5264 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5265 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5266 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5268 * For active open side clear flags
5269 * for re-use passive open is
5270 * blocked by connect.
5272 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5274 * You were aborted, passive
5275 * side always hits here
5277 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5280 * You get this once if you
5281 * are active open side
5283 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5289 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
5292 so->so_state &= ~(SS_ISCONNECTING |
5293 SS_ISDISCONNECTING |
5297 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5298 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5301 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
5307 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5308 error = EWOULDBLOCK;
5312 if (hold_sblock == 1) {
5313 SOCKBUF_UNLOCK(&so->so_rcv);
5316 /* we possibly have data we can read */
5317 /* sa_ignore FREED_MEMORY */
5318 control = TAILQ_FIRST(&inp->read_queue);
5319 if (control == NULL) {
5321 * This could be happening since the appender did the
5322 * increment but as not yet did the tailq insert onto the
5325 if (hold_rlock == 0) {
5326 SCTP_INP_READ_LOCK(inp);
5329 control = TAILQ_FIRST(&inp->read_queue);
5330 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5332 panic("Huh, its non zero and nothing on control?");
5334 so->so_rcv.sb_cc = 0;
5336 SCTP_INP_READ_UNLOCK(inp);
5340 if ((control->length == 0) &&
5341 (control->do_not_ref_stcb)) {
5343 * Clean up code for freeing assoc that left behind a
5344 * pdapi.. maybe a peer in EEOR that just closed after
5345 * sending and never indicated a EOR.
5347 if (hold_rlock == 0) {
5349 SCTP_INP_READ_LOCK(inp);
5351 control->held_length = 0;
5352 if (control->data) {
5353 /* Hmm there is data here .. fix */
5357 m_tmp = control->data;
5359 cnt += SCTP_BUF_LEN(m_tmp);
5360 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5361 control->tail_mbuf = m_tmp;
5362 control->end_added = 1;
5364 m_tmp = SCTP_BUF_NEXT(m_tmp);
5366 control->length = cnt;
5369 TAILQ_REMOVE(&inp->read_queue, control, next);
5370 /* Add back any hiddend data */
5371 sctp_free_remote_addr(control->whoFrom);
5372 sctp_free_a_readq(stcb, control);
5376 SCTP_INP_READ_UNLOCK(inp);
5380 if ((control->length == 0) &&
5381 (control->end_added == 1)) {
5383 * Do we also need to check for (control->pdapi_aborted ==
5386 if (hold_rlock == 0) {
5388 SCTP_INP_READ_LOCK(inp);
5390 TAILQ_REMOVE(&inp->read_queue, control, next);
5391 if (control->data) {
5393 panic("control->data not null but control->length == 0");
5395 SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5396 sctp_m_freem(control->data);
5397 control->data = NULL;
5400 if (control->aux_data) {
5401 sctp_m_free(control->aux_data);
5402 control->aux_data = NULL;
5404 sctp_free_remote_addr(control->whoFrom);
5405 sctp_free_a_readq(stcb, control);
5408 SCTP_INP_READ_UNLOCK(inp);
5412 if (control->length == 0) {
5413 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5415 /* find a more suitable one then this */
5416 ctl = TAILQ_NEXT(control, next);
5418 if ((ctl->stcb != control->stcb) && (ctl->length) &&
5420 (ctl->spec_flags & M_NOTIFICATION) ||
5421 ((ctl->do_not_ref_stcb == 0) &&
5422 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
5425 * If we have a different TCB next, and there is data
5426 * present. If we have already taken some (pdapi), OR we can
5427 * ref the tcb and no delivery as started on this stream, we
5428 * take it. Note we allow a notification on a different
5429 * assoc to be delivered..
5433 } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
5435 ((ctl->some_taken) ||
5436 ((ctl->do_not_ref_stcb == 0) &&
5437 ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5438 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
5440 * If we have the same tcb, and there is data present, and we
5441 * have the strm interleave feature present. Then if we have
5442 * taken some (pdapi) or we can refer to tht tcb AND we have
5443 * not started a delivery for this stream, we can take it.
5444 * Note we do NOT allow a notificaiton on the same assoc to
5450 ctl = TAILQ_NEXT(ctl, next);
5454 * if we reach here, not suitable replacement is available
5455 * <or> fragment interleave is NOT on. So stuff the sb_cc
5456 * into the our held count, and its time to sleep again.
5458 held_length = so->so_rcv.sb_cc;
5459 control->held_length = so->so_rcv.sb_cc;
5462 /* Clear the held length since there is something to read */
5463 control->held_length = 0;
5465 SCTP_INP_READ_UNLOCK(inp);
5470 * If we reach here, control has a some data for us to read off.
5471 * Note that stcb COULD be NULL.
5473 control->some_taken++;
5475 SOCKBUF_UNLOCK(&so->so_rcv);
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 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) && filling_sinfo) {
5523 memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
5524 nxt = TAILQ_NEXT(control, next);
5525 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
5526 struct sctp_extrcvinfo *s_extra;
5528 s_extra = (struct sctp_extrcvinfo *)sinfo;
5531 s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5532 if (nxt->sinfo_flags & SCTP_UNORDERED) {
5533 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5535 if (nxt->spec_flags & M_NOTIFICATION) {
5536 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5538 s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id;
5539 s_extra->sreinfo_next_length = nxt->length;
5540 s_extra->sreinfo_next_ppid = nxt->sinfo_ppid;
5541 s_extra->sreinfo_next_stream = nxt->sinfo_stream;
5542 if (nxt->tail_mbuf != NULL) {
5543 if (nxt->end_added) {
5544 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5549 * we explicitly 0 this, since the memcpy
5550 * got some other things beyond the older
5551 * sinfo_ that is on the control's structure
5555 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
5556 s_extra->sreinfo_next_aid = 0;
5557 s_extra->sreinfo_next_length = 0;
5558 s_extra->sreinfo_next_ppid = 0;
5559 s_extra->sreinfo_next_stream = 0;
5563 * update off the real current cum-ack, if we have an stcb.
5565 if ((control->do_not_ref_stcb == 0) && stcb)
5566 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5568 * mask off the high bits, we keep the actual chunk bits in
5571 sinfo->sinfo_flags &= 0x00ff;
5572 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
5573 sinfo->sinfo_flags |= SCTP_UNORDERED;
5576 #ifdef SCTP_ASOCLOG_OF_TSNS
5578 int index, newindex;
5579 struct sctp_pcbtsn_rlog *entry;
5582 index = inp->readlog_index;
5583 newindex = index + 1;
5584 if (newindex >= SCTP_READ_LOG_SIZE) {
5587 } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
5588 entry = &inp->readlog[index];
5589 entry->vtag = control->sinfo_assoc_id;
5590 entry->strm = control->sinfo_stream;
5591 entry->seq = control->sinfo_ssn;
5592 entry->sz = control->length;
5593 entry->flgs = control->sinfo_flags;
5596 if (fromlen && from) {
5597 struct sockaddr *to;
5600 cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sin.sin_len);
5601 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
5602 ((struct sockaddr_in *)from)->sin_port = control->port_from;
5604 /* No AF_INET use AF_INET6 */
5605 cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sin6.sin6_len);
5606 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
5607 ((struct sockaddr_in6 *)from)->sin6_port = control->port_from;
5611 #if defined(INET) && defined(INET6)
5612 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
5613 (to->sa_family == AF_INET) &&
5614 ((size_t)fromlen >= sizeof(struct sockaddr_in6))) {
5615 struct sockaddr_in *sin;
5616 struct sockaddr_in6 sin6;
5618 sin = (struct sockaddr_in *)to;
5619 bzero(&sin6, sizeof(sin6));
5620 sin6.sin6_family = AF_INET6;
5621 sin6.sin6_len = sizeof(struct sockaddr_in6);
5622 sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
5623 bcopy(&sin->sin_addr,
5624 &sin6.sin6_addr.s6_addr32[3],
5625 sizeof(sin6.sin6_addr.s6_addr32[3]));
5626 sin6.sin6_port = sin->sin_port;
5627 memcpy(from, (caddr_t)&sin6, sizeof(sin6));
5632 struct sockaddr_in6 lsa6, *to6;
5634 to6 = (struct sockaddr_in6 *)to;
5635 sctp_recover_scope_mac(to6, (&lsa6));
5639 /* now copy out what data we can */
5641 /* copy out each mbuf in the chain up to length */
5645 /* Move out all we can */
5646 cp_len = (int)uio->uio_resid;
5647 my_len = (int)SCTP_BUF_LEN(m);
5648 if (cp_len > my_len) {
5649 /* not enough in this buf */
5653 SCTP_INP_READ_UNLOCK(inp);
5657 error = uiomove(mtod(m, char *), cp_len, uio);
5659 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5662 if ((control->do_not_ref_stcb == 0) && stcb &&
5663 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5667 /* error we are out of here */
5670 if ((SCTP_BUF_NEXT(m) == NULL) &&
5671 (cp_len >= SCTP_BUF_LEN(m)) &&
5672 ((control->end_added == 0) ||
5673 (control->end_added &&
5674 (TAILQ_NEXT(control, next) == NULL)))
5676 SCTP_INP_READ_LOCK(inp);
5679 if (cp_len == SCTP_BUF_LEN(m)) {
5680 if ((SCTP_BUF_NEXT(m) == NULL) &&
5681 (control->end_added)) {
5682 out_flags |= MSG_EOR;
5683 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5684 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5686 if (control->spec_flags & M_NOTIFICATION) {
5687 out_flags |= MSG_NOTIFICATION;
5689 /* we ate up the mbuf */
5690 if (in_flags & MSG_PEEK) {
5692 m = SCTP_BUF_NEXT(m);
5693 copied_so_far += cp_len;
5695 /* dispose of the mbuf */
5696 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5697 sctp_sblog(&so->so_rcv,
5698 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5700 sctp_sbfree(control, stcb, &so->so_rcv, m);
5701 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5702 sctp_sblog(&so->so_rcv,
5703 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5706 copied_so_far += cp_len;
5707 freed_so_far += cp_len;
5708 freed_so_far += MSIZE;
5709 atomic_subtract_int(&control->length, cp_len);
5710 control->data = sctp_m_free(m);
5713 * been through it all, must hold sb
5714 * lock ok to null tail
5716 if (control->data == NULL) {
5718 if ((control->end_added == 0) ||
5719 (TAILQ_NEXT(control, next) == NULL)) {
5727 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5728 panic("Hmm we don't own the lock?");
5732 control->tail_mbuf = NULL;
5734 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5735 panic("end_added, nothing left and no MSG_EOR");
5741 /* Do we need to trim the mbuf? */
5742 if (control->spec_flags & M_NOTIFICATION) {
5743 out_flags |= MSG_NOTIFICATION;
5745 if ((in_flags & MSG_PEEK) == 0) {
5746 SCTP_BUF_RESV_UF(m, cp_len);
5747 SCTP_BUF_LEN(m) -= cp_len;
5748 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5749 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
5751 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5752 if ((control->do_not_ref_stcb == 0) &&
5754 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5756 copied_so_far += cp_len;
5758 freed_so_far += cp_len;
5759 freed_so_far += MSIZE;
5760 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5761 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5762 SCTP_LOG_SBRESULT, 0);
5764 atomic_subtract_int(&control->length, cp_len);
5766 copied_so_far += cp_len;
5769 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5772 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5773 (control->do_not_ref_stcb == 0) &&
5774 (freed_so_far >= rwnd_req)) {
5775 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5777 } /* end while(m) */
5779 * At this point we have looked at it all and we either have
5780 * a MSG_EOR/or read all the user wants... <OR>
5781 * control->length == 0.
5783 if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5784 /* we are done with this control */
5785 if (control->length == 0) {
5786 if (control->data) {
5788 panic("control->data not null at read eor?");
5790 SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5791 sctp_m_freem(control->data);
5792 control->data = NULL;
5796 if (TAILQ_NEXT(control, next) == NULL) {
5798 * If we don't have a next we need a
5799 * lock, if there is a next
5800 * interrupt is filling ahead of us
5801 * and we don't need a lock to
5802 * remove this guy (which is the
5803 * head of the queue).
5805 if (hold_rlock == 0) {
5806 SCTP_INP_READ_LOCK(inp);
5810 TAILQ_REMOVE(&inp->read_queue, control, next);
5811 /* Add back any hiddend data */
5812 if (control->held_length) {
5814 control->held_length = 0;
5815 wakeup_read_socket = 1;
5817 if (control->aux_data) {
5818 sctp_m_free(control->aux_data);
5819 control->aux_data = NULL;
5821 no_rcv_needed = control->do_not_ref_stcb;
5822 sctp_free_remote_addr(control->whoFrom);
5823 control->data = NULL;
5824 sctp_free_a_readq(stcb, control);
5826 if ((freed_so_far >= rwnd_req) &&
5827 (no_rcv_needed == 0))
5828 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5832 * The user did not read all of this
5833 * message, turn off the returned MSG_EOR
5834 * since we are leaving more behind on the
5838 if (control->end_added &&
5839 (control->data == NULL) &&
5840 (control->tail_mbuf == NULL)) {
5841 panic("Gak, control->length is corrupt?");
5844 no_rcv_needed = control->do_not_ref_stcb;
5845 out_flags &= ~MSG_EOR;
5848 if (out_flags & MSG_EOR) {
5851 if ((uio->uio_resid == 0) ||
5852 ((in_eeor_mode) && (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))
5857 * If I hit here the receiver wants more and this message is
5858 * NOT done (pd-api). So two questions. Can we block? if not
5859 * we are done. Did the user NOT set MSG_WAITALL?
5861 if (block_allowed == 0) {
5865 * We need to wait for more data a few things: - We don't
5866 * sbunlock() so we don't get someone else reading. - We
5867 * must be sure to account for the case where what is added
5868 * is NOT to our control when we wakeup.
5872 * Do we need to tell the transport a rwnd update might be
5873 * needed before we go to sleep?
5875 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5876 ((freed_so_far >= rwnd_req) &&
5877 (control->do_not_ref_stcb == 0) &&
5878 (no_rcv_needed == 0))) {
5879 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5882 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5885 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5888 if (hold_rlock == 1) {
5889 SCTP_INP_READ_UNLOCK(inp);
5892 if (hold_sblock == 0) {
5893 SOCKBUF_LOCK(&so->so_rcv);
5896 if ((copied_so_far) && (control->length == 0) &&
5897 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
5900 if (so->so_rcv.sb_cc <= control->held_length) {
5901 error = sbwait(&so->so_rcv);
5905 control->held_length = 0;
5908 SOCKBUF_UNLOCK(&so->so_rcv);
5911 if (control->length == 0) {
5912 /* still nothing here */
5913 if (control->end_added == 1) {
5914 /* he aborted, or is done i.e.did a shutdown */
5915 out_flags |= MSG_EOR;
5916 if (control->pdapi_aborted) {
5917 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5918 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5920 out_flags |= MSG_TRUNC;
5922 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5923 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5925 goto done_with_control;
5927 if (so->so_rcv.sb_cc > held_length) {
5928 control->held_length = so->so_rcv.sb_cc;
5931 goto wait_some_more;
5932 } else if (control->data == NULL) {
5934 * we must re-sync since data is probably being
5937 SCTP_INP_READ_LOCK(inp);
5938 if ((control->length > 0) && (control->data == NULL)) {
5940 * big trouble.. we have the lock and its
5944 panic("Impossible data==NULL length !=0");
5946 out_flags |= MSG_EOR;
5947 out_flags |= MSG_TRUNC;
5948 control->length = 0;
5949 SCTP_INP_READ_UNLOCK(inp);
5950 goto done_with_control;
5952 SCTP_INP_READ_UNLOCK(inp);
5953 /* We will fall around to get more data */
5958 * Give caller back the mbuf chain,
5959 * store in uio_resid the length
5961 wakeup_read_socket = 0;
5962 if ((control->end_added == 0) ||
5963 (TAILQ_NEXT(control, next) == NULL)) {
5964 /* Need to get rlock */
5965 if (hold_rlock == 0) {
5966 SCTP_INP_READ_LOCK(inp);
5970 if (control->end_added) {
5971 out_flags |= MSG_EOR;
5972 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5973 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5975 if (control->spec_flags & M_NOTIFICATION) {
5976 out_flags |= MSG_NOTIFICATION;
5978 uio->uio_resid = control->length;
5979 *mp = control->data;
5982 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5983 sctp_sblog(&so->so_rcv,
5984 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5986 sctp_sbfree(control, stcb, &so->so_rcv, m);
5987 freed_so_far += SCTP_BUF_LEN(m);
5988 freed_so_far += MSIZE;
5989 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5990 sctp_sblog(&so->so_rcv,
5991 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5993 m = SCTP_BUF_NEXT(m);
5995 control->data = control->tail_mbuf = NULL;
5996 control->length = 0;
5997 if (out_flags & MSG_EOR) {
5998 /* Done with this control */
5999 goto done_with_control;
6003 if (hold_rlock == 1) {
6004 SCTP_INP_READ_UNLOCK(inp);
6007 if (hold_sblock == 1) {
6008 SOCKBUF_UNLOCK(&so->so_rcv);
6011 sbunlock(&so->so_rcv);
6016 SOCKBUF_UNLOCK(&so->so_rcv);
6019 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6020 if ((freed_so_far >= rwnd_req) &&
6021 (control && (control->do_not_ref_stcb == 0)) &&
6022 (no_rcv_needed == 0))
6023 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6027 *msg_flags = out_flags;
6029 if (((out_flags & MSG_EOR) == 0) &&
6030 ((in_flags & MSG_PEEK) == 0) &&
6032 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO))) {
6033 struct sctp_extrcvinfo *s_extra;
6035 s_extra = (struct sctp_extrcvinfo *)sinfo;
6036 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
6038 if (hold_rlock == 1) {
6039 SCTP_INP_READ_UNLOCK(inp);
6043 SOCKBUF_UNLOCK(&so->so_rcv);
6047 sbunlock(&so->so_rcv);
6049 if (freecnt_applied) {
6051 * The lock on the socket buffer protects us so the free
6052 * code will stop. But since we used the socketbuf lock and
6053 * the sender uses the tcb_lock to increment, we need to use
6054 * the atomic add to the refcnt.
6058 panic("stcb for refcnt has gone NULL?");
6064 atomic_add_int(&stcb->asoc.refcnt, -1);
6065 freecnt_applied = 0;
6066 /* Save the value back for next time */
6067 stcb->freed_by_sorcv_sincelast = freed_so_far;
6069 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6071 sctp_misc_ints(SCTP_SORECV_DONE,
6073 ((uio) ? (slen - uio->uio_resid) : slen),
6077 sctp_misc_ints(SCTP_SORECV_DONE,
6079 ((uio) ? (slen - uio->uio_resid) : slen),
6085 if (wakeup_read_socket) {
6086 sctp_sorwakeup(inp, so);
6092 #ifdef SCTP_MBUF_LOGGING
6094 sctp_m_free(struct mbuf *m)
6096 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6097 if (SCTP_BUF_IS_EXTENDED(m)) {
6098 sctp_log_mb(m, SCTP_MBUF_IFREE);
6105 sctp_m_freem(struct mbuf *mb)
6108 mb = sctp_m_free(mb);
6114 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
6117 * Given a local address. For all associations that holds the
6118 * address, request a peer-set-primary.
6120 struct sctp_ifa *ifa;
6121 struct sctp_laddr *wi;
6123 ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
6125 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
6126 return (EADDRNOTAVAIL);
6129 * Now that we have the ifa we must awaken the iterator with this
6132 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6134 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
6137 /* Now incr the count and int wi structure */
6138 SCTP_INCR_LADDR_COUNT();
6139 bzero(wi, sizeof(*wi));
6140 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
6142 wi->action = SCTP_SET_PRIM_ADDR;
6143 atomic_add_int(&ifa->refcount, 1);
6145 /* Now add it to the work queue */
6146 SCTP_WQ_ADDR_LOCK();
6148 * Should this really be a tailq? As it is we will process the
6151 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6152 SCTP_WQ_ADDR_UNLOCK();
6153 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
6154 (struct sctp_inpcb *)NULL,
6155 (struct sctp_tcb *)NULL,
6156 (struct sctp_nets *)NULL);
6162 sctp_soreceive(struct socket *so,
6163 struct sockaddr **psa,
6166 struct mbuf **controlp,
6170 uint8_t sockbuf[256];
6171 struct sockaddr *from;
6172 struct sctp_extrcvinfo sinfo;
6173 int filling_sinfo = 1;
6174 struct sctp_inpcb *inp;
6176 inp = (struct sctp_inpcb *)so->so_pcb;
6177 /* pickup the assoc we are reading from */
6179 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6182 if ((sctp_is_feature_off(inp,
6183 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
6184 (controlp == NULL)) {
6185 /* user does not want the sndrcv ctl */
6189 from = (struct sockaddr *)sockbuf;
6190 fromlen = sizeof(sockbuf);
6197 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
6198 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6199 if ((controlp) && (filling_sinfo)) {
6200 /* copy back the sinfo in a CMSG format */
6202 *controlp = sctp_build_ctl_nchunk(inp,
6203 (struct sctp_sndrcvinfo *)&sinfo);
6208 /* copy back the address info */
6209 if (from && from->sa_len) {
6210 *psa = sodupsockaddr(from, M_NOWAIT);
6220 sctp_l_soreceive(struct socket *so,
6221 struct sockaddr **name,
6228 uint8_t sockbuf[256];
6229 struct sockaddr *from;
6230 struct sctp_extrcvinfo sinfo;
6231 int filling_sinfo = 1;
6232 struct sctp_inpcb *inp;
6234 inp = (struct sctp_inpcb *)so->so_pcb;
6235 /* pickup the assoc we are reading from */
6237 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6240 if ((sctp_is_feature_off(inp,
6241 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
6242 (controlp == NULL)) {
6243 /* user does not want the sndrcv ctl */
6247 from = (struct sockaddr *)sockbuf;
6248 fromlen = sizeof(sockbuf);
6255 error = sctp_sorecvmsg(so, uio,
6256 (struct mbuf **)NULL,
6257 from, fromlen, flag,
6258 (struct sctp_sndrcvinfo *)&sinfo,
6260 if ((controlp) && (filling_sinfo)) {
6262 * copy back the sinfo in a CMSG format note that the caller
6263 * has reponsibility for freeing the memory.
6266 *controlp = sctp_build_ctl_cchunk(inp,
6268 (struct sctp_sndrcvinfo *)&sinfo);
6271 /* copy back the address info */
6272 if (from && from->sa_len) {
6273 *name = sodupsockaddr(from, M_WAIT);
6288 sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6289 int totaddr, int *error)
6293 struct sctp_inpcb *inp;
6294 struct sockaddr *sa;
6298 inp = stcb->sctp_ep;
6300 for (i = 0; i < totaddr; i++) {
6301 if (sa->sa_family == AF_INET) {
6302 incr = sizeof(struct sockaddr_in);
6303 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
6304 /* assoc gone no un-lock */
6305 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6306 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
6311 } else if (sa->sa_family == AF_INET6) {
6312 incr = sizeof(struct sockaddr_in6);
6313 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
6314 /* assoc gone no un-lock */
6315 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6316 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
6322 sa = (struct sockaddr *)((caddr_t)sa + incr);
6329 sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6330 int *totaddr, int *num_v4, int *num_v6, int *error,
6331 int limit, int *bad_addr)
6333 struct sockaddr *sa;
6334 struct sctp_tcb *stcb = NULL;
6339 *error = *num_v6 = *num_v4 = 0;
6340 /* account and validate addresses */
6341 for (i = 0; i < (size_t)*totaddr; i++) {
6342 if (sa->sa_family == AF_INET) {
6344 incr = sizeof(struct sockaddr_in);
6345 if (sa->sa_len != incr) {
6346 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6351 } else if (sa->sa_family == AF_INET6) {
6352 struct sockaddr_in6 *sin6;
6354 sin6 = (struct sockaddr_in6 *)sa;
6355 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6356 /* Must be non-mapped for connectx */
6357 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6363 incr = sizeof(struct sockaddr_in6);
6364 if (sa->sa_len != incr) {
6365 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6375 SCTP_INP_INCR_REF(inp);
6376 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
6378 /* Already have or am bring up an association */
6381 SCTP_INP_DECR_REF(inp);
6383 if ((at + incr) > (size_t)limit) {
6387 sa = (struct sockaddr *)((caddr_t)sa + incr);
6389 return ((struct sctp_tcb *)NULL);
6393 * sctp_bindx(ADD) for one address.
6394 * assumes all arguments are valid/checked by caller.
6397 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
6398 struct sockaddr *sa, sctp_assoc_t assoc_id,
6399 uint32_t vrf_id, int *error, void *p)
6401 struct sockaddr *addr_touse;
6404 struct sockaddr_in sin;
6408 /* see if we're bound all already! */
6409 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6410 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6415 #if defined(INET6) && !defined(__Userspace__) /* TODO port in6_sin6_2_sin */
6416 if (sa->sa_family == AF_INET6) {
6417 struct sockaddr_in6 *sin6;
6419 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6420 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6424 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6425 /* can only bind v6 on PF_INET6 sockets */
6426 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6430 sin6 = (struct sockaddr_in6 *)addr_touse;
6431 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6432 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6433 SCTP_IPV6_V6ONLY(inp)) {
6434 /* can't bind v4-mapped on PF_INET sockets */
6435 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6439 in6_sin6_2_sin(&sin, sin6);
6440 addr_touse = (struct sockaddr *)&sin;
6444 if (sa->sa_family == AF_INET) {
6445 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6446 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6450 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6451 SCTP_IPV6_V6ONLY(inp)) {
6452 /* can't bind v4 on PF_INET sockets */
6453 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6458 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
6460 /* Can't get proc for Net/Open BSD */
6461 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6465 *error = sctp_inpcb_bind(so, addr_touse, NULL, p);
6469 * No locks required here since bind and mgmt_ep_sa all do their own
6470 * locking. If we do something for the FIX: below we may need to
6471 * lock in that case.
6473 if (assoc_id == 0) {
6474 /* add the address */
6475 struct sctp_inpcb *lep;
6476 struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
6478 /* validate the incoming port */
6479 if ((lsin->sin_port != 0) &&
6480 (lsin->sin_port != inp->sctp_lport)) {
6481 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6485 /* user specified 0 port, set it to existing port */
6486 lsin->sin_port = inp->sctp_lport;
6489 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
6492 * We must decrement the refcount since we have the
6493 * ep already and are binding. No remove going on
6496 SCTP_INP_DECR_REF(lep);
6499 /* already bound to it.. ok */
6501 } else if (lep == NULL) {
6502 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
6503 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6504 SCTP_ADD_IP_ADDRESS,
6507 *error = EADDRINUSE;
6513 * FIX: decide whether we allow assoc based bindx
6519 * sctp_bindx(DELETE) for one address.
6520 * assumes all arguments are valid/checked by caller.
6523 sctp_bindx_delete_address(struct socket *so, struct sctp_inpcb *inp,
6524 struct sockaddr *sa, sctp_assoc_t assoc_id,
6525 uint32_t vrf_id, int *error)
6527 struct sockaddr *addr_touse;
6530 struct sockaddr_in sin;
6534 /* see if we're bound all already! */
6535 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6536 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6541 #if defined(INET6) && !defined(__Userspace__) /* TODO port in6_sin6_2_sin */
6542 if (sa->sa_family == AF_INET6) {
6543 struct sockaddr_in6 *sin6;
6545 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6546 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6550 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6551 /* can only bind v6 on PF_INET6 sockets */
6552 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6556 sin6 = (struct sockaddr_in6 *)addr_touse;
6557 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6558 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6559 SCTP_IPV6_V6ONLY(inp)) {
6560 /* can't bind mapped-v4 on PF_INET sockets */
6561 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6565 in6_sin6_2_sin(&sin, sin6);
6566 addr_touse = (struct sockaddr *)&sin;
6570 if (sa->sa_family == AF_INET) {
6571 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6572 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6576 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6577 SCTP_IPV6_V6ONLY(inp)) {
6578 /* can't bind v4 on PF_INET sockets */
6579 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6585 * No lock required mgmt_ep_sa does its own locking. If the FIX:
6586 * below is ever changed we may need to lock before calling
6587 * association level binding.
6589 if (assoc_id == 0) {
6590 /* delete the address */
6591 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6592 SCTP_DEL_IP_ADDRESS,
6596 * FIX: decide whether we allow assoc based bindx
6602 * returns the valid local address count for an assoc, taking into account
6606 sctp_local_addr_count(struct sctp_tcb *stcb)
6608 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
6609 int ipv4_addr_legal, ipv6_addr_legal;
6610 struct sctp_vrf *vrf;
6611 struct sctp_ifn *sctp_ifn;
6612 struct sctp_ifa *sctp_ifa;
6615 /* Turn on all the appropriate scopes */
6616 loopback_scope = stcb->asoc.loopback_scope;
6617 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
6618 local_scope = stcb->asoc.local_scope;
6619 site_scope = stcb->asoc.site_scope;
6620 ipv4_addr_legal = ipv6_addr_legal = 0;
6621 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6622 ipv6_addr_legal = 1;
6623 if (SCTP_IPV6_V6ONLY(stcb->sctp_ep) == 0) {
6624 ipv4_addr_legal = 1;
6627 ipv4_addr_legal = 1;
6630 SCTP_IPI_ADDR_RLOCK();
6631 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
6633 /* no vrf, no addresses */
6634 SCTP_IPI_ADDR_RUNLOCK();
6637 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6639 * bound all case: go through all ifns on the vrf
6641 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
6642 if ((loopback_scope == 0) &&
6643 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
6646 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
6647 if (sctp_is_addr_restricted(stcb, sctp_ifa))
6649 switch (sctp_ifa->address.sa.sa_family) {
6651 if (ipv4_addr_legal) {
6652 struct sockaddr_in *sin;
6654 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
6655 if (sin->sin_addr.s_addr == 0) {
6662 if ((ipv4_local_scope == 0) &&
6663 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
6666 /* count this one */
6674 if (ipv6_addr_legal) {
6675 struct sockaddr_in6 *sin6;
6677 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
6678 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6681 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
6682 if (local_scope == 0)
6684 if (sin6->sin6_scope_id == 0) {
6685 if (sa6_recoverscope(sin6) != 0)
6703 if ((site_scope == 0) &&
6704 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
6707 /* count this one */
6722 struct sctp_laddr *laddr;
6724 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
6726 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
6729 /* count this one */
6733 SCTP_IPI_ADDR_RUNLOCK();
6737 #if defined(SCTP_LOCAL_TRACE_BUF)
6740 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)
6742 uint32_t saveindex, newindex;
6745 saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6746 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6749 newindex = saveindex + 1;
6751 } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6752 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6755 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6756 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6757 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6758 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6759 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6760 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6761 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6762 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6766 /* We will need to add support
6767 * to bind the ports and such here
6768 * so we can do UDP tunneling. In
6769 * the mean-time, we return error
6771 #include <netinet/udp.h>
6772 #include <netinet/udp_var.h>
6773 #include <sys/proc.h>
6775 #include <netinet6/sctp6_var.h>
6779 sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *ignored)
6782 struct mbuf *sp, *last;
6783 struct udphdr *uhdr;
6784 uint16_t port = 0, len;
6785 int header_size = sizeof(struct udphdr) + sizeof(struct sctphdr);
6788 * Split out the mbuf chain. Leave the IP header in m, place the
6791 if ((m->m_flags & M_PKTHDR) == 0) {
6792 /* Can't handle one that is not a pkt hdr */
6795 /* pull the src port */
6796 iph = mtod(m, struct ip *);
6797 uhdr = (struct udphdr *)((caddr_t)iph + off);
6799 port = uhdr->uh_sport;
6800 sp = m_split(m, off, M_DONTWAIT);
6802 /* Gak, drop packet, we can't do a split */
6805 if (sp->m_pkthdr.len < header_size) {
6806 /* Gak, packet can't have an SCTP header in it - to small */
6810 /* ok now pull up the UDP header and SCTP header together */
6811 sp = m_pullup(sp, header_size);
6813 /* Gak pullup failed */
6816 /* trim out the UDP header */
6817 m_adj(sp, sizeof(struct udphdr));
6819 /* Now reconstruct the mbuf chain */
6820 /* 1) find last one */
6822 while (last->m_next != NULL) {
6823 last = last->m_next;
6826 m->m_pkthdr.len += sp->m_pkthdr.len;
6828 while (last != NULL) {
6829 last = last->m_next;
6831 /* Now its ready for sctp_input or sctp6_input */
6832 iph = mtod(m, struct ip *);
6833 switch (iph->ip_v) {
6837 len = SCTP_GET_IPV4_LENGTH(iph);
6838 len -= sizeof(struct udphdr);
6839 SCTP_GET_IPV4_LENGTH(iph) = len;
6840 sctp_input_with_port(m, off, port);
6844 case IPV6_VERSION >> 4:
6846 /* its IPv6 - NOT supported */
6864 sctp_over_udp_stop(void)
6869 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6872 if (SCTP_BASE_INFO(udp_tun_socket) == NULL) {
6876 sop = SCTP_BASE_INFO(udp_tun_socket);
6878 SCTP_BASE_INFO(udp_tun_socket) = NULL;
6881 sctp_over_udp_start(void)
6885 struct sockaddr_in sin;
6886 struct socket *sop = NULL;
6891 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6894 port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
6896 /* Must have a port set */
6899 if (SCTP_BASE_INFO(udp_tun_socket) != NULL) {
6900 /* Already running -- must stop first */
6904 cred = th->td_ucred;
6905 if ((ret = socreate(PF_INET, &sop,
6906 SOCK_DGRAM, IPPROTO_UDP, cred, th))) {
6909 SCTP_BASE_INFO(udp_tun_socket) = sop;
6910 /* call the special UDP hook */
6911 ret = udp_set_kernel_tunneling(sop, sctp_recv_udp_tunneled_packet);
6913 goto exit_stage_left;
6915 /* Ok we have a socket, bind it to the port */
6916 memset(&sin, 0, sizeof(sin));
6917 sin.sin_len = sizeof(sin);
6918 sin.sin_family = AF_INET;
6919 sin.sin_port = htons(port);
6920 ret = sobind(sop, (struct sockaddr *)&sin, th);
6922 /* Close up we cant get the port */
6924 sctp_over_udp_stop();
6928 * Ok we should now get UDP packets directly to our input routine
6929 * sctp_recv_upd_tunneled_packet().