2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 /* $KAME: sctputil.c,v 1.37 2005/03/07 23:26:09 itojun Exp $ */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctputil.h>
41 #include <netinet/sctp_var.h>
42 #include <netinet/sctp_sysctl.h>
45 #include <netinet/sctp_header.h>
46 #include <netinet/sctp_output.h>
47 #include <netinet/sctp_uio.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_indata.h>/* for sctp_deliver_data() */
50 #include <netinet/sctp_auth.h>
51 #include <netinet/sctp_asconf.h>
52 #include <netinet/sctp_bsd_addr.h>
56 #define KTR_SCTP KTR_SUBSYS
59 extern struct sctp_cc_functions sctp_cc_functions[];
60 extern struct sctp_ss_functions sctp_ss_functions[];
63 sctp_sblog(struct sockbuf *sb,
64 struct sctp_tcb *stcb, int from, int incr)
66 struct sctp_cwnd_log sctp_clog;
68 sctp_clog.x.sb.stcb = stcb;
69 sctp_clog.x.sb.so_sbcc = sb->sb_cc;
71 sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
73 sctp_clog.x.sb.stcb_sbcc = 0;
74 sctp_clog.x.sb.incr = incr;
75 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
78 sctp_clog.x.misc.log1,
79 sctp_clog.x.misc.log2,
80 sctp_clog.x.misc.log3,
81 sctp_clog.x.misc.log4);
85 sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
87 struct sctp_cwnd_log sctp_clog;
89 sctp_clog.x.close.inp = (void *)inp;
90 sctp_clog.x.close.sctp_flags = inp->sctp_flags;
92 sctp_clog.x.close.stcb = (void *)stcb;
93 sctp_clog.x.close.state = (uint16_t) stcb->asoc.state;
95 sctp_clog.x.close.stcb = 0;
96 sctp_clog.x.close.state = 0;
98 sctp_clog.x.close.loc = loc;
99 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
100 SCTP_LOG_EVENT_CLOSE,
102 sctp_clog.x.misc.log1,
103 sctp_clog.x.misc.log2,
104 sctp_clog.x.misc.log3,
105 sctp_clog.x.misc.log4);
110 rto_logging(struct sctp_nets *net, int from)
112 struct sctp_cwnd_log sctp_clog;
114 memset(&sctp_clog, 0, sizeof(sctp_clog));
115 sctp_clog.x.rto.net = (void *)net;
116 sctp_clog.x.rto.rtt = net->rtt / 1000;
117 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
120 sctp_clog.x.misc.log1,
121 sctp_clog.x.misc.log2,
122 sctp_clog.x.misc.log3,
123 sctp_clog.x.misc.log4);
128 sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
130 struct sctp_cwnd_log sctp_clog;
132 sctp_clog.x.strlog.stcb = stcb;
133 sctp_clog.x.strlog.n_tsn = tsn;
134 sctp_clog.x.strlog.n_sseq = sseq;
135 sctp_clog.x.strlog.e_tsn = 0;
136 sctp_clog.x.strlog.e_sseq = 0;
137 sctp_clog.x.strlog.strm = stream;
138 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
141 sctp_clog.x.misc.log1,
142 sctp_clog.x.misc.log2,
143 sctp_clog.x.misc.log3,
144 sctp_clog.x.misc.log4);
149 sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
151 struct sctp_cwnd_log sctp_clog;
153 sctp_clog.x.nagle.stcb = (void *)stcb;
154 sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
155 sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
156 sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
157 sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
158 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
159 SCTP_LOG_EVENT_NAGLE,
161 sctp_clog.x.misc.log1,
162 sctp_clog.x.misc.log2,
163 sctp_clog.x.misc.log3,
164 sctp_clog.x.misc.log4);
169 sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
171 struct sctp_cwnd_log sctp_clog;
173 sctp_clog.x.sack.cumack = cumack;
174 sctp_clog.x.sack.oldcumack = old_cumack;
175 sctp_clog.x.sack.tsn = tsn;
176 sctp_clog.x.sack.numGaps = gaps;
177 sctp_clog.x.sack.numDups = dups;
178 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
181 sctp_clog.x.misc.log1,
182 sctp_clog.x.misc.log2,
183 sctp_clog.x.misc.log3,
184 sctp_clog.x.misc.log4);
188 sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
190 struct sctp_cwnd_log sctp_clog;
192 memset(&sctp_clog, 0, sizeof(sctp_clog));
193 sctp_clog.x.map.base = map;
194 sctp_clog.x.map.cum = cum;
195 sctp_clog.x.map.high = high;
196 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
199 sctp_clog.x.misc.log1,
200 sctp_clog.x.misc.log2,
201 sctp_clog.x.misc.log3,
202 sctp_clog.x.misc.log4);
206 sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn,
209 struct sctp_cwnd_log sctp_clog;
211 memset(&sctp_clog, 0, sizeof(sctp_clog));
212 sctp_clog.x.fr.largest_tsn = biggest_tsn;
213 sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
214 sctp_clog.x.fr.tsn = tsn;
215 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
218 sctp_clog.x.misc.log1,
219 sctp_clog.x.misc.log2,
220 sctp_clog.x.misc.log3,
221 sctp_clog.x.misc.log4);
227 sctp_log_mb(struct mbuf *m, int from)
229 struct sctp_cwnd_log sctp_clog;
231 sctp_clog.x.mb.mp = m;
232 sctp_clog.x.mb.mbuf_flags = (uint8_t) (SCTP_BUF_GET_FLAGS(m));
233 sctp_clog.x.mb.size = (uint16_t) (SCTP_BUF_LEN(m));
234 sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
235 if (SCTP_BUF_IS_EXTENDED(m)) {
236 sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
237 sctp_clog.x.mb.refcnt = (uint8_t) (SCTP_BUF_EXTEND_REFCNT(m));
239 sctp_clog.x.mb.ext = 0;
240 sctp_clog.x.mb.refcnt = 0;
242 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
245 sctp_clog.x.misc.log1,
246 sctp_clog.x.misc.log2,
247 sctp_clog.x.misc.log3,
248 sctp_clog.x.misc.log4);
253 sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk,
256 struct sctp_cwnd_log sctp_clog;
258 if (control == NULL) {
259 SCTP_PRINTF("Gak log of NULL?\n");
262 sctp_clog.x.strlog.stcb = control->stcb;
263 sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
264 sctp_clog.x.strlog.n_sseq = control->sinfo_ssn;
265 sctp_clog.x.strlog.strm = control->sinfo_stream;
266 if (poschk != NULL) {
267 sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
268 sctp_clog.x.strlog.e_sseq = poschk->sinfo_ssn;
270 sctp_clog.x.strlog.e_tsn = 0;
271 sctp_clog.x.strlog.e_sseq = 0;
273 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
276 sctp_clog.x.misc.log1,
277 sctp_clog.x.misc.log2,
278 sctp_clog.x.misc.log3,
279 sctp_clog.x.misc.log4);
284 sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
286 struct sctp_cwnd_log sctp_clog;
288 sctp_clog.x.cwnd.net = net;
289 if (stcb->asoc.send_queue_cnt > 255)
290 sctp_clog.x.cwnd.cnt_in_send = 255;
292 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
293 if (stcb->asoc.stream_queue_cnt > 255)
294 sctp_clog.x.cwnd.cnt_in_str = 255;
296 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
299 sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
300 sctp_clog.x.cwnd.inflight = net->flight_size;
301 sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
302 sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
303 sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
305 if (SCTP_CWNDLOG_PRESEND == from) {
306 sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
308 sctp_clog.x.cwnd.cwnd_augment = augment;
309 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
312 sctp_clog.x.misc.log1,
313 sctp_clog.x.misc.log2,
314 sctp_clog.x.misc.log3,
315 sctp_clog.x.misc.log4);
320 sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
322 struct sctp_cwnd_log sctp_clog;
324 memset(&sctp_clog, 0, sizeof(sctp_clog));
326 sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
329 sctp_clog.x.lock.sock = (void *)NULL;
331 sctp_clog.x.lock.inp = (void *)inp;
333 sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
335 sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
338 sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
339 sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
341 sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
342 sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
344 sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
345 if (inp && (inp->sctp_socket)) {
346 sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
347 sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
348 sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
350 sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
351 sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
352 sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
354 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
357 sctp_clog.x.misc.log1,
358 sctp_clog.x.misc.log2,
359 sctp_clog.x.misc.log3,
360 sctp_clog.x.misc.log4);
365 sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
367 struct sctp_cwnd_log sctp_clog;
369 memset(&sctp_clog, 0, sizeof(sctp_clog));
370 sctp_clog.x.cwnd.net = net;
371 sctp_clog.x.cwnd.cwnd_new_value = error;
372 sctp_clog.x.cwnd.inflight = net->flight_size;
373 sctp_clog.x.cwnd.cwnd_augment = burst;
374 if (stcb->asoc.send_queue_cnt > 255)
375 sctp_clog.x.cwnd.cnt_in_send = 255;
377 sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
378 if (stcb->asoc.stream_queue_cnt > 255)
379 sctp_clog.x.cwnd.cnt_in_str = 255;
381 sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
382 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
383 SCTP_LOG_EVENT_MAXBURST,
385 sctp_clog.x.misc.log1,
386 sctp_clog.x.misc.log2,
387 sctp_clog.x.misc.log3,
388 sctp_clog.x.misc.log4);
393 sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
395 struct sctp_cwnd_log sctp_clog;
397 sctp_clog.x.rwnd.rwnd = peers_rwnd;
398 sctp_clog.x.rwnd.send_size = snd_size;
399 sctp_clog.x.rwnd.overhead = overhead;
400 sctp_clog.x.rwnd.new_rwnd = 0;
401 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
404 sctp_clog.x.misc.log1,
405 sctp_clog.x.misc.log2,
406 sctp_clog.x.misc.log3,
407 sctp_clog.x.misc.log4);
411 sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
413 struct sctp_cwnd_log sctp_clog;
415 sctp_clog.x.rwnd.rwnd = peers_rwnd;
416 sctp_clog.x.rwnd.send_size = flight_size;
417 sctp_clog.x.rwnd.overhead = overhead;
418 sctp_clog.x.rwnd.new_rwnd = a_rwndval;
419 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
422 sctp_clog.x.misc.log1,
423 sctp_clog.x.misc.log2,
424 sctp_clog.x.misc.log3,
425 sctp_clog.x.misc.log4);
429 sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
431 struct sctp_cwnd_log sctp_clog;
433 sctp_clog.x.mbcnt.total_queue_size = total_oq;
434 sctp_clog.x.mbcnt.size_change = book;
435 sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
436 sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
437 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
438 SCTP_LOG_EVENT_MBCNT,
440 sctp_clog.x.misc.log1,
441 sctp_clog.x.misc.log2,
442 sctp_clog.x.misc.log3,
443 sctp_clog.x.misc.log4);
448 sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
450 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
457 sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t cumtsn, uint32_t wake_cnt, int from)
459 struct sctp_cwnd_log sctp_clog;
461 sctp_clog.x.wake.stcb = (void *)stcb;
462 sctp_clog.x.wake.wake_cnt = wake_cnt;
463 sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
464 sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
465 sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
467 if (stcb->asoc.stream_queue_cnt < 0xff)
468 sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
470 sctp_clog.x.wake.stream_qcnt = 0xff;
472 if (stcb->asoc.chunks_on_out_queue < 0xff)
473 sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
475 sctp_clog.x.wake.chunks_on_oque = 0xff;
477 sctp_clog.x.wake.sctpflags = 0;
478 /* set in the defered mode stuff */
479 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
480 sctp_clog.x.wake.sctpflags |= 1;
481 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
482 sctp_clog.x.wake.sctpflags |= 2;
483 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
484 sctp_clog.x.wake.sctpflags |= 4;
485 /* what about the sb */
486 if (stcb->sctp_socket) {
487 struct socket *so = stcb->sctp_socket;
489 sctp_clog.x.wake.sbflags = (uint8_t) ((so->so_snd.sb_flags & 0x00ff));
491 sctp_clog.x.wake.sbflags = 0xff;
493 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
496 sctp_clog.x.misc.log1,
497 sctp_clog.x.misc.log2,
498 sctp_clog.x.misc.log3,
499 sctp_clog.x.misc.log4);
504 sctp_log_block(uint8_t from, struct socket *so, struct sctp_association *asoc, int sendlen)
506 struct sctp_cwnd_log sctp_clog;
508 sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
509 sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
510 sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
511 sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
512 sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
513 sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight / 1024);
514 sctp_clog.x.blk.sndlen = sendlen;
515 SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
516 SCTP_LOG_EVENT_BLOCK,
518 sctp_clog.x.misc.log1,
519 sctp_clog.x.misc.log2,
520 sctp_clog.x.misc.log3,
521 sctp_clog.x.misc.log4);
526 sctp_fill_stat_log(void *optval, size_t *optsize)
528 /* May need to fix this if ktrdump does not work */
532 #ifdef SCTP_AUDITING_ENABLED
533 uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
534 static int sctp_audit_indx = 0;
538 sctp_print_audit_report(void)
544 for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
545 if ((sctp_audit_data[i][0] == 0xe0) &&
546 (sctp_audit_data[i][1] == 0x01)) {
549 } else if (sctp_audit_data[i][0] == 0xf0) {
552 } else if ((sctp_audit_data[i][0] == 0xc0) &&
553 (sctp_audit_data[i][1] == 0x01)) {
557 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
558 (uint32_t) sctp_audit_data[i][1]);
563 for (i = 0; i < sctp_audit_indx; i++) {
564 if ((sctp_audit_data[i][0] == 0xe0) &&
565 (sctp_audit_data[i][1] == 0x01)) {
568 } else if (sctp_audit_data[i][0] == 0xf0) {
571 } else if ((sctp_audit_data[i][0] == 0xc0) &&
572 (sctp_audit_data[i][1] == 0x01)) {
576 SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
577 (uint32_t) sctp_audit_data[i][1]);
586 sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
587 struct sctp_nets *net)
589 int resend_cnt, tot_out, rep, tot_book_cnt;
590 struct sctp_nets *lnet;
591 struct sctp_tmit_chunk *chk;
593 sctp_audit_data[sctp_audit_indx][0] = 0xAA;
594 sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
596 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
600 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
601 sctp_audit_data[sctp_audit_indx][1] = 0x01;
603 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
609 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
610 sctp_audit_data[sctp_audit_indx][1] = 0x02;
612 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
617 sctp_audit_data[sctp_audit_indx][0] = 0xA1;
618 sctp_audit_data[sctp_audit_indx][1] =
619 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
621 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
626 resend_cnt = tot_out = 0;
627 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
628 if (chk->sent == SCTP_DATAGRAM_RESEND) {
630 } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
631 tot_out += chk->book_size;
635 if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
636 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
637 sctp_audit_data[sctp_audit_indx][1] = 0xA1;
639 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
642 SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
643 resend_cnt, stcb->asoc.sent_queue_retran_cnt);
645 stcb->asoc.sent_queue_retran_cnt = resend_cnt;
646 sctp_audit_data[sctp_audit_indx][0] = 0xA2;
647 sctp_audit_data[sctp_audit_indx][1] =
648 (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
650 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
654 if (tot_out != stcb->asoc.total_flight) {
655 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
656 sctp_audit_data[sctp_audit_indx][1] = 0xA2;
658 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
662 SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
663 (int)stcb->asoc.total_flight);
664 stcb->asoc.total_flight = tot_out;
666 if (tot_book_cnt != stcb->asoc.total_flight_count) {
667 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
668 sctp_audit_data[sctp_audit_indx][1] = 0xA5;
670 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
674 SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
676 stcb->asoc.total_flight_count = tot_book_cnt;
679 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
680 tot_out += lnet->flight_size;
682 if (tot_out != stcb->asoc.total_flight) {
683 sctp_audit_data[sctp_audit_indx][0] = 0xAF;
684 sctp_audit_data[sctp_audit_indx][1] = 0xA3;
686 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
690 SCTP_PRINTF("real flight:%d net total was %d\n",
691 stcb->asoc.total_flight, tot_out);
692 /* now corrective action */
693 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
696 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
697 if ((chk->whoTo == lnet) &&
698 (chk->sent < SCTP_DATAGRAM_RESEND)) {
699 tot_out += chk->book_size;
702 if (lnet->flight_size != tot_out) {
703 SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
704 lnet, lnet->flight_size,
706 lnet->flight_size = tot_out;
711 sctp_print_audit_report();
716 sctp_audit_log(uint8_t ev, uint8_t fd)
719 sctp_audit_data[sctp_audit_indx][0] = ev;
720 sctp_audit_data[sctp_audit_indx][1] = fd;
722 if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
730 * sctp_stop_timers_for_shutdown() should be called
731 * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
732 * state to make sure that all timers are stopped.
735 sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
737 struct sctp_association *asoc;
738 struct sctp_nets *net;
742 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
743 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
744 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
745 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
746 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
747 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
748 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
749 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
750 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
755 * a list of sizes based on typical mtu's, used only if next hop size not
758 static uint32_t sctp_mtu_sizes[] = {
780 * Return the largest MTU smaller than val. If there is no
781 * entry, just return val.
784 sctp_get_prev_mtu(uint32_t val)
788 if (val <= sctp_mtu_sizes[0]) {
791 for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
792 if (val <= sctp_mtu_sizes[i]) {
796 return (sctp_mtu_sizes[i - 1]);
800 * Return the smallest MTU larger than val. If there is no
801 * entry, just return val.
804 sctp_get_next_mtu(struct sctp_inpcb *inp, uint32_t val)
806 /* select another MTU that is just bigger than this one */
809 for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
810 if (val < sctp_mtu_sizes[i]) {
811 return (sctp_mtu_sizes[i]);
818 sctp_fill_random_store(struct sctp_pcb *m)
821 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
822 * our counter. The result becomes our good random numbers and we
823 * then setup to give these out. Note that we do no locking to
824 * protect this. This is ok, since if competing folks call this we
825 * will get more gobbled gook in the random store which is what we
826 * want. There is a danger that two guys will use the same random
827 * numbers, but thats ok too since that is random as well :->
830 (void)sctp_hmac(SCTP_HMAC, (uint8_t *) m->random_numbers,
831 sizeof(m->random_numbers), (uint8_t *) & m->random_counter,
832 sizeof(m->random_counter), (uint8_t *) m->random_store);
837 sctp_select_initial_TSN(struct sctp_pcb *inp)
840 * A true implementation should use random selection process to get
841 * the initial stream sequence number, using RFC1750 as a good
846 int store_at, new_store;
848 if (inp->initial_sequence_debug != 0) {
851 ret = inp->initial_sequence_debug;
852 inp->initial_sequence_debug++;
856 store_at = inp->store_at;
857 new_store = store_at + sizeof(uint32_t);
858 if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
861 if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
864 if (new_store == 0) {
865 /* Refill the random store */
866 sctp_fill_random_store(inp);
868 p = &inp->random_store[store_at];
875 sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int save_in_twait)
877 uint32_t x, not_done;
880 (void)SCTP_GETTIME_TIMEVAL(&now);
883 x = sctp_select_initial_TSN(&inp->sctp_ep);
888 if (sctp_is_vtag_good(inp, x, lport, rport, &now, save_in_twait)) {
896 sctp_init_asoc(struct sctp_inpcb *m, struct sctp_tcb *stcb,
897 uint32_t override_tag, uint32_t vrf_id)
899 struct sctp_association *asoc;
902 * Anything set to zero is taken care of by the allocation routine's
907 * Up front select what scoping to apply on addresses I tell my peer
908 * Not sure what to do with these right now, we will need to come up
909 * with a way to set them. We may need to pass them through from the
910 * caller in the sctp_aloc_assoc() function.
915 /* init all variables to a known value. */
916 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE);
917 asoc->max_burst = m->sctp_ep.max_burst;
918 asoc->fr_max_burst = m->sctp_ep.fr_max_burst;
919 asoc->heart_beat_delay = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
920 asoc->cookie_life = m->sctp_ep.def_cookie_life;
921 asoc->sctp_cmt_on_off = m->sctp_cmt_on_off;
922 asoc->ecn_allowed = m->sctp_ecn_enable;
923 asoc->sctp_nr_sack_on_off = (uint8_t) SCTP_BASE_SYSCTL(sctp_nr_sack_on_off);
924 asoc->sctp_cmt_pf = (uint8_t) SCTP_BASE_SYSCTL(sctp_cmt_pf);
925 asoc->sctp_frag_point = m->sctp_frag_point;
927 asoc->default_tos = m->ip_inp.inp.inp_ip_tos;
929 asoc->default_tos = 0;
933 asoc->default_flowlabel = ((struct in6pcb *)m)->in6p_flowinfo;
935 asoc->default_flowlabel = 0;
937 asoc->sb_send_resv = 0;
939 asoc->my_vtag = override_tag;
941 asoc->my_vtag = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
943 /* Get the nonce tags */
944 asoc->my_vtag_nonce = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
945 asoc->peer_vtag_nonce = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
946 asoc->vrf_id = vrf_id;
948 if (sctp_is_feature_on(m, SCTP_PCB_FLAGS_DONOT_HEARTBEAT))
949 asoc->hb_is_disabled = 1;
951 asoc->hb_is_disabled = 0;
953 #ifdef SCTP_ASOCLOG_OF_TSNS
955 asoc->tsn_out_at = 0;
956 asoc->tsn_in_wrapped = 0;
957 asoc->tsn_out_wrapped = 0;
958 asoc->cumack_log_at = 0;
959 asoc->cumack_log_atsnt = 0;
961 #ifdef SCTP_FS_SPEC_LOG
965 asoc->assoc_up_sent = 0;
966 asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
967 sctp_select_initial_TSN(&m->sctp_ep);
968 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
969 /* we are optimisitic here */
970 asoc->peer_supports_pktdrop = 1;
971 asoc->peer_supports_nat = 0;
972 asoc->sent_queue_retran_cnt = 0;
975 asoc->last_net_cmt_send_started = NULL;
977 /* This will need to be adjusted */
978 asoc->last_acked_seq = asoc->init_seq_number - 1;
979 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
980 asoc->asconf_seq_in = asoc->last_acked_seq;
982 /* here we are different, we hold the next one we expect */
983 asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
985 asoc->initial_init_rto_max = m->sctp_ep.initial_init_rto_max;
986 asoc->initial_rto = m->sctp_ep.initial_rto;
988 asoc->max_init_times = m->sctp_ep.max_init_times;
989 asoc->max_send_times = m->sctp_ep.max_send_times;
990 asoc->def_net_failure = m->sctp_ep.def_net_failure;
991 asoc->free_chunk_cnt = 0;
993 asoc->iam_blocking = 0;
995 asoc->context = m->sctp_context;
996 asoc->def_send = m->def_send;
997 asoc->delayed_ack = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
998 asoc->sack_freq = m->sctp_ep.sctp_sack_freq;
999 asoc->pr_sctp_cnt = 0;
1000 asoc->total_output_queue_size = 0;
1002 if (m->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1003 struct in6pcb *inp6;
1005 /* Its a V6 socket */
1006 inp6 = (struct in6pcb *)m;
1007 asoc->ipv6_addr_legal = 1;
1008 /* Now look at the binding flag to see if V4 will be legal */
1009 if (SCTP_IPV6_V6ONLY(inp6) == 0) {
1010 asoc->ipv4_addr_legal = 1;
1012 /* V4 addresses are NOT legal on the association */
1013 asoc->ipv4_addr_legal = 0;
1016 /* Its a V4 socket, no - V6 */
1017 asoc->ipv4_addr_legal = 1;
1018 asoc->ipv6_addr_legal = 0;
1021 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(m->sctp_socket), SCTP_MINIMAL_RWND);
1022 asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(m->sctp_socket);
1024 asoc->smallest_mtu = m->sctp_frag_point;
1025 asoc->minrto = m->sctp_ep.sctp_minrto;
1026 asoc->maxrto = m->sctp_ep.sctp_maxrto;
1028 asoc->locked_on_sending = NULL;
1029 asoc->stream_locked_on = 0;
1030 asoc->ecn_echo_cnt_onq = 0;
1031 asoc->stream_locked = 0;
1033 asoc->send_sack = 1;
1035 LIST_INIT(&asoc->sctp_restricted_addrs);
1037 TAILQ_INIT(&asoc->nets);
1038 TAILQ_INIT(&asoc->pending_reply_queue);
1039 TAILQ_INIT(&asoc->asconf_ack_sent);
1040 /* Setup to fill the hb random cache at first HB */
1041 asoc->hb_random_idx = 4;
1043 asoc->sctp_autoclose_ticks = m->sctp_ep.auto_close_time;
1045 stcb->asoc.congestion_control_module = m->sctp_ep.sctp_default_cc_module;
1046 stcb->asoc.cc_functions = sctp_cc_functions[m->sctp_ep.sctp_default_cc_module];
1048 stcb->asoc.stream_scheduling_module = m->sctp_ep.sctp_default_ss_module;
1049 stcb->asoc.ss_functions = sctp_ss_functions[m->sctp_ep.sctp_default_ss_module];
1052 * Now the stream parameters, here we allocate space for all streams
1053 * that we request by default.
1055 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1056 m->sctp_ep.pre_open_stream_count;
1057 SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1058 asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1060 if (asoc->strmout == NULL) {
1061 /* big trouble no memory */
1062 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1065 for (i = 0; i < asoc->streamoutcnt; i++) {
1067 * inbound side must be set to 0xffff, also NOTE when we get
1068 * the INIT-ACK back (for INIT sender) we MUST reduce the
1069 * count (streamoutcnt) but first check if we sent to any of
1070 * the upper streams that were dropped (if some were). Those
1071 * that were dropped must be notified to the upper layer as
1074 asoc->strmout[i].next_sequence_sent = 0x0;
1075 TAILQ_INIT(&asoc->strmout[i].outqueue);
1076 asoc->strmout[i].stream_no = i;
1077 asoc->strmout[i].last_msg_incomplete = 0;
1078 asoc->ss_functions.sctp_ss_init_stream(&asoc->strmout[i], NULL);
1080 asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1082 /* Now the mapping array */
1083 asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1084 SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1086 if (asoc->mapping_array == NULL) {
1087 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1088 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1091 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1092 SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1094 if (asoc->nr_mapping_array == NULL) {
1095 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1096 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1097 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1100 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1102 /* Now the init of the other outqueues */
1103 TAILQ_INIT(&asoc->free_chunks);
1104 TAILQ_INIT(&asoc->control_send_queue);
1105 TAILQ_INIT(&asoc->asconf_send_queue);
1106 TAILQ_INIT(&asoc->send_queue);
1107 TAILQ_INIT(&asoc->sent_queue);
1108 TAILQ_INIT(&asoc->reasmqueue);
1109 TAILQ_INIT(&asoc->resetHead);
1110 asoc->max_inbound_streams = m->sctp_ep.max_open_streams_intome;
1111 TAILQ_INIT(&asoc->asconf_queue);
1112 /* authentication fields */
1113 asoc->authinfo.random = NULL;
1114 asoc->authinfo.active_keyid = 0;
1115 asoc->authinfo.assoc_key = NULL;
1116 asoc->authinfo.assoc_keyid = 0;
1117 asoc->authinfo.recv_key = NULL;
1118 asoc->authinfo.recv_keyid = 0;
1119 LIST_INIT(&asoc->shared_keys);
1120 asoc->marked_retrans = 0;
1124 asoc->timoshutdown = 0;
1125 asoc->timoheartbeat = 0;
1126 asoc->timocookie = 0;
1127 asoc->timoshutdownack = 0;
1128 (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1129 asoc->discontinuity_time = asoc->start_time;
1131 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
1132 * freed later when the association is freed.
1138 sctp_print_mapping_array(struct sctp_association *asoc)
1140 unsigned int i, limit;
1142 printf("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
1143 asoc->mapping_array_size,
1144 asoc->mapping_array_base_tsn,
1145 asoc->cumulative_tsn,
1146 asoc->highest_tsn_inside_map,
1147 asoc->highest_tsn_inside_nr_map);
1148 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1149 if (asoc->mapping_array[limit - 1]) {
1153 printf("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1154 for (i = 0; i < limit; i++) {
1155 printf("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1159 for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1160 if (asoc->nr_mapping_array[limit - 1]) {
1164 printf("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1165 for (i = 0; i < limit; i++) {
1166 printf("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1173 sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1175 /* mapping array needs to grow */
1176 uint8_t *new_array1, *new_array2;
1179 new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1180 SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1181 SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1182 if ((new_array1 == NULL) || (new_array2 == NULL)) {
1183 /* can't get more, forget it */
1184 SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1186 SCTP_FREE(new_array1, SCTP_M_MAP);
1189 SCTP_FREE(new_array2, SCTP_M_MAP);
1193 memset(new_array1, 0, new_size);
1194 memset(new_array2, 0, new_size);
1195 memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1196 memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1197 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1198 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1199 asoc->mapping_array = new_array1;
1200 asoc->nr_mapping_array = new_array2;
1201 asoc->mapping_array_size = new_size;
1207 sctp_iterator_work(struct sctp_iterator *it)
1209 int iteration_count = 0;
1212 struct sctp_inpcb *tinp;
1214 SCTP_INP_INFO_RLOCK();
1215 SCTP_ITERATOR_LOCK();
1217 SCTP_INP_RLOCK(it->inp);
1218 SCTP_INP_DECR_REF(it->inp);
1220 if (it->inp == NULL) {
1221 /* iterator is complete */
1223 SCTP_ITERATOR_UNLOCK();
1224 SCTP_INP_INFO_RUNLOCK();
1225 if (it->function_atend != NULL) {
1226 (*it->function_atend) (it->pointer, it->val);
1228 SCTP_FREE(it, SCTP_M_ITER);
1235 SCTP_INP_RLOCK(it->inp);
1237 while (((it->pcb_flags) &&
1238 ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1239 ((it->pcb_features) &&
1240 ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1241 /* endpoint flags or features don't match, so keep looking */
1242 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1243 SCTP_INP_RUNLOCK(it->inp);
1244 goto done_with_iterator;
1247 it->inp = LIST_NEXT(it->inp, sctp_list);
1248 SCTP_INP_RUNLOCK(tinp);
1249 if (it->inp == NULL) {
1250 goto done_with_iterator;
1252 SCTP_INP_RLOCK(it->inp);
1254 /* now go through each assoc which is in the desired state */
1255 if (it->done_current_ep == 0) {
1256 if (it->function_inp != NULL)
1257 inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
1258 it->done_current_ep = 1;
1260 if (it->stcb == NULL) {
1261 /* run the per instance function */
1262 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1264 if ((inp_skip) || it->stcb == NULL) {
1265 if (it->function_inp_end != NULL) {
1266 inp_skip = (*it->function_inp_end) (it->inp,
1270 SCTP_INP_RUNLOCK(it->inp);
1274 SCTP_TCB_LOCK(it->stcb);
1275 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1276 /* not in the right state... keep looking */
1277 SCTP_TCB_UNLOCK(it->stcb);
1280 /* see if we have limited out the iterator loop */
1282 if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1283 /* Pause to let others grab the lock */
1284 atomic_add_int(&it->stcb->asoc.refcnt, 1);
1285 SCTP_TCB_UNLOCK(it->stcb);
1286 SCTP_INP_INCR_REF(it->inp);
1287 SCTP_INP_RUNLOCK(it->inp);
1288 SCTP_ITERATOR_UNLOCK();
1289 SCTP_INP_INFO_RUNLOCK();
1290 SCTP_INP_INFO_RLOCK();
1291 SCTP_ITERATOR_LOCK();
1292 if (sctp_it_ctl.iterator_flags) {
1293 /* We won't be staying here */
1294 SCTP_INP_DECR_REF(it->inp);
1295 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1296 if (sctp_it_ctl.iterator_flags &
1297 SCTP_ITERATOR_MUST_EXIT) {
1298 goto done_with_iterator;
1300 if (sctp_it_ctl.iterator_flags &
1301 SCTP_ITERATOR_STOP_CUR_IT) {
1302 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1303 goto done_with_iterator;
1305 if (sctp_it_ctl.iterator_flags &
1306 SCTP_ITERATOR_STOP_CUR_INP) {
1307 sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1310 /* If we reach here huh? */
1311 printf("Unknown it ctl flag %x\n",
1312 sctp_it_ctl.iterator_flags);
1313 sctp_it_ctl.iterator_flags = 0;
1315 SCTP_INP_RLOCK(it->inp);
1316 SCTP_INP_DECR_REF(it->inp);
1317 SCTP_TCB_LOCK(it->stcb);
1318 atomic_add_int(&it->stcb->asoc.refcnt, -1);
1319 iteration_count = 0;
1321 /* run function on this one */
1322 (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
1325 * we lie here, it really needs to have its own type but
1326 * first I must verify that this won't effect things :-0
1328 if (it->no_chunk_output == 0)
1329 sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1331 SCTP_TCB_UNLOCK(it->stcb);
1333 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1334 if (it->stcb == NULL) {
1335 /* Run last function */
1336 if (it->function_inp_end != NULL) {
1337 inp_skip = (*it->function_inp_end) (it->inp,
1343 SCTP_INP_RUNLOCK(it->inp);
1345 /* done with all assocs on this endpoint, move on to next endpoint */
1346 it->done_current_ep = 0;
1347 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1350 it->inp = LIST_NEXT(it->inp, sctp_list);
1352 if (it->inp == NULL) {
1353 goto done_with_iterator;
1355 goto select_a_new_ep;
1359 sctp_iterator_worker(void)
1361 struct sctp_iterator *it, *nit;
1363 /* This function is called with the WQ lock in place */
1365 sctp_it_ctl.iterator_running = 1;
1366 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
1367 sctp_it_ctl.cur_it = it;
1368 /* now lets work on this one */
1369 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
1370 SCTP_IPI_ITERATOR_WQ_UNLOCK();
1371 CURVNET_SET(it->vn);
1372 sctp_iterator_work(it);
1373 sctp_it_ctl.cur_it = NULL;
1375 SCTP_IPI_ITERATOR_WQ_LOCK();
1376 if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) {
1379 /* sa_ignore FREED_MEMORY */
1381 sctp_it_ctl.iterator_running = 0;
1387 sctp_handle_addr_wq(void)
1389 /* deal with the ADDR wq from the rtsock calls */
1390 struct sctp_laddr *wi, *nwi;
1391 struct sctp_asconf_iterator *asc;
1393 SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1394 sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
1396 /* Try later, no memory */
1397 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1398 (struct sctp_inpcb *)NULL,
1399 (struct sctp_tcb *)NULL,
1400 (struct sctp_nets *)NULL);
1403 LIST_INIT(&asc->list_of_work);
1406 SCTP_WQ_ADDR_LOCK();
1407 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
1408 LIST_REMOVE(wi, sctp_nxt_addr);
1409 LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
1412 SCTP_WQ_ADDR_UNLOCK();
1414 if (asc->cnt == 0) {
1415 SCTP_FREE(asc, SCTP_M_ASC_IT);
1417 (void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
1418 sctp_asconf_iterator_stcb,
1419 NULL, /* No ep end for boundall */
1420 SCTP_PCB_FLAGS_BOUNDALL,
1421 SCTP_PCB_ANY_FEATURES,
1422 SCTP_ASOC_ANY_STATE,
1424 sctp_asconf_iterator_end, NULL, 0);
1432 sctp_timeout_handler(void *t)
1434 struct sctp_inpcb *inp;
1435 struct sctp_tcb *stcb;
1436 struct sctp_nets *net;
1437 struct sctp_timer *tmr;
1439 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1443 int did_output, type;
1445 tmr = (struct sctp_timer *)t;
1446 inp = (struct sctp_inpcb *)tmr->ep;
1447 stcb = (struct sctp_tcb *)tmr->tcb;
1448 net = (struct sctp_nets *)tmr->net;
1449 CURVNET_SET((struct vnet *)tmr->vnet);
1452 #ifdef SCTP_AUDITING_ENABLED
1453 sctp_audit_log(0xF0, (uint8_t) tmr->type);
1454 sctp_auditing(3, inp, stcb, net);
1457 /* sanity checks... */
1458 if (tmr->self != (void *)tmr) {
1460 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1466 tmr->stopped_from = 0xa001;
1467 if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1469 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1475 tmr->stopped_from = 0xa002;
1476 if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
1480 /* if this is an iterator timeout, get the struct and clear inp */
1481 tmr->stopped_from = 0xa003;
1484 SCTP_INP_INCR_REF(inp);
1485 if ((inp->sctp_socket == 0) &&
1486 ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1487 (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1488 (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1489 (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1490 (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1491 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1492 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1493 (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1494 (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1496 SCTP_INP_DECR_REF(inp);
1501 tmr->stopped_from = 0xa004;
1503 atomic_add_int(&stcb->asoc.refcnt, 1);
1504 if (stcb->asoc.state == 0) {
1505 atomic_add_int(&stcb->asoc.refcnt, -1);
1507 SCTP_INP_DECR_REF(inp);
1513 tmr->stopped_from = 0xa005;
1514 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type);
1515 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1517 SCTP_INP_DECR_REF(inp);
1520 atomic_add_int(&stcb->asoc.refcnt, -1);
1525 tmr->stopped_from = 0xa006;
1528 SCTP_TCB_LOCK(stcb);
1529 atomic_add_int(&stcb->asoc.refcnt, -1);
1530 if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) &&
1531 ((stcb->asoc.state == 0) ||
1532 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1533 SCTP_TCB_UNLOCK(stcb);
1535 SCTP_INP_DECR_REF(inp);
1541 /* record in stopped what t-o occured */
1542 tmr->stopped_from = tmr->type;
1544 /* mark as being serviced now */
1545 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1547 * Callout has been rescheduled.
1551 if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1553 * Not active, so no action.
1557 SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1559 /* call the handler for the appropriate timer type */
1560 switch (tmr->type) {
1561 case SCTP_TIMER_TYPE_ZERO_COPY:
1565 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1566 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
1569 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1573 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1574 SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket);
1577 case SCTP_TIMER_TYPE_ADDR_WQ:
1578 sctp_handle_addr_wq();
1580 case SCTP_TIMER_TYPE_SEND:
1581 if ((stcb == NULL) || (inp == NULL)) {
1584 SCTP_STAT_INCR(sctps_timodata);
1585 stcb->asoc.timodata++;
1586 stcb->asoc.num_send_timers_up--;
1587 if (stcb->asoc.num_send_timers_up < 0) {
1588 stcb->asoc.num_send_timers_up = 0;
1590 SCTP_TCB_LOCK_ASSERT(stcb);
1591 cur_oerr = stcb->asoc.overall_error_count;
1592 retcode = sctp_t3rxt_timer(inp, stcb, net);
1594 /* no need to unlock on tcb its gone */
1598 SCTP_TCB_LOCK_ASSERT(stcb);
1599 #ifdef SCTP_AUDITING_ENABLED
1600 sctp_auditing(4, inp, stcb, net);
1602 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1603 if ((stcb->asoc.num_send_timers_up == 0) &&
1604 (stcb->asoc.sent_queue_cnt > 0)) {
1605 struct sctp_tmit_chunk *chk;
1608 * safeguard. If there on some on the sent queue
1609 * somewhere but no timers running something is
1610 * wrong... so we start a timer on the first chunk
1611 * on the send queue on whatever net it is sent to.
1613 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1614 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1618 case SCTP_TIMER_TYPE_INIT:
1619 if ((stcb == NULL) || (inp == NULL)) {
1622 SCTP_STAT_INCR(sctps_timoinit);
1623 stcb->asoc.timoinit++;
1624 if (sctp_t1init_timer(inp, stcb, net)) {
1625 /* no need to unlock on tcb its gone */
1628 /* We do output but not here */
1631 case SCTP_TIMER_TYPE_RECV:
1632 if ((stcb == NULL) || (inp == NULL)) {
1635 SCTP_STAT_INCR(sctps_timosack);
1636 stcb->asoc.timosack++;
1637 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1639 #ifdef SCTP_AUDITING_ENABLED
1640 sctp_auditing(4, inp, stcb, net);
1642 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1644 case SCTP_TIMER_TYPE_SHUTDOWN:
1645 if ((stcb == NULL) || (inp == NULL)) {
1648 if (sctp_shutdown_timer(inp, stcb, net)) {
1649 /* no need to unlock on tcb its gone */
1652 SCTP_STAT_INCR(sctps_timoshutdown);
1653 stcb->asoc.timoshutdown++;
1654 #ifdef SCTP_AUDITING_ENABLED
1655 sctp_auditing(4, inp, stcb, net);
1657 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1659 case SCTP_TIMER_TYPE_HEARTBEAT:
1661 struct sctp_nets *lnet;
1662 int cnt_of_unconf = 0;
1664 if ((stcb == NULL) || (inp == NULL)) {
1667 SCTP_STAT_INCR(sctps_timoheartbeat);
1668 stcb->asoc.timoheartbeat++;
1669 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1670 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1671 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
1675 if (cnt_of_unconf == 0) {
1676 if (sctp_heartbeat_timer(inp, stcb, lnet,
1678 /* no need to unlock on tcb its gone */
1682 #ifdef SCTP_AUDITING_ENABLED
1683 sctp_auditing(4, inp, stcb, lnet);
1685 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
1686 stcb->sctp_ep, stcb, lnet);
1687 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1690 case SCTP_TIMER_TYPE_COOKIE:
1691 if ((stcb == NULL) || (inp == NULL)) {
1694 if (sctp_cookie_timer(inp, stcb, net)) {
1695 /* no need to unlock on tcb its gone */
1698 SCTP_STAT_INCR(sctps_timocookie);
1699 stcb->asoc.timocookie++;
1700 #ifdef SCTP_AUDITING_ENABLED
1701 sctp_auditing(4, inp, stcb, net);
1704 * We consider T3 and Cookie timer pretty much the same with
1705 * respect to where from in chunk_output.
1707 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1709 case SCTP_TIMER_TYPE_NEWCOOKIE:
1717 SCTP_STAT_INCR(sctps_timosecret);
1718 (void)SCTP_GETTIME_TIMEVAL(&tv);
1719 SCTP_INP_WLOCK(inp);
1720 inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1721 inp->sctp_ep.last_secret_number =
1722 inp->sctp_ep.current_secret_number;
1723 inp->sctp_ep.current_secret_number++;
1724 if (inp->sctp_ep.current_secret_number >=
1725 SCTP_HOW_MANY_SECRETS) {
1726 inp->sctp_ep.current_secret_number = 0;
1728 secret = (int)inp->sctp_ep.current_secret_number;
1729 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1730 inp->sctp_ep.secret_key[secret][i] =
1731 sctp_select_initial_TSN(&inp->sctp_ep);
1733 SCTP_INP_WUNLOCK(inp);
1734 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1738 case SCTP_TIMER_TYPE_PATHMTURAISE:
1739 if ((stcb == NULL) || (inp == NULL)) {
1742 SCTP_STAT_INCR(sctps_timopathmtu);
1743 sctp_pathmtu_timer(inp, stcb, net);
1746 case SCTP_TIMER_TYPE_SHUTDOWNACK:
1747 if ((stcb == NULL) || (inp == NULL)) {
1750 if (sctp_shutdownack_timer(inp, stcb, net)) {
1751 /* no need to unlock on tcb its gone */
1754 SCTP_STAT_INCR(sctps_timoshutdownack);
1755 stcb->asoc.timoshutdownack++;
1756 #ifdef SCTP_AUDITING_ENABLED
1757 sctp_auditing(4, inp, stcb, net);
1759 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1761 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1762 if ((stcb == NULL) || (inp == NULL)) {
1765 SCTP_STAT_INCR(sctps_timoshutdownguard);
1766 sctp_abort_an_association(inp, stcb,
1767 SCTP_SHUTDOWN_GUARD_EXPIRES, NULL, SCTP_SO_NOT_LOCKED);
1768 /* no need to unlock on tcb its gone */
1771 case SCTP_TIMER_TYPE_STRRESET:
1772 if ((stcb == NULL) || (inp == NULL)) {
1775 if (sctp_strreset_timer(inp, stcb, net)) {
1776 /* no need to unlock on tcb its gone */
1779 SCTP_STAT_INCR(sctps_timostrmrst);
1780 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1782 case SCTP_TIMER_TYPE_EARLYFR:
1783 /* Need to do FR of things for net */
1784 if ((stcb == NULL) || (inp == NULL)) {
1787 SCTP_STAT_INCR(sctps_timoearlyfr);
1788 sctp_early_fr_timer(inp, stcb, net);
1790 case SCTP_TIMER_TYPE_ASCONF:
1791 if ((stcb == NULL) || (inp == NULL)) {
1794 if (sctp_asconf_timer(inp, stcb, net)) {
1795 /* no need to unlock on tcb its gone */
1798 SCTP_STAT_INCR(sctps_timoasconf);
1799 #ifdef SCTP_AUDITING_ENABLED
1800 sctp_auditing(4, inp, stcb, net);
1802 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1804 case SCTP_TIMER_TYPE_PRIM_DELETED:
1805 if ((stcb == NULL) || (inp == NULL)) {
1808 sctp_delete_prim_timer(inp, stcb, net);
1809 SCTP_STAT_INCR(sctps_timodelprim);
1812 case SCTP_TIMER_TYPE_AUTOCLOSE:
1813 if ((stcb == NULL) || (inp == NULL)) {
1816 SCTP_STAT_INCR(sctps_timoautoclose);
1817 sctp_autoclose_timer(inp, stcb, net);
1818 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1821 case SCTP_TIMER_TYPE_ASOCKILL:
1822 if ((stcb == NULL) || (inp == NULL)) {
1825 SCTP_STAT_INCR(sctps_timoassockill);
1826 /* Can we free it yet? */
1827 SCTP_INP_DECR_REF(inp);
1828 sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1829 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1830 so = SCTP_INP_SO(inp);
1831 atomic_add_int(&stcb->asoc.refcnt, 1);
1832 SCTP_TCB_UNLOCK(stcb);
1833 SCTP_SOCKET_LOCK(so, 1);
1834 SCTP_TCB_LOCK(stcb);
1835 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1837 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1838 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1839 SCTP_SOCKET_UNLOCK(so, 1);
1842 * free asoc, always unlocks (or destroy's) so prevent
1843 * duplicate unlock or unlock of a free mtx :-0
1847 case SCTP_TIMER_TYPE_INPKILL:
1848 SCTP_STAT_INCR(sctps_timoinpkill);
1853 * special case, take away our increment since WE are the
1856 SCTP_INP_DECR_REF(inp);
1857 sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1858 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
1859 SCTP_CALLED_FROM_INPKILL_TIMER);
1863 SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1867 #ifdef SCTP_AUDITING_ENABLED
1868 sctp_audit_log(0xF1, (uint8_t) tmr->type);
1870 sctp_auditing(5, inp, stcb, net);
1872 if ((did_output) && stcb) {
1874 * Now we need to clean up the control chunk chain if an
1875 * ECNE is on it. It must be marked as UNSENT again so next
1876 * call will continue to send it until such time that we get
1877 * a CWR, to remove it. It is, however, less likely that we
1878 * will find a ecn echo on the chain though.
1880 sctp_fix_ecn_echo(&stcb->asoc);
1884 SCTP_TCB_UNLOCK(stcb);
1888 SCTP_INP_DECR_REF(inp);
1891 SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n",
1897 sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1898 struct sctp_nets *net)
1901 struct sctp_timer *tmr;
1903 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1910 SCTP_TCB_LOCK_ASSERT(stcb);
1913 case SCTP_TIMER_TYPE_ZERO_COPY:
1914 tmr = &inp->sctp_ep.zero_copy_timer;
1915 to_ticks = SCTP_ZERO_COPY_TICK_DELAY;
1917 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1918 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
1919 to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY;
1921 case SCTP_TIMER_TYPE_ADDR_WQ:
1922 /* Only 1 tick away :-) */
1923 tmr = &SCTP_BASE_INFO(addr_wq_timer);
1924 to_ticks = SCTP_ADDRESS_TICK_DELAY;
1926 case SCTP_TIMER_TYPE_SEND:
1927 /* Here we use the RTO timer */
1931 if ((stcb == NULL) || (net == NULL)) {
1934 tmr = &net->rxt_timer;
1935 if (net->RTO == 0) {
1936 rto_val = stcb->asoc.initial_rto;
1940 to_ticks = MSEC_TO_TICKS(rto_val);
1943 case SCTP_TIMER_TYPE_INIT:
1945 * Here we use the INIT timer default usually about 1
1948 if ((stcb == NULL) || (net == NULL)) {
1951 tmr = &net->rxt_timer;
1952 if (net->RTO == 0) {
1953 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1955 to_ticks = MSEC_TO_TICKS(net->RTO);
1958 case SCTP_TIMER_TYPE_RECV:
1960 * Here we use the Delayed-Ack timer value from the inp
1961 * ususually about 200ms.
1966 tmr = &stcb->asoc.dack_timer;
1967 to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
1969 case SCTP_TIMER_TYPE_SHUTDOWN:
1970 /* Here we use the RTO of the destination. */
1971 if ((stcb == NULL) || (net == NULL)) {
1974 if (net->RTO == 0) {
1975 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1977 to_ticks = MSEC_TO_TICKS(net->RTO);
1979 tmr = &net->rxt_timer;
1981 case SCTP_TIMER_TYPE_HEARTBEAT:
1983 * the net is used here so that we can add in the RTO. Even
1984 * though we use a different timer. We also add the HB timer
1985 * PLUS a random jitter.
1987 if ((inp == NULL) || (stcb == NULL)) {
1991 uint8_t this_random;
1992 int cnt_of_unconf = 0;
1993 struct sctp_nets *lnet;
1995 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
1996 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1997 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
2001 if (cnt_of_unconf) {
2003 (void)sctp_heartbeat_timer(inp, stcb, lnet, cnt_of_unconf);
2005 if (stcb->asoc.hb_random_idx > 3) {
2006 rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2007 memcpy(stcb->asoc.hb_random_values, &rndval,
2008 sizeof(stcb->asoc.hb_random_values));
2009 stcb->asoc.hb_random_idx = 0;
2011 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
2012 stcb->asoc.hb_random_idx++;
2013 stcb->asoc.hb_ect_randombit = 0;
2015 * this_random will be 0 - 256 ms RTO is in ms.
2017 if ((stcb->asoc.hb_is_disabled) &&
2018 (cnt_of_unconf == 0)) {
2024 delay = stcb->asoc.heart_beat_delay;
2025 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
2026 if ((lnet->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2027 ((lnet->dest_state & SCTP_ADDR_OUT_OF_SCOPE) == 0) &&
2028 (lnet->dest_state & SCTP_ADDR_REACHABLE)) {
2032 if (net->RTO == 0) {
2033 /* Never been checked */
2034 to_ticks = this_random + stcb->asoc.initial_rto + delay;
2036 /* set rto_val to the ms */
2037 to_ticks = delay + net->RTO + this_random;
2040 if (cnt_of_unconf) {
2041 to_ticks = this_random + stcb->asoc.initial_rto;
2043 to_ticks = stcb->asoc.heart_beat_delay + this_random + stcb->asoc.initial_rto;
2047 * Now we must convert the to_ticks that are now in
2050 to_ticks = MSEC_TO_TICKS(to_ticks);
2051 tmr = &stcb->asoc.hb_timer;
2054 case SCTP_TIMER_TYPE_COOKIE:
2056 * Here we can use the RTO timer from the network since one
2057 * RTT was compelete. If a retran happened then we will be
2058 * using the RTO initial value.
2060 if ((stcb == NULL) || (net == NULL)) {
2063 if (net->RTO == 0) {
2064 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2066 to_ticks = MSEC_TO_TICKS(net->RTO);
2068 tmr = &net->rxt_timer;
2070 case SCTP_TIMER_TYPE_NEWCOOKIE:
2072 * nothing needed but the endpoint here ususually about 60
2078 tmr = &inp->sctp_ep.signature_change;
2079 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2081 case SCTP_TIMER_TYPE_ASOCKILL:
2085 tmr = &stcb->asoc.strreset_timer;
2086 to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2088 case SCTP_TIMER_TYPE_INPKILL:
2090 * The inp is setup to die. We re-use the signature_chage
2091 * timer since that has stopped and we are in the GONE
2097 tmr = &inp->sctp_ep.signature_change;
2098 to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2100 case SCTP_TIMER_TYPE_PATHMTURAISE:
2102 * Here we use the value found in the EP for PMTU ususually
2105 if ((stcb == NULL) || (inp == NULL)) {
2111 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2112 tmr = &net->pmtu_timer;
2114 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2115 /* Here we use the RTO of the destination */
2116 if ((stcb == NULL) || (net == NULL)) {
2119 if (net->RTO == 0) {
2120 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2122 to_ticks = MSEC_TO_TICKS(net->RTO);
2124 tmr = &net->rxt_timer;
2126 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2128 * Here we use the endpoints shutdown guard timer usually
2131 if ((inp == NULL) || (stcb == NULL)) {
2134 to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2135 tmr = &stcb->asoc.shut_guard_timer;
2137 case SCTP_TIMER_TYPE_STRRESET:
2139 * Here the timer comes from the stcb but its value is from
2142 if ((stcb == NULL) || (net == NULL)) {
2145 if (net->RTO == 0) {
2146 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2148 to_ticks = MSEC_TO_TICKS(net->RTO);
2150 tmr = &stcb->asoc.strreset_timer;
2153 case SCTP_TIMER_TYPE_EARLYFR:
2157 if ((stcb == NULL) || (net == NULL)) {
2160 if (net->flight_size > net->cwnd) {
2161 /* no need to start */
2164 SCTP_STAT_INCR(sctps_earlyfrstart);
2165 if (net->lastsa == 0) {
2166 /* Hmm no rtt estimate yet? */
2167 msec = stcb->asoc.initial_rto >> 2;
2169 msec = ((net->lastsa >> 2) + net->lastsv) >> 1;
2171 if (msec < SCTP_BASE_SYSCTL(sctp_early_fr_msec)) {
2172 msec = SCTP_BASE_SYSCTL(sctp_early_fr_msec);
2173 if (msec < SCTP_MINFR_MSEC_FLOOR) {
2174 msec = SCTP_MINFR_MSEC_FLOOR;
2177 to_ticks = MSEC_TO_TICKS(msec);
2178 tmr = &net->fr_timer;
2181 case SCTP_TIMER_TYPE_ASCONF:
2183 * Here the timer comes from the stcb but its value is from
2186 if ((stcb == NULL) || (net == NULL)) {
2189 if (net->RTO == 0) {
2190 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2192 to_ticks = MSEC_TO_TICKS(net->RTO);
2194 tmr = &stcb->asoc.asconf_timer;
2196 case SCTP_TIMER_TYPE_PRIM_DELETED:
2197 if ((stcb == NULL) || (net != NULL)) {
2200 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2201 tmr = &stcb->asoc.delete_prim_timer;
2203 case SCTP_TIMER_TYPE_AUTOCLOSE:
2207 if (stcb->asoc.sctp_autoclose_ticks == 0) {
2209 * Really an error since stcb is NOT set to
2214 to_ticks = stcb->asoc.sctp_autoclose_ticks;
2215 tmr = &stcb->asoc.autoclose_timer;
2218 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2219 __FUNCTION__, t_type);
2223 if ((to_ticks <= 0) || (tmr == NULL)) {
2224 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2225 __FUNCTION__, t_type, to_ticks, tmr);
2228 if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2230 * we do NOT allow you to have it already running. if it is
2231 * we leave the current one up unchanged
2235 /* At this point we can proceed */
2236 if (t_type == SCTP_TIMER_TYPE_SEND) {
2237 stcb->asoc.num_send_timers_up++;
2239 tmr->stopped_from = 0;
2241 tmr->ep = (void *)inp;
2242 tmr->tcb = (void *)stcb;
2243 tmr->net = (void *)net;
2244 tmr->self = (void *)tmr;
2245 tmr->vnet = (void *)curvnet;
2246 tmr->ticks = sctp_get_tick_count();
2247 (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2252 sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2253 struct sctp_nets *net, uint32_t from)
2255 struct sctp_timer *tmr;
2257 if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2263 SCTP_TCB_LOCK_ASSERT(stcb);
2266 case SCTP_TIMER_TYPE_ZERO_COPY:
2267 tmr = &inp->sctp_ep.zero_copy_timer;
2269 case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
2270 tmr = &inp->sctp_ep.zero_copy_sendq_timer;
2272 case SCTP_TIMER_TYPE_ADDR_WQ:
2273 tmr = &SCTP_BASE_INFO(addr_wq_timer);
2275 case SCTP_TIMER_TYPE_EARLYFR:
2276 if ((stcb == NULL) || (net == NULL)) {
2279 tmr = &net->fr_timer;
2280 SCTP_STAT_INCR(sctps_earlyfrstop);
2282 case SCTP_TIMER_TYPE_SEND:
2283 if ((stcb == NULL) || (net == NULL)) {
2286 tmr = &net->rxt_timer;
2288 case SCTP_TIMER_TYPE_INIT:
2289 if ((stcb == NULL) || (net == NULL)) {
2292 tmr = &net->rxt_timer;
2294 case SCTP_TIMER_TYPE_RECV:
2298 tmr = &stcb->asoc.dack_timer;
2300 case SCTP_TIMER_TYPE_SHUTDOWN:
2301 if ((stcb == NULL) || (net == NULL)) {
2304 tmr = &net->rxt_timer;
2306 case SCTP_TIMER_TYPE_HEARTBEAT:
2310 tmr = &stcb->asoc.hb_timer;
2312 case SCTP_TIMER_TYPE_COOKIE:
2313 if ((stcb == NULL) || (net == NULL)) {
2316 tmr = &net->rxt_timer;
2318 case SCTP_TIMER_TYPE_NEWCOOKIE:
2319 /* nothing needed but the endpoint here */
2320 tmr = &inp->sctp_ep.signature_change;
2322 * We re-use the newcookie timer for the INP kill timer. We
2323 * must assure that we do not kill it by accident.
2326 case SCTP_TIMER_TYPE_ASOCKILL:
2328 * Stop the asoc kill timer.
2333 tmr = &stcb->asoc.strreset_timer;
2336 case SCTP_TIMER_TYPE_INPKILL:
2338 * The inp is setup to die. We re-use the signature_chage
2339 * timer since that has stopped and we are in the GONE
2342 tmr = &inp->sctp_ep.signature_change;
2344 case SCTP_TIMER_TYPE_PATHMTURAISE:
2345 if ((stcb == NULL) || (net == NULL)) {
2348 tmr = &net->pmtu_timer;
2350 case SCTP_TIMER_TYPE_SHUTDOWNACK:
2351 if ((stcb == NULL) || (net == NULL)) {
2354 tmr = &net->rxt_timer;
2356 case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2360 tmr = &stcb->asoc.shut_guard_timer;
2362 case SCTP_TIMER_TYPE_STRRESET:
2366 tmr = &stcb->asoc.strreset_timer;
2368 case SCTP_TIMER_TYPE_ASCONF:
2372 tmr = &stcb->asoc.asconf_timer;
2374 case SCTP_TIMER_TYPE_PRIM_DELETED:
2378 tmr = &stcb->asoc.delete_prim_timer;
2380 case SCTP_TIMER_TYPE_AUTOCLOSE:
2384 tmr = &stcb->asoc.autoclose_timer;
2387 SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2388 __FUNCTION__, t_type);
2394 if ((tmr->type != t_type) && tmr->type) {
2396 * Ok we have a timer that is under joint use. Cookie timer
2397 * per chance with the SEND timer. We therefore are NOT
2398 * running the timer that the caller wants stopped. So just
2403 if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2404 stcb->asoc.num_send_timers_up--;
2405 if (stcb->asoc.num_send_timers_up < 0) {
2406 stcb->asoc.num_send_timers_up = 0;
2410 tmr->stopped_from = from;
2411 (void)SCTP_OS_TIMER_STOP(&tmr->timer);
2416 sctp_calculate_len(struct mbuf *m)
2423 tlen += SCTP_BUF_LEN(at);
2424 at = SCTP_BUF_NEXT(at);
2430 sctp_mtu_size_reset(struct sctp_inpcb *inp,
2431 struct sctp_association *asoc, uint32_t mtu)
2434 * Reset the P-MTU size on this association, this involves changing
2435 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2436 * allow the DF flag to be cleared.
2438 struct sctp_tmit_chunk *chk;
2439 unsigned int eff_mtu, ovh;
2441 asoc->smallest_mtu = mtu;
2442 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2443 ovh = SCTP_MIN_OVERHEAD;
2445 ovh = SCTP_MIN_V4_OVERHEAD;
2447 eff_mtu = mtu - ovh;
2448 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2449 if (chk->send_size > eff_mtu) {
2450 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2453 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2454 if (chk->send_size > eff_mtu) {
2455 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2462 * given an association and starting time of the current RTT period return
2463 * RTO in number of msecs net should point to the current network
2467 sctp_calculate_rto(struct sctp_tcb *stcb,
2468 struct sctp_association *asoc,
2469 struct sctp_nets *net,
2470 struct timeval *told,
2471 int safe, int rtt_from_sack)
2474 * given an association and the starting time of the current RTT
2475 * period (in value1/value2) return RTO in number of msecs.
2477 int32_t rtt; /* RTT in ms */
2479 int first_measure = 0;
2480 struct timeval now, then, *old;
2482 /* Copy it out for sparc64 */
2483 if (safe == sctp_align_unsafe_makecopy) {
2485 memcpy(&then, told, sizeof(struct timeval));
2486 } else if (safe == sctp_align_safe_nocopy) {
2490 SCTP_PRINTF("Huh, bad rto calc call\n");
2493 /************************/
2494 /* 1. calculate new RTT */
2495 /************************/
2496 /* get the current time */
2497 if (stcb->asoc.use_precise_time) {
2498 (void)SCTP_GETPTIME_TIMEVAL(&now);
2500 (void)SCTP_GETTIME_TIMEVAL(&now);
2502 timevalsub(&now, old);
2503 /* store the current RTT in us */
2504 net->rtt = (uint64_t) 10000000 *(uint64_t) now.tv_sec +
2505 (uint64_t) now.tv_usec;
2507 /* computer rtt in ms */
2508 rtt = net->rtt / 1000;
2509 if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2511 * Tell the CC module that a new update has just occurred
2514 (*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2517 * Do we need to determine the lan? We do this only on sacks i.e.
2518 * RTT being determined from data not non-data (HB/INIT->INITACK).
2520 if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2521 (net->lan_type == SCTP_LAN_UNKNOWN)) {
2522 if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2523 net->lan_type = SCTP_LAN_INTERNET;
2525 net->lan_type = SCTP_LAN_LOCAL;
2528 /***************************/
2529 /* 2. update RTTVAR & SRTT */
2530 /***************************/
2532 * Compute the scaled average lastsa and the
2533 * scaled variance lastsv as described in van Jacobson
2534 * Paper "Congestion Avoidance and Control", Annex A.
2536 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
2537 * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar
2539 if (net->RTO_measured) {
2540 rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2545 rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2547 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2548 rto_logging(net, SCTP_LOG_RTTVAR);
2551 /* First RTO measurment */
2552 net->RTO_measured = 1;
2554 net->lastsa = rtt << SCTP_RTT_SHIFT;
2555 net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2556 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2557 rto_logging(net, SCTP_LOG_INITIAL_RTT);
2560 if (net->lastsv == 0) {
2561 net->lastsv = SCTP_CLOCK_GRANULARITY;
2563 new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2564 if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2565 (stcb->asoc.sat_network_lockout == 0)) {
2566 stcb->asoc.sat_network = 1;
2567 } else if ((!first_measure) && stcb->asoc.sat_network) {
2568 stcb->asoc.sat_network = 0;
2569 stcb->asoc.sat_network_lockout = 1;
2571 /* bound it, per C6/C7 in Section 5.3.1 */
2572 if (new_rto < stcb->asoc.minrto) {
2573 new_rto = stcb->asoc.minrto;
2575 if (new_rto > stcb->asoc.maxrto) {
2576 new_rto = stcb->asoc.maxrto;
2578 /* we are now returning the RTO */
2583 * return a pointer to a contiguous piece of data from the given mbuf chain
2584 * starting at 'off' for 'len' bytes. If the desired piece spans more than
2585 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2586 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2589 sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2595 if ((off < 0) || (len <= 0))
2598 /* find the desired start location */
2599 while ((m != NULL) && (off > 0)) {
2600 if (off < SCTP_BUF_LEN(m))
2602 off -= SCTP_BUF_LEN(m);
2603 m = SCTP_BUF_NEXT(m);
2608 /* is the current mbuf large enough (eg. contiguous)? */
2609 if ((SCTP_BUF_LEN(m) - off) >= len) {
2610 return (mtod(m, caddr_t)+off);
2612 /* else, it spans more than one mbuf, so save a temp copy... */
2613 while ((m != NULL) && (len > 0)) {
2614 count = min(SCTP_BUF_LEN(m) - off, len);
2615 bcopy(mtod(m, caddr_t)+off, ptr, count);
2619 m = SCTP_BUF_NEXT(m);
2621 if ((m == NULL) && (len > 0))
2624 return ((caddr_t)in_ptr);
2630 struct sctp_paramhdr *
2631 sctp_get_next_param(struct mbuf *m,
2633 struct sctp_paramhdr *pull,
2636 /* This just provides a typed signature to Peter's Pull routine */
2637 return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2643 sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2646 * add padlen bytes of 0 filled padding to the end of the mbuf. If
2647 * padlen is > 3 this routine will fail.
2653 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
2656 if (padlen <= M_TRAILINGSPACE(m)) {
2658 * The easy way. We hope the majority of the time we hit
2661 dp = (uint8_t *) (mtod(m, caddr_t)+SCTP_BUF_LEN(m));
2662 SCTP_BUF_LEN(m) += padlen;
2664 /* Hard way we must grow the mbuf */
2667 tmp = sctp_get_mbuf_for_msg(padlen, 0, M_DONTWAIT, 1, MT_DATA);
2669 /* Out of space GAK! we are in big trouble. */
2670 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
2673 /* setup and insert in middle */
2674 SCTP_BUF_LEN(tmp) = padlen;
2675 SCTP_BUF_NEXT(tmp) = NULL;
2676 SCTP_BUF_NEXT(m) = tmp;
2677 dp = mtod(tmp, uint8_t *);
2679 /* zero out the pad */
2680 for (i = 0; i < padlen; i++) {
2688 sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2690 /* find the last mbuf in chain and pad it */
2695 return (sctp_add_pad_tombuf(last_mbuf, padval));
2698 if (SCTP_BUF_NEXT(m_at) == NULL) {
2699 return (sctp_add_pad_tombuf(m_at, padval));
2701 m_at = SCTP_BUF_NEXT(m_at);
2704 SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
2709 sctp_notify_assoc_change(uint32_t event, struct sctp_tcb *stcb,
2710 uint32_t error, void *data, int so_locked
2711 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2716 struct mbuf *m_notify;
2717 struct sctp_assoc_change *sac;
2718 struct sctp_queued_to_read *control;
2720 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2726 * For TCP model AND UDP connected sockets we will send an error up
2727 * when an ABORT comes in.
2729 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2730 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2731 ((event == SCTP_COMM_LOST) || (event == SCTP_CANT_STR_ASSOC))) {
2732 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
2733 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
2734 stcb->sctp_socket->so_error = ECONNREFUSED;
2736 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
2737 stcb->sctp_socket->so_error = ECONNRESET;
2739 /* Wake ANY sleepers */
2740 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2741 so = SCTP_INP_SO(stcb->sctp_ep);
2743 atomic_add_int(&stcb->asoc.refcnt, 1);
2744 SCTP_TCB_UNLOCK(stcb);
2745 SCTP_SOCKET_LOCK(so, 1);
2746 SCTP_TCB_LOCK(stcb);
2747 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2748 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2749 SCTP_SOCKET_UNLOCK(so, 1);
2754 socantrcvmore(stcb->sctp_socket);
2755 sorwakeup(stcb->sctp_socket);
2756 sowwakeup(stcb->sctp_socket);
2757 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2759 SCTP_SOCKET_UNLOCK(so, 1);
2763 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2764 /* event not enabled */
2767 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_change), 0, M_DONTWAIT, 1, MT_DATA);
2768 if (m_notify == NULL)
2771 SCTP_BUF_LEN(m_notify) = 0;
2773 sac = mtod(m_notify, struct sctp_assoc_change *);
2774 sac->sac_type = SCTP_ASSOC_CHANGE;
2776 sac->sac_length = sizeof(struct sctp_assoc_change);
2777 sac->sac_state = event;
2778 sac->sac_error = error;
2779 /* XXX verify these stream counts */
2780 sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2781 sac->sac_inbound_streams = stcb->asoc.streamincnt;
2782 sac->sac_assoc_id = sctp_get_associd(stcb);
2783 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_change);
2784 SCTP_BUF_NEXT(m_notify) = NULL;
2785 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2788 if (control == NULL) {
2790 sctp_m_freem(m_notify);
2793 control->length = SCTP_BUF_LEN(m_notify);
2794 /* not that we need this */
2795 control->tail_mbuf = m_notify;
2796 control->spec_flags = M_NOTIFICATION;
2797 sctp_add_to_readq(stcb->sctp_ep, stcb,
2799 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2801 if (event == SCTP_COMM_LOST) {
2802 /* Wake up any sleeper */
2803 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2804 so = SCTP_INP_SO(stcb->sctp_ep);
2806 atomic_add_int(&stcb->asoc.refcnt, 1);
2807 SCTP_TCB_UNLOCK(stcb);
2808 SCTP_SOCKET_LOCK(so, 1);
2809 SCTP_TCB_LOCK(stcb);
2810 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2811 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2812 SCTP_SOCKET_UNLOCK(so, 1);
2817 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
2818 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2820 SCTP_SOCKET_UNLOCK(so, 1);
2827 sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2828 struct sockaddr *sa, uint32_t error)
2830 struct mbuf *m_notify;
2831 struct sctp_paddr_change *spc;
2832 struct sctp_queued_to_read *control;
2834 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2835 /* event not enabled */
2838 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_DONTWAIT, 1, MT_DATA);
2839 if (m_notify == NULL)
2841 SCTP_BUF_LEN(m_notify) = 0;
2842 spc = mtod(m_notify, struct sctp_paddr_change *);
2843 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2845 spc->spc_length = sizeof(struct sctp_paddr_change);
2846 switch (sa->sa_family) {
2849 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2855 struct sockaddr_in6 *sin6;
2857 memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2859 sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2860 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2861 if (sin6->sin6_scope_id == 0) {
2862 /* recover scope_id for user */
2863 (void)sa6_recoverscope(sin6);
2865 /* clear embedded scope_id for user */
2866 in6_clearscope(&sin6->sin6_addr);
2876 spc->spc_state = state;
2877 spc->spc_error = error;
2878 spc->spc_assoc_id = sctp_get_associd(stcb);
2880 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2881 SCTP_BUF_NEXT(m_notify) = NULL;
2883 /* append to socket */
2884 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2887 if (control == NULL) {
2889 sctp_m_freem(m_notify);
2892 control->length = SCTP_BUF_LEN(m_notify);
2893 control->spec_flags = M_NOTIFICATION;
2894 /* not that we need this */
2895 control->tail_mbuf = m_notify;
2896 sctp_add_to_readq(stcb->sctp_ep, stcb,
2898 &stcb->sctp_socket->so_rcv, 1,
2899 SCTP_READ_LOCK_NOT_HELD,
2900 SCTP_SO_NOT_LOCKED);
2905 sctp_notify_send_failed(struct sctp_tcb *stcb, uint32_t error,
2906 struct sctp_tmit_chunk *chk, int so_locked
2907 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2912 struct mbuf *m_notify;
2913 struct sctp_send_failed *ssf;
2914 struct sctp_queued_to_read *control;
2917 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)) {
2918 /* event not enabled */
2921 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 0, M_DONTWAIT, 1, MT_DATA);
2922 if (m_notify == NULL)
2925 length = sizeof(struct sctp_send_failed) + chk->send_size;
2926 length -= sizeof(struct sctp_data_chunk);
2927 SCTP_BUF_LEN(m_notify) = 0;
2928 ssf = mtod(m_notify, struct sctp_send_failed *);
2929 ssf->ssf_type = SCTP_SEND_FAILED;
2930 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
2931 ssf->ssf_flags = SCTP_DATA_UNSENT;
2933 ssf->ssf_flags = SCTP_DATA_SENT;
2934 ssf->ssf_length = length;
2935 ssf->ssf_error = error;
2936 /* not exactly what the user sent in, but should be close :) */
2937 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
2938 ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
2939 ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
2940 ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
2941 ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
2942 ssf->ssf_info.sinfo_context = chk->rec.data.context;
2943 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2944 ssf->ssf_assoc_id = sctp_get_associd(stcb);
2948 * trim off the sctp chunk header(it should be there)
2950 if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
2951 m_adj(chk->data, sizeof(struct sctp_data_chunk));
2952 sctp_mbuf_crush(chk->data);
2953 chk->send_size -= sizeof(struct sctp_data_chunk);
2956 SCTP_BUF_NEXT(m_notify) = chk->data;
2957 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
2958 /* Steal off the mbuf */
2961 * For this case, we check the actual socket buffer, since the assoc
2962 * is going away we don't want to overfill the socket buffer for a
2965 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
2966 sctp_m_freem(m_notify);
2969 /* append to socket */
2970 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
2973 if (control == NULL) {
2975 sctp_m_freem(m_notify);
2978 control->spec_flags = M_NOTIFICATION;
2979 sctp_add_to_readq(stcb->sctp_ep, stcb,
2981 &stcb->sctp_socket->so_rcv, 1,
2982 SCTP_READ_LOCK_NOT_HELD,
2988 sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
2989 struct sctp_stream_queue_pending *sp, int so_locked
2990 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2995 struct mbuf *m_notify;
2996 struct sctp_send_failed *ssf;
2997 struct sctp_queued_to_read *control;
3000 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)) {
3001 /* event not enabled */
3004 length = sizeof(struct sctp_send_failed) + sp->length;
3005 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_send_failed), 0, M_DONTWAIT, 1, MT_DATA);
3006 if (m_notify == NULL)
3009 SCTP_BUF_LEN(m_notify) = 0;
3010 ssf = mtod(m_notify, struct sctp_send_failed *);
3011 ssf->ssf_type = SCTP_SEND_FAILED;
3012 if (error == SCTP_NOTIFY_DATAGRAM_UNSENT)
3013 ssf->ssf_flags = SCTP_DATA_UNSENT;
3015 ssf->ssf_flags = SCTP_DATA_SENT;
3016 ssf->ssf_length = length;
3017 ssf->ssf_error = error;
3018 /* not exactly what the user sent in, but should be close :) */
3019 bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
3020 ssf->ssf_info.sinfo_stream = sp->stream;
3021 ssf->ssf_info.sinfo_ssn = sp->strseq;
3022 if (sp->some_taken) {
3023 ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3025 ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3027 ssf->ssf_info.sinfo_ppid = sp->ppid;
3028 ssf->ssf_info.sinfo_context = sp->context;
3029 ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3030 ssf->ssf_assoc_id = sctp_get_associd(stcb);
3031 SCTP_BUF_NEXT(m_notify) = sp->data;
3032 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
3034 /* Steal off the mbuf */
3037 * For this case, we check the actual socket buffer, since the assoc
3038 * is going away we don't want to overfill the socket buffer for a
3041 if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3042 sctp_m_freem(m_notify);
3045 /* append to socket */
3046 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3049 if (control == NULL) {
3051 sctp_m_freem(m_notify);
3054 control->spec_flags = M_NOTIFICATION;
3055 sctp_add_to_readq(stcb->sctp_ep, stcb,
3057 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3063 sctp_notify_adaptation_layer(struct sctp_tcb *stcb,
3066 struct mbuf *m_notify;
3067 struct sctp_adaptation_event *sai;
3068 struct sctp_queued_to_read *control;
3070 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3071 /* event not enabled */
3074 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_DONTWAIT, 1, MT_DATA);
3075 if (m_notify == NULL)
3078 SCTP_BUF_LEN(m_notify) = 0;
3079 sai = mtod(m_notify, struct sctp_adaptation_event *);
3080 sai->sai_type = SCTP_ADAPTATION_INDICATION;
3082 sai->sai_length = sizeof(struct sctp_adaptation_event);
3083 sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3084 sai->sai_assoc_id = sctp_get_associd(stcb);
3086 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3087 SCTP_BUF_NEXT(m_notify) = NULL;
3089 /* append to socket */
3090 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3093 if (control == NULL) {
3095 sctp_m_freem(m_notify);
3098 control->length = SCTP_BUF_LEN(m_notify);
3099 control->spec_flags = M_NOTIFICATION;
3100 /* not that we need this */
3101 control->tail_mbuf = m_notify;
3102 sctp_add_to_readq(stcb->sctp_ep, stcb,
3104 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3107 /* This always must be called with the read-queue LOCKED in the INP */
3109 sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3110 uint32_t val, int so_locked
3111 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3116 struct mbuf *m_notify;
3117 struct sctp_pdapi_event *pdapi;
3118 struct sctp_queued_to_read *control;
3121 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3122 /* event not enabled */
3125 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3128 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_DONTWAIT, 1, MT_DATA);
3129 if (m_notify == NULL)
3132 SCTP_BUF_LEN(m_notify) = 0;
3133 pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3134 pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3135 pdapi->pdapi_flags = 0;
3136 pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3137 pdapi->pdapi_indication = error;
3138 pdapi->pdapi_stream = (val >> 16);
3139 pdapi->pdapi_seq = (val & 0x0000ffff);
3140 pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3142 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3143 SCTP_BUF_NEXT(m_notify) = NULL;
3144 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3147 if (control == NULL) {
3149 sctp_m_freem(m_notify);
3152 control->spec_flags = M_NOTIFICATION;
3153 control->length = SCTP_BUF_LEN(m_notify);
3154 /* not that we need this */
3155 control->tail_mbuf = m_notify;
3156 control->held_length = 0;
3157 control->length = 0;
3158 sb = &stcb->sctp_socket->so_rcv;
3159 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3160 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
3162 sctp_sballoc(stcb, sb, m_notify);
3163 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3164 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
3166 atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify));
3167 control->end_added = 1;
3168 if (stcb->asoc.control_pdapi)
3169 TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
3171 /* we really should not see this case */
3172 TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
3174 if (stcb->sctp_ep && stcb->sctp_socket) {
3175 /* This should always be the case */
3176 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3179 so = SCTP_INP_SO(stcb->sctp_ep);
3181 atomic_add_int(&stcb->asoc.refcnt, 1);
3182 SCTP_TCB_UNLOCK(stcb);
3183 SCTP_SOCKET_LOCK(so, 1);
3184 SCTP_TCB_LOCK(stcb);
3185 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3186 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3187 SCTP_SOCKET_UNLOCK(so, 1);
3192 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3193 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3195 SCTP_SOCKET_UNLOCK(so, 1);
3202 sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3204 struct mbuf *m_notify;
3205 struct sctp_shutdown_event *sse;
3206 struct sctp_queued_to_read *control;
3209 * For TCP model AND UDP connected sockets we will send an error up
3210 * when an SHUTDOWN completes
3212 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3213 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3214 /* mark socket closed for read/write and wakeup! */
3215 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3218 so = SCTP_INP_SO(stcb->sctp_ep);
3219 atomic_add_int(&stcb->asoc.refcnt, 1);
3220 SCTP_TCB_UNLOCK(stcb);
3221 SCTP_SOCKET_LOCK(so, 1);
3222 SCTP_TCB_LOCK(stcb);
3223 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3224 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3225 SCTP_SOCKET_UNLOCK(so, 1);
3229 socantsendmore(stcb->sctp_socket);
3230 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3231 SCTP_SOCKET_UNLOCK(so, 1);
3234 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3235 /* event not enabled */
3238 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_DONTWAIT, 1, MT_DATA);
3239 if (m_notify == NULL)
3242 sse = mtod(m_notify, struct sctp_shutdown_event *);
3243 sse->sse_type = SCTP_SHUTDOWN_EVENT;
3245 sse->sse_length = sizeof(struct sctp_shutdown_event);
3246 sse->sse_assoc_id = sctp_get_associd(stcb);
3248 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3249 SCTP_BUF_NEXT(m_notify) = NULL;
3251 /* append to socket */
3252 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3255 if (control == NULL) {
3257 sctp_m_freem(m_notify);
3260 control->spec_flags = M_NOTIFICATION;
3261 control->length = SCTP_BUF_LEN(m_notify);
3262 /* not that we need this */
3263 control->tail_mbuf = m_notify;
3264 sctp_add_to_readq(stcb->sctp_ep, stcb,
3266 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3270 sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3272 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3277 struct mbuf *m_notify;
3278 struct sctp_sender_dry_event *event;
3279 struct sctp_queued_to_read *control;
3281 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_DRYEVNT)) {
3282 /* event not enabled */
3285 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_DONTWAIT, 1, MT_DATA);
3286 if (m_notify == NULL) {
3290 SCTP_BUF_LEN(m_notify) = 0;
3291 event = mtod(m_notify, struct sctp_sender_dry_event *);
3292 event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3293 event->sender_dry_flags = 0;
3294 event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3295 event->sender_dry_assoc_id = sctp_get_associd(stcb);
3297 SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3298 SCTP_BUF_NEXT(m_notify) = NULL;
3300 /* append to socket */
3301 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3302 0, 0, 0, 0, 0, 0, m_notify);
3303 if (control == NULL) {
3305 sctp_m_freem(m_notify);
3308 control->length = SCTP_BUF_LEN(m_notify);
3309 control->spec_flags = M_NOTIFICATION;
3310 /* not that we need this */
3311 control->tail_mbuf = m_notify;
3312 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3313 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3318 sctp_notify_stream_reset_add(struct sctp_tcb *stcb, int number_entries, int flag)
3320 struct mbuf *m_notify;
3321 struct sctp_queued_to_read *control;
3322 struct sctp_stream_reset_event *strreset;
3325 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
3326 /* event not enabled */
3329 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3330 if (m_notify == NULL)
3333 SCTP_BUF_LEN(m_notify) = 0;
3334 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3335 if (len > M_TRAILINGSPACE(m_notify)) {
3336 /* never enough room */
3337 sctp_m_freem(m_notify);
3340 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3341 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3342 strreset->strreset_flags = SCTP_STRRESET_ADD_STREAM | flag;
3343 strreset->strreset_length = len;
3344 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3345 strreset->strreset_list[0] = number_entries;
3347 SCTP_BUF_LEN(m_notify) = len;
3348 SCTP_BUF_NEXT(m_notify) = NULL;
3349 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3351 sctp_m_freem(m_notify);
3354 /* append to socket */
3355 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3358 if (control == NULL) {
3360 sctp_m_freem(m_notify);
3363 control->spec_flags = M_NOTIFICATION;
3364 control->length = SCTP_BUF_LEN(m_notify);
3365 /* not that we need this */
3366 control->tail_mbuf = m_notify;
3367 sctp_add_to_readq(stcb->sctp_ep, stcb,
3369 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3374 sctp_notify_stream_reset(struct sctp_tcb *stcb,
3375 int number_entries, uint16_t * list, int flag)
3377 struct mbuf *m_notify;
3378 struct sctp_queued_to_read *control;
3379 struct sctp_stream_reset_event *strreset;
3382 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
3383 /* event not enabled */
3386 m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3387 if (m_notify == NULL)
3390 SCTP_BUF_LEN(m_notify) = 0;
3391 len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3392 if (len > M_TRAILINGSPACE(m_notify)) {
3393 /* never enough room */
3394 sctp_m_freem(m_notify);
3397 strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3398 strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3399 if (number_entries == 0) {
3400 strreset->strreset_flags = flag | SCTP_STRRESET_ALL_STREAMS;
3402 strreset->strreset_flags = flag | SCTP_STRRESET_STREAM_LIST;
3404 strreset->strreset_length = len;
3405 strreset->strreset_assoc_id = sctp_get_associd(stcb);
3406 if (number_entries) {
3409 for (i = 0; i < number_entries; i++) {
3410 strreset->strreset_list[i] = ntohs(list[i]);
3413 SCTP_BUF_LEN(m_notify) = len;
3414 SCTP_BUF_NEXT(m_notify) = NULL;
3415 if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3417 sctp_m_freem(m_notify);
3420 /* append to socket */
3421 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3424 if (control == NULL) {
3426 sctp_m_freem(m_notify);
3429 control->spec_flags = M_NOTIFICATION;
3430 control->length = SCTP_BUF_LEN(m_notify);
3431 /* not that we need this */
3432 control->tail_mbuf = m_notify;
3433 sctp_add_to_readq(stcb->sctp_ep, stcb,
3435 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3440 sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3441 uint32_t error, void *data, int so_locked
3442 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3447 if ((stcb == NULL) ||
3448 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3449 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3450 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3451 /* If the socket is gone we are out of here */
3454 if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3457 if (stcb && ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3458 (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED))) {
3459 if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
3460 (notification == SCTP_NOTIFY_INTERFACE_UP) ||
3461 (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
3462 /* Don't report these in front states */
3466 switch (notification) {
3467 case SCTP_NOTIFY_ASSOC_UP:
3468 if (stcb->asoc.assoc_up_sent == 0) {
3469 sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, so_locked);
3470 stcb->asoc.assoc_up_sent = 1;
3472 if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
3473 sctp_notify_adaptation_layer(stcb, error);
3475 if (stcb->asoc.peer_supports_auth == 0) {
3476 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3480 case SCTP_NOTIFY_ASSOC_DOWN:
3481 sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, so_locked);
3483 case SCTP_NOTIFY_INTERFACE_DOWN:
3485 struct sctp_nets *net;
3487 net = (struct sctp_nets *)data;
3488 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3489 (struct sockaddr *)&net->ro._l_addr, error);
3492 case SCTP_NOTIFY_INTERFACE_UP:
3494 struct sctp_nets *net;
3496 net = (struct sctp_nets *)data;
3497 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3498 (struct sockaddr *)&net->ro._l_addr, error);
3501 case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3503 struct sctp_nets *net;
3505 net = (struct sctp_nets *)data;
3506 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3507 (struct sockaddr *)&net->ro._l_addr, error);
3510 case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3511 sctp_notify_send_failed2(stcb, error,
3512 (struct sctp_stream_queue_pending *)data, so_locked);
3514 case SCTP_NOTIFY_DG_FAIL:
3515 sctp_notify_send_failed(stcb, error,
3516 (struct sctp_tmit_chunk *)data, so_locked);
3518 case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
3522 val = *((uint32_t *) data);
3524 sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3527 case SCTP_NOTIFY_STRDATA_ERR:
3529 case SCTP_NOTIFY_ASSOC_ABORTED:
3530 if ((stcb) && (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3531 ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED))) {
3532 sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, NULL, so_locked);
3534 sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, NULL, so_locked);
3537 case SCTP_NOTIFY_PEER_OPENED_STREAM:
3539 case SCTP_NOTIFY_STREAM_OPENED_OK:
3541 case SCTP_NOTIFY_ASSOC_RESTART:
3542 sctp_notify_assoc_change(SCTP_RESTART, stcb, error, data, so_locked);
3543 if (stcb->asoc.peer_supports_auth == 0) {
3544 sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3548 case SCTP_NOTIFY_HB_RESP:
3550 case SCTP_NOTIFY_STR_RESET_INSTREAM_ADD_OK:
3551 sctp_notify_stream_reset_add(stcb, error, SCTP_STRRESET_INBOUND_STR);
3553 case SCTP_NOTIFY_STR_RESET_ADD_OK:
3554 sctp_notify_stream_reset_add(stcb, error, SCTP_STRRESET_OUTBOUND_STR);
3556 case SCTP_NOTIFY_STR_RESET_ADD_FAIL:
3557 sctp_notify_stream_reset_add(stcb, error, (SCTP_STRRESET_FAILED | SCTP_STRRESET_OUTBOUND_STR));
3560 case SCTP_NOTIFY_STR_RESET_SEND:
3561 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_OUTBOUND_STR);
3563 case SCTP_NOTIFY_STR_RESET_RECV:
3564 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STRRESET_INBOUND_STR);
3566 case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3567 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_OUTBOUND_STR | SCTP_STRRESET_FAILED));
3569 case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3570 sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), (SCTP_STRRESET_INBOUND_STR | SCTP_STRRESET_FAILED));
3572 case SCTP_NOTIFY_ASCONF_ADD_IP:
3573 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3576 case SCTP_NOTIFY_ASCONF_DELETE_IP:
3577 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3580 case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3581 sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3584 case SCTP_NOTIFY_ASCONF_SUCCESS:
3586 case SCTP_NOTIFY_ASCONF_FAILED:
3588 case SCTP_NOTIFY_PEER_SHUTDOWN:
3589 sctp_notify_shutdown_event(stcb);
3591 case SCTP_NOTIFY_AUTH_NEW_KEY:
3592 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY, error,
3593 (uint16_t) (uintptr_t) data,
3596 case SCTP_NOTIFY_AUTH_FREE_KEY:
3597 sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3598 (uint16_t) (uintptr_t) data,
3601 case SCTP_NOTIFY_NO_PEER_AUTH:
3602 sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3603 (uint16_t) (uintptr_t) data,
3606 case SCTP_NOTIFY_SENDER_DRY:
3607 sctp_notify_sender_dry_event(stcb, so_locked);
3610 SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3611 __FUNCTION__, notification, notification);
3617 sctp_report_all_outbound(struct sctp_tcb *stcb, int holds_lock, int so_locked
3618 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3623 struct sctp_association *asoc;
3624 struct sctp_stream_out *outs;
3625 struct sctp_tmit_chunk *chk, *nchk;
3626 struct sctp_stream_queue_pending *sp, *nsp;
3633 if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3634 /* already being freed */
3637 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3638 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3639 (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
3642 /* now through all the gunk freeing chunks */
3643 if (holds_lock == 0) {
3644 SCTP_TCB_SEND_LOCK(stcb);
3646 /* sent queue SHOULD be empty */
3647 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3648 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3649 asoc->sent_queue_cnt--;
3650 if (chk->data != NULL) {
3651 sctp_free_bufspace(stcb, asoc, chk, 1);
3652 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3653 SCTP_NOTIFY_DATAGRAM_SENT, chk, so_locked);
3655 sctp_m_freem(chk->data);
3659 sctp_free_a_chunk(stcb, chk, so_locked);
3660 /* sa_ignore FREED_MEMORY */
3662 /* pending send queue SHOULD be empty */
3663 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3664 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3665 asoc->send_queue_cnt--;
3666 if (chk->data != NULL) {
3667 sctp_free_bufspace(stcb, asoc, chk, 1);
3668 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3669 SCTP_NOTIFY_DATAGRAM_UNSENT, chk, so_locked);
3671 sctp_m_freem(chk->data);
3675 sctp_free_a_chunk(stcb, chk, so_locked);
3676 /* sa_ignore FREED_MEMORY */
3678 for (i = 0; i < asoc->streamoutcnt; i++) {
3679 /* For each stream */
3680 outs = &asoc->strmout[i];
3681 /* clean up any sends there */
3682 asoc->locked_on_sending = NULL;
3683 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
3684 asoc->stream_queue_cnt--;
3685 TAILQ_REMOVE(&outs->outqueue, sp, next);
3686 sctp_free_spbufspace(stcb, asoc, sp);
3688 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
3689 SCTP_NOTIFY_DATAGRAM_UNSENT, (void *)sp, so_locked);
3691 sctp_m_freem(sp->data);
3696 sctp_free_remote_addr(sp->net);
3699 /* Free the chunk */
3700 sctp_free_a_strmoq(stcb, sp, so_locked);
3701 /* sa_ignore FREED_MEMORY */
3705 if (holds_lock == 0) {
3706 SCTP_TCB_SEND_UNLOCK(stcb);
3711 sctp_abort_notification(struct sctp_tcb *stcb, int error, int so_locked
3712 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3721 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3722 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3723 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3726 /* Tell them we lost the asoc */
3727 sctp_report_all_outbound(stcb, 1, so_locked);
3728 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3729 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3730 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3731 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3733 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_ABORTED, stcb, error, NULL, so_locked);
3737 sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3738 struct mbuf *m, int iphlen, struct sctphdr *sh, struct mbuf *op_err,
3739 uint32_t vrf_id, uint16_t port)
3743 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3750 /* We have a TCB to abort, send notification too */
3751 vtag = stcb->asoc.peer_vtag;
3752 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
3753 /* get the assoc vrf id and table id */
3754 vrf_id = stcb->asoc.vrf_id;
3755 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3757 sctp_send_abort(m, iphlen, sh, vtag, op_err, vrf_id, port);
3759 /* Ok, now lets free it */
3760 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3761 so = SCTP_INP_SO(inp);
3762 atomic_add_int(&stcb->asoc.refcnt, 1);
3763 SCTP_TCB_UNLOCK(stcb);
3764 SCTP_SOCKET_LOCK(so, 1);
3765 SCTP_TCB_LOCK(stcb);
3766 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3768 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3769 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3770 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3771 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3773 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
3774 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3775 SCTP_SOCKET_UNLOCK(so, 1);
3780 #ifdef SCTP_ASOCLOG_OF_TSNS
3782 sctp_print_out_track_log(struct sctp_tcb *stcb)
3787 SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
3788 SCTP_PRINTF("IN bound TSN log-aaa\n");
3789 if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
3790 SCTP_PRINTF("None rcvd\n");
3793 if (stcb->asoc.tsn_in_wrapped) {
3794 for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
3795 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3796 stcb->asoc.in_tsnlog[i].tsn,
3797 stcb->asoc.in_tsnlog[i].strm,
3798 stcb->asoc.in_tsnlog[i].seq,
3799 stcb->asoc.in_tsnlog[i].flgs,
3800 stcb->asoc.in_tsnlog[i].sz);
3803 if (stcb->asoc.tsn_in_at) {
3804 for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
3805 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3806 stcb->asoc.in_tsnlog[i].tsn,
3807 stcb->asoc.in_tsnlog[i].strm,
3808 stcb->asoc.in_tsnlog[i].seq,
3809 stcb->asoc.in_tsnlog[i].flgs,
3810 stcb->asoc.in_tsnlog[i].sz);
3814 SCTP_PRINTF("OUT bound TSN log-aaa\n");
3815 if ((stcb->asoc.tsn_out_at == 0) &&
3816 (stcb->asoc.tsn_out_wrapped == 0)) {
3817 SCTP_PRINTF("None sent\n");
3819 if (stcb->asoc.tsn_out_wrapped) {
3820 for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
3821 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3822 stcb->asoc.out_tsnlog[i].tsn,
3823 stcb->asoc.out_tsnlog[i].strm,
3824 stcb->asoc.out_tsnlog[i].seq,
3825 stcb->asoc.out_tsnlog[i].flgs,
3826 stcb->asoc.out_tsnlog[i].sz);
3829 if (stcb->asoc.tsn_out_at) {
3830 for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
3831 SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3832 stcb->asoc.out_tsnlog[i].tsn,
3833 stcb->asoc.out_tsnlog[i].strm,
3834 stcb->asoc.out_tsnlog[i].seq,
3835 stcb->asoc.out_tsnlog[i].flgs,
3836 stcb->asoc.out_tsnlog[i].sz);
3845 sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3846 int error, struct mbuf *op_err,
3848 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3853 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3858 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3859 so = SCTP_INP_SO(inp);
3862 /* Got to have a TCB */
3863 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3864 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3865 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3866 SCTP_CALLED_DIRECTLY_NOCMPSET);
3871 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3873 /* notify the ulp */
3874 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0)
3875 sctp_abort_notification(stcb, error, so_locked);
3876 /* notify the peer */
3877 #if defined(SCTP_PANIC_ON_ABORT)
3878 panic("aborting an association");
3880 sctp_send_abort_tcb(stcb, op_err, so_locked);
3881 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3882 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3883 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3884 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3886 /* now free the asoc */
3887 #ifdef SCTP_ASOCLOG_OF_TSNS
3888 sctp_print_out_track_log(stcb);
3890 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3892 atomic_add_int(&stcb->asoc.refcnt, 1);
3893 SCTP_TCB_UNLOCK(stcb);
3894 SCTP_SOCKET_LOCK(so, 1);
3895 SCTP_TCB_LOCK(stcb);
3896 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3899 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
3900 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3902 SCTP_SOCKET_UNLOCK(so, 1);
3908 sctp_handle_ootb(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
3909 struct sctp_inpcb *inp, struct mbuf *op_err, uint32_t vrf_id, uint16_t port)
3911 struct sctp_chunkhdr *ch, chunk_buf;
3912 unsigned int chk_length;
3914 SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
3915 /* Generate a TO address for future reference */
3916 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3917 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3918 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3919 SCTP_CALLED_DIRECTLY_NOCMPSET);
3922 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3923 sizeof(*ch), (uint8_t *) & chunk_buf);
3924 while (ch != NULL) {
3925 chk_length = ntohs(ch->chunk_length);
3926 if (chk_length < sizeof(*ch)) {
3927 /* break to abort land */
3930 switch (ch->chunk_type) {
3931 case SCTP_COOKIE_ECHO:
3932 /* We hit here only if the assoc is being freed */
3934 case SCTP_PACKET_DROPPED:
3935 /* we don't respond to pkt-dropped */
3937 case SCTP_ABORT_ASSOCIATION:
3938 /* we don't respond with an ABORT to an ABORT */
3940 case SCTP_SHUTDOWN_COMPLETE:
3942 * we ignore it since we are not waiting for it and
3946 case SCTP_SHUTDOWN_ACK:
3947 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
3952 offset += SCTP_SIZE32(chk_length);
3953 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3954 sizeof(*ch), (uint8_t *) & chunk_buf);
3956 sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
3960 * check the inbound datagram to make sure there is not an abort inside it,
3961 * if there is return 1, else return 0.
3964 sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
3966 struct sctp_chunkhdr *ch;
3967 struct sctp_init_chunk *init_chk, chunk_buf;
3969 unsigned int chk_length;
3971 offset = iphlen + sizeof(struct sctphdr);
3972 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
3973 (uint8_t *) & chunk_buf);
3974 while (ch != NULL) {
3975 chk_length = ntohs(ch->chunk_length);
3976 if (chk_length < sizeof(*ch)) {
3977 /* packet is probably corrupt */
3980 /* we seem to be ok, is it an abort? */
3981 if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
3982 /* yep, tell them */
3985 if (ch->chunk_type == SCTP_INITIATION) {
3986 /* need to update the Vtag */
3987 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
3988 offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
3989 if (init_chk != NULL) {
3990 *vtagfill = ntohl(init_chk->init.initiate_tag);
3993 /* Nope, move to the next chunk */
3994 offset += SCTP_SIZE32(chk_length);
3995 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
3996 sizeof(*ch), (uint8_t *) & chunk_buf);
4002 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4003 * set (i.e. it's 0) so, create this function to compare link local scopes
4007 sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4009 struct sockaddr_in6 a, b;
4015 if (a.sin6_scope_id == 0)
4016 if (sa6_recoverscope(&a)) {
4017 /* can't get scope, so can't match */
4020 if (b.sin6_scope_id == 0)
4021 if (sa6_recoverscope(&b)) {
4022 /* can't get scope, so can't match */
4025 if (a.sin6_scope_id != b.sin6_scope_id)
4032 * returns a sockaddr_in6 with embedded scope recovered and removed
4034 struct sockaddr_in6 *
4035 sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4037 /* check and strip embedded scope junk */
4038 if (addr->sin6_family == AF_INET6) {
4039 if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4040 if (addr->sin6_scope_id == 0) {
4042 if (!sa6_recoverscope(store)) {
4043 /* use the recovered scope */
4047 /* else, return the original "to" addr */
4048 in6_clearscope(&addr->sin6_addr);
4058 * are the two addresses the same? currently a "scopeless" check returns: 1
4062 sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4066 if (sa1 == NULL || sa2 == NULL)
4069 /* must be the same family */
4070 if (sa1->sa_family != sa2->sa_family)
4073 switch (sa1->sa_family) {
4077 /* IPv6 addresses */
4078 struct sockaddr_in6 *sin6_1, *sin6_2;
4080 sin6_1 = (struct sockaddr_in6 *)sa1;
4081 sin6_2 = (struct sockaddr_in6 *)sa2;
4082 return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4089 /* IPv4 addresses */
4090 struct sockaddr_in *sin_1, *sin_2;
4092 sin_1 = (struct sockaddr_in *)sa1;
4093 sin_2 = (struct sockaddr_in *)sa2;
4094 return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
4098 /* we don't do these... */
4104 sctp_print_address(struct sockaddr *sa)
4107 char ip6buf[INET6_ADDRSTRLEN];
4112 switch (sa->sa_family) {
4116 struct sockaddr_in6 *sin6;
4118 sin6 = (struct sockaddr_in6 *)sa;
4119 SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
4120 ip6_sprintf(ip6buf, &sin6->sin6_addr),
4121 ntohs(sin6->sin6_port),
4122 sin6->sin6_scope_id);
4129 struct sockaddr_in *sin;
4132 sin = (struct sockaddr_in *)sa;
4133 p = (unsigned char *)&sin->sin_addr;
4134 SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4135 p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
4146 sctp_print_address_pkt(struct ip *iph, struct sctphdr *sh)
4148 switch (iph->ip_v) {
4152 struct sockaddr_in lsa, fsa;
4154 bzero(&lsa, sizeof(lsa));
4155 lsa.sin_len = sizeof(lsa);
4156 lsa.sin_family = AF_INET;
4157 lsa.sin_addr = iph->ip_src;
4158 lsa.sin_port = sh->src_port;
4159 bzero(&fsa, sizeof(fsa));
4160 fsa.sin_len = sizeof(fsa);
4161 fsa.sin_family = AF_INET;
4162 fsa.sin_addr = iph->ip_dst;
4163 fsa.sin_port = sh->dest_port;
4164 SCTP_PRINTF("src: ");
4165 sctp_print_address((struct sockaddr *)&lsa);
4166 SCTP_PRINTF("dest: ");
4167 sctp_print_address((struct sockaddr *)&fsa);
4172 case IPV6_VERSION >> 4:
4174 struct ip6_hdr *ip6;
4175 struct sockaddr_in6 lsa6, fsa6;
4177 ip6 = (struct ip6_hdr *)iph;
4178 bzero(&lsa6, sizeof(lsa6));
4179 lsa6.sin6_len = sizeof(lsa6);
4180 lsa6.sin6_family = AF_INET6;
4181 lsa6.sin6_addr = ip6->ip6_src;
4182 lsa6.sin6_port = sh->src_port;
4183 bzero(&fsa6, sizeof(fsa6));
4184 fsa6.sin6_len = sizeof(fsa6);
4185 fsa6.sin6_family = AF_INET6;
4186 fsa6.sin6_addr = ip6->ip6_dst;
4187 fsa6.sin6_port = sh->dest_port;
4188 SCTP_PRINTF("src: ");
4189 sctp_print_address((struct sockaddr *)&lsa6);
4190 SCTP_PRINTF("dest: ");
4191 sctp_print_address((struct sockaddr *)&fsa6);
4202 sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4203 struct sctp_inpcb *new_inp,
4204 struct sctp_tcb *stcb,
4208 * go through our old INP and pull off any control structures that
4209 * belong to stcb and move then to the new inp.
4211 struct socket *old_so, *new_so;
4212 struct sctp_queued_to_read *control, *nctl;
4213 struct sctp_readhead tmp_queue;
4217 old_so = old_inp->sctp_socket;
4218 new_so = new_inp->sctp_socket;
4219 TAILQ_INIT(&tmp_queue);
4220 error = sblock(&old_so->so_rcv, waitflags);
4223 * Gak, can't get sblock, we have a problem. data will be
4224 * left stranded.. and we don't dare look at it since the
4225 * other thread may be reading something. Oh well, its a
4226 * screwed up app that does a peeloff OR a accept while
4227 * reading from the main socket... actually its only the
4228 * peeloff() case, since I think read will fail on a
4229 * listening socket..
4233 /* lock the socket buffers */
4234 SCTP_INP_READ_LOCK(old_inp);
4235 TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4236 /* Pull off all for out target stcb */
4237 if (control->stcb == stcb) {
4238 /* remove it we want it */
4239 TAILQ_REMOVE(&old_inp->read_queue, control, next);
4240 TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4243 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4244 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
4246 sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4247 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4248 sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4250 m = SCTP_BUF_NEXT(m);
4254 SCTP_INP_READ_UNLOCK(old_inp);
4255 /* Remove the sb-lock on the old socket */
4257 sbunlock(&old_so->so_rcv);
4258 /* Now we move them over to the new socket buffer */
4259 SCTP_INP_READ_LOCK(new_inp);
4260 TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4261 TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4264 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4265 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4267 sctp_sballoc(stcb, &new_so->so_rcv, m);
4268 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4269 sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4271 m = SCTP_BUF_NEXT(m);
4274 SCTP_INP_READ_UNLOCK(new_inp);
4278 sctp_add_to_readq(struct sctp_inpcb *inp,
4279 struct sctp_tcb *stcb,
4280 struct sctp_queued_to_read *control,
4283 int inp_read_lock_held,
4285 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4291 * Here we must place the control on the end of the socket read
4292 * queue AND increment sb_cc so that select will work properly on
4295 struct mbuf *m, *prev = NULL;
4300 panic("Gak, inp NULL on add_to_readq");
4304 if (inp_read_lock_held == 0)
4305 SCTP_INP_READ_LOCK(inp);
4306 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4307 sctp_free_remote_addr(control->whoFrom);
4308 if (control->data) {
4309 sctp_m_freem(control->data);
4310 control->data = NULL;
4312 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4313 if (inp_read_lock_held == 0)
4314 SCTP_INP_READ_UNLOCK(inp);
4317 if (!(control->spec_flags & M_NOTIFICATION)) {
4318 atomic_add_int(&inp->total_recvs, 1);
4319 if (!control->do_not_ref_stcb) {
4320 atomic_add_int(&stcb->total_recvs, 1);
4324 control->held_length = 0;
4325 control->length = 0;
4327 if (SCTP_BUF_LEN(m) == 0) {
4328 /* Skip mbufs with NO length */
4331 control->data = sctp_m_free(m);
4334 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4335 m = SCTP_BUF_NEXT(prev);
4338 control->tail_mbuf = prev;
4343 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4344 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
4346 sctp_sballoc(stcb, sb, m);
4347 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4348 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4350 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4351 m = SCTP_BUF_NEXT(m);
4354 control->tail_mbuf = prev;
4356 /* Everything got collapsed out?? */
4357 sctp_free_remote_addr(control->whoFrom);
4358 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4359 if (inp_read_lock_held == 0)
4360 SCTP_INP_READ_UNLOCK(inp);
4364 control->end_added = 1;
4366 TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4367 if (inp_read_lock_held == 0)
4368 SCTP_INP_READ_UNLOCK(inp);
4369 if (inp && inp->sctp_socket) {
4370 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
4371 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4373 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4376 so = SCTP_INP_SO(inp);
4378 atomic_add_int(&stcb->asoc.refcnt, 1);
4379 SCTP_TCB_UNLOCK(stcb);
4380 SCTP_SOCKET_LOCK(so, 1);
4381 SCTP_TCB_LOCK(stcb);
4382 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4383 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4384 SCTP_SOCKET_UNLOCK(so, 1);
4389 sctp_sorwakeup(inp, inp->sctp_socket);
4390 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4392 SCTP_SOCKET_UNLOCK(so, 1);
4401 sctp_append_to_readq(struct sctp_inpcb *inp,
4402 struct sctp_tcb *stcb,
4403 struct sctp_queued_to_read *control,
4410 * A partial delivery API event is underway. OR we are appending on
4411 * the reassembly queue.
4413 * If PDAPI this means we need to add m to the end of the data.
4414 * Increase the length in the control AND increment the sb_cc.
4415 * Otherwise sb is NULL and all we need to do is put it at the end
4416 * of the mbuf chain.
4419 struct mbuf *mm, *tail = NULL, *prev = NULL;
4422 SCTP_INP_READ_LOCK(inp);
4424 if (control == NULL) {
4427 SCTP_INP_READ_UNLOCK(inp);
4431 if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) {
4432 SCTP_INP_READ_UNLOCK(inp);
4435 if (control->end_added) {
4436 /* huh this one is complete? */
4444 if (SCTP_BUF_LEN(mm) == 0) {
4445 /* Skip mbufs with NO lenght */
4448 m = sctp_m_free(mm);
4451 SCTP_BUF_NEXT(prev) = sctp_m_free(mm);
4452 mm = SCTP_BUF_NEXT(prev);
4457 len += SCTP_BUF_LEN(mm);
4459 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4460 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm));
4462 sctp_sballoc(stcb, sb, mm);
4463 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4464 sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
4467 mm = SCTP_BUF_NEXT(mm);
4472 /* Really there should always be a prev */
4474 /* Huh nothing left? */
4476 panic("Nothing left to add?");
4483 if (control->tail_mbuf) {
4485 SCTP_BUF_NEXT(control->tail_mbuf) = m;
4486 control->tail_mbuf = tail;
4490 if (control->data != NULL) {
4491 panic("This should NOT happen");
4495 control->tail_mbuf = tail;
4497 atomic_add_int(&control->length, len);
4499 /* message is complete */
4500 if (stcb && (control == stcb->asoc.control_pdapi)) {
4501 stcb->asoc.control_pdapi = NULL;
4503 control->held_length = 0;
4504 control->end_added = 1;
4507 control->do_not_ref_stcb = 1;
4510 * When we are appending in partial delivery, the cum-ack is used
4511 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
4512 * is populated in the outbound sinfo structure from the true cumack
4513 * if the association exists...
4515 control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
4517 SCTP_INP_READ_UNLOCK(inp);
4519 if (inp && inp->sctp_socket) {
4520 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
4521 SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4523 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4526 so = SCTP_INP_SO(inp);
4527 atomic_add_int(&stcb->asoc.refcnt, 1);
4528 SCTP_TCB_UNLOCK(stcb);
4529 SCTP_SOCKET_LOCK(so, 1);
4530 SCTP_TCB_LOCK(stcb);
4531 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4532 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4533 SCTP_SOCKET_UNLOCK(so, 1);
4537 sctp_sorwakeup(inp, inp->sctp_socket);
4538 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4539 SCTP_SOCKET_UNLOCK(so, 1);
4548 /*************HOLD THIS COMMENT FOR PATCH FILE OF
4549 *************ALTERNATE ROUTING CODE
4552 /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4553 *************ALTERNATE ROUTING CODE
4557 sctp_generate_invmanparam(int err)
4559 /* Return a MBUF with a invalid mandatory parameter */
4562 m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
4564 struct sctp_paramhdr *ph;
4566 SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
4567 ph = mtod(m, struct sctp_paramhdr *);
4568 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4569 ph->param_type = htons(err);
4574 #ifdef SCTP_MBCNT_LOGGING
4576 sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4577 struct sctp_tmit_chunk *tp1, int chk_cnt)
4579 if (tp1->data == NULL) {
4582 asoc->chunks_on_out_queue -= chk_cnt;
4583 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4584 sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4585 asoc->total_output_queue_size,
4590 if (asoc->total_output_queue_size >= tp1->book_size) {
4591 atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4593 asoc->total_output_queue_size = 0;
4596 if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4597 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4598 if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4599 stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4601 stcb->sctp_socket->so_snd.sb_cc = 0;
4610 sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4611 int reason, int so_locked
4612 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4617 struct sctp_stream_out *strq;
4618 struct sctp_tmit_chunk *chk = NULL, *tp2;
4619 struct sctp_stream_queue_pending *sp;
4620 uint16_t stream = 0, seq = 0;
4621 uint8_t foundeom = 0;
4624 int do_wakeup_routine = 0;
4626 stream = tp1->rec.data.stream_number;
4627 seq = tp1->rec.data.stream_seq;
4629 ret_sz += tp1->book_size;
4630 if (tp1->data != NULL) {
4631 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4632 sctp_flight_size_decrease(tp1);
4633 sctp_total_flight_decrease(stcb, tp1);
4635 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4636 stcb->asoc.peers_rwnd += tp1->send_size;
4637 stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
4638 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, reason, tp1, so_locked);
4640 sctp_m_freem(tp1->data);
4643 do_wakeup_routine = 1;
4644 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4645 stcb->asoc.sent_queue_cnt_removeable--;
4648 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4649 if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4650 SCTP_DATA_NOT_FRAG) {
4651 /* not frag'ed we ae done */
4654 } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4655 /* end of frag, we are done */
4660 * Its a begin or middle piece, we must mark all of
4664 tp1 = TAILQ_NEXT(tp1, sctp_next);
4666 } while (tp1 && notdone);
4667 if (foundeom == 0) {
4669 * The multi-part message was scattered across the send and
4672 TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
4673 if ((tp1->rec.data.stream_number != stream) ||
4674 (tp1->rec.data.stream_seq != seq)) {
4678 * save to chk in case we have some on stream out
4679 * queue. If so and we have an un-transmitted one we
4680 * don't have to fudge the TSN.
4683 ret_sz += tp1->book_size;
4684 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
4685 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb, reason, tp1, so_locked);
4687 sctp_m_freem(tp1->data);
4690 /* No flight involved here book the size to 0 */
4692 if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4695 do_wakeup_routine = 1;
4696 tp1->sent = SCTP_FORWARD_TSN_SKIP;
4697 TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
4699 * on to the sent queue so we can wait for it to be
4702 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
4704 stcb->asoc.send_queue_cnt--;
4705 stcb->asoc.sent_queue_cnt++;
4708 if (foundeom == 0) {
4710 * Still no eom found. That means there is stuff left on the
4711 * stream out queue.. yuck.
4713 strq = &stcb->asoc.strmout[stream];
4714 SCTP_TCB_SEND_LOCK(stcb);
4715 TAILQ_FOREACH(sp, &strq->outqueue, next) {
4716 /* FIXME: Shouldn't this be a serial number check? */
4717 if (sp->strseq > seq) {
4720 /* Check if its our SEQ */
4721 if (sp->strseq == seq) {
4722 sp->discard_rest = 1;
4724 * We may need to put a chunk on the queue
4725 * that holds the TSN that would have been
4726 * sent with the LAST bit.
4729 /* Yep, we have to */
4730 sctp_alloc_a_chunk(stcb, chk);
4733 * we are hosed. All we can
4734 * do is nothing.. which
4735 * will cause an abort if
4736 * the peer is paying
4741 memset(chk, 0, sizeof(*chk));
4742 chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG;
4743 chk->sent = SCTP_FORWARD_TSN_SKIP;
4744 chk->asoc = &stcb->asoc;
4745 chk->rec.data.stream_seq = sp->strseq;
4746 chk->rec.data.stream_number = sp->stream;
4747 chk->rec.data.payloadtype = sp->ppid;
4748 chk->rec.data.context = sp->context;
4749 chk->flags = sp->act_flags;
4751 chk->whoTo = sp->net;
4753 chk->whoTo = stcb->asoc.primary_destination;
4754 atomic_add_int(&chk->whoTo->ref_count, 1);
4755 chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
4756 stcb->asoc.pr_sctp_cnt++;
4757 chk->pr_sctp_on = 1;
4758 TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
4759 stcb->asoc.sent_queue_cnt++;
4760 stcb->asoc.pr_sctp_cnt++;
4762 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4767 * Pull any data to free up the SB
4768 * and allow sender to "add more"
4769 * whilc we will throw away :-)
4771 sctp_free_spbufspace(stcb, &stcb->asoc,
4773 ret_sz += sp->length;
4774 do_wakeup_routine = 1;
4776 sctp_m_freem(sp->data);
4779 sp->tail_mbuf = NULL;
4783 } /* End tailq_foreach */
4784 SCTP_TCB_SEND_UNLOCK(stcb);
4786 if (do_wakeup_routine) {
4787 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4790 so = SCTP_INP_SO(stcb->sctp_ep);
4792 atomic_add_int(&stcb->asoc.refcnt, 1);
4793 SCTP_TCB_UNLOCK(stcb);
4794 SCTP_SOCKET_LOCK(so, 1);
4795 SCTP_TCB_LOCK(stcb);
4796 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4797 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4798 /* assoc was freed while we were unlocked */
4799 SCTP_SOCKET_UNLOCK(so, 1);
4804 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
4805 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4807 SCTP_SOCKET_UNLOCK(so, 1);
4815 * checks to see if the given address, sa, is one that is currently known by
4816 * the kernel note: can't distinguish the same address on multiple interfaces
4817 * and doesn't handle multiple addresses with different zone/scope id's note:
4818 * ifa_ifwithaddr() compares the entire sockaddr struct
4821 sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
4824 struct sctp_laddr *laddr;
4826 if (holds_lock == 0) {
4827 SCTP_INP_RLOCK(inp);
4829 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4830 if (laddr->ifa == NULL)
4832 if (addr->sa_family != laddr->ifa->address.sa.sa_family)
4835 if (addr->sa_family == AF_INET) {
4836 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4837 laddr->ifa->address.sin.sin_addr.s_addr) {
4839 if (holds_lock == 0) {
4840 SCTP_INP_RUNLOCK(inp);
4842 return (laddr->ifa);
4848 if (addr->sa_family == AF_INET6) {
4849 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4850 &laddr->ifa->address.sin6)) {
4852 if (holds_lock == 0) {
4853 SCTP_INP_RUNLOCK(inp);
4855 return (laddr->ifa);
4861 if (holds_lock == 0) {
4862 SCTP_INP_RUNLOCK(inp);
4868 sctp_get_ifa_hash_val(struct sockaddr *addr)
4870 switch (addr->sa_family) {
4874 struct sockaddr_in *sin;
4876 sin = (struct sockaddr_in *)addr;
4877 return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
4883 struct sockaddr_in6 *sin6;
4884 uint32_t hash_of_addr;
4886 sin6 = (struct sockaddr_in6 *)addr;
4887 hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
4888 sin6->sin6_addr.s6_addr32[1] +
4889 sin6->sin6_addr.s6_addr32[2] +
4890 sin6->sin6_addr.s6_addr32[3]);
4891 hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
4892 return (hash_of_addr);
4902 sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
4904 struct sctp_ifa *sctp_ifap;
4905 struct sctp_vrf *vrf;
4906 struct sctp_ifalist *hash_head;
4907 uint32_t hash_of_addr;
4909 if (holds_lock == 0)
4910 SCTP_IPI_ADDR_RLOCK();
4912 vrf = sctp_find_vrf(vrf_id);
4915 if (holds_lock == 0)
4916 SCTP_IPI_ADDR_RUNLOCK();
4919 hash_of_addr = sctp_get_ifa_hash_val(addr);
4921 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
4922 if (hash_head == NULL) {
4923 SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
4924 hash_of_addr, (uint32_t) vrf->vrf_addr_hashmark,
4925 (uint32_t) (hash_of_addr & vrf->vrf_addr_hashmark));
4926 sctp_print_address(addr);
4927 SCTP_PRINTF("No such bucket for address\n");
4928 if (holds_lock == 0)
4929 SCTP_IPI_ADDR_RUNLOCK();
4933 LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
4934 if (sctp_ifap == NULL) {
4936 panic("Huh LIST_FOREACH corrupt");
4939 SCTP_PRINTF("LIST corrupt of sctp_ifap's?\n");
4943 if (addr->sa_family != sctp_ifap->address.sa.sa_family)
4946 if (addr->sa_family == AF_INET) {
4947 if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
4948 sctp_ifap->address.sin.sin_addr.s_addr) {
4950 if (holds_lock == 0)
4951 SCTP_IPI_ADDR_RUNLOCK();
4958 if (addr->sa_family == AF_INET6) {
4959 if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4960 &sctp_ifap->address.sin6)) {
4962 if (holds_lock == 0)
4963 SCTP_IPI_ADDR_RUNLOCK();
4970 if (holds_lock == 0)
4971 SCTP_IPI_ADDR_RUNLOCK();
4976 sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t * freed_so_far, int hold_rlock,
4979 /* User pulled some data, do we need a rwnd update? */
4982 struct socket *so = NULL;
4987 atomic_add_int(&stcb->asoc.refcnt, 1);
4989 if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
4990 SCTP_STATE_SHUTDOWN_RECEIVED |
4991 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4992 /* Pre-check If we are freeing no update */
4995 SCTP_INP_INCR_REF(stcb->sctp_ep);
4996 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4997 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5000 so = stcb->sctp_socket;
5004 atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5005 /* Have you have freed enough to look */
5007 /* Yep, its worth a look and the lock overhead */
5009 /* Figure out what the rwnd would be */
5010 rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5011 if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5012 dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5016 if (dif >= rwnd_req) {
5018 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5021 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5023 * One last check before we allow the guy possibly
5024 * to get in. There is a race, where the guy has not
5025 * reached the gate. In that case
5029 SCTP_TCB_LOCK(stcb);
5030 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5031 /* No reports here */
5032 SCTP_TCB_UNLOCK(stcb);
5035 SCTP_STAT_INCR(sctps_wu_sacks_sent);
5036 sctp_send_sack(stcb, SCTP_SO_LOCKED);
5038 sctp_chunk_output(stcb->sctp_ep, stcb,
5039 SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5040 /* make sure no timer is running */
5041 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5042 SCTP_TCB_UNLOCK(stcb);
5044 /* Update how much we have pending */
5045 stcb->freed_by_sorcv_sincelast = dif;
5048 if (so && r_unlocked && hold_rlock) {
5049 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5051 SCTP_INP_DECR_REF(stcb->sctp_ep);
5053 atomic_add_int(&stcb->asoc.refcnt, -1);
5058 sctp_sorecvmsg(struct socket *so,
5061 struct sockaddr *from,
5064 struct sctp_sndrcvinfo *sinfo,
5068 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5069 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5070 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5071 * On the way out we may send out any combination of:
5072 * MSG_NOTIFICATION MSG_EOR
5075 struct sctp_inpcb *inp = NULL;
5077 int cp_len = 0, error = 0;
5078 struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
5079 struct mbuf *m = NULL;
5080 struct sctp_tcb *stcb = NULL;
5081 int wakeup_read_socket = 0;
5082 int freecnt_applied = 0;
5083 int out_flags = 0, in_flags = 0;
5084 int block_allowed = 1;
5085 uint32_t freed_so_far = 0;
5086 uint32_t copied_so_far = 0;
5087 int in_eeor_mode = 0;
5088 int no_rcv_needed = 0;
5089 uint32_t rwnd_req = 0;
5090 int hold_sblock = 0;
5093 uint32_t held_length = 0;
5094 int sockbuf_lock = 0;
5097 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5101 in_flags = *msg_flags;
5102 if (in_flags & MSG_PEEK)
5103 SCTP_STAT_INCR(sctps_read_peeks);
5107 slen = uio->uio_resid;
5109 /* Pull in and set up our int flags */
5110 if (in_flags & MSG_OOB) {
5111 /* Out of band's NOT supported */
5112 return (EOPNOTSUPP);
5114 if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5115 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5118 if ((in_flags & (MSG_DONTWAIT
5121 SCTP_SO_IS_NBIO(so)) {
5124 /* setup the endpoint */
5125 inp = (struct sctp_inpcb *)so->so_pcb;
5127 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5130 rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5131 /* Must be at least a MTU's worth */
5132 if (rwnd_req < SCTP_MIN_RWND)
5133 rwnd_req = SCTP_MIN_RWND;
5134 in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5135 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5136 sctp_misc_ints(SCTP_SORECV_ENTER,
5137 rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
5139 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5140 sctp_misc_ints(SCTP_SORECV_ENTERPL,
5141 rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
5143 error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5146 goto release_unlocked;
5152 if (hold_sblock == 0) {
5153 SOCKBUF_LOCK(&so->so_rcv);
5156 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5157 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5160 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5162 error = so->so_error;
5163 if ((in_flags & MSG_PEEK) == 0)
5167 if (so->so_rcv.sb_cc == 0) {
5168 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5175 if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
5176 /* we need to wait for data */
5177 if ((so->so_rcv.sb_cc == 0) &&
5178 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5179 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5180 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5182 * For active open side clear flags for
5183 * re-use passive open is blocked by
5186 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5188 * You were aborted, passive side
5191 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5194 * You get this once if you are
5197 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5199 * Remove flag if on the
5202 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
5205 so->so_state &= ~(SS_ISCONNECTING |
5206 SS_ISDISCONNECTING |
5210 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5211 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5214 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
5220 error = sbwait(&so->so_rcv);
5225 goto restart_nosblocks;
5226 } else if (so->so_rcv.sb_cc == 0) {
5228 error = so->so_error;
5229 if ((in_flags & MSG_PEEK) == 0)
5232 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5233 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5234 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5236 * For active open side clear flags
5237 * for re-use passive open is
5238 * blocked by connect.
5240 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5242 * You were aborted, passive
5243 * side always hits here
5245 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5248 * You get this once if you
5249 * are active open side
5251 if (!(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5257 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_ABORTED;
5260 so->so_state &= ~(SS_ISCONNECTING |
5261 SS_ISDISCONNECTING |
5265 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5266 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5269 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAS_CONNECTED;
5275 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5276 error = EWOULDBLOCK;
5280 if (hold_sblock == 1) {
5281 SOCKBUF_UNLOCK(&so->so_rcv);
5284 /* we possibly have data we can read */
5285 /* sa_ignore FREED_MEMORY */
5286 control = TAILQ_FIRST(&inp->read_queue);
5287 if (control == NULL) {
5289 * This could be happening since the appender did the
5290 * increment but as not yet did the tailq insert onto the
5293 if (hold_rlock == 0) {
5294 SCTP_INP_READ_LOCK(inp);
5297 control = TAILQ_FIRST(&inp->read_queue);
5298 if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5300 panic("Huh, its non zero and nothing on control?");
5302 so->so_rcv.sb_cc = 0;
5304 SCTP_INP_READ_UNLOCK(inp);
5308 if ((control->length == 0) &&
5309 (control->do_not_ref_stcb)) {
5311 * Clean up code for freeing assoc that left behind a
5312 * pdapi.. maybe a peer in EEOR that just closed after
5313 * sending and never indicated a EOR.
5315 if (hold_rlock == 0) {
5317 SCTP_INP_READ_LOCK(inp);
5319 control->held_length = 0;
5320 if (control->data) {
5321 /* Hmm there is data here .. fix */
5325 m_tmp = control->data;
5327 cnt += SCTP_BUF_LEN(m_tmp);
5328 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5329 control->tail_mbuf = m_tmp;
5330 control->end_added = 1;
5332 m_tmp = SCTP_BUF_NEXT(m_tmp);
5334 control->length = cnt;
5337 TAILQ_REMOVE(&inp->read_queue, control, next);
5338 /* Add back any hiddend data */
5339 sctp_free_remote_addr(control->whoFrom);
5340 sctp_free_a_readq(stcb, control);
5344 SCTP_INP_READ_UNLOCK(inp);
5348 if ((control->length == 0) &&
5349 (control->end_added == 1)) {
5351 * Do we also need to check for (control->pdapi_aborted ==
5354 if (hold_rlock == 0) {
5356 SCTP_INP_READ_LOCK(inp);
5358 TAILQ_REMOVE(&inp->read_queue, control, next);
5359 if (control->data) {
5361 panic("control->data not null but control->length == 0");
5363 SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5364 sctp_m_freem(control->data);
5365 control->data = NULL;
5368 if (control->aux_data) {
5369 sctp_m_free(control->aux_data);
5370 control->aux_data = NULL;
5372 sctp_free_remote_addr(control->whoFrom);
5373 sctp_free_a_readq(stcb, control);
5376 SCTP_INP_READ_UNLOCK(inp);
5380 if (control->length == 0) {
5381 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5383 /* find a more suitable one then this */
5384 ctl = TAILQ_NEXT(control, next);
5386 if ((ctl->stcb != control->stcb) && (ctl->length) &&
5388 (ctl->spec_flags & M_NOTIFICATION) ||
5389 ((ctl->do_not_ref_stcb == 0) &&
5390 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
5393 * If we have a different TCB next, and there is data
5394 * present. If we have already taken some (pdapi), OR we can
5395 * ref the tcb and no delivery as started on this stream, we
5396 * take it. Note we allow a notification on a different
5397 * assoc to be delivered..
5401 } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
5403 ((ctl->some_taken) ||
5404 ((ctl->do_not_ref_stcb == 0) &&
5405 ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5406 (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
5408 * If we have the same tcb, and there is data present, and we
5409 * have the strm interleave feature present. Then if we have
5410 * taken some (pdapi) or we can refer to tht tcb AND we have
5411 * not started a delivery for this stream, we can take it.
5412 * Note we do NOT allow a notificaiton on the same assoc to
5418 ctl = TAILQ_NEXT(ctl, next);
5422 * if we reach here, not suitable replacement is available
5423 * <or> fragment interleave is NOT on. So stuff the sb_cc
5424 * into the our held count, and its time to sleep again.
5426 held_length = so->so_rcv.sb_cc;
5427 control->held_length = so->so_rcv.sb_cc;
5430 /* Clear the held length since there is something to read */
5431 control->held_length = 0;
5433 SCTP_INP_READ_UNLOCK(inp);
5438 * If we reach here, control has a some data for us to read off.
5439 * Note that stcb COULD be NULL.
5441 control->some_taken++;
5443 SOCKBUF_UNLOCK(&so->so_rcv);
5446 stcb = control->stcb;
5448 if ((control->do_not_ref_stcb == 0) &&
5449 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5450 if (freecnt_applied == 0)
5452 } else if (control->do_not_ref_stcb == 0) {
5453 /* you can't free it on me please */
5455 * The lock on the socket buffer protects us so the
5456 * free code will stop. But since we used the
5457 * socketbuf lock and the sender uses the tcb_lock
5458 * to increment, we need to use the atomic add to
5461 if (freecnt_applied) {
5463 panic("refcnt already incremented");
5465 printf("refcnt already incremented?\n");
5468 atomic_add_int(&stcb->asoc.refcnt, 1);
5469 freecnt_applied = 1;
5472 * Setup to remember how much we have not yet told
5473 * the peer our rwnd has opened up. Note we grab the
5474 * value from the tcb from last time. Note too that
5475 * sack sending clears this when a sack is sent,
5476 * which is fine. Once we hit the rwnd_req, we then
5477 * will go to the sctp_user_rcvd() that will not
5478 * lock until it KNOWs it MUST send a WUP-SACK.
5480 freed_so_far = stcb->freed_by_sorcv_sincelast;
5481 stcb->freed_by_sorcv_sincelast = 0;
5485 ((control->spec_flags & M_NOTIFICATION) == 0) &&
5486 control->do_not_ref_stcb == 0) {
5487 stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5489 /* First lets get off the sinfo and sockaddr info */
5490 if ((sinfo) && filling_sinfo) {
5491 memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
5492 nxt = TAILQ_NEXT(control, next);
5493 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
5494 struct sctp_extrcvinfo *s_extra;
5496 s_extra = (struct sctp_extrcvinfo *)sinfo;
5499 s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5500 if (nxt->sinfo_flags & SCTP_UNORDERED) {
5501 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5503 if (nxt->spec_flags & M_NOTIFICATION) {
5504 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5506 s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id;
5507 s_extra->sreinfo_next_length = nxt->length;
5508 s_extra->sreinfo_next_ppid = nxt->sinfo_ppid;
5509 s_extra->sreinfo_next_stream = nxt->sinfo_stream;
5510 if (nxt->tail_mbuf != NULL) {
5511 if (nxt->end_added) {
5512 s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5517 * we explicitly 0 this, since the memcpy
5518 * got some other things beyond the older
5519 * sinfo_ that is on the control's structure
5523 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
5524 s_extra->sreinfo_next_aid = 0;
5525 s_extra->sreinfo_next_length = 0;
5526 s_extra->sreinfo_next_ppid = 0;
5527 s_extra->sreinfo_next_stream = 0;
5531 * update off the real current cum-ack, if we have an stcb.
5533 if ((control->do_not_ref_stcb == 0) && stcb)
5534 sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5536 * mask off the high bits, we keep the actual chunk bits in
5539 sinfo->sinfo_flags &= 0x00ff;
5540 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
5541 sinfo->sinfo_flags |= SCTP_UNORDERED;
5544 #ifdef SCTP_ASOCLOG_OF_TSNS
5546 int index, newindex;
5547 struct sctp_pcbtsn_rlog *entry;
5550 index = inp->readlog_index;
5551 newindex = index + 1;
5552 if (newindex >= SCTP_READ_LOG_SIZE) {
5555 } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
5556 entry = &inp->readlog[index];
5557 entry->vtag = control->sinfo_assoc_id;
5558 entry->strm = control->sinfo_stream;
5559 entry->seq = control->sinfo_ssn;
5560 entry->sz = control->length;
5561 entry->flgs = control->sinfo_flags;
5564 if (fromlen && from) {
5565 struct sockaddr *to;
5568 cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sin.sin_len);
5569 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
5570 ((struct sockaddr_in *)from)->sin_port = control->port_from;
5572 /* No AF_INET use AF_INET6 */
5573 cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sin6.sin6_len);
5574 memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
5575 ((struct sockaddr_in6 *)from)->sin6_port = control->port_from;
5579 #if defined(INET) && defined(INET6)
5580 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
5581 (to->sa_family == AF_INET) &&
5582 ((size_t)fromlen >= sizeof(struct sockaddr_in6))) {
5583 struct sockaddr_in *sin;
5584 struct sockaddr_in6 sin6;
5586 sin = (struct sockaddr_in *)to;
5587 bzero(&sin6, sizeof(sin6));
5588 sin6.sin6_family = AF_INET6;
5589 sin6.sin6_len = sizeof(struct sockaddr_in6);
5590 sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
5591 bcopy(&sin->sin_addr,
5592 &sin6.sin6_addr.s6_addr32[3],
5593 sizeof(sin6.sin6_addr.s6_addr32[3]));
5594 sin6.sin6_port = sin->sin_port;
5595 memcpy(from, (caddr_t)&sin6, sizeof(sin6));
5600 struct sockaddr_in6 lsa6, *to6;
5602 to6 = (struct sockaddr_in6 *)to;
5603 sctp_recover_scope_mac(to6, (&lsa6));
5607 /* now copy out what data we can */
5609 /* copy out each mbuf in the chain up to length */
5613 /* Move out all we can */
5614 cp_len = (int)uio->uio_resid;
5615 my_len = (int)SCTP_BUF_LEN(m);
5616 if (cp_len > my_len) {
5617 /* not enough in this buf */
5621 SCTP_INP_READ_UNLOCK(inp);
5625 error = uiomove(mtod(m, char *), cp_len, uio);
5627 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5630 if ((control->do_not_ref_stcb == 0) && stcb &&
5631 stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5635 /* error we are out of here */
5638 if ((SCTP_BUF_NEXT(m) == NULL) &&
5639 (cp_len >= SCTP_BUF_LEN(m)) &&
5640 ((control->end_added == 0) ||
5641 (control->end_added &&
5642 (TAILQ_NEXT(control, next) == NULL)))
5644 SCTP_INP_READ_LOCK(inp);
5647 if (cp_len == SCTP_BUF_LEN(m)) {
5648 if ((SCTP_BUF_NEXT(m) == NULL) &&
5649 (control->end_added)) {
5650 out_flags |= MSG_EOR;
5651 if ((control->do_not_ref_stcb == 0) &&
5652 (control->stcb != NULL) &&
5653 ((control->spec_flags & M_NOTIFICATION) == 0))
5654 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5656 if (control->spec_flags & M_NOTIFICATION) {
5657 out_flags |= MSG_NOTIFICATION;
5659 /* we ate up the mbuf */
5660 if (in_flags & MSG_PEEK) {
5662 m = SCTP_BUF_NEXT(m);
5663 copied_so_far += cp_len;
5665 /* dispose of the mbuf */
5666 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5667 sctp_sblog(&so->so_rcv,
5668 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5670 sctp_sbfree(control, stcb, &so->so_rcv, m);
5671 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5672 sctp_sblog(&so->so_rcv,
5673 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5675 copied_so_far += cp_len;
5676 freed_so_far += cp_len;
5677 freed_so_far += MSIZE;
5678 atomic_subtract_int(&control->length, cp_len);
5679 control->data = sctp_m_free(m);
5682 * been through it all, must hold sb
5683 * lock ok to null tail
5685 if (control->data == NULL) {
5687 if ((control->end_added == 0) ||
5688 (TAILQ_NEXT(control, next) == NULL)) {
5696 if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5697 panic("Hmm we don't own the lock?");
5701 control->tail_mbuf = NULL;
5703 if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5704 panic("end_added, nothing left and no MSG_EOR");
5710 /* Do we need to trim the mbuf? */
5711 if (control->spec_flags & M_NOTIFICATION) {
5712 out_flags |= MSG_NOTIFICATION;
5714 if ((in_flags & MSG_PEEK) == 0) {
5715 SCTP_BUF_RESV_UF(m, cp_len);
5716 SCTP_BUF_LEN(m) -= cp_len;
5717 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5718 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
5720 atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
5721 if ((control->do_not_ref_stcb == 0) &&
5723 atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5725 copied_so_far += cp_len;
5726 freed_so_far += cp_len;
5727 freed_so_far += MSIZE;
5728 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5729 sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5730 SCTP_LOG_SBRESULT, 0);
5732 atomic_subtract_int(&control->length, cp_len);
5734 copied_so_far += cp_len;
5737 if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5740 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5741 (control->do_not_ref_stcb == 0) &&
5742 (freed_so_far >= rwnd_req)) {
5743 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5745 } /* end while(m) */
5747 * At this point we have looked at it all and we either have
5748 * a MSG_EOR/or read all the user wants... <OR>
5749 * control->length == 0.
5751 if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5752 /* we are done with this control */
5753 if (control->length == 0) {
5754 if (control->data) {
5756 panic("control->data not null at read eor?");
5758 SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5759 sctp_m_freem(control->data);
5760 control->data = NULL;
5764 if (TAILQ_NEXT(control, next) == NULL) {
5766 * If we don't have a next we need a
5767 * lock, if there is a next
5768 * interrupt is filling ahead of us
5769 * and we don't need a lock to
5770 * remove this guy (which is the
5771 * head of the queue).
5773 if (hold_rlock == 0) {
5774 SCTP_INP_READ_LOCK(inp);
5778 TAILQ_REMOVE(&inp->read_queue, control, next);
5779 /* Add back any hiddend data */
5780 if (control->held_length) {
5782 control->held_length = 0;
5783 wakeup_read_socket = 1;
5785 if (control->aux_data) {
5786 sctp_m_free(control->aux_data);
5787 control->aux_data = NULL;
5789 no_rcv_needed = control->do_not_ref_stcb;
5790 sctp_free_remote_addr(control->whoFrom);
5791 control->data = NULL;
5792 sctp_free_a_readq(stcb, control);
5794 if ((freed_so_far >= rwnd_req) &&
5795 (no_rcv_needed == 0))
5796 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5800 * The user did not read all of this
5801 * message, turn off the returned MSG_EOR
5802 * since we are leaving more behind on the
5806 if (control->end_added &&
5807 (control->data == NULL) &&
5808 (control->tail_mbuf == NULL)) {
5809 panic("Gak, control->length is corrupt?");
5812 no_rcv_needed = control->do_not_ref_stcb;
5813 out_flags &= ~MSG_EOR;
5816 if (out_flags & MSG_EOR) {
5819 if ((uio->uio_resid == 0) ||
5820 ((in_eeor_mode) && (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))
5825 * If I hit here the receiver wants more and this message is
5826 * NOT done (pd-api). So two questions. Can we block? if not
5827 * we are done. Did the user NOT set MSG_WAITALL?
5829 if (block_allowed == 0) {
5833 * We need to wait for more data a few things: - We don't
5834 * sbunlock() so we don't get someone else reading. - We
5835 * must be sure to account for the case where what is added
5836 * is NOT to our control when we wakeup.
5840 * Do we need to tell the transport a rwnd update might be
5841 * needed before we go to sleep?
5843 if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5844 ((freed_so_far >= rwnd_req) &&
5845 (control->do_not_ref_stcb == 0) &&
5846 (no_rcv_needed == 0))) {
5847 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5850 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5853 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5856 if (hold_rlock == 1) {
5857 SCTP_INP_READ_UNLOCK(inp);
5860 if (hold_sblock == 0) {
5861 SOCKBUF_LOCK(&so->so_rcv);
5864 if ((copied_so_far) && (control->length == 0) &&
5865 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
5868 if (so->so_rcv.sb_cc <= control->held_length) {
5869 error = sbwait(&so->so_rcv);
5873 control->held_length = 0;
5876 SOCKBUF_UNLOCK(&so->so_rcv);
5879 if (control->length == 0) {
5880 /* still nothing here */
5881 if (control->end_added == 1) {
5882 /* he aborted, or is done i.e.did a shutdown */
5883 out_flags |= MSG_EOR;
5884 if (control->pdapi_aborted) {
5885 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5886 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5888 out_flags |= MSG_TRUNC;
5890 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5891 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5893 goto done_with_control;
5895 if (so->so_rcv.sb_cc > held_length) {
5896 control->held_length = so->so_rcv.sb_cc;
5899 goto wait_some_more;
5900 } else if (control->data == NULL) {
5902 * we must re-sync since data is probably being
5905 SCTP_INP_READ_LOCK(inp);
5906 if ((control->length > 0) && (control->data == NULL)) {
5908 * big trouble.. we have the lock and its
5912 panic("Impossible data==NULL length !=0");
5914 out_flags |= MSG_EOR;
5915 out_flags |= MSG_TRUNC;
5916 control->length = 0;
5917 SCTP_INP_READ_UNLOCK(inp);
5918 goto done_with_control;
5920 SCTP_INP_READ_UNLOCK(inp);
5921 /* We will fall around to get more data */
5926 * Give caller back the mbuf chain,
5927 * store in uio_resid the length
5929 wakeup_read_socket = 0;
5930 if ((control->end_added == 0) ||
5931 (TAILQ_NEXT(control, next) == NULL)) {
5932 /* Need to get rlock */
5933 if (hold_rlock == 0) {
5934 SCTP_INP_READ_LOCK(inp);
5938 if (control->end_added) {
5939 out_flags |= MSG_EOR;
5940 if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5941 control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5943 if (control->spec_flags & M_NOTIFICATION) {
5944 out_flags |= MSG_NOTIFICATION;
5946 uio->uio_resid = control->length;
5947 *mp = control->data;
5950 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5951 sctp_sblog(&so->so_rcv,
5952 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
5954 sctp_sbfree(control, stcb, &so->so_rcv, m);
5955 freed_so_far += SCTP_BUF_LEN(m);
5956 freed_so_far += MSIZE;
5957 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5958 sctp_sblog(&so->so_rcv,
5959 control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
5961 m = SCTP_BUF_NEXT(m);
5963 control->data = control->tail_mbuf = NULL;
5964 control->length = 0;
5965 if (out_flags & MSG_EOR) {
5966 /* Done with this control */
5967 goto done_with_control;
5971 if (hold_rlock == 1) {
5972 SCTP_INP_READ_UNLOCK(inp);
5975 if (hold_sblock == 1) {
5976 SOCKBUF_UNLOCK(&so->so_rcv);
5979 sbunlock(&so->so_rcv);
5984 SOCKBUF_UNLOCK(&so->so_rcv);
5987 if ((stcb) && (in_flags & MSG_PEEK) == 0) {
5988 if ((freed_so_far >= rwnd_req) &&
5989 (control && (control->do_not_ref_stcb == 0)) &&
5990 (no_rcv_needed == 0))
5991 sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5995 *msg_flags = out_flags;
5997 if (((out_flags & MSG_EOR) == 0) &&
5998 ((in_flags & MSG_PEEK) == 0) &&
6000 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO))) {
6001 struct sctp_extrcvinfo *s_extra;
6003 s_extra = (struct sctp_extrcvinfo *)sinfo;
6004 s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
6006 if (hold_rlock == 1) {
6007 SCTP_INP_READ_UNLOCK(inp);
6011 SOCKBUF_UNLOCK(&so->so_rcv);
6015 sbunlock(&so->so_rcv);
6017 if (freecnt_applied) {
6019 * The lock on the socket buffer protects us so the free
6020 * code will stop. But since we used the socketbuf lock and
6021 * the sender uses the tcb_lock to increment, we need to use
6022 * the atomic add to the refcnt.
6026 panic("stcb for refcnt has gone NULL?");
6032 atomic_add_int(&stcb->asoc.refcnt, -1);
6033 freecnt_applied = 0;
6034 /* Save the value back for next time */
6035 stcb->freed_by_sorcv_sincelast = freed_so_far;
6037 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6039 sctp_misc_ints(SCTP_SORECV_DONE,
6041 ((uio) ? (slen - uio->uio_resid) : slen),
6045 sctp_misc_ints(SCTP_SORECV_DONE,
6047 ((uio) ? (slen - uio->uio_resid) : slen),
6053 if (wakeup_read_socket) {
6054 sctp_sorwakeup(inp, so);
6060 #ifdef SCTP_MBUF_LOGGING
6062 sctp_m_free(struct mbuf *m)
6064 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6065 if (SCTP_BUF_IS_EXTENDED(m)) {
6066 sctp_log_mb(m, SCTP_MBUF_IFREE);
6073 sctp_m_freem(struct mbuf *mb)
6076 mb = sctp_m_free(mb);
6082 sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
6085 * Given a local address. For all associations that holds the
6086 * address, request a peer-set-primary.
6088 struct sctp_ifa *ifa;
6089 struct sctp_laddr *wi;
6091 ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
6093 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
6094 return (EADDRNOTAVAIL);
6097 * Now that we have the ifa we must awaken the iterator with this
6100 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6102 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
6105 /* Now incr the count and int wi structure */
6106 SCTP_INCR_LADDR_COUNT();
6107 bzero(wi, sizeof(*wi));
6108 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
6110 wi->action = SCTP_SET_PRIM_ADDR;
6111 atomic_add_int(&ifa->refcount, 1);
6113 /* Now add it to the work queue */
6114 SCTP_WQ_ADDR_LOCK();
6116 * Should this really be a tailq? As it is we will process the
6119 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6120 SCTP_WQ_ADDR_UNLOCK();
6121 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
6122 (struct sctp_inpcb *)NULL,
6123 (struct sctp_tcb *)NULL,
6124 (struct sctp_nets *)NULL);
6130 sctp_soreceive(struct socket *so,
6131 struct sockaddr **psa,
6134 struct mbuf **controlp,
6138 uint8_t sockbuf[256];
6139 struct sockaddr *from;
6140 struct sctp_extrcvinfo sinfo;
6141 int filling_sinfo = 1;
6142 struct sctp_inpcb *inp;
6144 inp = (struct sctp_inpcb *)so->so_pcb;
6145 /* pickup the assoc we are reading from */
6147 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6150 if ((sctp_is_feature_off(inp,
6151 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
6152 (controlp == NULL)) {
6153 /* user does not want the sndrcv ctl */
6157 from = (struct sockaddr *)sockbuf;
6158 fromlen = sizeof(sockbuf);
6165 error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
6166 (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6167 if ((controlp) && (filling_sinfo)) {
6168 /* copy back the sinfo in a CMSG format */
6170 *controlp = sctp_build_ctl_nchunk(inp,
6171 (struct sctp_sndrcvinfo *)&sinfo);
6176 /* copy back the address info */
6177 if (from && from->sa_len) {
6178 *psa = sodupsockaddr(from, M_NOWAIT);
6188 sctp_l_soreceive(struct socket *so,
6189 struct sockaddr **name,
6196 uint8_t sockbuf[256];
6197 struct sockaddr *from;
6198 struct sctp_extrcvinfo sinfo;
6199 int filling_sinfo = 1;
6200 struct sctp_inpcb *inp;
6202 inp = (struct sctp_inpcb *)so->so_pcb;
6203 /* pickup the assoc we are reading from */
6205 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6208 if ((sctp_is_feature_off(inp,
6209 SCTP_PCB_FLAGS_RECVDATAIOEVNT)) ||
6210 (controlp == NULL)) {
6211 /* user does not want the sndrcv ctl */
6215 from = (struct sockaddr *)sockbuf;
6216 fromlen = sizeof(sockbuf);
6223 error = sctp_sorecvmsg(so, uio,
6224 (struct mbuf **)NULL,
6225 from, fromlen, flag,
6226 (struct sctp_sndrcvinfo *)&sinfo,
6228 if ((controlp) && (filling_sinfo)) {
6230 * copy back the sinfo in a CMSG format note that the caller
6231 * has reponsibility for freeing the memory.
6234 *controlp = sctp_build_ctl_cchunk(inp,
6236 (struct sctp_sndrcvinfo *)&sinfo);
6239 /* copy back the address info */
6240 if (from && from->sa_len) {
6241 *name = sodupsockaddr(from, M_WAIT);
6256 sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6257 int totaddr, int *error)
6261 struct sctp_inpcb *inp;
6262 struct sockaddr *sa;
6266 inp = stcb->sctp_ep;
6268 for (i = 0; i < totaddr; i++) {
6269 switch (sa->sa_family) {
6272 incr = sizeof(struct sockaddr_in);
6273 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
6274 /* assoc gone no un-lock */
6275 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6276 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
6285 incr = sizeof(struct sockaddr_in6);
6286 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
6287 /* assoc gone no un-lock */
6288 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6289 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
6299 sa = (struct sockaddr *)((caddr_t)sa + incr);
6306 sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6307 int *totaddr, int *num_v4, int *num_v6, int *error,
6308 int limit, int *bad_addr)
6310 struct sockaddr *sa;
6311 struct sctp_tcb *stcb = NULL;
6317 *error = *num_v6 = *num_v4 = 0;
6318 /* account and validate addresses */
6319 for (i = 0; i < (size_t)*totaddr; i++) {
6320 switch (sa->sa_family) {
6324 incr = sizeof(struct sockaddr_in);
6325 if (sa->sa_len != incr) {
6326 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6336 struct sockaddr_in6 *sin6;
6338 sin6 = (struct sockaddr_in6 *)sa;
6339 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6340 /* Must be non-mapped for connectx */
6341 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6347 incr = sizeof(struct sockaddr_in6);
6348 if (sa->sa_len != incr) {
6349 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6362 if (i == (size_t)*totaddr) {
6365 SCTP_INP_INCR_REF(inp);
6366 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
6368 /* Already have or am bring up an association */
6371 SCTP_INP_DECR_REF(inp);
6373 if ((at + incr) > (size_t)limit) {
6377 sa = (struct sockaddr *)((caddr_t)sa + incr);
6379 return ((struct sctp_tcb *)NULL);
6383 * sctp_bindx(ADD) for one address.
6384 * assumes all arguments are valid/checked by caller.
6387 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
6388 struct sockaddr *sa, sctp_assoc_t assoc_id,
6389 uint32_t vrf_id, int *error, void *p)
6391 struct sockaddr *addr_touse;
6394 struct sockaddr_in sin;
6398 /* see if we're bound all already! */
6399 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6400 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6406 if (sa->sa_family == AF_INET6) {
6407 struct sockaddr_in6 *sin6;
6409 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6410 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6414 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6415 /* can only bind v6 on PF_INET6 sockets */
6416 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6420 sin6 = (struct sockaddr_in6 *)addr_touse;
6421 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6422 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6423 SCTP_IPV6_V6ONLY(inp)) {
6424 /* can't bind v4-mapped on PF_INET sockets */
6425 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6429 in6_sin6_2_sin(&sin, sin6);
6430 addr_touse = (struct sockaddr *)&sin;
6435 if (sa->sa_family == AF_INET) {
6436 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6437 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6441 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6442 SCTP_IPV6_V6ONLY(inp)) {
6443 /* can't bind v4 on PF_INET sockets */
6444 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6450 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
6452 /* Can't get proc for Net/Open BSD */
6453 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6457 *error = sctp_inpcb_bind(so, addr_touse, NULL, p);
6461 * No locks required here since bind and mgmt_ep_sa all do their own
6462 * locking. If we do something for the FIX: below we may need to
6463 * lock in that case.
6465 if (assoc_id == 0) {
6466 /* add the address */
6467 struct sctp_inpcb *lep;
6468 struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
6470 /* validate the incoming port */
6471 if ((lsin->sin_port != 0) &&
6472 (lsin->sin_port != inp->sctp_lport)) {
6473 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6477 /* user specified 0 port, set it to existing port */
6478 lsin->sin_port = inp->sctp_lport;
6481 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
6484 * We must decrement the refcount since we have the
6485 * ep already and are binding. No remove going on
6488 SCTP_INP_DECR_REF(lep);
6491 /* already bound to it.. ok */
6493 } else if (lep == NULL) {
6494 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
6495 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6496 SCTP_ADD_IP_ADDRESS,
6499 *error = EADDRINUSE;
6505 * FIX: decide whether we allow assoc based bindx
6511 * sctp_bindx(DELETE) for one address.
6512 * assumes all arguments are valid/checked by caller.
6515 sctp_bindx_delete_address(struct socket *so, struct sctp_inpcb *inp,
6516 struct sockaddr *sa, sctp_assoc_t assoc_id,
6517 uint32_t vrf_id, int *error)
6519 struct sockaddr *addr_touse;
6522 struct sockaddr_in sin;
6526 /* see if we're bound all already! */
6527 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6528 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6533 #if defined(INET6) && !defined(__Userspace__) /* TODO port in6_sin6_2_sin */
6534 if (sa->sa_family == AF_INET6) {
6535 struct sockaddr_in6 *sin6;
6537 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6538 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6542 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6543 /* can only bind v6 on PF_INET6 sockets */
6544 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6548 sin6 = (struct sockaddr_in6 *)addr_touse;
6549 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6550 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6551 SCTP_IPV6_V6ONLY(inp)) {
6552 /* can't bind mapped-v4 on PF_INET sockets */
6553 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6557 in6_sin6_2_sin(&sin, sin6);
6558 addr_touse = (struct sockaddr *)&sin;
6563 if (sa->sa_family == AF_INET) {
6564 if (sa->sa_len != sizeof(struct sockaddr_in)) {
6565 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6569 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6570 SCTP_IPV6_V6ONLY(inp)) {
6571 /* can't bind v4 on PF_INET sockets */
6572 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6579 * No lock required mgmt_ep_sa does its own locking. If the FIX:
6580 * below is ever changed we may need to lock before calling
6581 * association level binding.
6583 if (assoc_id == 0) {
6584 /* delete the address */
6585 *error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
6586 SCTP_DEL_IP_ADDRESS,
6590 * FIX: decide whether we allow assoc based bindx
6596 * returns the valid local address count for an assoc, taking into account
6600 sctp_local_addr_count(struct sctp_tcb *stcb)
6602 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
6603 int ipv4_addr_legal, ipv6_addr_legal;
6604 struct sctp_vrf *vrf;
6605 struct sctp_ifn *sctp_ifn;
6606 struct sctp_ifa *sctp_ifa;
6609 /* Turn on all the appropriate scopes */
6610 loopback_scope = stcb->asoc.loopback_scope;
6611 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
6612 local_scope = stcb->asoc.local_scope;
6613 site_scope = stcb->asoc.site_scope;
6614 ipv4_addr_legal = ipv6_addr_legal = 0;
6615 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6616 ipv6_addr_legal = 1;
6617 if (SCTP_IPV6_V6ONLY(stcb->sctp_ep) == 0) {
6618 ipv4_addr_legal = 1;
6621 ipv4_addr_legal = 1;
6624 SCTP_IPI_ADDR_RLOCK();
6625 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
6627 /* no vrf, no addresses */
6628 SCTP_IPI_ADDR_RUNLOCK();
6631 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6633 * bound all case: go through all ifns on the vrf
6635 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
6636 if ((loopback_scope == 0) &&
6637 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
6640 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
6641 if (sctp_is_addr_restricted(stcb, sctp_ifa))
6643 switch (sctp_ifa->address.sa.sa_family) {
6646 if (ipv4_addr_legal) {
6647 struct sockaddr_in *sin;
6649 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
6650 if (sin->sin_addr.s_addr == 0) {
6657 if ((ipv4_local_scope == 0) &&
6658 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
6661 /* count this one */
6670 if (ipv6_addr_legal) {
6671 struct sockaddr_in6 *sin6;
6673 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
6674 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6677 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
6678 if (local_scope == 0)
6680 if (sin6->sin6_scope_id == 0) {
6681 if (sa6_recoverscope(sin6) != 0)
6699 if ((site_scope == 0) &&
6700 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
6703 /* count this one */
6718 struct sctp_laddr *laddr;
6720 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
6722 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
6725 /* count this one */
6729 SCTP_IPI_ADDR_RUNLOCK();
6733 #if defined(SCTP_LOCAL_TRACE_BUF)
6736 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)
6738 uint32_t saveindex, newindex;
6741 saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6742 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6745 newindex = saveindex + 1;
6747 } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6748 if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6751 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6752 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6753 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6754 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6755 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6756 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6757 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6758 SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6762 /* XXX: Remove the #ifdef after tunneling over IPv6 works also on FreeBSD. */
6764 /* We will need to add support
6765 * to bind the ports and such here
6766 * so we can do UDP tunneling. In
6767 * the mean-time, we return error
6769 #include <netinet/udp.h>
6770 #include <netinet/udp_var.h>
6771 #include <sys/proc.h>
6773 #include <netinet6/sctp6_var.h>
6777 sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *ignored)
6780 struct mbuf *sp, *last;
6781 struct udphdr *uhdr;
6783 int header_size = sizeof(struct udphdr) + sizeof(struct sctphdr);
6786 * Split out the mbuf chain. Leave the IP header in m, place the
6789 if ((m->m_flags & M_PKTHDR) == 0) {
6790 /* Can't handle one that is not a pkt hdr */
6793 /* pull the src port */
6794 iph = mtod(m, struct ip *);
6795 uhdr = (struct udphdr *)((caddr_t)iph + off);
6797 port = uhdr->uh_sport;
6798 sp = m_split(m, off, M_DONTWAIT);
6800 /* Gak, drop packet, we can't do a split */
6803 if (sp->m_pkthdr.len < header_size) {
6804 /* Gak, packet can't have an SCTP header in it - to small */
6808 /* ok now pull up the UDP header and SCTP header together */
6809 sp = m_pullup(sp, header_size);
6811 /* Gak pullup failed */
6814 /* trim out the UDP header */
6815 m_adj(sp, sizeof(struct udphdr));
6817 /* Now reconstruct the mbuf chain */
6818 /* 1) find last one */
6820 while (last->m_next != NULL) {
6821 last = last->m_next;
6824 m->m_pkthdr.len += sp->m_pkthdr.len;
6826 while (last != NULL) {
6827 last = last->m_next;
6829 /* Now its ready for sctp_input or sctp6_input */
6830 iph = mtod(m, struct ip *);
6831 switch (iph->ip_v) {
6838 len = SCTP_GET_IPV4_LENGTH(iph);
6839 len -= sizeof(struct udphdr);
6840 SCTP_GET_IPV4_LENGTH(iph) = len;
6841 sctp_input_with_port(m, off, port);
6846 case IPV6_VERSION >> 4:
6848 /* its IPv6 - NOT supported */
6866 sctp_over_udp_stop(void)
6871 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6874 if (SCTP_BASE_INFO(udp_tun_socket) == NULL) {
6878 sop = SCTP_BASE_INFO(udp_tun_socket);
6880 SCTP_BASE_INFO(udp_tun_socket) = NULL;
6884 sctp_over_udp_start(void)
6888 struct sockaddr_in sin;
6889 struct socket *sop = NULL;
6894 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6897 port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
6899 /* Must have a port set */
6902 if (SCTP_BASE_INFO(udp_tun_socket) != NULL) {
6903 /* Already running -- must stop first */
6907 cred = th->td_ucred;
6908 if ((ret = socreate(PF_INET, &sop,
6909 SOCK_DGRAM, IPPROTO_UDP, cred, th))) {
6912 SCTP_BASE_INFO(udp_tun_socket) = sop;
6913 /* call the special UDP hook */
6914 ret = udp_set_kernel_tunneling(sop, sctp_recv_udp_tunneled_packet);
6916 goto exit_stage_left;
6918 /* Ok we have a socket, bind it to the port */
6919 memset(&sin, 0, sizeof(sin));
6920 sin.sin_len = sizeof(sin);
6921 sin.sin_family = AF_INET;
6922 sin.sin_port = htons(port);
6923 ret = sobind(sop, (struct sockaddr *)&sin, th);
6925 /* Close up we cant get the port */
6927 sctp_over_udp_stop();
6931 * Ok we should now get UDP packets directly to our input routine
6932 * sctp_recv_upd_tunneled_packet().