2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctputil.c,v 1.37 2005/03/07 23:26:09 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctputil.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
43 #include <netinet/sctp_header.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_timer.h>
47 #include <netinet/sctp_indata.h>/* for sctp_deliver_data() */
48 #include <netinet/sctp_auth.h>
49 #include <netinet/sctp_asconf.h>
50 #include <netinet/sctp_cc_functions.h>
51 #include <netinet/sctp_bsd_addr.h>
55 #define KTR_SCTP KTR_SUBSYS
59 sctp_sblog(struct sockbuf *sb,
60 struct sctp_tcb *stcb, int from, int incr)
62 struct sctp_cwnd_log sctp_clog;
64 sctp_clog.x.sb.stcb = stcb;
65 sctp_clog.x.sb.so_sbcc = sb->sb_cc;
67 sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
69 sctp_clog.x.sb.stcb_sbcc = 0;
70 sctp_clog.x.sb.incr = incr;
71 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
74 sctp_clog.x.misc.log1,
75 sctp_clog.x.misc.log2,
76 sctp_clog.x.misc.log3,
77 sctp_clog.x.misc.log4);
81 sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
83 struct sctp_cwnd_log sctp_clog;
85 sctp_clog.x.close.inp = (void *)inp;
86 sctp_clog.x.close.sctp_flags = inp->sctp_flags;
88 sctp_clog.x.close.stcb = (void *)stcb;
89 sctp_clog.x.close.state = (uint16_t) stcb->asoc.state;
91 sctp_clog.x.close.stcb = 0;
92 sctp_clog.x.close.state = 0;
94 sctp_clog.x.close.loc = loc;
95 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
98 sctp_clog.x.misc.log1,
99 sctp_clog.x.misc.log2,
100 sctp_clog.x.misc.log3,
101 sctp_clog.x.misc.log4);
106 rto_logging(struct sctp_nets *net, int from)
108 struct sctp_cwnd_log sctp_clog;
110 memset(&sctp_clog, 0, sizeof(sctp_clog));
111 sctp_clog.x.rto.net = (void *)net;
112 sctp_clog.x.rto.rtt = net->prev_rtt;
113 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
116 sctp_clog.x.misc.log1,
117 sctp_clog.x.misc.log2,
118 sctp_clog.x.misc.log3,
119 sctp_clog.x.misc.log4);
124 sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
126 struct sctp_cwnd_log sctp_clog;
128 sctp_clog.x.strlog.stcb = stcb;
129 sctp_clog.x.strlog.n_tsn = tsn;
130 sctp_clog.x.strlog.n_sseq = sseq;
131 sctp_clog.x.strlog.e_tsn = 0;
132 sctp_clog.x.strlog.e_sseq = 0;
133 sctp_clog.x.strlog.strm = stream;
134 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
137 sctp_clog.x.misc.log1,
138 sctp_clog.x.misc.log2,
139 sctp_clog.x.misc.log3,
140 sctp_clog.x.misc.log4);
145 sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
147 struct sctp_cwnd_log sctp_clog;
149 sctp_clog.x.nagle.stcb = (void *)stcb;
150 sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
151 sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
152 sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
153 sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
154 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
155 SCTP_LOG_EVENT_NAGLE,
157 sctp_clog.x.misc.log1,
158 sctp_clog.x.misc.log2,
159 sctp_clog.x.misc.log3,
160 sctp_clog.x.misc.log4);
165 sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
167 struct sctp_cwnd_log sctp_clog;
169 sctp_clog.x.sack.cumack = cumack;
170 sctp_clog.x.sack.oldcumack = old_cumack;
171 sctp_clog.x.sack.tsn = tsn;
172 sctp_clog.x.sack.numGaps = gaps;
173 sctp_clog.x.sack.numDups = dups;
174 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
177 sctp_clog.x.misc.log1,
178 sctp_clog.x.misc.log2,
179 sctp_clog.x.misc.log3,
180 sctp_clog.x.misc.log4);
184 sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
186 struct sctp_cwnd_log sctp_clog;
188 memset(&sctp_clog, 0, sizeof(sctp_clog));
189 sctp_clog.x.map.base = map;
190 sctp_clog.x.map.cum = cum;
191 sctp_clog.x.map.high = high;
192 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
195 sctp_clog.x.misc.log1,
196 sctp_clog.x.misc.log2,
197 sctp_clog.x.misc.log3,
198 sctp_clog.x.misc.log4);
202 sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn,
205 struct sctp_cwnd_log sctp_clog;
207 memset(&sctp_clog, 0, sizeof(sctp_clog));
208 sctp_clog.x.fr.largest_tsn = biggest_tsn;
209 sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
210 sctp_clog.x.fr.tsn = tsn;
211 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
214 sctp_clog.x.misc.log1,
215 sctp_clog.x.misc.log2,
216 sctp_clog.x.misc.log3,
217 sctp_clog.x.misc.log4);
223 sctp_log_mb(struct mbuf *m, int from)
225 struct sctp_cwnd_log sctp_clog;
227 sctp_clog.x.mb.mp = m;
228 sctp_clog.x.mb.mbuf_flags = (uint8_t) (SCTP_BUF_GET_FLAGS(m));
229 sctp_clog.x.mb.size = (uint16_t) (SCTP_BUF_LEN(m));
230 sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
231 if (SCTP_BUF_IS_EXTENDED(m)) {
232 sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
233 sctp_clog.x.mb.refcnt = (uint8_t) (SCTP_BUF_EXTEND_REFCNT(m));
235 sctp_clog.x.mb.ext = 0;
236 sctp_clog.x.mb.refcnt = 0;
238 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
241 sctp_clog.x.misc.log1,
242 sctp_clog.x.misc.log2,
243 sctp_clog.x.misc.log3,
244 sctp_clog.x.misc.log4);
249 sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk,
252 struct sctp_cwnd_log sctp_clog;
254 if (control == NULL) {
255 SCTP_PRINTF("Gak log of NULL?\n");
258 sctp_clog.x.strlog.stcb = control->stcb;
259 sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
260 sctp_clog.x.strlog.n_sseq = control->sinfo_ssn;
261 sctp_clog.x.strlog.strm = control->sinfo_stream;
262 if (poschk != NULL) {
263 sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
264 sctp_clog.x.strlog.e_sseq = poschk->sinfo_ssn;
266 sctp_clog.x.strlog.e_tsn = 0;
267 sctp_clog.x.strlog.e_sseq = 0;
269 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
272 sctp_clog.x.misc.log1,
273 sctp_clog.x.misc.log2,
274 sctp_clog.x.misc.log3,
275 sctp_clog.x.misc.log4);
280 sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
282 struct sctp_cwnd_log sctp_clog;
284 sctp_clog.x.cwnd.net = net;
285 if (stcb->asoc.send_queue_cnt > 255)
286 sctp_clog.x.cwnd.cnt_in_send = 255;
288 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
289 if (stcb->asoc.stream_queue_cnt > 255)
290 sctp_clog.x.cwnd.cnt_in_str = 255;
292 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
295 sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
296 sctp_clog.x.cwnd.inflight = net->flight_size;
297 sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
298 sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
299 sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
301 if (SCTP_CWNDLOG_PRESEND == from) {
302 sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
304 sctp_clog.x.cwnd.cwnd_augment = augment;
305 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
308 sctp_clog.x.misc.log1,
309 sctp_clog.x.misc.log2,
310 sctp_clog.x.misc.log3,
311 sctp_clog.x.misc.log4);
316 sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
318 struct sctp_cwnd_log sctp_clog;
320 memset(&sctp_clog, 0, sizeof(sctp_clog));
322 sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
325 sctp_clog.x.lock.sock = (void *)NULL;
327 sctp_clog.x.lock.inp = (void *)inp;
329 sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
331 sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
334 sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
335 sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
337 sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
338 sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
340 sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
341 if (inp && (inp->sctp_socket)) {
342 sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
343 sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
344 sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
346 sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
347 sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
348 sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
350 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
353 sctp_clog.x.misc.log1,
354 sctp_clog.x.misc.log2,
355 sctp_clog.x.misc.log3,
356 sctp_clog.x.misc.log4);
361 sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
363 struct sctp_cwnd_log sctp_clog;
365 memset(&sctp_clog, 0, sizeof(sctp_clog));
366 sctp_clog.x.cwnd.net = net;
367 sctp_clog.x.cwnd.cwnd_new_value = error;
368 sctp_clog.x.cwnd.inflight = net->flight_size;
369 sctp_clog.x.cwnd.cwnd_augment = burst;
370 if (stcb->asoc.send_queue_cnt > 255)
371 sctp_clog.x.cwnd.cnt_in_send = 255;
373 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
374 if (stcb->asoc.stream_queue_cnt > 255)
375 sctp_clog.x.cwnd.cnt_in_str = 255;
377 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
378 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
379 SCTP_LOG_EVENT_MAXBURST,
381 sctp_clog.x.misc.log1,
382 sctp_clog.x.misc.log2,
383 sctp_clog.x.misc.log3,
384 sctp_clog.x.misc.log4);
389 sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
391 struct sctp_cwnd_log sctp_clog;
393 sctp_clog.x.rwnd.rwnd = peers_rwnd;
394 sctp_clog.x.rwnd.send_size = snd_size;
395 sctp_clog.x.rwnd.overhead = overhead;
396 sctp_clog.x.rwnd.new_rwnd = 0;
397 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
400 sctp_clog.x.misc.log1,
401 sctp_clog.x.misc.log2,
402 sctp_clog.x.misc.log3,
403 sctp_clog.x.misc.log4);
407 sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
409 struct sctp_cwnd_log sctp_clog;
411 sctp_clog.x.rwnd.rwnd = peers_rwnd;
412 sctp_clog.x.rwnd.send_size = flight_size;
413 sctp_clog.x.rwnd.overhead = overhead;
414 sctp_clog.x.rwnd.new_rwnd = a_rwndval;
415 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
418 sctp_clog.x.misc.log1,
419 sctp_clog.x.misc.log2,
420 sctp_clog.x.misc.log3,
421 sctp_clog.x.misc.log4);
425 sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
427 struct sctp_cwnd_log sctp_clog;
429 sctp_clog.x.mbcnt.total_queue_size = total_oq;
430 sctp_clog.x.mbcnt.size_change = book;
431 sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
432 sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
433 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
434 SCTP_LOG_EVENT_MBCNT,
436 sctp_clog.x.misc.log1,
437 sctp_clog.x.misc.log2,
438 sctp_clog.x.misc.log3,
439 sctp_clog.x.misc.log4);
444 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
446 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
453 sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t cumtsn, uint32_t wake_cnt, int from)
455 struct sctp_cwnd_log sctp_clog;
457 sctp_clog.x.wake.stcb = (void *)stcb;
458 sctp_clog.x.wake.wake_cnt = wake_cnt;
459 sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
460 sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
461 sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
463 if (stcb->asoc.stream_queue_cnt < 0xff)
464 sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
466 sctp_clog.x.wake.stream_qcnt = 0xff;
468 if (stcb->asoc.chunks_on_out_queue < 0xff)
469 sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
471 sctp_clog.x.wake.chunks_on_oque = 0xff;
473 sctp_clog.x.wake.sctpflags = 0;
474 /* set in the defered mode stuff */
475 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
476 sctp_clog.x.wake.sctpflags |= 1;
477 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
478 sctp_clog.x.wake.sctpflags |= 2;
479 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
480 sctp_clog.x.wake.sctpflags |= 4;
481 /* what about the sb */
482 if (stcb->sctp_socket) {
483 struct socket *so = stcb->sctp_socket;
485 sctp_clog.x.wake.sbflags = (uint8_t) ((so->so_snd.sb_flags & 0x00ff));
487 sctp_clog.x.wake.sbflags = 0xff;
489 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
492 sctp_clog.x.misc.log1,
493 sctp_clog.x.misc.log2,
494 sctp_clog.x.misc.log3,
495 sctp_clog.x.misc.log4);
500 sctp_log_block(uint8_t from, struct socket *so, struct sctp_association *asoc, int sendlen)
502 struct sctp_cwnd_log sctp_clog;
504 sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
505 sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
506 sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
507 sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
508 sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
509 sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight / 1024);
510 sctp_clog.x.blk.sndlen = sendlen;
511 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
512 SCTP_LOG_EVENT_BLOCK,
514 sctp_clog.x.misc.log1,
515 sctp_clog.x.misc.log2,
516 sctp_clog.x.misc.log3,
517 sctp_clog.x.misc.log4);
522 sctp_fill_stat_log(void *optval, size_t *optsize)
524 /* May need to fix this if ktrdump does not work */
528 #ifdef SCTP_AUDITING_ENABLED
529 uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
530 static int sctp_audit_indx = 0;
534 sctp_print_audit_report(void)
540 for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
541 if ((sctp_audit_data[i][0] == 0xe0) &&
542 (sctp_audit_data[i][1] == 0x01)) {
545 } else if (sctp_audit_data[i][0] == 0xf0) {
548 } else if ((sctp_audit_data[i][0] == 0xc0) &&
549 (sctp_audit_data[i][1] == 0x01)) {
553 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
554 (uint32_t) sctp_audit_data[i][1]);
559 for (i = 0; i < sctp_audit_indx; i++) {
560 if ((sctp_audit_data[i][0] == 0xe0) &&
561 (sctp_audit_data[i][1] == 0x01)) {
564 } else if (sctp_audit_data[i][0] == 0xf0) {
567 } else if ((sctp_audit_data[i][0] == 0xc0) &&
568 (sctp_audit_data[i][1] == 0x01)) {
572 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
573 (uint32_t) sctp_audit_data[i][1]);
582 sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
583 struct sctp_nets *net)
585 int resend_cnt, tot_out, rep, tot_book_cnt;
586 struct sctp_nets *lnet;
587 struct sctp_tmit_chunk *chk;
589 sctp_audit_data[sctp_audit_indx][0] = 0xAA;
590 sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
592 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
596 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
597 sctp_audit_data[sctp_audit_indx][1] = 0x01;
599 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
605 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
606 sctp_audit_data[sctp_audit_indx][1] = 0x02;
608 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
613 sctp_audit_data[sctp_audit_indx][0] = 0xA1;
614 sctp_audit_data[sctp_audit_indx][1] =
615 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
617 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
622 resend_cnt = tot_out = 0;
623 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
624 if (chk->sent == SCTP_DATAGRAM_RESEND) {
626 } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
627 tot_out += chk->book_size;
631 if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
632 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
633 sctp_audit_data[sctp_audit_indx][1] = 0xA1;
635 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
638 SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
639 resend_cnt, stcb->asoc.sent_queue_retran_cnt);
641 stcb->asoc.sent_queue_retran_cnt = resend_cnt;
642 sctp_audit_data[sctp_audit_indx][0] = 0xA2;
643 sctp_audit_data[sctp_audit_indx][1] =
644 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
646 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
650 if (tot_out != stcb->asoc.total_flight) {
651 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
652 sctp_audit_data[sctp_audit_indx][1] = 0xA2;
654 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
658 SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
659 (int)stcb->asoc.total_flight);
660 stcb->asoc.total_flight = tot_out;
662 if (tot_book_cnt != stcb->asoc.total_flight_count) {
663 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
664 sctp_audit_data[sctp_audit_indx][1] = 0xA5;
666 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
670 SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
672 stcb->asoc.total_flight_count = tot_book_cnt;
675 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
676 tot_out += lnet->flight_size;
678 if (tot_out != stcb->asoc.total_flight) {
679 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
680 sctp_audit_data[sctp_audit_indx][1] = 0xA3;
682 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
686 SCTP_PRINTF("real flight:%d net total was %d\n",
687 stcb->asoc.total_flight, tot_out);
688 /* now corrective action */
689 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
692 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
693 if ((chk->whoTo == lnet) &&
694 (chk->sent < SCTP_DATAGRAM_RESEND)) {
695 tot_out += chk->book_size;
698 if (lnet->flight_size != tot_out) {
699 SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
700 lnet, lnet->flight_size,
702 lnet->flight_size = tot_out;
707 sctp_print_audit_report();
712 sctp_audit_log(uint8_t ev, uint8_t fd)
715 sctp_audit_data[sctp_audit_indx][0] = ev;
716 sctp_audit_data[sctp_audit_indx][1] = fd;
718 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
726 * sctp_stop_timers_for_shutdown() should be called
727 * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
728 * state to make sure that all timers are stopped.
731 sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
733 struct sctp_association *asoc;
734 struct sctp_nets *net;
738 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
739 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
740 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
741 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
742 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
743 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
744 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
745 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
746 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
751 * a list of sizes based on typical mtu's, used only if next hop size not
754 static uint32_t sctp_mtu_sizes[] = {
776 * Return the largest MTU smaller than val. If there is no
777 * entry, just return val.
780 sctp_get_prev_mtu(uint32_t val)
784 if (val <= sctp_mtu_sizes[0]) {
787 for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
788 if (val <= sctp_mtu_sizes[i]) {
792 return (sctp_mtu_sizes[i - 1]);
796 * Return the smallest MTU larger than val. If there is no
797 * entry, just return val.
800 sctp_get_next_mtu(struct sctp_inpcb *inp, uint32_t val)
802 /* select another MTU that is just bigger than this one */
805 for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
806 if (val < sctp_mtu_sizes[i]) {
807 return (sctp_mtu_sizes[i]);
814 sctp_fill_random_store(struct sctp_pcb *m)
817 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
818 * our counter. The result becomes our good random numbers and we
819 * then setup to give these out. Note that we do no locking to
820 * protect this. This is ok, since if competing folks call this we
821 * will get more gobbled gook in the random store which is what we
822 * want. There is a danger that two guys will use the same random
823 * numbers, but thats ok too since that is random as well :->
826 (void)sctp_hmac(SCTP_HMAC, (uint8_t *) m->random_numbers,
827 sizeof(m->random_numbers), (uint8_t *) & m->random_counter,
828 sizeof(m->random_counter), (uint8_t *) m->random_store);
833 sctp_select_initial_TSN(struct sctp_pcb *inp)
836 * A true implementation should use random selection process to get
837 * the initial stream sequence number, using RFC1750 as a good
842 int store_at, new_store;
844 if (inp->initial_sequence_debug != 0) {
847 ret = inp->initial_sequence_debug;
848 inp->initial_sequence_debug++;
852 store_at = inp->store_at;
853 new_store = store_at + sizeof(uint32_t);
854 if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
857 if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
860 if (new_store == 0) {
861 /* Refill the random store */
862 sctp_fill_random_store(inp);
864 p = &inp->random_store[store_at];
871 sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int save_in_twait)
873 uint32_t x, not_done;
876 (void)SCTP_GETTIME_TIMEVAL(&now);
879 x = sctp_select_initial_TSN(&inp->sctp_ep);
884 if (sctp_is_vtag_good(inp, x, lport, rport, &now, save_in_twait)) {
892 sctp_init_asoc(struct sctp_inpcb *m, struct sctp_tcb *stcb,
893 uint32_t override_tag, uint32_t vrf_id)
895 struct sctp_association *asoc;
898 * Anything set to zero is taken care of by the allocation routine's
903 * Up front select what scoping to apply on addresses I tell my peer
904 * Not sure what to do with these right now, we will need to come up
905 * with a way to set them. We may need to pass them through from the
906 * caller in the sctp_aloc_assoc() function.
911 /* init all variables to a known value. */
912 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE);
913 asoc->max_burst = m->sctp_ep.max_burst;
914 asoc->heart_beat_delay = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
915 asoc->cookie_life = m->sctp_ep.def_cookie_life;
916 asoc->sctp_cmt_on_off = m->sctp_cmt_on_off;
917 asoc->sctp_nr_sack_on_off = (uint8_t) SCTP_BASE_SYSCTL(sctp_nr_sack_on_off);
918 asoc->sctp_cmt_pf = (uint8_t) SCTP_BASE_SYSCTL(sctp_cmt_pf);
919 asoc->sctp_frag_point = m->sctp_frag_point;
921 asoc->default_tos = m->ip_inp.inp.inp_ip_tos;
923 asoc->default_tos = 0;
927 asoc->default_flowlabel = ((struct in6pcb *)m)->in6p_flowinfo;
929 asoc->default_flowlabel = 0;
931 asoc->sb_send_resv = 0;
933 asoc->my_vtag = override_tag;
935 asoc->my_vtag = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
937 /* Get the nonce tags */
938 asoc->my_vtag_nonce = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
939 asoc->peer_vtag_nonce = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
940 asoc->vrf_id = vrf_id;
942 if (sctp_is_feature_on(m, SCTP_PCB_FLAGS_DONOT_HEARTBEAT))
943 asoc->hb_is_disabled = 1;
945 asoc->hb_is_disabled = 0;
947 #ifdef SCTP_ASOCLOG_OF_TSNS
949 asoc->tsn_out_at = 0;
950 asoc->tsn_in_wrapped = 0;
951 asoc->tsn_out_wrapped = 0;
952 asoc->cumack_log_at = 0;
953 asoc->cumack_log_atsnt = 0;
955 #ifdef SCTP_FS_SPEC_LOG
959 asoc->assoc_up_sent = 0;
960 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
961 sctp_select_initial_TSN(&m->sctp_ep);
962 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
963 /* we are optimisitic here */
964 asoc->peer_supports_pktdrop = 1;
965 asoc->peer_supports_nat = 0;
966 asoc->sent_queue_retran_cnt = 0;
969 asoc->last_net_cmt_send_started = NULL;
971 /* This will need to be adjusted */
972 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
973 asoc->last_acked_seq = asoc->init_seq_number - 1;
974 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
975 asoc->asconf_seq_in = asoc->last_acked_seq;
977 /* here we are different, we hold the next one we expect */
978 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
980 asoc->initial_init_rto_max = m->sctp_ep.initial_init_rto_max;
981 asoc->initial_rto = m->sctp_ep.initial_rto;
983 asoc->max_init_times = m->sctp_ep.max_init_times;
984 asoc->max_send_times = m->sctp_ep.max_send_times;
985 asoc->def_net_failure = m->sctp_ep.def_net_failure;
986 asoc->free_chunk_cnt = 0;
988 asoc->iam_blocking = 0;
989 /* ECN Nonce initialization */
990 asoc->context = m->sctp_context;
991 asoc->def_send = m->def_send;
992 asoc->ecn_nonce_allowed = 0;
993 asoc->receiver_nonce_sum = 1;
994 asoc->nonce_sum_expect_base = 1;
995 asoc->nonce_sum_check = 1;
996 asoc->nonce_resync_tsn = 0;
997 asoc->nonce_wait_for_ecne = 0;
998 asoc->nonce_wait_tsn = 0;
999 asoc->delayed_ack = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1000 asoc->sack_freq = m->sctp_ep.sctp_sack_freq;
1001 asoc->pr_sctp_cnt = 0;
1002 asoc->total_output_queue_size = 0;
1004 if (m->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1005 struct in6pcb *inp6;
1007 /* Its a V6 socket */
1008 inp6 = (struct in6pcb *)m;
1009 asoc->ipv6_addr_legal = 1;
1010 /* Now look at the binding flag to see if V4 will be legal */
1011 if (SCTP_IPV6_V6ONLY(inp6) == 0) {
1012 asoc->ipv4_addr_legal = 1;
1014 /* V4 addresses are NOT legal on the association */
1015 asoc->ipv4_addr_legal = 0;
1018 /* Its a V4 socket, no - V6 */
1019 asoc->ipv4_addr_legal = 1;
1020 asoc->ipv6_addr_legal = 0;
1023 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(m->sctp_socket), SCTP_MINIMAL_RWND);
1024 asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(m->sctp_socket);
1026 asoc->smallest_mtu = m->sctp_frag_point;
1027 asoc->minrto = m->sctp_ep.sctp_minrto;
1028 asoc->maxrto = m->sctp_ep.sctp_maxrto;
1030 asoc->locked_on_sending = NULL;
1031 asoc->stream_locked_on = 0;
1032 asoc->ecn_echo_cnt_onq = 0;
1033 asoc->stream_locked = 0;
1035 asoc->send_sack = 1;
1037 LIST_INIT(&asoc->sctp_restricted_addrs);
1039 TAILQ_INIT(&asoc->nets);
1040 TAILQ_INIT(&asoc->pending_reply_queue);
1041 TAILQ_INIT(&asoc->asconf_ack_sent);
1042 /* Setup to fill the hb random cache at first HB */
1043 asoc->hb_random_idx = 4;
1045 asoc->sctp_autoclose_ticks = m->sctp_ep.auto_close_time;
1048 * JRS - Pick the default congestion control module based on the
1051 switch (m->sctp_ep.sctp_default_cc_module) {
1052 /* JRS - Standard TCP congestion control */
1053 case SCTP_CC_RFC2581:
1055 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
1056 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
1057 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
1058 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
1059 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
1060 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
1061 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1062 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1063 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
1066 /* JRS - High Speed TCP congestion control (Floyd) */
1069 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
1070 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
1071 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
1072 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
1073 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
1074 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
1075 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1076 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1077 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
1080 /* JRS - HTCP congestion control */
1083 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
1084 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
1085 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
1086 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
1087 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
1088 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
1089 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1090 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1091 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
1094 /* JRS - By default, use RFC2581 */
1097 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
1098 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
1099 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
1100 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
1101 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
1102 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
1103 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
1104 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
1105 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
1111 * Now the stream parameters, here we allocate space for all streams
1112 * that we request by default.
1114 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1115 m->sctp_ep.pre_open_stream_count;
1116 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1117 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1119 if (asoc->strmout == NULL) {
1120 /* big trouble no memory */
1121 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1124 for (i = 0; i < asoc->streamoutcnt; i++) {
1126 * inbound side must be set to 0xffff, also NOTE when we get
1127 * the INIT-ACK back (for INIT sender) we MUST reduce the
1128 * count (streamoutcnt) but first check if we sent to any of
1129 * the upper streams that were dropped (if some were). Those
1130 * that were dropped must be notified to the upper layer as
1133 asoc->strmout[i].next_sequence_sent = 0x0;
1134 TAILQ_INIT(&asoc->strmout[i].outqueue);
1135 asoc->strmout[i].stream_no = i;
1136 asoc->strmout[i].last_msg_incomplete = 0;
1137 asoc->strmout[i].next_spoke.tqe_next = 0;
1138 asoc->strmout[i].next_spoke.tqe_prev = 0;
1140 /* Now the mapping array */
1141 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1142 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1144 if (asoc->mapping_array == NULL) {
1145 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1146 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1149 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1150 SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1152 if (asoc->nr_mapping_array == NULL) {
1153 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1154 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1155 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1158 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1160 /* Now the init of the other outqueues */
1161 TAILQ_INIT(&asoc->free_chunks);
1162 TAILQ_INIT(&asoc->out_wheel);
1163 TAILQ_INIT(&asoc->control_send_queue);
1164 TAILQ_INIT(&asoc->asconf_send_queue);
1165 TAILQ_INIT(&asoc->send_queue);
1166 TAILQ_INIT(&asoc->sent_queue);
1167 TAILQ_INIT(&asoc->reasmqueue);
1168 TAILQ_INIT(&asoc->resetHead);
1169 asoc->max_inbound_streams = m->sctp_ep.max_open_streams_intome;
1170 TAILQ_INIT(&asoc->asconf_queue);
1171 /* authentication fields */
1172 asoc->authinfo.random = NULL;
1173 asoc->authinfo.active_keyid = 0;
1174 asoc->authinfo.assoc_key = NULL;
1175 asoc->authinfo.assoc_keyid = 0;
1176 asoc->authinfo.recv_key = NULL;
1177 asoc->authinfo.recv_keyid = 0;
1178 LIST_INIT(&asoc->shared_keys);
1179 asoc->marked_retrans = 0;
1183 asoc->timoshutdown = 0;
1184 asoc->timoheartbeat = 0;
1185 asoc->timocookie = 0;
1186 asoc->timoshutdownack = 0;
1187 (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1188 asoc->discontinuity_time = asoc->start_time;
1190 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
1191 * freed later when the association is freed.
1197 sctp_print_mapping_array(struct sctp_association *asoc)
1199 unsigned int i, limit;
1201 printf("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
1202 asoc->mapping_array_size,
1203 asoc->mapping_array_base_tsn,
1204 asoc->cumulative_tsn,
1205 asoc->highest_tsn_inside_map,
1206 asoc->highest_tsn_inside_nr_map);
1207 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1208 if (asoc->mapping_array[limit - 1]) {
1212 printf("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1213 for (i = 0; i < limit; i++) {
1214 printf("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1218 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1219 if (asoc->nr_mapping_array[limit - 1]) {
1223 printf("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1224 for (i = 0; i < limit; i++) {
1225 printf("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1232 sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1234 /* mapping array needs to grow */
1235 uint8_t *new_array1, *new_array2;
1238 new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1239 SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1240 SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1241 if ((new_array1 == NULL) || (new_array2 == NULL)) {
1242 /* can't get more, forget it */
1243 SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1245 SCTP_FREE(new_array1, SCTP_M_MAP);
1248 SCTP_FREE(new_array2, SCTP_M_MAP);
1252 memset(new_array1, 0, new_size);
1253 memset(new_array2, 0, new_size);
1254 memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1255 memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1256 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1257 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1258 asoc->mapping_array = new_array1;
1259 asoc->nr_mapping_array = new_array2;
1260 asoc->mapping_array_size = new_size;
1266 sctp_iterator_work(struct sctp_iterator *it)
1268 int iteration_count = 0;
1271 struct sctp_inpcb *tinp;
1273 SCTP_INP_INFO_RLOCK();
1274 SCTP_ITERATOR_LOCK();
1276 SCTP_INP_RLOCK(it->inp);
1277 SCTP_INP_DECR_REF(it->inp);
1279 if (it->inp == NULL) {
1280 /* iterator is complete */
1282 SCTP_ITERATOR_UNLOCK();
1283 SCTP_INP_INFO_RUNLOCK();
1284 if (it->function_atend != NULL) {
1285 (*it->function_atend) (it->pointer, it->val);
1287 SCTP_FREE(it, SCTP_M_ITER);
1294 SCTP_INP_RLOCK(it->inp);
1296 while (((it->pcb_flags) &&
1297 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1298 ((it->pcb_features) &&
1299 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1300 /* endpoint flags or features don't match, so keep looking */
1301 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1302 SCTP_INP_RUNLOCK(it->inp);
1303 goto done_with_iterator;
1306 it->inp = LIST_NEXT(it->inp, sctp_list);
1307 SCTP_INP_RUNLOCK(tinp);
1308 if (it->inp == NULL) {
1309 goto done_with_iterator;
1311 SCTP_INP_RLOCK(it->inp);
1313 /* now go through each assoc which is in the desired state */
1314 if (it->done_current_ep == 0) {
1315 if (it->function_inp != NULL)
1316 inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1317 it->done_current_ep = 1;
1319 if (it->stcb == NULL) {
1320 /* run the per instance function */
1321 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1323 if ((inp_skip) || it->stcb == NULL) {
1324 if (it->function_inp_end != NULL) {
1325 inp_skip = (*it->function_inp_end) (it->inp,
1329 SCTP_INP_RUNLOCK(it->inp);
1333 SCTP_TCB_LOCK(it->stcb);
1334 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1335 /* not in the right state... keep looking */
1336 SCTP_TCB_UNLOCK(it->stcb);
1339 /* see if we have limited out the iterator loop */
1341 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1342 /* Pause to let others grab the lock */
1343 atomic_add_int(&it->stcb->asoc.refcnt, 1);
1344 SCTP_TCB_UNLOCK(it->stcb);
1345 SCTP_INP_INCR_REF(it->inp);
1346 SCTP_INP_RUNLOCK(it->inp);
1347 SCTP_ITERATOR_UNLOCK();
1348 SCTP_INP_INFO_RUNLOCK();
1349 SCTP_INP_INFO_RLOCK();
1350 SCTP_ITERATOR_LOCK();
1351 if (sctp_it_ctl.iterator_flags) {
1352 /* We won't be staying here */
1353 SCTP_INP_DECR_REF(it->inp);
1354 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1355 if (sctp_it_ctl.iterator_flags &
1356 SCTP_ITERATOR_MUST_EXIT) {
1357 goto done_with_iterator;
1359 if (sctp_it_ctl.iterator_flags &
1360 SCTP_ITERATOR_STOP_CUR_IT) {
1361 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1362 goto done_with_iterator;
1364 if (sctp_it_ctl.iterator_flags &
1365 SCTP_ITERATOR_STOP_CUR_INP) {
1366 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1369 /* If we reach here huh? */
1370 printf("Unknown it ctl flag %x\n",
1371 sctp_it_ctl.iterator_flags);
1372 sctp_it_ctl.iterator_flags = 0;
1374 SCTP_INP_RLOCK(it->inp);
1375 SCTP_INP_DECR_REF(it->inp);
1376 SCTP_TCB_LOCK(it->stcb);
1377 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1378 iteration_count = 0;
1380 /* run function on this one */
1381 (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1384 * we lie here, it really needs to have its own type but
1385 * first I must verify that this won't effect things :-0
1387 if (it->no_chunk_output == 0)
1388 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1390 SCTP_TCB_UNLOCK(it->stcb);
1392 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1393 if (it->stcb == NULL) {
1394 /* Run last function */
1395 if (it->function_inp_end != NULL) {
1396 inp_skip = (*it->function_inp_end) (it->inp,
1402 SCTP_INP_RUNLOCK(it->inp);
1404 /* done with all assocs on this endpoint, move on to next endpoint */
1405 it->done_current_ep = 0;
1406 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1409 it->inp = LIST_NEXT(it->inp, sctp_list);
1411 if (it->inp == NULL) {
1412 goto done_with_iterator;
1414 goto select_a_new_ep;
1418 sctp_iterator_worker(void)
1420 struct sctp_iterator *it = NULL;
1422 /* This function is called with the WQ lock in place */
1424 sctp_it_ctl.iterator_running = 1;
1425 sctp_it_ctl.cur_it = it = TAILQ_FIRST(&sctp_it_ctl.iteratorhead);
1427 /* now lets work on this one */
1428 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
1429 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1430 CURVNET_SET(it->vn);
1431 sctp_iterator_work(it);
1434 SCTP_IPI_ITERATOR_WQ_LOCK();
1435 if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) {
1436 sctp_it_ctl.cur_it = NULL;
1439 /* sa_ignore FREED_MEMORY */
1440 sctp_it_ctl.cur_it = it = TAILQ_FIRST(&sctp_it_ctl.iteratorhead);
1442 sctp_it_ctl.iterator_running = 0;
1448 sctp_handle_addr_wq(void)
1450 /* deal with the ADDR wq from the rtsock calls */
1451 struct sctp_laddr *wi;
1452 struct sctp_asconf_iterator *asc;
1454 SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1455 sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
1457 /* Try later, no memory */
1458 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1459 (struct sctp_inpcb *)NULL,
1460 (struct sctp_tcb *)NULL,
1461 (struct sctp_nets *)NULL);
1464 LIST_INIT(&asc->list_of_work);
1467 SCTP_WQ_ADDR_LOCK();
1468 wi = LIST_FIRST(&SCTP_BASE_INFO(addr_wq));
1469 while (wi != NULL) {
1470 LIST_REMOVE(wi, sctp_nxt_addr);
1471 LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
1473 wi = LIST_FIRST(&SCTP_BASE_INFO(addr_wq));
1475 SCTP_WQ_ADDR_UNLOCK();
1477 if (asc->cnt == 0) {
1478 SCTP_FREE(asc, SCTP_M_ASC_IT);
1480 (void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
1481 sctp_asconf_iterator_stcb,
1482 NULL, /* No ep end for boundall */
1483 SCTP_PCB_FLAGS_BOUNDALL,
1484 SCTP_PCB_ANY_FEATURES,
1485 SCTP_ASOC_ANY_STATE,
1487 sctp_asconf_iterator_end, NULL, 0);
1495 sctp_timeout_handler(void *t)
1497 struct sctp_inpcb *inp;
1498 struct sctp_tcb *stcb;
1499 struct sctp_nets *net;
1500 struct sctp_timer *tmr;
1502 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1506 int did_output, type;
1508 tmr = (struct sctp_timer *)t;
1509 inp = (struct sctp_inpcb *)tmr->ep;
1510 stcb = (struct sctp_tcb *)tmr->tcb;
1511 net = (struct sctp_nets *)tmr->net;
1512 CURVNET_SET((struct vnet *)tmr->vnet);
1515 #ifdef SCTP_AUDITING_ENABLED
1516 sctp_audit_log(0xF0, (uint8_t) tmr->type);
1517 sctp_auditing(3, inp, stcb, net);
1520 /* sanity checks... */
1521 if (tmr->self != (void *)tmr) {
1523 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1529 tmr->stopped_from = 0xa001;
1530 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1532 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1538 tmr->stopped_from = 0xa002;
1539 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1543 /* if this is an iterator timeout, get the struct and clear inp */
1544 tmr->stopped_from = 0xa003;
1547 SCTP_INP_INCR_REF(inp);
1548 if ((inp->sctp_socket == 0) &&
1549 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1550 (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1551 (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1552 (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1553 (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1554 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1555 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1556 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1557 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1559 SCTP_INP_DECR_REF(inp);
1564 tmr->stopped_from = 0xa004;
1566 atomic_add_int(&stcb->asoc.refcnt, 1);
1567 if (stcb->asoc.state == 0) {
1568 atomic_add_int(&stcb->asoc.refcnt, -1);
1570 SCTP_INP_DECR_REF(inp);
1576 tmr->stopped_from = 0xa005;
1577 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type);
1578 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1580 SCTP_INP_DECR_REF(inp);
1583 atomic_add_int(&stcb->asoc.refcnt, -1);
1588 tmr->stopped_from = 0xa006;
1591 SCTP_TCB_LOCK(stcb);
1592 atomic_add_int(&stcb->asoc.refcnt, -1);
1593 if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) &&
1594 ((stcb->asoc.state == 0) ||
1595 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1596 SCTP_TCB_UNLOCK(stcb);
1598 SCTP_INP_DECR_REF(inp);
1604 /* record in stopped what t-o occured */
1605 tmr->stopped_from = tmr->type;
1607 /* mark as being serviced now */
1608 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1610 * Callout has been rescheduled.
1614 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1616 * Not active, so no action.
1620 SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1622 /* call the handler for the appropriate timer type */
1623 switch (tmr->type) {
1624 case SCTP_TIMER_TYPE_ZERO_COPY:
1628 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1629 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
1632 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1636 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1637 SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket);
1640 case SCTP_TIMER_TYPE_ADDR_WQ:
1641 sctp_handle_addr_wq();
1643 case SCTP_TIMER_TYPE_SEND:
1644 if ((stcb == NULL) || (inp == NULL)) {
1647 SCTP_STAT_INCR(sctps_timodata);
1648 stcb->asoc.timodata++;
1649 stcb->asoc.num_send_timers_up--;
1650 if (stcb->asoc.num_send_timers_up < 0) {
1651 stcb->asoc.num_send_timers_up = 0;
1653 SCTP_TCB_LOCK_ASSERT(stcb);
1654 cur_oerr = stcb->asoc.overall_error_count;
1655 retcode = sctp_t3rxt_timer(inp, stcb, net);
1657 /* no need to unlock on tcb its gone */
1661 SCTP_TCB_LOCK_ASSERT(stcb);
1662 #ifdef SCTP_AUDITING_ENABLED
1663 sctp_auditing(4, inp, stcb, net);
1665 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1666 if ((stcb->asoc.num_send_timers_up == 0) &&
1667 (stcb->asoc.sent_queue_cnt > 0)
1669 struct sctp_tmit_chunk *chk;
1672 * safeguard. If there on some on the sent queue
1673 * somewhere but no timers running something is
1674 * wrong... so we start a timer on the first chunk
1675 * on the send queue on whatever net it is sent to.
1677 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1678 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1682 case SCTP_TIMER_TYPE_INIT:
1683 if ((stcb == NULL) || (inp == NULL)) {
1686 SCTP_STAT_INCR(sctps_timoinit);
1687 stcb->asoc.timoinit++;
1688 if (sctp_t1init_timer(inp, stcb, net)) {
1689 /* no need to unlock on tcb its gone */
1692 /* We do output but not here */
1695 case SCTP_TIMER_TYPE_RECV:
1696 if ((stcb == NULL) || (inp == NULL)) {
1699 SCTP_STAT_INCR(sctps_timosack);
1700 stcb->asoc.timosack++;
1701 sctp_send_sack(stcb);
1703 #ifdef SCTP_AUDITING_ENABLED
1704 sctp_auditing(4, inp, stcb, net);
1706 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1708 case SCTP_TIMER_TYPE_SHUTDOWN:
1709 if ((stcb == NULL) || (inp == NULL)) {
1712 if (sctp_shutdown_timer(inp, stcb, net)) {
1713 /* no need to unlock on tcb its gone */
1716 SCTP_STAT_INCR(sctps_timoshutdown);
1717 stcb->asoc.timoshutdown++;
1718 #ifdef SCTP_AUDITING_ENABLED
1719 sctp_auditing(4, inp, stcb, net);
1721 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1723 case SCTP_TIMER_TYPE_HEARTBEAT:
1725 struct sctp_nets *lnet;
1726 int cnt_of_unconf = 0;
1728 if ((stcb == NULL) || (inp == NULL)) {
1731 SCTP_STAT_INCR(sctps_timoheartbeat);
1732 stcb->asoc.timoheartbeat++;
1733 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1734 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1735 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
1739 if (cnt_of_unconf == 0) {
1740 if (sctp_heartbeat_timer(inp, stcb, lnet,
1742 /* no need to unlock on tcb its gone */
1746 #ifdef SCTP_AUDITING_ENABLED
1747 sctp_auditing(4, inp, stcb, lnet);
1749 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
1750 stcb->sctp_ep, stcb, lnet);
1751 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1754 case SCTP_TIMER_TYPE_COOKIE:
1755 if ((stcb == NULL) || (inp == NULL)) {
1758 if (sctp_cookie_timer(inp, stcb, net)) {
1759 /* no need to unlock on tcb its gone */
1762 SCTP_STAT_INCR(sctps_timocookie);
1763 stcb->asoc.timocookie++;
1764 #ifdef SCTP_AUDITING_ENABLED
1765 sctp_auditing(4, inp, stcb, net);
1768 * We consider T3 and Cookie timer pretty much the same with
1769 * respect to where from in chunk_output.
1771 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1773 case SCTP_TIMER_TYPE_NEWCOOKIE:
1781 SCTP_STAT_INCR(sctps_timosecret);
1782 (void)SCTP_GETTIME_TIMEVAL(&tv);
1783 SCTP_INP_WLOCK(inp);
1784 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1785 inp->sctp_ep.last_secret_number =
1786 inp->sctp_ep.current_secret_number;
1787 inp->sctp_ep.current_secret_number++;
1788 if (inp->sctp_ep.current_secret_number >=
1789 SCTP_HOW_MANY_SECRETS) {
1790 inp->sctp_ep.current_secret_number = 0;
1792 secret = (int)inp->sctp_ep.current_secret_number;
1793 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1794 inp->sctp_ep.secret_key[secret][i] =
1795 sctp_select_initial_TSN(&inp->sctp_ep);
1797 SCTP_INP_WUNLOCK(inp);
1798 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1802 case SCTP_TIMER_TYPE_PATHMTURAISE:
1803 if ((stcb == NULL) || (inp == NULL)) {
1806 SCTP_STAT_INCR(sctps_timopathmtu);
1807 sctp_pathmtu_timer(inp, stcb, net);
1810 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1811 if ((stcb == NULL) || (inp == NULL)) {
1814 if (sctp_shutdownack_timer(inp, stcb, net)) {
1815 /* no need to unlock on tcb its gone */
1818 SCTP_STAT_INCR(sctps_timoshutdownack);
1819 stcb->asoc.timoshutdownack++;
1820 #ifdef SCTP_AUDITING_ENABLED
1821 sctp_auditing(4, inp, stcb, net);
1823 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1825 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1826 if ((stcb == NULL) || (inp == NULL)) {
1829 SCTP_STAT_INCR(sctps_timoshutdownguard);
1830 sctp_abort_an_association(inp, stcb,
1831 SCTP_SHUTDOWN_GUARD_EXPIRES, NULL, SCTP_SO_NOT_LOCKED);
1832 /* no need to unlock on tcb its gone */
1835 case SCTP_TIMER_TYPE_STRRESET:
1836 if ((stcb == NULL) || (inp == NULL)) {
1839 if (sctp_strreset_timer(inp, stcb, net)) {
1840 /* no need to unlock on tcb its gone */
1843 SCTP_STAT_INCR(sctps_timostrmrst);
1844 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1846 case SCTP_TIMER_TYPE_EARLYFR:
1847 /* Need to do FR of things for net */
1848 if ((stcb == NULL) || (inp == NULL)) {
1851 SCTP_STAT_INCR(sctps_timoearlyfr);
1852 sctp_early_fr_timer(inp, stcb, net);
1854 case SCTP_TIMER_TYPE_ASCONF:
1855 if ((stcb == NULL) || (inp == NULL)) {
1858 if (sctp_asconf_timer(inp, stcb, net)) {
1859 /* no need to unlock on tcb its gone */
1862 SCTP_STAT_INCR(sctps_timoasconf);
1863 #ifdef SCTP_AUDITING_ENABLED
1864 sctp_auditing(4, inp, stcb, net);
1866 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1868 case SCTP_TIMER_TYPE_PRIM_DELETED:
1869 if ((stcb == NULL) || (inp == NULL)) {
1872 sctp_delete_prim_timer(inp, stcb, net);
1873 SCTP_STAT_INCR(sctps_timodelprim);
1876 case SCTP_TIMER_TYPE_AUTOCLOSE:
1877 if ((stcb == NULL) || (inp == NULL)) {
1880 SCTP_STAT_INCR(sctps_timoautoclose);
1881 sctp_autoclose_timer(inp, stcb, net);
1882 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1885 case SCTP_TIMER_TYPE_ASOCKILL:
1886 if ((stcb == NULL) || (inp == NULL)) {
1889 SCTP_STAT_INCR(sctps_timoassockill);
1890 /* Can we free it yet? */
1891 SCTP_INP_DECR_REF(inp);
1892 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1893 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1894 so = SCTP_INP_SO(inp);
1895 atomic_add_int(&stcb->asoc.refcnt, 1);
1896 SCTP_TCB_UNLOCK(stcb);
1897 SCTP_SOCKET_LOCK(so, 1);
1898 SCTP_TCB_LOCK(stcb);
1899 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1901 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1902 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1903 SCTP_SOCKET_UNLOCK(so, 1);
1906 * free asoc, always unlocks (or destroy's) so prevent
1907 * duplicate unlock or unlock of a free mtx :-0
1911 case SCTP_TIMER_TYPE_INPKILL:
1912 SCTP_STAT_INCR(sctps_timoinpkill);
1917 * special case, take away our increment since WE are the
1920 SCTP_INP_DECR_REF(inp);
1921 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1922 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
1923 SCTP_CALLED_FROM_INPKILL_TIMER);
1927 SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1931 #ifdef SCTP_AUDITING_ENABLED
1932 sctp_audit_log(0xF1, (uint8_t) tmr->type);
1934 sctp_auditing(5, inp, stcb, net);
1936 if ((did_output) && stcb) {
1938 * Now we need to clean up the control chunk chain if an
1939 * ECNE is on it. It must be marked as UNSENT again so next
1940 * call will continue to send it until such time that we get
1941 * a CWR, to remove it. It is, however, less likely that we
1942 * will find a ecn echo on the chain though.
1944 sctp_fix_ecn_echo(&stcb->asoc);
1948 SCTP_TCB_UNLOCK(stcb);
1952 SCTP_INP_DECR_REF(inp);
1955 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n",
1961 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1962 struct sctp_nets *net)
1965 struct sctp_timer *tmr;
1967 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1974 SCTP_TCB_LOCK_ASSERT(stcb);
1977 case SCTP_TIMER_TYPE_ZERO_COPY:
1978 tmr = &inp->sctp_ep.zero_copy_timer;
1979 to_ticks = SCTP_ZERO_COPY_TICK_DELAY;
1981 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1982 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
1983 to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY;
1985 case SCTP_TIMER_TYPE_ADDR_WQ:
1986 /* Only 1 tick away :-) */
1987 tmr = &SCTP_BASE_INFO(addr_wq_timer);
1988 to_ticks = SCTP_ADDRESS_TICK_DELAY;
1990 case SCTP_TIMER_TYPE_SEND:
1991 /* Here we use the RTO timer */
1995 if ((stcb == NULL) || (net == NULL)) {
1998 tmr = &net->rxt_timer;
1999 if (net->RTO == 0) {
2000 rto_val = stcb->asoc.initial_rto;
2004 to_ticks = MSEC_TO_TICKS(rto_val);
2007 case SCTP_TIMER_TYPE_INIT:
2009 * Here we use the INIT timer default usually about 1
2012 if ((stcb == NULL) || (net == NULL)) {
2015 tmr = &net->rxt_timer;
2016 if (net->RTO == 0) {
2017 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2019 to_ticks = MSEC_TO_TICKS(net->RTO);
2022 case SCTP_TIMER_TYPE_RECV:
2024 * Here we use the Delayed-Ack timer value from the inp
2025 * ususually about 200ms.
2030 tmr = &stcb->asoc.dack_timer;
2031 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
2033 case SCTP_TIMER_TYPE_SHUTDOWN:
2034 /* Here we use the RTO of the destination. */
2035 if ((stcb == NULL) || (net == NULL)) {
2038 if (net->RTO == 0) {
2039 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2041 to_ticks = MSEC_TO_TICKS(net->RTO);
2043 tmr = &net->rxt_timer;
2045 case SCTP_TIMER_TYPE_HEARTBEAT:
2047 * the net is used here so that we can add in the RTO. Even
2048 * though we use a different timer. We also add the HB timer
2049 * PLUS a random jitter.
2051 if ((inp == NULL) || (stcb == NULL)) {
2055 uint8_t this_random;
2056 int cnt_of_unconf = 0;
2057 struct sctp_nets *lnet;
2059 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
2060 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2061 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
2065 if (cnt_of_unconf) {
2067 (void)sctp_heartbeat_timer(inp, stcb, lnet, cnt_of_unconf);
2069 if (stcb->asoc.hb_random_idx > 3) {
2070 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2071 memcpy(stcb->asoc.hb_random_values, &rndval,
2072 sizeof(stcb->asoc.hb_random_values));
2073 stcb->asoc.hb_random_idx = 0;
2075 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
2076 stcb->asoc.hb_random_idx++;
2077 stcb->asoc.hb_ect_randombit = 0;
2079 * this_random will be 0 - 256 ms RTO is in ms.
2081 if ((stcb->asoc.hb_is_disabled) &&
2082 (cnt_of_unconf == 0)) {
2088 delay = stcb->asoc.heart_beat_delay;
2089 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
2090 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2091 ((lnet->dest_state & SCTP_ADDR_OUT_OF_SCOPE) == 0) &&
2092 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
2096 if (net->RTO == 0) {
2097 /* Never been checked */
2098 to_ticks = this_random + stcb->asoc.initial_rto + delay;
2100 /* set rto_val to the ms */
2101 to_ticks = delay + net->RTO + this_random;
2104 if (cnt_of_unconf) {
2105 to_ticks = this_random + stcb->asoc.initial_rto;
2107 to_ticks = stcb->asoc.heart_beat_delay + this_random + stcb->asoc.initial_rto;
2111 * Now we must convert the to_ticks that are now in
2114 to_ticks = MSEC_TO_TICKS(to_ticks);
2115 tmr = &stcb->asoc.hb_timer;
2118 case SCTP_TIMER_TYPE_COOKIE:
2120 * Here we can use the RTO timer from the network since one
2121 * RTT was compelete. If a retran happened then we will be
2122 * using the RTO initial value.
2124 if ((stcb == NULL) || (net == NULL)) {
2127 if (net->RTO == 0) {
2128 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2130 to_ticks = MSEC_TO_TICKS(net->RTO);
2132 tmr = &net->rxt_timer;
2134 case SCTP_TIMER_TYPE_NEWCOOKIE:
2136 * nothing needed but the endpoint here ususually about 60
2142 tmr = &inp->sctp_ep.signature_change;
2143 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2145 case SCTP_TIMER_TYPE_ASOCKILL:
2149 tmr = &stcb->asoc.strreset_timer;
2150 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2152 case SCTP_TIMER_TYPE_INPKILL:
2154 * The inp is setup to die. We re-use the signature_chage
2155 * timer since that has stopped and we are in the GONE
2161 tmr = &inp->sctp_ep.signature_change;
2162 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2164 case SCTP_TIMER_TYPE_PATHMTURAISE:
2166 * Here we use the value found in the EP for PMTU ususually
2169 if ((stcb == NULL) || (inp == NULL)) {
2175 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2176 tmr = &net->pmtu_timer;
2178 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2179 /* Here we use the RTO of the destination */
2180 if ((stcb == NULL) || (net == NULL)) {
2183 if (net->RTO == 0) {
2184 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2186 to_ticks = MSEC_TO_TICKS(net->RTO);
2188 tmr = &net->rxt_timer;
2190 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2192 * Here we use the endpoints shutdown guard timer usually
2195 if ((inp == NULL) || (stcb == NULL)) {
2198 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2199 tmr = &stcb->asoc.shut_guard_timer;
2201 case SCTP_TIMER_TYPE_STRRESET:
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.strreset_timer;
2217 case SCTP_TIMER_TYPE_EARLYFR:
2221 if ((stcb == NULL) || (net == NULL)) {
2224 if (net->flight_size > net->cwnd) {
2225 /* no need to start */
2228 SCTP_STAT_INCR(sctps_earlyfrstart);
2229 if (net->lastsa == 0) {
2230 /* Hmm no rtt estimate yet? */
2231 msec = stcb->asoc.initial_rto >> 2;
2233 msec = ((net->lastsa >> 2) + net->lastsv) >> 1;
2235 if (msec < SCTP_BASE_SYSCTL(sctp_early_fr_msec)) {
2236 msec = SCTP_BASE_SYSCTL(sctp_early_fr_msec);
2237 if (msec < SCTP_MINFR_MSEC_FLOOR) {
2238 msec = SCTP_MINFR_MSEC_FLOOR;
2241 to_ticks = MSEC_TO_TICKS(msec);
2242 tmr = &net->fr_timer;
2245 case SCTP_TIMER_TYPE_ASCONF:
2247 * Here the timer comes from the stcb but its value is from
2250 if ((stcb == NULL) || (net == NULL)) {
2253 if (net->RTO == 0) {
2254 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2256 to_ticks = MSEC_TO_TICKS(net->RTO);
2258 tmr = &stcb->asoc.asconf_timer;
2260 case SCTP_TIMER_TYPE_PRIM_DELETED:
2261 if ((stcb == NULL) || (net != NULL)) {
2264 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2265 tmr = &stcb->asoc.delete_prim_timer;
2267 case SCTP_TIMER_TYPE_AUTOCLOSE:
2271 if (stcb->asoc.sctp_autoclose_ticks == 0) {
2273 * Really an error since stcb is NOT set to
2278 to_ticks = stcb->asoc.sctp_autoclose_ticks;
2279 tmr = &stcb->asoc.autoclose_timer;
2282 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2283 __FUNCTION__, t_type);
2287 if ((to_ticks <= 0) || (tmr == NULL)) {
2288 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2289 __FUNCTION__, t_type, to_ticks, tmr);
2292 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2294 * we do NOT allow you to have it already running. if it is
2295 * we leave the current one up unchanged
2299 /* At this point we can proceed */
2300 if (t_type == SCTP_TIMER_TYPE_SEND) {
2301 stcb->asoc.num_send_timers_up++;
2303 tmr->stopped_from = 0;
2305 tmr->ep = (void *)inp;
2306 tmr->tcb = (void *)stcb;
2307 tmr->net = (void *)net;
2308 tmr->self = (void *)tmr;
2309 tmr->vnet = (void *)curvnet;
2310 tmr->ticks = sctp_get_tick_count();
2311 (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2316 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2317 struct sctp_nets *net, uint32_t from)
2319 struct sctp_timer *tmr;
2321 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2327 SCTP_TCB_LOCK_ASSERT(stcb);
2330 case SCTP_TIMER_TYPE_ZERO_COPY:
2331 tmr = &inp->sctp_ep.zero_copy_timer;
2333 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
2334 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
2336 case SCTP_TIMER_TYPE_ADDR_WQ:
2337 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2339 case SCTP_TIMER_TYPE_EARLYFR:
2340 if ((stcb == NULL) || (net == NULL)) {
2343 tmr = &net->fr_timer;
2344 SCTP_STAT_INCR(sctps_earlyfrstop);
2346 case SCTP_TIMER_TYPE_SEND:
2347 if ((stcb == NULL) || (net == NULL)) {
2350 tmr = &net->rxt_timer;
2352 case SCTP_TIMER_TYPE_INIT:
2353 if ((stcb == NULL) || (net == NULL)) {
2356 tmr = &net->rxt_timer;
2358 case SCTP_TIMER_TYPE_RECV:
2362 tmr = &stcb->asoc.dack_timer;
2364 case SCTP_TIMER_TYPE_SHUTDOWN:
2365 if ((stcb == NULL) || (net == NULL)) {
2368 tmr = &net->rxt_timer;
2370 case SCTP_TIMER_TYPE_HEARTBEAT:
2374 tmr = &stcb->asoc.hb_timer;
2376 case SCTP_TIMER_TYPE_COOKIE:
2377 if ((stcb == NULL) || (net == NULL)) {
2380 tmr = &net->rxt_timer;
2382 case SCTP_TIMER_TYPE_NEWCOOKIE:
2383 /* nothing needed but the endpoint here */
2384 tmr = &inp->sctp_ep.signature_change;
2386 * We re-use the newcookie timer for the INP kill timer. We
2387 * must assure that we do not kill it by accident.
2390 case SCTP_TIMER_TYPE_ASOCKILL:
2392 * Stop the asoc kill timer.
2397 tmr = &stcb->asoc.strreset_timer;
2400 case SCTP_TIMER_TYPE_INPKILL:
2402 * The inp is setup to die. We re-use the signature_chage
2403 * timer since that has stopped and we are in the GONE
2406 tmr = &inp->sctp_ep.signature_change;
2408 case SCTP_TIMER_TYPE_PATHMTURAISE:
2409 if ((stcb == NULL) || (net == NULL)) {
2412 tmr = &net->pmtu_timer;
2414 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2415 if ((stcb == NULL) || (net == NULL)) {
2418 tmr = &net->rxt_timer;
2420 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2424 tmr = &stcb->asoc.shut_guard_timer;
2426 case SCTP_TIMER_TYPE_STRRESET:
2430 tmr = &stcb->asoc.strreset_timer;
2432 case SCTP_TIMER_TYPE_ASCONF:
2436 tmr = &stcb->asoc.asconf_timer;
2438 case SCTP_TIMER_TYPE_PRIM_DELETED:
2442 tmr = &stcb->asoc.delete_prim_timer;
2444 case SCTP_TIMER_TYPE_AUTOCLOSE:
2448 tmr = &stcb->asoc.autoclose_timer;
2451 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2452 __FUNCTION__, t_type);
2458 if ((tmr->type != t_type) && tmr->type) {
2460 * Ok we have a timer that is under joint use. Cookie timer
2461 * per chance with the SEND timer. We therefore are NOT
2462 * running the timer that the caller wants stopped. So just
2467 if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2468 stcb->asoc.num_send_timers_up--;
2469 if (stcb->asoc.num_send_timers_up < 0) {
2470 stcb->asoc.num_send_timers_up = 0;
2474 tmr->stopped_from = from;
2475 (void)SCTP_OS_TIMER_STOP(&tmr->timer);
2480 sctp_calculate_len(struct mbuf *m)
2487 tlen += SCTP_BUF_LEN(at);
2488 at = SCTP_BUF_NEXT(at);
2494 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2495 struct sctp_association *asoc, uint32_t mtu)
2498 * Reset the P-MTU size on this association, this involves changing
2499 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2500 * allow the DF flag to be cleared.
2502 struct sctp_tmit_chunk *chk;
2503 unsigned int eff_mtu, ovh;
2505 asoc->smallest_mtu = mtu;
2506 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2507 ovh = SCTP_MIN_OVERHEAD;
2509 ovh = SCTP_MIN_V4_OVERHEAD;
2511 eff_mtu = mtu - ovh;
2512 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2513 if (chk->send_size > eff_mtu) {
2514 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2517 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2518 if (chk->send_size > eff_mtu) {
2519 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2526 * given an association and starting time of the current RTT period return
2527 * RTO in number of msecs net should point to the current network
2530 sctp_calculate_rto(struct sctp_tcb *stcb,
2531 struct sctp_association *asoc,
2532 struct sctp_nets *net,
2533 struct timeval *told,
2537 * given an association and the starting time of the current RTT
2538 * period (in value1/value2) return RTO in number of msecs.
2542 uint32_t new_rto = 0;
2543 int first_measure = 0;
2544 struct timeval now, then, *old;
2546 /* Copy it out for sparc64 */
2547 if (safe == sctp_align_unsafe_makecopy) {
2549 memcpy(&then, told, sizeof(struct timeval));
2550 } else if (safe == sctp_align_safe_nocopy) {
2554 SCTP_PRINTF("Huh, bad rto calc call\n");
2557 /************************/
2558 /* 1. calculate new RTT */
2559 /************************/
2560 /* get the current time */
2561 (void)SCTP_GETTIME_TIMEVAL(&now);
2562 /* compute the RTT value */
2563 if ((u_long)now.tv_sec > (u_long)old->tv_sec) {
2564 calc_time = ((u_long)now.tv_sec - (u_long)old->tv_sec) * 1000;
2565 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2566 calc_time += (((u_long)now.tv_usec -
2567 (u_long)old->tv_usec) / 1000);
2568 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2569 /* Borrow 1,000ms from current calculation */
2571 /* Add in the slop over */
2572 calc_time += ((int)now.tv_usec / 1000);
2573 /* Add in the pre-second ms's */
2574 calc_time += (((int)1000000 - (int)old->tv_usec) / 1000);
2576 } else if ((u_long)now.tv_sec == (u_long)old->tv_sec) {
2577 if ((u_long)now.tv_usec > (u_long)old->tv_usec) {
2578 calc_time = ((u_long)now.tv_usec -
2579 (u_long)old->tv_usec) / 1000;
2580 } else if ((u_long)now.tv_usec < (u_long)old->tv_usec) {
2581 /* impossible .. garbage in nothing out */
2583 } else if ((u_long)now.tv_usec == (u_long)old->tv_usec) {
2585 * We have to have 1 usec :-D this must be the
2590 /* impossible .. garbage in nothing out */
2594 /* Clock wrapped? */
2597 /***************************/
2598 /* 2. update RTTVAR & SRTT */
2599 /***************************/
2600 net->rtt = o_calctime = calc_time;
2601 /* this is Van Jacobson's integer version */
2602 if (net->RTO_measured) {
2603 calc_time -= (net->lastsa >> SCTP_RTT_SHIFT); /* take away 1/8th when
2605 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2606 rto_logging(net, SCTP_LOG_RTTVAR);
2608 net->prev_rtt = o_calctime;
2609 net->lastsa += calc_time; /* add 7/8th into sa when
2611 if (calc_time < 0) {
2612 calc_time = -calc_time;
2614 calc_time -= (net->lastsv >> SCTP_RTT_VAR_SHIFT); /* take away 1/4 when
2616 net->lastsv += calc_time;
2617 if (net->lastsv == 0) {
2618 net->lastsv = SCTP_CLOCK_GRANULARITY;
2621 /* First RTO measurment */
2622 net->RTO_measured = 1;
2623 net->lastsa = calc_time << SCTP_RTT_SHIFT; /* Multiply by 8 when
2625 net->lastsv = calc_time;
2626 if (net->lastsv == 0) {
2627 net->lastsv = SCTP_CLOCK_GRANULARITY;
2630 net->prev_rtt = o_calctime;
2631 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2632 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2636 new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2637 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2638 (stcb->asoc.sat_network_lockout == 0)) {
2639 stcb->asoc.sat_network = 1;
2640 } else if ((!first_measure) && stcb->asoc.sat_network) {
2641 stcb->asoc.sat_network = 0;
2642 stcb->asoc.sat_network_lockout = 1;
2644 /* bound it, per C6/C7 in Section 5.3.1 */
2645 if (new_rto < stcb->asoc.minrto) {
2646 new_rto = stcb->asoc.minrto;
2648 if (new_rto > stcb->asoc.maxrto) {
2649 new_rto = stcb->asoc.maxrto;
2651 /* we are now returning the RTO */
2656 * return a pointer to a contiguous piece of data from the given mbuf chain
2657 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2658 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2659 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2662 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2668 if ((off < 0) || (len <= 0))
2671 /* find the desired start location */
2672 while ((m != NULL) && (off > 0)) {
2673 if (off < SCTP_BUF_LEN(m))
2675 off -= SCTP_BUF_LEN(m);
2676 m = SCTP_BUF_NEXT(m);
2681 /* is the current mbuf large enough (eg. contiguous)? */
2682 if ((SCTP_BUF_LEN(m) - off) >= len) {
2683 return (mtod(m, caddr_t)+off);
2685 /* else, it spans more than one mbuf, so save a temp copy... */
2686 while ((m != NULL) && (len > 0)) {
2687 count = min(SCTP_BUF_LEN(m) - off, len);
2688 bcopy(mtod(m, caddr_t)+off, ptr, count);
2692 m = SCTP_BUF_NEXT(m);
2694 if ((m == NULL) && (len > 0))
2697 return ((caddr_t)in_ptr);
2703 struct sctp_paramhdr *
2704 sctp_get_next_param(struct mbuf *m,
2706 struct sctp_paramhdr *pull,
2709 /* This just provides a typed signature to Peter's Pull routine */
2710 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2716 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2719 * add padlen bytes of 0 filled padding to the end of the mbuf. If
2720 * padlen is > 3 this routine will fail.
2726 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
2729 if (padlen <= M_TRAILINGSPACE(m)) {
2731 * The easy way. We hope the majority of the time we hit
2734 dp = (uint8_t *) (mtod(m, caddr_t)+SCTP_BUF_LEN(m));
2735 SCTP_BUF_LEN(m) += padlen;
2737 /* Hard way we must grow the mbuf */
2740 tmp = sctp_get_mbuf_for_msg(padlen, 0, M_DONTWAIT, 1, MT_DATA);
2742 /* Out of space GAK! we are in big trouble. */
2743 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
2746 /* setup and insert in middle */
2747 SCTP_BUF_LEN(tmp) = padlen;
2748 SCTP_BUF_NEXT(tmp) = NULL;
2749 SCTP_BUF_NEXT(m) = tmp;
2750 dp = mtod(tmp, uint8_t *);
2752 /* zero out the pad */
2753 for (i = 0; i < padlen; i++) {
2761 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2763 /* find the last mbuf in chain and pad it */
2768 return (sctp_add_pad_tombuf(last_mbuf, padval));
2771 if (SCTP_BUF_NEXT(m_at) == NULL) {
2772 return (sctp_add_pad_tombuf(m_at, padval));
2774 m_at = SCTP_BUF_NEXT(m_at);
2777 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
2782 sctp_notify_assoc_change(uint32_t event, struct sctp_tcb *stcb,
2783 uint32_t error, void *data, int so_locked
2784 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2789 struct mbuf *m_notify;
2790 struct sctp_assoc_change *sac;
2791 struct sctp_queued_to_read *control;
2793 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2799 * For TCP model AND UDP connected sockets we will send an error up
2800 * when an ABORT comes in.
2802 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2803 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2804 ((event == SCTP_COMM_LOST) || (event == SCTP_CANT_STR_ASSOC))) {
2805 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
2806 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
2807 stcb->sctp_socket->so_error = ECONNREFUSED;
2809 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
2810 stcb->sctp_socket->so_error = ECONNRESET;
2812 /* Wake ANY sleepers */
2813 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2814 so = SCTP_INP_SO(stcb->sctp_ep);
2816 atomic_add_int(&stcb->asoc.refcnt, 1);
2817 SCTP_TCB_UNLOCK(stcb);
2818 SCTP_SOCKET_LOCK(so, 1);
2819 SCTP_TCB_LOCK(stcb);
2820 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2821 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2822 SCTP_SOCKET_UNLOCK(so, 1);
2827 socantrcvmore(stcb->sctp_socket);
2828 sorwakeup(stcb->sctp_socket);
2829 sowwakeup(stcb->sctp_socket);
2830 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2832 SCTP_SOCKET_UNLOCK(so, 1);
2836 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2837 /* event not enabled */
2840 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_change), 0, M_DONTWAIT, 1, MT_DATA);
2841 if (m_notify == NULL)
2844 SCTP_BUF_LEN(m_notify) = 0;
2846 sac = mtod(m_notify, struct sctp_assoc_change *);
2847 sac->sac_type = SCTP_ASSOC_CHANGE;
2849 sac->sac_length = sizeof(struct sctp_assoc_change);
2850 sac->sac_state = event;
2851 sac->sac_error = error;
2852 /* XXX verify these stream counts */
2853 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2854 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2855 sac->sac_assoc_id = sctp_get_associd(stcb);
2856 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_change);
2857 SCTP_BUF_NEXT(m_notify) = NULL;
2858 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2861 if (control == NULL) {
2863 sctp_m_freem(m_notify);
2866 control->length = SCTP_BUF_LEN(m_notify);
2867 /* not that we need this */
2868 control->tail_mbuf = m_notify;
2869 control->spec_flags = M_NOTIFICATION;
2870 sctp_add_to_readq(stcb->sctp_ep, stcb,
2872 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2874 if (event == SCTP_COMM_LOST) {
2875 /* Wake up any sleeper */
2876 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2877 so = SCTP_INP_SO(stcb->sctp_ep);
2879 atomic_add_int(&stcb->asoc.refcnt, 1);
2880 SCTP_TCB_UNLOCK(stcb);
2881 SCTP_SOCKET_LOCK(so, 1);
2882 SCTP_TCB_LOCK(stcb);
2883 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2884 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2885 SCTP_SOCKET_UNLOCK(so, 1);
2890 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
2891 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2893 SCTP_SOCKET_UNLOCK(so, 1);
2900 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2901 struct sockaddr *sa, uint32_t error)
2903 struct mbuf *m_notify;
2904 struct sctp_paddr_change *spc;
2905 struct sctp_queued_to_read *control;
2907 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2908 /* event not enabled */
2911 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_DONTWAIT, 1, MT_DATA);
2912 if (m_notify == NULL)
2914 SCTP_BUF_LEN(m_notify) = 0;
2915 spc = mtod(m_notify, struct sctp_paddr_change *);
2916 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2918 spc->spc_length = sizeof(struct sctp_paddr_change);
2919 switch (sa->sa_family) {
2921 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2926 struct sockaddr_in6 *sin6;
2928 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2930 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2931 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2932 if (sin6->sin6_scope_id == 0) {
2933 /* recover scope_id for user */
2934 (void)sa6_recoverscope(sin6);
2936 /* clear embedded scope_id for user */
2937 in6_clearscope(&sin6->sin6_addr);
2947 spc->spc_state = state;
2948 spc->spc_error = error;
2949 spc->spc_assoc_id = sctp_get_associd(stcb);
2951 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2952 SCTP_BUF_NEXT(m_notify) = NULL;
2954 /* append to socket */
2955 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2958 if (control == NULL) {
2960 sctp_m_freem(m_notify);
2963 control->length = SCTP_BUF_LEN(m_notify);
2964 control->spec_flags = M_NOTIFICATION;
2965 /* not that we need this */
2966 control->tail_mbuf = m_notify;
2967 sctp_add_to_readq(stcb->sctp_ep, stcb,
2969 &stcb->sctp_socket->so_rcv, 1,
2970 SCTP_READ_LOCK_NOT_HELD,
2971 SCTP_SO_NOT_LOCKED);
2976 sctp_notify_send_failed(struct sctp_tcb *stcb, uint32_t error,
2977 struct sctp_tmit_chunk *chk, int so_locked
2978 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2983 struct mbuf *m_notify;
2984 struct sctp_send_failed *ssf;
2985 struct sctp_queued_to_read *control;
2988 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)) {
2989 /* event not enabled */
2992 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 0, M_DONTWAIT, 1, MT_DATA);
2993 if (m_notify == NULL)
2996 length = sizeof(struct sctp_send_failed) + chk->send_size;
2997 length -= sizeof(struct sctp_data_chunk);
2998 SCTP_BUF_LEN(m_notify) = 0;
2999 ssf = mtod(m_notify, struct sctp_send_failed *);
3000 ssf->ssf_type = SCTP_SEND_FAILED;
3001 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
3002 ssf->ssf_flags = SCTP_DATA_UNSENT;
3004 ssf->ssf_flags = SCTP_DATA_SENT;
3005 ssf->ssf_length = length;
3006 ssf->ssf_error = error;
3007 /* not exactly what the user sent in, but should be close :) */
3008 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
3009 ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
3010 ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
3011 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
3012 ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
3013 ssf->ssf_info.sinfo_context = chk->rec.data.context;
3014 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3015 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3019 * trim off the sctp chunk header(it should be there)
3021 if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
3022 m_adj(chk->data, sizeof(struct sctp_data_chunk));
3023 sctp_mbuf_crush(chk->data);
3024 chk->send_size -= sizeof(struct sctp_data_chunk);
3027 SCTP_BUF_NEXT(m_notify) = chk->data;
3028 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
3029 /* Steal off the mbuf */
3032 * For this case, we check the actual socket buffer, since the assoc
3033 * is going away we don't want to overfill the socket buffer for a
3036 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3037 sctp_m_freem(m_notify);
3040 /* append to socket */
3041 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3044 if (control == NULL) {
3046 sctp_m_freem(m_notify);
3049 control->spec_flags = M_NOTIFICATION;
3050 sctp_add_to_readq(stcb->sctp_ep, stcb,
3052 &stcb->sctp_socket->so_rcv, 1,
3053 SCTP_READ_LOCK_NOT_HELD,
3059 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3060 struct sctp_stream_queue_pending *sp, int so_locked
3061 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3066 struct mbuf *m_notify;
3067 struct sctp_send_failed *ssf;
3068 struct sctp_queued_to_read *control;
3071 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)) {
3072 /* event not enabled */
3075 length = sizeof(struct sctp_send_failed) + sp->length;
3076 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 0, M_DONTWAIT, 1, MT_DATA);
3077 if (m_notify == NULL)
3080 SCTP_BUF_LEN(m_notify) = 0;
3081 ssf = mtod(m_notify, struct sctp_send_failed *);
3082 ssf->ssf_type = SCTP_SEND_FAILED;
3083 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
3084 ssf->ssf_flags = SCTP_DATA_UNSENT;
3086 ssf->ssf_flags = SCTP_DATA_SENT;
3087 ssf->ssf_length = length;
3088 ssf->ssf_error = error;
3089 /* not exactly what the user sent in, but should be close :) */
3090 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
3091 ssf->ssf_info.sinfo_stream = sp->stream;
3092 ssf->ssf_info.sinfo_ssn = sp->strseq;
3093 if (sp->some_taken) {
3094 ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3096 ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3098 ssf->ssf_info.sinfo_ppid = sp->ppid;
3099 ssf->ssf_info.sinfo_context = sp->context;
3100 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3101 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3102 SCTP_BUF_NEXT(m_notify) = sp->data;
3103 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
3105 /* Steal off the mbuf */
3108 * For this case, we check the actual socket buffer, since the assoc
3109 * is going away we don't want to overfill the socket buffer for a
3112 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3113 sctp_m_freem(m_notify);
3116 /* append to socket */
3117 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3120 if (control == NULL) {
3122 sctp_m_freem(m_notify);
3125 control->spec_flags = M_NOTIFICATION;
3126 sctp_add_to_readq(stcb->sctp_ep, stcb,
3128 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3134 sctp_notify_adaptation_layer(struct sctp_tcb *stcb,
3137 struct mbuf *m_notify;
3138 struct sctp_adaptation_event *sai;
3139 struct sctp_queued_to_read *control;
3141 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3142 /* event not enabled */
3145 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_DONTWAIT, 1, MT_DATA);
3146 if (m_notify == NULL)
3149 SCTP_BUF_LEN(m_notify) = 0;
3150 sai = mtod(m_notify, struct sctp_adaptation_event *);
3151 sai->sai_type = SCTP_ADAPTATION_INDICATION;
3153 sai->sai_length = sizeof(struct sctp_adaptation_event);
3154 sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3155 sai->sai_assoc_id = sctp_get_associd(stcb);
3157 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3158 SCTP_BUF_NEXT(m_notify) = NULL;
3160 /* append to socket */
3161 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3164 if (control == NULL) {
3166 sctp_m_freem(m_notify);
3169 control->length = SCTP_BUF_LEN(m_notify);
3170 control->spec_flags = M_NOTIFICATION;
3171 /* not that we need this */
3172 control->tail_mbuf = m_notify;
3173 sctp_add_to_readq(stcb->sctp_ep, stcb,
3175 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3178 /* This always must be called with the read-queue LOCKED in the INP */
3180 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3181 uint32_t val, int so_locked
3182 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3187 struct mbuf *m_notify;
3188 struct sctp_pdapi_event *pdapi;
3189 struct sctp_queued_to_read *control;
3192 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3193 /* event not enabled */
3196 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3199 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_DONTWAIT, 1, MT_DATA);
3200 if (m_notify == NULL)
3203 SCTP_BUF_LEN(m_notify) = 0;
3204 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3205 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3206 pdapi->pdapi_flags = 0;
3207 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3208 pdapi->pdapi_indication = error;
3209 pdapi->pdapi_stream = (val >> 16);
3210 pdapi->pdapi_seq = (val & 0x0000ffff);
3211 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3213 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3214 SCTP_BUF_NEXT(m_notify) = NULL;
3215 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3218 if (control == NULL) {
3220 sctp_m_freem(m_notify);
3223 control->spec_flags = M_NOTIFICATION;
3224 control->length = SCTP_BUF_LEN(m_notify);
3225 /* not that we need this */
3226 control->tail_mbuf = m_notify;
3227 control->held_length = 0;
3228 control->length = 0;
3229 sb = &stcb->sctp_socket->so_rcv;
3230 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3231 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
3233 sctp_sballoc(stcb, sb, m_notify);
3234 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3235 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3237 atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify));
3238 control->end_added = 1;
3239 if (stcb->asoc.control_pdapi)
3240 TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
3242 /* we really should not see this case */
3243 TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
3245 if (stcb->sctp_ep && stcb->sctp_socket) {
3246 /* This should always be the case */
3247 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3250 so = SCTP_INP_SO(stcb->sctp_ep);
3252 atomic_add_int(&stcb->asoc.refcnt, 1);
3253 SCTP_TCB_UNLOCK(stcb);
3254 SCTP_SOCKET_LOCK(so, 1);
3255 SCTP_TCB_LOCK(stcb);
3256 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3257 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3258 SCTP_SOCKET_UNLOCK(so, 1);
3263 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3264 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3266 SCTP_SOCKET_UNLOCK(so, 1);
3273 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3275 struct mbuf *m_notify;
3276 struct sctp_shutdown_event *sse;
3277 struct sctp_queued_to_read *control;
3280 * For TCP model AND UDP connected sockets we will send an error up
3281 * when an SHUTDOWN completes
3283 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3284 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3285 /* mark socket closed for read/write and wakeup! */
3286 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3289 so = SCTP_INP_SO(stcb->sctp_ep);
3290 atomic_add_int(&stcb->asoc.refcnt, 1);
3291 SCTP_TCB_UNLOCK(stcb);
3292 SCTP_SOCKET_LOCK(so, 1);
3293 SCTP_TCB_LOCK(stcb);
3294 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3295 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3296 SCTP_SOCKET_UNLOCK(so, 1);
3300 socantsendmore(stcb->sctp_socket);
3301 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3302 SCTP_SOCKET_UNLOCK(so, 1);
3305 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3306 /* event not enabled */
3309 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_DONTWAIT, 1, MT_DATA);
3310 if (m_notify == NULL)
3313 sse = mtod(m_notify, struct sctp_shutdown_event *);
3314 sse->sse_type = SCTP_SHUTDOWN_EVENT;
3316 sse->sse_length = sizeof(struct sctp_shutdown_event);
3317 sse->sse_assoc_id = sctp_get_associd(stcb);
3319 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3320 SCTP_BUF_NEXT(m_notify) = NULL;
3322 /* append to socket */
3323 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3326 if (control == NULL) {
3328 sctp_m_freem(m_notify);
3331 control->spec_flags = M_NOTIFICATION;
3332 control->length = SCTP_BUF_LEN(m_notify);
3333 /* not that we need this */
3334 control->tail_mbuf = m_notify;
3335 sctp_add_to_readq(stcb->sctp_ep, stcb,
3337 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3341 sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3343 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3348 struct mbuf *m_notify;
3349 struct sctp_sender_dry_event *event;
3350 struct sctp_queued_to_read *control;
3352 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_DRYEVNT)) {
3353 /* event not enabled */
3356 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_DONTWAIT, 1, MT_DATA);
3357 if (m_notify == NULL) {
3361 SCTP_BUF_LEN(m_notify) = 0;
3362 event = mtod(m_notify, struct sctp_sender_dry_event *);
3363 event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3364 event->sender_dry_flags = 0;
3365 event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3366 event->sender_dry_assoc_id = sctp_get_associd(stcb);
3368 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3369 SCTP_BUF_NEXT(m_notify) = NULL;
3371 /* append to socket */
3372 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3373 0, 0, 0, 0, 0, 0, m_notify);
3374 if (control == NULL) {
3376 sctp_m_freem(m_notify);
3379 control->length = SCTP_BUF_LEN(m_notify);
3380 control->spec_flags = M_NOTIFICATION;
3381 /* not that we need this */
3382 control->tail_mbuf = m_notify;
3383 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3384 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3389 sctp_notify_stream_reset_add(struct sctp_tcb *stcb, int number_entries, int flag)
3391 struct mbuf *m_notify;
3392 struct sctp_queued_to_read *control;
3393 struct sctp_stream_reset_event *strreset;
3396 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
3397 /* event not enabled */
3400 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3401 if (m_notify == NULL)
3404 SCTP_BUF_LEN(m_notify) = 0;
3405 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3406 if (len > M_TRAILINGSPACE(m_notify)) {
3407 /* never enough room */
3408 sctp_m_freem(m_notify);
3411 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3412 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3413 strreset->strreset_flags = SCTP_STRRESET_ADD_STREAM | flag;
3414 strreset->strreset_length = len;
3415 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3416 strreset->strreset_list[0] = number_entries;
3418 SCTP_BUF_LEN(m_notify) = len;
3419 SCTP_BUF_NEXT(m_notify) = NULL;
3420 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3422 sctp_m_freem(m_notify);
3425 /* append to socket */
3426 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3429 if (control == NULL) {
3431 sctp_m_freem(m_notify);
3434 control->spec_flags = M_NOTIFICATION;
3435 control->length = SCTP_BUF_LEN(m_notify);
3436 /* not that we need this */
3437 control->tail_mbuf = m_notify;
3438 sctp_add_to_readq(stcb->sctp_ep, stcb,
3440 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3445 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3446 int number_entries, uint16_t * list, int flag)
3448 struct mbuf *m_notify;
3449 struct sctp_queued_to_read *control;
3450 struct sctp_stream_reset_event *strreset;
3453 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
3454 /* event not enabled */
3457 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3458 if (m_notify == NULL)
3461 SCTP_BUF_LEN(m_notify) = 0;
3462 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3463 if (len > M_TRAILINGSPACE(m_notify)) {
3464 /* never enough room */
3465 sctp_m_freem(m_notify);
3468 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3469 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3470 if (number_entries == 0) {
3471 strreset->strreset_flags = flag | SCTP_STRRESET_ALL_STREAMS;
3473 strreset->strreset_flags = flag | SCTP_STRRESET_STREAM_LIST;
3475 strreset->strreset_length = len;
3476 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3477 if (number_entries) {
3480 for (i = 0; i < number_entries; i++) {
3481 strreset->strreset_list[i] = ntohs(list[i]);
3484 SCTP_BUF_LEN(m_notify) = len;
3485 SCTP_BUF_NEXT(m_notify) = NULL;
3486 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3488 sctp_m_freem(m_notify);
3491 /* append to socket */
3492 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3495 if (control == NULL) {
3497 sctp_m_freem(m_notify);
3500 control->spec_flags = M_NOTIFICATION;
3501 control->length = SCTP_BUF_LEN(m_notify);
3502 /* not that we need this */
3503 control->tail_mbuf = m_notify;
3504 sctp_add_to_readq(stcb->sctp_ep, stcb,
3506 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3511 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3512 uint32_t error, void *data, int so_locked
3513 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3518 if ((stcb == NULL) ||
3519 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3520 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3521 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3522 /* If the socket is gone we are out of here */
3525 if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3528 if (stcb && ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3529 (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED))) {
3530 if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
3531 (notification == SCTP_NOTIFY_INTERFACE_UP) ||
3532 (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
3533 /* Don't report these in front states */
3537 switch (notification) {
3538 case SCTP_NOTIFY_ASSOC_UP:
3539 if (stcb->asoc.assoc_up_sent == 0) {
3540 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, so_locked);
3541 stcb->asoc.assoc_up_sent = 1;
3543 if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
3544 sctp_notify_adaptation_layer(stcb, error);
3546 if (stcb->asoc.peer_supports_auth == 0) {
3547 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3551 case SCTP_NOTIFY_ASSOC_DOWN:
3552 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, so_locked);
3554 case SCTP_NOTIFY_INTERFACE_DOWN:
3556 struct sctp_nets *net;
3558 net = (struct sctp_nets *)data;
3559 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3560 (struct sockaddr *)&net->ro._l_addr, error);
3563 case SCTP_NOTIFY_INTERFACE_UP:
3565 struct sctp_nets *net;
3567 net = (struct sctp_nets *)data;
3568 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3569 (struct sockaddr *)&net->ro._l_addr, error);
3572 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3574 struct sctp_nets *net;
3576 net = (struct sctp_nets *)data;
3577 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3578 (struct sockaddr *)&net->ro._l_addr, error);
3581 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3582 sctp_notify_send_failed2(stcb, error,
3583 (struct sctp_stream_queue_pending *)data, so_locked);
3585 case SCTP_NOTIFY_DG_FAIL:
3586 sctp_notify_send_failed(stcb, error,
3587 (struct sctp_tmit_chunk *)data, so_locked);
3589 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3593 val = *((uint32_t *) data);
3595 sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3598 case SCTP_NOTIFY_STRDATA_ERR:
3600 case SCTP_NOTIFY_ASSOC_ABORTED:
3601 if ((stcb) && (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3602 ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED))) {
3603 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, NULL, so_locked);
3605 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, NULL, so_locked);
3608 case SCTP_NOTIFY_PEER_OPENED_STREAM:
3610 case SCTP_NOTIFY_STREAM_OPENED_OK:
3612 case SCTP_NOTIFY_ASSOC_RESTART:
3613 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, data, so_locked);
3614 if (stcb->asoc.peer_supports_auth == 0) {
3615 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3619 case SCTP_NOTIFY_HB_RESP:
3621 case SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK:
3622 sctp_notify_stream_reset_add(stcb, error, SCTP_STRRESET_INBOUND_STR);
3624 case SCTP_NOTIFY_STR_RESET_ADD_OK:
3625 sctp_notify_stream_reset_add(stcb, error, SCTP_STRRESET_OUTBOUND_STR);
3627 case SCTP_NOTIFY_STR_RESET_ADD_FAIL:
3628 sctp_notify_stream_reset_add(stcb, error, (SCTP_STRRESET_FAILED | SCTP_STRRESET_OUTBOUND_STR));
3631 case SCTP_NOTIFY_STR_RESET_SEND:
3632 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_OUTBOUND_STR);
3634 case SCTP_NOTIFY_STR_RESET_RECV:
3635 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_INBOUND_STR);
3637 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3638 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_OUTBOUND_STR | SCTP_STRRESET_FAILED));
3640 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3641 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_INBOUND_STR | SCTP_STRRESET_FAILED));
3643 case SCTP_NOTIFY_ASCONF_ADD_IP:
3644 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3647 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3648 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3651 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3652 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3655 case SCTP_NOTIFY_ASCONF_SUCCESS:
3657 case SCTP_NOTIFY_ASCONF_FAILED:
3659 case SCTP_NOTIFY_PEER_SHUTDOWN:
3660 sctp_notify_shutdown_event(stcb);
3662 case SCTP_NOTIFY_AUTH_NEW_KEY:
3663 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY, error,
3664 (uint16_t) (uintptr_t) data,
3667 case SCTP_NOTIFY_AUTH_FREE_KEY:
3668 sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3669 (uint16_t) (uintptr_t) data,
3672 case SCTP_NOTIFY_NO_PEER_AUTH:
3673 sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3674 (uint16_t) (uintptr_t) data,
3677 case SCTP_NOTIFY_SENDER_DRY:
3678 sctp_notify_sender_dry_event(stcb, so_locked);
3681 SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3682 __FUNCTION__, notification, notification);
3688 sctp_report_all_outbound(struct sctp_tcb *stcb, int holds_lock, int so_locked
3689 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3694 struct sctp_association *asoc;
3695 struct sctp_stream_out *outs;
3696 struct sctp_tmit_chunk *chk;
3697 struct sctp_stream_queue_pending *sp;
3705 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3706 /* already being freed */
3709 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3710 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3711 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3714 /* now through all the gunk freeing chunks */
3715 if (holds_lock == 0) {
3716 SCTP_TCB_SEND_LOCK(stcb);
3718 /* sent queue SHOULD be empty */
3719 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3720 chk = TAILQ_FIRST(&asoc->sent_queue);
3722 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3723 asoc->sent_queue_cnt--;
3724 if (chk->data != NULL) {
3725 sctp_free_bufspace(stcb, asoc, chk, 1);
3726 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3727 SCTP_NOTIFY_DATAGRAM_SENT, chk, so_locked);
3729 sctp_m_freem(chk->data);
3733 sctp_free_a_chunk(stcb, chk);
3734 /* sa_ignore FREED_MEMORY */
3735 chk = TAILQ_FIRST(&asoc->sent_queue);
3738 /* pending send queue SHOULD be empty */
3739 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3740 chk = TAILQ_FIRST(&asoc->send_queue);
3742 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3743 asoc->send_queue_cnt--;
3744 if (chk->data != NULL) {
3745 sctp_free_bufspace(stcb, asoc, chk, 1);
3746 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3747 SCTP_NOTIFY_DATAGRAM_UNSENT, chk, so_locked);
3749 sctp_m_freem(chk->data);
3753 sctp_free_a_chunk(stcb, chk);
3754 /* sa_ignore FREED_MEMORY */
3755 chk = TAILQ_FIRST(&asoc->send_queue);
3758 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3759 /* For each stream */
3760 outs = &stcb->asoc.strmout[i];
3761 /* clean up any sends there */
3762 stcb->asoc.locked_on_sending = NULL;
3763 sp = TAILQ_FIRST(&outs->outqueue);
3765 stcb->asoc.stream_queue_cnt--;
3766 TAILQ_REMOVE(&outs->outqueue, sp, next);
3767 sctp_free_spbufspace(stcb, asoc, sp);
3769 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3770 SCTP_NOTIFY_DATAGRAM_UNSENT, (void *)sp, so_locked);
3772 sctp_m_freem(sp->data);
3777 sctp_free_remote_addr(sp->net);
3780 /* Free the chunk */
3781 sctp_free_a_strmoq(stcb, sp);
3782 /* sa_ignore FREED_MEMORY */
3783 sp = TAILQ_FIRST(&outs->outqueue);
3787 if (holds_lock == 0) {
3788 SCTP_TCB_SEND_UNLOCK(stcb);
3793 sctp_abort_notification(struct sctp_tcb *stcb, int error, int so_locked
3794 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3803 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3804 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3805 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3808 /* Tell them we lost the asoc */
3809 sctp_report_all_outbound(stcb, 1, so_locked);
3810 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3811 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3812 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3813 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3815 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_ABORTED, stcb, error, NULL, so_locked);
3819 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3820 struct mbuf *m, int iphlen, struct sctphdr *sh, struct mbuf *op_err,
3821 uint32_t vrf_id, uint16_t port)
3825 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3832 /* We have a TCB to abort, send notification too */
3833 vtag = stcb->asoc.peer_vtag;
3834 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
3835 /* get the assoc vrf id and table id */
3836 vrf_id = stcb->asoc.vrf_id;
3837 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3839 sctp_send_abort(m, iphlen, sh, vtag, op_err, vrf_id, port);
3841 /* Ok, now lets free it */
3842 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3843 so = SCTP_INP_SO(inp);
3844 atomic_add_int(&stcb->asoc.refcnt, 1);
3845 SCTP_TCB_UNLOCK(stcb);
3846 SCTP_SOCKET_LOCK(so, 1);
3847 SCTP_TCB_LOCK(stcb);
3848 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3850 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3851 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3852 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3853 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3855 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
3856 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3857 SCTP_SOCKET_UNLOCK(so, 1);
3862 #ifdef SCTP_ASOCLOG_OF_TSNS
3864 sctp_print_out_track_log(struct sctp_tcb *stcb)
3869 SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
3870 SCTP_PRINTF("IN bound TSN log-aaa\n");
3871 if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
3872 SCTP_PRINTF("None rcvd\n");
3875 if (stcb->asoc.tsn_in_wrapped) {
3876 for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
3877 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3878 stcb->asoc.in_tsnlog[i].tsn,
3879 stcb->asoc.in_tsnlog[i].strm,
3880 stcb->asoc.in_tsnlog[i].seq,
3881 stcb->asoc.in_tsnlog[i].flgs,
3882 stcb->asoc.in_tsnlog[i].sz);
3885 if (stcb->asoc.tsn_in_at) {
3886 for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
3887 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3888 stcb->asoc.in_tsnlog[i].tsn,
3889 stcb->asoc.in_tsnlog[i].strm,
3890 stcb->asoc.in_tsnlog[i].seq,
3891 stcb->asoc.in_tsnlog[i].flgs,
3892 stcb->asoc.in_tsnlog[i].sz);
3896 SCTP_PRINTF("OUT bound TSN log-aaa\n");
3897 if ((stcb->asoc.tsn_out_at == 0) &&
3898 (stcb->asoc.tsn_out_wrapped == 0)) {
3899 SCTP_PRINTF("None sent\n");
3901 if (stcb->asoc.tsn_out_wrapped) {
3902 for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
3903 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3904 stcb->asoc.out_tsnlog[i].tsn,
3905 stcb->asoc.out_tsnlog[i].strm,
3906 stcb->asoc.out_tsnlog[i].seq,
3907 stcb->asoc.out_tsnlog[i].flgs,
3908 stcb->asoc.out_tsnlog[i].sz);
3911 if (stcb->asoc.tsn_out_at) {
3912 for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
3913 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3914 stcb->asoc.out_tsnlog[i].tsn,
3915 stcb->asoc.out_tsnlog[i].strm,
3916 stcb->asoc.out_tsnlog[i].seq,
3917 stcb->asoc.out_tsnlog[i].flgs,
3918 stcb->asoc.out_tsnlog[i].sz);
3927 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3928 int error, struct mbuf *op_err,
3930 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3937 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3942 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3943 so = SCTP_INP_SO(inp);
3946 /* Got to have a TCB */
3947 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3948 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3949 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3950 SCTP_CALLED_DIRECTLY_NOCMPSET);
3955 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3957 vtag = stcb->asoc.peer_vtag;
3958 /* notify the ulp */
3959 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0)
3960 sctp_abort_notification(stcb, error, so_locked);
3961 /* notify the peer */
3962 #if defined(SCTP_PANIC_ON_ABORT)
3963 panic("aborting an association");
3965 sctp_send_abort_tcb(stcb, op_err, so_locked);
3966 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3967 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3968 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3969 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3971 /* now free the asoc */
3972 #ifdef SCTP_ASOCLOG_OF_TSNS
3973 sctp_print_out_track_log(stcb);
3975 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3977 atomic_add_int(&stcb->asoc.refcnt, 1);
3978 SCTP_TCB_UNLOCK(stcb);
3979 SCTP_SOCKET_LOCK(so, 1);
3980 SCTP_TCB_LOCK(stcb);
3981 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3984 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
3985 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3987 SCTP_SOCKET_UNLOCK(so, 1);
3993 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
3994 struct sctp_inpcb *inp, struct mbuf *op_err, uint32_t vrf_id, uint16_t port)
3996 struct sctp_chunkhdr *ch, chunk_buf;
3997 unsigned int chk_length;
3999 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4000 /* Generate a TO address for future reference */
4001 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4002 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
4003 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4004 SCTP_CALLED_DIRECTLY_NOCMPSET);
4007 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4008 sizeof(*ch), (uint8_t *) & chunk_buf);
4009 while (ch != NULL) {
4010 chk_length = ntohs(ch->chunk_length);
4011 if (chk_length < sizeof(*ch)) {
4012 /* break to abort land */
4015 switch (ch->chunk_type) {
4016 case SCTP_COOKIE_ECHO:
4017 /* We hit here only if the assoc is being freed */
4019 case SCTP_PACKET_DROPPED:
4020 /* we don't respond to pkt-dropped */
4022 case SCTP_ABORT_ASSOCIATION:
4023 /* we don't respond with an ABORT to an ABORT */
4025 case SCTP_SHUTDOWN_COMPLETE:
4027 * we ignore it since we are not waiting for it and
4031 case SCTP_SHUTDOWN_ACK:
4032 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
4037 offset += SCTP_SIZE32(chk_length);
4038 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4039 sizeof(*ch), (uint8_t *) & chunk_buf);
4041 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
4045 * check the inbound datagram to make sure there is not an abort inside it,
4046 * if there is return 1, else return 0.
4049 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
4051 struct sctp_chunkhdr *ch;
4052 struct sctp_init_chunk *init_chk, chunk_buf;
4054 unsigned int chk_length;
4056 offset = iphlen + sizeof(struct sctphdr);
4057 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4058 (uint8_t *) & chunk_buf);
4059 while (ch != NULL) {
4060 chk_length = ntohs(ch->chunk_length);
4061 if (chk_length < sizeof(*ch)) {
4062 /* packet is probably corrupt */
4065 /* we seem to be ok, is it an abort? */
4066 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4067 /* yep, tell them */
4070 if (ch->chunk_type == SCTP_INITIATION) {
4071 /* need to update the Vtag */
4072 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4073 offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
4074 if (init_chk != NULL) {
4075 *vtagfill = ntohl(init_chk->init.initiate_tag);
4078 /* Nope, move to the next chunk */
4079 offset += SCTP_SIZE32(chk_length);
4080 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4081 sizeof(*ch), (uint8_t *) & chunk_buf);
4087 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4088 * set (i.e. it's 0) so, create this function to compare link local scopes
4092 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4094 struct sockaddr_in6 a, b;
4100 if (a.sin6_scope_id == 0)
4101 if (sa6_recoverscope(&a)) {
4102 /* can't get scope, so can't match */
4105 if (b.sin6_scope_id == 0)
4106 if (sa6_recoverscope(&b)) {
4107 /* can't get scope, so can't match */
4110 if (a.sin6_scope_id != b.sin6_scope_id)
4117 * returns a sockaddr_in6 with embedded scope recovered and removed
4119 struct sockaddr_in6 *
4120 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4122 /* check and strip embedded scope junk */
4123 if (addr->sin6_family == AF_INET6) {
4124 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4125 if (addr->sin6_scope_id == 0) {
4127 if (!sa6_recoverscope(store)) {
4128 /* use the recovered scope */
4132 /* else, return the original "to" addr */
4133 in6_clearscope(&addr->sin6_addr);
4143 * are the two addresses the same? currently a "scopeless" check returns: 1
4147 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4151 if (sa1 == NULL || sa2 == NULL)
4154 /* must be the same family */
4155 if (sa1->sa_family != sa2->sa_family)
4158 switch (sa1->sa_family) {
4162 /* IPv6 addresses */
4163 struct sockaddr_in6 *sin6_1, *sin6_2;
4165 sin6_1 = (struct sockaddr_in6 *)sa1;
4166 sin6_2 = (struct sockaddr_in6 *)sa2;
4167 return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4173 /* IPv4 addresses */
4174 struct sockaddr_in *sin_1, *sin_2;
4176 sin_1 = (struct sockaddr_in *)sa1;
4177 sin_2 = (struct sockaddr_in *)sa2;
4178 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
4181 /* we don't do these... */
4187 sctp_print_address(struct sockaddr *sa)
4190 char ip6buf[INET6_ADDRSTRLEN];
4195 switch (sa->sa_family) {
4199 struct sockaddr_in6 *sin6;
4201 sin6 = (struct sockaddr_in6 *)sa;
4202 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
4203 ip6_sprintf(ip6buf, &sin6->sin6_addr),
4204 ntohs(sin6->sin6_port),
4205 sin6->sin6_scope_id);
4211 struct sockaddr_in *sin;
4214 sin = (struct sockaddr_in *)sa;
4215 p = (unsigned char *)&sin->sin_addr;
4216 SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4217 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
4227 sctp_print_address_pkt(struct ip *iph, struct sctphdr *sh)
4229 switch (iph->ip_v) {
4232 struct sockaddr_in lsa, fsa;
4234 bzero(&lsa, sizeof(lsa));
4235 lsa.sin_len = sizeof(lsa);
4236 lsa.sin_family = AF_INET;
4237 lsa.sin_addr = iph->ip_src;
4238 lsa.sin_port = sh->src_port;
4239 bzero(&fsa, sizeof(fsa));
4240 fsa.sin_len = sizeof(fsa);
4241 fsa.sin_family = AF_INET;
4242 fsa.sin_addr = iph->ip_dst;
4243 fsa.sin_port = sh->dest_port;
4244 SCTP_PRINTF("src: ");
4245 sctp_print_address((struct sockaddr *)&lsa);
4246 SCTP_PRINTF("dest: ");
4247 sctp_print_address((struct sockaddr *)&fsa);
4251 case IPV6_VERSION >> 4:
4253 struct ip6_hdr *ip6;
4254 struct sockaddr_in6 lsa6, fsa6;
4256 ip6 = (struct ip6_hdr *)iph;
4257 bzero(&lsa6, sizeof(lsa6));
4258 lsa6.sin6_len = sizeof(lsa6);
4259 lsa6.sin6_family = AF_INET6;
4260 lsa6.sin6_addr = ip6->ip6_src;
4261 lsa6.sin6_port = sh->src_port;
4262 bzero(&fsa6, sizeof(fsa6));
4263 fsa6.sin6_len = sizeof(fsa6);
4264 fsa6.sin6_family = AF_INET6;
4265 fsa6.sin6_addr = ip6->ip6_dst;
4266 fsa6.sin6_port = sh->dest_port;
4267 SCTP_PRINTF("src: ");
4268 sctp_print_address((struct sockaddr *)&lsa6);
4269 SCTP_PRINTF("dest: ");
4270 sctp_print_address((struct sockaddr *)&fsa6);
4281 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4282 struct sctp_inpcb *new_inp,
4283 struct sctp_tcb *stcb,
4287 * go through our old INP and pull off any control structures that
4288 * belong to stcb and move then to the new inp.
4290 struct socket *old_so, *new_so;
4291 struct sctp_queued_to_read *control, *nctl;
4292 struct sctp_readhead tmp_queue;
4296 old_so = old_inp->sctp_socket;
4297 new_so = new_inp->sctp_socket;
4298 TAILQ_INIT(&tmp_queue);
4299 error = sblock(&old_so->so_rcv, waitflags);
4302 * Gak, can't get sblock, we have a problem. data will be
4303 * left stranded.. and we don't dare look at it since the
4304 * other thread may be reading something. Oh well, its a
4305 * screwed up app that does a peeloff OR a accept while
4306 * reading from the main socket... actually its only the
4307 * peeloff() case, since I think read will fail on a
4308 * listening socket..
4312 /* lock the socket buffers */
4313 SCTP_INP_READ_LOCK(old_inp);
4314 control = TAILQ_FIRST(&old_inp->read_queue);
4315 /* Pull off all for out target stcb */
4317 nctl = TAILQ_NEXT(control, next);
4318 if (control->stcb == stcb) {
4319 /* remove it we want it */
4320 TAILQ_REMOVE(&old_inp->read_queue, control, next);
4321 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4324 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4325 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
4327 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4328 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4329 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4331 m = SCTP_BUF_NEXT(m);
4336 SCTP_INP_READ_UNLOCK(old_inp);
4337 /* Remove the sb-lock on the old socket */
4339 sbunlock(&old_so->so_rcv);
4340 /* Now we move them over to the new socket buffer */
4341 control = TAILQ_FIRST(&tmp_queue);
4342 SCTP_INP_READ_LOCK(new_inp);
4344 nctl = TAILQ_NEXT(control, next);
4345 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4348 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4349 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4351 sctp_sballoc(stcb, &new_so->so_rcv, m);
4352 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4353 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4355 m = SCTP_BUF_NEXT(m);
4359 SCTP_INP_READ_UNLOCK(new_inp);
4363 sctp_add_to_readq(struct sctp_inpcb *inp,
4364 struct sctp_tcb *stcb,
4365 struct sctp_queued_to_read *control,
4368 int inp_read_lock_held,
4370 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4376 * Here we must place the control on the end of the socket read
4377 * queue AND increment sb_cc so that select will work properly on
4380 struct mbuf *m, *prev = NULL;
4385 panic("Gak, inp NULL on add_to_readq");
4389 if (inp_read_lock_held == 0)
4390 SCTP_INP_READ_LOCK(inp);
4391 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4392 sctp_free_remote_addr(control->whoFrom);
4393 if (control->data) {
4394 sctp_m_freem(control->data);
4395 control->data = NULL;
4397 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4398 if (inp_read_lock_held == 0)
4399 SCTP_INP_READ_UNLOCK(inp);
4402 if (!(control->spec_flags & M_NOTIFICATION)) {
4403 atomic_add_int(&inp->total_recvs, 1);
4404 if (!control->do_not_ref_stcb) {
4405 atomic_add_int(&stcb->total_recvs, 1);
4409 control->held_length = 0;
4410 control->length = 0;
4412 if (SCTP_BUF_LEN(m) == 0) {
4413 /* Skip mbufs with NO length */
4416 control->data = sctp_m_free(m);
4419 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4420 m = SCTP_BUF_NEXT(prev);
4423 control->tail_mbuf = prev;
4428 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4429 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4431 sctp_sballoc(stcb, sb, m);
4432 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4433 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4435 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4436 m = SCTP_BUF_NEXT(m);
4439 control->tail_mbuf = prev;
4441 /* Everything got collapsed out?? */
4442 sctp_free_remote_addr(control->whoFrom);
4443 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4444 if (inp_read_lock_held == 0)
4445 SCTP_INP_READ_UNLOCK(inp);
4449 control->end_added = 1;
4451 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4452 if (inp_read_lock_held == 0)
4453 SCTP_INP_READ_UNLOCK(inp);
4454 if (inp && inp->sctp_socket) {
4455 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
4456 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4458 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4461 so = SCTP_INP_SO(inp);
4463 atomic_add_int(&stcb->asoc.refcnt, 1);
4464 SCTP_TCB_UNLOCK(stcb);
4465 SCTP_SOCKET_LOCK(so, 1);
4466 SCTP_TCB_LOCK(stcb);
4467 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4468 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4469 SCTP_SOCKET_UNLOCK(so, 1);
4474 sctp_sorwakeup(inp, inp->sctp_socket);
4475 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4477 SCTP_SOCKET_UNLOCK(so, 1);
4486 sctp_append_to_readq(struct sctp_inpcb *inp,
4487 struct sctp_tcb *stcb,
4488 struct sctp_queued_to_read *control,
4495 * A partial delivery API event is underway. OR we are appending on
4496 * the reassembly queue.
4498 * If PDAPI this means we need to add m to the end of the data.
4499 * Increase the length in the control AND increment the sb_cc.
4500 * Otherwise sb is NULL and all we need to do is put it at the end
4501 * of the mbuf chain.
4504 struct mbuf *mm, *tail = NULL, *prev = NULL;
4507 SCTP_INP_READ_LOCK(inp);
4509 if (control == NULL) {
4512 SCTP_INP_READ_UNLOCK(inp);
4516 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) {
4517 SCTP_INP_READ_UNLOCK(inp);
4520 if (control->end_added) {
4521 /* huh this one is complete? */
4529 if (SCTP_BUF_LEN(mm) == 0) {
4530 /* Skip mbufs with NO lenght */
4533 m = sctp_m_free(mm);
4536 SCTP_BUF_NEXT(prev) = sctp_m_free(mm);
4537 mm = SCTP_BUF_NEXT(prev);
4542 len += SCTP_BUF_LEN(mm);
4544 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4545 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm));
4547 sctp_sballoc(stcb, sb, mm);
4548 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4549 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4552 mm = SCTP_BUF_NEXT(mm);
4557 /* Really there should always be a prev */
4559 /* Huh nothing left? */
4561 panic("Nothing left to add?");
4568 if (control->tail_mbuf) {
4570 SCTP_BUF_NEXT(control->tail_mbuf) = m;
4571 control->tail_mbuf = tail;
4575 if (control->data != NULL) {
4576 panic("This should NOT happen");
4580 control->tail_mbuf = tail;
4582 atomic_add_int(&control->length, len);
4584 /* message is complete */
4585 if (stcb && (control == stcb->asoc.control_pdapi)) {
4586 stcb->asoc.control_pdapi = NULL;
4588 control->held_length = 0;
4589 control->end_added = 1;
4592 control->do_not_ref_stcb = 1;
4595 * When we are appending in partial delivery, the cum-ack is used
4596 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
4597 * is populated in the outbound sinfo structure from the true cumack
4598 * if the association exists...
4600 control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
4602 SCTP_INP_READ_UNLOCK(inp);
4604 if (inp && inp->sctp_socket) {
4605 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
4606 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4608 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4611 so = SCTP_INP_SO(inp);
4612 atomic_add_int(&stcb->asoc.refcnt, 1);
4613 SCTP_TCB_UNLOCK(stcb);
4614 SCTP_SOCKET_LOCK(so, 1);
4615 SCTP_TCB_LOCK(stcb);
4616 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4617 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4618 SCTP_SOCKET_UNLOCK(so, 1);
4622 sctp_sorwakeup(inp, inp->sctp_socket);
4623 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4624 SCTP_SOCKET_UNLOCK(so, 1);
4633 /*************HOLD THIS COMMENT FOR PATCH FILE OF
4634 *************ALTERNATE ROUTING CODE
4637 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4638 *************ALTERNATE ROUTING CODE
4642 sctp_generate_invmanparam(int err)
4644 /* Return a MBUF with a invalid mandatory parameter */
4647 m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
4649 struct sctp_paramhdr *ph;
4651 SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
4652 ph = mtod(m, struct sctp_paramhdr *);
4653 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4654 ph->param_type = htons(err);
4659 #ifdef SCTP_MBCNT_LOGGING
4661 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4662 struct sctp_tmit_chunk *tp1, int chk_cnt)
4664 if (tp1->data == NULL) {
4667 asoc->chunks_on_out_queue -= chk_cnt;
4668 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4669 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4670 asoc->total_output_queue_size,
4675 if (asoc->total_output_queue_size >= tp1->book_size) {
4676 atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4678 asoc->total_output_queue_size = 0;
4681 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4682 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4683 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4684 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4686 stcb->sctp_socket->so_snd.sb_cc = 0;
4695 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4696 int reason, int so_locked
4697 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4702 struct sctp_stream_out *strq;
4703 struct sctp_tmit_chunk *chk = NULL;
4704 struct sctp_stream_queue_pending *sp;
4705 uint16_t stream = 0, seq = 0;
4706 uint8_t foundeom = 0;
4709 int do_wakeup_routine = 0;
4711 stream = tp1->rec.data.stream_number;
4712 seq = tp1->rec.data.stream_seq;
4714 ret_sz += tp1->book_size;
4715 if (tp1->data != NULL) {
4716 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4717 sctp_flight_size_decrease(tp1);
4718 sctp_total_flight_decrease(stcb, tp1);
4720 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4721 stcb->asoc.peers_rwnd += tp1->send_size;
4722 stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
4723 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, reason, tp1, so_locked);
4725 sctp_m_freem(tp1->data);
4728 do_wakeup_routine = 1;
4729 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4730 stcb->asoc.sent_queue_cnt_removeable--;
4733 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4734 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4735 SCTP_DATA_NOT_FRAG) {
4736 /* not frag'ed we ae done */
4739 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4740 /* end of frag, we are done */
4745 * Its a begin or middle piece, we must mark all of
4749 tp1 = TAILQ_NEXT(tp1, sctp_next);
4751 } while (tp1 && notdone);
4752 if (foundeom == 0) {
4754 * The multi-part message was scattered across the send and
4758 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
4760 * recurse throught the send_queue too, starting at the
4764 (tp1->rec.data.stream_number == stream) &&
4765 (tp1->rec.data.stream_seq == seq)) {
4767 * save to chk in case we have some on stream out
4768 * queue. If so and we have an un-transmitted one we
4769 * don't have to fudge the TSN.
4772 ret_sz += tp1->book_size;
4773 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4774 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, reason, tp1, so_locked);
4776 sctp_m_freem(tp1->data);
4779 /* No flight involved here book the size to 0 */
4781 if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4784 do_wakeup_routine = 1;
4785 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4786 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4788 * on to the sent queue so we can wait for it to be
4791 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4793 stcb->asoc.send_queue_cnt--;
4794 stcb->asoc.sent_queue_cnt++;
4798 if (foundeom == 0) {
4800 * Still no eom found. That means there is stuff left on the
4801 * stream out queue.. yuck.
4803 strq = &stcb->asoc.strmout[stream];
4804 SCTP_TCB_SEND_LOCK(stcb);
4805 sp = TAILQ_FIRST(&strq->outqueue);
4806 while (sp->strseq <= seq) {
4807 /* Check if its our SEQ */
4808 if (sp->strseq == seq) {
4809 sp->discard_rest = 1;
4811 * We may need to put a chunk on the queue
4812 * that holds the TSN that would have been
4813 * sent with the LAST bit.
4816 /* Yep, we have to */
4817 sctp_alloc_a_chunk(stcb, chk);
4820 * we are hosed. All we can
4821 * do is nothing.. which
4822 * will cause an abort if
4823 * the peer is paying
4828 memset(chk, 0, sizeof(*chk));
4829 chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG;
4830 chk->sent = SCTP_FORWARD_TSN_SKIP;
4831 chk->asoc = &stcb->asoc;
4832 chk->rec.data.stream_seq = sp->strseq;
4833 chk->rec.data.stream_number = sp->stream;
4834 chk->rec.data.payloadtype = sp->ppid;
4835 chk->rec.data.context = sp->context;
4836 chk->flags = sp->act_flags;
4838 chk->whoTo = sp->net;
4840 chk->whoTo = stcb->asoc.primary_destination;
4841 atomic_add_int(&chk->whoTo->ref_count, 1);
4842 chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
4843 stcb->asoc.pr_sctp_cnt++;
4844 chk->pr_sctp_on = 1;
4845 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
4846 stcb->asoc.sent_queue_cnt++;
4847 stcb->asoc.pr_sctp_cnt++;
4849 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4854 * Pull any data to free up the SB
4855 * and allow sender to "add more"
4856 * whilc we will throw away :-)
4858 sctp_free_spbufspace(stcb, &stcb->asoc,
4860 ret_sz += sp->length;
4861 do_wakeup_routine = 1;
4863 sctp_m_freem(sp->data);
4866 sp->tail_mbuf = NULL;
4870 /* Next one please */
4871 sp = TAILQ_NEXT(sp, next);
4874 SCTP_TCB_SEND_UNLOCK(stcb);
4876 if (do_wakeup_routine) {
4877 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4880 so = SCTP_INP_SO(stcb->sctp_ep);
4882 atomic_add_int(&stcb->asoc.refcnt, 1);
4883 SCTP_TCB_UNLOCK(stcb);
4884 SCTP_SOCKET_LOCK(so, 1);
4885 SCTP_TCB_LOCK(stcb);
4886 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4887 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4888 /* assoc was freed while we were unlocked */
4889 SCTP_SOCKET_UNLOCK(so, 1);
4894 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
4895 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4897 SCTP_SOCKET_UNLOCK(so, 1);
4905 * checks to see if the given address, sa, is one that is currently known by
4906 * the kernel note: can't distinguish the same address on multiple interfaces
4907 * and doesn't handle multiple addresses with different zone/scope id's note:
4908 * ifa_ifwithaddr() compares the entire sockaddr struct
4911 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
4914 struct sctp_laddr *laddr;
4916 if (holds_lock == 0) {
4917 SCTP_INP_RLOCK(inp);
4919 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4920 if (laddr->ifa == NULL)
4922 if (addr->sa_family != laddr->ifa->address.sa.sa_family)
4924 if (addr->sa_family == AF_INET) {
4925 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4926 laddr->ifa->address.sin.sin_addr.s_addr) {
4928 if (holds_lock == 0) {
4929 SCTP_INP_RUNLOCK(inp);
4931 return (laddr->ifa);
4936 if (addr->sa_family == AF_INET6) {
4937 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4938 &laddr->ifa->address.sin6)) {
4940 if (holds_lock == 0) {
4941 SCTP_INP_RUNLOCK(inp);
4943 return (laddr->ifa);
4949 if (holds_lock == 0) {
4950 SCTP_INP_RUNLOCK(inp);
4956 sctp_get_ifa_hash_val(struct sockaddr *addr)
4958 if (addr->sa_family == AF_INET) {
4959 struct sockaddr_in *sin;
4961 sin = (struct sockaddr_in *)addr;
4962 return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
4963 } else if (addr->sa_family == AF_INET6) {
4964 struct sockaddr_in6 *sin6;
4965 uint32_t hash_of_addr;
4967 sin6 = (struct sockaddr_in6 *)addr;
4968 hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
4969 sin6->sin6_addr.s6_addr32[1] +
4970 sin6->sin6_addr.s6_addr32[2] +
4971 sin6->sin6_addr.s6_addr32[3]);
4972 hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
4973 return (hash_of_addr);
4979 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
4981 struct sctp_ifa *sctp_ifap;
4982 struct sctp_vrf *vrf;
4983 struct sctp_ifalist *hash_head;
4984 uint32_t hash_of_addr;
4986 if (holds_lock == 0)
4987 SCTP_IPI_ADDR_RLOCK();
4989 vrf = sctp_find_vrf(vrf_id);
4992 if (holds_lock == 0)
4993 SCTP_IPI_ADDR_RUNLOCK();
4996 hash_of_addr = sctp_get_ifa_hash_val(addr);
4998 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
4999 if (hash_head == NULL) {
5000 SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5001 hash_of_addr, (uint32_t) vrf->vrf_addr_hashmark,
5002 (uint32_t) (hash_of_addr & vrf->vrf_addr_hashmark));
5003 sctp_print_address(addr);
5004 SCTP_PRINTF("No such bucket for address\n");
5005 if (holds_lock == 0)
5006 SCTP_IPI_ADDR_RUNLOCK();
5010 LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5011 if (sctp_ifap == NULL) {
5013 panic("Huh LIST_FOREACH corrupt");
5016 SCTP_PRINTF("LIST corrupt of sctp_ifap's?\n");
5020 if (addr->sa_family != sctp_ifap->address.sa.sa_family)
5022 if (addr->sa_family == AF_INET) {
5023 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5024 sctp_ifap->address.sin.sin_addr.s_addr) {
5026 if (holds_lock == 0)
5027 SCTP_IPI_ADDR_RUNLOCK();
5033 if (addr->sa_family == AF_INET6) {
5034 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5035 &sctp_ifap->address.sin6)) {
5037 if (holds_lock == 0)
5038 SCTP_IPI_ADDR_RUNLOCK();
5045 if (holds_lock == 0)
5046 SCTP_IPI_ADDR_RUNLOCK();
5051 sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t * freed_so_far, int hold_rlock,
5054 /* User pulled some data, do we need a rwnd update? */
5057 struct socket *so = NULL;
5062 atomic_add_int(&stcb->asoc.refcnt, 1);
5064 if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
5065 SCTP_STATE_SHUTDOWN_RECEIVED |
5066 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5067 /* Pre-check If we are freeing no update */
5070 SCTP_INP_INCR_REF(stcb->sctp_ep);
5071 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5072 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5075 so = stcb->sctp_socket;
5079 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5080 /* Have you have freed enough to look */
5082 /* Yep, its worth a look and the lock overhead */
5084 /* Figure out what the rwnd would be */
5085 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5086 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5087 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5091 if (dif >= rwnd_req) {
5093 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5096 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5098 * One last check before we allow the guy possibly
5099 * to get in. There is a race, where the guy has not
5100 * reached the gate. In that case
5104 SCTP_TCB_LOCK(stcb);
5105 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5106 /* No reports here */
5107 SCTP_TCB_UNLOCK(stcb);
5110 SCTP_STAT_INCR(sctps_wu_sacks_sent);
5111 sctp_send_sack(stcb);
5113 sctp_chunk_output(stcb->sctp_ep, stcb,
5114 SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5115 /* make sure no timer is running */
5116 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5117 SCTP_TCB_UNLOCK(stcb);
5119 /* Update how much we have pending */
5120 stcb->freed_by_sorcv_sincelast = dif;
5123 if (so && r_unlocked && hold_rlock) {
5124 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5126 SCTP_INP_DECR_REF(stcb->sctp_ep);
5128 atomic_add_int(&stcb->asoc.refcnt, -1);
5133 sctp_sorecvmsg(struct socket *so,
5136 struct sockaddr *from,
5139 struct sctp_sndrcvinfo *sinfo,
5143 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5144 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5145 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5146 * On the way out we may send out any combination of:
5147 * MSG_NOTIFICATION MSG_EOR
5150 struct sctp_inpcb *inp = NULL;
5152 int cp_len = 0, error = 0;
5153 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
5154 struct mbuf *m = NULL;
5155 struct sctp_tcb *stcb = NULL;
5156 int wakeup_read_socket = 0;
5157 int freecnt_applied = 0;
5158 int out_flags = 0, in_flags = 0;
5159 int block_allowed = 1;
5160 uint32_t freed_so_far = 0;
5161 uint32_t copied_so_far = 0;
5162 int in_eeor_mode = 0;
5163 int no_rcv_needed = 0;
5164 uint32_t rwnd_req = 0;
5165 int hold_sblock = 0;
5168 uint32_t held_length = 0;
5169 int sockbuf_lock = 0;
5172 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5176 in_flags = *msg_flags;
5177 if (in_flags & MSG_PEEK)
5178 SCTP_STAT_INCR(sctps_read_peeks);
5182 slen = uio->uio_resid;
5184 /* Pull in and set up our int flags */
5185 if (in_flags & MSG_OOB) {
5186 /* Out of band's NOT supported */
5187 return (EOPNOTSUPP);
5189 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5190 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5193 if ((in_flags & (MSG_DONTWAIT
5196 SCTP_SO_IS_NBIO(so)) {
5199 /* setup the endpoint */
5200 inp = (struct sctp_inpcb *)so->so_pcb;
5202 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5205 rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5206 /* Must be at least a MTU's worth */
5207 if (rwnd_req < SCTP_MIN_RWND)
5208 rwnd_req = SCTP_MIN_RWND;
5209 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5210 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5211 sctp_misc_ints(SCTP_SORECV_ENTER,
5212 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
5214 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5215 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5216 rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
5218 error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5221 goto release_unlocked;
5227 if (hold_sblock == 0) {
5228 SOCKBUF_LOCK(&so->so_rcv);
5231 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5232 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5235 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5237 error = so->so_error;
5238 if ((in_flags & MSG_PEEK) == 0)
5242 if (so->so_rcv.sb_cc == 0) {
5243 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5250 if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
5251 /* we need to wait for data */
5252 if ((so->so_rcv.sb_cc == 0) &&
5253 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5254 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5255 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5257 * For active open side clear flags for
5258 * re-use passive open is blocked by
5261 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5263 * You were aborted, passive side
5266 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5269 * You get this once if you are
5272 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5274 * Remove flag if on the
5277 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
5280 so->so_state &= ~(SS_ISCONNECTING |
5281 SS_ISDISCONNECTING |
5285 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5286 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5289 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
5295 error = sbwait(&so->so_rcv);
5300 goto restart_nosblocks;
5301 } else if (so->so_rcv.sb_cc == 0) {
5303 error = so->so_error;
5304 if ((in_flags & MSG_PEEK) == 0)
5307 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5308 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5309 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5311 * For active open side clear flags
5312 * for re-use passive open is
5313 * blocked by connect.
5315 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5317 * You were aborted, passive
5318 * side always hits here
5320 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5323 * You get this once if you
5324 * are active open side
5326 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5332 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
5335 so->so_state &= ~(SS_ISCONNECTING |
5336 SS_ISDISCONNECTING |
5340 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5341 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5344 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
5350 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5351 error = EWOULDBLOCK;
5355 if (hold_sblock == 1) {
5356 SOCKBUF_UNLOCK(&so->so_rcv);
5359 /* we possibly have data we can read */
5360 /* sa_ignore FREED_MEMORY */
5361 control = TAILQ_FIRST(&inp->read_queue);
5362 if (control == NULL) {
5364 * This could be happening since the appender did the
5365 * increment but as not yet did the tailq insert onto the
5368 if (hold_rlock == 0) {
5369 SCTP_INP_READ_LOCK(inp);
5372 control = TAILQ_FIRST(&inp->read_queue);
5373 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5375 panic("Huh, its non zero and nothing on control?");
5377 so->so_rcv.sb_cc = 0;
5379 SCTP_INP_READ_UNLOCK(inp);
5383 if ((control->length == 0) &&
5384 (control->do_not_ref_stcb)) {
5386 * Clean up code for freeing assoc that left behind a
5387 * pdapi.. maybe a peer in EEOR that just closed after
5388 * sending and never indicated a EOR.
5390 if (hold_rlock == 0) {
5392 SCTP_INP_READ_LOCK(inp);
5394 control->held_length = 0;
5395 if (control->data) {
5396 /* Hmm there is data here .. fix */
5400 m_tmp = control->data;
5402 cnt += SCTP_BUF_LEN(m_tmp);
5403 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5404 control->tail_mbuf = m_tmp;
5405 control->end_added = 1;
5407 m_tmp = SCTP_BUF_NEXT(m_tmp);
5409 control->length = cnt;
5412 TAILQ_REMOVE(&inp->read_queue, control, next);
5413 /* Add back any hiddend data */
5414 sctp_free_remote_addr(control->whoFrom);
5415 sctp_free_a_readq(stcb, control);
5419 SCTP_INP_READ_UNLOCK(inp);
5423 if ((control->length == 0) &&
5424 (control->end_added == 1)) {
5426 * Do we also need to check for (control->pdapi_aborted ==
5429 if (hold_rlock == 0) {
5431 SCTP_INP_READ_LOCK(inp);
5433 TAILQ_REMOVE(&inp->read_queue, control, next);
5434 if (control->data) {
5436 panic("control->data not null but control->length == 0");
5438 SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5439 sctp_m_freem(control->data);
5440 control->data = NULL;
5443 if (control->aux_data) {
5444 sctp_m_free(control->aux_data);
5445 control->aux_data = NULL;
5447 sctp_free_remote_addr(control->whoFrom);
5448 sctp_free_a_readq(stcb, control);
5451 SCTP_INP_READ_UNLOCK(inp);
5455 if (control->length == 0) {
5456 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5458 /* find a more suitable one then this */
5459 ctl = TAILQ_NEXT(control, next);
5461 if ((ctl->stcb != control->stcb) && (ctl->length) &&
5463 (ctl->spec_flags & M_NOTIFICATION) ||
5464 ((ctl->do_not_ref_stcb == 0) &&
5465 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
5468 * If we have a different TCB next, and there is data
5469 * present. If we have already taken some (pdapi), OR we can
5470 * ref the tcb and no delivery as started on this stream, we
5471 * take it. Note we allow a notification on a different
5472 * assoc to be delivered..
5476 } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
5478 ((ctl->some_taken) ||
5479 ((ctl->do_not_ref_stcb == 0) &&
5480 ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5481 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
5483 * If we have the same tcb, and there is data present, and we
5484 * have the strm interleave feature present. Then if we have
5485 * taken some (pdapi) or we can refer to tht tcb AND we have
5486 * not started a delivery for this stream, we can take it.
5487 * Note we do NOT allow a notificaiton on the same assoc to
5493 ctl = TAILQ_NEXT(ctl, next);
5497 * if we reach here, not suitable replacement is available
5498 * <or> fragment interleave is NOT on. So stuff the sb_cc
5499 * into the our held count, and its time to sleep again.
5501 held_length = so->so_rcv.sb_cc;
5502 control->held_length = so->so_rcv.sb_cc;
5505 /* Clear the held length since there is something to read */
5506 control->held_length = 0;
5508 SCTP_INP_READ_UNLOCK(inp);
5513 * If we reach here, control has a some data for us to read off.
5514 * Note that stcb COULD be NULL.
5516 control->some_taken++;
5518 SOCKBUF_UNLOCK(&so->so_rcv);
5521 stcb = control->stcb;
5523 if ((control->do_not_ref_stcb == 0) &&
5524 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5525 if (freecnt_applied == 0)
5527 } else if (control->do_not_ref_stcb == 0) {
5528 /* you can't free it on me please */
5530 * The lock on the socket buffer protects us so the
5531 * free code will stop. But since we used the
5532 * socketbuf lock and the sender uses the tcb_lock
5533 * to increment, we need to use the atomic add to
5536 if (freecnt_applied) {
5538 panic("refcnt already incremented");
5540 printf("refcnt already incremented?\n");
5543 atomic_add_int(&stcb->asoc.refcnt, 1);
5544 freecnt_applied = 1;
5547 * Setup to remember how much we have not yet told
5548 * the peer our rwnd has opened up. Note we grab the
5549 * value from the tcb from last time. Note too that
5550 * sack sending clears this when a sack is sent,
5551 * which is fine. Once we hit the rwnd_req, we then
5552 * will go to the sctp_user_rcvd() that will not
5553 * lock until it KNOWs it MUST send a WUP-SACK.
5555 freed_so_far = stcb->freed_by_sorcv_sincelast;
5556 stcb->freed_by_sorcv_sincelast = 0;
5560 ((control->spec_flags & M_NOTIFICATION) == 0) &&
5561 control->do_not_ref_stcb == 0) {
5562 stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5564 /* First lets get off the sinfo and sockaddr info */
5565 if ((sinfo) && filling_sinfo) {
5566 memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
5567 nxt = TAILQ_NEXT(control, next);
5568 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
5569 struct sctp_extrcvinfo *s_extra;
5571 s_extra = (struct sctp_extrcvinfo *)sinfo;
5574 s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5575 if (nxt->sinfo_flags & SCTP_UNORDERED) {
5576 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5578 if (nxt->spec_flags & M_NOTIFICATION) {
5579 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5581 s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id;
5582 s_extra->sreinfo_next_length = nxt->length;
5583 s_extra->sreinfo_next_ppid = nxt->sinfo_ppid;
5584 s_extra->sreinfo_next_stream = nxt->sinfo_stream;
5585 if (nxt->tail_mbuf != NULL) {
5586 if (nxt->end_added) {
5587 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5592 * we explicitly 0 this, since the memcpy
5593 * got some other things beyond the older
5594 * sinfo_ that is on the control's structure
5598 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
5599 s_extra->sreinfo_next_aid = 0;
5600 s_extra->sreinfo_next_length = 0;
5601 s_extra->sreinfo_next_ppid = 0;
5602 s_extra->sreinfo_next_stream = 0;
5606 * update off the real current cum-ack, if we have an stcb.
5608 if ((control->do_not_ref_stcb == 0) && stcb)
5609 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5611 * mask off the high bits, we keep the actual chunk bits in
5614 sinfo->sinfo_flags &= 0x00ff;
5615 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
5616 sinfo->sinfo_flags |= SCTP_UNORDERED;
5619 #ifdef SCTP_ASOCLOG_OF_TSNS
5621 int index, newindex;
5622 struct sctp_pcbtsn_rlog *entry;
5625 index = inp->readlog_index;
5626 newindex = index + 1;
5627 if (newindex >= SCTP_READ_LOG_SIZE) {
5630 } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
5631 entry = &inp->readlog[index];
5632 entry->vtag = control->sinfo_assoc_id;
5633 entry->strm = control->sinfo_stream;
5634 entry->seq = control->sinfo_ssn;
5635 entry->sz = control->length;
5636 entry->flgs = control->sinfo_flags;
5639 if (fromlen && from) {
5640 struct sockaddr *to;
5643 cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sin.sin_len);
5644 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
5645 ((struct sockaddr_in *)from)->sin_port = control->port_from;
5647 /* No AF_INET use AF_INET6 */
5648 cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sin6.sin6_len);
5649 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
5650 ((struct sockaddr_in6 *)from)->sin6_port = control->port_from;
5654 #if defined(INET) && defined(INET6)
5655 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
5656 (to->sa_family == AF_INET) &&
5657 ((size_t)fromlen >= sizeof(struct sockaddr_in6))) {
5658 struct sockaddr_in *sin;
5659 struct sockaddr_in6 sin6;
5661 sin = (struct sockaddr_in *)to;
5662 bzero(&sin6, sizeof(sin6));
5663 sin6.sin6_family = AF_INET6;
5664 sin6.sin6_len = sizeof(struct sockaddr_in6);
5665 sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
5666 bcopy(&sin->sin_addr,
5667 &sin6.sin6_addr.s6_addr32[3],
5668 sizeof(sin6.sin6_addr.s6_addr32[3]));
5669 sin6.sin6_port = sin->sin_port;
5670 memcpy(from, (caddr_t)&sin6, sizeof(sin6));
5675 struct sockaddr_in6 lsa6, *to6;
5677 to6 = (struct sockaddr_in6 *)to;
5678 sctp_recover_scope_mac(to6, (&lsa6));
5682 /* now copy out what data we can */
5684 /* copy out each mbuf in the chain up to length */
5688 /* Move out all we can */
5689 cp_len = (int)uio->uio_resid;
5690 my_len = (int)SCTP_BUF_LEN(m);
5691 if (cp_len > my_len) {
5692 /* not enough in this buf */
5696 SCTP_INP_READ_UNLOCK(inp);
5700 error = uiomove(mtod(m, char *), cp_len, uio);
5702 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5705 if ((control->do_not_ref_stcb == 0) && stcb &&
5706 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5710 /* error we are out of here */
5713 if ((SCTP_BUF_NEXT(m) == NULL) &&
5714 (cp_len >= SCTP_BUF_LEN(m)) &&
5715 ((control->end_added == 0) ||
5716 (control->end_added &&
5717 (TAILQ_NEXT(control, next) == NULL)))
5719 SCTP_INP_READ_LOCK(inp);
5722 if (cp_len == SCTP_BUF_LEN(m)) {
5723 if ((SCTP_BUF_NEXT(m) == NULL) &&
5724 (control->end_added)) {
5725 out_flags |= MSG_EOR;
5726 if ((control->do_not_ref_stcb == 0) &&
5727 (control->stcb != NULL) &&
5728 ((control->spec_flags & M_NOTIFICATION) == 0))
5729 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5731 if (control->spec_flags & M_NOTIFICATION) {
5732 out_flags |= MSG_NOTIFICATION;
5734 /* we ate up the mbuf */
5735 if (in_flags & MSG_PEEK) {
5737 m = SCTP_BUF_NEXT(m);
5738 copied_so_far += cp_len;
5740 /* dispose of the mbuf */
5741 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5742 sctp_sblog(&so->so_rcv,
5743 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5745 sctp_sbfree(control, stcb, &so->so_rcv, m);
5746 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5747 sctp_sblog(&so->so_rcv,
5748 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5750 copied_so_far += cp_len;
5751 freed_so_far += cp_len;
5752 freed_so_far += MSIZE;
5753 atomic_subtract_int(&control->length, cp_len);
5754 control->data = sctp_m_free(m);
5757 * been through it all, must hold sb
5758 * lock ok to null tail
5760 if (control->data == NULL) {
5762 if ((control->end_added == 0) ||
5763 (TAILQ_NEXT(control, next) == NULL)) {
5771 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5772 panic("Hmm we don't own the lock?");
5776 control->tail_mbuf = NULL;
5778 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5779 panic("end_added, nothing left and no MSG_EOR");
5785 /* Do we need to trim the mbuf? */
5786 if (control->spec_flags & M_NOTIFICATION) {
5787 out_flags |= MSG_NOTIFICATION;
5789 if ((in_flags & MSG_PEEK) == 0) {
5790 SCTP_BUF_RESV_UF(m, cp_len);
5791 SCTP_BUF_LEN(m) -= cp_len;
5792 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5793 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
5795 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5796 if ((control->do_not_ref_stcb == 0) &&
5798 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5800 copied_so_far += cp_len;
5801 freed_so_far += cp_len;
5802 freed_so_far += MSIZE;
5803 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5804 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5805 SCTP_LOG_SBRESULT, 0);
5807 atomic_subtract_int(&control->length, cp_len);
5809 copied_so_far += cp_len;
5812 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5815 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5816 (control->do_not_ref_stcb == 0) &&
5817 (freed_so_far >= rwnd_req)) {
5818 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5820 } /* end while(m) */
5822 * At this point we have looked at it all and we either have
5823 * a MSG_EOR/or read all the user wants... <OR>
5824 * control->length == 0.
5826 if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5827 /* we are done with this control */
5828 if (control->length == 0) {
5829 if (control->data) {
5831 panic("control->data not null at read eor?");
5833 SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5834 sctp_m_freem(control->data);
5835 control->data = NULL;
5839 if (TAILQ_NEXT(control, next) == NULL) {
5841 * If we don't have a next we need a
5842 * lock, if there is a next
5843 * interrupt is filling ahead of us
5844 * and we don't need a lock to
5845 * remove this guy (which is the
5846 * head of the queue).
5848 if (hold_rlock == 0) {
5849 SCTP_INP_READ_LOCK(inp);
5853 TAILQ_REMOVE(&inp->read_queue, control, next);
5854 /* Add back any hiddend data */
5855 if (control->held_length) {
5857 control->held_length = 0;
5858 wakeup_read_socket = 1;
5860 if (control->aux_data) {
5861 sctp_m_free(control->aux_data);
5862 control->aux_data = NULL;
5864 no_rcv_needed = control->do_not_ref_stcb;
5865 sctp_free_remote_addr(control->whoFrom);
5866 control->data = NULL;
5867 sctp_free_a_readq(stcb, control);
5869 if ((freed_so_far >= rwnd_req) &&
5870 (no_rcv_needed == 0))
5871 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5875 * The user did not read all of this
5876 * message, turn off the returned MSG_EOR
5877 * since we are leaving more behind on the
5881 if (control->end_added &&
5882 (control->data == NULL) &&
5883 (control->tail_mbuf == NULL)) {
5884 panic("Gak, control->length is corrupt?");
5887 no_rcv_needed = control->do_not_ref_stcb;
5888 out_flags &= ~MSG_EOR;
5891 if (out_flags & MSG_EOR) {
5894 if ((uio->uio_resid == 0) ||
5895 ((in_eeor_mode) && (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))
5900 * If I hit here the receiver wants more and this message is
5901 * NOT done (pd-api). So two questions. Can we block? if not
5902 * we are done. Did the user NOT set MSG_WAITALL?
5904 if (block_allowed == 0) {
5908 * We need to wait for more data a few things: - We don't
5909 * sbunlock() so we don't get someone else reading. - We
5910 * must be sure to account for the case where what is added
5911 * is NOT to our control when we wakeup.
5915 * Do we need to tell the transport a rwnd update might be
5916 * needed before we go to sleep?
5918 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5919 ((freed_so_far >= rwnd_req) &&
5920 (control->do_not_ref_stcb == 0) &&
5921 (no_rcv_needed == 0))) {
5922 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5925 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5928 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5931 if (hold_rlock == 1) {
5932 SCTP_INP_READ_UNLOCK(inp);
5935 if (hold_sblock == 0) {
5936 SOCKBUF_LOCK(&so->so_rcv);
5939 if ((copied_so_far) && (control->length == 0) &&
5940 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
5943 if (so->so_rcv.sb_cc <= control->held_length) {
5944 error = sbwait(&so->so_rcv);
5948 control->held_length = 0;
5951 SOCKBUF_UNLOCK(&so->so_rcv);
5954 if (control->length == 0) {
5955 /* still nothing here */
5956 if (control->end_added == 1) {
5957 /* he aborted, or is done i.e.did a shutdown */
5958 out_flags |= MSG_EOR;
5959 if (control->pdapi_aborted) {
5960 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5961 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5963 out_flags |= MSG_TRUNC;
5965 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5966 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5968 goto done_with_control;
5970 if (so->so_rcv.sb_cc > held_length) {
5971 control->held_length = so->so_rcv.sb_cc;
5974 goto wait_some_more;
5975 } else if (control->data == NULL) {
5977 * we must re-sync since data is probably being
5980 SCTP_INP_READ_LOCK(inp);
5981 if ((control->length > 0) && (control->data == NULL)) {
5983 * big trouble.. we have the lock and its
5987 panic("Impossible data==NULL length !=0");
5989 out_flags |= MSG_EOR;
5990 out_flags |= MSG_TRUNC;
5991 control->length = 0;
5992 SCTP_INP_READ_UNLOCK(inp);
5993 goto done_with_control;
5995 SCTP_INP_READ_UNLOCK(inp);
5996 /* We will fall around to get more data */
6001 * Give caller back the mbuf chain,
6002 * store in uio_resid the length
6004 wakeup_read_socket = 0;
6005 if ((control->end_added == 0) ||
6006 (TAILQ_NEXT(control, next) == NULL)) {
6007 /* Need to get rlock */
6008 if (hold_rlock == 0) {
6009 SCTP_INP_READ_LOCK(inp);
6013 if (control->end_added) {
6014 out_flags |= MSG_EOR;
6015 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6016 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6018 if (control->spec_flags & M_NOTIFICATION) {
6019 out_flags |= MSG_NOTIFICATION;
6021 uio->uio_resid = control->length;
6022 *mp = control->data;
6025 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6026 sctp_sblog(&so->so_rcv,
6027 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
6029 sctp_sbfree(control, stcb, &so->so_rcv, m);
6030 freed_so_far += SCTP_BUF_LEN(m);
6031 freed_so_far += MSIZE;
6032 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6033 sctp_sblog(&so->so_rcv,
6034 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
6036 m = SCTP_BUF_NEXT(m);
6038 control->data = control->tail_mbuf = NULL;
6039 control->length = 0;
6040 if (out_flags & MSG_EOR) {
6041 /* Done with this control */
6042 goto done_with_control;
6046 if (hold_rlock == 1) {
6047 SCTP_INP_READ_UNLOCK(inp);
6050 if (hold_sblock == 1) {
6051 SOCKBUF_UNLOCK(&so->so_rcv);
6054 sbunlock(&so->so_rcv);
6059 SOCKBUF_UNLOCK(&so->so_rcv);
6062 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6063 if ((freed_so_far >= rwnd_req) &&
6064 (control && (control->do_not_ref_stcb == 0)) &&
6065 (no_rcv_needed == 0))
6066 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6070 *msg_flags = out_flags;
6072 if (((out_flags & MSG_EOR) == 0) &&
6073 ((in_flags & MSG_PEEK) == 0) &&
6075 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO))) {
6076 struct sctp_extrcvinfo *s_extra;
6078 s_extra = (struct sctp_extrcvinfo *)sinfo;
6079 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
6081 if (hold_rlock == 1) {
6082 SCTP_INP_READ_UNLOCK(inp);
6086 SOCKBUF_UNLOCK(&so->so_rcv);
6090 sbunlock(&so->so_rcv);
6092 if (freecnt_applied) {
6094 * The lock on the socket buffer protects us so the free
6095 * code will stop. But since we used the socketbuf lock and
6096 * the sender uses the tcb_lock to increment, we need to use
6097 * the atomic add to the refcnt.
6101 panic("stcb for refcnt has gone NULL?");
6107 atomic_add_int(&stcb->asoc.refcnt, -1);
6108 freecnt_applied = 0;
6109 /* Save the value back for next time */
6110 stcb->freed_by_sorcv_sincelast = freed_so_far;
6112 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6114 sctp_misc_ints(SCTP_SORECV_DONE,
6116 ((uio) ? (slen - uio->uio_resid) : slen),
6120 sctp_misc_ints(SCTP_SORECV_DONE,
6122 ((uio) ? (slen - uio->uio_resid) : slen),
6128 if (wakeup_read_socket) {
6129 sctp_sorwakeup(inp, so);
6135 #ifdef SCTP_MBUF_LOGGING
6137 sctp_m_free(struct mbuf *m)
6139 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6140 if (SCTP_BUF_IS_EXTENDED(m)) {
6141 sctp_log_mb(m, SCTP_MBUF_IFREE);
6148 sctp_m_freem(struct mbuf *mb)
6151 mb = sctp_m_free(mb);
6157 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
6160 * Given a local address. For all associations that holds the
6161 * address, request a peer-set-primary.
6163 struct sctp_ifa *ifa;
6164 struct sctp_laddr *wi;
6166 ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
6168 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
6169 return (EADDRNOTAVAIL);
6172 * Now that we have the ifa we must awaken the iterator with this
6175 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6177 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
6180 /* Now incr the count and int wi structure */
6181 SCTP_INCR_LADDR_COUNT();
6182 bzero(wi, sizeof(*wi));
6183 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
6185 wi->action = SCTP_SET_PRIM_ADDR;
6186 atomic_add_int(&ifa->refcount, 1);
6188 /* Now add it to the work queue */
6189 SCTP_WQ_ADDR_LOCK();
6191 * Should this really be a tailq? As it is we will process the
6194 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6195 SCTP_WQ_ADDR_UNLOCK();
6196 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
6197 (struct sctp_inpcb *)NULL,
6198 (struct sctp_tcb *)NULL,
6199 (struct sctp_nets *)NULL);
6205 sctp_soreceive(struct socket *so,
6206 struct sockaddr **psa,
6209 struct mbuf **controlp,
6213 uint8_t sockbuf[256];
6214 struct sockaddr *from;
6215 struct sctp_extrcvinfo sinfo;
6216 int filling_sinfo = 1;
6217 struct sctp_inpcb *inp;
6219 inp = (struct sctp_inpcb *)so->so_pcb;
6220 /* pickup the assoc we are reading from */
6222 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6225 if ((sctp_is_feature_off(inp,
6226 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
6227 (controlp == NULL)) {
6228 /* user does not want the sndrcv ctl */
6232 from = (struct sockaddr *)sockbuf;
6233 fromlen = sizeof(sockbuf);
6240 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
6241 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6242 if ((controlp) && (filling_sinfo)) {
6243 /* copy back the sinfo in a CMSG format */
6245 *controlp = sctp_build_ctl_nchunk(inp,
6246 (struct sctp_sndrcvinfo *)&sinfo);
6251 /* copy back the address info */
6252 if (from && from->sa_len) {
6253 *psa = sodupsockaddr(from, M_NOWAIT);
6263 sctp_l_soreceive(struct socket *so,
6264 struct sockaddr **name,
6271 uint8_t sockbuf[256];
6272 struct sockaddr *from;
6273 struct sctp_extrcvinfo sinfo;
6274 int filling_sinfo = 1;
6275 struct sctp_inpcb *inp;
6277 inp = (struct sctp_inpcb *)so->so_pcb;
6278 /* pickup the assoc we are reading from */
6280 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6283 if ((sctp_is_feature_off(inp,
6284 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
6285 (controlp == NULL)) {
6286 /* user does not want the sndrcv ctl */
6290 from = (struct sockaddr *)sockbuf;
6291 fromlen = sizeof(sockbuf);
6298 error = sctp_sorecvmsg(so, uio,
6299 (struct mbuf **)NULL,
6300 from, fromlen, flag,
6301 (struct sctp_sndrcvinfo *)&sinfo,
6303 if ((controlp) && (filling_sinfo)) {
6305 * copy back the sinfo in a CMSG format note that the caller
6306 * has reponsibility for freeing the memory.
6309 *controlp = sctp_build_ctl_cchunk(inp,
6311 (struct sctp_sndrcvinfo *)&sinfo);
6314 /* copy back the address info */
6315 if (from && from->sa_len) {
6316 *name = sodupsockaddr(from, M_WAIT);
6331 sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6332 int totaddr, int *error)
6336 struct sctp_inpcb *inp;
6337 struct sockaddr *sa;
6341 inp = stcb->sctp_ep;
6343 for (i = 0; i < totaddr; i++) {
6344 if (sa->sa_family == AF_INET) {
6345 incr = sizeof(struct sockaddr_in);
6346 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
6347 /* assoc gone no un-lock */
6348 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6349 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
6354 } else if (sa->sa_family == AF_INET6) {
6355 incr = sizeof(struct sockaddr_in6);
6356 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
6357 /* assoc gone no un-lock */
6358 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6359 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
6365 sa = (struct sockaddr *)((caddr_t)sa + incr);
6372 sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6373 int *totaddr, int *num_v4, int *num_v6, int *error,
6374 int limit, int *bad_addr)
6376 struct sockaddr *sa;
6377 struct sctp_tcb *stcb = NULL;
6382 *error = *num_v6 = *num_v4 = 0;
6383 /* account and validate addresses */
6384 for (i = 0; i < (size_t)*totaddr; i++) {
6385 if (sa->sa_family == AF_INET) {
6387 incr = sizeof(struct sockaddr_in);
6388 if (sa->sa_len != incr) {
6389 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6394 } else if (sa->sa_family == AF_INET6) {
6395 struct sockaddr_in6 *sin6;
6397 sin6 = (struct sockaddr_in6 *)sa;
6398 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6399 /* Must be non-mapped for connectx */
6400 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6406 incr = sizeof(struct sockaddr_in6);
6407 if (sa->sa_len != incr) {
6408 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6418 SCTP_INP_INCR_REF(inp);
6419 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
6421 /* Already have or am bring up an association */
6424 SCTP_INP_DECR_REF(inp);
6426 if ((at + incr) > (size_t)limit) {
6430 sa = (struct sockaddr *)((caddr_t)sa + incr);
6432 return ((struct sctp_tcb *)NULL);
6436 * sctp_bindx(ADD) for one address.
6437 * assumes all arguments are valid/checked by caller.
6440 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
6441 struct sockaddr *sa, sctp_assoc_t assoc_id,
6442 uint32_t vrf_id, int *error, void *p)
6444 struct sockaddr *addr_touse;
6447 struct sockaddr_in sin;
6451 /* see if we're bound all already! */
6452 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6453 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6458 #if defined(INET6) && !defined(__Userspace__) /* TODO port in6_sin6_2_sin */
6459 if (sa->sa_family == AF_INET6) {
6460 struct sockaddr_in6 *sin6;
6462 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6463 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6467 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6468 /* can only bind v6 on PF_INET6 sockets */
6469 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6473 sin6 = (struct sockaddr_in6 *)addr_touse;
6474 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6475 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6476 SCTP_IPV6_V6ONLY(inp)) {
6477 /* can't bind v4-mapped on PF_INET sockets */
6478 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6482 in6_sin6_2_sin(&sin, sin6);
6483 addr_touse = (struct sockaddr *)&sin;
6487 if (sa->sa_family == AF_INET) {
6488 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6489 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6493 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6494 SCTP_IPV6_V6ONLY(inp)) {
6495 /* can't bind v4 on PF_INET sockets */
6496 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6501 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
6503 /* Can't get proc for Net/Open BSD */
6504 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6508 *error = sctp_inpcb_bind(so, addr_touse, NULL, p);
6512 * No locks required here since bind and mgmt_ep_sa all do their own
6513 * locking. If we do something for the FIX: below we may need to
6514 * lock in that case.
6516 if (assoc_id == 0) {
6517 /* add the address */
6518 struct sctp_inpcb *lep;
6519 struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
6521 /* validate the incoming port */
6522 if ((lsin->sin_port != 0) &&
6523 (lsin->sin_port != inp->sctp_lport)) {
6524 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6528 /* user specified 0 port, set it to existing port */
6529 lsin->sin_port = inp->sctp_lport;
6532 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
6535 * We must decrement the refcount since we have the
6536 * ep already and are binding. No remove going on
6539 SCTP_INP_DECR_REF(lep);
6542 /* already bound to it.. ok */
6544 } else if (lep == NULL) {
6545 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
6546 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6547 SCTP_ADD_IP_ADDRESS,
6550 *error = EADDRINUSE;
6556 * FIX: decide whether we allow assoc based bindx
6562 * sctp_bindx(DELETE) for one address.
6563 * assumes all arguments are valid/checked by caller.
6566 sctp_bindx_delete_address(struct socket *so, struct sctp_inpcb *inp,
6567 struct sockaddr *sa, sctp_assoc_t assoc_id,
6568 uint32_t vrf_id, int *error)
6570 struct sockaddr *addr_touse;
6573 struct sockaddr_in sin;
6577 /* see if we're bound all already! */
6578 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6579 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6584 #if defined(INET6) && !defined(__Userspace__) /* TODO port in6_sin6_2_sin */
6585 if (sa->sa_family == AF_INET6) {
6586 struct sockaddr_in6 *sin6;
6588 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6589 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6593 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6594 /* can only bind v6 on PF_INET6 sockets */
6595 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6599 sin6 = (struct sockaddr_in6 *)addr_touse;
6600 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6601 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6602 SCTP_IPV6_V6ONLY(inp)) {
6603 /* can't bind mapped-v4 on PF_INET sockets */
6604 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6608 in6_sin6_2_sin(&sin, sin6);
6609 addr_touse = (struct sockaddr *)&sin;
6613 if (sa->sa_family == AF_INET) {
6614 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6615 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6619 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6620 SCTP_IPV6_V6ONLY(inp)) {
6621 /* can't bind v4 on PF_INET sockets */
6622 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6628 * No lock required mgmt_ep_sa does its own locking. If the FIX:
6629 * below is ever changed we may need to lock before calling
6630 * association level binding.
6632 if (assoc_id == 0) {
6633 /* delete the address */
6634 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6635 SCTP_DEL_IP_ADDRESS,
6639 * FIX: decide whether we allow assoc based bindx
6645 * returns the valid local address count for an assoc, taking into account
6649 sctp_local_addr_count(struct sctp_tcb *stcb)
6651 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
6652 int ipv4_addr_legal, ipv6_addr_legal;
6653 struct sctp_vrf *vrf;
6654 struct sctp_ifn *sctp_ifn;
6655 struct sctp_ifa *sctp_ifa;
6658 /* Turn on all the appropriate scopes */
6659 loopback_scope = stcb->asoc.loopback_scope;
6660 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
6661 local_scope = stcb->asoc.local_scope;
6662 site_scope = stcb->asoc.site_scope;
6663 ipv4_addr_legal = ipv6_addr_legal = 0;
6664 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6665 ipv6_addr_legal = 1;
6666 if (SCTP_IPV6_V6ONLY(stcb->sctp_ep) == 0) {
6667 ipv4_addr_legal = 1;
6670 ipv4_addr_legal = 1;
6673 SCTP_IPI_ADDR_RLOCK();
6674 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
6676 /* no vrf, no addresses */
6677 SCTP_IPI_ADDR_RUNLOCK();
6680 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6682 * bound all case: go through all ifns on the vrf
6684 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
6685 if ((loopback_scope == 0) &&
6686 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
6689 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
6690 if (sctp_is_addr_restricted(stcb, sctp_ifa))
6692 switch (sctp_ifa->address.sa.sa_family) {
6694 if (ipv4_addr_legal) {
6695 struct sockaddr_in *sin;
6697 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
6698 if (sin->sin_addr.s_addr == 0) {
6705 if ((ipv4_local_scope == 0) &&
6706 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
6709 /* count this one */
6717 if (ipv6_addr_legal) {
6718 struct sockaddr_in6 *sin6;
6720 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
6721 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6724 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
6725 if (local_scope == 0)
6727 if (sin6->sin6_scope_id == 0) {
6728 if (sa6_recoverscope(sin6) != 0)
6746 if ((site_scope == 0) &&
6747 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
6750 /* count this one */
6765 struct sctp_laddr *laddr;
6767 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
6769 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
6772 /* count this one */
6776 SCTP_IPI_ADDR_RUNLOCK();
6780 #if defined(SCTP_LOCAL_TRACE_BUF)
6783 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)
6785 uint32_t saveindex, newindex;
6788 saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6789 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6792 newindex = saveindex + 1;
6794 } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6795 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6798 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6799 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6800 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6801 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6802 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6803 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6804 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6805 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6809 /* We will need to add support
6810 * to bind the ports and such here
6811 * so we can do UDP tunneling. In
6812 * the mean-time, we return error
6814 #include <netinet/udp.h>
6815 #include <netinet/udp_var.h>
6816 #include <sys/proc.h>
6818 #include <netinet6/sctp6_var.h>
6822 sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *ignored)
6825 struct mbuf *sp, *last;
6826 struct udphdr *uhdr;
6827 uint16_t port = 0, len;
6828 int header_size = sizeof(struct udphdr) + sizeof(struct sctphdr);
6831 * Split out the mbuf chain. Leave the IP header in m, place the
6834 if ((m->m_flags & M_PKTHDR) == 0) {
6835 /* Can't handle one that is not a pkt hdr */
6838 /* pull the src port */
6839 iph = mtod(m, struct ip *);
6840 uhdr = (struct udphdr *)((caddr_t)iph + off);
6842 port = uhdr->uh_sport;
6843 sp = m_split(m, off, M_DONTWAIT);
6845 /* Gak, drop packet, we can't do a split */
6848 if (sp->m_pkthdr.len < header_size) {
6849 /* Gak, packet can't have an SCTP header in it - to small */
6853 /* ok now pull up the UDP header and SCTP header together */
6854 sp = m_pullup(sp, header_size);
6856 /* Gak pullup failed */
6859 /* trim out the UDP header */
6860 m_adj(sp, sizeof(struct udphdr));
6862 /* Now reconstruct the mbuf chain */
6863 /* 1) find last one */
6865 while (last->m_next != NULL) {
6866 last = last->m_next;
6869 m->m_pkthdr.len += sp->m_pkthdr.len;
6871 while (last != NULL) {
6872 last = last->m_next;
6874 /* Now its ready for sctp_input or sctp6_input */
6875 iph = mtod(m, struct ip *);
6876 switch (iph->ip_v) {
6880 len = SCTP_GET_IPV4_LENGTH(iph);
6881 len -= sizeof(struct udphdr);
6882 SCTP_GET_IPV4_LENGTH(iph) = len;
6883 sctp_input_with_port(m, off, port);
6887 case IPV6_VERSION >> 4:
6889 /* its IPv6 - NOT supported */
6907 sctp_over_udp_stop(void)
6912 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6915 if (SCTP_BASE_INFO(udp_tun_socket) == NULL) {
6919 sop = SCTP_BASE_INFO(udp_tun_socket);
6921 SCTP_BASE_INFO(udp_tun_socket) = NULL;
6924 sctp_over_udp_start(void)
6928 struct sockaddr_in sin;
6929 struct socket *sop = NULL;
6934 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6937 port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
6939 /* Must have a port set */
6942 if (SCTP_BASE_INFO(udp_tun_socket) != NULL) {
6943 /* Already running -- must stop first */
6947 cred = th->td_ucred;
6948 if ((ret = socreate(PF_INET, &sop,
6949 SOCK_DGRAM, IPPROTO_UDP, cred, th))) {
6952 SCTP_BASE_INFO(udp_tun_socket) = sop;
6953 /* call the special UDP hook */
6954 ret = udp_set_kernel_tunneling(sop, sctp_recv_udp_tunneled_packet);
6956 goto exit_stage_left;
6958 /* Ok we have a socket, bind it to the port */
6959 memset(&sin, 0, sizeof(sin));
6960 sin.sin_len = sizeof(sin);
6961 sin.sin_family = AF_INET;
6962 sin.sin_port = htons(port);
6963 ret = sobind(sop, (struct sockaddr *)&sin, th);
6965 /* Close up we cant get the port */
6967 sctp_over_udp_stop();
6971 * Ok we should now get UDP packets directly to our input routine
6972 * sctp_recv_upd_tunneled_packet().