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>
52 #define NUMBER_OF_MTU_SIZES 18
55 #if defined(__Windows__) && !defined(SCTP_LOCAL_TRACE_BUF)
56 #include "eventrace_netinet.h"
57 #include "sctputil.tmh" /* this is the file that will be auto
61 #define KTR_SCTP KTR_SUBSYS
66 sctp_sblog(struct sockbuf *sb,
67 struct sctp_tcb *stcb, int from, int incr)
69 struct sctp_cwnd_log sctp_clog;
71 sctp_clog.x.sb.stcb = stcb;
72 sctp_clog.x.sb.so_sbcc = sb->sb_cc;
74 sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
76 sctp_clog.x.sb.stcb_sbcc = 0;
77 sctp_clog.x.sb.incr = incr;
78 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
81 sctp_clog.x.misc.log1,
82 sctp_clog.x.misc.log2,
83 sctp_clog.x.misc.log3,
84 sctp_clog.x.misc.log4);
88 sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
90 struct sctp_cwnd_log sctp_clog;
92 sctp_clog.x.close.inp = (void *)inp;
93 sctp_clog.x.close.sctp_flags = inp->sctp_flags;
95 sctp_clog.x.close.stcb = (void *)stcb;
96 sctp_clog.x.close.state = (uint16_t) stcb->asoc.state;
98 sctp_clog.x.close.stcb = 0;
99 sctp_clog.x.close.state = 0;
101 sctp_clog.x.close.loc = loc;
102 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
103 SCTP_LOG_EVENT_CLOSE,
105 sctp_clog.x.misc.log1,
106 sctp_clog.x.misc.log2,
107 sctp_clog.x.misc.log3,
108 sctp_clog.x.misc.log4);
113 rto_logging(struct sctp_nets *net, int from)
115 struct sctp_cwnd_log sctp_clog;
117 memset(&sctp_clog, 0, sizeof(sctp_clog));
118 sctp_clog.x.rto.net = (void *)net;
119 sctp_clog.x.rto.rtt = net->prev_rtt;
120 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
123 sctp_clog.x.misc.log1,
124 sctp_clog.x.misc.log2,
125 sctp_clog.x.misc.log3,
126 sctp_clog.x.misc.log4);
131 sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
133 struct sctp_cwnd_log sctp_clog;
135 sctp_clog.x.strlog.stcb = stcb;
136 sctp_clog.x.strlog.n_tsn = tsn;
137 sctp_clog.x.strlog.n_sseq = sseq;
138 sctp_clog.x.strlog.e_tsn = 0;
139 sctp_clog.x.strlog.e_sseq = 0;
140 sctp_clog.x.strlog.strm = stream;
141 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
144 sctp_clog.x.misc.log1,
145 sctp_clog.x.misc.log2,
146 sctp_clog.x.misc.log3,
147 sctp_clog.x.misc.log4);
152 sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
154 struct sctp_cwnd_log sctp_clog;
156 sctp_clog.x.nagle.stcb = (void *)stcb;
157 sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
158 sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
159 sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
160 sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
161 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
162 SCTP_LOG_EVENT_NAGLE,
164 sctp_clog.x.misc.log1,
165 sctp_clog.x.misc.log2,
166 sctp_clog.x.misc.log3,
167 sctp_clog.x.misc.log4);
172 sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
174 struct sctp_cwnd_log sctp_clog;
176 sctp_clog.x.sack.cumack = cumack;
177 sctp_clog.x.sack.oldcumack = old_cumack;
178 sctp_clog.x.sack.tsn = tsn;
179 sctp_clog.x.sack.numGaps = gaps;
180 sctp_clog.x.sack.numDups = dups;
181 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
184 sctp_clog.x.misc.log1,
185 sctp_clog.x.misc.log2,
186 sctp_clog.x.misc.log3,
187 sctp_clog.x.misc.log4);
191 sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
193 struct sctp_cwnd_log sctp_clog;
195 memset(&sctp_clog, 0, sizeof(sctp_clog));
196 sctp_clog.x.map.base = map;
197 sctp_clog.x.map.cum = cum;
198 sctp_clog.x.map.high = high;
199 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
202 sctp_clog.x.misc.log1,
203 sctp_clog.x.misc.log2,
204 sctp_clog.x.misc.log3,
205 sctp_clog.x.misc.log4);
209 sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn,
212 struct sctp_cwnd_log sctp_clog;
214 memset(&sctp_clog, 0, sizeof(sctp_clog));
215 sctp_clog.x.fr.largest_tsn = biggest_tsn;
216 sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
217 sctp_clog.x.fr.tsn = tsn;
218 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
221 sctp_clog.x.misc.log1,
222 sctp_clog.x.misc.log2,
223 sctp_clog.x.misc.log3,
224 sctp_clog.x.misc.log4);
230 sctp_log_mb(struct mbuf *m, int from)
232 struct sctp_cwnd_log sctp_clog;
234 sctp_clog.x.mb.mp = m;
235 sctp_clog.x.mb.mbuf_flags = (uint8_t) (SCTP_BUF_GET_FLAGS(m));
236 sctp_clog.x.mb.size = (uint16_t) (SCTP_BUF_LEN(m));
237 sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
238 if (SCTP_BUF_IS_EXTENDED(m)) {
239 sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
240 sctp_clog.x.mb.refcnt = (uint8_t) (SCTP_BUF_EXTEND_REFCNT(m));
242 sctp_clog.x.mb.ext = 0;
243 sctp_clog.x.mb.refcnt = 0;
245 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
248 sctp_clog.x.misc.log1,
249 sctp_clog.x.misc.log2,
250 sctp_clog.x.misc.log3,
251 sctp_clog.x.misc.log4);
256 sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk,
259 struct sctp_cwnd_log sctp_clog;
261 if (control == NULL) {
262 SCTP_PRINTF("Gak log of NULL?\n");
265 sctp_clog.x.strlog.stcb = control->stcb;
266 sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
267 sctp_clog.x.strlog.n_sseq = control->sinfo_ssn;
268 sctp_clog.x.strlog.strm = control->sinfo_stream;
269 if (poschk != NULL) {
270 sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
271 sctp_clog.x.strlog.e_sseq = poschk->sinfo_ssn;
273 sctp_clog.x.strlog.e_tsn = 0;
274 sctp_clog.x.strlog.e_sseq = 0;
276 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
279 sctp_clog.x.misc.log1,
280 sctp_clog.x.misc.log2,
281 sctp_clog.x.misc.log3,
282 sctp_clog.x.misc.log4);
287 sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
289 struct sctp_cwnd_log sctp_clog;
291 sctp_clog.x.cwnd.net = net;
292 if (stcb->asoc.send_queue_cnt > 255)
293 sctp_clog.x.cwnd.cnt_in_send = 255;
295 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
296 if (stcb->asoc.stream_queue_cnt > 255)
297 sctp_clog.x.cwnd.cnt_in_str = 255;
299 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
302 sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
303 sctp_clog.x.cwnd.inflight = net->flight_size;
304 sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
305 sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
306 sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
308 if (SCTP_CWNDLOG_PRESEND == from) {
309 sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
311 sctp_clog.x.cwnd.cwnd_augment = augment;
312 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
315 sctp_clog.x.misc.log1,
316 sctp_clog.x.misc.log2,
317 sctp_clog.x.misc.log3,
318 sctp_clog.x.misc.log4);
323 sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
325 struct sctp_cwnd_log sctp_clog;
327 memset(&sctp_clog, 0, sizeof(sctp_clog));
329 sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
332 sctp_clog.x.lock.sock = (void *)NULL;
334 sctp_clog.x.lock.inp = (void *)inp;
336 sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
338 sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
341 sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
342 sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
344 sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
345 sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
347 sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
348 if (inp->sctp_socket) {
349 sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
350 sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
351 sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
353 sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
354 sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
355 sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
357 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
360 sctp_clog.x.misc.log1,
361 sctp_clog.x.misc.log2,
362 sctp_clog.x.misc.log3,
363 sctp_clog.x.misc.log4);
368 sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
370 struct sctp_cwnd_log sctp_clog;
372 memset(&sctp_clog, 0, sizeof(sctp_clog));
373 sctp_clog.x.cwnd.net = net;
374 sctp_clog.x.cwnd.cwnd_new_value = error;
375 sctp_clog.x.cwnd.inflight = net->flight_size;
376 sctp_clog.x.cwnd.cwnd_augment = burst;
377 if (stcb->asoc.send_queue_cnt > 255)
378 sctp_clog.x.cwnd.cnt_in_send = 255;
380 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
381 if (stcb->asoc.stream_queue_cnt > 255)
382 sctp_clog.x.cwnd.cnt_in_str = 255;
384 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
385 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
386 SCTP_LOG_EVENT_MAXBURST,
388 sctp_clog.x.misc.log1,
389 sctp_clog.x.misc.log2,
390 sctp_clog.x.misc.log3,
391 sctp_clog.x.misc.log4);
396 sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
398 struct sctp_cwnd_log sctp_clog;
400 sctp_clog.x.rwnd.rwnd = peers_rwnd;
401 sctp_clog.x.rwnd.send_size = snd_size;
402 sctp_clog.x.rwnd.overhead = overhead;
403 sctp_clog.x.rwnd.new_rwnd = 0;
404 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
407 sctp_clog.x.misc.log1,
408 sctp_clog.x.misc.log2,
409 sctp_clog.x.misc.log3,
410 sctp_clog.x.misc.log4);
414 sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
416 struct sctp_cwnd_log sctp_clog;
418 sctp_clog.x.rwnd.rwnd = peers_rwnd;
419 sctp_clog.x.rwnd.send_size = flight_size;
420 sctp_clog.x.rwnd.overhead = overhead;
421 sctp_clog.x.rwnd.new_rwnd = a_rwndval;
422 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
425 sctp_clog.x.misc.log1,
426 sctp_clog.x.misc.log2,
427 sctp_clog.x.misc.log3,
428 sctp_clog.x.misc.log4);
432 sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
434 struct sctp_cwnd_log sctp_clog;
436 sctp_clog.x.mbcnt.total_queue_size = total_oq;
437 sctp_clog.x.mbcnt.size_change = book;
438 sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
439 sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
440 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
441 SCTP_LOG_EVENT_MBCNT,
443 sctp_clog.x.misc.log1,
444 sctp_clog.x.misc.log2,
445 sctp_clog.x.misc.log3,
446 sctp_clog.x.misc.log4);
451 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
453 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
460 sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t cumtsn, uint32_t wake_cnt, int from)
462 struct sctp_cwnd_log sctp_clog;
464 sctp_clog.x.wake.stcb = (void *)stcb;
465 sctp_clog.x.wake.wake_cnt = wake_cnt;
466 sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
467 sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
468 sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
470 if (stcb->asoc.stream_queue_cnt < 0xff)
471 sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
473 sctp_clog.x.wake.stream_qcnt = 0xff;
475 if (stcb->asoc.chunks_on_out_queue < 0xff)
476 sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
478 sctp_clog.x.wake.chunks_on_oque = 0xff;
480 sctp_clog.x.wake.sctpflags = 0;
481 /* set in the defered mode stuff */
482 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
483 sctp_clog.x.wake.sctpflags |= 1;
484 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
485 sctp_clog.x.wake.sctpflags |= 2;
486 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
487 sctp_clog.x.wake.sctpflags |= 4;
488 /* what about the sb */
489 if (stcb->sctp_socket) {
490 struct socket *so = stcb->sctp_socket;
492 sctp_clog.x.wake.sbflags = (uint8_t) ((so->so_snd.sb_flags & 0x00ff));
494 sctp_clog.x.wake.sbflags = 0xff;
496 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
499 sctp_clog.x.misc.log1,
500 sctp_clog.x.misc.log2,
501 sctp_clog.x.misc.log3,
502 sctp_clog.x.misc.log4);
507 sctp_log_block(uint8_t from, struct socket *so, struct sctp_association *asoc, int sendlen)
509 struct sctp_cwnd_log sctp_clog;
511 sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
512 sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
513 sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
514 sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
515 sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
516 sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight / 1024);
517 sctp_clog.x.blk.sndlen = sendlen;
518 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
519 SCTP_LOG_EVENT_BLOCK,
521 sctp_clog.x.misc.log1,
522 sctp_clog.x.misc.log2,
523 sctp_clog.x.misc.log3,
524 sctp_clog.x.misc.log4);
529 sctp_fill_stat_log(void *optval, size_t *optsize)
531 /* May need to fix this if ktrdump does not work */
535 #ifdef SCTP_AUDITING_ENABLED
536 uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
537 static int sctp_audit_indx = 0;
541 sctp_print_audit_report(void)
547 for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
548 if ((sctp_audit_data[i][0] == 0xe0) &&
549 (sctp_audit_data[i][1] == 0x01)) {
552 } else if (sctp_audit_data[i][0] == 0xf0) {
555 } else if ((sctp_audit_data[i][0] == 0xc0) &&
556 (sctp_audit_data[i][1] == 0x01)) {
560 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
561 (uint32_t) sctp_audit_data[i][1]);
566 for (i = 0; i < sctp_audit_indx; i++) {
567 if ((sctp_audit_data[i][0] == 0xe0) &&
568 (sctp_audit_data[i][1] == 0x01)) {
571 } else if (sctp_audit_data[i][0] == 0xf0) {
574 } else if ((sctp_audit_data[i][0] == 0xc0) &&
575 (sctp_audit_data[i][1] == 0x01)) {
579 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
580 (uint32_t) sctp_audit_data[i][1]);
589 sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
590 struct sctp_nets *net)
592 int resend_cnt, tot_out, rep, tot_book_cnt;
593 struct sctp_nets *lnet;
594 struct sctp_tmit_chunk *chk;
596 sctp_audit_data[sctp_audit_indx][0] = 0xAA;
597 sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
599 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
603 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
604 sctp_audit_data[sctp_audit_indx][1] = 0x01;
606 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
612 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
613 sctp_audit_data[sctp_audit_indx][1] = 0x02;
615 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
620 sctp_audit_data[sctp_audit_indx][0] = 0xA1;
621 sctp_audit_data[sctp_audit_indx][1] =
622 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
624 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
629 resend_cnt = tot_out = 0;
630 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
631 if (chk->sent == SCTP_DATAGRAM_RESEND) {
633 } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
634 tot_out += chk->book_size;
638 if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
639 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
640 sctp_audit_data[sctp_audit_indx][1] = 0xA1;
642 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
645 SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
646 resend_cnt, stcb->asoc.sent_queue_retran_cnt);
648 stcb->asoc.sent_queue_retran_cnt = resend_cnt;
649 sctp_audit_data[sctp_audit_indx][0] = 0xA2;
650 sctp_audit_data[sctp_audit_indx][1] =
651 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
653 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
657 if (tot_out != stcb->asoc.total_flight) {
658 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
659 sctp_audit_data[sctp_audit_indx][1] = 0xA2;
661 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
665 SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
666 (int)stcb->asoc.total_flight);
667 stcb->asoc.total_flight = tot_out;
669 if (tot_book_cnt != stcb->asoc.total_flight_count) {
670 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
671 sctp_audit_data[sctp_audit_indx][1] = 0xA5;
673 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
677 SCTP_PRINTF("tot_flt_book:%d\n", tot_book);
679 stcb->asoc.total_flight_count = tot_book_cnt;
682 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
683 tot_out += lnet->flight_size;
685 if (tot_out != stcb->asoc.total_flight) {
686 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
687 sctp_audit_data[sctp_audit_indx][1] = 0xA3;
689 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
693 SCTP_PRINTF("real flight:%d net total was %d\n",
694 stcb->asoc.total_flight, tot_out);
695 /* now corrective action */
696 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
699 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
700 if ((chk->whoTo == lnet) &&
701 (chk->sent < SCTP_DATAGRAM_RESEND)) {
702 tot_out += chk->book_size;
705 if (lnet->flight_size != tot_out) {
706 SCTP_PRINTF("net:%x flight was %d corrected to %d\n",
707 (uint32_t) lnet, lnet->flight_size,
709 lnet->flight_size = tot_out;
714 sctp_print_audit_report();
719 sctp_audit_log(uint8_t ev, uint8_t fd)
722 sctp_audit_data[sctp_audit_indx][0] = ev;
723 sctp_audit_data[sctp_audit_indx][1] = fd;
725 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
733 * a list of sizes based on typical mtu's, used only if next hop size not
736 static int sctp_mtu_sizes[] = {
758 sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
760 struct sctp_association *asoc;
761 struct sctp_nets *net;
765 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
766 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
767 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
768 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
769 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
770 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
771 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
772 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
773 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
778 find_next_best_mtu(int totsz)
783 * if we are in here we must find the next best fit based on the
784 * size of the dg that failed to be sent.
787 for (i = 0; i < NUMBER_OF_MTU_SIZES; i++) {
788 if (totsz < sctp_mtu_sizes[i]) {
795 return (sctp_mtu_sizes[perfer]);
799 sctp_fill_random_store(struct sctp_pcb *m)
802 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
803 * our counter. The result becomes our good random numbers and we
804 * then setup to give these out. Note that we do no locking to
805 * protect this. This is ok, since if competing folks call this we
806 * will get more gobbled gook in the random store which is what we
807 * want. There is a danger that two guys will use the same random
808 * numbers, but thats ok too since that is random as well :->
811 (void)sctp_hmac(SCTP_HMAC, (uint8_t *) m->random_numbers,
812 sizeof(m->random_numbers), (uint8_t *) & m->random_counter,
813 sizeof(m->random_counter), (uint8_t *) m->random_store);
818 sctp_select_initial_TSN(struct sctp_pcb *inp)
821 * A true implementation should use random selection process to get
822 * the initial stream sequence number, using RFC1750 as a good
827 int store_at, new_store;
829 if (inp->initial_sequence_debug != 0) {
832 ret = inp->initial_sequence_debug;
833 inp->initial_sequence_debug++;
837 store_at = inp->store_at;
838 new_store = store_at + sizeof(uint32_t);
839 if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
842 if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
845 if (new_store == 0) {
846 /* Refill the random store */
847 sctp_fill_random_store(inp);
849 p = &inp->random_store[store_at];
856 sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int save_in_twait)
861 (void)SCTP_GETTIME_TIMEVAL(&now);
864 x = sctp_select_initial_TSN(&inp->sctp_ep);
869 if (sctp_is_vtag_good(inp, x, lport, rport, &now, save_in_twait)) {
877 sctp_init_asoc(struct sctp_inpcb *m, struct sctp_tcb *stcb,
878 int for_a_init, uint32_t override_tag, uint32_t vrf_id)
880 struct sctp_association *asoc;
883 * Anything set to zero is taken care of by the allocation routine's
888 * Up front select what scoping to apply on addresses I tell my peer
889 * Not sure what to do with these right now, we will need to come up
890 * with a way to set them. We may need to pass them through from the
891 * caller in the sctp_aloc_assoc() function.
896 /* init all variables to a known value. */
897 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE);
898 asoc->max_burst = m->sctp_ep.max_burst;
899 asoc->heart_beat_delay = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
900 asoc->cookie_life = m->sctp_ep.def_cookie_life;
901 asoc->sctp_cmt_on_off = (uint8_t) SCTP_BASE_SYSCTL(sctp_cmt_on_off);
902 /* EY Init nr_sack variable */
903 asoc->sctp_nr_sack_on_off = (uint8_t) SCTP_BASE_SYSCTL(sctp_nr_sack_on_off);
904 /* JRS 5/21/07 - Init CMT PF variables */
905 asoc->sctp_cmt_pf = (uint8_t) SCTP_BASE_SYSCTL(sctp_cmt_pf);
906 asoc->sctp_frag_point = m->sctp_frag_point;
908 asoc->default_tos = m->ip_inp.inp.inp_ip_tos;
910 asoc->default_tos = 0;
914 asoc->default_flowlabel = ((struct in6pcb *)m)->in6p_flowinfo;
916 asoc->default_flowlabel = 0;
918 asoc->sb_send_resv = 0;
920 asoc->my_vtag = override_tag;
922 asoc->my_vtag = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
924 /* Get the nonce tags */
925 asoc->my_vtag_nonce = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
926 asoc->peer_vtag_nonce = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
927 asoc->vrf_id = vrf_id;
929 if (sctp_is_feature_on(m, SCTP_PCB_FLAGS_DONOT_HEARTBEAT))
930 asoc->hb_is_disabled = 1;
932 asoc->hb_is_disabled = 0;
934 #ifdef SCTP_ASOCLOG_OF_TSNS
936 asoc->tsn_out_at = 0;
937 asoc->tsn_in_wrapped = 0;
938 asoc->tsn_out_wrapped = 0;
939 asoc->cumack_log_at = 0;
940 asoc->cumack_log_atsnt = 0;
942 #ifdef SCTP_FS_SPEC_LOG
946 asoc->assoc_up_sent = 0;
947 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
948 sctp_select_initial_TSN(&m->sctp_ep);
949 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
950 /* we are optimisitic here */
951 asoc->peer_supports_pktdrop = 1;
952 asoc->peer_supports_nat = 0;
953 asoc->sent_queue_retran_cnt = 0;
956 asoc->last_net_cmt_send_started = NULL;
958 /* This will need to be adjusted */
959 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
960 asoc->last_acked_seq = asoc->init_seq_number - 1;
961 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
962 asoc->asconf_seq_in = asoc->last_acked_seq;
964 /* here we are different, we hold the next one we expect */
965 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
967 asoc->initial_init_rto_max = m->sctp_ep.initial_init_rto_max;
968 asoc->initial_rto = m->sctp_ep.initial_rto;
970 asoc->max_init_times = m->sctp_ep.max_init_times;
971 asoc->max_send_times = m->sctp_ep.max_send_times;
972 asoc->def_net_failure = m->sctp_ep.def_net_failure;
973 asoc->free_chunk_cnt = 0;
975 asoc->iam_blocking = 0;
976 /* ECN Nonce initialization */
977 asoc->context = m->sctp_context;
978 asoc->def_send = m->def_send;
979 asoc->ecn_nonce_allowed = 0;
980 asoc->receiver_nonce_sum = 1;
981 asoc->nonce_sum_expect_base = 1;
982 asoc->nonce_sum_check = 1;
983 asoc->nonce_resync_tsn = 0;
984 asoc->nonce_wait_for_ecne = 0;
985 asoc->nonce_wait_tsn = 0;
986 asoc->delayed_ack = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
987 asoc->sack_freq = m->sctp_ep.sctp_sack_freq;
988 asoc->pr_sctp_cnt = 0;
989 asoc->total_output_queue_size = 0;
991 if (m->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
994 /* Its a V6 socket */
995 inp6 = (struct in6pcb *)m;
996 asoc->ipv6_addr_legal = 1;
997 /* Now look at the binding flag to see if V4 will be legal */
998 if (SCTP_IPV6_V6ONLY(inp6) == 0) {
999 asoc->ipv4_addr_legal = 1;
1001 /* V4 addresses are NOT legal on the association */
1002 asoc->ipv4_addr_legal = 0;
1005 /* Its a V4 socket, no - V6 */
1006 asoc->ipv4_addr_legal = 1;
1007 asoc->ipv6_addr_legal = 0;
1010 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(m->sctp_socket), SCTP_MINIMAL_RWND);
1011 asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(m->sctp_socket);
1013 asoc->smallest_mtu = m->sctp_frag_point;
1014 #ifdef SCTP_PRINT_FOR_B_AND_M
1015 SCTP_PRINTF("smallest_mtu init'd with asoc to :%d\n",
1016 asoc->smallest_mtu);
1018 asoc->minrto = m->sctp_ep.sctp_minrto;
1019 asoc->maxrto = m->sctp_ep.sctp_maxrto;
1021 asoc->locked_on_sending = NULL;
1022 asoc->stream_locked_on = 0;
1023 asoc->ecn_echo_cnt_onq = 0;
1024 asoc->stream_locked = 0;
1026 asoc->send_sack = 1;
1028 LIST_INIT(&asoc->sctp_restricted_addrs);
1030 TAILQ_INIT(&asoc->nets);
1031 TAILQ_INIT(&asoc->pending_reply_queue);
1032 TAILQ_INIT(&asoc->asconf_ack_sent);
1033 /* Setup to fill the hb random cache at first HB */
1034 asoc->hb_random_idx = 4;
1036 asoc->sctp_autoclose_ticks = m->sctp_ep.auto_close_time;
1039 * JRS - Pick the default congestion control module based on the
1042 switch (m->sctp_ep.sctp_default_cc_module) {
1043 /* JRS - Standard TCP congestion control */
1044 case SCTP_CC_RFC2581:
1046 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
1047 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
1048 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
1049 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
1050 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
1051 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
1052 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1053 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1054 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
1057 /* JRS - High Speed TCP congestion control (Floyd) */
1060 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
1061 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
1062 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
1063 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
1064 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
1065 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
1066 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1067 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1068 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
1071 /* JRS - HTCP congestion control */
1074 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
1075 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
1076 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
1077 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
1078 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
1079 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
1080 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1081 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1082 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
1085 /* JRS - By default, use RFC2581 */
1088 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
1089 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
1090 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
1091 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
1092 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
1093 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
1094 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1095 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1096 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
1102 * Now the stream parameters, here we allocate space for all streams
1103 * that we request by default.
1105 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1106 m->sctp_ep.pre_open_stream_count;
1107 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1108 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1110 if (asoc->strmout == NULL) {
1111 /* big trouble no memory */
1112 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1115 for (i = 0; i < asoc->streamoutcnt; i++) {
1117 * inbound side must be set to 0xffff, also NOTE when we get
1118 * the INIT-ACK back (for INIT sender) we MUST reduce the
1119 * count (streamoutcnt) but first check if we sent to any of
1120 * the upper streams that were dropped (if some were). Those
1121 * that were dropped must be notified to the upper layer as
1124 asoc->strmout[i].next_sequence_sent = 0x0;
1125 TAILQ_INIT(&asoc->strmout[i].outqueue);
1126 asoc->strmout[i].stream_no = i;
1127 asoc->strmout[i].last_msg_incomplete = 0;
1128 asoc->strmout[i].next_spoke.tqe_next = 0;
1129 asoc->strmout[i].next_spoke.tqe_prev = 0;
1131 /* Now the mapping array */
1132 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1133 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1135 if (asoc->mapping_array == NULL) {
1136 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1137 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1140 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1141 /* EY - initialize the nr_mapping_array just like mapping array */
1142 asoc->nr_mapping_array_size = SCTP_INITIAL_NR_MAPPING_ARRAY;
1143 SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->nr_mapping_array_size,
1145 if (asoc->nr_mapping_array == NULL) {
1146 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1147 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1148 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1151 memset(asoc->nr_mapping_array, 0, asoc->nr_mapping_array_size);
1153 /* Now the init of the other outqueues */
1154 TAILQ_INIT(&asoc->free_chunks);
1155 TAILQ_INIT(&asoc->out_wheel);
1156 TAILQ_INIT(&asoc->control_send_queue);
1157 TAILQ_INIT(&asoc->asconf_send_queue);
1158 TAILQ_INIT(&asoc->send_queue);
1159 TAILQ_INIT(&asoc->sent_queue);
1160 TAILQ_INIT(&asoc->reasmqueue);
1161 TAILQ_INIT(&asoc->resetHead);
1162 asoc->max_inbound_streams = m->sctp_ep.max_open_streams_intome;
1163 TAILQ_INIT(&asoc->asconf_queue);
1164 /* authentication fields */
1165 asoc->authinfo.random = NULL;
1166 asoc->authinfo.active_keyid = 0;
1167 asoc->authinfo.assoc_key = NULL;
1168 asoc->authinfo.assoc_keyid = 0;
1169 asoc->authinfo.recv_key = NULL;
1170 asoc->authinfo.recv_keyid = 0;
1171 LIST_INIT(&asoc->shared_keys);
1172 asoc->marked_retrans = 0;
1176 asoc->timoshutdown = 0;
1177 asoc->timoheartbeat = 0;
1178 asoc->timocookie = 0;
1179 asoc->timoshutdownack = 0;
1180 (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1181 asoc->discontinuity_time = asoc->start_time;
1183 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
1184 * freed later whe the association is freed.
1190 sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1192 /* mapping array needs to grow */
1196 new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1197 SCTP_MALLOC(new_array, uint8_t *, new_size, SCTP_M_MAP);
1198 if (new_array == NULL) {
1199 /* can't get more, forget it */
1200 SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n",
1204 memset(new_array, 0, new_size);
1205 memcpy(new_array, asoc->mapping_array, asoc->mapping_array_size);
1206 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1207 asoc->mapping_array = new_array;
1208 asoc->mapping_array_size = new_size;
1209 if (asoc->peer_supports_nr_sack) {
1210 new_size = asoc->nr_mapping_array_size + ((needed + 7) / 8 + SCTP_NR_MAPPING_ARRAY_INCR);
1211 SCTP_MALLOC(new_array, uint8_t *, new_size, SCTP_M_MAP);
1212 if (new_array == NULL) {
1213 /* can't get more, forget it */
1214 SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n",
1218 memset(new_array, 0, new_size);
1219 memcpy(new_array, asoc->nr_mapping_array, asoc->nr_mapping_array_size);
1220 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1221 asoc->nr_mapping_array = new_array;
1222 asoc->nr_mapping_array_size = new_size;
1228 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
1230 sctp_iterator_work(struct sctp_iterator *it)
1232 int iteration_count = 0;
1235 SCTP_ITERATOR_LOCK();
1237 SCTP_INP_DECR_REF(it->inp);
1239 if (it->inp == NULL) {
1240 /* iterator is complete */
1242 SCTP_ITERATOR_UNLOCK();
1243 if (it->function_atend != NULL) {
1244 (*it->function_atend) (it->pointer, it->val);
1246 SCTP_FREE(it, SCTP_M_ITER);
1250 SCTP_INP_WLOCK(it->inp);
1251 while (((it->pcb_flags) &&
1252 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1253 ((it->pcb_features) &&
1254 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1255 /* endpoint flags or features don't match, so keep looking */
1256 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1257 SCTP_INP_WUNLOCK(it->inp);
1258 goto done_with_iterator;
1260 SCTP_INP_WUNLOCK(it->inp);
1261 it->inp = LIST_NEXT(it->inp, sctp_list);
1262 if (it->inp == NULL) {
1263 goto done_with_iterator;
1265 SCTP_INP_WLOCK(it->inp);
1268 SCTP_INP_WUNLOCK(it->inp);
1269 SCTP_INP_RLOCK(it->inp);
1271 /* now go through each assoc which is in the desired state */
1272 if (it->done_current_ep == 0) {
1273 if (it->function_inp != NULL)
1274 inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1275 it->done_current_ep = 1;
1277 if (it->stcb == NULL) {
1278 /* run the per instance function */
1279 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1281 if ((inp_skip) || it->stcb == NULL) {
1282 if (it->function_inp_end != NULL) {
1283 inp_skip = (*it->function_inp_end) (it->inp,
1287 SCTP_INP_RUNLOCK(it->inp);
1291 SCTP_TCB_LOCK(it->stcb);
1292 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1293 /* not in the right state... keep looking */
1294 SCTP_TCB_UNLOCK(it->stcb);
1297 /* see if we have limited out the iterator loop */
1299 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1300 /* Pause to let others grab the lock */
1301 atomic_add_int(&it->stcb->asoc.refcnt, 1);
1302 SCTP_TCB_UNLOCK(it->stcb);
1304 SCTP_INP_INCR_REF(it->inp);
1305 SCTP_INP_RUNLOCK(it->inp);
1306 SCTP_ITERATOR_UNLOCK();
1307 SCTP_ITERATOR_LOCK();
1308 SCTP_INP_RLOCK(it->inp);
1310 SCTP_INP_DECR_REF(it->inp);
1311 SCTP_TCB_LOCK(it->stcb);
1312 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1313 iteration_count = 0;
1315 /* run function on this one */
1316 (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1319 * we lie here, it really needs to have its own type but
1320 * first I must verify that this won't effect things :-0
1322 if (it->no_chunk_output == 0)
1323 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1325 SCTP_TCB_UNLOCK(it->stcb);
1327 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1328 if (it->stcb == NULL) {
1329 /* Run last function */
1330 if (it->function_inp_end != NULL) {
1331 inp_skip = (*it->function_inp_end) (it->inp,
1337 SCTP_INP_RUNLOCK(it->inp);
1339 /* done with all assocs on this endpoint, move on to next endpoint */
1340 it->done_current_ep = 0;
1341 SCTP_INP_WLOCK(it->inp);
1342 SCTP_INP_WUNLOCK(it->inp);
1343 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1346 SCTP_INP_INFO_RLOCK();
1347 it->inp = LIST_NEXT(it->inp, sctp_list);
1348 SCTP_INP_INFO_RUNLOCK();
1350 if (it->inp == NULL) {
1351 goto done_with_iterator;
1353 goto select_a_new_ep;
1357 sctp_iterator_worker(void)
1359 struct sctp_iterator *it = NULL;
1361 /* This function is called with the WQ lock in place */
1363 SCTP_BASE_INFO(iterator_running) = 1;
1365 it = TAILQ_FIRST(&SCTP_BASE_INFO(iteratorhead));
1367 /* now lets work on this one */
1368 TAILQ_REMOVE(&SCTP_BASE_INFO(iteratorhead), it, sctp_nxt_itr);
1369 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1370 sctp_iterator_work(it);
1371 SCTP_IPI_ITERATOR_WQ_LOCK();
1372 /* sa_ignore FREED_MEMORY */
1373 it = TAILQ_FIRST(&SCTP_BASE_INFO(iteratorhead));
1375 if (TAILQ_FIRST(&SCTP_BASE_INFO(iteratorhead))) {
1378 SCTP_BASE_INFO(iterator_running) = 0;
1386 sctp_handle_addr_wq(void)
1388 /* deal with the ADDR wq from the rtsock calls */
1389 struct sctp_laddr *wi;
1390 struct sctp_asconf_iterator *asc;
1392 SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1393 sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
1395 /* Try later, no memory */
1396 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1397 (struct sctp_inpcb *)NULL,
1398 (struct sctp_tcb *)NULL,
1399 (struct sctp_nets *)NULL);
1402 LIST_INIT(&asc->list_of_work);
1404 SCTP_IPI_ITERATOR_WQ_LOCK();
1405 wi = LIST_FIRST(&SCTP_BASE_INFO(addr_wq));
1406 while (wi != NULL) {
1407 LIST_REMOVE(wi, sctp_nxt_addr);
1408 LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
1410 wi = LIST_FIRST(&SCTP_BASE_INFO(addr_wq));
1412 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1413 if (asc->cnt == 0) {
1414 SCTP_FREE(asc, SCTP_M_ASC_IT);
1416 (void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
1417 sctp_asconf_iterator_stcb,
1418 NULL, /* No ep end for boundall */
1419 SCTP_PCB_FLAGS_BOUNDALL,
1420 SCTP_PCB_ANY_FEATURES,
1421 SCTP_ASOC_ANY_STATE,
1423 sctp_asconf_iterator_end, NULL, 0);
1431 sctp_timeout_handler(void *t)
1433 struct sctp_inpcb *inp;
1434 struct sctp_tcb *stcb;
1435 struct sctp_nets *net;
1436 struct sctp_timer *tmr;
1438 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1442 int did_output, type;
1443 struct sctp_iterator *it = NULL;
1445 tmr = (struct sctp_timer *)t;
1446 inp = (struct sctp_inpcb *)tmr->ep;
1447 stcb = (struct sctp_tcb *)tmr->tcb;
1448 net = (struct sctp_nets *)tmr->net;
1451 #ifdef SCTP_AUDITING_ENABLED
1452 sctp_audit_log(0xF0, (uint8_t) tmr->type);
1453 sctp_auditing(3, inp, stcb, net);
1456 /* sanity checks... */
1457 if (tmr->self != (void *)tmr) {
1459 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1464 tmr->stopped_from = 0xa001;
1465 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1467 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1472 tmr->stopped_from = 0xa002;
1473 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1476 /* if this is an iterator timeout, get the struct and clear inp */
1477 tmr->stopped_from = 0xa003;
1478 if (tmr->type == SCTP_TIMER_TYPE_ITERATOR) {
1479 it = (struct sctp_iterator *)inp;
1484 SCTP_INP_INCR_REF(inp);
1485 if ((inp->sctp_socket == 0) &&
1486 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1487 (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1488 (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1489 (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1490 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1491 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1492 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1493 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1495 SCTP_INP_DECR_REF(inp);
1499 tmr->stopped_from = 0xa004;
1501 atomic_add_int(&stcb->asoc.refcnt, 1);
1502 if (stcb->asoc.state == 0) {
1503 atomic_add_int(&stcb->asoc.refcnt, -1);
1505 SCTP_INP_DECR_REF(inp);
1510 tmr->stopped_from = 0xa005;
1511 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type);
1512 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1514 SCTP_INP_DECR_REF(inp);
1517 atomic_add_int(&stcb->asoc.refcnt, -1);
1521 tmr->stopped_from = 0xa006;
1524 SCTP_TCB_LOCK(stcb);
1525 atomic_add_int(&stcb->asoc.refcnt, -1);
1526 if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) &&
1527 ((stcb->asoc.state == 0) ||
1528 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1529 SCTP_TCB_UNLOCK(stcb);
1531 SCTP_INP_DECR_REF(inp);
1536 /* record in stopped what t-o occured */
1537 tmr->stopped_from = tmr->type;
1539 /* mark as being serviced now */
1540 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1542 * Callout has been rescheduled.
1546 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1548 * Not active, so no action.
1552 SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1554 /* call the handler for the appropriate timer type */
1555 switch (tmr->type) {
1556 case SCTP_TIMER_TYPE_ZERO_COPY:
1560 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1561 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
1564 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1568 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1569 SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket);
1572 case SCTP_TIMER_TYPE_ADDR_WQ:
1573 sctp_handle_addr_wq();
1575 case SCTP_TIMER_TYPE_ITERATOR:
1576 SCTP_STAT_INCR(sctps_timoiterator);
1577 sctp_iterator_timer(it);
1579 case SCTP_TIMER_TYPE_SEND:
1580 if ((stcb == NULL) || (inp == NULL)) {
1583 SCTP_STAT_INCR(sctps_timodata);
1584 stcb->asoc.timodata++;
1585 stcb->asoc.num_send_timers_up--;
1586 if (stcb->asoc.num_send_timers_up < 0) {
1587 stcb->asoc.num_send_timers_up = 0;
1589 SCTP_TCB_LOCK_ASSERT(stcb);
1590 cur_oerr = stcb->asoc.overall_error_count;
1591 retcode = sctp_t3rxt_timer(inp, stcb, net);
1593 /* no need to unlock on tcb its gone */
1597 SCTP_TCB_LOCK_ASSERT(stcb);
1598 #ifdef SCTP_AUDITING_ENABLED
1599 sctp_auditing(4, inp, stcb, net);
1601 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1602 if ((stcb->asoc.num_send_timers_up == 0) &&
1603 (stcb->asoc.sent_queue_cnt > 0)
1605 struct sctp_tmit_chunk *chk;
1608 * safeguard. If there on some on the sent queue
1609 * somewhere but no timers running something is
1610 * wrong... so we start a timer on the first chunk
1611 * on the send queue on whatever net it is sent to.
1613 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1614 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1618 case SCTP_TIMER_TYPE_INIT:
1619 if ((stcb == NULL) || (inp == NULL)) {
1622 SCTP_STAT_INCR(sctps_timoinit);
1623 stcb->asoc.timoinit++;
1624 if (sctp_t1init_timer(inp, stcb, net)) {
1625 /* no need to unlock on tcb its gone */
1628 /* We do output but not here */
1631 case SCTP_TIMER_TYPE_RECV:
1632 if ((stcb == NULL) || (inp == NULL)) {
1637 SCTP_STAT_INCR(sctps_timosack);
1638 stcb->asoc.timosack++;
1639 if (stcb->asoc.cumulative_tsn != stcb->asoc.highest_tsn_inside_map)
1640 sctp_sack_check(stcb, 0, 0, &abort_flag);
1643 * EY if nr_sacks used then send an nr-sack , a sack
1646 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
1647 sctp_send_nr_sack(stcb);
1649 sctp_send_sack(stcb);
1651 #ifdef SCTP_AUDITING_ENABLED
1652 sctp_auditing(4, inp, stcb, net);
1654 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1656 case SCTP_TIMER_TYPE_SHUTDOWN:
1657 if ((stcb == NULL) || (inp == NULL)) {
1660 if (sctp_shutdown_timer(inp, stcb, net)) {
1661 /* no need to unlock on tcb its gone */
1664 SCTP_STAT_INCR(sctps_timoshutdown);
1665 stcb->asoc.timoshutdown++;
1666 #ifdef SCTP_AUDITING_ENABLED
1667 sctp_auditing(4, inp, stcb, net);
1669 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1671 case SCTP_TIMER_TYPE_HEARTBEAT:
1673 struct sctp_nets *lnet;
1674 int cnt_of_unconf = 0;
1676 if ((stcb == NULL) || (inp == NULL)) {
1679 SCTP_STAT_INCR(sctps_timoheartbeat);
1680 stcb->asoc.timoheartbeat++;
1681 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1682 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1683 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
1687 if (cnt_of_unconf == 0) {
1688 if (sctp_heartbeat_timer(inp, stcb, lnet,
1690 /* no need to unlock on tcb its gone */
1694 #ifdef SCTP_AUDITING_ENABLED
1695 sctp_auditing(4, inp, stcb, lnet);
1697 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
1698 stcb->sctp_ep, stcb, lnet);
1699 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1702 case SCTP_TIMER_TYPE_COOKIE:
1703 if ((stcb == NULL) || (inp == NULL)) {
1706 if (sctp_cookie_timer(inp, stcb, net)) {
1707 /* no need to unlock on tcb its gone */
1710 SCTP_STAT_INCR(sctps_timocookie);
1711 stcb->asoc.timocookie++;
1712 #ifdef SCTP_AUDITING_ENABLED
1713 sctp_auditing(4, inp, stcb, net);
1716 * We consider T3 and Cookie timer pretty much the same with
1717 * respect to where from in chunk_output.
1719 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1721 case SCTP_TIMER_TYPE_NEWCOOKIE:
1729 SCTP_STAT_INCR(sctps_timosecret);
1730 (void)SCTP_GETTIME_TIMEVAL(&tv);
1731 SCTP_INP_WLOCK(inp);
1732 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1733 inp->sctp_ep.last_secret_number =
1734 inp->sctp_ep.current_secret_number;
1735 inp->sctp_ep.current_secret_number++;
1736 if (inp->sctp_ep.current_secret_number >=
1737 SCTP_HOW_MANY_SECRETS) {
1738 inp->sctp_ep.current_secret_number = 0;
1740 secret = (int)inp->sctp_ep.current_secret_number;
1741 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1742 inp->sctp_ep.secret_key[secret][i] =
1743 sctp_select_initial_TSN(&inp->sctp_ep);
1745 SCTP_INP_WUNLOCK(inp);
1746 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1750 case SCTP_TIMER_TYPE_PATHMTURAISE:
1751 if ((stcb == NULL) || (inp == NULL)) {
1754 SCTP_STAT_INCR(sctps_timopathmtu);
1755 sctp_pathmtu_timer(inp, stcb, net);
1758 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1759 if ((stcb == NULL) || (inp == NULL)) {
1762 if (sctp_shutdownack_timer(inp, stcb, net)) {
1763 /* no need to unlock on tcb its gone */
1766 SCTP_STAT_INCR(sctps_timoshutdownack);
1767 stcb->asoc.timoshutdownack++;
1768 #ifdef SCTP_AUDITING_ENABLED
1769 sctp_auditing(4, inp, stcb, net);
1771 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1773 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1774 if ((stcb == NULL) || (inp == NULL)) {
1777 SCTP_STAT_INCR(sctps_timoshutdownguard);
1778 sctp_abort_an_association(inp, stcb,
1779 SCTP_SHUTDOWN_GUARD_EXPIRES, NULL, SCTP_SO_NOT_LOCKED);
1780 /* no need to unlock on tcb its gone */
1783 case SCTP_TIMER_TYPE_STRRESET:
1784 if ((stcb == NULL) || (inp == NULL)) {
1787 if (sctp_strreset_timer(inp, stcb, net)) {
1788 /* no need to unlock on tcb its gone */
1791 SCTP_STAT_INCR(sctps_timostrmrst);
1792 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1794 case SCTP_TIMER_TYPE_EARLYFR:
1795 /* Need to do FR of things for net */
1796 if ((stcb == NULL) || (inp == NULL)) {
1799 SCTP_STAT_INCR(sctps_timoearlyfr);
1800 sctp_early_fr_timer(inp, stcb, net);
1802 case SCTP_TIMER_TYPE_ASCONF:
1803 if ((stcb == NULL) || (inp == NULL)) {
1806 if (sctp_asconf_timer(inp, stcb, net)) {
1807 /* no need to unlock on tcb its gone */
1810 SCTP_STAT_INCR(sctps_timoasconf);
1811 #ifdef SCTP_AUDITING_ENABLED
1812 sctp_auditing(4, inp, stcb, net);
1814 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1816 case SCTP_TIMER_TYPE_PRIM_DELETED:
1817 if ((stcb == NULL) || (inp == NULL)) {
1820 sctp_delete_prim_timer(inp, stcb, net);
1821 SCTP_STAT_INCR(sctps_timodelprim);
1824 case SCTP_TIMER_TYPE_AUTOCLOSE:
1825 if ((stcb == NULL) || (inp == NULL)) {
1828 SCTP_STAT_INCR(sctps_timoautoclose);
1829 sctp_autoclose_timer(inp, stcb, net);
1830 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1833 case SCTP_TIMER_TYPE_ASOCKILL:
1834 if ((stcb == NULL) || (inp == NULL)) {
1837 SCTP_STAT_INCR(sctps_timoassockill);
1838 /* Can we free it yet? */
1839 SCTP_INP_DECR_REF(inp);
1840 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1841 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1842 so = SCTP_INP_SO(inp);
1843 atomic_add_int(&stcb->asoc.refcnt, 1);
1844 SCTP_TCB_UNLOCK(stcb);
1845 SCTP_SOCKET_LOCK(so, 1);
1846 SCTP_TCB_LOCK(stcb);
1847 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1849 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1850 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1851 SCTP_SOCKET_UNLOCK(so, 1);
1854 * free asoc, always unlocks (or destroy's) so prevent
1855 * duplicate unlock or unlock of a free mtx :-0
1859 case SCTP_TIMER_TYPE_INPKILL:
1860 SCTP_STAT_INCR(sctps_timoinpkill);
1865 * special case, take away our increment since WE are the
1868 SCTP_INP_DECR_REF(inp);
1869 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1870 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
1871 SCTP_CALLED_DIRECTLY_NOCMPSET);
1875 SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1879 #ifdef SCTP_AUDITING_ENABLED
1880 sctp_audit_log(0xF1, (uint8_t) tmr->type);
1882 sctp_auditing(5, inp, stcb, net);
1884 if ((did_output) && stcb) {
1886 * Now we need to clean up the control chunk chain if an
1887 * ECNE is on it. It must be marked as UNSENT again so next
1888 * call will continue to send it until such time that we get
1889 * a CWR, to remove it. It is, however, less likely that we
1890 * will find a ecn echo on the chain though.
1892 sctp_fix_ecn_echo(&stcb->asoc);
1896 SCTP_TCB_UNLOCK(stcb);
1900 SCTP_INP_DECR_REF(inp);
1903 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n",
1908 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1909 struct sctp_nets *net)
1912 struct sctp_timer *tmr;
1914 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1921 SCTP_TCB_LOCK_ASSERT(stcb);
1924 case SCTP_TIMER_TYPE_ZERO_COPY:
1925 tmr = &inp->sctp_ep.zero_copy_timer;
1926 to_ticks = SCTP_ZERO_COPY_TICK_DELAY;
1928 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1929 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
1930 to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY;
1932 case SCTP_TIMER_TYPE_ADDR_WQ:
1933 /* Only 1 tick away :-) */
1934 tmr = &SCTP_BASE_INFO(addr_wq_timer);
1935 to_ticks = SCTP_ADDRESS_TICK_DELAY;
1937 case SCTP_TIMER_TYPE_ITERATOR:
1939 struct sctp_iterator *it;
1941 it = (struct sctp_iterator *)inp;
1943 to_ticks = SCTP_ITERATOR_TICKS;
1946 case SCTP_TIMER_TYPE_SEND:
1947 /* Here we use the RTO timer */
1951 if ((stcb == NULL) || (net == NULL)) {
1954 tmr = &net->rxt_timer;
1955 if (net->RTO == 0) {
1956 rto_val = stcb->asoc.initial_rto;
1960 to_ticks = MSEC_TO_TICKS(rto_val);
1963 case SCTP_TIMER_TYPE_INIT:
1965 * Here we use the INIT timer default usually about 1
1968 if ((stcb == NULL) || (net == NULL)) {
1971 tmr = &net->rxt_timer;
1972 if (net->RTO == 0) {
1973 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1975 to_ticks = MSEC_TO_TICKS(net->RTO);
1978 case SCTP_TIMER_TYPE_RECV:
1980 * Here we use the Delayed-Ack timer value from the inp
1981 * ususually about 200ms.
1986 tmr = &stcb->asoc.dack_timer;
1987 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
1989 case SCTP_TIMER_TYPE_SHUTDOWN:
1990 /* Here we use the RTO of the destination. */
1991 if ((stcb == NULL) || (net == NULL)) {
1994 if (net->RTO == 0) {
1995 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1997 to_ticks = MSEC_TO_TICKS(net->RTO);
1999 tmr = &net->rxt_timer;
2001 case SCTP_TIMER_TYPE_HEARTBEAT:
2003 * the net is used here so that we can add in the RTO. Even
2004 * though we use a different timer. We also add the HB timer
2005 * PLUS a random jitter.
2007 if ((inp == NULL) || (stcb == NULL)) {
2011 uint8_t this_random;
2012 int cnt_of_unconf = 0;
2013 struct sctp_nets *lnet;
2015 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
2016 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2017 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
2021 if (cnt_of_unconf) {
2023 (void)sctp_heartbeat_timer(inp, stcb, lnet, cnt_of_unconf);
2025 if (stcb->asoc.hb_random_idx > 3) {
2026 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2027 memcpy(stcb->asoc.hb_random_values, &rndval,
2028 sizeof(stcb->asoc.hb_random_values));
2029 stcb->asoc.hb_random_idx = 0;
2031 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
2032 stcb->asoc.hb_random_idx++;
2033 stcb->asoc.hb_ect_randombit = 0;
2035 * this_random will be 0 - 256 ms RTO is in ms.
2037 if ((stcb->asoc.hb_is_disabled) &&
2038 (cnt_of_unconf == 0)) {
2044 delay = stcb->asoc.heart_beat_delay;
2045 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
2046 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2047 ((lnet->dest_state & SCTP_ADDR_OUT_OF_SCOPE) == 0) &&
2048 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
2052 if (net->RTO == 0) {
2053 /* Never been checked */
2054 to_ticks = this_random + stcb->asoc.initial_rto + delay;
2056 /* set rto_val to the ms */
2057 to_ticks = delay + net->RTO + this_random;
2060 if (cnt_of_unconf) {
2061 to_ticks = this_random + stcb->asoc.initial_rto;
2063 to_ticks = stcb->asoc.heart_beat_delay + this_random + stcb->asoc.initial_rto;
2067 * Now we must convert the to_ticks that are now in
2070 to_ticks = MSEC_TO_TICKS(to_ticks);
2071 tmr = &stcb->asoc.hb_timer;
2074 case SCTP_TIMER_TYPE_COOKIE:
2076 * Here we can use the RTO timer from the network since one
2077 * RTT was compelete. If a retran happened then we will be
2078 * using the RTO initial value.
2080 if ((stcb == NULL) || (net == NULL)) {
2083 if (net->RTO == 0) {
2084 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2086 to_ticks = MSEC_TO_TICKS(net->RTO);
2088 tmr = &net->rxt_timer;
2090 case SCTP_TIMER_TYPE_NEWCOOKIE:
2092 * nothing needed but the endpoint here ususually about 60
2098 tmr = &inp->sctp_ep.signature_change;
2099 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2101 case SCTP_TIMER_TYPE_ASOCKILL:
2105 tmr = &stcb->asoc.strreset_timer;
2106 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2108 case SCTP_TIMER_TYPE_INPKILL:
2110 * The inp is setup to die. We re-use the signature_chage
2111 * timer since that has stopped and we are in the GONE
2117 tmr = &inp->sctp_ep.signature_change;
2118 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2120 case SCTP_TIMER_TYPE_PATHMTURAISE:
2122 * Here we use the value found in the EP for PMTU ususually
2125 if ((stcb == NULL) || (inp == NULL)) {
2131 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2132 tmr = &net->pmtu_timer;
2134 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2135 /* Here we use the RTO of the destination */
2136 if ((stcb == NULL) || (net == NULL)) {
2139 if (net->RTO == 0) {
2140 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2142 to_ticks = MSEC_TO_TICKS(net->RTO);
2144 tmr = &net->rxt_timer;
2146 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2148 * Here we use the endpoints shutdown guard timer usually
2151 if ((inp == NULL) || (stcb == NULL)) {
2154 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2155 tmr = &stcb->asoc.shut_guard_timer;
2157 case SCTP_TIMER_TYPE_STRRESET:
2159 * Here the timer comes from the stcb but its value is from
2162 if ((stcb == NULL) || (net == NULL)) {
2165 if (net->RTO == 0) {
2166 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2168 to_ticks = MSEC_TO_TICKS(net->RTO);
2170 tmr = &stcb->asoc.strreset_timer;
2173 case SCTP_TIMER_TYPE_EARLYFR:
2177 if ((stcb == NULL) || (net == NULL)) {
2180 if (net->flight_size > net->cwnd) {
2181 /* no need to start */
2184 SCTP_STAT_INCR(sctps_earlyfrstart);
2185 if (net->lastsa == 0) {
2186 /* Hmm no rtt estimate yet? */
2187 msec = stcb->asoc.initial_rto >> 2;
2189 msec = ((net->lastsa >> 2) + net->lastsv) >> 1;
2191 if (msec < SCTP_BASE_SYSCTL(sctp_early_fr_msec)) {
2192 msec = SCTP_BASE_SYSCTL(sctp_early_fr_msec);
2193 if (msec < SCTP_MINFR_MSEC_FLOOR) {
2194 msec = SCTP_MINFR_MSEC_FLOOR;
2197 to_ticks = MSEC_TO_TICKS(msec);
2198 tmr = &net->fr_timer;
2201 case SCTP_TIMER_TYPE_ASCONF:
2203 * Here the timer comes from the stcb but its value is from
2206 if ((stcb == NULL) || (net == NULL)) {
2209 if (net->RTO == 0) {
2210 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2212 to_ticks = MSEC_TO_TICKS(net->RTO);
2214 tmr = &stcb->asoc.asconf_timer;
2216 case SCTP_TIMER_TYPE_PRIM_DELETED:
2217 if ((stcb == NULL) || (net != NULL)) {
2220 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2221 tmr = &stcb->asoc.delete_prim_timer;
2223 case SCTP_TIMER_TYPE_AUTOCLOSE:
2227 if (stcb->asoc.sctp_autoclose_ticks == 0) {
2229 * Really an error since stcb is NOT set to
2234 to_ticks = stcb->asoc.sctp_autoclose_ticks;
2235 tmr = &stcb->asoc.autoclose_timer;
2238 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2239 __FUNCTION__, t_type);
2243 if ((to_ticks <= 0) || (tmr == NULL)) {
2244 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2245 __FUNCTION__, t_type, to_ticks, tmr);
2248 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2250 * we do NOT allow you to have it already running. if it is
2251 * we leave the current one up unchanged
2255 /* At this point we can proceed */
2256 if (t_type == SCTP_TIMER_TYPE_SEND) {
2257 stcb->asoc.num_send_timers_up++;
2259 tmr->stopped_from = 0;
2261 tmr->ep = (void *)inp;
2262 tmr->tcb = (void *)stcb;
2263 tmr->net = (void *)net;
2264 tmr->self = (void *)tmr;
2265 tmr->ticks = sctp_get_tick_count();
2266 (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2271 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2272 struct sctp_nets *net, uint32_t from)
2274 struct sctp_timer *tmr;
2276 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2282 SCTP_TCB_LOCK_ASSERT(stcb);
2285 case SCTP_TIMER_TYPE_ZERO_COPY:
2286 tmr = &inp->sctp_ep.zero_copy_timer;
2288 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
2289 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
2291 case SCTP_TIMER_TYPE_ADDR_WQ:
2292 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2294 case SCTP_TIMER_TYPE_EARLYFR:
2295 if ((stcb == NULL) || (net == NULL)) {
2298 tmr = &net->fr_timer;
2299 SCTP_STAT_INCR(sctps_earlyfrstop);
2301 case SCTP_TIMER_TYPE_ITERATOR:
2303 struct sctp_iterator *it;
2305 it = (struct sctp_iterator *)inp;
2309 case SCTP_TIMER_TYPE_SEND:
2310 if ((stcb == NULL) || (net == NULL)) {
2313 tmr = &net->rxt_timer;
2315 case SCTP_TIMER_TYPE_INIT:
2316 if ((stcb == NULL) || (net == NULL)) {
2319 tmr = &net->rxt_timer;
2321 case SCTP_TIMER_TYPE_RECV:
2325 tmr = &stcb->asoc.dack_timer;
2327 case SCTP_TIMER_TYPE_SHUTDOWN:
2328 if ((stcb == NULL) || (net == NULL)) {
2331 tmr = &net->rxt_timer;
2333 case SCTP_TIMER_TYPE_HEARTBEAT:
2337 tmr = &stcb->asoc.hb_timer;
2339 case SCTP_TIMER_TYPE_COOKIE:
2340 if ((stcb == NULL) || (net == NULL)) {
2343 tmr = &net->rxt_timer;
2345 case SCTP_TIMER_TYPE_NEWCOOKIE:
2346 /* nothing needed but the endpoint here */
2347 tmr = &inp->sctp_ep.signature_change;
2349 * We re-use the newcookie timer for the INP kill timer. We
2350 * must assure that we do not kill it by accident.
2353 case SCTP_TIMER_TYPE_ASOCKILL:
2355 * Stop the asoc kill timer.
2360 tmr = &stcb->asoc.strreset_timer;
2363 case SCTP_TIMER_TYPE_INPKILL:
2365 * The inp is setup to die. We re-use the signature_chage
2366 * timer since that has stopped and we are in the GONE
2369 tmr = &inp->sctp_ep.signature_change;
2371 case SCTP_TIMER_TYPE_PATHMTURAISE:
2372 if ((stcb == NULL) || (net == NULL)) {
2375 tmr = &net->pmtu_timer;
2377 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2378 if ((stcb == NULL) || (net == NULL)) {
2381 tmr = &net->rxt_timer;
2383 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2387 tmr = &stcb->asoc.shut_guard_timer;
2389 case SCTP_TIMER_TYPE_STRRESET:
2393 tmr = &stcb->asoc.strreset_timer;
2395 case SCTP_TIMER_TYPE_ASCONF:
2399 tmr = &stcb->asoc.asconf_timer;
2401 case SCTP_TIMER_TYPE_PRIM_DELETED:
2405 tmr = &stcb->asoc.delete_prim_timer;
2407 case SCTP_TIMER_TYPE_AUTOCLOSE:
2411 tmr = &stcb->asoc.autoclose_timer;
2414 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2415 __FUNCTION__, t_type);
2421 if ((tmr->type != t_type) && tmr->type) {
2423 * Ok we have a timer that is under joint use. Cookie timer
2424 * per chance with the SEND timer. We therefore are NOT
2425 * running the timer that the caller wants stopped. So just
2430 if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2431 stcb->asoc.num_send_timers_up--;
2432 if (stcb->asoc.num_send_timers_up < 0) {
2433 stcb->asoc.num_send_timers_up = 0;
2437 tmr->stopped_from = from;
2438 (void)SCTP_OS_TIMER_STOP(&tmr->timer);
2443 sctp_calculate_len(struct mbuf *m)
2450 tlen += SCTP_BUF_LEN(at);
2451 at = SCTP_BUF_NEXT(at);
2457 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2458 struct sctp_association *asoc, uint32_t mtu)
2461 * Reset the P-MTU size on this association, this involves changing
2462 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2463 * allow the DF flag to be cleared.
2465 struct sctp_tmit_chunk *chk;
2466 unsigned int eff_mtu, ovh;
2468 #ifdef SCTP_PRINT_FOR_B_AND_M
2469 SCTP_PRINTF("sctp_mtu_size_reset(%p, asoc:%p mtu:%d\n",
2472 asoc->smallest_mtu = mtu;
2473 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2474 ovh = SCTP_MIN_OVERHEAD;
2476 ovh = SCTP_MIN_V4_OVERHEAD;
2478 eff_mtu = mtu - ovh;
2479 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2481 if (chk->send_size > eff_mtu) {
2482 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2485 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2486 if (chk->send_size > eff_mtu) {
2487 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2494 * given an association and starting time of the current RTT period return
2495 * RTO in number of msecs net should point to the current network
2498 sctp_calculate_rto(struct sctp_tcb *stcb,
2499 struct sctp_association *asoc,
2500 struct sctp_nets *net,
2501 struct timeval *told,
2505 * given an association and the starting time of the current RTT
2506 * period (in value1/value2) return RTO in number of msecs.
2510 uint32_t new_rto = 0;
2511 int first_measure = 0;
2512 struct timeval now, then, *old;
2514 /* Copy it out for sparc64 */
2515 if (safe == sctp_align_unsafe_makecopy) {
2517 memcpy(&then, told, sizeof(struct timeval));
2518 } else if (safe == sctp_align_safe_nocopy) {
2522 SCTP_PRINTF("Huh, bad rto calc call\n");
2525 /************************/
2526 /* 1. calculate new RTT */
2527 /************************/
2528 /* get the current time */
2529 (void)SCTP_GETTIME_TIMEVAL(&now);
2530 /* compute the RTT value */
2531 if ((u_long)now.tv_sec > (u_long)old->tv_sec) {
2532 calc_time = ((u_long)now.tv_sec - (u_long)old->tv_sec) * 1000;
2533 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2534 calc_time += (((u_long)now.tv_usec -
2535 (u_long)old->tv_usec) / 1000);
2536 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2537 /* Borrow 1,000ms from current calculation */
2539 /* Add in the slop over */
2540 calc_time += ((int)now.tv_usec / 1000);
2541 /* Add in the pre-second ms's */
2542 calc_time += (((int)1000000 - (int)old->tv_usec) / 1000);
2544 } else if ((u_long)now.tv_sec == (u_long)old->tv_sec) {
2545 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2546 calc_time = ((u_long)now.tv_usec -
2547 (u_long)old->tv_usec) / 1000;
2548 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2549 /* impossible .. garbage in nothing out */
2551 } else if ((u_long)now.tv_usec == (u_long)old->tv_usec) {
2553 * We have to have 1 usec :-D this must be the
2558 /* impossible .. garbage in nothing out */
2562 /* Clock wrapped? */
2565 /***************************/
2566 /* 2. update RTTVAR & SRTT */
2567 /***************************/
2568 net->rtt = o_calctime = calc_time;
2569 /* this is Van Jacobson's integer version */
2570 if (net->RTO_measured) {
2571 calc_time -= (net->lastsa >> SCTP_RTT_SHIFT); /* take away 1/8th when
2573 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2574 rto_logging(net, SCTP_LOG_RTTVAR);
2576 net->prev_rtt = o_calctime;
2577 net->lastsa += calc_time; /* add 7/8th into sa when
2579 if (calc_time < 0) {
2580 calc_time = -calc_time;
2582 calc_time -= (net->lastsv >> SCTP_RTT_VAR_SHIFT); /* take away 1/4 when
2584 net->lastsv += calc_time;
2585 if (net->lastsv == 0) {
2586 net->lastsv = SCTP_CLOCK_GRANULARITY;
2589 /* First RTO measurment */
2590 net->RTO_measured = 1;
2591 net->lastsa = calc_time << SCTP_RTT_SHIFT; /* Multiply by 8 when
2593 net->lastsv = calc_time;
2594 if (net->lastsv == 0) {
2595 net->lastsv = SCTP_CLOCK_GRANULARITY;
2598 net->prev_rtt = o_calctime;
2599 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2600 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2604 new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2605 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2606 (stcb->asoc.sat_network_lockout == 0)) {
2607 stcb->asoc.sat_network = 1;
2608 } else if ((!first_measure) && stcb->asoc.sat_network) {
2609 stcb->asoc.sat_network = 0;
2610 stcb->asoc.sat_network_lockout = 1;
2612 /* bound it, per C6/C7 in Section 5.3.1 */
2613 if (new_rto < stcb->asoc.minrto) {
2614 new_rto = stcb->asoc.minrto;
2616 if (new_rto > stcb->asoc.maxrto) {
2617 new_rto = stcb->asoc.maxrto;
2619 /* we are now returning the RTO */
2624 * return a pointer to a contiguous piece of data from the given mbuf chain
2625 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2626 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2627 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2630 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2636 if ((off < 0) || (len <= 0))
2639 /* find the desired start location */
2640 while ((m != NULL) && (off > 0)) {
2641 if (off < SCTP_BUF_LEN(m))
2643 off -= SCTP_BUF_LEN(m);
2644 m = SCTP_BUF_NEXT(m);
2649 /* is the current mbuf large enough (eg. contiguous)? */
2650 if ((SCTP_BUF_LEN(m) - off) >= len) {
2651 return (mtod(m, caddr_t)+off);
2653 /* else, it spans more than one mbuf, so save a temp copy... */
2654 while ((m != NULL) && (len > 0)) {
2655 count = min(SCTP_BUF_LEN(m) - off, len);
2656 bcopy(mtod(m, caddr_t)+off, ptr, count);
2660 m = SCTP_BUF_NEXT(m);
2662 if ((m == NULL) && (len > 0))
2665 return ((caddr_t)in_ptr);
2671 struct sctp_paramhdr *
2672 sctp_get_next_param(struct mbuf *m,
2674 struct sctp_paramhdr *pull,
2677 /* This just provides a typed signature to Peter's Pull routine */
2678 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2684 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2687 * add padlen bytes of 0 filled padding to the end of the mbuf. If
2688 * padlen is > 3 this routine will fail.
2694 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
2697 if (padlen <= M_TRAILINGSPACE(m)) {
2699 * The easy way. We hope the majority of the time we hit
2702 dp = (uint8_t *) (mtod(m, caddr_t)+SCTP_BUF_LEN(m));
2703 SCTP_BUF_LEN(m) += padlen;
2705 /* Hard way we must grow the mbuf */
2708 tmp = sctp_get_mbuf_for_msg(padlen, 0, M_DONTWAIT, 1, MT_DATA);
2710 /* Out of space GAK! we are in big trouble. */
2711 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
2714 /* setup and insert in middle */
2715 SCTP_BUF_LEN(tmp) = padlen;
2716 SCTP_BUF_NEXT(tmp) = NULL;
2717 SCTP_BUF_NEXT(m) = tmp;
2718 dp = mtod(tmp, uint8_t *);
2720 /* zero out the pad */
2721 for (i = 0; i < padlen; i++) {
2729 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2731 /* find the last mbuf in chain and pad it */
2736 return (sctp_add_pad_tombuf(last_mbuf, padval));
2739 if (SCTP_BUF_NEXT(m_at) == NULL) {
2740 return (sctp_add_pad_tombuf(m_at, padval));
2742 m_at = SCTP_BUF_NEXT(m_at);
2745 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
2749 int sctp_asoc_change_wake = 0;
2752 sctp_notify_assoc_change(uint32_t event, struct sctp_tcb *stcb,
2753 uint32_t error, void *data, int so_locked
2754 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2759 struct mbuf *m_notify;
2760 struct sctp_assoc_change *sac;
2761 struct sctp_queued_to_read *control;
2763 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2769 * For TCP model AND UDP connected sockets we will send an error up
2770 * when an ABORT comes in.
2772 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2773 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2774 ((event == SCTP_COMM_LOST) || (event == SCTP_CANT_STR_ASSOC))) {
2775 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
2776 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
2777 stcb->sctp_socket->so_error = ECONNREFUSED;
2779 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
2780 stcb->sctp_socket->so_error = ECONNRESET;
2782 /* Wake ANY sleepers */
2783 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2784 so = SCTP_INP_SO(stcb->sctp_ep);
2786 atomic_add_int(&stcb->asoc.refcnt, 1);
2787 SCTP_TCB_UNLOCK(stcb);
2788 SCTP_SOCKET_LOCK(so, 1);
2789 SCTP_TCB_LOCK(stcb);
2790 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2791 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2792 SCTP_SOCKET_UNLOCK(so, 1);
2797 sorwakeup(stcb->sctp_socket);
2798 sowwakeup(stcb->sctp_socket);
2799 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2801 SCTP_SOCKET_UNLOCK(so, 1);
2804 sctp_asoc_change_wake++;
2806 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2807 /* event not enabled */
2810 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_change), 0, M_DONTWAIT, 1, MT_DATA);
2811 if (m_notify == NULL)
2814 SCTP_BUF_LEN(m_notify) = 0;
2816 sac = mtod(m_notify, struct sctp_assoc_change *);
2817 sac->sac_type = SCTP_ASSOC_CHANGE;
2819 sac->sac_length = sizeof(struct sctp_assoc_change);
2820 sac->sac_state = event;
2821 sac->sac_error = error;
2822 /* XXX verify these stream counts */
2823 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2824 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2825 sac->sac_assoc_id = sctp_get_associd(stcb);
2826 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_change);
2827 SCTP_BUF_NEXT(m_notify) = NULL;
2828 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2831 if (control == NULL) {
2833 sctp_m_freem(m_notify);
2836 control->length = SCTP_BUF_LEN(m_notify);
2837 /* not that we need this */
2838 control->tail_mbuf = m_notify;
2839 control->spec_flags = M_NOTIFICATION;
2840 sctp_add_to_readq(stcb->sctp_ep, stcb,
2842 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2844 if (event == SCTP_COMM_LOST) {
2845 /* Wake up any sleeper */
2846 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2847 so = SCTP_INP_SO(stcb->sctp_ep);
2849 atomic_add_int(&stcb->asoc.refcnt, 1);
2850 SCTP_TCB_UNLOCK(stcb);
2851 SCTP_SOCKET_LOCK(so, 1);
2852 SCTP_TCB_LOCK(stcb);
2853 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2854 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2855 SCTP_SOCKET_UNLOCK(so, 1);
2860 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
2861 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2863 SCTP_SOCKET_UNLOCK(so, 1);
2870 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2871 struct sockaddr *sa, uint32_t error)
2873 struct mbuf *m_notify;
2874 struct sctp_paddr_change *spc;
2875 struct sctp_queued_to_read *control;
2877 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2878 /* event not enabled */
2881 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_DONTWAIT, 1, MT_DATA);
2882 if (m_notify == NULL)
2884 SCTP_BUF_LEN(m_notify) = 0;
2885 spc = mtod(m_notify, struct sctp_paddr_change *);
2886 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2888 spc->spc_length = sizeof(struct sctp_paddr_change);
2889 switch (sa->sa_family) {
2891 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2896 struct sockaddr_in6 *sin6;
2898 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2900 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2901 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2902 if (sin6->sin6_scope_id == 0) {
2903 /* recover scope_id for user */
2904 (void)sa6_recoverscope(sin6);
2906 /* clear embedded scope_id for user */
2907 in6_clearscope(&sin6->sin6_addr);
2917 spc->spc_state = state;
2918 spc->spc_error = error;
2919 spc->spc_assoc_id = sctp_get_associd(stcb);
2921 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2922 SCTP_BUF_NEXT(m_notify) = NULL;
2924 /* append to socket */
2925 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2928 if (control == NULL) {
2930 sctp_m_freem(m_notify);
2933 control->length = SCTP_BUF_LEN(m_notify);
2934 control->spec_flags = M_NOTIFICATION;
2935 /* not that we need this */
2936 control->tail_mbuf = m_notify;
2937 sctp_add_to_readq(stcb->sctp_ep, stcb,
2939 &stcb->sctp_socket->so_rcv, 1,
2940 SCTP_READ_LOCK_NOT_HELD,
2941 SCTP_SO_NOT_LOCKED);
2946 sctp_notify_send_failed(struct sctp_tcb *stcb, uint32_t error,
2947 struct sctp_tmit_chunk *chk, int so_locked
2948 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2953 struct mbuf *m_notify;
2954 struct sctp_send_failed *ssf;
2955 struct sctp_queued_to_read *control;
2958 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)) {
2959 /* event not enabled */
2962 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 0, M_DONTWAIT, 1, MT_DATA);
2963 if (m_notify == NULL)
2966 length = sizeof(struct sctp_send_failed) + chk->send_size;
2967 length -= sizeof(struct sctp_data_chunk);
2968 SCTP_BUF_LEN(m_notify) = 0;
2969 ssf = mtod(m_notify, struct sctp_send_failed *);
2970 ssf->ssf_type = SCTP_SEND_FAILED;
2971 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
2972 ssf->ssf_flags = SCTP_DATA_UNSENT;
2974 ssf->ssf_flags = SCTP_DATA_SENT;
2975 ssf->ssf_length = length;
2976 ssf->ssf_error = error;
2977 /* not exactly what the user sent in, but should be close :) */
2978 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
2979 ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
2980 ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
2981 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
2982 ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
2983 ssf->ssf_info.sinfo_context = chk->rec.data.context;
2984 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2985 ssf->ssf_assoc_id = sctp_get_associd(stcb);
2987 SCTP_BUF_NEXT(m_notify) = chk->data;
2988 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
2991 * trim off the sctp chunk header(it should be there)
2993 if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
2994 m_adj(chk->data, sizeof(struct sctp_data_chunk));
2995 sctp_mbuf_crush(chk->data);
2996 chk->send_size -= sizeof(struct sctp_data_chunk);
2999 /* Steal off the mbuf */
3002 * For this case, we check the actual socket buffer, since the assoc
3003 * is going away we don't want to overfill the socket buffer for a
3006 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3007 sctp_m_freem(m_notify);
3010 /* append to socket */
3011 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3014 if (control == NULL) {
3016 sctp_m_freem(m_notify);
3019 control->spec_flags = M_NOTIFICATION;
3020 sctp_add_to_readq(stcb->sctp_ep, stcb,
3022 &stcb->sctp_socket->so_rcv, 1,
3023 SCTP_READ_LOCK_NOT_HELD,
3029 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3030 struct sctp_stream_queue_pending *sp, int so_locked
3031 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3036 struct mbuf *m_notify;
3037 struct sctp_send_failed *ssf;
3038 struct sctp_queued_to_read *control;
3041 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)) {
3042 /* event not enabled */
3045 length = sizeof(struct sctp_send_failed) + sp->length;
3046 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 0, M_DONTWAIT, 1, MT_DATA);
3047 if (m_notify == NULL)
3050 SCTP_BUF_LEN(m_notify) = 0;
3051 ssf = mtod(m_notify, struct sctp_send_failed *);
3052 ssf->ssf_type = SCTP_SEND_FAILED;
3053 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
3054 ssf->ssf_flags = SCTP_DATA_UNSENT;
3056 ssf->ssf_flags = SCTP_DATA_SENT;
3057 ssf->ssf_length = length;
3058 ssf->ssf_error = error;
3059 /* not exactly what the user sent in, but should be close :) */
3060 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
3061 ssf->ssf_info.sinfo_stream = sp->stream;
3062 ssf->ssf_info.sinfo_ssn = sp->strseq;
3063 if (sp->some_taken) {
3064 ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3066 ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3068 ssf->ssf_info.sinfo_ppid = sp->ppid;
3069 ssf->ssf_info.sinfo_context = sp->context;
3070 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3071 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3072 SCTP_BUF_NEXT(m_notify) = sp->data;
3073 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
3075 /* Steal off the mbuf */
3078 * For this case, we check the actual socket buffer, since the assoc
3079 * is going away we don't want to overfill the socket buffer for a
3082 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3083 sctp_m_freem(m_notify);
3086 /* append to socket */
3087 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3090 if (control == NULL) {
3092 sctp_m_freem(m_notify);
3095 control->spec_flags = M_NOTIFICATION;
3096 sctp_add_to_readq(stcb->sctp_ep, stcb,
3098 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3104 sctp_notify_adaptation_layer(struct sctp_tcb *stcb,
3107 struct mbuf *m_notify;
3108 struct sctp_adaptation_event *sai;
3109 struct sctp_queued_to_read *control;
3111 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3112 /* event not enabled */
3115 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_DONTWAIT, 1, MT_DATA);
3116 if (m_notify == NULL)
3119 SCTP_BUF_LEN(m_notify) = 0;
3120 sai = mtod(m_notify, struct sctp_adaptation_event *);
3121 sai->sai_type = SCTP_ADAPTATION_INDICATION;
3123 sai->sai_length = sizeof(struct sctp_adaptation_event);
3124 sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3125 sai->sai_assoc_id = sctp_get_associd(stcb);
3127 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3128 SCTP_BUF_NEXT(m_notify) = NULL;
3130 /* append to socket */
3131 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3134 if (control == NULL) {
3136 sctp_m_freem(m_notify);
3139 control->length = SCTP_BUF_LEN(m_notify);
3140 control->spec_flags = M_NOTIFICATION;
3141 /* not that we need this */
3142 control->tail_mbuf = m_notify;
3143 sctp_add_to_readq(stcb->sctp_ep, stcb,
3145 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3148 /* This always must be called with the read-queue LOCKED in the INP */
3150 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3151 int nolock, uint32_t val)
3153 struct mbuf *m_notify;
3154 struct sctp_pdapi_event *pdapi;
3155 struct sctp_queued_to_read *control;
3158 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3159 /* event not enabled */
3162 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_DONTWAIT, 1, MT_DATA);
3163 if (m_notify == NULL)
3166 SCTP_BUF_LEN(m_notify) = 0;
3167 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3168 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3169 pdapi->pdapi_flags = 0;
3170 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3171 pdapi->pdapi_indication = error;
3172 pdapi->pdapi_stream = (val >> 16);
3173 pdapi->pdapi_seq = (val & 0x0000ffff);
3174 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3176 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3177 SCTP_BUF_NEXT(m_notify) = NULL;
3178 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3181 if (control == NULL) {
3183 sctp_m_freem(m_notify);
3186 control->spec_flags = M_NOTIFICATION;
3187 control->length = SCTP_BUF_LEN(m_notify);
3188 /* not that we need this */
3189 control->tail_mbuf = m_notify;
3190 control->held_length = 0;
3191 control->length = 0;
3193 SCTP_INP_READ_LOCK(stcb->sctp_ep);
3195 sb = &stcb->sctp_socket->so_rcv;
3196 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3197 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
3199 sctp_sballoc(stcb, sb, m_notify);
3200 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3201 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3203 atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify));
3204 control->end_added = 1;
3205 if (stcb->asoc.control_pdapi)
3206 TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
3208 /* we really should not see this case */
3209 TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
3212 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
3214 if (stcb->sctp_ep && stcb->sctp_socket) {
3215 /* This should always be the case */
3216 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3221 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3223 struct mbuf *m_notify;
3224 struct sctp_shutdown_event *sse;
3225 struct sctp_queued_to_read *control;
3228 * For TCP model AND UDP connected sockets we will send an error up
3229 * when an SHUTDOWN completes
3231 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3232 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3233 /* mark socket closed for read/write and wakeup! */
3234 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3237 so = SCTP_INP_SO(stcb->sctp_ep);
3238 atomic_add_int(&stcb->asoc.refcnt, 1);
3239 SCTP_TCB_UNLOCK(stcb);
3240 SCTP_SOCKET_LOCK(so, 1);
3241 SCTP_TCB_LOCK(stcb);
3242 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3243 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3244 SCTP_SOCKET_UNLOCK(so, 1);
3248 socantsendmore(stcb->sctp_socket);
3249 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3250 SCTP_SOCKET_UNLOCK(so, 1);
3253 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3254 /* event not enabled */
3257 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_DONTWAIT, 1, MT_DATA);
3258 if (m_notify == NULL)
3261 sse = mtod(m_notify, struct sctp_shutdown_event *);
3262 sse->sse_type = SCTP_SHUTDOWN_EVENT;
3264 sse->sse_length = sizeof(struct sctp_shutdown_event);
3265 sse->sse_assoc_id = sctp_get_associd(stcb);
3267 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3268 SCTP_BUF_NEXT(m_notify) = NULL;
3270 /* append to socket */
3271 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3274 if (control == NULL) {
3276 sctp_m_freem(m_notify);
3279 control->spec_flags = M_NOTIFICATION;
3280 control->length = SCTP_BUF_LEN(m_notify);
3281 /* not that we need this */
3282 control->tail_mbuf = m_notify;
3283 sctp_add_to_readq(stcb->sctp_ep, stcb,
3285 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3289 sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3291 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3296 struct mbuf *m_notify;
3297 struct sctp_sender_dry_event *event;
3298 struct sctp_queued_to_read *control;
3300 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_DRYEVNT)) {
3301 /* event not enabled */
3304 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_DONTWAIT, 1, MT_DATA);
3305 if (m_notify == NULL) {
3309 SCTP_BUF_LEN(m_notify) = 0;
3310 event = mtod(m_notify, struct sctp_sender_dry_event *);
3311 event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3312 event->sender_dry_flags = 0;
3313 event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3314 event->sender_dry_assoc_id = sctp_get_associd(stcb);
3316 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3317 SCTP_BUF_NEXT(m_notify) = NULL;
3319 /* append to socket */
3320 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3321 0, 0, 0, 0, 0, 0, m_notify);
3322 if (control == NULL) {
3324 sctp_m_freem(m_notify);
3327 control->length = SCTP_BUF_LEN(m_notify);
3328 control->spec_flags = M_NOTIFICATION;
3329 /* not that we need this */
3330 control->tail_mbuf = m_notify;
3331 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3332 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3337 sctp_notify_stream_reset_add(struct sctp_tcb *stcb, int number_entries, int flag)
3339 struct mbuf *m_notify;
3340 struct sctp_queued_to_read *control;
3341 struct sctp_stream_reset_event *strreset;
3344 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
3345 /* event not enabled */
3348 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3349 if (m_notify == NULL)
3352 SCTP_BUF_LEN(m_notify) = 0;
3353 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3354 if (len > M_TRAILINGSPACE(m_notify)) {
3355 /* never enough room */
3356 sctp_m_freem(m_notify);
3359 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3360 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3361 strreset->strreset_flags = SCTP_STRRESET_ADD_STREAM | flag;
3362 strreset->strreset_length = len;
3363 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3364 strreset->strreset_list[0] = number_entries;
3366 SCTP_BUF_LEN(m_notify) = len;
3367 SCTP_BUF_NEXT(m_notify) = NULL;
3368 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3370 sctp_m_freem(m_notify);
3373 /* append to socket */
3374 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3377 if (control == NULL) {
3379 sctp_m_freem(m_notify);
3382 control->spec_flags = M_NOTIFICATION;
3383 control->length = SCTP_BUF_LEN(m_notify);
3384 /* not that we need this */
3385 control->tail_mbuf = m_notify;
3386 sctp_add_to_readq(stcb->sctp_ep, stcb,
3388 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3393 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3394 int number_entries, uint16_t * list, int flag)
3396 struct mbuf *m_notify;
3397 struct sctp_queued_to_read *control;
3398 struct sctp_stream_reset_event *strreset;
3401 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
3402 /* event not enabled */
3405 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3406 if (m_notify == NULL)
3409 SCTP_BUF_LEN(m_notify) = 0;
3410 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3411 if (len > M_TRAILINGSPACE(m_notify)) {
3412 /* never enough room */
3413 sctp_m_freem(m_notify);
3416 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3417 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3418 if (number_entries == 0) {
3419 strreset->strreset_flags = flag | SCTP_STRRESET_ALL_STREAMS;
3421 strreset->strreset_flags = flag | SCTP_STRRESET_STREAM_LIST;
3423 strreset->strreset_length = len;
3424 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3425 if (number_entries) {
3428 for (i = 0; i < number_entries; i++) {
3429 strreset->strreset_list[i] = ntohs(list[i]);
3432 SCTP_BUF_LEN(m_notify) = len;
3433 SCTP_BUF_NEXT(m_notify) = NULL;
3434 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3436 sctp_m_freem(m_notify);
3439 /* append to socket */
3440 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3443 if (control == NULL) {
3445 sctp_m_freem(m_notify);
3448 control->spec_flags = M_NOTIFICATION;
3449 control->length = SCTP_BUF_LEN(m_notify);
3450 /* not that we need this */
3451 control->tail_mbuf = m_notify;
3452 sctp_add_to_readq(stcb->sctp_ep, stcb,
3454 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3459 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3460 uint32_t error, void *data, int so_locked
3461 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3466 if ((stcb == NULL) ||
3467 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3468 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3469 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3470 /* If the socket is gone we are out of here */
3473 if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3476 if (stcb && ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3477 (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED))) {
3478 if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
3479 (notification == SCTP_NOTIFY_INTERFACE_UP) ||
3480 (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
3481 /* Don't report these in front states */
3485 switch (notification) {
3486 case SCTP_NOTIFY_ASSOC_UP:
3487 if (stcb->asoc.assoc_up_sent == 0) {
3488 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, so_locked);
3489 stcb->asoc.assoc_up_sent = 1;
3491 if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
3492 sctp_notify_adaptation_layer(stcb, error);
3494 if (stcb->asoc.peer_supports_auth == 0) {
3495 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3499 case SCTP_NOTIFY_ASSOC_DOWN:
3500 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, so_locked);
3502 case SCTP_NOTIFY_INTERFACE_DOWN:
3504 struct sctp_nets *net;
3506 net = (struct sctp_nets *)data;
3507 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3508 (struct sockaddr *)&net->ro._l_addr, error);
3511 case SCTP_NOTIFY_INTERFACE_UP:
3513 struct sctp_nets *net;
3515 net = (struct sctp_nets *)data;
3516 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3517 (struct sockaddr *)&net->ro._l_addr, error);
3520 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3522 struct sctp_nets *net;
3524 net = (struct sctp_nets *)data;
3525 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3526 (struct sockaddr *)&net->ro._l_addr, error);
3529 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3530 sctp_notify_send_failed2(stcb, error,
3531 (struct sctp_stream_queue_pending *)data, so_locked);
3533 case SCTP_NOTIFY_DG_FAIL:
3534 sctp_notify_send_failed(stcb, error,
3535 (struct sctp_tmit_chunk *)data, so_locked);
3537 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3541 val = *((uint32_t *) data);
3543 sctp_notify_partial_delivery_indication(stcb, error, 0, val);
3546 case SCTP_NOTIFY_STRDATA_ERR:
3548 case SCTP_NOTIFY_ASSOC_ABORTED:
3549 if ((stcb) && (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3550 ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED))) {
3551 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, NULL, so_locked);
3553 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, NULL, so_locked);
3556 case SCTP_NOTIFY_PEER_OPENED_STREAM:
3558 case SCTP_NOTIFY_STREAM_OPENED_OK:
3560 case SCTP_NOTIFY_ASSOC_RESTART:
3561 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, data, so_locked);
3562 if (stcb->asoc.peer_supports_auth == 0) {
3563 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3567 case SCTP_NOTIFY_HB_RESP:
3569 case SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK:
3570 sctp_notify_stream_reset_add(stcb, error, SCTP_STRRESET_INBOUND_STR);
3572 case SCTP_NOTIFY_STR_RESET_ADD_OK:
3573 sctp_notify_stream_reset_add(stcb, error, SCTP_STRRESET_OUTBOUND_STR);
3575 case SCTP_NOTIFY_STR_RESET_ADD_FAIL:
3576 sctp_notify_stream_reset_add(stcb, error, (SCTP_STRRESET_FAILED | SCTP_STRRESET_OUTBOUND_STR));
3579 case SCTP_NOTIFY_STR_RESET_SEND:
3580 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_OUTBOUND_STR);
3582 case SCTP_NOTIFY_STR_RESET_RECV:
3583 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_INBOUND_STR);
3585 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3586 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_OUTBOUND_STR | SCTP_STRRESET_FAILED));
3589 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3590 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_INBOUND_STR | SCTP_STRRESET_FAILED));
3593 case SCTP_NOTIFY_ASCONF_ADD_IP:
3594 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3597 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3598 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3601 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3602 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3605 case SCTP_NOTIFY_ASCONF_SUCCESS:
3607 case SCTP_NOTIFY_ASCONF_FAILED:
3609 case SCTP_NOTIFY_PEER_SHUTDOWN:
3610 sctp_notify_shutdown_event(stcb);
3612 case SCTP_NOTIFY_AUTH_NEW_KEY:
3613 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY, error,
3614 (uint16_t) (uintptr_t) data,
3617 case SCTP_NOTIFY_AUTH_FREE_KEY:
3618 sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3619 (uint16_t) (uintptr_t) data,
3622 case SCTP_NOTIFY_NO_PEER_AUTH:
3623 sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3624 (uint16_t) (uintptr_t) data,
3627 case SCTP_NOTIFY_SENDER_DRY:
3628 sctp_notify_sender_dry_event(stcb, so_locked);
3631 SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3632 __FUNCTION__, notification, notification);
3638 sctp_report_all_outbound(struct sctp_tcb *stcb, int holds_lock, int so_locked
3639 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3644 struct sctp_association *asoc;
3645 struct sctp_stream_out *outs;
3646 struct sctp_tmit_chunk *chk;
3647 struct sctp_stream_queue_pending *sp;
3655 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3656 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3657 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3660 /* now through all the gunk freeing chunks */
3661 if (holds_lock == 0) {
3662 SCTP_TCB_SEND_LOCK(stcb);
3664 /* sent queue SHOULD be empty */
3665 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3666 chk = TAILQ_FIRST(&asoc->sent_queue);
3668 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3669 asoc->sent_queue_cnt--;
3670 if (chk->data != NULL) {
3671 sctp_free_bufspace(stcb, asoc, chk, 1);
3672 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3673 SCTP_NOTIFY_DATAGRAM_SENT, chk, so_locked);
3674 sctp_m_freem(chk->data);
3677 sctp_free_a_chunk(stcb, chk);
3678 /* sa_ignore FREED_MEMORY */
3679 chk = TAILQ_FIRST(&asoc->sent_queue);
3682 /* pending send queue SHOULD be empty */
3683 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3684 chk = TAILQ_FIRST(&asoc->send_queue);
3686 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3687 asoc->send_queue_cnt--;
3688 if (chk->data != NULL) {
3689 sctp_free_bufspace(stcb, asoc, chk, 1);
3690 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3691 SCTP_NOTIFY_DATAGRAM_UNSENT, chk, so_locked);
3692 sctp_m_freem(chk->data);
3695 sctp_free_a_chunk(stcb, chk);
3696 /* sa_ignore FREED_MEMORY */
3697 chk = TAILQ_FIRST(&asoc->send_queue);
3700 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3701 /* For each stream */
3702 outs = &stcb->asoc.strmout[i];
3703 /* clean up any sends there */
3704 stcb->asoc.locked_on_sending = NULL;
3705 sp = TAILQ_FIRST(&outs->outqueue);
3707 stcb->asoc.stream_queue_cnt--;
3708 TAILQ_REMOVE(&outs->outqueue, sp, next);
3709 sctp_free_spbufspace(stcb, asoc, sp);
3710 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3711 SCTP_NOTIFY_DATAGRAM_UNSENT, (void *)sp, so_locked);
3713 sctp_m_freem(sp->data);
3717 sctp_free_remote_addr(sp->net);
3719 /* Free the chunk */
3720 sctp_free_a_strmoq(stcb, sp);
3721 /* sa_ignore FREED_MEMORY */
3722 sp = TAILQ_FIRST(&outs->outqueue);
3726 if (holds_lock == 0) {
3727 SCTP_TCB_SEND_UNLOCK(stcb);
3732 sctp_abort_notification(struct sctp_tcb *stcb, int error, int so_locked
3733 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3742 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3743 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3744 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3747 /* Tell them we lost the asoc */
3748 sctp_report_all_outbound(stcb, 1, so_locked);
3749 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3750 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3751 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3752 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3754 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_ABORTED, stcb, error, NULL, so_locked);
3758 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3759 struct mbuf *m, int iphlen, struct sctphdr *sh, struct mbuf *op_err,
3760 uint32_t vrf_id, uint16_t port)
3764 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3771 /* We have a TCB to abort, send notification too */
3772 vtag = stcb->asoc.peer_vtag;
3773 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
3774 /* get the assoc vrf id and table id */
3775 vrf_id = stcb->asoc.vrf_id;
3776 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3778 sctp_send_abort(m, iphlen, sh, vtag, op_err, vrf_id, port);
3780 /* Ok, now lets free it */
3781 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3782 so = SCTP_INP_SO(inp);
3783 atomic_add_int(&stcb->asoc.refcnt, 1);
3784 SCTP_TCB_UNLOCK(stcb);
3785 SCTP_SOCKET_LOCK(so, 1);
3786 SCTP_TCB_LOCK(stcb);
3787 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3789 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
3790 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3791 SCTP_SOCKET_UNLOCK(so, 1);
3794 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3795 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3796 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3797 SCTP_CALLED_DIRECTLY_NOCMPSET);
3803 #ifdef SCTP_ASOCLOG_OF_TSNS
3805 sctp_print_out_track_log(struct sctp_tcb *stcb)
3810 SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
3811 SCTP_PRINTF("IN bound TSN log-aaa\n");
3812 if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
3813 SCTP_PRINTF("None rcvd\n");
3816 if (stcb->asoc.tsn_in_wrapped) {
3817 for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
3818 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3819 stcb->asoc.in_tsnlog[i].tsn,
3820 stcb->asoc.in_tsnlog[i].strm,
3821 stcb->asoc.in_tsnlog[i].seq,
3822 stcb->asoc.in_tsnlog[i].flgs,
3823 stcb->asoc.in_tsnlog[i].sz);
3826 if (stcb->asoc.tsn_in_at) {
3827 for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
3828 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3829 stcb->asoc.in_tsnlog[i].tsn,
3830 stcb->asoc.in_tsnlog[i].strm,
3831 stcb->asoc.in_tsnlog[i].seq,
3832 stcb->asoc.in_tsnlog[i].flgs,
3833 stcb->asoc.in_tsnlog[i].sz);
3837 SCTP_PRINTF("OUT bound TSN log-aaa\n");
3838 if ((stcb->asoc.tsn_out_at == 0) &&
3839 (stcb->asoc.tsn_out_wrapped == 0)) {
3840 SCTP_PRINTF("None sent\n");
3842 if (stcb->asoc.tsn_out_wrapped) {
3843 for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
3844 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3845 stcb->asoc.out_tsnlog[i].tsn,
3846 stcb->asoc.out_tsnlog[i].strm,
3847 stcb->asoc.out_tsnlog[i].seq,
3848 stcb->asoc.out_tsnlog[i].flgs,
3849 stcb->asoc.out_tsnlog[i].sz);
3852 if (stcb->asoc.tsn_out_at) {
3853 for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
3854 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3855 stcb->asoc.out_tsnlog[i].tsn,
3856 stcb->asoc.out_tsnlog[i].strm,
3857 stcb->asoc.out_tsnlog[i].seq,
3858 stcb->asoc.out_tsnlog[i].flgs,
3859 stcb->asoc.out_tsnlog[i].sz);
3868 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3869 int error, struct mbuf *op_err,
3871 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3878 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3883 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3884 so = SCTP_INP_SO(inp);
3887 /* Got to have a TCB */
3888 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3889 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3890 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3891 SCTP_CALLED_DIRECTLY_NOCMPSET);
3896 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3898 vtag = stcb->asoc.peer_vtag;
3899 /* notify the ulp */
3900 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0)
3901 sctp_abort_notification(stcb, error, so_locked);
3902 /* notify the peer */
3903 #if defined(SCTP_PANIC_ON_ABORT)
3904 panic("aborting an association");
3906 sctp_send_abort_tcb(stcb, op_err, so_locked);
3907 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3908 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3909 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3910 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3912 /* now free the asoc */
3913 #ifdef SCTP_ASOCLOG_OF_TSNS
3914 sctp_print_out_track_log(stcb);
3916 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3918 atomic_add_int(&stcb->asoc.refcnt, 1);
3919 SCTP_TCB_UNLOCK(stcb);
3920 SCTP_SOCKET_LOCK(so, 1);
3921 SCTP_TCB_LOCK(stcb);
3922 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3925 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
3926 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3928 SCTP_SOCKET_UNLOCK(so, 1);
3934 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
3935 struct sctp_inpcb *inp, struct mbuf *op_err, uint32_t vrf_id, uint16_t port)
3937 struct sctp_chunkhdr *ch, chunk_buf;
3938 unsigned int chk_length;
3940 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
3941 /* Generate a TO address for future reference */
3942 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3943 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3944 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3945 SCTP_CALLED_DIRECTLY_NOCMPSET);
3948 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3949 sizeof(*ch), (uint8_t *) & chunk_buf);
3950 while (ch != NULL) {
3951 chk_length = ntohs(ch->chunk_length);
3952 if (chk_length < sizeof(*ch)) {
3953 /* break to abort land */
3956 switch (ch->chunk_type) {
3957 case SCTP_COOKIE_ECHO:
3958 /* We hit here only if the assoc is being freed */
3960 case SCTP_PACKET_DROPPED:
3961 /* we don't respond to pkt-dropped */
3963 case SCTP_ABORT_ASSOCIATION:
3964 /* we don't respond with an ABORT to an ABORT */
3966 case SCTP_SHUTDOWN_COMPLETE:
3968 * we ignore it since we are not waiting for it and
3972 case SCTP_SHUTDOWN_ACK:
3973 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
3978 offset += SCTP_SIZE32(chk_length);
3979 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3980 sizeof(*ch), (uint8_t *) & chunk_buf);
3982 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
3986 * check the inbound datagram to make sure there is not an abort inside it,
3987 * if there is return 1, else return 0.
3990 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
3992 struct sctp_chunkhdr *ch;
3993 struct sctp_init_chunk *init_chk, chunk_buf;
3995 unsigned int chk_length;
3997 offset = iphlen + sizeof(struct sctphdr);
3998 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
3999 (uint8_t *) & chunk_buf);
4000 while (ch != NULL) {
4001 chk_length = ntohs(ch->chunk_length);
4002 if (chk_length < sizeof(*ch)) {
4003 /* packet is probably corrupt */
4006 /* we seem to be ok, is it an abort? */
4007 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4008 /* yep, tell them */
4011 if (ch->chunk_type == SCTP_INITIATION) {
4012 /* need to update the Vtag */
4013 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4014 offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
4015 if (init_chk != NULL) {
4016 *vtagfill = ntohl(init_chk->init.initiate_tag);
4019 /* Nope, move to the next chunk */
4020 offset += SCTP_SIZE32(chk_length);
4021 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4022 sizeof(*ch), (uint8_t *) & chunk_buf);
4028 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4029 * set (i.e. it's 0) so, create this function to compare link local scopes
4033 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4035 struct sockaddr_in6 a, b;
4041 if (a.sin6_scope_id == 0)
4042 if (sa6_recoverscope(&a)) {
4043 /* can't get scope, so can't match */
4046 if (b.sin6_scope_id == 0)
4047 if (sa6_recoverscope(&b)) {
4048 /* can't get scope, so can't match */
4051 if (a.sin6_scope_id != b.sin6_scope_id)
4058 * returns a sockaddr_in6 with embedded scope recovered and removed
4060 struct sockaddr_in6 *
4061 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4063 /* check and strip embedded scope junk */
4064 if (addr->sin6_family == AF_INET6) {
4065 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4066 if (addr->sin6_scope_id == 0) {
4068 if (!sa6_recoverscope(store)) {
4069 /* use the recovered scope */
4073 /* else, return the original "to" addr */
4074 in6_clearscope(&addr->sin6_addr);
4084 * are the two addresses the same? currently a "scopeless" check returns: 1
4088 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4092 if (sa1 == NULL || sa2 == NULL)
4095 /* must be the same family */
4096 if (sa1->sa_family != sa2->sa_family)
4099 switch (sa1->sa_family) {
4103 /* IPv6 addresses */
4104 struct sockaddr_in6 *sin6_1, *sin6_2;
4106 sin6_1 = (struct sockaddr_in6 *)sa1;
4107 sin6_2 = (struct sockaddr_in6 *)sa2;
4108 return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4114 /* IPv4 addresses */
4115 struct sockaddr_in *sin_1, *sin_2;
4117 sin_1 = (struct sockaddr_in *)sa1;
4118 sin_2 = (struct sockaddr_in *)sa2;
4119 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
4122 /* we don't do these... */
4128 sctp_print_address(struct sockaddr *sa)
4131 char ip6buf[INET6_ADDRSTRLEN];
4136 switch (sa->sa_family) {
4140 struct sockaddr_in6 *sin6;
4142 sin6 = (struct sockaddr_in6 *)sa;
4143 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
4144 ip6_sprintf(ip6buf, &sin6->sin6_addr),
4145 ntohs(sin6->sin6_port),
4146 sin6->sin6_scope_id);
4152 struct sockaddr_in *sin;
4155 sin = (struct sockaddr_in *)sa;
4156 p = (unsigned char *)&sin->sin_addr;
4157 SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4158 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
4168 sctp_print_address_pkt(struct ip *iph, struct sctphdr *sh)
4170 switch (iph->ip_v) {
4173 struct sockaddr_in lsa, fsa;
4175 bzero(&lsa, sizeof(lsa));
4176 lsa.sin_len = sizeof(lsa);
4177 lsa.sin_family = AF_INET;
4178 lsa.sin_addr = iph->ip_src;
4179 lsa.sin_port = sh->src_port;
4180 bzero(&fsa, sizeof(fsa));
4181 fsa.sin_len = sizeof(fsa);
4182 fsa.sin_family = AF_INET;
4183 fsa.sin_addr = iph->ip_dst;
4184 fsa.sin_port = sh->dest_port;
4185 SCTP_PRINTF("src: ");
4186 sctp_print_address((struct sockaddr *)&lsa);
4187 SCTP_PRINTF("dest: ");
4188 sctp_print_address((struct sockaddr *)&fsa);
4192 case IPV6_VERSION >> 4:
4194 struct ip6_hdr *ip6;
4195 struct sockaddr_in6 lsa6, fsa6;
4197 ip6 = (struct ip6_hdr *)iph;
4198 bzero(&lsa6, sizeof(lsa6));
4199 lsa6.sin6_len = sizeof(lsa6);
4200 lsa6.sin6_family = AF_INET6;
4201 lsa6.sin6_addr = ip6->ip6_src;
4202 lsa6.sin6_port = sh->src_port;
4203 bzero(&fsa6, sizeof(fsa6));
4204 fsa6.sin6_len = sizeof(fsa6);
4205 fsa6.sin6_family = AF_INET6;
4206 fsa6.sin6_addr = ip6->ip6_dst;
4207 fsa6.sin6_port = sh->dest_port;
4208 SCTP_PRINTF("src: ");
4209 sctp_print_address((struct sockaddr *)&lsa6);
4210 SCTP_PRINTF("dest: ");
4211 sctp_print_address((struct sockaddr *)&fsa6);
4222 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4223 struct sctp_inpcb *new_inp,
4224 struct sctp_tcb *stcb,
4228 * go through our old INP and pull off any control structures that
4229 * belong to stcb and move then to the new inp.
4231 struct socket *old_so, *new_so;
4232 struct sctp_queued_to_read *control, *nctl;
4233 struct sctp_readhead tmp_queue;
4237 old_so = old_inp->sctp_socket;
4238 new_so = new_inp->sctp_socket;
4239 TAILQ_INIT(&tmp_queue);
4240 error = sblock(&old_so->so_rcv, waitflags);
4243 * Gak, can't get sblock, we have a problem. data will be
4244 * left stranded.. and we don't dare look at it since the
4245 * other thread may be reading something. Oh well, its a
4246 * screwed up app that does a peeloff OR a accept while
4247 * reading from the main socket... actually its only the
4248 * peeloff() case, since I think read will fail on a
4249 * listening socket..
4253 /* lock the socket buffers */
4254 SCTP_INP_READ_LOCK(old_inp);
4255 control = TAILQ_FIRST(&old_inp->read_queue);
4256 /* Pull off all for out target stcb */
4258 nctl = TAILQ_NEXT(control, next);
4259 if (control->stcb == stcb) {
4260 /* remove it we want it */
4261 TAILQ_REMOVE(&old_inp->read_queue, control, next);
4262 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4265 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4266 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
4268 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4269 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4270 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4272 m = SCTP_BUF_NEXT(m);
4277 SCTP_INP_READ_UNLOCK(old_inp);
4278 /* Remove the sb-lock on the old socket */
4280 sbunlock(&old_so->so_rcv);
4281 /* Now we move them over to the new socket buffer */
4282 control = TAILQ_FIRST(&tmp_queue);
4283 SCTP_INP_READ_LOCK(new_inp);
4285 nctl = TAILQ_NEXT(control, next);
4286 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4289 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4290 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4292 sctp_sballoc(stcb, &new_so->so_rcv, m);
4293 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4294 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4296 m = SCTP_BUF_NEXT(m);
4300 SCTP_INP_READ_UNLOCK(new_inp);
4304 sctp_add_to_readq(struct sctp_inpcb *inp,
4305 struct sctp_tcb *stcb,
4306 struct sctp_queued_to_read *control,
4309 int inp_read_lock_held,
4311 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4317 * Here we must place the control on the end of the socket read
4318 * queue AND increment sb_cc so that select will work properly on
4321 struct mbuf *m, *prev = NULL;
4326 panic("Gak, inp NULL on add_to_readq");
4330 if (inp_read_lock_held == 0)
4331 SCTP_INP_READ_LOCK(inp);
4332 if (!(control->spec_flags & M_NOTIFICATION)) {
4333 atomic_add_int(&inp->total_recvs, 1);
4334 if (!control->do_not_ref_stcb) {
4335 atomic_add_int(&stcb->total_recvs, 1);
4339 control->held_length = 0;
4340 control->length = 0;
4342 if (SCTP_BUF_LEN(m) == 0) {
4343 /* Skip mbufs with NO length */
4346 control->data = sctp_m_free(m);
4349 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4350 m = SCTP_BUF_NEXT(prev);
4353 control->tail_mbuf = prev;;
4358 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4359 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4361 sctp_sballoc(stcb, sb, m);
4362 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4363 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4365 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4366 m = SCTP_BUF_NEXT(m);
4369 control->tail_mbuf = prev;
4371 /* Everything got collapsed out?? */
4372 if (inp_read_lock_held == 0)
4373 SCTP_INP_READ_UNLOCK(inp);
4377 control->end_added = 1;
4379 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4380 if (inp_read_lock_held == 0)
4381 SCTP_INP_READ_UNLOCK(inp);
4382 if (inp && inp->sctp_socket) {
4383 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
4384 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4386 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4389 so = SCTP_INP_SO(inp);
4391 atomic_add_int(&stcb->asoc.refcnt, 1);
4392 SCTP_TCB_UNLOCK(stcb);
4393 SCTP_SOCKET_LOCK(so, 1);
4394 SCTP_TCB_LOCK(stcb);
4395 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4396 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4397 SCTP_SOCKET_UNLOCK(so, 1);
4402 sctp_sorwakeup(inp, inp->sctp_socket);
4403 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4405 SCTP_SOCKET_UNLOCK(so, 1);
4414 sctp_append_to_readq(struct sctp_inpcb *inp,
4415 struct sctp_tcb *stcb,
4416 struct sctp_queued_to_read *control,
4423 * A partial delivery API event is underway. OR we are appending on
4424 * the reassembly queue.
4426 * If PDAPI this means we need to add m to the end of the data.
4427 * Increase the length in the control AND increment the sb_cc.
4428 * Otherwise sb is NULL and all we need to do is put it at the end
4429 * of the mbuf chain.
4432 struct mbuf *mm, *tail = NULL, *prev = NULL;
4435 SCTP_INP_READ_LOCK(inp);
4437 if (control == NULL) {
4440 SCTP_INP_READ_UNLOCK(inp);
4444 if (control->end_added) {
4445 /* huh this one is complete? */
4453 if (SCTP_BUF_LEN(mm) == 0) {
4454 /* Skip mbufs with NO lenght */
4457 m = sctp_m_free(mm);
4460 SCTP_BUF_NEXT(prev) = sctp_m_free(mm);
4461 mm = SCTP_BUF_NEXT(prev);
4466 len += SCTP_BUF_LEN(mm);
4468 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4469 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm));
4471 sctp_sballoc(stcb, sb, mm);
4472 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4473 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4476 mm = SCTP_BUF_NEXT(mm);
4481 /* Really there should always be a prev */
4483 /* Huh nothing left? */
4485 panic("Nothing left to add?");
4492 if (control->tail_mbuf) {
4494 SCTP_BUF_NEXT(control->tail_mbuf) = m;
4495 control->tail_mbuf = tail;
4499 if (control->data != NULL) {
4500 panic("This should NOT happen");
4504 control->tail_mbuf = tail;
4506 atomic_add_int(&control->length, len);
4508 /* message is complete */
4509 if (stcb && (control == stcb->asoc.control_pdapi)) {
4510 stcb->asoc.control_pdapi = NULL;
4512 control->held_length = 0;
4513 control->end_added = 1;
4516 control->do_not_ref_stcb = 1;
4519 * When we are appending in partial delivery, the cum-ack is used
4520 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
4521 * is populated in the outbound sinfo structure from the true cumack
4522 * if the association exists...
4524 control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
4526 SCTP_INP_READ_UNLOCK(inp);
4528 if (inp && inp->sctp_socket) {
4529 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
4530 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4532 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4535 so = SCTP_INP_SO(inp);
4536 atomic_add_int(&stcb->asoc.refcnt, 1);
4537 SCTP_TCB_UNLOCK(stcb);
4538 SCTP_SOCKET_LOCK(so, 1);
4539 SCTP_TCB_LOCK(stcb);
4540 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4541 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4542 SCTP_SOCKET_UNLOCK(so, 1);
4546 sctp_sorwakeup(inp, inp->sctp_socket);
4547 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4548 SCTP_SOCKET_UNLOCK(so, 1);
4557 /*************HOLD THIS COMMENT FOR PATCH FILE OF
4558 *************ALTERNATE ROUTING CODE
4561 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4562 *************ALTERNATE ROUTING CODE
4566 sctp_generate_invmanparam(int err)
4568 /* Return a MBUF with a invalid mandatory parameter */
4571 m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
4573 struct sctp_paramhdr *ph;
4575 SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
4576 ph = mtod(m, struct sctp_paramhdr *);
4577 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4578 ph->param_type = htons(err);
4583 #ifdef SCTP_MBCNT_LOGGING
4585 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4586 struct sctp_tmit_chunk *tp1, int chk_cnt)
4588 if (tp1->data == NULL) {
4591 asoc->chunks_on_out_queue -= chk_cnt;
4592 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4593 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4594 asoc->total_output_queue_size,
4599 if (asoc->total_output_queue_size >= tp1->book_size) {
4600 atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4602 asoc->total_output_queue_size = 0;
4605 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4606 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4607 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4608 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4610 stcb->sctp_socket->so_snd.sb_cc = 0;
4619 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4620 int reason, int so_locked
4621 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4626 struct sctp_stream_out *strq;
4627 struct sctp_tmit_chunk *chk = NULL;
4628 struct sctp_stream_queue_pending *sp;
4629 uint16_t stream = 0, seq = 0;
4630 uint8_t foundeom = 0;
4633 int do_wakeup_routine = 0;
4635 stream = tp1->rec.data.stream_number;
4636 seq = tp1->rec.data.stream_seq;
4638 ret_sz += tp1->book_size;
4639 if (tp1->data != NULL) {
4640 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4641 sctp_flight_size_decrease(tp1);
4642 sctp_total_flight_decrease(stcb, tp1);
4644 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4645 stcb->asoc.peers_rwnd += tp1->send_size;
4646 stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
4647 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, reason, tp1, so_locked);
4648 sctp_m_freem(tp1->data);
4650 do_wakeup_routine = 1;
4651 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4652 stcb->asoc.sent_queue_cnt_removeable--;
4655 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4656 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4657 SCTP_DATA_NOT_FRAG) {
4658 /* not frag'ed we ae done */
4661 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4662 /* end of frag, we are done */
4667 * Its a begin or middle piece, we must mark all of
4671 tp1 = TAILQ_NEXT(tp1, sctp_next);
4673 } while (tp1 && notdone);
4674 if (foundeom == 0) {
4676 * The multi-part message was scattered across the send and
4680 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
4682 * recurse throught the send_queue too, starting at the
4686 (tp1->rec.data.stream_number == stream) &&
4687 (tp1->rec.data.stream_seq == seq)
4690 * save to chk in case we have some on stream out
4691 * queue. If so and we have an un-transmitted one we
4692 * don't have to fudge the TSN.
4695 ret_sz += tp1->book_size;
4696 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, reason, tp1, so_locked);
4697 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4698 sctp_m_freem(tp1->data);
4699 /* No flight involved here book the size to 0 */
4702 if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4705 do_wakeup_routine = 1;
4706 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4707 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4709 * on to the sent queue so we can wait for it to be
4712 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4714 stcb->asoc.send_queue_cnt--;
4715 stcb->asoc.sent_queue_cnt++;
4719 if (foundeom == 0) {
4721 * Still no eom found. That means there is stuff left on the
4722 * stream out queue.. yuck.
4724 strq = &stcb->asoc.strmout[stream];
4725 SCTP_TCB_SEND_LOCK(stcb);
4726 sp = TAILQ_FIRST(&strq->outqueue);
4727 while (sp->strseq <= seq) {
4728 /* Check if its our SEQ */
4729 if (sp->strseq == seq) {
4730 sp->discard_rest = 1;
4732 * We may need to put a chunk on the queue
4733 * that holds the TSN that would have been
4734 * sent with the LAST bit.
4737 /* Yep, we have to */
4738 sctp_alloc_a_chunk(stcb, chk);
4741 * we are hosed. All we can
4742 * do is nothing.. which
4743 * will cause an abort if
4744 * the peer is paying
4749 memset(chk, 0, sizeof(*chk));
4750 chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG;
4751 chk->sent = SCTP_FORWARD_TSN_SKIP;
4752 chk->asoc = &stcb->asoc;
4753 chk->rec.data.stream_seq = sp->strseq;
4754 chk->rec.data.stream_number = sp->stream;
4755 chk->rec.data.payloadtype = sp->ppid;
4756 chk->rec.data.context = sp->context;
4757 chk->flags = sp->act_flags;
4758 chk->addr_over = sp->addr_over;
4759 chk->whoTo = sp->net;
4760 atomic_add_int(&chk->whoTo->ref_count, 1);
4761 chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
4762 stcb->asoc.pr_sctp_cnt++;
4763 chk->pr_sctp_on = 1;
4764 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
4765 stcb->asoc.sent_queue_cnt++;
4766 stcb->asoc.pr_sctp_cnt++;
4768 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4773 * Pull any data to free up the SB
4774 * and allow sender to "add more"
4775 * whilc we will throw away :-)
4777 sctp_free_spbufspace(stcb, &stcb->asoc,
4779 ret_sz += sp->length;
4780 do_wakeup_routine = 1;
4782 sctp_m_freem(sp->data);
4785 sp->tail_mbuf = NULL;
4789 /* Next one please */
4790 sp = TAILQ_NEXT(sp, next);
4793 SCTP_TCB_SEND_UNLOCK(stcb);
4795 if (do_wakeup_routine) {
4796 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4799 so = SCTP_INP_SO(stcb->sctp_ep);
4801 atomic_add_int(&stcb->asoc.refcnt, 1);
4802 SCTP_TCB_UNLOCK(stcb);
4803 SCTP_SOCKET_LOCK(so, 1);
4804 SCTP_TCB_LOCK(stcb);
4805 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4806 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4807 /* assoc was freed while we were unlocked */
4808 SCTP_SOCKET_UNLOCK(so, 1);
4813 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
4814 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4816 SCTP_SOCKET_UNLOCK(so, 1);
4824 * checks to see if the given address, sa, is one that is currently known by
4825 * the kernel note: can't distinguish the same address on multiple interfaces
4826 * and doesn't handle multiple addresses with different zone/scope id's note:
4827 * ifa_ifwithaddr() compares the entire sockaddr struct
4830 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
4833 struct sctp_laddr *laddr;
4835 if (holds_lock == 0) {
4836 SCTP_INP_RLOCK(inp);
4838 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4839 if (laddr->ifa == NULL)
4841 if (addr->sa_family != laddr->ifa->address.sa.sa_family)
4843 if (addr->sa_family == AF_INET) {
4844 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4845 laddr->ifa->address.sin.sin_addr.s_addr) {
4847 if (holds_lock == 0) {
4848 SCTP_INP_RUNLOCK(inp);
4850 return (laddr->ifa);
4855 if (addr->sa_family == AF_INET6) {
4856 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4857 &laddr->ifa->address.sin6)) {
4859 if (holds_lock == 0) {
4860 SCTP_INP_RUNLOCK(inp);
4862 return (laddr->ifa);
4868 if (holds_lock == 0) {
4869 SCTP_INP_RUNLOCK(inp);
4875 sctp_get_ifa_hash_val(struct sockaddr *addr)
4877 if (addr->sa_family == AF_INET) {
4878 struct sockaddr_in *sin;
4880 sin = (struct sockaddr_in *)addr;
4881 return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
4882 } else if (addr->sa_family == AF_INET6) {
4883 struct sockaddr_in6 *sin6;
4884 uint32_t hash_of_addr;
4886 sin6 = (struct sockaddr_in6 *)addr;
4887 hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
4888 sin6->sin6_addr.s6_addr32[1] +
4889 sin6->sin6_addr.s6_addr32[2] +
4890 sin6->sin6_addr.s6_addr32[3]);
4891 hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
4892 return (hash_of_addr);
4898 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
4900 struct sctp_ifa *sctp_ifap;
4901 struct sctp_vrf *vrf;
4902 struct sctp_ifalist *hash_head;
4903 uint32_t hash_of_addr;
4905 if (holds_lock == 0)
4906 SCTP_IPI_ADDR_RLOCK();
4908 vrf = sctp_find_vrf(vrf_id);
4911 if (holds_lock == 0)
4912 SCTP_IPI_ADDR_RUNLOCK();
4915 hash_of_addr = sctp_get_ifa_hash_val(addr);
4917 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
4918 if (hash_head == NULL) {
4919 SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
4920 hash_of_addr, (uint32_t) vrf->vrf_addr_hashmark,
4921 (uint32_t) (hash_of_addr & vrf->vrf_addr_hashmark));
4922 sctp_print_address(addr);
4923 SCTP_PRINTF("No such bucket for address\n");
4924 if (holds_lock == 0)
4925 SCTP_IPI_ADDR_RUNLOCK();
4929 LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
4930 if (sctp_ifap == NULL) {
4932 panic("Huh LIST_FOREACH corrupt");
4935 SCTP_PRINTF("LIST corrupt of sctp_ifap's?\n");
4939 if (addr->sa_family != sctp_ifap->address.sa.sa_family)
4941 if (addr->sa_family == AF_INET) {
4942 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4943 sctp_ifap->address.sin.sin_addr.s_addr) {
4945 if (holds_lock == 0)
4946 SCTP_IPI_ADDR_RUNLOCK();
4952 if (addr->sa_family == AF_INET6) {
4953 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4954 &sctp_ifap->address.sin6)) {
4956 if (holds_lock == 0)
4957 SCTP_IPI_ADDR_RUNLOCK();
4964 if (holds_lock == 0)
4965 SCTP_IPI_ADDR_RUNLOCK();
4970 sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t * freed_so_far, int hold_rlock,
4973 /* User pulled some data, do we need a rwnd update? */
4976 struct socket *so = NULL;
4981 atomic_add_int(&stcb->asoc.refcnt, 1);
4983 if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
4984 SCTP_STATE_SHUTDOWN_RECEIVED |
4985 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4986 /* Pre-check If we are freeing no update */
4989 SCTP_INP_INCR_REF(stcb->sctp_ep);
4990 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4991 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4994 so = stcb->sctp_socket;
4998 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
4999 /* Have you have freed enough to look */
5001 /* Yep, its worth a look and the lock overhead */
5003 /* Figure out what the rwnd would be */
5004 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5005 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5006 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5010 if (dif >= rwnd_req) {
5012 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5015 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5017 * One last check before we allow the guy possibly
5018 * to get in. There is a race, where the guy has not
5019 * reached the gate. In that case
5023 SCTP_TCB_LOCK(stcb);
5024 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5025 /* No reports here */
5026 SCTP_TCB_UNLOCK(stcb);
5029 SCTP_STAT_INCR(sctps_wu_sacks_sent);
5031 * EY if nr_sacks used then send an nr-sack , a sack
5034 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
5035 sctp_send_nr_sack(stcb);
5037 sctp_send_sack(stcb);
5039 sctp_chunk_output(stcb->sctp_ep, stcb,
5040 SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5041 /* make sure no timer is running */
5042 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5043 SCTP_TCB_UNLOCK(stcb);
5045 /* Update how much we have pending */
5046 stcb->freed_by_sorcv_sincelast = dif;
5049 if (so && r_unlocked && hold_rlock) {
5050 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5052 SCTP_INP_DECR_REF(stcb->sctp_ep);
5054 atomic_add_int(&stcb->asoc.refcnt, -1);
5059 sctp_sorecvmsg(struct socket *so,
5062 struct sockaddr *from,
5065 struct sctp_sndrcvinfo *sinfo,
5069 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5070 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5071 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5072 * On the way out we may send out any combination of:
5073 * MSG_NOTIFICATION MSG_EOR
5076 struct sctp_inpcb *inp = NULL;
5078 int cp_len = 0, error = 0;
5079 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
5080 struct mbuf *m = NULL, *embuf = NULL;
5081 struct sctp_tcb *stcb = NULL;
5082 int wakeup_read_socket = 0;
5083 int freecnt_applied = 0;
5084 int out_flags = 0, in_flags = 0;
5085 int block_allowed = 1;
5086 uint32_t freed_so_far = 0;
5087 uint32_t copied_so_far = 0;
5088 int in_eeor_mode = 0;
5089 int no_rcv_needed = 0;
5090 uint32_t rwnd_req = 0;
5091 int hold_sblock = 0;
5094 uint32_t held_length = 0;
5095 int sockbuf_lock = 0;
5098 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5102 in_flags = *msg_flags;
5103 if (in_flags & MSG_PEEK)
5104 SCTP_STAT_INCR(sctps_read_peeks);
5108 slen = uio->uio_resid;
5110 /* Pull in and set up our int flags */
5111 if (in_flags & MSG_OOB) {
5112 /* Out of band's NOT supported */
5113 return (EOPNOTSUPP);
5115 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5116 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5119 if ((in_flags & (MSG_DONTWAIT
5122 SCTP_SO_IS_NBIO(so)) {
5125 /* setup the endpoint */
5126 inp = (struct sctp_inpcb *)so->so_pcb;
5128 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5131 rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5132 /* Must be at least a MTU's worth */
5133 if (rwnd_req < SCTP_MIN_RWND)
5134 rwnd_req = SCTP_MIN_RWND;
5135 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5136 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5137 sctp_misc_ints(SCTP_SORECV_ENTER,
5138 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
5140 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5141 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5142 rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
5144 error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5147 goto release_unlocked;
5153 if (hold_sblock == 0) {
5154 SOCKBUF_LOCK(&so->so_rcv);
5157 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5158 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5161 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5163 error = so->so_error;
5164 if ((in_flags & MSG_PEEK) == 0)
5168 if (so->so_rcv.sb_cc == 0) {
5169 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5176 if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
5177 /* we need to wait for data */
5178 if ((so->so_rcv.sb_cc == 0) &&
5179 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5180 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5181 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5183 * For active open side clear flags for
5184 * re-use passive open is blocked by
5187 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5189 * You were aborted, passive side
5192 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5195 * You get this once if you are
5198 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5200 * Remove flag if on the
5203 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
5206 so->so_state &= ~(SS_ISCONNECTING |
5207 SS_ISDISCONNECTING |
5211 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5212 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5215 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
5221 error = sbwait(&so->so_rcv);
5226 goto restart_nosblocks;
5227 } else if (so->so_rcv.sb_cc == 0) {
5229 error = so->so_error;
5230 if ((in_flags & MSG_PEEK) == 0)
5233 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5234 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5235 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5237 * For active open side clear flags
5238 * for re-use passive open is
5239 * blocked by connect.
5241 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5243 * You were aborted, passive
5244 * side always hits here
5246 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5249 * You get this once if you
5250 * are active open side
5252 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5258 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
5261 so->so_state &= ~(SS_ISCONNECTING |
5262 SS_ISDISCONNECTING |
5266 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5267 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5270 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
5276 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5277 error = EWOULDBLOCK;
5281 if (hold_sblock == 1) {
5282 SOCKBUF_UNLOCK(&so->so_rcv);
5285 /* we possibly have data we can read */
5286 /* sa_ignore FREED_MEMORY */
5287 control = TAILQ_FIRST(&inp->read_queue);
5288 if (control == NULL) {
5290 * This could be happening since the appender did the
5291 * increment but as not yet did the tailq insert onto the
5294 if (hold_rlock == 0) {
5295 SCTP_INP_READ_LOCK(inp);
5298 control = TAILQ_FIRST(&inp->read_queue);
5299 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5301 panic("Huh, its non zero and nothing on control?");
5303 so->so_rcv.sb_cc = 0;
5305 SCTP_INP_READ_UNLOCK(inp);
5309 if ((control->length == 0) &&
5310 (control->do_not_ref_stcb)) {
5312 * Clean up code for freeing assoc that left behind a
5313 * pdapi.. maybe a peer in EEOR that just closed after
5314 * sending and never indicated a EOR.
5316 if (hold_rlock == 0) {
5318 SCTP_INP_READ_LOCK(inp);
5320 control->held_length = 0;
5321 if (control->data) {
5322 /* Hmm there is data here .. fix */
5326 m_tmp = control->data;
5328 cnt += SCTP_BUF_LEN(m_tmp);
5329 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5330 control->tail_mbuf = m_tmp;
5331 control->end_added = 1;
5333 m_tmp = SCTP_BUF_NEXT(m_tmp);
5335 control->length = cnt;
5338 TAILQ_REMOVE(&inp->read_queue, control, next);
5339 /* Add back any hiddend data */
5340 sctp_free_remote_addr(control->whoFrom);
5341 sctp_free_a_readq(stcb, control);
5345 SCTP_INP_READ_UNLOCK(inp);
5349 if (control->length == 0) {
5350 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5352 /* find a more suitable one then this */
5353 ctl = TAILQ_NEXT(control, next);
5355 if ((ctl->stcb != control->stcb) && (ctl->length) &&
5357 (ctl->spec_flags & M_NOTIFICATION) ||
5358 ((ctl->do_not_ref_stcb == 0) &&
5359 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
5362 * If we have a different TCB next, and there is data
5363 * present. If we have already taken some (pdapi), OR we can
5364 * ref the tcb and no delivery as started on this stream, we
5365 * take it. Note we allow a notification on a different
5366 * assoc to be delivered..
5370 } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
5372 ((ctl->some_taken) ||
5373 ((ctl->do_not_ref_stcb == 0) &&
5374 ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5375 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
5378 * If we have the same tcb, and there is data present, and we
5379 * have the strm interleave feature present. Then if we have
5380 * taken some (pdapi) or we can refer to tht tcb AND we have
5381 * not started a delivery for this stream, we can take it.
5382 * Note we do NOT allow a notificaiton on the same assoc to
5388 ctl = TAILQ_NEXT(ctl, next);
5392 * if we reach here, not suitable replacement is available
5393 * <or> fragment interleave is NOT on. So stuff the sb_cc
5394 * into the our held count, and its time to sleep again.
5396 held_length = so->so_rcv.sb_cc;
5397 control->held_length = so->so_rcv.sb_cc;
5400 /* Clear the held length since there is something to read */
5401 control->held_length = 0;
5403 SCTP_INP_READ_UNLOCK(inp);
5408 * If we reach here, control has a some data for us to read off.
5409 * Note that stcb COULD be NULL.
5411 control->some_taken++;
5413 SOCKBUF_UNLOCK(&so->so_rcv);
5416 stcb = control->stcb;
5418 if ((control->do_not_ref_stcb == 0) &&
5419 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5420 if (freecnt_applied == 0)
5422 } else if (control->do_not_ref_stcb == 0) {
5423 /* you can't free it on me please */
5425 * The lock on the socket buffer protects us so the
5426 * free code will stop. But since we used the
5427 * socketbuf lock and the sender uses the tcb_lock
5428 * to increment, we need to use the atomic add to
5431 if (freecnt_applied) {
5433 panic("refcnt already incremented");
5435 printf("refcnt already incremented?\n");
5438 atomic_add_int(&stcb->asoc.refcnt, 1);
5439 freecnt_applied = 1;
5442 * Setup to remember how much we have not yet told
5443 * the peer our rwnd has opened up. Note we grab the
5444 * value from the tcb from last time. Note too that
5445 * sack sending clears this when a sack is sent,
5446 * which is fine. Once we hit the rwnd_req, we then
5447 * will go to the sctp_user_rcvd() that will not
5448 * lock until it KNOWs it MUST send a WUP-SACK.
5450 freed_so_far = stcb->freed_by_sorcv_sincelast;
5451 stcb->freed_by_sorcv_sincelast = 0;
5455 ((control->spec_flags & M_NOTIFICATION) == 0) &&
5456 control->do_not_ref_stcb == 0) {
5457 stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5459 /* First lets get off the sinfo and sockaddr info */
5460 if ((sinfo) && filling_sinfo) {
5461 memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
5462 nxt = TAILQ_NEXT(control, next);
5463 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
5464 struct sctp_extrcvinfo *s_extra;
5466 s_extra = (struct sctp_extrcvinfo *)sinfo;
5469 s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5470 if (nxt->sinfo_flags & SCTP_UNORDERED) {
5471 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5473 if (nxt->spec_flags & M_NOTIFICATION) {
5474 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5476 s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id;
5477 s_extra->sreinfo_next_length = nxt->length;
5478 s_extra->sreinfo_next_ppid = nxt->sinfo_ppid;
5479 s_extra->sreinfo_next_stream = nxt->sinfo_stream;
5480 if (nxt->tail_mbuf != NULL) {
5481 if (nxt->end_added) {
5482 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5487 * we explicitly 0 this, since the memcpy
5488 * got some other things beyond the older
5489 * sinfo_ that is on the control's structure
5493 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
5494 s_extra->sreinfo_next_aid = 0;
5495 s_extra->sreinfo_next_length = 0;
5496 s_extra->sreinfo_next_ppid = 0;
5497 s_extra->sreinfo_next_stream = 0;
5501 * update off the real current cum-ack, if we have an stcb.
5503 if ((control->do_not_ref_stcb == 0) && stcb)
5504 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5506 * mask off the high bits, we keep the actual chunk bits in
5509 sinfo->sinfo_flags &= 0x00ff;
5510 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
5511 sinfo->sinfo_flags |= SCTP_UNORDERED;
5514 #ifdef SCTP_ASOCLOG_OF_TSNS
5516 int index, newindex;
5517 struct sctp_pcbtsn_rlog *entry;
5520 index = inp->readlog_index;
5521 newindex = index + 1;
5522 if (newindex >= SCTP_READ_LOG_SIZE) {
5525 } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
5526 entry = &inp->readlog[index];
5527 entry->vtag = control->sinfo_assoc_id;
5528 entry->strm = control->sinfo_stream;
5529 entry->seq = control->sinfo_ssn;
5530 entry->sz = control->length;
5531 entry->flgs = control->sinfo_flags;
5534 if (fromlen && from) {
5535 struct sockaddr *to;
5538 cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sin.sin_len);
5539 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
5540 ((struct sockaddr_in *)from)->sin_port = control->port_from;
5542 /* No AF_INET use AF_INET6 */
5543 cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sin6.sin6_len);
5544 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
5545 ((struct sockaddr_in6 *)from)->sin6_port = control->port_from;
5549 #if defined(INET) && defined(INET6)
5550 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
5551 (to->sa_family == AF_INET) &&
5552 ((size_t)fromlen >= sizeof(struct sockaddr_in6))) {
5553 struct sockaddr_in *sin;
5554 struct sockaddr_in6 sin6;
5556 sin = (struct sockaddr_in *)to;
5557 bzero(&sin6, sizeof(sin6));
5558 sin6.sin6_family = AF_INET6;
5559 sin6.sin6_len = sizeof(struct sockaddr_in6);
5560 sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
5561 bcopy(&sin->sin_addr,
5562 &sin6.sin6_addr.s6_addr32[3],
5563 sizeof(sin6.sin6_addr.s6_addr32[3]));
5564 sin6.sin6_port = sin->sin_port;
5565 memcpy(from, (caddr_t)&sin6, sizeof(sin6));
5570 struct sockaddr_in6 lsa6, *to6;
5572 to6 = (struct sockaddr_in6 *)to;
5573 sctp_recover_scope_mac(to6, (&lsa6));
5577 /* now copy out what data we can */
5579 /* copy out each mbuf in the chain up to length */
5583 /* Move out all we can */
5584 cp_len = (int)uio->uio_resid;
5585 my_len = (int)SCTP_BUF_LEN(m);
5586 if (cp_len > my_len) {
5587 /* not enough in this buf */
5591 SCTP_INP_READ_UNLOCK(inp);
5595 error = uiomove(mtod(m, char *), cp_len, uio);
5597 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5600 if ((control->do_not_ref_stcb == 0) && stcb &&
5601 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5605 /* error we are out of here */
5608 if ((SCTP_BUF_NEXT(m) == NULL) &&
5609 (cp_len >= SCTP_BUF_LEN(m)) &&
5610 ((control->end_added == 0) ||
5611 (control->end_added &&
5612 (TAILQ_NEXT(control, next) == NULL)))
5614 SCTP_INP_READ_LOCK(inp);
5617 if (cp_len == SCTP_BUF_LEN(m)) {
5618 if ((SCTP_BUF_NEXT(m) == NULL) &&
5619 (control->end_added)) {
5620 out_flags |= MSG_EOR;
5621 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5622 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5624 if (control->spec_flags & M_NOTIFICATION) {
5625 out_flags |= MSG_NOTIFICATION;
5627 /* we ate up the mbuf */
5628 if (in_flags & MSG_PEEK) {
5630 m = SCTP_BUF_NEXT(m);
5631 copied_so_far += cp_len;
5633 /* dispose of the mbuf */
5634 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5635 sctp_sblog(&so->so_rcv,
5636 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5638 sctp_sbfree(control, stcb, &so->so_rcv, m);
5639 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5640 sctp_sblog(&so->so_rcv,
5641 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5644 copied_so_far += cp_len;
5645 freed_so_far += cp_len;
5646 freed_so_far += MSIZE;
5647 atomic_subtract_int(&control->length, cp_len);
5648 control->data = sctp_m_free(m);
5651 * been through it all, must hold sb
5652 * lock ok to null tail
5654 if (control->data == NULL) {
5656 if ((control->end_added == 0) ||
5657 (TAILQ_NEXT(control, next) == NULL)) {
5665 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5666 panic("Hmm we don't own the lock?");
5670 control->tail_mbuf = NULL;
5672 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5673 panic("end_added, nothing left and no MSG_EOR");
5679 /* Do we need to trim the mbuf? */
5680 if (control->spec_flags & M_NOTIFICATION) {
5681 out_flags |= MSG_NOTIFICATION;
5683 if ((in_flags & MSG_PEEK) == 0) {
5684 SCTP_BUF_RESV_UF(m, cp_len);
5685 SCTP_BUF_LEN(m) -= cp_len;
5686 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5687 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
5689 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5690 if ((control->do_not_ref_stcb == 0) &&
5692 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5694 copied_so_far += cp_len;
5696 freed_so_far += cp_len;
5697 freed_so_far += MSIZE;
5698 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5699 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5700 SCTP_LOG_SBRESULT, 0);
5702 atomic_subtract_int(&control->length, cp_len);
5704 copied_so_far += cp_len;
5707 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5710 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5711 (control->do_not_ref_stcb == 0) &&
5712 (freed_so_far >= rwnd_req)) {
5713 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5715 } /* end while(m) */
5717 * At this point we have looked at it all and we either have
5718 * a MSG_EOR/or read all the user wants... <OR>
5719 * control->length == 0.
5721 if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5722 /* we are done with this control */
5723 if (control->length == 0) {
5724 if (control->data) {
5726 panic("control->data not null at read eor?");
5728 SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5729 sctp_m_freem(control->data);
5730 control->data = NULL;
5734 if (TAILQ_NEXT(control, next) == NULL) {
5736 * If we don't have a next we need a
5737 * lock, if there is a next
5738 * interrupt is filling ahead of us
5739 * and we don't need a lock to
5740 * remove this guy (which is the
5741 * head of the queue).
5743 if (hold_rlock == 0) {
5744 SCTP_INP_READ_LOCK(inp);
5748 TAILQ_REMOVE(&inp->read_queue, control, next);
5749 /* Add back any hiddend data */
5750 if (control->held_length) {
5752 control->held_length = 0;
5753 wakeup_read_socket = 1;
5755 if (control->aux_data) {
5756 sctp_m_free(control->aux_data);
5757 control->aux_data = NULL;
5759 no_rcv_needed = control->do_not_ref_stcb;
5760 sctp_free_remote_addr(control->whoFrom);
5761 control->data = NULL;
5762 sctp_free_a_readq(stcb, control);
5764 if ((freed_so_far >= rwnd_req) &&
5765 (no_rcv_needed == 0))
5766 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5770 * The user did not read all of this
5771 * message, turn off the returned MSG_EOR
5772 * since we are leaving more behind on the
5776 if (control->end_added &&
5777 (control->data == NULL) &&
5778 (control->tail_mbuf == NULL)) {
5779 panic("Gak, control->length is corrupt?");
5782 no_rcv_needed = control->do_not_ref_stcb;
5783 out_flags &= ~MSG_EOR;
5786 if (out_flags & MSG_EOR) {
5789 if ((uio->uio_resid == 0) ||
5790 ((in_eeor_mode) && (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))
5795 * If I hit here the receiver wants more and this message is
5796 * NOT done (pd-api). So two questions. Can we block? if not
5797 * we are done. Did the user NOT set MSG_WAITALL?
5799 if (block_allowed == 0) {
5803 * We need to wait for more data a few things: - We don't
5804 * sbunlock() so we don't get someone else reading. - We
5805 * must be sure to account for the case where what is added
5806 * is NOT to our control when we wakeup.
5810 * Do we need to tell the transport a rwnd update might be
5811 * needed before we go to sleep?
5813 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5814 ((freed_so_far >= rwnd_req) &&
5815 (control->do_not_ref_stcb == 0) &&
5816 (no_rcv_needed == 0))) {
5817 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5820 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5823 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5826 if (hold_rlock == 1) {
5827 SCTP_INP_READ_UNLOCK(inp);
5830 if (hold_sblock == 0) {
5831 SOCKBUF_LOCK(&so->so_rcv);
5834 if ((copied_so_far) && (control->length == 0) &&
5835 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))
5839 if (so->so_rcv.sb_cc <= control->held_length) {
5840 error = sbwait(&so->so_rcv);
5844 control->held_length = 0;
5847 SOCKBUF_UNLOCK(&so->so_rcv);
5850 if (control->length == 0) {
5851 /* still nothing here */
5852 if (control->end_added == 1) {
5853 /* he aborted, or is done i.e.did a shutdown */
5854 out_flags |= MSG_EOR;
5855 if (control->pdapi_aborted) {
5856 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5857 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5859 out_flags |= MSG_TRUNC;
5861 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5862 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5864 goto done_with_control;
5866 if (so->so_rcv.sb_cc > held_length) {
5867 control->held_length = so->so_rcv.sb_cc;
5870 goto wait_some_more;
5871 } else if (control->data == NULL) {
5873 * we must re-sync since data is probably being
5876 SCTP_INP_READ_LOCK(inp);
5877 if ((control->length > 0) && (control->data == NULL)) {
5879 * big trouble.. we have the lock and its
5883 panic("Impossible data==NULL length !=0");
5885 out_flags |= MSG_EOR;
5886 out_flags |= MSG_TRUNC;
5887 control->length = 0;
5888 SCTP_INP_READ_UNLOCK(inp);
5889 goto done_with_control;
5891 SCTP_INP_READ_UNLOCK(inp);
5892 /* We will fall around to get more data */
5897 * Give caller back the mbuf chain,
5898 * store in uio_resid the length
5900 wakeup_read_socket = 0;
5901 if ((control->end_added == 0) ||
5902 (TAILQ_NEXT(control, next) == NULL)) {
5903 /* Need to get rlock */
5904 if (hold_rlock == 0) {
5905 SCTP_INP_READ_LOCK(inp);
5909 if (control->end_added) {
5910 out_flags |= MSG_EOR;
5911 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5912 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5914 if (control->spec_flags & M_NOTIFICATION) {
5915 out_flags |= MSG_NOTIFICATION;
5917 uio->uio_resid = control->length;
5918 *mp = control->data;
5921 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5922 sctp_sblog(&so->so_rcv,
5923 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5925 sctp_sbfree(control, stcb, &so->so_rcv, m);
5926 freed_so_far += SCTP_BUF_LEN(m);
5927 freed_so_far += MSIZE;
5928 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5929 sctp_sblog(&so->so_rcv,
5930 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5932 m = SCTP_BUF_NEXT(m);
5934 control->data = control->tail_mbuf = NULL;
5935 control->length = 0;
5936 if (out_flags & MSG_EOR) {
5937 /* Done with this control */
5938 goto done_with_control;
5942 if (hold_rlock == 1) {
5943 SCTP_INP_READ_UNLOCK(inp);
5946 if (hold_sblock == 1) {
5947 SOCKBUF_UNLOCK(&so->so_rcv);
5950 sbunlock(&so->so_rcv);
5955 SOCKBUF_UNLOCK(&so->so_rcv);
5958 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
5959 if ((freed_so_far >= rwnd_req) &&
5960 (control && (control->do_not_ref_stcb == 0)) &&
5961 (no_rcv_needed == 0))
5962 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5966 *msg_flags = out_flags;
5968 if (((out_flags & MSG_EOR) == 0) &&
5969 ((in_flags & MSG_PEEK) == 0) &&
5971 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO))) {
5972 struct sctp_extrcvinfo *s_extra;
5974 s_extra = (struct sctp_extrcvinfo *)sinfo;
5975 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
5977 if (hold_rlock == 1) {
5978 SCTP_INP_READ_UNLOCK(inp);
5982 SOCKBUF_UNLOCK(&so->so_rcv);
5986 sbunlock(&so->so_rcv);
5988 if (freecnt_applied) {
5990 * The lock on the socket buffer protects us so the free
5991 * code will stop. But since we used the socketbuf lock and
5992 * the sender uses the tcb_lock to increment, we need to use
5993 * the atomic add to the refcnt.
5997 panic("stcb for refcnt has gone NULL?");
6003 atomic_add_int(&stcb->asoc.refcnt, -1);
6004 freecnt_applied = 0;
6005 /* Save the value back for next time */
6006 stcb->freed_by_sorcv_sincelast = freed_so_far;
6008 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6010 sctp_misc_ints(SCTP_SORECV_DONE,
6012 ((uio) ? (slen - uio->uio_resid) : slen),
6016 sctp_misc_ints(SCTP_SORECV_DONE,
6018 ((uio) ? (slen - uio->uio_resid) : slen),
6024 if (wakeup_read_socket) {
6025 sctp_sorwakeup(inp, so);
6031 #ifdef SCTP_MBUF_LOGGING
6033 sctp_m_free(struct mbuf *m)
6035 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6036 if (SCTP_BUF_IS_EXTENDED(m)) {
6037 sctp_log_mb(m, SCTP_MBUF_IFREE);
6044 sctp_m_freem(struct mbuf *mb)
6047 mb = sctp_m_free(mb);
6053 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
6056 * Given a local address. For all associations that holds the
6057 * address, request a peer-set-primary.
6059 struct sctp_ifa *ifa;
6060 struct sctp_laddr *wi;
6062 ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
6064 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
6065 return (EADDRNOTAVAIL);
6068 * Now that we have the ifa we must awaken the iterator with this
6071 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6073 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
6076 /* Now incr the count and int wi structure */
6077 SCTP_INCR_LADDR_COUNT();
6078 bzero(wi, sizeof(*wi));
6079 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
6081 wi->action = SCTP_SET_PRIM_ADDR;
6082 atomic_add_int(&ifa->refcount, 1);
6084 /* Now add it to the work queue */
6085 SCTP_IPI_ITERATOR_WQ_LOCK();
6087 * Should this really be a tailq? As it is we will process the
6090 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6091 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
6092 (struct sctp_inpcb *)NULL,
6093 (struct sctp_tcb *)NULL,
6094 (struct sctp_nets *)NULL);
6095 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6101 sctp_soreceive(struct socket *so,
6102 struct sockaddr **psa,
6105 struct mbuf **controlp,
6109 uint8_t sockbuf[256];
6110 struct sockaddr *from;
6111 struct sctp_extrcvinfo sinfo;
6112 int filling_sinfo = 1;
6113 struct sctp_inpcb *inp;
6115 inp = (struct sctp_inpcb *)so->so_pcb;
6116 /* pickup the assoc we are reading from */
6118 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6121 if ((sctp_is_feature_off(inp,
6122 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
6123 (controlp == NULL)) {
6124 /* user does not want the sndrcv ctl */
6128 from = (struct sockaddr *)sockbuf;
6129 fromlen = sizeof(sockbuf);
6136 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
6137 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6138 if ((controlp) && (filling_sinfo)) {
6139 /* copy back the sinfo in a CMSG format */
6141 *controlp = sctp_build_ctl_nchunk(inp,
6142 (struct sctp_sndrcvinfo *)&sinfo);
6147 /* copy back the address info */
6148 if (from && from->sa_len) {
6149 *psa = sodupsockaddr(from, M_NOWAIT);
6159 sctp_l_soreceive(struct socket *so,
6160 struct sockaddr **name,
6167 uint8_t sockbuf[256];
6168 struct sockaddr *from;
6169 struct sctp_extrcvinfo sinfo;
6170 int filling_sinfo = 1;
6171 struct sctp_inpcb *inp;
6173 inp = (struct sctp_inpcb *)so->so_pcb;
6174 /* pickup the assoc we are reading from */
6176 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6179 if ((sctp_is_feature_off(inp,
6180 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
6181 (controlp == NULL)) {
6182 /* user does not want the sndrcv ctl */
6186 from = (struct sockaddr *)sockbuf;
6187 fromlen = sizeof(sockbuf);
6194 error = sctp_sorecvmsg(so, uio,
6195 (struct mbuf **)NULL,
6196 from, fromlen, flag,
6197 (struct sctp_sndrcvinfo *)&sinfo,
6199 if ((controlp) && (filling_sinfo)) {
6201 * copy back the sinfo in a CMSG format note that the caller
6202 * has reponsibility for freeing the memory.
6205 *controlp = sctp_build_ctl_cchunk(inp,
6207 (struct sctp_sndrcvinfo *)&sinfo);
6210 /* copy back the address info */
6211 if (from && from->sa_len) {
6212 *name = sodupsockaddr(from, M_WAIT);
6227 sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6228 int totaddr, int *error)
6232 struct sctp_inpcb *inp;
6233 struct sockaddr *sa;
6237 inp = stcb->sctp_ep;
6239 for (i = 0; i < totaddr; i++) {
6240 if (sa->sa_family == AF_INET) {
6241 incr = sizeof(struct sockaddr_in);
6242 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
6243 /* assoc gone no un-lock */
6244 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6245 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
6250 } else if (sa->sa_family == AF_INET6) {
6251 incr = sizeof(struct sockaddr_in6);
6252 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
6253 /* assoc gone no un-lock */
6254 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6255 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
6261 sa = (struct sockaddr *)((caddr_t)sa + incr);
6268 sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6269 int *totaddr, int *num_v4, int *num_v6, int *error,
6270 int limit, int *bad_addr)
6272 struct sockaddr *sa;
6273 struct sctp_tcb *stcb = NULL;
6278 *error = *num_v6 = *num_v4 = 0;
6279 /* account and validate addresses */
6280 for (i = 0; i < (size_t)*totaddr; i++) {
6281 if (sa->sa_family == AF_INET) {
6283 incr = sizeof(struct sockaddr_in);
6284 if (sa->sa_len != incr) {
6285 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6290 } else if (sa->sa_family == AF_INET6) {
6291 struct sockaddr_in6 *sin6;
6293 sin6 = (struct sockaddr_in6 *)sa;
6294 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6295 /* Must be non-mapped for connectx */
6296 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6302 incr = sizeof(struct sockaddr_in6);
6303 if (sa->sa_len != incr) {
6304 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6314 SCTP_INP_INCR_REF(inp);
6315 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
6317 /* Already have or am bring up an association */
6320 SCTP_INP_DECR_REF(inp);
6322 if ((at + incr) > (size_t)limit) {
6326 sa = (struct sockaddr *)((caddr_t)sa + incr);
6328 return ((struct sctp_tcb *)NULL);
6332 * sctp_bindx(ADD) for one address.
6333 * assumes all arguments are valid/checked by caller.
6336 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
6337 struct sockaddr *sa, sctp_assoc_t assoc_id,
6338 uint32_t vrf_id, int *error, void *p)
6340 struct sockaddr *addr_touse;
6343 struct sockaddr_in sin;
6347 /* see if we're bound all already! */
6348 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6349 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6354 #if defined(INET6) && !defined(__Userspace__) /* TODO port in6_sin6_2_sin */
6355 if (sa->sa_family == AF_INET6) {
6356 struct sockaddr_in6 *sin6;
6358 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6359 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6363 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6364 /* can only bind v6 on PF_INET6 sockets */
6365 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6369 sin6 = (struct sockaddr_in6 *)addr_touse;
6370 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6371 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6372 SCTP_IPV6_V6ONLY(inp)) {
6373 /* can't bind v4-mapped on PF_INET sockets */
6374 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6378 in6_sin6_2_sin(&sin, sin6);
6379 addr_touse = (struct sockaddr *)&sin;
6383 if (sa->sa_family == AF_INET) {
6384 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6385 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6389 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6390 SCTP_IPV6_V6ONLY(inp)) {
6391 /* can't bind v4 on PF_INET sockets */
6392 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6397 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
6399 /* Can't get proc for Net/Open BSD */
6400 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6404 *error = sctp_inpcb_bind(so, addr_touse, NULL, p);
6408 * No locks required here since bind and mgmt_ep_sa all do their own
6409 * locking. If we do something for the FIX: below we may need to
6410 * lock in that case.
6412 if (assoc_id == 0) {
6413 /* add the address */
6414 struct sctp_inpcb *lep;
6415 struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
6417 /* validate the incoming port */
6418 if ((lsin->sin_port != 0) &&
6419 (lsin->sin_port != inp->sctp_lport)) {
6420 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6424 /* user specified 0 port, set it to existing port */
6425 lsin->sin_port = inp->sctp_lport;
6428 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
6431 * We must decrement the refcount since we have the
6432 * ep already and are binding. No remove going on
6435 SCTP_INP_DECR_REF(lep);
6438 /* already bound to it.. ok */
6440 } else if (lep == NULL) {
6441 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
6442 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6443 SCTP_ADD_IP_ADDRESS,
6446 *error = EADDRINUSE;
6452 * FIX: decide whether we allow assoc based bindx
6458 * sctp_bindx(DELETE) for one address.
6459 * assumes all arguments are valid/checked by caller.
6462 sctp_bindx_delete_address(struct socket *so, struct sctp_inpcb *inp,
6463 struct sockaddr *sa, sctp_assoc_t assoc_id,
6464 uint32_t vrf_id, int *error)
6466 struct sockaddr *addr_touse;
6469 struct sockaddr_in sin;
6473 /* see if we're bound all already! */
6474 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6475 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6480 #if defined(INET6) && !defined(__Userspace__) /* TODO port in6_sin6_2_sin */
6481 if (sa->sa_family == AF_INET6) {
6482 struct sockaddr_in6 *sin6;
6484 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6485 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6489 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6490 /* can only bind v6 on PF_INET6 sockets */
6491 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6495 sin6 = (struct sockaddr_in6 *)addr_touse;
6496 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6497 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6498 SCTP_IPV6_V6ONLY(inp)) {
6499 /* can't bind mapped-v4 on PF_INET sockets */
6500 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6504 in6_sin6_2_sin(&sin, sin6);
6505 addr_touse = (struct sockaddr *)&sin;
6509 if (sa->sa_family == AF_INET) {
6510 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6511 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6515 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6516 SCTP_IPV6_V6ONLY(inp)) {
6517 /* can't bind v4 on PF_INET sockets */
6518 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6524 * No lock required mgmt_ep_sa does its own locking. If the FIX:
6525 * below is ever changed we may need to lock before calling
6526 * association level binding.
6528 if (assoc_id == 0) {
6529 /* delete the address */
6530 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6531 SCTP_DEL_IP_ADDRESS,
6535 * FIX: decide whether we allow assoc based bindx
6541 * returns the valid local address count for an assoc, taking into account
6545 sctp_local_addr_count(struct sctp_tcb *stcb)
6547 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
6548 int ipv4_addr_legal, ipv6_addr_legal;
6549 struct sctp_vrf *vrf;
6550 struct sctp_ifn *sctp_ifn;
6551 struct sctp_ifa *sctp_ifa;
6554 /* Turn on all the appropriate scopes */
6555 loopback_scope = stcb->asoc.loopback_scope;
6556 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
6557 local_scope = stcb->asoc.local_scope;
6558 site_scope = stcb->asoc.site_scope;
6559 ipv4_addr_legal = ipv6_addr_legal = 0;
6560 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6561 ipv6_addr_legal = 1;
6562 if (SCTP_IPV6_V6ONLY(stcb->sctp_ep) == 0) {
6563 ipv4_addr_legal = 1;
6566 ipv4_addr_legal = 1;
6569 SCTP_IPI_ADDR_RLOCK();
6570 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
6572 /* no vrf, no addresses */
6573 SCTP_IPI_ADDR_RUNLOCK();
6576 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6578 * bound all case: go through all ifns on the vrf
6580 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
6581 if ((loopback_scope == 0) &&
6582 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
6585 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
6586 if (sctp_is_addr_restricted(stcb, sctp_ifa))
6588 switch (sctp_ifa->address.sa.sa_family) {
6590 if (ipv4_addr_legal) {
6591 struct sockaddr_in *sin;
6593 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
6594 if (sin->sin_addr.s_addr == 0) {
6601 if ((ipv4_local_scope == 0) &&
6602 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
6605 /* count this one */
6613 if (ipv6_addr_legal) {
6614 struct sockaddr_in6 *sin6;
6616 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
6617 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6620 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
6621 if (local_scope == 0)
6623 if (sin6->sin6_scope_id == 0) {
6624 if (sa6_recoverscope(sin6) != 0)
6642 if ((site_scope == 0) &&
6643 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
6646 /* count this one */
6661 struct sctp_laddr *laddr;
6663 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
6665 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
6668 /* count this one */
6672 SCTP_IPI_ADDR_RUNLOCK();
6676 #if defined(SCTP_LOCAL_TRACE_BUF)
6679 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)
6681 uint32_t saveindex, newindex;
6684 saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6685 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6688 newindex = saveindex + 1;
6690 } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6691 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6694 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6695 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6696 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6697 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6698 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6699 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6700 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6701 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6705 /* We will need to add support
6706 * to bind the ports and such here
6707 * so we can do UDP tunneling. In
6708 * the mean-time, we return error
6710 #include <netinet/udp.h>
6711 #include <netinet/udp_var.h>
6712 #include <sys/proc.h>
6714 #include <netinet6/sctp6_var.h>
6718 sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *ignored)
6721 struct mbuf *sp, *last;
6722 struct udphdr *uhdr;
6723 uint16_t port = 0, len;
6724 int header_size = sizeof(struct udphdr) + sizeof(struct sctphdr);
6727 * Split out the mbuf chain. Leave the IP header in m, place the
6730 if ((m->m_flags & M_PKTHDR) == 0) {
6731 /* Can't handle one that is not a pkt hdr */
6734 /* pull the src port */
6735 iph = mtod(m, struct ip *);
6736 uhdr = (struct udphdr *)((caddr_t)iph + off);
6738 port = uhdr->uh_sport;
6739 sp = m_split(m, off, M_DONTWAIT);
6741 /* Gak, drop packet, we can't do a split */
6744 if (sp->m_pkthdr.len < header_size) {
6745 /* Gak, packet can't have an SCTP header in it - to small */
6749 /* ok now pull up the UDP header and SCTP header together */
6750 sp = m_pullup(sp, header_size);
6752 /* Gak pullup failed */
6755 /* trim out the UDP header */
6756 m_adj(sp, sizeof(struct udphdr));
6758 /* Now reconstruct the mbuf chain */
6759 /* 1) find last one */
6761 while (last->m_next != NULL) {
6762 last = last->m_next;
6765 m->m_pkthdr.len += sp->m_pkthdr.len;
6767 while (last != NULL) {
6768 last = last->m_next;
6770 /* Now its ready for sctp_input or sctp6_input */
6771 iph = mtod(m, struct ip *);
6772 switch (iph->ip_v) {
6776 len = SCTP_GET_IPV4_LENGTH(iph);
6777 len -= sizeof(struct udphdr);
6778 SCTP_GET_IPV4_LENGTH(iph) = len;
6779 sctp_input_with_port(m, off, port);
6783 case IPV6_VERSION >> 4:
6785 /* its IPv6 - NOT supported */
6803 sctp_over_udp_stop(void)
6808 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6811 if (SCTP_BASE_INFO(udp_tun_socket) == NULL) {
6815 sop = SCTP_BASE_INFO(udp_tun_socket);
6817 SCTP_BASE_INFO(udp_tun_socket) = NULL;
6820 sctp_over_udp_start(void)
6824 struct sockaddr_in sin;
6825 struct socket *sop = NULL;
6830 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6833 port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
6835 /* Must have a port set */
6838 if (SCTP_BASE_INFO(udp_tun_socket) != NULL) {
6839 /* Already running -- must stop first */
6843 cred = th->td_ucred;
6844 if ((ret = socreate(PF_INET, &sop,
6845 SOCK_DGRAM, IPPROTO_UDP, cred, th))) {
6848 SCTP_BASE_INFO(udp_tun_socket) = sop;
6849 /* call the special UDP hook */
6850 ret = udp_set_kernel_tunneling(sop, sctp_recv_udp_tunneled_packet);
6852 goto exit_stage_left;
6854 /* Ok we have a socket, bind it to the port */
6855 memset(&sin, 0, sizeof(sin));
6856 sin.sin_len = sizeof(sin);
6857 sin.sin_family = AF_INET;
6858 sin.sin_port = htons(port);
6859 ret = sobind(sop, (struct sockaddr *)&sin, th);
6861 /* Close up we cant get the port */
6863 sctp_over_udp_stop();
6867 * Ok we should now get UDP packets directly to our input routine
6868 * sctp_recv_upd_tunneled_packet().